Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/stat.h>
19 #include <dirent.h>
20 #include <limits.h>
21 #include <stddef.h>
22 #include <string.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <time.h>
26 #include <fcntl.h>
27 #include <errno.h>
28 #include <unistd.h>
29 #include <zlib.h>
30 #include <fnmatch.h>
31 #include <libgen.h>
33 #include "got_compat.h"
35 #include "got_error.h"
36 #include "got_repository.h"
37 #include "got_reference.h"
38 #include "got_object.h"
39 #include "got_path.h"
40 #include "got_cancel.h"
41 #include "got_worktree.h"
42 #include "got_opentemp.h"
43 #include "got_diff.h"
45 #include "got_lib_worktree.h"
46 #include "got_lib_sha1.h"
47 #include "got_lib_fileindex.h"
48 #include "got_lib_inflate.h"
49 #include "got_lib_delta.h"
50 #include "got_lib_object.h"
51 #include "got_lib_object_parse.h"
52 #include "got_lib_object_create.h"
53 #include "got_lib_object_idset.h"
54 #include "got_lib_diff.h"
55 #include "got_lib_gotconfig.h"
57 #ifndef MIN
58 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
59 #endif
61 #define GOT_MERGE_LABEL_MERGED "merged change"
62 #define GOT_MERGE_LABEL_BASE "3-way merge base"
64 static const struct got_error *
65 create_meta_file(const char *path_got, const char *name, const char *content)
66 {
67 const struct got_error *err = NULL;
68 char *path;
70 if (asprintf(&path, "%s/%s", path_got, name) == -1)
71 return got_error_from_errno("asprintf");
73 err = got_path_create_file(path, content);
74 free(path);
75 return err;
76 }
78 static const struct got_error *
79 update_meta_file(const char *path_got, const char *name, const char *content)
80 {
81 const struct got_error *err = NULL;
82 FILE *tmpfile = NULL;
83 char *tmppath = NULL;
84 char *path = NULL;
86 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
87 err = got_error_from_errno("asprintf");
88 path = NULL;
89 goto done;
90 }
92 err = got_opentemp_named(&tmppath, &tmpfile, path);
93 if (err)
94 goto done;
96 if (content) {
97 int len = fprintf(tmpfile, "%s\n", content);
98 if (len != strlen(content) + 1) {
99 err = got_error_from_errno2("fprintf", tmppath);
100 goto done;
104 if (rename(tmppath, path) != 0) {
105 err = got_error_from_errno3("rename", tmppath, path);
106 unlink(tmppath);
107 goto done;
110 done:
111 if (fclose(tmpfile) == EOF && err == NULL)
112 err = got_error_from_errno2("fclose", tmppath);
113 free(tmppath);
114 return err;
117 static const struct got_error *
118 write_head_ref(const char *path_got, struct got_reference *head_ref)
120 const struct got_error *err = NULL;
121 char *refstr = NULL;
123 if (got_ref_is_symbolic(head_ref)) {
124 refstr = got_ref_to_str(head_ref);
125 if (refstr == NULL)
126 return got_error_from_errno("got_ref_to_str");
127 } else {
128 refstr = strdup(got_ref_get_name(head_ref));
129 if (refstr == NULL)
130 return got_error_from_errno("strdup");
132 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
133 free(refstr);
134 return err;
137 const struct got_error *
138 got_worktree_init(const char *path, struct got_reference *head_ref,
139 const char *prefix, struct got_repository *repo)
141 const struct got_error *err = NULL;
142 struct got_object_id *commit_id = NULL;
143 uuid_t uuid;
144 uint32_t uuid_status;
145 int obj_type;
146 char *path_got = NULL;
147 char *formatstr = NULL;
148 char *absprefix = NULL;
149 char *basestr = NULL;
150 char *uuidstr = NULL;
152 if (strcmp(path, got_repo_get_path(repo)) == 0) {
153 err = got_error(GOT_ERR_WORKTREE_REPO);
154 goto done;
157 err = got_ref_resolve(&commit_id, repo, head_ref);
158 if (err)
159 return err;
160 err = got_object_get_type(&obj_type, repo, commit_id);
161 if (err)
162 return err;
163 if (obj_type != GOT_OBJ_TYPE_COMMIT)
164 return got_error(GOT_ERR_OBJ_TYPE);
166 if (!got_path_is_absolute(prefix)) {
167 if (asprintf(&absprefix, "/%s", prefix) == -1)
168 return got_error_from_errno("asprintf");
171 /* Create top-level directory (may already exist). */
172 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
173 err = got_error_from_errno2("mkdir", path);
174 goto done;
177 /* Create .got directory (may already exist). */
178 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
179 err = got_error_from_errno("asprintf");
180 goto done;
182 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
183 err = got_error_from_errno2("mkdir", path_got);
184 goto done;
187 /* Create an empty lock file. */
188 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
189 if (err)
190 goto done;
192 /* Create an empty file index. */
193 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
194 if (err)
195 goto done;
197 /* Write the HEAD reference. */
198 err = write_head_ref(path_got, head_ref);
199 if (err)
200 goto done;
202 /* Record our base commit. */
203 err = got_object_id_str(&basestr, commit_id);
204 if (err)
205 goto done;
206 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
207 if (err)
208 goto done;
210 /* Store path to repository. */
211 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
212 got_repo_get_path(repo));
213 if (err)
214 goto done;
216 /* Store in-repository path prefix. */
217 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
218 absprefix ? absprefix : prefix);
219 if (err)
220 goto done;
222 /* Generate UUID. */
223 uuid_create(&uuid, &uuid_status);
224 if (uuid_status != uuid_s_ok) {
225 err = got_error_uuid(uuid_status, "uuid_create");
226 goto done;
228 uuid_to_string(&uuid, &uuidstr, &uuid_status);
229 if (uuid_status != uuid_s_ok) {
230 err = got_error_uuid(uuid_status, "uuid_to_string");
231 goto done;
233 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
234 if (err)
235 goto done;
237 /* Stamp work tree with format file. */
238 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
239 err = got_error_from_errno("asprintf");
240 goto done;
242 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
243 if (err)
244 goto done;
246 done:
247 free(commit_id);
248 free(path_got);
249 free(formatstr);
250 free(absprefix);
251 free(basestr);
252 free(uuidstr);
253 return err;
256 const struct got_error *
257 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
258 const char *path_prefix)
260 char *absprefix = NULL;
262 if (!got_path_is_absolute(path_prefix)) {
263 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
264 return got_error_from_errno("asprintf");
266 *match = (strcmp(absprefix ? absprefix : path_prefix,
267 worktree->path_prefix) == 0);
268 free(absprefix);
269 return NULL;
272 const char *
273 got_worktree_get_head_ref_name(struct got_worktree *worktree)
275 return worktree->head_ref_name;
278 const struct got_error *
279 got_worktree_set_head_ref(struct got_worktree *worktree,
280 struct got_reference *head_ref)
282 const struct got_error *err = NULL;
283 char *path_got = NULL, *head_ref_name = NULL;
285 if (asprintf(&path_got, "%s/%s", worktree->root_path,
286 GOT_WORKTREE_GOT_DIR) == -1) {
287 err = got_error_from_errno("asprintf");
288 path_got = NULL;
289 goto done;
292 head_ref_name = strdup(got_ref_get_name(head_ref));
293 if (head_ref_name == NULL) {
294 err = got_error_from_errno("strdup");
295 goto done;
298 err = write_head_ref(path_got, head_ref);
299 if (err)
300 goto done;
302 free(worktree->head_ref_name);
303 worktree->head_ref_name = head_ref_name;
304 done:
305 free(path_got);
306 if (err)
307 free(head_ref_name);
308 return err;
311 struct got_object_id *
312 got_worktree_get_base_commit_id(struct got_worktree *worktree)
314 return worktree->base_commit_id;
317 const struct got_error *
318 got_worktree_set_base_commit_id(struct got_worktree *worktree,
319 struct got_repository *repo, struct got_object_id *commit_id)
321 const struct got_error *err;
322 struct got_object *obj = NULL;
323 char *id_str = NULL;
324 char *path_got = NULL;
326 if (asprintf(&path_got, "%s/%s", worktree->root_path,
327 GOT_WORKTREE_GOT_DIR) == -1) {
328 err = got_error_from_errno("asprintf");
329 path_got = NULL;
330 goto done;
333 err = got_object_open(&obj, repo, commit_id);
334 if (err)
335 return err;
337 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
338 err = got_error(GOT_ERR_OBJ_TYPE);
339 goto done;
342 /* Record our base commit. */
343 err = got_object_id_str(&id_str, commit_id);
344 if (err)
345 goto done;
346 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
347 if (err)
348 goto done;
350 free(worktree->base_commit_id);
351 worktree->base_commit_id = got_object_id_dup(commit_id);
352 if (worktree->base_commit_id == NULL) {
353 err = got_error_from_errno("got_object_id_dup");
354 goto done;
356 done:
357 if (obj)
358 got_object_close(obj);
359 free(id_str);
360 free(path_got);
361 return err;
364 const struct got_gotconfig *
365 got_worktree_get_gotconfig(struct got_worktree *worktree)
367 return worktree->gotconfig;
370 static const struct got_error *
371 lock_worktree(struct got_worktree *worktree, int operation)
373 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
374 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
375 : got_error_from_errno2("flock",
376 got_worktree_get_root_path(worktree)));
377 return NULL;
380 static const struct got_error *
381 add_dir_on_disk(struct got_worktree *worktree, const char *path)
383 const struct got_error *err = NULL;
384 char *abspath;
386 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
387 return got_error_from_errno("asprintf");
389 err = got_path_mkdir(abspath);
390 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
391 struct stat sb;
392 err = NULL;
393 if (lstat(abspath, &sb) == -1) {
394 err = got_error_from_errno2("lstat", abspath);
395 } else if (!S_ISDIR(sb.st_mode)) {
396 /* TODO directory is obstructed; do something */
397 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
400 free(abspath);
401 return err;
404 static const struct got_error *
405 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
407 const struct got_error *err = NULL;
408 uint8_t fbuf1[8192];
409 uint8_t fbuf2[8192];
410 size_t flen1 = 0, flen2 = 0;
412 *same = 1;
414 for (;;) {
415 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
416 if (flen1 == 0 && ferror(f1)) {
417 err = got_error_from_errno("fread");
418 break;
420 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
421 if (flen2 == 0 && ferror(f2)) {
422 err = got_error_from_errno("fread");
423 break;
425 if (flen1 == 0) {
426 if (flen2 != 0)
427 *same = 0;
428 break;
429 } else if (flen2 == 0) {
430 if (flen1 != 0)
431 *same = 0;
432 break;
433 } else if (flen1 == flen2) {
434 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
435 *same = 0;
436 break;
438 } else {
439 *same = 0;
440 break;
444 return err;
447 static const struct got_error *
448 check_files_equal(int *same, FILE *f1, FILE *f2)
450 struct stat sb;
451 size_t size1, size2;
453 *same = 1;
455 if (fstat(fileno(f1), &sb) != 0)
456 return got_error_from_errno("fstat");
457 size1 = sb.st_size;
459 if (fstat(fileno(f2), &sb) != 0)
460 return got_error_from_errno("fstat");
461 size2 = sb.st_size;
463 if (size1 != size2) {
464 *same = 0;
465 return NULL;
468 if (fseek(f1, 0L, SEEK_SET) == -1)
469 return got_ferror(f1, GOT_ERR_IO);
470 if (fseek(f2, 0L, SEEK_SET) == -1)
471 return got_ferror(f2, GOT_ERR_IO);
473 return check_file_contents_equal(same, f1, f2);
476 static const struct got_error *
477 copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
479 uint8_t fbuf[65536];
480 size_t flen;
481 ssize_t outlen;
483 *outsize = 0;
485 if (fseek(f, 0L, SEEK_SET) == -1)
486 return got_ferror(f, GOT_ERR_IO);
488 for (;;) {
489 flen = fread(fbuf, 1, sizeof(fbuf), f);
490 if (flen == 0) {
491 if (ferror(f))
492 return got_error_from_errno("fread");
493 if (feof(f))
494 break;
496 outlen = write(outfd, fbuf, flen);
497 if (outlen == -1)
498 return got_error_from_errno("write");
499 if (outlen != flen)
500 return got_error(GOT_ERR_IO);
501 *outsize += outlen;
504 return NULL;
507 static const struct got_error *
508 merge_binary_file(int *overlapcnt, int merged_fd,
509 FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
510 const char *label_deriv, const char *label_orig, const char *label_deriv2,
511 const char *ondisk_path)
513 const struct got_error *err = NULL;
514 int same_content, changed_deriv, changed_deriv2;
515 int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
516 off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
517 char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
518 char *base_path_orig = NULL, *base_path_deriv = NULL;
519 char *base_path_deriv2 = NULL;
521 *overlapcnt = 0;
523 err = check_files_equal(&same_content, f_deriv, f_deriv2);
524 if (err)
525 return err;
527 if (same_content)
528 return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
530 err = check_files_equal(&same_content, f_deriv, f_orig);
531 if (err)
532 return err;
533 changed_deriv = !same_content;
534 err = check_files_equal(&same_content, f_deriv2, f_orig);
535 if (err)
536 return err;
537 changed_deriv2 = !same_content;
539 if (changed_deriv && changed_deriv2) {
540 *overlapcnt = 1;
541 if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
542 err = got_error_from_errno("asprintf");
543 goto done;
545 if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
546 err = got_error_from_errno("asprintf");
547 goto done;
549 if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
550 err = got_error_from_errno("asprintf");
551 goto done;
553 err = got_opentemp_named_fd(&path_orig, &fd_orig,
554 base_path_orig);
555 if (err)
556 goto done;
557 err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
558 base_path_deriv);
559 if (err)
560 goto done;
561 err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
562 base_path_deriv2);
563 if (err)
564 goto done;
565 err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
566 if (err)
567 goto done;
568 err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
569 if (err)
570 goto done;
571 err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
572 if (err)
573 goto done;
574 if (dprintf(merged_fd, "Binary files differ and cannot be "
575 "merged automatically:\n") < 0) {
576 err = got_error_from_errno("dprintf");
577 goto done;
579 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
580 GOT_DIFF_CONFLICT_MARKER_BEGIN,
581 label_deriv ? " " : "",
582 label_deriv ? label_deriv : "",
583 path_deriv) < 0) {
584 err = got_error_from_errno("dprintf");
585 goto done;
587 if (size_orig > 0) {
588 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
589 GOT_DIFF_CONFLICT_MARKER_ORIG,
590 label_orig ? " " : "",
591 label_orig ? label_orig : "",
592 path_orig) < 0) {
593 err = got_error_from_errno("dprintf");
594 goto done;
597 if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
598 GOT_DIFF_CONFLICT_MARKER_SEP,
599 path_deriv2,
600 GOT_DIFF_CONFLICT_MARKER_END,
601 label_deriv2 ? " " : "",
602 label_deriv2 ? label_deriv2 : "") < 0) {
603 err = got_error_from_errno("dprintf");
604 goto done;
606 } else if (changed_deriv)
607 err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
608 else if (changed_deriv2)
609 err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
610 done:
611 if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
612 err == NULL)
613 err = got_error_from_errno2("unlink", path_orig);
614 if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
615 err = got_error_from_errno2("close", path_orig);
616 if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
617 err = got_error_from_errno2("close", path_deriv);
618 if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
619 err = got_error_from_errno2("close", path_deriv2);
620 free(path_orig);
621 free(path_deriv);
622 free(path_deriv2);
623 free(base_path_orig);
624 free(base_path_deriv);
625 free(base_path_deriv2);
626 return err;
629 /*
630 * Perform a 3-way merge where the file f_orig acts as the common
631 * ancestor, the file f_deriv acts as the first derived version,
632 * and the file f_deriv2 acts as the second derived version.
633 * The merge result will be written to a new file at ondisk_path; any
634 * existing file at this path will be replaced.
635 */
636 static const struct got_error *
637 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
638 FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
639 const char *path, uint16_t st_mode,
640 const char *label_orig, const char *label_deriv, const char *label_deriv2,
641 enum got_diff_algorithm diff_algo, struct got_repository *repo,
642 got_worktree_checkout_cb progress_cb, void *progress_arg)
644 const struct got_error *err = NULL;
645 int merged_fd = -1;
646 FILE *f_merged = NULL;
647 char *merged_path = NULL, *base_path = NULL;
648 int overlapcnt = 0;
649 char *parent = NULL;
651 *local_changes_subsumed = 0;
653 err = got_path_dirname(&parent, ondisk_path);
654 if (err)
655 return err;
657 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
658 err = got_error_from_errno("asprintf");
659 goto done;
662 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
663 if (err)
664 goto done;
666 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
667 f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
668 if (err) {
669 if (err->code != GOT_ERR_FILE_BINARY)
670 goto done;
671 err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
672 f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
673 ondisk_path);
674 if (err)
675 goto done;
678 err = (*progress_cb)(progress_arg,
679 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
680 if (err)
681 goto done;
683 if (fsync(merged_fd) != 0) {
684 err = got_error_from_errno("fsync");
685 goto done;
688 f_merged = fdopen(merged_fd, "r");
689 if (f_merged == NULL) {
690 err = got_error_from_errno("fdopen");
691 goto done;
693 merged_fd = -1;
695 /* Check if a clean merge has subsumed all local changes. */
696 if (overlapcnt == 0) {
697 err = check_files_equal(local_changes_subsumed, f_deriv,
698 f_merged);
699 if (err)
700 goto done;
703 if (fchmod(fileno(f_merged), st_mode) != 0) {
704 err = got_error_from_errno2("fchmod", merged_path);
705 goto done;
708 if (rename(merged_path, ondisk_path) != 0) {
709 err = got_error_from_errno3("rename", merged_path,
710 ondisk_path);
711 goto done;
713 done:
714 if (err) {
715 if (merged_path)
716 unlink(merged_path);
718 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
719 err = got_error_from_errno("close");
720 if (f_merged && fclose(f_merged) == EOF && err == NULL)
721 err = got_error_from_errno("fclose");
722 free(merged_path);
723 free(base_path);
724 free(parent);
725 return err;
728 static const struct got_error *
729 update_symlink(const char *ondisk_path, const char *target_path,
730 size_t target_len)
732 /* This is not atomic but matches what 'ln -sf' does. */
733 if (unlink(ondisk_path) == -1)
734 return got_error_from_errno2("unlink", ondisk_path);
735 if (symlink(target_path, ondisk_path) == -1)
736 return got_error_from_errno3("symlink", target_path,
737 ondisk_path);
738 return NULL;
741 /*
742 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
743 * in the work tree with a file that contains conflict markers and the
744 * conflicting target paths of the original version, a "derived version"
745 * of a symlink from an incoming change, and a local version of the symlink.
747 * The original versions's target path can be NULL if it is not available,
748 * such as if both derived versions added a new symlink at the same path.
750 * The incoming derived symlink target is NULL in case the incoming change
751 * has deleted this symlink.
752 */
753 static const struct got_error *
754 install_symlink_conflict(const char *deriv_target,
755 struct got_object_id *deriv_base_commit_id, const char *orig_target,
756 const char *label_orig, const char *local_target, const char *ondisk_path)
758 const struct got_error *err;
759 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
760 FILE *f = NULL;
762 err = got_object_id_str(&id_str, deriv_base_commit_id);
763 if (err)
764 return got_error_from_errno("asprintf");
766 if (asprintf(&label_deriv, "%s: commit %s",
767 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
768 err = got_error_from_errno("asprintf");
769 goto done;
772 err = got_opentemp_named(&path, &f, "got-symlink-conflict");
773 if (err)
774 goto done;
776 if (fchmod(fileno(f), GOT_DEFAULT_FILE_MODE) == -1) {
777 err = got_error_from_errno2("fchmod", path);
778 goto done;
781 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
782 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
783 deriv_target ? deriv_target : "(symlink was deleted)",
784 orig_target ? label_orig : "",
785 orig_target ? "\n" : "",
786 orig_target ? orig_target : "",
787 orig_target ? "\n" : "",
788 GOT_DIFF_CONFLICT_MARKER_SEP,
789 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
790 err = got_error_from_errno2("fprintf", path);
791 goto done;
794 if (unlink(ondisk_path) == -1) {
795 err = got_error_from_errno2("unlink", ondisk_path);
796 goto done;
798 if (rename(path, ondisk_path) == -1) {
799 err = got_error_from_errno3("rename", path, ondisk_path);
800 goto done;
802 done:
803 if (f != NULL && fclose(f) == EOF && err == NULL)
804 err = got_error_from_errno2("fclose", path);
805 free(path);
806 free(id_str);
807 free(label_deriv);
808 return err;
811 /* forward declaration */
812 static const struct got_error *
813 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
814 const char *, const char *, uint16_t, const char *,
815 struct got_blob_object *, struct got_object_id *,
816 struct got_repository *, got_worktree_checkout_cb, void *);
818 /*
819 * Merge a symlink into the work tree, where blob_orig acts as the common
820 * ancestor, deriv_target is the link target of the first derived version,
821 * and the symlink on disk acts as the second derived version.
822 * Assume that contents of both blobs represent symlinks.
823 */
824 static const struct got_error *
825 merge_symlink(struct got_worktree *worktree,
826 struct got_blob_object *blob_orig, const char *ondisk_path,
827 const char *path, const char *label_orig, const char *deriv_target,
828 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
829 got_worktree_checkout_cb progress_cb, void *progress_arg)
831 const struct got_error *err = NULL;
832 char *ancestor_target = NULL;
833 struct stat sb;
834 ssize_t ondisk_len, deriv_len;
835 char ondisk_target[PATH_MAX];
836 int have_local_change = 0;
837 int have_incoming_change = 0;
839 if (lstat(ondisk_path, &sb) == -1)
840 return got_error_from_errno2("lstat", ondisk_path);
842 ondisk_len = readlink(ondisk_path, ondisk_target,
843 sizeof(ondisk_target));
844 if (ondisk_len == -1) {
845 err = got_error_from_errno2("readlink",
846 ondisk_path);
847 goto done;
849 ondisk_target[ondisk_len] = '\0';
851 if (blob_orig) {
852 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
853 if (err)
854 goto done;
857 if (ancestor_target == NULL ||
858 (ondisk_len != strlen(ancestor_target) ||
859 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
860 have_local_change = 1;
862 deriv_len = strlen(deriv_target);
863 if (ancestor_target == NULL ||
864 (deriv_len != strlen(ancestor_target) ||
865 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
866 have_incoming_change = 1;
868 if (!have_local_change && !have_incoming_change) {
869 if (ancestor_target) {
870 /* Both sides made the same change. */
871 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
872 path);
873 } else if (deriv_len == ondisk_len &&
874 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
875 /* Both sides added the same symlink. */
876 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
877 path);
878 } else {
879 /* Both sides added symlinks which don't match. */
880 err = install_symlink_conflict(deriv_target,
881 deriv_base_commit_id, ancestor_target,
882 label_orig, ondisk_target, ondisk_path);
883 if (err)
884 goto done;
885 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
886 path);
888 } else if (!have_local_change && have_incoming_change) {
889 /* Apply the incoming change. */
890 err = update_symlink(ondisk_path, deriv_target,
891 strlen(deriv_target));
892 if (err)
893 goto done;
894 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
895 } else if (have_local_change && have_incoming_change) {
896 if (deriv_len == ondisk_len &&
897 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
898 /* Both sides made the same change. */
899 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
900 path);
901 } else {
902 err = install_symlink_conflict(deriv_target,
903 deriv_base_commit_id, ancestor_target, label_orig,
904 ondisk_target, ondisk_path);
905 if (err)
906 goto done;
907 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
908 path);
912 done:
913 free(ancestor_target);
914 return err;
917 static const struct got_error *
918 dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
920 const struct got_error *err = NULL;
921 char target_path[PATH_MAX];
922 ssize_t target_len;
923 size_t n;
924 FILE *f;
926 *outfile = NULL;
928 f = got_opentemp();
929 if (f == NULL)
930 return got_error_from_errno("got_opentemp");
931 target_len = readlink(ondisk_path, target_path, sizeof(target_path));
932 if (target_len == -1) {
933 err = got_error_from_errno2("readlink", ondisk_path);
934 goto done;
936 n = fwrite(target_path, 1, target_len, f);
937 if (n != target_len) {
938 err = got_ferror(f, GOT_ERR_IO);
939 goto done;
941 if (fflush(f) == EOF) {
942 err = got_error_from_errno("fflush");
943 goto done;
945 if (fseek(f, 0L, SEEK_SET) == -1) {
946 err = got_ferror(f, GOT_ERR_IO);
947 goto done;
949 done:
950 if (err)
951 fclose(f);
952 else
953 *outfile = f;
954 return err;
957 /*
958 * Perform a 3-way merge where blob_orig acts as the common ancestor,
959 * blob_deriv acts as the first derived version, and the file on disk
960 * acts as the second derived version.
961 */
962 static const struct got_error *
963 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
964 struct got_blob_object *blob_orig, const char *ondisk_path,
965 const char *path, uint16_t st_mode, const char *label_orig,
966 struct got_blob_object *blob_deriv,
967 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
968 got_worktree_checkout_cb progress_cb, void *progress_arg)
970 const struct got_error *err = NULL;
971 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
972 char *blob_orig_path = NULL;
973 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
974 char *label_deriv = NULL, *parent = NULL;
976 *local_changes_subsumed = 0;
978 err = got_path_dirname(&parent, ondisk_path);
979 if (err)
980 return err;
982 if (blob_orig) {
983 if (asprintf(&base_path, "%s/got-merge-blob-orig",
984 parent) == -1) {
985 err = got_error_from_errno("asprintf");
986 base_path = NULL;
987 goto done;
990 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
991 if (err)
992 goto done;
993 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
994 blob_orig);
995 if (err)
996 goto done;
997 free(base_path);
998 } else {
999 /*
1000 * No common ancestor exists. This is an "add vs add" conflict
1001 * and we simply use an empty ancestor file to make both files
1002 * appear in the merged result in their entirety.
1004 f_orig = got_opentemp();
1005 if (f_orig == NULL) {
1006 err = got_error_from_errno("got_opentemp");
1007 goto done;
1011 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1012 err = got_error_from_errno("asprintf");
1013 base_path = NULL;
1014 goto done;
1017 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1018 if (err)
1019 goto done;
1020 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1021 blob_deriv);
1022 if (err)
1023 goto done;
1025 err = got_object_id_str(&id_str, deriv_base_commit_id);
1026 if (err)
1027 goto done;
1028 if (asprintf(&label_deriv, "%s: commit %s",
1029 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1030 err = got_error_from_errno("asprintf");
1031 goto done;
1035 * In order the run a 3-way merge with a symlink we copy the symlink's
1036 * target path into a temporary file and use that file with diff3.
1038 if (S_ISLNK(st_mode)) {
1039 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1040 if (err)
1041 goto done;
1042 } else {
1043 int fd;
1044 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1045 if (fd == -1) {
1046 err = got_error_from_errno2("open", ondisk_path);
1047 goto done;
1049 f_deriv2 = fdopen(fd, "r");
1050 if (f_deriv2 == NULL) {
1051 err = got_error_from_errno2("fdopen", ondisk_path);
1052 close(fd);
1053 goto done;
1057 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1058 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1059 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1060 done:
1061 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1062 err = got_error_from_errno("fclose");
1063 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1064 err = got_error_from_errno("fclose");
1065 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1066 err = got_error_from_errno("fclose");
1067 free(base_path);
1068 if (blob_orig_path) {
1069 unlink(blob_orig_path);
1070 free(blob_orig_path);
1072 if (blob_deriv_path) {
1073 unlink(blob_deriv_path);
1074 free(blob_deriv_path);
1076 free(id_str);
1077 free(label_deriv);
1078 free(parent);
1079 return err;
1082 static const struct got_error *
1083 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1084 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1085 int wt_fd, const char *path, struct got_object_id *blob_id)
1087 const struct got_error *err = NULL;
1088 struct got_fileindex_entry *new_ie;
1090 *new_iep = NULL;
1092 err = got_fileindex_entry_alloc(&new_ie, path);
1093 if (err)
1094 return err;
1096 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1097 blob_id->sha1, base_commit_id->sha1, 1);
1098 if (err)
1099 goto done;
1101 err = got_fileindex_entry_add(fileindex, new_ie);
1102 done:
1103 if (err)
1104 got_fileindex_entry_free(new_ie);
1105 else
1106 *new_iep = new_ie;
1107 return err;
1110 static mode_t
1111 get_ondisk_perms(int executable, mode_t st_mode)
1113 mode_t xbits = S_IXUSR;
1115 if (executable) {
1116 /* Map read bits to execute bits. */
1117 if (st_mode & S_IRGRP)
1118 xbits |= S_IXGRP;
1119 if (st_mode & S_IROTH)
1120 xbits |= S_IXOTH;
1121 return st_mode | xbits;
1124 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1127 /* forward declaration */
1128 static const struct got_error *
1129 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1130 const char *path, mode_t te_mode, mode_t st_mode,
1131 struct got_blob_object *blob, int restoring_missing_file,
1132 int reverting_versioned_file, int installing_bad_symlink,
1133 int path_is_unversioned, struct got_repository *repo,
1134 got_worktree_checkout_cb progress_cb, void *progress_arg);
1137 * This function assumes that the provided symlink target points at a
1138 * safe location in the work tree!
1140 static const struct got_error *
1141 replace_existing_symlink(int *did_something, const char *ondisk_path,
1142 const char *target_path, size_t target_len)
1144 const struct got_error *err = NULL;
1145 ssize_t elen;
1146 char etarget[PATH_MAX];
1147 int fd;
1149 *did_something = 0;
1152 * "Bad" symlinks (those pointing outside the work tree or into the
1153 * .got directory) are installed in the work tree as a regular file
1154 * which contains the bad symlink target path.
1155 * The new symlink target has already been checked for safety by our
1156 * caller. If we can successfully open a regular file then we simply
1157 * replace this file with a symlink below.
1159 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1160 if (fd == -1) {
1161 if (!got_err_open_nofollow_on_symlink())
1162 return got_error_from_errno2("open", ondisk_path);
1164 /* We are updating an existing on-disk symlink. */
1165 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1166 if (elen == -1)
1167 return got_error_from_errno2("readlink", ondisk_path);
1169 if (elen == target_len &&
1170 memcmp(etarget, target_path, target_len) == 0)
1171 return NULL; /* nothing to do */
1174 *did_something = 1;
1175 err = update_symlink(ondisk_path, target_path, target_len);
1176 if (fd != -1 && close(fd) == -1 && err == NULL)
1177 err = got_error_from_errno2("close", ondisk_path);
1178 return err;
1181 static const struct got_error *
1182 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1183 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1185 const struct got_error *err = NULL;
1186 char canonpath[PATH_MAX];
1187 char *path_got = NULL;
1189 *is_bad_symlink = 0;
1191 if (target_len >= sizeof(canonpath)) {
1192 *is_bad_symlink = 1;
1193 return NULL;
1197 * We do not use realpath(3) to resolve the symlink's target
1198 * path because we don't want to resolve symlinks recursively.
1199 * Instead we make the path absolute and then canonicalize it.
1200 * Relative symlink target lookup should begin at the directory
1201 * in which the blob object is being installed.
1203 if (!got_path_is_absolute(target_path)) {
1204 char *abspath, *parent;
1205 err = got_path_dirname(&parent, ondisk_path);
1206 if (err)
1207 return err;
1208 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1209 free(parent);
1210 return got_error_from_errno("asprintf");
1212 free(parent);
1213 if (strlen(abspath) >= sizeof(canonpath)) {
1214 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1215 free(abspath);
1216 return err;
1218 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1219 free(abspath);
1220 if (err)
1221 return err;
1222 } else {
1223 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1224 if (err)
1225 return err;
1228 /* Only allow symlinks pointing at paths within the work tree. */
1229 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1230 *is_bad_symlink = 1;
1231 return NULL;
1234 /* Do not allow symlinks pointing into the .got directory. */
1235 if (asprintf(&path_got, "%s/%s", wtroot_path,
1236 GOT_WORKTREE_GOT_DIR) == -1)
1237 return got_error_from_errno("asprintf");
1238 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1239 *is_bad_symlink = 1;
1241 free(path_got);
1242 return NULL;
1245 static const struct got_error *
1246 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1247 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1248 int restoring_missing_file, int reverting_versioned_file,
1249 int path_is_unversioned, int allow_bad_symlinks,
1250 struct got_repository *repo,
1251 got_worktree_checkout_cb progress_cb, void *progress_arg)
1253 const struct got_error *err = NULL;
1254 char target_path[PATH_MAX];
1255 size_t len, target_len = 0;
1256 char *path_got = NULL;
1257 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1258 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1260 *is_bad_symlink = 0;
1263 * Blob object content specifies the target path of the link.
1264 * If a symbolic link cannot be installed we instead create
1265 * a regular file which contains the link target path stored
1266 * in the blob object.
1268 do {
1269 err = got_object_blob_read_block(&len, blob);
1270 if (len + target_len >= sizeof(target_path)) {
1271 /* Path too long; install as a regular file. */
1272 *is_bad_symlink = 1;
1273 got_object_blob_rewind(blob);
1274 return install_blob(worktree, ondisk_path, path,
1275 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1276 restoring_missing_file, reverting_versioned_file,
1277 1, path_is_unversioned, repo, progress_cb,
1278 progress_arg);
1280 if (len > 0) {
1281 /* Skip blob object header first time around. */
1282 memcpy(target_path + target_len, buf + hdrlen,
1283 len - hdrlen);
1284 target_len += len - hdrlen;
1285 hdrlen = 0;
1287 } while (len != 0);
1288 target_path[target_len] = '\0';
1290 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1291 ondisk_path, worktree->root_path);
1292 if (err)
1293 return err;
1295 if (*is_bad_symlink && !allow_bad_symlinks) {
1296 /* install as a regular file */
1297 got_object_blob_rewind(blob);
1298 err = install_blob(worktree, ondisk_path, path,
1299 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1300 restoring_missing_file, reverting_versioned_file, 1,
1301 path_is_unversioned, repo, progress_cb, progress_arg);
1302 goto done;
1305 if (symlink(target_path, ondisk_path) == -1) {
1306 if (errno == EEXIST) {
1307 int symlink_replaced;
1308 if (path_is_unversioned) {
1309 err = (*progress_cb)(progress_arg,
1310 GOT_STATUS_UNVERSIONED, path);
1311 goto done;
1313 err = replace_existing_symlink(&symlink_replaced,
1314 ondisk_path, target_path, target_len);
1315 if (err)
1316 goto done;
1317 if (progress_cb) {
1318 if (symlink_replaced) {
1319 err = (*progress_cb)(progress_arg,
1320 reverting_versioned_file ?
1321 GOT_STATUS_REVERT :
1322 GOT_STATUS_UPDATE, path);
1323 } else {
1324 err = (*progress_cb)(progress_arg,
1325 GOT_STATUS_EXISTS, path);
1328 goto done; /* Nothing else to do. */
1331 if (errno == ENOENT) {
1332 char *parent;
1333 err = got_path_dirname(&parent, ondisk_path);
1334 if (err)
1335 goto done;
1336 err = add_dir_on_disk(worktree, parent);
1337 free(parent);
1338 if (err)
1339 goto done;
1341 * Retry, and fall through to error handling
1342 * below if this second attempt fails.
1344 if (symlink(target_path, ondisk_path) != -1) {
1345 err = NULL; /* success */
1346 goto done;
1350 /* Handle errors from first or second creation attempt. */
1351 if (errno == ENAMETOOLONG) {
1352 /* bad target path; install as a regular file */
1353 *is_bad_symlink = 1;
1354 got_object_blob_rewind(blob);
1355 err = install_blob(worktree, ondisk_path, path,
1356 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1357 restoring_missing_file, reverting_versioned_file, 1,
1358 path_is_unversioned, repo,
1359 progress_cb, progress_arg);
1360 } else if (errno == ENOTDIR) {
1361 err = got_error_path(ondisk_path,
1362 GOT_ERR_FILE_OBSTRUCTED);
1363 } else {
1364 err = got_error_from_errno3("symlink",
1365 target_path, ondisk_path);
1367 } else if (progress_cb)
1368 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1369 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1370 done:
1371 free(path_got);
1372 return err;
1375 static const struct got_error *
1376 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1377 const char *path, mode_t te_mode, mode_t st_mode,
1378 struct got_blob_object *blob, int restoring_missing_file,
1379 int reverting_versioned_file, int installing_bad_symlink,
1380 int path_is_unversioned, struct got_repository *repo,
1381 got_worktree_checkout_cb progress_cb, void *progress_arg)
1383 const struct got_error *err = NULL;
1384 int fd = -1;
1385 size_t len, hdrlen;
1386 int update = 0;
1387 char *tmppath = NULL;
1389 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1390 O_CLOEXEC, GOT_DEFAULT_FILE_MODE);
1391 if (fd == -1) {
1392 if (errno == ENOENT) {
1393 char *parent;
1394 err = got_path_dirname(&parent, path);
1395 if (err)
1396 return err;
1397 err = add_dir_on_disk(worktree, parent);
1398 free(parent);
1399 if (err)
1400 return err;
1401 fd = open(ondisk_path,
1402 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1403 GOT_DEFAULT_FILE_MODE);
1404 if (fd == -1)
1405 return got_error_from_errno2("open",
1406 ondisk_path);
1407 } else if (errno == EEXIST) {
1408 if (path_is_unversioned) {
1409 err = (*progress_cb)(progress_arg,
1410 GOT_STATUS_UNVERSIONED, path);
1411 goto done;
1413 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1414 !S_ISREG(st_mode) && !installing_bad_symlink) {
1415 /* TODO file is obstructed; do something */
1416 err = got_error_path(ondisk_path,
1417 GOT_ERR_FILE_OBSTRUCTED);
1418 goto done;
1419 } else {
1420 err = got_opentemp_named_fd(&tmppath, &fd,
1421 ondisk_path);
1422 if (err)
1423 goto done;
1424 update = 1;
1426 } else
1427 return got_error_from_errno2("open", ondisk_path);
1430 if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1431 err = got_error_from_errno2("fchmod",
1432 update ? tmppath : ondisk_path);
1433 goto done;
1436 if (progress_cb) {
1437 if (restoring_missing_file)
1438 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1439 path);
1440 else if (reverting_versioned_file)
1441 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1442 path);
1443 else
1444 err = (*progress_cb)(progress_arg,
1445 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1446 if (err)
1447 goto done;
1450 hdrlen = got_object_blob_get_hdrlen(blob);
1451 do {
1452 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1453 err = got_object_blob_read_block(&len, blob);
1454 if (err)
1455 break;
1456 if (len > 0) {
1457 /* Skip blob object header first time around. */
1458 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1459 if (outlen == -1) {
1460 err = got_error_from_errno("write");
1461 goto done;
1462 } else if (outlen != len - hdrlen) {
1463 err = got_error(GOT_ERR_IO);
1464 goto done;
1466 hdrlen = 0;
1468 } while (len != 0);
1470 if (fsync(fd) != 0) {
1471 err = got_error_from_errno("fsync");
1472 goto done;
1475 if (update) {
1476 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1477 err = got_error_from_errno2("unlink", ondisk_path);
1478 goto done;
1480 if (rename(tmppath, ondisk_path) != 0) {
1481 err = got_error_from_errno3("rename", tmppath,
1482 ondisk_path);
1483 goto done;
1485 free(tmppath);
1486 tmppath = NULL;
1489 done:
1490 if (fd != -1 && close(fd) == -1 && err == NULL)
1491 err = got_error_from_errno("close");
1492 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1493 err = got_error_from_errno2("unlink", tmppath);
1494 free(tmppath);
1495 return err;
1498 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1499 static const struct got_error *
1500 get_modified_file_content_status(unsigned char *status, FILE *f)
1502 const struct got_error *err = NULL;
1503 const char *markers[3] = {
1504 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1505 GOT_DIFF_CONFLICT_MARKER_SEP,
1506 GOT_DIFF_CONFLICT_MARKER_END
1508 int i = 0;
1509 char *line = NULL;
1510 size_t linesize = 0;
1511 ssize_t linelen;
1513 while (*status == GOT_STATUS_MODIFY) {
1514 linelen = getline(&line, &linesize, f);
1515 if (linelen == -1) {
1516 if (feof(f))
1517 break;
1518 err = got_ferror(f, GOT_ERR_IO);
1519 break;
1522 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1523 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1524 == 0)
1525 *status = GOT_STATUS_CONFLICT;
1526 else
1527 i++;
1530 free(line);
1532 return err;
1535 static int
1536 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1538 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1539 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1542 static int
1543 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1545 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1546 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1547 ie->mtime_sec == sb->st_mtim.tv_sec &&
1548 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1549 ie->size == (sb->st_size & 0xffffffff) &&
1550 !xbit_differs(ie, sb->st_mode));
1553 static unsigned char
1554 get_staged_status(struct got_fileindex_entry *ie)
1556 switch (got_fileindex_entry_stage_get(ie)) {
1557 case GOT_FILEIDX_STAGE_ADD:
1558 return GOT_STATUS_ADD;
1559 case GOT_FILEIDX_STAGE_DELETE:
1560 return GOT_STATUS_DELETE;
1561 case GOT_FILEIDX_STAGE_MODIFY:
1562 return GOT_STATUS_MODIFY;
1563 default:
1564 return GOT_STATUS_NO_CHANGE;
1568 static const struct got_error *
1569 get_symlink_modification_status(unsigned char *status,
1570 struct got_fileindex_entry *ie, const char *abspath,
1571 int dirfd, const char *de_name, struct got_blob_object *blob)
1573 const struct got_error *err = NULL;
1574 char target_path[PATH_MAX];
1575 char etarget[PATH_MAX];
1576 ssize_t elen;
1577 size_t len, target_len = 0;
1578 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1579 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1581 *status = GOT_STATUS_NO_CHANGE;
1583 /* Blob object content specifies the target path of the link. */
1584 do {
1585 err = got_object_blob_read_block(&len, blob);
1586 if (err)
1587 return err;
1588 if (len + target_len >= sizeof(target_path)) {
1590 * Should not happen. The blob contents were OK
1591 * when this symlink was installed.
1593 return got_error(GOT_ERR_NO_SPACE);
1595 if (len > 0) {
1596 /* Skip blob object header first time around. */
1597 memcpy(target_path + target_len, buf + hdrlen,
1598 len - hdrlen);
1599 target_len += len - hdrlen;
1600 hdrlen = 0;
1602 } while (len != 0);
1603 target_path[target_len] = '\0';
1605 if (dirfd != -1) {
1606 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1607 if (elen == -1)
1608 return got_error_from_errno2("readlinkat", abspath);
1609 } else {
1610 elen = readlink(abspath, etarget, sizeof(etarget));
1611 if (elen == -1)
1612 return got_error_from_errno2("readlink", abspath);
1615 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1616 *status = GOT_STATUS_MODIFY;
1618 return NULL;
1621 static const struct got_error *
1622 get_file_status(unsigned char *status, struct stat *sb,
1623 struct got_fileindex_entry *ie, const char *abspath,
1624 int dirfd, const char *de_name, struct got_repository *repo)
1626 const struct got_error *err = NULL;
1627 struct got_object_id id;
1628 size_t hdrlen;
1629 int fd = -1;
1630 FILE *f = NULL;
1631 uint8_t fbuf[8192];
1632 struct got_blob_object *blob = NULL;
1633 size_t flen, blen;
1634 unsigned char staged_status = get_staged_status(ie);
1636 *status = GOT_STATUS_NO_CHANGE;
1637 memset(sb, 0, sizeof(*sb));
1640 * Whenever the caller provides a directory descriptor and a
1641 * directory entry name for the file, use them! This prevents
1642 * race conditions if filesystem paths change beneath our feet.
1644 if (dirfd != -1) {
1645 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1646 if (errno == ENOENT) {
1647 if (got_fileindex_entry_has_file_on_disk(ie))
1648 *status = GOT_STATUS_MISSING;
1649 else
1650 *status = GOT_STATUS_DELETE;
1651 goto done;
1653 err = got_error_from_errno2("fstatat", abspath);
1654 goto done;
1656 } else {
1657 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1658 if (fd == -1 && errno != ENOENT &&
1659 !got_err_open_nofollow_on_symlink())
1660 return got_error_from_errno2("open", abspath);
1661 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1662 if (lstat(abspath, sb) == -1)
1663 return got_error_from_errno2("lstat", abspath);
1664 } else if (fd == -1 || fstat(fd, sb) == -1) {
1665 if (errno == ENOENT) {
1666 if (got_fileindex_entry_has_file_on_disk(ie))
1667 *status = GOT_STATUS_MISSING;
1668 else
1669 *status = GOT_STATUS_DELETE;
1670 goto done;
1672 err = got_error_from_errno2("fstat", abspath);
1673 goto done;
1677 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1678 *status = GOT_STATUS_OBSTRUCTED;
1679 goto done;
1682 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1683 *status = GOT_STATUS_DELETE;
1684 goto done;
1685 } else if (!got_fileindex_entry_has_blob(ie) &&
1686 staged_status != GOT_STATUS_ADD) {
1687 *status = GOT_STATUS_ADD;
1688 goto done;
1691 if (!stat_info_differs(ie, sb))
1692 goto done;
1694 if (S_ISLNK(sb->st_mode) &&
1695 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1696 *status = GOT_STATUS_MODIFY;
1697 goto done;
1700 if (staged_status == GOT_STATUS_MODIFY ||
1701 staged_status == GOT_STATUS_ADD)
1702 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1703 else
1704 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1706 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1707 if (err)
1708 goto done;
1710 if (S_ISLNK(sb->st_mode)) {
1711 err = get_symlink_modification_status(status, ie,
1712 abspath, dirfd, de_name, blob);
1713 goto done;
1716 if (dirfd != -1) {
1717 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1718 if (fd == -1) {
1719 err = got_error_from_errno2("openat", abspath);
1720 goto done;
1724 f = fdopen(fd, "r");
1725 if (f == NULL) {
1726 err = got_error_from_errno2("fdopen", abspath);
1727 goto done;
1729 fd = -1;
1730 hdrlen = got_object_blob_get_hdrlen(blob);
1731 for (;;) {
1732 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1733 err = got_object_blob_read_block(&blen, blob);
1734 if (err)
1735 goto done;
1736 /* Skip length of blob object header first time around. */
1737 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1738 if (flen == 0 && ferror(f)) {
1739 err = got_error_from_errno("fread");
1740 goto done;
1742 if (blen - hdrlen == 0) {
1743 if (flen != 0)
1744 *status = GOT_STATUS_MODIFY;
1745 break;
1746 } else if (flen == 0) {
1747 if (blen - hdrlen != 0)
1748 *status = GOT_STATUS_MODIFY;
1749 break;
1750 } else if (blen - hdrlen == flen) {
1751 /* Skip blob object header first time around. */
1752 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1753 *status = GOT_STATUS_MODIFY;
1754 break;
1756 } else {
1757 *status = GOT_STATUS_MODIFY;
1758 break;
1760 hdrlen = 0;
1763 if (*status == GOT_STATUS_MODIFY) {
1764 rewind(f);
1765 err = get_modified_file_content_status(status, f);
1766 } else if (xbit_differs(ie, sb->st_mode))
1767 *status = GOT_STATUS_MODE_CHANGE;
1768 done:
1769 if (blob)
1770 got_object_blob_close(blob);
1771 if (f != NULL && fclose(f) == EOF && err == NULL)
1772 err = got_error_from_errno2("fclose", abspath);
1773 if (fd != -1 && close(fd) == -1 && err == NULL)
1774 err = got_error_from_errno2("close", abspath);
1775 return err;
1779 * Update timestamps in the file index if a file is unmodified and
1780 * we had to run a full content comparison to find out.
1782 static const struct got_error *
1783 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1784 struct got_fileindex_entry *ie, struct stat *sb)
1786 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1787 return got_fileindex_entry_update(ie, wt_fd, path,
1788 ie->blob_sha1, ie->commit_sha1, 1);
1790 return NULL;
1793 static const struct got_error *
1794 update_blob(struct got_worktree *worktree,
1795 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1796 struct got_tree_entry *te, const char *path,
1797 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1798 void *progress_arg)
1800 const struct got_error *err = NULL;
1801 struct got_blob_object *blob = NULL;
1802 char *ondisk_path;
1803 unsigned char status = GOT_STATUS_NO_CHANGE;
1804 struct stat sb;
1806 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1807 return got_error_from_errno("asprintf");
1809 if (ie) {
1810 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1811 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1812 goto done;
1814 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1815 repo);
1816 if (err)
1817 goto done;
1818 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1819 sb.st_mode = got_fileindex_perms_to_st(ie);
1820 } else {
1821 if (stat(ondisk_path, &sb) == -1) {
1822 if (errno != ENOENT) {
1823 err = got_error_from_errno2("stat",
1824 ondisk_path);
1825 goto done;
1827 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1828 status = GOT_STATUS_UNVERSIONED;
1829 } else {
1830 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1831 status = GOT_STATUS_UNVERSIONED;
1832 else
1833 status = GOT_STATUS_OBSTRUCTED;
1837 if (status == GOT_STATUS_OBSTRUCTED) {
1838 if (ie)
1839 got_fileindex_entry_mark_skipped(ie);
1840 err = (*progress_cb)(progress_arg, status, path);
1841 goto done;
1843 if (status == GOT_STATUS_CONFLICT) {
1844 if (ie)
1845 got_fileindex_entry_mark_skipped(ie);
1846 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1847 path);
1848 goto done;
1851 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1852 (S_ISLNK(te->mode) ||
1853 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1855 * This is a regular file or an installed bad symlink.
1856 * If the file index indicates that this file is already
1857 * up-to-date with respect to the repository we can skip
1858 * updating contents of this file.
1860 if (got_fileindex_entry_has_commit(ie) &&
1861 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1862 SHA1_DIGEST_LENGTH) == 0) {
1863 /* Same commit. */
1864 err = sync_timestamps(worktree->root_fd,
1865 path, status, ie, &sb);
1866 if (err)
1867 goto done;
1868 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1869 path);
1870 goto done;
1872 if (got_fileindex_entry_has_blob(ie) &&
1873 memcmp(ie->blob_sha1, te->id.sha1,
1874 SHA1_DIGEST_LENGTH) == 0) {
1875 /* Different commit but the same blob. */
1876 err = sync_timestamps(worktree->root_fd,
1877 path, status, ie, &sb);
1878 if (err)
1879 goto done;
1880 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1881 path);
1882 goto done;
1886 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1887 if (err)
1888 goto done;
1890 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1891 int update_timestamps;
1892 struct got_blob_object *blob2 = NULL;
1893 char *label_orig = NULL;
1894 if (got_fileindex_entry_has_blob(ie)) {
1895 struct got_object_id id2;
1896 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1897 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1898 if (err)
1899 goto done;
1901 if (got_fileindex_entry_has_commit(ie)) {
1902 char id_str[SHA1_DIGEST_STRING_LENGTH];
1903 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1904 sizeof(id_str)) == NULL) {
1905 err = got_error_path(id_str,
1906 GOT_ERR_BAD_OBJ_ID_STR);
1907 goto done;
1909 if (asprintf(&label_orig, "%s: commit %s",
1910 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1911 err = got_error_from_errno("asprintf");
1912 goto done;
1915 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1916 char *link_target;
1917 err = got_object_blob_read_to_str(&link_target, blob);
1918 if (err)
1919 goto done;
1920 err = merge_symlink(worktree, blob2, ondisk_path, path,
1921 label_orig, link_target, worktree->base_commit_id,
1922 repo, progress_cb, progress_arg);
1923 free(link_target);
1924 } else {
1925 err = merge_blob(&update_timestamps, worktree, blob2,
1926 ondisk_path, path, sb.st_mode, label_orig, blob,
1927 worktree->base_commit_id, repo,
1928 progress_cb, progress_arg);
1930 free(label_orig);
1931 if (blob2)
1932 got_object_blob_close(blob2);
1933 if (err)
1934 goto done;
1936 * Do not update timestamps of files with local changes.
1937 * Otherwise, a future status walk would treat them as
1938 * unmodified files again.
1940 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1941 blob->id.sha1, worktree->base_commit_id->sha1,
1942 update_timestamps);
1943 } else if (status == GOT_STATUS_MODE_CHANGE) {
1944 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1945 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1946 } else if (status == GOT_STATUS_DELETE) {
1947 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1948 if (err)
1949 goto done;
1950 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1951 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1952 if (err)
1953 goto done;
1954 } else {
1955 int is_bad_symlink = 0;
1956 if (S_ISLNK(te->mode)) {
1957 err = install_symlink(&is_bad_symlink, worktree,
1958 ondisk_path, path, blob,
1959 status == GOT_STATUS_MISSING, 0,
1960 status == GOT_STATUS_UNVERSIONED, 0,
1961 repo, progress_cb, progress_arg);
1962 } else {
1963 err = install_blob(worktree, ondisk_path, path,
1964 te->mode, sb.st_mode, blob,
1965 status == GOT_STATUS_MISSING, 0, 0,
1966 status == GOT_STATUS_UNVERSIONED, repo,
1967 progress_cb, progress_arg);
1969 if (err)
1970 goto done;
1972 if (ie) {
1973 err = got_fileindex_entry_update(ie,
1974 worktree->root_fd, path, blob->id.sha1,
1975 worktree->base_commit_id->sha1, 1);
1976 } else {
1977 err = create_fileindex_entry(&ie, fileindex,
1978 worktree->base_commit_id, worktree->root_fd, path,
1979 &blob->id);
1981 if (err)
1982 goto done;
1984 if (is_bad_symlink) {
1985 got_fileindex_entry_filetype_set(ie,
1986 GOT_FILEIDX_MODE_BAD_SYMLINK);
1989 got_object_blob_close(blob);
1990 done:
1991 free(ondisk_path);
1992 return err;
1995 static const struct got_error *
1996 remove_ondisk_file(const char *root_path, const char *path)
1998 const struct got_error *err = NULL;
1999 char *ondisk_path = NULL, *parent = NULL;
2001 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2002 return got_error_from_errno("asprintf");
2004 if (unlink(ondisk_path) == -1) {
2005 if (errno != ENOENT)
2006 err = got_error_from_errno2("unlink", ondisk_path);
2007 } else {
2008 size_t root_len = strlen(root_path);
2009 err = got_path_dirname(&parent, ondisk_path);
2010 if (err)
2011 goto done;
2012 while (got_path_cmp(parent, root_path,
2013 strlen(parent), root_len) != 0) {
2014 free(ondisk_path);
2015 ondisk_path = parent;
2016 parent = NULL;
2017 if (rmdir(ondisk_path) == -1) {
2018 if (errno != ENOTEMPTY)
2019 err = got_error_from_errno2("rmdir",
2020 ondisk_path);
2021 break;
2023 err = got_path_dirname(&parent, ondisk_path);
2024 if (err)
2025 break;
2028 done:
2029 free(ondisk_path);
2030 free(parent);
2031 return err;
2034 static const struct got_error *
2035 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2036 struct got_fileindex_entry *ie, struct got_repository *repo,
2037 got_worktree_checkout_cb progress_cb, void *progress_arg)
2039 const struct got_error *err = NULL;
2040 unsigned char status;
2041 struct stat sb;
2042 char *ondisk_path;
2044 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2045 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2047 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2048 == -1)
2049 return got_error_from_errno("asprintf");
2051 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2052 if (err)
2053 goto done;
2055 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2056 char ondisk_target[PATH_MAX];
2057 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2058 sizeof(ondisk_target));
2059 if (ondisk_len == -1) {
2060 err = got_error_from_errno2("readlink", ondisk_path);
2061 goto done;
2063 ondisk_target[ondisk_len] = '\0';
2064 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2065 NULL, NULL, /* XXX pass common ancestor info? */
2066 ondisk_target, ondisk_path);
2067 if (err)
2068 goto done;
2069 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2070 ie->path);
2071 goto done;
2074 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2075 status == GOT_STATUS_ADD) {
2076 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2077 if (err)
2078 goto done;
2080 * Preserve the working file and change the deleted blob's
2081 * entry into a schedule-add entry.
2083 err = got_fileindex_entry_update(ie, worktree->root_fd,
2084 ie->path, NULL, NULL, 0);
2085 } else {
2086 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2087 if (err)
2088 goto done;
2089 if (status == GOT_STATUS_NO_CHANGE) {
2090 err = remove_ondisk_file(worktree->root_path, ie->path);
2091 if (err)
2092 goto done;
2094 got_fileindex_entry_remove(fileindex, ie);
2096 done:
2097 free(ondisk_path);
2098 return err;
2101 struct diff_cb_arg {
2102 struct got_fileindex *fileindex;
2103 struct got_worktree *worktree;
2104 struct got_repository *repo;
2105 got_worktree_checkout_cb progress_cb;
2106 void *progress_arg;
2107 got_cancel_cb cancel_cb;
2108 void *cancel_arg;
2111 static const struct got_error *
2112 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2113 struct got_tree_entry *te, const char *parent_path)
2115 struct diff_cb_arg *a = arg;
2117 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2118 return got_error(GOT_ERR_CANCELLED);
2120 return update_blob(a->worktree, a->fileindex, ie, te,
2121 ie->path, a->repo, a->progress_cb, a->progress_arg);
2124 static const struct got_error *
2125 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2127 struct diff_cb_arg *a = arg;
2129 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2130 return got_error(GOT_ERR_CANCELLED);
2132 return delete_blob(a->worktree, a->fileindex, ie,
2133 a->repo, a->progress_cb, a->progress_arg);
2136 static const struct got_error *
2137 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2139 struct diff_cb_arg *a = arg;
2140 const struct got_error *err;
2141 char *path;
2143 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2144 return got_error(GOT_ERR_CANCELLED);
2146 if (got_object_tree_entry_is_submodule(te))
2147 return NULL;
2149 if (asprintf(&path, "%s%s%s", parent_path,
2150 parent_path[0] ? "/" : "", te->name)
2151 == -1)
2152 return got_error_from_errno("asprintf");
2154 if (S_ISDIR(te->mode))
2155 err = add_dir_on_disk(a->worktree, path);
2156 else
2157 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2158 a->repo, a->progress_cb, a->progress_arg);
2160 free(path);
2161 return err;
2164 const struct got_error *
2165 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2167 uint32_t uuid_status;
2169 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2170 if (uuid_status != uuid_s_ok) {
2171 *uuidstr = NULL;
2172 return got_error_uuid(uuid_status, "uuid_to_string");
2175 return NULL;
2178 static const struct got_error *
2179 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2181 const struct got_error *err = NULL;
2182 char *uuidstr = NULL;
2184 *refname = NULL;
2186 err = got_worktree_get_uuid(&uuidstr, worktree);
2187 if (err)
2188 return err;
2190 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2191 err = got_error_from_errno("asprintf");
2192 *refname = NULL;
2194 free(uuidstr);
2195 return err;
2198 const struct got_error *
2199 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2201 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2204 static const struct got_error *
2205 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2207 return get_ref_name(refname, worktree,
2208 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2211 static const struct got_error *
2212 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2214 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2217 static const struct got_error *
2218 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2220 return get_ref_name(refname, worktree,
2221 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2224 static const struct got_error *
2225 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2227 return get_ref_name(refname, worktree,
2228 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2231 static const struct got_error *
2232 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2234 return get_ref_name(refname, worktree,
2235 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2238 static const struct got_error *
2239 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2241 return get_ref_name(refname, worktree,
2242 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2245 static const struct got_error *
2246 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2248 return get_ref_name(refname, worktree,
2249 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2252 static const struct got_error *
2253 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2255 return get_ref_name(refname, worktree,
2256 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2259 const struct got_error *
2260 got_worktree_get_histedit_script_path(char **path,
2261 struct got_worktree *worktree)
2263 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2264 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2265 *path = NULL;
2266 return got_error_from_errno("asprintf");
2268 return NULL;
2271 static const struct got_error *
2272 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2274 return get_ref_name(refname, worktree,
2275 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2278 static const struct got_error *
2279 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2281 return get_ref_name(refname, worktree,
2282 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2286 * Prevent Git's garbage collector from deleting our base commit by
2287 * setting a reference to our base commit's ID.
2289 static const struct got_error *
2290 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2292 const struct got_error *err = NULL;
2293 struct got_reference *ref = NULL;
2294 char *refname;
2296 err = got_worktree_get_base_ref_name(&refname, worktree);
2297 if (err)
2298 return err;
2300 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2301 if (err)
2302 goto done;
2304 err = got_ref_write(ref, repo);
2305 done:
2306 free(refname);
2307 if (ref)
2308 got_ref_close(ref);
2309 return err;
2312 static const struct got_error *
2313 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2315 const struct got_error *err = NULL;
2317 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2318 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2319 err = got_error_from_errno("asprintf");
2320 *fileindex_path = NULL;
2322 return err;
2326 static const struct got_error *
2327 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2328 struct got_worktree *worktree)
2330 const struct got_error *err = NULL;
2331 FILE *index = NULL;
2333 *fileindex_path = NULL;
2334 *fileindex = got_fileindex_alloc();
2335 if (*fileindex == NULL)
2336 return got_error_from_errno("got_fileindex_alloc");
2338 err = get_fileindex_path(fileindex_path, worktree);
2339 if (err)
2340 goto done;
2342 index = fopen(*fileindex_path, "rbe");
2343 if (index == NULL) {
2344 if (errno != ENOENT)
2345 err = got_error_from_errno2("fopen", *fileindex_path);
2346 } else {
2347 err = got_fileindex_read(*fileindex, index);
2348 if (fclose(index) == EOF && err == NULL)
2349 err = got_error_from_errno("fclose");
2351 done:
2352 if (err) {
2353 free(*fileindex_path);
2354 *fileindex_path = NULL;
2355 got_fileindex_free(*fileindex);
2356 *fileindex = NULL;
2358 return err;
2361 struct bump_base_commit_id_arg {
2362 struct got_object_id *base_commit_id;
2363 const char *path;
2364 size_t path_len;
2365 const char *entry_name;
2366 got_worktree_checkout_cb progress_cb;
2367 void *progress_arg;
2370 /* Bump base commit ID of all files within an updated part of the work tree. */
2371 static const struct got_error *
2372 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2374 const struct got_error *err;
2375 struct bump_base_commit_id_arg *a = arg;
2377 if (a->entry_name) {
2378 if (strcmp(ie->path, a->path) != 0)
2379 return NULL;
2380 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2381 return NULL;
2383 if (got_fileindex_entry_was_skipped(ie))
2384 return NULL;
2386 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2387 SHA1_DIGEST_LENGTH) == 0)
2388 return NULL;
2390 if (a->progress_cb) {
2391 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2392 ie->path);
2393 if (err)
2394 return err;
2396 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2397 return NULL;
2400 static const struct got_error *
2401 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2402 struct got_fileindex *fileindex,
2403 got_worktree_checkout_cb progress_cb, void *progress_arg)
2405 struct bump_base_commit_id_arg bbc_arg;
2407 bbc_arg.base_commit_id = worktree->base_commit_id;
2408 bbc_arg.entry_name = NULL;
2409 bbc_arg.path = "";
2410 bbc_arg.path_len = 0;
2411 bbc_arg.progress_cb = progress_cb;
2412 bbc_arg.progress_arg = progress_arg;
2414 return got_fileindex_for_each_entry_safe(fileindex,
2415 bump_base_commit_id, &bbc_arg);
2418 static const struct got_error *
2419 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2421 const struct got_error *err = NULL;
2422 char *new_fileindex_path = NULL;
2423 FILE *new_index = NULL;
2424 struct timespec timeout;
2426 err = got_opentemp_named(&new_fileindex_path, &new_index,
2427 fileindex_path);
2428 if (err)
2429 goto done;
2431 err = got_fileindex_write(fileindex, new_index);
2432 if (err)
2433 goto done;
2435 if (rename(new_fileindex_path, fileindex_path) != 0) {
2436 err = got_error_from_errno3("rename", new_fileindex_path,
2437 fileindex_path);
2438 unlink(new_fileindex_path);
2442 * Sleep for a short amount of time to ensure that files modified after
2443 * this program exits have a different time stamp from the one which
2444 * was recorded in the file index.
2446 timeout.tv_sec = 0;
2447 timeout.tv_nsec = 1;
2448 nanosleep(&timeout, NULL);
2449 done:
2450 if (new_index)
2451 fclose(new_index);
2452 free(new_fileindex_path);
2453 return err;
2456 static const struct got_error *
2457 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2458 struct got_object_id **tree_id, const char *wt_relpath,
2459 struct got_commit_object *base_commit, struct got_worktree *worktree,
2460 struct got_repository *repo)
2462 const struct got_error *err = NULL;
2463 struct got_object_id *id = NULL;
2464 char *in_repo_path = NULL;
2465 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2467 *entry_type = GOT_OBJ_TYPE_ANY;
2468 *tree_relpath = NULL;
2469 *tree_id = NULL;
2471 if (wt_relpath[0] == '\0') {
2472 /* Check out all files within the work tree. */
2473 *entry_type = GOT_OBJ_TYPE_TREE;
2474 *tree_relpath = strdup("");
2475 if (*tree_relpath == NULL) {
2476 err = got_error_from_errno("strdup");
2477 goto done;
2479 err = got_object_id_by_path(tree_id, repo, base_commit,
2480 worktree->path_prefix);
2481 if (err)
2482 goto done;
2483 return NULL;
2486 /* Check out a subset of files in the work tree. */
2488 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2489 is_root_wt ? "" : "/", wt_relpath) == -1) {
2490 err = got_error_from_errno("asprintf");
2491 goto done;
2494 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2495 if (err)
2496 goto done;
2498 free(in_repo_path);
2499 in_repo_path = NULL;
2501 err = got_object_get_type(entry_type, repo, id);
2502 if (err)
2503 goto done;
2505 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2506 /* Check out a single file. */
2507 if (strchr(wt_relpath, '/') == NULL) {
2508 /* Check out a single file in work tree's root dir. */
2509 in_repo_path = strdup(worktree->path_prefix);
2510 if (in_repo_path == NULL) {
2511 err = got_error_from_errno("strdup");
2512 goto done;
2514 *tree_relpath = strdup("");
2515 if (*tree_relpath == NULL) {
2516 err = got_error_from_errno("strdup");
2517 goto done;
2519 } else {
2520 /* Check out a single file in a subdirectory. */
2521 err = got_path_dirname(tree_relpath, wt_relpath);
2522 if (err)
2523 return err;
2524 if (asprintf(&in_repo_path, "%s%s%s",
2525 worktree->path_prefix, is_root_wt ? "" : "/",
2526 *tree_relpath) == -1) {
2527 err = got_error_from_errno("asprintf");
2528 goto done;
2531 err = got_object_id_by_path(tree_id, repo,
2532 base_commit, in_repo_path);
2533 } else {
2534 /* Check out all files within a subdirectory. */
2535 *tree_id = got_object_id_dup(id);
2536 if (*tree_id == NULL) {
2537 err = got_error_from_errno("got_object_id_dup");
2538 goto done;
2540 *tree_relpath = strdup(wt_relpath);
2541 if (*tree_relpath == NULL) {
2542 err = got_error_from_errno("strdup");
2543 goto done;
2546 done:
2547 free(id);
2548 free(in_repo_path);
2549 if (err) {
2550 *entry_type = GOT_OBJ_TYPE_ANY;
2551 free(*tree_relpath);
2552 *tree_relpath = NULL;
2553 free(*tree_id);
2554 *tree_id = NULL;
2556 return err;
2559 static const struct got_error *
2560 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2561 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2562 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2563 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2565 const struct got_error *err = NULL;
2566 struct got_commit_object *commit = NULL;
2567 struct got_tree_object *tree = NULL;
2568 struct got_fileindex_diff_tree_cb diff_cb;
2569 struct diff_cb_arg arg;
2571 err = ref_base_commit(worktree, repo);
2572 if (err) {
2573 if (!(err->code == GOT_ERR_ERRNO &&
2574 (errno == EACCES || errno == EROFS)))
2575 goto done;
2576 err = (*progress_cb)(progress_arg,
2577 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2578 if (err)
2579 return err;
2582 err = got_object_open_as_commit(&commit, repo,
2583 worktree->base_commit_id);
2584 if (err)
2585 goto done;
2587 err = got_object_open_as_tree(&tree, repo, tree_id);
2588 if (err)
2589 goto done;
2591 if (entry_name &&
2592 got_object_tree_find_entry(tree, entry_name) == NULL) {
2593 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2594 goto done;
2597 diff_cb.diff_old_new = diff_old_new;
2598 diff_cb.diff_old = diff_old;
2599 diff_cb.diff_new = diff_new;
2600 arg.fileindex = fileindex;
2601 arg.worktree = worktree;
2602 arg.repo = repo;
2603 arg.progress_cb = progress_cb;
2604 arg.progress_arg = progress_arg;
2605 arg.cancel_cb = cancel_cb;
2606 arg.cancel_arg = cancel_arg;
2607 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2608 entry_name, repo, &diff_cb, &arg);
2609 done:
2610 if (tree)
2611 got_object_tree_close(tree);
2612 if (commit)
2613 got_object_commit_close(commit);
2614 return err;
2617 const struct got_error *
2618 got_worktree_checkout_files(struct got_worktree *worktree,
2619 struct got_pathlist_head *paths, struct got_repository *repo,
2620 got_worktree_checkout_cb progress_cb, void *progress_arg,
2621 got_cancel_cb cancel_cb, void *cancel_arg)
2623 const struct got_error *err = NULL, *sync_err, *unlockerr;
2624 struct got_commit_object *commit = NULL;
2625 struct got_tree_object *tree = NULL;
2626 struct got_fileindex *fileindex = NULL;
2627 char *fileindex_path = NULL;
2628 struct got_pathlist_entry *pe;
2629 struct tree_path_data {
2630 STAILQ_ENTRY(tree_path_data) entry;
2631 struct got_object_id *tree_id;
2632 int entry_type;
2633 char *relpath;
2634 char *entry_name;
2635 } *tpd = NULL;
2636 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2638 STAILQ_INIT(&tree_paths);
2640 err = lock_worktree(worktree, LOCK_EX);
2641 if (err)
2642 return err;
2644 err = got_object_open_as_commit(&commit, repo,
2645 worktree->base_commit_id);
2646 if (err)
2647 goto done;
2649 /* Map all specified paths to in-repository trees. */
2650 TAILQ_FOREACH(pe, paths, entry) {
2651 tpd = malloc(sizeof(*tpd));
2652 if (tpd == NULL) {
2653 err = got_error_from_errno("malloc");
2654 goto done;
2657 err = find_tree_entry_for_checkout(&tpd->entry_type,
2658 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2659 worktree, repo);
2660 if (err) {
2661 free(tpd);
2662 goto done;
2665 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2666 err = got_path_basename(&tpd->entry_name, pe->path);
2667 if (err) {
2668 free(tpd->relpath);
2669 free(tpd->tree_id);
2670 free(tpd);
2671 goto done;
2673 } else
2674 tpd->entry_name = NULL;
2676 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2680 * Read the file index.
2681 * Checking out files is supposed to be an idempotent operation.
2682 * If the on-disk file index is incomplete we will try to complete it.
2684 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2685 if (err)
2686 goto done;
2688 tpd = STAILQ_FIRST(&tree_paths);
2689 TAILQ_FOREACH(pe, paths, entry) {
2690 struct bump_base_commit_id_arg bbc_arg;
2692 err = checkout_files(worktree, fileindex, tpd->relpath,
2693 tpd->tree_id, tpd->entry_name, repo,
2694 progress_cb, progress_arg, cancel_cb, cancel_arg);
2695 if (err)
2696 break;
2698 bbc_arg.base_commit_id = worktree->base_commit_id;
2699 bbc_arg.entry_name = tpd->entry_name;
2700 bbc_arg.path = pe->path;
2701 bbc_arg.path_len = pe->path_len;
2702 bbc_arg.progress_cb = progress_cb;
2703 bbc_arg.progress_arg = progress_arg;
2704 err = got_fileindex_for_each_entry_safe(fileindex,
2705 bump_base_commit_id, &bbc_arg);
2706 if (err)
2707 break;
2709 tpd = STAILQ_NEXT(tpd, entry);
2711 sync_err = sync_fileindex(fileindex, fileindex_path);
2712 if (sync_err && err == NULL)
2713 err = sync_err;
2714 done:
2715 free(fileindex_path);
2716 if (tree)
2717 got_object_tree_close(tree);
2718 if (commit)
2719 got_object_commit_close(commit);
2720 if (fileindex)
2721 got_fileindex_free(fileindex);
2722 while (!STAILQ_EMPTY(&tree_paths)) {
2723 tpd = STAILQ_FIRST(&tree_paths);
2724 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2725 free(tpd->relpath);
2726 free(tpd->tree_id);
2727 free(tpd);
2729 unlockerr = lock_worktree(worktree, LOCK_SH);
2730 if (unlockerr && err == NULL)
2731 err = unlockerr;
2732 return err;
2735 struct merge_file_cb_arg {
2736 struct got_worktree *worktree;
2737 struct got_fileindex *fileindex;
2738 got_worktree_checkout_cb progress_cb;
2739 void *progress_arg;
2740 got_cancel_cb cancel_cb;
2741 void *cancel_arg;
2742 const char *label_orig;
2743 struct got_object_id *commit_id2;
2744 int allow_bad_symlinks;
2747 static const struct got_error *
2748 merge_file_cb(void *arg, struct got_blob_object *blob1,
2749 struct got_blob_object *blob2, struct got_object_id *id1,
2750 struct got_object_id *id2, const char *path1, const char *path2,
2751 mode_t mode1, mode_t mode2, struct got_repository *repo)
2753 static const struct got_error *err = NULL;
2754 struct merge_file_cb_arg *a = arg;
2755 struct got_fileindex_entry *ie;
2756 char *ondisk_path = NULL;
2757 struct stat sb;
2758 unsigned char status;
2759 int local_changes_subsumed;
2760 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2761 char *id_str = NULL, *label_deriv2 = NULL;
2763 if (blob1 && blob2) {
2764 ie = got_fileindex_entry_get(a->fileindex, path2,
2765 strlen(path2));
2766 if (ie == NULL)
2767 return (*a->progress_cb)(a->progress_arg,
2768 GOT_STATUS_MISSING, path2);
2770 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2771 path2) == -1)
2772 return got_error_from_errno("asprintf");
2774 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2775 repo);
2776 if (err)
2777 goto done;
2779 if (status == GOT_STATUS_DELETE) {
2780 err = (*a->progress_cb)(a->progress_arg,
2781 GOT_STATUS_MERGE, path2);
2782 goto done;
2784 if (status != GOT_STATUS_NO_CHANGE &&
2785 status != GOT_STATUS_MODIFY &&
2786 status != GOT_STATUS_CONFLICT &&
2787 status != GOT_STATUS_ADD) {
2788 err = (*a->progress_cb)(a->progress_arg, status, path2);
2789 goto done;
2792 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2793 char *link_target2;
2794 err = got_object_blob_read_to_str(&link_target2, blob2);
2795 if (err)
2796 goto done;
2797 err = merge_symlink(a->worktree, blob1, ondisk_path,
2798 path2, a->label_orig, link_target2, a->commit_id2,
2799 repo, a->progress_cb, a->progress_arg);
2800 free(link_target2);
2801 } else {
2802 int fd;
2804 f_orig = got_opentemp();
2805 if (f_orig == NULL) {
2806 err = got_error_from_errno("got_opentemp");
2807 goto done;
2809 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2810 f_orig, blob1);
2811 if (err)
2812 goto done;
2814 f_deriv2 = got_opentemp();
2815 if (f_deriv2 == NULL)
2816 goto done;
2817 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2818 f_deriv2, blob2);
2819 if (err)
2820 goto done;
2822 fd = open(ondisk_path,
2823 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2824 if (fd == -1) {
2825 err = got_error_from_errno2("open",
2826 ondisk_path);
2827 goto done;
2829 f_deriv = fdopen(fd, "r");
2830 if (f_deriv == NULL) {
2831 err = got_error_from_errno2("fdopen",
2832 ondisk_path);
2833 close(fd);
2834 goto done;
2836 err = got_object_id_str(&id_str, a->commit_id2);
2837 if (err)
2838 goto done;
2839 if (asprintf(&label_deriv2, "%s: commit %s",
2840 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2841 err = got_error_from_errno("asprintf");
2842 goto done;
2844 err = merge_file(&local_changes_subsumed, a->worktree,
2845 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2846 sb.st_mode, a->label_orig, NULL, label_deriv2,
2847 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2848 a->progress_cb, a->progress_arg);
2850 } else if (blob1) {
2851 ie = got_fileindex_entry_get(a->fileindex, path1,
2852 strlen(path1));
2853 if (ie == NULL)
2854 return (*a->progress_cb)(a->progress_arg,
2855 GOT_STATUS_MISSING, path1);
2857 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2858 path1) == -1)
2859 return got_error_from_errno("asprintf");
2861 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2862 repo);
2863 if (err)
2864 goto done;
2866 switch (status) {
2867 case GOT_STATUS_NO_CHANGE:
2868 err = (*a->progress_cb)(a->progress_arg,
2869 GOT_STATUS_DELETE, path1);
2870 if (err)
2871 goto done;
2872 err = remove_ondisk_file(a->worktree->root_path, path1);
2873 if (err)
2874 goto done;
2875 if (ie)
2876 got_fileindex_entry_mark_deleted_from_disk(ie);
2877 break;
2878 case GOT_STATUS_DELETE:
2879 case GOT_STATUS_MISSING:
2880 err = (*a->progress_cb)(a->progress_arg,
2881 GOT_STATUS_DELETE, path1);
2882 if (err)
2883 goto done;
2884 if (ie)
2885 got_fileindex_entry_mark_deleted_from_disk(ie);
2886 break;
2887 case GOT_STATUS_ADD: {
2888 struct got_object_id *id;
2889 FILE *blob1_f;
2890 off_t blob1_size;
2892 * Delete the added file only if its content already
2893 * exists in the repository.
2895 err = got_object_blob_file_create(&id, &blob1_f,
2896 &blob1_size, path1);
2897 if (err)
2898 goto done;
2899 if (got_object_id_cmp(id, id1) == 0) {
2900 err = (*a->progress_cb)(a->progress_arg,
2901 GOT_STATUS_DELETE, path1);
2902 if (err)
2903 goto done;
2904 err = remove_ondisk_file(a->worktree->root_path,
2905 path1);
2906 if (err)
2907 goto done;
2908 if (ie)
2909 got_fileindex_entry_remove(a->fileindex,
2910 ie);
2911 } else {
2912 err = (*a->progress_cb)(a->progress_arg,
2913 GOT_STATUS_CANNOT_DELETE, path1);
2915 if (fclose(blob1_f) == EOF && err == NULL)
2916 err = got_error_from_errno("fclose");
2917 free(id);
2918 if (err)
2919 goto done;
2920 break;
2922 case GOT_STATUS_MODIFY:
2923 case GOT_STATUS_CONFLICT:
2924 err = (*a->progress_cb)(a->progress_arg,
2925 GOT_STATUS_CANNOT_DELETE, path1);
2926 if (err)
2927 goto done;
2928 break;
2929 case GOT_STATUS_OBSTRUCTED:
2930 err = (*a->progress_cb)(a->progress_arg, status, path1);
2931 if (err)
2932 goto done;
2933 break;
2934 default:
2935 break;
2937 } else if (blob2) {
2938 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2939 path2) == -1)
2940 return got_error_from_errno("asprintf");
2941 ie = got_fileindex_entry_get(a->fileindex, path2,
2942 strlen(path2));
2943 if (ie) {
2944 err = get_file_status(&status, &sb, ie, ondisk_path,
2945 -1, NULL, repo);
2946 if (err)
2947 goto done;
2948 if (status != GOT_STATUS_NO_CHANGE &&
2949 status != GOT_STATUS_MODIFY &&
2950 status != GOT_STATUS_CONFLICT &&
2951 status != GOT_STATUS_ADD) {
2952 err = (*a->progress_cb)(a->progress_arg,
2953 status, path2);
2954 goto done;
2956 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2957 char *link_target2;
2958 err = got_object_blob_read_to_str(&link_target2,
2959 blob2);
2960 if (err)
2961 goto done;
2962 err = merge_symlink(a->worktree, NULL,
2963 ondisk_path, path2, a->label_orig,
2964 link_target2, a->commit_id2, repo,
2965 a->progress_cb, a->progress_arg);
2966 free(link_target2);
2967 } else if (S_ISREG(sb.st_mode)) {
2968 err = merge_blob(&local_changes_subsumed,
2969 a->worktree, NULL, ondisk_path, path2,
2970 sb.st_mode, a->label_orig, blob2,
2971 a->commit_id2, repo, a->progress_cb,
2972 a->progress_arg);
2973 } else {
2974 err = got_error_path(ondisk_path,
2975 GOT_ERR_FILE_OBSTRUCTED);
2977 if (err)
2978 goto done;
2979 if (status == GOT_STATUS_DELETE) {
2980 err = got_fileindex_entry_update(ie,
2981 a->worktree->root_fd, path2, blob2->id.sha1,
2982 a->worktree->base_commit_id->sha1, 0);
2983 if (err)
2984 goto done;
2986 } else {
2987 int is_bad_symlink = 0;
2988 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2989 if (S_ISLNK(mode2)) {
2990 err = install_symlink(&is_bad_symlink,
2991 a->worktree, ondisk_path, path2, blob2, 0,
2992 0, 1, a->allow_bad_symlinks, repo,
2993 a->progress_cb, a->progress_arg);
2994 } else {
2995 err = install_blob(a->worktree, ondisk_path, path2,
2996 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2997 a->progress_cb, a->progress_arg);
2999 if (err)
3000 goto done;
3001 err = got_fileindex_entry_alloc(&ie, path2);
3002 if (err)
3003 goto done;
3004 err = got_fileindex_entry_update(ie,
3005 a->worktree->root_fd, path2, NULL, NULL, 1);
3006 if (err) {
3007 got_fileindex_entry_free(ie);
3008 goto done;
3010 err = got_fileindex_entry_add(a->fileindex, ie);
3011 if (err) {
3012 got_fileindex_entry_free(ie);
3013 goto done;
3015 if (is_bad_symlink) {
3016 got_fileindex_entry_filetype_set(ie,
3017 GOT_FILEIDX_MODE_BAD_SYMLINK);
3021 done:
3022 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3023 err = got_error_from_errno("fclose");
3024 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3025 err = got_error_from_errno("fclose");
3026 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3027 err = got_error_from_errno("fclose");
3028 free(id_str);
3029 free(label_deriv2);
3030 free(ondisk_path);
3031 return err;
3034 static const struct got_error *
3035 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3037 struct got_worktree *worktree = arg;
3039 /* Reject merges into a work tree with mixed base commits. */
3040 if (got_fileindex_entry_has_commit(ie) &&
3041 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3042 SHA1_DIGEST_LENGTH) != 0)
3043 return got_error(GOT_ERR_MIXED_COMMITS);
3045 return NULL;
3048 struct check_merge_conflicts_arg {
3049 struct got_worktree *worktree;
3050 struct got_fileindex *fileindex;
3051 struct got_repository *repo;
3054 static const struct got_error *
3055 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3056 struct got_blob_object *blob2, struct got_object_id *id1,
3057 struct got_object_id *id2, const char *path1, const char *path2,
3058 mode_t mode1, mode_t mode2, struct got_repository *repo)
3060 const struct got_error *err = NULL;
3061 struct check_merge_conflicts_arg *a = arg;
3062 unsigned char status;
3063 struct stat sb;
3064 struct got_fileindex_entry *ie;
3065 const char *path = path2 ? path2 : path1;
3066 struct got_object_id *id = id2 ? id2 : id1;
3067 char *ondisk_path;
3069 if (id == NULL)
3070 return NULL;
3072 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3073 if (ie == NULL)
3074 return NULL;
3076 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3077 == -1)
3078 return got_error_from_errno("asprintf");
3080 /* Reject merges into a work tree with conflicted files. */
3081 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3082 free(ondisk_path);
3083 if (err)
3084 return err;
3085 if (status == GOT_STATUS_CONFLICT)
3086 return got_error(GOT_ERR_CONFLICTS);
3088 return NULL;
3091 static const struct got_error *
3092 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3093 const char *fileindex_path, struct got_object_id *commit_id1,
3094 struct got_object_id *commit_id2, struct got_repository *repo,
3095 got_worktree_checkout_cb progress_cb, void *progress_arg,
3096 got_cancel_cb cancel_cb, void *cancel_arg)
3098 const struct got_error *err = NULL, *sync_err;
3099 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3100 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3101 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3102 struct check_merge_conflicts_arg cmc_arg;
3103 struct merge_file_cb_arg arg;
3104 char *label_orig = NULL;
3106 if (commit_id1) {
3107 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3108 if (err)
3109 goto done;
3110 err = got_object_id_by_path(&tree_id1, repo, commit1,
3111 worktree->path_prefix);
3112 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3113 goto done;
3115 if (tree_id1) {
3116 char *id_str;
3118 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3119 if (err)
3120 goto done;
3122 err = got_object_id_str(&id_str, commit_id1);
3123 if (err)
3124 goto done;
3126 if (asprintf(&label_orig, "%s: commit %s",
3127 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3128 err = got_error_from_errno("asprintf");
3129 free(id_str);
3130 goto done;
3132 free(id_str);
3135 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3136 if (err)
3137 goto done;
3139 err = got_object_id_by_path(&tree_id2, repo, commit2,
3140 worktree->path_prefix);
3141 if (err)
3142 goto done;
3144 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3145 if (err)
3146 goto done;
3148 cmc_arg.worktree = worktree;
3149 cmc_arg.fileindex = fileindex;
3150 cmc_arg.repo = repo;
3151 err = got_diff_tree(tree1, tree2, "", "", repo,
3152 check_merge_conflicts, &cmc_arg, 0);
3153 if (err)
3154 goto done;
3156 arg.worktree = worktree;
3157 arg.fileindex = fileindex;
3158 arg.progress_cb = progress_cb;
3159 arg.progress_arg = progress_arg;
3160 arg.cancel_cb = cancel_cb;
3161 arg.cancel_arg = cancel_arg;
3162 arg.label_orig = label_orig;
3163 arg.commit_id2 = commit_id2;
3164 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3165 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3166 sync_err = sync_fileindex(fileindex, fileindex_path);
3167 if (sync_err && err == NULL)
3168 err = sync_err;
3169 done:
3170 if (commit1)
3171 got_object_commit_close(commit1);
3172 if (commit2)
3173 got_object_commit_close(commit2);
3174 if (tree1)
3175 got_object_tree_close(tree1);
3176 if (tree2)
3177 got_object_tree_close(tree2);
3178 free(label_orig);
3179 return err;
3182 const struct got_error *
3183 got_worktree_merge_files(struct got_worktree *worktree,
3184 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3185 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3186 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3188 const struct got_error *err, *unlockerr;
3189 char *fileindex_path = NULL;
3190 struct got_fileindex *fileindex = NULL;
3192 err = lock_worktree(worktree, LOCK_EX);
3193 if (err)
3194 return err;
3196 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3197 if (err)
3198 goto done;
3200 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3201 worktree);
3202 if (err)
3203 goto done;
3205 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3206 commit_id2, repo, progress_cb, progress_arg,
3207 cancel_cb, cancel_arg);
3208 done:
3209 if (fileindex)
3210 got_fileindex_free(fileindex);
3211 free(fileindex_path);
3212 unlockerr = lock_worktree(worktree, LOCK_SH);
3213 if (unlockerr && err == NULL)
3214 err = unlockerr;
3215 return err;
3218 struct diff_dir_cb_arg {
3219 struct got_fileindex *fileindex;
3220 struct got_worktree *worktree;
3221 const char *status_path;
3222 size_t status_path_len;
3223 struct got_repository *repo;
3224 got_worktree_status_cb status_cb;
3225 void *status_arg;
3226 got_cancel_cb cancel_cb;
3227 void *cancel_arg;
3228 /* A pathlist containing per-directory pathlists of ignore patterns. */
3229 struct got_pathlist_head *ignores;
3230 int report_unchanged;
3231 int no_ignores;
3234 static const struct got_error *
3235 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3236 int dirfd, const char *de_name,
3237 got_worktree_status_cb status_cb, void *status_arg,
3238 struct got_repository *repo, int report_unchanged)
3240 const struct got_error *err = NULL;
3241 unsigned char status = GOT_STATUS_NO_CHANGE;
3242 unsigned char staged_status = get_staged_status(ie);
3243 struct stat sb;
3244 struct got_object_id blob_id, commit_id, staged_blob_id;
3245 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3246 struct got_object_id *staged_blob_idp = NULL;
3248 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3249 if (err)
3250 return err;
3252 if (status == GOT_STATUS_NO_CHANGE &&
3253 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3254 return NULL;
3256 if (got_fileindex_entry_has_blob(ie)) {
3257 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3258 blob_idp = &blob_id;
3260 if (got_fileindex_entry_has_commit(ie)) {
3261 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3262 commit_idp = &commit_id;
3264 if (staged_status == GOT_STATUS_ADD ||
3265 staged_status == GOT_STATUS_MODIFY) {
3266 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3267 SHA1_DIGEST_LENGTH);
3268 staged_blob_idp = &staged_blob_id;
3271 return (*status_cb)(status_arg, status, staged_status,
3272 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3275 static const struct got_error *
3276 status_old_new(void *arg, struct got_fileindex_entry *ie,
3277 struct dirent *de, const char *parent_path, int dirfd)
3279 const struct got_error *err = NULL;
3280 struct diff_dir_cb_arg *a = arg;
3281 char *abspath;
3283 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3284 return got_error(GOT_ERR_CANCELLED);
3286 if (got_path_cmp(parent_path, a->status_path,
3287 strlen(parent_path), a->status_path_len) != 0 &&
3288 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3289 return NULL;
3291 if (parent_path[0]) {
3292 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3293 parent_path, de->d_name) == -1)
3294 return got_error_from_errno("asprintf");
3295 } else {
3296 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3297 de->d_name) == -1)
3298 return got_error_from_errno("asprintf");
3301 err = report_file_status(ie, abspath, dirfd, de->d_name,
3302 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3303 free(abspath);
3304 return err;
3307 static const struct got_error *
3308 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3310 struct diff_dir_cb_arg *a = arg;
3311 struct got_object_id blob_id, commit_id;
3312 unsigned char status;
3314 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3315 return got_error(GOT_ERR_CANCELLED);
3317 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3318 return NULL;
3320 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3321 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3322 if (got_fileindex_entry_has_file_on_disk(ie))
3323 status = GOT_STATUS_MISSING;
3324 else
3325 status = GOT_STATUS_DELETE;
3326 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3327 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3330 void
3331 free_ignorelist(struct got_pathlist_head *ignorelist)
3333 struct got_pathlist_entry *pe;
3335 TAILQ_FOREACH(pe, ignorelist, entry)
3336 free((char *)pe->path);
3337 got_pathlist_free(ignorelist);
3340 void
3341 free_ignores(struct got_pathlist_head *ignores)
3343 struct got_pathlist_entry *pe;
3345 TAILQ_FOREACH(pe, ignores, entry) {
3346 struct got_pathlist_head *ignorelist = pe->data;
3347 free_ignorelist(ignorelist);
3348 free((char *)pe->path);
3350 got_pathlist_free(ignores);
3353 static const struct got_error *
3354 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3356 const struct got_error *err = NULL;
3357 struct got_pathlist_entry *pe = NULL;
3358 struct got_pathlist_head *ignorelist;
3359 char *line = NULL, *pattern, *dirpath = NULL;
3360 size_t linesize = 0;
3361 ssize_t linelen;
3363 ignorelist = calloc(1, sizeof(*ignorelist));
3364 if (ignorelist == NULL)
3365 return got_error_from_errno("calloc");
3366 TAILQ_INIT(ignorelist);
3368 while ((linelen = getline(&line, &linesize, f)) != -1) {
3369 if (linelen > 0 && line[linelen - 1] == '\n')
3370 line[linelen - 1] = '\0';
3372 /* Git's ignores may contain comments. */
3373 if (line[0] == '#')
3374 continue;
3376 /* Git's negated patterns are not (yet?) supported. */
3377 if (line[0] == '!')
3378 continue;
3380 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3381 line) == -1) {
3382 err = got_error_from_errno("asprintf");
3383 goto done;
3385 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3386 if (err)
3387 goto done;
3389 if (ferror(f)) {
3390 err = got_error_from_errno("getline");
3391 goto done;
3394 dirpath = strdup(path);
3395 if (dirpath == NULL) {
3396 err = got_error_from_errno("strdup");
3397 goto done;
3399 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3400 done:
3401 free(line);
3402 if (err || pe == NULL) {
3403 free(dirpath);
3404 free_ignorelist(ignorelist);
3406 return err;
3409 int
3410 match_ignores(struct got_pathlist_head *ignores, const char *path)
3412 struct got_pathlist_entry *pe;
3414 /* Handle patterns which match in all directories. */
3415 TAILQ_FOREACH(pe, ignores, entry) {
3416 struct got_pathlist_head *ignorelist = pe->data;
3417 struct got_pathlist_entry *pi;
3419 TAILQ_FOREACH(pi, ignorelist, entry) {
3420 const char *p, *pattern = pi->path;
3422 if (strncmp(pattern, "**/", 3) != 0)
3423 continue;
3424 pattern += 3;
3425 p = path;
3426 while (*p) {
3427 if (fnmatch(pattern, p,
3428 FNM_PATHNAME | FNM_LEADING_DIR)) {
3429 /* Retry in next directory. */
3430 while (*p && *p != '/')
3431 p++;
3432 while (*p == '/')
3433 p++;
3434 continue;
3436 return 1;
3442 * The ignores pathlist contains ignore lists from children before
3443 * parents, so we can find the most specific ignorelist by walking
3444 * ignores backwards.
3446 pe = TAILQ_LAST(ignores, got_pathlist_head);
3447 while (pe) {
3448 if (got_path_is_child(path, pe->path, pe->path_len)) {
3449 struct got_pathlist_head *ignorelist = pe->data;
3450 struct got_pathlist_entry *pi;
3451 TAILQ_FOREACH(pi, ignorelist, entry) {
3452 const char *pattern = pi->path;
3453 int flags = FNM_LEADING_DIR;
3454 if (strstr(pattern, "/**/") == NULL)
3455 flags |= FNM_PATHNAME;
3456 if (fnmatch(pattern, path, flags))
3457 continue;
3458 return 1;
3461 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3464 return 0;
3467 static const struct got_error *
3468 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3469 const char *path, int dirfd, const char *ignores_filename)
3471 const struct got_error *err = NULL;
3472 char *ignorespath;
3473 int fd = -1;
3474 FILE *ignoresfile = NULL;
3476 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3477 path[0] ? "/" : "", ignores_filename) == -1)
3478 return got_error_from_errno("asprintf");
3480 if (dirfd != -1) {
3481 fd = openat(dirfd, ignores_filename,
3482 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3483 if (fd == -1) {
3484 if (errno != ENOENT && errno != EACCES)
3485 err = got_error_from_errno2("openat",
3486 ignorespath);
3487 } else {
3488 ignoresfile = fdopen(fd, "r");
3489 if (ignoresfile == NULL)
3490 err = got_error_from_errno2("fdopen",
3491 ignorespath);
3492 else {
3493 fd = -1;
3494 err = read_ignores(ignores, path, ignoresfile);
3497 } else {
3498 ignoresfile = fopen(ignorespath, "re");
3499 if (ignoresfile == NULL) {
3500 if (errno != ENOENT && errno != EACCES)
3501 err = got_error_from_errno2("fopen",
3502 ignorespath);
3503 } else
3504 err = read_ignores(ignores, path, ignoresfile);
3507 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3508 err = got_error_from_errno2("fclose", path);
3509 if (fd != -1 && close(fd) == -1 && err == NULL)
3510 err = got_error_from_errno2("close", path);
3511 free(ignorespath);
3512 return err;
3515 static const struct got_error *
3516 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3517 int dirfd)
3519 const struct got_error *err = NULL;
3520 struct diff_dir_cb_arg *a = arg;
3521 char *path = NULL;
3523 if (ignore != NULL)
3524 *ignore = 0;
3526 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3527 return got_error(GOT_ERR_CANCELLED);
3529 if (parent_path[0]) {
3530 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3531 return got_error_from_errno("asprintf");
3532 } else {
3533 path = de->d_name;
3536 if (de->d_type == DT_DIR) {
3537 if (!a->no_ignores && ignore != NULL &&
3538 match_ignores(a->ignores, path))
3539 *ignore = 1;
3540 } else if (!match_ignores(a->ignores, path) &&
3541 got_path_is_child(path, a->status_path, a->status_path_len))
3542 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3543 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3544 if (parent_path[0])
3545 free(path);
3546 return err;
3549 static const struct got_error *
3550 status_traverse(void *arg, const char *path, int dirfd)
3552 const struct got_error *err = NULL;
3553 struct diff_dir_cb_arg *a = arg;
3555 if (a->no_ignores)
3556 return NULL;
3558 err = add_ignores(a->ignores, a->worktree->root_path,
3559 path, dirfd, ".cvsignore");
3560 if (err)
3561 return err;
3563 err = add_ignores(a->ignores, a->worktree->root_path, path,
3564 dirfd, ".gitignore");
3566 return err;
3569 static const struct got_error *
3570 report_single_file_status(const char *path, const char *ondisk_path,
3571 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3572 void *status_arg, struct got_repository *repo, int report_unchanged,
3573 struct got_pathlist_head *ignores, int no_ignores)
3575 struct got_fileindex_entry *ie;
3576 struct stat sb;
3578 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3579 if (ie)
3580 return report_file_status(ie, ondisk_path, -1, NULL,
3581 status_cb, status_arg, repo, report_unchanged);
3583 if (lstat(ondisk_path, &sb) == -1) {
3584 if (errno != ENOENT)
3585 return got_error_from_errno2("lstat", ondisk_path);
3586 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3587 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3590 if (!no_ignores && match_ignores(ignores, path))
3591 return NULL;
3593 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3594 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3595 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3597 return NULL;
3600 static const struct got_error *
3601 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3602 const char *root_path, const char *path)
3604 const struct got_error *err;
3605 char *parent_path, *next_parent_path = NULL;
3607 err = add_ignores(ignores, root_path, "", -1,
3608 ".cvsignore");
3609 if (err)
3610 return err;
3612 err = add_ignores(ignores, root_path, "", -1,
3613 ".gitignore");
3614 if (err)
3615 return err;
3617 err = got_path_dirname(&parent_path, path);
3618 if (err) {
3619 if (err->code == GOT_ERR_BAD_PATH)
3620 return NULL; /* cannot traverse parent */
3621 return err;
3623 for (;;) {
3624 err = add_ignores(ignores, root_path, parent_path, -1,
3625 ".cvsignore");
3626 if (err)
3627 break;
3628 err = add_ignores(ignores, root_path, parent_path, -1,
3629 ".gitignore");
3630 if (err)
3631 break;
3632 err = got_path_dirname(&next_parent_path, parent_path);
3633 if (err) {
3634 if (err->code == GOT_ERR_BAD_PATH)
3635 err = NULL; /* traversed everything */
3636 break;
3638 if (got_path_is_root_dir(parent_path))
3639 break;
3640 free(parent_path);
3641 parent_path = next_parent_path;
3642 next_parent_path = NULL;
3645 free(parent_path);
3646 free(next_parent_path);
3647 return err;
3650 static const struct got_error *
3651 worktree_status(struct got_worktree *worktree, const char *path,
3652 struct got_fileindex *fileindex, struct got_repository *repo,
3653 got_worktree_status_cb status_cb, void *status_arg,
3654 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3655 int report_unchanged)
3657 const struct got_error *err = NULL;
3658 int fd = -1;
3659 struct got_fileindex_diff_dir_cb fdiff_cb;
3660 struct diff_dir_cb_arg arg;
3661 char *ondisk_path = NULL;
3662 struct got_pathlist_head ignores;
3663 struct got_fileindex_entry *ie;
3665 TAILQ_INIT(&ignores);
3667 if (asprintf(&ondisk_path, "%s%s%s",
3668 worktree->root_path, path[0] ? "/" : "", path) == -1)
3669 return got_error_from_errno("asprintf");
3671 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3672 if (ie) {
3673 err = report_single_file_status(path, ondisk_path,
3674 fileindex, status_cb, status_arg, repo,
3675 report_unchanged, &ignores, no_ignores);
3676 goto done;
3679 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3680 if (fd == -1) {
3681 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3682 !got_err_open_nofollow_on_symlink())
3683 err = got_error_from_errno2("open", ondisk_path);
3684 else {
3685 if (!no_ignores) {
3686 err = add_ignores_from_parent_paths(&ignores,
3687 worktree->root_path, ondisk_path);
3688 if (err)
3689 goto done;
3691 err = report_single_file_status(path, ondisk_path,
3692 fileindex, status_cb, status_arg, repo,
3693 report_unchanged, &ignores, no_ignores);
3695 } else {
3696 fdiff_cb.diff_old_new = status_old_new;
3697 fdiff_cb.diff_old = status_old;
3698 fdiff_cb.diff_new = status_new;
3699 fdiff_cb.diff_traverse = status_traverse;
3700 arg.fileindex = fileindex;
3701 arg.worktree = worktree;
3702 arg.status_path = path;
3703 arg.status_path_len = strlen(path);
3704 arg.repo = repo;
3705 arg.status_cb = status_cb;
3706 arg.status_arg = status_arg;
3707 arg.cancel_cb = cancel_cb;
3708 arg.cancel_arg = cancel_arg;
3709 arg.report_unchanged = report_unchanged;
3710 arg.no_ignores = no_ignores;
3711 if (!no_ignores) {
3712 err = add_ignores_from_parent_paths(&ignores,
3713 worktree->root_path, path);
3714 if (err)
3715 goto done;
3717 arg.ignores = &ignores;
3718 err = got_fileindex_diff_dir(fileindex, fd,
3719 worktree->root_path, path, repo, &fdiff_cb, &arg);
3721 done:
3722 free_ignores(&ignores);
3723 if (fd != -1 && close(fd) == -1 && err == NULL)
3724 err = got_error_from_errno("close");
3725 free(ondisk_path);
3726 return err;
3729 const struct got_error *
3730 got_worktree_status(struct got_worktree *worktree,
3731 struct got_pathlist_head *paths, struct got_repository *repo,
3732 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3733 got_cancel_cb cancel_cb, void *cancel_arg)
3735 const struct got_error *err = NULL;
3736 char *fileindex_path = NULL;
3737 struct got_fileindex *fileindex = NULL;
3738 struct got_pathlist_entry *pe;
3740 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3741 if (err)
3742 return err;
3744 TAILQ_FOREACH(pe, paths, entry) {
3745 err = worktree_status(worktree, pe->path, fileindex, repo,
3746 status_cb, status_arg, cancel_cb, cancel_arg,
3747 no_ignores, 0);
3748 if (err)
3749 break;
3751 free(fileindex_path);
3752 got_fileindex_free(fileindex);
3753 return err;
3756 const struct got_error *
3757 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3758 const char *arg)
3760 const struct got_error *err = NULL;
3761 char *resolved = NULL, *cwd = NULL, *path = NULL;
3762 size_t len;
3763 struct stat sb;
3764 char *abspath = NULL;
3765 char canonpath[PATH_MAX];
3767 *wt_path = NULL;
3769 cwd = getcwd(NULL, 0);
3770 if (cwd == NULL)
3771 return got_error_from_errno("getcwd");
3773 if (lstat(arg, &sb) == -1) {
3774 if (errno != ENOENT) {
3775 err = got_error_from_errno2("lstat", arg);
3776 goto done;
3778 sb.st_mode = 0;
3780 if (S_ISLNK(sb.st_mode)) {
3782 * We cannot use realpath(3) with symlinks since we want to
3783 * operate on the symlink itself.
3784 * But we can make the path absolute, assuming it is relative
3785 * to the current working directory, and then canonicalize it.
3787 if (!got_path_is_absolute(arg)) {
3788 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3789 err = got_error_from_errno("asprintf");
3790 goto done;
3794 err = got_canonpath(abspath ? abspath : arg, canonpath,
3795 sizeof(canonpath));
3796 if (err)
3797 goto done;
3798 resolved = strdup(canonpath);
3799 if (resolved == NULL) {
3800 err = got_error_from_errno("strdup");
3801 goto done;
3803 } else {
3804 resolved = realpath(arg, NULL);
3805 if (resolved == NULL) {
3806 if (errno != ENOENT) {
3807 err = got_error_from_errno2("realpath", arg);
3808 goto done;
3810 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3811 err = got_error_from_errno("asprintf");
3812 goto done;
3814 err = got_canonpath(abspath, canonpath,
3815 sizeof(canonpath));
3816 if (err)
3817 goto done;
3818 resolved = strdup(canonpath);
3819 if (resolved == NULL) {
3820 err = got_error_from_errno("strdup");
3821 goto done;
3826 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3827 strlen(got_worktree_get_root_path(worktree)))) {
3828 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3829 goto done;
3832 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3833 err = got_path_skip_common_ancestor(&path,
3834 got_worktree_get_root_path(worktree), resolved);
3835 if (err)
3836 goto done;
3837 } else {
3838 path = strdup("");
3839 if (path == NULL) {
3840 err = got_error_from_errno("strdup");
3841 goto done;
3845 /* XXX status walk can't deal with trailing slash! */
3846 len = strlen(path);
3847 while (len > 0 && path[len - 1] == '/') {
3848 path[len - 1] = '\0';
3849 len--;
3851 done:
3852 free(abspath);
3853 free(resolved);
3854 free(cwd);
3855 if (err == NULL)
3856 *wt_path = path;
3857 else
3858 free(path);
3859 return err;
3862 struct schedule_addition_args {
3863 struct got_worktree *worktree;
3864 struct got_fileindex *fileindex;
3865 got_worktree_checkout_cb progress_cb;
3866 void *progress_arg;
3867 struct got_repository *repo;
3870 static const struct got_error *
3871 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3872 const char *relpath, struct got_object_id *blob_id,
3873 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3874 int dirfd, const char *de_name)
3876 struct schedule_addition_args *a = arg;
3877 const struct got_error *err = NULL;
3878 struct got_fileindex_entry *ie;
3879 struct stat sb;
3880 char *ondisk_path;
3882 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3883 relpath) == -1)
3884 return got_error_from_errno("asprintf");
3886 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3887 if (ie) {
3888 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3889 de_name, a->repo);
3890 if (err)
3891 goto done;
3892 /* Re-adding an existing entry is a no-op. */
3893 if (status == GOT_STATUS_ADD)
3894 goto done;
3895 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3896 if (err)
3897 goto done;
3900 if (status != GOT_STATUS_UNVERSIONED) {
3901 if (status == GOT_STATUS_NONEXISTENT)
3902 err = got_error_set_errno(ENOENT, ondisk_path);
3903 else
3904 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3905 goto done;
3908 err = got_fileindex_entry_alloc(&ie, relpath);
3909 if (err)
3910 goto done;
3911 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3912 relpath, NULL, NULL, 1);
3913 if (err) {
3914 got_fileindex_entry_free(ie);
3915 goto done;
3917 err = got_fileindex_entry_add(a->fileindex, ie);
3918 if (err) {
3919 got_fileindex_entry_free(ie);
3920 goto done;
3922 done:
3923 free(ondisk_path);
3924 if (err)
3925 return err;
3926 if (status == GOT_STATUS_ADD)
3927 return NULL;
3928 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3931 const struct got_error *
3932 got_worktree_schedule_add(struct got_worktree *worktree,
3933 struct got_pathlist_head *paths,
3934 got_worktree_checkout_cb progress_cb, void *progress_arg,
3935 struct got_repository *repo, int no_ignores)
3937 struct got_fileindex *fileindex = NULL;
3938 char *fileindex_path = NULL;
3939 const struct got_error *err = NULL, *sync_err, *unlockerr;
3940 struct got_pathlist_entry *pe;
3941 struct schedule_addition_args saa;
3943 err = lock_worktree(worktree, LOCK_EX);
3944 if (err)
3945 return err;
3947 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3948 if (err)
3949 goto done;
3951 saa.worktree = worktree;
3952 saa.fileindex = fileindex;
3953 saa.progress_cb = progress_cb;
3954 saa.progress_arg = progress_arg;
3955 saa.repo = repo;
3957 TAILQ_FOREACH(pe, paths, entry) {
3958 err = worktree_status(worktree, pe->path, fileindex, repo,
3959 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3960 if (err)
3961 break;
3963 sync_err = sync_fileindex(fileindex, fileindex_path);
3964 if (sync_err && err == NULL)
3965 err = sync_err;
3966 done:
3967 free(fileindex_path);
3968 if (fileindex)
3969 got_fileindex_free(fileindex);
3970 unlockerr = lock_worktree(worktree, LOCK_SH);
3971 if (unlockerr && err == NULL)
3972 err = unlockerr;
3973 return err;
3976 struct schedule_deletion_args {
3977 struct got_worktree *worktree;
3978 struct got_fileindex *fileindex;
3979 got_worktree_delete_cb progress_cb;
3980 void *progress_arg;
3981 struct got_repository *repo;
3982 int delete_local_mods;
3983 int keep_on_disk;
3984 int ignore_missing_paths;
3985 const char *status_codes;
3988 static const struct got_error *
3989 schedule_for_deletion(void *arg, unsigned char status,
3990 unsigned char staged_status, const char *relpath,
3991 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3992 struct got_object_id *commit_id, int dirfd, const char *de_name)
3994 struct schedule_deletion_args *a = arg;
3995 const struct got_error *err = NULL;
3996 struct got_fileindex_entry *ie = NULL;
3997 struct stat sb;
3998 char *ondisk_path;
4000 if (status == GOT_STATUS_NONEXISTENT) {
4001 if (a->ignore_missing_paths)
4002 return NULL;
4003 return got_error_set_errno(ENOENT, relpath);
4006 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4007 if (ie == NULL)
4008 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4010 staged_status = get_staged_status(ie);
4011 if (staged_status != GOT_STATUS_NO_CHANGE) {
4012 if (staged_status == GOT_STATUS_DELETE)
4013 return NULL;
4014 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4017 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4018 relpath) == -1)
4019 return got_error_from_errno("asprintf");
4021 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4022 a->repo);
4023 if (err)
4024 goto done;
4026 if (a->status_codes) {
4027 size_t ncodes = strlen(a->status_codes);
4028 int i;
4029 for (i = 0; i < ncodes ; i++) {
4030 if (status == a->status_codes[i])
4031 break;
4033 if (i == ncodes) {
4034 /* Do not delete files in non-matching status. */
4035 free(ondisk_path);
4036 return NULL;
4038 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4039 a->status_codes[i] != GOT_STATUS_MISSING) {
4040 static char msg[64];
4041 snprintf(msg, sizeof(msg),
4042 "invalid status code '%c'", a->status_codes[i]);
4043 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4044 goto done;
4048 if (status != GOT_STATUS_NO_CHANGE) {
4049 if (status == GOT_STATUS_DELETE)
4050 goto done;
4051 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4052 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4053 goto done;
4055 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4056 err = got_error_set_errno(ENOENT, relpath);
4057 goto done;
4059 if (status != GOT_STATUS_MODIFY &&
4060 status != GOT_STATUS_MISSING) {
4061 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4062 goto done;
4066 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4067 size_t root_len;
4069 if (dirfd != -1) {
4070 if (unlinkat(dirfd, de_name, 0) != 0) {
4071 err = got_error_from_errno2("unlinkat",
4072 ondisk_path);
4073 goto done;
4075 } else if (unlink(ondisk_path) != 0) {
4076 err = got_error_from_errno2("unlink", ondisk_path);
4077 goto done;
4080 root_len = strlen(a->worktree->root_path);
4081 do {
4082 char *parent;
4083 err = got_path_dirname(&parent, ondisk_path);
4084 if (err)
4085 goto done;
4086 free(ondisk_path);
4087 ondisk_path = parent;
4088 if (rmdir(ondisk_path) == -1) {
4089 if (errno != ENOTEMPTY)
4090 err = got_error_from_errno2("rmdir",
4091 ondisk_path);
4092 break;
4094 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4095 strlen(ondisk_path), root_len) != 0);
4098 got_fileindex_entry_mark_deleted_from_disk(ie);
4099 done:
4100 free(ondisk_path);
4101 if (err)
4102 return err;
4103 if (status == GOT_STATUS_DELETE)
4104 return NULL;
4105 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4106 staged_status, relpath);
4109 const struct got_error *
4110 got_worktree_schedule_delete(struct got_worktree *worktree,
4111 struct got_pathlist_head *paths, int delete_local_mods,
4112 const char *status_codes,
4113 got_worktree_delete_cb progress_cb, void *progress_arg,
4114 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4116 struct got_fileindex *fileindex = NULL;
4117 char *fileindex_path = NULL;
4118 const struct got_error *err = NULL, *sync_err, *unlockerr;
4119 struct got_pathlist_entry *pe;
4120 struct schedule_deletion_args sda;
4122 err = lock_worktree(worktree, LOCK_EX);
4123 if (err)
4124 return err;
4126 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4127 if (err)
4128 goto done;
4130 sda.worktree = worktree;
4131 sda.fileindex = fileindex;
4132 sda.progress_cb = progress_cb;
4133 sda.progress_arg = progress_arg;
4134 sda.repo = repo;
4135 sda.delete_local_mods = delete_local_mods;
4136 sda.keep_on_disk = keep_on_disk;
4137 sda.ignore_missing_paths = ignore_missing_paths;
4138 sda.status_codes = status_codes;
4140 TAILQ_FOREACH(pe, paths, entry) {
4141 err = worktree_status(worktree, pe->path, fileindex, repo,
4142 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4143 if (err)
4144 break;
4146 sync_err = sync_fileindex(fileindex, fileindex_path);
4147 if (sync_err && err == NULL)
4148 err = sync_err;
4149 done:
4150 free(fileindex_path);
4151 if (fileindex)
4152 got_fileindex_free(fileindex);
4153 unlockerr = lock_worktree(worktree, LOCK_SH);
4154 if (unlockerr && err == NULL)
4155 err = unlockerr;
4156 return err;
4159 static const struct got_error *
4160 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4162 const struct got_error *err = NULL;
4163 char *line = NULL;
4164 size_t linesize = 0, n;
4165 ssize_t linelen;
4167 linelen = getline(&line, &linesize, infile);
4168 if (linelen == -1) {
4169 if (ferror(infile)) {
4170 err = got_error_from_errno("getline");
4171 goto done;
4173 return NULL;
4175 if (outfile) {
4176 n = fwrite(line, 1, linelen, outfile);
4177 if (n != linelen) {
4178 err = got_ferror(outfile, GOT_ERR_IO);
4179 goto done;
4182 if (rejectfile) {
4183 n = fwrite(line, 1, linelen, rejectfile);
4184 if (n != linelen)
4185 err = got_ferror(outfile, GOT_ERR_IO);
4187 done:
4188 free(line);
4189 return err;
4192 static const struct got_error *
4193 skip_one_line(FILE *f)
4195 char *line = NULL;
4196 size_t linesize = 0;
4197 ssize_t linelen;
4199 linelen = getline(&line, &linesize, f);
4200 if (linelen == -1) {
4201 if (ferror(f))
4202 return got_error_from_errno("getline");
4203 return NULL;
4205 free(line);
4206 return NULL;
4209 static const struct got_error *
4210 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4211 int start_old, int end_old, int start_new, int end_new,
4212 FILE *outfile, FILE *rejectfile)
4214 const struct got_error *err;
4216 /* Copy old file's lines leading up to patch. */
4217 while (!feof(f1) && *line_cur1 < start_old) {
4218 err = copy_one_line(f1, outfile, NULL);
4219 if (err)
4220 return err;
4221 (*line_cur1)++;
4223 /* Skip new file's lines leading up to patch. */
4224 while (!feof(f2) && *line_cur2 < start_new) {
4225 if (rejectfile)
4226 err = copy_one_line(f2, NULL, rejectfile);
4227 else
4228 err = skip_one_line(f2);
4229 if (err)
4230 return err;
4231 (*line_cur2)++;
4233 /* Copy patched lines. */
4234 while (!feof(f2) && *line_cur2 <= end_new) {
4235 err = copy_one_line(f2, outfile, NULL);
4236 if (err)
4237 return err;
4238 (*line_cur2)++;
4240 /* Skip over old file's replaced lines. */
4241 while (!feof(f1) && *line_cur1 <= end_old) {
4242 if (rejectfile)
4243 err = copy_one_line(f1, NULL, rejectfile);
4244 else
4245 err = skip_one_line(f1);
4246 if (err)
4247 return err;
4248 (*line_cur1)++;
4251 return NULL;
4254 static const struct got_error *
4255 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4256 FILE *outfile, FILE *rejectfile)
4258 const struct got_error *err;
4260 if (outfile) {
4261 /* Copy old file's lines until EOF. */
4262 while (!feof(f1)) {
4263 err = copy_one_line(f1, outfile, NULL);
4264 if (err)
4265 return err;
4266 (*line_cur1)++;
4269 if (rejectfile) {
4270 /* Copy new file's lines until EOF. */
4271 while (!feof(f2)) {
4272 err = copy_one_line(f2, NULL, rejectfile);
4273 if (err)
4274 return err;
4275 (*line_cur2)++;
4279 return NULL;
4282 static const struct got_error *
4283 apply_or_reject_change(int *choice, int *nchunks_used,
4284 struct diff_result *diff_result, int n,
4285 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4286 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4287 got_worktree_patch_cb patch_cb, void *patch_arg)
4289 const struct got_error *err = NULL;
4290 struct diff_chunk_context cc = {};
4291 int start_old, end_old, start_new, end_new;
4292 FILE *hunkfile;
4293 struct diff_output_unidiff_state *diff_state;
4294 struct diff_input_info diff_info;
4295 int rc;
4297 *choice = GOT_PATCH_CHOICE_NONE;
4299 /* Get changed line numbers without context lines for copy_change(). */
4300 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4301 start_old = cc.left.start;
4302 end_old = cc.left.end;
4303 start_new = cc.right.start;
4304 end_new = cc.right.end;
4306 /* Get the same change with context lines for display. */
4307 memset(&cc, 0, sizeof(cc));
4308 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4310 memset(&diff_info, 0, sizeof(diff_info));
4311 diff_info.left_path = relpath;
4312 diff_info.right_path = relpath;
4314 diff_state = diff_output_unidiff_state_alloc();
4315 if (diff_state == NULL)
4316 return got_error_set_errno(ENOMEM,
4317 "diff_output_unidiff_state_alloc");
4319 hunkfile = got_opentemp();
4320 if (hunkfile == NULL) {
4321 err = got_error_from_errno("got_opentemp");
4322 goto done;
4325 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4326 diff_result, &cc);
4327 if (rc != DIFF_RC_OK) {
4328 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4329 goto done;
4332 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4333 err = got_ferror(hunkfile, GOT_ERR_IO);
4334 goto done;
4337 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4338 hunkfile, changeno, nchanges);
4339 if (err)
4340 goto done;
4342 switch (*choice) {
4343 case GOT_PATCH_CHOICE_YES:
4344 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4345 end_old, start_new, end_new, outfile, rejectfile);
4346 break;
4347 case GOT_PATCH_CHOICE_NO:
4348 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4349 end_old, start_new, end_new, rejectfile, outfile);
4350 break;
4351 case GOT_PATCH_CHOICE_QUIT:
4352 break;
4353 default:
4354 err = got_error(GOT_ERR_PATCH_CHOICE);
4355 break;
4357 done:
4358 diff_output_unidiff_state_free(diff_state);
4359 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4360 err = got_error_from_errno("fclose");
4361 return err;
4364 struct revert_file_args {
4365 struct got_worktree *worktree;
4366 struct got_fileindex *fileindex;
4367 got_worktree_checkout_cb progress_cb;
4368 void *progress_arg;
4369 got_worktree_patch_cb patch_cb;
4370 void *patch_arg;
4371 struct got_repository *repo;
4372 int unlink_added_files;
4375 static const struct got_error *
4376 create_patched_content(char **path_outfile, int reverse_patch,
4377 struct got_object_id *blob_id, const char *path2,
4378 int dirfd2, const char *de_name2,
4379 const char *relpath, struct got_repository *repo,
4380 got_worktree_patch_cb patch_cb, void *patch_arg)
4382 const struct got_error *err, *free_err;
4383 struct got_blob_object *blob = NULL;
4384 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4385 int fd2 = -1;
4386 char link_target[PATH_MAX];
4387 ssize_t link_len = 0;
4388 char *path1 = NULL, *id_str = NULL;
4389 struct stat sb2;
4390 struct got_diffreg_result *diffreg_result = NULL;
4391 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4392 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4394 *path_outfile = NULL;
4396 err = got_object_id_str(&id_str, blob_id);
4397 if (err)
4398 return err;
4400 if (dirfd2 != -1) {
4401 fd2 = openat(dirfd2, de_name2,
4402 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4403 if (fd2 == -1) {
4404 if (!got_err_open_nofollow_on_symlink()) {
4405 err = got_error_from_errno2("openat", path2);
4406 goto done;
4408 link_len = readlinkat(dirfd2, de_name2,
4409 link_target, sizeof(link_target));
4410 if (link_len == -1) {
4411 return got_error_from_errno2("readlinkat",
4412 path2);
4414 sb2.st_mode = S_IFLNK;
4415 sb2.st_size = link_len;
4417 } else {
4418 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4419 if (fd2 == -1) {
4420 if (!got_err_open_nofollow_on_symlink()) {
4421 err = got_error_from_errno2("open", path2);
4422 goto done;
4424 link_len = readlink(path2, link_target,
4425 sizeof(link_target));
4426 if (link_len == -1)
4427 return got_error_from_errno2("readlink", path2);
4428 sb2.st_mode = S_IFLNK;
4429 sb2.st_size = link_len;
4432 if (fd2 != -1) {
4433 if (fstat(fd2, &sb2) == -1) {
4434 err = got_error_from_errno2("fstat", path2);
4435 goto done;
4438 f2 = fdopen(fd2, "r");
4439 if (f2 == NULL) {
4440 err = got_error_from_errno2("fdopen", path2);
4441 goto done;
4443 fd2 = -1;
4444 } else {
4445 size_t n;
4446 f2 = got_opentemp();
4447 if (f2 == NULL) {
4448 err = got_error_from_errno2("got_opentemp", path2);
4449 goto done;
4451 n = fwrite(link_target, 1, link_len, f2);
4452 if (n != link_len) {
4453 err = got_ferror(f2, GOT_ERR_IO);
4454 goto done;
4456 if (fflush(f2) == EOF) {
4457 err = got_error_from_errno("fflush");
4458 goto done;
4460 rewind(f2);
4463 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4464 if (err)
4465 goto done;
4467 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4468 if (err)
4469 goto done;
4471 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4472 if (err)
4473 goto done;
4475 err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4476 NULL);
4477 if (err)
4478 goto done;
4480 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4481 if (err)
4482 goto done;
4484 if (fseek(f1, 0L, SEEK_SET) == -1)
4485 return got_ferror(f1, GOT_ERR_IO);
4486 if (fseek(f2, 0L, SEEK_SET) == -1)
4487 return got_ferror(f2, GOT_ERR_IO);
4489 /* Count the number of actual changes in the diff result. */
4490 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4491 struct diff_chunk_context cc = {};
4492 diff_chunk_context_load_change(&cc, &nchunks_used,
4493 diffreg_result->result, n, 0);
4494 nchanges++;
4496 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4497 int choice;
4498 err = apply_or_reject_change(&choice, &nchunks_used,
4499 diffreg_result->result, n, relpath, f1, f2,
4500 &line_cur1, &line_cur2,
4501 reverse_patch ? NULL : outfile,
4502 reverse_patch ? outfile : NULL,
4503 ++i, nchanges, patch_cb, patch_arg);
4504 if (err)
4505 goto done;
4506 if (choice == GOT_PATCH_CHOICE_YES)
4507 have_content = 1;
4508 else if (choice == GOT_PATCH_CHOICE_QUIT)
4509 break;
4511 if (have_content) {
4512 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4513 reverse_patch ? NULL : outfile,
4514 reverse_patch ? outfile : NULL);
4515 if (err)
4516 goto done;
4518 if (!S_ISLNK(sb2.st_mode)) {
4519 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4520 err = got_error_from_errno2("fchmod", path2);
4521 goto done;
4525 done:
4526 free(id_str);
4527 if (blob)
4528 got_object_blob_close(blob);
4529 free_err = got_diffreg_result_free(diffreg_result);
4530 if (err == NULL)
4531 err = free_err;
4532 if (f1 && fclose(f1) == EOF && err == NULL)
4533 err = got_error_from_errno2("fclose", path1);
4534 if (f2 && fclose(f2) == EOF && err == NULL)
4535 err = got_error_from_errno2("fclose", path2);
4536 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4537 err = got_error_from_errno2("close", path2);
4538 if (outfile && fclose(outfile) == EOF && err == NULL)
4539 err = got_error_from_errno2("fclose", *path_outfile);
4540 if (path1 && unlink(path1) == -1 && err == NULL)
4541 err = got_error_from_errno2("unlink", path1);
4542 if (err || !have_content) {
4543 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4544 err = got_error_from_errno2("unlink", *path_outfile);
4545 free(*path_outfile);
4546 *path_outfile = NULL;
4548 free(path1);
4549 return err;
4552 static const struct got_error *
4553 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4554 const char *relpath, struct got_object_id *blob_id,
4555 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4556 int dirfd, const char *de_name)
4558 struct revert_file_args *a = arg;
4559 const struct got_error *err = NULL;
4560 char *parent_path = NULL;
4561 struct got_fileindex_entry *ie;
4562 struct got_commit_object *base_commit = NULL;
4563 struct got_tree_object *tree = NULL;
4564 struct got_object_id *tree_id = NULL;
4565 const struct got_tree_entry *te = NULL;
4566 char *tree_path = NULL, *te_name;
4567 char *ondisk_path = NULL, *path_content = NULL;
4568 struct got_blob_object *blob = NULL;
4570 /* Reverting a staged deletion is a no-op. */
4571 if (status == GOT_STATUS_DELETE &&
4572 staged_status != GOT_STATUS_NO_CHANGE)
4573 return NULL;
4575 if (status == GOT_STATUS_UNVERSIONED)
4576 return (*a->progress_cb)(a->progress_arg,
4577 GOT_STATUS_UNVERSIONED, relpath);
4579 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4580 if (ie == NULL)
4581 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4583 /* Construct in-repository path of tree which contains this blob. */
4584 err = got_path_dirname(&parent_path, ie->path);
4585 if (err) {
4586 if (err->code != GOT_ERR_BAD_PATH)
4587 goto done;
4588 parent_path = strdup("/");
4589 if (parent_path == NULL) {
4590 err = got_error_from_errno("strdup");
4591 goto done;
4594 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4595 tree_path = strdup(parent_path);
4596 if (tree_path == NULL) {
4597 err = got_error_from_errno("strdup");
4598 goto done;
4600 } else {
4601 if (got_path_is_root_dir(parent_path)) {
4602 tree_path = strdup(a->worktree->path_prefix);
4603 if (tree_path == NULL) {
4604 err = got_error_from_errno("strdup");
4605 goto done;
4607 } else {
4608 if (asprintf(&tree_path, "%s/%s",
4609 a->worktree->path_prefix, parent_path) == -1) {
4610 err = got_error_from_errno("asprintf");
4611 goto done;
4616 err = got_object_open_as_commit(&base_commit, a->repo,
4617 a->worktree->base_commit_id);
4618 if (err)
4619 goto done;
4621 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4622 if (err) {
4623 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4624 (status == GOT_STATUS_ADD ||
4625 staged_status == GOT_STATUS_ADD)))
4626 goto done;
4627 } else {
4628 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4629 if (err)
4630 goto done;
4632 err = got_path_basename(&te_name, ie->path);
4633 if (err)
4634 goto done;
4636 te = got_object_tree_find_entry(tree, te_name);
4637 free(te_name);
4638 if (te == NULL && status != GOT_STATUS_ADD &&
4639 staged_status != GOT_STATUS_ADD) {
4640 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4641 goto done;
4645 switch (status) {
4646 case GOT_STATUS_ADD:
4647 if (a->patch_cb) {
4648 int choice = GOT_PATCH_CHOICE_NONE;
4649 err = (*a->patch_cb)(&choice, a->patch_arg,
4650 status, ie->path, NULL, 1, 1);
4651 if (err)
4652 goto done;
4653 if (choice != GOT_PATCH_CHOICE_YES)
4654 break;
4656 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4657 ie->path);
4658 if (err)
4659 goto done;
4660 got_fileindex_entry_remove(a->fileindex, ie);
4661 if (a->unlink_added_files) {
4662 if (asprintf(&ondisk_path, "%s/%s",
4663 got_worktree_get_root_path(a->worktree),
4664 relpath) == -1) {
4665 err = got_error_from_errno("asprintf");
4666 goto done;
4668 if (unlink(ondisk_path) == -1) {
4669 err = got_error_from_errno2("unlink",
4670 ondisk_path);
4671 break;
4674 break;
4675 case GOT_STATUS_DELETE:
4676 if (a->patch_cb) {
4677 int choice = GOT_PATCH_CHOICE_NONE;
4678 err = (*a->patch_cb)(&choice, a->patch_arg,
4679 status, ie->path, NULL, 1, 1);
4680 if (err)
4681 goto done;
4682 if (choice != GOT_PATCH_CHOICE_YES)
4683 break;
4685 /* fall through */
4686 case GOT_STATUS_MODIFY:
4687 case GOT_STATUS_MODE_CHANGE:
4688 case GOT_STATUS_CONFLICT:
4689 case GOT_STATUS_MISSING: {
4690 struct got_object_id id;
4691 if (staged_status == GOT_STATUS_ADD ||
4692 staged_status == GOT_STATUS_MODIFY) {
4693 memcpy(id.sha1, ie->staged_blob_sha1,
4694 SHA1_DIGEST_LENGTH);
4695 } else
4696 memcpy(id.sha1, ie->blob_sha1,
4697 SHA1_DIGEST_LENGTH);
4698 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4699 if (err)
4700 goto done;
4702 if (asprintf(&ondisk_path, "%s/%s",
4703 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4704 err = got_error_from_errno("asprintf");
4705 goto done;
4708 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4709 status == GOT_STATUS_CONFLICT)) {
4710 int is_bad_symlink = 0;
4711 err = create_patched_content(&path_content, 1, &id,
4712 ondisk_path, dirfd, de_name, ie->path, a->repo,
4713 a->patch_cb, a->patch_arg);
4714 if (err || path_content == NULL)
4715 break;
4716 if (te && S_ISLNK(te->mode)) {
4717 if (unlink(path_content) == -1) {
4718 err = got_error_from_errno2("unlink",
4719 path_content);
4720 break;
4722 err = install_symlink(&is_bad_symlink,
4723 a->worktree, ondisk_path, ie->path,
4724 blob, 0, 1, 0, 0, a->repo,
4725 a->progress_cb, a->progress_arg);
4726 } else {
4727 if (rename(path_content, ondisk_path) == -1) {
4728 err = got_error_from_errno3("rename",
4729 path_content, ondisk_path);
4730 goto done;
4733 } else {
4734 int is_bad_symlink = 0;
4735 if (te && S_ISLNK(te->mode)) {
4736 err = install_symlink(&is_bad_symlink,
4737 a->worktree, ondisk_path, ie->path,
4738 blob, 0, 1, 0, 0, a->repo,
4739 a->progress_cb, a->progress_arg);
4740 } else {
4741 err = install_blob(a->worktree, ondisk_path,
4742 ie->path,
4743 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4744 got_fileindex_perms_to_st(ie), blob,
4745 0, 1, 0, 0, a->repo,
4746 a->progress_cb, a->progress_arg);
4748 if (err)
4749 goto done;
4750 if (status == GOT_STATUS_DELETE ||
4751 status == GOT_STATUS_MODE_CHANGE) {
4752 err = got_fileindex_entry_update(ie,
4753 a->worktree->root_fd, relpath,
4754 blob->id.sha1,
4755 a->worktree->base_commit_id->sha1, 1);
4756 if (err)
4757 goto done;
4759 if (is_bad_symlink) {
4760 got_fileindex_entry_filetype_set(ie,
4761 GOT_FILEIDX_MODE_BAD_SYMLINK);
4764 break;
4766 default:
4767 break;
4769 done:
4770 free(ondisk_path);
4771 free(path_content);
4772 free(parent_path);
4773 free(tree_path);
4774 if (blob)
4775 got_object_blob_close(blob);
4776 if (tree)
4777 got_object_tree_close(tree);
4778 free(tree_id);
4779 if (base_commit)
4780 got_object_commit_close(base_commit);
4781 return err;
4784 const struct got_error *
4785 got_worktree_revert(struct got_worktree *worktree,
4786 struct got_pathlist_head *paths,
4787 got_worktree_checkout_cb progress_cb, void *progress_arg,
4788 got_worktree_patch_cb patch_cb, void *patch_arg,
4789 struct got_repository *repo)
4791 struct got_fileindex *fileindex = NULL;
4792 char *fileindex_path = NULL;
4793 const struct got_error *err = NULL, *unlockerr = NULL;
4794 const struct got_error *sync_err = NULL;
4795 struct got_pathlist_entry *pe;
4796 struct revert_file_args rfa;
4798 err = lock_worktree(worktree, LOCK_EX);
4799 if (err)
4800 return err;
4802 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4803 if (err)
4804 goto done;
4806 rfa.worktree = worktree;
4807 rfa.fileindex = fileindex;
4808 rfa.progress_cb = progress_cb;
4809 rfa.progress_arg = progress_arg;
4810 rfa.patch_cb = patch_cb;
4811 rfa.patch_arg = patch_arg;
4812 rfa.repo = repo;
4813 rfa.unlink_added_files = 0;
4814 TAILQ_FOREACH(pe, paths, entry) {
4815 err = worktree_status(worktree, pe->path, fileindex, repo,
4816 revert_file, &rfa, NULL, NULL, 1, 0);
4817 if (err)
4818 break;
4820 sync_err = sync_fileindex(fileindex, fileindex_path);
4821 if (sync_err && err == NULL)
4822 err = sync_err;
4823 done:
4824 free(fileindex_path);
4825 if (fileindex)
4826 got_fileindex_free(fileindex);
4827 unlockerr = lock_worktree(worktree, LOCK_SH);
4828 if (unlockerr && err == NULL)
4829 err = unlockerr;
4830 return err;
4833 static void
4834 free_commitable(struct got_commitable *ct)
4836 free(ct->path);
4837 free(ct->in_repo_path);
4838 free(ct->ondisk_path);
4839 free(ct->blob_id);
4840 free(ct->base_blob_id);
4841 free(ct->staged_blob_id);
4842 free(ct->base_commit_id);
4843 free(ct);
4846 struct collect_commitables_arg {
4847 struct got_pathlist_head *commitable_paths;
4848 struct got_repository *repo;
4849 struct got_worktree *worktree;
4850 struct got_fileindex *fileindex;
4851 int have_staged_files;
4852 int allow_bad_symlinks;
4855 static const struct got_error *
4856 collect_commitables(void *arg, unsigned char status,
4857 unsigned char staged_status, const char *relpath,
4858 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4859 struct got_object_id *commit_id, int dirfd, const char *de_name)
4861 struct collect_commitables_arg *a = arg;
4862 const struct got_error *err = NULL;
4863 struct got_commitable *ct = NULL;
4864 struct got_pathlist_entry *new = NULL;
4865 char *parent_path = NULL, *path = NULL;
4866 struct stat sb;
4868 if (a->have_staged_files) {
4869 if (staged_status != GOT_STATUS_MODIFY &&
4870 staged_status != GOT_STATUS_ADD &&
4871 staged_status != GOT_STATUS_DELETE)
4872 return NULL;
4873 } else {
4874 if (status == GOT_STATUS_CONFLICT)
4875 return got_error(GOT_ERR_COMMIT_CONFLICT);
4877 if (status != GOT_STATUS_MODIFY &&
4878 status != GOT_STATUS_MODE_CHANGE &&
4879 status != GOT_STATUS_ADD &&
4880 status != GOT_STATUS_DELETE)
4881 return NULL;
4884 if (asprintf(&path, "/%s", relpath) == -1) {
4885 err = got_error_from_errno("asprintf");
4886 goto done;
4888 if (strcmp(path, "/") == 0) {
4889 parent_path = strdup("");
4890 if (parent_path == NULL)
4891 return got_error_from_errno("strdup");
4892 } else {
4893 err = got_path_dirname(&parent_path, path);
4894 if (err)
4895 return err;
4898 ct = calloc(1, sizeof(*ct));
4899 if (ct == NULL) {
4900 err = got_error_from_errno("calloc");
4901 goto done;
4904 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4905 relpath) == -1) {
4906 err = got_error_from_errno("asprintf");
4907 goto done;
4910 if (staged_status == GOT_STATUS_ADD ||
4911 staged_status == GOT_STATUS_MODIFY) {
4912 struct got_fileindex_entry *ie;
4913 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4914 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4915 case GOT_FILEIDX_MODE_REGULAR_FILE:
4916 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4917 ct->mode = S_IFREG;
4918 break;
4919 case GOT_FILEIDX_MODE_SYMLINK:
4920 ct->mode = S_IFLNK;
4921 break;
4922 default:
4923 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4924 goto done;
4926 ct->mode |= got_fileindex_entry_perms_get(ie);
4927 } else if (status != GOT_STATUS_DELETE &&
4928 staged_status != GOT_STATUS_DELETE) {
4929 if (dirfd != -1) {
4930 if (fstatat(dirfd, de_name, &sb,
4931 AT_SYMLINK_NOFOLLOW) == -1) {
4932 err = got_error_from_errno2("fstatat",
4933 ct->ondisk_path);
4934 goto done;
4936 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4937 err = got_error_from_errno2("lstat", ct->ondisk_path);
4938 goto done;
4940 ct->mode = sb.st_mode;
4943 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4944 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4945 relpath) == -1) {
4946 err = got_error_from_errno("asprintf");
4947 goto done;
4950 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4951 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4952 int is_bad_symlink;
4953 char target_path[PATH_MAX];
4954 ssize_t target_len;
4955 target_len = readlink(ct->ondisk_path, target_path,
4956 sizeof(target_path));
4957 if (target_len == -1) {
4958 err = got_error_from_errno2("readlink",
4959 ct->ondisk_path);
4960 goto done;
4962 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4963 target_len, ct->ondisk_path, a->worktree->root_path);
4964 if (err)
4965 goto done;
4966 if (is_bad_symlink) {
4967 err = got_error_path(ct->ondisk_path,
4968 GOT_ERR_BAD_SYMLINK);
4969 goto done;
4974 ct->status = status;
4975 ct->staged_status = staged_status;
4976 ct->blob_id = NULL; /* will be filled in when blob gets created */
4977 if (ct->status != GOT_STATUS_ADD &&
4978 ct->staged_status != GOT_STATUS_ADD) {
4979 ct->base_blob_id = got_object_id_dup(blob_id);
4980 if (ct->base_blob_id == NULL) {
4981 err = got_error_from_errno("got_object_id_dup");
4982 goto done;
4984 ct->base_commit_id = got_object_id_dup(commit_id);
4985 if (ct->base_commit_id == NULL) {
4986 err = got_error_from_errno("got_object_id_dup");
4987 goto done;
4990 if (ct->staged_status == GOT_STATUS_ADD ||
4991 ct->staged_status == GOT_STATUS_MODIFY) {
4992 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4993 if (ct->staged_blob_id == NULL) {
4994 err = got_error_from_errno("got_object_id_dup");
4995 goto done;
4998 ct->path = strdup(path);
4999 if (ct->path == NULL) {
5000 err = got_error_from_errno("strdup");
5001 goto done;
5003 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5004 done:
5005 if (ct && (err || new == NULL))
5006 free_commitable(ct);
5007 free(parent_path);
5008 free(path);
5009 return err;
5012 static const struct got_error *write_tree(struct got_object_id **, int *,
5013 struct got_tree_object *, const char *, struct got_pathlist_head *,
5014 got_worktree_status_cb status_cb, void *status_arg,
5015 struct got_repository *);
5017 static const struct got_error *
5018 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5019 struct got_tree_entry *te, const char *parent_path,
5020 struct got_pathlist_head *commitable_paths,
5021 got_worktree_status_cb status_cb, void *status_arg,
5022 struct got_repository *repo)
5024 const struct got_error *err = NULL;
5025 struct got_tree_object *subtree;
5026 char *subpath;
5028 if (asprintf(&subpath, "%s%s%s", parent_path,
5029 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5030 return got_error_from_errno("asprintf");
5032 err = got_object_open_as_tree(&subtree, repo, &te->id);
5033 if (err)
5034 return err;
5036 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5037 commitable_paths, status_cb, status_arg, repo);
5038 got_object_tree_close(subtree);
5039 free(subpath);
5040 return err;
5043 static const struct got_error *
5044 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5046 const struct got_error *err = NULL;
5047 char *ct_parent_path = NULL;
5049 *match = 0;
5051 if (strchr(ct->in_repo_path, '/') == NULL) {
5052 *match = got_path_is_root_dir(path);
5053 return NULL;
5056 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5057 if (err)
5058 return err;
5059 *match = (strcmp(path, ct_parent_path) == 0);
5060 free(ct_parent_path);
5061 return err;
5064 static mode_t
5065 get_ct_file_mode(struct got_commitable *ct)
5067 if (S_ISLNK(ct->mode))
5068 return S_IFLNK;
5070 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5073 static const struct got_error *
5074 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5075 struct got_tree_entry *te, struct got_commitable *ct)
5077 const struct got_error *err = NULL;
5079 *new_te = NULL;
5081 err = got_object_tree_entry_dup(new_te, te);
5082 if (err)
5083 goto done;
5085 (*new_te)->mode = get_ct_file_mode(ct);
5087 if (ct->staged_status == GOT_STATUS_MODIFY)
5088 memcpy(&(*new_te)->id, ct->staged_blob_id,
5089 sizeof((*new_te)->id));
5090 else
5091 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5092 done:
5093 if (err && *new_te) {
5094 free(*new_te);
5095 *new_te = NULL;
5097 return err;
5100 static const struct got_error *
5101 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5102 struct got_commitable *ct)
5104 const struct got_error *err = NULL;
5105 char *ct_name = NULL;
5107 *new_te = NULL;
5109 *new_te = calloc(1, sizeof(**new_te));
5110 if (*new_te == NULL)
5111 return got_error_from_errno("calloc");
5113 err = got_path_basename(&ct_name, ct->path);
5114 if (err)
5115 goto done;
5116 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5117 sizeof((*new_te)->name)) {
5118 err = got_error(GOT_ERR_NO_SPACE);
5119 goto done;
5122 (*new_te)->mode = get_ct_file_mode(ct);
5124 if (ct->staged_status == GOT_STATUS_ADD)
5125 memcpy(&(*new_te)->id, ct->staged_blob_id,
5126 sizeof((*new_te)->id));
5127 else
5128 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5129 done:
5130 free(ct_name);
5131 if (err && *new_te) {
5132 free(*new_te);
5133 *new_te = NULL;
5135 return err;
5138 static const struct got_error *
5139 insert_tree_entry(struct got_tree_entry *new_te,
5140 struct got_pathlist_head *paths)
5142 const struct got_error *err = NULL;
5143 struct got_pathlist_entry *new_pe;
5145 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5146 if (err)
5147 return err;
5148 if (new_pe == NULL)
5149 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5150 return NULL;
5153 static const struct got_error *
5154 report_ct_status(struct got_commitable *ct,
5155 got_worktree_status_cb status_cb, void *status_arg)
5157 const char *ct_path = ct->path;
5158 unsigned char status;
5160 if (status_cb == NULL) /* no commit progress output desired */
5161 return NULL;
5163 while (ct_path[0] == '/')
5164 ct_path++;
5166 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5167 status = ct->staged_status;
5168 else
5169 status = ct->status;
5171 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5172 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5175 static const struct got_error *
5176 match_modified_subtree(int *modified, struct got_tree_entry *te,
5177 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5179 const struct got_error *err = NULL;
5180 struct got_pathlist_entry *pe;
5181 char *te_path;
5183 *modified = 0;
5185 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5186 got_path_is_root_dir(base_tree_path) ? "" : "/",
5187 te->name) == -1)
5188 return got_error_from_errno("asprintf");
5190 TAILQ_FOREACH(pe, commitable_paths, entry) {
5191 struct got_commitable *ct = pe->data;
5192 *modified = got_path_is_child(ct->in_repo_path, te_path,
5193 strlen(te_path));
5194 if (*modified)
5195 break;
5198 free(te_path);
5199 return err;
5202 static const struct got_error *
5203 match_deleted_or_modified_ct(struct got_commitable **ctp,
5204 struct got_tree_entry *te, const char *base_tree_path,
5205 struct got_pathlist_head *commitable_paths)
5207 const struct got_error *err = NULL;
5208 struct got_pathlist_entry *pe;
5210 *ctp = NULL;
5212 TAILQ_FOREACH(pe, commitable_paths, entry) {
5213 struct got_commitable *ct = pe->data;
5214 char *ct_name = NULL;
5215 int path_matches;
5217 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5218 if (ct->status != GOT_STATUS_MODIFY &&
5219 ct->status != GOT_STATUS_MODE_CHANGE &&
5220 ct->status != GOT_STATUS_DELETE)
5221 continue;
5222 } else {
5223 if (ct->staged_status != GOT_STATUS_MODIFY &&
5224 ct->staged_status != GOT_STATUS_DELETE)
5225 continue;
5228 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5229 continue;
5231 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5232 if (err)
5233 return err;
5234 if (!path_matches)
5235 continue;
5237 err = got_path_basename(&ct_name, pe->path);
5238 if (err)
5239 return err;
5241 if (strcmp(te->name, ct_name) != 0) {
5242 free(ct_name);
5243 continue;
5245 free(ct_name);
5247 *ctp = ct;
5248 break;
5251 return err;
5254 static const struct got_error *
5255 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5256 const char *child_path, const char *path_base_tree,
5257 struct got_pathlist_head *commitable_paths,
5258 got_worktree_status_cb status_cb, void *status_arg,
5259 struct got_repository *repo)
5261 const struct got_error *err = NULL;
5262 struct got_tree_entry *new_te;
5263 char *subtree_path;
5264 struct got_object_id *id = NULL;
5265 int nentries;
5267 *new_tep = NULL;
5269 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5270 got_path_is_root_dir(path_base_tree) ? "" : "/",
5271 child_path) == -1)
5272 return got_error_from_errno("asprintf");
5274 new_te = calloc(1, sizeof(*new_te));
5275 if (new_te == NULL)
5276 return got_error_from_errno("calloc");
5277 new_te->mode = S_IFDIR;
5279 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5280 sizeof(new_te->name)) {
5281 err = got_error(GOT_ERR_NO_SPACE);
5282 goto done;
5284 err = write_tree(&id, &nentries, NULL, subtree_path,
5285 commitable_paths, status_cb, status_arg, repo);
5286 if (err) {
5287 free(new_te);
5288 goto done;
5290 memcpy(&new_te->id, id, sizeof(new_te->id));
5291 done:
5292 free(id);
5293 free(subtree_path);
5294 if (err == NULL)
5295 *new_tep = new_te;
5296 return err;
5299 static const struct got_error *
5300 write_tree(struct got_object_id **new_tree_id, int *nentries,
5301 struct got_tree_object *base_tree, const char *path_base_tree,
5302 struct got_pathlist_head *commitable_paths,
5303 got_worktree_status_cb status_cb, void *status_arg,
5304 struct got_repository *repo)
5306 const struct got_error *err = NULL;
5307 struct got_pathlist_head paths;
5308 struct got_tree_entry *te, *new_te = NULL;
5309 struct got_pathlist_entry *pe;
5311 TAILQ_INIT(&paths);
5312 *nentries = 0;
5314 /* Insert, and recurse into, newly added entries first. */
5315 TAILQ_FOREACH(pe, commitable_paths, entry) {
5316 struct got_commitable *ct = pe->data;
5317 char *child_path = NULL, *slash;
5319 if ((ct->status != GOT_STATUS_ADD &&
5320 ct->staged_status != GOT_STATUS_ADD) ||
5321 (ct->flags & GOT_COMMITABLE_ADDED))
5322 continue;
5324 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5325 strlen(path_base_tree)))
5326 continue;
5328 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5329 ct->in_repo_path);
5330 if (err)
5331 goto done;
5333 slash = strchr(child_path, '/');
5334 if (slash == NULL) {
5335 err = alloc_added_blob_tree_entry(&new_te, ct);
5336 if (err)
5337 goto done;
5338 err = report_ct_status(ct, status_cb, status_arg);
5339 if (err)
5340 goto done;
5341 ct->flags |= GOT_COMMITABLE_ADDED;
5342 err = insert_tree_entry(new_te, &paths);
5343 if (err)
5344 goto done;
5345 (*nentries)++;
5346 } else {
5347 *slash = '\0'; /* trim trailing path components */
5348 if (base_tree == NULL ||
5349 got_object_tree_find_entry(base_tree, child_path)
5350 == NULL) {
5351 err = make_subtree_for_added_blob(&new_te,
5352 child_path, path_base_tree,
5353 commitable_paths, status_cb, status_arg,
5354 repo);
5355 if (err)
5356 goto done;
5357 err = insert_tree_entry(new_te, &paths);
5358 if (err)
5359 goto done;
5360 (*nentries)++;
5365 if (base_tree) {
5366 int i, nbase_entries;
5367 /* Handle modified and deleted entries. */
5368 nbase_entries = got_object_tree_get_nentries(base_tree);
5369 for (i = 0; i < nbase_entries; i++) {
5370 struct got_commitable *ct = NULL;
5372 te = got_object_tree_get_entry(base_tree, i);
5373 if (got_object_tree_entry_is_submodule(te)) {
5374 /* Entry is a submodule; just copy it. */
5375 err = got_object_tree_entry_dup(&new_te, te);
5376 if (err)
5377 goto done;
5378 err = insert_tree_entry(new_te, &paths);
5379 if (err)
5380 goto done;
5381 (*nentries)++;
5382 continue;
5385 if (S_ISDIR(te->mode)) {
5386 int modified;
5387 err = got_object_tree_entry_dup(&new_te, te);
5388 if (err)
5389 goto done;
5390 err = match_modified_subtree(&modified, te,
5391 path_base_tree, commitable_paths);
5392 if (err)
5393 goto done;
5394 /* Avoid recursion into unmodified subtrees. */
5395 if (modified) {
5396 struct got_object_id *new_id;
5397 int nsubentries;
5398 err = write_subtree(&new_id,
5399 &nsubentries, te,
5400 path_base_tree, commitable_paths,
5401 status_cb, status_arg, repo);
5402 if (err)
5403 goto done;
5404 if (nsubentries == 0) {
5405 /* All entries were deleted. */
5406 free(new_id);
5407 continue;
5409 memcpy(&new_te->id, new_id,
5410 sizeof(new_te->id));
5411 free(new_id);
5413 err = insert_tree_entry(new_te, &paths);
5414 if (err)
5415 goto done;
5416 (*nentries)++;
5417 continue;
5420 err = match_deleted_or_modified_ct(&ct, te,
5421 path_base_tree, commitable_paths);
5422 if (err)
5423 goto done;
5424 if (ct) {
5425 /* NB: Deleted entries get dropped here. */
5426 if (ct->status == GOT_STATUS_MODIFY ||
5427 ct->status == GOT_STATUS_MODE_CHANGE ||
5428 ct->staged_status == GOT_STATUS_MODIFY) {
5429 err = alloc_modified_blob_tree_entry(
5430 &new_te, te, ct);
5431 if (err)
5432 goto done;
5433 err = insert_tree_entry(new_te, &paths);
5434 if (err)
5435 goto done;
5436 (*nentries)++;
5438 err = report_ct_status(ct, status_cb,
5439 status_arg);
5440 if (err)
5441 goto done;
5442 } else {
5443 /* Entry is unchanged; just copy it. */
5444 err = got_object_tree_entry_dup(&new_te, te);
5445 if (err)
5446 goto done;
5447 err = insert_tree_entry(new_te, &paths);
5448 if (err)
5449 goto done;
5450 (*nentries)++;
5455 /* Write new list of entries; deleted entries have been dropped. */
5456 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5457 done:
5458 got_pathlist_free(&paths);
5459 return err;
5462 static const struct got_error *
5463 update_fileindex_after_commit(struct got_worktree *worktree,
5464 struct got_pathlist_head *commitable_paths,
5465 struct got_object_id *new_base_commit_id,
5466 struct got_fileindex *fileindex, int have_staged_files)
5468 const struct got_error *err = NULL;
5469 struct got_pathlist_entry *pe;
5470 char *relpath = NULL;
5472 TAILQ_FOREACH(pe, commitable_paths, entry) {
5473 struct got_fileindex_entry *ie;
5474 struct got_commitable *ct = pe->data;
5476 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5478 err = got_path_skip_common_ancestor(&relpath,
5479 worktree->root_path, ct->ondisk_path);
5480 if (err)
5481 goto done;
5483 if (ie) {
5484 if (ct->status == GOT_STATUS_DELETE ||
5485 ct->staged_status == GOT_STATUS_DELETE) {
5486 got_fileindex_entry_remove(fileindex, ie);
5487 } else if (ct->staged_status == GOT_STATUS_ADD ||
5488 ct->staged_status == GOT_STATUS_MODIFY) {
5489 got_fileindex_entry_stage_set(ie,
5490 GOT_FILEIDX_STAGE_NONE);
5491 got_fileindex_entry_staged_filetype_set(ie, 0);
5493 err = got_fileindex_entry_update(ie,
5494 worktree->root_fd, relpath,
5495 ct->staged_blob_id->sha1,
5496 new_base_commit_id->sha1,
5497 !have_staged_files);
5498 } else
5499 err = got_fileindex_entry_update(ie,
5500 worktree->root_fd, relpath,
5501 ct->blob_id->sha1,
5502 new_base_commit_id->sha1,
5503 !have_staged_files);
5504 } else {
5505 err = got_fileindex_entry_alloc(&ie, pe->path);
5506 if (err)
5507 goto done;
5508 err = got_fileindex_entry_update(ie,
5509 worktree->root_fd, relpath, ct->blob_id->sha1,
5510 new_base_commit_id->sha1, 1);
5511 if (err) {
5512 got_fileindex_entry_free(ie);
5513 goto done;
5515 err = got_fileindex_entry_add(fileindex, ie);
5516 if (err) {
5517 got_fileindex_entry_free(ie);
5518 goto done;
5521 free(relpath);
5522 relpath = NULL;
5524 done:
5525 free(relpath);
5526 return err;
5530 static const struct got_error *
5531 check_out_of_date(const char *in_repo_path, unsigned char status,
5532 unsigned char staged_status, struct got_object_id *base_blob_id,
5533 struct got_object_id *base_commit_id,
5534 struct got_object_id *head_commit_id, struct got_repository *repo,
5535 int ood_errcode)
5537 const struct got_error *err = NULL;
5538 struct got_commit_object *commit = NULL;
5539 struct got_object_id *id = NULL;
5541 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5542 /* Trivial case: base commit == head commit */
5543 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5544 return NULL;
5546 * Ensure file content which local changes were based
5547 * on matches file content in the branch head.
5549 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5550 if (err)
5551 goto done;
5552 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5553 if (err) {
5554 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5555 err = got_error(ood_errcode);
5556 goto done;
5557 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5558 err = got_error(ood_errcode);
5559 } else {
5560 /* Require that added files don't exist in the branch head. */
5561 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5562 if (err)
5563 goto done;
5564 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5565 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5566 goto done;
5567 err = id ? got_error(ood_errcode) : NULL;
5569 done:
5570 free(id);
5571 if (commit)
5572 got_object_commit_close(commit);
5573 return err;
5576 const struct got_error *
5577 commit_worktree(struct got_object_id **new_commit_id,
5578 struct got_pathlist_head *commitable_paths,
5579 struct got_object_id *head_commit_id,
5580 struct got_object_id *parent_id2,
5581 struct got_worktree *worktree,
5582 const char *author, const char *committer,
5583 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5584 got_worktree_status_cb status_cb, void *status_arg,
5585 struct got_repository *repo)
5587 const struct got_error *err = NULL, *unlockerr = NULL;
5588 struct got_pathlist_entry *pe;
5589 const char *head_ref_name = NULL;
5590 struct got_commit_object *head_commit = NULL;
5591 struct got_reference *head_ref2 = NULL;
5592 struct got_object_id *head_commit_id2 = NULL;
5593 struct got_tree_object *head_tree = NULL;
5594 struct got_object_id *new_tree_id = NULL;
5595 int nentries, nparents = 0;
5596 struct got_object_id_queue parent_ids;
5597 struct got_object_qid *pid = NULL;
5598 char *logmsg = NULL;
5600 *new_commit_id = NULL;
5602 STAILQ_INIT(&parent_ids);
5604 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5605 if (err)
5606 goto done;
5608 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5609 if (err)
5610 goto done;
5612 if (commit_msg_cb != NULL) {
5613 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5614 if (err)
5615 goto done;
5618 if (logmsg == NULL || strlen(logmsg) == 0) {
5619 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5620 goto done;
5623 /* Create blobs from added and modified files and record their IDs. */
5624 TAILQ_FOREACH(pe, commitable_paths, entry) {
5625 struct got_commitable *ct = pe->data;
5626 char *ondisk_path;
5628 /* Blobs for staged files already exist. */
5629 if (ct->staged_status == GOT_STATUS_ADD ||
5630 ct->staged_status == GOT_STATUS_MODIFY)
5631 continue;
5633 if (ct->status != GOT_STATUS_ADD &&
5634 ct->status != GOT_STATUS_MODIFY &&
5635 ct->status != GOT_STATUS_MODE_CHANGE)
5636 continue;
5638 if (asprintf(&ondisk_path, "%s/%s",
5639 worktree->root_path, pe->path) == -1) {
5640 err = got_error_from_errno("asprintf");
5641 goto done;
5643 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5644 free(ondisk_path);
5645 if (err)
5646 goto done;
5649 /* Recursively write new tree objects. */
5650 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5651 commitable_paths, status_cb, status_arg, repo);
5652 if (err)
5653 goto done;
5655 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5656 if (err)
5657 goto done;
5658 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5659 nparents++;
5660 if (parent_id2) {
5661 err = got_object_qid_alloc(&pid, parent_id2);
5662 if (err)
5663 goto done;
5664 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5665 nparents++;
5667 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5668 nparents, author, time(NULL), committer, time(NULL), logmsg, repo);
5669 if (logmsg != NULL)
5670 free(logmsg);
5671 if (err)
5672 goto done;
5674 /* Check if a concurrent commit to our branch has occurred. */
5675 head_ref_name = got_worktree_get_head_ref_name(worktree);
5676 if (head_ref_name == NULL) {
5677 err = got_error_from_errno("got_worktree_get_head_ref_name");
5678 goto done;
5680 /* Lock the reference here to prevent concurrent modification. */
5681 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5682 if (err)
5683 goto done;
5684 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5685 if (err)
5686 goto done;
5687 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5688 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5689 goto done;
5691 /* Update branch head in repository. */
5692 err = got_ref_change_ref(head_ref2, *new_commit_id);
5693 if (err)
5694 goto done;
5695 err = got_ref_write(head_ref2, repo);
5696 if (err)
5697 goto done;
5699 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5700 if (err)
5701 goto done;
5703 err = ref_base_commit(worktree, repo);
5704 if (err)
5705 goto done;
5706 done:
5707 got_object_id_queue_free(&parent_ids);
5708 if (head_tree)
5709 got_object_tree_close(head_tree);
5710 if (head_commit)
5711 got_object_commit_close(head_commit);
5712 free(head_commit_id2);
5713 if (head_ref2) {
5714 unlockerr = got_ref_unlock(head_ref2);
5715 if (unlockerr && err == NULL)
5716 err = unlockerr;
5717 got_ref_close(head_ref2);
5719 return err;
5722 static const struct got_error *
5723 check_path_is_commitable(const char *path,
5724 struct got_pathlist_head *commitable_paths)
5726 struct got_pathlist_entry *cpe = NULL;
5727 size_t path_len = strlen(path);
5729 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5730 struct got_commitable *ct = cpe->data;
5731 const char *ct_path = ct->path;
5733 while (ct_path[0] == '/')
5734 ct_path++;
5736 if (strcmp(path, ct_path) == 0 ||
5737 got_path_is_child(ct_path, path, path_len))
5738 break;
5741 if (cpe == NULL)
5742 return got_error_path(path, GOT_ERR_BAD_PATH);
5744 return NULL;
5747 static const struct got_error *
5748 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5750 int *have_staged_files = arg;
5752 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5753 *have_staged_files = 1;
5754 return got_error(GOT_ERR_CANCELLED);
5757 return NULL;
5760 static const struct got_error *
5761 check_non_staged_files(struct got_fileindex *fileindex,
5762 struct got_pathlist_head *paths)
5764 struct got_pathlist_entry *pe;
5765 struct got_fileindex_entry *ie;
5767 TAILQ_FOREACH(pe, paths, entry) {
5768 if (pe->path[0] == '\0')
5769 continue;
5770 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5771 if (ie == NULL)
5772 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5773 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5774 return got_error_path(pe->path,
5775 GOT_ERR_FILE_NOT_STAGED);
5778 return NULL;
5781 const struct got_error *
5782 got_worktree_commit(struct got_object_id **new_commit_id,
5783 struct got_worktree *worktree, struct got_pathlist_head *paths,
5784 const char *author, const char *committer, int allow_bad_symlinks,
5785 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5786 got_worktree_status_cb status_cb, void *status_arg,
5787 struct got_repository *repo)
5789 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5790 struct got_fileindex *fileindex = NULL;
5791 char *fileindex_path = NULL;
5792 struct got_pathlist_head commitable_paths;
5793 struct collect_commitables_arg cc_arg;
5794 struct got_pathlist_entry *pe;
5795 struct got_reference *head_ref = NULL;
5796 struct got_object_id *head_commit_id = NULL;
5797 int have_staged_files = 0;
5799 *new_commit_id = NULL;
5801 TAILQ_INIT(&commitable_paths);
5803 err = lock_worktree(worktree, LOCK_EX);
5804 if (err)
5805 goto done;
5807 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5808 if (err)
5809 goto done;
5811 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5812 if (err)
5813 goto done;
5815 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5816 if (err)
5817 goto done;
5819 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5820 &have_staged_files);
5821 if (err && err->code != GOT_ERR_CANCELLED)
5822 goto done;
5823 if (have_staged_files) {
5824 err = check_non_staged_files(fileindex, paths);
5825 if (err)
5826 goto done;
5829 cc_arg.commitable_paths = &commitable_paths;
5830 cc_arg.worktree = worktree;
5831 cc_arg.fileindex = fileindex;
5832 cc_arg.repo = repo;
5833 cc_arg.have_staged_files = have_staged_files;
5834 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5835 TAILQ_FOREACH(pe, paths, entry) {
5836 err = worktree_status(worktree, pe->path, fileindex, repo,
5837 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
5838 if (err)
5839 goto done;
5842 if (TAILQ_EMPTY(&commitable_paths)) {
5843 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5844 goto done;
5847 TAILQ_FOREACH(pe, paths, entry) {
5848 err = check_path_is_commitable(pe->path, &commitable_paths);
5849 if (err)
5850 goto done;
5853 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5854 struct got_commitable *ct = pe->data;
5855 const char *ct_path = ct->in_repo_path;
5857 while (ct_path[0] == '/')
5858 ct_path++;
5859 err = check_out_of_date(ct_path, ct->status,
5860 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5861 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5862 if (err)
5863 goto done;
5867 err = commit_worktree(new_commit_id, &commitable_paths,
5868 head_commit_id, NULL, worktree, author, committer,
5869 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5870 if (err)
5871 goto done;
5873 err = update_fileindex_after_commit(worktree, &commitable_paths,
5874 *new_commit_id, fileindex, have_staged_files);
5875 sync_err = sync_fileindex(fileindex, fileindex_path);
5876 if (sync_err && err == NULL)
5877 err = sync_err;
5878 done:
5879 if (fileindex)
5880 got_fileindex_free(fileindex);
5881 free(fileindex_path);
5882 unlockerr = lock_worktree(worktree, LOCK_SH);
5883 if (unlockerr && err == NULL)
5884 err = unlockerr;
5885 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5886 struct got_commitable *ct = pe->data;
5887 free_commitable(ct);
5889 got_pathlist_free(&commitable_paths);
5890 return err;
5893 const char *
5894 got_commitable_get_path(struct got_commitable *ct)
5896 return ct->path;
5899 unsigned int
5900 got_commitable_get_status(struct got_commitable *ct)
5902 return ct->status;
5905 struct check_rebase_ok_arg {
5906 struct got_worktree *worktree;
5907 struct got_repository *repo;
5910 static const struct got_error *
5911 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5913 const struct got_error *err = NULL;
5914 struct check_rebase_ok_arg *a = arg;
5915 unsigned char status;
5916 struct stat sb;
5917 char *ondisk_path;
5919 /* Reject rebase of a work tree with mixed base commits. */
5920 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5921 SHA1_DIGEST_LENGTH))
5922 return got_error(GOT_ERR_MIXED_COMMITS);
5924 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5925 == -1)
5926 return got_error_from_errno("asprintf");
5928 /* Reject rebase of a work tree with modified or staged files. */
5929 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5930 free(ondisk_path);
5931 if (err)
5932 return err;
5934 if (status != GOT_STATUS_NO_CHANGE)
5935 return got_error(GOT_ERR_MODIFIED);
5936 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5937 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5939 return NULL;
5942 const struct got_error *
5943 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5944 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5945 struct got_worktree *worktree, struct got_reference *branch,
5946 struct got_repository *repo)
5948 const struct got_error *err = NULL;
5949 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5950 char *branch_ref_name = NULL;
5951 char *fileindex_path = NULL;
5952 struct check_rebase_ok_arg ok_arg;
5953 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5954 struct got_object_id *wt_branch_tip = NULL;
5956 *new_base_branch_ref = NULL;
5957 *tmp_branch = NULL;
5958 *fileindex = NULL;
5960 err = lock_worktree(worktree, LOCK_EX);
5961 if (err)
5962 return err;
5964 err = open_fileindex(fileindex, &fileindex_path, worktree);
5965 if (err)
5966 goto done;
5968 ok_arg.worktree = worktree;
5969 ok_arg.repo = repo;
5970 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5971 &ok_arg);
5972 if (err)
5973 goto done;
5975 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5976 if (err)
5977 goto done;
5979 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5980 if (err)
5981 goto done;
5983 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5984 if (err)
5985 goto done;
5987 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5988 0);
5989 if (err)
5990 goto done;
5992 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5993 if (err)
5994 goto done;
5995 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5996 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5997 goto done;
6000 err = got_ref_alloc_symref(new_base_branch_ref,
6001 new_base_branch_ref_name, wt_branch);
6002 if (err)
6003 goto done;
6004 err = got_ref_write(*new_base_branch_ref, repo);
6005 if (err)
6006 goto done;
6008 /* TODO Lock original branch's ref while rebasing? */
6010 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6011 if (err)
6012 goto done;
6014 err = got_ref_write(branch_ref, repo);
6015 if (err)
6016 goto done;
6018 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6019 worktree->base_commit_id);
6020 if (err)
6021 goto done;
6022 err = got_ref_write(*tmp_branch, repo);
6023 if (err)
6024 goto done;
6026 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6027 if (err)
6028 goto done;
6029 done:
6030 free(fileindex_path);
6031 free(tmp_branch_name);
6032 free(new_base_branch_ref_name);
6033 free(branch_ref_name);
6034 if (branch_ref)
6035 got_ref_close(branch_ref);
6036 if (wt_branch)
6037 got_ref_close(wt_branch);
6038 free(wt_branch_tip);
6039 if (err) {
6040 if (*new_base_branch_ref) {
6041 got_ref_close(*new_base_branch_ref);
6042 *new_base_branch_ref = NULL;
6044 if (*tmp_branch) {
6045 got_ref_close(*tmp_branch);
6046 *tmp_branch = NULL;
6048 if (*fileindex) {
6049 got_fileindex_free(*fileindex);
6050 *fileindex = NULL;
6052 lock_worktree(worktree, LOCK_SH);
6054 return err;
6057 const struct got_error *
6058 got_worktree_rebase_continue(struct got_object_id **commit_id,
6059 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6060 struct got_reference **branch, struct got_fileindex **fileindex,
6061 struct got_worktree *worktree, struct got_repository *repo)
6063 const struct got_error *err;
6064 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6065 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6066 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6067 char *fileindex_path = NULL;
6068 int have_staged_files = 0;
6070 *commit_id = NULL;
6071 *new_base_branch = NULL;
6072 *tmp_branch = NULL;
6073 *branch = NULL;
6074 *fileindex = NULL;
6076 err = lock_worktree(worktree, LOCK_EX);
6077 if (err)
6078 return err;
6080 err = open_fileindex(fileindex, &fileindex_path, worktree);
6081 if (err)
6082 goto done;
6084 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6085 &have_staged_files);
6086 if (err && err->code != GOT_ERR_CANCELLED)
6087 goto done;
6088 if (have_staged_files) {
6089 err = got_error(GOT_ERR_STAGED_PATHS);
6090 goto done;
6093 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6094 if (err)
6095 goto done;
6097 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6098 if (err)
6099 goto done;
6101 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6102 if (err)
6103 goto done;
6105 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6106 if (err)
6107 goto done;
6109 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6110 if (err)
6111 goto done;
6113 err = got_ref_open(branch, repo,
6114 got_ref_get_symref_target(branch_ref), 0);
6115 if (err)
6116 goto done;
6118 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6119 if (err)
6120 goto done;
6122 err = got_ref_resolve(commit_id, repo, commit_ref);
6123 if (err)
6124 goto done;
6126 err = got_ref_open(new_base_branch, repo,
6127 new_base_branch_ref_name, 0);
6128 if (err)
6129 goto done;
6131 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6132 if (err)
6133 goto done;
6134 done:
6135 free(commit_ref_name);
6136 free(branch_ref_name);
6137 free(fileindex_path);
6138 if (commit_ref)
6139 got_ref_close(commit_ref);
6140 if (branch_ref)
6141 got_ref_close(branch_ref);
6142 if (err) {
6143 free(*commit_id);
6144 *commit_id = NULL;
6145 if (*tmp_branch) {
6146 got_ref_close(*tmp_branch);
6147 *tmp_branch = NULL;
6149 if (*new_base_branch) {
6150 got_ref_close(*new_base_branch);
6151 *new_base_branch = NULL;
6153 if (*branch) {
6154 got_ref_close(*branch);
6155 *branch = NULL;
6157 if (*fileindex) {
6158 got_fileindex_free(*fileindex);
6159 *fileindex = NULL;
6161 lock_worktree(worktree, LOCK_SH);
6163 return err;
6166 const struct got_error *
6167 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6169 const struct got_error *err;
6170 char *tmp_branch_name = NULL;
6172 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6173 if (err)
6174 return err;
6176 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6177 free(tmp_branch_name);
6178 return NULL;
6181 static const struct got_error *
6182 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6183 char **logmsg, void *arg)
6185 *logmsg = arg;
6186 return NULL;
6189 static const struct got_error *
6190 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6191 const char *path, struct got_object_id *blob_id,
6192 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6193 int dirfd, const char *de_name)
6195 return NULL;
6198 struct collect_merged_paths_arg {
6199 got_worktree_checkout_cb progress_cb;
6200 void *progress_arg;
6201 struct got_pathlist_head *merged_paths;
6204 static const struct got_error *
6205 collect_merged_paths(void *arg, unsigned char status, const char *path)
6207 const struct got_error *err;
6208 struct collect_merged_paths_arg *a = arg;
6209 char *p;
6210 struct got_pathlist_entry *new;
6212 err = (*a->progress_cb)(a->progress_arg, status, path);
6213 if (err)
6214 return err;
6216 if (status != GOT_STATUS_MERGE &&
6217 status != GOT_STATUS_ADD &&
6218 status != GOT_STATUS_DELETE &&
6219 status != GOT_STATUS_CONFLICT)
6220 return NULL;
6222 p = strdup(path);
6223 if (p == NULL)
6224 return got_error_from_errno("strdup");
6226 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6227 if (err || new == NULL)
6228 free(p);
6229 return err;
6232 void
6233 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6235 struct got_pathlist_entry *pe;
6237 TAILQ_FOREACH(pe, merged_paths, entry)
6238 free((char *)pe->path);
6240 got_pathlist_free(merged_paths);
6243 static const struct got_error *
6244 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6245 int is_rebase, struct got_repository *repo)
6247 const struct got_error *err;
6248 struct got_reference *commit_ref = NULL;
6250 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6251 if (err) {
6252 if (err->code != GOT_ERR_NOT_REF)
6253 goto done;
6254 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6255 if (err)
6256 goto done;
6257 err = got_ref_write(commit_ref, repo);
6258 if (err)
6259 goto done;
6260 } else if (is_rebase) {
6261 struct got_object_id *stored_id;
6262 int cmp;
6264 err = got_ref_resolve(&stored_id, repo, commit_ref);
6265 if (err)
6266 goto done;
6267 cmp = got_object_id_cmp(commit_id, stored_id);
6268 free(stored_id);
6269 if (cmp != 0) {
6270 err = got_error(GOT_ERR_REBASE_COMMITID);
6271 goto done;
6274 done:
6275 if (commit_ref)
6276 got_ref_close(commit_ref);
6277 return err;
6280 static const struct got_error *
6281 rebase_merge_files(struct got_pathlist_head *merged_paths,
6282 const char *commit_ref_name, struct got_worktree *worktree,
6283 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6284 struct got_object_id *commit_id, struct got_repository *repo,
6285 got_worktree_checkout_cb progress_cb, void *progress_arg,
6286 got_cancel_cb cancel_cb, void *cancel_arg)
6288 const struct got_error *err;
6289 struct got_reference *commit_ref = NULL;
6290 struct collect_merged_paths_arg cmp_arg;
6291 char *fileindex_path;
6293 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6295 err = get_fileindex_path(&fileindex_path, worktree);
6296 if (err)
6297 return err;
6299 cmp_arg.progress_cb = progress_cb;
6300 cmp_arg.progress_arg = progress_arg;
6301 cmp_arg.merged_paths = merged_paths;
6302 err = merge_files(worktree, fileindex, fileindex_path,
6303 parent_commit_id, commit_id, repo, collect_merged_paths,
6304 &cmp_arg, cancel_cb, cancel_arg);
6305 if (commit_ref)
6306 got_ref_close(commit_ref);
6307 return err;
6310 const struct got_error *
6311 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6312 struct got_worktree *worktree, struct got_fileindex *fileindex,
6313 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6314 struct got_repository *repo,
6315 got_worktree_checkout_cb progress_cb, void *progress_arg,
6316 got_cancel_cb cancel_cb, void *cancel_arg)
6318 const struct got_error *err;
6319 char *commit_ref_name;
6321 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6322 if (err)
6323 return err;
6325 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6326 if (err)
6327 goto done;
6329 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6330 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6331 progress_arg, cancel_cb, cancel_arg);
6332 done:
6333 free(commit_ref_name);
6334 return err;
6337 const struct got_error *
6338 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6339 struct got_worktree *worktree, struct got_fileindex *fileindex,
6340 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6341 struct got_repository *repo,
6342 got_worktree_checkout_cb progress_cb, void *progress_arg,
6343 got_cancel_cb cancel_cb, void *cancel_arg)
6345 const struct got_error *err;
6346 char *commit_ref_name;
6348 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6349 if (err)
6350 return err;
6352 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6353 if (err)
6354 goto done;
6356 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6357 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6358 progress_arg, cancel_cb, cancel_arg);
6359 done:
6360 free(commit_ref_name);
6361 return err;
6364 static const struct got_error *
6365 rebase_commit(struct got_object_id **new_commit_id,
6366 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6367 struct got_worktree *worktree, struct got_fileindex *fileindex,
6368 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6369 const char *new_logmsg, struct got_repository *repo)
6371 const struct got_error *err, *sync_err;
6372 struct got_pathlist_head commitable_paths;
6373 struct collect_commitables_arg cc_arg;
6374 char *fileindex_path = NULL;
6375 struct got_reference *head_ref = NULL;
6376 struct got_object_id *head_commit_id = NULL;
6377 char *logmsg = NULL;
6379 TAILQ_INIT(&commitable_paths);
6380 *new_commit_id = NULL;
6382 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6384 err = get_fileindex_path(&fileindex_path, worktree);
6385 if (err)
6386 return err;
6388 cc_arg.commitable_paths = &commitable_paths;
6389 cc_arg.worktree = worktree;
6390 cc_arg.repo = repo;
6391 cc_arg.have_staged_files = 0;
6393 * If possible get the status of individual files directly to
6394 * avoid crawling the entire work tree once per rebased commit.
6396 * Ideally, merged_paths would contain a list of commitables
6397 * we could use so we could skip worktree_status() entirely.
6398 * However, we would then need carefully keep track of cumulative
6399 * effects of operations such as file additions and deletions
6400 * in 'got histedit -f' (folding multiple commits into one),
6401 * and this extra complexity is not really worth it.
6403 if (merged_paths) {
6404 struct got_pathlist_entry *pe;
6405 TAILQ_FOREACH(pe, merged_paths, entry) {
6406 err = worktree_status(worktree, pe->path, fileindex,
6407 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6408 0);
6409 if (err)
6410 goto done;
6412 } else {
6413 err = worktree_status(worktree, "", fileindex, repo,
6414 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6415 if (err)
6416 goto done;
6419 if (TAILQ_EMPTY(&commitable_paths)) {
6420 /* No-op change; commit will be elided. */
6421 err = got_ref_delete(commit_ref, repo);
6422 if (err)
6423 goto done;
6424 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6425 goto done;
6428 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6429 if (err)
6430 goto done;
6432 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6433 if (err)
6434 goto done;
6436 if (new_logmsg) {
6437 logmsg = strdup(new_logmsg);
6438 if (logmsg == NULL) {
6439 err = got_error_from_errno("strdup");
6440 goto done;
6442 } else {
6443 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6444 if (err)
6445 goto done;
6448 /* NB: commit_worktree will call free(logmsg) */
6449 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6450 NULL, worktree, got_object_commit_get_author(orig_commit),
6451 got_object_commit_get_committer(orig_commit),
6452 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6453 if (err)
6454 goto done;
6456 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6457 if (err)
6458 goto done;
6460 err = got_ref_delete(commit_ref, repo);
6461 if (err)
6462 goto done;
6464 err = update_fileindex_after_commit(worktree, &commitable_paths,
6465 *new_commit_id, fileindex, 0);
6466 sync_err = sync_fileindex(fileindex, fileindex_path);
6467 if (sync_err && err == NULL)
6468 err = sync_err;
6469 done:
6470 free(fileindex_path);
6471 free(head_commit_id);
6472 if (head_ref)
6473 got_ref_close(head_ref);
6474 if (err) {
6475 free(*new_commit_id);
6476 *new_commit_id = NULL;
6478 return err;
6481 const struct got_error *
6482 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6483 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6484 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6485 struct got_commit_object *orig_commit,
6486 struct got_object_id *orig_commit_id, struct got_repository *repo)
6488 const struct got_error *err;
6489 char *commit_ref_name;
6490 struct got_reference *commit_ref = NULL;
6491 struct got_object_id *commit_id = NULL;
6493 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6494 if (err)
6495 return err;
6497 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6498 if (err)
6499 goto done;
6500 err = got_ref_resolve(&commit_id, repo, commit_ref);
6501 if (err)
6502 goto done;
6503 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6504 err = got_error(GOT_ERR_REBASE_COMMITID);
6505 goto done;
6508 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6509 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6510 done:
6511 if (commit_ref)
6512 got_ref_close(commit_ref);
6513 free(commit_ref_name);
6514 free(commit_id);
6515 return err;
6518 const struct got_error *
6519 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6520 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6521 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6522 struct got_commit_object *orig_commit,
6523 struct got_object_id *orig_commit_id, const char *new_logmsg,
6524 struct got_repository *repo)
6526 const struct got_error *err;
6527 char *commit_ref_name;
6528 struct got_reference *commit_ref = NULL;
6530 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6531 if (err)
6532 return err;
6534 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6535 if (err)
6536 goto done;
6538 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6539 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6540 done:
6541 if (commit_ref)
6542 got_ref_close(commit_ref);
6543 free(commit_ref_name);
6544 return err;
6547 const struct got_error *
6548 got_worktree_rebase_postpone(struct got_worktree *worktree,
6549 struct got_fileindex *fileindex)
6551 if (fileindex)
6552 got_fileindex_free(fileindex);
6553 return lock_worktree(worktree, LOCK_SH);
6556 static const struct got_error *
6557 delete_ref(const char *name, struct got_repository *repo)
6559 const struct got_error *err;
6560 struct got_reference *ref;
6562 err = got_ref_open(&ref, repo, name, 0);
6563 if (err) {
6564 if (err->code == GOT_ERR_NOT_REF)
6565 return NULL;
6566 return err;
6569 err = got_ref_delete(ref, repo);
6570 got_ref_close(ref);
6571 return err;
6574 static const struct got_error *
6575 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6577 const struct got_error *err;
6578 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6579 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6581 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6582 if (err)
6583 goto done;
6584 err = delete_ref(tmp_branch_name, repo);
6585 if (err)
6586 goto done;
6588 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6589 if (err)
6590 goto done;
6591 err = delete_ref(new_base_branch_ref_name, repo);
6592 if (err)
6593 goto done;
6595 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6596 if (err)
6597 goto done;
6598 err = delete_ref(branch_ref_name, repo);
6599 if (err)
6600 goto done;
6602 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6603 if (err)
6604 goto done;
6605 err = delete_ref(commit_ref_name, repo);
6606 if (err)
6607 goto done;
6609 done:
6610 free(tmp_branch_name);
6611 free(new_base_branch_ref_name);
6612 free(branch_ref_name);
6613 free(commit_ref_name);
6614 return err;
6617 const struct got_error *
6618 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6619 struct got_object_id *new_commit_id, struct got_repository *repo)
6621 const struct got_error *err;
6622 struct got_reference *ref = NULL;
6623 struct got_object_id *old_commit_id = NULL;
6624 const char *branch_name = NULL;
6625 char *new_id_str = NULL;
6626 char *refname = NULL;
6628 branch_name = got_ref_get_name(branch);
6629 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6630 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6631 branch_name += 11;
6633 err = got_object_id_str(&new_id_str, new_commit_id);
6634 if (err)
6635 return err;
6637 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6638 new_id_str) == -1) {
6639 err = got_error_from_errno("asprintf");
6640 goto done;
6643 err = got_ref_resolve(&old_commit_id, repo, branch);
6644 if (err)
6645 goto done;
6647 err = got_ref_alloc(&ref, refname, old_commit_id);
6648 if (err)
6649 goto done;
6651 err = got_ref_write(ref, repo);
6652 done:
6653 free(new_id_str);
6654 free(refname);
6655 free(old_commit_id);
6656 if (ref)
6657 got_ref_close(ref);
6658 return err;
6661 const struct got_error *
6662 got_worktree_rebase_complete(struct got_worktree *worktree,
6663 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6664 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6665 struct got_repository *repo, int create_backup)
6667 const struct got_error *err, *unlockerr, *sync_err;
6668 struct got_object_id *new_head_commit_id = NULL;
6669 char *fileindex_path = NULL;
6671 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6672 if (err)
6673 return err;
6675 if (create_backup) {
6676 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6677 rebased_branch, new_head_commit_id, repo);
6678 if (err)
6679 goto done;
6682 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6683 if (err)
6684 goto done;
6686 err = got_ref_write(rebased_branch, repo);
6687 if (err)
6688 goto done;
6690 err = got_worktree_set_head_ref(worktree, rebased_branch);
6691 if (err)
6692 goto done;
6694 err = delete_rebase_refs(worktree, repo);
6695 if (err)
6696 goto done;
6698 err = get_fileindex_path(&fileindex_path, worktree);
6699 if (err)
6700 goto done;
6701 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6702 sync_err = sync_fileindex(fileindex, fileindex_path);
6703 if (sync_err && err == NULL)
6704 err = sync_err;
6705 done:
6706 got_fileindex_free(fileindex);
6707 free(fileindex_path);
6708 free(new_head_commit_id);
6709 unlockerr = lock_worktree(worktree, LOCK_SH);
6710 if (unlockerr && err == NULL)
6711 err = unlockerr;
6712 return err;
6715 const struct got_error *
6716 got_worktree_rebase_abort(struct got_worktree *worktree,
6717 struct got_fileindex *fileindex, struct got_repository *repo,
6718 struct got_reference *new_base_branch,
6719 got_worktree_checkout_cb progress_cb, void *progress_arg)
6721 const struct got_error *err, *unlockerr, *sync_err;
6722 struct got_reference *resolved = NULL;
6723 struct got_object_id *commit_id = NULL;
6724 struct got_commit_object *commit = NULL;
6725 char *fileindex_path = NULL;
6726 struct revert_file_args rfa;
6727 struct got_object_id *tree_id = NULL;
6729 err = lock_worktree(worktree, LOCK_EX);
6730 if (err)
6731 return err;
6733 err = got_object_open_as_commit(&commit, repo,
6734 worktree->base_commit_id);
6735 if (err)
6736 goto done;
6738 err = got_ref_open(&resolved, repo,
6739 got_ref_get_symref_target(new_base_branch), 0);
6740 if (err)
6741 goto done;
6743 err = got_worktree_set_head_ref(worktree, resolved);
6744 if (err)
6745 goto done;
6748 * XXX commits to the base branch could have happened while
6749 * we were busy rebasing; should we store the original commit ID
6750 * when rebase begins and read it back here?
6752 err = got_ref_resolve(&commit_id, repo, resolved);
6753 if (err)
6754 goto done;
6756 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6757 if (err)
6758 goto done;
6760 err = got_object_id_by_path(&tree_id, repo, commit,
6761 worktree->path_prefix);
6762 if (err)
6763 goto done;
6765 err = delete_rebase_refs(worktree, repo);
6766 if (err)
6767 goto done;
6769 err = get_fileindex_path(&fileindex_path, worktree);
6770 if (err)
6771 goto done;
6773 rfa.worktree = worktree;
6774 rfa.fileindex = fileindex;
6775 rfa.progress_cb = progress_cb;
6776 rfa.progress_arg = progress_arg;
6777 rfa.patch_cb = NULL;
6778 rfa.patch_arg = NULL;
6779 rfa.repo = repo;
6780 rfa.unlink_added_files = 0;
6781 err = worktree_status(worktree, "", fileindex, repo,
6782 revert_file, &rfa, NULL, NULL, 1, 0);
6783 if (err)
6784 goto sync;
6786 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6787 repo, progress_cb, progress_arg, NULL, NULL);
6788 sync:
6789 sync_err = sync_fileindex(fileindex, fileindex_path);
6790 if (sync_err && err == NULL)
6791 err = sync_err;
6792 done:
6793 got_ref_close(resolved);
6794 free(tree_id);
6795 free(commit_id);
6796 if (commit)
6797 got_object_commit_close(commit);
6798 if (fileindex)
6799 got_fileindex_free(fileindex);
6800 free(fileindex_path);
6802 unlockerr = lock_worktree(worktree, LOCK_SH);
6803 if (unlockerr && err == NULL)
6804 err = unlockerr;
6805 return err;
6808 const struct got_error *
6809 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6810 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6811 struct got_fileindex **fileindex, struct got_worktree *worktree,
6812 struct got_repository *repo)
6814 const struct got_error *err = NULL;
6815 char *tmp_branch_name = NULL;
6816 char *branch_ref_name = NULL;
6817 char *base_commit_ref_name = NULL;
6818 char *fileindex_path = NULL;
6819 struct check_rebase_ok_arg ok_arg;
6820 struct got_reference *wt_branch = NULL;
6821 struct got_reference *base_commit_ref = NULL;
6823 *tmp_branch = NULL;
6824 *branch_ref = NULL;
6825 *base_commit_id = NULL;
6826 *fileindex = NULL;
6828 err = lock_worktree(worktree, LOCK_EX);
6829 if (err)
6830 return err;
6832 err = open_fileindex(fileindex, &fileindex_path, worktree);
6833 if (err)
6834 goto done;
6836 ok_arg.worktree = worktree;
6837 ok_arg.repo = repo;
6838 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6839 &ok_arg);
6840 if (err)
6841 goto done;
6843 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6844 if (err)
6845 goto done;
6847 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6848 if (err)
6849 goto done;
6851 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6852 worktree);
6853 if (err)
6854 goto done;
6856 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6857 0);
6858 if (err)
6859 goto done;
6861 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6862 if (err)
6863 goto done;
6865 err = got_ref_write(*branch_ref, repo);
6866 if (err)
6867 goto done;
6869 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6870 worktree->base_commit_id);
6871 if (err)
6872 goto done;
6873 err = got_ref_write(base_commit_ref, repo);
6874 if (err)
6875 goto done;
6876 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6877 if (*base_commit_id == NULL) {
6878 err = got_error_from_errno("got_object_id_dup");
6879 goto done;
6882 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6883 worktree->base_commit_id);
6884 if (err)
6885 goto done;
6886 err = got_ref_write(*tmp_branch, repo);
6887 if (err)
6888 goto done;
6890 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6891 if (err)
6892 goto done;
6893 done:
6894 free(fileindex_path);
6895 free(tmp_branch_name);
6896 free(branch_ref_name);
6897 free(base_commit_ref_name);
6898 if (wt_branch)
6899 got_ref_close(wt_branch);
6900 if (err) {
6901 if (*branch_ref) {
6902 got_ref_close(*branch_ref);
6903 *branch_ref = NULL;
6905 if (*tmp_branch) {
6906 got_ref_close(*tmp_branch);
6907 *tmp_branch = NULL;
6909 free(*base_commit_id);
6910 if (*fileindex) {
6911 got_fileindex_free(*fileindex);
6912 *fileindex = NULL;
6914 lock_worktree(worktree, LOCK_SH);
6916 return err;
6919 const struct got_error *
6920 got_worktree_histedit_postpone(struct got_worktree *worktree,
6921 struct got_fileindex *fileindex)
6923 if (fileindex)
6924 got_fileindex_free(fileindex);
6925 return lock_worktree(worktree, LOCK_SH);
6928 const struct got_error *
6929 got_worktree_histedit_in_progress(int *in_progress,
6930 struct got_worktree *worktree)
6932 const struct got_error *err;
6933 char *tmp_branch_name = NULL;
6935 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6936 if (err)
6937 return err;
6939 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6940 free(tmp_branch_name);
6941 return NULL;
6944 const struct got_error *
6945 got_worktree_histedit_continue(struct got_object_id **commit_id,
6946 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6947 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6948 struct got_worktree *worktree, struct got_repository *repo)
6950 const struct got_error *err;
6951 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6952 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6953 struct got_reference *commit_ref = NULL;
6954 struct got_reference *base_commit_ref = NULL;
6955 char *fileindex_path = NULL;
6956 int have_staged_files = 0;
6958 *commit_id = NULL;
6959 *tmp_branch = NULL;
6960 *base_commit_id = NULL;
6961 *fileindex = NULL;
6963 err = lock_worktree(worktree, LOCK_EX);
6964 if (err)
6965 return err;
6967 err = open_fileindex(fileindex, &fileindex_path, worktree);
6968 if (err)
6969 goto done;
6971 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6972 &have_staged_files);
6973 if (err && err->code != GOT_ERR_CANCELLED)
6974 goto done;
6975 if (have_staged_files) {
6976 err = got_error(GOT_ERR_STAGED_PATHS);
6977 goto done;
6980 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6981 if (err)
6982 goto done;
6984 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6985 if (err)
6986 goto done;
6988 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6989 if (err)
6990 goto done;
6992 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6993 worktree);
6994 if (err)
6995 goto done;
6997 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6998 if (err)
6999 goto done;
7001 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7002 if (err)
7003 goto done;
7004 err = got_ref_resolve(commit_id, repo, commit_ref);
7005 if (err)
7006 goto done;
7008 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7009 if (err)
7010 goto done;
7011 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7012 if (err)
7013 goto done;
7015 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7016 if (err)
7017 goto done;
7018 done:
7019 free(commit_ref_name);
7020 free(branch_ref_name);
7021 free(fileindex_path);
7022 if (commit_ref)
7023 got_ref_close(commit_ref);
7024 if (base_commit_ref)
7025 got_ref_close(base_commit_ref);
7026 if (err) {
7027 free(*commit_id);
7028 *commit_id = NULL;
7029 free(*base_commit_id);
7030 *base_commit_id = NULL;
7031 if (*tmp_branch) {
7032 got_ref_close(*tmp_branch);
7033 *tmp_branch = NULL;
7035 if (*fileindex) {
7036 got_fileindex_free(*fileindex);
7037 *fileindex = NULL;
7039 lock_worktree(worktree, LOCK_EX);
7041 return err;
7044 static const struct got_error *
7045 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7047 const struct got_error *err;
7048 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7049 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7051 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7052 if (err)
7053 goto done;
7054 err = delete_ref(tmp_branch_name, repo);
7055 if (err)
7056 goto done;
7058 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7059 worktree);
7060 if (err)
7061 goto done;
7062 err = delete_ref(base_commit_ref_name, repo);
7063 if (err)
7064 goto done;
7066 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7067 if (err)
7068 goto done;
7069 err = delete_ref(branch_ref_name, repo);
7070 if (err)
7071 goto done;
7073 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7074 if (err)
7075 goto done;
7076 err = delete_ref(commit_ref_name, repo);
7077 if (err)
7078 goto done;
7079 done:
7080 free(tmp_branch_name);
7081 free(base_commit_ref_name);
7082 free(branch_ref_name);
7083 free(commit_ref_name);
7084 return err;
7087 const struct got_error *
7088 got_worktree_histedit_abort(struct got_worktree *worktree,
7089 struct got_fileindex *fileindex, struct got_repository *repo,
7090 struct got_reference *branch, struct got_object_id *base_commit_id,
7091 got_worktree_checkout_cb progress_cb, void *progress_arg)
7093 const struct got_error *err, *unlockerr, *sync_err;
7094 struct got_reference *resolved = NULL;
7095 char *fileindex_path = NULL;
7096 struct got_commit_object *commit = NULL;
7097 struct got_object_id *tree_id = NULL;
7098 struct revert_file_args rfa;
7100 err = lock_worktree(worktree, LOCK_EX);
7101 if (err)
7102 return err;
7104 err = got_object_open_as_commit(&commit, repo,
7105 worktree->base_commit_id);
7106 if (err)
7107 goto done;
7109 err = got_ref_open(&resolved, repo,
7110 got_ref_get_symref_target(branch), 0);
7111 if (err)
7112 goto done;
7114 err = got_worktree_set_head_ref(worktree, resolved);
7115 if (err)
7116 goto done;
7118 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7119 if (err)
7120 goto done;
7122 err = got_object_id_by_path(&tree_id, repo, commit,
7123 worktree->path_prefix);
7124 if (err)
7125 goto done;
7127 err = delete_histedit_refs(worktree, repo);
7128 if (err)
7129 goto done;
7131 err = get_fileindex_path(&fileindex_path, worktree);
7132 if (err)
7133 goto done;
7135 rfa.worktree = worktree;
7136 rfa.fileindex = fileindex;
7137 rfa.progress_cb = progress_cb;
7138 rfa.progress_arg = progress_arg;
7139 rfa.patch_cb = NULL;
7140 rfa.patch_arg = NULL;
7141 rfa.repo = repo;
7142 rfa.unlink_added_files = 0;
7143 err = worktree_status(worktree, "", fileindex, repo,
7144 revert_file, &rfa, NULL, NULL, 1, 0);
7145 if (err)
7146 goto sync;
7148 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7149 repo, progress_cb, progress_arg, NULL, NULL);
7150 sync:
7151 sync_err = sync_fileindex(fileindex, fileindex_path);
7152 if (sync_err && err == NULL)
7153 err = sync_err;
7154 done:
7155 got_ref_close(resolved);
7156 free(tree_id);
7157 free(fileindex_path);
7159 unlockerr = lock_worktree(worktree, LOCK_SH);
7160 if (unlockerr && err == NULL)
7161 err = unlockerr;
7162 return err;
7165 const struct got_error *
7166 got_worktree_histedit_complete(struct got_worktree *worktree,
7167 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7168 struct got_reference *edited_branch, struct got_repository *repo)
7170 const struct got_error *err, *unlockerr, *sync_err;
7171 struct got_object_id *new_head_commit_id = NULL;
7172 struct got_reference *resolved = NULL;
7173 char *fileindex_path = NULL;
7175 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7176 if (err)
7177 return err;
7179 err = got_ref_open(&resolved, repo,
7180 got_ref_get_symref_target(edited_branch), 0);
7181 if (err)
7182 goto done;
7184 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7185 resolved, new_head_commit_id, repo);
7186 if (err)
7187 goto done;
7189 err = got_ref_change_ref(resolved, new_head_commit_id);
7190 if (err)
7191 goto done;
7193 err = got_ref_write(resolved, repo);
7194 if (err)
7195 goto done;
7197 err = got_worktree_set_head_ref(worktree, resolved);
7198 if (err)
7199 goto done;
7201 err = delete_histedit_refs(worktree, repo);
7202 if (err)
7203 goto done;
7205 err = get_fileindex_path(&fileindex_path, worktree);
7206 if (err)
7207 goto done;
7208 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7209 sync_err = sync_fileindex(fileindex, fileindex_path);
7210 if (sync_err && err == NULL)
7211 err = sync_err;
7212 done:
7213 got_fileindex_free(fileindex);
7214 free(fileindex_path);
7215 free(new_head_commit_id);
7216 unlockerr = lock_worktree(worktree, LOCK_SH);
7217 if (unlockerr && err == NULL)
7218 err = unlockerr;
7219 return err;
7222 const struct got_error *
7223 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7224 struct got_object_id *commit_id, struct got_repository *repo)
7226 const struct got_error *err;
7227 char *commit_ref_name;
7229 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7230 if (err)
7231 return err;
7233 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7234 if (err)
7235 goto done;
7237 err = delete_ref(commit_ref_name, repo);
7238 done:
7239 free(commit_ref_name);
7240 return err;
7243 const struct got_error *
7244 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7245 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7246 struct got_worktree *worktree, const char *refname,
7247 struct got_repository *repo)
7249 const struct got_error *err = NULL;
7250 char *fileindex_path = NULL;
7251 struct check_rebase_ok_arg ok_arg;
7253 *fileindex = NULL;
7254 *branch_ref = NULL;
7255 *base_branch_ref = NULL;
7257 err = lock_worktree(worktree, LOCK_EX);
7258 if (err)
7259 return err;
7261 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7262 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7263 "cannot integrate a branch into itself; "
7264 "update -b or different branch name required");
7265 goto done;
7268 err = open_fileindex(fileindex, &fileindex_path, worktree);
7269 if (err)
7270 goto done;
7272 /* Preconditions are the same as for rebase. */
7273 ok_arg.worktree = worktree;
7274 ok_arg.repo = repo;
7275 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7276 &ok_arg);
7277 if (err)
7278 goto done;
7280 err = got_ref_open(branch_ref, repo, refname, 1);
7281 if (err)
7282 goto done;
7284 err = got_ref_open(base_branch_ref, repo,
7285 got_worktree_get_head_ref_name(worktree), 1);
7286 done:
7287 if (err) {
7288 if (*branch_ref) {
7289 got_ref_close(*branch_ref);
7290 *branch_ref = NULL;
7292 if (*base_branch_ref) {
7293 got_ref_close(*base_branch_ref);
7294 *base_branch_ref = NULL;
7296 if (*fileindex) {
7297 got_fileindex_free(*fileindex);
7298 *fileindex = NULL;
7300 lock_worktree(worktree, LOCK_SH);
7302 return err;
7305 const struct got_error *
7306 got_worktree_integrate_continue(struct got_worktree *worktree,
7307 struct got_fileindex *fileindex, struct got_repository *repo,
7308 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7309 got_worktree_checkout_cb progress_cb, void *progress_arg,
7310 got_cancel_cb cancel_cb, void *cancel_arg)
7312 const struct got_error *err = NULL, *sync_err, *unlockerr;
7313 char *fileindex_path = NULL;
7314 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7315 struct got_commit_object *commit = NULL;
7317 err = get_fileindex_path(&fileindex_path, worktree);
7318 if (err)
7319 goto done;
7321 err = got_ref_resolve(&commit_id, repo, branch_ref);
7322 if (err)
7323 goto done;
7325 err = got_object_open_as_commit(&commit, repo, commit_id);
7326 if (err)
7327 goto done;
7329 err = got_object_id_by_path(&tree_id, repo, commit,
7330 worktree->path_prefix);
7331 if (err)
7332 goto done;
7334 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7335 if (err)
7336 goto done;
7338 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7339 progress_cb, progress_arg, cancel_cb, cancel_arg);
7340 if (err)
7341 goto sync;
7343 err = got_ref_change_ref(base_branch_ref, commit_id);
7344 if (err)
7345 goto sync;
7347 err = got_ref_write(base_branch_ref, repo);
7348 if (err)
7349 goto sync;
7351 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7352 sync:
7353 sync_err = sync_fileindex(fileindex, fileindex_path);
7354 if (sync_err && err == NULL)
7355 err = sync_err;
7357 done:
7358 unlockerr = got_ref_unlock(branch_ref);
7359 if (unlockerr && err == NULL)
7360 err = unlockerr;
7361 got_ref_close(branch_ref);
7363 unlockerr = got_ref_unlock(base_branch_ref);
7364 if (unlockerr && err == NULL)
7365 err = unlockerr;
7366 got_ref_close(base_branch_ref);
7368 got_fileindex_free(fileindex);
7369 free(fileindex_path);
7370 free(tree_id);
7371 if (commit)
7372 got_object_commit_close(commit);
7374 unlockerr = lock_worktree(worktree, LOCK_SH);
7375 if (unlockerr && err == NULL)
7376 err = unlockerr;
7377 return err;
7380 const struct got_error *
7381 got_worktree_integrate_abort(struct got_worktree *worktree,
7382 struct got_fileindex *fileindex, struct got_repository *repo,
7383 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7385 const struct got_error *err = NULL, *unlockerr = NULL;
7387 got_fileindex_free(fileindex);
7389 err = lock_worktree(worktree, LOCK_SH);
7391 unlockerr = got_ref_unlock(branch_ref);
7392 if (unlockerr && err == NULL)
7393 err = unlockerr;
7394 got_ref_close(branch_ref);
7396 unlockerr = got_ref_unlock(base_branch_ref);
7397 if (unlockerr && err == NULL)
7398 err = unlockerr;
7399 got_ref_close(base_branch_ref);
7401 return err;
7404 const struct got_error *
7405 got_worktree_merge_postpone(struct got_worktree *worktree,
7406 struct got_fileindex *fileindex)
7408 const struct got_error *err, *sync_err;
7409 char *fileindex_path = NULL;
7411 err = get_fileindex_path(&fileindex_path, worktree);
7412 if (err)
7413 goto done;
7415 sync_err = sync_fileindex(fileindex, fileindex_path);
7417 err = lock_worktree(worktree, LOCK_SH);
7418 if (sync_err && err == NULL)
7419 err = sync_err;
7420 done:
7421 got_fileindex_free(fileindex);
7422 free(fileindex_path);
7423 return err;
7426 static const struct got_error *
7427 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7429 const struct got_error *err;
7430 char *branch_refname = NULL, *commit_refname = NULL;
7432 err = get_merge_branch_ref_name(&branch_refname, worktree);
7433 if (err)
7434 goto done;
7435 err = delete_ref(branch_refname, repo);
7436 if (err)
7437 goto done;
7439 err = get_merge_commit_ref_name(&commit_refname, worktree);
7440 if (err)
7441 goto done;
7442 err = delete_ref(commit_refname, repo);
7443 if (err)
7444 goto done;
7446 done:
7447 free(branch_refname);
7448 free(commit_refname);
7449 return err;
7452 struct merge_commit_msg_arg {
7453 struct got_worktree *worktree;
7454 const char *branch_name;
7457 static const struct got_error *
7458 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths, char **logmsg,
7459 void *arg)
7461 struct merge_commit_msg_arg *a = arg;
7463 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7464 got_worktree_get_head_ref_name(a->worktree)) == -1)
7465 return got_error_from_errno("asprintf");
7467 return NULL;
7471 const struct got_error *
7472 got_worktree_merge_branch(struct got_worktree *worktree,
7473 struct got_fileindex *fileindex,
7474 struct got_object_id *yca_commit_id,
7475 struct got_object_id *branch_tip,
7476 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7477 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7479 const struct got_error *err;
7480 char *fileindex_path = NULL;
7482 err = get_fileindex_path(&fileindex_path, worktree);
7483 if (err)
7484 goto done;
7486 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7487 worktree);
7488 if (err)
7489 goto done;
7491 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7492 branch_tip, repo, progress_cb, progress_arg,
7493 cancel_cb, cancel_arg);
7494 done:
7495 free(fileindex_path);
7496 return err;
7499 const struct got_error *
7500 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7501 struct got_worktree *worktree, struct got_fileindex *fileindex,
7502 const char *author, const char *committer, int allow_bad_symlinks,
7503 struct got_object_id *branch_tip, const char *branch_name,
7504 struct got_repository *repo,
7505 got_worktree_status_cb status_cb, void *status_arg)
7508 const struct got_error *err = NULL, *sync_err;
7509 struct got_pathlist_head commitable_paths;
7510 struct collect_commitables_arg cc_arg;
7511 struct got_pathlist_entry *pe;
7512 struct got_reference *head_ref = NULL;
7513 struct got_object_id *head_commit_id = NULL;
7514 int have_staged_files = 0;
7515 struct merge_commit_msg_arg mcm_arg;
7516 char *fileindex_path = NULL;
7518 *new_commit_id = NULL;
7520 TAILQ_INIT(&commitable_paths);
7522 err = get_fileindex_path(&fileindex_path, worktree);
7523 if (err)
7524 goto done;
7526 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7527 if (err)
7528 goto done;
7530 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7531 if (err)
7532 goto done;
7534 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7535 &have_staged_files);
7536 if (err && err->code != GOT_ERR_CANCELLED)
7537 goto done;
7538 if (have_staged_files) {
7539 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7540 goto done;
7543 cc_arg.commitable_paths = &commitable_paths;
7544 cc_arg.worktree = worktree;
7545 cc_arg.fileindex = fileindex;
7546 cc_arg.repo = repo;
7547 cc_arg.have_staged_files = have_staged_files;
7548 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7549 err = worktree_status(worktree, "", fileindex, repo,
7550 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7551 if (err)
7552 goto done;
7554 if (TAILQ_EMPTY(&commitable_paths)) {
7555 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7556 "merge of %s cannot proceed", branch_name);
7557 goto done;
7560 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7561 struct got_commitable *ct = pe->data;
7562 const char *ct_path = ct->in_repo_path;
7564 while (ct_path[0] == '/')
7565 ct_path++;
7566 err = check_out_of_date(ct_path, ct->status,
7567 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7568 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7569 if (err)
7570 goto done;
7574 mcm_arg.worktree = worktree;
7575 mcm_arg.branch_name = branch_name;
7576 err = commit_worktree(new_commit_id, &commitable_paths,
7577 head_commit_id, branch_tip, worktree, author, committer,
7578 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7579 if (err)
7580 goto done;
7582 err = update_fileindex_after_commit(worktree, &commitable_paths,
7583 *new_commit_id, fileindex, have_staged_files);
7584 sync_err = sync_fileindex(fileindex, fileindex_path);
7585 if (sync_err && err == NULL)
7586 err = sync_err;
7587 done:
7588 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7589 struct got_commitable *ct = pe->data;
7590 free_commitable(ct);
7592 got_pathlist_free(&commitable_paths);
7593 free(fileindex_path);
7594 return err;
7597 const struct got_error *
7598 got_worktree_merge_complete(struct got_worktree *worktree,
7599 struct got_fileindex *fileindex, struct got_repository *repo)
7601 const struct got_error *err, *unlockerr, *sync_err;
7602 char *fileindex_path = NULL;
7604 err = delete_merge_refs(worktree, repo);
7605 if (err)
7606 goto done;
7608 err = get_fileindex_path(&fileindex_path, worktree);
7609 if (err)
7610 goto done;
7611 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7612 sync_err = sync_fileindex(fileindex, fileindex_path);
7613 if (sync_err && err == NULL)
7614 err = sync_err;
7615 done:
7616 got_fileindex_free(fileindex);
7617 free(fileindex_path);
7618 unlockerr = lock_worktree(worktree, LOCK_SH);
7619 if (unlockerr && err == NULL)
7620 err = unlockerr;
7621 return err;
7624 const struct got_error *
7625 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7626 struct got_repository *repo)
7628 const struct got_error *err;
7629 char *branch_refname = NULL;
7630 struct got_reference *branch_ref = NULL;
7632 *in_progress = 0;
7634 err = get_merge_branch_ref_name(&branch_refname, worktree);
7635 if (err)
7636 return err;
7637 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7638 free(branch_refname);
7639 if (err) {
7640 if (err->code != GOT_ERR_NOT_REF)
7641 return err;
7642 } else
7643 *in_progress = 1;
7645 return NULL;
7648 const struct got_error *got_worktree_merge_prepare(
7649 struct got_fileindex **fileindex, struct got_worktree *worktree,
7650 struct got_reference *branch, struct got_repository *repo)
7652 const struct got_error *err = NULL;
7653 char *fileindex_path = NULL;
7654 char *branch_refname = NULL, *commit_refname = NULL;
7655 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
7656 struct got_reference *commit_ref = NULL;
7657 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
7658 struct check_rebase_ok_arg ok_arg;
7660 *fileindex = NULL;
7662 err = lock_worktree(worktree, LOCK_EX);
7663 if (err)
7664 return err;
7666 err = open_fileindex(fileindex, &fileindex_path, worktree);
7667 if (err)
7668 goto done;
7670 /* Preconditions are the same as for rebase. */
7671 ok_arg.worktree = worktree;
7672 ok_arg.repo = repo;
7673 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7674 &ok_arg);
7675 if (err)
7676 goto done;
7678 err = get_merge_branch_ref_name(&branch_refname, worktree);
7679 if (err)
7680 return err;
7682 err = get_merge_commit_ref_name(&commit_refname, worktree);
7683 if (err)
7684 return err;
7686 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7687 0);
7688 if (err)
7689 goto done;
7691 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
7692 if (err)
7693 goto done;
7695 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
7696 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
7697 goto done;
7700 err = got_ref_resolve(&branch_tip, repo, branch);
7701 if (err)
7702 goto done;
7704 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
7705 if (err)
7706 goto done;
7707 err = got_ref_write(branch_ref, repo);
7708 if (err)
7709 goto done;
7711 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
7712 if (err)
7713 goto done;
7714 err = got_ref_write(commit_ref, repo);
7715 if (err)
7716 goto done;
7718 done:
7719 free(branch_refname);
7720 free(commit_refname);
7721 free(fileindex_path);
7722 if (branch_ref)
7723 got_ref_close(branch_ref);
7724 if (commit_ref)
7725 got_ref_close(commit_ref);
7726 if (wt_branch)
7727 got_ref_close(wt_branch);
7728 free(wt_branch_tip);
7729 if (err) {
7730 if (*fileindex) {
7731 got_fileindex_free(*fileindex);
7732 *fileindex = NULL;
7734 lock_worktree(worktree, LOCK_SH);
7736 return err;
7739 const struct got_error *
7740 got_worktree_merge_continue(char **branch_name,
7741 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
7742 struct got_worktree *worktree, struct got_repository *repo)
7744 const struct got_error *err;
7745 char *commit_refname = NULL, *branch_refname = NULL;
7746 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
7747 char *fileindex_path = NULL;
7748 int have_staged_files = 0;
7750 *branch_name = NULL;
7751 *branch_tip = NULL;
7752 *fileindex = NULL;
7754 err = lock_worktree(worktree, LOCK_EX);
7755 if (err)
7756 return err;
7758 err = open_fileindex(fileindex, &fileindex_path, worktree);
7759 if (err)
7760 goto done;
7762 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7763 &have_staged_files);
7764 if (err && err->code != GOT_ERR_CANCELLED)
7765 goto done;
7766 if (have_staged_files) {
7767 err = got_error(GOT_ERR_STAGED_PATHS);
7768 goto done;
7771 err = get_merge_branch_ref_name(&branch_refname, worktree);
7772 if (err)
7773 goto done;
7775 err = get_merge_commit_ref_name(&commit_refname, worktree);
7776 if (err)
7777 goto done;
7779 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7780 if (err)
7781 goto done;
7783 if (!got_ref_is_symbolic(branch_ref)) {
7784 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7785 "%s is not a symbolic reference",
7786 got_ref_get_name(branch_ref));
7787 goto done;
7789 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
7790 if (*branch_name == NULL) {
7791 err = got_error_from_errno("strdup");
7792 goto done;
7795 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
7796 if (err)
7797 goto done;
7799 err = got_ref_resolve(branch_tip, repo, commit_ref);
7800 if (err)
7801 goto done;
7802 done:
7803 free(commit_refname);
7804 free(branch_refname);
7805 free(fileindex_path);
7806 if (commit_ref)
7807 got_ref_close(commit_ref);
7808 if (branch_ref)
7809 got_ref_close(branch_ref);
7810 if (err) {
7811 if (*branch_name) {
7812 free(*branch_name);
7813 *branch_name = NULL;
7815 free(*branch_tip);
7816 *branch_tip = NULL;
7817 if (*fileindex) {
7818 got_fileindex_free(*fileindex);
7819 *fileindex = NULL;
7821 lock_worktree(worktree, LOCK_SH);
7823 return err;
7826 const struct got_error *
7827 got_worktree_merge_abort(struct got_worktree *worktree,
7828 struct got_fileindex *fileindex, struct got_repository *repo,
7829 got_worktree_checkout_cb progress_cb, void *progress_arg)
7831 const struct got_error *err, *unlockerr, *sync_err;
7832 struct got_object_id *commit_id = NULL;
7833 struct got_commit_object *commit = NULL;
7834 char *fileindex_path = NULL;
7835 struct revert_file_args rfa;
7836 struct got_object_id *tree_id = NULL;
7838 err = got_object_open_as_commit(&commit, repo,
7839 worktree->base_commit_id);
7840 if (err)
7841 goto done;
7843 err = got_object_id_by_path(&tree_id, repo, commit,
7844 worktree->path_prefix);
7845 if (err)
7846 goto done;
7848 err = delete_merge_refs(worktree, repo);
7849 if (err)
7850 goto done;
7852 err = get_fileindex_path(&fileindex_path, worktree);
7853 if (err)
7854 goto done;
7856 rfa.worktree = worktree;
7857 rfa.fileindex = fileindex;
7858 rfa.progress_cb = progress_cb;
7859 rfa.progress_arg = progress_arg;
7860 rfa.patch_cb = NULL;
7861 rfa.patch_arg = NULL;
7862 rfa.repo = repo;
7863 rfa.unlink_added_files = 1;
7864 err = worktree_status(worktree, "", fileindex, repo,
7865 revert_file, &rfa, NULL, NULL, 1, 0);
7866 if (err)
7867 goto sync;
7869 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7870 repo, progress_cb, progress_arg, NULL, NULL);
7871 sync:
7872 sync_err = sync_fileindex(fileindex, fileindex_path);
7873 if (sync_err && err == NULL)
7874 err = sync_err;
7875 done:
7876 free(tree_id);
7877 free(commit_id);
7878 if (commit)
7879 got_object_commit_close(commit);
7880 if (fileindex)
7881 got_fileindex_free(fileindex);
7882 free(fileindex_path);
7884 unlockerr = lock_worktree(worktree, LOCK_SH);
7885 if (unlockerr && err == NULL)
7886 err = unlockerr;
7887 return err;
7890 struct check_stage_ok_arg {
7891 struct got_object_id *head_commit_id;
7892 struct got_worktree *worktree;
7893 struct got_fileindex *fileindex;
7894 struct got_repository *repo;
7895 int have_changes;
7898 const struct got_error *
7899 check_stage_ok(void *arg, unsigned char status,
7900 unsigned char staged_status, const char *relpath,
7901 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7902 struct got_object_id *commit_id, int dirfd, const char *de_name)
7904 struct check_stage_ok_arg *a = arg;
7905 const struct got_error *err = NULL;
7906 struct got_fileindex_entry *ie;
7907 struct got_object_id base_commit_id;
7908 struct got_object_id *base_commit_idp = NULL;
7909 char *in_repo_path = NULL, *p;
7911 if (status == GOT_STATUS_UNVERSIONED ||
7912 status == GOT_STATUS_NO_CHANGE)
7913 return NULL;
7914 if (status == GOT_STATUS_NONEXISTENT)
7915 return got_error_set_errno(ENOENT, relpath);
7917 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7918 if (ie == NULL)
7919 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7921 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7922 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7923 relpath) == -1)
7924 return got_error_from_errno("asprintf");
7926 if (got_fileindex_entry_has_commit(ie)) {
7927 memcpy(base_commit_id.sha1, ie->commit_sha1,
7928 SHA1_DIGEST_LENGTH);
7929 base_commit_idp = &base_commit_id;
7932 if (status == GOT_STATUS_CONFLICT) {
7933 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7934 goto done;
7935 } else if (status != GOT_STATUS_ADD &&
7936 status != GOT_STATUS_MODIFY &&
7937 status != GOT_STATUS_DELETE) {
7938 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7939 goto done;
7942 a->have_changes = 1;
7944 p = in_repo_path;
7945 while (p[0] == '/')
7946 p++;
7947 err = check_out_of_date(p, status, staged_status,
7948 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7949 GOT_ERR_STAGE_OUT_OF_DATE);
7950 done:
7951 free(in_repo_path);
7952 return err;
7955 struct stage_path_arg {
7956 struct got_worktree *worktree;
7957 struct got_fileindex *fileindex;
7958 struct got_repository *repo;
7959 got_worktree_status_cb status_cb;
7960 void *status_arg;
7961 got_worktree_patch_cb patch_cb;
7962 void *patch_arg;
7963 int staged_something;
7964 int allow_bad_symlinks;
7967 static const struct got_error *
7968 stage_path(void *arg, unsigned char status,
7969 unsigned char staged_status, const char *relpath,
7970 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7971 struct got_object_id *commit_id, int dirfd, const char *de_name)
7973 struct stage_path_arg *a = arg;
7974 const struct got_error *err = NULL;
7975 struct got_fileindex_entry *ie;
7976 char *ondisk_path = NULL, *path_content = NULL;
7977 uint32_t stage;
7978 struct got_object_id *new_staged_blob_id = NULL;
7979 struct stat sb;
7981 if (status == GOT_STATUS_UNVERSIONED)
7982 return NULL;
7984 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7985 if (ie == NULL)
7986 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7988 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7989 relpath)== -1)
7990 return got_error_from_errno("asprintf");
7992 switch (status) {
7993 case GOT_STATUS_ADD:
7994 case GOT_STATUS_MODIFY:
7995 /* XXX could sb.st_mode be passed in by our caller? */
7996 if (lstat(ondisk_path, &sb) == -1) {
7997 err = got_error_from_errno2("lstat", ondisk_path);
7998 break;
8000 if (a->patch_cb) {
8001 if (status == GOT_STATUS_ADD) {
8002 int choice = GOT_PATCH_CHOICE_NONE;
8003 err = (*a->patch_cb)(&choice, a->patch_arg,
8004 status, ie->path, NULL, 1, 1);
8005 if (err)
8006 break;
8007 if (choice != GOT_PATCH_CHOICE_YES)
8008 break;
8009 } else {
8010 err = create_patched_content(&path_content, 0,
8011 staged_blob_id ? staged_blob_id : blob_id,
8012 ondisk_path, dirfd, de_name, ie->path,
8013 a->repo, a->patch_cb, a->patch_arg);
8014 if (err || path_content == NULL)
8015 break;
8018 err = got_object_blob_create(&new_staged_blob_id,
8019 path_content ? path_content : ondisk_path, a->repo);
8020 if (err)
8021 break;
8022 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8023 SHA1_DIGEST_LENGTH);
8024 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8025 stage = GOT_FILEIDX_STAGE_ADD;
8026 else
8027 stage = GOT_FILEIDX_STAGE_MODIFY;
8028 got_fileindex_entry_stage_set(ie, stage);
8029 if (S_ISLNK(sb.st_mode)) {
8030 int is_bad_symlink = 0;
8031 if (!a->allow_bad_symlinks) {
8032 char target_path[PATH_MAX];
8033 ssize_t target_len;
8034 target_len = readlink(ondisk_path, target_path,
8035 sizeof(target_path));
8036 if (target_len == -1) {
8037 err = got_error_from_errno2("readlink",
8038 ondisk_path);
8039 break;
8041 err = is_bad_symlink_target(&is_bad_symlink,
8042 target_path, target_len, ondisk_path,
8043 a->worktree->root_path);
8044 if (err)
8045 break;
8046 if (is_bad_symlink) {
8047 err = got_error_path(ondisk_path,
8048 GOT_ERR_BAD_SYMLINK);
8049 break;
8052 if (is_bad_symlink)
8053 got_fileindex_entry_staged_filetype_set(ie,
8054 GOT_FILEIDX_MODE_BAD_SYMLINK);
8055 else
8056 got_fileindex_entry_staged_filetype_set(ie,
8057 GOT_FILEIDX_MODE_SYMLINK);
8058 } else {
8059 got_fileindex_entry_staged_filetype_set(ie,
8060 GOT_FILEIDX_MODE_REGULAR_FILE);
8062 a->staged_something = 1;
8063 if (a->status_cb == NULL)
8064 break;
8065 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8066 get_staged_status(ie), relpath, blob_id,
8067 new_staged_blob_id, NULL, dirfd, de_name);
8068 break;
8069 case GOT_STATUS_DELETE:
8070 if (staged_status == GOT_STATUS_DELETE)
8071 break;
8072 if (a->patch_cb) {
8073 int choice = GOT_PATCH_CHOICE_NONE;
8074 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8075 ie->path, NULL, 1, 1);
8076 if (err)
8077 break;
8078 if (choice == GOT_PATCH_CHOICE_NO)
8079 break;
8080 if (choice != GOT_PATCH_CHOICE_YES) {
8081 err = got_error(GOT_ERR_PATCH_CHOICE);
8082 break;
8085 stage = GOT_FILEIDX_STAGE_DELETE;
8086 got_fileindex_entry_stage_set(ie, stage);
8087 a->staged_something = 1;
8088 if (a->status_cb == NULL)
8089 break;
8090 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8091 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8092 de_name);
8093 break;
8094 case GOT_STATUS_NO_CHANGE:
8095 break;
8096 case GOT_STATUS_CONFLICT:
8097 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8098 break;
8099 case GOT_STATUS_NONEXISTENT:
8100 err = got_error_set_errno(ENOENT, relpath);
8101 break;
8102 default:
8103 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8104 break;
8107 if (path_content && unlink(path_content) == -1 && err == NULL)
8108 err = got_error_from_errno2("unlink", path_content);
8109 free(path_content);
8110 free(ondisk_path);
8111 free(new_staged_blob_id);
8112 return err;
8115 const struct got_error *
8116 got_worktree_stage(struct got_worktree *worktree,
8117 struct got_pathlist_head *paths,
8118 got_worktree_status_cb status_cb, void *status_arg,
8119 got_worktree_patch_cb patch_cb, void *patch_arg,
8120 int allow_bad_symlinks, struct got_repository *repo)
8122 const struct got_error *err = NULL, *sync_err, *unlockerr;
8123 struct got_pathlist_entry *pe;
8124 struct got_fileindex *fileindex = NULL;
8125 char *fileindex_path = NULL;
8126 struct got_reference *head_ref = NULL;
8127 struct got_object_id *head_commit_id = NULL;
8128 struct check_stage_ok_arg oka;
8129 struct stage_path_arg spa;
8131 err = lock_worktree(worktree, LOCK_EX);
8132 if (err)
8133 return err;
8135 err = got_ref_open(&head_ref, repo,
8136 got_worktree_get_head_ref_name(worktree), 0);
8137 if (err)
8138 goto done;
8139 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8140 if (err)
8141 goto done;
8142 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8143 if (err)
8144 goto done;
8146 /* Check pre-conditions before staging anything. */
8147 oka.head_commit_id = head_commit_id;
8148 oka.worktree = worktree;
8149 oka.fileindex = fileindex;
8150 oka.repo = repo;
8151 oka.have_changes = 0;
8152 TAILQ_FOREACH(pe, paths, entry) {
8153 err = worktree_status(worktree, pe->path, fileindex, repo,
8154 check_stage_ok, &oka, NULL, NULL, 1, 0);
8155 if (err)
8156 goto done;
8158 if (!oka.have_changes) {
8159 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8160 goto done;
8163 spa.worktree = worktree;
8164 spa.fileindex = fileindex;
8165 spa.repo = repo;
8166 spa.patch_cb = patch_cb;
8167 spa.patch_arg = patch_arg;
8168 spa.status_cb = status_cb;
8169 spa.status_arg = status_arg;
8170 spa.staged_something = 0;
8171 spa.allow_bad_symlinks = allow_bad_symlinks;
8172 TAILQ_FOREACH(pe, paths, entry) {
8173 err = worktree_status(worktree, pe->path, fileindex, repo,
8174 stage_path, &spa, NULL, NULL, 1, 0);
8175 if (err)
8176 goto done;
8178 if (!spa.staged_something) {
8179 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8180 goto done;
8183 sync_err = sync_fileindex(fileindex, fileindex_path);
8184 if (sync_err && err == NULL)
8185 err = sync_err;
8186 done:
8187 if (head_ref)
8188 got_ref_close(head_ref);
8189 free(head_commit_id);
8190 free(fileindex_path);
8191 if (fileindex)
8192 got_fileindex_free(fileindex);
8193 unlockerr = lock_worktree(worktree, LOCK_SH);
8194 if (unlockerr && err == NULL)
8195 err = unlockerr;
8196 return err;
8199 struct unstage_path_arg {
8200 struct got_worktree *worktree;
8201 struct got_fileindex *fileindex;
8202 struct got_repository *repo;
8203 got_worktree_checkout_cb progress_cb;
8204 void *progress_arg;
8205 got_worktree_patch_cb patch_cb;
8206 void *patch_arg;
8209 static const struct got_error *
8210 create_unstaged_content(char **path_unstaged_content,
8211 char **path_new_staged_content, struct got_object_id *blob_id,
8212 struct got_object_id *staged_blob_id, const char *relpath,
8213 struct got_repository *repo,
8214 got_worktree_patch_cb patch_cb, void *patch_arg)
8216 const struct got_error *err, *free_err;
8217 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8218 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8219 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8220 struct got_diffreg_result *diffreg_result = NULL;
8221 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8222 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8224 *path_unstaged_content = NULL;
8225 *path_new_staged_content = NULL;
8227 err = got_object_id_str(&label1, blob_id);
8228 if (err)
8229 return err;
8230 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
8231 if (err)
8232 goto done;
8234 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
8235 if (err)
8236 goto done;
8238 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8239 if (err)
8240 goto done;
8242 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
8243 if (err)
8244 goto done;
8246 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
8247 if (err)
8248 goto done;
8250 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8251 if (err)
8252 goto done;
8254 err = got_diff_files(&diffreg_result, f1, label1, f2,
8255 path2, 3, 0, 1, NULL);
8256 if (err)
8257 goto done;
8259 err = got_opentemp_named(path_unstaged_content, &outfile,
8260 "got-unstaged-content");
8261 if (err)
8262 goto done;
8263 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8264 "got-new-staged-content");
8265 if (err)
8266 goto done;
8268 if (fseek(f1, 0L, SEEK_SET) == -1) {
8269 err = got_ferror(f1, GOT_ERR_IO);
8270 goto done;
8272 if (fseek(f2, 0L, SEEK_SET) == -1) {
8273 err = got_ferror(f2, GOT_ERR_IO);
8274 goto done;
8276 /* Count the number of actual changes in the diff result. */
8277 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8278 struct diff_chunk_context cc = {};
8279 diff_chunk_context_load_change(&cc, &nchunks_used,
8280 diffreg_result->result, n, 0);
8281 nchanges++;
8283 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8284 int choice;
8285 err = apply_or_reject_change(&choice, &nchunks_used,
8286 diffreg_result->result, n, relpath, f1, f2,
8287 &line_cur1, &line_cur2,
8288 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8289 if (err)
8290 goto done;
8291 if (choice == GOT_PATCH_CHOICE_YES)
8292 have_content = 1;
8293 else
8294 have_rejected_content = 1;
8295 if (choice == GOT_PATCH_CHOICE_QUIT)
8296 break;
8298 if (have_content || have_rejected_content)
8299 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8300 outfile, rejectfile);
8301 done:
8302 free(label1);
8303 if (blob)
8304 got_object_blob_close(blob);
8305 if (staged_blob)
8306 got_object_blob_close(staged_blob);
8307 free_err = got_diffreg_result_free(diffreg_result);
8308 if (free_err && err == NULL)
8309 err = free_err;
8310 if (f1 && fclose(f1) == EOF && err == NULL)
8311 err = got_error_from_errno2("fclose", path1);
8312 if (f2 && fclose(f2) == EOF && err == NULL)
8313 err = got_error_from_errno2("fclose", path2);
8314 if (outfile && fclose(outfile) == EOF && err == NULL)
8315 err = got_error_from_errno2("fclose", *path_unstaged_content);
8316 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8317 err = got_error_from_errno2("fclose", *path_new_staged_content);
8318 if (path1 && unlink(path1) == -1 && err == NULL)
8319 err = got_error_from_errno2("unlink", path1);
8320 if (path2 && unlink(path2) == -1 && err == NULL)
8321 err = got_error_from_errno2("unlink", path2);
8322 if (err || !have_content) {
8323 if (*path_unstaged_content &&
8324 unlink(*path_unstaged_content) == -1 && err == NULL)
8325 err = got_error_from_errno2("unlink",
8326 *path_unstaged_content);
8327 free(*path_unstaged_content);
8328 *path_unstaged_content = NULL;
8330 if (err || !have_content || !have_rejected_content) {
8331 if (*path_new_staged_content &&
8332 unlink(*path_new_staged_content) == -1 && err == NULL)
8333 err = got_error_from_errno2("unlink",
8334 *path_new_staged_content);
8335 free(*path_new_staged_content);
8336 *path_new_staged_content = NULL;
8338 free(path1);
8339 free(path2);
8340 return err;
8343 static const struct got_error *
8344 unstage_hunks(struct got_object_id *staged_blob_id,
8345 struct got_blob_object *blob_base,
8346 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8347 const char *ondisk_path, const char *label_orig,
8348 struct got_worktree *worktree, struct got_repository *repo,
8349 got_worktree_patch_cb patch_cb, void *patch_arg,
8350 got_worktree_checkout_cb progress_cb, void *progress_arg)
8352 const struct got_error *err = NULL;
8353 char *path_unstaged_content = NULL;
8354 char *path_new_staged_content = NULL;
8355 char *parent = NULL, *base_path = NULL;
8356 char *blob_base_path = NULL;
8357 struct got_object_id *new_staged_blob_id = NULL;
8358 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8359 struct stat sb;
8361 err = create_unstaged_content(&path_unstaged_content,
8362 &path_new_staged_content, blob_id, staged_blob_id,
8363 ie->path, repo, patch_cb, patch_arg);
8364 if (err)
8365 return err;
8367 if (path_unstaged_content == NULL)
8368 return NULL;
8370 if (path_new_staged_content) {
8371 err = got_object_blob_create(&new_staged_blob_id,
8372 path_new_staged_content, repo);
8373 if (err)
8374 goto done;
8377 f = fopen(path_unstaged_content, "re");
8378 if (f == NULL) {
8379 err = got_error_from_errno2("fopen",
8380 path_unstaged_content);
8381 goto done;
8383 if (fstat(fileno(f), &sb) == -1) {
8384 err = got_error_from_errno2("fstat", path_unstaged_content);
8385 goto done;
8387 if (got_fileindex_entry_staged_filetype_get(ie) ==
8388 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8389 char link_target[PATH_MAX];
8390 size_t r;
8391 r = fread(link_target, 1, sizeof(link_target), f);
8392 if (r == 0 && ferror(f)) {
8393 err = got_error_from_errno("fread");
8394 goto done;
8396 if (r >= sizeof(link_target)) { /* should not happen */
8397 err = got_error(GOT_ERR_NO_SPACE);
8398 goto done;
8400 link_target[r] = '\0';
8401 err = merge_symlink(worktree, blob_base,
8402 ondisk_path, ie->path, label_orig, link_target,
8403 worktree->base_commit_id, repo, progress_cb,
8404 progress_arg);
8405 } else {
8406 int local_changes_subsumed;
8408 err = got_path_dirname(&parent, ondisk_path);
8409 if (err)
8410 return err;
8412 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8413 parent) == -1) {
8414 err = got_error_from_errno("asprintf");
8415 base_path = NULL;
8416 goto done;
8419 err = got_opentemp_named(&blob_base_path, &f_base, base_path);
8420 if (err)
8421 goto done;
8422 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8423 blob_base);
8424 if (err)
8425 goto done;
8428 * In order the run a 3-way merge with a symlink we copy the symlink's
8429 * target path into a temporary file and use that file with diff3.
8431 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8432 err = dump_symlink_target_path_to_file(&f_deriv2,
8433 ondisk_path);
8434 if (err)
8435 goto done;
8436 } else {
8437 int fd;
8438 fd = open(ondisk_path,
8439 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8440 if (fd == -1) {
8441 err = got_error_from_errno2("open", ondisk_path);
8442 goto done;
8444 f_deriv2 = fdopen(fd, "r");
8445 if (f_deriv2 == NULL) {
8446 err = got_error_from_errno2("fdopen", ondisk_path);
8447 close(fd);
8448 goto done;
8452 err = merge_file(&local_changes_subsumed, worktree,
8453 f_base, f, f_deriv2, ondisk_path, ie->path,
8454 got_fileindex_perms_to_st(ie),
8455 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8456 repo, progress_cb, progress_arg);
8458 if (err)
8459 goto done;
8461 if (new_staged_blob_id) {
8462 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8463 SHA1_DIGEST_LENGTH);
8464 } else {
8465 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8466 got_fileindex_entry_staged_filetype_set(ie, 0);
8468 done:
8469 free(new_staged_blob_id);
8470 if (path_unstaged_content &&
8471 unlink(path_unstaged_content) == -1 && err == NULL)
8472 err = got_error_from_errno2("unlink", path_unstaged_content);
8473 if (path_new_staged_content &&
8474 unlink(path_new_staged_content) == -1 && err == NULL)
8475 err = got_error_from_errno2("unlink", path_new_staged_content);
8476 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8477 err = got_error_from_errno2("unlink", blob_base_path);
8478 if (f_base && fclose(f_base) == EOF && err == NULL)
8479 err = got_error_from_errno2("fclose", path_unstaged_content);
8480 if (f && fclose(f) == EOF && err == NULL)
8481 err = got_error_from_errno2("fclose", path_unstaged_content);
8482 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8483 err = got_error_from_errno2("fclose", ondisk_path);
8484 free(path_unstaged_content);
8485 free(path_new_staged_content);
8486 free(blob_base_path);
8487 free(parent);
8488 free(base_path);
8489 return err;
8492 static const struct got_error *
8493 unstage_path(void *arg, unsigned char status,
8494 unsigned char staged_status, const char *relpath,
8495 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8496 struct got_object_id *commit_id, int dirfd, const char *de_name)
8498 const struct got_error *err = NULL;
8499 struct unstage_path_arg *a = arg;
8500 struct got_fileindex_entry *ie;
8501 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8502 char *ondisk_path = NULL;
8503 char *id_str = NULL, *label_orig = NULL;
8504 int local_changes_subsumed;
8505 struct stat sb;
8507 if (staged_status != GOT_STATUS_ADD &&
8508 staged_status != GOT_STATUS_MODIFY &&
8509 staged_status != GOT_STATUS_DELETE)
8510 return NULL;
8512 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8513 if (ie == NULL)
8514 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8516 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8517 == -1)
8518 return got_error_from_errno("asprintf");
8520 err = got_object_id_str(&id_str,
8521 commit_id ? commit_id : a->worktree->base_commit_id);
8522 if (err)
8523 goto done;
8524 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8525 id_str) == -1) {
8526 err = got_error_from_errno("asprintf");
8527 goto done;
8530 switch (staged_status) {
8531 case GOT_STATUS_MODIFY:
8532 err = got_object_open_as_blob(&blob_base, a->repo,
8533 blob_id, 8192);
8534 if (err)
8535 break;
8536 /* fall through */
8537 case GOT_STATUS_ADD:
8538 if (a->patch_cb) {
8539 if (staged_status == GOT_STATUS_ADD) {
8540 int choice = GOT_PATCH_CHOICE_NONE;
8541 err = (*a->patch_cb)(&choice, a->patch_arg,
8542 staged_status, ie->path, NULL, 1, 1);
8543 if (err)
8544 break;
8545 if (choice != GOT_PATCH_CHOICE_YES)
8546 break;
8547 } else {
8548 err = unstage_hunks(staged_blob_id,
8549 blob_base, blob_id, ie, ondisk_path,
8550 label_orig, a->worktree, a->repo,
8551 a->patch_cb, a->patch_arg,
8552 a->progress_cb, a->progress_arg);
8553 break; /* Done with this file. */
8556 err = got_object_open_as_blob(&blob_staged, a->repo,
8557 staged_blob_id, 8192);
8558 if (err)
8559 break;
8560 switch (got_fileindex_entry_staged_filetype_get(ie)) {
8561 case GOT_FILEIDX_MODE_BAD_SYMLINK:
8562 case GOT_FILEIDX_MODE_REGULAR_FILE:
8563 err = merge_blob(&local_changes_subsumed, a->worktree,
8564 blob_base, ondisk_path, relpath,
8565 got_fileindex_perms_to_st(ie), label_orig,
8566 blob_staged, commit_id ? commit_id :
8567 a->worktree->base_commit_id, a->repo,
8568 a->progress_cb, a->progress_arg);
8569 break;
8570 case GOT_FILEIDX_MODE_SYMLINK:
8571 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8572 char *staged_target;
8573 err = got_object_blob_read_to_str(
8574 &staged_target, blob_staged);
8575 if (err)
8576 goto done;
8577 err = merge_symlink(a->worktree, blob_base,
8578 ondisk_path, relpath, label_orig,
8579 staged_target, commit_id ? commit_id :
8580 a->worktree->base_commit_id,
8581 a->repo, a->progress_cb, a->progress_arg);
8582 free(staged_target);
8583 } else {
8584 err = merge_blob(&local_changes_subsumed,
8585 a->worktree, blob_base, ondisk_path,
8586 relpath, got_fileindex_perms_to_st(ie),
8587 label_orig, blob_staged,
8588 commit_id ? commit_id :
8589 a->worktree->base_commit_id, a->repo,
8590 a->progress_cb, a->progress_arg);
8592 break;
8593 default:
8594 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8595 break;
8597 if (err == NULL) {
8598 got_fileindex_entry_stage_set(ie,
8599 GOT_FILEIDX_STAGE_NONE);
8600 got_fileindex_entry_staged_filetype_set(ie, 0);
8602 break;
8603 case GOT_STATUS_DELETE:
8604 if (a->patch_cb) {
8605 int choice = GOT_PATCH_CHOICE_NONE;
8606 err = (*a->patch_cb)(&choice, a->patch_arg,
8607 staged_status, ie->path, NULL, 1, 1);
8608 if (err)
8609 break;
8610 if (choice == GOT_PATCH_CHOICE_NO)
8611 break;
8612 if (choice != GOT_PATCH_CHOICE_YES) {
8613 err = got_error(GOT_ERR_PATCH_CHOICE);
8614 break;
8617 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8618 got_fileindex_entry_staged_filetype_set(ie, 0);
8619 err = get_file_status(&status, &sb, ie, ondisk_path,
8620 dirfd, de_name, a->repo);
8621 if (err)
8622 break;
8623 err = (*a->progress_cb)(a->progress_arg, status, relpath);
8624 break;
8626 done:
8627 free(ondisk_path);
8628 if (blob_base)
8629 got_object_blob_close(blob_base);
8630 if (blob_staged)
8631 got_object_blob_close(blob_staged);
8632 free(id_str);
8633 free(label_orig);
8634 return err;
8637 const struct got_error *
8638 got_worktree_unstage(struct got_worktree *worktree,
8639 struct got_pathlist_head *paths,
8640 got_worktree_checkout_cb progress_cb, void *progress_arg,
8641 got_worktree_patch_cb patch_cb, void *patch_arg,
8642 struct got_repository *repo)
8644 const struct got_error *err = NULL, *sync_err, *unlockerr;
8645 struct got_pathlist_entry *pe;
8646 struct got_fileindex *fileindex = NULL;
8647 char *fileindex_path = NULL;
8648 struct unstage_path_arg upa;
8650 err = lock_worktree(worktree, LOCK_EX);
8651 if (err)
8652 return err;
8654 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8655 if (err)
8656 goto done;
8658 upa.worktree = worktree;
8659 upa.fileindex = fileindex;
8660 upa.repo = repo;
8661 upa.progress_cb = progress_cb;
8662 upa.progress_arg = progress_arg;
8663 upa.patch_cb = patch_cb;
8664 upa.patch_arg = patch_arg;
8665 TAILQ_FOREACH(pe, paths, entry) {
8666 err = worktree_status(worktree, pe->path, fileindex, repo,
8667 unstage_path, &upa, NULL, NULL, 1, 0);
8668 if (err)
8669 goto done;
8672 sync_err = sync_fileindex(fileindex, fileindex_path);
8673 if (sync_err && err == NULL)
8674 err = sync_err;
8675 done:
8676 free(fileindex_path);
8677 if (fileindex)
8678 got_fileindex_free(fileindex);
8679 unlockerr = lock_worktree(worktree, LOCK_SH);
8680 if (unlockerr && err == NULL)
8681 err = unlockerr;
8682 return err;
8685 struct report_file_info_arg {
8686 struct got_worktree *worktree;
8687 got_worktree_path_info_cb info_cb;
8688 void *info_arg;
8689 struct got_pathlist_head *paths;
8690 got_cancel_cb cancel_cb;
8691 void *cancel_arg;
8694 static const struct got_error *
8695 report_file_info(void *arg, struct got_fileindex_entry *ie)
8697 struct report_file_info_arg *a = arg;
8698 struct got_pathlist_entry *pe;
8699 struct got_object_id blob_id, staged_blob_id, commit_id;
8700 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8701 struct got_object_id *commit_idp = NULL;
8702 int stage;
8704 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8705 return got_error(GOT_ERR_CANCELLED);
8707 TAILQ_FOREACH(pe, a->paths, entry) {
8708 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8709 got_path_is_child(ie->path, pe->path, pe->path_len))
8710 break;
8712 if (pe == NULL) /* not found */
8713 return NULL;
8715 if (got_fileindex_entry_has_blob(ie)) {
8716 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8717 blob_idp = &blob_id;
8719 stage = got_fileindex_entry_stage_get(ie);
8720 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8721 stage == GOT_FILEIDX_STAGE_ADD) {
8722 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8723 SHA1_DIGEST_LENGTH);
8724 staged_blob_idp = &staged_blob_id;
8727 if (got_fileindex_entry_has_commit(ie)) {
8728 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8729 commit_idp = &commit_id;
8732 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8733 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8736 const struct got_error *
8737 got_worktree_path_info(struct got_worktree *worktree,
8738 struct got_pathlist_head *paths,
8739 got_worktree_path_info_cb info_cb, void *info_arg,
8740 got_cancel_cb cancel_cb, void *cancel_arg)
8743 const struct got_error *err = NULL, *unlockerr;
8744 struct got_fileindex *fileindex = NULL;
8745 char *fileindex_path = NULL;
8746 struct report_file_info_arg arg;
8748 err = lock_worktree(worktree, LOCK_SH);
8749 if (err)
8750 return err;
8752 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8753 if (err)
8754 goto done;
8756 arg.worktree = worktree;
8757 arg.info_cb = info_cb;
8758 arg.info_arg = info_arg;
8759 arg.paths = paths;
8760 arg.cancel_cb = cancel_cb;
8761 arg.cancel_arg = cancel_arg;
8762 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8763 &arg);
8764 done:
8765 free(fileindex_path);
8766 if (fileindex)
8767 got_fileindex_free(fileindex);
8768 unlockerr = lock_worktree(worktree, LOCK_UN);
8769 if (unlockerr && err == NULL)
8770 err = unlockerr;
8771 return err;
8774 static const struct got_error *
8775 patch_check_path(const char *p, char **path, unsigned char *status,
8776 unsigned char *staged_status, struct got_fileindex *fileindex,
8777 struct got_worktree *worktree, struct got_repository *repo)
8779 const struct got_error *err;
8780 struct got_fileindex_entry *ie;
8781 struct stat sb;
8782 char *ondisk_path = NULL;
8784 err = got_worktree_resolve_path(path, worktree, p);
8785 if (err)
8786 return err;
8788 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
8789 *path[0] ? "/" : "", *path) == -1)
8790 return got_error_from_errno("asprintf");
8792 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
8793 if (ie) {
8794 *staged_status = get_staged_status(ie);
8795 err = get_file_status(status, &sb, ie, *path, -1, NULL, repo);
8796 if (err)
8797 goto done;
8798 } else {
8799 *staged_status = GOT_STATUS_NO_CHANGE;
8800 *status = GOT_STATUS_UNVERSIONED;
8801 if (lstat(ondisk_path, &sb) == -1) {
8802 if (errno != ENOENT) {
8803 err = got_error_from_errno2("lstat",
8804 ondisk_path);
8805 goto done;
8807 *status = GOT_STATUS_NONEXISTENT;
8811 done:
8812 free(ondisk_path);
8813 return err;
8816 static const struct got_error *
8817 patch_can_rm(const char *path, unsigned char status,
8818 unsigned char staged_status)
8820 if (status == GOT_STATUS_NONEXISTENT)
8821 return got_error_set_errno(ENOENT, path);
8822 if (status != GOT_STATUS_NO_CHANGE &&
8823 status != GOT_STATUS_ADD &&
8824 status != GOT_STATUS_MODIFY &&
8825 status != GOT_STATUS_MODE_CHANGE)
8826 return got_error_path(path, GOT_ERR_FILE_STATUS);
8827 if (staged_status == GOT_STATUS_DELETE)
8828 return got_error_path(path, GOT_ERR_FILE_STATUS);
8829 return NULL;
8832 static const struct got_error *
8833 patch_can_add(const char *path, unsigned char status)
8835 if (status != GOT_STATUS_NONEXISTENT)
8836 return got_error_path(path, GOT_ERR_FILE_STATUS);
8837 return NULL;
8840 static const struct got_error *
8841 patch_can_edit(const char *path, unsigned char status,
8842 unsigned char staged_status)
8844 if (status == GOT_STATUS_NONEXISTENT)
8845 return got_error_set_errno(ENOENT, path);
8846 if (status != GOT_STATUS_NO_CHANGE &&
8847 status != GOT_STATUS_ADD &&
8848 status != GOT_STATUS_MODIFY)
8849 return got_error_path(path, GOT_ERR_FILE_STATUS);
8850 if (staged_status == GOT_STATUS_DELETE)
8851 return got_error_path(path, GOT_ERR_FILE_STATUS);
8852 return NULL;
8855 const struct got_error *
8856 got_worktree_patch_prepare(struct got_fileindex **fileindex,
8857 struct got_worktree *worktree)
8859 const struct got_error *err;
8860 char *fileindex_path = NULL;
8862 err = open_fileindex(fileindex, &fileindex_path, worktree);
8863 free(fileindex_path);
8864 return err;
8867 const struct got_error *
8868 got_worktree_patch_check_path(const char *old, const char *new,
8869 char **oldpath, char **newpath, struct got_worktree *worktree,
8870 struct got_repository *repo, struct got_fileindex *fileindex)
8872 const struct got_error *err = NULL;
8873 int file_renamed = 0;
8874 unsigned char status_old, staged_status_old;
8875 unsigned char status_new, staged_status_new;
8877 *oldpath = NULL;
8878 *newpath = NULL;
8880 err = patch_check_path(old != NULL ? old : new, oldpath,
8881 &status_old, &staged_status_old, fileindex, worktree, repo);
8882 if (err)
8883 goto done;
8885 err = patch_check_path(new != NULL ? new : old, newpath,
8886 &status_new, &staged_status_new, fileindex, worktree, repo);
8887 if (err)
8888 goto done;
8890 if (old != NULL && new != NULL && strcmp(old, new) != 0)
8891 file_renamed = 1;
8893 if (old != NULL && new == NULL)
8894 err = patch_can_rm(*oldpath, status_old, staged_status_old);
8895 else if (file_renamed) {
8896 err = patch_can_rm(*oldpath, status_old, staged_status_old);
8897 if (err == NULL)
8898 err = patch_can_add(*newpath, status_new);
8899 } else if (old == NULL)
8900 err = patch_can_add(*newpath, status_new);
8901 else
8902 err = patch_can_edit(*newpath, status_new, staged_status_new);
8904 done:
8905 if (err) {
8906 free(*oldpath);
8907 *oldpath = NULL;
8908 free(*newpath);
8909 *newpath = NULL;
8911 return err;
8914 const struct got_error *
8915 got_worktree_patch_complete(struct got_fileindex *fileindex)
8917 got_fileindex_free(fileindex);
8918 return NULL;