Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/stat.h>
19 #include <dirent.h>
20 #include <limits.h>
21 #include <stddef.h>
22 #include <string.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <time.h>
26 #include <fcntl.h>
27 #include <errno.h>
28 #include <unistd.h>
29 #include <zlib.h>
30 #include <fnmatch.h>
31 #include <libgen.h>
33 #include "got_compat.h"
35 #include "got_error.h"
36 #include "got_repository.h"
37 #include "got_reference.h"
38 #include "got_object.h"
39 #include "got_path.h"
40 #include "got_cancel.h"
41 #include "got_worktree.h"
42 #include "got_opentemp.h"
43 #include "got_diff.h"
45 #include "got_lib_worktree.h"
46 #include "got_lib_sha1.h"
47 #include "got_lib_fileindex.h"
48 #include "got_lib_inflate.h"
49 #include "got_lib_delta.h"
50 #include "got_lib_object.h"
51 #include "got_lib_object_parse.h"
52 #include "got_lib_object_create.h"
53 #include "got_lib_object_idset.h"
54 #include "got_lib_diff.h"
55 #include "got_lib_gotconfig.h"
57 #ifndef MIN
58 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
59 #endif
61 #define GOT_MERGE_LABEL_MERGED "merged change"
62 #define GOT_MERGE_LABEL_BASE "3-way merge base"
64 static const struct got_error *
65 create_meta_file(const char *path_got, const char *name, const char *content)
66 {
67 const struct got_error *err = NULL;
68 char *path;
70 if (asprintf(&path, "%s/%s", path_got, name) == -1)
71 return got_error_from_errno("asprintf");
73 err = got_path_create_file(path, content);
74 free(path);
75 return err;
76 }
78 static const struct got_error *
79 update_meta_file(const char *path_got, const char *name, const char *content)
80 {
81 const struct got_error *err = NULL;
82 FILE *tmpfile = NULL;
83 char *tmppath = NULL;
84 char *path = NULL;
86 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
87 err = got_error_from_errno("asprintf");
88 path = NULL;
89 goto done;
90 }
92 err = got_opentemp_named(&tmppath, &tmpfile, path);
93 if (err)
94 goto done;
96 if (content) {
97 int len = fprintf(tmpfile, "%s\n", content);
98 if (len != strlen(content) + 1) {
99 err = got_error_from_errno2("fprintf", tmppath);
100 goto done;
104 if (rename(tmppath, path) != 0) {
105 err = got_error_from_errno3("rename", tmppath, path);
106 unlink(tmppath);
107 goto done;
110 done:
111 if (fclose(tmpfile) == EOF && err == NULL)
112 err = got_error_from_errno2("fclose", tmppath);
113 free(tmppath);
114 return err;
117 static const struct got_error *
118 write_head_ref(const char *path_got, struct got_reference *head_ref)
120 const struct got_error *err = NULL;
121 char *refstr = NULL;
123 if (got_ref_is_symbolic(head_ref)) {
124 refstr = got_ref_to_str(head_ref);
125 if (refstr == NULL)
126 return got_error_from_errno("got_ref_to_str");
127 } else {
128 refstr = strdup(got_ref_get_name(head_ref));
129 if (refstr == NULL)
130 return got_error_from_errno("strdup");
132 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
133 free(refstr);
134 return err;
137 const struct got_error *
138 got_worktree_init(const char *path, struct got_reference *head_ref,
139 const char *prefix, struct got_repository *repo)
141 const struct got_error *err = NULL;
142 struct got_object_id *commit_id = NULL;
143 uuid_t uuid;
144 uint32_t uuid_status;
145 int obj_type;
146 char *path_got = NULL;
147 char *formatstr = NULL;
148 char *absprefix = NULL;
149 char *basestr = NULL;
150 char *uuidstr = NULL;
152 if (strcmp(path, got_repo_get_path(repo)) == 0) {
153 err = got_error(GOT_ERR_WORKTREE_REPO);
154 goto done;
157 err = got_ref_resolve(&commit_id, repo, head_ref);
158 if (err)
159 return err;
160 err = got_object_get_type(&obj_type, repo, commit_id);
161 if (err)
162 return err;
163 if (obj_type != GOT_OBJ_TYPE_COMMIT)
164 return got_error(GOT_ERR_OBJ_TYPE);
166 if (!got_path_is_absolute(prefix)) {
167 if (asprintf(&absprefix, "/%s", prefix) == -1)
168 return got_error_from_errno("asprintf");
171 /* Create top-level directory (may already exist). */
172 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
173 err = got_error_from_errno2("mkdir", path);
174 goto done;
177 /* Create .got directory (may already exist). */
178 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
179 err = got_error_from_errno("asprintf");
180 goto done;
182 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
183 err = got_error_from_errno2("mkdir", path_got);
184 goto done;
187 /* Create an empty lock file. */
188 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
189 if (err)
190 goto done;
192 /* Create an empty file index. */
193 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
194 if (err)
195 goto done;
197 /* Write the HEAD reference. */
198 err = write_head_ref(path_got, head_ref);
199 if (err)
200 goto done;
202 /* Record our base commit. */
203 err = got_object_id_str(&basestr, commit_id);
204 if (err)
205 goto done;
206 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
207 if (err)
208 goto done;
210 /* Store path to repository. */
211 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
212 got_repo_get_path(repo));
213 if (err)
214 goto done;
216 /* Store in-repository path prefix. */
217 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
218 absprefix ? absprefix : prefix);
219 if (err)
220 goto done;
222 /* Generate UUID. */
223 uuid_create(&uuid, &uuid_status);
224 if (uuid_status != uuid_s_ok) {
225 err = got_error_uuid(uuid_status, "uuid_create");
226 goto done;
228 uuid_to_string(&uuid, &uuidstr, &uuid_status);
229 if (uuid_status != uuid_s_ok) {
230 err = got_error_uuid(uuid_status, "uuid_to_string");
231 goto done;
233 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
234 if (err)
235 goto done;
237 /* Stamp work tree with format file. */
238 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
239 err = got_error_from_errno("asprintf");
240 goto done;
242 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
243 if (err)
244 goto done;
246 done:
247 free(commit_id);
248 free(path_got);
249 free(formatstr);
250 free(absprefix);
251 free(basestr);
252 free(uuidstr);
253 return err;
256 const struct got_error *
257 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
258 const char *path_prefix)
260 char *absprefix = NULL;
262 if (!got_path_is_absolute(path_prefix)) {
263 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
264 return got_error_from_errno("asprintf");
266 *match = (strcmp(absprefix ? absprefix : path_prefix,
267 worktree->path_prefix) == 0);
268 free(absprefix);
269 return NULL;
272 const char *
273 got_worktree_get_head_ref_name(struct got_worktree *worktree)
275 return worktree->head_ref_name;
278 const struct got_error *
279 got_worktree_set_head_ref(struct got_worktree *worktree,
280 struct got_reference *head_ref)
282 const struct got_error *err = NULL;
283 char *path_got = NULL, *head_ref_name = NULL;
285 if (asprintf(&path_got, "%s/%s", worktree->root_path,
286 GOT_WORKTREE_GOT_DIR) == -1) {
287 err = got_error_from_errno("asprintf");
288 path_got = NULL;
289 goto done;
292 head_ref_name = strdup(got_ref_get_name(head_ref));
293 if (head_ref_name == NULL) {
294 err = got_error_from_errno("strdup");
295 goto done;
298 err = write_head_ref(path_got, head_ref);
299 if (err)
300 goto done;
302 free(worktree->head_ref_name);
303 worktree->head_ref_name = head_ref_name;
304 done:
305 free(path_got);
306 if (err)
307 free(head_ref_name);
308 return err;
311 struct got_object_id *
312 got_worktree_get_base_commit_id(struct got_worktree *worktree)
314 return worktree->base_commit_id;
317 const struct got_error *
318 got_worktree_set_base_commit_id(struct got_worktree *worktree,
319 struct got_repository *repo, struct got_object_id *commit_id)
321 const struct got_error *err;
322 struct got_object *obj = NULL;
323 char *id_str = NULL;
324 char *path_got = NULL;
326 if (asprintf(&path_got, "%s/%s", worktree->root_path,
327 GOT_WORKTREE_GOT_DIR) == -1) {
328 err = got_error_from_errno("asprintf");
329 path_got = NULL;
330 goto done;
333 err = got_object_open(&obj, repo, commit_id);
334 if (err)
335 return err;
337 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
338 err = got_error(GOT_ERR_OBJ_TYPE);
339 goto done;
342 /* Record our base commit. */
343 err = got_object_id_str(&id_str, commit_id);
344 if (err)
345 goto done;
346 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
347 if (err)
348 goto done;
350 free(worktree->base_commit_id);
351 worktree->base_commit_id = got_object_id_dup(commit_id);
352 if (worktree->base_commit_id == NULL) {
353 err = got_error_from_errno("got_object_id_dup");
354 goto done;
356 done:
357 if (obj)
358 got_object_close(obj);
359 free(id_str);
360 free(path_got);
361 return err;
364 const struct got_gotconfig *
365 got_worktree_get_gotconfig(struct got_worktree *worktree)
367 return worktree->gotconfig;
370 static const struct got_error *
371 lock_worktree(struct got_worktree *worktree, int operation)
373 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
374 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
375 : got_error_from_errno2("flock",
376 got_worktree_get_root_path(worktree)));
377 return NULL;
380 static const struct got_error *
381 add_dir_on_disk(struct got_worktree *worktree, const char *path)
383 const struct got_error *err = NULL;
384 char *abspath;
386 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
387 return got_error_from_errno("asprintf");
389 err = got_path_mkdir(abspath);
390 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
391 struct stat sb;
392 err = NULL;
393 if (lstat(abspath, &sb) == -1) {
394 err = got_error_from_errno2("lstat", abspath);
395 } else if (!S_ISDIR(sb.st_mode)) {
396 /* TODO directory is obstructed; do something */
397 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
400 free(abspath);
401 return err;
404 static const struct got_error *
405 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
407 const struct got_error *err = NULL;
408 uint8_t fbuf1[8192];
409 uint8_t fbuf2[8192];
410 size_t flen1 = 0, flen2 = 0;
412 *same = 1;
414 for (;;) {
415 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
416 if (flen1 == 0 && ferror(f1)) {
417 err = got_error_from_errno("fread");
418 break;
420 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
421 if (flen2 == 0 && ferror(f2)) {
422 err = got_error_from_errno("fread");
423 break;
425 if (flen1 == 0) {
426 if (flen2 != 0)
427 *same = 0;
428 break;
429 } else if (flen2 == 0) {
430 if (flen1 != 0)
431 *same = 0;
432 break;
433 } else if (flen1 == flen2) {
434 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
435 *same = 0;
436 break;
438 } else {
439 *same = 0;
440 break;
444 return err;
447 static const struct got_error *
448 check_files_equal(int *same, FILE *f1, FILE *f2)
450 struct stat sb;
451 size_t size1, size2;
453 *same = 1;
455 if (fstat(fileno(f1), &sb) != 0)
456 return got_error_from_errno("fstat");
457 size1 = sb.st_size;
459 if (fstat(fileno(f2), &sb) != 0)
460 return got_error_from_errno("fstat");
461 size2 = sb.st_size;
463 if (size1 != size2) {
464 *same = 0;
465 return NULL;
468 if (fseek(f1, 0L, SEEK_SET) == -1)
469 return got_ferror(f1, GOT_ERR_IO);
470 if (fseek(f2, 0L, SEEK_SET) == -1)
471 return got_ferror(f2, GOT_ERR_IO);
473 return check_file_contents_equal(same, f1, f2);
476 static const struct got_error *
477 copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
479 uint8_t fbuf[65536];
480 size_t flen;
481 ssize_t outlen;
483 *outsize = 0;
485 if (fseek(f, 0L, SEEK_SET) == -1)
486 return got_ferror(f, GOT_ERR_IO);
488 for (;;) {
489 flen = fread(fbuf, 1, sizeof(fbuf), f);
490 if (flen == 0) {
491 if (ferror(f))
492 return got_error_from_errno("fread");
493 if (feof(f))
494 break;
496 outlen = write(outfd, fbuf, flen);
497 if (outlen == -1)
498 return got_error_from_errno("write");
499 if (outlen != flen)
500 return got_error(GOT_ERR_IO);
501 *outsize += outlen;
504 return NULL;
507 static const struct got_error *
508 merge_binary_file(int *overlapcnt, int merged_fd,
509 FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
510 const char *label_deriv, const char *label_orig, const char *label_deriv2,
511 const char *ondisk_path)
513 const struct got_error *err = NULL;
514 int same_content, changed_deriv, changed_deriv2;
515 int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
516 off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
517 char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
518 char *base_path_orig = NULL, *base_path_deriv = NULL;
519 char *base_path_deriv2 = NULL;
521 *overlapcnt = 0;
523 err = check_files_equal(&same_content, f_deriv, f_deriv2);
524 if (err)
525 return err;
527 if (same_content)
528 return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
530 err = check_files_equal(&same_content, f_deriv, f_orig);
531 if (err)
532 return err;
533 changed_deriv = !same_content;
534 err = check_files_equal(&same_content, f_deriv2, f_orig);
535 if (err)
536 return err;
537 changed_deriv2 = !same_content;
539 if (changed_deriv && changed_deriv2) {
540 *overlapcnt = 1;
541 if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
542 err = got_error_from_errno("asprintf");
543 goto done;
545 if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
546 err = got_error_from_errno("asprintf");
547 goto done;
549 if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
550 err = got_error_from_errno("asprintf");
551 goto done;
553 err = got_opentemp_named_fd(&path_orig, &fd_orig,
554 base_path_orig);
555 if (err)
556 goto done;
557 err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
558 base_path_deriv);
559 if (err)
560 goto done;
561 err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
562 base_path_deriv2);
563 if (err)
564 goto done;
565 err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
566 if (err)
567 goto done;
568 err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
569 if (err)
570 goto done;
571 err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
572 if (err)
573 goto done;
574 if (dprintf(merged_fd, "Binary files differ and cannot be "
575 "merged automatically:\n") < 0) {
576 err = got_error_from_errno("dprintf");
577 goto done;
579 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
580 GOT_DIFF_CONFLICT_MARKER_BEGIN,
581 label_deriv ? " " : "",
582 label_deriv ? label_deriv : "",
583 path_deriv) < 0) {
584 err = got_error_from_errno("dprintf");
585 goto done;
587 if (size_orig > 0) {
588 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
589 GOT_DIFF_CONFLICT_MARKER_ORIG,
590 label_orig ? " " : "",
591 label_orig ? label_orig : "",
592 path_orig) < 0) {
593 err = got_error_from_errno("dprintf");
594 goto done;
597 if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
598 GOT_DIFF_CONFLICT_MARKER_SEP,
599 path_deriv2,
600 GOT_DIFF_CONFLICT_MARKER_END,
601 label_deriv2 ? " " : "",
602 label_deriv2 ? label_deriv2 : "") < 0) {
603 err = got_error_from_errno("dprintf");
604 goto done;
606 } else if (changed_deriv)
607 err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
608 else if (changed_deriv2)
609 err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
610 done:
611 if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
612 err == NULL)
613 err = got_error_from_errno2("unlink", path_orig);
614 if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
615 err = got_error_from_errno2("close", path_orig);
616 if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
617 err = got_error_from_errno2("close", path_deriv);
618 if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
619 err = got_error_from_errno2("close", path_deriv2);
620 free(path_orig);
621 free(path_deriv);
622 free(path_deriv2);
623 free(base_path_orig);
624 free(base_path_deriv);
625 free(base_path_deriv2);
626 return err;
629 /*
630 * Perform a 3-way merge where the file f_orig acts as the common
631 * ancestor, the file f_deriv acts as the first derived version,
632 * and the file f_deriv2 acts as the second derived version.
633 * The merge result will be written to a new file at ondisk_path; any
634 * existing file at this path will be replaced.
635 */
636 static const struct got_error *
637 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
638 FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
639 const char *path, uint16_t st_mode,
640 const char *label_orig, const char *label_deriv, const char *label_deriv2,
641 enum got_diff_algorithm diff_algo, struct got_repository *repo,
642 got_worktree_checkout_cb progress_cb, void *progress_arg)
644 const struct got_error *err = NULL;
645 int merged_fd = -1;
646 FILE *f_merged = NULL;
647 char *merged_path = NULL, *base_path = NULL;
648 int overlapcnt = 0;
649 char *parent = NULL;
651 *local_changes_subsumed = 0;
653 err = got_path_dirname(&parent, ondisk_path);
654 if (err)
655 return err;
657 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
658 err = got_error_from_errno("asprintf");
659 goto done;
662 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
663 if (err)
664 goto done;
666 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
667 f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
668 if (err) {
669 if (err->code != GOT_ERR_FILE_BINARY)
670 goto done;
671 err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
672 f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
673 ondisk_path);
674 if (err)
675 goto done;
678 err = (*progress_cb)(progress_arg,
679 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
680 if (err)
681 goto done;
683 if (fsync(merged_fd) != 0) {
684 err = got_error_from_errno("fsync");
685 goto done;
688 f_merged = fdopen(merged_fd, "r");
689 if (f_merged == NULL) {
690 err = got_error_from_errno("fdopen");
691 goto done;
693 merged_fd = -1;
695 /* Check if a clean merge has subsumed all local changes. */
696 if (overlapcnt == 0) {
697 err = check_files_equal(local_changes_subsumed, f_deriv,
698 f_merged);
699 if (err)
700 goto done;
703 if (fchmod(fileno(f_merged), st_mode) != 0) {
704 err = got_error_from_errno2("fchmod", merged_path);
705 goto done;
708 if (rename(merged_path, ondisk_path) != 0) {
709 err = got_error_from_errno3("rename", merged_path,
710 ondisk_path);
711 goto done;
713 done:
714 if (err) {
715 if (merged_path)
716 unlink(merged_path);
718 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
719 err = got_error_from_errno("close");
720 if (f_merged && fclose(f_merged) == EOF && err == NULL)
721 err = got_error_from_errno("fclose");
722 free(merged_path);
723 free(base_path);
724 free(parent);
725 return err;
728 static const struct got_error *
729 update_symlink(const char *ondisk_path, const char *target_path,
730 size_t target_len)
732 /* This is not atomic but matches what 'ln -sf' does. */
733 if (unlink(ondisk_path) == -1)
734 return got_error_from_errno2("unlink", ondisk_path);
735 if (symlink(target_path, ondisk_path) == -1)
736 return got_error_from_errno3("symlink", target_path,
737 ondisk_path);
738 return NULL;
741 /*
742 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
743 * in the work tree with a file that contains conflict markers and the
744 * conflicting target paths of the original version, a "derived version"
745 * of a symlink from an incoming change, and a local version of the symlink.
747 * The original versions's target path can be NULL if it is not available,
748 * such as if both derived versions added a new symlink at the same path.
750 * The incoming derived symlink target is NULL in case the incoming change
751 * has deleted this symlink.
752 */
753 static const struct got_error *
754 install_symlink_conflict(const char *deriv_target,
755 struct got_object_id *deriv_base_commit_id, const char *orig_target,
756 const char *label_orig, const char *local_target, const char *ondisk_path)
758 const struct got_error *err;
759 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
760 FILE *f = NULL;
762 err = got_object_id_str(&id_str, deriv_base_commit_id);
763 if (err)
764 return got_error_from_errno("asprintf");
766 if (asprintf(&label_deriv, "%s: commit %s",
767 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
768 err = got_error_from_errno("asprintf");
769 goto done;
772 err = got_opentemp_named(&path, &f, "got-symlink-conflict");
773 if (err)
774 goto done;
776 if (fchmod(fileno(f), GOT_DEFAULT_FILE_MODE) == -1) {
777 err = got_error_from_errno2("fchmod", path);
778 goto done;
781 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
782 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
783 deriv_target ? deriv_target : "(symlink was deleted)",
784 orig_target ? label_orig : "",
785 orig_target ? "\n" : "",
786 orig_target ? orig_target : "",
787 orig_target ? "\n" : "",
788 GOT_DIFF_CONFLICT_MARKER_SEP,
789 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
790 err = got_error_from_errno2("fprintf", path);
791 goto done;
794 if (unlink(ondisk_path) == -1) {
795 err = got_error_from_errno2("unlink", ondisk_path);
796 goto done;
798 if (rename(path, ondisk_path) == -1) {
799 err = got_error_from_errno3("rename", path, ondisk_path);
800 goto done;
802 done:
803 if (f != NULL && fclose(f) == EOF && err == NULL)
804 err = got_error_from_errno2("fclose", path);
805 free(path);
806 free(id_str);
807 free(label_deriv);
808 return err;
811 /* forward declaration */
812 static const struct got_error *
813 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
814 const char *, const char *, uint16_t, const char *,
815 struct got_blob_object *, struct got_object_id *,
816 struct got_repository *, got_worktree_checkout_cb, void *);
818 /*
819 * Merge a symlink into the work tree, where blob_orig acts as the common
820 * ancestor, deriv_target is the link target of the first derived version,
821 * and the symlink on disk acts as the second derived version.
822 * Assume that contents of both blobs represent symlinks.
823 */
824 static const struct got_error *
825 merge_symlink(struct got_worktree *worktree,
826 struct got_blob_object *blob_orig, const char *ondisk_path,
827 const char *path, const char *label_orig, const char *deriv_target,
828 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
829 got_worktree_checkout_cb progress_cb, void *progress_arg)
831 const struct got_error *err = NULL;
832 char *ancestor_target = NULL;
833 struct stat sb;
834 ssize_t ondisk_len, deriv_len;
835 char ondisk_target[PATH_MAX];
836 int have_local_change = 0;
837 int have_incoming_change = 0;
839 if (lstat(ondisk_path, &sb) == -1)
840 return got_error_from_errno2("lstat", ondisk_path);
842 ondisk_len = readlink(ondisk_path, ondisk_target,
843 sizeof(ondisk_target));
844 if (ondisk_len == -1) {
845 err = got_error_from_errno2("readlink",
846 ondisk_path);
847 goto done;
849 ondisk_target[ondisk_len] = '\0';
851 if (blob_orig) {
852 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
853 if (err)
854 goto done;
857 if (ancestor_target == NULL ||
858 (ondisk_len != strlen(ancestor_target) ||
859 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
860 have_local_change = 1;
862 deriv_len = strlen(deriv_target);
863 if (ancestor_target == NULL ||
864 (deriv_len != strlen(ancestor_target) ||
865 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
866 have_incoming_change = 1;
868 if (!have_local_change && !have_incoming_change) {
869 if (ancestor_target) {
870 /* Both sides made the same change. */
871 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
872 path);
873 } else if (deriv_len == ondisk_len &&
874 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
875 /* Both sides added the same symlink. */
876 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
877 path);
878 } else {
879 /* Both sides added symlinks which don't match. */
880 err = install_symlink_conflict(deriv_target,
881 deriv_base_commit_id, ancestor_target,
882 label_orig, ondisk_target, ondisk_path);
883 if (err)
884 goto done;
885 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
886 path);
888 } else if (!have_local_change && have_incoming_change) {
889 /* Apply the incoming change. */
890 err = update_symlink(ondisk_path, deriv_target,
891 strlen(deriv_target));
892 if (err)
893 goto done;
894 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
895 } else if (have_local_change && have_incoming_change) {
896 if (deriv_len == ondisk_len &&
897 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
898 /* Both sides made the same change. */
899 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
900 path);
901 } else {
902 err = install_symlink_conflict(deriv_target,
903 deriv_base_commit_id, ancestor_target, label_orig,
904 ondisk_target, ondisk_path);
905 if (err)
906 goto done;
907 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
908 path);
912 done:
913 free(ancestor_target);
914 return err;
917 static const struct got_error *
918 dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
920 const struct got_error *err = NULL;
921 char target_path[PATH_MAX];
922 ssize_t target_len;
923 size_t n;
924 FILE *f;
926 *outfile = NULL;
928 f = got_opentemp();
929 if (f == NULL)
930 return got_error_from_errno("got_opentemp");
931 target_len = readlink(ondisk_path, target_path, sizeof(target_path));
932 if (target_len == -1) {
933 err = got_error_from_errno2("readlink", ondisk_path);
934 goto done;
936 n = fwrite(target_path, 1, target_len, f);
937 if (n != target_len) {
938 err = got_ferror(f, GOT_ERR_IO);
939 goto done;
941 if (fflush(f) == EOF) {
942 err = got_error_from_errno("fflush");
943 goto done;
945 if (fseek(f, 0L, SEEK_SET) == -1) {
946 err = got_ferror(f, GOT_ERR_IO);
947 goto done;
949 done:
950 if (err)
951 fclose(f);
952 else
953 *outfile = f;
954 return err;
957 /*
958 * Perform a 3-way merge where blob_orig acts as the common ancestor,
959 * blob_deriv acts as the first derived version, and the file on disk
960 * acts as the second derived version.
961 */
962 static const struct got_error *
963 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
964 struct got_blob_object *blob_orig, const char *ondisk_path,
965 const char *path, uint16_t st_mode, const char *label_orig,
966 struct got_blob_object *blob_deriv,
967 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
968 got_worktree_checkout_cb progress_cb, void *progress_arg)
970 const struct got_error *err = NULL;
971 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
972 char *blob_orig_path = NULL;
973 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
974 char *label_deriv = NULL, *parent = NULL;
976 *local_changes_subsumed = 0;
978 err = got_path_dirname(&parent, ondisk_path);
979 if (err)
980 return err;
982 if (blob_orig) {
983 if (asprintf(&base_path, "%s/got-merge-blob-orig",
984 parent) == -1) {
985 err = got_error_from_errno("asprintf");
986 base_path = NULL;
987 goto done;
990 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
991 if (err)
992 goto done;
993 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
994 blob_orig);
995 if (err)
996 goto done;
997 free(base_path);
998 } else {
999 /*
1000 * No common ancestor exists. This is an "add vs add" conflict
1001 * and we simply use an empty ancestor file to make both files
1002 * appear in the merged result in their entirety.
1004 f_orig = got_opentemp();
1005 if (f_orig == NULL) {
1006 err = got_error_from_errno("got_opentemp");
1007 goto done;
1011 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1012 err = got_error_from_errno("asprintf");
1013 base_path = NULL;
1014 goto done;
1017 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1018 if (err)
1019 goto done;
1020 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1021 blob_deriv);
1022 if (err)
1023 goto done;
1025 err = got_object_id_str(&id_str, deriv_base_commit_id);
1026 if (err)
1027 goto done;
1028 if (asprintf(&label_deriv, "%s: commit %s",
1029 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1030 err = got_error_from_errno("asprintf");
1031 goto done;
1035 * In order the run a 3-way merge with a symlink we copy the symlink's
1036 * target path into a temporary file and use that file with diff3.
1038 if (S_ISLNK(st_mode)) {
1039 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1040 if (err)
1041 goto done;
1042 } else {
1043 int fd;
1044 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1045 if (fd == -1) {
1046 err = got_error_from_errno2("open", ondisk_path);
1047 goto done;
1049 f_deriv2 = fdopen(fd, "r");
1050 if (f_deriv2 == NULL) {
1051 err = got_error_from_errno2("fdopen", ondisk_path);
1052 close(fd);
1053 goto done;
1057 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1058 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1059 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1060 done:
1061 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1062 err = got_error_from_errno("fclose");
1063 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1064 err = got_error_from_errno("fclose");
1065 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1066 err = got_error_from_errno("fclose");
1067 free(base_path);
1068 if (blob_orig_path) {
1069 unlink(blob_orig_path);
1070 free(blob_orig_path);
1072 if (blob_deriv_path) {
1073 unlink(blob_deriv_path);
1074 free(blob_deriv_path);
1076 free(id_str);
1077 free(label_deriv);
1078 free(parent);
1079 return err;
1082 static const struct got_error *
1083 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1084 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1085 int wt_fd, const char *path, struct got_object_id *blob_id)
1087 const struct got_error *err = NULL;
1088 struct got_fileindex_entry *new_ie;
1090 *new_iep = NULL;
1092 err = got_fileindex_entry_alloc(&new_ie, path);
1093 if (err)
1094 return err;
1096 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1097 blob_id->sha1, base_commit_id->sha1, 1);
1098 if (err)
1099 goto done;
1101 err = got_fileindex_entry_add(fileindex, new_ie);
1102 done:
1103 if (err)
1104 got_fileindex_entry_free(new_ie);
1105 else
1106 *new_iep = new_ie;
1107 return err;
1110 static mode_t
1111 get_ondisk_perms(int executable, mode_t st_mode)
1113 mode_t xbits = S_IXUSR;
1115 if (executable) {
1116 /* Map read bits to execute bits. */
1117 if (st_mode & S_IRGRP)
1118 xbits |= S_IXGRP;
1119 if (st_mode & S_IROTH)
1120 xbits |= S_IXOTH;
1121 return st_mode | xbits;
1124 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1127 /* forward declaration */
1128 static const struct got_error *
1129 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1130 const char *path, mode_t te_mode, mode_t st_mode,
1131 struct got_blob_object *blob, int restoring_missing_file,
1132 int reverting_versioned_file, int installing_bad_symlink,
1133 int path_is_unversioned, struct got_repository *repo,
1134 got_worktree_checkout_cb progress_cb, void *progress_arg);
1137 * This function assumes that the provided symlink target points at a
1138 * safe location in the work tree!
1140 static const struct got_error *
1141 replace_existing_symlink(int *did_something, const char *ondisk_path,
1142 const char *target_path, size_t target_len)
1144 const struct got_error *err = NULL;
1145 ssize_t elen;
1146 char etarget[PATH_MAX];
1147 int fd;
1149 *did_something = 0;
1152 * "Bad" symlinks (those pointing outside the work tree or into the
1153 * .got directory) are installed in the work tree as a regular file
1154 * which contains the bad symlink target path.
1155 * The new symlink target has already been checked for safety by our
1156 * caller. If we can successfully open a regular file then we simply
1157 * replace this file with a symlink below.
1159 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1160 if (fd == -1) {
1161 if (!got_err_open_nofollow_on_symlink())
1162 return got_error_from_errno2("open", ondisk_path);
1164 /* We are updating an existing on-disk symlink. */
1165 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1166 if (elen == -1)
1167 return got_error_from_errno2("readlink", ondisk_path);
1169 if (elen == target_len &&
1170 memcmp(etarget, target_path, target_len) == 0)
1171 return NULL; /* nothing to do */
1174 *did_something = 1;
1175 err = update_symlink(ondisk_path, target_path, target_len);
1176 if (fd != -1 && close(fd) == -1 && err == NULL)
1177 err = got_error_from_errno2("close", ondisk_path);
1178 return err;
1181 static const struct got_error *
1182 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1183 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1185 const struct got_error *err = NULL;
1186 char canonpath[PATH_MAX];
1187 char *path_got = NULL;
1189 *is_bad_symlink = 0;
1191 if (target_len >= sizeof(canonpath)) {
1192 *is_bad_symlink = 1;
1193 return NULL;
1197 * We do not use realpath(3) to resolve the symlink's target
1198 * path because we don't want to resolve symlinks recursively.
1199 * Instead we make the path absolute and then canonicalize it.
1200 * Relative symlink target lookup should begin at the directory
1201 * in which the blob object is being installed.
1203 if (!got_path_is_absolute(target_path)) {
1204 char *abspath, *parent;
1205 err = got_path_dirname(&parent, ondisk_path);
1206 if (err)
1207 return err;
1208 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1209 free(parent);
1210 return got_error_from_errno("asprintf");
1212 free(parent);
1213 if (strlen(abspath) >= sizeof(canonpath)) {
1214 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1215 free(abspath);
1216 return err;
1218 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1219 free(abspath);
1220 if (err)
1221 return err;
1222 } else {
1223 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1224 if (err)
1225 return err;
1228 /* Only allow symlinks pointing at paths within the work tree. */
1229 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1230 *is_bad_symlink = 1;
1231 return NULL;
1234 /* Do not allow symlinks pointing into the .got directory. */
1235 if (asprintf(&path_got, "%s/%s", wtroot_path,
1236 GOT_WORKTREE_GOT_DIR) == -1)
1237 return got_error_from_errno("asprintf");
1238 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1239 *is_bad_symlink = 1;
1241 free(path_got);
1242 return NULL;
1245 static const struct got_error *
1246 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1247 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1248 int restoring_missing_file, int reverting_versioned_file,
1249 int path_is_unversioned, int allow_bad_symlinks,
1250 struct got_repository *repo,
1251 got_worktree_checkout_cb progress_cb, void *progress_arg)
1253 const struct got_error *err = NULL;
1254 char target_path[PATH_MAX];
1255 size_t len, target_len = 0;
1256 char *path_got = NULL;
1257 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1258 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1260 *is_bad_symlink = 0;
1263 * Blob object content specifies the target path of the link.
1264 * If a symbolic link cannot be installed we instead create
1265 * a regular file which contains the link target path stored
1266 * in the blob object.
1268 do {
1269 err = got_object_blob_read_block(&len, blob);
1270 if (len + target_len >= sizeof(target_path)) {
1271 /* Path too long; install as a regular file. */
1272 *is_bad_symlink = 1;
1273 got_object_blob_rewind(blob);
1274 return install_blob(worktree, ondisk_path, path,
1275 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1276 restoring_missing_file, reverting_versioned_file,
1277 1, path_is_unversioned, repo, progress_cb,
1278 progress_arg);
1280 if (len > 0) {
1281 /* Skip blob object header first time around. */
1282 memcpy(target_path + target_len, buf + hdrlen,
1283 len - hdrlen);
1284 target_len += len - hdrlen;
1285 hdrlen = 0;
1287 } while (len != 0);
1288 target_path[target_len] = '\0';
1290 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1291 ondisk_path, worktree->root_path);
1292 if (err)
1293 return err;
1295 if (*is_bad_symlink && !allow_bad_symlinks) {
1296 /* install as a regular file */
1297 got_object_blob_rewind(blob);
1298 err = install_blob(worktree, ondisk_path, path,
1299 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1300 restoring_missing_file, reverting_versioned_file, 1,
1301 path_is_unversioned, repo, progress_cb, progress_arg);
1302 goto done;
1305 if (symlink(target_path, ondisk_path) == -1) {
1306 if (errno == EEXIST) {
1307 int symlink_replaced;
1308 if (path_is_unversioned) {
1309 err = (*progress_cb)(progress_arg,
1310 GOT_STATUS_UNVERSIONED, path);
1311 goto done;
1313 err = replace_existing_symlink(&symlink_replaced,
1314 ondisk_path, target_path, target_len);
1315 if (err)
1316 goto done;
1317 if (progress_cb) {
1318 if (symlink_replaced) {
1319 err = (*progress_cb)(progress_arg,
1320 reverting_versioned_file ?
1321 GOT_STATUS_REVERT :
1322 GOT_STATUS_UPDATE, path);
1323 } else {
1324 err = (*progress_cb)(progress_arg,
1325 GOT_STATUS_EXISTS, path);
1328 goto done; /* Nothing else to do. */
1331 if (errno == ENOENT) {
1332 char *parent;
1333 err = got_path_dirname(&parent, ondisk_path);
1334 if (err)
1335 goto done;
1336 err = add_dir_on_disk(worktree, parent);
1337 free(parent);
1338 if (err)
1339 goto done;
1341 * Retry, and fall through to error handling
1342 * below if this second attempt fails.
1344 if (symlink(target_path, ondisk_path) != -1) {
1345 err = NULL; /* success */
1346 goto done;
1350 /* Handle errors from first or second creation attempt. */
1351 if (errno == ENAMETOOLONG) {
1352 /* bad target path; install as a regular file */
1353 *is_bad_symlink = 1;
1354 got_object_blob_rewind(blob);
1355 err = install_blob(worktree, ondisk_path, path,
1356 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1357 restoring_missing_file, reverting_versioned_file, 1,
1358 path_is_unversioned, repo,
1359 progress_cb, progress_arg);
1360 } else if (errno == ENOTDIR) {
1361 err = got_error_path(ondisk_path,
1362 GOT_ERR_FILE_OBSTRUCTED);
1363 } else {
1364 err = got_error_from_errno3("symlink",
1365 target_path, ondisk_path);
1367 } else if (progress_cb)
1368 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1369 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1370 done:
1371 free(path_got);
1372 return err;
1375 static const struct got_error *
1376 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1377 const char *path, mode_t te_mode, mode_t st_mode,
1378 struct got_blob_object *blob, int restoring_missing_file,
1379 int reverting_versioned_file, int installing_bad_symlink,
1380 int path_is_unversioned, struct got_repository *repo,
1381 got_worktree_checkout_cb progress_cb, void *progress_arg)
1383 const struct got_error *err = NULL;
1384 int fd = -1;
1385 size_t len, hdrlen;
1386 int update = 0;
1387 char *tmppath = NULL;
1389 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1390 O_CLOEXEC, GOT_DEFAULT_FILE_MODE);
1391 if (fd == -1) {
1392 if (errno == ENOENT) {
1393 char *parent;
1394 err = got_path_dirname(&parent, path);
1395 if (err)
1396 return err;
1397 err = add_dir_on_disk(worktree, parent);
1398 free(parent);
1399 if (err)
1400 return err;
1401 fd = open(ondisk_path,
1402 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1403 GOT_DEFAULT_FILE_MODE);
1404 if (fd == -1)
1405 return got_error_from_errno2("open",
1406 ondisk_path);
1407 } else if (errno == EEXIST) {
1408 if (path_is_unversioned) {
1409 err = (*progress_cb)(progress_arg,
1410 GOT_STATUS_UNVERSIONED, path);
1411 goto done;
1413 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1414 !S_ISREG(st_mode) && !installing_bad_symlink) {
1415 /* TODO file is obstructed; do something */
1416 err = got_error_path(ondisk_path,
1417 GOT_ERR_FILE_OBSTRUCTED);
1418 goto done;
1419 } else {
1420 err = got_opentemp_named_fd(&tmppath, &fd,
1421 ondisk_path);
1422 if (err)
1423 goto done;
1424 update = 1;
1426 } else
1427 return got_error_from_errno2("open", ondisk_path);
1430 if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1431 err = got_error_from_errno2("fchmod",
1432 update ? tmppath : ondisk_path);
1433 goto done;
1436 if (progress_cb) {
1437 if (restoring_missing_file)
1438 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1439 path);
1440 else if (reverting_versioned_file)
1441 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1442 path);
1443 else
1444 err = (*progress_cb)(progress_arg,
1445 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1446 if (err)
1447 goto done;
1450 hdrlen = got_object_blob_get_hdrlen(blob);
1451 do {
1452 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1453 err = got_object_blob_read_block(&len, blob);
1454 if (err)
1455 break;
1456 if (len > 0) {
1457 /* Skip blob object header first time around. */
1458 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1459 if (outlen == -1) {
1460 err = got_error_from_errno("write");
1461 goto done;
1462 } else if (outlen != len - hdrlen) {
1463 err = got_error(GOT_ERR_IO);
1464 goto done;
1466 hdrlen = 0;
1468 } while (len != 0);
1470 if (fsync(fd) != 0) {
1471 err = got_error_from_errno("fsync");
1472 goto done;
1475 if (update) {
1476 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1477 err = got_error_from_errno2("unlink", ondisk_path);
1478 goto done;
1480 if (rename(tmppath, ondisk_path) != 0) {
1481 err = got_error_from_errno3("rename", tmppath,
1482 ondisk_path);
1483 goto done;
1485 free(tmppath);
1486 tmppath = NULL;
1489 done:
1490 if (fd != -1 && close(fd) == -1 && err == NULL)
1491 err = got_error_from_errno("close");
1492 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1493 err = got_error_from_errno2("unlink", tmppath);
1494 free(tmppath);
1495 return err;
1498 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1499 static const struct got_error *
1500 get_modified_file_content_status(unsigned char *status, FILE *f)
1502 const struct got_error *err = NULL;
1503 const char *markers[3] = {
1504 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1505 GOT_DIFF_CONFLICT_MARKER_SEP,
1506 GOT_DIFF_CONFLICT_MARKER_END
1508 int i = 0;
1509 char *line = NULL;
1510 size_t linesize = 0;
1511 ssize_t linelen;
1513 while (*status == GOT_STATUS_MODIFY) {
1514 linelen = getline(&line, &linesize, f);
1515 if (linelen == -1) {
1516 if (feof(f))
1517 break;
1518 err = got_ferror(f, GOT_ERR_IO);
1519 break;
1522 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1523 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1524 == 0)
1525 *status = GOT_STATUS_CONFLICT;
1526 else
1527 i++;
1530 free(line);
1532 return err;
1535 static int
1536 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1538 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1539 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1542 static int
1543 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1545 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1546 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1547 ie->mtime_sec == sb->st_mtim.tv_sec &&
1548 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1549 ie->size == (sb->st_size & 0xffffffff) &&
1550 !xbit_differs(ie, sb->st_mode));
1553 static unsigned char
1554 get_staged_status(struct got_fileindex_entry *ie)
1556 switch (got_fileindex_entry_stage_get(ie)) {
1557 case GOT_FILEIDX_STAGE_ADD:
1558 return GOT_STATUS_ADD;
1559 case GOT_FILEIDX_STAGE_DELETE:
1560 return GOT_STATUS_DELETE;
1561 case GOT_FILEIDX_STAGE_MODIFY:
1562 return GOT_STATUS_MODIFY;
1563 default:
1564 return GOT_STATUS_NO_CHANGE;
1568 static const struct got_error *
1569 get_symlink_modification_status(unsigned char *status,
1570 struct got_fileindex_entry *ie, const char *abspath,
1571 int dirfd, const char *de_name, struct got_blob_object *blob)
1573 const struct got_error *err = NULL;
1574 char target_path[PATH_MAX];
1575 char etarget[PATH_MAX];
1576 ssize_t elen;
1577 size_t len, target_len = 0;
1578 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1579 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1581 *status = GOT_STATUS_NO_CHANGE;
1583 /* Blob object content specifies the target path of the link. */
1584 do {
1585 err = got_object_blob_read_block(&len, blob);
1586 if (err)
1587 return err;
1588 if (len + target_len >= sizeof(target_path)) {
1590 * Should not happen. The blob contents were OK
1591 * when this symlink was installed.
1593 return got_error(GOT_ERR_NO_SPACE);
1595 if (len > 0) {
1596 /* Skip blob object header first time around. */
1597 memcpy(target_path + target_len, buf + hdrlen,
1598 len - hdrlen);
1599 target_len += len - hdrlen;
1600 hdrlen = 0;
1602 } while (len != 0);
1603 target_path[target_len] = '\0';
1605 if (dirfd != -1) {
1606 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1607 if (elen == -1)
1608 return got_error_from_errno2("readlinkat", abspath);
1609 } else {
1610 elen = readlink(abspath, etarget, sizeof(etarget));
1611 if (elen == -1)
1612 return got_error_from_errno2("readlink", abspath);
1615 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1616 *status = GOT_STATUS_MODIFY;
1618 return NULL;
1621 static const struct got_error *
1622 get_file_status(unsigned char *status, struct stat *sb,
1623 struct got_fileindex_entry *ie, const char *abspath,
1624 int dirfd, const char *de_name, struct got_repository *repo)
1626 const struct got_error *err = NULL;
1627 struct got_object_id id;
1628 size_t hdrlen;
1629 int fd = -1, fd1 = -1;
1630 FILE *f = NULL;
1631 uint8_t fbuf[8192];
1632 struct got_blob_object *blob = NULL;
1633 size_t flen, blen;
1634 unsigned char staged_status = get_staged_status(ie);
1636 *status = GOT_STATUS_NO_CHANGE;
1637 memset(sb, 0, sizeof(*sb));
1640 * Whenever the caller provides a directory descriptor and a
1641 * directory entry name for the file, use them! This prevents
1642 * race conditions if filesystem paths change beneath our feet.
1644 if (dirfd != -1) {
1645 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1646 if (errno == ENOENT) {
1647 if (got_fileindex_entry_has_file_on_disk(ie))
1648 *status = GOT_STATUS_MISSING;
1649 else
1650 *status = GOT_STATUS_DELETE;
1651 goto done;
1653 err = got_error_from_errno2("fstatat", abspath);
1654 goto done;
1656 } else {
1657 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1658 if (fd == -1 && errno != ENOENT &&
1659 !got_err_open_nofollow_on_symlink())
1660 return got_error_from_errno2("open", abspath);
1661 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1662 if (lstat(abspath, sb) == -1)
1663 return got_error_from_errno2("lstat", abspath);
1664 } else if (fd == -1 || fstat(fd, sb) == -1) {
1665 if (errno == ENOENT) {
1666 if (got_fileindex_entry_has_file_on_disk(ie))
1667 *status = GOT_STATUS_MISSING;
1668 else
1669 *status = GOT_STATUS_DELETE;
1670 goto done;
1672 err = got_error_from_errno2("fstat", abspath);
1673 goto done;
1677 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1678 *status = GOT_STATUS_OBSTRUCTED;
1679 goto done;
1682 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1683 *status = GOT_STATUS_DELETE;
1684 goto done;
1685 } else if (!got_fileindex_entry_has_blob(ie) &&
1686 staged_status != GOT_STATUS_ADD) {
1687 *status = GOT_STATUS_ADD;
1688 goto done;
1691 if (!stat_info_differs(ie, sb))
1692 goto done;
1694 if (S_ISLNK(sb->st_mode) &&
1695 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1696 *status = GOT_STATUS_MODIFY;
1697 goto done;
1700 if (staged_status == GOT_STATUS_MODIFY ||
1701 staged_status == GOT_STATUS_ADD)
1702 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1703 else
1704 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1706 fd1 = got_opentempfd();
1707 if (fd1 == -1) {
1708 err = got_error_from_errno("got_opentempfd");
1709 goto done;
1711 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1712 if (err)
1713 goto done;
1715 if (S_ISLNK(sb->st_mode)) {
1716 err = get_symlink_modification_status(status, ie,
1717 abspath, dirfd, de_name, blob);
1718 goto done;
1721 if (dirfd != -1) {
1722 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1723 if (fd == -1) {
1724 err = got_error_from_errno2("openat", abspath);
1725 goto done;
1729 f = fdopen(fd, "r");
1730 if (f == NULL) {
1731 err = got_error_from_errno2("fdopen", abspath);
1732 goto done;
1734 fd = -1;
1735 hdrlen = got_object_blob_get_hdrlen(blob);
1736 for (;;) {
1737 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1738 err = got_object_blob_read_block(&blen, blob);
1739 if (err)
1740 goto done;
1741 /* Skip length of blob object header first time around. */
1742 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1743 if (flen == 0 && ferror(f)) {
1744 err = got_error_from_errno("fread");
1745 goto done;
1747 if (blen - hdrlen == 0) {
1748 if (flen != 0)
1749 *status = GOT_STATUS_MODIFY;
1750 break;
1751 } else if (flen == 0) {
1752 if (blen - hdrlen != 0)
1753 *status = GOT_STATUS_MODIFY;
1754 break;
1755 } else if (blen - hdrlen == flen) {
1756 /* Skip blob object header first time around. */
1757 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1758 *status = GOT_STATUS_MODIFY;
1759 break;
1761 } else {
1762 *status = GOT_STATUS_MODIFY;
1763 break;
1765 hdrlen = 0;
1768 if (*status == GOT_STATUS_MODIFY) {
1769 rewind(f);
1770 err = get_modified_file_content_status(status, f);
1771 } else if (xbit_differs(ie, sb->st_mode))
1772 *status = GOT_STATUS_MODE_CHANGE;
1773 done:
1774 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1775 err = got_error_from_errno("close");
1776 if (blob)
1777 got_object_blob_close(blob);
1778 if (f != NULL && fclose(f) == EOF && err == NULL)
1779 err = got_error_from_errno2("fclose", abspath);
1780 if (fd != -1 && close(fd) == -1 && err == NULL)
1781 err = got_error_from_errno2("close", abspath);
1782 return err;
1786 * Update timestamps in the file index if a file is unmodified and
1787 * we had to run a full content comparison to find out.
1789 static const struct got_error *
1790 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1791 struct got_fileindex_entry *ie, struct stat *sb)
1793 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1794 return got_fileindex_entry_update(ie, wt_fd, path,
1795 ie->blob_sha1, ie->commit_sha1, 1);
1797 return NULL;
1800 static const struct got_error *
1801 update_blob(struct got_worktree *worktree,
1802 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1803 struct got_tree_entry *te, const char *path,
1804 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1805 void *progress_arg)
1807 const struct got_error *err = NULL;
1808 struct got_blob_object *blob = NULL;
1809 char *ondisk_path = NULL;
1810 unsigned char status = GOT_STATUS_NO_CHANGE;
1811 struct stat sb;
1812 int fd1 = -1, fd2 = -1;
1814 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1815 return got_error_from_errno("asprintf");
1817 if (ie) {
1818 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1819 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1820 goto done;
1822 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1823 repo);
1824 if (err)
1825 goto done;
1826 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1827 sb.st_mode = got_fileindex_perms_to_st(ie);
1828 } else {
1829 if (stat(ondisk_path, &sb) == -1) {
1830 if (errno != ENOENT) {
1831 err = got_error_from_errno2("stat",
1832 ondisk_path);
1833 goto done;
1835 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1836 status = GOT_STATUS_UNVERSIONED;
1837 } else {
1838 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1839 status = GOT_STATUS_UNVERSIONED;
1840 else
1841 status = GOT_STATUS_OBSTRUCTED;
1845 if (status == GOT_STATUS_OBSTRUCTED) {
1846 if (ie)
1847 got_fileindex_entry_mark_skipped(ie);
1848 err = (*progress_cb)(progress_arg, status, path);
1849 goto done;
1851 if (status == GOT_STATUS_CONFLICT) {
1852 if (ie)
1853 got_fileindex_entry_mark_skipped(ie);
1854 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1855 path);
1856 goto done;
1859 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1860 (S_ISLNK(te->mode) ||
1861 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1863 * This is a regular file or an installed bad symlink.
1864 * If the file index indicates that this file is already
1865 * up-to-date with respect to the repository we can skip
1866 * updating contents of this file.
1868 if (got_fileindex_entry_has_commit(ie) &&
1869 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1870 SHA1_DIGEST_LENGTH) == 0) {
1871 /* Same commit. */
1872 err = sync_timestamps(worktree->root_fd,
1873 path, status, ie, &sb);
1874 if (err)
1875 goto done;
1876 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1877 path);
1878 goto done;
1880 if (got_fileindex_entry_has_blob(ie) &&
1881 memcmp(ie->blob_sha1, te->id.sha1,
1882 SHA1_DIGEST_LENGTH) == 0) {
1883 /* Different commit but the same blob. */
1884 err = sync_timestamps(worktree->root_fd,
1885 path, status, ie, &sb);
1886 if (err)
1887 goto done;
1888 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1889 path);
1890 goto done;
1894 fd1 = got_opentempfd();
1895 if (fd1 == -1) {
1896 err = got_error_from_errno("got_opentempfd");
1897 goto done;
1899 err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
1900 if (err)
1901 goto done;
1903 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1904 int update_timestamps;
1905 struct got_blob_object *blob2 = NULL;
1906 char *label_orig = NULL;
1907 if (got_fileindex_entry_has_blob(ie)) {
1908 fd2 = got_opentempfd();
1909 if (fd2 == -1) {
1910 err = got_error_from_errno("got_opentempfd");
1911 goto done;
1913 struct got_object_id id2;
1914 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1915 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
1916 fd2);
1917 if (err)
1918 goto done;
1920 if (got_fileindex_entry_has_commit(ie)) {
1921 char id_str[SHA1_DIGEST_STRING_LENGTH];
1922 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1923 sizeof(id_str)) == NULL) {
1924 err = got_error_path(id_str,
1925 GOT_ERR_BAD_OBJ_ID_STR);
1926 goto done;
1928 if (asprintf(&label_orig, "%s: commit %s",
1929 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1930 err = got_error_from_errno("asprintf");
1931 goto done;
1934 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1935 char *link_target;
1936 err = got_object_blob_read_to_str(&link_target, blob);
1937 if (err)
1938 goto done;
1939 err = merge_symlink(worktree, blob2, ondisk_path, path,
1940 label_orig, link_target, worktree->base_commit_id,
1941 repo, progress_cb, progress_arg);
1942 free(link_target);
1943 } else {
1944 err = merge_blob(&update_timestamps, worktree, blob2,
1945 ondisk_path, path, sb.st_mode, label_orig, blob,
1946 worktree->base_commit_id, repo,
1947 progress_cb, progress_arg);
1949 free(label_orig);
1950 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
1951 err = got_error_from_errno("close");
1952 goto done;
1954 if (blob2)
1955 got_object_blob_close(blob2);
1956 if (err)
1957 goto done;
1959 * Do not update timestamps of files with local changes.
1960 * Otherwise, a future status walk would treat them as
1961 * unmodified files again.
1963 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1964 blob->id.sha1, worktree->base_commit_id->sha1,
1965 update_timestamps);
1966 } else if (status == GOT_STATUS_MODE_CHANGE) {
1967 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1968 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1969 } else if (status == GOT_STATUS_DELETE) {
1970 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1971 if (err)
1972 goto done;
1973 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1974 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1975 if (err)
1976 goto done;
1977 } else {
1978 int is_bad_symlink = 0;
1979 if (S_ISLNK(te->mode)) {
1980 err = install_symlink(&is_bad_symlink, worktree,
1981 ondisk_path, path, blob,
1982 status == GOT_STATUS_MISSING, 0,
1983 status == GOT_STATUS_UNVERSIONED, 0,
1984 repo, progress_cb, progress_arg);
1985 } else {
1986 err = install_blob(worktree, ondisk_path, path,
1987 te->mode, sb.st_mode, blob,
1988 status == GOT_STATUS_MISSING, 0, 0,
1989 status == GOT_STATUS_UNVERSIONED, repo,
1990 progress_cb, progress_arg);
1992 if (err)
1993 goto done;
1995 if (ie) {
1996 err = got_fileindex_entry_update(ie,
1997 worktree->root_fd, path, blob->id.sha1,
1998 worktree->base_commit_id->sha1, 1);
1999 } else {
2000 err = create_fileindex_entry(&ie, fileindex,
2001 worktree->base_commit_id, worktree->root_fd, path,
2002 &blob->id);
2004 if (err)
2005 goto done;
2007 if (is_bad_symlink) {
2008 got_fileindex_entry_filetype_set(ie,
2009 GOT_FILEIDX_MODE_BAD_SYMLINK);
2013 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2014 err = got_error_from_errno("close");
2015 goto done;
2017 got_object_blob_close(blob);
2018 done:
2019 free(ondisk_path);
2020 return err;
2023 static const struct got_error *
2024 remove_ondisk_file(const char *root_path, const char *path)
2026 const struct got_error *err = NULL;
2027 char *ondisk_path = NULL, *parent = NULL;
2029 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2030 return got_error_from_errno("asprintf");
2032 if (unlink(ondisk_path) == -1) {
2033 if (errno != ENOENT)
2034 err = got_error_from_errno2("unlink", ondisk_path);
2035 } else {
2036 size_t root_len = strlen(root_path);
2037 err = got_path_dirname(&parent, ondisk_path);
2038 if (err)
2039 goto done;
2040 while (got_path_cmp(parent, root_path,
2041 strlen(parent), root_len) != 0) {
2042 free(ondisk_path);
2043 ondisk_path = parent;
2044 parent = NULL;
2045 if (rmdir(ondisk_path) == -1) {
2046 if (errno != ENOTEMPTY)
2047 err = got_error_from_errno2("rmdir",
2048 ondisk_path);
2049 break;
2051 err = got_path_dirname(&parent, ondisk_path);
2052 if (err)
2053 break;
2056 done:
2057 free(ondisk_path);
2058 free(parent);
2059 return err;
2062 static const struct got_error *
2063 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2064 struct got_fileindex_entry *ie, struct got_repository *repo,
2065 got_worktree_checkout_cb progress_cb, void *progress_arg)
2067 const struct got_error *err = NULL;
2068 unsigned char status;
2069 struct stat sb;
2070 char *ondisk_path;
2072 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2073 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2075 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2076 == -1)
2077 return got_error_from_errno("asprintf");
2079 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2080 if (err)
2081 goto done;
2083 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2084 char ondisk_target[PATH_MAX];
2085 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2086 sizeof(ondisk_target));
2087 if (ondisk_len == -1) {
2088 err = got_error_from_errno2("readlink", ondisk_path);
2089 goto done;
2091 ondisk_target[ondisk_len] = '\0';
2092 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2093 NULL, NULL, /* XXX pass common ancestor info? */
2094 ondisk_target, ondisk_path);
2095 if (err)
2096 goto done;
2097 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2098 ie->path);
2099 goto done;
2102 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2103 status == GOT_STATUS_ADD) {
2104 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2105 if (err)
2106 goto done;
2108 * Preserve the working file and change the deleted blob's
2109 * entry into a schedule-add entry.
2111 err = got_fileindex_entry_update(ie, worktree->root_fd,
2112 ie->path, NULL, NULL, 0);
2113 } else {
2114 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2115 if (err)
2116 goto done;
2117 if (status == GOT_STATUS_NO_CHANGE) {
2118 err = remove_ondisk_file(worktree->root_path, ie->path);
2119 if (err)
2120 goto done;
2122 got_fileindex_entry_remove(fileindex, ie);
2124 done:
2125 free(ondisk_path);
2126 return err;
2129 struct diff_cb_arg {
2130 struct got_fileindex *fileindex;
2131 struct got_worktree *worktree;
2132 struct got_repository *repo;
2133 got_worktree_checkout_cb progress_cb;
2134 void *progress_arg;
2135 got_cancel_cb cancel_cb;
2136 void *cancel_arg;
2139 static const struct got_error *
2140 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2141 struct got_tree_entry *te, const char *parent_path)
2143 struct diff_cb_arg *a = arg;
2145 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2146 return got_error(GOT_ERR_CANCELLED);
2148 return update_blob(a->worktree, a->fileindex, ie, te,
2149 ie->path, a->repo, a->progress_cb, a->progress_arg);
2152 static const struct got_error *
2153 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2155 struct diff_cb_arg *a = arg;
2157 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2158 return got_error(GOT_ERR_CANCELLED);
2160 return delete_blob(a->worktree, a->fileindex, ie,
2161 a->repo, a->progress_cb, a->progress_arg);
2164 static const struct got_error *
2165 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2167 struct diff_cb_arg *a = arg;
2168 const struct got_error *err;
2169 char *path;
2171 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2172 return got_error(GOT_ERR_CANCELLED);
2174 if (got_object_tree_entry_is_submodule(te))
2175 return NULL;
2177 if (asprintf(&path, "%s%s%s", parent_path,
2178 parent_path[0] ? "/" : "", te->name)
2179 == -1)
2180 return got_error_from_errno("asprintf");
2182 if (S_ISDIR(te->mode))
2183 err = add_dir_on_disk(a->worktree, path);
2184 else
2185 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2186 a->repo, a->progress_cb, a->progress_arg);
2188 free(path);
2189 return err;
2192 const struct got_error *
2193 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2195 uint32_t uuid_status;
2197 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2198 if (uuid_status != uuid_s_ok) {
2199 *uuidstr = NULL;
2200 return got_error_uuid(uuid_status, "uuid_to_string");
2203 return NULL;
2206 static const struct got_error *
2207 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2209 const struct got_error *err = NULL;
2210 char *uuidstr = NULL;
2212 *refname = NULL;
2214 err = got_worktree_get_uuid(&uuidstr, worktree);
2215 if (err)
2216 return err;
2218 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2219 err = got_error_from_errno("asprintf");
2220 *refname = NULL;
2222 free(uuidstr);
2223 return err;
2226 const struct got_error *
2227 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2229 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2232 static const struct got_error *
2233 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2235 return get_ref_name(refname, worktree,
2236 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2239 static const struct got_error *
2240 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2242 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2245 static const struct got_error *
2246 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2248 return get_ref_name(refname, worktree,
2249 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2252 static const struct got_error *
2253 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2255 return get_ref_name(refname, worktree,
2256 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2259 static const struct got_error *
2260 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2262 return get_ref_name(refname, worktree,
2263 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2266 static const struct got_error *
2267 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2269 return get_ref_name(refname, worktree,
2270 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2273 static const struct got_error *
2274 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2276 return get_ref_name(refname, worktree,
2277 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2280 static const struct got_error *
2281 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2283 return get_ref_name(refname, worktree,
2284 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2287 const struct got_error *
2288 got_worktree_get_histedit_script_path(char **path,
2289 struct got_worktree *worktree)
2291 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2292 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2293 *path = NULL;
2294 return got_error_from_errno("asprintf");
2296 return NULL;
2299 static const struct got_error *
2300 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2302 return get_ref_name(refname, worktree,
2303 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2306 static const struct got_error *
2307 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2309 return get_ref_name(refname, worktree,
2310 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2314 * Prevent Git's garbage collector from deleting our base commit by
2315 * setting a reference to our base commit's ID.
2317 static const struct got_error *
2318 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2320 const struct got_error *err = NULL;
2321 struct got_reference *ref = NULL;
2322 char *refname;
2324 err = got_worktree_get_base_ref_name(&refname, worktree);
2325 if (err)
2326 return err;
2328 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2329 if (err)
2330 goto done;
2332 err = got_ref_write(ref, repo);
2333 done:
2334 free(refname);
2335 if (ref)
2336 got_ref_close(ref);
2337 return err;
2340 static const struct got_error *
2341 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2343 const struct got_error *err = NULL;
2345 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2346 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2347 err = got_error_from_errno("asprintf");
2348 *fileindex_path = NULL;
2350 return err;
2354 static const struct got_error *
2355 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2356 struct got_worktree *worktree)
2358 const struct got_error *err = NULL;
2359 FILE *index = NULL;
2361 *fileindex_path = NULL;
2362 *fileindex = got_fileindex_alloc();
2363 if (*fileindex == NULL)
2364 return got_error_from_errno("got_fileindex_alloc");
2366 err = get_fileindex_path(fileindex_path, worktree);
2367 if (err)
2368 goto done;
2370 index = fopen(*fileindex_path, "rbe");
2371 if (index == NULL) {
2372 if (errno != ENOENT)
2373 err = got_error_from_errno2("fopen", *fileindex_path);
2374 } else {
2375 err = got_fileindex_read(*fileindex, index);
2376 if (fclose(index) == EOF && err == NULL)
2377 err = got_error_from_errno("fclose");
2379 done:
2380 if (err) {
2381 free(*fileindex_path);
2382 *fileindex_path = NULL;
2383 got_fileindex_free(*fileindex);
2384 *fileindex = NULL;
2386 return err;
2389 struct bump_base_commit_id_arg {
2390 struct got_object_id *base_commit_id;
2391 const char *path;
2392 size_t path_len;
2393 const char *entry_name;
2394 got_worktree_checkout_cb progress_cb;
2395 void *progress_arg;
2398 /* Bump base commit ID of all files within an updated part of the work tree. */
2399 static const struct got_error *
2400 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2402 const struct got_error *err;
2403 struct bump_base_commit_id_arg *a = arg;
2405 if (a->entry_name) {
2406 if (strcmp(ie->path, a->path) != 0)
2407 return NULL;
2408 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2409 return NULL;
2411 if (got_fileindex_entry_was_skipped(ie))
2412 return NULL;
2414 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2415 SHA1_DIGEST_LENGTH) == 0)
2416 return NULL;
2418 if (a->progress_cb) {
2419 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2420 ie->path);
2421 if (err)
2422 return err;
2424 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2425 return NULL;
2428 static const struct got_error *
2429 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2430 struct got_fileindex *fileindex,
2431 got_worktree_checkout_cb progress_cb, void *progress_arg)
2433 struct bump_base_commit_id_arg bbc_arg;
2435 bbc_arg.base_commit_id = worktree->base_commit_id;
2436 bbc_arg.entry_name = NULL;
2437 bbc_arg.path = "";
2438 bbc_arg.path_len = 0;
2439 bbc_arg.progress_cb = progress_cb;
2440 bbc_arg.progress_arg = progress_arg;
2442 return got_fileindex_for_each_entry_safe(fileindex,
2443 bump_base_commit_id, &bbc_arg);
2446 static const struct got_error *
2447 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2449 const struct got_error *err = NULL;
2450 char *new_fileindex_path = NULL;
2451 FILE *new_index = NULL;
2452 struct timespec timeout;
2454 err = got_opentemp_named(&new_fileindex_path, &new_index,
2455 fileindex_path);
2456 if (err)
2457 goto done;
2459 err = got_fileindex_write(fileindex, new_index);
2460 if (err)
2461 goto done;
2463 if (rename(new_fileindex_path, fileindex_path) != 0) {
2464 err = got_error_from_errno3("rename", new_fileindex_path,
2465 fileindex_path);
2466 unlink(new_fileindex_path);
2470 * Sleep for a short amount of time to ensure that files modified after
2471 * this program exits have a different time stamp from the one which
2472 * was recorded in the file index.
2474 timeout.tv_sec = 0;
2475 timeout.tv_nsec = 1;
2476 nanosleep(&timeout, NULL);
2477 done:
2478 if (new_index)
2479 fclose(new_index);
2480 free(new_fileindex_path);
2481 return err;
2484 static const struct got_error *
2485 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2486 struct got_object_id **tree_id, const char *wt_relpath,
2487 struct got_commit_object *base_commit, struct got_worktree *worktree,
2488 struct got_repository *repo)
2490 const struct got_error *err = NULL;
2491 struct got_object_id *id = NULL;
2492 char *in_repo_path = NULL;
2493 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2495 *entry_type = GOT_OBJ_TYPE_ANY;
2496 *tree_relpath = NULL;
2497 *tree_id = NULL;
2499 if (wt_relpath[0] == '\0') {
2500 /* Check out all files within the work tree. */
2501 *entry_type = GOT_OBJ_TYPE_TREE;
2502 *tree_relpath = strdup("");
2503 if (*tree_relpath == NULL) {
2504 err = got_error_from_errno("strdup");
2505 goto done;
2507 err = got_object_id_by_path(tree_id, repo, base_commit,
2508 worktree->path_prefix);
2509 if (err)
2510 goto done;
2511 return NULL;
2514 /* Check out a subset of files in the work tree. */
2516 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2517 is_root_wt ? "" : "/", wt_relpath) == -1) {
2518 err = got_error_from_errno("asprintf");
2519 goto done;
2522 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2523 if (err)
2524 goto done;
2526 free(in_repo_path);
2527 in_repo_path = NULL;
2529 err = got_object_get_type(entry_type, repo, id);
2530 if (err)
2531 goto done;
2533 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2534 /* Check out a single file. */
2535 if (strchr(wt_relpath, '/') == NULL) {
2536 /* Check out a single file in work tree's root dir. */
2537 in_repo_path = strdup(worktree->path_prefix);
2538 if (in_repo_path == NULL) {
2539 err = got_error_from_errno("strdup");
2540 goto done;
2542 *tree_relpath = strdup("");
2543 if (*tree_relpath == NULL) {
2544 err = got_error_from_errno("strdup");
2545 goto done;
2547 } else {
2548 /* Check out a single file in a subdirectory. */
2549 err = got_path_dirname(tree_relpath, wt_relpath);
2550 if (err)
2551 return err;
2552 if (asprintf(&in_repo_path, "%s%s%s",
2553 worktree->path_prefix, is_root_wt ? "" : "/",
2554 *tree_relpath) == -1) {
2555 err = got_error_from_errno("asprintf");
2556 goto done;
2559 err = got_object_id_by_path(tree_id, repo,
2560 base_commit, in_repo_path);
2561 } else {
2562 /* Check out all files within a subdirectory. */
2563 *tree_id = got_object_id_dup(id);
2564 if (*tree_id == NULL) {
2565 err = got_error_from_errno("got_object_id_dup");
2566 goto done;
2568 *tree_relpath = strdup(wt_relpath);
2569 if (*tree_relpath == NULL) {
2570 err = got_error_from_errno("strdup");
2571 goto done;
2574 done:
2575 free(id);
2576 free(in_repo_path);
2577 if (err) {
2578 *entry_type = GOT_OBJ_TYPE_ANY;
2579 free(*tree_relpath);
2580 *tree_relpath = NULL;
2581 free(*tree_id);
2582 *tree_id = NULL;
2584 return err;
2587 static const struct got_error *
2588 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2589 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2590 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2591 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2593 const struct got_error *err = NULL;
2594 struct got_commit_object *commit = NULL;
2595 struct got_tree_object *tree = NULL;
2596 struct got_fileindex_diff_tree_cb diff_cb;
2597 struct diff_cb_arg arg;
2599 err = ref_base_commit(worktree, repo);
2600 if (err) {
2601 if (!(err->code == GOT_ERR_ERRNO &&
2602 (errno == EACCES || errno == EROFS)))
2603 goto done;
2604 err = (*progress_cb)(progress_arg,
2605 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2606 if (err)
2607 return err;
2610 err = got_object_open_as_commit(&commit, repo,
2611 worktree->base_commit_id);
2612 if (err)
2613 goto done;
2615 err = got_object_open_as_tree(&tree, repo, tree_id);
2616 if (err)
2617 goto done;
2619 if (entry_name &&
2620 got_object_tree_find_entry(tree, entry_name) == NULL) {
2621 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2622 goto done;
2625 diff_cb.diff_old_new = diff_old_new;
2626 diff_cb.diff_old = diff_old;
2627 diff_cb.diff_new = diff_new;
2628 arg.fileindex = fileindex;
2629 arg.worktree = worktree;
2630 arg.repo = repo;
2631 arg.progress_cb = progress_cb;
2632 arg.progress_arg = progress_arg;
2633 arg.cancel_cb = cancel_cb;
2634 arg.cancel_arg = cancel_arg;
2635 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2636 entry_name, repo, &diff_cb, &arg);
2637 done:
2638 if (tree)
2639 got_object_tree_close(tree);
2640 if (commit)
2641 got_object_commit_close(commit);
2642 return err;
2645 const struct got_error *
2646 got_worktree_checkout_files(struct got_worktree *worktree,
2647 struct got_pathlist_head *paths, struct got_repository *repo,
2648 got_worktree_checkout_cb progress_cb, void *progress_arg,
2649 got_cancel_cb cancel_cb, void *cancel_arg)
2651 const struct got_error *err = NULL, *sync_err, *unlockerr;
2652 struct got_commit_object *commit = NULL;
2653 struct got_tree_object *tree = NULL;
2654 struct got_fileindex *fileindex = NULL;
2655 char *fileindex_path = NULL;
2656 struct got_pathlist_entry *pe;
2657 struct tree_path_data {
2658 STAILQ_ENTRY(tree_path_data) entry;
2659 struct got_object_id *tree_id;
2660 int entry_type;
2661 char *relpath;
2662 char *entry_name;
2663 } *tpd = NULL;
2664 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2666 STAILQ_INIT(&tree_paths);
2668 err = lock_worktree(worktree, LOCK_EX);
2669 if (err)
2670 return err;
2672 err = got_object_open_as_commit(&commit, repo,
2673 worktree->base_commit_id);
2674 if (err)
2675 goto done;
2677 /* Map all specified paths to in-repository trees. */
2678 TAILQ_FOREACH(pe, paths, entry) {
2679 tpd = malloc(sizeof(*tpd));
2680 if (tpd == NULL) {
2681 err = got_error_from_errno("malloc");
2682 goto done;
2685 err = find_tree_entry_for_checkout(&tpd->entry_type,
2686 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2687 worktree, repo);
2688 if (err) {
2689 free(tpd);
2690 goto done;
2693 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2694 err = got_path_basename(&tpd->entry_name, pe->path);
2695 if (err) {
2696 free(tpd->relpath);
2697 free(tpd->tree_id);
2698 free(tpd);
2699 goto done;
2701 } else
2702 tpd->entry_name = NULL;
2704 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2708 * Read the file index.
2709 * Checking out files is supposed to be an idempotent operation.
2710 * If the on-disk file index is incomplete we will try to complete it.
2712 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2713 if (err)
2714 goto done;
2716 tpd = STAILQ_FIRST(&tree_paths);
2717 TAILQ_FOREACH(pe, paths, entry) {
2718 struct bump_base_commit_id_arg bbc_arg;
2720 err = checkout_files(worktree, fileindex, tpd->relpath,
2721 tpd->tree_id, tpd->entry_name, repo,
2722 progress_cb, progress_arg, cancel_cb, cancel_arg);
2723 if (err)
2724 break;
2726 bbc_arg.base_commit_id = worktree->base_commit_id;
2727 bbc_arg.entry_name = tpd->entry_name;
2728 bbc_arg.path = pe->path;
2729 bbc_arg.path_len = pe->path_len;
2730 bbc_arg.progress_cb = progress_cb;
2731 bbc_arg.progress_arg = progress_arg;
2732 err = got_fileindex_for_each_entry_safe(fileindex,
2733 bump_base_commit_id, &bbc_arg);
2734 if (err)
2735 break;
2737 tpd = STAILQ_NEXT(tpd, entry);
2739 sync_err = sync_fileindex(fileindex, fileindex_path);
2740 if (sync_err && err == NULL)
2741 err = sync_err;
2742 done:
2743 free(fileindex_path);
2744 if (tree)
2745 got_object_tree_close(tree);
2746 if (commit)
2747 got_object_commit_close(commit);
2748 if (fileindex)
2749 got_fileindex_free(fileindex);
2750 while (!STAILQ_EMPTY(&tree_paths)) {
2751 tpd = STAILQ_FIRST(&tree_paths);
2752 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2753 free(tpd->relpath);
2754 free(tpd->tree_id);
2755 free(tpd);
2757 unlockerr = lock_worktree(worktree, LOCK_SH);
2758 if (unlockerr && err == NULL)
2759 err = unlockerr;
2760 return err;
2763 struct merge_file_cb_arg {
2764 struct got_worktree *worktree;
2765 struct got_fileindex *fileindex;
2766 got_worktree_checkout_cb progress_cb;
2767 void *progress_arg;
2768 got_cancel_cb cancel_cb;
2769 void *cancel_arg;
2770 const char *label_orig;
2771 struct got_object_id *commit_id2;
2772 int allow_bad_symlinks;
2775 static const struct got_error *
2776 merge_file_cb(void *arg, struct got_blob_object *blob1,
2777 struct got_blob_object *blob2, FILE *f1, FILE *f2,
2778 struct got_object_id *id1, struct got_object_id *id2,
2779 const char *path1, const char *path2,
2780 mode_t mode1, mode_t mode2, struct got_repository *repo)
2782 static const struct got_error *err = NULL;
2783 struct merge_file_cb_arg *a = arg;
2784 struct got_fileindex_entry *ie;
2785 char *ondisk_path = NULL;
2786 struct stat sb;
2787 unsigned char status;
2788 int local_changes_subsumed;
2789 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2790 char *id_str = NULL, *label_deriv2 = NULL;
2792 if (blob1 && blob2) {
2793 ie = got_fileindex_entry_get(a->fileindex, path2,
2794 strlen(path2));
2795 if (ie == NULL)
2796 return (*a->progress_cb)(a->progress_arg,
2797 GOT_STATUS_MISSING, path2);
2799 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2800 path2) == -1)
2801 return got_error_from_errno("asprintf");
2803 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2804 repo);
2805 if (err)
2806 goto done;
2808 if (status == GOT_STATUS_DELETE) {
2809 err = (*a->progress_cb)(a->progress_arg,
2810 GOT_STATUS_MERGE, path2);
2811 goto done;
2813 if (status != GOT_STATUS_NO_CHANGE &&
2814 status != GOT_STATUS_MODIFY &&
2815 status != GOT_STATUS_CONFLICT &&
2816 status != GOT_STATUS_ADD) {
2817 err = (*a->progress_cb)(a->progress_arg, status, path2);
2818 goto done;
2821 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2822 char *link_target2;
2823 err = got_object_blob_read_to_str(&link_target2, blob2);
2824 if (err)
2825 goto done;
2826 err = merge_symlink(a->worktree, blob1, ondisk_path,
2827 path2, a->label_orig, link_target2, a->commit_id2,
2828 repo, a->progress_cb, a->progress_arg);
2829 free(link_target2);
2830 } else {
2831 int fd;
2833 f_orig = got_opentemp();
2834 if (f_orig == NULL) {
2835 err = got_error_from_errno("got_opentemp");
2836 goto done;
2838 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2839 f_orig, blob1);
2840 if (err)
2841 goto done;
2843 f_deriv2 = got_opentemp();
2844 if (f_deriv2 == NULL)
2845 goto done;
2846 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2847 f_deriv2, blob2);
2848 if (err)
2849 goto done;
2851 fd = open(ondisk_path,
2852 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2853 if (fd == -1) {
2854 err = got_error_from_errno2("open",
2855 ondisk_path);
2856 goto done;
2858 f_deriv = fdopen(fd, "r");
2859 if (f_deriv == NULL) {
2860 err = got_error_from_errno2("fdopen",
2861 ondisk_path);
2862 close(fd);
2863 goto done;
2865 err = got_object_id_str(&id_str, a->commit_id2);
2866 if (err)
2867 goto done;
2868 if (asprintf(&label_deriv2, "%s: commit %s",
2869 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2870 err = got_error_from_errno("asprintf");
2871 goto done;
2873 err = merge_file(&local_changes_subsumed, a->worktree,
2874 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2875 sb.st_mode, a->label_orig, NULL, label_deriv2,
2876 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2877 a->progress_cb, a->progress_arg);
2879 } else if (blob1) {
2880 ie = got_fileindex_entry_get(a->fileindex, path1,
2881 strlen(path1));
2882 if (ie == NULL)
2883 return (*a->progress_cb)(a->progress_arg,
2884 GOT_STATUS_MISSING, path1);
2886 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2887 path1) == -1)
2888 return got_error_from_errno("asprintf");
2890 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2891 repo);
2892 if (err)
2893 goto done;
2895 switch (status) {
2896 case GOT_STATUS_NO_CHANGE:
2897 err = (*a->progress_cb)(a->progress_arg,
2898 GOT_STATUS_DELETE, path1);
2899 if (err)
2900 goto done;
2901 err = remove_ondisk_file(a->worktree->root_path, path1);
2902 if (err)
2903 goto done;
2904 if (ie)
2905 got_fileindex_entry_mark_deleted_from_disk(ie);
2906 break;
2907 case GOT_STATUS_DELETE:
2908 case GOT_STATUS_MISSING:
2909 err = (*a->progress_cb)(a->progress_arg,
2910 GOT_STATUS_DELETE, path1);
2911 if (err)
2912 goto done;
2913 if (ie)
2914 got_fileindex_entry_mark_deleted_from_disk(ie);
2915 break;
2916 case GOT_STATUS_ADD: {
2917 struct got_object_id *id;
2918 FILE *blob1_f;
2919 off_t blob1_size;
2921 * Delete the added file only if its content already
2922 * exists in the repository.
2924 err = got_object_blob_file_create(&id, &blob1_f,
2925 &blob1_size, path1);
2926 if (err)
2927 goto done;
2928 if (got_object_id_cmp(id, id1) == 0) {
2929 err = (*a->progress_cb)(a->progress_arg,
2930 GOT_STATUS_DELETE, path1);
2931 if (err)
2932 goto done;
2933 err = remove_ondisk_file(a->worktree->root_path,
2934 path1);
2935 if (err)
2936 goto done;
2937 if (ie)
2938 got_fileindex_entry_remove(a->fileindex,
2939 ie);
2940 } else {
2941 err = (*a->progress_cb)(a->progress_arg,
2942 GOT_STATUS_CANNOT_DELETE, path1);
2944 if (fclose(blob1_f) == EOF && err == NULL)
2945 err = got_error_from_errno("fclose");
2946 free(id);
2947 if (err)
2948 goto done;
2949 break;
2951 case GOT_STATUS_MODIFY:
2952 case GOT_STATUS_CONFLICT:
2953 err = (*a->progress_cb)(a->progress_arg,
2954 GOT_STATUS_CANNOT_DELETE, path1);
2955 if (err)
2956 goto done;
2957 break;
2958 case GOT_STATUS_OBSTRUCTED:
2959 err = (*a->progress_cb)(a->progress_arg, status, path1);
2960 if (err)
2961 goto done;
2962 break;
2963 default:
2964 break;
2966 } else if (blob2) {
2967 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2968 path2) == -1)
2969 return got_error_from_errno("asprintf");
2970 ie = got_fileindex_entry_get(a->fileindex, path2,
2971 strlen(path2));
2972 if (ie) {
2973 err = get_file_status(&status, &sb, ie, ondisk_path,
2974 -1, NULL, repo);
2975 if (err)
2976 goto done;
2977 if (status != GOT_STATUS_NO_CHANGE &&
2978 status != GOT_STATUS_MODIFY &&
2979 status != GOT_STATUS_CONFLICT &&
2980 status != GOT_STATUS_ADD) {
2981 err = (*a->progress_cb)(a->progress_arg,
2982 status, path2);
2983 goto done;
2985 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2986 char *link_target2;
2987 err = got_object_blob_read_to_str(&link_target2,
2988 blob2);
2989 if (err)
2990 goto done;
2991 err = merge_symlink(a->worktree, NULL,
2992 ondisk_path, path2, a->label_orig,
2993 link_target2, a->commit_id2, repo,
2994 a->progress_cb, a->progress_arg);
2995 free(link_target2);
2996 } else if (S_ISREG(sb.st_mode)) {
2997 err = merge_blob(&local_changes_subsumed,
2998 a->worktree, NULL, ondisk_path, path2,
2999 sb.st_mode, a->label_orig, blob2,
3000 a->commit_id2, repo, a->progress_cb,
3001 a->progress_arg);
3002 } else {
3003 err = got_error_path(ondisk_path,
3004 GOT_ERR_FILE_OBSTRUCTED);
3006 if (err)
3007 goto done;
3008 if (status == GOT_STATUS_DELETE) {
3009 err = got_fileindex_entry_update(ie,
3010 a->worktree->root_fd, path2, blob2->id.sha1,
3011 a->worktree->base_commit_id->sha1, 0);
3012 if (err)
3013 goto done;
3015 } else {
3016 int is_bad_symlink = 0;
3017 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3018 if (S_ISLNK(mode2)) {
3019 err = install_symlink(&is_bad_symlink,
3020 a->worktree, ondisk_path, path2, blob2, 0,
3021 0, 1, a->allow_bad_symlinks, repo,
3022 a->progress_cb, a->progress_arg);
3023 } else {
3024 err = install_blob(a->worktree, ondisk_path, path2,
3025 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3026 a->progress_cb, a->progress_arg);
3028 if (err)
3029 goto done;
3030 err = got_fileindex_entry_alloc(&ie, path2);
3031 if (err)
3032 goto done;
3033 err = got_fileindex_entry_update(ie,
3034 a->worktree->root_fd, path2, NULL, NULL, 1);
3035 if (err) {
3036 got_fileindex_entry_free(ie);
3037 goto done;
3039 err = got_fileindex_entry_add(a->fileindex, ie);
3040 if (err) {
3041 got_fileindex_entry_free(ie);
3042 goto done;
3044 if (is_bad_symlink) {
3045 got_fileindex_entry_filetype_set(ie,
3046 GOT_FILEIDX_MODE_BAD_SYMLINK);
3050 done:
3051 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3052 err = got_error_from_errno("fclose");
3053 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3054 err = got_error_from_errno("fclose");
3055 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3056 err = got_error_from_errno("fclose");
3057 free(id_str);
3058 free(label_deriv2);
3059 free(ondisk_path);
3060 return err;
3063 static const struct got_error *
3064 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3066 struct got_worktree *worktree = arg;
3068 /* Reject merges into a work tree with mixed base commits. */
3069 if (got_fileindex_entry_has_commit(ie) &&
3070 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3071 SHA1_DIGEST_LENGTH) != 0)
3072 return got_error(GOT_ERR_MIXED_COMMITS);
3074 return NULL;
3077 struct check_merge_conflicts_arg {
3078 struct got_worktree *worktree;
3079 struct got_fileindex *fileindex;
3080 struct got_repository *repo;
3083 static const struct got_error *
3084 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3085 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3086 struct got_object_id *id1, struct got_object_id *id2,
3087 const char *path1, const char *path2,
3088 mode_t mode1, mode_t mode2, struct got_repository *repo)
3090 const struct got_error *err = NULL;
3091 struct check_merge_conflicts_arg *a = arg;
3092 unsigned char status;
3093 struct stat sb;
3094 struct got_fileindex_entry *ie;
3095 const char *path = path2 ? path2 : path1;
3096 struct got_object_id *id = id2 ? id2 : id1;
3097 char *ondisk_path;
3099 if (id == NULL)
3100 return NULL;
3102 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3103 if (ie == NULL)
3104 return NULL;
3106 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3107 == -1)
3108 return got_error_from_errno("asprintf");
3110 /* Reject merges into a work tree with conflicted files. */
3111 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3112 free(ondisk_path);
3113 if (err)
3114 return err;
3115 if (status == GOT_STATUS_CONFLICT)
3116 return got_error(GOT_ERR_CONFLICTS);
3118 return NULL;
3121 static const struct got_error *
3122 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3123 const char *fileindex_path, struct got_object_id *commit_id1,
3124 struct got_object_id *commit_id2, struct got_repository *repo,
3125 got_worktree_checkout_cb progress_cb, void *progress_arg,
3126 got_cancel_cb cancel_cb, void *cancel_arg)
3128 const struct got_error *err = NULL, *sync_err;
3129 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3130 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3131 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3132 struct check_merge_conflicts_arg cmc_arg;
3133 struct merge_file_cb_arg arg;
3134 char *label_orig = NULL;
3135 FILE *f1 = NULL, *f2 = NULL;
3137 if (commit_id1) {
3138 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3139 if (err)
3140 goto done;
3141 err = got_object_id_by_path(&tree_id1, repo, commit1,
3142 worktree->path_prefix);
3143 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3144 goto done;
3146 if (tree_id1) {
3147 char *id_str;
3149 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3150 if (err)
3151 goto done;
3153 err = got_object_id_str(&id_str, commit_id1);
3154 if (err)
3155 goto done;
3157 if (asprintf(&label_orig, "%s: commit %s",
3158 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3159 err = got_error_from_errno("asprintf");
3160 free(id_str);
3161 goto done;
3163 free(id_str);
3165 f1 = got_opentemp();
3166 if (f1 == NULL) {
3167 err = got_error_from_errno("got_opentemp");
3168 goto done;
3172 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3173 if (err)
3174 goto done;
3176 err = got_object_id_by_path(&tree_id2, repo, commit2,
3177 worktree->path_prefix);
3178 if (err)
3179 goto done;
3181 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3182 if (err)
3183 goto done;
3185 f2 = got_opentemp();
3186 if (f2 == NULL) {
3187 err = got_error_from_errno("got_opentemp");
3188 goto done;
3191 cmc_arg.worktree = worktree;
3192 cmc_arg.fileindex = fileindex;
3193 cmc_arg.repo = repo;
3194 err = got_diff_tree(tree1, tree2, f1, f2, "", "", repo,
3195 check_merge_conflicts, &cmc_arg, 0);
3196 if (err)
3197 goto done;
3199 arg.worktree = worktree;
3200 arg.fileindex = fileindex;
3201 arg.progress_cb = progress_cb;
3202 arg.progress_arg = progress_arg;
3203 arg.cancel_cb = cancel_cb;
3204 arg.cancel_arg = cancel_arg;
3205 arg.label_orig = label_orig;
3206 arg.commit_id2 = commit_id2;
3207 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3208 err = got_diff_tree(tree1, tree2, f1, f2, "", "", repo,
3209 merge_file_cb, &arg, 1);
3210 sync_err = sync_fileindex(fileindex, fileindex_path);
3211 if (sync_err && err == NULL)
3212 err = sync_err;
3213 done:
3214 if (commit1)
3215 got_object_commit_close(commit1);
3216 if (commit2)
3217 got_object_commit_close(commit2);
3218 if (tree1)
3219 got_object_tree_close(tree1);
3220 if (tree2)
3221 got_object_tree_close(tree2);
3222 if (f1 && fclose(f1) == EOF && err == NULL)
3223 err = got_error_from_errno("fclose");
3224 if (f2 && fclose(f2) == EOF && err == NULL)
3225 err = got_error_from_errno("fclose");
3226 free(label_orig);
3227 return err;
3230 const struct got_error *
3231 got_worktree_merge_files(struct got_worktree *worktree,
3232 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3233 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3234 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3236 const struct got_error *err, *unlockerr;
3237 char *fileindex_path = NULL;
3238 struct got_fileindex *fileindex = NULL;
3240 err = lock_worktree(worktree, LOCK_EX);
3241 if (err)
3242 return err;
3244 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3245 if (err)
3246 goto done;
3248 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3249 worktree);
3250 if (err)
3251 goto done;
3253 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3254 commit_id2, repo, progress_cb, progress_arg,
3255 cancel_cb, cancel_arg);
3256 done:
3257 if (fileindex)
3258 got_fileindex_free(fileindex);
3259 free(fileindex_path);
3260 unlockerr = lock_worktree(worktree, LOCK_SH);
3261 if (unlockerr && err == NULL)
3262 err = unlockerr;
3263 return err;
3266 struct diff_dir_cb_arg {
3267 struct got_fileindex *fileindex;
3268 struct got_worktree *worktree;
3269 const char *status_path;
3270 size_t status_path_len;
3271 struct got_repository *repo;
3272 got_worktree_status_cb status_cb;
3273 void *status_arg;
3274 got_cancel_cb cancel_cb;
3275 void *cancel_arg;
3276 /* A pathlist containing per-directory pathlists of ignore patterns. */
3277 struct got_pathlist_head *ignores;
3278 int report_unchanged;
3279 int no_ignores;
3282 static const struct got_error *
3283 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3284 int dirfd, const char *de_name,
3285 got_worktree_status_cb status_cb, void *status_arg,
3286 struct got_repository *repo, int report_unchanged)
3288 const struct got_error *err = NULL;
3289 unsigned char status = GOT_STATUS_NO_CHANGE;
3290 unsigned char staged_status = get_staged_status(ie);
3291 struct stat sb;
3292 struct got_object_id blob_id, commit_id, staged_blob_id;
3293 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3294 struct got_object_id *staged_blob_idp = NULL;
3296 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3297 if (err)
3298 return err;
3300 if (status == GOT_STATUS_NO_CHANGE &&
3301 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3302 return NULL;
3304 if (got_fileindex_entry_has_blob(ie)) {
3305 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3306 blob_idp = &blob_id;
3308 if (got_fileindex_entry_has_commit(ie)) {
3309 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3310 commit_idp = &commit_id;
3312 if (staged_status == GOT_STATUS_ADD ||
3313 staged_status == GOT_STATUS_MODIFY) {
3314 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3315 SHA1_DIGEST_LENGTH);
3316 staged_blob_idp = &staged_blob_id;
3319 return (*status_cb)(status_arg, status, staged_status,
3320 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3323 static const struct got_error *
3324 status_old_new(void *arg, struct got_fileindex_entry *ie,
3325 struct dirent *de, const char *parent_path, int dirfd)
3327 const struct got_error *err = NULL;
3328 struct diff_dir_cb_arg *a = arg;
3329 char *abspath;
3331 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3332 return got_error(GOT_ERR_CANCELLED);
3334 if (got_path_cmp(parent_path, a->status_path,
3335 strlen(parent_path), a->status_path_len) != 0 &&
3336 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3337 return NULL;
3339 if (parent_path[0]) {
3340 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3341 parent_path, de->d_name) == -1)
3342 return got_error_from_errno("asprintf");
3343 } else {
3344 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3345 de->d_name) == -1)
3346 return got_error_from_errno("asprintf");
3349 err = report_file_status(ie, abspath, dirfd, de->d_name,
3350 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3351 free(abspath);
3352 return err;
3355 static const struct got_error *
3356 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3358 struct diff_dir_cb_arg *a = arg;
3359 struct got_object_id blob_id, commit_id;
3360 unsigned char status;
3362 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3363 return got_error(GOT_ERR_CANCELLED);
3365 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3366 return NULL;
3368 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3369 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3370 if (got_fileindex_entry_has_file_on_disk(ie))
3371 status = GOT_STATUS_MISSING;
3372 else
3373 status = GOT_STATUS_DELETE;
3374 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3375 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3378 static void
3379 free_ignorelist(struct got_pathlist_head *ignorelist)
3381 struct got_pathlist_entry *pe;
3383 TAILQ_FOREACH(pe, ignorelist, entry)
3384 free((char *)pe->path);
3385 got_pathlist_free(ignorelist);
3388 static void
3389 free_ignores(struct got_pathlist_head *ignores)
3391 struct got_pathlist_entry *pe;
3393 TAILQ_FOREACH(pe, ignores, entry) {
3394 struct got_pathlist_head *ignorelist = pe->data;
3395 free_ignorelist(ignorelist);
3396 free((char *)pe->path);
3398 got_pathlist_free(ignores);
3401 static const struct got_error *
3402 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3404 const struct got_error *err = NULL;
3405 struct got_pathlist_entry *pe = NULL;
3406 struct got_pathlist_head *ignorelist;
3407 char *line = NULL, *pattern, *dirpath = NULL;
3408 size_t linesize = 0;
3409 ssize_t linelen;
3411 ignorelist = calloc(1, sizeof(*ignorelist));
3412 if (ignorelist == NULL)
3413 return got_error_from_errno("calloc");
3414 TAILQ_INIT(ignorelist);
3416 while ((linelen = getline(&line, &linesize, f)) != -1) {
3417 if (linelen > 0 && line[linelen - 1] == '\n')
3418 line[linelen - 1] = '\0';
3420 /* Git's ignores may contain comments. */
3421 if (line[0] == '#')
3422 continue;
3424 /* Git's negated patterns are not (yet?) supported. */
3425 if (line[0] == '!')
3426 continue;
3428 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3429 line) == -1) {
3430 err = got_error_from_errno("asprintf");
3431 goto done;
3433 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3434 if (err)
3435 goto done;
3437 if (ferror(f)) {
3438 err = got_error_from_errno("getline");
3439 goto done;
3442 dirpath = strdup(path);
3443 if (dirpath == NULL) {
3444 err = got_error_from_errno("strdup");
3445 goto done;
3447 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3448 done:
3449 free(line);
3450 if (err || pe == NULL) {
3451 free(dirpath);
3452 free_ignorelist(ignorelist);
3454 return err;
3457 static int
3458 match_ignores(struct got_pathlist_head *ignores, const char *path)
3460 struct got_pathlist_entry *pe;
3462 /* Handle patterns which match in all directories. */
3463 TAILQ_FOREACH(pe, ignores, entry) {
3464 struct got_pathlist_head *ignorelist = pe->data;
3465 struct got_pathlist_entry *pi;
3467 TAILQ_FOREACH(pi, ignorelist, entry) {
3468 const char *p, *pattern = pi->path;
3470 if (strncmp(pattern, "**/", 3) != 0)
3471 continue;
3472 pattern += 3;
3473 p = path;
3474 while (*p) {
3475 if (fnmatch(pattern, p,
3476 FNM_PATHNAME | FNM_LEADING_DIR)) {
3477 /* Retry in next directory. */
3478 while (*p && *p != '/')
3479 p++;
3480 while (*p == '/')
3481 p++;
3482 continue;
3484 return 1;
3490 * The ignores pathlist contains ignore lists from children before
3491 * parents, so we can find the most specific ignorelist by walking
3492 * ignores backwards.
3494 pe = TAILQ_LAST(ignores, got_pathlist_head);
3495 while (pe) {
3496 if (got_path_is_child(path, pe->path, pe->path_len)) {
3497 struct got_pathlist_head *ignorelist = pe->data;
3498 struct got_pathlist_entry *pi;
3499 TAILQ_FOREACH(pi, ignorelist, entry) {
3500 const char *pattern = pi->path;
3501 int flags = FNM_LEADING_DIR;
3502 if (strstr(pattern, "/**/") == NULL)
3503 flags |= FNM_PATHNAME;
3504 if (fnmatch(pattern, path, flags))
3505 continue;
3506 return 1;
3509 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3512 return 0;
3515 static const struct got_error *
3516 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3517 const char *path, int dirfd, const char *ignores_filename)
3519 const struct got_error *err = NULL;
3520 char *ignorespath;
3521 int fd = -1;
3522 FILE *ignoresfile = NULL;
3524 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3525 path[0] ? "/" : "", ignores_filename) == -1)
3526 return got_error_from_errno("asprintf");
3528 if (dirfd != -1) {
3529 fd = openat(dirfd, ignores_filename,
3530 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3531 if (fd == -1) {
3532 if (errno != ENOENT && errno != EACCES)
3533 err = got_error_from_errno2("openat",
3534 ignorespath);
3535 } else {
3536 ignoresfile = fdopen(fd, "r");
3537 if (ignoresfile == NULL)
3538 err = got_error_from_errno2("fdopen",
3539 ignorespath);
3540 else {
3541 fd = -1;
3542 err = read_ignores(ignores, path, ignoresfile);
3545 } else {
3546 ignoresfile = fopen(ignorespath, "re");
3547 if (ignoresfile == NULL) {
3548 if (errno != ENOENT && errno != EACCES)
3549 err = got_error_from_errno2("fopen",
3550 ignorespath);
3551 } else
3552 err = read_ignores(ignores, path, ignoresfile);
3555 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3556 err = got_error_from_errno2("fclose", path);
3557 if (fd != -1 && close(fd) == -1 && err == NULL)
3558 err = got_error_from_errno2("close", path);
3559 free(ignorespath);
3560 return err;
3563 static const struct got_error *
3564 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3565 int dirfd)
3567 const struct got_error *err = NULL;
3568 struct diff_dir_cb_arg *a = arg;
3569 char *path = NULL;
3571 if (ignore != NULL)
3572 *ignore = 0;
3574 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3575 return got_error(GOT_ERR_CANCELLED);
3577 if (parent_path[0]) {
3578 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3579 return got_error_from_errno("asprintf");
3580 } else {
3581 path = de->d_name;
3584 if (de->d_type == DT_DIR) {
3585 if (!a->no_ignores && ignore != NULL &&
3586 match_ignores(a->ignores, path))
3587 *ignore = 1;
3588 } else if (!match_ignores(a->ignores, path) &&
3589 got_path_is_child(path, a->status_path, a->status_path_len))
3590 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3591 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3592 if (parent_path[0])
3593 free(path);
3594 return err;
3597 static const struct got_error *
3598 status_traverse(void *arg, const char *path, int dirfd)
3600 const struct got_error *err = NULL;
3601 struct diff_dir_cb_arg *a = arg;
3603 if (a->no_ignores)
3604 return NULL;
3606 err = add_ignores(a->ignores, a->worktree->root_path,
3607 path, dirfd, ".cvsignore");
3608 if (err)
3609 return err;
3611 err = add_ignores(a->ignores, a->worktree->root_path, path,
3612 dirfd, ".gitignore");
3614 return err;
3617 static const struct got_error *
3618 report_single_file_status(const char *path, const char *ondisk_path,
3619 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3620 void *status_arg, struct got_repository *repo, int report_unchanged,
3621 struct got_pathlist_head *ignores, int no_ignores)
3623 struct got_fileindex_entry *ie;
3624 struct stat sb;
3626 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3627 if (ie)
3628 return report_file_status(ie, ondisk_path, -1, NULL,
3629 status_cb, status_arg, repo, report_unchanged);
3631 if (lstat(ondisk_path, &sb) == -1) {
3632 if (errno != ENOENT)
3633 return got_error_from_errno2("lstat", ondisk_path);
3634 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3635 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3638 if (!no_ignores && match_ignores(ignores, path))
3639 return NULL;
3641 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3642 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3643 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3645 return NULL;
3648 static const struct got_error *
3649 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3650 const char *root_path, const char *path)
3652 const struct got_error *err;
3653 char *parent_path, *next_parent_path = NULL;
3655 err = add_ignores(ignores, root_path, "", -1,
3656 ".cvsignore");
3657 if (err)
3658 return err;
3660 err = add_ignores(ignores, root_path, "", -1,
3661 ".gitignore");
3662 if (err)
3663 return err;
3665 err = got_path_dirname(&parent_path, path);
3666 if (err) {
3667 if (err->code == GOT_ERR_BAD_PATH)
3668 return NULL; /* cannot traverse parent */
3669 return err;
3671 for (;;) {
3672 err = add_ignores(ignores, root_path, parent_path, -1,
3673 ".cvsignore");
3674 if (err)
3675 break;
3676 err = add_ignores(ignores, root_path, parent_path, -1,
3677 ".gitignore");
3678 if (err)
3679 break;
3680 err = got_path_dirname(&next_parent_path, parent_path);
3681 if (err) {
3682 if (err->code == GOT_ERR_BAD_PATH)
3683 err = NULL; /* traversed everything */
3684 break;
3686 if (got_path_is_root_dir(parent_path))
3687 break;
3688 free(parent_path);
3689 parent_path = next_parent_path;
3690 next_parent_path = NULL;
3693 free(parent_path);
3694 free(next_parent_path);
3695 return err;
3698 static const struct got_error *
3699 worktree_status(struct got_worktree *worktree, const char *path,
3700 struct got_fileindex *fileindex, struct got_repository *repo,
3701 got_worktree_status_cb status_cb, void *status_arg,
3702 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3703 int report_unchanged)
3705 const struct got_error *err = NULL;
3706 int fd = -1;
3707 struct got_fileindex_diff_dir_cb fdiff_cb;
3708 struct diff_dir_cb_arg arg;
3709 char *ondisk_path = NULL;
3710 struct got_pathlist_head ignores;
3711 struct got_fileindex_entry *ie;
3713 TAILQ_INIT(&ignores);
3715 if (asprintf(&ondisk_path, "%s%s%s",
3716 worktree->root_path, path[0] ? "/" : "", path) == -1)
3717 return got_error_from_errno("asprintf");
3719 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3720 if (ie) {
3721 err = report_single_file_status(path, ondisk_path,
3722 fileindex, status_cb, status_arg, repo,
3723 report_unchanged, &ignores, no_ignores);
3724 goto done;
3727 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3728 if (fd == -1) {
3729 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3730 !got_err_open_nofollow_on_symlink())
3731 err = got_error_from_errno2("open", ondisk_path);
3732 else {
3733 if (!no_ignores) {
3734 err = add_ignores_from_parent_paths(&ignores,
3735 worktree->root_path, ondisk_path);
3736 if (err)
3737 goto done;
3739 err = report_single_file_status(path, ondisk_path,
3740 fileindex, status_cb, status_arg, repo,
3741 report_unchanged, &ignores, no_ignores);
3743 } else {
3744 fdiff_cb.diff_old_new = status_old_new;
3745 fdiff_cb.diff_old = status_old;
3746 fdiff_cb.diff_new = status_new;
3747 fdiff_cb.diff_traverse = status_traverse;
3748 arg.fileindex = fileindex;
3749 arg.worktree = worktree;
3750 arg.status_path = path;
3751 arg.status_path_len = strlen(path);
3752 arg.repo = repo;
3753 arg.status_cb = status_cb;
3754 arg.status_arg = status_arg;
3755 arg.cancel_cb = cancel_cb;
3756 arg.cancel_arg = cancel_arg;
3757 arg.report_unchanged = report_unchanged;
3758 arg.no_ignores = no_ignores;
3759 if (!no_ignores) {
3760 err = add_ignores_from_parent_paths(&ignores,
3761 worktree->root_path, path);
3762 if (err)
3763 goto done;
3765 arg.ignores = &ignores;
3766 err = got_fileindex_diff_dir(fileindex, fd,
3767 worktree->root_path, path, repo, &fdiff_cb, &arg);
3769 done:
3770 free_ignores(&ignores);
3771 if (fd != -1 && close(fd) == -1 && err == NULL)
3772 err = got_error_from_errno("close");
3773 free(ondisk_path);
3774 return err;
3777 const struct got_error *
3778 got_worktree_status(struct got_worktree *worktree,
3779 struct got_pathlist_head *paths, struct got_repository *repo,
3780 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3781 got_cancel_cb cancel_cb, void *cancel_arg)
3783 const struct got_error *err = NULL;
3784 char *fileindex_path = NULL;
3785 struct got_fileindex *fileindex = NULL;
3786 struct got_pathlist_entry *pe;
3788 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3789 if (err)
3790 return err;
3792 TAILQ_FOREACH(pe, paths, entry) {
3793 err = worktree_status(worktree, pe->path, fileindex, repo,
3794 status_cb, status_arg, cancel_cb, cancel_arg,
3795 no_ignores, 0);
3796 if (err)
3797 break;
3799 free(fileindex_path);
3800 got_fileindex_free(fileindex);
3801 return err;
3804 const struct got_error *
3805 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3806 const char *arg)
3808 const struct got_error *err = NULL;
3809 char *resolved = NULL, *cwd = NULL, *path = NULL;
3810 size_t len;
3811 struct stat sb;
3812 char *abspath = NULL;
3813 char canonpath[PATH_MAX];
3815 *wt_path = NULL;
3817 cwd = getcwd(NULL, 0);
3818 if (cwd == NULL)
3819 return got_error_from_errno("getcwd");
3821 if (lstat(arg, &sb) == -1) {
3822 if (errno != ENOENT) {
3823 err = got_error_from_errno2("lstat", arg);
3824 goto done;
3826 sb.st_mode = 0;
3828 if (S_ISLNK(sb.st_mode)) {
3830 * We cannot use realpath(3) with symlinks since we want to
3831 * operate on the symlink itself.
3832 * But we can make the path absolute, assuming it is relative
3833 * to the current working directory, and then canonicalize it.
3835 if (!got_path_is_absolute(arg)) {
3836 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3837 err = got_error_from_errno("asprintf");
3838 goto done;
3842 err = got_canonpath(abspath ? abspath : arg, canonpath,
3843 sizeof(canonpath));
3844 if (err)
3845 goto done;
3846 resolved = strdup(canonpath);
3847 if (resolved == NULL) {
3848 err = got_error_from_errno("strdup");
3849 goto done;
3851 } else {
3852 resolved = realpath(arg, NULL);
3853 if (resolved == NULL) {
3854 if (errno != ENOENT) {
3855 err = got_error_from_errno2("realpath", arg);
3856 goto done;
3858 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3859 err = got_error_from_errno("asprintf");
3860 goto done;
3862 err = got_canonpath(abspath, canonpath,
3863 sizeof(canonpath));
3864 if (err)
3865 goto done;
3866 resolved = strdup(canonpath);
3867 if (resolved == NULL) {
3868 err = got_error_from_errno("strdup");
3869 goto done;
3874 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3875 strlen(got_worktree_get_root_path(worktree)))) {
3876 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3877 goto done;
3880 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3881 err = got_path_skip_common_ancestor(&path,
3882 got_worktree_get_root_path(worktree), resolved);
3883 if (err)
3884 goto done;
3885 } else {
3886 path = strdup("");
3887 if (path == NULL) {
3888 err = got_error_from_errno("strdup");
3889 goto done;
3893 /* XXX status walk can't deal with trailing slash! */
3894 len = strlen(path);
3895 while (len > 0 && path[len - 1] == '/') {
3896 path[len - 1] = '\0';
3897 len--;
3899 done:
3900 free(abspath);
3901 free(resolved);
3902 free(cwd);
3903 if (err == NULL)
3904 *wt_path = path;
3905 else
3906 free(path);
3907 return err;
3910 struct schedule_addition_args {
3911 struct got_worktree *worktree;
3912 struct got_fileindex *fileindex;
3913 got_worktree_checkout_cb progress_cb;
3914 void *progress_arg;
3915 struct got_repository *repo;
3918 static const struct got_error *
3919 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3920 const char *relpath, struct got_object_id *blob_id,
3921 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3922 int dirfd, const char *de_name)
3924 struct schedule_addition_args *a = arg;
3925 const struct got_error *err = NULL;
3926 struct got_fileindex_entry *ie;
3927 struct stat sb;
3928 char *ondisk_path;
3930 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3931 relpath) == -1)
3932 return got_error_from_errno("asprintf");
3934 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3935 if (ie) {
3936 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3937 de_name, a->repo);
3938 if (err)
3939 goto done;
3940 /* Re-adding an existing entry is a no-op. */
3941 if (status == GOT_STATUS_ADD)
3942 goto done;
3943 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3944 if (err)
3945 goto done;
3948 if (status != GOT_STATUS_UNVERSIONED) {
3949 if (status == GOT_STATUS_NONEXISTENT)
3950 err = got_error_set_errno(ENOENT, ondisk_path);
3951 else
3952 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3953 goto done;
3956 err = got_fileindex_entry_alloc(&ie, relpath);
3957 if (err)
3958 goto done;
3959 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3960 relpath, NULL, NULL, 1);
3961 if (err) {
3962 got_fileindex_entry_free(ie);
3963 goto done;
3965 err = got_fileindex_entry_add(a->fileindex, ie);
3966 if (err) {
3967 got_fileindex_entry_free(ie);
3968 goto done;
3970 done:
3971 free(ondisk_path);
3972 if (err)
3973 return err;
3974 if (status == GOT_STATUS_ADD)
3975 return NULL;
3976 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3979 const struct got_error *
3980 got_worktree_schedule_add(struct got_worktree *worktree,
3981 struct got_pathlist_head *paths,
3982 got_worktree_checkout_cb progress_cb, void *progress_arg,
3983 struct got_repository *repo, int no_ignores)
3985 struct got_fileindex *fileindex = NULL;
3986 char *fileindex_path = NULL;
3987 const struct got_error *err = NULL, *sync_err, *unlockerr;
3988 struct got_pathlist_entry *pe;
3989 struct schedule_addition_args saa;
3991 err = lock_worktree(worktree, LOCK_EX);
3992 if (err)
3993 return err;
3995 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3996 if (err)
3997 goto done;
3999 saa.worktree = worktree;
4000 saa.fileindex = fileindex;
4001 saa.progress_cb = progress_cb;
4002 saa.progress_arg = progress_arg;
4003 saa.repo = repo;
4005 TAILQ_FOREACH(pe, paths, entry) {
4006 err = worktree_status(worktree, pe->path, fileindex, repo,
4007 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4008 if (err)
4009 break;
4011 sync_err = sync_fileindex(fileindex, fileindex_path);
4012 if (sync_err && err == NULL)
4013 err = sync_err;
4014 done:
4015 free(fileindex_path);
4016 if (fileindex)
4017 got_fileindex_free(fileindex);
4018 unlockerr = lock_worktree(worktree, LOCK_SH);
4019 if (unlockerr && err == NULL)
4020 err = unlockerr;
4021 return err;
4024 struct schedule_deletion_args {
4025 struct got_worktree *worktree;
4026 struct got_fileindex *fileindex;
4027 got_worktree_delete_cb progress_cb;
4028 void *progress_arg;
4029 struct got_repository *repo;
4030 int delete_local_mods;
4031 int keep_on_disk;
4032 int ignore_missing_paths;
4033 const char *status_codes;
4036 static const struct got_error *
4037 schedule_for_deletion(void *arg, unsigned char status,
4038 unsigned char staged_status, const char *relpath,
4039 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4040 struct got_object_id *commit_id, int dirfd, const char *de_name)
4042 struct schedule_deletion_args *a = arg;
4043 const struct got_error *err = NULL;
4044 struct got_fileindex_entry *ie = NULL;
4045 struct stat sb;
4046 char *ondisk_path;
4048 if (status == GOT_STATUS_NONEXISTENT) {
4049 if (a->ignore_missing_paths)
4050 return NULL;
4051 return got_error_set_errno(ENOENT, relpath);
4054 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4055 if (ie == NULL)
4056 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4058 staged_status = get_staged_status(ie);
4059 if (staged_status != GOT_STATUS_NO_CHANGE) {
4060 if (staged_status == GOT_STATUS_DELETE)
4061 return NULL;
4062 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4065 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4066 relpath) == -1)
4067 return got_error_from_errno("asprintf");
4069 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4070 a->repo);
4071 if (err)
4072 goto done;
4074 if (a->status_codes) {
4075 size_t ncodes = strlen(a->status_codes);
4076 int i;
4077 for (i = 0; i < ncodes ; i++) {
4078 if (status == a->status_codes[i])
4079 break;
4081 if (i == ncodes) {
4082 /* Do not delete files in non-matching status. */
4083 free(ondisk_path);
4084 return NULL;
4086 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4087 a->status_codes[i] != GOT_STATUS_MISSING) {
4088 static char msg[64];
4089 snprintf(msg, sizeof(msg),
4090 "invalid status code '%c'", a->status_codes[i]);
4091 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4092 goto done;
4096 if (status != GOT_STATUS_NO_CHANGE) {
4097 if (status == GOT_STATUS_DELETE)
4098 goto done;
4099 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4100 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4101 goto done;
4103 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4104 err = got_error_set_errno(ENOENT, relpath);
4105 goto done;
4107 if (status != GOT_STATUS_MODIFY &&
4108 status != GOT_STATUS_MISSING) {
4109 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4110 goto done;
4114 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4115 size_t root_len;
4117 if (dirfd != -1) {
4118 if (unlinkat(dirfd, de_name, 0) != 0) {
4119 err = got_error_from_errno2("unlinkat",
4120 ondisk_path);
4121 goto done;
4123 } else if (unlink(ondisk_path) != 0) {
4124 err = got_error_from_errno2("unlink", ondisk_path);
4125 goto done;
4128 root_len = strlen(a->worktree->root_path);
4129 do {
4130 char *parent;
4131 err = got_path_dirname(&parent, ondisk_path);
4132 if (err)
4133 goto done;
4134 free(ondisk_path);
4135 ondisk_path = parent;
4136 if (rmdir(ondisk_path) == -1) {
4137 if (errno != ENOTEMPTY)
4138 err = got_error_from_errno2("rmdir",
4139 ondisk_path);
4140 break;
4142 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4143 strlen(ondisk_path), root_len) != 0);
4146 got_fileindex_entry_mark_deleted_from_disk(ie);
4147 done:
4148 free(ondisk_path);
4149 if (err)
4150 return err;
4151 if (status == GOT_STATUS_DELETE)
4152 return NULL;
4153 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4154 staged_status, relpath);
4157 const struct got_error *
4158 got_worktree_schedule_delete(struct got_worktree *worktree,
4159 struct got_pathlist_head *paths, int delete_local_mods,
4160 const char *status_codes,
4161 got_worktree_delete_cb progress_cb, void *progress_arg,
4162 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4164 struct got_fileindex *fileindex = NULL;
4165 char *fileindex_path = NULL;
4166 const struct got_error *err = NULL, *sync_err, *unlockerr;
4167 struct got_pathlist_entry *pe;
4168 struct schedule_deletion_args sda;
4170 err = lock_worktree(worktree, LOCK_EX);
4171 if (err)
4172 return err;
4174 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4175 if (err)
4176 goto done;
4178 sda.worktree = worktree;
4179 sda.fileindex = fileindex;
4180 sda.progress_cb = progress_cb;
4181 sda.progress_arg = progress_arg;
4182 sda.repo = repo;
4183 sda.delete_local_mods = delete_local_mods;
4184 sda.keep_on_disk = keep_on_disk;
4185 sda.ignore_missing_paths = ignore_missing_paths;
4186 sda.status_codes = status_codes;
4188 TAILQ_FOREACH(pe, paths, entry) {
4189 err = worktree_status(worktree, pe->path, fileindex, repo,
4190 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4191 if (err)
4192 break;
4194 sync_err = sync_fileindex(fileindex, fileindex_path);
4195 if (sync_err && err == NULL)
4196 err = sync_err;
4197 done:
4198 free(fileindex_path);
4199 if (fileindex)
4200 got_fileindex_free(fileindex);
4201 unlockerr = lock_worktree(worktree, LOCK_SH);
4202 if (unlockerr && err == NULL)
4203 err = unlockerr;
4204 return err;
4207 static const struct got_error *
4208 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4210 const struct got_error *err = NULL;
4211 char *line = NULL;
4212 size_t linesize = 0, n;
4213 ssize_t linelen;
4215 linelen = getline(&line, &linesize, infile);
4216 if (linelen == -1) {
4217 if (ferror(infile)) {
4218 err = got_error_from_errno("getline");
4219 goto done;
4221 return NULL;
4223 if (outfile) {
4224 n = fwrite(line, 1, linelen, outfile);
4225 if (n != linelen) {
4226 err = got_ferror(outfile, GOT_ERR_IO);
4227 goto done;
4230 if (rejectfile) {
4231 n = fwrite(line, 1, linelen, rejectfile);
4232 if (n != linelen)
4233 err = got_ferror(outfile, GOT_ERR_IO);
4235 done:
4236 free(line);
4237 return err;
4240 static const struct got_error *
4241 skip_one_line(FILE *f)
4243 char *line = NULL;
4244 size_t linesize = 0;
4245 ssize_t linelen;
4247 linelen = getline(&line, &linesize, f);
4248 if (linelen == -1) {
4249 if (ferror(f))
4250 return got_error_from_errno("getline");
4251 return NULL;
4253 free(line);
4254 return NULL;
4257 static const struct got_error *
4258 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4259 int start_old, int end_old, int start_new, int end_new,
4260 FILE *outfile, FILE *rejectfile)
4262 const struct got_error *err;
4264 /* Copy old file's lines leading up to patch. */
4265 while (!feof(f1) && *line_cur1 < start_old) {
4266 err = copy_one_line(f1, outfile, NULL);
4267 if (err)
4268 return err;
4269 (*line_cur1)++;
4271 /* Skip new file's lines leading up to patch. */
4272 while (!feof(f2) && *line_cur2 < start_new) {
4273 if (rejectfile)
4274 err = copy_one_line(f2, NULL, rejectfile);
4275 else
4276 err = skip_one_line(f2);
4277 if (err)
4278 return err;
4279 (*line_cur2)++;
4281 /* Copy patched lines. */
4282 while (!feof(f2) && *line_cur2 <= end_new) {
4283 err = copy_one_line(f2, outfile, NULL);
4284 if (err)
4285 return err;
4286 (*line_cur2)++;
4288 /* Skip over old file's replaced lines. */
4289 while (!feof(f1) && *line_cur1 <= end_old) {
4290 if (rejectfile)
4291 err = copy_one_line(f1, NULL, rejectfile);
4292 else
4293 err = skip_one_line(f1);
4294 if (err)
4295 return err;
4296 (*line_cur1)++;
4299 return NULL;
4302 static const struct got_error *
4303 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4304 FILE *outfile, FILE *rejectfile)
4306 const struct got_error *err;
4308 if (outfile) {
4309 /* Copy old file's lines until EOF. */
4310 while (!feof(f1)) {
4311 err = copy_one_line(f1, outfile, NULL);
4312 if (err)
4313 return err;
4314 (*line_cur1)++;
4317 if (rejectfile) {
4318 /* Copy new file's lines until EOF. */
4319 while (!feof(f2)) {
4320 err = copy_one_line(f2, NULL, rejectfile);
4321 if (err)
4322 return err;
4323 (*line_cur2)++;
4327 return NULL;
4330 static const struct got_error *
4331 apply_or_reject_change(int *choice, int *nchunks_used,
4332 struct diff_result *diff_result, int n,
4333 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4334 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4335 got_worktree_patch_cb patch_cb, void *patch_arg)
4337 const struct got_error *err = NULL;
4338 struct diff_chunk_context cc = {};
4339 int start_old, end_old, start_new, end_new;
4340 FILE *hunkfile;
4341 struct diff_output_unidiff_state *diff_state;
4342 struct diff_input_info diff_info;
4343 int rc;
4345 *choice = GOT_PATCH_CHOICE_NONE;
4347 /* Get changed line numbers without context lines for copy_change(). */
4348 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4349 start_old = cc.left.start;
4350 end_old = cc.left.end;
4351 start_new = cc.right.start;
4352 end_new = cc.right.end;
4354 /* Get the same change with context lines for display. */
4355 memset(&cc, 0, sizeof(cc));
4356 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4358 memset(&diff_info, 0, sizeof(diff_info));
4359 diff_info.left_path = relpath;
4360 diff_info.right_path = relpath;
4362 diff_state = diff_output_unidiff_state_alloc();
4363 if (diff_state == NULL)
4364 return got_error_set_errno(ENOMEM,
4365 "diff_output_unidiff_state_alloc");
4367 hunkfile = got_opentemp();
4368 if (hunkfile == NULL) {
4369 err = got_error_from_errno("got_opentemp");
4370 goto done;
4373 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4374 diff_result, &cc);
4375 if (rc != DIFF_RC_OK) {
4376 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4377 goto done;
4380 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4381 err = got_ferror(hunkfile, GOT_ERR_IO);
4382 goto done;
4385 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4386 hunkfile, changeno, nchanges);
4387 if (err)
4388 goto done;
4390 switch (*choice) {
4391 case GOT_PATCH_CHOICE_YES:
4392 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4393 end_old, start_new, end_new, outfile, rejectfile);
4394 break;
4395 case GOT_PATCH_CHOICE_NO:
4396 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4397 end_old, start_new, end_new, rejectfile, outfile);
4398 break;
4399 case GOT_PATCH_CHOICE_QUIT:
4400 break;
4401 default:
4402 err = got_error(GOT_ERR_PATCH_CHOICE);
4403 break;
4405 done:
4406 diff_output_unidiff_state_free(diff_state);
4407 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4408 err = got_error_from_errno("fclose");
4409 return err;
4412 struct revert_file_args {
4413 struct got_worktree *worktree;
4414 struct got_fileindex *fileindex;
4415 got_worktree_checkout_cb progress_cb;
4416 void *progress_arg;
4417 got_worktree_patch_cb patch_cb;
4418 void *patch_arg;
4419 struct got_repository *repo;
4420 int unlink_added_files;
4423 static const struct got_error *
4424 create_patched_content(char **path_outfile, int reverse_patch,
4425 struct got_object_id *blob_id, const char *path2,
4426 int dirfd2, const char *de_name2,
4427 const char *relpath, struct got_repository *repo,
4428 got_worktree_patch_cb patch_cb, void *patch_arg)
4430 const struct got_error *err, *free_err;
4431 struct got_blob_object *blob = NULL;
4432 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4433 int fd = -1, fd2 = -1;
4434 char link_target[PATH_MAX];
4435 ssize_t link_len = 0;
4436 char *path1 = NULL, *id_str = NULL;
4437 struct stat sb2;
4438 struct got_diffreg_result *diffreg_result = NULL;
4439 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4440 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4442 *path_outfile = NULL;
4444 err = got_object_id_str(&id_str, blob_id);
4445 if (err)
4446 return err;
4448 if (dirfd2 != -1) {
4449 fd2 = openat(dirfd2, de_name2,
4450 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4451 if (fd2 == -1) {
4452 if (!got_err_open_nofollow_on_symlink()) {
4453 err = got_error_from_errno2("openat", path2);
4454 goto done;
4456 link_len = readlinkat(dirfd2, de_name2,
4457 link_target, sizeof(link_target));
4458 if (link_len == -1) {
4459 return got_error_from_errno2("readlinkat",
4460 path2);
4462 sb2.st_mode = S_IFLNK;
4463 sb2.st_size = link_len;
4465 } else {
4466 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4467 if (fd2 == -1) {
4468 if (!got_err_open_nofollow_on_symlink()) {
4469 err = got_error_from_errno2("open", path2);
4470 goto done;
4472 link_len = readlink(path2, link_target,
4473 sizeof(link_target));
4474 if (link_len == -1)
4475 return got_error_from_errno2("readlink", path2);
4476 sb2.st_mode = S_IFLNK;
4477 sb2.st_size = link_len;
4480 if (fd2 != -1) {
4481 if (fstat(fd2, &sb2) == -1) {
4482 err = got_error_from_errno2("fstat", path2);
4483 goto done;
4486 f2 = fdopen(fd2, "r");
4487 if (f2 == NULL) {
4488 err = got_error_from_errno2("fdopen", path2);
4489 goto done;
4491 fd2 = -1;
4492 } else {
4493 size_t n;
4494 f2 = got_opentemp();
4495 if (f2 == NULL) {
4496 err = got_error_from_errno2("got_opentemp", path2);
4497 goto done;
4499 n = fwrite(link_target, 1, link_len, f2);
4500 if (n != link_len) {
4501 err = got_ferror(f2, GOT_ERR_IO);
4502 goto done;
4504 if (fflush(f2) == EOF) {
4505 err = got_error_from_errno("fflush");
4506 goto done;
4508 rewind(f2);
4511 fd = got_opentempfd();
4512 if (fd == -1) {
4513 err = got_error_from_errno("got_opentempfd");
4514 goto done;
4517 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4518 if (err)
4519 goto done;
4521 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4522 if (err)
4523 goto done;
4525 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4526 if (err)
4527 goto done;
4529 err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4530 NULL);
4531 if (err)
4532 goto done;
4534 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4535 if (err)
4536 goto done;
4538 if (fseek(f1, 0L, SEEK_SET) == -1)
4539 return got_ferror(f1, GOT_ERR_IO);
4540 if (fseek(f2, 0L, SEEK_SET) == -1)
4541 return got_ferror(f2, GOT_ERR_IO);
4543 /* Count the number of actual changes in the diff result. */
4544 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4545 struct diff_chunk_context cc = {};
4546 diff_chunk_context_load_change(&cc, &nchunks_used,
4547 diffreg_result->result, n, 0);
4548 nchanges++;
4550 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4551 int choice;
4552 err = apply_or_reject_change(&choice, &nchunks_used,
4553 diffreg_result->result, n, relpath, f1, f2,
4554 &line_cur1, &line_cur2,
4555 reverse_patch ? NULL : outfile,
4556 reverse_patch ? outfile : NULL,
4557 ++i, nchanges, patch_cb, patch_arg);
4558 if (err)
4559 goto done;
4560 if (choice == GOT_PATCH_CHOICE_YES)
4561 have_content = 1;
4562 else if (choice == GOT_PATCH_CHOICE_QUIT)
4563 break;
4565 if (have_content) {
4566 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4567 reverse_patch ? NULL : outfile,
4568 reverse_patch ? outfile : NULL);
4569 if (err)
4570 goto done;
4572 if (!S_ISLNK(sb2.st_mode)) {
4573 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4574 err = got_error_from_errno2("fchmod", path2);
4575 goto done;
4579 done:
4580 free(id_str);
4581 if (fd != -1 && close(fd) == -1 && err == NULL)
4582 err = got_error_from_errno("close");
4583 if (blob)
4584 got_object_blob_close(blob);
4585 free_err = got_diffreg_result_free(diffreg_result);
4586 if (err == NULL)
4587 err = free_err;
4588 if (f1 && fclose(f1) == EOF && err == NULL)
4589 err = got_error_from_errno2("fclose", path1);
4590 if (f2 && fclose(f2) == EOF && err == NULL)
4591 err = got_error_from_errno2("fclose", path2);
4592 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4593 err = got_error_from_errno2("close", path2);
4594 if (outfile && fclose(outfile) == EOF && err == NULL)
4595 err = got_error_from_errno2("fclose", *path_outfile);
4596 if (path1 && unlink(path1) == -1 && err == NULL)
4597 err = got_error_from_errno2("unlink", path1);
4598 if (err || !have_content) {
4599 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4600 err = got_error_from_errno2("unlink", *path_outfile);
4601 free(*path_outfile);
4602 *path_outfile = NULL;
4604 free(path1);
4605 return err;
4608 static const struct got_error *
4609 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4610 const char *relpath, struct got_object_id *blob_id,
4611 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4612 int dirfd, const char *de_name)
4614 struct revert_file_args *a = arg;
4615 const struct got_error *err = NULL;
4616 char *parent_path = NULL;
4617 struct got_fileindex_entry *ie;
4618 struct got_commit_object *base_commit = NULL;
4619 struct got_tree_object *tree = NULL;
4620 struct got_object_id *tree_id = NULL;
4621 const struct got_tree_entry *te = NULL;
4622 char *tree_path = NULL, *te_name;
4623 char *ondisk_path = NULL, *path_content = NULL;
4624 struct got_blob_object *blob = NULL;
4625 int fd = -1;
4627 /* Reverting a staged deletion is a no-op. */
4628 if (status == GOT_STATUS_DELETE &&
4629 staged_status != GOT_STATUS_NO_CHANGE)
4630 return NULL;
4632 if (status == GOT_STATUS_UNVERSIONED)
4633 return (*a->progress_cb)(a->progress_arg,
4634 GOT_STATUS_UNVERSIONED, relpath);
4636 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4637 if (ie == NULL)
4638 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4640 /* Construct in-repository path of tree which contains this blob. */
4641 err = got_path_dirname(&parent_path, ie->path);
4642 if (err) {
4643 if (err->code != GOT_ERR_BAD_PATH)
4644 goto done;
4645 parent_path = strdup("/");
4646 if (parent_path == NULL) {
4647 err = got_error_from_errno("strdup");
4648 goto done;
4651 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4652 tree_path = strdup(parent_path);
4653 if (tree_path == NULL) {
4654 err = got_error_from_errno("strdup");
4655 goto done;
4657 } else {
4658 if (got_path_is_root_dir(parent_path)) {
4659 tree_path = strdup(a->worktree->path_prefix);
4660 if (tree_path == NULL) {
4661 err = got_error_from_errno("strdup");
4662 goto done;
4664 } else {
4665 if (asprintf(&tree_path, "%s/%s",
4666 a->worktree->path_prefix, parent_path) == -1) {
4667 err = got_error_from_errno("asprintf");
4668 goto done;
4673 err = got_object_open_as_commit(&base_commit, a->repo,
4674 a->worktree->base_commit_id);
4675 if (err)
4676 goto done;
4678 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4679 if (err) {
4680 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4681 (status == GOT_STATUS_ADD ||
4682 staged_status == GOT_STATUS_ADD)))
4683 goto done;
4684 } else {
4685 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4686 if (err)
4687 goto done;
4689 err = got_path_basename(&te_name, ie->path);
4690 if (err)
4691 goto done;
4693 te = got_object_tree_find_entry(tree, te_name);
4694 free(te_name);
4695 if (te == NULL && status != GOT_STATUS_ADD &&
4696 staged_status != GOT_STATUS_ADD) {
4697 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4698 goto done;
4702 switch (status) {
4703 case GOT_STATUS_ADD:
4704 if (a->patch_cb) {
4705 int choice = GOT_PATCH_CHOICE_NONE;
4706 err = (*a->patch_cb)(&choice, a->patch_arg,
4707 status, ie->path, NULL, 1, 1);
4708 if (err)
4709 goto done;
4710 if (choice != GOT_PATCH_CHOICE_YES)
4711 break;
4713 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4714 ie->path);
4715 if (err)
4716 goto done;
4717 got_fileindex_entry_remove(a->fileindex, ie);
4718 if (a->unlink_added_files) {
4719 if (asprintf(&ondisk_path, "%s/%s",
4720 got_worktree_get_root_path(a->worktree),
4721 relpath) == -1) {
4722 err = got_error_from_errno("asprintf");
4723 goto done;
4725 if (unlink(ondisk_path) == -1) {
4726 err = got_error_from_errno2("unlink",
4727 ondisk_path);
4728 break;
4731 break;
4732 case GOT_STATUS_DELETE:
4733 if (a->patch_cb) {
4734 int choice = GOT_PATCH_CHOICE_NONE;
4735 err = (*a->patch_cb)(&choice, a->patch_arg,
4736 status, ie->path, NULL, 1, 1);
4737 if (err)
4738 goto done;
4739 if (choice != GOT_PATCH_CHOICE_YES)
4740 break;
4742 /* fall through */
4743 case GOT_STATUS_MODIFY:
4744 case GOT_STATUS_MODE_CHANGE:
4745 case GOT_STATUS_CONFLICT:
4746 case GOT_STATUS_MISSING: {
4747 struct got_object_id id;
4748 if (staged_status == GOT_STATUS_ADD ||
4749 staged_status == GOT_STATUS_MODIFY) {
4750 memcpy(id.sha1, ie->staged_blob_sha1,
4751 SHA1_DIGEST_LENGTH);
4752 } else
4753 memcpy(id.sha1, ie->blob_sha1,
4754 SHA1_DIGEST_LENGTH);
4755 fd = got_opentempfd();
4756 if (fd == -1) {
4757 err = got_error_from_errno("got_opentempfd");
4758 goto done;
4761 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4762 if (err)
4763 goto done;
4765 if (asprintf(&ondisk_path, "%s/%s",
4766 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4767 err = got_error_from_errno("asprintf");
4768 goto done;
4771 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4772 status == GOT_STATUS_CONFLICT)) {
4773 int is_bad_symlink = 0;
4774 err = create_patched_content(&path_content, 1, &id,
4775 ondisk_path, dirfd, de_name, ie->path, a->repo,
4776 a->patch_cb, a->patch_arg);
4777 if (err || path_content == NULL)
4778 break;
4779 if (te && S_ISLNK(te->mode)) {
4780 if (unlink(path_content) == -1) {
4781 err = got_error_from_errno2("unlink",
4782 path_content);
4783 break;
4785 err = install_symlink(&is_bad_symlink,
4786 a->worktree, ondisk_path, ie->path,
4787 blob, 0, 1, 0, 0, a->repo,
4788 a->progress_cb, a->progress_arg);
4789 } else {
4790 if (rename(path_content, ondisk_path) == -1) {
4791 err = got_error_from_errno3("rename",
4792 path_content, ondisk_path);
4793 goto done;
4796 } else {
4797 int is_bad_symlink = 0;
4798 if (te && S_ISLNK(te->mode)) {
4799 err = install_symlink(&is_bad_symlink,
4800 a->worktree, ondisk_path, ie->path,
4801 blob, 0, 1, 0, 0, a->repo,
4802 a->progress_cb, a->progress_arg);
4803 } else {
4804 err = install_blob(a->worktree, ondisk_path,
4805 ie->path,
4806 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4807 got_fileindex_perms_to_st(ie), blob,
4808 0, 1, 0, 0, a->repo,
4809 a->progress_cb, a->progress_arg);
4811 if (err)
4812 goto done;
4813 if (status == GOT_STATUS_DELETE ||
4814 status == GOT_STATUS_MODE_CHANGE) {
4815 err = got_fileindex_entry_update(ie,
4816 a->worktree->root_fd, relpath,
4817 blob->id.sha1,
4818 a->worktree->base_commit_id->sha1, 1);
4819 if (err)
4820 goto done;
4822 if (is_bad_symlink) {
4823 got_fileindex_entry_filetype_set(ie,
4824 GOT_FILEIDX_MODE_BAD_SYMLINK);
4827 break;
4829 default:
4830 break;
4832 done:
4833 free(ondisk_path);
4834 free(path_content);
4835 free(parent_path);
4836 free(tree_path);
4837 if (fd != -1 && close(fd) == -1 && err == NULL)
4838 err = got_error_from_errno("close");
4839 if (blob)
4840 got_object_blob_close(blob);
4841 if (tree)
4842 got_object_tree_close(tree);
4843 free(tree_id);
4844 if (base_commit)
4845 got_object_commit_close(base_commit);
4846 return err;
4849 const struct got_error *
4850 got_worktree_revert(struct got_worktree *worktree,
4851 struct got_pathlist_head *paths,
4852 got_worktree_checkout_cb progress_cb, void *progress_arg,
4853 got_worktree_patch_cb patch_cb, void *patch_arg,
4854 struct got_repository *repo)
4856 struct got_fileindex *fileindex = NULL;
4857 char *fileindex_path = NULL;
4858 const struct got_error *err = NULL, *unlockerr = NULL;
4859 const struct got_error *sync_err = NULL;
4860 struct got_pathlist_entry *pe;
4861 struct revert_file_args rfa;
4863 err = lock_worktree(worktree, LOCK_EX);
4864 if (err)
4865 return err;
4867 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4868 if (err)
4869 goto done;
4871 rfa.worktree = worktree;
4872 rfa.fileindex = fileindex;
4873 rfa.progress_cb = progress_cb;
4874 rfa.progress_arg = progress_arg;
4875 rfa.patch_cb = patch_cb;
4876 rfa.patch_arg = patch_arg;
4877 rfa.repo = repo;
4878 rfa.unlink_added_files = 0;
4879 TAILQ_FOREACH(pe, paths, entry) {
4880 err = worktree_status(worktree, pe->path, fileindex, repo,
4881 revert_file, &rfa, NULL, NULL, 1, 0);
4882 if (err)
4883 break;
4885 sync_err = sync_fileindex(fileindex, fileindex_path);
4886 if (sync_err && err == NULL)
4887 err = sync_err;
4888 done:
4889 free(fileindex_path);
4890 if (fileindex)
4891 got_fileindex_free(fileindex);
4892 unlockerr = lock_worktree(worktree, LOCK_SH);
4893 if (unlockerr && err == NULL)
4894 err = unlockerr;
4895 return err;
4898 static void
4899 free_commitable(struct got_commitable *ct)
4901 free(ct->path);
4902 free(ct->in_repo_path);
4903 free(ct->ondisk_path);
4904 free(ct->blob_id);
4905 free(ct->base_blob_id);
4906 free(ct->staged_blob_id);
4907 free(ct->base_commit_id);
4908 free(ct);
4911 struct collect_commitables_arg {
4912 struct got_pathlist_head *commitable_paths;
4913 struct got_repository *repo;
4914 struct got_worktree *worktree;
4915 struct got_fileindex *fileindex;
4916 int have_staged_files;
4917 int allow_bad_symlinks;
4920 static const struct got_error *
4921 collect_commitables(void *arg, unsigned char status,
4922 unsigned char staged_status, const char *relpath,
4923 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4924 struct got_object_id *commit_id, int dirfd, const char *de_name)
4926 struct collect_commitables_arg *a = arg;
4927 const struct got_error *err = NULL;
4928 struct got_commitable *ct = NULL;
4929 struct got_pathlist_entry *new = NULL;
4930 char *parent_path = NULL, *path = NULL;
4931 struct stat sb;
4933 if (a->have_staged_files) {
4934 if (staged_status != GOT_STATUS_MODIFY &&
4935 staged_status != GOT_STATUS_ADD &&
4936 staged_status != GOT_STATUS_DELETE)
4937 return NULL;
4938 } else {
4939 if (status == GOT_STATUS_CONFLICT)
4940 return got_error(GOT_ERR_COMMIT_CONFLICT);
4942 if (status != GOT_STATUS_MODIFY &&
4943 status != GOT_STATUS_MODE_CHANGE &&
4944 status != GOT_STATUS_ADD &&
4945 status != GOT_STATUS_DELETE)
4946 return NULL;
4949 if (asprintf(&path, "/%s", relpath) == -1) {
4950 err = got_error_from_errno("asprintf");
4951 goto done;
4953 if (strcmp(path, "/") == 0) {
4954 parent_path = strdup("");
4955 if (parent_path == NULL)
4956 return got_error_from_errno("strdup");
4957 } else {
4958 err = got_path_dirname(&parent_path, path);
4959 if (err)
4960 return err;
4963 ct = calloc(1, sizeof(*ct));
4964 if (ct == NULL) {
4965 err = got_error_from_errno("calloc");
4966 goto done;
4969 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4970 relpath) == -1) {
4971 err = got_error_from_errno("asprintf");
4972 goto done;
4975 if (staged_status == GOT_STATUS_ADD ||
4976 staged_status == GOT_STATUS_MODIFY) {
4977 struct got_fileindex_entry *ie;
4978 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4979 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4980 case GOT_FILEIDX_MODE_REGULAR_FILE:
4981 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4982 ct->mode = S_IFREG;
4983 break;
4984 case GOT_FILEIDX_MODE_SYMLINK:
4985 ct->mode = S_IFLNK;
4986 break;
4987 default:
4988 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4989 goto done;
4991 ct->mode |= got_fileindex_entry_perms_get(ie);
4992 } else if (status != GOT_STATUS_DELETE &&
4993 staged_status != GOT_STATUS_DELETE) {
4994 if (dirfd != -1) {
4995 if (fstatat(dirfd, de_name, &sb,
4996 AT_SYMLINK_NOFOLLOW) == -1) {
4997 err = got_error_from_errno2("fstatat",
4998 ct->ondisk_path);
4999 goto done;
5001 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5002 err = got_error_from_errno2("lstat", ct->ondisk_path);
5003 goto done;
5005 ct->mode = sb.st_mode;
5008 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5009 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5010 relpath) == -1) {
5011 err = got_error_from_errno("asprintf");
5012 goto done;
5015 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5016 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5017 int is_bad_symlink;
5018 char target_path[PATH_MAX];
5019 ssize_t target_len;
5020 target_len = readlink(ct->ondisk_path, target_path,
5021 sizeof(target_path));
5022 if (target_len == -1) {
5023 err = got_error_from_errno2("readlink",
5024 ct->ondisk_path);
5025 goto done;
5027 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5028 target_len, ct->ondisk_path, a->worktree->root_path);
5029 if (err)
5030 goto done;
5031 if (is_bad_symlink) {
5032 err = got_error_path(ct->ondisk_path,
5033 GOT_ERR_BAD_SYMLINK);
5034 goto done;
5039 ct->status = status;
5040 ct->staged_status = staged_status;
5041 ct->blob_id = NULL; /* will be filled in when blob gets created */
5042 if (ct->status != GOT_STATUS_ADD &&
5043 ct->staged_status != GOT_STATUS_ADD) {
5044 ct->base_blob_id = got_object_id_dup(blob_id);
5045 if (ct->base_blob_id == NULL) {
5046 err = got_error_from_errno("got_object_id_dup");
5047 goto done;
5049 ct->base_commit_id = got_object_id_dup(commit_id);
5050 if (ct->base_commit_id == NULL) {
5051 err = got_error_from_errno("got_object_id_dup");
5052 goto done;
5055 if (ct->staged_status == GOT_STATUS_ADD ||
5056 ct->staged_status == GOT_STATUS_MODIFY) {
5057 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5058 if (ct->staged_blob_id == NULL) {
5059 err = got_error_from_errno("got_object_id_dup");
5060 goto done;
5063 ct->path = strdup(path);
5064 if (ct->path == NULL) {
5065 err = got_error_from_errno("strdup");
5066 goto done;
5068 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5069 done:
5070 if (ct && (err || new == NULL))
5071 free_commitable(ct);
5072 free(parent_path);
5073 free(path);
5074 return err;
5077 static const struct got_error *write_tree(struct got_object_id **, int *,
5078 struct got_tree_object *, const char *, struct got_pathlist_head *,
5079 got_worktree_status_cb status_cb, void *status_arg,
5080 struct got_repository *);
5082 static const struct got_error *
5083 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5084 struct got_tree_entry *te, const char *parent_path,
5085 struct got_pathlist_head *commitable_paths,
5086 got_worktree_status_cb status_cb, void *status_arg,
5087 struct got_repository *repo)
5089 const struct got_error *err = NULL;
5090 struct got_tree_object *subtree;
5091 char *subpath;
5093 if (asprintf(&subpath, "%s%s%s", parent_path,
5094 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5095 return got_error_from_errno("asprintf");
5097 err = got_object_open_as_tree(&subtree, repo, &te->id);
5098 if (err)
5099 return err;
5101 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5102 commitable_paths, status_cb, status_arg, repo);
5103 got_object_tree_close(subtree);
5104 free(subpath);
5105 return err;
5108 static const struct got_error *
5109 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5111 const struct got_error *err = NULL;
5112 char *ct_parent_path = NULL;
5114 *match = 0;
5116 if (strchr(ct->in_repo_path, '/') == NULL) {
5117 *match = got_path_is_root_dir(path);
5118 return NULL;
5121 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5122 if (err)
5123 return err;
5124 *match = (strcmp(path, ct_parent_path) == 0);
5125 free(ct_parent_path);
5126 return err;
5129 static mode_t
5130 get_ct_file_mode(struct got_commitable *ct)
5132 if (S_ISLNK(ct->mode))
5133 return S_IFLNK;
5135 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5138 static const struct got_error *
5139 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5140 struct got_tree_entry *te, struct got_commitable *ct)
5142 const struct got_error *err = NULL;
5144 *new_te = NULL;
5146 err = got_object_tree_entry_dup(new_te, te);
5147 if (err)
5148 goto done;
5150 (*new_te)->mode = get_ct_file_mode(ct);
5152 if (ct->staged_status == GOT_STATUS_MODIFY)
5153 memcpy(&(*new_te)->id, ct->staged_blob_id,
5154 sizeof((*new_te)->id));
5155 else
5156 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5157 done:
5158 if (err && *new_te) {
5159 free(*new_te);
5160 *new_te = NULL;
5162 return err;
5165 static const struct got_error *
5166 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5167 struct got_commitable *ct)
5169 const struct got_error *err = NULL;
5170 char *ct_name = NULL;
5172 *new_te = NULL;
5174 *new_te = calloc(1, sizeof(**new_te));
5175 if (*new_te == NULL)
5176 return got_error_from_errno("calloc");
5178 err = got_path_basename(&ct_name, ct->path);
5179 if (err)
5180 goto done;
5181 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5182 sizeof((*new_te)->name)) {
5183 err = got_error(GOT_ERR_NO_SPACE);
5184 goto done;
5187 (*new_te)->mode = get_ct_file_mode(ct);
5189 if (ct->staged_status == GOT_STATUS_ADD)
5190 memcpy(&(*new_te)->id, ct->staged_blob_id,
5191 sizeof((*new_te)->id));
5192 else
5193 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5194 done:
5195 free(ct_name);
5196 if (err && *new_te) {
5197 free(*new_te);
5198 *new_te = NULL;
5200 return err;
5203 static const struct got_error *
5204 insert_tree_entry(struct got_tree_entry *new_te,
5205 struct got_pathlist_head *paths)
5207 const struct got_error *err = NULL;
5208 struct got_pathlist_entry *new_pe;
5210 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5211 if (err)
5212 return err;
5213 if (new_pe == NULL)
5214 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5215 return NULL;
5218 static const struct got_error *
5219 report_ct_status(struct got_commitable *ct,
5220 got_worktree_status_cb status_cb, void *status_arg)
5222 const char *ct_path = ct->path;
5223 unsigned char status;
5225 if (status_cb == NULL) /* no commit progress output desired */
5226 return NULL;
5228 while (ct_path[0] == '/')
5229 ct_path++;
5231 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5232 status = ct->staged_status;
5233 else
5234 status = ct->status;
5236 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5237 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5240 static const struct got_error *
5241 match_modified_subtree(int *modified, struct got_tree_entry *te,
5242 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5244 const struct got_error *err = NULL;
5245 struct got_pathlist_entry *pe;
5246 char *te_path;
5248 *modified = 0;
5250 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5251 got_path_is_root_dir(base_tree_path) ? "" : "/",
5252 te->name) == -1)
5253 return got_error_from_errno("asprintf");
5255 TAILQ_FOREACH(pe, commitable_paths, entry) {
5256 struct got_commitable *ct = pe->data;
5257 *modified = got_path_is_child(ct->in_repo_path, te_path,
5258 strlen(te_path));
5259 if (*modified)
5260 break;
5263 free(te_path);
5264 return err;
5267 static const struct got_error *
5268 match_deleted_or_modified_ct(struct got_commitable **ctp,
5269 struct got_tree_entry *te, const char *base_tree_path,
5270 struct got_pathlist_head *commitable_paths)
5272 const struct got_error *err = NULL;
5273 struct got_pathlist_entry *pe;
5275 *ctp = NULL;
5277 TAILQ_FOREACH(pe, commitable_paths, entry) {
5278 struct got_commitable *ct = pe->data;
5279 char *ct_name = NULL;
5280 int path_matches;
5282 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5283 if (ct->status != GOT_STATUS_MODIFY &&
5284 ct->status != GOT_STATUS_MODE_CHANGE &&
5285 ct->status != GOT_STATUS_DELETE)
5286 continue;
5287 } else {
5288 if (ct->staged_status != GOT_STATUS_MODIFY &&
5289 ct->staged_status != GOT_STATUS_DELETE)
5290 continue;
5293 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5294 continue;
5296 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5297 if (err)
5298 return err;
5299 if (!path_matches)
5300 continue;
5302 err = got_path_basename(&ct_name, pe->path);
5303 if (err)
5304 return err;
5306 if (strcmp(te->name, ct_name) != 0) {
5307 free(ct_name);
5308 continue;
5310 free(ct_name);
5312 *ctp = ct;
5313 break;
5316 return err;
5319 static const struct got_error *
5320 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5321 const char *child_path, const char *path_base_tree,
5322 struct got_pathlist_head *commitable_paths,
5323 got_worktree_status_cb status_cb, void *status_arg,
5324 struct got_repository *repo)
5326 const struct got_error *err = NULL;
5327 struct got_tree_entry *new_te;
5328 char *subtree_path;
5329 struct got_object_id *id = NULL;
5330 int nentries;
5332 *new_tep = NULL;
5334 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5335 got_path_is_root_dir(path_base_tree) ? "" : "/",
5336 child_path) == -1)
5337 return got_error_from_errno("asprintf");
5339 new_te = calloc(1, sizeof(*new_te));
5340 if (new_te == NULL)
5341 return got_error_from_errno("calloc");
5342 new_te->mode = S_IFDIR;
5344 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5345 sizeof(new_te->name)) {
5346 err = got_error(GOT_ERR_NO_SPACE);
5347 goto done;
5349 err = write_tree(&id, &nentries, NULL, subtree_path,
5350 commitable_paths, status_cb, status_arg, repo);
5351 if (err) {
5352 free(new_te);
5353 goto done;
5355 memcpy(&new_te->id, id, sizeof(new_te->id));
5356 done:
5357 free(id);
5358 free(subtree_path);
5359 if (err == NULL)
5360 *new_tep = new_te;
5361 return err;
5364 static const struct got_error *
5365 write_tree(struct got_object_id **new_tree_id, int *nentries,
5366 struct got_tree_object *base_tree, const char *path_base_tree,
5367 struct got_pathlist_head *commitable_paths,
5368 got_worktree_status_cb status_cb, void *status_arg,
5369 struct got_repository *repo)
5371 const struct got_error *err = NULL;
5372 struct got_pathlist_head paths;
5373 struct got_tree_entry *te, *new_te = NULL;
5374 struct got_pathlist_entry *pe;
5376 TAILQ_INIT(&paths);
5377 *nentries = 0;
5379 /* Insert, and recurse into, newly added entries first. */
5380 TAILQ_FOREACH(pe, commitable_paths, entry) {
5381 struct got_commitable *ct = pe->data;
5382 char *child_path = NULL, *slash;
5384 if ((ct->status != GOT_STATUS_ADD &&
5385 ct->staged_status != GOT_STATUS_ADD) ||
5386 (ct->flags & GOT_COMMITABLE_ADDED))
5387 continue;
5389 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5390 strlen(path_base_tree)))
5391 continue;
5393 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5394 ct->in_repo_path);
5395 if (err)
5396 goto done;
5398 slash = strchr(child_path, '/');
5399 if (slash == NULL) {
5400 err = alloc_added_blob_tree_entry(&new_te, ct);
5401 if (err)
5402 goto done;
5403 err = report_ct_status(ct, status_cb, status_arg);
5404 if (err)
5405 goto done;
5406 ct->flags |= GOT_COMMITABLE_ADDED;
5407 err = insert_tree_entry(new_te, &paths);
5408 if (err)
5409 goto done;
5410 (*nentries)++;
5411 } else {
5412 *slash = '\0'; /* trim trailing path components */
5413 if (base_tree == NULL ||
5414 got_object_tree_find_entry(base_tree, child_path)
5415 == NULL) {
5416 err = make_subtree_for_added_blob(&new_te,
5417 child_path, path_base_tree,
5418 commitable_paths, status_cb, status_arg,
5419 repo);
5420 if (err)
5421 goto done;
5422 err = insert_tree_entry(new_te, &paths);
5423 if (err)
5424 goto done;
5425 (*nentries)++;
5430 if (base_tree) {
5431 int i, nbase_entries;
5432 /* Handle modified and deleted entries. */
5433 nbase_entries = got_object_tree_get_nentries(base_tree);
5434 for (i = 0; i < nbase_entries; i++) {
5435 struct got_commitable *ct = NULL;
5437 te = got_object_tree_get_entry(base_tree, i);
5438 if (got_object_tree_entry_is_submodule(te)) {
5439 /* Entry is a submodule; just copy it. */
5440 err = got_object_tree_entry_dup(&new_te, te);
5441 if (err)
5442 goto done;
5443 err = insert_tree_entry(new_te, &paths);
5444 if (err)
5445 goto done;
5446 (*nentries)++;
5447 continue;
5450 if (S_ISDIR(te->mode)) {
5451 int modified;
5452 err = got_object_tree_entry_dup(&new_te, te);
5453 if (err)
5454 goto done;
5455 err = match_modified_subtree(&modified, te,
5456 path_base_tree, commitable_paths);
5457 if (err)
5458 goto done;
5459 /* Avoid recursion into unmodified subtrees. */
5460 if (modified) {
5461 struct got_object_id *new_id;
5462 int nsubentries;
5463 err = write_subtree(&new_id,
5464 &nsubentries, te,
5465 path_base_tree, commitable_paths,
5466 status_cb, status_arg, repo);
5467 if (err)
5468 goto done;
5469 if (nsubentries == 0) {
5470 /* All entries were deleted. */
5471 free(new_id);
5472 continue;
5474 memcpy(&new_te->id, new_id,
5475 sizeof(new_te->id));
5476 free(new_id);
5478 err = insert_tree_entry(new_te, &paths);
5479 if (err)
5480 goto done;
5481 (*nentries)++;
5482 continue;
5485 err = match_deleted_or_modified_ct(&ct, te,
5486 path_base_tree, commitable_paths);
5487 if (err)
5488 goto done;
5489 if (ct) {
5490 /* NB: Deleted entries get dropped here. */
5491 if (ct->status == GOT_STATUS_MODIFY ||
5492 ct->status == GOT_STATUS_MODE_CHANGE ||
5493 ct->staged_status == GOT_STATUS_MODIFY) {
5494 err = alloc_modified_blob_tree_entry(
5495 &new_te, te, ct);
5496 if (err)
5497 goto done;
5498 err = insert_tree_entry(new_te, &paths);
5499 if (err)
5500 goto done;
5501 (*nentries)++;
5503 err = report_ct_status(ct, status_cb,
5504 status_arg);
5505 if (err)
5506 goto done;
5507 } else {
5508 /* Entry is unchanged; just copy it. */
5509 err = got_object_tree_entry_dup(&new_te, te);
5510 if (err)
5511 goto done;
5512 err = insert_tree_entry(new_te, &paths);
5513 if (err)
5514 goto done;
5515 (*nentries)++;
5520 /* Write new list of entries; deleted entries have been dropped. */
5521 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5522 done:
5523 got_pathlist_free(&paths);
5524 return err;
5527 static const struct got_error *
5528 update_fileindex_after_commit(struct got_worktree *worktree,
5529 struct got_pathlist_head *commitable_paths,
5530 struct got_object_id *new_base_commit_id,
5531 struct got_fileindex *fileindex, int have_staged_files)
5533 const struct got_error *err = NULL;
5534 struct got_pathlist_entry *pe;
5535 char *relpath = NULL;
5537 TAILQ_FOREACH(pe, commitable_paths, entry) {
5538 struct got_fileindex_entry *ie;
5539 struct got_commitable *ct = pe->data;
5541 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5543 err = got_path_skip_common_ancestor(&relpath,
5544 worktree->root_path, ct->ondisk_path);
5545 if (err)
5546 goto done;
5548 if (ie) {
5549 if (ct->status == GOT_STATUS_DELETE ||
5550 ct->staged_status == GOT_STATUS_DELETE) {
5551 got_fileindex_entry_remove(fileindex, ie);
5552 } else if (ct->staged_status == GOT_STATUS_ADD ||
5553 ct->staged_status == GOT_STATUS_MODIFY) {
5554 got_fileindex_entry_stage_set(ie,
5555 GOT_FILEIDX_STAGE_NONE);
5556 got_fileindex_entry_staged_filetype_set(ie, 0);
5558 err = got_fileindex_entry_update(ie,
5559 worktree->root_fd, relpath,
5560 ct->staged_blob_id->sha1,
5561 new_base_commit_id->sha1,
5562 !have_staged_files);
5563 } else
5564 err = got_fileindex_entry_update(ie,
5565 worktree->root_fd, relpath,
5566 ct->blob_id->sha1,
5567 new_base_commit_id->sha1,
5568 !have_staged_files);
5569 } else {
5570 err = got_fileindex_entry_alloc(&ie, pe->path);
5571 if (err)
5572 goto done;
5573 err = got_fileindex_entry_update(ie,
5574 worktree->root_fd, relpath, ct->blob_id->sha1,
5575 new_base_commit_id->sha1, 1);
5576 if (err) {
5577 got_fileindex_entry_free(ie);
5578 goto done;
5580 err = got_fileindex_entry_add(fileindex, ie);
5581 if (err) {
5582 got_fileindex_entry_free(ie);
5583 goto done;
5586 free(relpath);
5587 relpath = NULL;
5589 done:
5590 free(relpath);
5591 return err;
5595 static const struct got_error *
5596 check_out_of_date(const char *in_repo_path, unsigned char status,
5597 unsigned char staged_status, struct got_object_id *base_blob_id,
5598 struct got_object_id *base_commit_id,
5599 struct got_object_id *head_commit_id, struct got_repository *repo,
5600 int ood_errcode)
5602 const struct got_error *err = NULL;
5603 struct got_commit_object *commit = NULL;
5604 struct got_object_id *id = NULL;
5606 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5607 /* Trivial case: base commit == head commit */
5608 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5609 return NULL;
5611 * Ensure file content which local changes were based
5612 * on matches file content in the branch head.
5614 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5615 if (err)
5616 goto done;
5617 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5618 if (err) {
5619 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5620 err = got_error(ood_errcode);
5621 goto done;
5622 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5623 err = got_error(ood_errcode);
5624 } else {
5625 /* Require that added files don't exist in the branch head. */
5626 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5627 if (err)
5628 goto done;
5629 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5630 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5631 goto done;
5632 err = id ? got_error(ood_errcode) : NULL;
5634 done:
5635 free(id);
5636 if (commit)
5637 got_object_commit_close(commit);
5638 return err;
5641 static const struct got_error *
5642 commit_worktree(struct got_object_id **new_commit_id,
5643 struct got_pathlist_head *commitable_paths,
5644 struct got_object_id *head_commit_id,
5645 struct got_object_id *parent_id2,
5646 struct got_worktree *worktree,
5647 const char *author, const char *committer,
5648 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5649 got_worktree_status_cb status_cb, void *status_arg,
5650 struct got_repository *repo)
5652 const struct got_error *err = NULL, *unlockerr = NULL;
5653 struct got_pathlist_entry *pe;
5654 const char *head_ref_name = NULL;
5655 struct got_commit_object *head_commit = NULL;
5656 struct got_reference *head_ref2 = NULL;
5657 struct got_object_id *head_commit_id2 = NULL;
5658 struct got_tree_object *head_tree = NULL;
5659 struct got_object_id *new_tree_id = NULL;
5660 int nentries, nparents = 0;
5661 struct got_object_id_queue parent_ids;
5662 struct got_object_qid *pid = NULL;
5663 char *logmsg = NULL;
5665 *new_commit_id = NULL;
5667 STAILQ_INIT(&parent_ids);
5669 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5670 if (err)
5671 goto done;
5673 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5674 if (err)
5675 goto done;
5677 if (commit_msg_cb != NULL) {
5678 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5679 if (err)
5680 goto done;
5683 if (logmsg == NULL || strlen(logmsg) == 0) {
5684 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5685 goto done;
5688 /* Create blobs from added and modified files and record their IDs. */
5689 TAILQ_FOREACH(pe, commitable_paths, entry) {
5690 struct got_commitable *ct = pe->data;
5691 char *ondisk_path;
5693 /* Blobs for staged files already exist. */
5694 if (ct->staged_status == GOT_STATUS_ADD ||
5695 ct->staged_status == GOT_STATUS_MODIFY)
5696 continue;
5698 if (ct->status != GOT_STATUS_ADD &&
5699 ct->status != GOT_STATUS_MODIFY &&
5700 ct->status != GOT_STATUS_MODE_CHANGE)
5701 continue;
5703 if (asprintf(&ondisk_path, "%s/%s",
5704 worktree->root_path, pe->path) == -1) {
5705 err = got_error_from_errno("asprintf");
5706 goto done;
5708 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5709 free(ondisk_path);
5710 if (err)
5711 goto done;
5714 /* Recursively write new tree objects. */
5715 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5716 commitable_paths, status_cb, status_arg, repo);
5717 if (err)
5718 goto done;
5720 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5721 if (err)
5722 goto done;
5723 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5724 nparents++;
5725 if (parent_id2) {
5726 err = got_object_qid_alloc(&pid, parent_id2);
5727 if (err)
5728 goto done;
5729 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5730 nparents++;
5732 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5733 nparents, author, time(NULL), committer, time(NULL), logmsg, repo);
5734 if (logmsg != NULL)
5735 free(logmsg);
5736 if (err)
5737 goto done;
5739 /* Check if a concurrent commit to our branch has occurred. */
5740 head_ref_name = got_worktree_get_head_ref_name(worktree);
5741 if (head_ref_name == NULL) {
5742 err = got_error_from_errno("got_worktree_get_head_ref_name");
5743 goto done;
5745 /* Lock the reference here to prevent concurrent modification. */
5746 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5747 if (err)
5748 goto done;
5749 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5750 if (err)
5751 goto done;
5752 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5753 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5754 goto done;
5756 /* Update branch head in repository. */
5757 err = got_ref_change_ref(head_ref2, *new_commit_id);
5758 if (err)
5759 goto done;
5760 err = got_ref_write(head_ref2, repo);
5761 if (err)
5762 goto done;
5764 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5765 if (err)
5766 goto done;
5768 err = ref_base_commit(worktree, repo);
5769 if (err)
5770 goto done;
5771 done:
5772 got_object_id_queue_free(&parent_ids);
5773 if (head_tree)
5774 got_object_tree_close(head_tree);
5775 if (head_commit)
5776 got_object_commit_close(head_commit);
5777 free(head_commit_id2);
5778 if (head_ref2) {
5779 unlockerr = got_ref_unlock(head_ref2);
5780 if (unlockerr && err == NULL)
5781 err = unlockerr;
5782 got_ref_close(head_ref2);
5784 return err;
5787 static const struct got_error *
5788 check_path_is_commitable(const char *path,
5789 struct got_pathlist_head *commitable_paths)
5791 struct got_pathlist_entry *cpe = NULL;
5792 size_t path_len = strlen(path);
5794 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5795 struct got_commitable *ct = cpe->data;
5796 const char *ct_path = ct->path;
5798 while (ct_path[0] == '/')
5799 ct_path++;
5801 if (strcmp(path, ct_path) == 0 ||
5802 got_path_is_child(ct_path, path, path_len))
5803 break;
5806 if (cpe == NULL)
5807 return got_error_path(path, GOT_ERR_BAD_PATH);
5809 return NULL;
5812 static const struct got_error *
5813 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5815 int *have_staged_files = arg;
5817 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5818 *have_staged_files = 1;
5819 return got_error(GOT_ERR_CANCELLED);
5822 return NULL;
5825 static const struct got_error *
5826 check_non_staged_files(struct got_fileindex *fileindex,
5827 struct got_pathlist_head *paths)
5829 struct got_pathlist_entry *pe;
5830 struct got_fileindex_entry *ie;
5832 TAILQ_FOREACH(pe, paths, entry) {
5833 if (pe->path[0] == '\0')
5834 continue;
5835 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5836 if (ie == NULL)
5837 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5838 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5839 return got_error_path(pe->path,
5840 GOT_ERR_FILE_NOT_STAGED);
5843 return NULL;
5846 const struct got_error *
5847 got_worktree_commit(struct got_object_id **new_commit_id,
5848 struct got_worktree *worktree, struct got_pathlist_head *paths,
5849 const char *author, const char *committer, int allow_bad_symlinks,
5850 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5851 got_worktree_status_cb status_cb, void *status_arg,
5852 struct got_repository *repo)
5854 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5855 struct got_fileindex *fileindex = NULL;
5856 char *fileindex_path = NULL;
5857 struct got_pathlist_head commitable_paths;
5858 struct collect_commitables_arg cc_arg;
5859 struct got_pathlist_entry *pe;
5860 struct got_reference *head_ref = NULL;
5861 struct got_object_id *head_commit_id = NULL;
5862 int have_staged_files = 0;
5864 *new_commit_id = NULL;
5866 TAILQ_INIT(&commitable_paths);
5868 err = lock_worktree(worktree, LOCK_EX);
5869 if (err)
5870 goto done;
5872 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5873 if (err)
5874 goto done;
5876 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5877 if (err)
5878 goto done;
5880 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5881 if (err)
5882 goto done;
5884 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5885 &have_staged_files);
5886 if (err && err->code != GOT_ERR_CANCELLED)
5887 goto done;
5888 if (have_staged_files) {
5889 err = check_non_staged_files(fileindex, paths);
5890 if (err)
5891 goto done;
5894 cc_arg.commitable_paths = &commitable_paths;
5895 cc_arg.worktree = worktree;
5896 cc_arg.fileindex = fileindex;
5897 cc_arg.repo = repo;
5898 cc_arg.have_staged_files = have_staged_files;
5899 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5900 TAILQ_FOREACH(pe, paths, entry) {
5901 err = worktree_status(worktree, pe->path, fileindex, repo,
5902 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
5903 if (err)
5904 goto done;
5907 if (TAILQ_EMPTY(&commitable_paths)) {
5908 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5909 goto done;
5912 TAILQ_FOREACH(pe, paths, entry) {
5913 err = check_path_is_commitable(pe->path, &commitable_paths);
5914 if (err)
5915 goto done;
5918 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5919 struct got_commitable *ct = pe->data;
5920 const char *ct_path = ct->in_repo_path;
5922 while (ct_path[0] == '/')
5923 ct_path++;
5924 err = check_out_of_date(ct_path, ct->status,
5925 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5926 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5927 if (err)
5928 goto done;
5932 err = commit_worktree(new_commit_id, &commitable_paths,
5933 head_commit_id, NULL, worktree, author, committer,
5934 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5935 if (err)
5936 goto done;
5938 err = update_fileindex_after_commit(worktree, &commitable_paths,
5939 *new_commit_id, fileindex, have_staged_files);
5940 sync_err = sync_fileindex(fileindex, fileindex_path);
5941 if (sync_err && err == NULL)
5942 err = sync_err;
5943 done:
5944 if (fileindex)
5945 got_fileindex_free(fileindex);
5946 free(fileindex_path);
5947 unlockerr = lock_worktree(worktree, LOCK_SH);
5948 if (unlockerr && err == NULL)
5949 err = unlockerr;
5950 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5951 struct got_commitable *ct = pe->data;
5952 free_commitable(ct);
5954 got_pathlist_free(&commitable_paths);
5955 return err;
5958 const char *
5959 got_commitable_get_path(struct got_commitable *ct)
5961 return ct->path;
5964 unsigned int
5965 got_commitable_get_status(struct got_commitable *ct)
5967 return ct->status;
5970 struct check_rebase_ok_arg {
5971 struct got_worktree *worktree;
5972 struct got_repository *repo;
5975 static const struct got_error *
5976 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5978 const struct got_error *err = NULL;
5979 struct check_rebase_ok_arg *a = arg;
5980 unsigned char status;
5981 struct stat sb;
5982 char *ondisk_path;
5984 /* Reject rebase of a work tree with mixed base commits. */
5985 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5986 SHA1_DIGEST_LENGTH))
5987 return got_error(GOT_ERR_MIXED_COMMITS);
5989 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5990 == -1)
5991 return got_error_from_errno("asprintf");
5993 /* Reject rebase of a work tree with modified or staged files. */
5994 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5995 free(ondisk_path);
5996 if (err)
5997 return err;
5999 if (status != GOT_STATUS_NO_CHANGE)
6000 return got_error(GOT_ERR_MODIFIED);
6001 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6002 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6004 return NULL;
6007 const struct got_error *
6008 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6009 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6010 struct got_worktree *worktree, struct got_reference *branch,
6011 struct got_repository *repo)
6013 const struct got_error *err = NULL;
6014 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6015 char *branch_ref_name = NULL;
6016 char *fileindex_path = NULL;
6017 struct check_rebase_ok_arg ok_arg;
6018 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6019 struct got_object_id *wt_branch_tip = NULL;
6021 *new_base_branch_ref = NULL;
6022 *tmp_branch = NULL;
6023 *fileindex = NULL;
6025 err = lock_worktree(worktree, LOCK_EX);
6026 if (err)
6027 return err;
6029 err = open_fileindex(fileindex, &fileindex_path, worktree);
6030 if (err)
6031 goto done;
6033 ok_arg.worktree = worktree;
6034 ok_arg.repo = repo;
6035 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6036 &ok_arg);
6037 if (err)
6038 goto done;
6040 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6041 if (err)
6042 goto done;
6044 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6045 if (err)
6046 goto done;
6048 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6049 if (err)
6050 goto done;
6052 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6053 0);
6054 if (err)
6055 goto done;
6057 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6058 if (err)
6059 goto done;
6060 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6061 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6062 goto done;
6065 err = got_ref_alloc_symref(new_base_branch_ref,
6066 new_base_branch_ref_name, wt_branch);
6067 if (err)
6068 goto done;
6069 err = got_ref_write(*new_base_branch_ref, repo);
6070 if (err)
6071 goto done;
6073 /* TODO Lock original branch's ref while rebasing? */
6075 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6076 if (err)
6077 goto done;
6079 err = got_ref_write(branch_ref, repo);
6080 if (err)
6081 goto done;
6083 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6084 worktree->base_commit_id);
6085 if (err)
6086 goto done;
6087 err = got_ref_write(*tmp_branch, repo);
6088 if (err)
6089 goto done;
6091 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6092 if (err)
6093 goto done;
6094 done:
6095 free(fileindex_path);
6096 free(tmp_branch_name);
6097 free(new_base_branch_ref_name);
6098 free(branch_ref_name);
6099 if (branch_ref)
6100 got_ref_close(branch_ref);
6101 if (wt_branch)
6102 got_ref_close(wt_branch);
6103 free(wt_branch_tip);
6104 if (err) {
6105 if (*new_base_branch_ref) {
6106 got_ref_close(*new_base_branch_ref);
6107 *new_base_branch_ref = NULL;
6109 if (*tmp_branch) {
6110 got_ref_close(*tmp_branch);
6111 *tmp_branch = NULL;
6113 if (*fileindex) {
6114 got_fileindex_free(*fileindex);
6115 *fileindex = NULL;
6117 lock_worktree(worktree, LOCK_SH);
6119 return err;
6122 const struct got_error *
6123 got_worktree_rebase_continue(struct got_object_id **commit_id,
6124 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6125 struct got_reference **branch, struct got_fileindex **fileindex,
6126 struct got_worktree *worktree, struct got_repository *repo)
6128 const struct got_error *err;
6129 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6130 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6131 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6132 char *fileindex_path = NULL;
6133 int have_staged_files = 0;
6135 *commit_id = NULL;
6136 *new_base_branch = NULL;
6137 *tmp_branch = NULL;
6138 *branch = NULL;
6139 *fileindex = NULL;
6141 err = lock_worktree(worktree, LOCK_EX);
6142 if (err)
6143 return err;
6145 err = open_fileindex(fileindex, &fileindex_path, worktree);
6146 if (err)
6147 goto done;
6149 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6150 &have_staged_files);
6151 if (err && err->code != GOT_ERR_CANCELLED)
6152 goto done;
6153 if (have_staged_files) {
6154 err = got_error(GOT_ERR_STAGED_PATHS);
6155 goto done;
6158 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6159 if (err)
6160 goto done;
6162 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6163 if (err)
6164 goto done;
6166 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6167 if (err)
6168 goto done;
6170 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6171 if (err)
6172 goto done;
6174 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6175 if (err)
6176 goto done;
6178 err = got_ref_open(branch, repo,
6179 got_ref_get_symref_target(branch_ref), 0);
6180 if (err)
6181 goto done;
6183 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6184 if (err)
6185 goto done;
6187 err = got_ref_resolve(commit_id, repo, commit_ref);
6188 if (err)
6189 goto done;
6191 err = got_ref_open(new_base_branch, repo,
6192 new_base_branch_ref_name, 0);
6193 if (err)
6194 goto done;
6196 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6197 if (err)
6198 goto done;
6199 done:
6200 free(commit_ref_name);
6201 free(branch_ref_name);
6202 free(fileindex_path);
6203 if (commit_ref)
6204 got_ref_close(commit_ref);
6205 if (branch_ref)
6206 got_ref_close(branch_ref);
6207 if (err) {
6208 free(*commit_id);
6209 *commit_id = NULL;
6210 if (*tmp_branch) {
6211 got_ref_close(*tmp_branch);
6212 *tmp_branch = NULL;
6214 if (*new_base_branch) {
6215 got_ref_close(*new_base_branch);
6216 *new_base_branch = NULL;
6218 if (*branch) {
6219 got_ref_close(*branch);
6220 *branch = NULL;
6222 if (*fileindex) {
6223 got_fileindex_free(*fileindex);
6224 *fileindex = NULL;
6226 lock_worktree(worktree, LOCK_SH);
6228 return err;
6231 const struct got_error *
6232 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6234 const struct got_error *err;
6235 char *tmp_branch_name = NULL;
6237 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6238 if (err)
6239 return err;
6241 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6242 free(tmp_branch_name);
6243 return NULL;
6246 static const struct got_error *
6247 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6248 char **logmsg, void *arg)
6250 *logmsg = arg;
6251 return NULL;
6254 static const struct got_error *
6255 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6256 const char *path, struct got_object_id *blob_id,
6257 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6258 int dirfd, const char *de_name)
6260 return NULL;
6263 struct collect_merged_paths_arg {
6264 got_worktree_checkout_cb progress_cb;
6265 void *progress_arg;
6266 struct got_pathlist_head *merged_paths;
6269 static const struct got_error *
6270 collect_merged_paths(void *arg, unsigned char status, const char *path)
6272 const struct got_error *err;
6273 struct collect_merged_paths_arg *a = arg;
6274 char *p;
6275 struct got_pathlist_entry *new;
6277 err = (*a->progress_cb)(a->progress_arg, status, path);
6278 if (err)
6279 return err;
6281 if (status != GOT_STATUS_MERGE &&
6282 status != GOT_STATUS_ADD &&
6283 status != GOT_STATUS_DELETE &&
6284 status != GOT_STATUS_CONFLICT)
6285 return NULL;
6287 p = strdup(path);
6288 if (p == NULL)
6289 return got_error_from_errno("strdup");
6291 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6292 if (err || new == NULL)
6293 free(p);
6294 return err;
6297 void
6298 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6300 struct got_pathlist_entry *pe;
6302 TAILQ_FOREACH(pe, merged_paths, entry)
6303 free((char *)pe->path);
6305 got_pathlist_free(merged_paths);
6308 static const struct got_error *
6309 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6310 int is_rebase, struct got_repository *repo)
6312 const struct got_error *err;
6313 struct got_reference *commit_ref = NULL;
6315 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6316 if (err) {
6317 if (err->code != GOT_ERR_NOT_REF)
6318 goto done;
6319 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6320 if (err)
6321 goto done;
6322 err = got_ref_write(commit_ref, repo);
6323 if (err)
6324 goto done;
6325 } else if (is_rebase) {
6326 struct got_object_id *stored_id;
6327 int cmp;
6329 err = got_ref_resolve(&stored_id, repo, commit_ref);
6330 if (err)
6331 goto done;
6332 cmp = got_object_id_cmp(commit_id, stored_id);
6333 free(stored_id);
6334 if (cmp != 0) {
6335 err = got_error(GOT_ERR_REBASE_COMMITID);
6336 goto done;
6339 done:
6340 if (commit_ref)
6341 got_ref_close(commit_ref);
6342 return err;
6345 static const struct got_error *
6346 rebase_merge_files(struct got_pathlist_head *merged_paths,
6347 const char *commit_ref_name, struct got_worktree *worktree,
6348 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6349 struct got_object_id *commit_id, struct got_repository *repo,
6350 got_worktree_checkout_cb progress_cb, void *progress_arg,
6351 got_cancel_cb cancel_cb, void *cancel_arg)
6353 const struct got_error *err;
6354 struct got_reference *commit_ref = NULL;
6355 struct collect_merged_paths_arg cmp_arg;
6356 char *fileindex_path;
6358 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6360 err = get_fileindex_path(&fileindex_path, worktree);
6361 if (err)
6362 return err;
6364 cmp_arg.progress_cb = progress_cb;
6365 cmp_arg.progress_arg = progress_arg;
6366 cmp_arg.merged_paths = merged_paths;
6367 err = merge_files(worktree, fileindex, fileindex_path,
6368 parent_commit_id, commit_id, repo, collect_merged_paths,
6369 &cmp_arg, cancel_cb, cancel_arg);
6370 if (commit_ref)
6371 got_ref_close(commit_ref);
6372 return err;
6375 const struct got_error *
6376 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6377 struct got_worktree *worktree, struct got_fileindex *fileindex,
6378 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6379 struct got_repository *repo,
6380 got_worktree_checkout_cb progress_cb, void *progress_arg,
6381 got_cancel_cb cancel_cb, void *cancel_arg)
6383 const struct got_error *err;
6384 char *commit_ref_name;
6386 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6387 if (err)
6388 return err;
6390 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6391 if (err)
6392 goto done;
6394 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6395 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6396 progress_arg, cancel_cb, cancel_arg);
6397 done:
6398 free(commit_ref_name);
6399 return err;
6402 const struct got_error *
6403 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6404 struct got_worktree *worktree, struct got_fileindex *fileindex,
6405 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6406 struct got_repository *repo,
6407 got_worktree_checkout_cb progress_cb, void *progress_arg,
6408 got_cancel_cb cancel_cb, void *cancel_arg)
6410 const struct got_error *err;
6411 char *commit_ref_name;
6413 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6414 if (err)
6415 return err;
6417 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6418 if (err)
6419 goto done;
6421 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6422 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6423 progress_arg, cancel_cb, cancel_arg);
6424 done:
6425 free(commit_ref_name);
6426 return err;
6429 static const struct got_error *
6430 rebase_commit(struct got_object_id **new_commit_id,
6431 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6432 struct got_worktree *worktree, struct got_fileindex *fileindex,
6433 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6434 const char *new_logmsg, struct got_repository *repo)
6436 const struct got_error *err, *sync_err;
6437 struct got_pathlist_head commitable_paths;
6438 struct collect_commitables_arg cc_arg;
6439 char *fileindex_path = NULL;
6440 struct got_reference *head_ref = NULL;
6441 struct got_object_id *head_commit_id = NULL;
6442 char *logmsg = NULL;
6444 TAILQ_INIT(&commitable_paths);
6445 *new_commit_id = NULL;
6447 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6449 err = get_fileindex_path(&fileindex_path, worktree);
6450 if (err)
6451 return err;
6453 cc_arg.commitable_paths = &commitable_paths;
6454 cc_arg.worktree = worktree;
6455 cc_arg.repo = repo;
6456 cc_arg.have_staged_files = 0;
6458 * If possible get the status of individual files directly to
6459 * avoid crawling the entire work tree once per rebased commit.
6461 * Ideally, merged_paths would contain a list of commitables
6462 * we could use so we could skip worktree_status() entirely.
6463 * However, we would then need carefully keep track of cumulative
6464 * effects of operations such as file additions and deletions
6465 * in 'got histedit -f' (folding multiple commits into one),
6466 * and this extra complexity is not really worth it.
6468 if (merged_paths) {
6469 struct got_pathlist_entry *pe;
6470 TAILQ_FOREACH(pe, merged_paths, entry) {
6471 err = worktree_status(worktree, pe->path, fileindex,
6472 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6473 0);
6474 if (err)
6475 goto done;
6477 } else {
6478 err = worktree_status(worktree, "", fileindex, repo,
6479 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6480 if (err)
6481 goto done;
6484 if (TAILQ_EMPTY(&commitable_paths)) {
6485 /* No-op change; commit will be elided. */
6486 err = got_ref_delete(commit_ref, repo);
6487 if (err)
6488 goto done;
6489 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6490 goto done;
6493 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6494 if (err)
6495 goto done;
6497 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6498 if (err)
6499 goto done;
6501 if (new_logmsg) {
6502 logmsg = strdup(new_logmsg);
6503 if (logmsg == NULL) {
6504 err = got_error_from_errno("strdup");
6505 goto done;
6507 } else {
6508 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6509 if (err)
6510 goto done;
6513 /* NB: commit_worktree will call free(logmsg) */
6514 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6515 NULL, worktree, got_object_commit_get_author(orig_commit),
6516 got_object_commit_get_committer(orig_commit),
6517 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6518 if (err)
6519 goto done;
6521 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6522 if (err)
6523 goto done;
6525 err = got_ref_delete(commit_ref, repo);
6526 if (err)
6527 goto done;
6529 err = update_fileindex_after_commit(worktree, &commitable_paths,
6530 *new_commit_id, fileindex, 0);
6531 sync_err = sync_fileindex(fileindex, fileindex_path);
6532 if (sync_err && err == NULL)
6533 err = sync_err;
6534 done:
6535 free(fileindex_path);
6536 free(head_commit_id);
6537 if (head_ref)
6538 got_ref_close(head_ref);
6539 if (err) {
6540 free(*new_commit_id);
6541 *new_commit_id = NULL;
6543 return err;
6546 const struct got_error *
6547 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6548 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6549 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6550 struct got_commit_object *orig_commit,
6551 struct got_object_id *orig_commit_id, struct got_repository *repo)
6553 const struct got_error *err;
6554 char *commit_ref_name;
6555 struct got_reference *commit_ref = NULL;
6556 struct got_object_id *commit_id = NULL;
6558 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6559 if (err)
6560 return err;
6562 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6563 if (err)
6564 goto done;
6565 err = got_ref_resolve(&commit_id, repo, commit_ref);
6566 if (err)
6567 goto done;
6568 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6569 err = got_error(GOT_ERR_REBASE_COMMITID);
6570 goto done;
6573 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6574 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6575 done:
6576 if (commit_ref)
6577 got_ref_close(commit_ref);
6578 free(commit_ref_name);
6579 free(commit_id);
6580 return err;
6583 const struct got_error *
6584 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6585 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6586 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6587 struct got_commit_object *orig_commit,
6588 struct got_object_id *orig_commit_id, const char *new_logmsg,
6589 struct got_repository *repo)
6591 const struct got_error *err;
6592 char *commit_ref_name;
6593 struct got_reference *commit_ref = NULL;
6595 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6596 if (err)
6597 return err;
6599 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6600 if (err)
6601 goto done;
6603 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6604 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6605 done:
6606 if (commit_ref)
6607 got_ref_close(commit_ref);
6608 free(commit_ref_name);
6609 return err;
6612 const struct got_error *
6613 got_worktree_rebase_postpone(struct got_worktree *worktree,
6614 struct got_fileindex *fileindex)
6616 if (fileindex)
6617 got_fileindex_free(fileindex);
6618 return lock_worktree(worktree, LOCK_SH);
6621 static const struct got_error *
6622 delete_ref(const char *name, struct got_repository *repo)
6624 const struct got_error *err;
6625 struct got_reference *ref;
6627 err = got_ref_open(&ref, repo, name, 0);
6628 if (err) {
6629 if (err->code == GOT_ERR_NOT_REF)
6630 return NULL;
6631 return err;
6634 err = got_ref_delete(ref, repo);
6635 got_ref_close(ref);
6636 return err;
6639 static const struct got_error *
6640 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6642 const struct got_error *err;
6643 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6644 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6646 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6647 if (err)
6648 goto done;
6649 err = delete_ref(tmp_branch_name, repo);
6650 if (err)
6651 goto done;
6653 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6654 if (err)
6655 goto done;
6656 err = delete_ref(new_base_branch_ref_name, repo);
6657 if (err)
6658 goto done;
6660 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6661 if (err)
6662 goto done;
6663 err = delete_ref(branch_ref_name, repo);
6664 if (err)
6665 goto done;
6667 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6668 if (err)
6669 goto done;
6670 err = delete_ref(commit_ref_name, repo);
6671 if (err)
6672 goto done;
6674 done:
6675 free(tmp_branch_name);
6676 free(new_base_branch_ref_name);
6677 free(branch_ref_name);
6678 free(commit_ref_name);
6679 return err;
6682 static const struct got_error *
6683 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6684 struct got_object_id *new_commit_id, struct got_repository *repo)
6686 const struct got_error *err;
6687 struct got_reference *ref = NULL;
6688 struct got_object_id *old_commit_id = NULL;
6689 const char *branch_name = NULL;
6690 char *new_id_str = NULL;
6691 char *refname = NULL;
6693 branch_name = got_ref_get_name(branch);
6694 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6695 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6696 branch_name += 11;
6698 err = got_object_id_str(&new_id_str, new_commit_id);
6699 if (err)
6700 return err;
6702 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6703 new_id_str) == -1) {
6704 err = got_error_from_errno("asprintf");
6705 goto done;
6708 err = got_ref_resolve(&old_commit_id, repo, branch);
6709 if (err)
6710 goto done;
6712 err = got_ref_alloc(&ref, refname, old_commit_id);
6713 if (err)
6714 goto done;
6716 err = got_ref_write(ref, repo);
6717 done:
6718 free(new_id_str);
6719 free(refname);
6720 free(old_commit_id);
6721 if (ref)
6722 got_ref_close(ref);
6723 return err;
6726 const struct got_error *
6727 got_worktree_rebase_complete(struct got_worktree *worktree,
6728 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6729 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6730 struct got_repository *repo, int create_backup)
6732 const struct got_error *err, *unlockerr, *sync_err;
6733 struct got_object_id *new_head_commit_id = NULL;
6734 char *fileindex_path = NULL;
6736 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6737 if (err)
6738 return err;
6740 if (create_backup) {
6741 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6742 rebased_branch, new_head_commit_id, repo);
6743 if (err)
6744 goto done;
6747 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6748 if (err)
6749 goto done;
6751 err = got_ref_write(rebased_branch, repo);
6752 if (err)
6753 goto done;
6755 err = got_worktree_set_head_ref(worktree, rebased_branch);
6756 if (err)
6757 goto done;
6759 err = delete_rebase_refs(worktree, repo);
6760 if (err)
6761 goto done;
6763 err = get_fileindex_path(&fileindex_path, worktree);
6764 if (err)
6765 goto done;
6766 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6767 sync_err = sync_fileindex(fileindex, fileindex_path);
6768 if (sync_err && err == NULL)
6769 err = sync_err;
6770 done:
6771 got_fileindex_free(fileindex);
6772 free(fileindex_path);
6773 free(new_head_commit_id);
6774 unlockerr = lock_worktree(worktree, LOCK_SH);
6775 if (unlockerr && err == NULL)
6776 err = unlockerr;
6777 return err;
6780 const struct got_error *
6781 got_worktree_rebase_abort(struct got_worktree *worktree,
6782 struct got_fileindex *fileindex, struct got_repository *repo,
6783 struct got_reference *new_base_branch,
6784 got_worktree_checkout_cb progress_cb, void *progress_arg)
6786 const struct got_error *err, *unlockerr, *sync_err;
6787 struct got_reference *resolved = NULL;
6788 struct got_object_id *commit_id = NULL;
6789 struct got_commit_object *commit = NULL;
6790 char *fileindex_path = NULL;
6791 struct revert_file_args rfa;
6792 struct got_object_id *tree_id = NULL;
6794 err = lock_worktree(worktree, LOCK_EX);
6795 if (err)
6796 return err;
6798 err = got_object_open_as_commit(&commit, repo,
6799 worktree->base_commit_id);
6800 if (err)
6801 goto done;
6803 err = got_ref_open(&resolved, repo,
6804 got_ref_get_symref_target(new_base_branch), 0);
6805 if (err)
6806 goto done;
6808 err = got_worktree_set_head_ref(worktree, resolved);
6809 if (err)
6810 goto done;
6813 * XXX commits to the base branch could have happened while
6814 * we were busy rebasing; should we store the original commit ID
6815 * when rebase begins and read it back here?
6817 err = got_ref_resolve(&commit_id, repo, resolved);
6818 if (err)
6819 goto done;
6821 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6822 if (err)
6823 goto done;
6825 err = got_object_id_by_path(&tree_id, repo, commit,
6826 worktree->path_prefix);
6827 if (err)
6828 goto done;
6830 err = delete_rebase_refs(worktree, repo);
6831 if (err)
6832 goto done;
6834 err = get_fileindex_path(&fileindex_path, worktree);
6835 if (err)
6836 goto done;
6838 rfa.worktree = worktree;
6839 rfa.fileindex = fileindex;
6840 rfa.progress_cb = progress_cb;
6841 rfa.progress_arg = progress_arg;
6842 rfa.patch_cb = NULL;
6843 rfa.patch_arg = NULL;
6844 rfa.repo = repo;
6845 rfa.unlink_added_files = 0;
6846 err = worktree_status(worktree, "", fileindex, repo,
6847 revert_file, &rfa, NULL, NULL, 1, 0);
6848 if (err)
6849 goto sync;
6851 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6852 repo, progress_cb, progress_arg, NULL, NULL);
6853 sync:
6854 sync_err = sync_fileindex(fileindex, fileindex_path);
6855 if (sync_err && err == NULL)
6856 err = sync_err;
6857 done:
6858 got_ref_close(resolved);
6859 free(tree_id);
6860 free(commit_id);
6861 if (commit)
6862 got_object_commit_close(commit);
6863 if (fileindex)
6864 got_fileindex_free(fileindex);
6865 free(fileindex_path);
6867 unlockerr = lock_worktree(worktree, LOCK_SH);
6868 if (unlockerr && err == NULL)
6869 err = unlockerr;
6870 return err;
6873 const struct got_error *
6874 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6875 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6876 struct got_fileindex **fileindex, struct got_worktree *worktree,
6877 struct got_repository *repo)
6879 const struct got_error *err = NULL;
6880 char *tmp_branch_name = NULL;
6881 char *branch_ref_name = NULL;
6882 char *base_commit_ref_name = NULL;
6883 char *fileindex_path = NULL;
6884 struct check_rebase_ok_arg ok_arg;
6885 struct got_reference *wt_branch = NULL;
6886 struct got_reference *base_commit_ref = NULL;
6888 *tmp_branch = NULL;
6889 *branch_ref = NULL;
6890 *base_commit_id = NULL;
6891 *fileindex = NULL;
6893 err = lock_worktree(worktree, LOCK_EX);
6894 if (err)
6895 return err;
6897 err = open_fileindex(fileindex, &fileindex_path, worktree);
6898 if (err)
6899 goto done;
6901 ok_arg.worktree = worktree;
6902 ok_arg.repo = repo;
6903 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6904 &ok_arg);
6905 if (err)
6906 goto done;
6908 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6909 if (err)
6910 goto done;
6912 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6913 if (err)
6914 goto done;
6916 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6917 worktree);
6918 if (err)
6919 goto done;
6921 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6922 0);
6923 if (err)
6924 goto done;
6926 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6927 if (err)
6928 goto done;
6930 err = got_ref_write(*branch_ref, repo);
6931 if (err)
6932 goto done;
6934 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6935 worktree->base_commit_id);
6936 if (err)
6937 goto done;
6938 err = got_ref_write(base_commit_ref, repo);
6939 if (err)
6940 goto done;
6941 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6942 if (*base_commit_id == NULL) {
6943 err = got_error_from_errno("got_object_id_dup");
6944 goto done;
6947 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6948 worktree->base_commit_id);
6949 if (err)
6950 goto done;
6951 err = got_ref_write(*tmp_branch, repo);
6952 if (err)
6953 goto done;
6955 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6956 if (err)
6957 goto done;
6958 done:
6959 free(fileindex_path);
6960 free(tmp_branch_name);
6961 free(branch_ref_name);
6962 free(base_commit_ref_name);
6963 if (wt_branch)
6964 got_ref_close(wt_branch);
6965 if (err) {
6966 if (*branch_ref) {
6967 got_ref_close(*branch_ref);
6968 *branch_ref = NULL;
6970 if (*tmp_branch) {
6971 got_ref_close(*tmp_branch);
6972 *tmp_branch = NULL;
6974 free(*base_commit_id);
6975 if (*fileindex) {
6976 got_fileindex_free(*fileindex);
6977 *fileindex = NULL;
6979 lock_worktree(worktree, LOCK_SH);
6981 return err;
6984 const struct got_error *
6985 got_worktree_histedit_postpone(struct got_worktree *worktree,
6986 struct got_fileindex *fileindex)
6988 if (fileindex)
6989 got_fileindex_free(fileindex);
6990 return lock_worktree(worktree, LOCK_SH);
6993 const struct got_error *
6994 got_worktree_histedit_in_progress(int *in_progress,
6995 struct got_worktree *worktree)
6997 const struct got_error *err;
6998 char *tmp_branch_name = NULL;
7000 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7001 if (err)
7002 return err;
7004 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7005 free(tmp_branch_name);
7006 return NULL;
7009 const struct got_error *
7010 got_worktree_histedit_continue(struct got_object_id **commit_id,
7011 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7012 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7013 struct got_worktree *worktree, struct got_repository *repo)
7015 const struct got_error *err;
7016 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7017 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7018 struct got_reference *commit_ref = NULL;
7019 struct got_reference *base_commit_ref = NULL;
7020 char *fileindex_path = NULL;
7021 int have_staged_files = 0;
7023 *commit_id = NULL;
7024 *tmp_branch = NULL;
7025 *base_commit_id = NULL;
7026 *fileindex = NULL;
7028 err = lock_worktree(worktree, LOCK_EX);
7029 if (err)
7030 return err;
7032 err = open_fileindex(fileindex, &fileindex_path, worktree);
7033 if (err)
7034 goto done;
7036 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7037 &have_staged_files);
7038 if (err && err->code != GOT_ERR_CANCELLED)
7039 goto done;
7040 if (have_staged_files) {
7041 err = got_error(GOT_ERR_STAGED_PATHS);
7042 goto done;
7045 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7046 if (err)
7047 goto done;
7049 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7050 if (err)
7051 goto done;
7053 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7054 if (err)
7055 goto done;
7057 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7058 worktree);
7059 if (err)
7060 goto done;
7062 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7063 if (err)
7064 goto done;
7066 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7067 if (err)
7068 goto done;
7069 err = got_ref_resolve(commit_id, repo, commit_ref);
7070 if (err)
7071 goto done;
7073 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7074 if (err)
7075 goto done;
7076 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7077 if (err)
7078 goto done;
7080 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7081 if (err)
7082 goto done;
7083 done:
7084 free(commit_ref_name);
7085 free(branch_ref_name);
7086 free(fileindex_path);
7087 if (commit_ref)
7088 got_ref_close(commit_ref);
7089 if (base_commit_ref)
7090 got_ref_close(base_commit_ref);
7091 if (err) {
7092 free(*commit_id);
7093 *commit_id = NULL;
7094 free(*base_commit_id);
7095 *base_commit_id = NULL;
7096 if (*tmp_branch) {
7097 got_ref_close(*tmp_branch);
7098 *tmp_branch = NULL;
7100 if (*fileindex) {
7101 got_fileindex_free(*fileindex);
7102 *fileindex = NULL;
7104 lock_worktree(worktree, LOCK_EX);
7106 return err;
7109 static const struct got_error *
7110 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7112 const struct got_error *err;
7113 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7114 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7116 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7117 if (err)
7118 goto done;
7119 err = delete_ref(tmp_branch_name, repo);
7120 if (err)
7121 goto done;
7123 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7124 worktree);
7125 if (err)
7126 goto done;
7127 err = delete_ref(base_commit_ref_name, repo);
7128 if (err)
7129 goto done;
7131 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7132 if (err)
7133 goto done;
7134 err = delete_ref(branch_ref_name, repo);
7135 if (err)
7136 goto done;
7138 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7139 if (err)
7140 goto done;
7141 err = delete_ref(commit_ref_name, repo);
7142 if (err)
7143 goto done;
7144 done:
7145 free(tmp_branch_name);
7146 free(base_commit_ref_name);
7147 free(branch_ref_name);
7148 free(commit_ref_name);
7149 return err;
7152 const struct got_error *
7153 got_worktree_histedit_abort(struct got_worktree *worktree,
7154 struct got_fileindex *fileindex, struct got_repository *repo,
7155 struct got_reference *branch, struct got_object_id *base_commit_id,
7156 got_worktree_checkout_cb progress_cb, void *progress_arg)
7158 const struct got_error *err, *unlockerr, *sync_err;
7159 struct got_reference *resolved = NULL;
7160 char *fileindex_path = NULL;
7161 struct got_commit_object *commit = NULL;
7162 struct got_object_id *tree_id = NULL;
7163 struct revert_file_args rfa;
7165 err = lock_worktree(worktree, LOCK_EX);
7166 if (err)
7167 return err;
7169 err = got_object_open_as_commit(&commit, repo,
7170 worktree->base_commit_id);
7171 if (err)
7172 goto done;
7174 err = got_ref_open(&resolved, repo,
7175 got_ref_get_symref_target(branch), 0);
7176 if (err)
7177 goto done;
7179 err = got_worktree_set_head_ref(worktree, resolved);
7180 if (err)
7181 goto done;
7183 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7184 if (err)
7185 goto done;
7187 err = got_object_id_by_path(&tree_id, repo, commit,
7188 worktree->path_prefix);
7189 if (err)
7190 goto done;
7192 err = delete_histedit_refs(worktree, repo);
7193 if (err)
7194 goto done;
7196 err = get_fileindex_path(&fileindex_path, worktree);
7197 if (err)
7198 goto done;
7200 rfa.worktree = worktree;
7201 rfa.fileindex = fileindex;
7202 rfa.progress_cb = progress_cb;
7203 rfa.progress_arg = progress_arg;
7204 rfa.patch_cb = NULL;
7205 rfa.patch_arg = NULL;
7206 rfa.repo = repo;
7207 rfa.unlink_added_files = 0;
7208 err = worktree_status(worktree, "", fileindex, repo,
7209 revert_file, &rfa, NULL, NULL, 1, 0);
7210 if (err)
7211 goto sync;
7213 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7214 repo, progress_cb, progress_arg, NULL, NULL);
7215 sync:
7216 sync_err = sync_fileindex(fileindex, fileindex_path);
7217 if (sync_err && err == NULL)
7218 err = sync_err;
7219 done:
7220 got_ref_close(resolved);
7221 free(tree_id);
7222 free(fileindex_path);
7224 unlockerr = lock_worktree(worktree, LOCK_SH);
7225 if (unlockerr && err == NULL)
7226 err = unlockerr;
7227 return err;
7230 const struct got_error *
7231 got_worktree_histedit_complete(struct got_worktree *worktree,
7232 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7233 struct got_reference *edited_branch, struct got_repository *repo)
7235 const struct got_error *err, *unlockerr, *sync_err;
7236 struct got_object_id *new_head_commit_id = NULL;
7237 struct got_reference *resolved = NULL;
7238 char *fileindex_path = NULL;
7240 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7241 if (err)
7242 return err;
7244 err = got_ref_open(&resolved, repo,
7245 got_ref_get_symref_target(edited_branch), 0);
7246 if (err)
7247 goto done;
7249 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7250 resolved, new_head_commit_id, repo);
7251 if (err)
7252 goto done;
7254 err = got_ref_change_ref(resolved, new_head_commit_id);
7255 if (err)
7256 goto done;
7258 err = got_ref_write(resolved, repo);
7259 if (err)
7260 goto done;
7262 err = got_worktree_set_head_ref(worktree, resolved);
7263 if (err)
7264 goto done;
7266 err = delete_histedit_refs(worktree, repo);
7267 if (err)
7268 goto done;
7270 err = get_fileindex_path(&fileindex_path, worktree);
7271 if (err)
7272 goto done;
7273 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7274 sync_err = sync_fileindex(fileindex, fileindex_path);
7275 if (sync_err && err == NULL)
7276 err = sync_err;
7277 done:
7278 got_fileindex_free(fileindex);
7279 free(fileindex_path);
7280 free(new_head_commit_id);
7281 unlockerr = lock_worktree(worktree, LOCK_SH);
7282 if (unlockerr && err == NULL)
7283 err = unlockerr;
7284 return err;
7287 const struct got_error *
7288 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7289 struct got_object_id *commit_id, struct got_repository *repo)
7291 const struct got_error *err;
7292 char *commit_ref_name;
7294 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7295 if (err)
7296 return err;
7298 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7299 if (err)
7300 goto done;
7302 err = delete_ref(commit_ref_name, repo);
7303 done:
7304 free(commit_ref_name);
7305 return err;
7308 const struct got_error *
7309 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7310 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7311 struct got_worktree *worktree, const char *refname,
7312 struct got_repository *repo)
7314 const struct got_error *err = NULL;
7315 char *fileindex_path = NULL;
7316 struct check_rebase_ok_arg ok_arg;
7318 *fileindex = NULL;
7319 *branch_ref = NULL;
7320 *base_branch_ref = NULL;
7322 err = lock_worktree(worktree, LOCK_EX);
7323 if (err)
7324 return err;
7326 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7327 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7328 "cannot integrate a branch into itself; "
7329 "update -b or different branch name required");
7330 goto done;
7333 err = open_fileindex(fileindex, &fileindex_path, worktree);
7334 if (err)
7335 goto done;
7337 /* Preconditions are the same as for rebase. */
7338 ok_arg.worktree = worktree;
7339 ok_arg.repo = repo;
7340 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7341 &ok_arg);
7342 if (err)
7343 goto done;
7345 err = got_ref_open(branch_ref, repo, refname, 1);
7346 if (err)
7347 goto done;
7349 err = got_ref_open(base_branch_ref, repo,
7350 got_worktree_get_head_ref_name(worktree), 1);
7351 done:
7352 if (err) {
7353 if (*branch_ref) {
7354 got_ref_close(*branch_ref);
7355 *branch_ref = NULL;
7357 if (*base_branch_ref) {
7358 got_ref_close(*base_branch_ref);
7359 *base_branch_ref = NULL;
7361 if (*fileindex) {
7362 got_fileindex_free(*fileindex);
7363 *fileindex = NULL;
7365 lock_worktree(worktree, LOCK_SH);
7367 return err;
7370 const struct got_error *
7371 got_worktree_integrate_continue(struct got_worktree *worktree,
7372 struct got_fileindex *fileindex, struct got_repository *repo,
7373 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7374 got_worktree_checkout_cb progress_cb, void *progress_arg,
7375 got_cancel_cb cancel_cb, void *cancel_arg)
7377 const struct got_error *err = NULL, *sync_err, *unlockerr;
7378 char *fileindex_path = NULL;
7379 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7380 struct got_commit_object *commit = NULL;
7382 err = get_fileindex_path(&fileindex_path, worktree);
7383 if (err)
7384 goto done;
7386 err = got_ref_resolve(&commit_id, repo, branch_ref);
7387 if (err)
7388 goto done;
7390 err = got_object_open_as_commit(&commit, repo, commit_id);
7391 if (err)
7392 goto done;
7394 err = got_object_id_by_path(&tree_id, repo, commit,
7395 worktree->path_prefix);
7396 if (err)
7397 goto done;
7399 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7400 if (err)
7401 goto done;
7403 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7404 progress_cb, progress_arg, cancel_cb, cancel_arg);
7405 if (err)
7406 goto sync;
7408 err = got_ref_change_ref(base_branch_ref, commit_id);
7409 if (err)
7410 goto sync;
7412 err = got_ref_write(base_branch_ref, repo);
7413 if (err)
7414 goto sync;
7416 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7417 sync:
7418 sync_err = sync_fileindex(fileindex, fileindex_path);
7419 if (sync_err && err == NULL)
7420 err = sync_err;
7422 done:
7423 unlockerr = got_ref_unlock(branch_ref);
7424 if (unlockerr && err == NULL)
7425 err = unlockerr;
7426 got_ref_close(branch_ref);
7428 unlockerr = got_ref_unlock(base_branch_ref);
7429 if (unlockerr && err == NULL)
7430 err = unlockerr;
7431 got_ref_close(base_branch_ref);
7433 got_fileindex_free(fileindex);
7434 free(fileindex_path);
7435 free(tree_id);
7436 if (commit)
7437 got_object_commit_close(commit);
7439 unlockerr = lock_worktree(worktree, LOCK_SH);
7440 if (unlockerr && err == NULL)
7441 err = unlockerr;
7442 return err;
7445 const struct got_error *
7446 got_worktree_integrate_abort(struct got_worktree *worktree,
7447 struct got_fileindex *fileindex, struct got_repository *repo,
7448 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7450 const struct got_error *err = NULL, *unlockerr = NULL;
7452 got_fileindex_free(fileindex);
7454 err = lock_worktree(worktree, LOCK_SH);
7456 unlockerr = got_ref_unlock(branch_ref);
7457 if (unlockerr && err == NULL)
7458 err = unlockerr;
7459 got_ref_close(branch_ref);
7461 unlockerr = got_ref_unlock(base_branch_ref);
7462 if (unlockerr && err == NULL)
7463 err = unlockerr;
7464 got_ref_close(base_branch_ref);
7466 return err;
7469 const struct got_error *
7470 got_worktree_merge_postpone(struct got_worktree *worktree,
7471 struct got_fileindex *fileindex)
7473 const struct got_error *err, *sync_err;
7474 char *fileindex_path = NULL;
7476 err = get_fileindex_path(&fileindex_path, worktree);
7477 if (err)
7478 goto done;
7480 sync_err = sync_fileindex(fileindex, fileindex_path);
7482 err = lock_worktree(worktree, LOCK_SH);
7483 if (sync_err && err == NULL)
7484 err = sync_err;
7485 done:
7486 got_fileindex_free(fileindex);
7487 free(fileindex_path);
7488 return err;
7491 static const struct got_error *
7492 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7494 const struct got_error *err;
7495 char *branch_refname = NULL, *commit_refname = NULL;
7497 err = get_merge_branch_ref_name(&branch_refname, worktree);
7498 if (err)
7499 goto done;
7500 err = delete_ref(branch_refname, repo);
7501 if (err)
7502 goto done;
7504 err = get_merge_commit_ref_name(&commit_refname, worktree);
7505 if (err)
7506 goto done;
7507 err = delete_ref(commit_refname, repo);
7508 if (err)
7509 goto done;
7511 done:
7512 free(branch_refname);
7513 free(commit_refname);
7514 return err;
7517 struct merge_commit_msg_arg {
7518 struct got_worktree *worktree;
7519 const char *branch_name;
7522 static const struct got_error *
7523 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths, char **logmsg,
7524 void *arg)
7526 struct merge_commit_msg_arg *a = arg;
7528 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7529 got_worktree_get_head_ref_name(a->worktree)) == -1)
7530 return got_error_from_errno("asprintf");
7532 return NULL;
7536 const struct got_error *
7537 got_worktree_merge_branch(struct got_worktree *worktree,
7538 struct got_fileindex *fileindex,
7539 struct got_object_id *yca_commit_id,
7540 struct got_object_id *branch_tip,
7541 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7542 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7544 const struct got_error *err;
7545 char *fileindex_path = NULL;
7547 err = get_fileindex_path(&fileindex_path, worktree);
7548 if (err)
7549 goto done;
7551 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7552 worktree);
7553 if (err)
7554 goto done;
7556 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7557 branch_tip, repo, progress_cb, progress_arg,
7558 cancel_cb, cancel_arg);
7559 done:
7560 free(fileindex_path);
7561 return err;
7564 const struct got_error *
7565 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7566 struct got_worktree *worktree, struct got_fileindex *fileindex,
7567 const char *author, const char *committer, int allow_bad_symlinks,
7568 struct got_object_id *branch_tip, const char *branch_name,
7569 struct got_repository *repo,
7570 got_worktree_status_cb status_cb, void *status_arg)
7573 const struct got_error *err = NULL, *sync_err;
7574 struct got_pathlist_head commitable_paths;
7575 struct collect_commitables_arg cc_arg;
7576 struct got_pathlist_entry *pe;
7577 struct got_reference *head_ref = NULL;
7578 struct got_object_id *head_commit_id = NULL;
7579 int have_staged_files = 0;
7580 struct merge_commit_msg_arg mcm_arg;
7581 char *fileindex_path = NULL;
7583 *new_commit_id = NULL;
7585 TAILQ_INIT(&commitable_paths);
7587 err = get_fileindex_path(&fileindex_path, worktree);
7588 if (err)
7589 goto done;
7591 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7592 if (err)
7593 goto done;
7595 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7596 if (err)
7597 goto done;
7599 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7600 &have_staged_files);
7601 if (err && err->code != GOT_ERR_CANCELLED)
7602 goto done;
7603 if (have_staged_files) {
7604 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7605 goto done;
7608 cc_arg.commitable_paths = &commitable_paths;
7609 cc_arg.worktree = worktree;
7610 cc_arg.fileindex = fileindex;
7611 cc_arg.repo = repo;
7612 cc_arg.have_staged_files = have_staged_files;
7613 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7614 err = worktree_status(worktree, "", fileindex, repo,
7615 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7616 if (err)
7617 goto done;
7619 if (TAILQ_EMPTY(&commitable_paths)) {
7620 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7621 "merge of %s cannot proceed", branch_name);
7622 goto done;
7625 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7626 struct got_commitable *ct = pe->data;
7627 const char *ct_path = ct->in_repo_path;
7629 while (ct_path[0] == '/')
7630 ct_path++;
7631 err = check_out_of_date(ct_path, ct->status,
7632 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7633 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7634 if (err)
7635 goto done;
7639 mcm_arg.worktree = worktree;
7640 mcm_arg.branch_name = branch_name;
7641 err = commit_worktree(new_commit_id, &commitable_paths,
7642 head_commit_id, branch_tip, worktree, author, committer,
7643 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7644 if (err)
7645 goto done;
7647 err = update_fileindex_after_commit(worktree, &commitable_paths,
7648 *new_commit_id, fileindex, have_staged_files);
7649 sync_err = sync_fileindex(fileindex, fileindex_path);
7650 if (sync_err && err == NULL)
7651 err = sync_err;
7652 done:
7653 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7654 struct got_commitable *ct = pe->data;
7655 free_commitable(ct);
7657 got_pathlist_free(&commitable_paths);
7658 free(fileindex_path);
7659 return err;
7662 const struct got_error *
7663 got_worktree_merge_complete(struct got_worktree *worktree,
7664 struct got_fileindex *fileindex, struct got_repository *repo)
7666 const struct got_error *err, *unlockerr, *sync_err;
7667 char *fileindex_path = NULL;
7669 err = delete_merge_refs(worktree, repo);
7670 if (err)
7671 goto done;
7673 err = get_fileindex_path(&fileindex_path, worktree);
7674 if (err)
7675 goto done;
7676 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7677 sync_err = sync_fileindex(fileindex, fileindex_path);
7678 if (sync_err && err == NULL)
7679 err = sync_err;
7680 done:
7681 got_fileindex_free(fileindex);
7682 free(fileindex_path);
7683 unlockerr = lock_worktree(worktree, LOCK_SH);
7684 if (unlockerr && err == NULL)
7685 err = unlockerr;
7686 return err;
7689 const struct got_error *
7690 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7691 struct got_repository *repo)
7693 const struct got_error *err;
7694 char *branch_refname = NULL;
7695 struct got_reference *branch_ref = NULL;
7697 *in_progress = 0;
7699 err = get_merge_branch_ref_name(&branch_refname, worktree);
7700 if (err)
7701 return err;
7702 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7703 free(branch_refname);
7704 if (err) {
7705 if (err->code != GOT_ERR_NOT_REF)
7706 return err;
7707 } else
7708 *in_progress = 1;
7710 return NULL;
7713 const struct got_error *got_worktree_merge_prepare(
7714 struct got_fileindex **fileindex, struct got_worktree *worktree,
7715 struct got_reference *branch, struct got_repository *repo)
7717 const struct got_error *err = NULL;
7718 char *fileindex_path = NULL;
7719 char *branch_refname = NULL, *commit_refname = NULL;
7720 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
7721 struct got_reference *commit_ref = NULL;
7722 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
7723 struct check_rebase_ok_arg ok_arg;
7725 *fileindex = NULL;
7727 err = lock_worktree(worktree, LOCK_EX);
7728 if (err)
7729 return err;
7731 err = open_fileindex(fileindex, &fileindex_path, worktree);
7732 if (err)
7733 goto done;
7735 /* Preconditions are the same as for rebase. */
7736 ok_arg.worktree = worktree;
7737 ok_arg.repo = repo;
7738 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7739 &ok_arg);
7740 if (err)
7741 goto done;
7743 err = get_merge_branch_ref_name(&branch_refname, worktree);
7744 if (err)
7745 return err;
7747 err = get_merge_commit_ref_name(&commit_refname, worktree);
7748 if (err)
7749 return err;
7751 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7752 0);
7753 if (err)
7754 goto done;
7756 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
7757 if (err)
7758 goto done;
7760 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
7761 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
7762 goto done;
7765 err = got_ref_resolve(&branch_tip, repo, branch);
7766 if (err)
7767 goto done;
7769 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
7770 if (err)
7771 goto done;
7772 err = got_ref_write(branch_ref, repo);
7773 if (err)
7774 goto done;
7776 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
7777 if (err)
7778 goto done;
7779 err = got_ref_write(commit_ref, repo);
7780 if (err)
7781 goto done;
7783 done:
7784 free(branch_refname);
7785 free(commit_refname);
7786 free(fileindex_path);
7787 if (branch_ref)
7788 got_ref_close(branch_ref);
7789 if (commit_ref)
7790 got_ref_close(commit_ref);
7791 if (wt_branch)
7792 got_ref_close(wt_branch);
7793 free(wt_branch_tip);
7794 if (err) {
7795 if (*fileindex) {
7796 got_fileindex_free(*fileindex);
7797 *fileindex = NULL;
7799 lock_worktree(worktree, LOCK_SH);
7801 return err;
7804 const struct got_error *
7805 got_worktree_merge_continue(char **branch_name,
7806 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
7807 struct got_worktree *worktree, struct got_repository *repo)
7809 const struct got_error *err;
7810 char *commit_refname = NULL, *branch_refname = NULL;
7811 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
7812 char *fileindex_path = NULL;
7813 int have_staged_files = 0;
7815 *branch_name = NULL;
7816 *branch_tip = NULL;
7817 *fileindex = NULL;
7819 err = lock_worktree(worktree, LOCK_EX);
7820 if (err)
7821 return err;
7823 err = open_fileindex(fileindex, &fileindex_path, worktree);
7824 if (err)
7825 goto done;
7827 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7828 &have_staged_files);
7829 if (err && err->code != GOT_ERR_CANCELLED)
7830 goto done;
7831 if (have_staged_files) {
7832 err = got_error(GOT_ERR_STAGED_PATHS);
7833 goto done;
7836 err = get_merge_branch_ref_name(&branch_refname, worktree);
7837 if (err)
7838 goto done;
7840 err = get_merge_commit_ref_name(&commit_refname, worktree);
7841 if (err)
7842 goto done;
7844 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7845 if (err)
7846 goto done;
7848 if (!got_ref_is_symbolic(branch_ref)) {
7849 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7850 "%s is not a symbolic reference",
7851 got_ref_get_name(branch_ref));
7852 goto done;
7854 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
7855 if (*branch_name == NULL) {
7856 err = got_error_from_errno("strdup");
7857 goto done;
7860 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
7861 if (err)
7862 goto done;
7864 err = got_ref_resolve(branch_tip, repo, commit_ref);
7865 if (err)
7866 goto done;
7867 done:
7868 free(commit_refname);
7869 free(branch_refname);
7870 free(fileindex_path);
7871 if (commit_ref)
7872 got_ref_close(commit_ref);
7873 if (branch_ref)
7874 got_ref_close(branch_ref);
7875 if (err) {
7876 if (*branch_name) {
7877 free(*branch_name);
7878 *branch_name = NULL;
7880 free(*branch_tip);
7881 *branch_tip = NULL;
7882 if (*fileindex) {
7883 got_fileindex_free(*fileindex);
7884 *fileindex = NULL;
7886 lock_worktree(worktree, LOCK_SH);
7888 return err;
7891 const struct got_error *
7892 got_worktree_merge_abort(struct got_worktree *worktree,
7893 struct got_fileindex *fileindex, struct got_repository *repo,
7894 got_worktree_checkout_cb progress_cb, void *progress_arg)
7896 const struct got_error *err, *unlockerr, *sync_err;
7897 struct got_object_id *commit_id = NULL;
7898 struct got_commit_object *commit = NULL;
7899 char *fileindex_path = NULL;
7900 struct revert_file_args rfa;
7901 struct got_object_id *tree_id = NULL;
7903 err = got_object_open_as_commit(&commit, repo,
7904 worktree->base_commit_id);
7905 if (err)
7906 goto done;
7908 err = got_object_id_by_path(&tree_id, repo, commit,
7909 worktree->path_prefix);
7910 if (err)
7911 goto done;
7913 err = delete_merge_refs(worktree, repo);
7914 if (err)
7915 goto done;
7917 err = get_fileindex_path(&fileindex_path, worktree);
7918 if (err)
7919 goto done;
7921 rfa.worktree = worktree;
7922 rfa.fileindex = fileindex;
7923 rfa.progress_cb = progress_cb;
7924 rfa.progress_arg = progress_arg;
7925 rfa.patch_cb = NULL;
7926 rfa.patch_arg = NULL;
7927 rfa.repo = repo;
7928 rfa.unlink_added_files = 1;
7929 err = worktree_status(worktree, "", fileindex, repo,
7930 revert_file, &rfa, NULL, NULL, 1, 0);
7931 if (err)
7932 goto sync;
7934 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7935 repo, progress_cb, progress_arg, NULL, NULL);
7936 sync:
7937 sync_err = sync_fileindex(fileindex, fileindex_path);
7938 if (sync_err && err == NULL)
7939 err = sync_err;
7940 done:
7941 free(tree_id);
7942 free(commit_id);
7943 if (commit)
7944 got_object_commit_close(commit);
7945 if (fileindex)
7946 got_fileindex_free(fileindex);
7947 free(fileindex_path);
7949 unlockerr = lock_worktree(worktree, LOCK_SH);
7950 if (unlockerr && err == NULL)
7951 err = unlockerr;
7952 return err;
7955 struct check_stage_ok_arg {
7956 struct got_object_id *head_commit_id;
7957 struct got_worktree *worktree;
7958 struct got_fileindex *fileindex;
7959 struct got_repository *repo;
7960 int have_changes;
7963 static const struct got_error *
7964 check_stage_ok(void *arg, unsigned char status,
7965 unsigned char staged_status, const char *relpath,
7966 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7967 struct got_object_id *commit_id, int dirfd, const char *de_name)
7969 struct check_stage_ok_arg *a = arg;
7970 const struct got_error *err = NULL;
7971 struct got_fileindex_entry *ie;
7972 struct got_object_id base_commit_id;
7973 struct got_object_id *base_commit_idp = NULL;
7974 char *in_repo_path = NULL, *p;
7976 if (status == GOT_STATUS_UNVERSIONED ||
7977 status == GOT_STATUS_NO_CHANGE)
7978 return NULL;
7979 if (status == GOT_STATUS_NONEXISTENT)
7980 return got_error_set_errno(ENOENT, relpath);
7982 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7983 if (ie == NULL)
7984 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7986 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7987 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7988 relpath) == -1)
7989 return got_error_from_errno("asprintf");
7991 if (got_fileindex_entry_has_commit(ie)) {
7992 memcpy(base_commit_id.sha1, ie->commit_sha1,
7993 SHA1_DIGEST_LENGTH);
7994 base_commit_idp = &base_commit_id;
7997 if (status == GOT_STATUS_CONFLICT) {
7998 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7999 goto done;
8000 } else if (status != GOT_STATUS_ADD &&
8001 status != GOT_STATUS_MODIFY &&
8002 status != GOT_STATUS_DELETE) {
8003 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8004 goto done;
8007 a->have_changes = 1;
8009 p = in_repo_path;
8010 while (p[0] == '/')
8011 p++;
8012 err = check_out_of_date(p, status, staged_status,
8013 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8014 GOT_ERR_STAGE_OUT_OF_DATE);
8015 done:
8016 free(in_repo_path);
8017 return err;
8020 struct stage_path_arg {
8021 struct got_worktree *worktree;
8022 struct got_fileindex *fileindex;
8023 struct got_repository *repo;
8024 got_worktree_status_cb status_cb;
8025 void *status_arg;
8026 got_worktree_patch_cb patch_cb;
8027 void *patch_arg;
8028 int staged_something;
8029 int allow_bad_symlinks;
8032 static const struct got_error *
8033 stage_path(void *arg, unsigned char status,
8034 unsigned char staged_status, const char *relpath,
8035 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8036 struct got_object_id *commit_id, int dirfd, const char *de_name)
8038 struct stage_path_arg *a = arg;
8039 const struct got_error *err = NULL;
8040 struct got_fileindex_entry *ie;
8041 char *ondisk_path = NULL, *path_content = NULL;
8042 uint32_t stage;
8043 struct got_object_id *new_staged_blob_id = NULL;
8044 struct stat sb;
8046 if (status == GOT_STATUS_UNVERSIONED)
8047 return NULL;
8049 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8050 if (ie == NULL)
8051 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8053 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8054 relpath)== -1)
8055 return got_error_from_errno("asprintf");
8057 switch (status) {
8058 case GOT_STATUS_ADD:
8059 case GOT_STATUS_MODIFY:
8060 /* XXX could sb.st_mode be passed in by our caller? */
8061 if (lstat(ondisk_path, &sb) == -1) {
8062 err = got_error_from_errno2("lstat", ondisk_path);
8063 break;
8065 if (a->patch_cb) {
8066 if (status == GOT_STATUS_ADD) {
8067 int choice = GOT_PATCH_CHOICE_NONE;
8068 err = (*a->patch_cb)(&choice, a->patch_arg,
8069 status, ie->path, NULL, 1, 1);
8070 if (err)
8071 break;
8072 if (choice != GOT_PATCH_CHOICE_YES)
8073 break;
8074 } else {
8075 err = create_patched_content(&path_content, 0,
8076 staged_blob_id ? staged_blob_id : blob_id,
8077 ondisk_path, dirfd, de_name, ie->path,
8078 a->repo, a->patch_cb, a->patch_arg);
8079 if (err || path_content == NULL)
8080 break;
8083 err = got_object_blob_create(&new_staged_blob_id,
8084 path_content ? path_content : ondisk_path, a->repo);
8085 if (err)
8086 break;
8087 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8088 SHA1_DIGEST_LENGTH);
8089 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8090 stage = GOT_FILEIDX_STAGE_ADD;
8091 else
8092 stage = GOT_FILEIDX_STAGE_MODIFY;
8093 got_fileindex_entry_stage_set(ie, stage);
8094 if (S_ISLNK(sb.st_mode)) {
8095 int is_bad_symlink = 0;
8096 if (!a->allow_bad_symlinks) {
8097 char target_path[PATH_MAX];
8098 ssize_t target_len;
8099 target_len = readlink(ondisk_path, target_path,
8100 sizeof(target_path));
8101 if (target_len == -1) {
8102 err = got_error_from_errno2("readlink",
8103 ondisk_path);
8104 break;
8106 err = is_bad_symlink_target(&is_bad_symlink,
8107 target_path, target_len, ondisk_path,
8108 a->worktree->root_path);
8109 if (err)
8110 break;
8111 if (is_bad_symlink) {
8112 err = got_error_path(ondisk_path,
8113 GOT_ERR_BAD_SYMLINK);
8114 break;
8117 if (is_bad_symlink)
8118 got_fileindex_entry_staged_filetype_set(ie,
8119 GOT_FILEIDX_MODE_BAD_SYMLINK);
8120 else
8121 got_fileindex_entry_staged_filetype_set(ie,
8122 GOT_FILEIDX_MODE_SYMLINK);
8123 } else {
8124 got_fileindex_entry_staged_filetype_set(ie,
8125 GOT_FILEIDX_MODE_REGULAR_FILE);
8127 a->staged_something = 1;
8128 if (a->status_cb == NULL)
8129 break;
8130 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8131 get_staged_status(ie), relpath, blob_id,
8132 new_staged_blob_id, NULL, dirfd, de_name);
8133 if (err)
8134 break;
8136 * When staging the reverse of the staged diff,
8137 * implicitly unstage the file.
8139 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8140 sizeof(ie->blob_sha1)) == 0) {
8141 got_fileindex_entry_stage_set(ie,
8142 GOT_FILEIDX_STAGE_NONE);
8144 break;
8145 case GOT_STATUS_DELETE:
8146 if (staged_status == GOT_STATUS_DELETE)
8147 break;
8148 if (a->patch_cb) {
8149 int choice = GOT_PATCH_CHOICE_NONE;
8150 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8151 ie->path, NULL, 1, 1);
8152 if (err)
8153 break;
8154 if (choice == GOT_PATCH_CHOICE_NO)
8155 break;
8156 if (choice != GOT_PATCH_CHOICE_YES) {
8157 err = got_error(GOT_ERR_PATCH_CHOICE);
8158 break;
8161 stage = GOT_FILEIDX_STAGE_DELETE;
8162 got_fileindex_entry_stage_set(ie, stage);
8163 a->staged_something = 1;
8164 if (a->status_cb == NULL)
8165 break;
8166 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8167 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8168 de_name);
8169 break;
8170 case GOT_STATUS_NO_CHANGE:
8171 break;
8172 case GOT_STATUS_CONFLICT:
8173 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8174 break;
8175 case GOT_STATUS_NONEXISTENT:
8176 err = got_error_set_errno(ENOENT, relpath);
8177 break;
8178 default:
8179 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8180 break;
8183 if (path_content && unlink(path_content) == -1 && err == NULL)
8184 err = got_error_from_errno2("unlink", path_content);
8185 free(path_content);
8186 free(ondisk_path);
8187 free(new_staged_blob_id);
8188 return err;
8191 const struct got_error *
8192 got_worktree_stage(struct got_worktree *worktree,
8193 struct got_pathlist_head *paths,
8194 got_worktree_status_cb status_cb, void *status_arg,
8195 got_worktree_patch_cb patch_cb, void *patch_arg,
8196 int allow_bad_symlinks, struct got_repository *repo)
8198 const struct got_error *err = NULL, *sync_err, *unlockerr;
8199 struct got_pathlist_entry *pe;
8200 struct got_fileindex *fileindex = NULL;
8201 char *fileindex_path = NULL;
8202 struct got_reference *head_ref = NULL;
8203 struct got_object_id *head_commit_id = NULL;
8204 struct check_stage_ok_arg oka;
8205 struct stage_path_arg spa;
8207 err = lock_worktree(worktree, LOCK_EX);
8208 if (err)
8209 return err;
8211 err = got_ref_open(&head_ref, repo,
8212 got_worktree_get_head_ref_name(worktree), 0);
8213 if (err)
8214 goto done;
8215 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8216 if (err)
8217 goto done;
8218 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8219 if (err)
8220 goto done;
8222 /* Check pre-conditions before staging anything. */
8223 oka.head_commit_id = head_commit_id;
8224 oka.worktree = worktree;
8225 oka.fileindex = fileindex;
8226 oka.repo = repo;
8227 oka.have_changes = 0;
8228 TAILQ_FOREACH(pe, paths, entry) {
8229 err = worktree_status(worktree, pe->path, fileindex, repo,
8230 check_stage_ok, &oka, NULL, NULL, 1, 0);
8231 if (err)
8232 goto done;
8234 if (!oka.have_changes) {
8235 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8236 goto done;
8239 spa.worktree = worktree;
8240 spa.fileindex = fileindex;
8241 spa.repo = repo;
8242 spa.patch_cb = patch_cb;
8243 spa.patch_arg = patch_arg;
8244 spa.status_cb = status_cb;
8245 spa.status_arg = status_arg;
8246 spa.staged_something = 0;
8247 spa.allow_bad_symlinks = allow_bad_symlinks;
8248 TAILQ_FOREACH(pe, paths, entry) {
8249 err = worktree_status(worktree, pe->path, fileindex, repo,
8250 stage_path, &spa, NULL, NULL, 1, 0);
8251 if (err)
8252 goto done;
8254 if (!spa.staged_something) {
8255 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8256 goto done;
8259 sync_err = sync_fileindex(fileindex, fileindex_path);
8260 if (sync_err && err == NULL)
8261 err = sync_err;
8262 done:
8263 if (head_ref)
8264 got_ref_close(head_ref);
8265 free(head_commit_id);
8266 free(fileindex_path);
8267 if (fileindex)
8268 got_fileindex_free(fileindex);
8269 unlockerr = lock_worktree(worktree, LOCK_SH);
8270 if (unlockerr && err == NULL)
8271 err = unlockerr;
8272 return err;
8275 struct unstage_path_arg {
8276 struct got_worktree *worktree;
8277 struct got_fileindex *fileindex;
8278 struct got_repository *repo;
8279 got_worktree_checkout_cb progress_cb;
8280 void *progress_arg;
8281 got_worktree_patch_cb patch_cb;
8282 void *patch_arg;
8285 static const struct got_error *
8286 create_unstaged_content(char **path_unstaged_content,
8287 char **path_new_staged_content, struct got_object_id *blob_id,
8288 struct got_object_id *staged_blob_id, const char *relpath,
8289 struct got_repository *repo,
8290 got_worktree_patch_cb patch_cb, void *patch_arg)
8292 const struct got_error *err, *free_err;
8293 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8294 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8295 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8296 struct got_diffreg_result *diffreg_result = NULL;
8297 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8298 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8299 int fd1 = -1, fd2 = -1;
8301 *path_unstaged_content = NULL;
8302 *path_new_staged_content = NULL;
8304 err = got_object_id_str(&label1, blob_id);
8305 if (err)
8306 return err;
8308 fd1 = got_opentempfd();
8309 if (fd1 == -1) {
8310 err = got_error_from_errno("got_opentempfd");
8311 goto done;
8313 fd2 = got_opentempfd();
8314 if (fd2 == -1) {
8315 err = got_error_from_errno("got_opentempfd");
8316 goto done;
8319 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8320 if (err)
8321 goto done;
8323 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
8324 if (err)
8325 goto done;
8327 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8328 if (err)
8329 goto done;
8331 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8332 fd2);
8333 if (err)
8334 goto done;
8336 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
8337 if (err)
8338 goto done;
8340 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8341 if (err)
8342 goto done;
8344 err = got_diff_files(&diffreg_result, f1, label1, f2,
8345 path2, 3, 0, 1, NULL);
8346 if (err)
8347 goto done;
8349 err = got_opentemp_named(path_unstaged_content, &outfile,
8350 "got-unstaged-content");
8351 if (err)
8352 goto done;
8353 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8354 "got-new-staged-content");
8355 if (err)
8356 goto done;
8358 if (fseek(f1, 0L, SEEK_SET) == -1) {
8359 err = got_ferror(f1, GOT_ERR_IO);
8360 goto done;
8362 if (fseek(f2, 0L, SEEK_SET) == -1) {
8363 err = got_ferror(f2, GOT_ERR_IO);
8364 goto done;
8366 /* Count the number of actual changes in the diff result. */
8367 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8368 struct diff_chunk_context cc = {};
8369 diff_chunk_context_load_change(&cc, &nchunks_used,
8370 diffreg_result->result, n, 0);
8371 nchanges++;
8373 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8374 int choice;
8375 err = apply_or_reject_change(&choice, &nchunks_used,
8376 diffreg_result->result, n, relpath, f1, f2,
8377 &line_cur1, &line_cur2,
8378 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8379 if (err)
8380 goto done;
8381 if (choice == GOT_PATCH_CHOICE_YES)
8382 have_content = 1;
8383 else
8384 have_rejected_content = 1;
8385 if (choice == GOT_PATCH_CHOICE_QUIT)
8386 break;
8388 if (have_content || have_rejected_content)
8389 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8390 outfile, rejectfile);
8391 done:
8392 free(label1);
8393 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8394 err = got_error_from_errno("close");
8395 if (blob)
8396 got_object_blob_close(blob);
8397 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8398 err = got_error_from_errno("close");
8399 if (staged_blob)
8400 got_object_blob_close(staged_blob);
8401 free_err = got_diffreg_result_free(diffreg_result);
8402 if (free_err && err == NULL)
8403 err = free_err;
8404 if (f1 && fclose(f1) == EOF && err == NULL)
8405 err = got_error_from_errno2("fclose", path1);
8406 if (f2 && fclose(f2) == EOF && err == NULL)
8407 err = got_error_from_errno2("fclose", path2);
8408 if (outfile && fclose(outfile) == EOF && err == NULL)
8409 err = got_error_from_errno2("fclose", *path_unstaged_content);
8410 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8411 err = got_error_from_errno2("fclose", *path_new_staged_content);
8412 if (path1 && unlink(path1) == -1 && err == NULL)
8413 err = got_error_from_errno2("unlink", path1);
8414 if (path2 && unlink(path2) == -1 && err == NULL)
8415 err = got_error_from_errno2("unlink", path2);
8416 if (err || !have_content) {
8417 if (*path_unstaged_content &&
8418 unlink(*path_unstaged_content) == -1 && err == NULL)
8419 err = got_error_from_errno2("unlink",
8420 *path_unstaged_content);
8421 free(*path_unstaged_content);
8422 *path_unstaged_content = NULL;
8424 if (err || !have_content || !have_rejected_content) {
8425 if (*path_new_staged_content &&
8426 unlink(*path_new_staged_content) == -1 && err == NULL)
8427 err = got_error_from_errno2("unlink",
8428 *path_new_staged_content);
8429 free(*path_new_staged_content);
8430 *path_new_staged_content = NULL;
8432 free(path1);
8433 free(path2);
8434 return err;
8437 static const struct got_error *
8438 unstage_hunks(struct got_object_id *staged_blob_id,
8439 struct got_blob_object *blob_base,
8440 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8441 const char *ondisk_path, const char *label_orig,
8442 struct got_worktree *worktree, struct got_repository *repo,
8443 got_worktree_patch_cb patch_cb, void *patch_arg,
8444 got_worktree_checkout_cb progress_cb, void *progress_arg)
8446 const struct got_error *err = NULL;
8447 char *path_unstaged_content = NULL;
8448 char *path_new_staged_content = NULL;
8449 char *parent = NULL, *base_path = NULL;
8450 char *blob_base_path = NULL;
8451 struct got_object_id *new_staged_blob_id = NULL;
8452 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8453 struct stat sb;
8455 err = create_unstaged_content(&path_unstaged_content,
8456 &path_new_staged_content, blob_id, staged_blob_id,
8457 ie->path, repo, patch_cb, patch_arg);
8458 if (err)
8459 return err;
8461 if (path_unstaged_content == NULL)
8462 return NULL;
8464 if (path_new_staged_content) {
8465 err = got_object_blob_create(&new_staged_blob_id,
8466 path_new_staged_content, repo);
8467 if (err)
8468 goto done;
8471 f = fopen(path_unstaged_content, "re");
8472 if (f == NULL) {
8473 err = got_error_from_errno2("fopen",
8474 path_unstaged_content);
8475 goto done;
8477 if (fstat(fileno(f), &sb) == -1) {
8478 err = got_error_from_errno2("fstat", path_unstaged_content);
8479 goto done;
8481 if (got_fileindex_entry_staged_filetype_get(ie) ==
8482 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8483 char link_target[PATH_MAX];
8484 size_t r;
8485 r = fread(link_target, 1, sizeof(link_target), f);
8486 if (r == 0 && ferror(f)) {
8487 err = got_error_from_errno("fread");
8488 goto done;
8490 if (r >= sizeof(link_target)) { /* should not happen */
8491 err = got_error(GOT_ERR_NO_SPACE);
8492 goto done;
8494 link_target[r] = '\0';
8495 err = merge_symlink(worktree, blob_base,
8496 ondisk_path, ie->path, label_orig, link_target,
8497 worktree->base_commit_id, repo, progress_cb,
8498 progress_arg);
8499 } else {
8500 int local_changes_subsumed;
8502 err = got_path_dirname(&parent, ondisk_path);
8503 if (err)
8504 return err;
8506 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8507 parent) == -1) {
8508 err = got_error_from_errno("asprintf");
8509 base_path = NULL;
8510 goto done;
8513 err = got_opentemp_named(&blob_base_path, &f_base, base_path);
8514 if (err)
8515 goto done;
8516 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8517 blob_base);
8518 if (err)
8519 goto done;
8522 * In order the run a 3-way merge with a symlink we copy the symlink's
8523 * target path into a temporary file and use that file with diff3.
8525 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8526 err = dump_symlink_target_path_to_file(&f_deriv2,
8527 ondisk_path);
8528 if (err)
8529 goto done;
8530 } else {
8531 int fd;
8532 fd = open(ondisk_path,
8533 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8534 if (fd == -1) {
8535 err = got_error_from_errno2("open", ondisk_path);
8536 goto done;
8538 f_deriv2 = fdopen(fd, "r");
8539 if (f_deriv2 == NULL) {
8540 err = got_error_from_errno2("fdopen", ondisk_path);
8541 close(fd);
8542 goto done;
8546 err = merge_file(&local_changes_subsumed, worktree,
8547 f_base, f, f_deriv2, ondisk_path, ie->path,
8548 got_fileindex_perms_to_st(ie),
8549 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8550 repo, progress_cb, progress_arg);
8552 if (err)
8553 goto done;
8555 if (new_staged_blob_id) {
8556 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8557 SHA1_DIGEST_LENGTH);
8558 } else {
8559 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8560 got_fileindex_entry_staged_filetype_set(ie, 0);
8562 done:
8563 free(new_staged_blob_id);
8564 if (path_unstaged_content &&
8565 unlink(path_unstaged_content) == -1 && err == NULL)
8566 err = got_error_from_errno2("unlink", path_unstaged_content);
8567 if (path_new_staged_content &&
8568 unlink(path_new_staged_content) == -1 && err == NULL)
8569 err = got_error_from_errno2("unlink", path_new_staged_content);
8570 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8571 err = got_error_from_errno2("unlink", blob_base_path);
8572 if (f_base && fclose(f_base) == EOF && err == NULL)
8573 err = got_error_from_errno2("fclose", path_unstaged_content);
8574 if (f && fclose(f) == EOF && err == NULL)
8575 err = got_error_from_errno2("fclose", path_unstaged_content);
8576 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8577 err = got_error_from_errno2("fclose", ondisk_path);
8578 free(path_unstaged_content);
8579 free(path_new_staged_content);
8580 free(blob_base_path);
8581 free(parent);
8582 free(base_path);
8583 return err;
8586 static const struct got_error *
8587 unstage_path(void *arg, unsigned char status,
8588 unsigned char staged_status, const char *relpath,
8589 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8590 struct got_object_id *commit_id, int dirfd, const char *de_name)
8592 const struct got_error *err = NULL;
8593 struct unstage_path_arg *a = arg;
8594 struct got_fileindex_entry *ie;
8595 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8596 char *ondisk_path = NULL;
8597 char *id_str = NULL, *label_orig = NULL;
8598 int local_changes_subsumed;
8599 struct stat sb;
8600 int fd1 = -1, fd2 = -1;
8602 if (staged_status != GOT_STATUS_ADD &&
8603 staged_status != GOT_STATUS_MODIFY &&
8604 staged_status != GOT_STATUS_DELETE)
8605 return NULL;
8607 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8608 if (ie == NULL)
8609 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8611 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8612 == -1)
8613 return got_error_from_errno("asprintf");
8615 err = got_object_id_str(&id_str,
8616 commit_id ? commit_id : a->worktree->base_commit_id);
8617 if (err)
8618 goto done;
8619 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8620 id_str) == -1) {
8621 err = got_error_from_errno("asprintf");
8622 goto done;
8625 fd1 = got_opentempfd();
8626 if (fd1 == -1) {
8627 err = got_error_from_errno("got_opentempfd");
8628 goto done;
8630 fd2 = got_opentempfd();
8631 if (fd2 == -1) {
8632 err = got_error_from_errno("got_opentempfd");
8633 goto done;
8636 switch (staged_status) {
8637 case GOT_STATUS_MODIFY:
8638 err = got_object_open_as_blob(&blob_base, a->repo,
8639 blob_id, 8192, fd1);
8640 if (err)
8641 break;
8642 /* fall through */
8643 case GOT_STATUS_ADD:
8644 if (a->patch_cb) {
8645 if (staged_status == GOT_STATUS_ADD) {
8646 int choice = GOT_PATCH_CHOICE_NONE;
8647 err = (*a->patch_cb)(&choice, a->patch_arg,
8648 staged_status, ie->path, NULL, 1, 1);
8649 if (err)
8650 break;
8651 if (choice != GOT_PATCH_CHOICE_YES)
8652 break;
8653 } else {
8654 err = unstage_hunks(staged_blob_id,
8655 blob_base, blob_id, ie, ondisk_path,
8656 label_orig, a->worktree, a->repo,
8657 a->patch_cb, a->patch_arg,
8658 a->progress_cb, a->progress_arg);
8659 break; /* Done with this file. */
8662 err = got_object_open_as_blob(&blob_staged, a->repo,
8663 staged_blob_id, 8192, fd2);
8664 if (err)
8665 break;
8666 switch (got_fileindex_entry_staged_filetype_get(ie)) {
8667 case GOT_FILEIDX_MODE_BAD_SYMLINK:
8668 case GOT_FILEIDX_MODE_REGULAR_FILE:
8669 err = merge_blob(&local_changes_subsumed, a->worktree,
8670 blob_base, ondisk_path, relpath,
8671 got_fileindex_perms_to_st(ie), label_orig,
8672 blob_staged, commit_id ? commit_id :
8673 a->worktree->base_commit_id, a->repo,
8674 a->progress_cb, a->progress_arg);
8675 break;
8676 case GOT_FILEIDX_MODE_SYMLINK:
8677 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8678 char *staged_target;
8679 err = got_object_blob_read_to_str(
8680 &staged_target, blob_staged);
8681 if (err)
8682 goto done;
8683 err = merge_symlink(a->worktree, blob_base,
8684 ondisk_path, relpath, label_orig,
8685 staged_target, commit_id ? commit_id :
8686 a->worktree->base_commit_id,
8687 a->repo, a->progress_cb, a->progress_arg);
8688 free(staged_target);
8689 } else {
8690 err = merge_blob(&local_changes_subsumed,
8691 a->worktree, blob_base, ondisk_path,
8692 relpath, got_fileindex_perms_to_st(ie),
8693 label_orig, blob_staged,
8694 commit_id ? commit_id :
8695 a->worktree->base_commit_id, a->repo,
8696 a->progress_cb, a->progress_arg);
8698 break;
8699 default:
8700 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8701 break;
8703 if (err == NULL) {
8704 got_fileindex_entry_stage_set(ie,
8705 GOT_FILEIDX_STAGE_NONE);
8706 got_fileindex_entry_staged_filetype_set(ie, 0);
8708 break;
8709 case GOT_STATUS_DELETE:
8710 if (a->patch_cb) {
8711 int choice = GOT_PATCH_CHOICE_NONE;
8712 err = (*a->patch_cb)(&choice, a->patch_arg,
8713 staged_status, ie->path, NULL, 1, 1);
8714 if (err)
8715 break;
8716 if (choice == GOT_PATCH_CHOICE_NO)
8717 break;
8718 if (choice != GOT_PATCH_CHOICE_YES) {
8719 err = got_error(GOT_ERR_PATCH_CHOICE);
8720 break;
8723 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8724 got_fileindex_entry_staged_filetype_set(ie, 0);
8725 err = get_file_status(&status, &sb, ie, ondisk_path,
8726 dirfd, de_name, a->repo);
8727 if (err)
8728 break;
8729 err = (*a->progress_cb)(a->progress_arg, status, relpath);
8730 break;
8732 done:
8733 free(ondisk_path);
8734 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8735 err = got_error_from_errno("close");
8736 if (blob_base)
8737 got_object_blob_close(blob_base);
8738 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8739 err = got_error_from_errno("close");
8740 if (blob_staged)
8741 got_object_blob_close(blob_staged);
8742 free(id_str);
8743 free(label_orig);
8744 return err;
8747 const struct got_error *
8748 got_worktree_unstage(struct got_worktree *worktree,
8749 struct got_pathlist_head *paths,
8750 got_worktree_checkout_cb progress_cb, void *progress_arg,
8751 got_worktree_patch_cb patch_cb, void *patch_arg,
8752 struct got_repository *repo)
8754 const struct got_error *err = NULL, *sync_err, *unlockerr;
8755 struct got_pathlist_entry *pe;
8756 struct got_fileindex *fileindex = NULL;
8757 char *fileindex_path = NULL;
8758 struct unstage_path_arg upa;
8760 err = lock_worktree(worktree, LOCK_EX);
8761 if (err)
8762 return err;
8764 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8765 if (err)
8766 goto done;
8768 upa.worktree = worktree;
8769 upa.fileindex = fileindex;
8770 upa.repo = repo;
8771 upa.progress_cb = progress_cb;
8772 upa.progress_arg = progress_arg;
8773 upa.patch_cb = patch_cb;
8774 upa.patch_arg = patch_arg;
8775 TAILQ_FOREACH(pe, paths, entry) {
8776 err = worktree_status(worktree, pe->path, fileindex, repo,
8777 unstage_path, &upa, NULL, NULL, 1, 0);
8778 if (err)
8779 goto done;
8782 sync_err = sync_fileindex(fileindex, fileindex_path);
8783 if (sync_err && err == NULL)
8784 err = sync_err;
8785 done:
8786 free(fileindex_path);
8787 if (fileindex)
8788 got_fileindex_free(fileindex);
8789 unlockerr = lock_worktree(worktree, LOCK_SH);
8790 if (unlockerr && err == NULL)
8791 err = unlockerr;
8792 return err;
8795 struct report_file_info_arg {
8796 struct got_worktree *worktree;
8797 got_worktree_path_info_cb info_cb;
8798 void *info_arg;
8799 struct got_pathlist_head *paths;
8800 got_cancel_cb cancel_cb;
8801 void *cancel_arg;
8804 static const struct got_error *
8805 report_file_info(void *arg, struct got_fileindex_entry *ie)
8807 struct report_file_info_arg *a = arg;
8808 struct got_pathlist_entry *pe;
8809 struct got_object_id blob_id, staged_blob_id, commit_id;
8810 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8811 struct got_object_id *commit_idp = NULL;
8812 int stage;
8814 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8815 return got_error(GOT_ERR_CANCELLED);
8817 TAILQ_FOREACH(pe, a->paths, entry) {
8818 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8819 got_path_is_child(ie->path, pe->path, pe->path_len))
8820 break;
8822 if (pe == NULL) /* not found */
8823 return NULL;
8825 if (got_fileindex_entry_has_blob(ie)) {
8826 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8827 blob_idp = &blob_id;
8829 stage = got_fileindex_entry_stage_get(ie);
8830 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8831 stage == GOT_FILEIDX_STAGE_ADD) {
8832 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8833 SHA1_DIGEST_LENGTH);
8834 staged_blob_idp = &staged_blob_id;
8837 if (got_fileindex_entry_has_commit(ie)) {
8838 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8839 commit_idp = &commit_id;
8842 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8843 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8846 const struct got_error *
8847 got_worktree_path_info(struct got_worktree *worktree,
8848 struct got_pathlist_head *paths,
8849 got_worktree_path_info_cb info_cb, void *info_arg,
8850 got_cancel_cb cancel_cb, void *cancel_arg)
8853 const struct got_error *err = NULL, *unlockerr;
8854 struct got_fileindex *fileindex = NULL;
8855 char *fileindex_path = NULL;
8856 struct report_file_info_arg arg;
8858 err = lock_worktree(worktree, LOCK_SH);
8859 if (err)
8860 return err;
8862 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8863 if (err)
8864 goto done;
8866 arg.worktree = worktree;
8867 arg.info_cb = info_cb;
8868 arg.info_arg = info_arg;
8869 arg.paths = paths;
8870 arg.cancel_cb = cancel_cb;
8871 arg.cancel_arg = cancel_arg;
8872 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8873 &arg);
8874 done:
8875 free(fileindex_path);
8876 if (fileindex)
8877 got_fileindex_free(fileindex);
8878 unlockerr = lock_worktree(worktree, LOCK_UN);
8879 if (unlockerr && err == NULL)
8880 err = unlockerr;
8881 return err;
8884 static const struct got_error *
8885 patch_check_path(const char *p, char **path, unsigned char *status,
8886 unsigned char *staged_status, struct got_fileindex *fileindex,
8887 struct got_worktree *worktree, struct got_repository *repo)
8889 const struct got_error *err;
8890 struct got_fileindex_entry *ie;
8891 struct stat sb;
8892 char *ondisk_path = NULL;
8894 err = got_worktree_resolve_path(path, worktree, p);
8895 if (err)
8896 return err;
8898 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
8899 *path[0] ? "/" : "", *path) == -1)
8900 return got_error_from_errno("asprintf");
8902 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
8903 if (ie) {
8904 *staged_status = get_staged_status(ie);
8905 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
8906 repo);
8907 if (err)
8908 goto done;
8909 } else {
8910 *staged_status = GOT_STATUS_NO_CHANGE;
8911 *status = GOT_STATUS_UNVERSIONED;
8912 if (lstat(ondisk_path, &sb) == -1) {
8913 if (errno != ENOENT) {
8914 err = got_error_from_errno2("lstat",
8915 ondisk_path);
8916 goto done;
8918 *status = GOT_STATUS_NONEXISTENT;
8922 done:
8923 free(ondisk_path);
8924 return err;
8927 static const struct got_error *
8928 patch_can_rm(const char *path, unsigned char status,
8929 unsigned char staged_status)
8931 if (status == GOT_STATUS_NONEXISTENT)
8932 return got_error_set_errno(ENOENT, path);
8933 if (status != GOT_STATUS_NO_CHANGE &&
8934 status != GOT_STATUS_ADD &&
8935 status != GOT_STATUS_MODIFY &&
8936 status != GOT_STATUS_MODE_CHANGE)
8937 return got_error_path(path, GOT_ERR_FILE_STATUS);
8938 if (staged_status == GOT_STATUS_DELETE)
8939 return got_error_path(path, GOT_ERR_FILE_STATUS);
8940 return NULL;
8943 static const struct got_error *
8944 patch_can_add(const char *path, unsigned char status)
8946 if (status != GOT_STATUS_NONEXISTENT)
8947 return got_error_path(path, GOT_ERR_FILE_STATUS);
8948 return NULL;
8951 static const struct got_error *
8952 patch_can_edit(const char *path, unsigned char status,
8953 unsigned char staged_status)
8955 if (status == GOT_STATUS_NONEXISTENT)
8956 return got_error_set_errno(ENOENT, path);
8957 if (status != GOT_STATUS_NO_CHANGE &&
8958 status != GOT_STATUS_ADD &&
8959 status != GOT_STATUS_MODIFY)
8960 return got_error_path(path, GOT_ERR_FILE_STATUS);
8961 if (staged_status == GOT_STATUS_DELETE)
8962 return got_error_path(path, GOT_ERR_FILE_STATUS);
8963 return NULL;
8966 const struct got_error *
8967 got_worktree_patch_prepare(struct got_fileindex **fileindex,
8968 char **fileindex_path, struct got_worktree *worktree)
8970 return open_fileindex(fileindex, fileindex_path, worktree);
8973 const struct got_error *
8974 got_worktree_patch_check_path(const char *old, const char *new,
8975 char **oldpath, char **newpath, struct got_worktree *worktree,
8976 struct got_repository *repo, struct got_fileindex *fileindex)
8978 const struct got_error *err = NULL;
8979 int file_renamed = 0;
8980 unsigned char status_old, staged_status_old;
8981 unsigned char status_new, staged_status_new;
8983 *oldpath = NULL;
8984 *newpath = NULL;
8986 err = patch_check_path(old != NULL ? old : new, oldpath,
8987 &status_old, &staged_status_old, fileindex, worktree, repo);
8988 if (err)
8989 goto done;
8991 err = patch_check_path(new != NULL ? new : old, newpath,
8992 &status_new, &staged_status_new, fileindex, worktree, repo);
8993 if (err)
8994 goto done;
8996 if (old != NULL && new != NULL && strcmp(old, new) != 0)
8997 file_renamed = 1;
8999 if (old != NULL && new == NULL)
9000 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9001 else if (file_renamed) {
9002 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9003 if (err == NULL)
9004 err = patch_can_add(*newpath, status_new);
9005 } else if (old == NULL)
9006 err = patch_can_add(*newpath, status_new);
9007 else
9008 err = patch_can_edit(*newpath, status_new, staged_status_new);
9010 done:
9011 if (err) {
9012 free(*oldpath);
9013 *oldpath = NULL;
9014 free(*newpath);
9015 *newpath = NULL;
9017 return err;
9020 const struct got_error *
9021 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9022 struct got_worktree *worktree, struct got_fileindex *fileindex,
9023 got_worktree_checkout_cb progress_cb, void *progress_arg)
9025 struct schedule_addition_args saa;
9027 memset(&saa, 0, sizeof(saa));
9028 saa.worktree = worktree;
9029 saa.fileindex = fileindex;
9030 saa.progress_cb = progress_cb;
9031 saa.progress_arg = progress_arg;
9032 saa.repo = repo;
9034 return worktree_status(worktree, path, fileindex, repo,
9035 schedule_addition, &saa, NULL, NULL, 1, 0);
9038 const struct got_error *
9039 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9040 struct got_worktree *worktree, struct got_fileindex *fileindex,
9041 got_worktree_delete_cb progress_cb, void *progress_arg)
9043 struct schedule_deletion_args sda;
9045 memset(&sda, 0, sizeof(sda));
9046 sda.worktree = worktree;
9047 sda.fileindex = fileindex;
9048 sda.progress_cb = progress_cb;
9049 sda.progress_arg = progress_arg;
9050 sda.repo = repo;
9051 sda.delete_local_mods = 0;
9052 sda.keep_on_disk = 0;
9053 sda.ignore_missing_paths = 0;
9054 sda.status_codes = NULL;
9056 return worktree_status(worktree, path, fileindex, repo,
9057 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9060 const struct got_error *
9061 got_worktree_patch_complete(struct got_fileindex *fileindex,
9062 const char *fileindex_path)
9064 const struct got_error *err = NULL;
9066 err = sync_fileindex(fileindex, fileindex_path);
9067 got_fileindex_free(fileindex);
9069 return err;