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 "got_compat.h"
19 #include <sys/stat.h>
20 #include <sys/queue.h>
22 #include <dirent.h>
23 #include <limits.h>
24 #include <stddef.h>
25 #include <string.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <time.h>
29 #include <fcntl.h>
30 #include <errno.h>
31 #include <unistd.h>
32 #include <zlib.h>
33 #include <fnmatch.h>
34 #include <libgen.h>
36 #include "got_error.h"
37 #include "got_repository.h"
38 #include "got_reference.h"
39 #include "got_object.h"
40 #include "got_path.h"
41 #include "got_cancel.h"
42 #include "got_worktree.h"
43 #include "got_opentemp.h"
44 #include "got_diff.h"
46 #include "got_lib_worktree.h"
47 #include "got_lib_hash.h"
48 #include "got_lib_fileindex.h"
49 #include "got_lib_inflate.h"
50 #include "got_lib_delta.h"
51 #include "got_lib_object.h"
52 #include "got_lib_object_parse.h"
53 #include "got_lib_object_create.h"
54 #include "got_lib_object_idset.h"
55 #include "got_lib_diff.h"
56 #include "got_lib_gotconfig.h"
58 #ifndef MIN
59 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
60 #endif
62 #define GOT_MERGE_LABEL_MERGED "merged change"
63 #define GOT_MERGE_LABEL_BASE "3-way merge base"
65 static mode_t
66 apply_umask(mode_t mode)
67 {
68 mode_t um;
70 um = umask(000);
71 umask(um);
72 return mode & ~um;
73 }
75 static const struct got_error *
76 create_meta_file(const char *path_got, const char *name, const char *content)
77 {
78 const struct got_error *err = NULL;
79 char *path;
81 if (asprintf(&path, "%s/%s", path_got, name) == -1)
82 return got_error_from_errno("asprintf");
84 err = got_path_create_file(path, content);
85 free(path);
86 return err;
87 }
89 static const struct got_error *
90 update_meta_file(const char *path_got, const char *name, const char *content)
91 {
92 const struct got_error *err = NULL;
93 FILE *tmpfile = NULL;
94 char *tmppath = NULL;
95 char *path = NULL;
97 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
98 err = got_error_from_errno("asprintf");
99 path = NULL;
100 goto done;
103 err = got_opentemp_named(&tmppath, &tmpfile, path, "");
104 if (err)
105 goto done;
107 if (content) {
108 int len = fprintf(tmpfile, "%s\n", content);
109 if (len != strlen(content) + 1) {
110 err = got_error_from_errno2("fprintf", tmppath);
111 goto done;
115 if (rename(tmppath, path) != 0) {
116 err = got_error_from_errno3("rename", tmppath, path);
117 unlink(tmppath);
118 goto done;
121 done:
122 if (fclose(tmpfile) == EOF && err == NULL)
123 err = got_error_from_errno2("fclose", tmppath);
124 free(tmppath);
125 return err;
128 static const struct got_error *
129 write_head_ref(const char *path_got, struct got_reference *head_ref)
131 const struct got_error *err = NULL;
132 char *refstr = NULL;
134 if (got_ref_is_symbolic(head_ref)) {
135 refstr = got_ref_to_str(head_ref);
136 if (refstr == NULL)
137 return got_error_from_errno("got_ref_to_str");
138 } else {
139 refstr = strdup(got_ref_get_name(head_ref));
140 if (refstr == NULL)
141 return got_error_from_errno("strdup");
143 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
144 free(refstr);
145 return err;
148 const struct got_error *
149 got_worktree_init(const char *path, struct got_reference *head_ref,
150 const char *prefix, struct got_repository *repo)
152 const struct got_error *err = NULL;
153 struct got_object_id *commit_id = NULL;
154 uuid_t uuid;
155 uint32_t uuid_status;
156 int obj_type;
157 char *path_got = NULL;
158 char *formatstr = NULL;
159 char *absprefix = NULL;
160 char *basestr = NULL;
161 char *uuidstr = NULL;
163 if (strcmp(path, got_repo_get_path(repo)) == 0) {
164 err = got_error(GOT_ERR_WORKTREE_REPO);
165 goto done;
168 err = got_ref_resolve(&commit_id, repo, head_ref);
169 if (err)
170 return err;
171 err = got_object_get_type(&obj_type, repo, commit_id);
172 if (err)
173 return err;
174 if (obj_type != GOT_OBJ_TYPE_COMMIT)
175 return got_error(GOT_ERR_OBJ_TYPE);
177 if (!got_path_is_absolute(prefix)) {
178 if (asprintf(&absprefix, "/%s", prefix) == -1)
179 return got_error_from_errno("asprintf");
182 /* Create top-level directory (may already exist). */
183 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
184 err = got_error_from_errno2("mkdir", path);
185 goto done;
188 /* Create .got directory (may already exist). */
189 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
190 err = got_error_from_errno("asprintf");
191 goto done;
193 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
194 err = got_error_from_errno2("mkdir", path_got);
195 goto done;
198 /* Create an empty lock file. */
199 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
200 if (err)
201 goto done;
203 /* Create an empty file index. */
204 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
205 if (err)
206 goto done;
208 /* Write the HEAD reference. */
209 err = write_head_ref(path_got, head_ref);
210 if (err)
211 goto done;
213 /* Record our base commit. */
214 err = got_object_id_str(&basestr, commit_id);
215 if (err)
216 goto done;
217 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
218 if (err)
219 goto done;
221 /* Store path to repository. */
222 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
223 got_repo_get_path(repo));
224 if (err)
225 goto done;
227 /* Store in-repository path prefix. */
228 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
229 absprefix ? absprefix : prefix);
230 if (err)
231 goto done;
233 /* Generate UUID. */
234 uuid_create(&uuid, &uuid_status);
235 if (uuid_status != uuid_s_ok) {
236 err = got_error_uuid(uuid_status, "uuid_create");
237 goto done;
239 uuid_to_string(&uuid, &uuidstr, &uuid_status);
240 if (uuid_status != uuid_s_ok) {
241 err = got_error_uuid(uuid_status, "uuid_to_string");
242 goto done;
244 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
245 if (err)
246 goto done;
248 /* Stamp work tree with format file. */
249 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
250 err = got_error_from_errno("asprintf");
251 goto done;
253 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
254 if (err)
255 goto done;
257 done:
258 free(commit_id);
259 free(path_got);
260 free(formatstr);
261 free(absprefix);
262 free(basestr);
263 free(uuidstr);
264 return err;
267 const struct got_error *
268 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
269 const char *path_prefix)
271 char *absprefix = NULL;
273 if (!got_path_is_absolute(path_prefix)) {
274 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
275 return got_error_from_errno("asprintf");
277 *match = (strcmp(absprefix ? absprefix : path_prefix,
278 worktree->path_prefix) == 0);
279 free(absprefix);
280 return NULL;
283 const char *
284 got_worktree_get_head_ref_name(struct got_worktree *worktree)
286 return worktree->head_ref_name;
289 const struct got_error *
290 got_worktree_set_head_ref(struct got_worktree *worktree,
291 struct got_reference *head_ref)
293 const struct got_error *err = NULL;
294 char *path_got = NULL, *head_ref_name = NULL;
296 if (asprintf(&path_got, "%s/%s", worktree->root_path,
297 GOT_WORKTREE_GOT_DIR) == -1) {
298 err = got_error_from_errno("asprintf");
299 path_got = NULL;
300 goto done;
303 head_ref_name = strdup(got_ref_get_name(head_ref));
304 if (head_ref_name == NULL) {
305 err = got_error_from_errno("strdup");
306 goto done;
309 err = write_head_ref(path_got, head_ref);
310 if (err)
311 goto done;
313 free(worktree->head_ref_name);
314 worktree->head_ref_name = head_ref_name;
315 done:
316 free(path_got);
317 if (err)
318 free(head_ref_name);
319 return err;
322 struct got_object_id *
323 got_worktree_get_base_commit_id(struct got_worktree *worktree)
325 return worktree->base_commit_id;
328 const struct got_error *
329 got_worktree_set_base_commit_id(struct got_worktree *worktree,
330 struct got_repository *repo, struct got_object_id *commit_id)
332 const struct got_error *err;
333 struct got_object *obj = NULL;
334 char *id_str = NULL;
335 char *path_got = NULL;
337 if (asprintf(&path_got, "%s/%s", worktree->root_path,
338 GOT_WORKTREE_GOT_DIR) == -1) {
339 err = got_error_from_errno("asprintf");
340 path_got = NULL;
341 goto done;
344 err = got_object_open(&obj, repo, commit_id);
345 if (err)
346 return err;
348 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
349 err = got_error(GOT_ERR_OBJ_TYPE);
350 goto done;
353 /* Record our base commit. */
354 err = got_object_id_str(&id_str, commit_id);
355 if (err)
356 goto done;
357 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
358 if (err)
359 goto done;
361 free(worktree->base_commit_id);
362 worktree->base_commit_id = got_object_id_dup(commit_id);
363 if (worktree->base_commit_id == NULL) {
364 err = got_error_from_errno("got_object_id_dup");
365 goto done;
367 done:
368 if (obj)
369 got_object_close(obj);
370 free(id_str);
371 free(path_got);
372 return err;
375 const struct got_gotconfig *
376 got_worktree_get_gotconfig(struct got_worktree *worktree)
378 return worktree->gotconfig;
381 static const struct got_error *
382 lock_worktree(struct got_worktree *worktree, int operation)
384 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
385 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
386 : got_error_from_errno2("flock",
387 got_worktree_get_root_path(worktree)));
388 return NULL;
391 static const struct got_error *
392 add_dir_on_disk(struct got_worktree *worktree, const char *path)
394 const struct got_error *err = NULL;
395 char *abspath;
397 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
398 return got_error_from_errno("asprintf");
400 err = got_path_mkdir(abspath);
401 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
402 struct stat sb;
403 err = NULL;
404 if (lstat(abspath, &sb) == -1) {
405 err = got_error_from_errno2("lstat", abspath);
406 } else if (!S_ISDIR(sb.st_mode)) {
407 /* TODO directory is obstructed; do something */
408 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
411 free(abspath);
412 return err;
415 static const struct got_error *
416 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
418 const struct got_error *err = NULL;
419 uint8_t fbuf1[8192];
420 uint8_t fbuf2[8192];
421 size_t flen1 = 0, flen2 = 0;
423 *same = 1;
425 for (;;) {
426 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
427 if (flen1 == 0 && ferror(f1)) {
428 err = got_error_from_errno("fread");
429 break;
431 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
432 if (flen2 == 0 && ferror(f2)) {
433 err = got_error_from_errno("fread");
434 break;
436 if (flen1 == 0) {
437 if (flen2 != 0)
438 *same = 0;
439 break;
440 } else if (flen2 == 0) {
441 if (flen1 != 0)
442 *same = 0;
443 break;
444 } else if (flen1 == flen2) {
445 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
446 *same = 0;
447 break;
449 } else {
450 *same = 0;
451 break;
455 return err;
458 static const struct got_error *
459 check_files_equal(int *same, FILE *f1, FILE *f2)
461 struct stat sb;
462 size_t size1, size2;
464 *same = 1;
466 if (fstat(fileno(f1), &sb) != 0)
467 return got_error_from_errno("fstat");
468 size1 = sb.st_size;
470 if (fstat(fileno(f2), &sb) != 0)
471 return got_error_from_errno("fstat");
472 size2 = sb.st_size;
474 if (size1 != size2) {
475 *same = 0;
476 return NULL;
479 if (fseek(f1, 0L, SEEK_SET) == -1)
480 return got_ferror(f1, GOT_ERR_IO);
481 if (fseek(f2, 0L, SEEK_SET) == -1)
482 return got_ferror(f2, GOT_ERR_IO);
484 return check_file_contents_equal(same, f1, f2);
487 static const struct got_error *
488 copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
490 uint8_t fbuf[65536];
491 size_t flen;
492 ssize_t outlen;
494 *outsize = 0;
496 if (fseek(f, 0L, SEEK_SET) == -1)
497 return got_ferror(f, GOT_ERR_IO);
499 for (;;) {
500 flen = fread(fbuf, 1, sizeof(fbuf), f);
501 if (flen == 0) {
502 if (ferror(f))
503 return got_error_from_errno("fread");
504 if (feof(f))
505 break;
507 outlen = write(outfd, fbuf, flen);
508 if (outlen == -1)
509 return got_error_from_errno("write");
510 if (outlen != flen)
511 return got_error(GOT_ERR_IO);
512 *outsize += outlen;
515 return NULL;
518 static const struct got_error *
519 merge_binary_file(int *overlapcnt, int merged_fd,
520 FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
521 const char *label_deriv, const char *label_orig, const char *label_deriv2,
522 const char *ondisk_path)
524 const struct got_error *err = NULL;
525 int same_content, changed_deriv, changed_deriv2;
526 int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
527 off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
528 char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
529 char *base_path_orig = NULL, *base_path_deriv = NULL;
530 char *base_path_deriv2 = NULL;
532 *overlapcnt = 0;
534 err = check_files_equal(&same_content, f_deriv, f_deriv2);
535 if (err)
536 return err;
538 if (same_content)
539 return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
541 err = check_files_equal(&same_content, f_deriv, f_orig);
542 if (err)
543 return err;
544 changed_deriv = !same_content;
545 err = check_files_equal(&same_content, f_deriv2, f_orig);
546 if (err)
547 return err;
548 changed_deriv2 = !same_content;
550 if (changed_deriv && changed_deriv2) {
551 *overlapcnt = 1;
552 if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
553 err = got_error_from_errno("asprintf");
554 goto done;
556 if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
557 err = got_error_from_errno("asprintf");
558 goto done;
560 if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
561 err = got_error_from_errno("asprintf");
562 goto done;
564 err = got_opentemp_named_fd(&path_orig, &fd_orig,
565 base_path_orig, "");
566 if (err)
567 goto done;
568 err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
569 base_path_deriv, "");
570 if (err)
571 goto done;
572 err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
573 base_path_deriv2, "");
574 if (err)
575 goto done;
576 err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
577 if (err)
578 goto done;
579 err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
580 if (err)
581 goto done;
582 err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
583 if (err)
584 goto done;
585 if (dprintf(merged_fd, "Binary files differ and cannot be "
586 "merged automatically:\n") < 0) {
587 err = got_error_from_errno("dprintf");
588 goto done;
590 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
591 GOT_DIFF_CONFLICT_MARKER_BEGIN,
592 label_deriv ? " " : "",
593 label_deriv ? label_deriv : "",
594 path_deriv) < 0) {
595 err = got_error_from_errno("dprintf");
596 goto done;
598 if (size_orig > 0) {
599 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
600 GOT_DIFF_CONFLICT_MARKER_ORIG,
601 label_orig ? " " : "",
602 label_orig ? label_orig : "",
603 path_orig) < 0) {
604 err = got_error_from_errno("dprintf");
605 goto done;
608 if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
609 GOT_DIFF_CONFLICT_MARKER_SEP,
610 path_deriv2,
611 GOT_DIFF_CONFLICT_MARKER_END,
612 label_deriv2 ? " " : "",
613 label_deriv2 ? label_deriv2 : "") < 0) {
614 err = got_error_from_errno("dprintf");
615 goto done;
617 } else if (changed_deriv)
618 err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
619 else if (changed_deriv2)
620 err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
621 done:
622 if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
623 err == NULL)
624 err = got_error_from_errno2("unlink", path_orig);
625 if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
626 err = got_error_from_errno2("close", path_orig);
627 if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
628 err = got_error_from_errno2("close", path_deriv);
629 if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
630 err = got_error_from_errno2("close", path_deriv2);
631 free(path_orig);
632 free(path_deriv);
633 free(path_deriv2);
634 free(base_path_orig);
635 free(base_path_deriv);
636 free(base_path_deriv2);
637 return err;
640 /*
641 * Perform a 3-way merge where the file f_orig acts as the common
642 * ancestor, the file f_deriv acts as the first derived version,
643 * and the file f_deriv2 acts as the second derived version.
644 * The merge result will be written to a new file at ondisk_path; any
645 * existing file at this path will be replaced.
646 */
647 static const struct got_error *
648 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
649 FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
650 const char *path, uint16_t st_mode,
651 const char *label_orig, const char *label_deriv, const char *label_deriv2,
652 enum got_diff_algorithm diff_algo, struct got_repository *repo,
653 got_worktree_checkout_cb progress_cb, void *progress_arg)
655 const struct got_error *err = NULL;
656 int merged_fd = -1;
657 FILE *f_merged = NULL;
658 char *merged_path = NULL, *base_path = NULL;
659 int overlapcnt = 0;
660 char *parent = NULL;
662 *local_changes_subsumed = 0;
664 err = got_path_dirname(&parent, ondisk_path);
665 if (err)
666 return err;
668 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
669 err = got_error_from_errno("asprintf");
670 goto done;
673 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path, "");
674 if (err)
675 goto done;
677 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
678 f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
679 if (err) {
680 if (err->code != GOT_ERR_FILE_BINARY)
681 goto done;
682 err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
683 f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
684 ondisk_path);
685 if (err)
686 goto done;
689 err = (*progress_cb)(progress_arg,
690 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
691 if (err)
692 goto done;
694 if (fsync(merged_fd) != 0) {
695 err = got_error_from_errno("fsync");
696 goto done;
699 f_merged = fdopen(merged_fd, "r");
700 if (f_merged == NULL) {
701 err = got_error_from_errno("fdopen");
702 goto done;
704 merged_fd = -1;
706 /* Check if a clean merge has subsumed all local changes. */
707 if (overlapcnt == 0) {
708 err = check_files_equal(local_changes_subsumed, f_deriv,
709 f_merged);
710 if (err)
711 goto done;
714 if (fchmod(fileno(f_merged), apply_umask(st_mode)) != 0) {
715 err = got_error_from_errno2("fchmod", merged_path);
716 goto done;
719 if (rename(merged_path, ondisk_path) != 0) {
720 err = got_error_from_errno3("rename", merged_path,
721 ondisk_path);
722 goto done;
724 done:
725 if (err) {
726 if (merged_path)
727 unlink(merged_path);
729 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
730 err = got_error_from_errno("close");
731 if (f_merged && fclose(f_merged) == EOF && err == NULL)
732 err = got_error_from_errno("fclose");
733 free(merged_path);
734 free(base_path);
735 free(parent);
736 return err;
739 static const struct got_error *
740 update_symlink(const char *ondisk_path, const char *target_path,
741 size_t target_len)
743 /* This is not atomic but matches what 'ln -sf' does. */
744 if (unlink(ondisk_path) == -1)
745 return got_error_from_errno2("unlink", ondisk_path);
746 if (symlink(target_path, ondisk_path) == -1)
747 return got_error_from_errno3("symlink", target_path,
748 ondisk_path);
749 return NULL;
752 /*
753 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
754 * in the work tree with a file that contains conflict markers and the
755 * conflicting target paths of the original version, a "derived version"
756 * of a symlink from an incoming change, and a local version of the symlink.
758 * The original versions's target path can be NULL if it is not available,
759 * such as if both derived versions added a new symlink at the same path.
761 * The incoming derived symlink target is NULL in case the incoming change
762 * has deleted this symlink.
763 */
764 static const struct got_error *
765 install_symlink_conflict(const char *deriv_target,
766 struct got_object_id *deriv_base_commit_id, const char *orig_target,
767 const char *label_orig, const char *local_target, const char *ondisk_path)
769 const struct got_error *err;
770 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
771 FILE *f = NULL;
773 err = got_object_id_str(&id_str, deriv_base_commit_id);
774 if (err)
775 return got_error_from_errno("asprintf");
777 if (asprintf(&label_deriv, "%s: commit %s",
778 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
779 err = got_error_from_errno("asprintf");
780 goto done;
783 err = got_opentemp_named(&path, &f, "got-symlink-conflict", "");
784 if (err)
785 goto done;
787 if (fchmod(fileno(f), apply_umask(GOT_DEFAULT_FILE_MODE)) == -1) {
788 err = got_error_from_errno2("fchmod", path);
789 goto done;
792 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
793 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
794 deriv_target ? deriv_target : "(symlink was deleted)",
795 orig_target ? label_orig : "",
796 orig_target ? "\n" : "",
797 orig_target ? orig_target : "",
798 orig_target ? "\n" : "",
799 GOT_DIFF_CONFLICT_MARKER_SEP,
800 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
801 err = got_error_from_errno2("fprintf", path);
802 goto done;
805 if (unlink(ondisk_path) == -1) {
806 err = got_error_from_errno2("unlink", ondisk_path);
807 goto done;
809 if (rename(path, ondisk_path) == -1) {
810 err = got_error_from_errno3("rename", path, ondisk_path);
811 goto done;
813 done:
814 if (f != NULL && fclose(f) == EOF && err == NULL)
815 err = got_error_from_errno2("fclose", path);
816 free(path);
817 free(id_str);
818 free(label_deriv);
819 return err;
822 /* forward declaration */
823 static const struct got_error *
824 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
825 const char *, const char *, uint16_t, const char *,
826 struct got_blob_object *, struct got_object_id *,
827 struct got_repository *, got_worktree_checkout_cb, void *);
829 /*
830 * Merge a symlink into the work tree, where blob_orig acts as the common
831 * ancestor, deriv_target is the link target of the first derived version,
832 * and the symlink on disk acts as the second derived version.
833 * Assume that contents of both blobs represent symlinks.
834 */
835 static const struct got_error *
836 merge_symlink(struct got_worktree *worktree,
837 struct got_blob_object *blob_orig, const char *ondisk_path,
838 const char *path, const char *label_orig, const char *deriv_target,
839 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
840 got_worktree_checkout_cb progress_cb, void *progress_arg)
842 const struct got_error *err = NULL;
843 char *ancestor_target = NULL;
844 struct stat sb;
845 ssize_t ondisk_len, deriv_len;
846 char ondisk_target[PATH_MAX];
847 int have_local_change = 0;
848 int have_incoming_change = 0;
850 if (lstat(ondisk_path, &sb) == -1)
851 return got_error_from_errno2("lstat", ondisk_path);
853 ondisk_len = readlink(ondisk_path, ondisk_target,
854 sizeof(ondisk_target));
855 if (ondisk_len == -1) {
856 err = got_error_from_errno2("readlink",
857 ondisk_path);
858 goto done;
860 ondisk_target[ondisk_len] = '\0';
862 if (blob_orig) {
863 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
864 if (err)
865 goto done;
868 if (ancestor_target == NULL ||
869 (ondisk_len != strlen(ancestor_target) ||
870 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
871 have_local_change = 1;
873 deriv_len = strlen(deriv_target);
874 if (ancestor_target == NULL ||
875 (deriv_len != strlen(ancestor_target) ||
876 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
877 have_incoming_change = 1;
879 if (!have_local_change && !have_incoming_change) {
880 if (ancestor_target) {
881 /* Both sides made the same change. */
882 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
883 path);
884 } else if (deriv_len == ondisk_len &&
885 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
886 /* Both sides added the same symlink. */
887 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
888 path);
889 } else {
890 /* Both sides added symlinks which don't match. */
891 err = install_symlink_conflict(deriv_target,
892 deriv_base_commit_id, ancestor_target,
893 label_orig, ondisk_target, ondisk_path);
894 if (err)
895 goto done;
896 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
897 path);
899 } else if (!have_local_change && have_incoming_change) {
900 /* Apply the incoming change. */
901 err = update_symlink(ondisk_path, deriv_target,
902 strlen(deriv_target));
903 if (err)
904 goto done;
905 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
906 } else if (have_local_change && have_incoming_change) {
907 if (deriv_len == ondisk_len &&
908 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
909 /* Both sides made the same change. */
910 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
911 path);
912 } else {
913 err = install_symlink_conflict(deriv_target,
914 deriv_base_commit_id, ancestor_target, label_orig,
915 ondisk_target, ondisk_path);
916 if (err)
917 goto done;
918 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
919 path);
923 done:
924 free(ancestor_target);
925 return err;
928 static const struct got_error *
929 dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
931 const struct got_error *err = NULL;
932 char target_path[PATH_MAX];
933 ssize_t target_len;
934 size_t n;
935 FILE *f;
937 *outfile = NULL;
939 f = got_opentemp();
940 if (f == NULL)
941 return got_error_from_errno("got_opentemp");
942 target_len = readlink(ondisk_path, target_path, sizeof(target_path));
943 if (target_len == -1) {
944 err = got_error_from_errno2("readlink", ondisk_path);
945 goto done;
947 n = fwrite(target_path, 1, target_len, f);
948 if (n != target_len) {
949 err = got_ferror(f, GOT_ERR_IO);
950 goto done;
952 if (fflush(f) == EOF) {
953 err = got_error_from_errno("fflush");
954 goto done;
956 if (fseek(f, 0L, SEEK_SET) == -1) {
957 err = got_ferror(f, GOT_ERR_IO);
958 goto done;
960 done:
961 if (err)
962 fclose(f);
963 else
964 *outfile = f;
965 return err;
968 /*
969 * Perform a 3-way merge where blob_orig acts as the common ancestor,
970 * blob_deriv acts as the first derived version, and the file on disk
971 * acts as the second derived version.
972 */
973 static const struct got_error *
974 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
975 struct got_blob_object *blob_orig, const char *ondisk_path,
976 const char *path, uint16_t st_mode, const char *label_orig,
977 struct got_blob_object *blob_deriv,
978 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
979 got_worktree_checkout_cb progress_cb, void *progress_arg)
981 const struct got_error *err = NULL;
982 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
983 char *blob_orig_path = NULL;
984 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
985 char *label_deriv = NULL, *parent = NULL;
987 *local_changes_subsumed = 0;
989 err = got_path_dirname(&parent, ondisk_path);
990 if (err)
991 return err;
993 if (blob_orig) {
994 if (asprintf(&base_path, "%s/got-merge-blob-orig",
995 parent) == -1) {
996 err = got_error_from_errno("asprintf");
997 base_path = NULL;
998 goto done;
1001 err = got_opentemp_named(&blob_orig_path, &f_orig,
1002 base_path, "");
1003 if (err)
1004 goto done;
1005 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1006 blob_orig);
1007 if (err)
1008 goto done;
1009 free(base_path);
1010 } else {
1012 * No common ancestor exists. This is an "add vs add" conflict
1013 * and we simply use an empty ancestor file to make both files
1014 * appear in the merged result in their entirety.
1016 f_orig = got_opentemp();
1017 if (f_orig == NULL) {
1018 err = got_error_from_errno("got_opentemp");
1019 goto done;
1023 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1024 err = got_error_from_errno("asprintf");
1025 base_path = NULL;
1026 goto done;
1029 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path, "");
1030 if (err)
1031 goto done;
1032 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1033 blob_deriv);
1034 if (err)
1035 goto done;
1037 err = got_object_id_str(&id_str, deriv_base_commit_id);
1038 if (err)
1039 goto done;
1040 if (asprintf(&label_deriv, "%s: commit %s",
1041 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1042 err = got_error_from_errno("asprintf");
1043 goto done;
1047 * In order the run a 3-way merge with a symlink we copy the symlink's
1048 * target path into a temporary file and use that file with diff3.
1050 if (S_ISLNK(st_mode)) {
1051 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1052 if (err)
1053 goto done;
1054 } else {
1055 int fd;
1056 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1057 if (fd == -1) {
1058 err = got_error_from_errno2("open", ondisk_path);
1059 goto done;
1061 f_deriv2 = fdopen(fd, "r");
1062 if (f_deriv2 == NULL) {
1063 err = got_error_from_errno2("fdopen", ondisk_path);
1064 close(fd);
1065 goto done;
1069 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1070 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1071 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1072 done:
1073 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1074 err = got_error_from_errno("fclose");
1075 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1076 err = got_error_from_errno("fclose");
1077 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1078 err = got_error_from_errno("fclose");
1079 free(base_path);
1080 if (blob_orig_path) {
1081 unlink(blob_orig_path);
1082 free(blob_orig_path);
1084 if (blob_deriv_path) {
1085 unlink(blob_deriv_path);
1086 free(blob_deriv_path);
1088 free(id_str);
1089 free(label_deriv);
1090 free(parent);
1091 return err;
1094 static const struct got_error *
1095 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1096 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1097 int wt_fd, const char *path, struct got_object_id *blob_id)
1099 const struct got_error *err = NULL;
1100 struct got_fileindex_entry *new_ie;
1102 *new_iep = NULL;
1104 err = got_fileindex_entry_alloc(&new_ie, path);
1105 if (err)
1106 return err;
1108 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1109 blob_id->sha1, base_commit_id->sha1, 1);
1110 if (err)
1111 goto done;
1113 err = got_fileindex_entry_add(fileindex, new_ie);
1114 done:
1115 if (err)
1116 got_fileindex_entry_free(new_ie);
1117 else
1118 *new_iep = new_ie;
1119 return err;
1122 static mode_t
1123 get_ondisk_perms(int executable, mode_t st_mode)
1125 mode_t xbits = S_IXUSR;
1127 if (executable) {
1128 /* Map read bits to execute bits. */
1129 if (st_mode & S_IRGRP)
1130 xbits |= S_IXGRP;
1131 if (st_mode & S_IROTH)
1132 xbits |= S_IXOTH;
1133 return st_mode | xbits;
1136 return st_mode;
1139 /* forward declaration */
1140 static const struct got_error *
1141 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1142 const char *path, mode_t te_mode, mode_t st_mode,
1143 struct got_blob_object *blob, int restoring_missing_file,
1144 int reverting_versioned_file, int installing_bad_symlink,
1145 int path_is_unversioned, struct got_repository *repo,
1146 got_worktree_checkout_cb progress_cb, void *progress_arg);
1149 * This function assumes that the provided symlink target points at a
1150 * safe location in the work tree!
1152 static const struct got_error *
1153 replace_existing_symlink(int *did_something, const char *ondisk_path,
1154 const char *target_path, size_t target_len)
1156 const struct got_error *err = NULL;
1157 ssize_t elen;
1158 char etarget[PATH_MAX];
1159 int fd;
1161 *did_something = 0;
1164 * "Bad" symlinks (those pointing outside the work tree or into the
1165 * .got directory) are installed in the work tree as a regular file
1166 * which contains the bad symlink target path.
1167 * The new symlink target has already been checked for safety by our
1168 * caller. If we can successfully open a regular file then we simply
1169 * replace this file with a symlink below.
1171 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1172 if (fd == -1) {
1173 if (!got_err_open_nofollow_on_symlink())
1174 return got_error_from_errno2("open", ondisk_path);
1176 /* We are updating an existing on-disk symlink. */
1177 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1178 if (elen == -1)
1179 return got_error_from_errno2("readlink", ondisk_path);
1181 if (elen == target_len &&
1182 memcmp(etarget, target_path, target_len) == 0)
1183 return NULL; /* nothing to do */
1186 *did_something = 1;
1187 err = update_symlink(ondisk_path, target_path, target_len);
1188 if (fd != -1 && close(fd) == -1 && err == NULL)
1189 err = got_error_from_errno2("close", ondisk_path);
1190 return err;
1193 static const struct got_error *
1194 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1195 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1197 const struct got_error *err = NULL;
1198 char canonpath[PATH_MAX];
1199 char *path_got = NULL;
1201 *is_bad_symlink = 0;
1203 if (target_len >= sizeof(canonpath)) {
1204 *is_bad_symlink = 1;
1205 return NULL;
1209 * We do not use realpath(3) to resolve the symlink's target
1210 * path because we don't want to resolve symlinks recursively.
1211 * Instead we make the path absolute and then canonicalize it.
1212 * Relative symlink target lookup should begin at the directory
1213 * in which the blob object is being installed.
1215 if (!got_path_is_absolute(target_path)) {
1216 char *abspath, *parent;
1217 err = got_path_dirname(&parent, ondisk_path);
1218 if (err)
1219 return err;
1220 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1221 free(parent);
1222 return got_error_from_errno("asprintf");
1224 free(parent);
1225 if (strlen(abspath) >= sizeof(canonpath)) {
1226 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1227 free(abspath);
1228 return err;
1230 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1231 free(abspath);
1232 if (err)
1233 return err;
1234 } else {
1235 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1236 if (err)
1237 return err;
1240 /* Only allow symlinks pointing at paths within the work tree. */
1241 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1242 *is_bad_symlink = 1;
1243 return NULL;
1246 /* Do not allow symlinks pointing into the .got directory. */
1247 if (asprintf(&path_got, "%s/%s", wtroot_path,
1248 GOT_WORKTREE_GOT_DIR) == -1)
1249 return got_error_from_errno("asprintf");
1250 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1251 *is_bad_symlink = 1;
1253 free(path_got);
1254 return NULL;
1257 static const struct got_error *
1258 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1259 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1260 int restoring_missing_file, int reverting_versioned_file,
1261 int path_is_unversioned, int allow_bad_symlinks,
1262 struct got_repository *repo,
1263 got_worktree_checkout_cb progress_cb, void *progress_arg)
1265 const struct got_error *err = NULL;
1266 char target_path[PATH_MAX];
1267 size_t len, target_len = 0;
1268 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1269 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1271 *is_bad_symlink = 0;
1274 * Blob object content specifies the target path of the link.
1275 * If a symbolic link cannot be installed we instead create
1276 * a regular file which contains the link target path stored
1277 * in the blob object.
1279 do {
1280 err = got_object_blob_read_block(&len, blob);
1281 if (err)
1282 return err;
1284 if (len + target_len >= sizeof(target_path)) {
1285 /* Path too long; install as a regular file. */
1286 *is_bad_symlink = 1;
1287 got_object_blob_rewind(blob);
1288 return install_blob(worktree, ondisk_path, path,
1289 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1290 restoring_missing_file, reverting_versioned_file,
1291 1, path_is_unversioned, repo, progress_cb,
1292 progress_arg);
1294 if (len > 0) {
1295 /* Skip blob object header first time around. */
1296 memcpy(target_path + target_len, buf + hdrlen,
1297 len - hdrlen);
1298 target_len += len - hdrlen;
1299 hdrlen = 0;
1301 } while (len != 0);
1302 target_path[target_len] = '\0';
1304 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1305 ondisk_path, worktree->root_path);
1306 if (err)
1307 return err;
1309 if (*is_bad_symlink && !allow_bad_symlinks) {
1310 /* install as a regular file */
1311 got_object_blob_rewind(blob);
1312 err = install_blob(worktree, ondisk_path, path,
1313 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1314 restoring_missing_file, reverting_versioned_file, 1,
1315 path_is_unversioned, repo, progress_cb, progress_arg);
1316 return err;
1319 if (symlink(target_path, ondisk_path) == -1) {
1320 if (errno == EEXIST) {
1321 int symlink_replaced;
1322 if (path_is_unversioned) {
1323 err = (*progress_cb)(progress_arg,
1324 GOT_STATUS_UNVERSIONED, path);
1325 return err;
1327 err = replace_existing_symlink(&symlink_replaced,
1328 ondisk_path, target_path, target_len);
1329 if (err)
1330 return err;
1331 if (progress_cb) {
1332 if (symlink_replaced) {
1333 err = (*progress_cb)(progress_arg,
1334 reverting_versioned_file ?
1335 GOT_STATUS_REVERT :
1336 GOT_STATUS_UPDATE, path);
1337 } else {
1338 err = (*progress_cb)(progress_arg,
1339 GOT_STATUS_EXISTS, path);
1342 return err; /* Nothing else to do. */
1345 if (errno == ENOENT) {
1346 char *parent;
1347 err = got_path_dirname(&parent, ondisk_path);
1348 if (err)
1349 return err;
1350 err = add_dir_on_disk(worktree, parent);
1351 free(parent);
1352 if (err)
1353 return err;
1355 * Retry, and fall through to error handling
1356 * below if this second attempt fails.
1358 if (symlink(target_path, ondisk_path) != -1) {
1359 err = NULL; /* success */
1360 return err;
1364 /* Handle errors from first or second creation attempt. */
1365 if (errno == ENAMETOOLONG) {
1366 /* bad target path; install as a regular file */
1367 *is_bad_symlink = 1;
1368 got_object_blob_rewind(blob);
1369 err = install_blob(worktree, ondisk_path, path,
1370 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1371 restoring_missing_file, reverting_versioned_file, 1,
1372 path_is_unversioned, repo,
1373 progress_cb, progress_arg);
1374 } else if (errno == ENOTDIR) {
1375 err = got_error_path(ondisk_path,
1376 GOT_ERR_FILE_OBSTRUCTED);
1377 } else {
1378 err = got_error_from_errno3("symlink",
1379 target_path, ondisk_path);
1381 } else if (progress_cb)
1382 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1383 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1384 return err;
1387 static const struct got_error *
1388 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1389 const char *path, mode_t te_mode, mode_t st_mode,
1390 struct got_blob_object *blob, int restoring_missing_file,
1391 int reverting_versioned_file, int installing_bad_symlink,
1392 int path_is_unversioned, struct got_repository *repo,
1393 got_worktree_checkout_cb progress_cb, void *progress_arg)
1395 const struct got_error *err = NULL;
1396 int fd = -1;
1397 size_t len, hdrlen;
1398 int update = 0;
1399 char *tmppath = NULL;
1400 mode_t mode;
1402 mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1403 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1404 O_CLOEXEC, mode);
1405 if (fd == -1) {
1406 if (errno == ENOENT || errno == ENOTDIR) {
1407 char *parent;
1408 err = got_path_dirname(&parent, path);
1409 if (err)
1410 return err;
1411 err = add_dir_on_disk(worktree, parent);
1412 if (err && err->code == GOT_ERR_FILE_OBSTRUCTED)
1413 err = got_error_path(path, err->code);
1414 free(parent);
1415 if (err)
1416 return err;
1417 fd = open(ondisk_path,
1418 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1419 mode);
1420 if (fd == -1)
1421 return got_error_from_errno2("open",
1422 ondisk_path);
1423 } else if (errno == EEXIST) {
1424 if (path_is_unversioned) {
1425 err = (*progress_cb)(progress_arg,
1426 GOT_STATUS_UNVERSIONED, path);
1427 goto done;
1429 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1430 !S_ISREG(st_mode) && !installing_bad_symlink) {
1431 /* TODO file is obstructed; do something */
1432 err = got_error_path(ondisk_path,
1433 GOT_ERR_FILE_OBSTRUCTED);
1434 goto done;
1435 } else {
1436 err = got_opentemp_named_fd(&tmppath, &fd,
1437 ondisk_path, "");
1438 if (err)
1439 goto done;
1440 update = 1;
1442 if (fchmod(fd, apply_umask(mode)) == -1) {
1443 err = got_error_from_errno2("fchmod",
1444 tmppath);
1445 goto done;
1448 } else
1449 return got_error_from_errno2("open", ondisk_path);
1452 if (progress_cb) {
1453 if (restoring_missing_file)
1454 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1455 path);
1456 else if (reverting_versioned_file)
1457 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1458 path);
1459 else
1460 err = (*progress_cb)(progress_arg,
1461 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1462 if (err)
1463 goto done;
1466 hdrlen = got_object_blob_get_hdrlen(blob);
1467 do {
1468 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1469 err = got_object_blob_read_block(&len, blob);
1470 if (err)
1471 break;
1472 if (len > 0) {
1473 /* Skip blob object header first time around. */
1474 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1475 if (outlen == -1) {
1476 err = got_error_from_errno("write");
1477 goto done;
1478 } else if (outlen != len - hdrlen) {
1479 err = got_error(GOT_ERR_IO);
1480 goto done;
1482 hdrlen = 0;
1484 } while (len != 0);
1486 if (fsync(fd) != 0) {
1487 err = got_error_from_errno("fsync");
1488 goto done;
1491 if (update) {
1492 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1493 err = got_error_from_errno2("unlink", ondisk_path);
1494 goto done;
1496 if (rename(tmppath, ondisk_path) != 0) {
1497 err = got_error_from_errno3("rename", tmppath,
1498 ondisk_path);
1499 goto done;
1501 free(tmppath);
1502 tmppath = NULL;
1505 done:
1506 if (fd != -1 && close(fd) == -1 && err == NULL)
1507 err = got_error_from_errno("close");
1508 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1509 err = got_error_from_errno2("unlink", tmppath);
1510 free(tmppath);
1511 return err;
1515 * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
1516 * conflict marker is found in newly added lines only.
1518 static const struct got_error *
1519 get_modified_file_content_status(unsigned char *status,
1520 struct got_blob_object *blob, const char *path, struct stat *sb,
1521 FILE *ondisk_file)
1523 const struct got_error *err, *free_err;
1524 const char *markers[3] = {
1525 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1526 GOT_DIFF_CONFLICT_MARKER_SEP,
1527 GOT_DIFF_CONFLICT_MARKER_END
1529 FILE *f1 = NULL;
1530 struct got_diffreg_result *diffreg_result = NULL;
1531 struct diff_result *r;
1532 int nchunks_parsed, n, i = 0, ln = 0;
1533 char *line = NULL;
1534 size_t linesize = 0;
1535 ssize_t linelen;
1537 if (*status != GOT_STATUS_MODIFY)
1538 return NULL;
1540 f1 = got_opentemp();
1541 if (f1 == NULL)
1542 return got_error_from_errno("got_opentemp");
1544 if (blob) {
1545 got_object_blob_rewind(blob);
1546 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
1547 if (err)
1548 goto done;
1551 err = got_diff_files(&diffreg_result, f1, 1, NULL, ondisk_file,
1552 1, NULL, 0, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
1553 if (err)
1554 goto done;
1556 r = diffreg_result->result;
1558 for (n = 0; n < r->chunks.len; n += nchunks_parsed) {
1559 struct diff_chunk *c;
1560 struct diff_chunk_context cc = {};
1561 off_t pos;
1564 * We can optimise a little by advancing straight
1565 * to the next chunk if this one has no added lines.
1567 c = diff_chunk_get(r, n);
1569 if (diff_chunk_type(c) != CHUNK_PLUS) {
1570 nchunks_parsed = 1;
1571 continue; /* removed or unchanged lines */
1574 pos = diff_chunk_get_right_start_pos(c);
1575 if (fseek(ondisk_file, pos, SEEK_SET) == -1) {
1576 err = got_ferror(ondisk_file, GOT_ERR_IO);
1577 goto done;
1580 diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
1581 ln = cc.right.start;
1583 while (ln < cc.right.end) {
1584 linelen = getline(&line, &linesize, ondisk_file);
1585 if (linelen == -1) {
1586 if (feof(ondisk_file))
1587 break;
1588 err = got_ferror(ondisk_file, GOT_ERR_IO);
1589 break;
1592 if (line && strncmp(line, markers[i],
1593 strlen(markers[i])) == 0) {
1594 if (strcmp(markers[i],
1595 GOT_DIFF_CONFLICT_MARKER_END) == 0) {
1596 *status = GOT_STATUS_CONFLICT;
1597 goto done;
1598 } else
1599 i++;
1601 ++ln;
1605 done:
1606 free(line);
1607 if (f1 != NULL && fclose(f1) == EOF && err == NULL)
1608 err = got_error_from_errno("fclose");
1609 free_err = got_diffreg_result_free(diffreg_result);
1610 if (err == NULL)
1611 err = free_err;
1613 return err;
1616 static int
1617 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1619 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1620 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1623 static int
1624 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1626 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1627 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1628 ie->mtime_sec == sb->st_mtim.tv_sec &&
1629 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1630 ie->size == (sb->st_size & 0xffffffff) &&
1631 !xbit_differs(ie, sb->st_mode));
1634 static unsigned char
1635 get_staged_status(struct got_fileindex_entry *ie)
1637 switch (got_fileindex_entry_stage_get(ie)) {
1638 case GOT_FILEIDX_STAGE_ADD:
1639 return GOT_STATUS_ADD;
1640 case GOT_FILEIDX_STAGE_DELETE:
1641 return GOT_STATUS_DELETE;
1642 case GOT_FILEIDX_STAGE_MODIFY:
1643 return GOT_STATUS_MODIFY;
1644 default:
1645 return GOT_STATUS_NO_CHANGE;
1649 static const struct got_error *
1650 get_symlink_modification_status(unsigned char *status,
1651 struct got_fileindex_entry *ie, const char *abspath,
1652 int dirfd, const char *de_name, struct got_blob_object *blob)
1654 const struct got_error *err = NULL;
1655 char target_path[PATH_MAX];
1656 char etarget[PATH_MAX];
1657 ssize_t elen;
1658 size_t len, target_len = 0;
1659 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1660 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1662 *status = GOT_STATUS_NO_CHANGE;
1664 /* Blob object content specifies the target path of the link. */
1665 do {
1666 err = got_object_blob_read_block(&len, blob);
1667 if (err)
1668 return err;
1669 if (len + target_len >= sizeof(target_path)) {
1671 * Should not happen. The blob contents were OK
1672 * when this symlink was installed.
1674 return got_error(GOT_ERR_NO_SPACE);
1676 if (len > 0) {
1677 /* Skip blob object header first time around. */
1678 memcpy(target_path + target_len, buf + hdrlen,
1679 len - hdrlen);
1680 target_len += len - hdrlen;
1681 hdrlen = 0;
1683 } while (len != 0);
1684 target_path[target_len] = '\0';
1686 if (dirfd != -1) {
1687 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1688 if (elen == -1)
1689 return got_error_from_errno2("readlinkat", abspath);
1690 } else {
1691 elen = readlink(abspath, etarget, sizeof(etarget));
1692 if (elen == -1)
1693 return got_error_from_errno2("readlink", abspath);
1696 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1697 *status = GOT_STATUS_MODIFY;
1699 return NULL;
1702 static const struct got_error *
1703 get_file_status(unsigned char *status, struct stat *sb,
1704 struct got_fileindex_entry *ie, const char *abspath,
1705 int dirfd, const char *de_name, struct got_repository *repo)
1707 const struct got_error *err = NULL;
1708 struct got_object_id id;
1709 size_t hdrlen;
1710 int fd = -1, fd1 = -1;
1711 FILE *f = NULL;
1712 uint8_t fbuf[8192];
1713 struct got_blob_object *blob = NULL;
1714 size_t flen, blen;
1715 unsigned char staged_status;
1717 staged_status = get_staged_status(ie);
1718 *status = GOT_STATUS_NO_CHANGE;
1719 memset(sb, 0, sizeof(*sb));
1722 * Whenever the caller provides a directory descriptor and a
1723 * directory entry name for the file, use them! This prevents
1724 * race conditions if filesystem paths change beneath our feet.
1726 if (dirfd != -1) {
1727 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1728 if (errno == ENOENT) {
1729 if (got_fileindex_entry_has_file_on_disk(ie))
1730 *status = GOT_STATUS_MISSING;
1731 else
1732 *status = GOT_STATUS_DELETE;
1733 goto done;
1735 err = got_error_from_errno2("fstatat", abspath);
1736 goto done;
1738 } else {
1739 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1740 if (fd == -1 && errno != ENOENT &&
1741 !got_err_open_nofollow_on_symlink())
1742 return got_error_from_errno2("open", abspath);
1743 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1744 if (lstat(abspath, sb) == -1)
1745 return got_error_from_errno2("lstat", abspath);
1746 } else if (fd == -1 || fstat(fd, sb) == -1) {
1747 if (errno == ENOENT) {
1748 if (got_fileindex_entry_has_file_on_disk(ie))
1749 *status = GOT_STATUS_MISSING;
1750 else
1751 *status = GOT_STATUS_DELETE;
1752 goto done;
1754 err = got_error_from_errno2("fstat", abspath);
1755 goto done;
1759 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1760 *status = GOT_STATUS_OBSTRUCTED;
1761 goto done;
1764 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1765 *status = GOT_STATUS_DELETE;
1766 goto done;
1767 } else if (!got_fileindex_entry_has_blob(ie) &&
1768 staged_status != GOT_STATUS_ADD) {
1769 *status = GOT_STATUS_ADD;
1770 goto done;
1773 if (!stat_info_differs(ie, sb))
1774 goto done;
1776 if (S_ISLNK(sb->st_mode) &&
1777 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1778 *status = GOT_STATUS_MODIFY;
1779 goto done;
1782 if (staged_status == GOT_STATUS_MODIFY ||
1783 staged_status == GOT_STATUS_ADD)
1784 got_fileindex_entry_get_staged_blob_id(&id, ie);
1785 else
1786 got_fileindex_entry_get_blob_id(&id, ie);
1788 fd1 = got_opentempfd();
1789 if (fd1 == -1) {
1790 err = got_error_from_errno("got_opentempfd");
1791 goto done;
1793 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1794 if (err)
1795 goto done;
1797 if (S_ISLNK(sb->st_mode)) {
1798 err = get_symlink_modification_status(status, ie,
1799 abspath, dirfd, de_name, blob);
1800 goto done;
1803 if (dirfd != -1) {
1804 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1805 if (fd == -1) {
1806 err = got_error_from_errno2("openat", abspath);
1807 goto done;
1811 f = fdopen(fd, "r");
1812 if (f == NULL) {
1813 err = got_error_from_errno2("fdopen", abspath);
1814 goto done;
1816 fd = -1;
1817 hdrlen = got_object_blob_get_hdrlen(blob);
1818 for (;;) {
1819 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1820 err = got_object_blob_read_block(&blen, blob);
1821 if (err)
1822 goto done;
1823 /* Skip length of blob object header first time around. */
1824 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1825 if (flen == 0 && ferror(f)) {
1826 err = got_error_from_errno("fread");
1827 goto done;
1829 if (blen - hdrlen == 0) {
1830 if (flen != 0)
1831 *status = GOT_STATUS_MODIFY;
1832 break;
1833 } else if (flen == 0) {
1834 if (blen - hdrlen != 0)
1835 *status = GOT_STATUS_MODIFY;
1836 break;
1837 } else if (blen - hdrlen == flen) {
1838 /* Skip blob object header first time around. */
1839 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1840 *status = GOT_STATUS_MODIFY;
1841 break;
1843 } else {
1844 *status = GOT_STATUS_MODIFY;
1845 break;
1847 hdrlen = 0;
1850 if (*status == GOT_STATUS_MODIFY) {
1851 rewind(f);
1852 err = get_modified_file_content_status(status, blob, ie->path,
1853 sb, f);
1854 } else if (xbit_differs(ie, sb->st_mode))
1855 *status = GOT_STATUS_MODE_CHANGE;
1856 done:
1857 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1858 err = got_error_from_errno("close");
1859 if (blob)
1860 got_object_blob_close(blob);
1861 if (f != NULL && fclose(f) == EOF && err == NULL)
1862 err = got_error_from_errno2("fclose", abspath);
1863 if (fd != -1 && close(fd) == -1 && err == NULL)
1864 err = got_error_from_errno2("close", abspath);
1865 return err;
1869 * Update timestamps in the file index if a file is unmodified and
1870 * we had to run a full content comparison to find out.
1872 static const struct got_error *
1873 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1874 struct got_fileindex_entry *ie, struct stat *sb)
1876 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1877 return got_fileindex_entry_update(ie, wt_fd, path,
1878 ie->blob_sha1, ie->commit_sha1, 1);
1880 return NULL;
1883 static const struct got_error *remove_ondisk_file(const char *, const char *);
1885 static const struct got_error *
1886 update_blob(struct got_worktree *worktree,
1887 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1888 struct got_tree_entry *te, const char *path,
1889 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1890 void *progress_arg)
1892 const struct got_error *err = NULL;
1893 struct got_blob_object *blob = NULL;
1894 char *ondisk_path = NULL;
1895 unsigned char status = GOT_STATUS_NO_CHANGE;
1896 struct stat sb;
1897 int fd1 = -1, fd2 = -1;
1899 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1900 return got_error_from_errno("asprintf");
1902 if (ie) {
1903 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1904 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1905 goto done;
1907 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1908 repo);
1909 if (err)
1910 goto done;
1911 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1912 sb.st_mode = got_fileindex_perms_to_st(ie);
1913 } else {
1914 if (stat(ondisk_path, &sb) == -1) {
1915 if (errno != ENOENT && errno != ENOTDIR) {
1916 err = got_error_from_errno2("stat",
1917 ondisk_path);
1918 goto done;
1920 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1921 status = GOT_STATUS_UNVERSIONED;
1922 } else {
1923 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1924 status = GOT_STATUS_UNVERSIONED;
1925 else
1926 status = GOT_STATUS_OBSTRUCTED;
1930 if (status == GOT_STATUS_OBSTRUCTED) {
1931 if (ie)
1932 got_fileindex_entry_mark_skipped(ie);
1933 err = (*progress_cb)(progress_arg, status, path);
1934 goto done;
1936 if (status == GOT_STATUS_CONFLICT) {
1937 if (ie)
1938 got_fileindex_entry_mark_skipped(ie);
1939 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1940 path);
1941 goto done;
1944 if (S_ISDIR(te->mode)) { /* file changing into a directory */
1945 if (status == GOT_STATUS_UNVERSIONED) {
1946 err = (*progress_cb)(progress_arg, status, path);
1947 } else if (status != GOT_STATUS_NO_CHANGE &&
1948 status != GOT_STATUS_DELETE &&
1949 status != GOT_STATUS_NONEXISTENT &&
1950 status != GOT_STATUS_MISSING) {
1951 err = (*progress_cb)(progress_arg,
1952 GOT_STATUS_CANNOT_DELETE, path);
1953 } else if (ie) {
1954 if (status != GOT_STATUS_DELETE &&
1955 status != GOT_STATUS_NONEXISTENT &&
1956 status != GOT_STATUS_MISSING) {
1957 err = remove_ondisk_file(worktree->root_path,
1958 ie->path);
1959 if (err && !(err->code == GOT_ERR_ERRNO &&
1960 errno == ENOENT))
1961 goto done;
1963 got_fileindex_entry_remove(fileindex, ie);
1964 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE,
1965 ie->path);
1967 goto done; /* nothing else to do */
1970 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1971 (S_ISLNK(te->mode) ||
1972 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1974 * This is a regular file or an installed bad symlink.
1975 * If the file index indicates that this file is already
1976 * up-to-date with respect to the repository we can skip
1977 * updating contents of this file.
1979 if (got_fileindex_entry_has_commit(ie) &&
1980 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1981 SHA1_DIGEST_LENGTH) == 0) {
1982 /* Same commit. */
1983 err = sync_timestamps(worktree->root_fd,
1984 path, status, ie, &sb);
1985 if (err)
1986 goto done;
1987 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1988 path);
1989 goto done;
1991 if (got_fileindex_entry_has_blob(ie) &&
1992 memcmp(ie->blob_sha1, te->id.sha1,
1993 SHA1_DIGEST_LENGTH) == 0) {
1994 /* Different commit but the same blob. */
1995 if (got_fileindex_entry_has_commit(ie)) {
1996 /* Update the base commit ID of this file. */
1997 memcpy(ie->commit_sha1,
1998 worktree->base_commit_id->sha1,
1999 sizeof(ie->commit_sha1));
2001 err = sync_timestamps(worktree->root_fd,
2002 path, status, ie, &sb);
2003 if (err)
2004 goto done;
2005 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2006 path);
2007 goto done;
2011 fd1 = got_opentempfd();
2012 if (fd1 == -1) {
2013 err = got_error_from_errno("got_opentempfd");
2014 goto done;
2016 err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
2017 if (err)
2018 goto done;
2020 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
2021 int update_timestamps;
2022 struct got_blob_object *blob2 = NULL;
2023 char *label_orig = NULL;
2024 if (got_fileindex_entry_has_blob(ie)) {
2025 fd2 = got_opentempfd();
2026 if (fd2 == -1) {
2027 err = got_error_from_errno("got_opentempfd");
2028 goto done;
2030 struct got_object_id id2;
2031 got_fileindex_entry_get_blob_id(&id2, ie);
2032 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
2033 fd2);
2034 if (err)
2035 goto done;
2037 if (got_fileindex_entry_has_commit(ie)) {
2038 char id_str[SHA1_DIGEST_STRING_LENGTH];
2039 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2040 sizeof(id_str)) == NULL) {
2041 err = got_error_path(id_str,
2042 GOT_ERR_BAD_OBJ_ID_STR);
2043 goto done;
2045 if (asprintf(&label_orig, "%s: commit %s",
2046 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2047 err = got_error_from_errno("asprintf");
2048 goto done;
2051 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2052 char *link_target;
2053 err = got_object_blob_read_to_str(&link_target, blob);
2054 if (err)
2055 goto done;
2056 err = merge_symlink(worktree, blob2, ondisk_path, path,
2057 label_orig, link_target, worktree->base_commit_id,
2058 repo, progress_cb, progress_arg);
2059 free(link_target);
2060 } else {
2061 err = merge_blob(&update_timestamps, worktree, blob2,
2062 ondisk_path, path, sb.st_mode, label_orig, blob,
2063 worktree->base_commit_id, repo,
2064 progress_cb, progress_arg);
2066 free(label_orig);
2067 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2068 err = got_error_from_errno("close");
2069 goto done;
2071 if (blob2)
2072 got_object_blob_close(blob2);
2073 if (err)
2074 goto done;
2076 * Do not update timestamps of files with local changes.
2077 * Otherwise, a future status walk would treat them as
2078 * unmodified files again.
2080 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2081 blob->id.sha1, worktree->base_commit_id->sha1,
2082 update_timestamps);
2083 } else if (status == GOT_STATUS_MODE_CHANGE) {
2084 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2085 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2086 } else if (status == GOT_STATUS_DELETE) {
2087 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2088 if (err)
2089 goto done;
2090 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2091 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2092 if (err)
2093 goto done;
2094 } else {
2095 int is_bad_symlink = 0;
2096 if (S_ISLNK(te->mode)) {
2097 err = install_symlink(&is_bad_symlink, worktree,
2098 ondisk_path, path, blob,
2099 status == GOT_STATUS_MISSING, 0,
2100 status == GOT_STATUS_UNVERSIONED, 0,
2101 repo, progress_cb, progress_arg);
2102 } else {
2103 err = install_blob(worktree, ondisk_path, path,
2104 te->mode, sb.st_mode, blob,
2105 status == GOT_STATUS_MISSING, 0, 0,
2106 status == GOT_STATUS_UNVERSIONED, repo,
2107 progress_cb, progress_arg);
2109 if (err)
2110 goto done;
2112 if (ie) {
2113 err = got_fileindex_entry_update(ie,
2114 worktree->root_fd, path, blob->id.sha1,
2115 worktree->base_commit_id->sha1, 1);
2116 } else {
2117 err = create_fileindex_entry(&ie, fileindex,
2118 worktree->base_commit_id, worktree->root_fd, path,
2119 &blob->id);
2121 if (err)
2122 goto done;
2124 if (is_bad_symlink) {
2125 got_fileindex_entry_filetype_set(ie,
2126 GOT_FILEIDX_MODE_BAD_SYMLINK);
2130 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2131 err = got_error_from_errno("close");
2132 goto done;
2134 got_object_blob_close(blob);
2135 done:
2136 free(ondisk_path);
2137 return err;
2140 static const struct got_error *
2141 remove_ondisk_file(const char *root_path, const char *path)
2143 const struct got_error *err = NULL;
2144 char *ondisk_path = NULL, *parent = NULL;
2146 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2147 return got_error_from_errno("asprintf");
2149 if (unlink(ondisk_path) == -1) {
2150 if (errno != ENOENT)
2151 err = got_error_from_errno2("unlink", ondisk_path);
2152 } else {
2153 size_t root_len = strlen(root_path);
2154 err = got_path_dirname(&parent, ondisk_path);
2155 if (err)
2156 goto done;
2157 while (got_path_cmp(parent, root_path,
2158 strlen(parent), root_len) != 0) {
2159 free(ondisk_path);
2160 ondisk_path = parent;
2161 parent = NULL;
2162 if (rmdir(ondisk_path) == -1) {
2163 if (errno != ENOTEMPTY)
2164 err = got_error_from_errno2("rmdir",
2165 ondisk_path);
2166 break;
2168 err = got_path_dirname(&parent, ondisk_path);
2169 if (err)
2170 break;
2173 done:
2174 free(ondisk_path);
2175 free(parent);
2176 return err;
2179 static const struct got_error *
2180 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2181 struct got_fileindex_entry *ie, struct got_repository *repo,
2182 got_worktree_checkout_cb progress_cb, void *progress_arg)
2184 const struct got_error *err = NULL;
2185 unsigned char status;
2186 struct stat sb;
2187 char *ondisk_path;
2189 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2190 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2192 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2193 == -1)
2194 return got_error_from_errno("asprintf");
2196 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2197 if (err)
2198 goto done;
2200 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2201 char ondisk_target[PATH_MAX];
2202 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2203 sizeof(ondisk_target));
2204 if (ondisk_len == -1) {
2205 err = got_error_from_errno2("readlink", ondisk_path);
2206 goto done;
2208 ondisk_target[ondisk_len] = '\0';
2209 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2210 NULL, NULL, /* XXX pass common ancestor info? */
2211 ondisk_target, ondisk_path);
2212 if (err)
2213 goto done;
2214 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2215 ie->path);
2216 goto done;
2219 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2220 status == GOT_STATUS_ADD) {
2221 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2222 if (err)
2223 goto done;
2225 * Preserve the working file and change the deleted blob's
2226 * entry into a schedule-add entry.
2228 err = got_fileindex_entry_update(ie, worktree->root_fd,
2229 ie->path, NULL, NULL, 0);
2230 } else {
2231 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2232 if (err)
2233 goto done;
2234 if (status == GOT_STATUS_NO_CHANGE) {
2235 err = remove_ondisk_file(worktree->root_path, ie->path);
2236 if (err)
2237 goto done;
2239 got_fileindex_entry_remove(fileindex, ie);
2241 done:
2242 free(ondisk_path);
2243 return err;
2246 struct diff_cb_arg {
2247 struct got_fileindex *fileindex;
2248 struct got_worktree *worktree;
2249 struct got_repository *repo;
2250 got_worktree_checkout_cb progress_cb;
2251 void *progress_arg;
2252 got_cancel_cb cancel_cb;
2253 void *cancel_arg;
2256 static const struct got_error *
2257 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2258 struct got_tree_entry *te, const char *parent_path)
2260 struct diff_cb_arg *a = arg;
2262 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2263 return got_error(GOT_ERR_CANCELLED);
2265 return update_blob(a->worktree, a->fileindex, ie, te,
2266 ie->path, a->repo, a->progress_cb, a->progress_arg);
2269 static const struct got_error *
2270 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2272 struct diff_cb_arg *a = arg;
2274 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2275 return got_error(GOT_ERR_CANCELLED);
2277 return delete_blob(a->worktree, a->fileindex, ie,
2278 a->repo, a->progress_cb, a->progress_arg);
2281 static const struct got_error *
2282 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2284 struct diff_cb_arg *a = arg;
2285 const struct got_error *err;
2286 char *path;
2288 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2289 return got_error(GOT_ERR_CANCELLED);
2291 if (got_object_tree_entry_is_submodule(te))
2292 return NULL;
2294 if (asprintf(&path, "%s%s%s", parent_path,
2295 parent_path[0] ? "/" : "", te->name)
2296 == -1)
2297 return got_error_from_errno("asprintf");
2299 if (S_ISDIR(te->mode))
2300 err = add_dir_on_disk(a->worktree, path);
2301 else
2302 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2303 a->repo, a->progress_cb, a->progress_arg);
2305 free(path);
2306 return err;
2309 const struct got_error *
2310 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2312 uint32_t uuid_status;
2314 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2315 if (uuid_status != uuid_s_ok) {
2316 *uuidstr = NULL;
2317 return got_error_uuid(uuid_status, "uuid_to_string");
2320 return NULL;
2323 static const struct got_error *
2324 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2326 const struct got_error *err = NULL;
2327 char *uuidstr = NULL;
2329 *refname = NULL;
2331 err = got_worktree_get_uuid(&uuidstr, worktree);
2332 if (err)
2333 return err;
2335 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2336 err = got_error_from_errno("asprintf");
2337 *refname = NULL;
2339 free(uuidstr);
2340 return err;
2343 const struct got_error *
2344 got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2345 const char *prefix)
2347 return get_ref_name(refname, worktree, prefix);
2350 static const struct got_error *
2351 get_base_ref_name(char **refname, struct got_worktree *worktree)
2353 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2356 static const struct got_error *
2357 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2359 return get_ref_name(refname, worktree,
2360 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2363 static const struct got_error *
2364 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2366 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2369 static const struct got_error *
2370 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2372 return get_ref_name(refname, worktree,
2373 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2376 static const struct got_error *
2377 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2379 return get_ref_name(refname, worktree,
2380 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2383 static const struct got_error *
2384 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2386 return get_ref_name(refname, worktree,
2387 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2390 static const struct got_error *
2391 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2393 return get_ref_name(refname, worktree,
2394 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2397 static const struct got_error *
2398 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2400 return get_ref_name(refname, worktree,
2401 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2404 static const struct got_error *
2405 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2407 return get_ref_name(refname, worktree,
2408 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2411 const struct got_error *
2412 got_worktree_get_histedit_script_path(char **path,
2413 struct got_worktree *worktree)
2415 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2416 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2417 *path = NULL;
2418 return got_error_from_errno("asprintf");
2420 return NULL;
2423 static const struct got_error *
2424 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2426 return get_ref_name(refname, worktree,
2427 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2430 static const struct got_error *
2431 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2433 return get_ref_name(refname, worktree,
2434 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2438 * Prevent Git's garbage collector from deleting our base commit by
2439 * setting a reference to our base commit's ID.
2441 static const struct got_error *
2442 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2444 const struct got_error *err = NULL;
2445 struct got_reference *ref = NULL;
2446 char *refname;
2448 err = get_base_ref_name(&refname, worktree);
2449 if (err)
2450 return err;
2452 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2453 if (err)
2454 goto done;
2456 err = got_ref_write(ref, repo);
2457 done:
2458 free(refname);
2459 if (ref)
2460 got_ref_close(ref);
2461 return err;
2464 static const struct got_error *
2465 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2467 const struct got_error *err = NULL;
2469 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2470 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2471 err = got_error_from_errno("asprintf");
2472 *fileindex_path = NULL;
2474 return err;
2478 static const struct got_error *
2479 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2480 struct got_worktree *worktree)
2482 const struct got_error *err = NULL;
2483 FILE *index = NULL;
2485 *fileindex_path = NULL;
2486 *fileindex = got_fileindex_alloc();
2487 if (*fileindex == NULL)
2488 return got_error_from_errno("got_fileindex_alloc");
2490 err = get_fileindex_path(fileindex_path, worktree);
2491 if (err)
2492 goto done;
2494 index = fopen(*fileindex_path, "rbe");
2495 if (index == NULL) {
2496 if (errno != ENOENT)
2497 err = got_error_from_errno2("fopen", *fileindex_path);
2498 } else {
2499 err = got_fileindex_read(*fileindex, index);
2500 if (fclose(index) == EOF && err == NULL)
2501 err = got_error_from_errno("fclose");
2503 done:
2504 if (err) {
2505 free(*fileindex_path);
2506 *fileindex_path = NULL;
2507 got_fileindex_free(*fileindex);
2508 *fileindex = NULL;
2510 return err;
2513 struct bump_base_commit_id_arg {
2514 struct got_object_id *base_commit_id;
2515 const char *path;
2516 size_t path_len;
2517 const char *entry_name;
2518 got_worktree_checkout_cb progress_cb;
2519 void *progress_arg;
2522 static const struct got_error *
2523 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2525 const struct got_error *err;
2526 struct bump_base_commit_id_arg *a = arg;
2528 if (a->entry_name) {
2529 if (strcmp(ie->path, a->path) != 0)
2530 return NULL;
2531 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2532 return NULL;
2534 if (got_fileindex_entry_was_skipped(ie))
2535 return NULL;
2537 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2538 SHA1_DIGEST_LENGTH) == 0)
2539 return NULL;
2541 if (a->progress_cb) {
2542 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2543 ie->path);
2544 if (err)
2545 return err;
2547 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2548 return NULL;
2551 /* Bump base commit ID of all files within an updated part of the work tree. */
2552 static const struct got_error *
2553 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2554 struct got_fileindex *fileindex,
2555 got_worktree_checkout_cb progress_cb, void *progress_arg)
2557 struct bump_base_commit_id_arg bbc_arg;
2559 bbc_arg.base_commit_id = worktree->base_commit_id;
2560 bbc_arg.entry_name = NULL;
2561 bbc_arg.path = "";
2562 bbc_arg.path_len = 0;
2563 bbc_arg.progress_cb = progress_cb;
2564 bbc_arg.progress_arg = progress_arg;
2566 return got_fileindex_for_each_entry_safe(fileindex,
2567 bump_base_commit_id, &bbc_arg);
2570 static const struct got_error *
2571 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2573 const struct got_error *err = NULL;
2574 char *new_fileindex_path = NULL;
2575 FILE *new_index = NULL;
2576 struct timespec timeout;
2578 err = got_opentemp_named(&new_fileindex_path, &new_index,
2579 fileindex_path, "");
2580 if (err)
2581 goto done;
2583 err = got_fileindex_write(fileindex, new_index);
2584 if (err)
2585 goto done;
2587 if (rename(new_fileindex_path, fileindex_path) != 0) {
2588 err = got_error_from_errno3("rename", new_fileindex_path,
2589 fileindex_path);
2590 unlink(new_fileindex_path);
2594 * Sleep for a short amount of time to ensure that files modified after
2595 * this program exits have a different time stamp from the one which
2596 * was recorded in the file index.
2598 timeout.tv_sec = 0;
2599 timeout.tv_nsec = 1;
2600 nanosleep(&timeout, NULL);
2601 done:
2602 if (new_index)
2603 fclose(new_index);
2604 free(new_fileindex_path);
2605 return err;
2608 static const struct got_error *
2609 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2610 struct got_object_id **tree_id, const char *wt_relpath,
2611 struct got_commit_object *base_commit, struct got_worktree *worktree,
2612 struct got_repository *repo)
2614 const struct got_error *err = NULL;
2615 struct got_object_id *id = NULL;
2616 char *in_repo_path = NULL;
2617 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2619 *entry_type = GOT_OBJ_TYPE_ANY;
2620 *tree_relpath = NULL;
2621 *tree_id = NULL;
2623 if (wt_relpath[0] == '\0') {
2624 /* Check out all files within the work tree. */
2625 *entry_type = GOT_OBJ_TYPE_TREE;
2626 *tree_relpath = strdup("");
2627 if (*tree_relpath == NULL) {
2628 err = got_error_from_errno("strdup");
2629 goto done;
2631 err = got_object_id_by_path(tree_id, repo, base_commit,
2632 worktree->path_prefix);
2633 if (err)
2634 goto done;
2635 return NULL;
2638 /* Check out a subset of files in the work tree. */
2640 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2641 is_root_wt ? "" : "/", wt_relpath) == -1) {
2642 err = got_error_from_errno("asprintf");
2643 goto done;
2646 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2647 if (err)
2648 goto done;
2650 free(in_repo_path);
2651 in_repo_path = NULL;
2653 err = got_object_get_type(entry_type, repo, id);
2654 if (err)
2655 goto done;
2657 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2658 /* Check out a single file. */
2659 if (strchr(wt_relpath, '/') == NULL) {
2660 /* Check out a single file in work tree's root dir. */
2661 in_repo_path = strdup(worktree->path_prefix);
2662 if (in_repo_path == NULL) {
2663 err = got_error_from_errno("strdup");
2664 goto done;
2666 *tree_relpath = strdup("");
2667 if (*tree_relpath == NULL) {
2668 err = got_error_from_errno("strdup");
2669 goto done;
2671 } else {
2672 /* Check out a single file in a subdirectory. */
2673 err = got_path_dirname(tree_relpath, wt_relpath);
2674 if (err)
2675 return err;
2676 if (asprintf(&in_repo_path, "%s%s%s",
2677 worktree->path_prefix, is_root_wt ? "" : "/",
2678 *tree_relpath) == -1) {
2679 err = got_error_from_errno("asprintf");
2680 goto done;
2683 err = got_object_id_by_path(tree_id, repo,
2684 base_commit, in_repo_path);
2685 } else {
2686 /* Check out all files within a subdirectory. */
2687 *tree_id = got_object_id_dup(id);
2688 if (*tree_id == NULL) {
2689 err = got_error_from_errno("got_object_id_dup");
2690 goto done;
2692 *tree_relpath = strdup(wt_relpath);
2693 if (*tree_relpath == NULL) {
2694 err = got_error_from_errno("strdup");
2695 goto done;
2698 done:
2699 free(id);
2700 free(in_repo_path);
2701 if (err) {
2702 *entry_type = GOT_OBJ_TYPE_ANY;
2703 free(*tree_relpath);
2704 *tree_relpath = NULL;
2705 free(*tree_id);
2706 *tree_id = NULL;
2708 return err;
2711 static const struct got_error *
2712 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2713 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2714 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2715 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2717 const struct got_error *err = NULL;
2718 struct got_commit_object *commit = NULL;
2719 struct got_tree_object *tree = NULL;
2720 struct got_fileindex_diff_tree_cb diff_cb;
2721 struct diff_cb_arg arg;
2723 err = ref_base_commit(worktree, repo);
2724 if (err) {
2725 if (!(err->code == GOT_ERR_ERRNO &&
2726 (errno == EACCES || errno == EROFS)))
2727 goto done;
2728 err = (*progress_cb)(progress_arg,
2729 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2730 if (err)
2731 return err;
2734 err = got_object_open_as_commit(&commit, repo,
2735 worktree->base_commit_id);
2736 if (err)
2737 goto done;
2739 err = got_object_open_as_tree(&tree, repo, tree_id);
2740 if (err)
2741 goto done;
2743 if (entry_name &&
2744 got_object_tree_find_entry(tree, entry_name) == NULL) {
2745 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2746 goto done;
2749 diff_cb.diff_old_new = diff_old_new;
2750 diff_cb.diff_old = diff_old;
2751 diff_cb.diff_new = diff_new;
2752 arg.fileindex = fileindex;
2753 arg.worktree = worktree;
2754 arg.repo = repo;
2755 arg.progress_cb = progress_cb;
2756 arg.progress_arg = progress_arg;
2757 arg.cancel_cb = cancel_cb;
2758 arg.cancel_arg = cancel_arg;
2759 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2760 entry_name, repo, &diff_cb, &arg);
2761 done:
2762 if (tree)
2763 got_object_tree_close(tree);
2764 if (commit)
2765 got_object_commit_close(commit);
2766 return err;
2769 const struct got_error *
2770 got_worktree_checkout_files(struct got_worktree *worktree,
2771 struct got_pathlist_head *paths, struct got_repository *repo,
2772 got_worktree_checkout_cb progress_cb, void *progress_arg,
2773 got_cancel_cb cancel_cb, void *cancel_arg)
2775 const struct got_error *err = NULL, *sync_err, *unlockerr;
2776 struct got_commit_object *commit = NULL;
2777 struct got_tree_object *tree = NULL;
2778 struct got_fileindex *fileindex = NULL;
2779 char *fileindex_path = NULL;
2780 struct got_pathlist_entry *pe;
2781 struct tree_path_data {
2782 STAILQ_ENTRY(tree_path_data) entry;
2783 struct got_object_id *tree_id;
2784 int entry_type;
2785 char *relpath;
2786 char *entry_name;
2787 } *tpd = NULL;
2788 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2790 STAILQ_INIT(&tree_paths);
2792 err = lock_worktree(worktree, LOCK_EX);
2793 if (err)
2794 return err;
2796 err = got_object_open_as_commit(&commit, repo,
2797 worktree->base_commit_id);
2798 if (err)
2799 goto done;
2801 /* Map all specified paths to in-repository trees. */
2802 TAILQ_FOREACH(pe, paths, entry) {
2803 tpd = malloc(sizeof(*tpd));
2804 if (tpd == NULL) {
2805 err = got_error_from_errno("malloc");
2806 goto done;
2809 err = find_tree_entry_for_checkout(&tpd->entry_type,
2810 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2811 worktree, repo);
2812 if (err) {
2813 free(tpd);
2814 goto done;
2817 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2818 err = got_path_basename(&tpd->entry_name, pe->path);
2819 if (err) {
2820 free(tpd->relpath);
2821 free(tpd->tree_id);
2822 free(tpd);
2823 goto done;
2825 } else
2826 tpd->entry_name = NULL;
2828 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2832 * Read the file index.
2833 * Checking out files is supposed to be an idempotent operation.
2834 * If the on-disk file index is incomplete we will try to complete it.
2836 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2837 if (err)
2838 goto done;
2840 tpd = STAILQ_FIRST(&tree_paths);
2841 TAILQ_FOREACH(pe, paths, entry) {
2842 struct bump_base_commit_id_arg bbc_arg;
2844 err = checkout_files(worktree, fileindex, tpd->relpath,
2845 tpd->tree_id, tpd->entry_name, repo,
2846 progress_cb, progress_arg, cancel_cb, cancel_arg);
2847 if (err)
2848 break;
2850 bbc_arg.base_commit_id = worktree->base_commit_id;
2851 bbc_arg.entry_name = tpd->entry_name;
2852 bbc_arg.path = pe->path;
2853 bbc_arg.path_len = pe->path_len;
2854 bbc_arg.progress_cb = progress_cb;
2855 bbc_arg.progress_arg = progress_arg;
2856 err = got_fileindex_for_each_entry_safe(fileindex,
2857 bump_base_commit_id, &bbc_arg);
2858 if (err)
2859 break;
2861 tpd = STAILQ_NEXT(tpd, entry);
2863 sync_err = sync_fileindex(fileindex, fileindex_path);
2864 if (sync_err && err == NULL)
2865 err = sync_err;
2866 done:
2867 free(fileindex_path);
2868 if (tree)
2869 got_object_tree_close(tree);
2870 if (commit)
2871 got_object_commit_close(commit);
2872 if (fileindex)
2873 got_fileindex_free(fileindex);
2874 while (!STAILQ_EMPTY(&tree_paths)) {
2875 tpd = STAILQ_FIRST(&tree_paths);
2876 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2877 free(tpd->relpath);
2878 free(tpd->tree_id);
2879 free(tpd);
2881 unlockerr = lock_worktree(worktree, LOCK_SH);
2882 if (unlockerr && err == NULL)
2883 err = unlockerr;
2884 return err;
2887 static const struct got_error *
2888 add_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2889 struct got_fileindex_entry *ie, const char *ondisk_path,
2890 const char *path2, struct got_blob_object *blob2, mode_t mode2,
2891 int restoring_missing_file, int reverting_versioned_file,
2892 int path_is_unversioned, int allow_bad_symlinks,
2893 struct got_repository *repo,
2894 got_worktree_checkout_cb progress_cb, void *progress_arg)
2896 const struct got_error *err = NULL;
2897 int is_bad_symlink = 0;
2899 if (S_ISLNK(mode2)) {
2900 err = install_symlink(&is_bad_symlink,
2901 worktree, ondisk_path, path2, blob2,
2902 restoring_missing_file,
2903 reverting_versioned_file,
2904 path_is_unversioned, allow_bad_symlinks,
2905 repo, progress_cb, progress_arg);
2906 } else {
2907 err = install_blob(worktree, ondisk_path, path2,
2908 mode2, GOT_DEFAULT_FILE_MODE, blob2,
2909 restoring_missing_file, reverting_versioned_file, 0,
2910 path_is_unversioned, repo, progress_cb, progress_arg);
2912 if (err)
2913 return err;
2914 if (ie == NULL) {
2915 /* Adding an unversioned file. */
2916 err = got_fileindex_entry_alloc(&ie, path2);
2917 if (err)
2918 return err;
2919 err = got_fileindex_entry_update(ie,
2920 worktree->root_fd, path2, NULL, NULL, 1);
2921 if (err) {
2922 got_fileindex_entry_free(ie);
2923 return err;
2925 err = got_fileindex_entry_add(fileindex, ie);
2926 if (err) {
2927 got_fileindex_entry_free(ie);
2928 return err;
2930 } else {
2931 /* Re-adding a locally deleted file. */
2932 err = got_fileindex_entry_update(ie,
2933 worktree->root_fd, path2, ie->blob_sha1,
2934 worktree->base_commit_id->sha1, 0);
2935 if (err)
2936 return err;
2939 if (is_bad_symlink) {
2940 got_fileindex_entry_filetype_set(ie,
2941 GOT_FILEIDX_MODE_BAD_SYMLINK);
2944 return NULL;
2947 struct merge_file_cb_arg {
2948 struct got_worktree *worktree;
2949 struct got_fileindex *fileindex;
2950 got_worktree_checkout_cb progress_cb;
2951 void *progress_arg;
2952 got_cancel_cb cancel_cb;
2953 void *cancel_arg;
2954 const char *label_orig;
2955 struct got_object_id *commit_id2;
2956 int allow_bad_symlinks;
2959 static const struct got_error *
2960 merge_file_cb(void *arg, struct got_blob_object *blob1,
2961 struct got_blob_object *blob2, FILE *f1, FILE *f2,
2962 struct got_object_id *id1, struct got_object_id *id2,
2963 const char *path1, const char *path2,
2964 mode_t mode1, mode_t mode2, struct got_repository *repo)
2966 static const struct got_error *err = NULL;
2967 struct merge_file_cb_arg *a = arg;
2968 struct got_fileindex_entry *ie;
2969 char *ondisk_path = NULL;
2970 struct stat sb;
2971 unsigned char status;
2972 int local_changes_subsumed;
2973 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2974 char *id_str = NULL, *label_deriv2 = NULL;
2976 if (blob1 && blob2) {
2977 ie = got_fileindex_entry_get(a->fileindex, path2,
2978 strlen(path2));
2979 if (ie == NULL)
2980 return (*a->progress_cb)(a->progress_arg,
2981 GOT_STATUS_MISSING, path2);
2983 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2984 path2) == -1)
2985 return got_error_from_errno("asprintf");
2987 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2988 repo);
2989 if (err)
2990 goto done;
2992 if (status == GOT_STATUS_DELETE) {
2993 err = (*a->progress_cb)(a->progress_arg,
2994 GOT_STATUS_MERGE, path2);
2995 goto done;
2997 if (status != GOT_STATUS_NO_CHANGE &&
2998 status != GOT_STATUS_MODIFY &&
2999 status != GOT_STATUS_CONFLICT &&
3000 status != GOT_STATUS_ADD) {
3001 err = (*a->progress_cb)(a->progress_arg, status, path2);
3002 goto done;
3005 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
3006 char *link_target2;
3007 err = got_object_blob_read_to_str(&link_target2, blob2);
3008 if (err)
3009 goto done;
3010 err = merge_symlink(a->worktree, blob1, ondisk_path,
3011 path2, a->label_orig, link_target2, a->commit_id2,
3012 repo, a->progress_cb, a->progress_arg);
3013 free(link_target2);
3014 } else {
3015 int fd;
3017 f_orig = got_opentemp();
3018 if (f_orig == NULL) {
3019 err = got_error_from_errno("got_opentemp");
3020 goto done;
3022 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3023 f_orig, blob1);
3024 if (err)
3025 goto done;
3027 f_deriv2 = got_opentemp();
3028 if (f_deriv2 == NULL)
3029 goto done;
3030 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3031 f_deriv2, blob2);
3032 if (err)
3033 goto done;
3035 fd = open(ondisk_path,
3036 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3037 if (fd == -1) {
3038 err = got_error_from_errno2("open",
3039 ondisk_path);
3040 goto done;
3042 f_deriv = fdopen(fd, "r");
3043 if (f_deriv == NULL) {
3044 err = got_error_from_errno2("fdopen",
3045 ondisk_path);
3046 close(fd);
3047 goto done;
3049 err = got_object_id_str(&id_str, a->commit_id2);
3050 if (err)
3051 goto done;
3052 if (asprintf(&label_deriv2, "%s: commit %s",
3053 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
3054 err = got_error_from_errno("asprintf");
3055 goto done;
3057 err = merge_file(&local_changes_subsumed, a->worktree,
3058 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
3059 mode2, a->label_orig, NULL, label_deriv2,
3060 GOT_DIFF_ALGORITHM_PATIENCE, repo,
3061 a->progress_cb, a->progress_arg);
3063 } else if (blob1) {
3064 ie = got_fileindex_entry_get(a->fileindex, path1,
3065 strlen(path1));
3066 if (ie == NULL)
3067 return (*a->progress_cb)(a->progress_arg,
3068 GOT_STATUS_MISSING, path1);
3070 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3071 path1) == -1)
3072 return got_error_from_errno("asprintf");
3074 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3075 repo);
3076 if (err)
3077 goto done;
3079 switch (status) {
3080 case GOT_STATUS_NO_CHANGE:
3081 err = (*a->progress_cb)(a->progress_arg,
3082 GOT_STATUS_DELETE, path1);
3083 if (err)
3084 goto done;
3085 err = remove_ondisk_file(a->worktree->root_path, path1);
3086 if (err)
3087 goto done;
3088 if (ie)
3089 got_fileindex_entry_mark_deleted_from_disk(ie);
3090 break;
3091 case GOT_STATUS_DELETE:
3092 case GOT_STATUS_MISSING:
3093 err = (*a->progress_cb)(a->progress_arg,
3094 GOT_STATUS_DELETE, path1);
3095 if (err)
3096 goto done;
3097 if (ie)
3098 got_fileindex_entry_mark_deleted_from_disk(ie);
3099 break;
3100 case GOT_STATUS_ADD: {
3101 struct got_object_id *id;
3102 FILE *blob1_f;
3103 off_t blob1_size;
3105 * Delete the added file only if its content already
3106 * exists in the repository.
3108 err = got_object_blob_file_create(&id, &blob1_f,
3109 &blob1_size, path1);
3110 if (err)
3111 goto done;
3112 if (got_object_id_cmp(id, id1) == 0) {
3113 err = (*a->progress_cb)(a->progress_arg,
3114 GOT_STATUS_DELETE, path1);
3115 if (err)
3116 goto done;
3117 err = remove_ondisk_file(a->worktree->root_path,
3118 path1);
3119 if (err)
3120 goto done;
3121 if (ie)
3122 got_fileindex_entry_remove(a->fileindex,
3123 ie);
3124 } else {
3125 err = (*a->progress_cb)(a->progress_arg,
3126 GOT_STATUS_CANNOT_DELETE, path1);
3128 if (fclose(blob1_f) == EOF && err == NULL)
3129 err = got_error_from_errno("fclose");
3130 free(id);
3131 if (err)
3132 goto done;
3133 break;
3135 case GOT_STATUS_MODIFY:
3136 case GOT_STATUS_CONFLICT:
3137 err = (*a->progress_cb)(a->progress_arg,
3138 GOT_STATUS_CANNOT_DELETE, path1);
3139 if (err)
3140 goto done;
3141 break;
3142 case GOT_STATUS_OBSTRUCTED:
3143 err = (*a->progress_cb)(a->progress_arg, status, path1);
3144 if (err)
3145 goto done;
3146 break;
3147 default:
3148 break;
3150 } else if (blob2) {
3151 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3152 path2) == -1)
3153 return got_error_from_errno("asprintf");
3154 ie = got_fileindex_entry_get(a->fileindex, path2,
3155 strlen(path2));
3156 if (ie) {
3157 err = get_file_status(&status, &sb, ie, ondisk_path,
3158 -1, NULL, repo);
3159 if (err)
3160 goto done;
3161 if (status != GOT_STATUS_NO_CHANGE &&
3162 status != GOT_STATUS_MODIFY &&
3163 status != GOT_STATUS_CONFLICT &&
3164 status != GOT_STATUS_ADD &&
3165 status != GOT_STATUS_DELETE) {
3166 err = (*a->progress_cb)(a->progress_arg,
3167 status, path2);
3168 goto done;
3170 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3171 char *link_target2;
3172 err = got_object_blob_read_to_str(&link_target2,
3173 blob2);
3174 if (err)
3175 goto done;
3176 err = merge_symlink(a->worktree, NULL,
3177 ondisk_path, path2, a->label_orig,
3178 link_target2, a->commit_id2, repo,
3179 a->progress_cb, a->progress_arg);
3180 free(link_target2);
3181 } else if (S_ISREG(sb.st_mode)) {
3182 err = merge_blob(&local_changes_subsumed,
3183 a->worktree, NULL, ondisk_path, path2,
3184 sb.st_mode, a->label_orig, blob2,
3185 a->commit_id2, repo, a->progress_cb,
3186 a->progress_arg);
3187 } else if (status != GOT_STATUS_DELETE) {
3188 err = got_error_path(ondisk_path,
3189 GOT_ERR_FILE_OBSTRUCTED);
3191 if (err)
3192 goto done;
3193 if (status == GOT_STATUS_DELETE) {
3194 /* Re-add file with content from new blob. */
3195 err = add_file(a->worktree, a->fileindex, ie,
3196 ondisk_path, path2, blob2, mode2,
3197 0, 0, 0, a->allow_bad_symlinks,
3198 repo, a->progress_cb, a->progress_arg);
3199 if (err)
3200 goto done;
3202 } else {
3203 err = add_file(a->worktree, a->fileindex, NULL,
3204 ondisk_path, path2, blob2, mode2,
3205 0, 0, 1, a->allow_bad_symlinks,
3206 repo, a->progress_cb, a->progress_arg);
3207 if (err)
3208 goto done;
3211 done:
3212 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3213 err = got_error_from_errno("fclose");
3214 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3215 err = got_error_from_errno("fclose");
3216 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3217 err = got_error_from_errno("fclose");
3218 free(id_str);
3219 free(label_deriv2);
3220 free(ondisk_path);
3221 return err;
3224 static const struct got_error *
3225 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3227 struct got_worktree *worktree = arg;
3229 /* Reject merges into a work tree with mixed base commits. */
3230 if (got_fileindex_entry_has_commit(ie) &&
3231 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3232 SHA1_DIGEST_LENGTH) != 0)
3233 return got_error(GOT_ERR_MIXED_COMMITS);
3235 return NULL;
3238 struct check_merge_conflicts_arg {
3239 struct got_worktree *worktree;
3240 struct got_fileindex *fileindex;
3241 struct got_repository *repo;
3244 static const struct got_error *
3245 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3246 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3247 struct got_object_id *id1, struct got_object_id *id2,
3248 const char *path1, const char *path2,
3249 mode_t mode1, mode_t mode2, struct got_repository *repo)
3251 const struct got_error *err = NULL;
3252 struct check_merge_conflicts_arg *a = arg;
3253 unsigned char status;
3254 struct stat sb;
3255 struct got_fileindex_entry *ie;
3256 const char *path = path2 ? path2 : path1;
3257 struct got_object_id *id = id2 ? id2 : id1;
3258 char *ondisk_path;
3260 if (id == NULL)
3261 return NULL;
3263 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3264 if (ie == NULL)
3265 return NULL;
3267 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3268 == -1)
3269 return got_error_from_errno("asprintf");
3271 /* Reject merges into a work tree with conflicted files. */
3272 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3273 free(ondisk_path);
3274 if (err)
3275 return err;
3276 if (status == GOT_STATUS_CONFLICT)
3277 return got_error(GOT_ERR_CONFLICTS);
3279 return NULL;
3282 static const struct got_error *
3283 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3284 const char *fileindex_path, struct got_object_id *commit_id1,
3285 struct got_object_id *commit_id2, struct got_repository *repo,
3286 got_worktree_checkout_cb progress_cb, void *progress_arg,
3287 got_cancel_cb cancel_cb, void *cancel_arg)
3289 const struct got_error *err = NULL, *sync_err;
3290 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3291 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3292 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3293 struct check_merge_conflicts_arg cmc_arg;
3294 struct merge_file_cb_arg arg;
3295 char *label_orig = NULL;
3296 FILE *f1 = NULL, *f2 = NULL;
3297 int fd1 = -1, fd2 = -1;
3299 if (commit_id1) {
3300 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3301 if (err)
3302 goto done;
3303 err = got_object_id_by_path(&tree_id1, repo, commit1,
3304 worktree->path_prefix);
3305 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3306 goto done;
3308 if (tree_id1) {
3309 char *id_str;
3311 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3312 if (err)
3313 goto done;
3315 err = got_object_id_str(&id_str, commit_id1);
3316 if (err)
3317 goto done;
3319 if (asprintf(&label_orig, "%s: commit %s",
3320 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3321 err = got_error_from_errno("asprintf");
3322 free(id_str);
3323 goto done;
3325 free(id_str);
3327 f1 = got_opentemp();
3328 if (f1 == NULL) {
3329 err = got_error_from_errno("got_opentemp");
3330 goto done;
3334 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3335 if (err)
3336 goto done;
3338 err = got_object_id_by_path(&tree_id2, repo, commit2,
3339 worktree->path_prefix);
3340 if (err)
3341 goto done;
3343 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3344 if (err)
3345 goto done;
3347 f2 = got_opentemp();
3348 if (f2 == NULL) {
3349 err = got_error_from_errno("got_opentemp");
3350 goto done;
3353 fd1 = got_opentempfd();
3354 if (fd1 == -1) {
3355 err = got_error_from_errno("got_opentempfd");
3356 goto done;
3359 fd2 = got_opentempfd();
3360 if (fd2 == -1) {
3361 err = got_error_from_errno("got_opentempfd");
3362 goto done;
3365 cmc_arg.worktree = worktree;
3366 cmc_arg.fileindex = fileindex;
3367 cmc_arg.repo = repo;
3368 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3369 check_merge_conflicts, &cmc_arg, 0);
3370 if (err)
3371 goto done;
3373 arg.worktree = worktree;
3374 arg.fileindex = fileindex;
3375 arg.progress_cb = progress_cb;
3376 arg.progress_arg = progress_arg;
3377 arg.cancel_cb = cancel_cb;
3378 arg.cancel_arg = cancel_arg;
3379 arg.label_orig = label_orig;
3380 arg.commit_id2 = commit_id2;
3381 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3382 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3383 merge_file_cb, &arg, 1);
3384 sync_err = sync_fileindex(fileindex, fileindex_path);
3385 if (sync_err && err == NULL)
3386 err = sync_err;
3387 done:
3388 if (commit1)
3389 got_object_commit_close(commit1);
3390 if (commit2)
3391 got_object_commit_close(commit2);
3392 if (tree1)
3393 got_object_tree_close(tree1);
3394 if (tree2)
3395 got_object_tree_close(tree2);
3396 if (f1 && fclose(f1) == EOF && err == NULL)
3397 err = got_error_from_errno("fclose");
3398 if (f2 && fclose(f2) == EOF && err == NULL)
3399 err = got_error_from_errno("fclose");
3400 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3401 err = got_error_from_errno("close");
3402 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3403 err = got_error_from_errno("close");
3404 free(label_orig);
3405 return err;
3408 const struct got_error *
3409 got_worktree_merge_files(struct got_worktree *worktree,
3410 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3411 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3412 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3414 const struct got_error *err, *unlockerr;
3415 char *fileindex_path = NULL;
3416 struct got_fileindex *fileindex = NULL;
3418 err = lock_worktree(worktree, LOCK_EX);
3419 if (err)
3420 return err;
3422 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3423 if (err)
3424 goto done;
3426 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3427 worktree);
3428 if (err)
3429 goto done;
3431 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3432 commit_id2, repo, progress_cb, progress_arg,
3433 cancel_cb, cancel_arg);
3434 done:
3435 if (fileindex)
3436 got_fileindex_free(fileindex);
3437 free(fileindex_path);
3438 unlockerr = lock_worktree(worktree, LOCK_SH);
3439 if (unlockerr && err == NULL)
3440 err = unlockerr;
3441 return err;
3444 struct diff_dir_cb_arg {
3445 struct got_fileindex *fileindex;
3446 struct got_worktree *worktree;
3447 const char *status_path;
3448 size_t status_path_len;
3449 struct got_repository *repo;
3450 got_worktree_status_cb status_cb;
3451 void *status_arg;
3452 got_cancel_cb cancel_cb;
3453 void *cancel_arg;
3454 /* A pathlist containing per-directory pathlists of ignore patterns. */
3455 struct got_pathlist_head *ignores;
3456 int report_unchanged;
3457 int no_ignores;
3460 static const struct got_error *
3461 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3462 int dirfd, const char *de_name,
3463 got_worktree_status_cb status_cb, void *status_arg,
3464 struct got_repository *repo, int report_unchanged)
3466 const struct got_error *err = NULL;
3467 unsigned char status = GOT_STATUS_NO_CHANGE;
3468 unsigned char staged_status;
3469 struct stat sb;
3470 struct got_object_id blob_id, commit_id, staged_blob_id;
3471 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3472 struct got_object_id *staged_blob_idp = NULL;
3474 staged_status = get_staged_status(ie);
3475 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3476 if (err)
3477 return err;
3479 if (status == GOT_STATUS_NO_CHANGE &&
3480 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3481 return NULL;
3483 if (got_fileindex_entry_has_blob(ie))
3484 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3485 if (got_fileindex_entry_has_commit(ie))
3486 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3487 if (staged_status == GOT_STATUS_ADD ||
3488 staged_status == GOT_STATUS_MODIFY) {
3489 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3490 &staged_blob_id, ie);
3493 return (*status_cb)(status_arg, status, staged_status,
3494 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3497 static const struct got_error *
3498 status_old_new(void *arg, struct got_fileindex_entry *ie,
3499 struct dirent *de, const char *parent_path, int dirfd)
3501 const struct got_error *err = NULL;
3502 struct diff_dir_cb_arg *a = arg;
3503 char *abspath;
3505 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3506 return got_error(GOT_ERR_CANCELLED);
3508 if (got_path_cmp(parent_path, a->status_path,
3509 strlen(parent_path), a->status_path_len) != 0 &&
3510 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3511 return NULL;
3513 if (parent_path[0]) {
3514 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3515 parent_path, de->d_name) == -1)
3516 return got_error_from_errno("asprintf");
3517 } else {
3518 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3519 de->d_name) == -1)
3520 return got_error_from_errno("asprintf");
3523 err = report_file_status(ie, abspath, dirfd, de->d_name,
3524 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3525 free(abspath);
3526 return err;
3529 static const struct got_error *
3530 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3532 struct diff_dir_cb_arg *a = arg;
3533 struct got_object_id blob_id, commit_id;
3534 unsigned char status;
3536 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3537 return got_error(GOT_ERR_CANCELLED);
3539 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3540 return NULL;
3542 got_fileindex_entry_get_blob_id(&blob_id, ie);
3543 got_fileindex_entry_get_commit_id(&commit_id, ie);
3544 if (got_fileindex_entry_has_file_on_disk(ie))
3545 status = GOT_STATUS_MISSING;
3546 else
3547 status = GOT_STATUS_DELETE;
3548 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3549 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3552 static void
3553 free_ignores(struct got_pathlist_head *ignores)
3555 struct got_pathlist_entry *pe;
3557 TAILQ_FOREACH(pe, ignores, entry) {
3558 struct got_pathlist_head *ignorelist = pe->data;
3560 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3562 got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3565 static const struct got_error *
3566 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3568 const struct got_error *err = NULL;
3569 struct got_pathlist_entry *pe = NULL;
3570 struct got_pathlist_head *ignorelist;
3571 char *line = NULL, *pattern, *dirpath = NULL;
3572 size_t linesize = 0;
3573 ssize_t linelen;
3575 ignorelist = calloc(1, sizeof(*ignorelist));
3576 if (ignorelist == NULL)
3577 return got_error_from_errno("calloc");
3578 TAILQ_INIT(ignorelist);
3580 while ((linelen = getline(&line, &linesize, f)) != -1) {
3581 if (linelen > 0 && line[linelen - 1] == '\n')
3582 line[linelen - 1] = '\0';
3584 /* Git's ignores may contain comments. */
3585 if (line[0] == '#')
3586 continue;
3588 /* Git's negated patterns are not (yet?) supported. */
3589 if (line[0] == '!')
3590 continue;
3592 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3593 line) == -1) {
3594 err = got_error_from_errno("asprintf");
3595 goto done;
3597 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3598 if (err)
3599 goto done;
3601 if (ferror(f)) {
3602 err = got_error_from_errno("getline");
3603 goto done;
3606 dirpath = strdup(path);
3607 if (dirpath == NULL) {
3608 err = got_error_from_errno("strdup");
3609 goto done;
3611 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3612 done:
3613 free(line);
3614 if (err || pe == NULL) {
3615 free(dirpath);
3616 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3618 return err;
3621 static int
3622 match_path(const char *pattern, size_t pattern_len, const char *path,
3623 int flags)
3625 char buf[PATH_MAX];
3628 * Trailing slashes signify directories.
3629 * Append a * to make such patterns conform to fnmatch rules.
3631 if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3632 if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3633 return FNM_NOMATCH; /* XXX */
3635 return fnmatch(buf, path, flags);
3638 return fnmatch(pattern, path, flags);
3641 static int
3642 match_ignores(struct got_pathlist_head *ignores, const char *path)
3644 struct got_pathlist_entry *pe;
3646 /* Handle patterns which match in all directories. */
3647 TAILQ_FOREACH(pe, ignores, entry) {
3648 struct got_pathlist_head *ignorelist = pe->data;
3649 struct got_pathlist_entry *pi;
3651 TAILQ_FOREACH(pi, ignorelist, entry) {
3652 const char *p;
3654 if (pi->path_len < 3 ||
3655 strncmp(pi->path, "**/", 3) != 0)
3656 continue;
3657 p = path;
3658 while (*p) {
3659 if (match_path(pi->path + 3,
3660 pi->path_len - 3, p,
3661 FNM_PATHNAME | FNM_LEADING_DIR)) {
3662 /* Retry in next directory. */
3663 while (*p && *p != '/')
3664 p++;
3665 while (*p == '/')
3666 p++;
3667 continue;
3669 return 1;
3675 * The ignores pathlist contains ignore lists from children before
3676 * parents, so we can find the most specific ignorelist by walking
3677 * ignores backwards.
3679 pe = TAILQ_LAST(ignores, got_pathlist_head);
3680 while (pe) {
3681 if (got_path_is_child(path, pe->path, pe->path_len)) {
3682 struct got_pathlist_head *ignorelist = pe->data;
3683 struct got_pathlist_entry *pi;
3684 TAILQ_FOREACH(pi, ignorelist, entry) {
3685 int flags = FNM_LEADING_DIR;
3686 if (strstr(pi->path, "/**/") == NULL)
3687 flags |= FNM_PATHNAME;
3688 if (match_path(pi->path, pi->path_len,
3689 path, flags))
3690 continue;
3691 return 1;
3694 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3697 return 0;
3700 static const struct got_error *
3701 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3702 const char *path, int dirfd, const char *ignores_filename)
3704 const struct got_error *err = NULL;
3705 char *ignorespath;
3706 int fd = -1;
3707 FILE *ignoresfile = NULL;
3709 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3710 path[0] ? "/" : "", ignores_filename) == -1)
3711 return got_error_from_errno("asprintf");
3713 if (dirfd != -1) {
3714 fd = openat(dirfd, ignores_filename,
3715 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3716 if (fd == -1) {
3717 if (errno != ENOENT && errno != EACCES)
3718 err = got_error_from_errno2("openat",
3719 ignorespath);
3720 } else {
3721 ignoresfile = fdopen(fd, "r");
3722 if (ignoresfile == NULL)
3723 err = got_error_from_errno2("fdopen",
3724 ignorespath);
3725 else {
3726 fd = -1;
3727 err = read_ignores(ignores, path, ignoresfile);
3730 } else {
3731 ignoresfile = fopen(ignorespath, "re");
3732 if (ignoresfile == NULL) {
3733 if (errno != ENOENT && errno != EACCES)
3734 err = got_error_from_errno2("fopen",
3735 ignorespath);
3736 } else
3737 err = read_ignores(ignores, path, ignoresfile);
3740 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3741 err = got_error_from_errno2("fclose", path);
3742 if (fd != -1 && close(fd) == -1 && err == NULL)
3743 err = got_error_from_errno2("close", path);
3744 free(ignorespath);
3745 return err;
3748 static const struct got_error *
3749 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3750 int dirfd)
3752 const struct got_error *err = NULL;
3753 struct diff_dir_cb_arg *a = arg;
3754 char *path = NULL;
3756 if (ignore != NULL)
3757 *ignore = 0;
3759 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3760 return got_error(GOT_ERR_CANCELLED);
3762 if (parent_path[0]) {
3763 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3764 return got_error_from_errno("asprintf");
3765 } else {
3766 path = de->d_name;
3769 if (de->d_type == DT_DIR) {
3770 if (!a->no_ignores && ignore != NULL &&
3771 match_ignores(a->ignores, path))
3772 *ignore = 1;
3773 } else if (!match_ignores(a->ignores, path) &&
3774 got_path_is_child(path, a->status_path, a->status_path_len))
3775 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3776 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3777 if (parent_path[0])
3778 free(path);
3779 return err;
3782 static const struct got_error *
3783 status_traverse(void *arg, const char *path, int dirfd)
3785 const struct got_error *err = NULL;
3786 struct diff_dir_cb_arg *a = arg;
3788 if (a->no_ignores)
3789 return NULL;
3791 err = add_ignores(a->ignores, a->worktree->root_path,
3792 path, dirfd, ".cvsignore");
3793 if (err)
3794 return err;
3796 err = add_ignores(a->ignores, a->worktree->root_path, path,
3797 dirfd, ".gitignore");
3799 return err;
3802 static const struct got_error *
3803 report_single_file_status(const char *path, const char *ondisk_path,
3804 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3805 void *status_arg, struct got_repository *repo, int report_unchanged,
3806 struct got_pathlist_head *ignores, int no_ignores)
3808 struct got_fileindex_entry *ie;
3809 struct stat sb;
3811 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3812 if (ie)
3813 return report_file_status(ie, ondisk_path, -1, NULL,
3814 status_cb, status_arg, repo, report_unchanged);
3816 if (lstat(ondisk_path, &sb) == -1) {
3817 if (errno != ENOENT)
3818 return got_error_from_errno2("lstat", ondisk_path);
3819 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3820 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3823 if (!no_ignores && match_ignores(ignores, path))
3824 return NULL;
3826 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3827 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3828 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3830 return NULL;
3833 static const struct got_error *
3834 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3835 const char *root_path, const char *path)
3837 const struct got_error *err;
3838 char *parent_path, *next_parent_path = NULL;
3840 err = add_ignores(ignores, root_path, "", -1,
3841 ".cvsignore");
3842 if (err)
3843 return err;
3845 err = add_ignores(ignores, root_path, "", -1,
3846 ".gitignore");
3847 if (err)
3848 return err;
3850 err = got_path_dirname(&parent_path, path);
3851 if (err) {
3852 if (err->code == GOT_ERR_BAD_PATH)
3853 return NULL; /* cannot traverse parent */
3854 return err;
3856 for (;;) {
3857 err = add_ignores(ignores, root_path, parent_path, -1,
3858 ".cvsignore");
3859 if (err)
3860 break;
3861 err = add_ignores(ignores, root_path, parent_path, -1,
3862 ".gitignore");
3863 if (err)
3864 break;
3865 err = got_path_dirname(&next_parent_path, parent_path);
3866 if (err) {
3867 if (err->code == GOT_ERR_BAD_PATH)
3868 err = NULL; /* traversed everything */
3869 break;
3871 if (got_path_is_root_dir(parent_path))
3872 break;
3873 free(parent_path);
3874 parent_path = next_parent_path;
3875 next_parent_path = NULL;
3878 free(parent_path);
3879 free(next_parent_path);
3880 return err;
3883 struct find_missing_children_args {
3884 const char *parent_path;
3885 size_t parent_len;
3886 struct got_pathlist_head *children;
3887 got_cancel_cb cancel_cb;
3888 void *cancel_arg;
3891 static const struct got_error *
3892 find_missing_children(void *arg, struct got_fileindex_entry *ie)
3894 const struct got_error *err = NULL;
3895 struct find_missing_children_args *a = arg;
3897 if (a->cancel_cb) {
3898 err = a->cancel_cb(a->cancel_arg);
3899 if (err)
3900 return err;
3903 if (got_path_is_child(ie->path, a->parent_path, a->parent_len))
3904 err = got_pathlist_append(a->children, ie->path, NULL);
3906 return err;
3909 static const struct got_error *
3910 report_children(struct got_pathlist_head *children,
3911 struct got_worktree *worktree, struct got_fileindex *fileindex,
3912 struct got_repository *repo, int is_root_dir, int report_unchanged,
3913 struct got_pathlist_head *ignores, int no_ignores,
3914 got_worktree_status_cb status_cb, void *status_arg,
3915 got_cancel_cb cancel_cb, void *cancel_arg)
3917 const struct got_error *err = NULL;
3918 struct got_pathlist_entry *pe;
3919 char *ondisk_path = NULL;
3921 TAILQ_FOREACH(pe, children, entry) {
3922 if (cancel_cb) {
3923 err = cancel_cb(cancel_arg);
3924 if (err)
3925 break;
3928 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
3929 !is_root_dir ? "/" : "", pe->path) == -1) {
3930 err = got_error_from_errno("asprintf");
3931 ondisk_path = NULL;
3932 break;
3935 err = report_single_file_status(pe->path, ondisk_path,
3936 fileindex, status_cb, status_arg, repo, report_unchanged,
3937 ignores, no_ignores);
3938 if (err)
3939 break;
3941 free(ondisk_path);
3942 ondisk_path = NULL;
3945 free(ondisk_path);
3946 return err;
3949 static const struct got_error *
3950 worktree_status(struct got_worktree *worktree, const char *path,
3951 struct got_fileindex *fileindex, struct got_repository *repo,
3952 got_worktree_status_cb status_cb, void *status_arg,
3953 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3954 int report_unchanged)
3956 const struct got_error *err = NULL;
3957 int fd = -1;
3958 struct got_fileindex_diff_dir_cb fdiff_cb;
3959 struct diff_dir_cb_arg arg;
3960 char *ondisk_path = NULL;
3961 struct got_pathlist_head ignores, missing_children;
3962 struct got_fileindex_entry *ie;
3964 TAILQ_INIT(&ignores);
3965 TAILQ_INIT(&missing_children);
3967 if (asprintf(&ondisk_path, "%s%s%s",
3968 worktree->root_path, path[0] ? "/" : "", path) == -1)
3969 return got_error_from_errno("asprintf");
3971 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3972 if (ie) {
3973 err = report_single_file_status(path, ondisk_path,
3974 fileindex, status_cb, status_arg, repo,
3975 report_unchanged, &ignores, no_ignores);
3976 goto done;
3977 } else {
3978 struct find_missing_children_args fmca;
3979 fmca.parent_path = path;
3980 fmca.parent_len = strlen(path);
3981 fmca.children = &missing_children;
3982 fmca.cancel_cb = cancel_cb;
3983 fmca.cancel_arg = cancel_arg;
3984 err = got_fileindex_for_each_entry_safe(fileindex,
3985 find_missing_children, &fmca);
3986 if (err)
3987 goto done;
3990 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3991 if (fd == -1) {
3992 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3993 !got_err_open_nofollow_on_symlink())
3994 err = got_error_from_errno2("open", ondisk_path);
3995 else {
3996 if (!no_ignores) {
3997 err = add_ignores_from_parent_paths(&ignores,
3998 worktree->root_path, ondisk_path);
3999 if (err)
4000 goto done;
4002 if (TAILQ_EMPTY(&missing_children)) {
4003 err = report_single_file_status(path,
4004 ondisk_path, fileindex,
4005 status_cb, status_arg, repo,
4006 report_unchanged, &ignores, no_ignores);
4007 if (err)
4008 goto done;
4009 } else {
4010 err = report_children(&missing_children,
4011 worktree, fileindex, repo,
4012 (path[0] == '\0'), report_unchanged,
4013 &ignores, no_ignores,
4014 status_cb, status_arg,
4015 cancel_cb, cancel_arg);
4016 if (err)
4017 goto done;
4020 } else {
4021 fdiff_cb.diff_old_new = status_old_new;
4022 fdiff_cb.diff_old = status_old;
4023 fdiff_cb.diff_new = status_new;
4024 fdiff_cb.diff_traverse = status_traverse;
4025 arg.fileindex = fileindex;
4026 arg.worktree = worktree;
4027 arg.status_path = path;
4028 arg.status_path_len = strlen(path);
4029 arg.repo = repo;
4030 arg.status_cb = status_cb;
4031 arg.status_arg = status_arg;
4032 arg.cancel_cb = cancel_cb;
4033 arg.cancel_arg = cancel_arg;
4034 arg.report_unchanged = report_unchanged;
4035 arg.no_ignores = no_ignores;
4036 if (!no_ignores) {
4037 err = add_ignores_from_parent_paths(&ignores,
4038 worktree->root_path, path);
4039 if (err)
4040 goto done;
4042 arg.ignores = &ignores;
4043 err = got_fileindex_diff_dir(fileindex, fd,
4044 worktree->root_path, path, repo, &fdiff_cb, &arg);
4046 done:
4047 free_ignores(&ignores);
4048 if (fd != -1 && close(fd) == -1 && err == NULL)
4049 err = got_error_from_errno("close");
4050 free(ondisk_path);
4051 return err;
4054 const struct got_error *
4055 got_worktree_status(struct got_worktree *worktree,
4056 struct got_pathlist_head *paths, struct got_repository *repo,
4057 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
4058 got_cancel_cb cancel_cb, void *cancel_arg)
4060 const struct got_error *err = NULL;
4061 char *fileindex_path = NULL;
4062 struct got_fileindex *fileindex = NULL;
4063 struct got_pathlist_entry *pe;
4065 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4066 if (err)
4067 return err;
4069 TAILQ_FOREACH(pe, paths, entry) {
4070 err = worktree_status(worktree, pe->path, fileindex, repo,
4071 status_cb, status_arg, cancel_cb, cancel_arg,
4072 no_ignores, 0);
4073 if (err)
4074 break;
4076 free(fileindex_path);
4077 got_fileindex_free(fileindex);
4078 return err;
4081 const struct got_error *
4082 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
4083 const char *arg)
4085 const struct got_error *err = NULL;
4086 char *resolved = NULL, *cwd = NULL, *path = NULL;
4087 size_t len;
4088 struct stat sb;
4089 char *abspath = NULL;
4090 char canonpath[PATH_MAX];
4092 *wt_path = NULL;
4094 cwd = getcwd(NULL, 0);
4095 if (cwd == NULL)
4096 return got_error_from_errno("getcwd");
4098 if (lstat(arg, &sb) == -1) {
4099 if (errno != ENOENT) {
4100 err = got_error_from_errno2("lstat", arg);
4101 goto done;
4103 sb.st_mode = 0;
4105 if (S_ISLNK(sb.st_mode)) {
4107 * We cannot use realpath(3) with symlinks since we want to
4108 * operate on the symlink itself.
4109 * But we can make the path absolute, assuming it is relative
4110 * to the current working directory, and then canonicalize it.
4112 if (!got_path_is_absolute(arg)) {
4113 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4114 err = got_error_from_errno("asprintf");
4115 goto done;
4119 err = got_canonpath(abspath ? abspath : arg, canonpath,
4120 sizeof(canonpath));
4121 if (err)
4122 goto done;
4123 resolved = strdup(canonpath);
4124 if (resolved == NULL) {
4125 err = got_error_from_errno("strdup");
4126 goto done;
4128 } else {
4129 resolved = realpath(arg, NULL);
4130 if (resolved == NULL) {
4131 if (errno != ENOENT) {
4132 err = got_error_from_errno2("realpath", arg);
4133 goto done;
4135 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4136 err = got_error_from_errno("asprintf");
4137 goto done;
4139 err = got_canonpath(abspath, canonpath,
4140 sizeof(canonpath));
4141 if (err)
4142 goto done;
4143 resolved = strdup(canonpath);
4144 if (resolved == NULL) {
4145 err = got_error_from_errno("strdup");
4146 goto done;
4151 if (strncmp(got_worktree_get_root_path(worktree), resolved,
4152 strlen(got_worktree_get_root_path(worktree)))) {
4153 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
4154 goto done;
4157 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
4158 err = got_path_skip_common_ancestor(&path,
4159 got_worktree_get_root_path(worktree), resolved);
4160 if (err)
4161 goto done;
4162 } else {
4163 path = strdup("");
4164 if (path == NULL) {
4165 err = got_error_from_errno("strdup");
4166 goto done;
4170 /* XXX status walk can't deal with trailing slash! */
4171 len = strlen(path);
4172 while (len > 0 && path[len - 1] == '/') {
4173 path[len - 1] = '\0';
4174 len--;
4176 done:
4177 free(abspath);
4178 free(resolved);
4179 free(cwd);
4180 if (err == NULL)
4181 *wt_path = path;
4182 else
4183 free(path);
4184 return err;
4187 struct schedule_addition_args {
4188 struct got_worktree *worktree;
4189 struct got_fileindex *fileindex;
4190 got_worktree_checkout_cb progress_cb;
4191 void *progress_arg;
4192 struct got_repository *repo;
4195 static int
4196 add_noop_status(unsigned char status)
4198 return (status == GOT_STATUS_ADD ||
4199 status == GOT_STATUS_MODIFY ||
4200 status == GOT_STATUS_CONFLICT ||
4201 status == GOT_STATUS_MODE_CHANGE ||
4202 status == GOT_STATUS_NO_CHANGE);
4205 static const struct got_error *
4206 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4207 const char *relpath, struct got_object_id *blob_id,
4208 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4209 int dirfd, const char *de_name)
4211 struct schedule_addition_args *a = arg;
4212 const struct got_error *err = NULL;
4213 struct got_fileindex_entry *ie;
4214 struct stat sb;
4215 char *ondisk_path;
4217 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4218 relpath) == -1)
4219 return got_error_from_errno("asprintf");
4221 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4222 if (ie) {
4223 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4224 de_name, a->repo);
4225 if (err)
4226 goto done;
4227 /* Re-adding an existing entry is a no-op. */
4228 if (staged_status == GOT_STATUS_NO_CHANGE &&
4229 add_noop_status(status))
4230 goto done;
4231 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4232 if (err)
4233 goto done;
4236 if (status != GOT_STATUS_UNVERSIONED) {
4237 if (status == GOT_STATUS_NONEXISTENT)
4238 err = got_error_set_errno(ENOENT, ondisk_path);
4239 else
4240 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4241 goto done;
4244 err = got_fileindex_entry_alloc(&ie, relpath);
4245 if (err)
4246 goto done;
4247 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4248 relpath, NULL, NULL, 1);
4249 if (err) {
4250 got_fileindex_entry_free(ie);
4251 goto done;
4253 err = got_fileindex_entry_add(a->fileindex, ie);
4254 if (err) {
4255 got_fileindex_entry_free(ie);
4256 goto done;
4258 done:
4259 free(ondisk_path);
4260 if (err)
4261 return err;
4262 if (staged_status == GOT_STATUS_NO_CHANGE && add_noop_status(status))
4263 return NULL;
4264 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4267 const struct got_error *
4268 got_worktree_schedule_add(struct got_worktree *worktree,
4269 struct got_pathlist_head *paths,
4270 got_worktree_checkout_cb progress_cb, void *progress_arg,
4271 struct got_repository *repo, int no_ignores)
4273 struct got_fileindex *fileindex = NULL;
4274 char *fileindex_path = NULL;
4275 const struct got_error *err = NULL, *sync_err, *unlockerr;
4276 struct got_pathlist_entry *pe;
4277 struct schedule_addition_args saa;
4279 err = lock_worktree(worktree, LOCK_EX);
4280 if (err)
4281 return err;
4283 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4284 if (err)
4285 goto done;
4287 saa.worktree = worktree;
4288 saa.fileindex = fileindex;
4289 saa.progress_cb = progress_cb;
4290 saa.progress_arg = progress_arg;
4291 saa.repo = repo;
4293 TAILQ_FOREACH(pe, paths, entry) {
4294 err = worktree_status(worktree, pe->path, fileindex, repo,
4295 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4296 if (err)
4297 break;
4299 sync_err = sync_fileindex(fileindex, fileindex_path);
4300 if (sync_err && err == NULL)
4301 err = sync_err;
4302 done:
4303 free(fileindex_path);
4304 if (fileindex)
4305 got_fileindex_free(fileindex);
4306 unlockerr = lock_worktree(worktree, LOCK_SH);
4307 if (unlockerr && err == NULL)
4308 err = unlockerr;
4309 return err;
4312 struct schedule_deletion_args {
4313 struct got_worktree *worktree;
4314 struct got_fileindex *fileindex;
4315 got_worktree_delete_cb progress_cb;
4316 void *progress_arg;
4317 struct got_repository *repo;
4318 int delete_local_mods;
4319 int keep_on_disk;
4320 int ignore_missing_paths;
4321 const char *status_path;
4322 size_t status_path_len;
4323 const char *status_codes;
4326 static const struct got_error *
4327 schedule_for_deletion(void *arg, unsigned char status,
4328 unsigned char staged_status, const char *relpath,
4329 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4330 struct got_object_id *commit_id, int dirfd, const char *de_name)
4332 struct schedule_deletion_args *a = arg;
4333 const struct got_error *err = NULL;
4334 struct got_fileindex_entry *ie = NULL;
4335 struct stat sb;
4336 char *ondisk_path;
4338 if (status == GOT_STATUS_NONEXISTENT) {
4339 if (a->ignore_missing_paths)
4340 return NULL;
4341 return got_error_set_errno(ENOENT, relpath);
4344 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4345 if (ie == NULL)
4346 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4348 staged_status = get_staged_status(ie);
4349 if (staged_status != GOT_STATUS_NO_CHANGE) {
4350 if (staged_status == GOT_STATUS_DELETE)
4351 return NULL;
4352 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4355 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4356 relpath) == -1)
4357 return got_error_from_errno("asprintf");
4359 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4360 a->repo);
4361 if (err)
4362 goto done;
4364 if (a->status_codes) {
4365 size_t ncodes = strlen(a->status_codes);
4366 int i;
4367 for (i = 0; i < ncodes ; i++) {
4368 if (status == a->status_codes[i])
4369 break;
4371 if (i == ncodes) {
4372 /* Do not delete files in non-matching status. */
4373 free(ondisk_path);
4374 return NULL;
4376 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4377 a->status_codes[i] != GOT_STATUS_MISSING) {
4378 static char msg[64];
4379 snprintf(msg, sizeof(msg),
4380 "invalid status code '%c'", a->status_codes[i]);
4381 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4382 goto done;
4386 if (status != GOT_STATUS_NO_CHANGE) {
4387 if (status == GOT_STATUS_DELETE)
4388 goto done;
4389 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4390 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4391 goto done;
4393 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4394 err = got_error_set_errno(ENOENT, relpath);
4395 goto done;
4397 if (status != GOT_STATUS_MODIFY &&
4398 status != GOT_STATUS_MISSING) {
4399 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4400 goto done;
4404 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4405 size_t root_len;
4407 if (dirfd != -1) {
4408 if (unlinkat(dirfd, de_name, 0) == -1) {
4409 err = got_error_from_errno2("unlinkat",
4410 ondisk_path);
4411 goto done;
4413 } else if (unlink(ondisk_path) == -1) {
4414 err = got_error_from_errno2("unlink", ondisk_path);
4415 goto done;
4418 root_len = strlen(a->worktree->root_path);
4419 do {
4420 char *parent;
4422 err = got_path_dirname(&parent, ondisk_path);
4423 if (err)
4424 goto done;
4425 free(ondisk_path);
4426 ondisk_path = parent;
4427 if (got_path_cmp(ondisk_path, a->status_path,
4428 strlen(ondisk_path), a->status_path_len) != 0 &&
4429 !got_path_is_child(ondisk_path, a->status_path,
4430 a->status_path_len))
4431 break;
4432 if (rmdir(ondisk_path) == -1) {
4433 if (errno != ENOTEMPTY)
4434 err = got_error_from_errno2("rmdir",
4435 ondisk_path);
4436 break;
4438 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4439 strlen(ondisk_path), root_len) != 0);
4442 got_fileindex_entry_mark_deleted_from_disk(ie);
4443 done:
4444 free(ondisk_path);
4445 if (err)
4446 return err;
4447 if (status == GOT_STATUS_DELETE)
4448 return NULL;
4449 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4450 staged_status, relpath);
4453 const struct got_error *
4454 got_worktree_schedule_delete(struct got_worktree *worktree,
4455 struct got_pathlist_head *paths, int delete_local_mods,
4456 const char *status_codes,
4457 got_worktree_delete_cb progress_cb, void *progress_arg,
4458 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4460 struct got_fileindex *fileindex = NULL;
4461 char *fileindex_path = NULL;
4462 const struct got_error *err = NULL, *sync_err, *unlockerr;
4463 struct got_pathlist_entry *pe;
4464 struct schedule_deletion_args sda;
4466 err = lock_worktree(worktree, LOCK_EX);
4467 if (err)
4468 return err;
4470 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4471 if (err)
4472 goto done;
4474 sda.worktree = worktree;
4475 sda.fileindex = fileindex;
4476 sda.progress_cb = progress_cb;
4477 sda.progress_arg = progress_arg;
4478 sda.repo = repo;
4479 sda.delete_local_mods = delete_local_mods;
4480 sda.keep_on_disk = keep_on_disk;
4481 sda.ignore_missing_paths = ignore_missing_paths;
4482 sda.status_codes = status_codes;
4484 TAILQ_FOREACH(pe, paths, entry) {
4485 char *ondisk_status_path;
4487 if (asprintf(&ondisk_status_path, "%s%s%s",
4488 got_worktree_get_root_path(worktree),
4489 pe->path[0] == '\0' ? "" : "/", pe->path) == -1) {
4490 err = got_error_from_errno("asprintf");
4491 goto done;
4493 sda.status_path = ondisk_status_path;
4494 sda.status_path_len = strlen(ondisk_status_path);
4495 err = worktree_status(worktree, pe->path, fileindex, repo,
4496 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4497 free(ondisk_status_path);
4498 if (err)
4499 break;
4501 sync_err = sync_fileindex(fileindex, fileindex_path);
4502 if (sync_err && err == NULL)
4503 err = sync_err;
4504 done:
4505 free(fileindex_path);
4506 if (fileindex)
4507 got_fileindex_free(fileindex);
4508 unlockerr = lock_worktree(worktree, LOCK_SH);
4509 if (unlockerr && err == NULL)
4510 err = unlockerr;
4511 return err;
4514 static const struct got_error *
4515 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4517 const struct got_error *err = NULL;
4518 char *line = NULL;
4519 size_t linesize = 0, n;
4520 ssize_t linelen;
4522 linelen = getline(&line, &linesize, infile);
4523 if (linelen == -1) {
4524 if (ferror(infile)) {
4525 err = got_error_from_errno("getline");
4526 goto done;
4528 return NULL;
4530 if (outfile) {
4531 n = fwrite(line, 1, linelen, outfile);
4532 if (n != linelen) {
4533 err = got_ferror(outfile, GOT_ERR_IO);
4534 goto done;
4537 if (rejectfile) {
4538 n = fwrite(line, 1, linelen, rejectfile);
4539 if (n != linelen)
4540 err = got_ferror(rejectfile, GOT_ERR_IO);
4542 done:
4543 free(line);
4544 return err;
4547 static const struct got_error *
4548 skip_one_line(FILE *f)
4550 char *line = NULL;
4551 size_t linesize = 0;
4552 ssize_t linelen;
4554 linelen = getline(&line, &linesize, f);
4555 if (linelen == -1) {
4556 if (ferror(f))
4557 return got_error_from_errno("getline");
4558 return NULL;
4560 free(line);
4561 return NULL;
4564 static const struct got_error *
4565 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4566 int start_old, int end_old, int start_new, int end_new,
4567 FILE *outfile, FILE *rejectfile)
4569 const struct got_error *err;
4571 /* Copy old file's lines leading up to patch. */
4572 while (!feof(f1) && *line_cur1 < start_old) {
4573 err = copy_one_line(f1, outfile, NULL);
4574 if (err)
4575 return err;
4576 (*line_cur1)++;
4578 /* Skip new file's lines leading up to patch. */
4579 while (!feof(f2) && *line_cur2 < start_new) {
4580 if (rejectfile)
4581 err = copy_one_line(f2, NULL, rejectfile);
4582 else
4583 err = skip_one_line(f2);
4584 if (err)
4585 return err;
4586 (*line_cur2)++;
4588 /* Copy patched lines. */
4589 while (!feof(f2) && *line_cur2 <= end_new) {
4590 err = copy_one_line(f2, outfile, NULL);
4591 if (err)
4592 return err;
4593 (*line_cur2)++;
4595 /* Skip over old file's replaced lines. */
4596 while (!feof(f1) && *line_cur1 <= end_old) {
4597 if (rejectfile)
4598 err = copy_one_line(f1, NULL, rejectfile);
4599 else
4600 err = skip_one_line(f1);
4601 if (err)
4602 return err;
4603 (*line_cur1)++;
4606 return NULL;
4609 static const struct got_error *
4610 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4611 FILE *outfile, FILE *rejectfile)
4613 const struct got_error *err;
4615 if (outfile) {
4616 /* Copy old file's lines until EOF. */
4617 while (!feof(f1)) {
4618 err = copy_one_line(f1, outfile, NULL);
4619 if (err)
4620 return err;
4621 (*line_cur1)++;
4624 if (rejectfile) {
4625 /* Copy new file's lines until EOF. */
4626 while (!feof(f2)) {
4627 err = copy_one_line(f2, NULL, rejectfile);
4628 if (err)
4629 return err;
4630 (*line_cur2)++;
4634 return NULL;
4637 static const struct got_error *
4638 apply_or_reject_change(int *choice, int *nchunks_used,
4639 struct diff_result *diff_result, int n,
4640 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4641 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4642 got_worktree_patch_cb patch_cb, void *patch_arg)
4644 const struct got_error *err = NULL;
4645 struct diff_chunk_context cc = {};
4646 int start_old, end_old, start_new, end_new;
4647 FILE *hunkfile;
4648 struct diff_output_unidiff_state *diff_state;
4649 struct diff_input_info diff_info;
4650 int rc;
4652 *choice = GOT_PATCH_CHOICE_NONE;
4654 /* Get changed line numbers without context lines for copy_change(). */
4655 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4656 start_old = cc.left.start;
4657 end_old = cc.left.end;
4658 start_new = cc.right.start;
4659 end_new = cc.right.end;
4661 /* Get the same change with context lines for display. */
4662 memset(&cc, 0, sizeof(cc));
4663 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4665 memset(&diff_info, 0, sizeof(diff_info));
4666 diff_info.left_path = relpath;
4667 diff_info.right_path = relpath;
4669 diff_state = diff_output_unidiff_state_alloc();
4670 if (diff_state == NULL)
4671 return got_error_set_errno(ENOMEM,
4672 "diff_output_unidiff_state_alloc");
4674 hunkfile = got_opentemp();
4675 if (hunkfile == NULL) {
4676 err = got_error_from_errno("got_opentemp");
4677 goto done;
4680 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4681 diff_result, &cc);
4682 if (rc != DIFF_RC_OK) {
4683 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4684 goto done;
4687 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4688 err = got_ferror(hunkfile, GOT_ERR_IO);
4689 goto done;
4692 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4693 hunkfile, changeno, nchanges);
4694 if (err)
4695 goto done;
4697 switch (*choice) {
4698 case GOT_PATCH_CHOICE_YES:
4699 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4700 end_old, start_new, end_new, outfile, rejectfile);
4701 break;
4702 case GOT_PATCH_CHOICE_NO:
4703 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4704 end_old, start_new, end_new, rejectfile, outfile);
4705 break;
4706 case GOT_PATCH_CHOICE_QUIT:
4707 break;
4708 default:
4709 err = got_error(GOT_ERR_PATCH_CHOICE);
4710 break;
4712 done:
4713 diff_output_unidiff_state_free(diff_state);
4714 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4715 err = got_error_from_errno("fclose");
4716 return err;
4719 struct revert_file_args {
4720 struct got_worktree *worktree;
4721 struct got_fileindex *fileindex;
4722 got_worktree_checkout_cb progress_cb;
4723 void *progress_arg;
4724 got_worktree_patch_cb patch_cb;
4725 void *patch_arg;
4726 struct got_repository *repo;
4727 int unlink_added_files;
4728 struct got_pathlist_head *added_files_to_unlink;
4731 static const struct got_error *
4732 create_patched_content(char **path_outfile, int reverse_patch,
4733 struct got_object_id *blob_id, const char *path2,
4734 int dirfd2, const char *de_name2,
4735 const char *relpath, struct got_repository *repo,
4736 got_worktree_patch_cb patch_cb, void *patch_arg)
4738 const struct got_error *err, *free_err;
4739 struct got_blob_object *blob = NULL;
4740 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4741 int fd = -1, fd2 = -1;
4742 char link_target[PATH_MAX];
4743 ssize_t link_len = 0;
4744 char *path1 = NULL, *id_str = NULL;
4745 struct stat sb2;
4746 struct got_diffreg_result *diffreg_result = NULL;
4747 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4748 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4750 *path_outfile = NULL;
4752 err = got_object_id_str(&id_str, blob_id);
4753 if (err)
4754 return err;
4756 if (dirfd2 != -1) {
4757 fd2 = openat(dirfd2, de_name2,
4758 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4759 if (fd2 == -1) {
4760 if (!got_err_open_nofollow_on_symlink()) {
4761 err = got_error_from_errno2("openat", path2);
4762 goto done;
4764 link_len = readlinkat(dirfd2, de_name2,
4765 link_target, sizeof(link_target));
4766 if (link_len == -1) {
4767 return got_error_from_errno2("readlinkat",
4768 path2);
4770 sb2.st_mode = S_IFLNK;
4771 sb2.st_size = link_len;
4773 } else {
4774 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4775 if (fd2 == -1) {
4776 if (!got_err_open_nofollow_on_symlink()) {
4777 err = got_error_from_errno2("open", path2);
4778 goto done;
4780 link_len = readlink(path2, link_target,
4781 sizeof(link_target));
4782 if (link_len == -1)
4783 return got_error_from_errno2("readlink", path2);
4784 sb2.st_mode = S_IFLNK;
4785 sb2.st_size = link_len;
4788 if (fd2 != -1) {
4789 if (fstat(fd2, &sb2) == -1) {
4790 err = got_error_from_errno2("fstat", path2);
4791 goto done;
4794 f2 = fdopen(fd2, "r");
4795 if (f2 == NULL) {
4796 err = got_error_from_errno2("fdopen", path2);
4797 goto done;
4799 fd2 = -1;
4800 } else {
4801 size_t n;
4802 f2 = got_opentemp();
4803 if (f2 == NULL) {
4804 err = got_error_from_errno2("got_opentemp", path2);
4805 goto done;
4807 n = fwrite(link_target, 1, link_len, f2);
4808 if (n != link_len) {
4809 err = got_ferror(f2, GOT_ERR_IO);
4810 goto done;
4812 if (fflush(f2) == EOF) {
4813 err = got_error_from_errno("fflush");
4814 goto done;
4816 rewind(f2);
4819 fd = got_opentempfd();
4820 if (fd == -1) {
4821 err = got_error_from_errno("got_opentempfd");
4822 goto done;
4825 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4826 if (err)
4827 goto done;
4829 err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4830 if (err)
4831 goto done;
4833 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4834 if (err)
4835 goto done;
4837 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4838 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4839 if (err)
4840 goto done;
4842 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4843 "");
4844 if (err)
4845 goto done;
4847 if (fseek(f1, 0L, SEEK_SET) == -1)
4848 return got_ferror(f1, GOT_ERR_IO);
4849 if (fseek(f2, 0L, SEEK_SET) == -1)
4850 return got_ferror(f2, GOT_ERR_IO);
4852 /* Count the number of actual changes in the diff result. */
4853 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4854 struct diff_chunk_context cc = {};
4855 diff_chunk_context_load_change(&cc, &nchunks_used,
4856 diffreg_result->result, n, 0);
4857 nchanges++;
4859 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4860 int choice;
4861 err = apply_or_reject_change(&choice, &nchunks_used,
4862 diffreg_result->result, n, relpath, f1, f2,
4863 &line_cur1, &line_cur2,
4864 reverse_patch ? NULL : outfile,
4865 reverse_patch ? outfile : NULL,
4866 ++i, nchanges, patch_cb, patch_arg);
4867 if (err)
4868 goto done;
4869 if (choice == GOT_PATCH_CHOICE_YES)
4870 have_content = 1;
4871 else if (choice == GOT_PATCH_CHOICE_QUIT)
4872 break;
4874 if (have_content) {
4875 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4876 reverse_patch ? NULL : outfile,
4877 reverse_patch ? outfile : NULL);
4878 if (err)
4879 goto done;
4881 if (!S_ISLNK(sb2.st_mode)) {
4882 mode_t mode;
4884 mode = apply_umask(sb2.st_mode);
4885 if (fchmod(fileno(outfile), mode) == -1) {
4886 err = got_error_from_errno2("fchmod", path2);
4887 goto done;
4891 done:
4892 free(id_str);
4893 if (fd != -1 && close(fd) == -1 && err == NULL)
4894 err = got_error_from_errno("close");
4895 if (blob)
4896 got_object_blob_close(blob);
4897 free_err = got_diffreg_result_free(diffreg_result);
4898 if (err == NULL)
4899 err = free_err;
4900 if (f1 && fclose(f1) == EOF && err == NULL)
4901 err = got_error_from_errno2("fclose", path1);
4902 if (f2 && fclose(f2) == EOF && err == NULL)
4903 err = got_error_from_errno2("fclose", path2);
4904 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4905 err = got_error_from_errno2("close", path2);
4906 if (outfile && fclose(outfile) == EOF && err == NULL)
4907 err = got_error_from_errno2("fclose", *path_outfile);
4908 if (path1 && unlink(path1) == -1 && err == NULL)
4909 err = got_error_from_errno2("unlink", path1);
4910 if (err || !have_content) {
4911 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4912 err = got_error_from_errno2("unlink", *path_outfile);
4913 free(*path_outfile);
4914 *path_outfile = NULL;
4916 free(path1);
4917 return err;
4920 static const struct got_error *
4921 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4922 const char *relpath, struct got_object_id *blob_id,
4923 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4924 int dirfd, const char *de_name)
4926 struct revert_file_args *a = arg;
4927 const struct got_error *err = NULL;
4928 char *parent_path = NULL;
4929 struct got_fileindex_entry *ie;
4930 struct got_commit_object *base_commit = NULL;
4931 struct got_tree_object *tree = NULL;
4932 struct got_object_id *tree_id = NULL;
4933 const struct got_tree_entry *te = NULL;
4934 char *tree_path = NULL, *te_name;
4935 char *ondisk_path = NULL, *path_content = NULL;
4936 struct got_blob_object *blob = NULL;
4937 int fd = -1;
4939 /* Reverting a staged deletion is a no-op. */
4940 if (status == GOT_STATUS_DELETE &&
4941 staged_status != GOT_STATUS_NO_CHANGE)
4942 return NULL;
4944 if (status == GOT_STATUS_UNVERSIONED)
4945 return (*a->progress_cb)(a->progress_arg,
4946 GOT_STATUS_UNVERSIONED, relpath);
4948 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4949 if (ie == NULL)
4950 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4952 /* Construct in-repository path of tree which contains this blob. */
4953 err = got_path_dirname(&parent_path, ie->path);
4954 if (err) {
4955 if (err->code != GOT_ERR_BAD_PATH)
4956 goto done;
4957 parent_path = strdup("/");
4958 if (parent_path == NULL) {
4959 err = got_error_from_errno("strdup");
4960 goto done;
4963 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4964 tree_path = strdup(parent_path);
4965 if (tree_path == NULL) {
4966 err = got_error_from_errno("strdup");
4967 goto done;
4969 } else {
4970 if (got_path_is_root_dir(parent_path)) {
4971 tree_path = strdup(a->worktree->path_prefix);
4972 if (tree_path == NULL) {
4973 err = got_error_from_errno("strdup");
4974 goto done;
4976 } else {
4977 if (asprintf(&tree_path, "%s/%s",
4978 a->worktree->path_prefix, parent_path) == -1) {
4979 err = got_error_from_errno("asprintf");
4980 goto done;
4985 err = got_object_open_as_commit(&base_commit, a->repo,
4986 a->worktree->base_commit_id);
4987 if (err)
4988 goto done;
4990 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4991 if (err) {
4992 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4993 (status == GOT_STATUS_ADD ||
4994 staged_status == GOT_STATUS_ADD)))
4995 goto done;
4996 } else {
4997 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4998 if (err)
4999 goto done;
5001 err = got_path_basename(&te_name, ie->path);
5002 if (err)
5003 goto done;
5005 te = got_object_tree_find_entry(tree, te_name);
5006 free(te_name);
5007 if (te == NULL && status != GOT_STATUS_ADD &&
5008 staged_status != GOT_STATUS_ADD) {
5009 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
5010 goto done;
5014 switch (status) {
5015 case GOT_STATUS_ADD:
5016 if (a->patch_cb) {
5017 int choice = GOT_PATCH_CHOICE_NONE;
5018 err = (*a->patch_cb)(&choice, a->patch_arg,
5019 status, ie->path, NULL, 1, 1);
5020 if (err)
5021 goto done;
5022 if (choice != GOT_PATCH_CHOICE_YES)
5023 break;
5025 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
5026 ie->path);
5027 if (err)
5028 goto done;
5029 got_fileindex_entry_remove(a->fileindex, ie);
5030 if (a->unlink_added_files) {
5031 int do_unlink = a->added_files_to_unlink ? 0 : 1;
5033 if (a->added_files_to_unlink) {
5034 struct got_pathlist_entry *pe;
5036 TAILQ_FOREACH(pe, a->added_files_to_unlink,
5037 entry) {
5038 if (got_path_cmp(pe->path, relpath,
5039 pe->path_len, strlen(relpath)))
5040 continue;
5041 do_unlink = 1;
5042 break;
5046 if (do_unlink) {
5047 if (asprintf(&ondisk_path, "%s/%s",
5048 got_worktree_get_root_path(a->worktree),
5049 relpath) == -1) {
5050 err = got_error_from_errno("asprintf");
5051 goto done;
5053 if (unlink(ondisk_path) == -1) {
5054 err = got_error_from_errno2("unlink",
5055 ondisk_path);
5056 break;
5060 break;
5061 case GOT_STATUS_DELETE:
5062 if (a->patch_cb) {
5063 int choice = GOT_PATCH_CHOICE_NONE;
5064 err = (*a->patch_cb)(&choice, a->patch_arg,
5065 status, ie->path, NULL, 1, 1);
5066 if (err)
5067 goto done;
5068 if (choice != GOT_PATCH_CHOICE_YES)
5069 break;
5071 /* fall through */
5072 case GOT_STATUS_MODIFY:
5073 case GOT_STATUS_MODE_CHANGE:
5074 case GOT_STATUS_CONFLICT:
5075 case GOT_STATUS_MISSING: {
5076 struct got_object_id id;
5077 if (staged_status == GOT_STATUS_ADD ||
5078 staged_status == GOT_STATUS_MODIFY)
5079 got_fileindex_entry_get_staged_blob_id(&id, ie);
5080 else
5081 got_fileindex_entry_get_blob_id(&id, ie);
5082 fd = got_opentempfd();
5083 if (fd == -1) {
5084 err = got_error_from_errno("got_opentempfd");
5085 goto done;
5088 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
5089 if (err)
5090 goto done;
5092 if (asprintf(&ondisk_path, "%s/%s",
5093 got_worktree_get_root_path(a->worktree), relpath) == -1) {
5094 err = got_error_from_errno("asprintf");
5095 goto done;
5098 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
5099 status == GOT_STATUS_CONFLICT)) {
5100 int is_bad_symlink = 0;
5101 err = create_patched_content(&path_content, 1, &id,
5102 ondisk_path, dirfd, de_name, ie->path, a->repo,
5103 a->patch_cb, a->patch_arg);
5104 if (err || path_content == NULL)
5105 break;
5106 if (te && S_ISLNK(te->mode)) {
5107 if (unlink(path_content) == -1) {
5108 err = got_error_from_errno2("unlink",
5109 path_content);
5110 break;
5112 err = install_symlink(&is_bad_symlink,
5113 a->worktree, ondisk_path, ie->path,
5114 blob, 0, 1, 0, 0, a->repo,
5115 a->progress_cb, a->progress_arg);
5116 } else {
5117 if (rename(path_content, ondisk_path) == -1) {
5118 err = got_error_from_errno3("rename",
5119 path_content, ondisk_path);
5120 goto done;
5123 } else {
5124 int is_bad_symlink = 0;
5125 if (te && S_ISLNK(te->mode)) {
5126 err = install_symlink(&is_bad_symlink,
5127 a->worktree, ondisk_path, ie->path,
5128 blob, 0, 1, 0, 0, a->repo,
5129 a->progress_cb, a->progress_arg);
5130 } else {
5131 err = install_blob(a->worktree, ondisk_path,
5132 ie->path,
5133 te ? te->mode : GOT_DEFAULT_FILE_MODE,
5134 got_fileindex_perms_to_st(ie), blob,
5135 0, 1, 0, 0, a->repo,
5136 a->progress_cb, a->progress_arg);
5138 if (err)
5139 goto done;
5140 if (status == GOT_STATUS_DELETE ||
5141 status == GOT_STATUS_MODE_CHANGE) {
5142 err = got_fileindex_entry_update(ie,
5143 a->worktree->root_fd, relpath,
5144 blob->id.sha1,
5145 a->worktree->base_commit_id->sha1, 1);
5146 if (err)
5147 goto done;
5149 if (is_bad_symlink) {
5150 got_fileindex_entry_filetype_set(ie,
5151 GOT_FILEIDX_MODE_BAD_SYMLINK);
5154 break;
5156 default:
5157 break;
5159 done:
5160 free(ondisk_path);
5161 free(path_content);
5162 free(parent_path);
5163 free(tree_path);
5164 if (fd != -1 && close(fd) == -1 && err == NULL)
5165 err = got_error_from_errno("close");
5166 if (blob)
5167 got_object_blob_close(blob);
5168 if (tree)
5169 got_object_tree_close(tree);
5170 free(tree_id);
5171 if (base_commit)
5172 got_object_commit_close(base_commit);
5173 return err;
5176 const struct got_error *
5177 got_worktree_revert(struct got_worktree *worktree,
5178 struct got_pathlist_head *paths,
5179 got_worktree_checkout_cb progress_cb, void *progress_arg,
5180 got_worktree_patch_cb patch_cb, void *patch_arg,
5181 struct got_repository *repo)
5183 struct got_fileindex *fileindex = NULL;
5184 char *fileindex_path = NULL;
5185 const struct got_error *err = NULL, *unlockerr = NULL;
5186 const struct got_error *sync_err = NULL;
5187 struct got_pathlist_entry *pe;
5188 struct revert_file_args rfa;
5190 err = lock_worktree(worktree, LOCK_EX);
5191 if (err)
5192 return err;
5194 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5195 if (err)
5196 goto done;
5198 rfa.worktree = worktree;
5199 rfa.fileindex = fileindex;
5200 rfa.progress_cb = progress_cb;
5201 rfa.progress_arg = progress_arg;
5202 rfa.patch_cb = patch_cb;
5203 rfa.patch_arg = patch_arg;
5204 rfa.repo = repo;
5205 rfa.unlink_added_files = 0;
5206 TAILQ_FOREACH(pe, paths, entry) {
5207 err = worktree_status(worktree, pe->path, fileindex, repo,
5208 revert_file, &rfa, NULL, NULL, 1, 0);
5209 if (err)
5210 break;
5212 sync_err = sync_fileindex(fileindex, fileindex_path);
5213 if (sync_err && err == NULL)
5214 err = sync_err;
5215 done:
5216 free(fileindex_path);
5217 if (fileindex)
5218 got_fileindex_free(fileindex);
5219 unlockerr = lock_worktree(worktree, LOCK_SH);
5220 if (unlockerr && err == NULL)
5221 err = unlockerr;
5222 return err;
5225 static void
5226 free_commitable(struct got_commitable *ct)
5228 free(ct->path);
5229 free(ct->in_repo_path);
5230 free(ct->ondisk_path);
5231 free(ct->blob_id);
5232 free(ct->base_blob_id);
5233 free(ct->staged_blob_id);
5234 free(ct->base_commit_id);
5235 free(ct);
5238 struct collect_commitables_arg {
5239 struct got_pathlist_head *commitable_paths;
5240 struct got_repository *repo;
5241 struct got_worktree *worktree;
5242 struct got_fileindex *fileindex;
5243 int have_staged_files;
5244 int allow_bad_symlinks;
5245 int diff_header_shown;
5246 int commit_conflicts;
5247 FILE *diff_outfile;
5248 FILE *f1;
5249 FILE *f2;
5253 * Create a file which contains the target path of a symlink so we can feed
5254 * it as content to the diff engine.
5256 static const struct got_error *
5257 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5258 const char *abspath)
5260 const struct got_error *err = NULL;
5261 char target_path[PATH_MAX];
5262 ssize_t target_len, outlen;
5264 *fd = -1;
5266 if (dirfd != -1) {
5267 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5268 if (target_len == -1)
5269 return got_error_from_errno2("readlinkat", abspath);
5270 } else {
5271 target_len = readlink(abspath, target_path, PATH_MAX);
5272 if (target_len == -1)
5273 return got_error_from_errno2("readlink", abspath);
5276 *fd = got_opentempfd();
5277 if (*fd == -1)
5278 return got_error_from_errno("got_opentempfd");
5280 outlen = write(*fd, target_path, target_len);
5281 if (outlen == -1) {
5282 err = got_error_from_errno("got_opentempfd");
5283 goto done;
5286 if (lseek(*fd, 0, SEEK_SET) == -1) {
5287 err = got_error_from_errno2("lseek", abspath);
5288 goto done;
5290 done:
5291 if (err) {
5292 close(*fd);
5293 *fd = -1;
5295 return err;
5298 static const struct got_error *
5299 append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5300 FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5301 int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5303 const struct got_error *err = NULL;
5304 struct got_blob_object *blob1 = NULL;
5305 int fd = -1, fd1 = -1, fd2 = -1;
5306 FILE *ondisk_file = NULL;
5307 char *label1 = NULL;
5308 struct stat sb;
5309 off_t size1 = 0;
5310 int f2_exists = 0;
5311 char *id_str = NULL;
5313 memset(&sb, 0, sizeof(sb));
5315 if (diff_staged) {
5316 if (ct->staged_status != GOT_STATUS_MODIFY &&
5317 ct->staged_status != GOT_STATUS_ADD &&
5318 ct->staged_status != GOT_STATUS_DELETE)
5319 return NULL;
5320 } else {
5321 if (ct->status != GOT_STATUS_MODIFY &&
5322 ct->status != GOT_STATUS_ADD &&
5323 ct->status != GOT_STATUS_DELETE &&
5324 ct->status != GOT_STATUS_CONFLICT)
5325 return NULL;
5328 err = got_opentemp_truncate(f1);
5329 if (err)
5330 return got_error_from_errno("got_opentemp_truncate");
5331 err = got_opentemp_truncate(f2);
5332 if (err)
5333 return got_error_from_errno("got_opentemp_truncate");
5335 if (!*diff_header_shown) {
5336 err = got_object_id_str(&id_str, worktree->base_commit_id);
5337 if (err)
5338 return err;
5339 fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5340 got_worktree_get_root_path(worktree));
5341 fprintf(diff_outfile, "commit - %s\n", id_str);
5342 fprintf(diff_outfile, "path + %s%s\n",
5343 got_worktree_get_root_path(worktree),
5344 diff_staged ? " (staged changes)" : "");
5345 *diff_header_shown = 1;
5348 if (diff_staged) {
5349 const char *label1 = NULL, *label2 = NULL;
5350 switch (ct->staged_status) {
5351 case GOT_STATUS_MODIFY:
5352 label1 = ct->path;
5353 label2 = ct->path;
5354 break;
5355 case GOT_STATUS_ADD:
5356 label2 = ct->path;
5357 break;
5358 case GOT_STATUS_DELETE:
5359 label1 = ct->path;
5360 break;
5361 default:
5362 return got_error(GOT_ERR_FILE_STATUS);
5364 fd1 = got_opentempfd();
5365 if (fd1 == -1) {
5366 err = got_error_from_errno("got_opentempfd");
5367 goto done;
5369 fd2 = got_opentempfd();
5370 if (fd2 == -1) {
5371 err = got_error_from_errno("got_opentempfd");
5372 goto done;
5374 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5375 fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5376 label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5377 NULL, repo, diff_outfile);
5378 goto done;
5381 fd1 = got_opentempfd();
5382 if (fd1 == -1) {
5383 err = got_error_from_errno("got_opentempfd");
5384 goto done;
5387 if (ct->status != GOT_STATUS_ADD) {
5388 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5389 8192, fd1);
5390 if (err)
5391 goto done;
5394 if (ct->status != GOT_STATUS_DELETE) {
5395 if (dirfd != -1) {
5396 fd = openat(dirfd, de_name,
5397 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5398 if (fd == -1) {
5399 if (!got_err_open_nofollow_on_symlink()) {
5400 err = got_error_from_errno2("openat",
5401 ct->ondisk_path);
5402 goto done;
5404 err = get_symlink_target_file(&fd, dirfd,
5405 de_name, ct->ondisk_path);
5406 if (err)
5407 goto done;
5409 } else {
5410 fd = open(ct->ondisk_path,
5411 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5412 if (fd == -1) {
5413 if (!got_err_open_nofollow_on_symlink()) {
5414 err = got_error_from_errno2("open",
5415 ct->ondisk_path);
5416 goto done;
5418 err = get_symlink_target_file(&fd, dirfd,
5419 de_name, ct->ondisk_path);
5420 if (err)
5421 goto done;
5424 if (fstatat(fd, ct->ondisk_path, &sb,
5425 AT_SYMLINK_NOFOLLOW) == -1) {
5426 err = got_error_from_errno2("fstatat", ct->ondisk_path);
5427 goto done;
5429 ondisk_file = fdopen(fd, "r");
5430 if (ondisk_file == NULL) {
5431 err = got_error_from_errno2("fdopen", ct->ondisk_path);
5432 goto done;
5434 fd = -1;
5435 f2_exists = 1;
5438 if (blob1) {
5439 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5440 f1, blob1);
5441 if (err)
5442 goto done;
5445 err = got_diff_blob_file(blob1, f1, size1, label1,
5446 ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5447 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5448 done:
5449 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5450 err = got_error_from_errno("close");
5451 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5452 err = got_error_from_errno("close");
5453 if (blob1)
5454 got_object_blob_close(blob1);
5455 if (fd != -1 && close(fd) == -1 && err == NULL)
5456 err = got_error_from_errno("close");
5457 if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5458 err = got_error_from_errno("fclose");
5459 return err;
5462 static const struct got_error *
5463 collect_commitables(void *arg, unsigned char status,
5464 unsigned char staged_status, const char *relpath,
5465 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5466 struct got_object_id *commit_id, int dirfd, const char *de_name)
5468 struct collect_commitables_arg *a = arg;
5469 const struct got_error *err = NULL;
5470 struct got_commitable *ct = NULL;
5471 struct got_pathlist_entry *new = NULL;
5472 char *parent_path = NULL, *path = NULL;
5473 struct stat sb;
5475 if (a->have_staged_files) {
5476 if (staged_status != GOT_STATUS_MODIFY &&
5477 staged_status != GOT_STATUS_ADD &&
5478 staged_status != GOT_STATUS_DELETE)
5479 return NULL;
5480 } else {
5481 if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5482 printf("C %s\n", relpath);
5483 return got_error(GOT_ERR_COMMIT_CONFLICT);
5486 if (status != GOT_STATUS_MODIFY &&
5487 status != GOT_STATUS_MODE_CHANGE &&
5488 status != GOT_STATUS_ADD &&
5489 status != GOT_STATUS_DELETE &&
5490 status != GOT_STATUS_CONFLICT)
5491 return NULL;
5494 if (asprintf(&path, "/%s", relpath) == -1) {
5495 err = got_error_from_errno("asprintf");
5496 goto done;
5498 if (strcmp(path, "/") == 0) {
5499 parent_path = strdup("");
5500 if (parent_path == NULL)
5501 return got_error_from_errno("strdup");
5502 } else {
5503 err = got_path_dirname(&parent_path, path);
5504 if (err)
5505 return err;
5508 ct = calloc(1, sizeof(*ct));
5509 if (ct == NULL) {
5510 err = got_error_from_errno("calloc");
5511 goto done;
5514 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5515 relpath) == -1) {
5516 err = got_error_from_errno("asprintf");
5517 goto done;
5520 if (staged_status == GOT_STATUS_ADD ||
5521 staged_status == GOT_STATUS_MODIFY) {
5522 struct got_fileindex_entry *ie;
5523 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5524 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5525 case GOT_FILEIDX_MODE_REGULAR_FILE:
5526 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5527 ct->mode = S_IFREG;
5528 break;
5529 case GOT_FILEIDX_MODE_SYMLINK:
5530 ct->mode = S_IFLNK;
5531 break;
5532 default:
5533 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5534 goto done;
5536 ct->mode |= got_fileindex_entry_perms_get(ie);
5537 } else if (status != GOT_STATUS_DELETE &&
5538 staged_status != GOT_STATUS_DELETE) {
5539 if (dirfd != -1) {
5540 if (fstatat(dirfd, de_name, &sb,
5541 AT_SYMLINK_NOFOLLOW) == -1) {
5542 err = got_error_from_errno2("fstatat",
5543 ct->ondisk_path);
5544 goto done;
5546 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5547 err = got_error_from_errno2("lstat", ct->ondisk_path);
5548 goto done;
5550 ct->mode = sb.st_mode;
5553 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5554 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5555 relpath) == -1) {
5556 err = got_error_from_errno("asprintf");
5557 goto done;
5560 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5561 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5562 int is_bad_symlink;
5563 char target_path[PATH_MAX];
5564 ssize_t target_len;
5565 target_len = readlink(ct->ondisk_path, target_path,
5566 sizeof(target_path));
5567 if (target_len == -1) {
5568 err = got_error_from_errno2("readlink",
5569 ct->ondisk_path);
5570 goto done;
5572 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5573 target_len, ct->ondisk_path, a->worktree->root_path);
5574 if (err)
5575 goto done;
5576 if (is_bad_symlink) {
5577 err = got_error_path(ct->ondisk_path,
5578 GOT_ERR_BAD_SYMLINK);
5579 goto done;
5584 ct->status = status;
5585 ct->staged_status = staged_status;
5586 ct->blob_id = NULL; /* will be filled in when blob gets created */
5587 if (ct->status != GOT_STATUS_ADD &&
5588 ct->staged_status != GOT_STATUS_ADD) {
5589 ct->base_blob_id = got_object_id_dup(blob_id);
5590 if (ct->base_blob_id == NULL) {
5591 err = got_error_from_errno("got_object_id_dup");
5592 goto done;
5594 ct->base_commit_id = got_object_id_dup(commit_id);
5595 if (ct->base_commit_id == NULL) {
5596 err = got_error_from_errno("got_object_id_dup");
5597 goto done;
5600 if (ct->staged_status == GOT_STATUS_ADD ||
5601 ct->staged_status == GOT_STATUS_MODIFY) {
5602 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5603 if (ct->staged_blob_id == NULL) {
5604 err = got_error_from_errno("got_object_id_dup");
5605 goto done;
5608 ct->path = strdup(path);
5609 if (ct->path == NULL) {
5610 err = got_error_from_errno("strdup");
5611 goto done;
5613 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5614 if (err)
5615 goto done;
5617 if (a->diff_outfile && ct && new != NULL) {
5618 err = append_ct_diff(ct, &a->diff_header_shown,
5619 a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5620 a->have_staged_files, a->repo, a->worktree);
5621 if (err)
5622 goto done;
5624 done:
5625 if (ct && (err || new == NULL))
5626 free_commitable(ct);
5627 free(parent_path);
5628 free(path);
5629 return err;
5632 static const struct got_error *write_tree(struct got_object_id **, int *,
5633 struct got_tree_object *, const char *, struct got_pathlist_head *,
5634 got_worktree_status_cb status_cb, void *status_arg,
5635 struct got_repository *);
5637 static const struct got_error *
5638 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5639 struct got_tree_entry *te, const char *parent_path,
5640 struct got_pathlist_head *commitable_paths,
5641 got_worktree_status_cb status_cb, void *status_arg,
5642 struct got_repository *repo)
5644 const struct got_error *err = NULL;
5645 struct got_tree_object *subtree;
5646 char *subpath;
5648 if (asprintf(&subpath, "%s%s%s", parent_path,
5649 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5650 return got_error_from_errno("asprintf");
5652 err = got_object_open_as_tree(&subtree, repo, &te->id);
5653 if (err)
5654 return err;
5656 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5657 commitable_paths, status_cb, status_arg, repo);
5658 got_object_tree_close(subtree);
5659 free(subpath);
5660 return err;
5663 static const struct got_error *
5664 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5666 const struct got_error *err = NULL;
5667 char *ct_parent_path = NULL;
5669 *match = 0;
5671 if (strchr(ct->in_repo_path, '/') == NULL) {
5672 *match = got_path_is_root_dir(path);
5673 return NULL;
5676 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5677 if (err)
5678 return err;
5679 *match = (strcmp(path, ct_parent_path) == 0);
5680 free(ct_parent_path);
5681 return err;
5684 static mode_t
5685 get_ct_file_mode(struct got_commitable *ct)
5687 if (S_ISLNK(ct->mode))
5688 return S_IFLNK;
5690 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5693 static const struct got_error *
5694 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5695 struct got_tree_entry *te, struct got_commitable *ct)
5697 const struct got_error *err = NULL;
5699 *new_te = NULL;
5701 err = got_object_tree_entry_dup(new_te, te);
5702 if (err)
5703 goto done;
5705 (*new_te)->mode = get_ct_file_mode(ct);
5707 if (ct->staged_status == GOT_STATUS_MODIFY)
5708 memcpy(&(*new_te)->id, ct->staged_blob_id,
5709 sizeof((*new_te)->id));
5710 else
5711 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5712 done:
5713 if (err && *new_te) {
5714 free(*new_te);
5715 *new_te = NULL;
5717 return err;
5720 static const struct got_error *
5721 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5722 struct got_commitable *ct)
5724 const struct got_error *err = NULL;
5725 char *ct_name = NULL;
5727 *new_te = NULL;
5729 *new_te = calloc(1, sizeof(**new_te));
5730 if (*new_te == NULL)
5731 return got_error_from_errno("calloc");
5733 err = got_path_basename(&ct_name, ct->path);
5734 if (err)
5735 goto done;
5736 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5737 sizeof((*new_te)->name)) {
5738 err = got_error(GOT_ERR_NO_SPACE);
5739 goto done;
5742 (*new_te)->mode = get_ct_file_mode(ct);
5744 if (ct->staged_status == GOT_STATUS_ADD)
5745 memcpy(&(*new_te)->id, ct->staged_blob_id,
5746 sizeof((*new_te)->id));
5747 else
5748 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5749 done:
5750 free(ct_name);
5751 if (err && *new_te) {
5752 free(*new_te);
5753 *new_te = NULL;
5755 return err;
5758 static const struct got_error *
5759 insert_tree_entry(struct got_tree_entry *new_te,
5760 struct got_pathlist_head *paths)
5762 const struct got_error *err = NULL;
5763 struct got_pathlist_entry *new_pe;
5765 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5766 if (err)
5767 return err;
5768 if (new_pe == NULL)
5769 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5770 return NULL;
5773 static const struct got_error *
5774 report_ct_status(struct got_commitable *ct,
5775 got_worktree_status_cb status_cb, void *status_arg)
5777 const char *ct_path = ct->path;
5778 unsigned char status;
5780 if (status_cb == NULL) /* no commit progress output desired */
5781 return NULL;
5783 while (ct_path[0] == '/')
5784 ct_path++;
5786 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5787 status = ct->staged_status;
5788 else
5789 status = ct->status;
5791 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5792 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5795 static const struct got_error *
5796 match_modified_subtree(int *modified, struct got_tree_entry *te,
5797 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5799 const struct got_error *err = NULL;
5800 struct got_pathlist_entry *pe;
5801 char *te_path;
5803 *modified = 0;
5805 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5806 got_path_is_root_dir(base_tree_path) ? "" : "/",
5807 te->name) == -1)
5808 return got_error_from_errno("asprintf");
5810 TAILQ_FOREACH(pe, commitable_paths, entry) {
5811 struct got_commitable *ct = pe->data;
5812 *modified = got_path_is_child(ct->in_repo_path, te_path,
5813 strlen(te_path));
5814 if (*modified)
5815 break;
5818 free(te_path);
5819 return err;
5822 static const struct got_error *
5823 match_deleted_or_modified_ct(struct got_commitable **ctp,
5824 struct got_tree_entry *te, const char *base_tree_path,
5825 struct got_pathlist_head *commitable_paths)
5827 const struct got_error *err = NULL;
5828 struct got_pathlist_entry *pe;
5830 *ctp = NULL;
5832 TAILQ_FOREACH(pe, commitable_paths, entry) {
5833 struct got_commitable *ct = pe->data;
5834 char *ct_name = NULL;
5835 int path_matches;
5837 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5838 if (ct->status != GOT_STATUS_MODIFY &&
5839 ct->status != GOT_STATUS_MODE_CHANGE &&
5840 ct->status != GOT_STATUS_DELETE &&
5841 ct->status != GOT_STATUS_CONFLICT)
5842 continue;
5843 } else {
5844 if (ct->staged_status != GOT_STATUS_MODIFY &&
5845 ct->staged_status != GOT_STATUS_DELETE)
5846 continue;
5849 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5850 continue;
5852 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5853 if (err)
5854 return err;
5855 if (!path_matches)
5856 continue;
5858 err = got_path_basename(&ct_name, pe->path);
5859 if (err)
5860 return err;
5862 if (strcmp(te->name, ct_name) != 0) {
5863 free(ct_name);
5864 continue;
5866 free(ct_name);
5868 *ctp = ct;
5869 break;
5872 return err;
5875 static const struct got_error *
5876 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5877 const char *child_path, const char *path_base_tree,
5878 struct got_pathlist_head *commitable_paths,
5879 got_worktree_status_cb status_cb, void *status_arg,
5880 struct got_repository *repo)
5882 const struct got_error *err = NULL;
5883 struct got_tree_entry *new_te;
5884 char *subtree_path;
5885 struct got_object_id *id = NULL;
5886 int nentries;
5888 *new_tep = NULL;
5890 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5891 got_path_is_root_dir(path_base_tree) ? "" : "/",
5892 child_path) == -1)
5893 return got_error_from_errno("asprintf");
5895 new_te = calloc(1, sizeof(*new_te));
5896 if (new_te == NULL)
5897 return got_error_from_errno("calloc");
5898 new_te->mode = S_IFDIR;
5900 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5901 sizeof(new_te->name)) {
5902 err = got_error(GOT_ERR_NO_SPACE);
5903 goto done;
5905 err = write_tree(&id, &nentries, NULL, subtree_path,
5906 commitable_paths, status_cb, status_arg, repo);
5907 if (err) {
5908 free(new_te);
5909 goto done;
5911 memcpy(&new_te->id, id, sizeof(new_te->id));
5912 done:
5913 free(id);
5914 free(subtree_path);
5915 if (err == NULL)
5916 *new_tep = new_te;
5917 return err;
5920 static const struct got_error *
5921 write_tree(struct got_object_id **new_tree_id, int *nentries,
5922 struct got_tree_object *base_tree, const char *path_base_tree,
5923 struct got_pathlist_head *commitable_paths,
5924 got_worktree_status_cb status_cb, void *status_arg,
5925 struct got_repository *repo)
5927 const struct got_error *err = NULL;
5928 struct got_pathlist_head paths;
5929 struct got_tree_entry *te, *new_te = NULL;
5930 struct got_pathlist_entry *pe;
5932 TAILQ_INIT(&paths);
5933 *nentries = 0;
5935 /* Insert, and recurse into, newly added entries first. */
5936 TAILQ_FOREACH(pe, commitable_paths, entry) {
5937 struct got_commitable *ct = pe->data;
5938 char *child_path = NULL, *slash;
5940 if ((ct->status != GOT_STATUS_ADD &&
5941 ct->staged_status != GOT_STATUS_ADD) ||
5942 (ct->flags & GOT_COMMITABLE_ADDED))
5943 continue;
5945 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5946 strlen(path_base_tree)))
5947 continue;
5949 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5950 ct->in_repo_path);
5951 if (err)
5952 goto done;
5954 slash = strchr(child_path, '/');
5955 if (slash == NULL) {
5956 err = alloc_added_blob_tree_entry(&new_te, ct);
5957 if (err)
5958 goto done;
5959 err = report_ct_status(ct, status_cb, status_arg);
5960 if (err)
5961 goto done;
5962 ct->flags |= GOT_COMMITABLE_ADDED;
5963 err = insert_tree_entry(new_te, &paths);
5964 if (err)
5965 goto done;
5966 (*nentries)++;
5967 } else {
5968 *slash = '\0'; /* trim trailing path components */
5969 if (base_tree == NULL ||
5970 got_object_tree_find_entry(base_tree, child_path)
5971 == NULL) {
5972 err = make_subtree_for_added_blob(&new_te,
5973 child_path, path_base_tree,
5974 commitable_paths, status_cb, status_arg,
5975 repo);
5976 if (err)
5977 goto done;
5978 err = insert_tree_entry(new_te, &paths);
5979 if (err)
5980 goto done;
5981 (*nentries)++;
5986 if (base_tree) {
5987 int i, nbase_entries;
5988 /* Handle modified and deleted entries. */
5989 nbase_entries = got_object_tree_get_nentries(base_tree);
5990 for (i = 0; i < nbase_entries; i++) {
5991 struct got_commitable *ct = NULL;
5993 te = got_object_tree_get_entry(base_tree, i);
5994 if (got_object_tree_entry_is_submodule(te)) {
5995 /* Entry is a submodule; just copy it. */
5996 err = got_object_tree_entry_dup(&new_te, te);
5997 if (err)
5998 goto done;
5999 err = insert_tree_entry(new_te, &paths);
6000 if (err)
6001 goto done;
6002 (*nentries)++;
6003 continue;
6006 if (S_ISDIR(te->mode)) {
6007 int modified;
6008 err = got_object_tree_entry_dup(&new_te, te);
6009 if (err)
6010 goto done;
6011 err = match_modified_subtree(&modified, te,
6012 path_base_tree, commitable_paths);
6013 if (err)
6014 goto done;
6015 /* Avoid recursion into unmodified subtrees. */
6016 if (modified) {
6017 struct got_object_id *new_id;
6018 int nsubentries;
6019 err = write_subtree(&new_id,
6020 &nsubentries, te,
6021 path_base_tree, commitable_paths,
6022 status_cb, status_arg, repo);
6023 if (err)
6024 goto done;
6025 if (nsubentries == 0) {
6026 /* All entries were deleted. */
6027 free(new_id);
6028 continue;
6030 memcpy(&new_te->id, new_id,
6031 sizeof(new_te->id));
6032 free(new_id);
6034 err = insert_tree_entry(new_te, &paths);
6035 if (err)
6036 goto done;
6037 (*nentries)++;
6038 continue;
6041 err = match_deleted_or_modified_ct(&ct, te,
6042 path_base_tree, commitable_paths);
6043 if (err)
6044 goto done;
6045 if (ct) {
6046 /* NB: Deleted entries get dropped here. */
6047 if (ct->status == GOT_STATUS_MODIFY ||
6048 ct->status == GOT_STATUS_MODE_CHANGE ||
6049 ct->status == GOT_STATUS_CONFLICT ||
6050 ct->staged_status == GOT_STATUS_MODIFY) {
6051 err = alloc_modified_blob_tree_entry(
6052 &new_te, te, ct);
6053 if (err)
6054 goto done;
6055 err = insert_tree_entry(new_te, &paths);
6056 if (err)
6057 goto done;
6058 (*nentries)++;
6060 err = report_ct_status(ct, status_cb,
6061 status_arg);
6062 if (err)
6063 goto done;
6064 } else {
6065 /* Entry is unchanged; just copy it. */
6066 err = got_object_tree_entry_dup(&new_te, te);
6067 if (err)
6068 goto done;
6069 err = insert_tree_entry(new_te, &paths);
6070 if (err)
6071 goto done;
6072 (*nentries)++;
6077 /* Write new list of entries; deleted entries have been dropped. */
6078 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
6079 done:
6080 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
6081 return err;
6084 static const struct got_error *
6085 update_fileindex_after_commit(struct got_worktree *worktree,
6086 struct got_pathlist_head *commitable_paths,
6087 struct got_object_id *new_base_commit_id,
6088 struct got_fileindex *fileindex, int have_staged_files)
6090 const struct got_error *err = NULL;
6091 struct got_pathlist_entry *pe;
6092 char *relpath = NULL;
6094 TAILQ_FOREACH(pe, commitable_paths, entry) {
6095 struct got_fileindex_entry *ie;
6096 struct got_commitable *ct = pe->data;
6098 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6100 err = got_path_skip_common_ancestor(&relpath,
6101 worktree->root_path, ct->ondisk_path);
6102 if (err)
6103 goto done;
6105 if (ie) {
6106 if (ct->status == GOT_STATUS_DELETE ||
6107 ct->staged_status == GOT_STATUS_DELETE) {
6108 got_fileindex_entry_remove(fileindex, ie);
6109 } else if (ct->staged_status == GOT_STATUS_ADD ||
6110 ct->staged_status == GOT_STATUS_MODIFY) {
6111 got_fileindex_entry_stage_set(ie,
6112 GOT_FILEIDX_STAGE_NONE);
6113 got_fileindex_entry_staged_filetype_set(ie, 0);
6115 err = got_fileindex_entry_update(ie,
6116 worktree->root_fd, relpath,
6117 ct->staged_blob_id->sha1,
6118 new_base_commit_id->sha1,
6119 !have_staged_files);
6120 } else
6121 err = got_fileindex_entry_update(ie,
6122 worktree->root_fd, relpath,
6123 ct->blob_id->sha1,
6124 new_base_commit_id->sha1,
6125 !have_staged_files);
6126 } else {
6127 err = got_fileindex_entry_alloc(&ie, pe->path);
6128 if (err)
6129 goto done;
6130 err = got_fileindex_entry_update(ie,
6131 worktree->root_fd, relpath, ct->blob_id->sha1,
6132 new_base_commit_id->sha1, 1);
6133 if (err) {
6134 got_fileindex_entry_free(ie);
6135 goto done;
6137 err = got_fileindex_entry_add(fileindex, ie);
6138 if (err) {
6139 got_fileindex_entry_free(ie);
6140 goto done;
6143 free(relpath);
6144 relpath = NULL;
6146 done:
6147 free(relpath);
6148 return err;
6152 static const struct got_error *
6153 check_out_of_date(const char *in_repo_path, unsigned char status,
6154 unsigned char staged_status, struct got_object_id *base_blob_id,
6155 struct got_object_id *base_commit_id,
6156 struct got_object_id *head_commit_id, struct got_repository *repo,
6157 int ood_errcode)
6159 const struct got_error *err = NULL;
6160 struct got_commit_object *commit = NULL;
6161 struct got_object_id *id = NULL;
6163 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
6164 /* Trivial case: base commit == head commit */
6165 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
6166 return NULL;
6168 * Ensure file content which local changes were based
6169 * on matches file content in the branch head.
6171 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6172 if (err)
6173 goto done;
6174 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6175 if (err) {
6176 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6177 err = got_error(ood_errcode);
6178 goto done;
6179 } else if (got_object_id_cmp(id, base_blob_id) != 0)
6180 err = got_error(ood_errcode);
6181 } else {
6182 /* Require that added files don't exist in the branch head. */
6183 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6184 if (err)
6185 goto done;
6186 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6187 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
6188 goto done;
6189 err = id ? got_error(ood_errcode) : NULL;
6191 done:
6192 free(id);
6193 if (commit)
6194 got_object_commit_close(commit);
6195 return err;
6198 static const struct got_error *
6199 commit_worktree(struct got_object_id **new_commit_id,
6200 struct got_pathlist_head *commitable_paths,
6201 struct got_object_id *head_commit_id,
6202 struct got_object_id *parent_id2,
6203 struct got_worktree *worktree,
6204 const char *author, const char *committer, char *diff_path,
6205 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6206 got_worktree_status_cb status_cb, void *status_arg,
6207 struct got_repository *repo)
6209 const struct got_error *err = NULL, *unlockerr = NULL;
6210 struct got_pathlist_entry *pe;
6211 const char *head_ref_name = NULL;
6212 struct got_commit_object *head_commit = NULL;
6213 struct got_reference *head_ref2 = NULL;
6214 struct got_object_id *head_commit_id2 = NULL;
6215 struct got_tree_object *head_tree = NULL;
6216 struct got_object_id *new_tree_id = NULL;
6217 int nentries, nparents = 0;
6218 struct got_object_id_queue parent_ids;
6219 struct got_object_qid *pid = NULL;
6220 char *logmsg = NULL;
6221 time_t timestamp;
6223 *new_commit_id = NULL;
6225 STAILQ_INIT(&parent_ids);
6227 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6228 if (err)
6229 goto done;
6231 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6232 if (err)
6233 goto done;
6235 if (commit_msg_cb != NULL) {
6236 err = commit_msg_cb(commitable_paths, diff_path,
6237 &logmsg, commit_arg);
6238 if (err)
6239 goto done;
6242 if (logmsg == NULL || strlen(logmsg) == 0) {
6243 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6244 goto done;
6247 /* Create blobs from added and modified files and record their IDs. */
6248 TAILQ_FOREACH(pe, commitable_paths, entry) {
6249 struct got_commitable *ct = pe->data;
6250 char *ondisk_path;
6252 /* Blobs for staged files already exist. */
6253 if (ct->staged_status == GOT_STATUS_ADD ||
6254 ct->staged_status == GOT_STATUS_MODIFY)
6255 continue;
6257 if (ct->status != GOT_STATUS_ADD &&
6258 ct->status != GOT_STATUS_MODIFY &&
6259 ct->status != GOT_STATUS_MODE_CHANGE &&
6260 ct->status != GOT_STATUS_CONFLICT)
6261 continue;
6263 if (asprintf(&ondisk_path, "%s/%s",
6264 worktree->root_path, pe->path) == -1) {
6265 err = got_error_from_errno("asprintf");
6266 goto done;
6268 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6269 free(ondisk_path);
6270 if (err)
6271 goto done;
6274 /* Recursively write new tree objects. */
6275 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6276 commitable_paths, status_cb, status_arg, repo);
6277 if (err)
6278 goto done;
6280 err = got_object_qid_alloc(&pid, head_commit_id);
6281 if (err)
6282 goto done;
6283 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6284 nparents++;
6285 if (parent_id2) {
6286 err = got_object_qid_alloc(&pid, parent_id2);
6287 if (err)
6288 goto done;
6289 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6290 nparents++;
6292 timestamp = time(NULL);
6293 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6294 nparents, author, timestamp, committer, timestamp, logmsg, repo);
6295 if (logmsg != NULL)
6296 free(logmsg);
6297 if (err)
6298 goto done;
6300 /* Check if a concurrent commit to our branch has occurred. */
6301 head_ref_name = got_worktree_get_head_ref_name(worktree);
6302 if (head_ref_name == NULL) {
6303 err = got_error_from_errno("got_worktree_get_head_ref_name");
6304 goto done;
6306 /* Lock the reference here to prevent concurrent modification. */
6307 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6308 if (err)
6309 goto done;
6310 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6311 if (err)
6312 goto done;
6313 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6314 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6315 goto done;
6317 /* Update branch head in repository. */
6318 err = got_ref_change_ref(head_ref2, *new_commit_id);
6319 if (err)
6320 goto done;
6321 err = got_ref_write(head_ref2, repo);
6322 if (err)
6323 goto done;
6325 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6326 if (err)
6327 goto done;
6329 err = ref_base_commit(worktree, repo);
6330 if (err)
6331 goto done;
6332 done:
6333 got_object_id_queue_free(&parent_ids);
6334 if (head_tree)
6335 got_object_tree_close(head_tree);
6336 if (head_commit)
6337 got_object_commit_close(head_commit);
6338 free(head_commit_id2);
6339 if (head_ref2) {
6340 unlockerr = got_ref_unlock(head_ref2);
6341 if (unlockerr && err == NULL)
6342 err = unlockerr;
6343 got_ref_close(head_ref2);
6345 return err;
6348 static const struct got_error *
6349 check_path_is_commitable(const char *path,
6350 struct got_pathlist_head *commitable_paths)
6352 struct got_pathlist_entry *cpe = NULL;
6353 size_t path_len = strlen(path);
6355 TAILQ_FOREACH(cpe, commitable_paths, entry) {
6356 struct got_commitable *ct = cpe->data;
6357 const char *ct_path = ct->path;
6359 while (ct_path[0] == '/')
6360 ct_path++;
6362 if (strcmp(path, ct_path) == 0 ||
6363 got_path_is_child(ct_path, path, path_len))
6364 break;
6367 if (cpe == NULL)
6368 return got_error_path(path, GOT_ERR_BAD_PATH);
6370 return NULL;
6373 static const struct got_error *
6374 check_staged_file(void *arg, struct got_fileindex_entry *ie)
6376 int *have_staged_files = arg;
6378 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6379 *have_staged_files = 1;
6380 return got_error(GOT_ERR_CANCELLED);
6383 return NULL;
6386 static const struct got_error *
6387 check_non_staged_files(struct got_fileindex *fileindex,
6388 struct got_pathlist_head *paths)
6390 struct got_pathlist_entry *pe;
6391 struct got_fileindex_entry *ie;
6393 TAILQ_FOREACH(pe, paths, entry) {
6394 if (pe->path[0] == '\0')
6395 continue;
6396 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6397 if (ie == NULL)
6398 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6399 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6400 return got_error_path(pe->path,
6401 GOT_ERR_FILE_NOT_STAGED);
6404 return NULL;
6407 const struct got_error *
6408 got_worktree_commit(struct got_object_id **new_commit_id,
6409 struct got_worktree *worktree, struct got_pathlist_head *paths,
6410 const char *author, const char *committer, int allow_bad_symlinks,
6411 int show_diff, int commit_conflicts,
6412 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6413 got_worktree_status_cb status_cb, void *status_arg,
6414 struct got_repository *repo)
6416 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6417 struct got_fileindex *fileindex = NULL;
6418 char *fileindex_path = NULL;
6419 struct got_pathlist_head commitable_paths;
6420 struct collect_commitables_arg cc_arg;
6421 struct got_pathlist_entry *pe;
6422 struct got_reference *head_ref = NULL;
6423 struct got_object_id *head_commit_id = NULL;
6424 char *diff_path = NULL;
6425 int have_staged_files = 0;
6427 *new_commit_id = NULL;
6429 memset(&cc_arg, 0, sizeof(cc_arg));
6430 TAILQ_INIT(&commitable_paths);
6432 err = lock_worktree(worktree, LOCK_EX);
6433 if (err)
6434 goto done;
6436 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6437 if (err)
6438 goto done;
6440 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6441 if (err)
6442 goto done;
6444 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6445 if (err)
6446 goto done;
6448 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6449 &have_staged_files);
6450 if (err && err->code != GOT_ERR_CANCELLED)
6451 goto done;
6452 if (have_staged_files) {
6453 err = check_non_staged_files(fileindex, paths);
6454 if (err)
6455 goto done;
6458 cc_arg.commitable_paths = &commitable_paths;
6459 cc_arg.worktree = worktree;
6460 cc_arg.fileindex = fileindex;
6461 cc_arg.repo = repo;
6462 cc_arg.have_staged_files = have_staged_files;
6463 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6464 cc_arg.diff_header_shown = 0;
6465 cc_arg.commit_conflicts = commit_conflicts;
6466 if (show_diff) {
6467 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6468 GOT_TMPDIR_STR "/got", ".diff");
6469 if (err)
6470 goto done;
6471 cc_arg.f1 = got_opentemp();
6472 if (cc_arg.f1 == NULL) {
6473 err = got_error_from_errno("got_opentemp");
6474 goto done;
6476 cc_arg.f2 = got_opentemp();
6477 if (cc_arg.f2 == NULL) {
6478 err = got_error_from_errno("got_opentemp");
6479 goto done;
6483 TAILQ_FOREACH(pe, paths, entry) {
6484 err = worktree_status(worktree, pe->path, fileindex, repo,
6485 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6486 if (err)
6487 goto done;
6490 if (show_diff) {
6491 if (fflush(cc_arg.diff_outfile) == EOF) {
6492 err = got_error_from_errno("fflush");
6493 goto done;
6497 if (TAILQ_EMPTY(&commitable_paths)) {
6498 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6499 goto done;
6502 TAILQ_FOREACH(pe, paths, entry) {
6503 err = check_path_is_commitable(pe->path, &commitable_paths);
6504 if (err)
6505 goto done;
6508 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6509 struct got_commitable *ct = pe->data;
6510 const char *ct_path = ct->in_repo_path;
6512 while (ct_path[0] == '/')
6513 ct_path++;
6514 err = check_out_of_date(ct_path, ct->status,
6515 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6516 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6517 if (err)
6518 goto done;
6522 err = commit_worktree(new_commit_id, &commitable_paths,
6523 head_commit_id, NULL, worktree, author, committer,
6524 (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6525 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6526 if (err)
6527 goto done;
6529 err = update_fileindex_after_commit(worktree, &commitable_paths,
6530 *new_commit_id, fileindex, have_staged_files);
6531 sync_err = sync_fileindex(fileindex, fileindex_path);
6532 if (sync_err && err == NULL)
6533 err = sync_err;
6534 done:
6535 if (fileindex)
6536 got_fileindex_free(fileindex);
6537 free(fileindex_path);
6538 unlockerr = lock_worktree(worktree, LOCK_SH);
6539 if (unlockerr && err == NULL)
6540 err = unlockerr;
6541 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6542 struct got_commitable *ct = pe->data;
6544 free_commitable(ct);
6546 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6547 if (diff_path && unlink(diff_path) == -1 && err == NULL)
6548 err = got_error_from_errno2("unlink", diff_path);
6549 free(diff_path);
6550 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6551 err == NULL)
6552 err = got_error_from_errno("fclose");
6553 return err;
6556 const char *
6557 got_commitable_get_path(struct got_commitable *ct)
6559 return ct->path;
6562 unsigned int
6563 got_commitable_get_status(struct got_commitable *ct)
6565 return ct->status;
6568 struct check_rebase_ok_arg {
6569 struct got_worktree *worktree;
6570 struct got_repository *repo;
6573 static const struct got_error *
6574 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6576 const struct got_error *err = NULL;
6577 struct check_rebase_ok_arg *a = arg;
6578 unsigned char status;
6579 struct stat sb;
6580 char *ondisk_path;
6582 /* Reject rebase of a work tree with mixed base commits. */
6583 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6584 SHA1_DIGEST_LENGTH))
6585 return got_error(GOT_ERR_MIXED_COMMITS);
6587 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6588 == -1)
6589 return got_error_from_errno("asprintf");
6591 /* Reject rebase of a work tree with modified or staged files. */
6592 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6593 free(ondisk_path);
6594 if (err)
6595 return err;
6597 if (status != GOT_STATUS_NO_CHANGE)
6598 return got_error(GOT_ERR_MODIFIED);
6599 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6600 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6602 return NULL;
6605 const struct got_error *
6606 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6607 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6608 struct got_worktree *worktree, struct got_reference *branch,
6609 struct got_repository *repo)
6611 const struct got_error *err = NULL;
6612 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6613 char *branch_ref_name = NULL;
6614 char *fileindex_path = NULL;
6615 struct check_rebase_ok_arg ok_arg;
6616 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6617 struct got_object_id *wt_branch_tip = NULL;
6619 *new_base_branch_ref = NULL;
6620 *tmp_branch = NULL;
6621 *fileindex = NULL;
6623 err = lock_worktree(worktree, LOCK_EX);
6624 if (err)
6625 return err;
6627 err = open_fileindex(fileindex, &fileindex_path, worktree);
6628 if (err)
6629 goto done;
6631 ok_arg.worktree = worktree;
6632 ok_arg.repo = repo;
6633 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6634 &ok_arg);
6635 if (err)
6636 goto done;
6638 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6639 if (err)
6640 goto done;
6642 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6643 if (err)
6644 goto done;
6646 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6647 if (err)
6648 goto done;
6650 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6651 0);
6652 if (err)
6653 goto done;
6655 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6656 if (err)
6657 goto done;
6658 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6659 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6660 goto done;
6663 err = got_ref_alloc_symref(new_base_branch_ref,
6664 new_base_branch_ref_name, wt_branch);
6665 if (err)
6666 goto done;
6667 err = got_ref_write(*new_base_branch_ref, repo);
6668 if (err)
6669 goto done;
6671 /* TODO Lock original branch's ref while rebasing? */
6673 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6674 if (err)
6675 goto done;
6677 err = got_ref_write(branch_ref, repo);
6678 if (err)
6679 goto done;
6681 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6682 worktree->base_commit_id);
6683 if (err)
6684 goto done;
6685 err = got_ref_write(*tmp_branch, repo);
6686 if (err)
6687 goto done;
6689 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6690 if (err)
6691 goto done;
6692 done:
6693 free(fileindex_path);
6694 free(tmp_branch_name);
6695 free(new_base_branch_ref_name);
6696 free(branch_ref_name);
6697 if (branch_ref)
6698 got_ref_close(branch_ref);
6699 if (wt_branch)
6700 got_ref_close(wt_branch);
6701 free(wt_branch_tip);
6702 if (err) {
6703 if (*new_base_branch_ref) {
6704 got_ref_close(*new_base_branch_ref);
6705 *new_base_branch_ref = NULL;
6707 if (*tmp_branch) {
6708 got_ref_close(*tmp_branch);
6709 *tmp_branch = NULL;
6711 if (*fileindex) {
6712 got_fileindex_free(*fileindex);
6713 *fileindex = NULL;
6715 lock_worktree(worktree, LOCK_SH);
6717 return err;
6720 const struct got_error *
6721 got_worktree_rebase_continue(struct got_object_id **commit_id,
6722 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6723 struct got_reference **branch, struct got_fileindex **fileindex,
6724 struct got_worktree *worktree, struct got_repository *repo)
6726 const struct got_error *err;
6727 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6728 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6729 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6730 char *fileindex_path = NULL;
6731 int have_staged_files = 0;
6733 *commit_id = NULL;
6734 *new_base_branch = NULL;
6735 *tmp_branch = NULL;
6736 *branch = NULL;
6737 *fileindex = NULL;
6739 err = lock_worktree(worktree, LOCK_EX);
6740 if (err)
6741 return err;
6743 err = open_fileindex(fileindex, &fileindex_path, worktree);
6744 if (err)
6745 goto done;
6747 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6748 &have_staged_files);
6749 if (err && err->code != GOT_ERR_CANCELLED)
6750 goto done;
6751 if (have_staged_files) {
6752 err = got_error(GOT_ERR_STAGED_PATHS);
6753 goto done;
6756 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6757 if (err)
6758 goto done;
6760 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6761 if (err)
6762 goto done;
6764 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6765 if (err)
6766 goto done;
6768 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6769 if (err)
6770 goto done;
6772 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6773 if (err)
6774 goto done;
6776 err = got_ref_open(branch, repo,
6777 got_ref_get_symref_target(branch_ref), 0);
6778 if (err)
6779 goto done;
6781 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6782 if (err)
6783 goto done;
6785 err = got_ref_resolve(commit_id, repo, commit_ref);
6786 if (err)
6787 goto done;
6789 err = got_ref_open(new_base_branch, repo,
6790 new_base_branch_ref_name, 0);
6791 if (err)
6792 goto done;
6794 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6795 if (err)
6796 goto done;
6797 done:
6798 free(commit_ref_name);
6799 free(branch_ref_name);
6800 free(fileindex_path);
6801 if (commit_ref)
6802 got_ref_close(commit_ref);
6803 if (branch_ref)
6804 got_ref_close(branch_ref);
6805 if (err) {
6806 free(*commit_id);
6807 *commit_id = NULL;
6808 if (*tmp_branch) {
6809 got_ref_close(*tmp_branch);
6810 *tmp_branch = NULL;
6812 if (*new_base_branch) {
6813 got_ref_close(*new_base_branch);
6814 *new_base_branch = NULL;
6816 if (*branch) {
6817 got_ref_close(*branch);
6818 *branch = NULL;
6820 if (*fileindex) {
6821 got_fileindex_free(*fileindex);
6822 *fileindex = NULL;
6824 lock_worktree(worktree, LOCK_SH);
6826 return err;
6829 const struct got_error *
6830 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6832 const struct got_error *err;
6833 char *tmp_branch_name = NULL;
6835 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6836 if (err)
6837 return err;
6839 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6840 free(tmp_branch_name);
6841 return NULL;
6844 static const struct got_error *
6845 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6846 const char *diff_path, char **logmsg, void *arg)
6848 *logmsg = arg;
6849 return NULL;
6852 static const struct got_error *
6853 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6854 const char *path, struct got_object_id *blob_id,
6855 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6856 int dirfd, const char *de_name)
6858 return NULL;
6861 struct collect_merged_paths_arg {
6862 got_worktree_checkout_cb progress_cb;
6863 void *progress_arg;
6864 struct got_pathlist_head *merged_paths;
6867 static const struct got_error *
6868 collect_merged_paths(void *arg, unsigned char status, const char *path)
6870 const struct got_error *err;
6871 struct collect_merged_paths_arg *a = arg;
6872 char *p;
6873 struct got_pathlist_entry *new;
6875 err = (*a->progress_cb)(a->progress_arg, status, path);
6876 if (err)
6877 return err;
6879 if (status != GOT_STATUS_MERGE &&
6880 status != GOT_STATUS_ADD &&
6881 status != GOT_STATUS_DELETE &&
6882 status != GOT_STATUS_CONFLICT)
6883 return NULL;
6885 p = strdup(path);
6886 if (p == NULL)
6887 return got_error_from_errno("strdup");
6889 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6890 if (err || new == NULL)
6891 free(p);
6892 return err;
6895 static const struct got_error *
6896 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6897 int is_rebase, struct got_repository *repo)
6899 const struct got_error *err;
6900 struct got_reference *commit_ref = NULL;
6902 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6903 if (err) {
6904 if (err->code != GOT_ERR_NOT_REF)
6905 goto done;
6906 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6907 if (err)
6908 goto done;
6909 err = got_ref_write(commit_ref, repo);
6910 if (err)
6911 goto done;
6912 } else if (is_rebase) {
6913 struct got_object_id *stored_id;
6914 int cmp;
6916 err = got_ref_resolve(&stored_id, repo, commit_ref);
6917 if (err)
6918 goto done;
6919 cmp = got_object_id_cmp(commit_id, stored_id);
6920 free(stored_id);
6921 if (cmp != 0) {
6922 err = got_error(GOT_ERR_REBASE_COMMITID);
6923 goto done;
6926 done:
6927 if (commit_ref)
6928 got_ref_close(commit_ref);
6929 return err;
6932 static const struct got_error *
6933 rebase_merge_files(struct got_pathlist_head *merged_paths,
6934 const char *commit_ref_name, struct got_worktree *worktree,
6935 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6936 struct got_object_id *commit_id, struct got_repository *repo,
6937 got_worktree_checkout_cb progress_cb, void *progress_arg,
6938 got_cancel_cb cancel_cb, void *cancel_arg)
6940 const struct got_error *err;
6941 struct got_reference *commit_ref = NULL;
6942 struct collect_merged_paths_arg cmp_arg;
6943 char *fileindex_path;
6945 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6947 err = get_fileindex_path(&fileindex_path, worktree);
6948 if (err)
6949 return err;
6951 cmp_arg.progress_cb = progress_cb;
6952 cmp_arg.progress_arg = progress_arg;
6953 cmp_arg.merged_paths = merged_paths;
6954 err = merge_files(worktree, fileindex, fileindex_path,
6955 parent_commit_id, commit_id, repo, collect_merged_paths,
6956 &cmp_arg, cancel_cb, cancel_arg);
6957 if (commit_ref)
6958 got_ref_close(commit_ref);
6959 return err;
6962 const struct got_error *
6963 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6964 struct got_worktree *worktree, struct got_fileindex *fileindex,
6965 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6966 struct got_repository *repo,
6967 got_worktree_checkout_cb progress_cb, void *progress_arg,
6968 got_cancel_cb cancel_cb, void *cancel_arg)
6970 const struct got_error *err;
6971 char *commit_ref_name;
6973 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6974 if (err)
6975 return err;
6977 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6978 if (err)
6979 goto done;
6981 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6982 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6983 progress_arg, cancel_cb, cancel_arg);
6984 done:
6985 free(commit_ref_name);
6986 return err;
6989 const struct got_error *
6990 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6991 struct got_worktree *worktree, struct got_fileindex *fileindex,
6992 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6993 struct got_repository *repo,
6994 got_worktree_checkout_cb progress_cb, void *progress_arg,
6995 got_cancel_cb cancel_cb, void *cancel_arg)
6997 const struct got_error *err;
6998 char *commit_ref_name;
7000 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7001 if (err)
7002 return err;
7004 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7005 if (err)
7006 goto done;
7008 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7009 fileindex, parent_commit_id, commit_id, repo, progress_cb,
7010 progress_arg, cancel_cb, cancel_arg);
7011 done:
7012 free(commit_ref_name);
7013 return err;
7016 static const struct got_error *
7017 rebase_commit(struct got_object_id **new_commit_id,
7018 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
7019 struct got_worktree *worktree, struct got_fileindex *fileindex,
7020 struct got_reference *tmp_branch, const char *committer,
7021 struct got_commit_object *orig_commit, const char *new_logmsg,
7022 int allow_conflict, struct got_repository *repo)
7024 const struct got_error *err, *sync_err;
7025 struct got_pathlist_head commitable_paths;
7026 struct collect_commitables_arg cc_arg;
7027 char *fileindex_path = NULL;
7028 struct got_reference *head_ref = NULL;
7029 struct got_object_id *head_commit_id = NULL;
7030 char *logmsg = NULL;
7032 memset(&cc_arg, 0, sizeof(cc_arg));
7033 TAILQ_INIT(&commitable_paths);
7034 *new_commit_id = NULL;
7036 /* Work tree is locked/unlocked during rebase preparation/teardown. */
7038 err = get_fileindex_path(&fileindex_path, worktree);
7039 if (err)
7040 return err;
7042 cc_arg.commitable_paths = &commitable_paths;
7043 cc_arg.worktree = worktree;
7044 cc_arg.repo = repo;
7045 cc_arg.have_staged_files = 0;
7046 cc_arg.commit_conflicts = allow_conflict;
7048 * If possible get the status of individual files directly to
7049 * avoid crawling the entire work tree once per rebased commit.
7051 * Ideally, merged_paths would contain a list of commitables
7052 * we could use so we could skip worktree_status() entirely.
7053 * However, we would then need carefully keep track of cumulative
7054 * effects of operations such as file additions and deletions
7055 * in 'got histedit -f' (folding multiple commits into one),
7056 * and this extra complexity is not really worth it.
7058 if (merged_paths) {
7059 struct got_pathlist_entry *pe;
7060 TAILQ_FOREACH(pe, merged_paths, entry) {
7061 err = worktree_status(worktree, pe->path, fileindex,
7062 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
7063 0);
7064 if (err)
7065 goto done;
7067 } else {
7068 err = worktree_status(worktree, "", fileindex, repo,
7069 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7070 if (err)
7071 goto done;
7074 if (TAILQ_EMPTY(&commitable_paths)) {
7075 /* No-op change; commit will be elided. */
7076 err = got_ref_delete(commit_ref, repo);
7077 if (err)
7078 goto done;
7079 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
7080 goto done;
7083 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7084 if (err)
7085 goto done;
7087 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7088 if (err)
7089 goto done;
7091 if (new_logmsg) {
7092 logmsg = strdup(new_logmsg);
7093 if (logmsg == NULL) {
7094 err = got_error_from_errno("strdup");
7095 goto done;
7097 } else {
7098 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
7099 if (err)
7100 goto done;
7103 /* NB: commit_worktree will call free(logmsg) */
7104 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
7105 NULL, worktree, got_object_commit_get_author(orig_commit),
7106 committer ? committer :
7107 got_object_commit_get_committer(orig_commit), NULL,
7108 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
7109 if (err)
7110 goto done;
7112 err = got_ref_change_ref(tmp_branch, *new_commit_id);
7113 if (err)
7114 goto done;
7116 err = got_ref_delete(commit_ref, repo);
7117 if (err)
7118 goto done;
7120 err = update_fileindex_after_commit(worktree, &commitable_paths,
7121 *new_commit_id, fileindex, 0);
7122 sync_err = sync_fileindex(fileindex, fileindex_path);
7123 if (sync_err && err == NULL)
7124 err = sync_err;
7125 done:
7126 free(fileindex_path);
7127 free(head_commit_id);
7128 if (head_ref)
7129 got_ref_close(head_ref);
7130 if (err) {
7131 free(*new_commit_id);
7132 *new_commit_id = NULL;
7134 return err;
7137 const struct got_error *
7138 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
7139 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7140 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7141 const char *committer, struct got_commit_object *orig_commit,
7142 struct got_object_id *orig_commit_id, int allow_conflict,
7143 struct got_repository *repo)
7145 const struct got_error *err;
7146 char *commit_ref_name;
7147 struct got_reference *commit_ref = NULL;
7148 struct got_object_id *commit_id = NULL;
7150 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7151 if (err)
7152 return err;
7154 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7155 if (err)
7156 goto done;
7157 err = got_ref_resolve(&commit_id, repo, commit_ref);
7158 if (err)
7159 goto done;
7160 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
7161 err = got_error(GOT_ERR_REBASE_COMMITID);
7162 goto done;
7165 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7166 worktree, fileindex, tmp_branch, committer, orig_commit,
7167 NULL, allow_conflict, repo);
7168 done:
7169 if (commit_ref)
7170 got_ref_close(commit_ref);
7171 free(commit_ref_name);
7172 free(commit_id);
7173 return err;
7176 const struct got_error *
7177 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
7178 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7179 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7180 const char *committer, struct got_commit_object *orig_commit,
7181 struct got_object_id *orig_commit_id, const char *new_logmsg,
7182 int allow_conflict, struct got_repository *repo)
7184 const struct got_error *err;
7185 char *commit_ref_name;
7186 struct got_reference *commit_ref = NULL;
7188 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7189 if (err)
7190 return err;
7192 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7193 if (err)
7194 goto done;
7196 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7197 worktree, fileindex, tmp_branch, committer, orig_commit,
7198 new_logmsg, allow_conflict, repo);
7199 done:
7200 if (commit_ref)
7201 got_ref_close(commit_ref);
7202 free(commit_ref_name);
7203 return err;
7206 const struct got_error *
7207 got_worktree_rebase_postpone(struct got_worktree *worktree,
7208 struct got_fileindex *fileindex)
7210 if (fileindex)
7211 got_fileindex_free(fileindex);
7212 return lock_worktree(worktree, LOCK_SH);
7215 static const struct got_error *
7216 delete_ref(const char *name, struct got_repository *repo)
7218 const struct got_error *err;
7219 struct got_reference *ref;
7221 err = got_ref_open(&ref, repo, name, 0);
7222 if (err) {
7223 if (err->code == GOT_ERR_NOT_REF)
7224 return NULL;
7225 return err;
7228 err = got_ref_delete(ref, repo);
7229 got_ref_close(ref);
7230 return err;
7233 static const struct got_error *
7234 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7236 const struct got_error *err;
7237 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7238 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7240 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7241 if (err)
7242 goto done;
7243 err = delete_ref(tmp_branch_name, repo);
7244 if (err)
7245 goto done;
7247 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7248 if (err)
7249 goto done;
7250 err = delete_ref(new_base_branch_ref_name, repo);
7251 if (err)
7252 goto done;
7254 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7255 if (err)
7256 goto done;
7257 err = delete_ref(branch_ref_name, repo);
7258 if (err)
7259 goto done;
7261 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7262 if (err)
7263 goto done;
7264 err = delete_ref(commit_ref_name, repo);
7265 if (err)
7266 goto done;
7268 done:
7269 free(tmp_branch_name);
7270 free(new_base_branch_ref_name);
7271 free(branch_ref_name);
7272 free(commit_ref_name);
7273 return err;
7276 static const struct got_error *
7277 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7278 struct got_object_id *new_commit_id, struct got_repository *repo)
7280 const struct got_error *err;
7281 struct got_reference *ref = NULL;
7282 struct got_object_id *old_commit_id = NULL;
7283 const char *branch_name = NULL;
7284 char *new_id_str = NULL;
7285 char *refname = NULL;
7287 branch_name = got_ref_get_name(branch);
7288 if (strncmp(branch_name, "refs/heads/", 11) != 0)
7289 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7290 branch_name += 11;
7292 err = got_object_id_str(&new_id_str, new_commit_id);
7293 if (err)
7294 return err;
7296 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7297 new_id_str) == -1) {
7298 err = got_error_from_errno("asprintf");
7299 goto done;
7302 err = got_ref_resolve(&old_commit_id, repo, branch);
7303 if (err)
7304 goto done;
7306 err = got_ref_alloc(&ref, refname, old_commit_id);
7307 if (err)
7308 goto done;
7310 err = got_ref_write(ref, repo);
7311 done:
7312 free(new_id_str);
7313 free(refname);
7314 free(old_commit_id);
7315 if (ref)
7316 got_ref_close(ref);
7317 return err;
7320 const struct got_error *
7321 got_worktree_rebase_complete(struct got_worktree *worktree,
7322 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7323 struct got_reference *rebased_branch, struct got_repository *repo,
7324 int create_backup)
7326 const struct got_error *err, *unlockerr, *sync_err;
7327 struct got_object_id *new_head_commit_id = NULL;
7328 char *fileindex_path = NULL;
7330 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7331 if (err)
7332 return err;
7334 if (create_backup) {
7335 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7336 rebased_branch, new_head_commit_id, repo);
7337 if (err)
7338 goto done;
7341 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7342 if (err)
7343 goto done;
7345 err = got_ref_write(rebased_branch, repo);
7346 if (err)
7347 goto done;
7349 err = got_worktree_set_head_ref(worktree, rebased_branch);
7350 if (err)
7351 goto done;
7353 err = delete_rebase_refs(worktree, repo);
7354 if (err)
7355 goto done;
7357 err = get_fileindex_path(&fileindex_path, worktree);
7358 if (err)
7359 goto done;
7360 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7361 sync_err = sync_fileindex(fileindex, fileindex_path);
7362 if (sync_err && err == NULL)
7363 err = sync_err;
7364 done:
7365 got_fileindex_free(fileindex);
7366 free(fileindex_path);
7367 free(new_head_commit_id);
7368 unlockerr = lock_worktree(worktree, LOCK_SH);
7369 if (unlockerr && err == NULL)
7370 err = unlockerr;
7371 return err;
7374 static const struct got_error *
7375 get_paths_changed_between_commits(struct got_pathlist_head *paths,
7376 struct got_object_id *id1, struct got_object_id *id2,
7377 struct got_repository *repo)
7379 const struct got_error *err;
7380 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
7381 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7383 if (id1) {
7384 err = got_object_open_as_commit(&commit1, repo, id1);
7385 if (err)
7386 goto done;
7388 err = got_object_open_as_tree(&tree1, repo,
7389 got_object_commit_get_tree_id(commit1));
7390 if (err)
7391 goto done;
7394 if (id2) {
7395 err = got_object_open_as_commit(&commit2, repo, id2);
7396 if (err)
7397 goto done;
7399 err = got_object_open_as_tree(&tree2, repo,
7400 got_object_commit_get_tree_id(commit2));
7401 if (err)
7402 goto done;
7405 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
7406 got_diff_tree_collect_changed_paths, paths, 0);
7407 if (err)
7408 goto done;
7409 done:
7410 if (commit1)
7411 got_object_commit_close(commit1);
7412 if (commit2)
7413 got_object_commit_close(commit2);
7414 if (tree1)
7415 got_object_tree_close(tree1);
7416 if (tree2)
7417 got_object_tree_close(tree2);
7418 return err;
7421 static const struct got_error *
7422 get_paths_added_between_commits(struct got_pathlist_head *added_paths,
7423 struct got_object_id *id1, struct got_object_id *id2,
7424 const char *path_prefix, struct got_repository *repo)
7426 const struct got_error *err;
7427 struct got_pathlist_head merged_paths;
7428 struct got_pathlist_entry *pe;
7429 char *abspath = NULL, *wt_path = NULL;
7431 TAILQ_INIT(&merged_paths);
7433 err = get_paths_changed_between_commits(&merged_paths, id1, id2, repo);
7434 if (err)
7435 goto done;
7437 TAILQ_FOREACH(pe, &merged_paths, entry) {
7438 struct got_diff_changed_path *change = pe->data;
7440 if (change->status != GOT_STATUS_ADD)
7441 continue;
7443 if (got_path_is_root_dir(path_prefix)) {
7444 wt_path = strdup(pe->path);
7445 if (wt_path == NULL) {
7446 err = got_error_from_errno("strdup");
7447 goto done;
7449 } else {
7450 if (asprintf(&abspath, "/%s", pe->path) == -1) {
7451 err = got_error_from_errno("asprintf");
7452 goto done;
7455 err = got_path_skip_common_ancestor(&wt_path,
7456 path_prefix, abspath);
7457 if (err)
7458 goto done;
7459 free(abspath);
7460 abspath = NULL;
7463 err = got_pathlist_append(added_paths, wt_path, NULL);
7464 if (err)
7465 goto done;
7466 wt_path = NULL;
7469 done:
7470 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_ALL);
7471 free(abspath);
7472 free(wt_path);
7473 return err;
7476 static const struct got_error *
7477 get_paths_added_in_commit(struct got_pathlist_head *added_paths,
7478 struct got_object_id *id, const char *path_prefix,
7479 struct got_repository *repo)
7481 const struct got_error *err;
7482 struct got_commit_object *commit = NULL;
7483 struct got_object_qid *pid;
7485 err = got_object_open_as_commit(&commit, repo, id);
7486 if (err)
7487 goto done;
7489 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7491 err = get_paths_added_between_commits(added_paths,
7492 pid ? &pid->id : NULL, id, path_prefix, repo);
7493 if (err)
7494 goto done;
7495 done:
7496 if (commit)
7497 got_object_commit_close(commit);
7498 return err;
7501 const struct got_error *
7502 got_worktree_rebase_abort(struct got_worktree *worktree,
7503 struct got_fileindex *fileindex, struct got_repository *repo,
7504 struct got_reference *new_base_branch,
7505 got_worktree_checkout_cb progress_cb, void *progress_arg)
7507 const struct got_error *err, *unlockerr, *sync_err;
7508 struct got_reference *resolved = NULL;
7509 struct got_object_id *commit_id = NULL;
7510 struct got_object_id *merged_commit_id = NULL;
7511 struct got_commit_object *commit = NULL;
7512 char *fileindex_path = NULL;
7513 char *commit_ref_name = NULL;
7514 struct got_reference *commit_ref = NULL;
7515 struct revert_file_args rfa;
7516 struct got_object_id *tree_id = NULL;
7517 struct got_pathlist_head added_paths;
7519 TAILQ_INIT(&added_paths);
7521 err = lock_worktree(worktree, LOCK_EX);
7522 if (err)
7523 return err;
7525 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7526 if (err)
7527 goto done;
7529 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7530 if (err)
7531 goto done;
7533 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
7534 if (err)
7535 goto done;
7538 * Determine which files in added status can be safely removed
7539 * from disk while reverting changes in the work tree.
7540 * We want to avoid deleting unrelated files which were added by
7541 * the user for conflict resolution purposes.
7543 err = get_paths_added_in_commit(&added_paths, merged_commit_id,
7544 got_worktree_get_path_prefix(worktree), repo);
7545 if (err)
7546 goto done;
7548 err = got_ref_open(&resolved, repo,
7549 got_ref_get_symref_target(new_base_branch), 0);
7550 if (err)
7551 goto done;
7553 err = got_worktree_set_head_ref(worktree, resolved);
7554 if (err)
7555 goto done;
7558 * XXX commits to the base branch could have happened while
7559 * we were busy rebasing; should we store the original commit ID
7560 * when rebase begins and read it back here?
7562 err = got_ref_resolve(&commit_id, repo, resolved);
7563 if (err)
7564 goto done;
7566 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7567 if (err)
7568 goto done;
7570 err = got_object_open_as_commit(&commit, repo,
7571 worktree->base_commit_id);
7572 if (err)
7573 goto done;
7575 err = got_object_id_by_path(&tree_id, repo, commit,
7576 worktree->path_prefix);
7577 if (err)
7578 goto done;
7580 err = delete_rebase_refs(worktree, repo);
7581 if (err)
7582 goto done;
7584 err = get_fileindex_path(&fileindex_path, worktree);
7585 if (err)
7586 goto done;
7588 rfa.worktree = worktree;
7589 rfa.fileindex = fileindex;
7590 rfa.progress_cb = progress_cb;
7591 rfa.progress_arg = progress_arg;
7592 rfa.patch_cb = NULL;
7593 rfa.patch_arg = NULL;
7594 rfa.repo = repo;
7595 rfa.unlink_added_files = 1;
7596 rfa.added_files_to_unlink = &added_paths;
7597 err = worktree_status(worktree, "", fileindex, repo,
7598 revert_file, &rfa, NULL, NULL, 1, 0);
7599 if (err)
7600 goto sync;
7602 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7603 repo, progress_cb, progress_arg, NULL, NULL);
7604 sync:
7605 sync_err = sync_fileindex(fileindex, fileindex_path);
7606 if (sync_err && err == NULL)
7607 err = sync_err;
7608 done:
7609 got_pathlist_free(&added_paths, GOT_PATHLIST_FREE_PATH);
7610 got_ref_close(resolved);
7611 free(tree_id);
7612 free(commit_id);
7613 free(merged_commit_id);
7614 if (commit)
7615 got_object_commit_close(commit);
7616 if (fileindex)
7617 got_fileindex_free(fileindex);
7618 free(fileindex_path);
7619 free(commit_ref_name);
7620 if (commit_ref)
7621 got_ref_close(commit_ref);
7623 unlockerr = lock_worktree(worktree, LOCK_SH);
7624 if (unlockerr && err == NULL)
7625 err = unlockerr;
7626 return err;
7629 const struct got_error *
7630 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7631 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7632 struct got_fileindex **fileindex, struct got_worktree *worktree,
7633 struct got_repository *repo)
7635 const struct got_error *err = NULL;
7636 char *tmp_branch_name = NULL;
7637 char *branch_ref_name = NULL;
7638 char *base_commit_ref_name = NULL;
7639 char *fileindex_path = NULL;
7640 struct check_rebase_ok_arg ok_arg;
7641 struct got_reference *wt_branch = NULL;
7642 struct got_reference *base_commit_ref = NULL;
7644 *tmp_branch = NULL;
7645 *branch_ref = NULL;
7646 *base_commit_id = NULL;
7647 *fileindex = NULL;
7649 err = lock_worktree(worktree, LOCK_EX);
7650 if (err)
7651 return err;
7653 err = open_fileindex(fileindex, &fileindex_path, worktree);
7654 if (err)
7655 goto done;
7657 ok_arg.worktree = worktree;
7658 ok_arg.repo = repo;
7659 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7660 &ok_arg);
7661 if (err)
7662 goto done;
7664 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7665 if (err)
7666 goto done;
7668 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7669 if (err)
7670 goto done;
7672 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7673 worktree);
7674 if (err)
7675 goto done;
7677 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7678 0);
7679 if (err)
7680 goto done;
7682 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7683 if (err)
7684 goto done;
7686 err = got_ref_write(*branch_ref, repo);
7687 if (err)
7688 goto done;
7690 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7691 worktree->base_commit_id);
7692 if (err)
7693 goto done;
7694 err = got_ref_write(base_commit_ref, repo);
7695 if (err)
7696 goto done;
7697 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7698 if (*base_commit_id == NULL) {
7699 err = got_error_from_errno("got_object_id_dup");
7700 goto done;
7703 err = got_ref_alloc(tmp_branch, tmp_branch_name,
7704 worktree->base_commit_id);
7705 if (err)
7706 goto done;
7707 err = got_ref_write(*tmp_branch, repo);
7708 if (err)
7709 goto done;
7711 err = got_worktree_set_head_ref(worktree, *tmp_branch);
7712 if (err)
7713 goto done;
7714 done:
7715 free(fileindex_path);
7716 free(tmp_branch_name);
7717 free(branch_ref_name);
7718 free(base_commit_ref_name);
7719 if (wt_branch)
7720 got_ref_close(wt_branch);
7721 if (err) {
7722 if (*branch_ref) {
7723 got_ref_close(*branch_ref);
7724 *branch_ref = NULL;
7726 if (*tmp_branch) {
7727 got_ref_close(*tmp_branch);
7728 *tmp_branch = NULL;
7730 free(*base_commit_id);
7731 if (*fileindex) {
7732 got_fileindex_free(*fileindex);
7733 *fileindex = NULL;
7735 lock_worktree(worktree, LOCK_SH);
7737 return err;
7740 const struct got_error *
7741 got_worktree_histedit_postpone(struct got_worktree *worktree,
7742 struct got_fileindex *fileindex)
7744 if (fileindex)
7745 got_fileindex_free(fileindex);
7746 return lock_worktree(worktree, LOCK_SH);
7749 const struct got_error *
7750 got_worktree_histedit_in_progress(int *in_progress,
7751 struct got_worktree *worktree)
7753 const struct got_error *err;
7754 char *tmp_branch_name = NULL;
7756 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7757 if (err)
7758 return err;
7760 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7761 free(tmp_branch_name);
7762 return NULL;
7765 const struct got_error *
7766 got_worktree_histedit_continue(struct got_object_id **commit_id,
7767 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7768 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7769 struct got_worktree *worktree, struct got_repository *repo)
7771 const struct got_error *err;
7772 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7773 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7774 struct got_reference *commit_ref = NULL;
7775 struct got_reference *base_commit_ref = NULL;
7776 char *fileindex_path = NULL;
7777 int have_staged_files = 0;
7779 *commit_id = NULL;
7780 *tmp_branch = NULL;
7781 *base_commit_id = NULL;
7782 *fileindex = NULL;
7784 err = lock_worktree(worktree, LOCK_EX);
7785 if (err)
7786 return err;
7788 err = open_fileindex(fileindex, &fileindex_path, worktree);
7789 if (err)
7790 goto done;
7792 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7793 &have_staged_files);
7794 if (err && err->code != GOT_ERR_CANCELLED)
7795 goto done;
7796 if (have_staged_files) {
7797 err = got_error(GOT_ERR_STAGED_PATHS);
7798 goto done;
7801 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7802 if (err)
7803 goto done;
7805 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7806 if (err)
7807 goto done;
7809 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7810 if (err)
7811 goto done;
7813 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7814 worktree);
7815 if (err)
7816 goto done;
7818 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7819 if (err)
7820 goto done;
7822 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7823 if (err)
7824 goto done;
7825 err = got_ref_resolve(commit_id, repo, commit_ref);
7826 if (err)
7827 goto done;
7829 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7830 if (err)
7831 goto done;
7832 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7833 if (err)
7834 goto done;
7836 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7837 if (err)
7838 goto done;
7839 done:
7840 free(commit_ref_name);
7841 free(branch_ref_name);
7842 free(fileindex_path);
7843 if (commit_ref)
7844 got_ref_close(commit_ref);
7845 if (base_commit_ref)
7846 got_ref_close(base_commit_ref);
7847 if (err) {
7848 free(*commit_id);
7849 *commit_id = NULL;
7850 free(*base_commit_id);
7851 *base_commit_id = NULL;
7852 if (*tmp_branch) {
7853 got_ref_close(*tmp_branch);
7854 *tmp_branch = NULL;
7856 if (*fileindex) {
7857 got_fileindex_free(*fileindex);
7858 *fileindex = NULL;
7860 lock_worktree(worktree, LOCK_EX);
7862 return err;
7865 static const struct got_error *
7866 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7868 const struct got_error *err;
7869 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7870 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7872 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7873 if (err)
7874 goto done;
7875 err = delete_ref(tmp_branch_name, repo);
7876 if (err)
7877 goto done;
7879 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7880 worktree);
7881 if (err)
7882 goto done;
7883 err = delete_ref(base_commit_ref_name, repo);
7884 if (err)
7885 goto done;
7887 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7888 if (err)
7889 goto done;
7890 err = delete_ref(branch_ref_name, repo);
7891 if (err)
7892 goto done;
7894 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7895 if (err)
7896 goto done;
7897 err = delete_ref(commit_ref_name, repo);
7898 if (err)
7899 goto done;
7900 done:
7901 free(tmp_branch_name);
7902 free(base_commit_ref_name);
7903 free(branch_ref_name);
7904 free(commit_ref_name);
7905 return err;
7908 const struct got_error *
7909 got_worktree_histedit_abort(struct got_worktree *worktree,
7910 struct got_fileindex *fileindex, struct got_repository *repo,
7911 struct got_reference *branch, struct got_object_id *base_commit_id,
7912 got_worktree_checkout_cb progress_cb, void *progress_arg)
7914 const struct got_error *err, *unlockerr, *sync_err;
7915 struct got_reference *resolved = NULL;
7916 char *fileindex_path = NULL;
7917 struct got_object_id *merged_commit_id = NULL;
7918 struct got_commit_object *commit = NULL;
7919 char *commit_ref_name = NULL;
7920 struct got_reference *commit_ref = NULL;
7921 struct got_object_id *tree_id = NULL;
7922 struct revert_file_args rfa;
7923 struct got_pathlist_head added_paths;
7925 TAILQ_INIT(&added_paths);
7927 err = lock_worktree(worktree, LOCK_EX);
7928 if (err)
7929 return err;
7931 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7932 if (err)
7933 goto done;
7935 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7936 if (err) {
7937 if (err->code != GOT_ERR_NOT_REF)
7938 goto done;
7939 /* Can happen on early abort due to invalid histedit script. */
7940 commit_ref = NULL;
7943 if (commit_ref) {
7944 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
7945 if (err)
7946 goto done;
7949 * Determine which files in added status can be safely removed
7950 * from disk while reverting changes in the work tree.
7951 * We want to avoid deleting unrelated files added by the
7952 * user during conflict resolution or during histedit -e.
7954 err = get_paths_added_in_commit(&added_paths, merged_commit_id,
7955 got_worktree_get_path_prefix(worktree), repo);
7956 if (err)
7957 goto done;
7960 err = got_ref_open(&resolved, repo,
7961 got_ref_get_symref_target(branch), 0);
7962 if (err)
7963 goto done;
7965 err = got_worktree_set_head_ref(worktree, resolved);
7966 if (err)
7967 goto done;
7969 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7970 if (err)
7971 goto done;
7973 err = got_object_open_as_commit(&commit, repo,
7974 worktree->base_commit_id);
7975 if (err)
7976 goto done;
7978 err = got_object_id_by_path(&tree_id, repo, commit,
7979 worktree->path_prefix);
7980 if (err)
7981 goto done;
7983 err = delete_histedit_refs(worktree, repo);
7984 if (err)
7985 goto done;
7987 err = get_fileindex_path(&fileindex_path, worktree);
7988 if (err)
7989 goto done;
7991 rfa.worktree = worktree;
7992 rfa.fileindex = fileindex;
7993 rfa.progress_cb = progress_cb;
7994 rfa.progress_arg = progress_arg;
7995 rfa.patch_cb = NULL;
7996 rfa.patch_arg = NULL;
7997 rfa.repo = repo;
7998 rfa.unlink_added_files = 1;
7999 rfa.added_files_to_unlink = &added_paths;
8000 err = worktree_status(worktree, "", fileindex, repo,
8001 revert_file, &rfa, NULL, NULL, 1, 0);
8002 if (err)
8003 goto sync;
8005 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8006 repo, progress_cb, progress_arg, NULL, NULL);
8007 sync:
8008 sync_err = sync_fileindex(fileindex, fileindex_path);
8009 if (sync_err && err == NULL)
8010 err = sync_err;
8011 done:
8012 if (resolved)
8013 got_ref_close(resolved);
8014 if (commit_ref)
8015 got_ref_close(commit_ref);
8016 free(merged_commit_id);
8017 free(tree_id);
8018 free(fileindex_path);
8019 free(commit_ref_name);
8021 unlockerr = lock_worktree(worktree, LOCK_SH);
8022 if (unlockerr && err == NULL)
8023 err = unlockerr;
8024 return err;
8027 const struct got_error *
8028 got_worktree_histedit_complete(struct got_worktree *worktree,
8029 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8030 struct got_reference *edited_branch, struct got_repository *repo)
8032 const struct got_error *err, *unlockerr, *sync_err;
8033 struct got_object_id *new_head_commit_id = NULL;
8034 struct got_reference *resolved = NULL;
8035 char *fileindex_path = NULL;
8037 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
8038 if (err)
8039 return err;
8041 err = got_ref_open(&resolved, repo,
8042 got_ref_get_symref_target(edited_branch), 0);
8043 if (err)
8044 goto done;
8046 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
8047 resolved, new_head_commit_id, repo);
8048 if (err)
8049 goto done;
8051 err = got_ref_change_ref(resolved, new_head_commit_id);
8052 if (err)
8053 goto done;
8055 err = got_ref_write(resolved, repo);
8056 if (err)
8057 goto done;
8059 err = got_worktree_set_head_ref(worktree, resolved);
8060 if (err)
8061 goto done;
8063 err = delete_histedit_refs(worktree, repo);
8064 if (err)
8065 goto done;
8067 err = get_fileindex_path(&fileindex_path, worktree);
8068 if (err)
8069 goto done;
8070 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8071 sync_err = sync_fileindex(fileindex, fileindex_path);
8072 if (sync_err && err == NULL)
8073 err = sync_err;
8074 done:
8075 got_fileindex_free(fileindex);
8076 free(fileindex_path);
8077 free(new_head_commit_id);
8078 unlockerr = lock_worktree(worktree, LOCK_SH);
8079 if (unlockerr && err == NULL)
8080 err = unlockerr;
8081 return err;
8084 const struct got_error *
8085 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
8086 struct got_object_id *commit_id, struct got_repository *repo)
8088 const struct got_error *err;
8089 char *commit_ref_name;
8091 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8092 if (err)
8093 return err;
8095 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
8096 if (err)
8097 goto done;
8099 err = delete_ref(commit_ref_name, repo);
8100 done:
8101 free(commit_ref_name);
8102 return err;
8105 const struct got_error *
8106 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
8107 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
8108 struct got_worktree *worktree, const char *refname,
8109 struct got_repository *repo)
8111 const struct got_error *err = NULL;
8112 char *fileindex_path = NULL;
8113 struct check_rebase_ok_arg ok_arg;
8115 *fileindex = NULL;
8116 *branch_ref = NULL;
8117 *base_branch_ref = NULL;
8119 err = lock_worktree(worktree, LOCK_EX);
8120 if (err)
8121 return err;
8123 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
8124 err = got_error_msg(GOT_ERR_SAME_BRANCH,
8125 "cannot integrate a branch into itself; "
8126 "update -b or different branch name required");
8127 goto done;
8130 err = open_fileindex(fileindex, &fileindex_path, worktree);
8131 if (err)
8132 goto done;
8134 /* Preconditions are the same as for rebase. */
8135 ok_arg.worktree = worktree;
8136 ok_arg.repo = repo;
8137 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8138 &ok_arg);
8139 if (err)
8140 goto done;
8142 err = got_ref_open(branch_ref, repo, refname, 1);
8143 if (err)
8144 goto done;
8146 err = got_ref_open(base_branch_ref, repo,
8147 got_worktree_get_head_ref_name(worktree), 1);
8148 done:
8149 if (err) {
8150 if (*branch_ref) {
8151 got_ref_close(*branch_ref);
8152 *branch_ref = NULL;
8154 if (*base_branch_ref) {
8155 got_ref_close(*base_branch_ref);
8156 *base_branch_ref = NULL;
8158 if (*fileindex) {
8159 got_fileindex_free(*fileindex);
8160 *fileindex = NULL;
8162 lock_worktree(worktree, LOCK_SH);
8164 return err;
8167 const struct got_error *
8168 got_worktree_integrate_continue(struct got_worktree *worktree,
8169 struct got_fileindex *fileindex, struct got_repository *repo,
8170 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
8171 got_worktree_checkout_cb progress_cb, void *progress_arg,
8172 got_cancel_cb cancel_cb, void *cancel_arg)
8174 const struct got_error *err = NULL, *sync_err, *unlockerr;
8175 char *fileindex_path = NULL;
8176 struct got_object_id *tree_id = NULL, *commit_id = NULL;
8177 struct got_commit_object *commit = NULL;
8179 err = get_fileindex_path(&fileindex_path, worktree);
8180 if (err)
8181 goto done;
8183 err = got_ref_resolve(&commit_id, repo, branch_ref);
8184 if (err)
8185 goto done;
8187 err = got_object_open_as_commit(&commit, repo, commit_id);
8188 if (err)
8189 goto done;
8191 err = got_object_id_by_path(&tree_id, repo, commit,
8192 worktree->path_prefix);
8193 if (err)
8194 goto done;
8196 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
8197 if (err)
8198 goto done;
8200 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
8201 progress_cb, progress_arg, cancel_cb, cancel_arg);
8202 if (err)
8203 goto sync;
8205 err = got_ref_change_ref(base_branch_ref, commit_id);
8206 if (err)
8207 goto sync;
8209 err = got_ref_write(base_branch_ref, repo);
8210 if (err)
8211 goto sync;
8213 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8214 sync:
8215 sync_err = sync_fileindex(fileindex, fileindex_path);
8216 if (sync_err && err == NULL)
8217 err = sync_err;
8219 done:
8220 unlockerr = got_ref_unlock(branch_ref);
8221 if (unlockerr && err == NULL)
8222 err = unlockerr;
8223 got_ref_close(branch_ref);
8225 unlockerr = got_ref_unlock(base_branch_ref);
8226 if (unlockerr && err == NULL)
8227 err = unlockerr;
8228 got_ref_close(base_branch_ref);
8230 got_fileindex_free(fileindex);
8231 free(fileindex_path);
8232 free(tree_id);
8233 if (commit)
8234 got_object_commit_close(commit);
8236 unlockerr = lock_worktree(worktree, LOCK_SH);
8237 if (unlockerr && err == NULL)
8238 err = unlockerr;
8239 return err;
8242 const struct got_error *
8243 got_worktree_integrate_abort(struct got_worktree *worktree,
8244 struct got_fileindex *fileindex, struct got_repository *repo,
8245 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
8247 const struct got_error *err = NULL, *unlockerr = NULL;
8249 got_fileindex_free(fileindex);
8251 err = lock_worktree(worktree, LOCK_SH);
8253 unlockerr = got_ref_unlock(branch_ref);
8254 if (unlockerr && err == NULL)
8255 err = unlockerr;
8256 got_ref_close(branch_ref);
8258 unlockerr = got_ref_unlock(base_branch_ref);
8259 if (unlockerr && err == NULL)
8260 err = unlockerr;
8261 got_ref_close(base_branch_ref);
8263 return err;
8266 const struct got_error *
8267 got_worktree_merge_postpone(struct got_worktree *worktree,
8268 struct got_fileindex *fileindex)
8270 const struct got_error *err, *sync_err;
8271 char *fileindex_path = NULL;
8273 err = get_fileindex_path(&fileindex_path, worktree);
8274 if (err)
8275 goto done;
8277 sync_err = sync_fileindex(fileindex, fileindex_path);
8279 err = lock_worktree(worktree, LOCK_SH);
8280 if (sync_err && err == NULL)
8281 err = sync_err;
8282 done:
8283 got_fileindex_free(fileindex);
8284 free(fileindex_path);
8285 return err;
8288 static const struct got_error *
8289 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
8291 const struct got_error *err;
8292 char *branch_refname = NULL, *commit_refname = NULL;
8294 err = get_merge_branch_ref_name(&branch_refname, worktree);
8295 if (err)
8296 goto done;
8297 err = delete_ref(branch_refname, repo);
8298 if (err)
8299 goto done;
8301 err = get_merge_commit_ref_name(&commit_refname, worktree);
8302 if (err)
8303 goto done;
8304 err = delete_ref(commit_refname, repo);
8305 if (err)
8306 goto done;
8308 done:
8309 free(branch_refname);
8310 free(commit_refname);
8311 return err;
8314 struct merge_commit_msg_arg {
8315 struct got_worktree *worktree;
8316 const char *branch_name;
8319 static const struct got_error *
8320 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
8321 const char *diff_path, char **logmsg, void *arg)
8323 struct merge_commit_msg_arg *a = arg;
8325 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
8326 got_worktree_get_head_ref_name(a->worktree)) == -1)
8327 return got_error_from_errno("asprintf");
8329 return NULL;
8333 const struct got_error *
8334 got_worktree_merge_branch(struct got_worktree *worktree,
8335 struct got_fileindex *fileindex,
8336 struct got_object_id *yca_commit_id,
8337 struct got_object_id *branch_tip,
8338 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
8339 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
8341 const struct got_error *err;
8342 char *fileindex_path = NULL;
8344 err = get_fileindex_path(&fileindex_path, worktree);
8345 if (err)
8346 goto done;
8348 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
8349 worktree);
8350 if (err)
8351 goto done;
8353 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
8354 branch_tip, repo, progress_cb, progress_arg,
8355 cancel_cb, cancel_arg);
8356 done:
8357 free(fileindex_path);
8358 return err;
8361 const struct got_error *
8362 got_worktree_merge_commit(struct got_object_id **new_commit_id,
8363 struct got_worktree *worktree, struct got_fileindex *fileindex,
8364 const char *author, const char *committer, int allow_bad_symlinks,
8365 struct got_object_id *branch_tip, const char *branch_name,
8366 int allow_conflict, struct got_repository *repo,
8367 got_worktree_status_cb status_cb, void *status_arg)
8370 const struct got_error *err = NULL, *sync_err;
8371 struct got_pathlist_head commitable_paths;
8372 struct collect_commitables_arg cc_arg;
8373 struct got_pathlist_entry *pe;
8374 struct got_reference *head_ref = NULL;
8375 struct got_object_id *head_commit_id = NULL;
8376 int have_staged_files = 0;
8377 struct merge_commit_msg_arg mcm_arg;
8378 char *fileindex_path = NULL;
8380 memset(&cc_arg, 0, sizeof(cc_arg));
8381 *new_commit_id = NULL;
8383 TAILQ_INIT(&commitable_paths);
8385 err = get_fileindex_path(&fileindex_path, worktree);
8386 if (err)
8387 goto done;
8389 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
8390 if (err)
8391 goto done;
8393 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8394 if (err)
8395 goto done;
8397 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
8398 &have_staged_files);
8399 if (err && err->code != GOT_ERR_CANCELLED)
8400 goto done;
8401 if (have_staged_files) {
8402 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
8403 goto done;
8406 cc_arg.commitable_paths = &commitable_paths;
8407 cc_arg.worktree = worktree;
8408 cc_arg.fileindex = fileindex;
8409 cc_arg.repo = repo;
8410 cc_arg.have_staged_files = have_staged_files;
8411 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
8412 cc_arg.commit_conflicts = allow_conflict;
8413 err = worktree_status(worktree, "", fileindex, repo,
8414 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8415 if (err)
8416 goto done;
8418 mcm_arg.worktree = worktree;
8419 mcm_arg.branch_name = branch_name;
8420 err = commit_worktree(new_commit_id, &commitable_paths,
8421 head_commit_id, branch_tip, worktree, author, committer, NULL,
8422 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8423 if (err)
8424 goto done;
8426 err = update_fileindex_after_commit(worktree, &commitable_paths,
8427 *new_commit_id, fileindex, have_staged_files);
8428 sync_err = sync_fileindex(fileindex, fileindex_path);
8429 if (sync_err && err == NULL)
8430 err = sync_err;
8431 done:
8432 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8433 struct got_commitable *ct = pe->data;
8435 free_commitable(ct);
8437 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8438 free(fileindex_path);
8439 return err;
8442 const struct got_error *
8443 got_worktree_merge_complete(struct got_worktree *worktree,
8444 struct got_fileindex *fileindex, struct got_repository *repo)
8446 const struct got_error *err, *unlockerr, *sync_err;
8447 char *fileindex_path = NULL;
8449 err = delete_merge_refs(worktree, repo);
8450 if (err)
8451 goto done;
8453 err = get_fileindex_path(&fileindex_path, worktree);
8454 if (err)
8455 goto done;
8456 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8457 sync_err = sync_fileindex(fileindex, fileindex_path);
8458 if (sync_err && err == NULL)
8459 err = sync_err;
8460 done:
8461 got_fileindex_free(fileindex);
8462 free(fileindex_path);
8463 unlockerr = lock_worktree(worktree, LOCK_SH);
8464 if (unlockerr && err == NULL)
8465 err = unlockerr;
8466 return err;
8469 const struct got_error *
8470 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8471 struct got_repository *repo)
8473 const struct got_error *err;
8474 char *branch_refname = NULL;
8475 struct got_reference *branch_ref = NULL;
8477 *in_progress = 0;
8479 err = get_merge_branch_ref_name(&branch_refname, worktree);
8480 if (err)
8481 return err;
8482 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8483 free(branch_refname);
8484 if (err) {
8485 if (err->code != GOT_ERR_NOT_REF)
8486 return err;
8487 } else
8488 *in_progress = 1;
8490 return NULL;
8493 const struct got_error *got_worktree_merge_prepare(
8494 struct got_fileindex **fileindex, struct got_worktree *worktree,
8495 struct got_repository *repo)
8497 const struct got_error *err = NULL;
8498 char *fileindex_path = NULL;
8499 struct got_reference *wt_branch = NULL;
8500 struct got_object_id *wt_branch_tip = NULL;
8501 struct check_rebase_ok_arg ok_arg;
8503 *fileindex = NULL;
8505 err = lock_worktree(worktree, LOCK_EX);
8506 if (err)
8507 return err;
8509 err = open_fileindex(fileindex, &fileindex_path, worktree);
8510 if (err)
8511 goto done;
8513 /* Preconditions are the same as for rebase. */
8514 ok_arg.worktree = worktree;
8515 ok_arg.repo = repo;
8516 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8517 &ok_arg);
8518 if (err)
8519 goto done;
8521 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8522 0);
8523 if (err)
8524 goto done;
8526 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8527 if (err)
8528 goto done;
8530 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8531 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8532 goto done;
8535 done:
8536 free(fileindex_path);
8537 if (wt_branch)
8538 got_ref_close(wt_branch);
8539 free(wt_branch_tip);
8540 if (err) {
8541 if (*fileindex) {
8542 got_fileindex_free(*fileindex);
8543 *fileindex = NULL;
8545 lock_worktree(worktree, LOCK_SH);
8547 return err;
8550 const struct got_error *got_worktree_merge_write_refs(
8551 struct got_worktree *worktree, struct got_reference *branch,
8552 struct got_repository *repo)
8554 const struct got_error *err = NULL;
8555 char *branch_refname = NULL, *commit_refname = NULL;
8556 struct got_reference *branch_ref = NULL, *commit_ref = NULL;
8557 struct got_object_id *branch_tip = NULL;
8559 err = get_merge_branch_ref_name(&branch_refname, worktree);
8560 if (err)
8561 return err;
8563 err = get_merge_commit_ref_name(&commit_refname, worktree);
8564 if (err)
8565 return err;
8567 err = got_ref_resolve(&branch_tip, repo, branch);
8568 if (err)
8569 goto done;
8571 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8572 if (err)
8573 goto done;
8574 err = got_ref_write(branch_ref, repo);
8575 if (err)
8576 goto done;
8578 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8579 if (err)
8580 goto done;
8581 err = got_ref_write(commit_ref, repo);
8582 if (err)
8583 goto done;
8585 done:
8586 free(branch_refname);
8587 free(commit_refname);
8588 if (branch_ref)
8589 got_ref_close(branch_ref);
8590 if (commit_ref)
8591 got_ref_close(commit_ref);
8592 free(branch_tip);
8593 return err;
8596 const struct got_error *
8597 got_worktree_merge_continue(char **branch_name,
8598 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8599 struct got_worktree *worktree, struct got_repository *repo)
8601 const struct got_error *err;
8602 char *commit_refname = NULL, *branch_refname = NULL;
8603 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8604 char *fileindex_path = NULL;
8605 int have_staged_files = 0;
8607 *branch_name = NULL;
8608 *branch_tip = NULL;
8609 *fileindex = NULL;
8611 err = lock_worktree(worktree, LOCK_EX);
8612 if (err)
8613 return err;
8615 err = open_fileindex(fileindex, &fileindex_path, worktree);
8616 if (err)
8617 goto done;
8619 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8620 &have_staged_files);
8621 if (err && err->code != GOT_ERR_CANCELLED)
8622 goto done;
8623 if (have_staged_files) {
8624 err = got_error(GOT_ERR_STAGED_PATHS);
8625 goto done;
8628 err = get_merge_branch_ref_name(&branch_refname, worktree);
8629 if (err)
8630 goto done;
8632 err = get_merge_commit_ref_name(&commit_refname, worktree);
8633 if (err)
8634 goto done;
8636 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8637 if (err)
8638 goto done;
8640 if (!got_ref_is_symbolic(branch_ref)) {
8641 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8642 "%s is not a symbolic reference",
8643 got_ref_get_name(branch_ref));
8644 goto done;
8646 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8647 if (*branch_name == NULL) {
8648 err = got_error_from_errno("strdup");
8649 goto done;
8652 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8653 if (err)
8654 goto done;
8656 err = got_ref_resolve(branch_tip, repo, commit_ref);
8657 if (err)
8658 goto done;
8659 done:
8660 free(commit_refname);
8661 free(branch_refname);
8662 free(fileindex_path);
8663 if (commit_ref)
8664 got_ref_close(commit_ref);
8665 if (branch_ref)
8666 got_ref_close(branch_ref);
8667 if (err) {
8668 if (*branch_name) {
8669 free(*branch_name);
8670 *branch_name = NULL;
8672 free(*branch_tip);
8673 *branch_tip = NULL;
8674 if (*fileindex) {
8675 got_fileindex_free(*fileindex);
8676 *fileindex = NULL;
8678 lock_worktree(worktree, LOCK_SH);
8680 return err;
8683 const struct got_error *
8684 got_worktree_merge_abort(struct got_worktree *worktree,
8685 struct got_fileindex *fileindex, struct got_repository *repo,
8686 got_worktree_checkout_cb progress_cb, void *progress_arg)
8688 const struct got_error *err, *unlockerr, *sync_err;
8689 struct got_commit_object *commit = NULL;
8690 char *fileindex_path = NULL;
8691 struct revert_file_args rfa;
8692 char *commit_ref_name = NULL;
8693 struct got_reference *commit_ref = NULL;
8694 struct got_object_id *merged_commit_id = NULL;
8695 struct got_object_id *tree_id = NULL;
8696 struct got_pathlist_head added_paths;
8698 TAILQ_INIT(&added_paths);
8700 err = get_merge_commit_ref_name(&commit_ref_name, worktree);
8701 if (err)
8702 goto done;
8704 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8705 if (err)
8706 goto done;
8708 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8709 if (err)
8710 goto done;
8713 * Determine which files in added status can be safely removed
8714 * from disk while reverting changes in the work tree.
8715 * We want to avoid deleting unrelated files which were added by
8716 * the user for conflict resolution purposes.
8718 err = get_paths_added_between_commits(&added_paths,
8719 got_worktree_get_base_commit_id(worktree), merged_commit_id,
8720 got_worktree_get_path_prefix(worktree), repo);
8721 if (err)
8722 goto done;
8725 err = got_object_open_as_commit(&commit, repo,
8726 worktree->base_commit_id);
8727 if (err)
8728 goto done;
8730 err = got_object_id_by_path(&tree_id, repo, commit,
8731 worktree->path_prefix);
8732 if (err)
8733 goto done;
8735 err = delete_merge_refs(worktree, repo);
8736 if (err)
8737 goto done;
8739 err = get_fileindex_path(&fileindex_path, worktree);
8740 if (err)
8741 goto done;
8743 rfa.worktree = worktree;
8744 rfa.fileindex = fileindex;
8745 rfa.progress_cb = progress_cb;
8746 rfa.progress_arg = progress_arg;
8747 rfa.patch_cb = NULL;
8748 rfa.patch_arg = NULL;
8749 rfa.repo = repo;
8750 rfa.unlink_added_files = 1;
8751 rfa.added_files_to_unlink = &added_paths;
8752 err = worktree_status(worktree, "", fileindex, repo,
8753 revert_file, &rfa, NULL, NULL, 1, 0);
8754 if (err)
8755 goto sync;
8757 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8758 repo, progress_cb, progress_arg, NULL, NULL);
8759 sync:
8760 sync_err = sync_fileindex(fileindex, fileindex_path);
8761 if (sync_err && err == NULL)
8762 err = sync_err;
8763 done:
8764 free(tree_id);
8765 free(merged_commit_id);
8766 if (commit)
8767 got_object_commit_close(commit);
8768 if (fileindex)
8769 got_fileindex_free(fileindex);
8770 free(fileindex_path);
8771 if (commit_ref)
8772 got_ref_close(commit_ref);
8773 free(commit_ref_name);
8775 unlockerr = lock_worktree(worktree, LOCK_SH);
8776 if (unlockerr && err == NULL)
8777 err = unlockerr;
8778 return err;
8781 struct check_stage_ok_arg {
8782 struct got_object_id *head_commit_id;
8783 struct got_worktree *worktree;
8784 struct got_fileindex *fileindex;
8785 struct got_repository *repo;
8786 int have_changes;
8789 static const struct got_error *
8790 check_stage_ok(void *arg, unsigned char status,
8791 unsigned char staged_status, const char *relpath,
8792 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8793 struct got_object_id *commit_id, int dirfd, const char *de_name)
8795 struct check_stage_ok_arg *a = arg;
8796 const struct got_error *err = NULL;
8797 struct got_fileindex_entry *ie;
8798 struct got_object_id base_commit_id;
8799 struct got_object_id *base_commit_idp = NULL;
8800 char *in_repo_path = NULL, *p;
8802 if (status == GOT_STATUS_UNVERSIONED ||
8803 status == GOT_STATUS_NO_CHANGE)
8804 return NULL;
8805 if (status == GOT_STATUS_NONEXISTENT)
8806 return got_error_set_errno(ENOENT, relpath);
8808 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8809 if (ie == NULL)
8810 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8812 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8813 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8814 relpath) == -1)
8815 return got_error_from_errno("asprintf");
8817 if (got_fileindex_entry_has_commit(ie)) {
8818 base_commit_idp = got_fileindex_entry_get_commit_id(
8819 &base_commit_id, ie);
8822 if (status == GOT_STATUS_CONFLICT) {
8823 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8824 goto done;
8825 } else if (status != GOT_STATUS_ADD &&
8826 status != GOT_STATUS_MODIFY &&
8827 status != GOT_STATUS_DELETE) {
8828 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8829 goto done;
8832 a->have_changes = 1;
8834 p = in_repo_path;
8835 while (p[0] == '/')
8836 p++;
8837 err = check_out_of_date(p, status, staged_status,
8838 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8839 GOT_ERR_STAGE_OUT_OF_DATE);
8840 done:
8841 free(in_repo_path);
8842 return err;
8845 struct stage_path_arg {
8846 struct got_worktree *worktree;
8847 struct got_fileindex *fileindex;
8848 struct got_repository *repo;
8849 got_worktree_status_cb status_cb;
8850 void *status_arg;
8851 got_worktree_patch_cb patch_cb;
8852 void *patch_arg;
8853 int staged_something;
8854 int allow_bad_symlinks;
8857 static const struct got_error *
8858 stage_path(void *arg, unsigned char status,
8859 unsigned char staged_status, const char *relpath,
8860 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8861 struct got_object_id *commit_id, int dirfd, const char *de_name)
8863 struct stage_path_arg *a = arg;
8864 const struct got_error *err = NULL;
8865 struct got_fileindex_entry *ie;
8866 char *ondisk_path = NULL, *path_content = NULL;
8867 uint32_t stage;
8868 struct got_object_id *new_staged_blob_id = NULL;
8869 struct stat sb;
8871 if (status == GOT_STATUS_UNVERSIONED)
8872 return NULL;
8874 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8875 if (ie == NULL)
8876 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8878 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8879 relpath)== -1)
8880 return got_error_from_errno("asprintf");
8882 switch (status) {
8883 case GOT_STATUS_ADD:
8884 case GOT_STATUS_MODIFY:
8885 /* XXX could sb.st_mode be passed in by our caller? */
8886 if (lstat(ondisk_path, &sb) == -1) {
8887 err = got_error_from_errno2("lstat", ondisk_path);
8888 break;
8890 if (a->patch_cb) {
8891 if (status == GOT_STATUS_ADD) {
8892 int choice = GOT_PATCH_CHOICE_NONE;
8893 err = (*a->patch_cb)(&choice, a->patch_arg,
8894 status, ie->path, NULL, 1, 1);
8895 if (err)
8896 break;
8897 if (choice != GOT_PATCH_CHOICE_YES)
8898 break;
8899 } else {
8900 err = create_patched_content(&path_content, 0,
8901 staged_blob_id ? staged_blob_id : blob_id,
8902 ondisk_path, dirfd, de_name, ie->path,
8903 a->repo, a->patch_cb, a->patch_arg);
8904 if (err || path_content == NULL)
8905 break;
8908 err = got_object_blob_create(&new_staged_blob_id,
8909 path_content ? path_content : ondisk_path, a->repo);
8910 if (err)
8911 break;
8912 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8913 SHA1_DIGEST_LENGTH);
8914 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8915 stage = GOT_FILEIDX_STAGE_ADD;
8916 else
8917 stage = GOT_FILEIDX_STAGE_MODIFY;
8918 got_fileindex_entry_stage_set(ie, stage);
8919 if (S_ISLNK(sb.st_mode)) {
8920 int is_bad_symlink = 0;
8921 if (!a->allow_bad_symlinks) {
8922 char target_path[PATH_MAX];
8923 ssize_t target_len;
8924 target_len = readlink(ondisk_path, target_path,
8925 sizeof(target_path));
8926 if (target_len == -1) {
8927 err = got_error_from_errno2("readlink",
8928 ondisk_path);
8929 break;
8931 err = is_bad_symlink_target(&is_bad_symlink,
8932 target_path, target_len, ondisk_path,
8933 a->worktree->root_path);
8934 if (err)
8935 break;
8936 if (is_bad_symlink) {
8937 err = got_error_path(ondisk_path,
8938 GOT_ERR_BAD_SYMLINK);
8939 break;
8942 if (is_bad_symlink)
8943 got_fileindex_entry_staged_filetype_set(ie,
8944 GOT_FILEIDX_MODE_BAD_SYMLINK);
8945 else
8946 got_fileindex_entry_staged_filetype_set(ie,
8947 GOT_FILEIDX_MODE_SYMLINK);
8948 } else {
8949 got_fileindex_entry_staged_filetype_set(ie,
8950 GOT_FILEIDX_MODE_REGULAR_FILE);
8952 a->staged_something = 1;
8953 if (a->status_cb == NULL)
8954 break;
8955 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8956 get_staged_status(ie), relpath, blob_id,
8957 new_staged_blob_id, NULL, dirfd, de_name);
8958 if (err)
8959 break;
8961 * When staging the reverse of the staged diff,
8962 * implicitly unstage the file.
8964 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8965 sizeof(ie->blob_sha1)) == 0) {
8966 got_fileindex_entry_stage_set(ie,
8967 GOT_FILEIDX_STAGE_NONE);
8969 break;
8970 case GOT_STATUS_DELETE:
8971 if (staged_status == GOT_STATUS_DELETE)
8972 break;
8973 if (a->patch_cb) {
8974 int choice = GOT_PATCH_CHOICE_NONE;
8975 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8976 ie->path, NULL, 1, 1);
8977 if (err)
8978 break;
8979 if (choice == GOT_PATCH_CHOICE_NO)
8980 break;
8981 if (choice != GOT_PATCH_CHOICE_YES) {
8982 err = got_error(GOT_ERR_PATCH_CHOICE);
8983 break;
8986 stage = GOT_FILEIDX_STAGE_DELETE;
8987 got_fileindex_entry_stage_set(ie, stage);
8988 a->staged_something = 1;
8989 if (a->status_cb == NULL)
8990 break;
8991 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8992 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8993 de_name);
8994 break;
8995 case GOT_STATUS_NO_CHANGE:
8996 break;
8997 case GOT_STATUS_CONFLICT:
8998 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8999 break;
9000 case GOT_STATUS_NONEXISTENT:
9001 err = got_error_set_errno(ENOENT, relpath);
9002 break;
9003 default:
9004 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
9005 break;
9008 if (path_content && unlink(path_content) == -1 && err == NULL)
9009 err = got_error_from_errno2("unlink", path_content);
9010 free(path_content);
9011 free(ondisk_path);
9012 free(new_staged_blob_id);
9013 return err;
9016 const struct got_error *
9017 got_worktree_stage(struct got_worktree *worktree,
9018 struct got_pathlist_head *paths,
9019 got_worktree_status_cb status_cb, void *status_arg,
9020 got_worktree_patch_cb patch_cb, void *patch_arg,
9021 int allow_bad_symlinks, struct got_repository *repo)
9023 const struct got_error *err = NULL, *sync_err, *unlockerr;
9024 struct got_pathlist_entry *pe;
9025 struct got_fileindex *fileindex = NULL;
9026 char *fileindex_path = NULL;
9027 struct got_reference *head_ref = NULL;
9028 struct got_object_id *head_commit_id = NULL;
9029 struct check_stage_ok_arg oka;
9030 struct stage_path_arg spa;
9032 err = lock_worktree(worktree, LOCK_EX);
9033 if (err)
9034 return err;
9036 err = got_ref_open(&head_ref, repo,
9037 got_worktree_get_head_ref_name(worktree), 0);
9038 if (err)
9039 goto done;
9040 err = got_ref_resolve(&head_commit_id, repo, head_ref);
9041 if (err)
9042 goto done;
9043 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9044 if (err)
9045 goto done;
9047 /* Check pre-conditions before staging anything. */
9048 oka.head_commit_id = head_commit_id;
9049 oka.worktree = worktree;
9050 oka.fileindex = fileindex;
9051 oka.repo = repo;
9052 oka.have_changes = 0;
9053 TAILQ_FOREACH(pe, paths, entry) {
9054 err = worktree_status(worktree, pe->path, fileindex, repo,
9055 check_stage_ok, &oka, NULL, NULL, 1, 0);
9056 if (err)
9057 goto done;
9059 if (!oka.have_changes) {
9060 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9061 goto done;
9064 spa.worktree = worktree;
9065 spa.fileindex = fileindex;
9066 spa.repo = repo;
9067 spa.patch_cb = patch_cb;
9068 spa.patch_arg = patch_arg;
9069 spa.status_cb = status_cb;
9070 spa.status_arg = status_arg;
9071 spa.staged_something = 0;
9072 spa.allow_bad_symlinks = allow_bad_symlinks;
9073 TAILQ_FOREACH(pe, paths, entry) {
9074 err = worktree_status(worktree, pe->path, fileindex, repo,
9075 stage_path, &spa, NULL, NULL, 1, 0);
9076 if (err)
9077 goto done;
9079 if (!spa.staged_something) {
9080 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9081 goto done;
9084 sync_err = sync_fileindex(fileindex, fileindex_path);
9085 if (sync_err && err == NULL)
9086 err = sync_err;
9087 done:
9088 if (head_ref)
9089 got_ref_close(head_ref);
9090 free(head_commit_id);
9091 free(fileindex_path);
9092 if (fileindex)
9093 got_fileindex_free(fileindex);
9094 unlockerr = lock_worktree(worktree, LOCK_SH);
9095 if (unlockerr && err == NULL)
9096 err = unlockerr;
9097 return err;
9100 struct unstage_path_arg {
9101 struct got_worktree *worktree;
9102 struct got_fileindex *fileindex;
9103 struct got_repository *repo;
9104 got_worktree_checkout_cb progress_cb;
9105 void *progress_arg;
9106 got_worktree_patch_cb patch_cb;
9107 void *patch_arg;
9110 static const struct got_error *
9111 create_unstaged_content(char **path_unstaged_content,
9112 char **path_new_staged_content, struct got_object_id *blob_id,
9113 struct got_object_id *staged_blob_id, const char *relpath,
9114 struct got_repository *repo,
9115 got_worktree_patch_cb patch_cb, void *patch_arg)
9117 const struct got_error *err, *free_err;
9118 struct got_blob_object *blob = NULL, *staged_blob = NULL;
9119 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
9120 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
9121 struct got_diffreg_result *diffreg_result = NULL;
9122 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
9123 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
9124 int fd1 = -1, fd2 = -1;
9126 *path_unstaged_content = NULL;
9127 *path_new_staged_content = NULL;
9129 err = got_object_id_str(&label1, blob_id);
9130 if (err)
9131 return err;
9133 fd1 = got_opentempfd();
9134 if (fd1 == -1) {
9135 err = got_error_from_errno("got_opentempfd");
9136 goto done;
9138 fd2 = got_opentempfd();
9139 if (fd2 == -1) {
9140 err = got_error_from_errno("got_opentempfd");
9141 goto done;
9144 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
9145 if (err)
9146 goto done;
9148 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
9149 if (err)
9150 goto done;
9152 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
9153 if (err)
9154 goto done;
9156 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
9157 fd2);
9158 if (err)
9159 goto done;
9161 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
9162 if (err)
9163 goto done;
9165 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
9166 if (err)
9167 goto done;
9169 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
9170 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
9171 if (err)
9172 goto done;
9174 err = got_opentemp_named(path_unstaged_content, &outfile,
9175 "got-unstaged-content", "");
9176 if (err)
9177 goto done;
9178 err = got_opentemp_named(path_new_staged_content, &rejectfile,
9179 "got-new-staged-content", "");
9180 if (err)
9181 goto done;
9183 if (fseek(f1, 0L, SEEK_SET) == -1) {
9184 err = got_ferror(f1, GOT_ERR_IO);
9185 goto done;
9187 if (fseek(f2, 0L, SEEK_SET) == -1) {
9188 err = got_ferror(f2, GOT_ERR_IO);
9189 goto done;
9191 /* Count the number of actual changes in the diff result. */
9192 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9193 struct diff_chunk_context cc = {};
9194 diff_chunk_context_load_change(&cc, &nchunks_used,
9195 diffreg_result->result, n, 0);
9196 nchanges++;
9198 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9199 int choice;
9200 err = apply_or_reject_change(&choice, &nchunks_used,
9201 diffreg_result->result, n, relpath, f1, f2,
9202 &line_cur1, &line_cur2,
9203 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
9204 if (err)
9205 goto done;
9206 if (choice == GOT_PATCH_CHOICE_YES)
9207 have_content = 1;
9208 else
9209 have_rejected_content = 1;
9210 if (choice == GOT_PATCH_CHOICE_QUIT)
9211 break;
9213 if (have_content || have_rejected_content)
9214 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
9215 outfile, rejectfile);
9216 done:
9217 free(label1);
9218 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9219 err = got_error_from_errno("close");
9220 if (blob)
9221 got_object_blob_close(blob);
9222 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9223 err = got_error_from_errno("close");
9224 if (staged_blob)
9225 got_object_blob_close(staged_blob);
9226 free_err = got_diffreg_result_free(diffreg_result);
9227 if (free_err && err == NULL)
9228 err = free_err;
9229 if (f1 && fclose(f1) == EOF && err == NULL)
9230 err = got_error_from_errno2("fclose", path1);
9231 if (f2 && fclose(f2) == EOF && err == NULL)
9232 err = got_error_from_errno2("fclose", path2);
9233 if (outfile && fclose(outfile) == EOF && err == NULL)
9234 err = got_error_from_errno2("fclose", *path_unstaged_content);
9235 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
9236 err = got_error_from_errno2("fclose", *path_new_staged_content);
9237 if (path1 && unlink(path1) == -1 && err == NULL)
9238 err = got_error_from_errno2("unlink", path1);
9239 if (path2 && unlink(path2) == -1 && err == NULL)
9240 err = got_error_from_errno2("unlink", path2);
9241 if (err || !have_content) {
9242 if (*path_unstaged_content &&
9243 unlink(*path_unstaged_content) == -1 && err == NULL)
9244 err = got_error_from_errno2("unlink",
9245 *path_unstaged_content);
9246 free(*path_unstaged_content);
9247 *path_unstaged_content = NULL;
9249 if (err || !have_content || !have_rejected_content) {
9250 if (*path_new_staged_content &&
9251 unlink(*path_new_staged_content) == -1 && err == NULL)
9252 err = got_error_from_errno2("unlink",
9253 *path_new_staged_content);
9254 free(*path_new_staged_content);
9255 *path_new_staged_content = NULL;
9257 free(path1);
9258 free(path2);
9259 return err;
9262 static const struct got_error *
9263 unstage_hunks(struct got_object_id *staged_blob_id,
9264 struct got_blob_object *blob_base,
9265 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
9266 const char *ondisk_path, const char *label_orig,
9267 struct got_worktree *worktree, struct got_repository *repo,
9268 got_worktree_patch_cb patch_cb, void *patch_arg,
9269 got_worktree_checkout_cb progress_cb, void *progress_arg)
9271 const struct got_error *err = NULL;
9272 char *path_unstaged_content = NULL;
9273 char *path_new_staged_content = NULL;
9274 char *parent = NULL, *base_path = NULL;
9275 char *blob_base_path = NULL;
9276 struct got_object_id *new_staged_blob_id = NULL;
9277 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
9278 struct stat sb;
9280 err = create_unstaged_content(&path_unstaged_content,
9281 &path_new_staged_content, blob_id, staged_blob_id,
9282 ie->path, repo, patch_cb, patch_arg);
9283 if (err)
9284 return err;
9286 if (path_unstaged_content == NULL)
9287 return NULL;
9289 if (path_new_staged_content) {
9290 err = got_object_blob_create(&new_staged_blob_id,
9291 path_new_staged_content, repo);
9292 if (err)
9293 goto done;
9296 f = fopen(path_unstaged_content, "re");
9297 if (f == NULL) {
9298 err = got_error_from_errno2("fopen",
9299 path_unstaged_content);
9300 goto done;
9302 if (fstat(fileno(f), &sb) == -1) {
9303 err = got_error_from_errno2("fstat", path_unstaged_content);
9304 goto done;
9306 if (got_fileindex_entry_staged_filetype_get(ie) ==
9307 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
9308 char link_target[PATH_MAX];
9309 size_t r;
9310 r = fread(link_target, 1, sizeof(link_target), f);
9311 if (r == 0 && ferror(f)) {
9312 err = got_error_from_errno("fread");
9313 goto done;
9315 if (r >= sizeof(link_target)) { /* should not happen */
9316 err = got_error(GOT_ERR_NO_SPACE);
9317 goto done;
9319 link_target[r] = '\0';
9320 err = merge_symlink(worktree, blob_base,
9321 ondisk_path, ie->path, label_orig, link_target,
9322 worktree->base_commit_id, repo, progress_cb,
9323 progress_arg);
9324 } else {
9325 int local_changes_subsumed;
9327 err = got_path_dirname(&parent, ondisk_path);
9328 if (err)
9329 return err;
9331 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
9332 parent) == -1) {
9333 err = got_error_from_errno("asprintf");
9334 base_path = NULL;
9335 goto done;
9338 err = got_opentemp_named(&blob_base_path, &f_base,
9339 base_path, "");
9340 if (err)
9341 goto done;
9342 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
9343 blob_base);
9344 if (err)
9345 goto done;
9348 * In order the run a 3-way merge with a symlink we copy the symlink's
9349 * target path into a temporary file and use that file with diff3.
9351 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9352 err = dump_symlink_target_path_to_file(&f_deriv2,
9353 ondisk_path);
9354 if (err)
9355 goto done;
9356 } else {
9357 int fd;
9358 fd = open(ondisk_path,
9359 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
9360 if (fd == -1) {
9361 err = got_error_from_errno2("open", ondisk_path);
9362 goto done;
9364 f_deriv2 = fdopen(fd, "r");
9365 if (f_deriv2 == NULL) {
9366 err = got_error_from_errno2("fdopen", ondisk_path);
9367 close(fd);
9368 goto done;
9372 err = merge_file(&local_changes_subsumed, worktree,
9373 f_base, f, f_deriv2, ondisk_path, ie->path,
9374 got_fileindex_perms_to_st(ie),
9375 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
9376 repo, progress_cb, progress_arg);
9378 if (err)
9379 goto done;
9381 if (new_staged_blob_id) {
9382 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9383 SHA1_DIGEST_LENGTH);
9384 } else {
9385 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9386 got_fileindex_entry_staged_filetype_set(ie, 0);
9388 done:
9389 free(new_staged_blob_id);
9390 if (path_unstaged_content &&
9391 unlink(path_unstaged_content) == -1 && err == NULL)
9392 err = got_error_from_errno2("unlink", path_unstaged_content);
9393 if (path_new_staged_content &&
9394 unlink(path_new_staged_content) == -1 && err == NULL)
9395 err = got_error_from_errno2("unlink", path_new_staged_content);
9396 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
9397 err = got_error_from_errno2("unlink", blob_base_path);
9398 if (f_base && fclose(f_base) == EOF && err == NULL)
9399 err = got_error_from_errno2("fclose", path_unstaged_content);
9400 if (f && fclose(f) == EOF && err == NULL)
9401 err = got_error_from_errno2("fclose", path_unstaged_content);
9402 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
9403 err = got_error_from_errno2("fclose", ondisk_path);
9404 free(path_unstaged_content);
9405 free(path_new_staged_content);
9406 free(blob_base_path);
9407 free(parent);
9408 free(base_path);
9409 return err;
9412 static const struct got_error *
9413 unstage_path(void *arg, unsigned char status,
9414 unsigned char staged_status, const char *relpath,
9415 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9416 struct got_object_id *commit_id, int dirfd, const char *de_name)
9418 const struct got_error *err = NULL;
9419 struct unstage_path_arg *a = arg;
9420 struct got_fileindex_entry *ie;
9421 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
9422 char *ondisk_path = NULL;
9423 char *id_str = NULL, *label_orig = NULL;
9424 int local_changes_subsumed;
9425 struct stat sb;
9426 int fd1 = -1, fd2 = -1;
9428 if (staged_status != GOT_STATUS_ADD &&
9429 staged_status != GOT_STATUS_MODIFY &&
9430 staged_status != GOT_STATUS_DELETE)
9431 return NULL;
9433 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9434 if (ie == NULL)
9435 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9437 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9438 == -1)
9439 return got_error_from_errno("asprintf");
9441 err = got_object_id_str(&id_str,
9442 commit_id ? commit_id : a->worktree->base_commit_id);
9443 if (err)
9444 goto done;
9445 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9446 id_str) == -1) {
9447 err = got_error_from_errno("asprintf");
9448 goto done;
9451 fd1 = got_opentempfd();
9452 if (fd1 == -1) {
9453 err = got_error_from_errno("got_opentempfd");
9454 goto done;
9456 fd2 = got_opentempfd();
9457 if (fd2 == -1) {
9458 err = got_error_from_errno("got_opentempfd");
9459 goto done;
9462 switch (staged_status) {
9463 case GOT_STATUS_MODIFY:
9464 err = got_object_open_as_blob(&blob_base, a->repo,
9465 blob_id, 8192, fd1);
9466 if (err)
9467 break;
9468 /* fall through */
9469 case GOT_STATUS_ADD:
9470 if (a->patch_cb) {
9471 if (staged_status == GOT_STATUS_ADD) {
9472 int choice = GOT_PATCH_CHOICE_NONE;
9473 err = (*a->patch_cb)(&choice, a->patch_arg,
9474 staged_status, ie->path, NULL, 1, 1);
9475 if (err)
9476 break;
9477 if (choice != GOT_PATCH_CHOICE_YES)
9478 break;
9479 } else {
9480 err = unstage_hunks(staged_blob_id,
9481 blob_base, blob_id, ie, ondisk_path,
9482 label_orig, a->worktree, a->repo,
9483 a->patch_cb, a->patch_arg,
9484 a->progress_cb, a->progress_arg);
9485 break; /* Done with this file. */
9488 err = got_object_open_as_blob(&blob_staged, a->repo,
9489 staged_blob_id, 8192, fd2);
9490 if (err)
9491 break;
9492 switch (got_fileindex_entry_staged_filetype_get(ie)) {
9493 case GOT_FILEIDX_MODE_BAD_SYMLINK:
9494 case GOT_FILEIDX_MODE_REGULAR_FILE:
9495 err = merge_blob(&local_changes_subsumed, a->worktree,
9496 blob_base, ondisk_path, relpath,
9497 got_fileindex_perms_to_st(ie), label_orig,
9498 blob_staged, commit_id ? commit_id :
9499 a->worktree->base_commit_id, a->repo,
9500 a->progress_cb, a->progress_arg);
9501 break;
9502 case GOT_FILEIDX_MODE_SYMLINK:
9503 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9504 char *staged_target;
9505 err = got_object_blob_read_to_str(
9506 &staged_target, blob_staged);
9507 if (err)
9508 goto done;
9509 err = merge_symlink(a->worktree, blob_base,
9510 ondisk_path, relpath, label_orig,
9511 staged_target, commit_id ? commit_id :
9512 a->worktree->base_commit_id,
9513 a->repo, a->progress_cb, a->progress_arg);
9514 free(staged_target);
9515 } else {
9516 err = merge_blob(&local_changes_subsumed,
9517 a->worktree, blob_base, ondisk_path,
9518 relpath, got_fileindex_perms_to_st(ie),
9519 label_orig, blob_staged,
9520 commit_id ? commit_id :
9521 a->worktree->base_commit_id, a->repo,
9522 a->progress_cb, a->progress_arg);
9524 break;
9525 default:
9526 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9527 break;
9529 if (err == NULL) {
9530 got_fileindex_entry_stage_set(ie,
9531 GOT_FILEIDX_STAGE_NONE);
9532 got_fileindex_entry_staged_filetype_set(ie, 0);
9534 break;
9535 case GOT_STATUS_DELETE:
9536 if (a->patch_cb) {
9537 int choice = GOT_PATCH_CHOICE_NONE;
9538 err = (*a->patch_cb)(&choice, a->patch_arg,
9539 staged_status, ie->path, NULL, 1, 1);
9540 if (err)
9541 break;
9542 if (choice == GOT_PATCH_CHOICE_NO)
9543 break;
9544 if (choice != GOT_PATCH_CHOICE_YES) {
9545 err = got_error(GOT_ERR_PATCH_CHOICE);
9546 break;
9549 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9550 got_fileindex_entry_staged_filetype_set(ie, 0);
9551 err = get_file_status(&status, &sb, ie, ondisk_path,
9552 dirfd, de_name, a->repo);
9553 if (err)
9554 break;
9555 err = (*a->progress_cb)(a->progress_arg, status, relpath);
9556 break;
9558 done:
9559 free(ondisk_path);
9560 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9561 err = got_error_from_errno("close");
9562 if (blob_base)
9563 got_object_blob_close(blob_base);
9564 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9565 err = got_error_from_errno("close");
9566 if (blob_staged)
9567 got_object_blob_close(blob_staged);
9568 free(id_str);
9569 free(label_orig);
9570 return err;
9573 const struct got_error *
9574 got_worktree_unstage(struct got_worktree *worktree,
9575 struct got_pathlist_head *paths,
9576 got_worktree_checkout_cb progress_cb, void *progress_arg,
9577 got_worktree_patch_cb patch_cb, void *patch_arg,
9578 struct got_repository *repo)
9580 const struct got_error *err = NULL, *sync_err, *unlockerr;
9581 struct got_pathlist_entry *pe;
9582 struct got_fileindex *fileindex = NULL;
9583 char *fileindex_path = NULL;
9584 struct unstage_path_arg upa;
9586 err = lock_worktree(worktree, LOCK_EX);
9587 if (err)
9588 return err;
9590 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9591 if (err)
9592 goto done;
9594 upa.worktree = worktree;
9595 upa.fileindex = fileindex;
9596 upa.repo = repo;
9597 upa.progress_cb = progress_cb;
9598 upa.progress_arg = progress_arg;
9599 upa.patch_cb = patch_cb;
9600 upa.patch_arg = patch_arg;
9601 TAILQ_FOREACH(pe, paths, entry) {
9602 err = worktree_status(worktree, pe->path, fileindex, repo,
9603 unstage_path, &upa, NULL, NULL, 1, 0);
9604 if (err)
9605 goto done;
9608 sync_err = sync_fileindex(fileindex, fileindex_path);
9609 if (sync_err && err == NULL)
9610 err = sync_err;
9611 done:
9612 free(fileindex_path);
9613 if (fileindex)
9614 got_fileindex_free(fileindex);
9615 unlockerr = lock_worktree(worktree, LOCK_SH);
9616 if (unlockerr && err == NULL)
9617 err = unlockerr;
9618 return err;
9621 struct report_file_info_arg {
9622 struct got_worktree *worktree;
9623 got_worktree_path_info_cb info_cb;
9624 void *info_arg;
9625 struct got_pathlist_head *paths;
9626 got_cancel_cb cancel_cb;
9627 void *cancel_arg;
9630 static const struct got_error *
9631 report_file_info(void *arg, struct got_fileindex_entry *ie)
9633 struct report_file_info_arg *a = arg;
9634 struct got_pathlist_entry *pe;
9635 struct got_object_id blob_id, staged_blob_id, commit_id;
9636 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9637 struct got_object_id *commit_idp = NULL;
9638 int stage;
9640 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9641 return got_error(GOT_ERR_CANCELLED);
9643 TAILQ_FOREACH(pe, a->paths, entry) {
9644 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9645 got_path_is_child(ie->path, pe->path, pe->path_len))
9646 break;
9648 if (pe == NULL) /* not found */
9649 return NULL;
9651 if (got_fileindex_entry_has_blob(ie))
9652 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9653 stage = got_fileindex_entry_stage_get(ie);
9654 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9655 stage == GOT_FILEIDX_STAGE_ADD) {
9656 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9657 &staged_blob_id, ie);
9660 if (got_fileindex_entry_has_commit(ie))
9661 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9663 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9664 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9667 const struct got_error *
9668 got_worktree_path_info(struct got_worktree *worktree,
9669 struct got_pathlist_head *paths,
9670 got_worktree_path_info_cb info_cb, void *info_arg,
9671 got_cancel_cb cancel_cb, void *cancel_arg)
9674 const struct got_error *err = NULL, *unlockerr;
9675 struct got_fileindex *fileindex = NULL;
9676 char *fileindex_path = NULL;
9677 struct report_file_info_arg arg;
9679 err = lock_worktree(worktree, LOCK_SH);
9680 if (err)
9681 return err;
9683 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9684 if (err)
9685 goto done;
9687 arg.worktree = worktree;
9688 arg.info_cb = info_cb;
9689 arg.info_arg = info_arg;
9690 arg.paths = paths;
9691 arg.cancel_cb = cancel_cb;
9692 arg.cancel_arg = cancel_arg;
9693 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9694 &arg);
9695 done:
9696 free(fileindex_path);
9697 if (fileindex)
9698 got_fileindex_free(fileindex);
9699 unlockerr = lock_worktree(worktree, LOCK_UN);
9700 if (unlockerr && err == NULL)
9701 err = unlockerr;
9702 return err;
9705 static const struct got_error *
9706 patch_check_path(const char *p, char **path, unsigned char *status,
9707 unsigned char *staged_status, struct got_fileindex *fileindex,
9708 struct got_worktree *worktree, struct got_repository *repo)
9710 const struct got_error *err;
9711 struct got_fileindex_entry *ie;
9712 struct stat sb;
9713 char *ondisk_path = NULL;
9715 err = got_worktree_resolve_path(path, worktree, p);
9716 if (err)
9717 return err;
9719 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9720 *path[0] ? "/" : "", *path) == -1)
9721 return got_error_from_errno("asprintf");
9723 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9724 if (ie) {
9725 *staged_status = get_staged_status(ie);
9726 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9727 repo);
9728 if (err)
9729 goto done;
9730 } else {
9731 *staged_status = GOT_STATUS_NO_CHANGE;
9732 *status = GOT_STATUS_UNVERSIONED;
9733 if (lstat(ondisk_path, &sb) == -1) {
9734 if (errno != ENOENT) {
9735 err = got_error_from_errno2("lstat",
9736 ondisk_path);
9737 goto done;
9739 *status = GOT_STATUS_NONEXISTENT;
9743 done:
9744 free(ondisk_path);
9745 return err;
9748 static const struct got_error *
9749 patch_can_rm(const char *path, unsigned char status,
9750 unsigned char staged_status)
9752 if (status == GOT_STATUS_NONEXISTENT)
9753 return got_error_set_errno(ENOENT, path);
9754 if (status != GOT_STATUS_NO_CHANGE &&
9755 status != GOT_STATUS_ADD &&
9756 status != GOT_STATUS_MODIFY &&
9757 status != GOT_STATUS_MODE_CHANGE)
9758 return got_error_path(path, GOT_ERR_FILE_STATUS);
9759 if (staged_status == GOT_STATUS_DELETE)
9760 return got_error_path(path, GOT_ERR_FILE_STATUS);
9761 return NULL;
9764 static const struct got_error *
9765 patch_can_add(const char *path, unsigned char status)
9767 if (status != GOT_STATUS_NONEXISTENT)
9768 return got_error_path(path, GOT_ERR_FILE_STATUS);
9769 return NULL;
9772 static const struct got_error *
9773 patch_can_edit(const char *path, unsigned char status,
9774 unsigned char staged_status)
9776 if (status == GOT_STATUS_NONEXISTENT)
9777 return got_error_set_errno(ENOENT, path);
9778 if (status != GOT_STATUS_NO_CHANGE &&
9779 status != GOT_STATUS_ADD &&
9780 status != GOT_STATUS_MODIFY)
9781 return got_error_path(path, GOT_ERR_FILE_STATUS);
9782 if (staged_status == GOT_STATUS_DELETE)
9783 return got_error_path(path, GOT_ERR_FILE_STATUS);
9784 return NULL;
9787 const struct got_error *
9788 got_worktree_patch_prepare(struct got_fileindex **fileindex,
9789 char **fileindex_path, struct got_worktree *worktree)
9791 return open_fileindex(fileindex, fileindex_path, worktree);
9794 const struct got_error *
9795 got_worktree_patch_check_path(const char *old, const char *new,
9796 char **oldpath, char **newpath, struct got_worktree *worktree,
9797 struct got_repository *repo, struct got_fileindex *fileindex)
9799 const struct got_error *err = NULL;
9800 int file_renamed = 0;
9801 unsigned char status_old, staged_status_old;
9802 unsigned char status_new, staged_status_new;
9804 *oldpath = NULL;
9805 *newpath = NULL;
9807 err = patch_check_path(old != NULL ? old : new, oldpath,
9808 &status_old, &staged_status_old, fileindex, worktree, repo);
9809 if (err)
9810 goto done;
9812 err = patch_check_path(new != NULL ? new : old, newpath,
9813 &status_new, &staged_status_new, fileindex, worktree, repo);
9814 if (err)
9815 goto done;
9817 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9818 file_renamed = 1;
9820 if (old != NULL && new == NULL)
9821 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9822 else if (file_renamed) {
9823 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9824 if (err == NULL)
9825 err = patch_can_add(*newpath, status_new);
9826 } else if (old == NULL)
9827 err = patch_can_add(*newpath, status_new);
9828 else
9829 err = patch_can_edit(*newpath, status_new, staged_status_new);
9831 done:
9832 if (err) {
9833 free(*oldpath);
9834 *oldpath = NULL;
9835 free(*newpath);
9836 *newpath = NULL;
9838 return err;
9841 const struct got_error *
9842 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9843 struct got_worktree *worktree, struct got_fileindex *fileindex,
9844 got_worktree_checkout_cb progress_cb, void *progress_arg)
9846 struct schedule_addition_args saa;
9848 memset(&saa, 0, sizeof(saa));
9849 saa.worktree = worktree;
9850 saa.fileindex = fileindex;
9851 saa.progress_cb = progress_cb;
9852 saa.progress_arg = progress_arg;
9853 saa.repo = repo;
9855 return worktree_status(worktree, path, fileindex, repo,
9856 schedule_addition, &saa, NULL, NULL, 1, 0);
9859 const struct got_error *
9860 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9861 struct got_worktree *worktree, struct got_fileindex *fileindex,
9862 got_worktree_delete_cb progress_cb, void *progress_arg)
9864 const struct got_error *err;
9865 struct schedule_deletion_args sda;
9866 char *ondisk_status_path;
9868 memset(&sda, 0, sizeof(sda));
9869 sda.worktree = worktree;
9870 sda.fileindex = fileindex;
9871 sda.progress_cb = progress_cb;
9872 sda.progress_arg = progress_arg;
9873 sda.repo = repo;
9874 sda.delete_local_mods = 0;
9875 sda.keep_on_disk = 0;
9876 sda.ignore_missing_paths = 0;
9877 sda.status_codes = NULL;
9878 if (asprintf(&ondisk_status_path, "%s/%s",
9879 got_worktree_get_root_path(worktree), path) == -1)
9880 return got_error_from_errno("asprintf");
9881 sda.status_path = ondisk_status_path;
9882 sda.status_path_len = strlen(ondisk_status_path);
9884 err = worktree_status(worktree, path, fileindex, repo,
9885 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9886 free(ondisk_status_path);
9887 return err;
9890 const struct got_error *
9891 got_worktree_patch_complete(struct got_fileindex *fileindex,
9892 const char *fileindex_path)
9894 const struct got_error *err = NULL;
9896 err = sync_fileindex(fileindex, fileindex_path);
9897 got_fileindex_free(fileindex);
9899 return err;