Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/stat.h>
19 #include <dirent.h>
20 #include <limits.h>
21 #include <stddef.h>
22 #include <string.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <time.h>
26 #include <fcntl.h>
27 #include <errno.h>
28 #include <unistd.h>
29 #include <zlib.h>
30 #include <fnmatch.h>
31 #include <libgen.h>
33 #include "got_compat.h"
35 #include "got_error.h"
36 #include "got_repository.h"
37 #include "got_reference.h"
38 #include "got_object.h"
39 #include "got_path.h"
40 #include "got_cancel.h"
41 #include "got_worktree.h"
42 #include "got_opentemp.h"
43 #include "got_diff.h"
45 #include "got_lib_worktree.h"
46 #include "got_lib_sha1.h"
47 #include "got_lib_fileindex.h"
48 #include "got_lib_inflate.h"
49 #include "got_lib_delta.h"
50 #include "got_lib_object.h"
51 #include "got_lib_object_parse.h"
52 #include "got_lib_object_create.h"
53 #include "got_lib_object_idset.h"
54 #include "got_lib_diff.h"
55 #include "got_lib_gotconfig.h"
57 #ifndef MIN
58 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
59 #endif
61 #define GOT_MERGE_LABEL_MERGED "merged change"
62 #define GOT_MERGE_LABEL_BASE "3-way merge base"
64 static const struct got_error *
65 create_meta_file(const char *path_got, const char *name, const char *content)
66 {
67 const struct got_error *err = NULL;
68 char *path;
70 if (asprintf(&path, "%s/%s", path_got, name) == -1)
71 return got_error_from_errno("asprintf");
73 err = got_path_create_file(path, content);
74 free(path);
75 return err;
76 }
78 static const struct got_error *
79 update_meta_file(const char *path_got, const char *name, const char *content)
80 {
81 const struct got_error *err = NULL;
82 FILE *tmpfile = NULL;
83 char *tmppath = NULL;
84 char *path = NULL;
86 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
87 err = got_error_from_errno("asprintf");
88 path = NULL;
89 goto done;
90 }
92 err = got_opentemp_named(&tmppath, &tmpfile, path);
93 if (err)
94 goto done;
96 if (content) {
97 int len = fprintf(tmpfile, "%s\n", content);
98 if (len != strlen(content) + 1) {
99 err = got_error_from_errno2("fprintf", tmppath);
100 goto done;
104 if (rename(tmppath, path) != 0) {
105 err = got_error_from_errno3("rename", tmppath, path);
106 unlink(tmppath);
107 goto done;
110 done:
111 if (fclose(tmpfile) == EOF && err == NULL)
112 err = got_error_from_errno2("fclose", tmppath);
113 free(tmppath);
114 return err;
117 static const struct got_error *
118 write_head_ref(const char *path_got, struct got_reference *head_ref)
120 const struct got_error *err = NULL;
121 char *refstr = NULL;
123 if (got_ref_is_symbolic(head_ref)) {
124 refstr = got_ref_to_str(head_ref);
125 if (refstr == NULL)
126 return got_error_from_errno("got_ref_to_str");
127 } else {
128 refstr = strdup(got_ref_get_name(head_ref));
129 if (refstr == NULL)
130 return got_error_from_errno("strdup");
132 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
133 free(refstr);
134 return err;
137 const struct got_error *
138 got_worktree_init(const char *path, struct got_reference *head_ref,
139 const char *prefix, struct got_repository *repo)
141 const struct got_error *err = NULL;
142 struct got_object_id *commit_id = NULL;
143 uuid_t uuid;
144 uint32_t uuid_status;
145 int obj_type;
146 char *path_got = NULL;
147 char *formatstr = NULL;
148 char *absprefix = NULL;
149 char *basestr = NULL;
150 char *uuidstr = NULL;
152 if (strcmp(path, got_repo_get_path(repo)) == 0) {
153 err = got_error(GOT_ERR_WORKTREE_REPO);
154 goto done;
157 err = got_ref_resolve(&commit_id, repo, head_ref);
158 if (err)
159 return err;
160 err = got_object_get_type(&obj_type, repo, commit_id);
161 if (err)
162 return err;
163 if (obj_type != GOT_OBJ_TYPE_COMMIT)
164 return got_error(GOT_ERR_OBJ_TYPE);
166 if (!got_path_is_absolute(prefix)) {
167 if (asprintf(&absprefix, "/%s", prefix) == -1)
168 return got_error_from_errno("asprintf");
171 /* Create top-level directory (may already exist). */
172 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
173 err = got_error_from_errno2("mkdir", path);
174 goto done;
177 /* Create .got directory (may already exist). */
178 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
179 err = got_error_from_errno("asprintf");
180 goto done;
182 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
183 err = got_error_from_errno2("mkdir", path_got);
184 goto done;
187 /* Create an empty lock file. */
188 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
189 if (err)
190 goto done;
192 /* Create an empty file index. */
193 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
194 if (err)
195 goto done;
197 /* Write the HEAD reference. */
198 err = write_head_ref(path_got, head_ref);
199 if (err)
200 goto done;
202 /* Record our base commit. */
203 err = got_object_id_str(&basestr, commit_id);
204 if (err)
205 goto done;
206 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
207 if (err)
208 goto done;
210 /* Store path to repository. */
211 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
212 got_repo_get_path(repo));
213 if (err)
214 goto done;
216 /* Store in-repository path prefix. */
217 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
218 absprefix ? absprefix : prefix);
219 if (err)
220 goto done;
222 /* Generate UUID. */
223 uuid_create(&uuid, &uuid_status);
224 if (uuid_status != uuid_s_ok) {
225 err = got_error_uuid(uuid_status, "uuid_create");
226 goto done;
228 uuid_to_string(&uuid, &uuidstr, &uuid_status);
229 if (uuid_status != uuid_s_ok) {
230 err = got_error_uuid(uuid_status, "uuid_to_string");
231 goto done;
233 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
234 if (err)
235 goto done;
237 /* Stamp work tree with format file. */
238 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
239 err = got_error_from_errno("asprintf");
240 goto done;
242 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
243 if (err)
244 goto done;
246 done:
247 free(commit_id);
248 free(path_got);
249 free(formatstr);
250 free(absprefix);
251 free(basestr);
252 free(uuidstr);
253 return err;
256 const struct got_error *
257 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
258 const char *path_prefix)
260 char *absprefix = NULL;
262 if (!got_path_is_absolute(path_prefix)) {
263 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
264 return got_error_from_errno("asprintf");
266 *match = (strcmp(absprefix ? absprefix : path_prefix,
267 worktree->path_prefix) == 0);
268 free(absprefix);
269 return NULL;
272 const char *
273 got_worktree_get_head_ref_name(struct got_worktree *worktree)
275 return worktree->head_ref_name;
278 const struct got_error *
279 got_worktree_set_head_ref(struct got_worktree *worktree,
280 struct got_reference *head_ref)
282 const struct got_error *err = NULL;
283 char *path_got = NULL, *head_ref_name = NULL;
285 if (asprintf(&path_got, "%s/%s", worktree->root_path,
286 GOT_WORKTREE_GOT_DIR) == -1) {
287 err = got_error_from_errno("asprintf");
288 path_got = NULL;
289 goto done;
292 head_ref_name = strdup(got_ref_get_name(head_ref));
293 if (head_ref_name == NULL) {
294 err = got_error_from_errno("strdup");
295 goto done;
298 err = write_head_ref(path_got, head_ref);
299 if (err)
300 goto done;
302 free(worktree->head_ref_name);
303 worktree->head_ref_name = head_ref_name;
304 done:
305 free(path_got);
306 if (err)
307 free(head_ref_name);
308 return err;
311 struct got_object_id *
312 got_worktree_get_base_commit_id(struct got_worktree *worktree)
314 return worktree->base_commit_id;
317 const struct got_error *
318 got_worktree_set_base_commit_id(struct got_worktree *worktree,
319 struct got_repository *repo, struct got_object_id *commit_id)
321 const struct got_error *err;
322 struct got_object *obj = NULL;
323 char *id_str = NULL;
324 char *path_got = NULL;
326 if (asprintf(&path_got, "%s/%s", worktree->root_path,
327 GOT_WORKTREE_GOT_DIR) == -1) {
328 err = got_error_from_errno("asprintf");
329 path_got = NULL;
330 goto done;
333 err = got_object_open(&obj, repo, commit_id);
334 if (err)
335 return err;
337 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
338 err = got_error(GOT_ERR_OBJ_TYPE);
339 goto done;
342 /* Record our base commit. */
343 err = got_object_id_str(&id_str, commit_id);
344 if (err)
345 goto done;
346 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
347 if (err)
348 goto done;
350 free(worktree->base_commit_id);
351 worktree->base_commit_id = got_object_id_dup(commit_id);
352 if (worktree->base_commit_id == NULL) {
353 err = got_error_from_errno("got_object_id_dup");
354 goto done;
356 done:
357 if (obj)
358 got_object_close(obj);
359 free(id_str);
360 free(path_got);
361 return err;
364 const struct got_gotconfig *
365 got_worktree_get_gotconfig(struct got_worktree *worktree)
367 return worktree->gotconfig;
370 static const struct got_error *
371 lock_worktree(struct got_worktree *worktree, int operation)
373 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
374 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
375 : got_error_from_errno2("flock",
376 got_worktree_get_root_path(worktree)));
377 return NULL;
380 static const struct got_error *
381 add_dir_on_disk(struct got_worktree *worktree, const char *path)
383 const struct got_error *err = NULL;
384 char *abspath;
386 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
387 return got_error_from_errno("asprintf");
389 err = got_path_mkdir(abspath);
390 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
391 struct stat sb;
392 err = NULL;
393 if (lstat(abspath, &sb) == -1) {
394 err = got_error_from_errno2("lstat", abspath);
395 } else if (!S_ISDIR(sb.st_mode)) {
396 /* TODO directory is obstructed; do something */
397 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
400 free(abspath);
401 return err;
404 static const struct got_error *
405 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
407 const struct got_error *err = NULL;
408 uint8_t fbuf1[8192];
409 uint8_t fbuf2[8192];
410 size_t flen1 = 0, flen2 = 0;
412 *same = 1;
414 for (;;) {
415 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
416 if (flen1 == 0 && ferror(f1)) {
417 err = got_error_from_errno("fread");
418 break;
420 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
421 if (flen2 == 0 && ferror(f2)) {
422 err = got_error_from_errno("fread");
423 break;
425 if (flen1 == 0) {
426 if (flen2 != 0)
427 *same = 0;
428 break;
429 } else if (flen2 == 0) {
430 if (flen1 != 0)
431 *same = 0;
432 break;
433 } else if (flen1 == flen2) {
434 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
435 *same = 0;
436 break;
438 } else {
439 *same = 0;
440 break;
444 return err;
447 static const struct got_error *
448 check_files_equal(int *same, FILE *f1, FILE *f2)
450 struct stat sb;
451 size_t size1, size2;
453 *same = 1;
455 if (fstat(fileno(f1), &sb) != 0)
456 return got_error_from_errno("fstat");
457 size1 = sb.st_size;
459 if (fstat(fileno(f2), &sb) != 0)
460 return got_error_from_errno("fstat");
461 size2 = sb.st_size;
463 if (size1 != size2) {
464 *same = 0;
465 return NULL;
468 if (fseek(f1, 0L, SEEK_SET) == -1)
469 return got_ferror(f1, GOT_ERR_IO);
470 if (fseek(f2, 0L, SEEK_SET) == -1)
471 return got_ferror(f2, GOT_ERR_IO);
473 return check_file_contents_equal(same, f1, f2);
476 static const struct got_error *
477 copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
479 uint8_t fbuf[65536];
480 size_t flen;
481 ssize_t outlen;
483 *outsize = 0;
485 if (fseek(f, 0L, SEEK_SET) == -1)
486 return got_ferror(f, GOT_ERR_IO);
488 for (;;) {
489 flen = fread(fbuf, 1, sizeof(fbuf), f);
490 if (flen == 0) {
491 if (ferror(f))
492 return got_error_from_errno("fread");
493 if (feof(f))
494 break;
496 outlen = write(outfd, fbuf, flen);
497 if (outlen == -1)
498 return got_error_from_errno("write");
499 if (outlen != flen)
500 return got_error(GOT_ERR_IO);
501 *outsize += outlen;
504 return NULL;
507 static const struct got_error *
508 merge_binary_file(int *overlapcnt, int merged_fd,
509 FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
510 const char *label_deriv, const char *label_orig, const char *label_deriv2,
511 const char *ondisk_path)
513 const struct got_error *err = NULL;
514 int same_content, changed_deriv, changed_deriv2;
515 int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
516 off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
517 char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
518 char *base_path_orig = NULL, *base_path_deriv = NULL;
519 char *base_path_deriv2 = NULL;
521 *overlapcnt = 0;
523 err = check_files_equal(&same_content, f_deriv, f_deriv2);
524 if (err)
525 return err;
527 if (same_content)
528 return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
530 err = check_files_equal(&same_content, f_deriv, f_orig);
531 if (err)
532 return err;
533 changed_deriv = !same_content;
534 err = check_files_equal(&same_content, f_deriv2, f_orig);
535 if (err)
536 return err;
537 changed_deriv2 = !same_content;
539 if (changed_deriv && changed_deriv2) {
540 *overlapcnt = 1;
541 if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
542 err = got_error_from_errno("asprintf");
543 goto done;
545 if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
546 err = got_error_from_errno("asprintf");
547 goto done;
549 if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
550 err = got_error_from_errno("asprintf");
551 goto done;
553 err = got_opentemp_named_fd(&path_orig, &fd_orig,
554 base_path_orig);
555 if (err)
556 goto done;
557 err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
558 base_path_deriv);
559 if (err)
560 goto done;
561 err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
562 base_path_deriv2);
563 if (err)
564 goto done;
565 err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
566 if (err)
567 goto done;
568 err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
569 if (err)
570 goto done;
571 err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
572 if (err)
573 goto done;
574 if (dprintf(merged_fd, "Binary files differ and cannot be "
575 "merged automatically:\n") < 0) {
576 err = got_error_from_errno("dprintf");
577 goto done;
579 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
580 GOT_DIFF_CONFLICT_MARKER_BEGIN,
581 label_deriv ? " " : "",
582 label_deriv ? label_deriv : "",
583 path_deriv) < 0) {
584 err = got_error_from_errno("dprintf");
585 goto done;
587 if (size_orig > 0) {
588 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
589 GOT_DIFF_CONFLICT_MARKER_ORIG,
590 label_orig ? " " : "",
591 label_orig ? label_orig : "",
592 path_orig) < 0) {
593 err = got_error_from_errno("dprintf");
594 goto done;
597 if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
598 GOT_DIFF_CONFLICT_MARKER_SEP,
599 path_deriv2,
600 GOT_DIFF_CONFLICT_MARKER_END,
601 label_deriv2 ? " " : "",
602 label_deriv2 ? label_deriv2 : "") < 0) {
603 err = got_error_from_errno("dprintf");
604 goto done;
606 } else if (changed_deriv)
607 err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
608 else if (changed_deriv2)
609 err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
610 done:
611 if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
612 err == NULL)
613 err = got_error_from_errno2("unlink", path_orig);
614 if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
615 err = got_error_from_errno2("close", path_orig);
616 if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
617 err = got_error_from_errno2("close", path_deriv);
618 if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
619 err = got_error_from_errno2("close", path_deriv2);
620 free(path_orig);
621 free(path_deriv);
622 free(path_deriv2);
623 free(base_path_orig);
624 free(base_path_deriv);
625 free(base_path_deriv2);
626 return err;
629 /*
630 * Perform a 3-way merge where the file f_orig acts as the common
631 * ancestor, the file f_deriv acts as the first derived version,
632 * and the file f_deriv2 acts as the second derived version.
633 * The merge result will be written to a new file at ondisk_path; any
634 * existing file at this path will be replaced.
635 */
636 static const struct got_error *
637 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
638 FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
639 const char *path, uint16_t st_mode,
640 const char *label_orig, const char *label_deriv, const char *label_deriv2,
641 enum got_diff_algorithm diff_algo, struct got_repository *repo,
642 got_worktree_checkout_cb progress_cb, void *progress_arg)
644 const struct got_error *err = NULL;
645 int merged_fd = -1;
646 FILE *f_merged = NULL;
647 char *merged_path = NULL, *base_path = NULL;
648 int overlapcnt = 0;
649 char *parent = NULL;
651 *local_changes_subsumed = 0;
653 err = got_path_dirname(&parent, ondisk_path);
654 if (err)
655 return err;
657 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
658 err = got_error_from_errno("asprintf");
659 goto done;
662 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
663 if (err)
664 goto done;
666 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
667 f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
668 if (err) {
669 if (err->code != GOT_ERR_FILE_BINARY)
670 goto done;
671 err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
672 f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
673 ondisk_path);
674 if (err)
675 goto done;
678 err = (*progress_cb)(progress_arg,
679 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
680 if (err)
681 goto done;
683 if (fsync(merged_fd) != 0) {
684 err = got_error_from_errno("fsync");
685 goto done;
688 f_merged = fdopen(merged_fd, "r");
689 if (f_merged == NULL) {
690 err = got_error_from_errno("fdopen");
691 goto done;
693 merged_fd = -1;
695 /* Check if a clean merge has subsumed all local changes. */
696 if (overlapcnt == 0) {
697 err = check_files_equal(local_changes_subsumed, f_deriv,
698 f_merged);
699 if (err)
700 goto done;
703 if (fchmod(fileno(f_merged), st_mode) != 0) {
704 err = got_error_from_errno2("fchmod", merged_path);
705 goto done;
708 if (rename(merged_path, ondisk_path) != 0) {
709 err = got_error_from_errno3("rename", merged_path,
710 ondisk_path);
711 goto done;
713 done:
714 if (err) {
715 if (merged_path)
716 unlink(merged_path);
718 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
719 err = got_error_from_errno("close");
720 if (f_merged && fclose(f_merged) == EOF && err == NULL)
721 err = got_error_from_errno("fclose");
722 free(merged_path);
723 free(base_path);
724 free(parent);
725 return err;
728 static const struct got_error *
729 update_symlink(const char *ondisk_path, const char *target_path,
730 size_t target_len)
732 /* This is not atomic but matches what 'ln -sf' does. */
733 if (unlink(ondisk_path) == -1)
734 return got_error_from_errno2("unlink", ondisk_path);
735 if (symlink(target_path, ondisk_path) == -1)
736 return got_error_from_errno3("symlink", target_path,
737 ondisk_path);
738 return NULL;
741 /*
742 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
743 * in the work tree with a file that contains conflict markers and the
744 * conflicting target paths of the original version, a "derived version"
745 * of a symlink from an incoming change, and a local version of the symlink.
747 * The original versions's target path can be NULL if it is not available,
748 * such as if both derived versions added a new symlink at the same path.
750 * The incoming derived symlink target is NULL in case the incoming change
751 * has deleted this symlink.
752 */
753 static const struct got_error *
754 install_symlink_conflict(const char *deriv_target,
755 struct got_object_id *deriv_base_commit_id, const char *orig_target,
756 const char *label_orig, const char *local_target, const char *ondisk_path)
758 const struct got_error *err;
759 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
760 FILE *f = NULL;
762 err = got_object_id_str(&id_str, deriv_base_commit_id);
763 if (err)
764 return got_error_from_errno("asprintf");
766 if (asprintf(&label_deriv, "%s: commit %s",
767 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
768 err = got_error_from_errno("asprintf");
769 goto done;
772 err = got_opentemp_named(&path, &f, "got-symlink-conflict");
773 if (err)
774 goto done;
776 if (fchmod(fileno(f), GOT_DEFAULT_FILE_MODE) == -1) {
777 err = got_error_from_errno2("fchmod", path);
778 goto done;
781 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
782 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
783 deriv_target ? deriv_target : "(symlink was deleted)",
784 orig_target ? label_orig : "",
785 orig_target ? "\n" : "",
786 orig_target ? orig_target : "",
787 orig_target ? "\n" : "",
788 GOT_DIFF_CONFLICT_MARKER_SEP,
789 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
790 err = got_error_from_errno2("fprintf", path);
791 goto done;
794 if (unlink(ondisk_path) == -1) {
795 err = got_error_from_errno2("unlink", ondisk_path);
796 goto done;
798 if (rename(path, ondisk_path) == -1) {
799 err = got_error_from_errno3("rename", path, ondisk_path);
800 goto done;
802 done:
803 if (f != NULL && fclose(f) == EOF && err == NULL)
804 err = got_error_from_errno2("fclose", path);
805 free(path);
806 free(id_str);
807 free(label_deriv);
808 return err;
811 /* forward declaration */
812 static const struct got_error *
813 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
814 const char *, const char *, uint16_t, const char *,
815 struct got_blob_object *, struct got_object_id *,
816 struct got_repository *, got_worktree_checkout_cb, void *);
818 /*
819 * Merge a symlink into the work tree, where blob_orig acts as the common
820 * ancestor, deriv_target is the link target of the first derived version,
821 * and the symlink on disk acts as the second derived version.
822 * Assume that contents of both blobs represent symlinks.
823 */
824 static const struct got_error *
825 merge_symlink(struct got_worktree *worktree,
826 struct got_blob_object *blob_orig, const char *ondisk_path,
827 const char *path, const char *label_orig, const char *deriv_target,
828 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
829 got_worktree_checkout_cb progress_cb, void *progress_arg)
831 const struct got_error *err = NULL;
832 char *ancestor_target = NULL;
833 struct stat sb;
834 ssize_t ondisk_len, deriv_len;
835 char ondisk_target[PATH_MAX];
836 int have_local_change = 0;
837 int have_incoming_change = 0;
839 if (lstat(ondisk_path, &sb) == -1)
840 return got_error_from_errno2("lstat", ondisk_path);
842 ondisk_len = readlink(ondisk_path, ondisk_target,
843 sizeof(ondisk_target));
844 if (ondisk_len == -1) {
845 err = got_error_from_errno2("readlink",
846 ondisk_path);
847 goto done;
849 ondisk_target[ondisk_len] = '\0';
851 if (blob_orig) {
852 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
853 if (err)
854 goto done;
857 if (ancestor_target == NULL ||
858 (ondisk_len != strlen(ancestor_target) ||
859 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
860 have_local_change = 1;
862 deriv_len = strlen(deriv_target);
863 if (ancestor_target == NULL ||
864 (deriv_len != strlen(ancestor_target) ||
865 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
866 have_incoming_change = 1;
868 if (!have_local_change && !have_incoming_change) {
869 if (ancestor_target) {
870 /* Both sides made the same change. */
871 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
872 path);
873 } else if (deriv_len == ondisk_len &&
874 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
875 /* Both sides added the same symlink. */
876 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
877 path);
878 } else {
879 /* Both sides added symlinks which don't match. */
880 err = install_symlink_conflict(deriv_target,
881 deriv_base_commit_id, ancestor_target,
882 label_orig, ondisk_target, ondisk_path);
883 if (err)
884 goto done;
885 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
886 path);
888 } else if (!have_local_change && have_incoming_change) {
889 /* Apply the incoming change. */
890 err = update_symlink(ondisk_path, deriv_target,
891 strlen(deriv_target));
892 if (err)
893 goto done;
894 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
895 } else if (have_local_change && have_incoming_change) {
896 if (deriv_len == ondisk_len &&
897 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
898 /* Both sides made the same change. */
899 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
900 path);
901 } else {
902 err = install_symlink_conflict(deriv_target,
903 deriv_base_commit_id, ancestor_target, label_orig,
904 ondisk_target, ondisk_path);
905 if (err)
906 goto done;
907 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
908 path);
912 done:
913 free(ancestor_target);
914 return err;
917 static const struct got_error *
918 dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
920 const struct got_error *err = NULL;
921 char target_path[PATH_MAX];
922 ssize_t target_len;
923 size_t n;
924 FILE *f;
926 *outfile = NULL;
928 f = got_opentemp();
929 if (f == NULL)
930 return got_error_from_errno("got_opentemp");
931 target_len = readlink(ondisk_path, target_path, sizeof(target_path));
932 if (target_len == -1) {
933 err = got_error_from_errno2("readlink", ondisk_path);
934 goto done;
936 n = fwrite(target_path, 1, target_len, f);
937 if (n != target_len) {
938 err = got_ferror(f, GOT_ERR_IO);
939 goto done;
941 if (fflush(f) == EOF) {
942 err = got_error_from_errno("fflush");
943 goto done;
945 if (fseek(f, 0L, SEEK_SET) == -1) {
946 err = got_ferror(f, GOT_ERR_IO);
947 goto done;
949 done:
950 if (err)
951 fclose(f);
952 else
953 *outfile = f;
954 return err;
957 /*
958 * Perform a 3-way merge where blob_orig acts as the common ancestor,
959 * blob_deriv acts as the first derived version, and the file on disk
960 * acts as the second derived version.
961 */
962 static const struct got_error *
963 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
964 struct got_blob_object *blob_orig, const char *ondisk_path,
965 const char *path, uint16_t st_mode, const char *label_orig,
966 struct got_blob_object *blob_deriv,
967 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
968 got_worktree_checkout_cb progress_cb, void *progress_arg)
970 const struct got_error *err = NULL;
971 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
972 char *blob_orig_path = NULL;
973 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
974 char *label_deriv = NULL, *parent = NULL;
976 *local_changes_subsumed = 0;
978 err = got_path_dirname(&parent, ondisk_path);
979 if (err)
980 return err;
982 if (blob_orig) {
983 if (asprintf(&base_path, "%s/got-merge-blob-orig",
984 parent) == -1) {
985 err = got_error_from_errno("asprintf");
986 base_path = NULL;
987 goto done;
990 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
991 if (err)
992 goto done;
993 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
994 blob_orig);
995 if (err)
996 goto done;
997 free(base_path);
998 } else {
999 /*
1000 * No common ancestor exists. This is an "add vs add" conflict
1001 * and we simply use an empty ancestor file to make both files
1002 * appear in the merged result in their entirety.
1004 f_orig = got_opentemp();
1005 if (f_orig == NULL) {
1006 err = got_error_from_errno("got_opentemp");
1007 goto done;
1011 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1012 err = got_error_from_errno("asprintf");
1013 base_path = NULL;
1014 goto done;
1017 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1018 if (err)
1019 goto done;
1020 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1021 blob_deriv);
1022 if (err)
1023 goto done;
1025 err = got_object_id_str(&id_str, deriv_base_commit_id);
1026 if (err)
1027 goto done;
1028 if (asprintf(&label_deriv, "%s: commit %s",
1029 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1030 err = got_error_from_errno("asprintf");
1031 goto done;
1035 * In order the run a 3-way merge with a symlink we copy the symlink's
1036 * target path into a temporary file and use that file with diff3.
1038 if (S_ISLNK(st_mode)) {
1039 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1040 if (err)
1041 goto done;
1042 } else {
1043 int fd;
1044 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1045 if (fd == -1) {
1046 err = got_error_from_errno2("open", ondisk_path);
1047 goto done;
1049 f_deriv2 = fdopen(fd, "r");
1050 if (f_deriv2 == NULL) {
1051 err = got_error_from_errno2("fdopen", ondisk_path);
1052 close(fd);
1053 goto done;
1057 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1058 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1059 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1060 done:
1061 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1062 err = got_error_from_errno("fclose");
1063 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1064 err = got_error_from_errno("fclose");
1065 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1066 err = got_error_from_errno("fclose");
1067 free(base_path);
1068 if (blob_orig_path) {
1069 unlink(blob_orig_path);
1070 free(blob_orig_path);
1072 if (blob_deriv_path) {
1073 unlink(blob_deriv_path);
1074 free(blob_deriv_path);
1076 free(id_str);
1077 free(label_deriv);
1078 free(parent);
1079 return err;
1082 static const struct got_error *
1083 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1084 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1085 int wt_fd, const char *path, struct got_object_id *blob_id)
1087 const struct got_error *err = NULL;
1088 struct got_fileindex_entry *new_ie;
1090 *new_iep = NULL;
1092 err = got_fileindex_entry_alloc(&new_ie, path);
1093 if (err)
1094 return err;
1096 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1097 blob_id->sha1, base_commit_id->sha1, 1);
1098 if (err)
1099 goto done;
1101 err = got_fileindex_entry_add(fileindex, new_ie);
1102 done:
1103 if (err)
1104 got_fileindex_entry_free(new_ie);
1105 else
1106 *new_iep = new_ie;
1107 return err;
1110 static mode_t
1111 get_ondisk_perms(int executable, mode_t st_mode)
1113 mode_t xbits = S_IXUSR;
1115 if (executable) {
1116 /* Map read bits to execute bits. */
1117 if (st_mode & S_IRGRP)
1118 xbits |= S_IXGRP;
1119 if (st_mode & S_IROTH)
1120 xbits |= S_IXOTH;
1121 return st_mode | xbits;
1124 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1127 /* forward declaration */
1128 static const struct got_error *
1129 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1130 const char *path, mode_t te_mode, mode_t st_mode,
1131 struct got_blob_object *blob, int restoring_missing_file,
1132 int reverting_versioned_file, int installing_bad_symlink,
1133 int path_is_unversioned, struct got_repository *repo,
1134 got_worktree_checkout_cb progress_cb, void *progress_arg);
1137 * This function assumes that the provided symlink target points at a
1138 * safe location in the work tree!
1140 static const struct got_error *
1141 replace_existing_symlink(int *did_something, const char *ondisk_path,
1142 const char *target_path, size_t target_len)
1144 const struct got_error *err = NULL;
1145 ssize_t elen;
1146 char etarget[PATH_MAX];
1147 int fd;
1149 *did_something = 0;
1152 * "Bad" symlinks (those pointing outside the work tree or into the
1153 * .got directory) are installed in the work tree as a regular file
1154 * which contains the bad symlink target path.
1155 * The new symlink target has already been checked for safety by our
1156 * caller. If we can successfully open a regular file then we simply
1157 * replace this file with a symlink below.
1159 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1160 if (fd == -1) {
1161 if (!got_err_open_nofollow_on_symlink())
1162 return got_error_from_errno2("open", ondisk_path);
1164 /* We are updating an existing on-disk symlink. */
1165 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1166 if (elen == -1)
1167 return got_error_from_errno2("readlink", ondisk_path);
1169 if (elen == target_len &&
1170 memcmp(etarget, target_path, target_len) == 0)
1171 return NULL; /* nothing to do */
1174 *did_something = 1;
1175 err = update_symlink(ondisk_path, target_path, target_len);
1176 if (fd != -1 && close(fd) == -1 && err == NULL)
1177 err = got_error_from_errno2("close", ondisk_path);
1178 return err;
1181 static const struct got_error *
1182 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1183 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1185 const struct got_error *err = NULL;
1186 char canonpath[PATH_MAX];
1187 char *path_got = NULL;
1189 *is_bad_symlink = 0;
1191 if (target_len >= sizeof(canonpath)) {
1192 *is_bad_symlink = 1;
1193 return NULL;
1197 * We do not use realpath(3) to resolve the symlink's target
1198 * path because we don't want to resolve symlinks recursively.
1199 * Instead we make the path absolute and then canonicalize it.
1200 * Relative symlink target lookup should begin at the directory
1201 * in which the blob object is being installed.
1203 if (!got_path_is_absolute(target_path)) {
1204 char *abspath, *parent;
1205 err = got_path_dirname(&parent, ondisk_path);
1206 if (err)
1207 return err;
1208 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1209 free(parent);
1210 return got_error_from_errno("asprintf");
1212 free(parent);
1213 if (strlen(abspath) >= sizeof(canonpath)) {
1214 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1215 free(abspath);
1216 return err;
1218 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1219 free(abspath);
1220 if (err)
1221 return err;
1222 } else {
1223 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1224 if (err)
1225 return err;
1228 /* Only allow symlinks pointing at paths within the work tree. */
1229 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1230 *is_bad_symlink = 1;
1231 return NULL;
1234 /* Do not allow symlinks pointing into the .got directory. */
1235 if (asprintf(&path_got, "%s/%s", wtroot_path,
1236 GOT_WORKTREE_GOT_DIR) == -1)
1237 return got_error_from_errno("asprintf");
1238 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1239 *is_bad_symlink = 1;
1241 free(path_got);
1242 return NULL;
1245 static const struct got_error *
1246 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1247 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1248 int restoring_missing_file, int reverting_versioned_file,
1249 int path_is_unversioned, int allow_bad_symlinks,
1250 struct got_repository *repo,
1251 got_worktree_checkout_cb progress_cb, void *progress_arg)
1253 const struct got_error *err = NULL;
1254 char target_path[PATH_MAX];
1255 size_t len, target_len = 0;
1256 char *path_got = NULL;
1257 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1258 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1260 *is_bad_symlink = 0;
1263 * Blob object content specifies the target path of the link.
1264 * If a symbolic link cannot be installed we instead create
1265 * a regular file which contains the link target path stored
1266 * in the blob object.
1268 do {
1269 err = got_object_blob_read_block(&len, blob);
1270 if (len + target_len >= sizeof(target_path)) {
1271 /* Path too long; install as a regular file. */
1272 *is_bad_symlink = 1;
1273 got_object_blob_rewind(blob);
1274 return install_blob(worktree, ondisk_path, path,
1275 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1276 restoring_missing_file, reverting_versioned_file,
1277 1, path_is_unversioned, repo, progress_cb,
1278 progress_arg);
1280 if (len > 0) {
1281 /* Skip blob object header first time around. */
1282 memcpy(target_path + target_len, buf + hdrlen,
1283 len - hdrlen);
1284 target_len += len - hdrlen;
1285 hdrlen = 0;
1287 } while (len != 0);
1288 target_path[target_len] = '\0';
1290 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1291 ondisk_path, worktree->root_path);
1292 if (err)
1293 return err;
1295 if (*is_bad_symlink && !allow_bad_symlinks) {
1296 /* install as a regular file */
1297 got_object_blob_rewind(blob);
1298 err = install_blob(worktree, ondisk_path, path,
1299 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1300 restoring_missing_file, reverting_versioned_file, 1,
1301 path_is_unversioned, repo, progress_cb, progress_arg);
1302 goto done;
1305 if (symlink(target_path, ondisk_path) == -1) {
1306 if (errno == EEXIST) {
1307 int symlink_replaced;
1308 if (path_is_unversioned) {
1309 err = (*progress_cb)(progress_arg,
1310 GOT_STATUS_UNVERSIONED, path);
1311 goto done;
1313 err = replace_existing_symlink(&symlink_replaced,
1314 ondisk_path, target_path, target_len);
1315 if (err)
1316 goto done;
1317 if (progress_cb) {
1318 if (symlink_replaced) {
1319 err = (*progress_cb)(progress_arg,
1320 reverting_versioned_file ?
1321 GOT_STATUS_REVERT :
1322 GOT_STATUS_UPDATE, path);
1323 } else {
1324 err = (*progress_cb)(progress_arg,
1325 GOT_STATUS_EXISTS, path);
1328 goto done; /* Nothing else to do. */
1331 if (errno == ENOENT) {
1332 char *parent;
1333 err = got_path_dirname(&parent, ondisk_path);
1334 if (err)
1335 goto done;
1336 err = add_dir_on_disk(worktree, parent);
1337 free(parent);
1338 if (err)
1339 goto done;
1341 * Retry, and fall through to error handling
1342 * below if this second attempt fails.
1344 if (symlink(target_path, ondisk_path) != -1) {
1345 err = NULL; /* success */
1346 goto done;
1350 /* Handle errors from first or second creation attempt. */
1351 if (errno == ENAMETOOLONG) {
1352 /* bad target path; install as a regular file */
1353 *is_bad_symlink = 1;
1354 got_object_blob_rewind(blob);
1355 err = install_blob(worktree, ondisk_path, path,
1356 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1357 restoring_missing_file, reverting_versioned_file, 1,
1358 path_is_unversioned, repo,
1359 progress_cb, progress_arg);
1360 } else if (errno == ENOTDIR) {
1361 err = got_error_path(ondisk_path,
1362 GOT_ERR_FILE_OBSTRUCTED);
1363 } else {
1364 err = got_error_from_errno3("symlink",
1365 target_path, ondisk_path);
1367 } else if (progress_cb)
1368 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1369 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1370 done:
1371 free(path_got);
1372 return err;
1375 static const struct got_error *
1376 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1377 const char *path, mode_t te_mode, mode_t st_mode,
1378 struct got_blob_object *blob, int restoring_missing_file,
1379 int reverting_versioned_file, int installing_bad_symlink,
1380 int path_is_unversioned, struct got_repository *repo,
1381 got_worktree_checkout_cb progress_cb, void *progress_arg)
1383 const struct got_error *err = NULL;
1384 int fd = -1;
1385 size_t len, hdrlen;
1386 int update = 0;
1387 char *tmppath = NULL;
1389 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1390 O_CLOEXEC, GOT_DEFAULT_FILE_MODE);
1391 if (fd == -1) {
1392 if (errno == ENOENT) {
1393 char *parent;
1394 err = got_path_dirname(&parent, path);
1395 if (err)
1396 return err;
1397 err = add_dir_on_disk(worktree, parent);
1398 free(parent);
1399 if (err)
1400 return err;
1401 fd = open(ondisk_path,
1402 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1403 GOT_DEFAULT_FILE_MODE);
1404 if (fd == -1)
1405 return got_error_from_errno2("open",
1406 ondisk_path);
1407 } else if (errno == EEXIST) {
1408 if (path_is_unversioned) {
1409 err = (*progress_cb)(progress_arg,
1410 GOT_STATUS_UNVERSIONED, path);
1411 goto done;
1413 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1414 !S_ISREG(st_mode) && !installing_bad_symlink) {
1415 /* TODO file is obstructed; do something */
1416 err = got_error_path(ondisk_path,
1417 GOT_ERR_FILE_OBSTRUCTED);
1418 goto done;
1419 } else {
1420 err = got_opentemp_named_fd(&tmppath, &fd,
1421 ondisk_path);
1422 if (err)
1423 goto done;
1424 update = 1;
1426 } else
1427 return got_error_from_errno2("open", ondisk_path);
1430 if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1431 err = got_error_from_errno2("fchmod",
1432 update ? tmppath : ondisk_path);
1433 goto done;
1436 if (progress_cb) {
1437 if (restoring_missing_file)
1438 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1439 path);
1440 else if (reverting_versioned_file)
1441 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1442 path);
1443 else
1444 err = (*progress_cb)(progress_arg,
1445 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1446 if (err)
1447 goto done;
1450 hdrlen = got_object_blob_get_hdrlen(blob);
1451 do {
1452 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1453 err = got_object_blob_read_block(&len, blob);
1454 if (err)
1455 break;
1456 if (len > 0) {
1457 /* Skip blob object header first time around. */
1458 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1459 if (outlen == -1) {
1460 err = got_error_from_errno("write");
1461 goto done;
1462 } else if (outlen != len - hdrlen) {
1463 err = got_error(GOT_ERR_IO);
1464 goto done;
1466 hdrlen = 0;
1468 } while (len != 0);
1470 if (fsync(fd) != 0) {
1471 err = got_error_from_errno("fsync");
1472 goto done;
1475 if (update) {
1476 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1477 err = got_error_from_errno2("unlink", ondisk_path);
1478 goto done;
1480 if (rename(tmppath, ondisk_path) != 0) {
1481 err = got_error_from_errno3("rename", tmppath,
1482 ondisk_path);
1483 goto done;
1485 free(tmppath);
1486 tmppath = NULL;
1489 done:
1490 if (fd != -1 && close(fd) == -1 && err == NULL)
1491 err = got_error_from_errno("close");
1492 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1493 err = got_error_from_errno2("unlink", tmppath);
1494 free(tmppath);
1495 return err;
1498 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1499 static const struct got_error *
1500 get_modified_file_content_status(unsigned char *status, FILE *f)
1502 const struct got_error *err = NULL;
1503 const char *markers[3] = {
1504 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1505 GOT_DIFF_CONFLICT_MARKER_SEP,
1506 GOT_DIFF_CONFLICT_MARKER_END
1508 int i = 0;
1509 char *line = NULL;
1510 size_t linesize = 0;
1511 ssize_t linelen;
1513 while (*status == GOT_STATUS_MODIFY) {
1514 linelen = getline(&line, &linesize, f);
1515 if (linelen == -1) {
1516 if (feof(f))
1517 break;
1518 err = got_ferror(f, GOT_ERR_IO);
1519 break;
1522 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1523 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1524 == 0)
1525 *status = GOT_STATUS_CONFLICT;
1526 else
1527 i++;
1530 free(line);
1532 return err;
1535 static int
1536 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1538 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1539 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1542 static int
1543 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1545 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1546 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1547 ie->mtime_sec == sb->st_mtim.tv_sec &&
1548 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1549 ie->size == (sb->st_size & 0xffffffff) &&
1550 !xbit_differs(ie, sb->st_mode));
1553 static unsigned char
1554 get_staged_status(struct got_fileindex_entry *ie)
1556 switch (got_fileindex_entry_stage_get(ie)) {
1557 case GOT_FILEIDX_STAGE_ADD:
1558 return GOT_STATUS_ADD;
1559 case GOT_FILEIDX_STAGE_DELETE:
1560 return GOT_STATUS_DELETE;
1561 case GOT_FILEIDX_STAGE_MODIFY:
1562 return GOT_STATUS_MODIFY;
1563 default:
1564 return GOT_STATUS_NO_CHANGE;
1568 static const struct got_error *
1569 get_symlink_modification_status(unsigned char *status,
1570 struct got_fileindex_entry *ie, const char *abspath,
1571 int dirfd, const char *de_name, struct got_blob_object *blob)
1573 const struct got_error *err = NULL;
1574 char target_path[PATH_MAX];
1575 char etarget[PATH_MAX];
1576 ssize_t elen;
1577 size_t len, target_len = 0;
1578 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1579 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1581 *status = GOT_STATUS_NO_CHANGE;
1583 /* Blob object content specifies the target path of the link. */
1584 do {
1585 err = got_object_blob_read_block(&len, blob);
1586 if (err)
1587 return err;
1588 if (len + target_len >= sizeof(target_path)) {
1590 * Should not happen. The blob contents were OK
1591 * when this symlink was installed.
1593 return got_error(GOT_ERR_NO_SPACE);
1595 if (len > 0) {
1596 /* Skip blob object header first time around. */
1597 memcpy(target_path + target_len, buf + hdrlen,
1598 len - hdrlen);
1599 target_len += len - hdrlen;
1600 hdrlen = 0;
1602 } while (len != 0);
1603 target_path[target_len] = '\0';
1605 if (dirfd != -1) {
1606 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1607 if (elen == -1)
1608 return got_error_from_errno2("readlinkat", abspath);
1609 } else {
1610 elen = readlink(abspath, etarget, sizeof(etarget));
1611 if (elen == -1)
1612 return got_error_from_errno2("readlink", abspath);
1615 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1616 *status = GOT_STATUS_MODIFY;
1618 return NULL;
1621 static const struct got_error *
1622 get_file_status(unsigned char *status, struct stat *sb,
1623 struct got_fileindex_entry *ie, const char *abspath,
1624 int dirfd, const char *de_name, struct got_repository *repo)
1626 const struct got_error *err = NULL;
1627 struct got_object_id id;
1628 size_t hdrlen;
1629 int fd = -1;
1630 FILE *f = NULL;
1631 uint8_t fbuf[8192];
1632 struct got_blob_object *blob = NULL;
1633 size_t flen, blen;
1634 unsigned char staged_status = get_staged_status(ie);
1636 *status = GOT_STATUS_NO_CHANGE;
1637 memset(sb, 0, sizeof(*sb));
1640 * Whenever the caller provides a directory descriptor and a
1641 * directory entry name for the file, use them! This prevents
1642 * race conditions if filesystem paths change beneath our feet.
1644 if (dirfd != -1) {
1645 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1646 if (errno == ENOENT) {
1647 if (got_fileindex_entry_has_file_on_disk(ie))
1648 *status = GOT_STATUS_MISSING;
1649 else
1650 *status = GOT_STATUS_DELETE;
1651 goto done;
1653 err = got_error_from_errno2("fstatat", abspath);
1654 goto done;
1656 } else {
1657 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1658 if (fd == -1 && errno != ENOENT &&
1659 !got_err_open_nofollow_on_symlink())
1660 return got_error_from_errno2("open", abspath);
1661 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1662 if (lstat(abspath, sb) == -1)
1663 return got_error_from_errno2("lstat", abspath);
1664 } else if (fd == -1 || fstat(fd, sb) == -1) {
1665 if (errno == ENOENT) {
1666 if (got_fileindex_entry_has_file_on_disk(ie))
1667 *status = GOT_STATUS_MISSING;
1668 else
1669 *status = GOT_STATUS_DELETE;
1670 goto done;
1672 err = got_error_from_errno2("fstat", abspath);
1673 goto done;
1677 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1678 *status = GOT_STATUS_OBSTRUCTED;
1679 goto done;
1682 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1683 *status = GOT_STATUS_DELETE;
1684 goto done;
1685 } else if (!got_fileindex_entry_has_blob(ie) &&
1686 staged_status != GOT_STATUS_ADD) {
1687 *status = GOT_STATUS_ADD;
1688 goto done;
1691 if (!stat_info_differs(ie, sb))
1692 goto done;
1694 if (S_ISLNK(sb->st_mode) &&
1695 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1696 *status = GOT_STATUS_MODIFY;
1697 goto done;
1700 if (staged_status == GOT_STATUS_MODIFY ||
1701 staged_status == GOT_STATUS_ADD)
1702 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1703 else
1704 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1706 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1707 if (err)
1708 goto done;
1710 if (S_ISLNK(sb->st_mode)) {
1711 err = get_symlink_modification_status(status, ie,
1712 abspath, dirfd, de_name, blob);
1713 goto done;
1716 if (dirfd != -1) {
1717 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1718 if (fd == -1) {
1719 err = got_error_from_errno2("openat", abspath);
1720 goto done;
1724 f = fdopen(fd, "r");
1725 if (f == NULL) {
1726 err = got_error_from_errno2("fdopen", abspath);
1727 goto done;
1729 fd = -1;
1730 hdrlen = got_object_blob_get_hdrlen(blob);
1731 for (;;) {
1732 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1733 err = got_object_blob_read_block(&blen, blob);
1734 if (err)
1735 goto done;
1736 /* Skip length of blob object header first time around. */
1737 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1738 if (flen == 0 && ferror(f)) {
1739 err = got_error_from_errno("fread");
1740 goto done;
1742 if (blen - hdrlen == 0) {
1743 if (flen != 0)
1744 *status = GOT_STATUS_MODIFY;
1745 break;
1746 } else if (flen == 0) {
1747 if (blen - hdrlen != 0)
1748 *status = GOT_STATUS_MODIFY;
1749 break;
1750 } else if (blen - hdrlen == flen) {
1751 /* Skip blob object header first time around. */
1752 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1753 *status = GOT_STATUS_MODIFY;
1754 break;
1756 } else {
1757 *status = GOT_STATUS_MODIFY;
1758 break;
1760 hdrlen = 0;
1763 if (*status == GOT_STATUS_MODIFY) {
1764 rewind(f);
1765 err = get_modified_file_content_status(status, f);
1766 } else if (xbit_differs(ie, sb->st_mode))
1767 *status = GOT_STATUS_MODE_CHANGE;
1768 done:
1769 if (blob)
1770 got_object_blob_close(blob);
1771 if (f != NULL && fclose(f) == EOF && err == NULL)
1772 err = got_error_from_errno2("fclose", abspath);
1773 if (fd != -1 && close(fd) == -1 && err == NULL)
1774 err = got_error_from_errno2("close", abspath);
1775 return err;
1779 * Update timestamps in the file index if a file is unmodified and
1780 * we had to run a full content comparison to find out.
1782 static const struct got_error *
1783 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1784 struct got_fileindex_entry *ie, struct stat *sb)
1786 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1787 return got_fileindex_entry_update(ie, wt_fd, path,
1788 ie->blob_sha1, ie->commit_sha1, 1);
1790 return NULL;
1793 static const struct got_error *
1794 update_blob(struct got_worktree *worktree,
1795 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1796 struct got_tree_entry *te, const char *path,
1797 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1798 void *progress_arg)
1800 const struct got_error *err = NULL;
1801 struct got_blob_object *blob = NULL;
1802 char *ondisk_path;
1803 unsigned char status = GOT_STATUS_NO_CHANGE;
1804 struct stat sb;
1806 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1807 return got_error_from_errno("asprintf");
1809 if (ie) {
1810 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1811 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1812 goto done;
1814 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1815 repo);
1816 if (err)
1817 goto done;
1818 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1819 sb.st_mode = got_fileindex_perms_to_st(ie);
1820 } else {
1821 if (stat(ondisk_path, &sb) == -1) {
1822 if (errno != ENOENT) {
1823 err = got_error_from_errno2("stat",
1824 ondisk_path);
1825 goto done;
1827 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1828 status = GOT_STATUS_UNVERSIONED;
1829 } else {
1830 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1831 status = GOT_STATUS_UNVERSIONED;
1832 else
1833 status = GOT_STATUS_OBSTRUCTED;
1837 if (status == GOT_STATUS_OBSTRUCTED) {
1838 if (ie)
1839 got_fileindex_entry_mark_skipped(ie);
1840 err = (*progress_cb)(progress_arg, status, path);
1841 goto done;
1843 if (status == GOT_STATUS_CONFLICT) {
1844 if (ie)
1845 got_fileindex_entry_mark_skipped(ie);
1846 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1847 path);
1848 goto done;
1851 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1852 (S_ISLNK(te->mode) ||
1853 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1855 * This is a regular file or an installed bad symlink.
1856 * If the file index indicates that this file is already
1857 * up-to-date with respect to the repository we can skip
1858 * updating contents of this file.
1860 if (got_fileindex_entry_has_commit(ie) &&
1861 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1862 SHA1_DIGEST_LENGTH) == 0) {
1863 /* Same commit. */
1864 err = sync_timestamps(worktree->root_fd,
1865 path, status, ie, &sb);
1866 if (err)
1867 goto done;
1868 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1869 path);
1870 goto done;
1872 if (got_fileindex_entry_has_blob(ie) &&
1873 memcmp(ie->blob_sha1, te->id.sha1,
1874 SHA1_DIGEST_LENGTH) == 0) {
1875 /* Different commit but the same blob. */
1876 err = sync_timestamps(worktree->root_fd,
1877 path, status, ie, &sb);
1878 if (err)
1879 goto done;
1880 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1881 path);
1882 goto done;
1886 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1887 if (err)
1888 goto done;
1890 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1891 int update_timestamps;
1892 struct got_blob_object *blob2 = NULL;
1893 char *label_orig = NULL;
1894 if (got_fileindex_entry_has_blob(ie)) {
1895 struct got_object_id id2;
1896 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1897 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1898 if (err)
1899 goto done;
1901 if (got_fileindex_entry_has_commit(ie)) {
1902 char id_str[SHA1_DIGEST_STRING_LENGTH];
1903 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1904 sizeof(id_str)) == NULL) {
1905 err = got_error_path(id_str,
1906 GOT_ERR_BAD_OBJ_ID_STR);
1907 goto done;
1909 if (asprintf(&label_orig, "%s: commit %s",
1910 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1911 err = got_error_from_errno("asprintf");
1912 goto done;
1915 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1916 char *link_target;
1917 err = got_object_blob_read_to_str(&link_target, blob);
1918 if (err)
1919 goto done;
1920 err = merge_symlink(worktree, blob2, ondisk_path, path,
1921 label_orig, link_target, worktree->base_commit_id,
1922 repo, progress_cb, progress_arg);
1923 free(link_target);
1924 } else {
1925 err = merge_blob(&update_timestamps, worktree, blob2,
1926 ondisk_path, path, sb.st_mode, label_orig, blob,
1927 worktree->base_commit_id, repo,
1928 progress_cb, progress_arg);
1930 free(label_orig);
1931 if (blob2)
1932 got_object_blob_close(blob2);
1933 if (err)
1934 goto done;
1936 * Do not update timestamps of files with local changes.
1937 * Otherwise, a future status walk would treat them as
1938 * unmodified files again.
1940 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1941 blob->id.sha1, worktree->base_commit_id->sha1,
1942 update_timestamps);
1943 } else if (status == GOT_STATUS_MODE_CHANGE) {
1944 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1945 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1946 } else if (status == GOT_STATUS_DELETE) {
1947 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1948 if (err)
1949 goto done;
1950 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1951 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1952 if (err)
1953 goto done;
1954 } else {
1955 int is_bad_symlink = 0;
1956 if (S_ISLNK(te->mode)) {
1957 err = install_symlink(&is_bad_symlink, worktree,
1958 ondisk_path, path, blob,
1959 status == GOT_STATUS_MISSING, 0,
1960 status == GOT_STATUS_UNVERSIONED, 0,
1961 repo, progress_cb, progress_arg);
1962 } else {
1963 err = install_blob(worktree, ondisk_path, path,
1964 te->mode, sb.st_mode, blob,
1965 status == GOT_STATUS_MISSING, 0, 0,
1966 status == GOT_STATUS_UNVERSIONED, repo,
1967 progress_cb, progress_arg);
1969 if (err)
1970 goto done;
1972 if (ie) {
1973 err = got_fileindex_entry_update(ie,
1974 worktree->root_fd, path, blob->id.sha1,
1975 worktree->base_commit_id->sha1, 1);
1976 } else {
1977 err = create_fileindex_entry(&ie, fileindex,
1978 worktree->base_commit_id, worktree->root_fd, path,
1979 &blob->id);
1981 if (err)
1982 goto done;
1984 if (is_bad_symlink) {
1985 got_fileindex_entry_filetype_set(ie,
1986 GOT_FILEIDX_MODE_BAD_SYMLINK);
1989 got_object_blob_close(blob);
1990 done:
1991 free(ondisk_path);
1992 return err;
1995 static const struct got_error *
1996 remove_ondisk_file(const char *root_path, const char *path)
1998 const struct got_error *err = NULL;
1999 char *ondisk_path = NULL, *parent = NULL;
2001 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2002 return got_error_from_errno("asprintf");
2004 if (unlink(ondisk_path) == -1) {
2005 if (errno != ENOENT)
2006 err = got_error_from_errno2("unlink", ondisk_path);
2007 } else {
2008 size_t root_len = strlen(root_path);
2009 err = got_path_dirname(&parent, ondisk_path);
2010 if (err)
2011 goto done;
2012 while (got_path_cmp(parent, root_path,
2013 strlen(parent), root_len) != 0) {
2014 free(ondisk_path);
2015 ondisk_path = parent;
2016 parent = NULL;
2017 if (rmdir(ondisk_path) == -1) {
2018 if (errno != ENOTEMPTY)
2019 err = got_error_from_errno2("rmdir",
2020 ondisk_path);
2021 break;
2023 err = got_path_dirname(&parent, ondisk_path);
2024 if (err)
2025 break;
2028 done:
2029 free(ondisk_path);
2030 free(parent);
2031 return err;
2034 static const struct got_error *
2035 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2036 struct got_fileindex_entry *ie, struct got_repository *repo,
2037 got_worktree_checkout_cb progress_cb, void *progress_arg)
2039 const struct got_error *err = NULL;
2040 unsigned char status;
2041 struct stat sb;
2042 char *ondisk_path;
2044 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2045 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2047 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2048 == -1)
2049 return got_error_from_errno("asprintf");
2051 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2052 if (err)
2053 goto done;
2055 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2056 char ondisk_target[PATH_MAX];
2057 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2058 sizeof(ondisk_target));
2059 if (ondisk_len == -1) {
2060 err = got_error_from_errno2("readlink", ondisk_path);
2061 goto done;
2063 ondisk_target[ondisk_len] = '\0';
2064 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2065 NULL, NULL, /* XXX pass common ancestor info? */
2066 ondisk_target, ondisk_path);
2067 if (err)
2068 goto done;
2069 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2070 ie->path);
2071 goto done;
2074 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2075 status == GOT_STATUS_ADD) {
2076 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2077 if (err)
2078 goto done;
2080 * Preserve the working file and change the deleted blob's
2081 * entry into a schedule-add entry.
2083 err = got_fileindex_entry_update(ie, worktree->root_fd,
2084 ie->path, NULL, NULL, 0);
2085 } else {
2086 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2087 if (err)
2088 goto done;
2089 if (status == GOT_STATUS_NO_CHANGE) {
2090 err = remove_ondisk_file(worktree->root_path, ie->path);
2091 if (err)
2092 goto done;
2094 got_fileindex_entry_remove(fileindex, ie);
2096 done:
2097 free(ondisk_path);
2098 return err;
2101 struct diff_cb_arg {
2102 struct got_fileindex *fileindex;
2103 struct got_worktree *worktree;
2104 struct got_repository *repo;
2105 got_worktree_checkout_cb progress_cb;
2106 void *progress_arg;
2107 got_cancel_cb cancel_cb;
2108 void *cancel_arg;
2111 static const struct got_error *
2112 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2113 struct got_tree_entry *te, const char *parent_path)
2115 struct diff_cb_arg *a = arg;
2117 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2118 return got_error(GOT_ERR_CANCELLED);
2120 return update_blob(a->worktree, a->fileindex, ie, te,
2121 ie->path, a->repo, a->progress_cb, a->progress_arg);
2124 static const struct got_error *
2125 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2127 struct diff_cb_arg *a = arg;
2129 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2130 return got_error(GOT_ERR_CANCELLED);
2132 return delete_blob(a->worktree, a->fileindex, ie,
2133 a->repo, a->progress_cb, a->progress_arg);
2136 static const struct got_error *
2137 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2139 struct diff_cb_arg *a = arg;
2140 const struct got_error *err;
2141 char *path;
2143 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2144 return got_error(GOT_ERR_CANCELLED);
2146 if (got_object_tree_entry_is_submodule(te))
2147 return NULL;
2149 if (asprintf(&path, "%s%s%s", parent_path,
2150 parent_path[0] ? "/" : "", te->name)
2151 == -1)
2152 return got_error_from_errno("asprintf");
2154 if (S_ISDIR(te->mode))
2155 err = add_dir_on_disk(a->worktree, path);
2156 else
2157 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2158 a->repo, a->progress_cb, a->progress_arg);
2160 free(path);
2161 return err;
2164 const struct got_error *
2165 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2167 uint32_t uuid_status;
2169 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2170 if (uuid_status != uuid_s_ok) {
2171 *uuidstr = NULL;
2172 return got_error_uuid(uuid_status, "uuid_to_string");
2175 return NULL;
2178 static const struct got_error *
2179 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2181 const struct got_error *err = NULL;
2182 char *uuidstr = NULL;
2184 *refname = NULL;
2186 err = got_worktree_get_uuid(&uuidstr, worktree);
2187 if (err)
2188 return err;
2190 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2191 err = got_error_from_errno("asprintf");
2192 *refname = NULL;
2194 free(uuidstr);
2195 return err;
2198 const struct got_error *
2199 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2201 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2204 static const struct got_error *
2205 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2207 return get_ref_name(refname, worktree,
2208 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2211 static const struct got_error *
2212 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2214 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2217 static const struct got_error *
2218 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2220 return get_ref_name(refname, worktree,
2221 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2224 static const struct got_error *
2225 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2227 return get_ref_name(refname, worktree,
2228 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2231 static const struct got_error *
2232 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2234 return get_ref_name(refname, worktree,
2235 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2238 static const struct got_error *
2239 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2241 return get_ref_name(refname, worktree,
2242 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2245 static const struct got_error *
2246 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2248 return get_ref_name(refname, worktree,
2249 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2252 static const struct got_error *
2253 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2255 return get_ref_name(refname, worktree,
2256 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2259 const struct got_error *
2260 got_worktree_get_histedit_script_path(char **path,
2261 struct got_worktree *worktree)
2263 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2264 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2265 *path = NULL;
2266 return got_error_from_errno("asprintf");
2268 return NULL;
2271 static const struct got_error *
2272 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2274 return get_ref_name(refname, worktree,
2275 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2278 static const struct got_error *
2279 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2281 return get_ref_name(refname, worktree,
2282 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2286 * Prevent Git's garbage collector from deleting our base commit by
2287 * setting a reference to our base commit's ID.
2289 static const struct got_error *
2290 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2292 const struct got_error *err = NULL;
2293 struct got_reference *ref = NULL;
2294 char *refname;
2296 err = got_worktree_get_base_ref_name(&refname, worktree);
2297 if (err)
2298 return err;
2300 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2301 if (err)
2302 goto done;
2304 err = got_ref_write(ref, repo);
2305 done:
2306 free(refname);
2307 if (ref)
2308 got_ref_close(ref);
2309 return err;
2312 static const struct got_error *
2313 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2315 const struct got_error *err = NULL;
2317 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2318 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2319 err = got_error_from_errno("asprintf");
2320 *fileindex_path = NULL;
2322 return err;
2326 static const struct got_error *
2327 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2328 struct got_worktree *worktree)
2330 const struct got_error *err = NULL;
2331 FILE *index = NULL;
2333 *fileindex_path = NULL;
2334 *fileindex = got_fileindex_alloc();
2335 if (*fileindex == NULL)
2336 return got_error_from_errno("got_fileindex_alloc");
2338 err = get_fileindex_path(fileindex_path, worktree);
2339 if (err)
2340 goto done;
2342 index = fopen(*fileindex_path, "rbe");
2343 if (index == NULL) {
2344 if (errno != ENOENT)
2345 err = got_error_from_errno2("fopen", *fileindex_path);
2346 } else {
2347 err = got_fileindex_read(*fileindex, index);
2348 if (fclose(index) == EOF && err == NULL)
2349 err = got_error_from_errno("fclose");
2351 done:
2352 if (err) {
2353 free(*fileindex_path);
2354 *fileindex_path = NULL;
2355 got_fileindex_free(*fileindex);
2356 *fileindex = NULL;
2358 return err;
2361 struct bump_base_commit_id_arg {
2362 struct got_object_id *base_commit_id;
2363 const char *path;
2364 size_t path_len;
2365 const char *entry_name;
2366 got_worktree_checkout_cb progress_cb;
2367 void *progress_arg;
2370 /* Bump base commit ID of all files within an updated part of the work tree. */
2371 static const struct got_error *
2372 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2374 const struct got_error *err;
2375 struct bump_base_commit_id_arg *a = arg;
2377 if (a->entry_name) {
2378 if (strcmp(ie->path, a->path) != 0)
2379 return NULL;
2380 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2381 return NULL;
2383 if (got_fileindex_entry_was_skipped(ie))
2384 return NULL;
2386 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2387 SHA1_DIGEST_LENGTH) == 0)
2388 return NULL;
2390 if (a->progress_cb) {
2391 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2392 ie->path);
2393 if (err)
2394 return err;
2396 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2397 return NULL;
2400 static const struct got_error *
2401 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2402 struct got_fileindex *fileindex,
2403 got_worktree_checkout_cb progress_cb, void *progress_arg)
2405 struct bump_base_commit_id_arg bbc_arg;
2407 bbc_arg.base_commit_id = worktree->base_commit_id;
2408 bbc_arg.entry_name = NULL;
2409 bbc_arg.path = "";
2410 bbc_arg.path_len = 0;
2411 bbc_arg.progress_cb = progress_cb;
2412 bbc_arg.progress_arg = progress_arg;
2414 return got_fileindex_for_each_entry_safe(fileindex,
2415 bump_base_commit_id, &bbc_arg);
2418 static const struct got_error *
2419 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2421 const struct got_error *err = NULL;
2422 char *new_fileindex_path = NULL;
2423 FILE *new_index = NULL;
2424 struct timespec timeout;
2426 err = got_opentemp_named(&new_fileindex_path, &new_index,
2427 fileindex_path);
2428 if (err)
2429 goto done;
2431 err = got_fileindex_write(fileindex, new_index);
2432 if (err)
2433 goto done;
2435 if (rename(new_fileindex_path, fileindex_path) != 0) {
2436 err = got_error_from_errno3("rename", new_fileindex_path,
2437 fileindex_path);
2438 unlink(new_fileindex_path);
2442 * Sleep for a short amount of time to ensure that files modified after
2443 * this program exits have a different time stamp from the one which
2444 * was recorded in the file index.
2446 timeout.tv_sec = 0;
2447 timeout.tv_nsec = 1;
2448 nanosleep(&timeout, NULL);
2449 done:
2450 if (new_index)
2451 fclose(new_index);
2452 free(new_fileindex_path);
2453 return err;
2456 static const struct got_error *
2457 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2458 struct got_object_id **tree_id, const char *wt_relpath,
2459 struct got_worktree *worktree, struct got_repository *repo)
2461 const struct got_error *err = NULL;
2462 struct got_object_id *id = NULL;
2463 char *in_repo_path = NULL;
2464 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2466 *entry_type = GOT_OBJ_TYPE_ANY;
2467 *tree_relpath = NULL;
2468 *tree_id = NULL;
2470 if (wt_relpath[0] == '\0') {
2471 /* Check out all files within the work tree. */
2472 *entry_type = GOT_OBJ_TYPE_TREE;
2473 *tree_relpath = strdup("");
2474 if (*tree_relpath == NULL) {
2475 err = got_error_from_errno("strdup");
2476 goto done;
2478 err = got_object_id_by_path(tree_id, repo,
2479 worktree->base_commit_id, worktree->path_prefix);
2480 if (err)
2481 goto done;
2482 return NULL;
2485 /* Check out a subset of files in the work tree. */
2487 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2488 is_root_wt ? "" : "/", wt_relpath) == -1) {
2489 err = got_error_from_errno("asprintf");
2490 goto done;
2493 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2494 in_repo_path);
2495 if (err)
2496 goto done;
2498 free(in_repo_path);
2499 in_repo_path = NULL;
2501 err = got_object_get_type(entry_type, repo, id);
2502 if (err)
2503 goto done;
2505 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2506 /* Check out a single file. */
2507 if (strchr(wt_relpath, '/') == NULL) {
2508 /* Check out a single file in work tree's root dir. */
2509 in_repo_path = strdup(worktree->path_prefix);
2510 if (in_repo_path == NULL) {
2511 err = got_error_from_errno("strdup");
2512 goto done;
2514 *tree_relpath = strdup("");
2515 if (*tree_relpath == NULL) {
2516 err = got_error_from_errno("strdup");
2517 goto done;
2519 } else {
2520 /* Check out a single file in a subdirectory. */
2521 err = got_path_dirname(tree_relpath, wt_relpath);
2522 if (err)
2523 return err;
2524 if (asprintf(&in_repo_path, "%s%s%s",
2525 worktree->path_prefix, is_root_wt ? "" : "/",
2526 *tree_relpath) == -1) {
2527 err = got_error_from_errno("asprintf");
2528 goto done;
2531 err = got_object_id_by_path(tree_id, repo,
2532 worktree->base_commit_id, in_repo_path);
2533 } else {
2534 /* Check out all files within a subdirectory. */
2535 *tree_id = got_object_id_dup(id);
2536 if (*tree_id == NULL) {
2537 err = got_error_from_errno("got_object_id_dup");
2538 goto done;
2540 *tree_relpath = strdup(wt_relpath);
2541 if (*tree_relpath == NULL) {
2542 err = got_error_from_errno("strdup");
2543 goto done;
2546 done:
2547 free(id);
2548 free(in_repo_path);
2549 if (err) {
2550 *entry_type = GOT_OBJ_TYPE_ANY;
2551 free(*tree_relpath);
2552 *tree_relpath = NULL;
2553 free(*tree_id);
2554 *tree_id = NULL;
2556 return err;
2559 static const struct got_error *
2560 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2561 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2562 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2563 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2565 const struct got_error *err = NULL;
2566 struct got_commit_object *commit = NULL;
2567 struct got_tree_object *tree = NULL;
2568 struct got_fileindex_diff_tree_cb diff_cb;
2569 struct diff_cb_arg arg;
2571 err = ref_base_commit(worktree, repo);
2572 if (err) {
2573 if (!(err->code == GOT_ERR_ERRNO &&
2574 (errno == EACCES || errno == EROFS)))
2575 goto done;
2576 err = (*progress_cb)(progress_arg,
2577 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2578 if (err)
2579 return err;
2582 err = got_object_open_as_commit(&commit, repo,
2583 worktree->base_commit_id);
2584 if (err)
2585 goto done;
2587 err = got_object_open_as_tree(&tree, repo, tree_id);
2588 if (err)
2589 goto done;
2591 if (entry_name &&
2592 got_object_tree_find_entry(tree, entry_name) == NULL) {
2593 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2594 goto done;
2597 diff_cb.diff_old_new = diff_old_new;
2598 diff_cb.diff_old = diff_old;
2599 diff_cb.diff_new = diff_new;
2600 arg.fileindex = fileindex;
2601 arg.worktree = worktree;
2602 arg.repo = repo;
2603 arg.progress_cb = progress_cb;
2604 arg.progress_arg = progress_arg;
2605 arg.cancel_cb = cancel_cb;
2606 arg.cancel_arg = cancel_arg;
2607 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2608 entry_name, repo, &diff_cb, &arg);
2609 done:
2610 if (tree)
2611 got_object_tree_close(tree);
2612 if (commit)
2613 got_object_commit_close(commit);
2614 return err;
2617 const struct got_error *
2618 got_worktree_checkout_files(struct got_worktree *worktree,
2619 struct got_pathlist_head *paths, struct got_repository *repo,
2620 got_worktree_checkout_cb progress_cb, void *progress_arg,
2621 got_cancel_cb cancel_cb, void *cancel_arg)
2623 const struct got_error *err = NULL, *sync_err, *unlockerr;
2624 struct got_commit_object *commit = NULL;
2625 struct got_tree_object *tree = NULL;
2626 struct got_fileindex *fileindex = NULL;
2627 char *fileindex_path = NULL;
2628 struct got_pathlist_entry *pe;
2629 struct tree_path_data {
2630 STAILQ_ENTRY(tree_path_data) entry;
2631 struct got_object_id *tree_id;
2632 int entry_type;
2633 char *relpath;
2634 char *entry_name;
2635 } *tpd = NULL;
2636 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2638 STAILQ_INIT(&tree_paths);
2640 err = lock_worktree(worktree, LOCK_EX);
2641 if (err)
2642 return err;
2644 /* Map all specified paths to in-repository trees. */
2645 TAILQ_FOREACH(pe, paths, entry) {
2646 tpd = malloc(sizeof(*tpd));
2647 if (tpd == NULL) {
2648 err = got_error_from_errno("malloc");
2649 goto done;
2652 err = find_tree_entry_for_checkout(&tpd->entry_type,
2653 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2654 if (err) {
2655 free(tpd);
2656 goto done;
2659 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2660 err = got_path_basename(&tpd->entry_name, pe->path);
2661 if (err) {
2662 free(tpd->relpath);
2663 free(tpd->tree_id);
2664 free(tpd);
2665 goto done;
2667 } else
2668 tpd->entry_name = NULL;
2670 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2674 * Read the file index.
2675 * Checking out files is supposed to be an idempotent operation.
2676 * If the on-disk file index is incomplete we will try to complete it.
2678 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2679 if (err)
2680 goto done;
2682 tpd = STAILQ_FIRST(&tree_paths);
2683 TAILQ_FOREACH(pe, paths, entry) {
2684 struct bump_base_commit_id_arg bbc_arg;
2686 err = checkout_files(worktree, fileindex, tpd->relpath,
2687 tpd->tree_id, tpd->entry_name, repo,
2688 progress_cb, progress_arg, cancel_cb, cancel_arg);
2689 if (err)
2690 break;
2692 bbc_arg.base_commit_id = worktree->base_commit_id;
2693 bbc_arg.entry_name = tpd->entry_name;
2694 bbc_arg.path = pe->path;
2695 bbc_arg.path_len = pe->path_len;
2696 bbc_arg.progress_cb = progress_cb;
2697 bbc_arg.progress_arg = progress_arg;
2698 err = got_fileindex_for_each_entry_safe(fileindex,
2699 bump_base_commit_id, &bbc_arg);
2700 if (err)
2701 break;
2703 tpd = STAILQ_NEXT(tpd, entry);
2705 sync_err = sync_fileindex(fileindex, fileindex_path);
2706 if (sync_err && err == NULL)
2707 err = sync_err;
2708 done:
2709 free(fileindex_path);
2710 if (tree)
2711 got_object_tree_close(tree);
2712 if (commit)
2713 got_object_commit_close(commit);
2714 if (fileindex)
2715 got_fileindex_free(fileindex);
2716 while (!STAILQ_EMPTY(&tree_paths)) {
2717 tpd = STAILQ_FIRST(&tree_paths);
2718 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2719 free(tpd->relpath);
2720 free(tpd->tree_id);
2721 free(tpd);
2723 unlockerr = lock_worktree(worktree, LOCK_SH);
2724 if (unlockerr && err == NULL)
2725 err = unlockerr;
2726 return err;
2729 struct merge_file_cb_arg {
2730 struct got_worktree *worktree;
2731 struct got_fileindex *fileindex;
2732 got_worktree_checkout_cb progress_cb;
2733 void *progress_arg;
2734 got_cancel_cb cancel_cb;
2735 void *cancel_arg;
2736 const char *label_orig;
2737 struct got_object_id *commit_id2;
2738 int allow_bad_symlinks;
2741 static const struct got_error *
2742 merge_file_cb(void *arg, struct got_blob_object *blob1,
2743 struct got_blob_object *blob2, struct got_object_id *id1,
2744 struct got_object_id *id2, const char *path1, const char *path2,
2745 mode_t mode1, mode_t mode2, struct got_repository *repo)
2747 static const struct got_error *err = NULL;
2748 struct merge_file_cb_arg *a = arg;
2749 struct got_fileindex_entry *ie;
2750 char *ondisk_path = NULL;
2751 struct stat sb;
2752 unsigned char status;
2753 int local_changes_subsumed;
2754 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2755 char *id_str = NULL, *label_deriv2 = NULL;
2757 if (blob1 && blob2) {
2758 ie = got_fileindex_entry_get(a->fileindex, path2,
2759 strlen(path2));
2760 if (ie == NULL)
2761 return (*a->progress_cb)(a->progress_arg,
2762 GOT_STATUS_MISSING, path2);
2764 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2765 path2) == -1)
2766 return got_error_from_errno("asprintf");
2768 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2769 repo);
2770 if (err)
2771 goto done;
2773 if (status == GOT_STATUS_DELETE) {
2774 err = (*a->progress_cb)(a->progress_arg,
2775 GOT_STATUS_MERGE, path2);
2776 goto done;
2778 if (status != GOT_STATUS_NO_CHANGE &&
2779 status != GOT_STATUS_MODIFY &&
2780 status != GOT_STATUS_CONFLICT &&
2781 status != GOT_STATUS_ADD) {
2782 err = (*a->progress_cb)(a->progress_arg, status, path2);
2783 goto done;
2786 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2787 char *link_target2;
2788 err = got_object_blob_read_to_str(&link_target2, blob2);
2789 if (err)
2790 goto done;
2791 err = merge_symlink(a->worktree, blob1, ondisk_path,
2792 path2, a->label_orig, link_target2, a->commit_id2,
2793 repo, a->progress_cb, a->progress_arg);
2794 free(link_target2);
2795 } else {
2796 int fd;
2798 f_orig = got_opentemp();
2799 if (f_orig == NULL) {
2800 err = got_error_from_errno("got_opentemp");
2801 goto done;
2803 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2804 f_orig, blob1);
2805 if (err)
2806 goto done;
2808 f_deriv2 = got_opentemp();
2809 if (f_deriv2 == NULL)
2810 goto done;
2811 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2812 f_deriv2, blob2);
2813 if (err)
2814 goto done;
2816 fd = open(ondisk_path,
2817 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2818 if (fd == -1) {
2819 err = got_error_from_errno2("open",
2820 ondisk_path);
2821 goto done;
2823 f_deriv = fdopen(fd, "r");
2824 if (f_deriv == NULL) {
2825 err = got_error_from_errno2("fdopen",
2826 ondisk_path);
2827 close(fd);
2828 goto done;
2830 err = got_object_id_str(&id_str, a->commit_id2);
2831 if (err)
2832 goto done;
2833 if (asprintf(&label_deriv2, "%s: commit %s",
2834 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2835 err = got_error_from_errno("asprintf");
2836 goto done;
2838 err = merge_file(&local_changes_subsumed, a->worktree,
2839 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2840 sb.st_mode, a->label_orig, NULL, label_deriv2,
2841 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2842 a->progress_cb, a->progress_arg);
2844 } else if (blob1) {
2845 ie = got_fileindex_entry_get(a->fileindex, path1,
2846 strlen(path1));
2847 if (ie == NULL)
2848 return (*a->progress_cb)(a->progress_arg,
2849 GOT_STATUS_MISSING, path1);
2851 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2852 path1) == -1)
2853 return got_error_from_errno("asprintf");
2855 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2856 repo);
2857 if (err)
2858 goto done;
2860 switch (status) {
2861 case GOT_STATUS_NO_CHANGE:
2862 err = (*a->progress_cb)(a->progress_arg,
2863 GOT_STATUS_DELETE, path1);
2864 if (err)
2865 goto done;
2866 err = remove_ondisk_file(a->worktree->root_path, path1);
2867 if (err)
2868 goto done;
2869 if (ie)
2870 got_fileindex_entry_mark_deleted_from_disk(ie);
2871 break;
2872 case GOT_STATUS_DELETE:
2873 case GOT_STATUS_MISSING:
2874 err = (*a->progress_cb)(a->progress_arg,
2875 GOT_STATUS_DELETE, path1);
2876 if (err)
2877 goto done;
2878 if (ie)
2879 got_fileindex_entry_mark_deleted_from_disk(ie);
2880 break;
2881 case GOT_STATUS_ADD: {
2882 struct got_object_id *id;
2883 FILE *blob1_f;
2884 off_t blob1_size;
2886 * Delete the added file only if its content already
2887 * exists in the repository.
2889 err = got_object_blob_file_create(&id, &blob1_f,
2890 &blob1_size, path1);
2891 if (err)
2892 goto done;
2893 if (got_object_id_cmp(id, id1) == 0) {
2894 err = (*a->progress_cb)(a->progress_arg,
2895 GOT_STATUS_DELETE, path1);
2896 if (err)
2897 goto done;
2898 err = remove_ondisk_file(a->worktree->root_path,
2899 path1);
2900 if (err)
2901 goto done;
2902 if (ie)
2903 got_fileindex_entry_remove(a->fileindex,
2904 ie);
2905 } else {
2906 err = (*a->progress_cb)(a->progress_arg,
2907 GOT_STATUS_CANNOT_DELETE, path1);
2909 if (fclose(blob1_f) == EOF && err == NULL)
2910 err = got_error_from_errno("fclose");
2911 free(id);
2912 if (err)
2913 goto done;
2914 break;
2916 case GOT_STATUS_MODIFY:
2917 case GOT_STATUS_CONFLICT:
2918 err = (*a->progress_cb)(a->progress_arg,
2919 GOT_STATUS_CANNOT_DELETE, path1);
2920 if (err)
2921 goto done;
2922 break;
2923 case GOT_STATUS_OBSTRUCTED:
2924 err = (*a->progress_cb)(a->progress_arg, status, path1);
2925 if (err)
2926 goto done;
2927 break;
2928 default:
2929 break;
2931 } else if (blob2) {
2932 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2933 path2) == -1)
2934 return got_error_from_errno("asprintf");
2935 ie = got_fileindex_entry_get(a->fileindex, path2,
2936 strlen(path2));
2937 if (ie) {
2938 err = get_file_status(&status, &sb, ie, ondisk_path,
2939 -1, NULL, repo);
2940 if (err)
2941 goto done;
2942 if (status != GOT_STATUS_NO_CHANGE &&
2943 status != GOT_STATUS_MODIFY &&
2944 status != GOT_STATUS_CONFLICT &&
2945 status != GOT_STATUS_ADD) {
2946 err = (*a->progress_cb)(a->progress_arg,
2947 status, path2);
2948 goto done;
2950 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2951 char *link_target2;
2952 err = got_object_blob_read_to_str(&link_target2,
2953 blob2);
2954 if (err)
2955 goto done;
2956 err = merge_symlink(a->worktree, NULL,
2957 ondisk_path, path2, a->label_orig,
2958 link_target2, a->commit_id2, repo,
2959 a->progress_cb, a->progress_arg);
2960 free(link_target2);
2961 } else if (S_ISREG(sb.st_mode)) {
2962 err = merge_blob(&local_changes_subsumed,
2963 a->worktree, NULL, ondisk_path, path2,
2964 sb.st_mode, a->label_orig, blob2,
2965 a->commit_id2, repo, a->progress_cb,
2966 a->progress_arg);
2967 } else {
2968 err = got_error_path(ondisk_path,
2969 GOT_ERR_FILE_OBSTRUCTED);
2971 if (err)
2972 goto done;
2973 if (status == GOT_STATUS_DELETE) {
2974 err = got_fileindex_entry_update(ie,
2975 a->worktree->root_fd, path2, blob2->id.sha1,
2976 a->worktree->base_commit_id->sha1, 0);
2977 if (err)
2978 goto done;
2980 } else {
2981 int is_bad_symlink = 0;
2982 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2983 if (S_ISLNK(mode2)) {
2984 err = install_symlink(&is_bad_symlink,
2985 a->worktree, ondisk_path, path2, blob2, 0,
2986 0, 1, a->allow_bad_symlinks, repo,
2987 a->progress_cb, a->progress_arg);
2988 } else {
2989 err = install_blob(a->worktree, ondisk_path, path2,
2990 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2991 a->progress_cb, a->progress_arg);
2993 if (err)
2994 goto done;
2995 err = got_fileindex_entry_alloc(&ie, path2);
2996 if (err)
2997 goto done;
2998 err = got_fileindex_entry_update(ie,
2999 a->worktree->root_fd, path2, NULL, NULL, 1);
3000 if (err) {
3001 got_fileindex_entry_free(ie);
3002 goto done;
3004 err = got_fileindex_entry_add(a->fileindex, ie);
3005 if (err) {
3006 got_fileindex_entry_free(ie);
3007 goto done;
3009 if (is_bad_symlink) {
3010 got_fileindex_entry_filetype_set(ie,
3011 GOT_FILEIDX_MODE_BAD_SYMLINK);
3015 done:
3016 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3017 err = got_error_from_errno("fclose");
3018 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3019 err = got_error_from_errno("fclose");
3020 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3021 err = got_error_from_errno("fclose");
3022 free(id_str);
3023 free(label_deriv2);
3024 free(ondisk_path);
3025 return err;
3028 static const struct got_error *
3029 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3031 struct got_worktree *worktree = arg;
3033 /* Reject merges into a work tree with mixed base commits. */
3034 if (got_fileindex_entry_has_commit(ie) &&
3035 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3036 SHA1_DIGEST_LENGTH) != 0)
3037 return got_error(GOT_ERR_MIXED_COMMITS);
3039 return NULL;
3042 struct check_merge_conflicts_arg {
3043 struct got_worktree *worktree;
3044 struct got_fileindex *fileindex;
3045 struct got_repository *repo;
3048 static const struct got_error *
3049 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3050 struct got_blob_object *blob2, struct got_object_id *id1,
3051 struct got_object_id *id2, const char *path1, const char *path2,
3052 mode_t mode1, mode_t mode2, struct got_repository *repo)
3054 const struct got_error *err = NULL;
3055 struct check_merge_conflicts_arg *a = arg;
3056 unsigned char status;
3057 struct stat sb;
3058 struct got_fileindex_entry *ie;
3059 const char *path = path2 ? path2 : path1;
3060 struct got_object_id *id = id2 ? id2 : id1;
3061 char *ondisk_path;
3063 if (id == NULL)
3064 return NULL;
3066 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3067 if (ie == NULL)
3068 return NULL;
3070 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3071 == -1)
3072 return got_error_from_errno("asprintf");
3074 /* Reject merges into a work tree with conflicted files. */
3075 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3076 free(ondisk_path);
3077 if (err)
3078 return err;
3079 if (status == GOT_STATUS_CONFLICT)
3080 return got_error(GOT_ERR_CONFLICTS);
3082 return NULL;
3085 static const struct got_error *
3086 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3087 const char *fileindex_path, struct got_object_id *commit_id1,
3088 struct got_object_id *commit_id2, struct got_repository *repo,
3089 got_worktree_checkout_cb progress_cb, void *progress_arg,
3090 got_cancel_cb cancel_cb, void *cancel_arg)
3092 const struct got_error *err = NULL, *sync_err;
3093 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3094 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3095 struct check_merge_conflicts_arg cmc_arg;
3096 struct merge_file_cb_arg arg;
3097 char *label_orig = NULL;
3099 if (commit_id1) {
3100 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3101 worktree->path_prefix);
3102 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3103 goto done;
3105 if (tree_id1) {
3106 char *id_str;
3108 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3109 if (err)
3110 goto done;
3112 err = got_object_id_str(&id_str, commit_id1);
3113 if (err)
3114 goto done;
3116 if (asprintf(&label_orig, "%s: commit %s",
3117 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3118 err = got_error_from_errno("asprintf");
3119 free(id_str);
3120 goto done;
3122 free(id_str);
3125 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3126 worktree->path_prefix);
3127 if (err)
3128 goto done;
3130 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3131 if (err)
3132 goto done;
3134 cmc_arg.worktree = worktree;
3135 cmc_arg.fileindex = fileindex;
3136 cmc_arg.repo = repo;
3137 err = got_diff_tree(tree1, tree2, "", "", repo,
3138 check_merge_conflicts, &cmc_arg, 0);
3139 if (err)
3140 goto done;
3142 arg.worktree = worktree;
3143 arg.fileindex = fileindex;
3144 arg.progress_cb = progress_cb;
3145 arg.progress_arg = progress_arg;
3146 arg.cancel_cb = cancel_cb;
3147 arg.cancel_arg = cancel_arg;
3148 arg.label_orig = label_orig;
3149 arg.commit_id2 = commit_id2;
3150 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3151 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3152 sync_err = sync_fileindex(fileindex, fileindex_path);
3153 if (sync_err && err == NULL)
3154 err = sync_err;
3155 done:
3156 if (tree1)
3157 got_object_tree_close(tree1);
3158 if (tree2)
3159 got_object_tree_close(tree2);
3160 free(label_orig);
3161 return err;
3164 const struct got_error *
3165 got_worktree_merge_files(struct got_worktree *worktree,
3166 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3167 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3168 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3170 const struct got_error *err, *unlockerr;
3171 char *fileindex_path = NULL;
3172 struct got_fileindex *fileindex = NULL;
3174 err = lock_worktree(worktree, LOCK_EX);
3175 if (err)
3176 return err;
3178 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3179 if (err)
3180 goto done;
3182 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3183 worktree);
3184 if (err)
3185 goto done;
3187 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3188 commit_id2, repo, progress_cb, progress_arg,
3189 cancel_cb, cancel_arg);
3190 done:
3191 if (fileindex)
3192 got_fileindex_free(fileindex);
3193 free(fileindex_path);
3194 unlockerr = lock_worktree(worktree, LOCK_SH);
3195 if (unlockerr && err == NULL)
3196 err = unlockerr;
3197 return err;
3200 struct diff_dir_cb_arg {
3201 struct got_fileindex *fileindex;
3202 struct got_worktree *worktree;
3203 const char *status_path;
3204 size_t status_path_len;
3205 struct got_repository *repo;
3206 got_worktree_status_cb status_cb;
3207 void *status_arg;
3208 got_cancel_cb cancel_cb;
3209 void *cancel_arg;
3210 /* A pathlist containing per-directory pathlists of ignore patterns. */
3211 struct got_pathlist_head *ignores;
3212 int report_unchanged;
3213 int no_ignores;
3216 static const struct got_error *
3217 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3218 int dirfd, const char *de_name,
3219 got_worktree_status_cb status_cb, void *status_arg,
3220 struct got_repository *repo, int report_unchanged)
3222 const struct got_error *err = NULL;
3223 unsigned char status = GOT_STATUS_NO_CHANGE;
3224 unsigned char staged_status = get_staged_status(ie);
3225 struct stat sb;
3226 struct got_object_id blob_id, commit_id, staged_blob_id;
3227 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3228 struct got_object_id *staged_blob_idp = NULL;
3230 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3231 if (err)
3232 return err;
3234 if (status == GOT_STATUS_NO_CHANGE &&
3235 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3236 return NULL;
3238 if (got_fileindex_entry_has_blob(ie)) {
3239 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3240 blob_idp = &blob_id;
3242 if (got_fileindex_entry_has_commit(ie)) {
3243 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3244 commit_idp = &commit_id;
3246 if (staged_status == GOT_STATUS_ADD ||
3247 staged_status == GOT_STATUS_MODIFY) {
3248 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3249 SHA1_DIGEST_LENGTH);
3250 staged_blob_idp = &staged_blob_id;
3253 return (*status_cb)(status_arg, status, staged_status,
3254 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3257 static const struct got_error *
3258 status_old_new(void *arg, struct got_fileindex_entry *ie,
3259 struct dirent *de, const char *parent_path, int dirfd)
3261 const struct got_error *err = NULL;
3262 struct diff_dir_cb_arg *a = arg;
3263 char *abspath;
3265 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3266 return got_error(GOT_ERR_CANCELLED);
3268 if (got_path_cmp(parent_path, a->status_path,
3269 strlen(parent_path), a->status_path_len) != 0 &&
3270 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3271 return NULL;
3273 if (parent_path[0]) {
3274 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3275 parent_path, de->d_name) == -1)
3276 return got_error_from_errno("asprintf");
3277 } else {
3278 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3279 de->d_name) == -1)
3280 return got_error_from_errno("asprintf");
3283 err = report_file_status(ie, abspath, dirfd, de->d_name,
3284 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3285 free(abspath);
3286 return err;
3289 static const struct got_error *
3290 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3292 struct diff_dir_cb_arg *a = arg;
3293 struct got_object_id blob_id, commit_id;
3294 unsigned char status;
3296 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3297 return got_error(GOT_ERR_CANCELLED);
3299 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3300 return NULL;
3302 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3303 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3304 if (got_fileindex_entry_has_file_on_disk(ie))
3305 status = GOT_STATUS_MISSING;
3306 else
3307 status = GOT_STATUS_DELETE;
3308 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3309 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3312 void
3313 free_ignorelist(struct got_pathlist_head *ignorelist)
3315 struct got_pathlist_entry *pe;
3317 TAILQ_FOREACH(pe, ignorelist, entry)
3318 free((char *)pe->path);
3319 got_pathlist_free(ignorelist);
3322 void
3323 free_ignores(struct got_pathlist_head *ignores)
3325 struct got_pathlist_entry *pe;
3327 TAILQ_FOREACH(pe, ignores, entry) {
3328 struct got_pathlist_head *ignorelist = pe->data;
3329 free_ignorelist(ignorelist);
3330 free((char *)pe->path);
3332 got_pathlist_free(ignores);
3335 static const struct got_error *
3336 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3338 const struct got_error *err = NULL;
3339 struct got_pathlist_entry *pe = NULL;
3340 struct got_pathlist_head *ignorelist;
3341 char *line = NULL, *pattern, *dirpath = NULL;
3342 size_t linesize = 0;
3343 ssize_t linelen;
3345 ignorelist = calloc(1, sizeof(*ignorelist));
3346 if (ignorelist == NULL)
3347 return got_error_from_errno("calloc");
3348 TAILQ_INIT(ignorelist);
3350 while ((linelen = getline(&line, &linesize, f)) != -1) {
3351 if (linelen > 0 && line[linelen - 1] == '\n')
3352 line[linelen - 1] = '\0';
3354 /* Git's ignores may contain comments. */
3355 if (line[0] == '#')
3356 continue;
3358 /* Git's negated patterns are not (yet?) supported. */
3359 if (line[0] == '!')
3360 continue;
3362 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3363 line) == -1) {
3364 err = got_error_from_errno("asprintf");
3365 goto done;
3367 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3368 if (err)
3369 goto done;
3371 if (ferror(f)) {
3372 err = got_error_from_errno("getline");
3373 goto done;
3376 dirpath = strdup(path);
3377 if (dirpath == NULL) {
3378 err = got_error_from_errno("strdup");
3379 goto done;
3381 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3382 done:
3383 free(line);
3384 if (err || pe == NULL) {
3385 free(dirpath);
3386 free_ignorelist(ignorelist);
3388 return err;
3391 int
3392 match_ignores(struct got_pathlist_head *ignores, const char *path)
3394 struct got_pathlist_entry *pe;
3396 /* Handle patterns which match in all directories. */
3397 TAILQ_FOREACH(pe, ignores, entry) {
3398 struct got_pathlist_head *ignorelist = pe->data;
3399 struct got_pathlist_entry *pi;
3401 TAILQ_FOREACH(pi, ignorelist, entry) {
3402 const char *p, *pattern = pi->path;
3404 if (strncmp(pattern, "**/", 3) != 0)
3405 continue;
3406 pattern += 3;
3407 p = path;
3408 while (*p) {
3409 if (fnmatch(pattern, p,
3410 FNM_PATHNAME | FNM_LEADING_DIR)) {
3411 /* Retry in next directory. */
3412 while (*p && *p != '/')
3413 p++;
3414 while (*p == '/')
3415 p++;
3416 continue;
3418 return 1;
3424 * The ignores pathlist contains ignore lists from children before
3425 * parents, so we can find the most specific ignorelist by walking
3426 * ignores backwards.
3428 pe = TAILQ_LAST(ignores, got_pathlist_head);
3429 while (pe) {
3430 if (got_path_is_child(path, pe->path, pe->path_len)) {
3431 struct got_pathlist_head *ignorelist = pe->data;
3432 struct got_pathlist_entry *pi;
3433 TAILQ_FOREACH(pi, ignorelist, entry) {
3434 const char *pattern = pi->path;
3435 int flags = FNM_LEADING_DIR;
3436 if (strstr(pattern, "/**/") == NULL)
3437 flags |= FNM_PATHNAME;
3438 if (fnmatch(pattern, path, flags))
3439 continue;
3440 return 1;
3443 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3446 return 0;
3449 static const struct got_error *
3450 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3451 const char *path, int dirfd, const char *ignores_filename)
3453 const struct got_error *err = NULL;
3454 char *ignorespath;
3455 int fd = -1;
3456 FILE *ignoresfile = NULL;
3458 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3459 path[0] ? "/" : "", ignores_filename) == -1)
3460 return got_error_from_errno("asprintf");
3462 if (dirfd != -1) {
3463 fd = openat(dirfd, ignores_filename,
3464 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3465 if (fd == -1) {
3466 if (errno != ENOENT && errno != EACCES)
3467 err = got_error_from_errno2("openat",
3468 ignorespath);
3469 } else {
3470 ignoresfile = fdopen(fd, "r");
3471 if (ignoresfile == NULL)
3472 err = got_error_from_errno2("fdopen",
3473 ignorespath);
3474 else {
3475 fd = -1;
3476 err = read_ignores(ignores, path, ignoresfile);
3479 } else {
3480 ignoresfile = fopen(ignorespath, "re");
3481 if (ignoresfile == NULL) {
3482 if (errno != ENOENT && errno != EACCES)
3483 err = got_error_from_errno2("fopen",
3484 ignorespath);
3485 } else
3486 err = read_ignores(ignores, path, ignoresfile);
3489 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3490 err = got_error_from_errno2("fclose", path);
3491 if (fd != -1 && close(fd) == -1 && err == NULL)
3492 err = got_error_from_errno2("close", path);
3493 free(ignorespath);
3494 return err;
3497 static const struct got_error *
3498 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3499 int dirfd)
3501 const struct got_error *err = NULL;
3502 struct diff_dir_cb_arg *a = arg;
3503 char *path = NULL;
3505 if (ignore != NULL)
3506 *ignore = 0;
3508 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3509 return got_error(GOT_ERR_CANCELLED);
3511 if (parent_path[0]) {
3512 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3513 return got_error_from_errno("asprintf");
3514 } else {
3515 path = de->d_name;
3518 if (de->d_type == DT_DIR) {
3519 if (!a->no_ignores && ignore != NULL &&
3520 match_ignores(a->ignores, path))
3521 *ignore = 1;
3522 } else if (!match_ignores(a->ignores, path) &&
3523 got_path_is_child(path, a->status_path, a->status_path_len))
3524 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3525 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3526 if (parent_path[0])
3527 free(path);
3528 return err;
3531 static const struct got_error *
3532 status_traverse(void *arg, const char *path, int dirfd)
3534 const struct got_error *err = NULL;
3535 struct diff_dir_cb_arg *a = arg;
3537 if (a->no_ignores)
3538 return NULL;
3540 err = add_ignores(a->ignores, a->worktree->root_path,
3541 path, dirfd, ".cvsignore");
3542 if (err)
3543 return err;
3545 err = add_ignores(a->ignores, a->worktree->root_path, path,
3546 dirfd, ".gitignore");
3548 return err;
3551 static const struct got_error *
3552 report_single_file_status(const char *path, const char *ondisk_path,
3553 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3554 void *status_arg, struct got_repository *repo, int report_unchanged,
3555 struct got_pathlist_head *ignores, int no_ignores)
3557 struct got_fileindex_entry *ie;
3558 struct stat sb;
3560 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3561 if (ie)
3562 return report_file_status(ie, ondisk_path, -1, NULL,
3563 status_cb, status_arg, repo, report_unchanged);
3565 if (lstat(ondisk_path, &sb) == -1) {
3566 if (errno != ENOENT)
3567 return got_error_from_errno2("lstat", ondisk_path);
3568 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3569 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3572 if (!no_ignores && match_ignores(ignores, path))
3573 return NULL;
3575 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3576 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3577 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3579 return NULL;
3582 static const struct got_error *
3583 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3584 const char *root_path, const char *path)
3586 const struct got_error *err;
3587 char *parent_path, *next_parent_path = NULL;
3589 err = add_ignores(ignores, root_path, "", -1,
3590 ".cvsignore");
3591 if (err)
3592 return err;
3594 err = add_ignores(ignores, root_path, "", -1,
3595 ".gitignore");
3596 if (err)
3597 return err;
3599 err = got_path_dirname(&parent_path, path);
3600 if (err) {
3601 if (err->code == GOT_ERR_BAD_PATH)
3602 return NULL; /* cannot traverse parent */
3603 return err;
3605 for (;;) {
3606 err = add_ignores(ignores, root_path, parent_path, -1,
3607 ".cvsignore");
3608 if (err)
3609 break;
3610 err = add_ignores(ignores, root_path, parent_path, -1,
3611 ".gitignore");
3612 if (err)
3613 break;
3614 err = got_path_dirname(&next_parent_path, parent_path);
3615 if (err) {
3616 if (err->code == GOT_ERR_BAD_PATH)
3617 err = NULL; /* traversed everything */
3618 break;
3620 if (got_path_is_root_dir(parent_path))
3621 break;
3622 free(parent_path);
3623 parent_path = next_parent_path;
3624 next_parent_path = NULL;
3627 free(parent_path);
3628 free(next_parent_path);
3629 return err;
3632 static const struct got_error *
3633 worktree_status(struct got_worktree *worktree, const char *path,
3634 struct got_fileindex *fileindex, struct got_repository *repo,
3635 got_worktree_status_cb status_cb, void *status_arg,
3636 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3637 int report_unchanged)
3639 const struct got_error *err = NULL;
3640 int fd = -1;
3641 struct got_fileindex_diff_dir_cb fdiff_cb;
3642 struct diff_dir_cb_arg arg;
3643 char *ondisk_path = NULL;
3644 struct got_pathlist_head ignores;
3645 struct got_fileindex_entry *ie;
3647 TAILQ_INIT(&ignores);
3649 if (asprintf(&ondisk_path, "%s%s%s",
3650 worktree->root_path, path[0] ? "/" : "", path) == -1)
3651 return got_error_from_errno("asprintf");
3653 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3654 if (ie) {
3655 err = report_single_file_status(path, ondisk_path,
3656 fileindex, status_cb, status_arg, repo,
3657 report_unchanged, &ignores, no_ignores);
3658 goto done;
3661 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3662 if (fd == -1) {
3663 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3664 !got_err_open_nofollow_on_symlink())
3665 err = got_error_from_errno2("open", ondisk_path);
3666 else {
3667 if (!no_ignores) {
3668 err = add_ignores_from_parent_paths(&ignores,
3669 worktree->root_path, ondisk_path);
3670 if (err)
3671 goto done;
3673 err = report_single_file_status(path, ondisk_path,
3674 fileindex, status_cb, status_arg, repo,
3675 report_unchanged, &ignores, no_ignores);
3677 } else {
3678 fdiff_cb.diff_old_new = status_old_new;
3679 fdiff_cb.diff_old = status_old;
3680 fdiff_cb.diff_new = status_new;
3681 fdiff_cb.diff_traverse = status_traverse;
3682 arg.fileindex = fileindex;
3683 arg.worktree = worktree;
3684 arg.status_path = path;
3685 arg.status_path_len = strlen(path);
3686 arg.repo = repo;
3687 arg.status_cb = status_cb;
3688 arg.status_arg = status_arg;
3689 arg.cancel_cb = cancel_cb;
3690 arg.cancel_arg = cancel_arg;
3691 arg.report_unchanged = report_unchanged;
3692 arg.no_ignores = no_ignores;
3693 if (!no_ignores) {
3694 err = add_ignores_from_parent_paths(&ignores,
3695 worktree->root_path, path);
3696 if (err)
3697 goto done;
3699 arg.ignores = &ignores;
3700 err = got_fileindex_diff_dir(fileindex, fd,
3701 worktree->root_path, path, repo, &fdiff_cb, &arg);
3703 done:
3704 free_ignores(&ignores);
3705 if (fd != -1 && close(fd) == -1 && err == NULL)
3706 err = got_error_from_errno("close");
3707 free(ondisk_path);
3708 return err;
3711 const struct got_error *
3712 got_worktree_status(struct got_worktree *worktree,
3713 struct got_pathlist_head *paths, struct got_repository *repo,
3714 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3715 got_cancel_cb cancel_cb, void *cancel_arg)
3717 const struct got_error *err = NULL;
3718 char *fileindex_path = NULL;
3719 struct got_fileindex *fileindex = NULL;
3720 struct got_pathlist_entry *pe;
3722 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3723 if (err)
3724 return err;
3726 TAILQ_FOREACH(pe, paths, entry) {
3727 err = worktree_status(worktree, pe->path, fileindex, repo,
3728 status_cb, status_arg, cancel_cb, cancel_arg,
3729 no_ignores, 0);
3730 if (err)
3731 break;
3733 free(fileindex_path);
3734 got_fileindex_free(fileindex);
3735 return err;
3738 const struct got_error *
3739 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3740 const char *arg)
3742 const struct got_error *err = NULL;
3743 char *resolved = NULL, *cwd = NULL, *path = NULL;
3744 size_t len;
3745 struct stat sb;
3746 char *abspath = NULL;
3747 char canonpath[PATH_MAX];
3749 *wt_path = NULL;
3751 cwd = getcwd(NULL, 0);
3752 if (cwd == NULL)
3753 return got_error_from_errno("getcwd");
3755 if (lstat(arg, &sb) == -1) {
3756 if (errno != ENOENT) {
3757 err = got_error_from_errno2("lstat", arg);
3758 goto done;
3760 sb.st_mode = 0;
3762 if (S_ISLNK(sb.st_mode)) {
3764 * We cannot use realpath(3) with symlinks since we want to
3765 * operate on the symlink itself.
3766 * But we can make the path absolute, assuming it is relative
3767 * to the current working directory, and then canonicalize it.
3769 if (!got_path_is_absolute(arg)) {
3770 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3771 err = got_error_from_errno("asprintf");
3772 goto done;
3776 err = got_canonpath(abspath ? abspath : arg, canonpath,
3777 sizeof(canonpath));
3778 if (err)
3779 goto done;
3780 resolved = strdup(canonpath);
3781 if (resolved == NULL) {
3782 err = got_error_from_errno("strdup");
3783 goto done;
3785 } else {
3786 resolved = realpath(arg, NULL);
3787 if (resolved == NULL) {
3788 if (errno != ENOENT) {
3789 err = got_error_from_errno2("realpath", arg);
3790 goto done;
3792 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3793 err = got_error_from_errno("asprintf");
3794 goto done;
3796 err = got_canonpath(abspath, canonpath,
3797 sizeof(canonpath));
3798 if (err)
3799 goto done;
3800 resolved = strdup(canonpath);
3801 if (resolved == NULL) {
3802 err = got_error_from_errno("strdup");
3803 goto done;
3808 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3809 strlen(got_worktree_get_root_path(worktree)))) {
3810 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3811 goto done;
3814 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3815 err = got_path_skip_common_ancestor(&path,
3816 got_worktree_get_root_path(worktree), resolved);
3817 if (err)
3818 goto done;
3819 } else {
3820 path = strdup("");
3821 if (path == NULL) {
3822 err = got_error_from_errno("strdup");
3823 goto done;
3827 /* XXX status walk can't deal with trailing slash! */
3828 len = strlen(path);
3829 while (len > 0 && path[len - 1] == '/') {
3830 path[len - 1] = '\0';
3831 len--;
3833 done:
3834 free(abspath);
3835 free(resolved);
3836 free(cwd);
3837 if (err == NULL)
3838 *wt_path = path;
3839 else
3840 free(path);
3841 return err;
3844 struct schedule_addition_args {
3845 struct got_worktree *worktree;
3846 struct got_fileindex *fileindex;
3847 got_worktree_checkout_cb progress_cb;
3848 void *progress_arg;
3849 struct got_repository *repo;
3852 static const struct got_error *
3853 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3854 const char *relpath, struct got_object_id *blob_id,
3855 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3856 int dirfd, const char *de_name)
3858 struct schedule_addition_args *a = arg;
3859 const struct got_error *err = NULL;
3860 struct got_fileindex_entry *ie;
3861 struct stat sb;
3862 char *ondisk_path;
3864 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3865 relpath) == -1)
3866 return got_error_from_errno("asprintf");
3868 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3869 if (ie) {
3870 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3871 de_name, a->repo);
3872 if (err)
3873 goto done;
3874 /* Re-adding an existing entry is a no-op. */
3875 if (status == GOT_STATUS_ADD)
3876 goto done;
3877 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3878 if (err)
3879 goto done;
3882 if (status != GOT_STATUS_UNVERSIONED) {
3883 if (status == GOT_STATUS_NONEXISTENT)
3884 err = got_error_set_errno(ENOENT, ondisk_path);
3885 else
3886 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3887 goto done;
3890 err = got_fileindex_entry_alloc(&ie, relpath);
3891 if (err)
3892 goto done;
3893 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3894 relpath, NULL, NULL, 1);
3895 if (err) {
3896 got_fileindex_entry_free(ie);
3897 goto done;
3899 err = got_fileindex_entry_add(a->fileindex, ie);
3900 if (err) {
3901 got_fileindex_entry_free(ie);
3902 goto done;
3904 done:
3905 free(ondisk_path);
3906 if (err)
3907 return err;
3908 if (status == GOT_STATUS_ADD)
3909 return NULL;
3910 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3913 const struct got_error *
3914 got_worktree_schedule_add(struct got_worktree *worktree,
3915 struct got_pathlist_head *paths,
3916 got_worktree_checkout_cb progress_cb, void *progress_arg,
3917 struct got_repository *repo, int no_ignores)
3919 struct got_fileindex *fileindex = NULL;
3920 char *fileindex_path = NULL;
3921 const struct got_error *err = NULL, *sync_err, *unlockerr;
3922 struct got_pathlist_entry *pe;
3923 struct schedule_addition_args saa;
3925 err = lock_worktree(worktree, LOCK_EX);
3926 if (err)
3927 return err;
3929 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3930 if (err)
3931 goto done;
3933 saa.worktree = worktree;
3934 saa.fileindex = fileindex;
3935 saa.progress_cb = progress_cb;
3936 saa.progress_arg = progress_arg;
3937 saa.repo = repo;
3939 TAILQ_FOREACH(pe, paths, entry) {
3940 err = worktree_status(worktree, pe->path, fileindex, repo,
3941 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3942 if (err)
3943 break;
3945 sync_err = sync_fileindex(fileindex, fileindex_path);
3946 if (sync_err && err == NULL)
3947 err = sync_err;
3948 done:
3949 free(fileindex_path);
3950 if (fileindex)
3951 got_fileindex_free(fileindex);
3952 unlockerr = lock_worktree(worktree, LOCK_SH);
3953 if (unlockerr && err == NULL)
3954 err = unlockerr;
3955 return err;
3958 struct schedule_deletion_args {
3959 struct got_worktree *worktree;
3960 struct got_fileindex *fileindex;
3961 got_worktree_delete_cb progress_cb;
3962 void *progress_arg;
3963 struct got_repository *repo;
3964 int delete_local_mods;
3965 int keep_on_disk;
3966 int ignore_missing_paths;
3967 const char *status_codes;
3970 static const struct got_error *
3971 schedule_for_deletion(void *arg, unsigned char status,
3972 unsigned char staged_status, const char *relpath,
3973 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3974 struct got_object_id *commit_id, int dirfd, const char *de_name)
3976 struct schedule_deletion_args *a = arg;
3977 const struct got_error *err = NULL;
3978 struct got_fileindex_entry *ie = NULL;
3979 struct stat sb;
3980 char *ondisk_path;
3982 if (status == GOT_STATUS_NONEXISTENT) {
3983 if (a->ignore_missing_paths)
3984 return NULL;
3985 return got_error_set_errno(ENOENT, relpath);
3988 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3989 if (ie == NULL)
3990 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
3992 staged_status = get_staged_status(ie);
3993 if (staged_status != GOT_STATUS_NO_CHANGE) {
3994 if (staged_status == GOT_STATUS_DELETE)
3995 return NULL;
3996 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3999 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4000 relpath) == -1)
4001 return got_error_from_errno("asprintf");
4003 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4004 a->repo);
4005 if (err)
4006 goto done;
4008 if (a->status_codes) {
4009 size_t ncodes = strlen(a->status_codes);
4010 int i;
4011 for (i = 0; i < ncodes ; i++) {
4012 if (status == a->status_codes[i])
4013 break;
4015 if (i == ncodes) {
4016 /* Do not delete files in non-matching status. */
4017 free(ondisk_path);
4018 return NULL;
4020 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4021 a->status_codes[i] != GOT_STATUS_MISSING) {
4022 static char msg[64];
4023 snprintf(msg, sizeof(msg),
4024 "invalid status code '%c'", a->status_codes[i]);
4025 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4026 goto done;
4030 if (status != GOT_STATUS_NO_CHANGE) {
4031 if (status == GOT_STATUS_DELETE)
4032 goto done;
4033 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4034 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4035 goto done;
4037 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4038 err = got_error_set_errno(ENOENT, relpath);
4039 goto done;
4041 if (status != GOT_STATUS_MODIFY &&
4042 status != GOT_STATUS_MISSING) {
4043 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4044 goto done;
4048 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4049 size_t root_len;
4051 if (dirfd != -1) {
4052 if (unlinkat(dirfd, de_name, 0) != 0) {
4053 err = got_error_from_errno2("unlinkat",
4054 ondisk_path);
4055 goto done;
4057 } else if (unlink(ondisk_path) != 0) {
4058 err = got_error_from_errno2("unlink", ondisk_path);
4059 goto done;
4062 root_len = strlen(a->worktree->root_path);
4063 do {
4064 char *parent;
4065 err = got_path_dirname(&parent, ondisk_path);
4066 if (err)
4067 goto done;
4068 free(ondisk_path);
4069 ondisk_path = parent;
4070 if (rmdir(ondisk_path) == -1) {
4071 if (errno != ENOTEMPTY)
4072 err = got_error_from_errno2("rmdir",
4073 ondisk_path);
4074 break;
4076 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4077 strlen(ondisk_path), root_len) != 0);
4080 got_fileindex_entry_mark_deleted_from_disk(ie);
4081 done:
4082 free(ondisk_path);
4083 if (err)
4084 return err;
4085 if (status == GOT_STATUS_DELETE)
4086 return NULL;
4087 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4088 staged_status, relpath);
4091 const struct got_error *
4092 got_worktree_schedule_delete(struct got_worktree *worktree,
4093 struct got_pathlist_head *paths, int delete_local_mods,
4094 const char *status_codes,
4095 got_worktree_delete_cb progress_cb, void *progress_arg,
4096 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4098 struct got_fileindex *fileindex = NULL;
4099 char *fileindex_path = NULL;
4100 const struct got_error *err = NULL, *sync_err, *unlockerr;
4101 struct got_pathlist_entry *pe;
4102 struct schedule_deletion_args sda;
4104 err = lock_worktree(worktree, LOCK_EX);
4105 if (err)
4106 return err;
4108 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4109 if (err)
4110 goto done;
4112 sda.worktree = worktree;
4113 sda.fileindex = fileindex;
4114 sda.progress_cb = progress_cb;
4115 sda.progress_arg = progress_arg;
4116 sda.repo = repo;
4117 sda.delete_local_mods = delete_local_mods;
4118 sda.keep_on_disk = keep_on_disk;
4119 sda.ignore_missing_paths = ignore_missing_paths;
4120 sda.status_codes = status_codes;
4122 TAILQ_FOREACH(pe, paths, entry) {
4123 err = worktree_status(worktree, pe->path, fileindex, repo,
4124 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4125 if (err)
4126 break;
4128 sync_err = sync_fileindex(fileindex, fileindex_path);
4129 if (sync_err && err == NULL)
4130 err = sync_err;
4131 done:
4132 free(fileindex_path);
4133 if (fileindex)
4134 got_fileindex_free(fileindex);
4135 unlockerr = lock_worktree(worktree, LOCK_SH);
4136 if (unlockerr && err == NULL)
4137 err = unlockerr;
4138 return err;
4141 static const struct got_error *
4142 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4144 const struct got_error *err = NULL;
4145 char *line = NULL;
4146 size_t linesize = 0, n;
4147 ssize_t linelen;
4149 linelen = getline(&line, &linesize, infile);
4150 if (linelen == -1) {
4151 if (ferror(infile)) {
4152 err = got_error_from_errno("getline");
4153 goto done;
4155 return NULL;
4157 if (outfile) {
4158 n = fwrite(line, 1, linelen, outfile);
4159 if (n != linelen) {
4160 err = got_ferror(outfile, GOT_ERR_IO);
4161 goto done;
4164 if (rejectfile) {
4165 n = fwrite(line, 1, linelen, rejectfile);
4166 if (n != linelen)
4167 err = got_ferror(outfile, GOT_ERR_IO);
4169 done:
4170 free(line);
4171 return err;
4174 static const struct got_error *
4175 skip_one_line(FILE *f)
4177 char *line = NULL;
4178 size_t linesize = 0;
4179 ssize_t linelen;
4181 linelen = getline(&line, &linesize, f);
4182 if (linelen == -1) {
4183 if (ferror(f))
4184 return got_error_from_errno("getline");
4185 return NULL;
4187 free(line);
4188 return NULL;
4191 static const struct got_error *
4192 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4193 int start_old, int end_old, int start_new, int end_new,
4194 FILE *outfile, FILE *rejectfile)
4196 const struct got_error *err;
4198 /* Copy old file's lines leading up to patch. */
4199 while (!feof(f1) && *line_cur1 < start_old) {
4200 err = copy_one_line(f1, outfile, NULL);
4201 if (err)
4202 return err;
4203 (*line_cur1)++;
4205 /* Skip new file's lines leading up to patch. */
4206 while (!feof(f2) && *line_cur2 < start_new) {
4207 if (rejectfile)
4208 err = copy_one_line(f2, NULL, rejectfile);
4209 else
4210 err = skip_one_line(f2);
4211 if (err)
4212 return err;
4213 (*line_cur2)++;
4215 /* Copy patched lines. */
4216 while (!feof(f2) && *line_cur2 <= end_new) {
4217 err = copy_one_line(f2, outfile, NULL);
4218 if (err)
4219 return err;
4220 (*line_cur2)++;
4222 /* Skip over old file's replaced lines. */
4223 while (!feof(f1) && *line_cur1 <= end_old) {
4224 if (rejectfile)
4225 err = copy_one_line(f1, NULL, rejectfile);
4226 else
4227 err = skip_one_line(f1);
4228 if (err)
4229 return err;
4230 (*line_cur1)++;
4233 return NULL;
4236 static const struct got_error *
4237 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4238 FILE *outfile, FILE *rejectfile)
4240 const struct got_error *err;
4242 if (outfile) {
4243 /* Copy old file's lines until EOF. */
4244 while (!feof(f1)) {
4245 err = copy_one_line(f1, outfile, NULL);
4246 if (err)
4247 return err;
4248 (*line_cur1)++;
4251 if (rejectfile) {
4252 /* Copy new file's lines until EOF. */
4253 while (!feof(f2)) {
4254 err = copy_one_line(f2, NULL, rejectfile);
4255 if (err)
4256 return err;
4257 (*line_cur2)++;
4261 return NULL;
4264 static const struct got_error *
4265 apply_or_reject_change(int *choice, int *nchunks_used,
4266 struct diff_result *diff_result, int n,
4267 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4268 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4269 got_worktree_patch_cb patch_cb, void *patch_arg)
4271 const struct got_error *err = NULL;
4272 struct diff_chunk_context cc = {};
4273 int start_old, end_old, start_new, end_new;
4274 FILE *hunkfile;
4275 struct diff_output_unidiff_state *diff_state;
4276 struct diff_input_info diff_info;
4277 int rc;
4279 *choice = GOT_PATCH_CHOICE_NONE;
4281 /* Get changed line numbers without context lines for copy_change(). */
4282 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4283 start_old = cc.left.start;
4284 end_old = cc.left.end;
4285 start_new = cc.right.start;
4286 end_new = cc.right.end;
4288 /* Get the same change with context lines for display. */
4289 memset(&cc, 0, sizeof(cc));
4290 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4292 memset(&diff_info, 0, sizeof(diff_info));
4293 diff_info.left_path = relpath;
4294 diff_info.right_path = relpath;
4296 diff_state = diff_output_unidiff_state_alloc();
4297 if (diff_state == NULL)
4298 return got_error_set_errno(ENOMEM,
4299 "diff_output_unidiff_state_alloc");
4301 hunkfile = got_opentemp();
4302 if (hunkfile == NULL) {
4303 err = got_error_from_errno("got_opentemp");
4304 goto done;
4307 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4308 diff_result, &cc);
4309 if (rc != DIFF_RC_OK) {
4310 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4311 goto done;
4314 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4315 err = got_ferror(hunkfile, GOT_ERR_IO);
4316 goto done;
4319 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4320 hunkfile, changeno, nchanges);
4321 if (err)
4322 goto done;
4324 switch (*choice) {
4325 case GOT_PATCH_CHOICE_YES:
4326 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4327 end_old, start_new, end_new, outfile, rejectfile);
4328 break;
4329 case GOT_PATCH_CHOICE_NO:
4330 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4331 end_old, start_new, end_new, rejectfile, outfile);
4332 break;
4333 case GOT_PATCH_CHOICE_QUIT:
4334 break;
4335 default:
4336 err = got_error(GOT_ERR_PATCH_CHOICE);
4337 break;
4339 done:
4340 diff_output_unidiff_state_free(diff_state);
4341 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4342 err = got_error_from_errno("fclose");
4343 return err;
4346 struct revert_file_args {
4347 struct got_worktree *worktree;
4348 struct got_fileindex *fileindex;
4349 got_worktree_checkout_cb progress_cb;
4350 void *progress_arg;
4351 got_worktree_patch_cb patch_cb;
4352 void *patch_arg;
4353 struct got_repository *repo;
4354 int unlink_added_files;
4357 static const struct got_error *
4358 create_patched_content(char **path_outfile, int reverse_patch,
4359 struct got_object_id *blob_id, const char *path2,
4360 int dirfd2, const char *de_name2,
4361 const char *relpath, struct got_repository *repo,
4362 got_worktree_patch_cb patch_cb, void *patch_arg)
4364 const struct got_error *err, *free_err;
4365 struct got_blob_object *blob = NULL;
4366 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4367 int fd2 = -1;
4368 char link_target[PATH_MAX];
4369 ssize_t link_len = 0;
4370 char *path1 = NULL, *id_str = NULL;
4371 struct stat sb2;
4372 struct got_diffreg_result *diffreg_result = NULL;
4373 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4374 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4376 *path_outfile = NULL;
4378 err = got_object_id_str(&id_str, blob_id);
4379 if (err)
4380 return err;
4382 if (dirfd2 != -1) {
4383 fd2 = openat(dirfd2, de_name2,
4384 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4385 if (fd2 == -1) {
4386 if (!got_err_open_nofollow_on_symlink()) {
4387 err = got_error_from_errno2("openat", path2);
4388 goto done;
4390 link_len = readlinkat(dirfd2, de_name2,
4391 link_target, sizeof(link_target));
4392 if (link_len == -1) {
4393 return got_error_from_errno2("readlinkat",
4394 path2);
4396 sb2.st_mode = S_IFLNK;
4397 sb2.st_size = link_len;
4399 } else {
4400 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4401 if (fd2 == -1) {
4402 if (!got_err_open_nofollow_on_symlink()) {
4403 err = got_error_from_errno2("open", path2);
4404 goto done;
4406 link_len = readlink(path2, link_target,
4407 sizeof(link_target));
4408 if (link_len == -1)
4409 return got_error_from_errno2("readlink", path2);
4410 sb2.st_mode = S_IFLNK;
4411 sb2.st_size = link_len;
4414 if (fd2 != -1) {
4415 if (fstat(fd2, &sb2) == -1) {
4416 err = got_error_from_errno2("fstat", path2);
4417 goto done;
4420 f2 = fdopen(fd2, "r");
4421 if (f2 == NULL) {
4422 err = got_error_from_errno2("fdopen", path2);
4423 goto done;
4425 fd2 = -1;
4426 } else {
4427 size_t n;
4428 f2 = got_opentemp();
4429 if (f2 == NULL) {
4430 err = got_error_from_errno2("got_opentemp", path2);
4431 goto done;
4433 n = fwrite(link_target, 1, link_len, f2);
4434 if (n != link_len) {
4435 err = got_ferror(f2, GOT_ERR_IO);
4436 goto done;
4438 if (fflush(f2) == EOF) {
4439 err = got_error_from_errno("fflush");
4440 goto done;
4442 rewind(f2);
4445 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4446 if (err)
4447 goto done;
4449 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4450 if (err)
4451 goto done;
4453 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4454 if (err)
4455 goto done;
4457 err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4458 NULL);
4459 if (err)
4460 goto done;
4462 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4463 if (err)
4464 goto done;
4466 if (fseek(f1, 0L, SEEK_SET) == -1)
4467 return got_ferror(f1, GOT_ERR_IO);
4468 if (fseek(f2, 0L, SEEK_SET) == -1)
4469 return got_ferror(f2, GOT_ERR_IO);
4471 /* Count the number of actual changes in the diff result. */
4472 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4473 struct diff_chunk_context cc = {};
4474 diff_chunk_context_load_change(&cc, &nchunks_used,
4475 diffreg_result->result, n, 0);
4476 nchanges++;
4478 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4479 int choice;
4480 err = apply_or_reject_change(&choice, &nchunks_used,
4481 diffreg_result->result, n, relpath, f1, f2,
4482 &line_cur1, &line_cur2,
4483 reverse_patch ? NULL : outfile,
4484 reverse_patch ? outfile : NULL,
4485 ++i, nchanges, patch_cb, patch_arg);
4486 if (err)
4487 goto done;
4488 if (choice == GOT_PATCH_CHOICE_YES)
4489 have_content = 1;
4490 else if (choice == GOT_PATCH_CHOICE_QUIT)
4491 break;
4493 if (have_content) {
4494 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4495 reverse_patch ? NULL : outfile,
4496 reverse_patch ? outfile : NULL);
4497 if (err)
4498 goto done;
4500 if (!S_ISLNK(sb2.st_mode)) {
4501 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4502 err = got_error_from_errno2("fchmod", path2);
4503 goto done;
4507 done:
4508 free(id_str);
4509 if (blob)
4510 got_object_blob_close(blob);
4511 free_err = got_diffreg_result_free(diffreg_result);
4512 if (err == NULL)
4513 err = free_err;
4514 if (f1 && fclose(f1) == EOF && err == NULL)
4515 err = got_error_from_errno2("fclose", path1);
4516 if (f2 && fclose(f2) == EOF && err == NULL)
4517 err = got_error_from_errno2("fclose", path2);
4518 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4519 err = got_error_from_errno2("close", path2);
4520 if (outfile && fclose(outfile) == EOF && err == NULL)
4521 err = got_error_from_errno2("fclose", *path_outfile);
4522 if (path1 && unlink(path1) == -1 && err == NULL)
4523 err = got_error_from_errno2("unlink", path1);
4524 if (err || !have_content) {
4525 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4526 err = got_error_from_errno2("unlink", *path_outfile);
4527 free(*path_outfile);
4528 *path_outfile = NULL;
4530 free(path1);
4531 return err;
4534 static const struct got_error *
4535 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4536 const char *relpath, struct got_object_id *blob_id,
4537 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4538 int dirfd, const char *de_name)
4540 struct revert_file_args *a = arg;
4541 const struct got_error *err = NULL;
4542 char *parent_path = NULL;
4543 struct got_fileindex_entry *ie;
4544 struct got_tree_object *tree = NULL;
4545 struct got_object_id *tree_id = NULL;
4546 const struct got_tree_entry *te = NULL;
4547 char *tree_path = NULL, *te_name;
4548 char *ondisk_path = NULL, *path_content = NULL;
4549 struct got_blob_object *blob = NULL;
4551 /* Reverting a staged deletion is a no-op. */
4552 if (status == GOT_STATUS_DELETE &&
4553 staged_status != GOT_STATUS_NO_CHANGE)
4554 return NULL;
4556 if (status == GOT_STATUS_UNVERSIONED)
4557 return (*a->progress_cb)(a->progress_arg,
4558 GOT_STATUS_UNVERSIONED, relpath);
4560 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4561 if (ie == NULL)
4562 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4564 /* Construct in-repository path of tree which contains this blob. */
4565 err = got_path_dirname(&parent_path, ie->path);
4566 if (err) {
4567 if (err->code != GOT_ERR_BAD_PATH)
4568 goto done;
4569 parent_path = strdup("/");
4570 if (parent_path == NULL) {
4571 err = got_error_from_errno("strdup");
4572 goto done;
4575 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4576 tree_path = strdup(parent_path);
4577 if (tree_path == NULL) {
4578 err = got_error_from_errno("strdup");
4579 goto done;
4581 } else {
4582 if (got_path_is_root_dir(parent_path)) {
4583 tree_path = strdup(a->worktree->path_prefix);
4584 if (tree_path == NULL) {
4585 err = got_error_from_errno("strdup");
4586 goto done;
4588 } else {
4589 if (asprintf(&tree_path, "%s/%s",
4590 a->worktree->path_prefix, parent_path) == -1) {
4591 err = got_error_from_errno("asprintf");
4592 goto done;
4597 err = got_object_id_by_path(&tree_id, a->repo,
4598 a->worktree->base_commit_id, tree_path);
4599 if (err) {
4600 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4601 (status == GOT_STATUS_ADD ||
4602 staged_status == GOT_STATUS_ADD)))
4603 goto done;
4604 } else {
4605 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4606 if (err)
4607 goto done;
4609 err = got_path_basename(&te_name, ie->path);
4610 if (err)
4611 goto done;
4613 te = got_object_tree_find_entry(tree, te_name);
4614 free(te_name);
4615 if (te == NULL && status != GOT_STATUS_ADD &&
4616 staged_status != GOT_STATUS_ADD) {
4617 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4618 goto done;
4622 switch (status) {
4623 case GOT_STATUS_ADD:
4624 if (a->patch_cb) {
4625 int choice = GOT_PATCH_CHOICE_NONE;
4626 err = (*a->patch_cb)(&choice, a->patch_arg,
4627 status, ie->path, NULL, 1, 1);
4628 if (err)
4629 goto done;
4630 if (choice != GOT_PATCH_CHOICE_YES)
4631 break;
4633 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4634 ie->path);
4635 if (err)
4636 goto done;
4637 got_fileindex_entry_remove(a->fileindex, ie);
4638 if (a->unlink_added_files) {
4639 if (asprintf(&ondisk_path, "%s/%s",
4640 got_worktree_get_root_path(a->worktree),
4641 relpath) == -1) {
4642 err = got_error_from_errno("asprintf");
4643 goto done;
4645 if (unlink(ondisk_path) == -1) {
4646 err = got_error_from_errno2("unlink",
4647 ondisk_path);
4648 break;
4651 break;
4652 case GOT_STATUS_DELETE:
4653 if (a->patch_cb) {
4654 int choice = GOT_PATCH_CHOICE_NONE;
4655 err = (*a->patch_cb)(&choice, a->patch_arg,
4656 status, ie->path, NULL, 1, 1);
4657 if (err)
4658 goto done;
4659 if (choice != GOT_PATCH_CHOICE_YES)
4660 break;
4662 /* fall through */
4663 case GOT_STATUS_MODIFY:
4664 case GOT_STATUS_MODE_CHANGE:
4665 case GOT_STATUS_CONFLICT:
4666 case GOT_STATUS_MISSING: {
4667 struct got_object_id id;
4668 if (staged_status == GOT_STATUS_ADD ||
4669 staged_status == GOT_STATUS_MODIFY) {
4670 memcpy(id.sha1, ie->staged_blob_sha1,
4671 SHA1_DIGEST_LENGTH);
4672 } else
4673 memcpy(id.sha1, ie->blob_sha1,
4674 SHA1_DIGEST_LENGTH);
4675 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4676 if (err)
4677 goto done;
4679 if (asprintf(&ondisk_path, "%s/%s",
4680 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4681 err = got_error_from_errno("asprintf");
4682 goto done;
4685 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4686 status == GOT_STATUS_CONFLICT)) {
4687 int is_bad_symlink = 0;
4688 err = create_patched_content(&path_content, 1, &id,
4689 ondisk_path, dirfd, de_name, ie->path, a->repo,
4690 a->patch_cb, a->patch_arg);
4691 if (err || path_content == NULL)
4692 break;
4693 if (te && S_ISLNK(te->mode)) {
4694 if (unlink(path_content) == -1) {
4695 err = got_error_from_errno2("unlink",
4696 path_content);
4697 break;
4699 err = install_symlink(&is_bad_symlink,
4700 a->worktree, ondisk_path, ie->path,
4701 blob, 0, 1, 0, 0, a->repo,
4702 a->progress_cb, a->progress_arg);
4703 } else {
4704 if (rename(path_content, ondisk_path) == -1) {
4705 err = got_error_from_errno3("rename",
4706 path_content, ondisk_path);
4707 goto done;
4710 } else {
4711 int is_bad_symlink = 0;
4712 if (te && S_ISLNK(te->mode)) {
4713 err = install_symlink(&is_bad_symlink,
4714 a->worktree, ondisk_path, ie->path,
4715 blob, 0, 1, 0, 0, a->repo,
4716 a->progress_cb, a->progress_arg);
4717 } else {
4718 err = install_blob(a->worktree, ondisk_path,
4719 ie->path,
4720 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4721 got_fileindex_perms_to_st(ie), blob,
4722 0, 1, 0, 0, a->repo,
4723 a->progress_cb, a->progress_arg);
4725 if (err)
4726 goto done;
4727 if (status == GOT_STATUS_DELETE ||
4728 status == GOT_STATUS_MODE_CHANGE) {
4729 err = got_fileindex_entry_update(ie,
4730 a->worktree->root_fd, relpath,
4731 blob->id.sha1,
4732 a->worktree->base_commit_id->sha1, 1);
4733 if (err)
4734 goto done;
4736 if (is_bad_symlink) {
4737 got_fileindex_entry_filetype_set(ie,
4738 GOT_FILEIDX_MODE_BAD_SYMLINK);
4741 break;
4743 default:
4744 break;
4746 done:
4747 free(ondisk_path);
4748 free(path_content);
4749 free(parent_path);
4750 free(tree_path);
4751 if (blob)
4752 got_object_blob_close(blob);
4753 if (tree)
4754 got_object_tree_close(tree);
4755 free(tree_id);
4756 return err;
4759 const struct got_error *
4760 got_worktree_revert(struct got_worktree *worktree,
4761 struct got_pathlist_head *paths,
4762 got_worktree_checkout_cb progress_cb, void *progress_arg,
4763 got_worktree_patch_cb patch_cb, void *patch_arg,
4764 struct got_repository *repo)
4766 struct got_fileindex *fileindex = NULL;
4767 char *fileindex_path = NULL;
4768 const struct got_error *err = NULL, *unlockerr = NULL;
4769 const struct got_error *sync_err = NULL;
4770 struct got_pathlist_entry *pe;
4771 struct revert_file_args rfa;
4773 err = lock_worktree(worktree, LOCK_EX);
4774 if (err)
4775 return err;
4777 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4778 if (err)
4779 goto done;
4781 rfa.worktree = worktree;
4782 rfa.fileindex = fileindex;
4783 rfa.progress_cb = progress_cb;
4784 rfa.progress_arg = progress_arg;
4785 rfa.patch_cb = patch_cb;
4786 rfa.patch_arg = patch_arg;
4787 rfa.repo = repo;
4788 rfa.unlink_added_files = 0;
4789 TAILQ_FOREACH(pe, paths, entry) {
4790 err = worktree_status(worktree, pe->path, fileindex, repo,
4791 revert_file, &rfa, NULL, NULL, 1, 0);
4792 if (err)
4793 break;
4795 sync_err = sync_fileindex(fileindex, fileindex_path);
4796 if (sync_err && err == NULL)
4797 err = sync_err;
4798 done:
4799 free(fileindex_path);
4800 if (fileindex)
4801 got_fileindex_free(fileindex);
4802 unlockerr = lock_worktree(worktree, LOCK_SH);
4803 if (unlockerr && err == NULL)
4804 err = unlockerr;
4805 return err;
4808 static void
4809 free_commitable(struct got_commitable *ct)
4811 free(ct->path);
4812 free(ct->in_repo_path);
4813 free(ct->ondisk_path);
4814 free(ct->blob_id);
4815 free(ct->base_blob_id);
4816 free(ct->staged_blob_id);
4817 free(ct->base_commit_id);
4818 free(ct);
4821 struct collect_commitables_arg {
4822 struct got_pathlist_head *commitable_paths;
4823 struct got_repository *repo;
4824 struct got_worktree *worktree;
4825 struct got_fileindex *fileindex;
4826 int have_staged_files;
4827 int allow_bad_symlinks;
4830 static const struct got_error *
4831 collect_commitables(void *arg, unsigned char status,
4832 unsigned char staged_status, const char *relpath,
4833 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4834 struct got_object_id *commit_id, int dirfd, const char *de_name)
4836 struct collect_commitables_arg *a = arg;
4837 const struct got_error *err = NULL;
4838 struct got_commitable *ct = NULL;
4839 struct got_pathlist_entry *new = NULL;
4840 char *parent_path = NULL, *path = NULL;
4841 struct stat sb;
4843 if (a->have_staged_files) {
4844 if (staged_status != GOT_STATUS_MODIFY &&
4845 staged_status != GOT_STATUS_ADD &&
4846 staged_status != GOT_STATUS_DELETE)
4847 return NULL;
4848 } else {
4849 if (status == GOT_STATUS_CONFLICT)
4850 return got_error(GOT_ERR_COMMIT_CONFLICT);
4852 if (status != GOT_STATUS_MODIFY &&
4853 status != GOT_STATUS_MODE_CHANGE &&
4854 status != GOT_STATUS_ADD &&
4855 status != GOT_STATUS_DELETE)
4856 return NULL;
4859 if (asprintf(&path, "/%s", relpath) == -1) {
4860 err = got_error_from_errno("asprintf");
4861 goto done;
4863 if (strcmp(path, "/") == 0) {
4864 parent_path = strdup("");
4865 if (parent_path == NULL)
4866 return got_error_from_errno("strdup");
4867 } else {
4868 err = got_path_dirname(&parent_path, path);
4869 if (err)
4870 return err;
4873 ct = calloc(1, sizeof(*ct));
4874 if (ct == NULL) {
4875 err = got_error_from_errno("calloc");
4876 goto done;
4879 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4880 relpath) == -1) {
4881 err = got_error_from_errno("asprintf");
4882 goto done;
4885 if (staged_status == GOT_STATUS_ADD ||
4886 staged_status == GOT_STATUS_MODIFY) {
4887 struct got_fileindex_entry *ie;
4888 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4889 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4890 case GOT_FILEIDX_MODE_REGULAR_FILE:
4891 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4892 ct->mode = S_IFREG;
4893 break;
4894 case GOT_FILEIDX_MODE_SYMLINK:
4895 ct->mode = S_IFLNK;
4896 break;
4897 default:
4898 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4899 goto done;
4901 ct->mode |= got_fileindex_entry_perms_get(ie);
4902 } else if (status != GOT_STATUS_DELETE &&
4903 staged_status != GOT_STATUS_DELETE) {
4904 if (dirfd != -1) {
4905 if (fstatat(dirfd, de_name, &sb,
4906 AT_SYMLINK_NOFOLLOW) == -1) {
4907 err = got_error_from_errno2("fstatat",
4908 ct->ondisk_path);
4909 goto done;
4911 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4912 err = got_error_from_errno2("lstat", ct->ondisk_path);
4913 goto done;
4915 ct->mode = sb.st_mode;
4918 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4919 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4920 relpath) == -1) {
4921 err = got_error_from_errno("asprintf");
4922 goto done;
4925 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4926 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4927 int is_bad_symlink;
4928 char target_path[PATH_MAX];
4929 ssize_t target_len;
4930 target_len = readlink(ct->ondisk_path, target_path,
4931 sizeof(target_path));
4932 if (target_len == -1) {
4933 err = got_error_from_errno2("readlink",
4934 ct->ondisk_path);
4935 goto done;
4937 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4938 target_len, ct->ondisk_path, a->worktree->root_path);
4939 if (err)
4940 goto done;
4941 if (is_bad_symlink) {
4942 err = got_error_path(ct->ondisk_path,
4943 GOT_ERR_BAD_SYMLINK);
4944 goto done;
4949 ct->status = status;
4950 ct->staged_status = staged_status;
4951 ct->blob_id = NULL; /* will be filled in when blob gets created */
4952 if (ct->status != GOT_STATUS_ADD &&
4953 ct->staged_status != GOT_STATUS_ADD) {
4954 ct->base_blob_id = got_object_id_dup(blob_id);
4955 if (ct->base_blob_id == NULL) {
4956 err = got_error_from_errno("got_object_id_dup");
4957 goto done;
4959 ct->base_commit_id = got_object_id_dup(commit_id);
4960 if (ct->base_commit_id == NULL) {
4961 err = got_error_from_errno("got_object_id_dup");
4962 goto done;
4965 if (ct->staged_status == GOT_STATUS_ADD ||
4966 ct->staged_status == GOT_STATUS_MODIFY) {
4967 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4968 if (ct->staged_blob_id == NULL) {
4969 err = got_error_from_errno("got_object_id_dup");
4970 goto done;
4973 ct->path = strdup(path);
4974 if (ct->path == NULL) {
4975 err = got_error_from_errno("strdup");
4976 goto done;
4978 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4979 done:
4980 if (ct && (err || new == NULL))
4981 free_commitable(ct);
4982 free(parent_path);
4983 free(path);
4984 return err;
4987 static const struct got_error *write_tree(struct got_object_id **, int *,
4988 struct got_tree_object *, const char *, struct got_pathlist_head *,
4989 got_worktree_status_cb status_cb, void *status_arg,
4990 struct got_repository *);
4992 static const struct got_error *
4993 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4994 struct got_tree_entry *te, const char *parent_path,
4995 struct got_pathlist_head *commitable_paths,
4996 got_worktree_status_cb status_cb, void *status_arg,
4997 struct got_repository *repo)
4999 const struct got_error *err = NULL;
5000 struct got_tree_object *subtree;
5001 char *subpath;
5003 if (asprintf(&subpath, "%s%s%s", parent_path,
5004 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5005 return got_error_from_errno("asprintf");
5007 err = got_object_open_as_tree(&subtree, repo, &te->id);
5008 if (err)
5009 return err;
5011 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5012 commitable_paths, status_cb, status_arg, repo);
5013 got_object_tree_close(subtree);
5014 free(subpath);
5015 return err;
5018 static const struct got_error *
5019 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5021 const struct got_error *err = NULL;
5022 char *ct_parent_path = NULL;
5024 *match = 0;
5026 if (strchr(ct->in_repo_path, '/') == NULL) {
5027 *match = got_path_is_root_dir(path);
5028 return NULL;
5031 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5032 if (err)
5033 return err;
5034 *match = (strcmp(path, ct_parent_path) == 0);
5035 free(ct_parent_path);
5036 return err;
5039 static mode_t
5040 get_ct_file_mode(struct got_commitable *ct)
5042 if (S_ISLNK(ct->mode))
5043 return S_IFLNK;
5045 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5048 static const struct got_error *
5049 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5050 struct got_tree_entry *te, struct got_commitable *ct)
5052 const struct got_error *err = NULL;
5054 *new_te = NULL;
5056 err = got_object_tree_entry_dup(new_te, te);
5057 if (err)
5058 goto done;
5060 (*new_te)->mode = get_ct_file_mode(ct);
5062 if (ct->staged_status == GOT_STATUS_MODIFY)
5063 memcpy(&(*new_te)->id, ct->staged_blob_id,
5064 sizeof((*new_te)->id));
5065 else
5066 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5067 done:
5068 if (err && *new_te) {
5069 free(*new_te);
5070 *new_te = NULL;
5072 return err;
5075 static const struct got_error *
5076 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5077 struct got_commitable *ct)
5079 const struct got_error *err = NULL;
5080 char *ct_name = NULL;
5082 *new_te = NULL;
5084 *new_te = calloc(1, sizeof(**new_te));
5085 if (*new_te == NULL)
5086 return got_error_from_errno("calloc");
5088 err = got_path_basename(&ct_name, ct->path);
5089 if (err)
5090 goto done;
5091 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5092 sizeof((*new_te)->name)) {
5093 err = got_error(GOT_ERR_NO_SPACE);
5094 goto done;
5097 (*new_te)->mode = get_ct_file_mode(ct);
5099 if (ct->staged_status == GOT_STATUS_ADD)
5100 memcpy(&(*new_te)->id, ct->staged_blob_id,
5101 sizeof((*new_te)->id));
5102 else
5103 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5104 done:
5105 free(ct_name);
5106 if (err && *new_te) {
5107 free(*new_te);
5108 *new_te = NULL;
5110 return err;
5113 static const struct got_error *
5114 insert_tree_entry(struct got_tree_entry *new_te,
5115 struct got_pathlist_head *paths)
5117 const struct got_error *err = NULL;
5118 struct got_pathlist_entry *new_pe;
5120 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5121 if (err)
5122 return err;
5123 if (new_pe == NULL)
5124 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5125 return NULL;
5128 static const struct got_error *
5129 report_ct_status(struct got_commitable *ct,
5130 got_worktree_status_cb status_cb, void *status_arg)
5132 const char *ct_path = ct->path;
5133 unsigned char status;
5135 if (status_cb == NULL) /* no commit progress output desired */
5136 return NULL;
5138 while (ct_path[0] == '/')
5139 ct_path++;
5141 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5142 status = ct->staged_status;
5143 else
5144 status = ct->status;
5146 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5147 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5150 static const struct got_error *
5151 match_modified_subtree(int *modified, struct got_tree_entry *te,
5152 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5154 const struct got_error *err = NULL;
5155 struct got_pathlist_entry *pe;
5156 char *te_path;
5158 *modified = 0;
5160 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5161 got_path_is_root_dir(base_tree_path) ? "" : "/",
5162 te->name) == -1)
5163 return got_error_from_errno("asprintf");
5165 TAILQ_FOREACH(pe, commitable_paths, entry) {
5166 struct got_commitable *ct = pe->data;
5167 *modified = got_path_is_child(ct->in_repo_path, te_path,
5168 strlen(te_path));
5169 if (*modified)
5170 break;
5173 free(te_path);
5174 return err;
5177 static const struct got_error *
5178 match_deleted_or_modified_ct(struct got_commitable **ctp,
5179 struct got_tree_entry *te, const char *base_tree_path,
5180 struct got_pathlist_head *commitable_paths)
5182 const struct got_error *err = NULL;
5183 struct got_pathlist_entry *pe;
5185 *ctp = NULL;
5187 TAILQ_FOREACH(pe, commitable_paths, entry) {
5188 struct got_commitable *ct = pe->data;
5189 char *ct_name = NULL;
5190 int path_matches;
5192 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5193 if (ct->status != GOT_STATUS_MODIFY &&
5194 ct->status != GOT_STATUS_MODE_CHANGE &&
5195 ct->status != GOT_STATUS_DELETE)
5196 continue;
5197 } else {
5198 if (ct->staged_status != GOT_STATUS_MODIFY &&
5199 ct->staged_status != GOT_STATUS_DELETE)
5200 continue;
5203 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5204 continue;
5206 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5207 if (err)
5208 return err;
5209 if (!path_matches)
5210 continue;
5212 err = got_path_basename(&ct_name, pe->path);
5213 if (err)
5214 return err;
5216 if (strcmp(te->name, ct_name) != 0) {
5217 free(ct_name);
5218 continue;
5220 free(ct_name);
5222 *ctp = ct;
5223 break;
5226 return err;
5229 static const struct got_error *
5230 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5231 const char *child_path, const char *path_base_tree,
5232 struct got_pathlist_head *commitable_paths,
5233 got_worktree_status_cb status_cb, void *status_arg,
5234 struct got_repository *repo)
5236 const struct got_error *err = NULL;
5237 struct got_tree_entry *new_te;
5238 char *subtree_path;
5239 struct got_object_id *id = NULL;
5240 int nentries;
5242 *new_tep = NULL;
5244 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5245 got_path_is_root_dir(path_base_tree) ? "" : "/",
5246 child_path) == -1)
5247 return got_error_from_errno("asprintf");
5249 new_te = calloc(1, sizeof(*new_te));
5250 if (new_te == NULL)
5251 return got_error_from_errno("calloc");
5252 new_te->mode = S_IFDIR;
5254 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5255 sizeof(new_te->name)) {
5256 err = got_error(GOT_ERR_NO_SPACE);
5257 goto done;
5259 err = write_tree(&id, &nentries, NULL, subtree_path,
5260 commitable_paths, status_cb, status_arg, repo);
5261 if (err) {
5262 free(new_te);
5263 goto done;
5265 memcpy(&new_te->id, id, sizeof(new_te->id));
5266 done:
5267 free(id);
5268 free(subtree_path);
5269 if (err == NULL)
5270 *new_tep = new_te;
5271 return err;
5274 static const struct got_error *
5275 write_tree(struct got_object_id **new_tree_id, int *nentries,
5276 struct got_tree_object *base_tree, const char *path_base_tree,
5277 struct got_pathlist_head *commitable_paths,
5278 got_worktree_status_cb status_cb, void *status_arg,
5279 struct got_repository *repo)
5281 const struct got_error *err = NULL;
5282 struct got_pathlist_head paths;
5283 struct got_tree_entry *te, *new_te = NULL;
5284 struct got_pathlist_entry *pe;
5286 TAILQ_INIT(&paths);
5287 *nentries = 0;
5289 /* Insert, and recurse into, newly added entries first. */
5290 TAILQ_FOREACH(pe, commitable_paths, entry) {
5291 struct got_commitable *ct = pe->data;
5292 char *child_path = NULL, *slash;
5294 if ((ct->status != GOT_STATUS_ADD &&
5295 ct->staged_status != GOT_STATUS_ADD) ||
5296 (ct->flags & GOT_COMMITABLE_ADDED))
5297 continue;
5299 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5300 strlen(path_base_tree)))
5301 continue;
5303 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5304 ct->in_repo_path);
5305 if (err)
5306 goto done;
5308 slash = strchr(child_path, '/');
5309 if (slash == NULL) {
5310 err = alloc_added_blob_tree_entry(&new_te, ct);
5311 if (err)
5312 goto done;
5313 err = report_ct_status(ct, status_cb, status_arg);
5314 if (err)
5315 goto done;
5316 ct->flags |= GOT_COMMITABLE_ADDED;
5317 err = insert_tree_entry(new_te, &paths);
5318 if (err)
5319 goto done;
5320 (*nentries)++;
5321 } else {
5322 *slash = '\0'; /* trim trailing path components */
5323 if (base_tree == NULL ||
5324 got_object_tree_find_entry(base_tree, child_path)
5325 == NULL) {
5326 err = make_subtree_for_added_blob(&new_te,
5327 child_path, path_base_tree,
5328 commitable_paths, status_cb, status_arg,
5329 repo);
5330 if (err)
5331 goto done;
5332 err = insert_tree_entry(new_te, &paths);
5333 if (err)
5334 goto done;
5335 (*nentries)++;
5340 if (base_tree) {
5341 int i, nbase_entries;
5342 /* Handle modified and deleted entries. */
5343 nbase_entries = got_object_tree_get_nentries(base_tree);
5344 for (i = 0; i < nbase_entries; i++) {
5345 struct got_commitable *ct = NULL;
5347 te = got_object_tree_get_entry(base_tree, i);
5348 if (got_object_tree_entry_is_submodule(te)) {
5349 /* Entry is a submodule; just copy it. */
5350 err = got_object_tree_entry_dup(&new_te, te);
5351 if (err)
5352 goto done;
5353 err = insert_tree_entry(new_te, &paths);
5354 if (err)
5355 goto done;
5356 (*nentries)++;
5357 continue;
5360 if (S_ISDIR(te->mode)) {
5361 int modified;
5362 err = got_object_tree_entry_dup(&new_te, te);
5363 if (err)
5364 goto done;
5365 err = match_modified_subtree(&modified, te,
5366 path_base_tree, commitable_paths);
5367 if (err)
5368 goto done;
5369 /* Avoid recursion into unmodified subtrees. */
5370 if (modified) {
5371 struct got_object_id *new_id;
5372 int nsubentries;
5373 err = write_subtree(&new_id,
5374 &nsubentries, te,
5375 path_base_tree, commitable_paths,
5376 status_cb, status_arg, repo);
5377 if (err)
5378 goto done;
5379 if (nsubentries == 0) {
5380 /* All entries were deleted. */
5381 free(new_id);
5382 continue;
5384 memcpy(&new_te->id, new_id,
5385 sizeof(new_te->id));
5386 free(new_id);
5388 err = insert_tree_entry(new_te, &paths);
5389 if (err)
5390 goto done;
5391 (*nentries)++;
5392 continue;
5395 err = match_deleted_or_modified_ct(&ct, te,
5396 path_base_tree, commitable_paths);
5397 if (err)
5398 goto done;
5399 if (ct) {
5400 /* NB: Deleted entries get dropped here. */
5401 if (ct->status == GOT_STATUS_MODIFY ||
5402 ct->status == GOT_STATUS_MODE_CHANGE ||
5403 ct->staged_status == GOT_STATUS_MODIFY) {
5404 err = alloc_modified_blob_tree_entry(
5405 &new_te, te, ct);
5406 if (err)
5407 goto done;
5408 err = insert_tree_entry(new_te, &paths);
5409 if (err)
5410 goto done;
5411 (*nentries)++;
5413 err = report_ct_status(ct, status_cb,
5414 status_arg);
5415 if (err)
5416 goto done;
5417 } else {
5418 /* Entry is unchanged; just copy it. */
5419 err = got_object_tree_entry_dup(&new_te, te);
5420 if (err)
5421 goto done;
5422 err = insert_tree_entry(new_te, &paths);
5423 if (err)
5424 goto done;
5425 (*nentries)++;
5430 /* Write new list of entries; deleted entries have been dropped. */
5431 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5432 done:
5433 got_pathlist_free(&paths);
5434 return err;
5437 static const struct got_error *
5438 update_fileindex_after_commit(struct got_worktree *worktree,
5439 struct got_pathlist_head *commitable_paths,
5440 struct got_object_id *new_base_commit_id,
5441 struct got_fileindex *fileindex, int have_staged_files)
5443 const struct got_error *err = NULL;
5444 struct got_pathlist_entry *pe;
5445 char *relpath = NULL;
5447 TAILQ_FOREACH(pe, commitable_paths, entry) {
5448 struct got_fileindex_entry *ie;
5449 struct got_commitable *ct = pe->data;
5451 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5453 err = got_path_skip_common_ancestor(&relpath,
5454 worktree->root_path, ct->ondisk_path);
5455 if (err)
5456 goto done;
5458 if (ie) {
5459 if (ct->status == GOT_STATUS_DELETE ||
5460 ct->staged_status == GOT_STATUS_DELETE) {
5461 got_fileindex_entry_remove(fileindex, ie);
5462 } else if (ct->staged_status == GOT_STATUS_ADD ||
5463 ct->staged_status == GOT_STATUS_MODIFY) {
5464 got_fileindex_entry_stage_set(ie,
5465 GOT_FILEIDX_STAGE_NONE);
5466 got_fileindex_entry_staged_filetype_set(ie, 0);
5468 err = got_fileindex_entry_update(ie,
5469 worktree->root_fd, relpath,
5470 ct->staged_blob_id->sha1,
5471 new_base_commit_id->sha1,
5472 !have_staged_files);
5473 } else
5474 err = got_fileindex_entry_update(ie,
5475 worktree->root_fd, relpath,
5476 ct->blob_id->sha1,
5477 new_base_commit_id->sha1,
5478 !have_staged_files);
5479 } else {
5480 err = got_fileindex_entry_alloc(&ie, pe->path);
5481 if (err)
5482 goto done;
5483 err = got_fileindex_entry_update(ie,
5484 worktree->root_fd, relpath, ct->blob_id->sha1,
5485 new_base_commit_id->sha1, 1);
5486 if (err) {
5487 got_fileindex_entry_free(ie);
5488 goto done;
5490 err = got_fileindex_entry_add(fileindex, ie);
5491 if (err) {
5492 got_fileindex_entry_free(ie);
5493 goto done;
5496 free(relpath);
5497 relpath = NULL;
5499 done:
5500 free(relpath);
5501 return err;
5505 static const struct got_error *
5506 check_out_of_date(const char *in_repo_path, unsigned char status,
5507 unsigned char staged_status, struct got_object_id *base_blob_id,
5508 struct got_object_id *base_commit_id,
5509 struct got_object_id *head_commit_id, struct got_repository *repo,
5510 int ood_errcode)
5512 const struct got_error *err = NULL;
5513 struct got_object_id *id = NULL;
5515 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5516 /* Trivial case: base commit == head commit */
5517 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5518 return NULL;
5520 * Ensure file content which local changes were based
5521 * on matches file content in the branch head.
5523 err = got_object_id_by_path(&id, repo, head_commit_id,
5524 in_repo_path);
5525 if (err) {
5526 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5527 err = got_error(ood_errcode);
5528 goto done;
5529 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5530 err = got_error(ood_errcode);
5531 } else {
5532 /* Require that added files don't exist in the branch head. */
5533 err = got_object_id_by_path(&id, repo, head_commit_id,
5534 in_repo_path);
5535 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5536 goto done;
5537 err = id ? got_error(ood_errcode) : NULL;
5539 done:
5540 free(id);
5541 return err;
5544 const struct got_error *
5545 commit_worktree(struct got_object_id **new_commit_id,
5546 struct got_pathlist_head *commitable_paths,
5547 struct got_object_id *head_commit_id,
5548 struct got_object_id *parent_id2,
5549 struct got_worktree *worktree,
5550 const char *author, const char *committer,
5551 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5552 got_worktree_status_cb status_cb, void *status_arg,
5553 struct got_repository *repo)
5555 const struct got_error *err = NULL, *unlockerr = NULL;
5556 struct got_pathlist_entry *pe;
5557 const char *head_ref_name = NULL;
5558 struct got_commit_object *head_commit = NULL;
5559 struct got_reference *head_ref2 = NULL;
5560 struct got_object_id *head_commit_id2 = NULL;
5561 struct got_tree_object *head_tree = NULL;
5562 struct got_object_id *new_tree_id = NULL;
5563 int nentries, nparents = 0;
5564 struct got_object_id_queue parent_ids;
5565 struct got_object_qid *pid = NULL;
5566 char *logmsg = NULL;
5568 *new_commit_id = NULL;
5570 STAILQ_INIT(&parent_ids);
5572 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5573 if (err)
5574 goto done;
5576 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5577 if (err)
5578 goto done;
5580 if (commit_msg_cb != NULL) {
5581 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5582 if (err)
5583 goto done;
5586 if (logmsg == NULL || strlen(logmsg) == 0) {
5587 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5588 goto done;
5591 /* Create blobs from added and modified files and record their IDs. */
5592 TAILQ_FOREACH(pe, commitable_paths, entry) {
5593 struct got_commitable *ct = pe->data;
5594 char *ondisk_path;
5596 /* Blobs for staged files already exist. */
5597 if (ct->staged_status == GOT_STATUS_ADD ||
5598 ct->staged_status == GOT_STATUS_MODIFY)
5599 continue;
5601 if (ct->status != GOT_STATUS_ADD &&
5602 ct->status != GOT_STATUS_MODIFY &&
5603 ct->status != GOT_STATUS_MODE_CHANGE)
5604 continue;
5606 if (asprintf(&ondisk_path, "%s/%s",
5607 worktree->root_path, pe->path) == -1) {
5608 err = got_error_from_errno("asprintf");
5609 goto done;
5611 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5612 free(ondisk_path);
5613 if (err)
5614 goto done;
5617 /* Recursively write new tree objects. */
5618 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5619 commitable_paths, status_cb, status_arg, repo);
5620 if (err)
5621 goto done;
5623 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5624 if (err)
5625 goto done;
5626 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5627 nparents++;
5628 if (parent_id2) {
5629 err = got_object_qid_alloc(&pid, parent_id2);
5630 if (err)
5631 goto done;
5632 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5633 nparents++;
5635 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5636 nparents, author, time(NULL), committer, time(NULL), logmsg, repo);
5637 if (logmsg != NULL)
5638 free(logmsg);
5639 if (err)
5640 goto done;
5642 /* Check if a concurrent commit to our branch has occurred. */
5643 head_ref_name = got_worktree_get_head_ref_name(worktree);
5644 if (head_ref_name == NULL) {
5645 err = got_error_from_errno("got_worktree_get_head_ref_name");
5646 goto done;
5648 /* Lock the reference here to prevent concurrent modification. */
5649 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5650 if (err)
5651 goto done;
5652 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5653 if (err)
5654 goto done;
5655 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5656 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5657 goto done;
5659 /* Update branch head in repository. */
5660 err = got_ref_change_ref(head_ref2, *new_commit_id);
5661 if (err)
5662 goto done;
5663 err = got_ref_write(head_ref2, repo);
5664 if (err)
5665 goto done;
5667 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5668 if (err)
5669 goto done;
5671 err = ref_base_commit(worktree, repo);
5672 if (err)
5673 goto done;
5674 done:
5675 got_object_id_queue_free(&parent_ids);
5676 if (head_tree)
5677 got_object_tree_close(head_tree);
5678 if (head_commit)
5679 got_object_commit_close(head_commit);
5680 free(head_commit_id2);
5681 if (head_ref2) {
5682 unlockerr = got_ref_unlock(head_ref2);
5683 if (unlockerr && err == NULL)
5684 err = unlockerr;
5685 got_ref_close(head_ref2);
5687 return err;
5690 static const struct got_error *
5691 check_path_is_commitable(const char *path,
5692 struct got_pathlist_head *commitable_paths)
5694 struct got_pathlist_entry *cpe = NULL;
5695 size_t path_len = strlen(path);
5697 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5698 struct got_commitable *ct = cpe->data;
5699 const char *ct_path = ct->path;
5701 while (ct_path[0] == '/')
5702 ct_path++;
5704 if (strcmp(path, ct_path) == 0 ||
5705 got_path_is_child(ct_path, path, path_len))
5706 break;
5709 if (cpe == NULL)
5710 return got_error_path(path, GOT_ERR_BAD_PATH);
5712 return NULL;
5715 static const struct got_error *
5716 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5718 int *have_staged_files = arg;
5720 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5721 *have_staged_files = 1;
5722 return got_error(GOT_ERR_CANCELLED);
5725 return NULL;
5728 static const struct got_error *
5729 check_non_staged_files(struct got_fileindex *fileindex,
5730 struct got_pathlist_head *paths)
5732 struct got_pathlist_entry *pe;
5733 struct got_fileindex_entry *ie;
5735 TAILQ_FOREACH(pe, paths, entry) {
5736 if (pe->path[0] == '\0')
5737 continue;
5738 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5739 if (ie == NULL)
5740 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5741 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5742 return got_error_path(pe->path,
5743 GOT_ERR_FILE_NOT_STAGED);
5746 return NULL;
5749 const struct got_error *
5750 got_worktree_commit(struct got_object_id **new_commit_id,
5751 struct got_worktree *worktree, struct got_pathlist_head *paths,
5752 const char *author, const char *committer, int allow_bad_symlinks,
5753 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5754 got_worktree_status_cb status_cb, void *status_arg,
5755 struct got_repository *repo)
5757 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5758 struct got_fileindex *fileindex = NULL;
5759 char *fileindex_path = NULL;
5760 struct got_pathlist_head commitable_paths;
5761 struct collect_commitables_arg cc_arg;
5762 struct got_pathlist_entry *pe;
5763 struct got_reference *head_ref = NULL;
5764 struct got_object_id *head_commit_id = NULL;
5765 int have_staged_files = 0;
5767 *new_commit_id = NULL;
5769 TAILQ_INIT(&commitable_paths);
5771 err = lock_worktree(worktree, LOCK_EX);
5772 if (err)
5773 goto done;
5775 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5776 if (err)
5777 goto done;
5779 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5780 if (err)
5781 goto done;
5783 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5784 if (err)
5785 goto done;
5787 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5788 &have_staged_files);
5789 if (err && err->code != GOT_ERR_CANCELLED)
5790 goto done;
5791 if (have_staged_files) {
5792 err = check_non_staged_files(fileindex, paths);
5793 if (err)
5794 goto done;
5797 cc_arg.commitable_paths = &commitable_paths;
5798 cc_arg.worktree = worktree;
5799 cc_arg.fileindex = fileindex;
5800 cc_arg.repo = repo;
5801 cc_arg.have_staged_files = have_staged_files;
5802 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5803 TAILQ_FOREACH(pe, paths, entry) {
5804 err = worktree_status(worktree, pe->path, fileindex, repo,
5805 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
5806 if (err)
5807 goto done;
5810 if (TAILQ_EMPTY(&commitable_paths)) {
5811 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5812 goto done;
5815 TAILQ_FOREACH(pe, paths, entry) {
5816 err = check_path_is_commitable(pe->path, &commitable_paths);
5817 if (err)
5818 goto done;
5821 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5822 struct got_commitable *ct = pe->data;
5823 const char *ct_path = ct->in_repo_path;
5825 while (ct_path[0] == '/')
5826 ct_path++;
5827 err = check_out_of_date(ct_path, ct->status,
5828 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5829 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5830 if (err)
5831 goto done;
5835 err = commit_worktree(new_commit_id, &commitable_paths,
5836 head_commit_id, NULL, worktree, author, committer,
5837 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5838 if (err)
5839 goto done;
5841 err = update_fileindex_after_commit(worktree, &commitable_paths,
5842 *new_commit_id, fileindex, have_staged_files);
5843 sync_err = sync_fileindex(fileindex, fileindex_path);
5844 if (sync_err && err == NULL)
5845 err = sync_err;
5846 done:
5847 if (fileindex)
5848 got_fileindex_free(fileindex);
5849 free(fileindex_path);
5850 unlockerr = lock_worktree(worktree, LOCK_SH);
5851 if (unlockerr && err == NULL)
5852 err = unlockerr;
5853 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5854 struct got_commitable *ct = pe->data;
5855 free_commitable(ct);
5857 got_pathlist_free(&commitable_paths);
5858 return err;
5861 const char *
5862 got_commitable_get_path(struct got_commitable *ct)
5864 return ct->path;
5867 unsigned int
5868 got_commitable_get_status(struct got_commitable *ct)
5870 return ct->status;
5873 struct check_rebase_ok_arg {
5874 struct got_worktree *worktree;
5875 struct got_repository *repo;
5878 static const struct got_error *
5879 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5881 const struct got_error *err = NULL;
5882 struct check_rebase_ok_arg *a = arg;
5883 unsigned char status;
5884 struct stat sb;
5885 char *ondisk_path;
5887 /* Reject rebase of a work tree with mixed base commits. */
5888 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5889 SHA1_DIGEST_LENGTH))
5890 return got_error(GOT_ERR_MIXED_COMMITS);
5892 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5893 == -1)
5894 return got_error_from_errno("asprintf");
5896 /* Reject rebase of a work tree with modified or staged files. */
5897 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5898 free(ondisk_path);
5899 if (err)
5900 return err;
5902 if (status != GOT_STATUS_NO_CHANGE)
5903 return got_error(GOT_ERR_MODIFIED);
5904 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5905 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5907 return NULL;
5910 const struct got_error *
5911 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5912 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5913 struct got_worktree *worktree, struct got_reference *branch,
5914 struct got_repository *repo)
5916 const struct got_error *err = NULL;
5917 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5918 char *branch_ref_name = NULL;
5919 char *fileindex_path = NULL;
5920 struct check_rebase_ok_arg ok_arg;
5921 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5922 struct got_object_id *wt_branch_tip = NULL;
5924 *new_base_branch_ref = NULL;
5925 *tmp_branch = NULL;
5926 *fileindex = NULL;
5928 err = lock_worktree(worktree, LOCK_EX);
5929 if (err)
5930 return err;
5932 err = open_fileindex(fileindex, &fileindex_path, worktree);
5933 if (err)
5934 goto done;
5936 ok_arg.worktree = worktree;
5937 ok_arg.repo = repo;
5938 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5939 &ok_arg);
5940 if (err)
5941 goto done;
5943 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5944 if (err)
5945 goto done;
5947 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5948 if (err)
5949 goto done;
5951 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5952 if (err)
5953 goto done;
5955 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5956 0);
5957 if (err)
5958 goto done;
5960 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5961 if (err)
5962 goto done;
5963 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5964 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5965 goto done;
5968 err = got_ref_alloc_symref(new_base_branch_ref,
5969 new_base_branch_ref_name, wt_branch);
5970 if (err)
5971 goto done;
5972 err = got_ref_write(*new_base_branch_ref, repo);
5973 if (err)
5974 goto done;
5976 /* TODO Lock original branch's ref while rebasing? */
5978 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5979 if (err)
5980 goto done;
5982 err = got_ref_write(branch_ref, repo);
5983 if (err)
5984 goto done;
5986 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5987 worktree->base_commit_id);
5988 if (err)
5989 goto done;
5990 err = got_ref_write(*tmp_branch, repo);
5991 if (err)
5992 goto done;
5994 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5995 if (err)
5996 goto done;
5997 done:
5998 free(fileindex_path);
5999 free(tmp_branch_name);
6000 free(new_base_branch_ref_name);
6001 free(branch_ref_name);
6002 if (branch_ref)
6003 got_ref_close(branch_ref);
6004 if (wt_branch)
6005 got_ref_close(wt_branch);
6006 free(wt_branch_tip);
6007 if (err) {
6008 if (*new_base_branch_ref) {
6009 got_ref_close(*new_base_branch_ref);
6010 *new_base_branch_ref = NULL;
6012 if (*tmp_branch) {
6013 got_ref_close(*tmp_branch);
6014 *tmp_branch = NULL;
6016 if (*fileindex) {
6017 got_fileindex_free(*fileindex);
6018 *fileindex = NULL;
6020 lock_worktree(worktree, LOCK_SH);
6022 return err;
6025 const struct got_error *
6026 got_worktree_rebase_continue(struct got_object_id **commit_id,
6027 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6028 struct got_reference **branch, struct got_fileindex **fileindex,
6029 struct got_worktree *worktree, struct got_repository *repo)
6031 const struct got_error *err;
6032 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6033 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6034 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6035 char *fileindex_path = NULL;
6036 int have_staged_files = 0;
6038 *commit_id = NULL;
6039 *new_base_branch = NULL;
6040 *tmp_branch = NULL;
6041 *branch = NULL;
6042 *fileindex = NULL;
6044 err = lock_worktree(worktree, LOCK_EX);
6045 if (err)
6046 return err;
6048 err = open_fileindex(fileindex, &fileindex_path, worktree);
6049 if (err)
6050 goto done;
6052 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6053 &have_staged_files);
6054 if (err && err->code != GOT_ERR_CANCELLED)
6055 goto done;
6056 if (have_staged_files) {
6057 err = got_error(GOT_ERR_STAGED_PATHS);
6058 goto done;
6061 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6062 if (err)
6063 goto done;
6065 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6066 if (err)
6067 goto done;
6069 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6070 if (err)
6071 goto done;
6073 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6074 if (err)
6075 goto done;
6077 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6078 if (err)
6079 goto done;
6081 err = got_ref_open(branch, repo,
6082 got_ref_get_symref_target(branch_ref), 0);
6083 if (err)
6084 goto done;
6086 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6087 if (err)
6088 goto done;
6090 err = got_ref_resolve(commit_id, repo, commit_ref);
6091 if (err)
6092 goto done;
6094 err = got_ref_open(new_base_branch, repo,
6095 new_base_branch_ref_name, 0);
6096 if (err)
6097 goto done;
6099 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6100 if (err)
6101 goto done;
6102 done:
6103 free(commit_ref_name);
6104 free(branch_ref_name);
6105 free(fileindex_path);
6106 if (commit_ref)
6107 got_ref_close(commit_ref);
6108 if (branch_ref)
6109 got_ref_close(branch_ref);
6110 if (err) {
6111 free(*commit_id);
6112 *commit_id = NULL;
6113 if (*tmp_branch) {
6114 got_ref_close(*tmp_branch);
6115 *tmp_branch = NULL;
6117 if (*new_base_branch) {
6118 got_ref_close(*new_base_branch);
6119 *new_base_branch = NULL;
6121 if (*branch) {
6122 got_ref_close(*branch);
6123 *branch = NULL;
6125 if (*fileindex) {
6126 got_fileindex_free(*fileindex);
6127 *fileindex = NULL;
6129 lock_worktree(worktree, LOCK_SH);
6131 return err;
6134 const struct got_error *
6135 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6137 const struct got_error *err;
6138 char *tmp_branch_name = NULL;
6140 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6141 if (err)
6142 return err;
6144 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6145 free(tmp_branch_name);
6146 return NULL;
6149 static const struct got_error *
6150 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6151 char **logmsg, void *arg)
6153 *logmsg = arg;
6154 return NULL;
6157 static const struct got_error *
6158 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6159 const char *path, struct got_object_id *blob_id,
6160 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6161 int dirfd, const char *de_name)
6163 return NULL;
6166 struct collect_merged_paths_arg {
6167 got_worktree_checkout_cb progress_cb;
6168 void *progress_arg;
6169 struct got_pathlist_head *merged_paths;
6172 static const struct got_error *
6173 collect_merged_paths(void *arg, unsigned char status, const char *path)
6175 const struct got_error *err;
6176 struct collect_merged_paths_arg *a = arg;
6177 char *p;
6178 struct got_pathlist_entry *new;
6180 err = (*a->progress_cb)(a->progress_arg, status, path);
6181 if (err)
6182 return err;
6184 if (status != GOT_STATUS_MERGE &&
6185 status != GOT_STATUS_ADD &&
6186 status != GOT_STATUS_DELETE &&
6187 status != GOT_STATUS_CONFLICT)
6188 return NULL;
6190 p = strdup(path);
6191 if (p == NULL)
6192 return got_error_from_errno("strdup");
6194 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6195 if (err || new == NULL)
6196 free(p);
6197 return err;
6200 void
6201 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6203 struct got_pathlist_entry *pe;
6205 TAILQ_FOREACH(pe, merged_paths, entry)
6206 free((char *)pe->path);
6208 got_pathlist_free(merged_paths);
6211 static const struct got_error *
6212 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6213 int is_rebase, struct got_repository *repo)
6215 const struct got_error *err;
6216 struct got_reference *commit_ref = NULL;
6218 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6219 if (err) {
6220 if (err->code != GOT_ERR_NOT_REF)
6221 goto done;
6222 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6223 if (err)
6224 goto done;
6225 err = got_ref_write(commit_ref, repo);
6226 if (err)
6227 goto done;
6228 } else if (is_rebase) {
6229 struct got_object_id *stored_id;
6230 int cmp;
6232 err = got_ref_resolve(&stored_id, repo, commit_ref);
6233 if (err)
6234 goto done;
6235 cmp = got_object_id_cmp(commit_id, stored_id);
6236 free(stored_id);
6237 if (cmp != 0) {
6238 err = got_error(GOT_ERR_REBASE_COMMITID);
6239 goto done;
6242 done:
6243 if (commit_ref)
6244 got_ref_close(commit_ref);
6245 return err;
6248 static const struct got_error *
6249 rebase_merge_files(struct got_pathlist_head *merged_paths,
6250 const char *commit_ref_name, struct got_worktree *worktree,
6251 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6252 struct got_object_id *commit_id, struct got_repository *repo,
6253 got_worktree_checkout_cb progress_cb, void *progress_arg,
6254 got_cancel_cb cancel_cb, void *cancel_arg)
6256 const struct got_error *err;
6257 struct got_reference *commit_ref = NULL;
6258 struct collect_merged_paths_arg cmp_arg;
6259 char *fileindex_path;
6261 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6263 err = get_fileindex_path(&fileindex_path, worktree);
6264 if (err)
6265 return err;
6267 cmp_arg.progress_cb = progress_cb;
6268 cmp_arg.progress_arg = progress_arg;
6269 cmp_arg.merged_paths = merged_paths;
6270 err = merge_files(worktree, fileindex, fileindex_path,
6271 parent_commit_id, commit_id, repo, collect_merged_paths,
6272 &cmp_arg, cancel_cb, cancel_arg);
6273 if (commit_ref)
6274 got_ref_close(commit_ref);
6275 return err;
6278 const struct got_error *
6279 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6280 struct got_worktree *worktree, struct got_fileindex *fileindex,
6281 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6282 struct got_repository *repo,
6283 got_worktree_checkout_cb progress_cb, void *progress_arg,
6284 got_cancel_cb cancel_cb, void *cancel_arg)
6286 const struct got_error *err;
6287 char *commit_ref_name;
6289 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6290 if (err)
6291 return err;
6293 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6294 if (err)
6295 goto done;
6297 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6298 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6299 progress_arg, cancel_cb, cancel_arg);
6300 done:
6301 free(commit_ref_name);
6302 return err;
6305 const struct got_error *
6306 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6307 struct got_worktree *worktree, struct got_fileindex *fileindex,
6308 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6309 struct got_repository *repo,
6310 got_worktree_checkout_cb progress_cb, void *progress_arg,
6311 got_cancel_cb cancel_cb, void *cancel_arg)
6313 const struct got_error *err;
6314 char *commit_ref_name;
6316 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6317 if (err)
6318 return err;
6320 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6321 if (err)
6322 goto done;
6324 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6325 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6326 progress_arg, cancel_cb, cancel_arg);
6327 done:
6328 free(commit_ref_name);
6329 return err;
6332 static const struct got_error *
6333 rebase_commit(struct got_object_id **new_commit_id,
6334 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6335 struct got_worktree *worktree, struct got_fileindex *fileindex,
6336 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6337 const char *new_logmsg, struct got_repository *repo)
6339 const struct got_error *err, *sync_err;
6340 struct got_pathlist_head commitable_paths;
6341 struct collect_commitables_arg cc_arg;
6342 char *fileindex_path = NULL;
6343 struct got_reference *head_ref = NULL;
6344 struct got_object_id *head_commit_id = NULL;
6345 char *logmsg = NULL;
6347 TAILQ_INIT(&commitable_paths);
6348 *new_commit_id = NULL;
6350 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6352 err = get_fileindex_path(&fileindex_path, worktree);
6353 if (err)
6354 return err;
6356 cc_arg.commitable_paths = &commitable_paths;
6357 cc_arg.worktree = worktree;
6358 cc_arg.repo = repo;
6359 cc_arg.have_staged_files = 0;
6361 * If possible get the status of individual files directly to
6362 * avoid crawling the entire work tree once per rebased commit.
6364 * Ideally, merged_paths would contain a list of commitables
6365 * we could use so we could skip worktree_status() entirely.
6366 * However, we would then need carefully keep track of cumulative
6367 * effects of operations such as file additions and deletions
6368 * in 'got histedit -f' (folding multiple commits into one),
6369 * and this extra complexity is not really worth it.
6371 if (merged_paths) {
6372 struct got_pathlist_entry *pe;
6373 TAILQ_FOREACH(pe, merged_paths, entry) {
6374 err = worktree_status(worktree, pe->path, fileindex,
6375 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6376 0);
6377 if (err)
6378 goto done;
6380 } else {
6381 err = worktree_status(worktree, "", fileindex, repo,
6382 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6383 if (err)
6384 goto done;
6387 if (TAILQ_EMPTY(&commitable_paths)) {
6388 /* No-op change; commit will be elided. */
6389 err = got_ref_delete(commit_ref, repo);
6390 if (err)
6391 goto done;
6392 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6393 goto done;
6396 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6397 if (err)
6398 goto done;
6400 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6401 if (err)
6402 goto done;
6404 if (new_logmsg) {
6405 logmsg = strdup(new_logmsg);
6406 if (logmsg == NULL) {
6407 err = got_error_from_errno("strdup");
6408 goto done;
6410 } else {
6411 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6412 if (err)
6413 goto done;
6416 /* NB: commit_worktree will call free(logmsg) */
6417 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6418 NULL, worktree, got_object_commit_get_author(orig_commit),
6419 got_object_commit_get_committer(orig_commit),
6420 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6421 if (err)
6422 goto done;
6424 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6425 if (err)
6426 goto done;
6428 err = got_ref_delete(commit_ref, repo);
6429 if (err)
6430 goto done;
6432 err = update_fileindex_after_commit(worktree, &commitable_paths,
6433 *new_commit_id, fileindex, 0);
6434 sync_err = sync_fileindex(fileindex, fileindex_path);
6435 if (sync_err && err == NULL)
6436 err = sync_err;
6437 done:
6438 free(fileindex_path);
6439 free(head_commit_id);
6440 if (head_ref)
6441 got_ref_close(head_ref);
6442 if (err) {
6443 free(*new_commit_id);
6444 *new_commit_id = NULL;
6446 return err;
6449 const struct got_error *
6450 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6451 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6452 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6453 struct got_commit_object *orig_commit,
6454 struct got_object_id *orig_commit_id, struct got_repository *repo)
6456 const struct got_error *err;
6457 char *commit_ref_name;
6458 struct got_reference *commit_ref = NULL;
6459 struct got_object_id *commit_id = NULL;
6461 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6462 if (err)
6463 return err;
6465 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6466 if (err)
6467 goto done;
6468 err = got_ref_resolve(&commit_id, repo, commit_ref);
6469 if (err)
6470 goto done;
6471 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6472 err = got_error(GOT_ERR_REBASE_COMMITID);
6473 goto done;
6476 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6477 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6478 done:
6479 if (commit_ref)
6480 got_ref_close(commit_ref);
6481 free(commit_ref_name);
6482 free(commit_id);
6483 return err;
6486 const struct got_error *
6487 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6488 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6489 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6490 struct got_commit_object *orig_commit,
6491 struct got_object_id *orig_commit_id, const char *new_logmsg,
6492 struct got_repository *repo)
6494 const struct got_error *err;
6495 char *commit_ref_name;
6496 struct got_reference *commit_ref = NULL;
6498 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6499 if (err)
6500 return err;
6502 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6503 if (err)
6504 goto done;
6506 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6507 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6508 done:
6509 if (commit_ref)
6510 got_ref_close(commit_ref);
6511 free(commit_ref_name);
6512 return err;
6515 const struct got_error *
6516 got_worktree_rebase_postpone(struct got_worktree *worktree,
6517 struct got_fileindex *fileindex)
6519 if (fileindex)
6520 got_fileindex_free(fileindex);
6521 return lock_worktree(worktree, LOCK_SH);
6524 static const struct got_error *
6525 delete_ref(const char *name, struct got_repository *repo)
6527 const struct got_error *err;
6528 struct got_reference *ref;
6530 err = got_ref_open(&ref, repo, name, 0);
6531 if (err) {
6532 if (err->code == GOT_ERR_NOT_REF)
6533 return NULL;
6534 return err;
6537 err = got_ref_delete(ref, repo);
6538 got_ref_close(ref);
6539 return err;
6542 static const struct got_error *
6543 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6545 const struct got_error *err;
6546 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6547 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6549 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6550 if (err)
6551 goto done;
6552 err = delete_ref(tmp_branch_name, repo);
6553 if (err)
6554 goto done;
6556 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6557 if (err)
6558 goto done;
6559 err = delete_ref(new_base_branch_ref_name, repo);
6560 if (err)
6561 goto done;
6563 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6564 if (err)
6565 goto done;
6566 err = delete_ref(branch_ref_name, repo);
6567 if (err)
6568 goto done;
6570 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6571 if (err)
6572 goto done;
6573 err = delete_ref(commit_ref_name, repo);
6574 if (err)
6575 goto done;
6577 done:
6578 free(tmp_branch_name);
6579 free(new_base_branch_ref_name);
6580 free(branch_ref_name);
6581 free(commit_ref_name);
6582 return err;
6585 const struct got_error *
6586 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6587 struct got_object_id *new_commit_id, struct got_repository *repo)
6589 const struct got_error *err;
6590 struct got_reference *ref = NULL;
6591 struct got_object_id *old_commit_id = NULL;
6592 const char *branch_name = NULL;
6593 char *new_id_str = NULL;
6594 char *refname = NULL;
6596 branch_name = got_ref_get_name(branch);
6597 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6598 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6599 branch_name += 11;
6601 err = got_object_id_str(&new_id_str, new_commit_id);
6602 if (err)
6603 return err;
6605 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6606 new_id_str) == -1) {
6607 err = got_error_from_errno("asprintf");
6608 goto done;
6611 err = got_ref_resolve(&old_commit_id, repo, branch);
6612 if (err)
6613 goto done;
6615 err = got_ref_alloc(&ref, refname, old_commit_id);
6616 if (err)
6617 goto done;
6619 err = got_ref_write(ref, repo);
6620 done:
6621 free(new_id_str);
6622 free(refname);
6623 free(old_commit_id);
6624 if (ref)
6625 got_ref_close(ref);
6626 return err;
6629 const struct got_error *
6630 got_worktree_rebase_complete(struct got_worktree *worktree,
6631 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6632 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6633 struct got_repository *repo, int create_backup)
6635 const struct got_error *err, *unlockerr, *sync_err;
6636 struct got_object_id *new_head_commit_id = NULL;
6637 char *fileindex_path = NULL;
6639 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6640 if (err)
6641 return err;
6643 if (create_backup) {
6644 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6645 rebased_branch, new_head_commit_id, repo);
6646 if (err)
6647 goto done;
6650 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6651 if (err)
6652 goto done;
6654 err = got_ref_write(rebased_branch, repo);
6655 if (err)
6656 goto done;
6658 err = got_worktree_set_head_ref(worktree, rebased_branch);
6659 if (err)
6660 goto done;
6662 err = delete_rebase_refs(worktree, repo);
6663 if (err)
6664 goto done;
6666 err = get_fileindex_path(&fileindex_path, worktree);
6667 if (err)
6668 goto done;
6669 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6670 sync_err = sync_fileindex(fileindex, fileindex_path);
6671 if (sync_err && err == NULL)
6672 err = sync_err;
6673 done:
6674 got_fileindex_free(fileindex);
6675 free(fileindex_path);
6676 free(new_head_commit_id);
6677 unlockerr = lock_worktree(worktree, LOCK_SH);
6678 if (unlockerr && err == NULL)
6679 err = unlockerr;
6680 return err;
6683 const struct got_error *
6684 got_worktree_rebase_abort(struct got_worktree *worktree,
6685 struct got_fileindex *fileindex, struct got_repository *repo,
6686 struct got_reference *new_base_branch,
6687 got_worktree_checkout_cb progress_cb, void *progress_arg)
6689 const struct got_error *err, *unlockerr, *sync_err;
6690 struct got_reference *resolved = NULL;
6691 struct got_object_id *commit_id = NULL;
6692 char *fileindex_path = NULL;
6693 struct revert_file_args rfa;
6694 struct got_object_id *tree_id = NULL;
6696 err = lock_worktree(worktree, LOCK_EX);
6697 if (err)
6698 return err;
6700 err = got_ref_open(&resolved, repo,
6701 got_ref_get_symref_target(new_base_branch), 0);
6702 if (err)
6703 goto done;
6705 err = got_worktree_set_head_ref(worktree, resolved);
6706 if (err)
6707 goto done;
6710 * XXX commits to the base branch could have happened while
6711 * we were busy rebasing; should we store the original commit ID
6712 * when rebase begins and read it back here?
6714 err = got_ref_resolve(&commit_id, repo, resolved);
6715 if (err)
6716 goto done;
6718 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6719 if (err)
6720 goto done;
6722 err = got_object_id_by_path(&tree_id, repo,
6723 worktree->base_commit_id, worktree->path_prefix);
6724 if (err)
6725 goto done;
6727 err = delete_rebase_refs(worktree, repo);
6728 if (err)
6729 goto done;
6731 err = get_fileindex_path(&fileindex_path, worktree);
6732 if (err)
6733 goto done;
6735 rfa.worktree = worktree;
6736 rfa.fileindex = fileindex;
6737 rfa.progress_cb = progress_cb;
6738 rfa.progress_arg = progress_arg;
6739 rfa.patch_cb = NULL;
6740 rfa.patch_arg = NULL;
6741 rfa.repo = repo;
6742 rfa.unlink_added_files = 0;
6743 err = worktree_status(worktree, "", fileindex, repo,
6744 revert_file, &rfa, NULL, NULL, 1, 0);
6745 if (err)
6746 goto sync;
6748 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6749 repo, progress_cb, progress_arg, NULL, NULL);
6750 sync:
6751 sync_err = sync_fileindex(fileindex, fileindex_path);
6752 if (sync_err && err == NULL)
6753 err = sync_err;
6754 done:
6755 got_ref_close(resolved);
6756 free(tree_id);
6757 free(commit_id);
6758 if (fileindex)
6759 got_fileindex_free(fileindex);
6760 free(fileindex_path);
6762 unlockerr = lock_worktree(worktree, LOCK_SH);
6763 if (unlockerr && err == NULL)
6764 err = unlockerr;
6765 return err;
6768 const struct got_error *
6769 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6770 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6771 struct got_fileindex **fileindex, struct got_worktree *worktree,
6772 struct got_repository *repo)
6774 const struct got_error *err = NULL;
6775 char *tmp_branch_name = NULL;
6776 char *branch_ref_name = NULL;
6777 char *base_commit_ref_name = NULL;
6778 char *fileindex_path = NULL;
6779 struct check_rebase_ok_arg ok_arg;
6780 struct got_reference *wt_branch = NULL;
6781 struct got_reference *base_commit_ref = NULL;
6783 *tmp_branch = NULL;
6784 *branch_ref = NULL;
6785 *base_commit_id = NULL;
6786 *fileindex = NULL;
6788 err = lock_worktree(worktree, LOCK_EX);
6789 if (err)
6790 return err;
6792 err = open_fileindex(fileindex, &fileindex_path, worktree);
6793 if (err)
6794 goto done;
6796 ok_arg.worktree = worktree;
6797 ok_arg.repo = repo;
6798 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6799 &ok_arg);
6800 if (err)
6801 goto done;
6803 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6804 if (err)
6805 goto done;
6807 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6808 if (err)
6809 goto done;
6811 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6812 worktree);
6813 if (err)
6814 goto done;
6816 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6817 0);
6818 if (err)
6819 goto done;
6821 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6822 if (err)
6823 goto done;
6825 err = got_ref_write(*branch_ref, repo);
6826 if (err)
6827 goto done;
6829 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6830 worktree->base_commit_id);
6831 if (err)
6832 goto done;
6833 err = got_ref_write(base_commit_ref, repo);
6834 if (err)
6835 goto done;
6836 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6837 if (*base_commit_id == NULL) {
6838 err = got_error_from_errno("got_object_id_dup");
6839 goto done;
6842 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6843 worktree->base_commit_id);
6844 if (err)
6845 goto done;
6846 err = got_ref_write(*tmp_branch, repo);
6847 if (err)
6848 goto done;
6850 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6851 if (err)
6852 goto done;
6853 done:
6854 free(fileindex_path);
6855 free(tmp_branch_name);
6856 free(branch_ref_name);
6857 free(base_commit_ref_name);
6858 if (wt_branch)
6859 got_ref_close(wt_branch);
6860 if (err) {
6861 if (*branch_ref) {
6862 got_ref_close(*branch_ref);
6863 *branch_ref = NULL;
6865 if (*tmp_branch) {
6866 got_ref_close(*tmp_branch);
6867 *tmp_branch = NULL;
6869 free(*base_commit_id);
6870 if (*fileindex) {
6871 got_fileindex_free(*fileindex);
6872 *fileindex = NULL;
6874 lock_worktree(worktree, LOCK_SH);
6876 return err;
6879 const struct got_error *
6880 got_worktree_histedit_postpone(struct got_worktree *worktree,
6881 struct got_fileindex *fileindex)
6883 if (fileindex)
6884 got_fileindex_free(fileindex);
6885 return lock_worktree(worktree, LOCK_SH);
6888 const struct got_error *
6889 got_worktree_histedit_in_progress(int *in_progress,
6890 struct got_worktree *worktree)
6892 const struct got_error *err;
6893 char *tmp_branch_name = NULL;
6895 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6896 if (err)
6897 return err;
6899 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6900 free(tmp_branch_name);
6901 return NULL;
6904 const struct got_error *
6905 got_worktree_histedit_continue(struct got_object_id **commit_id,
6906 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6907 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6908 struct got_worktree *worktree, struct got_repository *repo)
6910 const struct got_error *err;
6911 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6912 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6913 struct got_reference *commit_ref = NULL;
6914 struct got_reference *base_commit_ref = NULL;
6915 char *fileindex_path = NULL;
6916 int have_staged_files = 0;
6918 *commit_id = NULL;
6919 *tmp_branch = NULL;
6920 *base_commit_id = NULL;
6921 *fileindex = NULL;
6923 err = lock_worktree(worktree, LOCK_EX);
6924 if (err)
6925 return err;
6927 err = open_fileindex(fileindex, &fileindex_path, worktree);
6928 if (err)
6929 goto done;
6931 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6932 &have_staged_files);
6933 if (err && err->code != GOT_ERR_CANCELLED)
6934 goto done;
6935 if (have_staged_files) {
6936 err = got_error(GOT_ERR_STAGED_PATHS);
6937 goto done;
6940 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6941 if (err)
6942 goto done;
6944 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6945 if (err)
6946 goto done;
6948 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6949 if (err)
6950 goto done;
6952 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6953 worktree);
6954 if (err)
6955 goto done;
6957 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6958 if (err)
6959 goto done;
6961 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6962 if (err)
6963 goto done;
6964 err = got_ref_resolve(commit_id, repo, commit_ref);
6965 if (err)
6966 goto done;
6968 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6969 if (err)
6970 goto done;
6971 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6972 if (err)
6973 goto done;
6975 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6976 if (err)
6977 goto done;
6978 done:
6979 free(commit_ref_name);
6980 free(branch_ref_name);
6981 free(fileindex_path);
6982 if (commit_ref)
6983 got_ref_close(commit_ref);
6984 if (base_commit_ref)
6985 got_ref_close(base_commit_ref);
6986 if (err) {
6987 free(*commit_id);
6988 *commit_id = NULL;
6989 free(*base_commit_id);
6990 *base_commit_id = NULL;
6991 if (*tmp_branch) {
6992 got_ref_close(*tmp_branch);
6993 *tmp_branch = NULL;
6995 if (*fileindex) {
6996 got_fileindex_free(*fileindex);
6997 *fileindex = NULL;
6999 lock_worktree(worktree, LOCK_EX);
7001 return err;
7004 static const struct got_error *
7005 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7007 const struct got_error *err;
7008 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7009 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7011 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7012 if (err)
7013 goto done;
7014 err = delete_ref(tmp_branch_name, repo);
7015 if (err)
7016 goto done;
7018 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7019 worktree);
7020 if (err)
7021 goto done;
7022 err = delete_ref(base_commit_ref_name, repo);
7023 if (err)
7024 goto done;
7026 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7027 if (err)
7028 goto done;
7029 err = delete_ref(branch_ref_name, repo);
7030 if (err)
7031 goto done;
7033 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7034 if (err)
7035 goto done;
7036 err = delete_ref(commit_ref_name, repo);
7037 if (err)
7038 goto done;
7039 done:
7040 free(tmp_branch_name);
7041 free(base_commit_ref_name);
7042 free(branch_ref_name);
7043 free(commit_ref_name);
7044 return err;
7047 const struct got_error *
7048 got_worktree_histedit_abort(struct got_worktree *worktree,
7049 struct got_fileindex *fileindex, struct got_repository *repo,
7050 struct got_reference *branch, struct got_object_id *base_commit_id,
7051 got_worktree_checkout_cb progress_cb, void *progress_arg)
7053 const struct got_error *err, *unlockerr, *sync_err;
7054 struct got_reference *resolved = NULL;
7055 char *fileindex_path = NULL;
7056 struct got_object_id *tree_id = NULL;
7057 struct revert_file_args rfa;
7059 err = lock_worktree(worktree, LOCK_EX);
7060 if (err)
7061 return err;
7063 err = got_ref_open(&resolved, repo,
7064 got_ref_get_symref_target(branch), 0);
7065 if (err)
7066 goto done;
7068 err = got_worktree_set_head_ref(worktree, resolved);
7069 if (err)
7070 goto done;
7072 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7073 if (err)
7074 goto done;
7076 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
7077 worktree->path_prefix);
7078 if (err)
7079 goto done;
7081 err = delete_histedit_refs(worktree, repo);
7082 if (err)
7083 goto done;
7085 err = get_fileindex_path(&fileindex_path, worktree);
7086 if (err)
7087 goto done;
7089 rfa.worktree = worktree;
7090 rfa.fileindex = fileindex;
7091 rfa.progress_cb = progress_cb;
7092 rfa.progress_arg = progress_arg;
7093 rfa.patch_cb = NULL;
7094 rfa.patch_arg = NULL;
7095 rfa.repo = repo;
7096 rfa.unlink_added_files = 0;
7097 err = worktree_status(worktree, "", fileindex, repo,
7098 revert_file, &rfa, NULL, NULL, 1, 0);
7099 if (err)
7100 goto sync;
7102 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7103 repo, progress_cb, progress_arg, NULL, NULL);
7104 sync:
7105 sync_err = sync_fileindex(fileindex, fileindex_path);
7106 if (sync_err && err == NULL)
7107 err = sync_err;
7108 done:
7109 got_ref_close(resolved);
7110 free(tree_id);
7111 free(fileindex_path);
7113 unlockerr = lock_worktree(worktree, LOCK_SH);
7114 if (unlockerr && err == NULL)
7115 err = unlockerr;
7116 return err;
7119 const struct got_error *
7120 got_worktree_histedit_complete(struct got_worktree *worktree,
7121 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7122 struct got_reference *edited_branch, struct got_repository *repo)
7124 const struct got_error *err, *unlockerr, *sync_err;
7125 struct got_object_id *new_head_commit_id = NULL;
7126 struct got_reference *resolved = NULL;
7127 char *fileindex_path = NULL;
7129 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7130 if (err)
7131 return err;
7133 err = got_ref_open(&resolved, repo,
7134 got_ref_get_symref_target(edited_branch), 0);
7135 if (err)
7136 goto done;
7138 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7139 resolved, new_head_commit_id, repo);
7140 if (err)
7141 goto done;
7143 err = got_ref_change_ref(resolved, new_head_commit_id);
7144 if (err)
7145 goto done;
7147 err = got_ref_write(resolved, repo);
7148 if (err)
7149 goto done;
7151 err = got_worktree_set_head_ref(worktree, resolved);
7152 if (err)
7153 goto done;
7155 err = delete_histedit_refs(worktree, repo);
7156 if (err)
7157 goto done;
7159 err = get_fileindex_path(&fileindex_path, worktree);
7160 if (err)
7161 goto done;
7162 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7163 sync_err = sync_fileindex(fileindex, fileindex_path);
7164 if (sync_err && err == NULL)
7165 err = sync_err;
7166 done:
7167 got_fileindex_free(fileindex);
7168 free(fileindex_path);
7169 free(new_head_commit_id);
7170 unlockerr = lock_worktree(worktree, LOCK_SH);
7171 if (unlockerr && err == NULL)
7172 err = unlockerr;
7173 return err;
7176 const struct got_error *
7177 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7178 struct got_object_id *commit_id, struct got_repository *repo)
7180 const struct got_error *err;
7181 char *commit_ref_name;
7183 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7184 if (err)
7185 return err;
7187 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7188 if (err)
7189 goto done;
7191 err = delete_ref(commit_ref_name, repo);
7192 done:
7193 free(commit_ref_name);
7194 return err;
7197 const struct got_error *
7198 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7199 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7200 struct got_worktree *worktree, const char *refname,
7201 struct got_repository *repo)
7203 const struct got_error *err = NULL;
7204 char *fileindex_path = NULL;
7205 struct check_rebase_ok_arg ok_arg;
7207 *fileindex = NULL;
7208 *branch_ref = NULL;
7209 *base_branch_ref = NULL;
7211 err = lock_worktree(worktree, LOCK_EX);
7212 if (err)
7213 return err;
7215 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7216 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7217 "cannot integrate a branch into itself; "
7218 "update -b or different branch name required");
7219 goto done;
7222 err = open_fileindex(fileindex, &fileindex_path, worktree);
7223 if (err)
7224 goto done;
7226 /* Preconditions are the same as for rebase. */
7227 ok_arg.worktree = worktree;
7228 ok_arg.repo = repo;
7229 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7230 &ok_arg);
7231 if (err)
7232 goto done;
7234 err = got_ref_open(branch_ref, repo, refname, 1);
7235 if (err)
7236 goto done;
7238 err = got_ref_open(base_branch_ref, repo,
7239 got_worktree_get_head_ref_name(worktree), 1);
7240 done:
7241 if (err) {
7242 if (*branch_ref) {
7243 got_ref_close(*branch_ref);
7244 *branch_ref = NULL;
7246 if (*base_branch_ref) {
7247 got_ref_close(*base_branch_ref);
7248 *base_branch_ref = NULL;
7250 if (*fileindex) {
7251 got_fileindex_free(*fileindex);
7252 *fileindex = NULL;
7254 lock_worktree(worktree, LOCK_SH);
7256 return err;
7259 const struct got_error *
7260 got_worktree_integrate_continue(struct got_worktree *worktree,
7261 struct got_fileindex *fileindex, struct got_repository *repo,
7262 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7263 got_worktree_checkout_cb progress_cb, void *progress_arg,
7264 got_cancel_cb cancel_cb, void *cancel_arg)
7266 const struct got_error *err = NULL, *sync_err, *unlockerr;
7267 char *fileindex_path = NULL;
7268 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7270 err = get_fileindex_path(&fileindex_path, worktree);
7271 if (err)
7272 goto done;
7274 err = got_ref_resolve(&commit_id, repo, branch_ref);
7275 if (err)
7276 goto done;
7278 err = got_object_id_by_path(&tree_id, repo, commit_id,
7279 worktree->path_prefix);
7280 if (err)
7281 goto done;
7283 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7284 if (err)
7285 goto done;
7287 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7288 progress_cb, progress_arg, cancel_cb, cancel_arg);
7289 if (err)
7290 goto sync;
7292 err = got_ref_change_ref(base_branch_ref, commit_id);
7293 if (err)
7294 goto sync;
7296 err = got_ref_write(base_branch_ref, repo);
7297 if (err)
7298 goto sync;
7300 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7301 sync:
7302 sync_err = sync_fileindex(fileindex, fileindex_path);
7303 if (sync_err && err == NULL)
7304 err = sync_err;
7306 done:
7307 unlockerr = got_ref_unlock(branch_ref);
7308 if (unlockerr && err == NULL)
7309 err = unlockerr;
7310 got_ref_close(branch_ref);
7312 unlockerr = got_ref_unlock(base_branch_ref);
7313 if (unlockerr && err == NULL)
7314 err = unlockerr;
7315 got_ref_close(base_branch_ref);
7317 got_fileindex_free(fileindex);
7318 free(fileindex_path);
7319 free(tree_id);
7321 unlockerr = lock_worktree(worktree, LOCK_SH);
7322 if (unlockerr && err == NULL)
7323 err = unlockerr;
7324 return err;
7327 const struct got_error *
7328 got_worktree_integrate_abort(struct got_worktree *worktree,
7329 struct got_fileindex *fileindex, struct got_repository *repo,
7330 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7332 const struct got_error *err = NULL, *unlockerr = NULL;
7334 got_fileindex_free(fileindex);
7336 err = lock_worktree(worktree, LOCK_SH);
7338 unlockerr = got_ref_unlock(branch_ref);
7339 if (unlockerr && err == NULL)
7340 err = unlockerr;
7341 got_ref_close(branch_ref);
7343 unlockerr = got_ref_unlock(base_branch_ref);
7344 if (unlockerr && err == NULL)
7345 err = unlockerr;
7346 got_ref_close(base_branch_ref);
7348 return err;
7351 const struct got_error *
7352 got_worktree_merge_postpone(struct got_worktree *worktree,
7353 struct got_fileindex *fileindex)
7355 const struct got_error *err, *sync_err;
7356 char *fileindex_path = NULL;
7358 err = get_fileindex_path(&fileindex_path, worktree);
7359 if (err)
7360 goto done;
7362 sync_err = sync_fileindex(fileindex, fileindex_path);
7364 err = lock_worktree(worktree, LOCK_SH);
7365 if (sync_err && err == NULL)
7366 err = sync_err;
7367 done:
7368 got_fileindex_free(fileindex);
7369 free(fileindex_path);
7370 return err;
7373 static const struct got_error *
7374 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7376 const struct got_error *err;
7377 char *branch_refname = NULL, *commit_refname = NULL;
7379 err = get_merge_branch_ref_name(&branch_refname, worktree);
7380 if (err)
7381 goto done;
7382 err = delete_ref(branch_refname, repo);
7383 if (err)
7384 goto done;
7386 err = get_merge_commit_ref_name(&commit_refname, worktree);
7387 if (err)
7388 goto done;
7389 err = delete_ref(commit_refname, repo);
7390 if (err)
7391 goto done;
7393 done:
7394 free(branch_refname);
7395 free(commit_refname);
7396 return err;
7399 struct merge_commit_msg_arg {
7400 struct got_worktree *worktree;
7401 const char *branch_name;
7404 static const struct got_error *
7405 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths, char **logmsg,
7406 void *arg)
7408 struct merge_commit_msg_arg *a = arg;
7410 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7411 got_worktree_get_head_ref_name(a->worktree)) == -1)
7412 return got_error_from_errno("asprintf");
7414 return NULL;
7418 const struct got_error *
7419 got_worktree_merge_branch(struct got_worktree *worktree,
7420 struct got_fileindex *fileindex,
7421 struct got_object_id *yca_commit_id,
7422 struct got_object_id *branch_tip,
7423 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7424 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7426 const struct got_error *err;
7427 char *fileindex_path = NULL;
7429 err = get_fileindex_path(&fileindex_path, worktree);
7430 if (err)
7431 goto done;
7433 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7434 worktree);
7435 if (err)
7436 goto done;
7438 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7439 branch_tip, repo, progress_cb, progress_arg,
7440 cancel_cb, cancel_arg);
7441 done:
7442 free(fileindex_path);
7443 return err;
7446 const struct got_error *
7447 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7448 struct got_worktree *worktree, struct got_fileindex *fileindex,
7449 const char *author, const char *committer, int allow_bad_symlinks,
7450 struct got_object_id *branch_tip, const char *branch_name,
7451 struct got_repository *repo,
7452 got_worktree_status_cb status_cb, void *status_arg)
7455 const struct got_error *err = NULL, *sync_err;
7456 struct got_pathlist_head commitable_paths;
7457 struct collect_commitables_arg cc_arg;
7458 struct got_pathlist_entry *pe;
7459 struct got_reference *head_ref = NULL;
7460 struct got_object_id *head_commit_id = NULL;
7461 int have_staged_files = 0;
7462 struct merge_commit_msg_arg mcm_arg;
7463 char *fileindex_path = NULL;
7465 *new_commit_id = NULL;
7467 TAILQ_INIT(&commitable_paths);
7469 err = get_fileindex_path(&fileindex_path, worktree);
7470 if (err)
7471 goto done;
7473 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7474 if (err)
7475 goto done;
7477 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7478 if (err)
7479 goto done;
7481 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7482 &have_staged_files);
7483 if (err && err->code != GOT_ERR_CANCELLED)
7484 goto done;
7485 if (have_staged_files) {
7486 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7487 goto done;
7490 cc_arg.commitable_paths = &commitable_paths;
7491 cc_arg.worktree = worktree;
7492 cc_arg.fileindex = fileindex;
7493 cc_arg.repo = repo;
7494 cc_arg.have_staged_files = have_staged_files;
7495 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7496 err = worktree_status(worktree, "", fileindex, repo,
7497 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7498 if (err)
7499 goto done;
7501 if (TAILQ_EMPTY(&commitable_paths)) {
7502 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7503 "merge of %s cannot proceed", branch_name);
7504 goto done;
7507 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7508 struct got_commitable *ct = pe->data;
7509 const char *ct_path = ct->in_repo_path;
7511 while (ct_path[0] == '/')
7512 ct_path++;
7513 err = check_out_of_date(ct_path, ct->status,
7514 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7515 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7516 if (err)
7517 goto done;
7521 mcm_arg.worktree = worktree;
7522 mcm_arg.branch_name = branch_name;
7523 err = commit_worktree(new_commit_id, &commitable_paths,
7524 head_commit_id, branch_tip, worktree, author, committer,
7525 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7526 if (err)
7527 goto done;
7529 err = update_fileindex_after_commit(worktree, &commitable_paths,
7530 *new_commit_id, fileindex, have_staged_files);
7531 sync_err = sync_fileindex(fileindex, fileindex_path);
7532 if (sync_err && err == NULL)
7533 err = sync_err;
7534 done:
7535 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7536 struct got_commitable *ct = pe->data;
7537 free_commitable(ct);
7539 got_pathlist_free(&commitable_paths);
7540 free(fileindex_path);
7541 return err;
7544 const struct got_error *
7545 got_worktree_merge_complete(struct got_worktree *worktree,
7546 struct got_fileindex *fileindex, struct got_repository *repo)
7548 const struct got_error *err, *unlockerr, *sync_err;
7549 char *fileindex_path = NULL;
7551 err = delete_merge_refs(worktree, repo);
7552 if (err)
7553 goto done;
7555 err = get_fileindex_path(&fileindex_path, worktree);
7556 if (err)
7557 goto done;
7558 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7559 sync_err = sync_fileindex(fileindex, fileindex_path);
7560 if (sync_err && err == NULL)
7561 err = sync_err;
7562 done:
7563 got_fileindex_free(fileindex);
7564 free(fileindex_path);
7565 unlockerr = lock_worktree(worktree, LOCK_SH);
7566 if (unlockerr && err == NULL)
7567 err = unlockerr;
7568 return err;
7571 const struct got_error *
7572 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7573 struct got_repository *repo)
7575 const struct got_error *err;
7576 char *branch_refname = NULL;
7577 struct got_reference *branch_ref = NULL;
7579 *in_progress = 0;
7581 err = get_merge_branch_ref_name(&branch_refname, worktree);
7582 if (err)
7583 return err;
7584 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7585 free(branch_refname);
7586 if (err) {
7587 if (err->code != GOT_ERR_NOT_REF)
7588 return err;
7589 } else
7590 *in_progress = 1;
7592 return NULL;
7595 const struct got_error *got_worktree_merge_prepare(
7596 struct got_fileindex **fileindex, struct got_worktree *worktree,
7597 struct got_reference *branch, struct got_repository *repo)
7599 const struct got_error *err = NULL;
7600 char *fileindex_path = NULL;
7601 char *branch_refname = NULL, *commit_refname = NULL;
7602 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
7603 struct got_reference *commit_ref = NULL;
7604 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
7605 struct check_rebase_ok_arg ok_arg;
7607 *fileindex = NULL;
7609 err = lock_worktree(worktree, LOCK_EX);
7610 if (err)
7611 return err;
7613 err = open_fileindex(fileindex, &fileindex_path, worktree);
7614 if (err)
7615 goto done;
7617 /* Preconditions are the same as for rebase. */
7618 ok_arg.worktree = worktree;
7619 ok_arg.repo = repo;
7620 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7621 &ok_arg);
7622 if (err)
7623 goto done;
7625 err = get_merge_branch_ref_name(&branch_refname, worktree);
7626 if (err)
7627 return err;
7629 err = get_merge_commit_ref_name(&commit_refname, worktree);
7630 if (err)
7631 return err;
7633 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7634 0);
7635 if (err)
7636 goto done;
7638 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
7639 if (err)
7640 goto done;
7642 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
7643 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
7644 goto done;
7647 err = got_ref_resolve(&branch_tip, repo, branch);
7648 if (err)
7649 goto done;
7651 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
7652 if (err)
7653 goto done;
7654 err = got_ref_write(branch_ref, repo);
7655 if (err)
7656 goto done;
7658 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
7659 if (err)
7660 goto done;
7661 err = got_ref_write(commit_ref, repo);
7662 if (err)
7663 goto done;
7665 done:
7666 free(branch_refname);
7667 free(commit_refname);
7668 free(fileindex_path);
7669 if (branch_ref)
7670 got_ref_close(branch_ref);
7671 if (commit_ref)
7672 got_ref_close(commit_ref);
7673 if (wt_branch)
7674 got_ref_close(wt_branch);
7675 free(wt_branch_tip);
7676 if (err) {
7677 if (*fileindex) {
7678 got_fileindex_free(*fileindex);
7679 *fileindex = NULL;
7681 lock_worktree(worktree, LOCK_SH);
7683 return err;
7686 const struct got_error *
7687 got_worktree_merge_continue(char **branch_name,
7688 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
7689 struct got_worktree *worktree, struct got_repository *repo)
7691 const struct got_error *err;
7692 char *commit_refname = NULL, *branch_refname = NULL;
7693 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
7694 char *fileindex_path = NULL;
7695 int have_staged_files = 0;
7697 *branch_name = NULL;
7698 *branch_tip = NULL;
7699 *fileindex = NULL;
7701 err = lock_worktree(worktree, LOCK_EX);
7702 if (err)
7703 return err;
7705 err = open_fileindex(fileindex, &fileindex_path, worktree);
7706 if (err)
7707 goto done;
7709 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7710 &have_staged_files);
7711 if (err && err->code != GOT_ERR_CANCELLED)
7712 goto done;
7713 if (have_staged_files) {
7714 err = got_error(GOT_ERR_STAGED_PATHS);
7715 goto done;
7718 err = get_merge_branch_ref_name(&branch_refname, worktree);
7719 if (err)
7720 goto done;
7722 err = get_merge_commit_ref_name(&commit_refname, worktree);
7723 if (err)
7724 goto done;
7726 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7727 if (err)
7728 goto done;
7730 if (!got_ref_is_symbolic(branch_ref)) {
7731 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7732 "%s is not a symbolic reference",
7733 got_ref_get_name(branch_ref));
7734 goto done;
7736 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
7737 if (*branch_name == NULL) {
7738 err = got_error_from_errno("strdup");
7739 goto done;
7742 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
7743 if (err)
7744 goto done;
7746 err = got_ref_resolve(branch_tip, repo, commit_ref);
7747 if (err)
7748 goto done;
7749 done:
7750 free(commit_refname);
7751 free(branch_refname);
7752 free(fileindex_path);
7753 if (commit_ref)
7754 got_ref_close(commit_ref);
7755 if (branch_ref)
7756 got_ref_close(branch_ref);
7757 if (err) {
7758 if (*branch_name) {
7759 free(*branch_name);
7760 *branch_name = NULL;
7762 free(*branch_tip);
7763 *branch_tip = NULL;
7764 if (*fileindex) {
7765 got_fileindex_free(*fileindex);
7766 *fileindex = NULL;
7768 lock_worktree(worktree, LOCK_SH);
7770 return err;
7773 const struct got_error *
7774 got_worktree_merge_abort(struct got_worktree *worktree,
7775 struct got_fileindex *fileindex, struct got_repository *repo,
7776 got_worktree_checkout_cb progress_cb, void *progress_arg)
7778 const struct got_error *err, *unlockerr, *sync_err;
7779 struct got_object_id *commit_id = NULL;
7780 char *fileindex_path = NULL;
7781 struct revert_file_args rfa;
7782 struct got_object_id *tree_id = NULL;
7784 err = got_object_id_by_path(&tree_id, repo,
7785 worktree->base_commit_id, worktree->path_prefix);
7786 if (err)
7787 goto done;
7789 err = delete_merge_refs(worktree, repo);
7790 if (err)
7791 goto done;
7793 err = get_fileindex_path(&fileindex_path, worktree);
7794 if (err)
7795 goto done;
7797 rfa.worktree = worktree;
7798 rfa.fileindex = fileindex;
7799 rfa.progress_cb = progress_cb;
7800 rfa.progress_arg = progress_arg;
7801 rfa.patch_cb = NULL;
7802 rfa.patch_arg = NULL;
7803 rfa.repo = repo;
7804 rfa.unlink_added_files = 1;
7805 err = worktree_status(worktree, "", fileindex, repo,
7806 revert_file, &rfa, NULL, NULL, 1, 0);
7807 if (err)
7808 goto sync;
7810 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7811 repo, progress_cb, progress_arg, NULL, NULL);
7812 sync:
7813 sync_err = sync_fileindex(fileindex, fileindex_path);
7814 if (sync_err && err == NULL)
7815 err = sync_err;
7816 done:
7817 free(tree_id);
7818 free(commit_id);
7819 if (fileindex)
7820 got_fileindex_free(fileindex);
7821 free(fileindex_path);
7823 unlockerr = lock_worktree(worktree, LOCK_SH);
7824 if (unlockerr && err == NULL)
7825 err = unlockerr;
7826 return err;
7829 struct check_stage_ok_arg {
7830 struct got_object_id *head_commit_id;
7831 struct got_worktree *worktree;
7832 struct got_fileindex *fileindex;
7833 struct got_repository *repo;
7834 int have_changes;
7837 const struct got_error *
7838 check_stage_ok(void *arg, unsigned char status,
7839 unsigned char staged_status, const char *relpath,
7840 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7841 struct got_object_id *commit_id, int dirfd, const char *de_name)
7843 struct check_stage_ok_arg *a = arg;
7844 const struct got_error *err = NULL;
7845 struct got_fileindex_entry *ie;
7846 struct got_object_id base_commit_id;
7847 struct got_object_id *base_commit_idp = NULL;
7848 char *in_repo_path = NULL, *p;
7850 if (status == GOT_STATUS_UNVERSIONED ||
7851 status == GOT_STATUS_NO_CHANGE)
7852 return NULL;
7853 if (status == GOT_STATUS_NONEXISTENT)
7854 return got_error_set_errno(ENOENT, relpath);
7856 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7857 if (ie == NULL)
7858 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7860 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7861 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7862 relpath) == -1)
7863 return got_error_from_errno("asprintf");
7865 if (got_fileindex_entry_has_commit(ie)) {
7866 memcpy(base_commit_id.sha1, ie->commit_sha1,
7867 SHA1_DIGEST_LENGTH);
7868 base_commit_idp = &base_commit_id;
7871 if (status == GOT_STATUS_CONFLICT) {
7872 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7873 goto done;
7874 } else if (status != GOT_STATUS_ADD &&
7875 status != GOT_STATUS_MODIFY &&
7876 status != GOT_STATUS_DELETE) {
7877 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7878 goto done;
7881 a->have_changes = 1;
7883 p = in_repo_path;
7884 while (p[0] == '/')
7885 p++;
7886 err = check_out_of_date(p, status, staged_status,
7887 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7888 GOT_ERR_STAGE_OUT_OF_DATE);
7889 done:
7890 free(in_repo_path);
7891 return err;
7894 struct stage_path_arg {
7895 struct got_worktree *worktree;
7896 struct got_fileindex *fileindex;
7897 struct got_repository *repo;
7898 got_worktree_status_cb status_cb;
7899 void *status_arg;
7900 got_worktree_patch_cb patch_cb;
7901 void *patch_arg;
7902 int staged_something;
7903 int allow_bad_symlinks;
7906 static const struct got_error *
7907 stage_path(void *arg, unsigned char status,
7908 unsigned char staged_status, const char *relpath,
7909 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7910 struct got_object_id *commit_id, int dirfd, const char *de_name)
7912 struct stage_path_arg *a = arg;
7913 const struct got_error *err = NULL;
7914 struct got_fileindex_entry *ie;
7915 char *ondisk_path = NULL, *path_content = NULL;
7916 uint32_t stage;
7917 struct got_object_id *new_staged_blob_id = NULL;
7918 struct stat sb;
7920 if (status == GOT_STATUS_UNVERSIONED)
7921 return NULL;
7923 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7924 if (ie == NULL)
7925 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7927 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7928 relpath)== -1)
7929 return got_error_from_errno("asprintf");
7931 switch (status) {
7932 case GOT_STATUS_ADD:
7933 case GOT_STATUS_MODIFY:
7934 /* XXX could sb.st_mode be passed in by our caller? */
7935 if (lstat(ondisk_path, &sb) == -1) {
7936 err = got_error_from_errno2("lstat", ondisk_path);
7937 break;
7939 if (a->patch_cb) {
7940 if (status == GOT_STATUS_ADD) {
7941 int choice = GOT_PATCH_CHOICE_NONE;
7942 err = (*a->patch_cb)(&choice, a->patch_arg,
7943 status, ie->path, NULL, 1, 1);
7944 if (err)
7945 break;
7946 if (choice != GOT_PATCH_CHOICE_YES)
7947 break;
7948 } else {
7949 err = create_patched_content(&path_content, 0,
7950 staged_blob_id ? staged_blob_id : blob_id,
7951 ondisk_path, dirfd, de_name, ie->path,
7952 a->repo, a->patch_cb, a->patch_arg);
7953 if (err || path_content == NULL)
7954 break;
7957 err = got_object_blob_create(&new_staged_blob_id,
7958 path_content ? path_content : ondisk_path, a->repo);
7959 if (err)
7960 break;
7961 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7962 SHA1_DIGEST_LENGTH);
7963 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7964 stage = GOT_FILEIDX_STAGE_ADD;
7965 else
7966 stage = GOT_FILEIDX_STAGE_MODIFY;
7967 got_fileindex_entry_stage_set(ie, stage);
7968 if (S_ISLNK(sb.st_mode)) {
7969 int is_bad_symlink = 0;
7970 if (!a->allow_bad_symlinks) {
7971 char target_path[PATH_MAX];
7972 ssize_t target_len;
7973 target_len = readlink(ondisk_path, target_path,
7974 sizeof(target_path));
7975 if (target_len == -1) {
7976 err = got_error_from_errno2("readlink",
7977 ondisk_path);
7978 break;
7980 err = is_bad_symlink_target(&is_bad_symlink,
7981 target_path, target_len, ondisk_path,
7982 a->worktree->root_path);
7983 if (err)
7984 break;
7985 if (is_bad_symlink) {
7986 err = got_error_path(ondisk_path,
7987 GOT_ERR_BAD_SYMLINK);
7988 break;
7991 if (is_bad_symlink)
7992 got_fileindex_entry_staged_filetype_set(ie,
7993 GOT_FILEIDX_MODE_BAD_SYMLINK);
7994 else
7995 got_fileindex_entry_staged_filetype_set(ie,
7996 GOT_FILEIDX_MODE_SYMLINK);
7997 } else {
7998 got_fileindex_entry_staged_filetype_set(ie,
7999 GOT_FILEIDX_MODE_REGULAR_FILE);
8001 a->staged_something = 1;
8002 if (a->status_cb == NULL)
8003 break;
8004 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8005 get_staged_status(ie), relpath, blob_id,
8006 new_staged_blob_id, NULL, dirfd, de_name);
8007 break;
8008 case GOT_STATUS_DELETE:
8009 if (staged_status == GOT_STATUS_DELETE)
8010 break;
8011 if (a->patch_cb) {
8012 int choice = GOT_PATCH_CHOICE_NONE;
8013 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8014 ie->path, NULL, 1, 1);
8015 if (err)
8016 break;
8017 if (choice == GOT_PATCH_CHOICE_NO)
8018 break;
8019 if (choice != GOT_PATCH_CHOICE_YES) {
8020 err = got_error(GOT_ERR_PATCH_CHOICE);
8021 break;
8024 stage = GOT_FILEIDX_STAGE_DELETE;
8025 got_fileindex_entry_stage_set(ie, stage);
8026 a->staged_something = 1;
8027 if (a->status_cb == NULL)
8028 break;
8029 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8030 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8031 de_name);
8032 break;
8033 case GOT_STATUS_NO_CHANGE:
8034 break;
8035 case GOT_STATUS_CONFLICT:
8036 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8037 break;
8038 case GOT_STATUS_NONEXISTENT:
8039 err = got_error_set_errno(ENOENT, relpath);
8040 break;
8041 default:
8042 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8043 break;
8046 if (path_content && unlink(path_content) == -1 && err == NULL)
8047 err = got_error_from_errno2("unlink", path_content);
8048 free(path_content);
8049 free(ondisk_path);
8050 free(new_staged_blob_id);
8051 return err;
8054 const struct got_error *
8055 got_worktree_stage(struct got_worktree *worktree,
8056 struct got_pathlist_head *paths,
8057 got_worktree_status_cb status_cb, void *status_arg,
8058 got_worktree_patch_cb patch_cb, void *patch_arg,
8059 int allow_bad_symlinks, struct got_repository *repo)
8061 const struct got_error *err = NULL, *sync_err, *unlockerr;
8062 struct got_pathlist_entry *pe;
8063 struct got_fileindex *fileindex = NULL;
8064 char *fileindex_path = NULL;
8065 struct got_reference *head_ref = NULL;
8066 struct got_object_id *head_commit_id = NULL;
8067 struct check_stage_ok_arg oka;
8068 struct stage_path_arg spa;
8070 err = lock_worktree(worktree, LOCK_EX);
8071 if (err)
8072 return err;
8074 err = got_ref_open(&head_ref, repo,
8075 got_worktree_get_head_ref_name(worktree), 0);
8076 if (err)
8077 goto done;
8078 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8079 if (err)
8080 goto done;
8081 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8082 if (err)
8083 goto done;
8085 /* Check pre-conditions before staging anything. */
8086 oka.head_commit_id = head_commit_id;
8087 oka.worktree = worktree;
8088 oka.fileindex = fileindex;
8089 oka.repo = repo;
8090 oka.have_changes = 0;
8091 TAILQ_FOREACH(pe, paths, entry) {
8092 err = worktree_status(worktree, pe->path, fileindex, repo,
8093 check_stage_ok, &oka, NULL, NULL, 1, 0);
8094 if (err)
8095 goto done;
8097 if (!oka.have_changes) {
8098 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8099 goto done;
8102 spa.worktree = worktree;
8103 spa.fileindex = fileindex;
8104 spa.repo = repo;
8105 spa.patch_cb = patch_cb;
8106 spa.patch_arg = patch_arg;
8107 spa.status_cb = status_cb;
8108 spa.status_arg = status_arg;
8109 spa.staged_something = 0;
8110 spa.allow_bad_symlinks = allow_bad_symlinks;
8111 TAILQ_FOREACH(pe, paths, entry) {
8112 err = worktree_status(worktree, pe->path, fileindex, repo,
8113 stage_path, &spa, NULL, NULL, 1, 0);
8114 if (err)
8115 goto done;
8117 if (!spa.staged_something) {
8118 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8119 goto done;
8122 sync_err = sync_fileindex(fileindex, fileindex_path);
8123 if (sync_err && err == NULL)
8124 err = sync_err;
8125 done:
8126 if (head_ref)
8127 got_ref_close(head_ref);
8128 free(head_commit_id);
8129 free(fileindex_path);
8130 if (fileindex)
8131 got_fileindex_free(fileindex);
8132 unlockerr = lock_worktree(worktree, LOCK_SH);
8133 if (unlockerr && err == NULL)
8134 err = unlockerr;
8135 return err;
8138 struct unstage_path_arg {
8139 struct got_worktree *worktree;
8140 struct got_fileindex *fileindex;
8141 struct got_repository *repo;
8142 got_worktree_checkout_cb progress_cb;
8143 void *progress_arg;
8144 got_worktree_patch_cb patch_cb;
8145 void *patch_arg;
8148 static const struct got_error *
8149 create_unstaged_content(char **path_unstaged_content,
8150 char **path_new_staged_content, struct got_object_id *blob_id,
8151 struct got_object_id *staged_blob_id, const char *relpath,
8152 struct got_repository *repo,
8153 got_worktree_patch_cb patch_cb, void *patch_arg)
8155 const struct got_error *err, *free_err;
8156 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8157 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8158 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8159 struct got_diffreg_result *diffreg_result = NULL;
8160 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8161 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8163 *path_unstaged_content = NULL;
8164 *path_new_staged_content = NULL;
8166 err = got_object_id_str(&label1, blob_id);
8167 if (err)
8168 return err;
8169 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
8170 if (err)
8171 goto done;
8173 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
8174 if (err)
8175 goto done;
8177 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8178 if (err)
8179 goto done;
8181 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
8182 if (err)
8183 goto done;
8185 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
8186 if (err)
8187 goto done;
8189 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8190 if (err)
8191 goto done;
8193 err = got_diff_files(&diffreg_result, f1, label1, f2,
8194 path2, 3, 0, 1, NULL);
8195 if (err)
8196 goto done;
8198 err = got_opentemp_named(path_unstaged_content, &outfile,
8199 "got-unstaged-content");
8200 if (err)
8201 goto done;
8202 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8203 "got-new-staged-content");
8204 if (err)
8205 goto done;
8207 if (fseek(f1, 0L, SEEK_SET) == -1) {
8208 err = got_ferror(f1, GOT_ERR_IO);
8209 goto done;
8211 if (fseek(f2, 0L, SEEK_SET) == -1) {
8212 err = got_ferror(f2, GOT_ERR_IO);
8213 goto done;
8215 /* Count the number of actual changes in the diff result. */
8216 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8217 struct diff_chunk_context cc = {};
8218 diff_chunk_context_load_change(&cc, &nchunks_used,
8219 diffreg_result->result, n, 0);
8220 nchanges++;
8222 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8223 int choice;
8224 err = apply_or_reject_change(&choice, &nchunks_used,
8225 diffreg_result->result, n, relpath, f1, f2,
8226 &line_cur1, &line_cur2,
8227 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8228 if (err)
8229 goto done;
8230 if (choice == GOT_PATCH_CHOICE_YES)
8231 have_content = 1;
8232 else
8233 have_rejected_content = 1;
8234 if (choice == GOT_PATCH_CHOICE_QUIT)
8235 break;
8237 if (have_content || have_rejected_content)
8238 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8239 outfile, rejectfile);
8240 done:
8241 free(label1);
8242 if (blob)
8243 got_object_blob_close(blob);
8244 if (staged_blob)
8245 got_object_blob_close(staged_blob);
8246 free_err = got_diffreg_result_free(diffreg_result);
8247 if (free_err && err == NULL)
8248 err = free_err;
8249 if (f1 && fclose(f1) == EOF && err == NULL)
8250 err = got_error_from_errno2("fclose", path1);
8251 if (f2 && fclose(f2) == EOF && err == NULL)
8252 err = got_error_from_errno2("fclose", path2);
8253 if (outfile && fclose(outfile) == EOF && err == NULL)
8254 err = got_error_from_errno2("fclose", *path_unstaged_content);
8255 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8256 err = got_error_from_errno2("fclose", *path_new_staged_content);
8257 if (path1 && unlink(path1) == -1 && err == NULL)
8258 err = got_error_from_errno2("unlink", path1);
8259 if (path2 && unlink(path2) == -1 && err == NULL)
8260 err = got_error_from_errno2("unlink", path2);
8261 if (err || !have_content) {
8262 if (*path_unstaged_content &&
8263 unlink(*path_unstaged_content) == -1 && err == NULL)
8264 err = got_error_from_errno2("unlink",
8265 *path_unstaged_content);
8266 free(*path_unstaged_content);
8267 *path_unstaged_content = NULL;
8269 if (err || !have_content || !have_rejected_content) {
8270 if (*path_new_staged_content &&
8271 unlink(*path_new_staged_content) == -1 && err == NULL)
8272 err = got_error_from_errno2("unlink",
8273 *path_new_staged_content);
8274 free(*path_new_staged_content);
8275 *path_new_staged_content = NULL;
8277 free(path1);
8278 free(path2);
8279 return err;
8282 static const struct got_error *
8283 unstage_hunks(struct got_object_id *staged_blob_id,
8284 struct got_blob_object *blob_base,
8285 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8286 const char *ondisk_path, const char *label_orig,
8287 struct got_worktree *worktree, struct got_repository *repo,
8288 got_worktree_patch_cb patch_cb, void *patch_arg,
8289 got_worktree_checkout_cb progress_cb, void *progress_arg)
8291 const struct got_error *err = NULL;
8292 char *path_unstaged_content = NULL;
8293 char *path_new_staged_content = NULL;
8294 char *parent = NULL, *base_path = NULL;
8295 char *blob_base_path = NULL;
8296 struct got_object_id *new_staged_blob_id = NULL;
8297 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8298 struct stat sb;
8300 err = create_unstaged_content(&path_unstaged_content,
8301 &path_new_staged_content, blob_id, staged_blob_id,
8302 ie->path, repo, patch_cb, patch_arg);
8303 if (err)
8304 return err;
8306 if (path_unstaged_content == NULL)
8307 return NULL;
8309 if (path_new_staged_content) {
8310 err = got_object_blob_create(&new_staged_blob_id,
8311 path_new_staged_content, repo);
8312 if (err)
8313 goto done;
8316 f = fopen(path_unstaged_content, "re");
8317 if (f == NULL) {
8318 err = got_error_from_errno2("fopen",
8319 path_unstaged_content);
8320 goto done;
8322 if (fstat(fileno(f), &sb) == -1) {
8323 err = got_error_from_errno2("fstat", path_unstaged_content);
8324 goto done;
8326 if (got_fileindex_entry_staged_filetype_get(ie) ==
8327 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8328 char link_target[PATH_MAX];
8329 size_t r;
8330 r = fread(link_target, 1, sizeof(link_target), f);
8331 if (r == 0 && ferror(f)) {
8332 err = got_error_from_errno("fread");
8333 goto done;
8335 if (r >= sizeof(link_target)) { /* should not happen */
8336 err = got_error(GOT_ERR_NO_SPACE);
8337 goto done;
8339 link_target[r] = '\0';
8340 err = merge_symlink(worktree, blob_base,
8341 ondisk_path, ie->path, label_orig, link_target,
8342 worktree->base_commit_id, repo, progress_cb,
8343 progress_arg);
8344 } else {
8345 int local_changes_subsumed;
8347 err = got_path_dirname(&parent, ondisk_path);
8348 if (err)
8349 return err;
8351 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8352 parent) == -1) {
8353 err = got_error_from_errno("asprintf");
8354 base_path = NULL;
8355 goto done;
8358 err = got_opentemp_named(&blob_base_path, &f_base, base_path);
8359 if (err)
8360 goto done;
8361 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8362 blob_base);
8363 if (err)
8364 goto done;
8367 * In order the run a 3-way merge with a symlink we copy the symlink's
8368 * target path into a temporary file and use that file with diff3.
8370 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8371 err = dump_symlink_target_path_to_file(&f_deriv2,
8372 ondisk_path);
8373 if (err)
8374 goto done;
8375 } else {
8376 int fd;
8377 fd = open(ondisk_path,
8378 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8379 if (fd == -1) {
8380 err = got_error_from_errno2("open", ondisk_path);
8381 goto done;
8383 f_deriv2 = fdopen(fd, "r");
8384 if (f_deriv2 == NULL) {
8385 err = got_error_from_errno2("fdopen", ondisk_path);
8386 close(fd);
8387 goto done;
8391 err = merge_file(&local_changes_subsumed, worktree,
8392 f_base, f, f_deriv2, ondisk_path, ie->path,
8393 got_fileindex_perms_to_st(ie),
8394 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8395 repo, progress_cb, progress_arg);
8397 if (err)
8398 goto done;
8400 if (new_staged_blob_id) {
8401 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8402 SHA1_DIGEST_LENGTH);
8403 } else {
8404 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8405 got_fileindex_entry_staged_filetype_set(ie, 0);
8407 done:
8408 free(new_staged_blob_id);
8409 if (path_unstaged_content &&
8410 unlink(path_unstaged_content) == -1 && err == NULL)
8411 err = got_error_from_errno2("unlink", path_unstaged_content);
8412 if (path_new_staged_content &&
8413 unlink(path_new_staged_content) == -1 && err == NULL)
8414 err = got_error_from_errno2("unlink", path_new_staged_content);
8415 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8416 err = got_error_from_errno2("unlink", blob_base_path);
8417 if (f_base && fclose(f_base) == EOF && err == NULL)
8418 err = got_error_from_errno2("fclose", path_unstaged_content);
8419 if (f && fclose(f) == EOF && err == NULL)
8420 err = got_error_from_errno2("fclose", path_unstaged_content);
8421 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8422 err = got_error_from_errno2("fclose", ondisk_path);
8423 free(path_unstaged_content);
8424 free(path_new_staged_content);
8425 free(blob_base_path);
8426 free(parent);
8427 free(base_path);
8428 return err;
8431 static const struct got_error *
8432 unstage_path(void *arg, unsigned char status,
8433 unsigned char staged_status, const char *relpath,
8434 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8435 struct got_object_id *commit_id, int dirfd, const char *de_name)
8437 const struct got_error *err = NULL;
8438 struct unstage_path_arg *a = arg;
8439 struct got_fileindex_entry *ie;
8440 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8441 char *ondisk_path = NULL;
8442 char *id_str = NULL, *label_orig = NULL;
8443 int local_changes_subsumed;
8444 struct stat sb;
8446 if (staged_status != GOT_STATUS_ADD &&
8447 staged_status != GOT_STATUS_MODIFY &&
8448 staged_status != GOT_STATUS_DELETE)
8449 return NULL;
8451 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8452 if (ie == NULL)
8453 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8455 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8456 == -1)
8457 return got_error_from_errno("asprintf");
8459 err = got_object_id_str(&id_str,
8460 commit_id ? commit_id : a->worktree->base_commit_id);
8461 if (err)
8462 goto done;
8463 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8464 id_str) == -1) {
8465 err = got_error_from_errno("asprintf");
8466 goto done;
8469 switch (staged_status) {
8470 case GOT_STATUS_MODIFY:
8471 err = got_object_open_as_blob(&blob_base, a->repo,
8472 blob_id, 8192);
8473 if (err)
8474 break;
8475 /* fall through */
8476 case GOT_STATUS_ADD:
8477 if (a->patch_cb) {
8478 if (staged_status == GOT_STATUS_ADD) {
8479 int choice = GOT_PATCH_CHOICE_NONE;
8480 err = (*a->patch_cb)(&choice, a->patch_arg,
8481 staged_status, ie->path, NULL, 1, 1);
8482 if (err)
8483 break;
8484 if (choice != GOT_PATCH_CHOICE_YES)
8485 break;
8486 } else {
8487 err = unstage_hunks(staged_blob_id,
8488 blob_base, blob_id, ie, ondisk_path,
8489 label_orig, a->worktree, a->repo,
8490 a->patch_cb, a->patch_arg,
8491 a->progress_cb, a->progress_arg);
8492 break; /* Done with this file. */
8495 err = got_object_open_as_blob(&blob_staged, a->repo,
8496 staged_blob_id, 8192);
8497 if (err)
8498 break;
8499 switch (got_fileindex_entry_staged_filetype_get(ie)) {
8500 case GOT_FILEIDX_MODE_BAD_SYMLINK:
8501 case GOT_FILEIDX_MODE_REGULAR_FILE:
8502 err = merge_blob(&local_changes_subsumed, a->worktree,
8503 blob_base, ondisk_path, relpath,
8504 got_fileindex_perms_to_st(ie), label_orig,
8505 blob_staged, commit_id ? commit_id :
8506 a->worktree->base_commit_id, a->repo,
8507 a->progress_cb, a->progress_arg);
8508 break;
8509 case GOT_FILEIDX_MODE_SYMLINK:
8510 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8511 char *staged_target;
8512 err = got_object_blob_read_to_str(
8513 &staged_target, blob_staged);
8514 if (err)
8515 goto done;
8516 err = merge_symlink(a->worktree, blob_base,
8517 ondisk_path, relpath, label_orig,
8518 staged_target, commit_id ? commit_id :
8519 a->worktree->base_commit_id,
8520 a->repo, a->progress_cb, a->progress_arg);
8521 free(staged_target);
8522 } else {
8523 err = merge_blob(&local_changes_subsumed,
8524 a->worktree, blob_base, ondisk_path,
8525 relpath, got_fileindex_perms_to_st(ie),
8526 label_orig, blob_staged,
8527 commit_id ? commit_id :
8528 a->worktree->base_commit_id, a->repo,
8529 a->progress_cb, a->progress_arg);
8531 break;
8532 default:
8533 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8534 break;
8536 if (err == NULL) {
8537 got_fileindex_entry_stage_set(ie,
8538 GOT_FILEIDX_STAGE_NONE);
8539 got_fileindex_entry_staged_filetype_set(ie, 0);
8541 break;
8542 case GOT_STATUS_DELETE:
8543 if (a->patch_cb) {
8544 int choice = GOT_PATCH_CHOICE_NONE;
8545 err = (*a->patch_cb)(&choice, a->patch_arg,
8546 staged_status, ie->path, NULL, 1, 1);
8547 if (err)
8548 break;
8549 if (choice == GOT_PATCH_CHOICE_NO)
8550 break;
8551 if (choice != GOT_PATCH_CHOICE_YES) {
8552 err = got_error(GOT_ERR_PATCH_CHOICE);
8553 break;
8556 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8557 got_fileindex_entry_staged_filetype_set(ie, 0);
8558 err = get_file_status(&status, &sb, ie, ondisk_path,
8559 dirfd, de_name, a->repo);
8560 if (err)
8561 break;
8562 err = (*a->progress_cb)(a->progress_arg, status, relpath);
8563 break;
8565 done:
8566 free(ondisk_path);
8567 if (blob_base)
8568 got_object_blob_close(blob_base);
8569 if (blob_staged)
8570 got_object_blob_close(blob_staged);
8571 free(id_str);
8572 free(label_orig);
8573 return err;
8576 const struct got_error *
8577 got_worktree_unstage(struct got_worktree *worktree,
8578 struct got_pathlist_head *paths,
8579 got_worktree_checkout_cb progress_cb, void *progress_arg,
8580 got_worktree_patch_cb patch_cb, void *patch_arg,
8581 struct got_repository *repo)
8583 const struct got_error *err = NULL, *sync_err, *unlockerr;
8584 struct got_pathlist_entry *pe;
8585 struct got_fileindex *fileindex = NULL;
8586 char *fileindex_path = NULL;
8587 struct unstage_path_arg upa;
8589 err = lock_worktree(worktree, LOCK_EX);
8590 if (err)
8591 return err;
8593 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8594 if (err)
8595 goto done;
8597 upa.worktree = worktree;
8598 upa.fileindex = fileindex;
8599 upa.repo = repo;
8600 upa.progress_cb = progress_cb;
8601 upa.progress_arg = progress_arg;
8602 upa.patch_cb = patch_cb;
8603 upa.patch_arg = patch_arg;
8604 TAILQ_FOREACH(pe, paths, entry) {
8605 err = worktree_status(worktree, pe->path, fileindex, repo,
8606 unstage_path, &upa, NULL, NULL, 1, 0);
8607 if (err)
8608 goto done;
8611 sync_err = sync_fileindex(fileindex, fileindex_path);
8612 if (sync_err && err == NULL)
8613 err = sync_err;
8614 done:
8615 free(fileindex_path);
8616 if (fileindex)
8617 got_fileindex_free(fileindex);
8618 unlockerr = lock_worktree(worktree, LOCK_SH);
8619 if (unlockerr && err == NULL)
8620 err = unlockerr;
8621 return err;
8624 struct report_file_info_arg {
8625 struct got_worktree *worktree;
8626 got_worktree_path_info_cb info_cb;
8627 void *info_arg;
8628 struct got_pathlist_head *paths;
8629 got_cancel_cb cancel_cb;
8630 void *cancel_arg;
8633 static const struct got_error *
8634 report_file_info(void *arg, struct got_fileindex_entry *ie)
8636 struct report_file_info_arg *a = arg;
8637 struct got_pathlist_entry *pe;
8638 struct got_object_id blob_id, staged_blob_id, commit_id;
8639 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8640 struct got_object_id *commit_idp = NULL;
8641 int stage;
8643 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8644 return got_error(GOT_ERR_CANCELLED);
8646 TAILQ_FOREACH(pe, a->paths, entry) {
8647 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8648 got_path_is_child(ie->path, pe->path, pe->path_len))
8649 break;
8651 if (pe == NULL) /* not found */
8652 return NULL;
8654 if (got_fileindex_entry_has_blob(ie)) {
8655 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8656 blob_idp = &blob_id;
8658 stage = got_fileindex_entry_stage_get(ie);
8659 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8660 stage == GOT_FILEIDX_STAGE_ADD) {
8661 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8662 SHA1_DIGEST_LENGTH);
8663 staged_blob_idp = &staged_blob_id;
8666 if (got_fileindex_entry_has_commit(ie)) {
8667 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8668 commit_idp = &commit_id;
8671 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8672 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8675 const struct got_error *
8676 got_worktree_path_info(struct got_worktree *worktree,
8677 struct got_pathlist_head *paths,
8678 got_worktree_path_info_cb info_cb, void *info_arg,
8679 got_cancel_cb cancel_cb, void *cancel_arg)
8682 const struct got_error *err = NULL, *unlockerr;
8683 struct got_fileindex *fileindex = NULL;
8684 char *fileindex_path = NULL;
8685 struct report_file_info_arg arg;
8687 err = lock_worktree(worktree, LOCK_SH);
8688 if (err)
8689 return err;
8691 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8692 if (err)
8693 goto done;
8695 arg.worktree = worktree;
8696 arg.info_cb = info_cb;
8697 arg.info_arg = info_arg;
8698 arg.paths = paths;
8699 arg.cancel_cb = cancel_cb;
8700 arg.cancel_arg = cancel_arg;
8701 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8702 &arg);
8703 done:
8704 free(fileindex_path);
8705 if (fileindex)
8706 got_fileindex_free(fileindex);
8707 unlockerr = lock_worktree(worktree, LOCK_UN);
8708 if (unlockerr && err == NULL)
8709 err = unlockerr;
8710 return err;