Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/stat.h>
18 #include <sys/queue.h>
19 #include <sys/tree.h>
21 #include <dirent.h>
22 #include <limits.h>
23 #include <stddef.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <time.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <sha1.h>
32 #include <sha2.h>
33 #include <zlib.h>
34 #include <fnmatch.h>
35 #include <libgen.h>
36 #include <uuid.h>
37 #include <util.h>
39 #include "got_error.h"
40 #include "got_repository.h"
41 #include "got_reference.h"
42 #include "got_object.h"
43 #include "got_path.h"
44 #include "got_cancel.h"
45 #include "got_worktree.h"
46 #include "got_opentemp.h"
47 #include "got_diff.h"
49 #include "got_lib_worktree.h"
50 #include "got_lib_hash.h"
51 #include "got_lib_fileindex.h"
52 #include "got_lib_inflate.h"
53 #include "got_lib_delta.h"
54 #include "got_lib_object.h"
55 #include "got_lib_object_parse.h"
56 #include "got_lib_object_create.h"
57 #include "got_lib_object_idset.h"
58 #include "got_lib_diff.h"
59 #include "got_lib_gotconfig.h"
61 #ifndef MIN
62 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
63 #endif
65 #define GOT_MERGE_LABEL_MERGED "merged change"
66 #define GOT_MERGE_LABEL_BASE "3-way merge base"
68 static mode_t apply_umask(mode_t);
70 static const struct got_error *
71 create_meta_file(const char *path_got, const char *name, const char *content)
72 {
73 const struct got_error *err = NULL;
74 char *path;
76 if (asprintf(&path, "%s/%s", path_got, name) == -1)
77 return got_error_from_errno("asprintf");
79 err = got_path_create_file(path, content);
80 free(path);
81 return err;
82 }
84 static const struct got_error *
85 update_meta_file(const char *path_got, const char *name, const char *content)
86 {
87 const struct got_error *err = NULL;
88 FILE *tmpfile = NULL;
89 char *tmppath = NULL;
90 char *path = NULL;
92 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
93 err = got_error_from_errno("asprintf");
94 path = NULL;
95 goto done;
96 }
98 err = got_opentemp_named(&tmppath, &tmpfile, path, "");
99 if (err)
100 goto done;
102 if (content) {
103 int len = fprintf(tmpfile, "%s\n", content);
104 if (len != strlen(content) + 1) {
105 err = got_error_from_errno2("fprintf", tmppath);
106 goto done;
110 if (rename(tmppath, path) != 0) {
111 err = got_error_from_errno3("rename", tmppath, path);
112 unlink(tmppath);
113 goto done;
116 done:
117 if (fclose(tmpfile) == EOF && err == NULL)
118 err = got_error_from_errno2("fclose", tmppath);
119 free(tmppath);
120 return err;
123 static const struct got_error *
124 write_head_ref(const char *path_got, struct got_reference *head_ref)
126 const struct got_error *err = NULL;
127 char *refstr = NULL;
129 if (got_ref_is_symbolic(head_ref)) {
130 refstr = got_ref_to_str(head_ref);
131 if (refstr == NULL)
132 return got_error_from_errno("got_ref_to_str");
133 } else {
134 refstr = strdup(got_ref_get_name(head_ref));
135 if (refstr == NULL)
136 return got_error_from_errno("strdup");
138 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
139 free(refstr);
140 return err;
143 const struct got_error *
144 got_worktree_init(const char *path, struct got_reference *head_ref,
145 const char *prefix, struct got_repository *repo)
147 const struct got_error *err = NULL;
148 struct got_object_id *commit_id = NULL;
149 uuid_t uuid;
150 uint32_t uuid_status;
151 int obj_type;
152 char *path_got = NULL;
153 char *formatstr = NULL;
154 char *absprefix = NULL;
155 char *basestr = NULL;
156 char *uuidstr = NULL;
158 if (strcmp(path, got_repo_get_path(repo)) == 0) {
159 err = got_error(GOT_ERR_WORKTREE_REPO);
160 goto done;
163 err = got_ref_resolve(&commit_id, repo, head_ref);
164 if (err)
165 return err;
166 err = got_object_get_type(&obj_type, repo, commit_id);
167 if (err)
168 return err;
169 if (obj_type != GOT_OBJ_TYPE_COMMIT)
170 return got_error(GOT_ERR_OBJ_TYPE);
172 if (!got_path_is_absolute(prefix)) {
173 if (asprintf(&absprefix, "/%s", prefix) == -1)
174 return got_error_from_errno("asprintf");
177 /* Create top-level directory (may already exist). */
178 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
179 err = got_error_from_errno2("mkdir", path);
180 goto done;
183 /* Create .got directory (may already exist). */
184 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
185 err = got_error_from_errno("asprintf");
186 goto done;
188 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
189 err = got_error_from_errno2("mkdir", path_got);
190 goto done;
193 /* Create an empty lock file. */
194 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
195 if (err)
196 goto done;
198 /* Create an empty file index. */
199 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
200 if (err)
201 goto done;
203 /* Write the HEAD reference. */
204 err = write_head_ref(path_got, head_ref);
205 if (err)
206 goto done;
208 /* Record our base commit. */
209 err = got_object_id_str(&basestr, commit_id);
210 if (err)
211 goto done;
212 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
213 if (err)
214 goto done;
216 /* Store path to repository. */
217 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
218 got_repo_get_path(repo));
219 if (err)
220 goto done;
222 /* Store in-repository path prefix. */
223 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
224 absprefix ? absprefix : prefix);
225 if (err)
226 goto done;
228 /* Generate UUID. */
229 uuid_create(&uuid, &uuid_status);
230 if (uuid_status != uuid_s_ok) {
231 err = got_error_uuid(uuid_status, "uuid_create");
232 goto done;
234 uuid_to_string(&uuid, &uuidstr, &uuid_status);
235 if (uuid_status != uuid_s_ok) {
236 err = got_error_uuid(uuid_status, "uuid_to_string");
237 goto done;
239 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
240 if (err)
241 goto done;
243 /* Stamp work tree with format file. */
244 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
245 err = got_error_from_errno("asprintf");
246 goto done;
248 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
249 if (err)
250 goto done;
252 done:
253 free(commit_id);
254 free(path_got);
255 free(formatstr);
256 free(absprefix);
257 free(basestr);
258 free(uuidstr);
259 return err;
262 const struct got_error *
263 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
264 const char *path_prefix)
266 char *absprefix = NULL;
268 if (!got_path_is_absolute(path_prefix)) {
269 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
270 return got_error_from_errno("asprintf");
272 *match = (strcmp(absprefix ? absprefix : path_prefix,
273 worktree->path_prefix) == 0);
274 free(absprefix);
275 return NULL;
278 const char *
279 got_worktree_get_head_ref_name(struct got_worktree *worktree)
281 return worktree->head_ref_name;
284 const struct got_error *
285 got_worktree_set_head_ref(struct got_worktree *worktree,
286 struct got_reference *head_ref)
288 const struct got_error *err = NULL;
289 char *path_got = NULL, *head_ref_name = NULL;
291 if (asprintf(&path_got, "%s/%s", worktree->root_path,
292 GOT_WORKTREE_GOT_DIR) == -1) {
293 err = got_error_from_errno("asprintf");
294 path_got = NULL;
295 goto done;
298 head_ref_name = strdup(got_ref_get_name(head_ref));
299 if (head_ref_name == NULL) {
300 err = got_error_from_errno("strdup");
301 goto done;
304 err = write_head_ref(path_got, head_ref);
305 if (err)
306 goto done;
308 free(worktree->head_ref_name);
309 worktree->head_ref_name = head_ref_name;
310 done:
311 free(path_got);
312 if (err)
313 free(head_ref_name);
314 return err;
317 struct got_object_id *
318 got_worktree_get_base_commit_id(struct got_worktree *worktree)
320 return worktree->base_commit_id;
323 const struct got_error *
324 got_worktree_set_base_commit_id(struct got_worktree *worktree,
325 struct got_repository *repo, struct got_object_id *commit_id)
327 const struct got_error *err;
328 struct got_object *obj = NULL;
329 char *id_str = NULL;
330 char *path_got = NULL;
332 if (asprintf(&path_got, "%s/%s", worktree->root_path,
333 GOT_WORKTREE_GOT_DIR) == -1) {
334 err = got_error_from_errno("asprintf");
335 path_got = NULL;
336 goto done;
339 err = got_object_open(&obj, repo, commit_id);
340 if (err)
341 return err;
343 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
344 err = got_error(GOT_ERR_OBJ_TYPE);
345 goto done;
348 /* Record our base commit. */
349 err = got_object_id_str(&id_str, commit_id);
350 if (err)
351 goto done;
352 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
353 if (err)
354 goto done;
356 free(worktree->base_commit_id);
357 worktree->base_commit_id = got_object_id_dup(commit_id);
358 if (worktree->base_commit_id == NULL) {
359 err = got_error_from_errno("got_object_id_dup");
360 goto done;
362 done:
363 if (obj)
364 got_object_close(obj);
365 free(id_str);
366 free(path_got);
367 return err;
370 const struct got_gotconfig *
371 got_worktree_get_gotconfig(struct got_worktree *worktree)
373 return worktree->gotconfig;
376 static const struct got_error *
377 lock_worktree(struct got_worktree *worktree, int operation)
379 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
380 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
381 : got_error_from_errno2("flock",
382 got_worktree_get_root_path(worktree)));
383 return NULL;
386 static const struct got_error *
387 add_dir_on_disk(struct got_worktree *worktree, const char *path)
389 const struct got_error *err = NULL;
390 char *abspath;
392 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
393 return got_error_from_errno("asprintf");
395 err = got_path_mkdir(abspath);
396 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
397 struct stat sb;
398 err = NULL;
399 if (lstat(abspath, &sb) == -1) {
400 err = got_error_from_errno2("lstat", abspath);
401 } else if (!S_ISDIR(sb.st_mode)) {
402 /* TODO directory is obstructed; do something */
403 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
406 free(abspath);
407 return err;
410 static const struct got_error *
411 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
413 const struct got_error *err = NULL;
414 uint8_t fbuf1[8192];
415 uint8_t fbuf2[8192];
416 size_t flen1 = 0, flen2 = 0;
418 *same = 1;
420 for (;;) {
421 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
422 if (flen1 == 0 && ferror(f1)) {
423 err = got_error_from_errno("fread");
424 break;
426 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
427 if (flen2 == 0 && ferror(f2)) {
428 err = got_error_from_errno("fread");
429 break;
431 if (flen1 == 0) {
432 if (flen2 != 0)
433 *same = 0;
434 break;
435 } else if (flen2 == 0) {
436 if (flen1 != 0)
437 *same = 0;
438 break;
439 } else if (flen1 == flen2) {
440 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
441 *same = 0;
442 break;
444 } else {
445 *same = 0;
446 break;
450 return err;
453 static const struct got_error *
454 check_files_equal(int *same, FILE *f1, FILE *f2)
456 struct stat sb;
457 size_t size1, size2;
459 *same = 1;
461 if (fstat(fileno(f1), &sb) != 0)
462 return got_error_from_errno("fstat");
463 size1 = sb.st_size;
465 if (fstat(fileno(f2), &sb) != 0)
466 return got_error_from_errno("fstat");
467 size2 = sb.st_size;
469 if (size1 != size2) {
470 *same = 0;
471 return NULL;
474 if (fseek(f1, 0L, SEEK_SET) == -1)
475 return got_ferror(f1, GOT_ERR_IO);
476 if (fseek(f2, 0L, SEEK_SET) == -1)
477 return got_ferror(f2, GOT_ERR_IO);
479 return check_file_contents_equal(same, f1, f2);
482 static const struct got_error *
483 copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
485 uint8_t fbuf[65536];
486 size_t flen;
487 ssize_t outlen;
489 *outsize = 0;
491 if (fseek(f, 0L, SEEK_SET) == -1)
492 return got_ferror(f, GOT_ERR_IO);
494 for (;;) {
495 flen = fread(fbuf, 1, sizeof(fbuf), f);
496 if (flen == 0) {
497 if (ferror(f))
498 return got_error_from_errno("fread");
499 if (feof(f))
500 break;
502 outlen = write(outfd, fbuf, flen);
503 if (outlen == -1)
504 return got_error_from_errno("write");
505 if (outlen != flen)
506 return got_error(GOT_ERR_IO);
507 *outsize += outlen;
510 return NULL;
513 static const struct got_error *
514 merge_binary_file(int *overlapcnt, int merged_fd,
515 FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
516 const char *label_deriv, const char *label_orig, const char *label_deriv2,
517 const char *ondisk_path)
519 const struct got_error *err = NULL;
520 int same_content, changed_deriv, changed_deriv2;
521 int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
522 off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
523 char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
524 char *base_path_orig = NULL, *base_path_deriv = NULL;
525 char *base_path_deriv2 = NULL;
527 *overlapcnt = 0;
529 err = check_files_equal(&same_content, f_deriv, f_deriv2);
530 if (err)
531 return err;
533 if (same_content)
534 return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
536 err = check_files_equal(&same_content, f_deriv, f_orig);
537 if (err)
538 return err;
539 changed_deriv = !same_content;
540 err = check_files_equal(&same_content, f_deriv2, f_orig);
541 if (err)
542 return err;
543 changed_deriv2 = !same_content;
545 if (changed_deriv && changed_deriv2) {
546 *overlapcnt = 1;
547 if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
548 err = got_error_from_errno("asprintf");
549 goto done;
551 if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
552 err = got_error_from_errno("asprintf");
553 goto done;
555 if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
556 err = got_error_from_errno("asprintf");
557 goto done;
559 err = got_opentemp_named_fd(&path_orig, &fd_orig,
560 base_path_orig, "");
561 if (err)
562 goto done;
563 err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
564 base_path_deriv, "");
565 if (err)
566 goto done;
567 err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
568 base_path_deriv2, "");
569 if (err)
570 goto done;
571 err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
572 if (err)
573 goto done;
574 err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
575 if (err)
576 goto done;
577 err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
578 if (err)
579 goto done;
580 if (dprintf(merged_fd, "Binary files differ and cannot be "
581 "merged automatically:\n") < 0) {
582 err = got_error_from_errno("dprintf");
583 goto done;
585 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
586 GOT_DIFF_CONFLICT_MARKER_BEGIN,
587 label_deriv ? " " : "",
588 label_deriv ? label_deriv : "",
589 path_deriv) < 0) {
590 err = got_error_from_errno("dprintf");
591 goto done;
593 if (size_orig > 0) {
594 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
595 GOT_DIFF_CONFLICT_MARKER_ORIG,
596 label_orig ? " " : "",
597 label_orig ? label_orig : "",
598 path_orig) < 0) {
599 err = got_error_from_errno("dprintf");
600 goto done;
603 if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
604 GOT_DIFF_CONFLICT_MARKER_SEP,
605 path_deriv2,
606 GOT_DIFF_CONFLICT_MARKER_END,
607 label_deriv2 ? " " : "",
608 label_deriv2 ? label_deriv2 : "") < 0) {
609 err = got_error_from_errno("dprintf");
610 goto done;
612 } else if (changed_deriv)
613 err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
614 else if (changed_deriv2)
615 err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
616 done:
617 if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
618 err == NULL)
619 err = got_error_from_errno2("unlink", path_orig);
620 if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
621 err = got_error_from_errno2("close", path_orig);
622 if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
623 err = got_error_from_errno2("close", path_deriv);
624 if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
625 err = got_error_from_errno2("close", path_deriv2);
626 free(path_orig);
627 free(path_deriv);
628 free(path_deriv2);
629 free(base_path_orig);
630 free(base_path_deriv);
631 free(base_path_deriv2);
632 return err;
635 /*
636 * Perform a 3-way merge where the file f_orig acts as the common
637 * ancestor, the file f_deriv acts as the first derived version,
638 * and the file f_deriv2 acts as the second derived version.
639 * The merge result will be written to a new file at ondisk_path; any
640 * existing file at this path will be replaced.
641 */
642 static const struct got_error *
643 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
644 FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
645 const char *path, uint16_t st_mode,
646 const char *label_orig, const char *label_deriv, const char *label_deriv2,
647 enum got_diff_algorithm diff_algo, struct got_repository *repo,
648 got_worktree_checkout_cb progress_cb, void *progress_arg)
650 const struct got_error *err = NULL;
651 int merged_fd = -1;
652 FILE *f_merged = NULL;
653 char *merged_path = NULL, *base_path = NULL;
654 int overlapcnt = 0;
655 char *parent = NULL;
657 *local_changes_subsumed = 0;
659 err = got_path_dirname(&parent, ondisk_path);
660 if (err)
661 return err;
663 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
664 err = got_error_from_errno("asprintf");
665 goto done;
668 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path, "");
669 if (err)
670 goto done;
672 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
673 f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
674 if (err) {
675 if (err->code != GOT_ERR_FILE_BINARY)
676 goto done;
677 err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
678 f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
679 ondisk_path);
680 if (err)
681 goto done;
684 err = (*progress_cb)(progress_arg,
685 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
686 if (err)
687 goto done;
689 if (fsync(merged_fd) != 0) {
690 err = got_error_from_errno("fsync");
691 goto done;
694 f_merged = fdopen(merged_fd, "r");
695 if (f_merged == NULL) {
696 err = got_error_from_errno("fdopen");
697 goto done;
699 merged_fd = -1;
701 /* Check if a clean merge has subsumed all local changes. */
702 if (overlapcnt == 0) {
703 err = check_files_equal(local_changes_subsumed, f_deriv,
704 f_merged);
705 if (err)
706 goto done;
709 if (fchmod(fileno(f_merged), apply_umask(st_mode)) != 0) {
710 err = got_error_from_errno2("fchmod", merged_path);
711 goto done;
714 if (rename(merged_path, ondisk_path) != 0) {
715 err = got_error_from_errno3("rename", merged_path,
716 ondisk_path);
717 goto done;
719 done:
720 if (err) {
721 if (merged_path)
722 unlink(merged_path);
724 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
725 err = got_error_from_errno("close");
726 if (f_merged && fclose(f_merged) == EOF && err == NULL)
727 err = got_error_from_errno("fclose");
728 free(merged_path);
729 free(base_path);
730 free(parent);
731 return err;
734 static const struct got_error *
735 update_symlink(const char *ondisk_path, const char *target_path,
736 size_t target_len)
738 /* This is not atomic but matches what 'ln -sf' does. */
739 if (unlink(ondisk_path) == -1)
740 return got_error_from_errno2("unlink", ondisk_path);
741 if (symlink(target_path, ondisk_path) == -1)
742 return got_error_from_errno3("symlink", target_path,
743 ondisk_path);
744 return NULL;
747 /*
748 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
749 * in the work tree with a file that contains conflict markers and the
750 * conflicting target paths of the original version, a "derived version"
751 * of a symlink from an incoming change, and a local version of the symlink.
753 * The original versions's target path can be NULL if it is not available,
754 * such as if both derived versions added a new symlink at the same path.
756 * The incoming derived symlink target is NULL in case the incoming change
757 * has deleted this symlink.
758 */
759 static const struct got_error *
760 install_symlink_conflict(const char *deriv_target,
761 struct got_object_id *deriv_base_commit_id, const char *orig_target,
762 const char *label_orig, const char *local_target, const char *ondisk_path)
764 const struct got_error *err;
765 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
766 FILE *f = NULL;
768 err = got_object_id_str(&id_str, deriv_base_commit_id);
769 if (err)
770 return got_error_from_errno("asprintf");
772 if (asprintf(&label_deriv, "%s: commit %s",
773 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
774 err = got_error_from_errno("asprintf");
775 goto done;
778 err = got_opentemp_named(&path, &f, "got-symlink-conflict", "");
779 if (err)
780 goto done;
782 if (fchmod(fileno(f), apply_umask(GOT_DEFAULT_FILE_MODE)) == -1) {
783 err = got_error_from_errno2("fchmod", path);
784 goto done;
787 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
788 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
789 deriv_target ? deriv_target : "(symlink was deleted)",
790 orig_target ? label_orig : "",
791 orig_target ? "\n" : "",
792 orig_target ? orig_target : "",
793 orig_target ? "\n" : "",
794 GOT_DIFF_CONFLICT_MARKER_SEP,
795 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
796 err = got_error_from_errno2("fprintf", path);
797 goto done;
800 if (unlink(ondisk_path) == -1) {
801 err = got_error_from_errno2("unlink", ondisk_path);
802 goto done;
804 if (rename(path, ondisk_path) == -1) {
805 err = got_error_from_errno3("rename", path, ondisk_path);
806 goto done;
808 done:
809 if (f != NULL && fclose(f) == EOF && err == NULL)
810 err = got_error_from_errno2("fclose", path);
811 free(path);
812 free(id_str);
813 free(label_deriv);
814 return err;
817 /* forward declaration */
818 static const struct got_error *
819 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
820 const char *, const char *, uint16_t, const char *,
821 struct got_blob_object *, struct got_object_id *,
822 struct got_repository *, got_worktree_checkout_cb, void *);
824 /*
825 * Merge a symlink into the work tree, where blob_orig acts as the common
826 * ancestor, deriv_target is the link target of the first derived version,
827 * and the symlink on disk acts as the second derived version.
828 * Assume that contents of both blobs represent symlinks.
829 */
830 static const struct got_error *
831 merge_symlink(struct got_worktree *worktree,
832 struct got_blob_object *blob_orig, const char *ondisk_path,
833 const char *path, const char *label_orig, const char *deriv_target,
834 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
835 got_worktree_checkout_cb progress_cb, void *progress_arg)
837 const struct got_error *err = NULL;
838 char *ancestor_target = NULL;
839 struct stat sb;
840 ssize_t ondisk_len, deriv_len;
841 char ondisk_target[PATH_MAX];
842 int have_local_change = 0;
843 int have_incoming_change = 0;
845 if (lstat(ondisk_path, &sb) == -1)
846 return got_error_from_errno2("lstat", ondisk_path);
848 ondisk_len = readlink(ondisk_path, ondisk_target,
849 sizeof(ondisk_target));
850 if (ondisk_len == -1) {
851 err = got_error_from_errno2("readlink",
852 ondisk_path);
853 goto done;
855 ondisk_target[ondisk_len] = '\0';
857 if (blob_orig) {
858 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
859 if (err)
860 goto done;
863 if (ancestor_target == NULL ||
864 (ondisk_len != strlen(ancestor_target) ||
865 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
866 have_local_change = 1;
868 deriv_len = strlen(deriv_target);
869 if (ancestor_target == NULL ||
870 (deriv_len != strlen(ancestor_target) ||
871 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
872 have_incoming_change = 1;
874 if (!have_local_change && !have_incoming_change) {
875 if (ancestor_target) {
876 /* Both sides made the same change. */
877 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
878 path);
879 } else if (deriv_len == ondisk_len &&
880 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
881 /* Both sides added the same symlink. */
882 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
883 path);
884 } else {
885 /* Both sides added symlinks which don't match. */
886 err = install_symlink_conflict(deriv_target,
887 deriv_base_commit_id, ancestor_target,
888 label_orig, ondisk_target, ondisk_path);
889 if (err)
890 goto done;
891 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
892 path);
894 } else if (!have_local_change && have_incoming_change) {
895 /* Apply the incoming change. */
896 err = update_symlink(ondisk_path, deriv_target,
897 strlen(deriv_target));
898 if (err)
899 goto done;
900 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
901 } else if (have_local_change && have_incoming_change) {
902 if (deriv_len == ondisk_len &&
903 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
904 /* Both sides made the same change. */
905 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
906 path);
907 } else {
908 err = install_symlink_conflict(deriv_target,
909 deriv_base_commit_id, ancestor_target, label_orig,
910 ondisk_target, ondisk_path);
911 if (err)
912 goto done;
913 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
914 path);
918 done:
919 free(ancestor_target);
920 return err;
923 static const struct got_error *
924 dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
926 const struct got_error *err = NULL;
927 char target_path[PATH_MAX];
928 ssize_t target_len;
929 size_t n;
930 FILE *f;
932 *outfile = NULL;
934 f = got_opentemp();
935 if (f == NULL)
936 return got_error_from_errno("got_opentemp");
937 target_len = readlink(ondisk_path, target_path, sizeof(target_path));
938 if (target_len == -1) {
939 err = got_error_from_errno2("readlink", ondisk_path);
940 goto done;
942 n = fwrite(target_path, 1, target_len, f);
943 if (n != target_len) {
944 err = got_ferror(f, GOT_ERR_IO);
945 goto done;
947 if (fflush(f) == EOF) {
948 err = got_error_from_errno("fflush");
949 goto done;
951 if (fseek(f, 0L, SEEK_SET) == -1) {
952 err = got_ferror(f, GOT_ERR_IO);
953 goto done;
955 done:
956 if (err)
957 fclose(f);
958 else
959 *outfile = f;
960 return err;
963 /*
964 * Perform a 3-way merge where blob_orig acts as the common ancestor,
965 * blob_deriv acts as the first derived version, and the file on disk
966 * acts as the second derived version.
967 */
968 static const struct got_error *
969 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
970 struct got_blob_object *blob_orig, const char *ondisk_path,
971 const char *path, uint16_t st_mode, const char *label_orig,
972 struct got_blob_object *blob_deriv,
973 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
974 got_worktree_checkout_cb progress_cb, void *progress_arg)
976 const struct got_error *err = NULL;
977 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
978 char *blob_orig_path = NULL;
979 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
980 char *label_deriv = NULL, *parent = NULL;
982 *local_changes_subsumed = 0;
984 err = got_path_dirname(&parent, ondisk_path);
985 if (err)
986 return err;
988 if (blob_orig) {
989 if (asprintf(&base_path, "%s/got-merge-blob-orig",
990 parent) == -1) {
991 err = got_error_from_errno("asprintf");
992 base_path = NULL;
993 goto done;
996 err = got_opentemp_named(&blob_orig_path, &f_orig,
997 base_path, "");
998 if (err)
999 goto done;
1000 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1001 blob_orig);
1002 if (err)
1003 goto done;
1004 free(base_path);
1005 } else {
1007 * No common ancestor exists. This is an "add vs add" conflict
1008 * and we simply use an empty ancestor file to make both files
1009 * appear in the merged result in their entirety.
1011 f_orig = got_opentemp();
1012 if (f_orig == NULL) {
1013 err = got_error_from_errno("got_opentemp");
1014 goto done;
1018 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1019 err = got_error_from_errno("asprintf");
1020 base_path = NULL;
1021 goto done;
1024 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path, "");
1025 if (err)
1026 goto done;
1027 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1028 blob_deriv);
1029 if (err)
1030 goto done;
1032 err = got_object_id_str(&id_str, deriv_base_commit_id);
1033 if (err)
1034 goto done;
1035 if (asprintf(&label_deriv, "%s: commit %s",
1036 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1037 err = got_error_from_errno("asprintf");
1038 goto done;
1042 * In order the run a 3-way merge with a symlink we copy the symlink's
1043 * target path into a temporary file and use that file with diff3.
1045 if (S_ISLNK(st_mode)) {
1046 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1047 if (err)
1048 goto done;
1049 } else {
1050 int fd;
1051 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1052 if (fd == -1) {
1053 err = got_error_from_errno2("open", ondisk_path);
1054 goto done;
1056 f_deriv2 = fdopen(fd, "r");
1057 if (f_deriv2 == NULL) {
1058 err = got_error_from_errno2("fdopen", ondisk_path);
1059 close(fd);
1060 goto done;
1064 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1065 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1066 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1067 done:
1068 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1069 err = got_error_from_errno("fclose");
1070 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1071 err = got_error_from_errno("fclose");
1072 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1073 err = got_error_from_errno("fclose");
1074 free(base_path);
1075 if (blob_orig_path) {
1076 unlink(blob_orig_path);
1077 free(blob_orig_path);
1079 if (blob_deriv_path) {
1080 unlink(blob_deriv_path);
1081 free(blob_deriv_path);
1083 free(id_str);
1084 free(label_deriv);
1085 free(parent);
1086 return err;
1089 static const struct got_error *
1090 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1091 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1092 int wt_fd, const char *path, struct got_object_id *blob_id)
1094 const struct got_error *err = NULL;
1095 struct got_fileindex_entry *new_ie;
1097 *new_iep = NULL;
1099 err = got_fileindex_entry_alloc(&new_ie, path);
1100 if (err)
1101 return err;
1103 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1104 blob_id->sha1, base_commit_id->sha1, 1);
1105 if (err)
1106 goto done;
1108 err = got_fileindex_entry_add(fileindex, new_ie);
1109 done:
1110 if (err)
1111 got_fileindex_entry_free(new_ie);
1112 else
1113 *new_iep = new_ie;
1114 return err;
1117 static mode_t
1118 get_ondisk_perms(int executable, mode_t st_mode)
1120 mode_t xbits = S_IXUSR;
1122 if (executable) {
1123 /* Map read bits to execute bits. */
1124 if (st_mode & S_IRGRP)
1125 xbits |= S_IXGRP;
1126 if (st_mode & S_IROTH)
1127 xbits |= S_IXOTH;
1128 return st_mode | xbits;
1131 return st_mode;
1134 static mode_t
1135 apply_umask(mode_t mode)
1137 mode_t um;
1139 um = umask(000);
1140 umask(um);
1141 return mode & ~um;
1144 /* forward declaration */
1145 static const struct got_error *
1146 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1147 const char *path, mode_t te_mode, mode_t st_mode,
1148 struct got_blob_object *blob, int restoring_missing_file,
1149 int reverting_versioned_file, int installing_bad_symlink,
1150 int path_is_unversioned, struct got_repository *repo,
1151 got_worktree_checkout_cb progress_cb, void *progress_arg);
1154 * This function assumes that the provided symlink target points at a
1155 * safe location in the work tree!
1157 static const struct got_error *
1158 replace_existing_symlink(int *did_something, const char *ondisk_path,
1159 const char *target_path, size_t target_len)
1161 const struct got_error *err = NULL;
1162 ssize_t elen;
1163 char etarget[PATH_MAX];
1164 int fd;
1166 *did_something = 0;
1169 * "Bad" symlinks (those pointing outside the work tree or into the
1170 * .got directory) are installed in the work tree as a regular file
1171 * which contains the bad symlink target path.
1172 * The new symlink target has already been checked for safety by our
1173 * caller. If we can successfully open a regular file then we simply
1174 * replace this file with a symlink below.
1176 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1177 if (fd == -1) {
1178 if (!got_err_open_nofollow_on_symlink())
1179 return got_error_from_errno2("open", ondisk_path);
1181 /* We are updating an existing on-disk symlink. */
1182 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1183 if (elen == -1)
1184 return got_error_from_errno2("readlink", ondisk_path);
1186 if (elen == target_len &&
1187 memcmp(etarget, target_path, target_len) == 0)
1188 return NULL; /* nothing to do */
1191 *did_something = 1;
1192 err = update_symlink(ondisk_path, target_path, target_len);
1193 if (fd != -1 && close(fd) == -1 && err == NULL)
1194 err = got_error_from_errno2("close", ondisk_path);
1195 return err;
1198 static const struct got_error *
1199 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1200 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1202 const struct got_error *err = NULL;
1203 char canonpath[PATH_MAX];
1204 char *path_got = NULL;
1206 *is_bad_symlink = 0;
1208 if (target_len >= sizeof(canonpath)) {
1209 *is_bad_symlink = 1;
1210 return NULL;
1214 * We do not use realpath(3) to resolve the symlink's target
1215 * path because we don't want to resolve symlinks recursively.
1216 * Instead we make the path absolute and then canonicalize it.
1217 * Relative symlink target lookup should begin at the directory
1218 * in which the blob object is being installed.
1220 if (!got_path_is_absolute(target_path)) {
1221 char *abspath, *parent;
1222 err = got_path_dirname(&parent, ondisk_path);
1223 if (err)
1224 return err;
1225 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1226 free(parent);
1227 return got_error_from_errno("asprintf");
1229 free(parent);
1230 if (strlen(abspath) >= sizeof(canonpath)) {
1231 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1232 free(abspath);
1233 return err;
1235 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1236 free(abspath);
1237 if (err)
1238 return err;
1239 } else {
1240 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1241 if (err)
1242 return err;
1245 /* Only allow symlinks pointing at paths within the work tree. */
1246 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1247 *is_bad_symlink = 1;
1248 return NULL;
1251 /* Do not allow symlinks pointing into the .got directory. */
1252 if (asprintf(&path_got, "%s/%s", wtroot_path,
1253 GOT_WORKTREE_GOT_DIR) == -1)
1254 return got_error_from_errno("asprintf");
1255 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1256 *is_bad_symlink = 1;
1258 free(path_got);
1259 return NULL;
1262 static const struct got_error *
1263 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1264 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1265 int restoring_missing_file, int reverting_versioned_file,
1266 int path_is_unversioned, int allow_bad_symlinks,
1267 struct got_repository *repo,
1268 got_worktree_checkout_cb progress_cb, void *progress_arg)
1270 const struct got_error *err = NULL;
1271 char target_path[PATH_MAX];
1272 size_t len, target_len = 0;
1273 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1274 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1276 *is_bad_symlink = 0;
1279 * Blob object content specifies the target path of the link.
1280 * If a symbolic link cannot be installed we instead create
1281 * a regular file which contains the link target path stored
1282 * in the blob object.
1284 do {
1285 err = got_object_blob_read_block(&len, blob);
1286 if (err)
1287 return err;
1289 if (len + target_len >= sizeof(target_path)) {
1290 /* Path too long; install as a regular file. */
1291 *is_bad_symlink = 1;
1292 got_object_blob_rewind(blob);
1293 return install_blob(worktree, ondisk_path, path,
1294 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1295 restoring_missing_file, reverting_versioned_file,
1296 1, path_is_unversioned, repo, progress_cb,
1297 progress_arg);
1299 if (len > 0) {
1300 /* Skip blob object header first time around. */
1301 memcpy(target_path + target_len, buf + hdrlen,
1302 len - hdrlen);
1303 target_len += len - hdrlen;
1304 hdrlen = 0;
1306 } while (len != 0);
1307 target_path[target_len] = '\0';
1309 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1310 ondisk_path, worktree->root_path);
1311 if (err)
1312 return err;
1314 if (*is_bad_symlink && !allow_bad_symlinks) {
1315 /* install as a regular file */
1316 got_object_blob_rewind(blob);
1317 err = install_blob(worktree, ondisk_path, path,
1318 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1319 restoring_missing_file, reverting_versioned_file, 1,
1320 path_is_unversioned, repo, progress_cb, progress_arg);
1321 return err;
1324 if (symlink(target_path, ondisk_path) == -1) {
1325 if (errno == EEXIST) {
1326 int symlink_replaced;
1327 if (path_is_unversioned) {
1328 err = (*progress_cb)(progress_arg,
1329 GOT_STATUS_UNVERSIONED, path);
1330 return err;
1332 err = replace_existing_symlink(&symlink_replaced,
1333 ondisk_path, target_path, target_len);
1334 if (err)
1335 return err;
1336 if (progress_cb) {
1337 if (symlink_replaced) {
1338 err = (*progress_cb)(progress_arg,
1339 reverting_versioned_file ?
1340 GOT_STATUS_REVERT :
1341 GOT_STATUS_UPDATE, path);
1342 } else {
1343 err = (*progress_cb)(progress_arg,
1344 GOT_STATUS_EXISTS, path);
1347 return err; /* Nothing else to do. */
1350 if (errno == ENOENT) {
1351 char *parent;
1352 err = got_path_dirname(&parent, ondisk_path);
1353 if (err)
1354 return err;
1355 err = add_dir_on_disk(worktree, parent);
1356 free(parent);
1357 if (err)
1358 return err;
1360 * Retry, and fall through to error handling
1361 * below if this second attempt fails.
1363 if (symlink(target_path, ondisk_path) != -1) {
1364 err = NULL; /* success */
1365 return err;
1369 /* Handle errors from first or second creation attempt. */
1370 if (errno == ENAMETOOLONG) {
1371 /* bad target path; install as a regular file */
1372 *is_bad_symlink = 1;
1373 got_object_blob_rewind(blob);
1374 err = install_blob(worktree, ondisk_path, path,
1375 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1376 restoring_missing_file, reverting_versioned_file, 1,
1377 path_is_unversioned, repo,
1378 progress_cb, progress_arg);
1379 } else if (errno == ENOTDIR) {
1380 err = got_error_path(ondisk_path,
1381 GOT_ERR_FILE_OBSTRUCTED);
1382 } else {
1383 err = got_error_from_errno3("symlink",
1384 target_path, ondisk_path);
1386 } else if (progress_cb)
1387 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1388 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1389 return err;
1392 static const struct got_error *
1393 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1394 const char *path, mode_t te_mode, mode_t st_mode,
1395 struct got_blob_object *blob, int restoring_missing_file,
1396 int reverting_versioned_file, int installing_bad_symlink,
1397 int path_is_unversioned, struct got_repository *repo,
1398 got_worktree_checkout_cb progress_cb, void *progress_arg)
1400 const struct got_error *err = NULL;
1401 int fd = -1;
1402 size_t len, hdrlen;
1403 int update = 0;
1404 char *tmppath = NULL;
1405 mode_t mode;
1407 mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1408 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1409 O_CLOEXEC, mode);
1410 if (fd == -1) {
1411 if (errno == ENOENT || errno == ENOTDIR) {
1412 char *parent;
1413 err = got_path_dirname(&parent, path);
1414 if (err)
1415 return err;
1416 err = add_dir_on_disk(worktree, parent);
1417 if (err && err->code == GOT_ERR_FILE_OBSTRUCTED)
1418 err = got_error_path(path, err->code);
1419 free(parent);
1420 if (err)
1421 return err;
1422 fd = open(ondisk_path,
1423 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1424 mode);
1425 if (fd == -1)
1426 return got_error_from_errno2("open",
1427 ondisk_path);
1428 } else if (errno == EEXIST) {
1429 if (path_is_unversioned) {
1430 err = (*progress_cb)(progress_arg,
1431 GOT_STATUS_UNVERSIONED, path);
1432 goto done;
1434 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1435 !S_ISREG(st_mode) && !installing_bad_symlink) {
1436 /* TODO file is obstructed; do something */
1437 err = got_error_path(ondisk_path,
1438 GOT_ERR_FILE_OBSTRUCTED);
1439 goto done;
1440 } else {
1441 err = got_opentemp_named_fd(&tmppath, &fd,
1442 ondisk_path, "");
1443 if (err)
1444 goto done;
1445 update = 1;
1447 if (fchmod(fd, apply_umask(mode)) == -1) {
1448 err = got_error_from_errno2("fchmod",
1449 tmppath);
1450 goto done;
1453 } else
1454 return got_error_from_errno2("open", ondisk_path);
1457 if (progress_cb) {
1458 if (restoring_missing_file)
1459 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1460 path);
1461 else if (reverting_versioned_file)
1462 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1463 path);
1464 else
1465 err = (*progress_cb)(progress_arg,
1466 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1467 if (err)
1468 goto done;
1471 hdrlen = got_object_blob_get_hdrlen(blob);
1472 do {
1473 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1474 err = got_object_blob_read_block(&len, blob);
1475 if (err)
1476 break;
1477 if (len > 0) {
1478 /* Skip blob object header first time around. */
1479 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1480 if (outlen == -1) {
1481 err = got_error_from_errno("write");
1482 goto done;
1483 } else if (outlen != len - hdrlen) {
1484 err = got_error(GOT_ERR_IO);
1485 goto done;
1487 hdrlen = 0;
1489 } while (len != 0);
1491 if (fsync(fd) != 0) {
1492 err = got_error_from_errno("fsync");
1493 goto done;
1496 if (update) {
1497 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1498 err = got_error_from_errno2("unlink", ondisk_path);
1499 goto done;
1501 if (rename(tmppath, ondisk_path) != 0) {
1502 err = got_error_from_errno3("rename", tmppath,
1503 ondisk_path);
1504 goto done;
1506 free(tmppath);
1507 tmppath = NULL;
1510 done:
1511 if (fd != -1 && close(fd) == -1 && err == NULL)
1512 err = got_error_from_errno("close");
1513 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1514 err = got_error_from_errno2("unlink", tmppath);
1515 free(tmppath);
1516 return err;
1520 * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
1521 * conflict marker is found in newly added lines only.
1523 static const struct got_error *
1524 get_modified_file_content_status(unsigned char *status,
1525 struct got_blob_object *blob, const char *path, struct stat *sb,
1526 FILE *ondisk_file)
1528 const struct got_error *err, *free_err;
1529 const char *markers[3] = {
1530 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1531 GOT_DIFF_CONFLICT_MARKER_SEP,
1532 GOT_DIFF_CONFLICT_MARKER_END
1534 FILE *f1 = NULL;
1535 struct got_diffreg_result *diffreg_result = NULL;
1536 struct diff_result *r;
1537 int nchunks_parsed, n, i = 0, ln = 0;
1538 char *line = NULL;
1539 size_t linesize = 0;
1540 ssize_t linelen;
1542 if (*status != GOT_STATUS_MODIFY)
1543 return NULL;
1545 f1 = got_opentemp();
1546 if (f1 == NULL)
1547 return got_error_from_errno("got_opentemp");
1549 if (blob) {
1550 got_object_blob_rewind(blob);
1551 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
1552 if (err)
1553 goto done;
1556 err = got_diff_files(&diffreg_result, f1, 1, NULL, ondisk_file,
1557 1, NULL, 0, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
1558 if (err)
1559 goto done;
1561 r = diffreg_result->result;
1563 for (n = 0; n < r->chunks.len; n += nchunks_parsed) {
1564 struct diff_chunk *c;
1565 struct diff_chunk_context cc = {};
1566 off_t pos;
1569 * We can optimise a little by advancing straight
1570 * to the next chunk if this one has no added lines.
1572 c = diff_chunk_get(r, n);
1574 if (diff_chunk_type(c) != CHUNK_PLUS) {
1575 nchunks_parsed = 1;
1576 continue; /* removed or unchanged lines */
1579 pos = diff_chunk_get_right_start_pos(c);
1580 if (fseek(ondisk_file, pos, SEEK_SET) == -1) {
1581 err = got_ferror(ondisk_file, GOT_ERR_IO);
1582 goto done;
1585 diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
1586 ln = cc.right.start;
1588 while (ln < cc.right.end) {
1589 linelen = getline(&line, &linesize, ondisk_file);
1590 if (linelen == -1) {
1591 if (feof(ondisk_file))
1592 break;
1593 err = got_ferror(ondisk_file, GOT_ERR_IO);
1594 break;
1597 if (line && strncmp(line, markers[i],
1598 strlen(markers[i])) == 0) {
1599 if (strcmp(markers[i],
1600 GOT_DIFF_CONFLICT_MARKER_END) == 0) {
1601 *status = GOT_STATUS_CONFLICT;
1602 goto done;
1603 } else
1604 i++;
1606 ++ln;
1610 done:
1611 free(line);
1612 if (f1 != NULL && fclose(f1) == EOF && err == NULL)
1613 err = got_error_from_errno("fclose");
1614 free_err = got_diffreg_result_free(diffreg_result);
1615 if (err == NULL)
1616 err = free_err;
1618 return err;
1621 static int
1622 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1624 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1625 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1628 static int
1629 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1631 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1632 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1633 ie->mtime_sec == sb->st_mtim.tv_sec &&
1634 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1635 ie->size == (sb->st_size & 0xffffffff) &&
1636 !xbit_differs(ie, sb->st_mode));
1639 static unsigned char
1640 get_staged_status(struct got_fileindex_entry *ie)
1642 switch (got_fileindex_entry_stage_get(ie)) {
1643 case GOT_FILEIDX_STAGE_ADD:
1644 return GOT_STATUS_ADD;
1645 case GOT_FILEIDX_STAGE_DELETE:
1646 return GOT_STATUS_DELETE;
1647 case GOT_FILEIDX_STAGE_MODIFY:
1648 return GOT_STATUS_MODIFY;
1649 default:
1650 return GOT_STATUS_NO_CHANGE;
1654 static const struct got_error *
1655 get_symlink_modification_status(unsigned char *status,
1656 struct got_fileindex_entry *ie, const char *abspath,
1657 int dirfd, const char *de_name, struct got_blob_object *blob)
1659 const struct got_error *err = NULL;
1660 char target_path[PATH_MAX];
1661 char etarget[PATH_MAX];
1662 ssize_t elen;
1663 size_t len, target_len = 0;
1664 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1665 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1667 *status = GOT_STATUS_NO_CHANGE;
1669 /* Blob object content specifies the target path of the link. */
1670 do {
1671 err = got_object_blob_read_block(&len, blob);
1672 if (err)
1673 return err;
1674 if (len + target_len >= sizeof(target_path)) {
1676 * Should not happen. The blob contents were OK
1677 * when this symlink was installed.
1679 return got_error(GOT_ERR_NO_SPACE);
1681 if (len > 0) {
1682 /* Skip blob object header first time around. */
1683 memcpy(target_path + target_len, buf + hdrlen,
1684 len - hdrlen);
1685 target_len += len - hdrlen;
1686 hdrlen = 0;
1688 } while (len != 0);
1689 target_path[target_len] = '\0';
1691 if (dirfd != -1) {
1692 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1693 if (elen == -1)
1694 return got_error_from_errno2("readlinkat", abspath);
1695 } else {
1696 elen = readlink(abspath, etarget, sizeof(etarget));
1697 if (elen == -1)
1698 return got_error_from_errno2("readlink", abspath);
1701 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1702 *status = GOT_STATUS_MODIFY;
1704 return NULL;
1707 static const struct got_error *
1708 get_file_status(unsigned char *status, struct stat *sb,
1709 struct got_fileindex_entry *ie, const char *abspath,
1710 int dirfd, const char *de_name, struct got_repository *repo)
1712 const struct got_error *err = NULL;
1713 struct got_object_id id;
1714 size_t hdrlen;
1715 int fd = -1, fd1 = -1;
1716 FILE *f = NULL;
1717 uint8_t fbuf[8192];
1718 struct got_blob_object *blob = NULL;
1719 size_t flen, blen;
1720 unsigned char staged_status;
1722 staged_status = get_staged_status(ie);
1723 *status = GOT_STATUS_NO_CHANGE;
1724 memset(sb, 0, sizeof(*sb));
1727 * Whenever the caller provides a directory descriptor and a
1728 * directory entry name for the file, use them! This prevents
1729 * race conditions if filesystem paths change beneath our feet.
1731 if (dirfd != -1) {
1732 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1733 if (errno == ENOENT) {
1734 if (got_fileindex_entry_has_file_on_disk(ie))
1735 *status = GOT_STATUS_MISSING;
1736 else
1737 *status = GOT_STATUS_DELETE;
1738 goto done;
1740 err = got_error_from_errno2("fstatat", abspath);
1741 goto done;
1743 } else {
1744 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1745 if (fd == -1 && errno != ENOENT &&
1746 !got_err_open_nofollow_on_symlink())
1747 return got_error_from_errno2("open", abspath);
1748 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1749 if (lstat(abspath, sb) == -1)
1750 return got_error_from_errno2("lstat", abspath);
1751 } else if (fd == -1 || fstat(fd, sb) == -1) {
1752 if (errno == ENOENT) {
1753 if (got_fileindex_entry_has_file_on_disk(ie))
1754 *status = GOT_STATUS_MISSING;
1755 else
1756 *status = GOT_STATUS_DELETE;
1757 goto done;
1759 err = got_error_from_errno2("fstat", abspath);
1760 goto done;
1764 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1765 *status = GOT_STATUS_OBSTRUCTED;
1766 goto done;
1769 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1770 *status = GOT_STATUS_DELETE;
1771 goto done;
1772 } else if (!got_fileindex_entry_has_blob(ie) &&
1773 staged_status != GOT_STATUS_ADD) {
1774 *status = GOT_STATUS_ADD;
1775 goto done;
1778 if (!stat_info_differs(ie, sb))
1779 goto done;
1781 if (S_ISLNK(sb->st_mode) &&
1782 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1783 *status = GOT_STATUS_MODIFY;
1784 goto done;
1787 if (staged_status == GOT_STATUS_MODIFY ||
1788 staged_status == GOT_STATUS_ADD)
1789 got_fileindex_entry_get_staged_blob_id(&id, ie);
1790 else
1791 got_fileindex_entry_get_blob_id(&id, ie);
1793 fd1 = got_opentempfd();
1794 if (fd1 == -1) {
1795 err = got_error_from_errno("got_opentempfd");
1796 goto done;
1798 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1799 if (err)
1800 goto done;
1802 if (S_ISLNK(sb->st_mode)) {
1803 err = get_symlink_modification_status(status, ie,
1804 abspath, dirfd, de_name, blob);
1805 goto done;
1808 if (dirfd != -1) {
1809 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1810 if (fd == -1) {
1811 err = got_error_from_errno2("openat", abspath);
1812 goto done;
1816 f = fdopen(fd, "r");
1817 if (f == NULL) {
1818 err = got_error_from_errno2("fdopen", abspath);
1819 goto done;
1821 fd = -1;
1822 hdrlen = got_object_blob_get_hdrlen(blob);
1823 for (;;) {
1824 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1825 err = got_object_blob_read_block(&blen, blob);
1826 if (err)
1827 goto done;
1828 /* Skip length of blob object header first time around. */
1829 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1830 if (flen == 0 && ferror(f)) {
1831 err = got_error_from_errno("fread");
1832 goto done;
1834 if (blen - hdrlen == 0) {
1835 if (flen != 0)
1836 *status = GOT_STATUS_MODIFY;
1837 break;
1838 } else if (flen == 0) {
1839 if (blen - hdrlen != 0)
1840 *status = GOT_STATUS_MODIFY;
1841 break;
1842 } else if (blen - hdrlen == flen) {
1843 /* Skip blob object header first time around. */
1844 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1845 *status = GOT_STATUS_MODIFY;
1846 break;
1848 } else {
1849 *status = GOT_STATUS_MODIFY;
1850 break;
1852 hdrlen = 0;
1855 if (*status == GOT_STATUS_MODIFY) {
1856 rewind(f);
1857 err = get_modified_file_content_status(status, blob, ie->path,
1858 sb, f);
1859 } else if (xbit_differs(ie, sb->st_mode))
1860 *status = GOT_STATUS_MODE_CHANGE;
1861 done:
1862 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1863 err = got_error_from_errno("close");
1864 if (blob)
1865 got_object_blob_close(blob);
1866 if (f != NULL && fclose(f) == EOF && err == NULL)
1867 err = got_error_from_errno2("fclose", abspath);
1868 if (fd != -1 && close(fd) == -1 && err == NULL)
1869 err = got_error_from_errno2("close", abspath);
1870 return err;
1874 * Update timestamps in the file index if a file is unmodified and
1875 * we had to run a full content comparison to find out.
1877 static const struct got_error *
1878 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1879 struct got_fileindex_entry *ie, struct stat *sb)
1881 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1882 return got_fileindex_entry_update(ie, wt_fd, path,
1883 ie->blob_sha1, ie->commit_sha1, 1);
1885 return NULL;
1888 static const struct got_error *remove_ondisk_file(const char *, const char *);
1890 static const struct got_error *
1891 update_blob(struct got_worktree *worktree,
1892 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1893 struct got_tree_entry *te, const char *path,
1894 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1895 void *progress_arg)
1897 const struct got_error *err = NULL;
1898 struct got_blob_object *blob = NULL;
1899 char *ondisk_path = NULL;
1900 unsigned char status = GOT_STATUS_NO_CHANGE;
1901 struct stat sb;
1902 int fd1 = -1, fd2 = -1;
1904 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1905 return got_error_from_errno("asprintf");
1907 if (ie) {
1908 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1909 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1910 goto done;
1912 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1913 repo);
1914 if (err)
1915 goto done;
1916 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1917 sb.st_mode = got_fileindex_perms_to_st(ie);
1918 } else {
1919 if (stat(ondisk_path, &sb) == -1) {
1920 if (errno != ENOENT && errno != ENOTDIR) {
1921 err = got_error_from_errno2("stat",
1922 ondisk_path);
1923 goto done;
1925 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1926 status = GOT_STATUS_UNVERSIONED;
1927 } else {
1928 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1929 status = GOT_STATUS_UNVERSIONED;
1930 else
1931 status = GOT_STATUS_OBSTRUCTED;
1935 if (status == GOT_STATUS_OBSTRUCTED) {
1936 if (ie)
1937 got_fileindex_entry_mark_skipped(ie);
1938 err = (*progress_cb)(progress_arg, status, path);
1939 goto done;
1941 if (status == GOT_STATUS_CONFLICT) {
1942 if (ie)
1943 got_fileindex_entry_mark_skipped(ie);
1944 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1945 path);
1946 goto done;
1949 if (S_ISDIR(te->mode)) { /* file changing into a directory */
1950 if (status == GOT_STATUS_UNVERSIONED) {
1951 err = (*progress_cb)(progress_arg, status, path);
1952 } else if (status != GOT_STATUS_NO_CHANGE &&
1953 status != GOT_STATUS_DELETE &&
1954 status != GOT_STATUS_NONEXISTENT &&
1955 status != GOT_STATUS_MISSING) {
1956 err = (*progress_cb)(progress_arg,
1957 GOT_STATUS_CANNOT_DELETE, path);
1958 } else if (ie) {
1959 if (status != GOT_STATUS_DELETE &&
1960 status != GOT_STATUS_NONEXISTENT &&
1961 status != GOT_STATUS_MISSING) {
1962 err = remove_ondisk_file(worktree->root_path,
1963 ie->path);
1964 if (err && !(err->code == GOT_ERR_ERRNO &&
1965 errno == ENOENT))
1966 goto done;
1968 got_fileindex_entry_remove(fileindex, ie);
1969 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE,
1970 ie->path);
1972 goto done; /* nothing else to do */
1975 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1976 (S_ISLNK(te->mode) ||
1977 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1979 * This is a regular file or an installed bad symlink.
1980 * If the file index indicates that this file is already
1981 * up-to-date with respect to the repository we can skip
1982 * updating contents of this file.
1984 if (got_fileindex_entry_has_commit(ie) &&
1985 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1986 SHA1_DIGEST_LENGTH) == 0) {
1987 /* Same commit. */
1988 err = sync_timestamps(worktree->root_fd,
1989 path, status, ie, &sb);
1990 if (err)
1991 goto done;
1992 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1993 path);
1994 goto done;
1996 if (got_fileindex_entry_has_blob(ie) &&
1997 memcmp(ie->blob_sha1, te->id.sha1,
1998 SHA1_DIGEST_LENGTH) == 0) {
1999 /* Different commit but the same blob. */
2000 if (got_fileindex_entry_has_commit(ie)) {
2001 /* Update the base commit ID of this file. */
2002 memcpy(ie->commit_sha1,
2003 worktree->base_commit_id->sha1,
2004 sizeof(ie->commit_sha1));
2006 err = sync_timestamps(worktree->root_fd,
2007 path, status, ie, &sb);
2008 if (err)
2009 goto done;
2010 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2011 path);
2012 goto done;
2016 fd1 = got_opentempfd();
2017 if (fd1 == -1) {
2018 err = got_error_from_errno("got_opentempfd");
2019 goto done;
2021 err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
2022 if (err)
2023 goto done;
2025 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
2026 int update_timestamps;
2027 struct got_blob_object *blob2 = NULL;
2028 char *label_orig = NULL;
2029 if (got_fileindex_entry_has_blob(ie)) {
2030 fd2 = got_opentempfd();
2031 if (fd2 == -1) {
2032 err = got_error_from_errno("got_opentempfd");
2033 goto done;
2035 struct got_object_id id2;
2036 got_fileindex_entry_get_blob_id(&id2, ie);
2037 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
2038 fd2);
2039 if (err)
2040 goto done;
2042 if (got_fileindex_entry_has_commit(ie)) {
2043 char id_str[SHA1_DIGEST_STRING_LENGTH];
2044 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2045 sizeof(id_str)) == NULL) {
2046 err = got_error_path(id_str,
2047 GOT_ERR_BAD_OBJ_ID_STR);
2048 goto done;
2050 if (asprintf(&label_orig, "%s: commit %s",
2051 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2052 err = got_error_from_errno("asprintf");
2053 goto done;
2056 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2057 char *link_target;
2058 err = got_object_blob_read_to_str(&link_target, blob);
2059 if (err)
2060 goto done;
2061 err = merge_symlink(worktree, blob2, ondisk_path, path,
2062 label_orig, link_target, worktree->base_commit_id,
2063 repo, progress_cb, progress_arg);
2064 free(link_target);
2065 } else {
2066 err = merge_blob(&update_timestamps, worktree, blob2,
2067 ondisk_path, path, sb.st_mode, label_orig, blob,
2068 worktree->base_commit_id, repo,
2069 progress_cb, progress_arg);
2071 free(label_orig);
2072 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2073 err = got_error_from_errno("close");
2074 goto done;
2076 if (blob2)
2077 got_object_blob_close(blob2);
2078 if (err)
2079 goto done;
2081 * Do not update timestamps of files with local changes.
2082 * Otherwise, a future status walk would treat them as
2083 * unmodified files again.
2085 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2086 blob->id.sha1, worktree->base_commit_id->sha1,
2087 update_timestamps);
2088 } else if (status == GOT_STATUS_MODE_CHANGE) {
2089 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2090 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2091 } else if (status == GOT_STATUS_DELETE) {
2092 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2093 if (err)
2094 goto done;
2095 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2096 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2097 if (err)
2098 goto done;
2099 } else {
2100 int is_bad_symlink = 0;
2101 if (S_ISLNK(te->mode)) {
2102 err = install_symlink(&is_bad_symlink, worktree,
2103 ondisk_path, path, blob,
2104 status == GOT_STATUS_MISSING, 0,
2105 status == GOT_STATUS_UNVERSIONED, 0,
2106 repo, progress_cb, progress_arg);
2107 } else {
2108 err = install_blob(worktree, ondisk_path, path,
2109 te->mode, sb.st_mode, blob,
2110 status == GOT_STATUS_MISSING, 0, 0,
2111 status == GOT_STATUS_UNVERSIONED, repo,
2112 progress_cb, progress_arg);
2114 if (err)
2115 goto done;
2117 if (ie) {
2118 err = got_fileindex_entry_update(ie,
2119 worktree->root_fd, path, blob->id.sha1,
2120 worktree->base_commit_id->sha1, 1);
2121 } else {
2122 err = create_fileindex_entry(&ie, fileindex,
2123 worktree->base_commit_id, worktree->root_fd, path,
2124 &blob->id);
2126 if (err)
2127 goto done;
2129 if (is_bad_symlink) {
2130 got_fileindex_entry_filetype_set(ie,
2131 GOT_FILEIDX_MODE_BAD_SYMLINK);
2135 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2136 err = got_error_from_errno("close");
2137 goto done;
2139 got_object_blob_close(blob);
2140 done:
2141 free(ondisk_path);
2142 return err;
2145 static const struct got_error *
2146 remove_ondisk_file(const char *root_path, const char *path)
2148 const struct got_error *err = NULL;
2149 char *ondisk_path = NULL, *parent = NULL;
2151 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2152 return got_error_from_errno("asprintf");
2154 if (unlink(ondisk_path) == -1) {
2155 if (errno != ENOENT)
2156 err = got_error_from_errno2("unlink", ondisk_path);
2157 } else {
2158 size_t root_len = strlen(root_path);
2159 err = got_path_dirname(&parent, ondisk_path);
2160 if (err)
2161 goto done;
2162 while (got_path_cmp(parent, root_path,
2163 strlen(parent), root_len) != 0) {
2164 free(ondisk_path);
2165 ondisk_path = parent;
2166 parent = NULL;
2167 if (rmdir(ondisk_path) == -1) {
2168 if (errno != ENOTEMPTY)
2169 err = got_error_from_errno2("rmdir",
2170 ondisk_path);
2171 break;
2173 err = got_path_dirname(&parent, ondisk_path);
2174 if (err)
2175 break;
2178 done:
2179 free(ondisk_path);
2180 free(parent);
2181 return err;
2184 static const struct got_error *
2185 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2186 struct got_fileindex_entry *ie, struct got_repository *repo,
2187 got_worktree_checkout_cb progress_cb, void *progress_arg)
2189 const struct got_error *err = NULL;
2190 unsigned char status;
2191 struct stat sb;
2192 char *ondisk_path;
2194 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2195 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2197 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2198 == -1)
2199 return got_error_from_errno("asprintf");
2201 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2202 if (err)
2203 goto done;
2205 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2206 char ondisk_target[PATH_MAX];
2207 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2208 sizeof(ondisk_target));
2209 if (ondisk_len == -1) {
2210 err = got_error_from_errno2("readlink", ondisk_path);
2211 goto done;
2213 ondisk_target[ondisk_len] = '\0';
2214 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2215 NULL, NULL, /* XXX pass common ancestor info? */
2216 ondisk_target, ondisk_path);
2217 if (err)
2218 goto done;
2219 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2220 ie->path);
2221 goto done;
2224 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2225 status == GOT_STATUS_ADD) {
2226 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2227 if (err)
2228 goto done;
2230 * Preserve the working file and change the deleted blob's
2231 * entry into a schedule-add entry.
2233 err = got_fileindex_entry_update(ie, worktree->root_fd,
2234 ie->path, NULL, NULL, 0);
2235 } else {
2236 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2237 if (err)
2238 goto done;
2239 if (status == GOT_STATUS_NO_CHANGE) {
2240 err = remove_ondisk_file(worktree->root_path, ie->path);
2241 if (err)
2242 goto done;
2244 got_fileindex_entry_remove(fileindex, ie);
2246 done:
2247 free(ondisk_path);
2248 return err;
2251 struct diff_cb_arg {
2252 struct got_fileindex *fileindex;
2253 struct got_worktree *worktree;
2254 struct got_repository *repo;
2255 got_worktree_checkout_cb progress_cb;
2256 void *progress_arg;
2257 got_cancel_cb cancel_cb;
2258 void *cancel_arg;
2261 static const struct got_error *
2262 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2263 struct got_tree_entry *te, const char *parent_path)
2265 struct diff_cb_arg *a = arg;
2267 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2268 return got_error(GOT_ERR_CANCELLED);
2270 return update_blob(a->worktree, a->fileindex, ie, te,
2271 ie->path, a->repo, a->progress_cb, a->progress_arg);
2274 static const struct got_error *
2275 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2277 struct diff_cb_arg *a = arg;
2279 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2280 return got_error(GOT_ERR_CANCELLED);
2282 return delete_blob(a->worktree, a->fileindex, ie,
2283 a->repo, a->progress_cb, a->progress_arg);
2286 static const struct got_error *
2287 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2289 struct diff_cb_arg *a = arg;
2290 const struct got_error *err;
2291 char *path;
2293 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2294 return got_error(GOT_ERR_CANCELLED);
2296 if (got_object_tree_entry_is_submodule(te))
2297 return NULL;
2299 if (asprintf(&path, "%s%s%s", parent_path,
2300 parent_path[0] ? "/" : "", te->name)
2301 == -1)
2302 return got_error_from_errno("asprintf");
2304 if (S_ISDIR(te->mode))
2305 err = add_dir_on_disk(a->worktree, path);
2306 else
2307 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2308 a->repo, a->progress_cb, a->progress_arg);
2310 free(path);
2311 return err;
2314 const struct got_error *
2315 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2317 uint32_t uuid_status;
2319 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2320 if (uuid_status != uuid_s_ok) {
2321 *uuidstr = NULL;
2322 return got_error_uuid(uuid_status, "uuid_to_string");
2325 return NULL;
2328 static const struct got_error *
2329 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2331 const struct got_error *err = NULL;
2332 char *uuidstr = NULL;
2334 *refname = NULL;
2336 err = got_worktree_get_uuid(&uuidstr, worktree);
2337 if (err)
2338 return err;
2340 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2341 err = got_error_from_errno("asprintf");
2342 *refname = NULL;
2344 free(uuidstr);
2345 return err;
2348 const struct got_error *
2349 got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2350 const char *prefix)
2352 return get_ref_name(refname, worktree, prefix);
2355 const struct got_error *
2356 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2358 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2361 static const struct got_error *
2362 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2364 return get_ref_name(refname, worktree,
2365 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2368 static const struct got_error *
2369 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2371 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2374 static const struct got_error *
2375 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2377 return get_ref_name(refname, worktree,
2378 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2381 static const struct got_error *
2382 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2384 return get_ref_name(refname, worktree,
2385 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2388 static const struct got_error *
2389 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2391 return get_ref_name(refname, worktree,
2392 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2395 static const struct got_error *
2396 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2398 return get_ref_name(refname, worktree,
2399 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2402 static const struct got_error *
2403 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2405 return get_ref_name(refname, worktree,
2406 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2409 static const struct got_error *
2410 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2412 return get_ref_name(refname, worktree,
2413 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2416 const struct got_error *
2417 got_worktree_get_histedit_script_path(char **path,
2418 struct got_worktree *worktree)
2420 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2421 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2422 *path = NULL;
2423 return got_error_from_errno("asprintf");
2425 return NULL;
2428 static const struct got_error *
2429 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2431 return get_ref_name(refname, worktree,
2432 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2435 static const struct got_error *
2436 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2438 return get_ref_name(refname, worktree,
2439 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2443 * Prevent Git's garbage collector from deleting our base commit by
2444 * setting a reference to our base commit's ID.
2446 static const struct got_error *
2447 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2449 const struct got_error *err = NULL;
2450 struct got_reference *ref = NULL;
2451 char *refname;
2453 err = got_worktree_get_base_ref_name(&refname, worktree);
2454 if (err)
2455 return err;
2457 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2458 if (err)
2459 goto done;
2461 err = got_ref_write(ref, repo);
2462 done:
2463 free(refname);
2464 if (ref)
2465 got_ref_close(ref);
2466 return err;
2469 static const struct got_error *
2470 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2472 const struct got_error *err = NULL;
2474 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2475 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2476 err = got_error_from_errno("asprintf");
2477 *fileindex_path = NULL;
2479 return err;
2483 static const struct got_error *
2484 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2485 struct got_worktree *worktree)
2487 const struct got_error *err = NULL;
2488 FILE *index = NULL;
2490 *fileindex_path = NULL;
2491 *fileindex = got_fileindex_alloc();
2492 if (*fileindex == NULL)
2493 return got_error_from_errno("got_fileindex_alloc");
2495 err = get_fileindex_path(fileindex_path, worktree);
2496 if (err)
2497 goto done;
2499 index = fopen(*fileindex_path, "rbe");
2500 if (index == NULL) {
2501 if (errno != ENOENT)
2502 err = got_error_from_errno2("fopen", *fileindex_path);
2503 } else {
2504 err = got_fileindex_read(*fileindex, index);
2505 if (fclose(index) == EOF && err == NULL)
2506 err = got_error_from_errno("fclose");
2508 done:
2509 if (err) {
2510 free(*fileindex_path);
2511 *fileindex_path = NULL;
2512 got_fileindex_free(*fileindex);
2513 *fileindex = NULL;
2515 return err;
2518 struct bump_base_commit_id_arg {
2519 struct got_object_id *base_commit_id;
2520 const char *path;
2521 size_t path_len;
2522 const char *entry_name;
2523 got_worktree_checkout_cb progress_cb;
2524 void *progress_arg;
2527 static const struct got_error *
2528 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2530 const struct got_error *err;
2531 struct bump_base_commit_id_arg *a = arg;
2533 if (a->entry_name) {
2534 if (strcmp(ie->path, a->path) != 0)
2535 return NULL;
2536 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2537 return NULL;
2539 if (got_fileindex_entry_was_skipped(ie))
2540 return NULL;
2542 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2543 SHA1_DIGEST_LENGTH) == 0)
2544 return NULL;
2546 if (a->progress_cb) {
2547 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2548 ie->path);
2549 if (err)
2550 return err;
2552 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2553 return NULL;
2556 /* Bump base commit ID of all files within an updated part of the work tree. */
2557 static const struct got_error *
2558 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2559 struct got_fileindex *fileindex,
2560 got_worktree_checkout_cb progress_cb, void *progress_arg)
2562 struct bump_base_commit_id_arg bbc_arg;
2564 bbc_arg.base_commit_id = worktree->base_commit_id;
2565 bbc_arg.entry_name = NULL;
2566 bbc_arg.path = "";
2567 bbc_arg.path_len = 0;
2568 bbc_arg.progress_cb = progress_cb;
2569 bbc_arg.progress_arg = progress_arg;
2571 return got_fileindex_for_each_entry_safe(fileindex,
2572 bump_base_commit_id, &bbc_arg);
2575 static const struct got_error *
2576 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2578 const struct got_error *err = NULL;
2579 char *new_fileindex_path = NULL;
2580 FILE *new_index = NULL;
2581 struct timespec timeout;
2583 err = got_opentemp_named(&new_fileindex_path, &new_index,
2584 fileindex_path, "");
2585 if (err)
2586 goto done;
2588 err = got_fileindex_write(fileindex, new_index);
2589 if (err)
2590 goto done;
2592 if (rename(new_fileindex_path, fileindex_path) != 0) {
2593 err = got_error_from_errno3("rename", new_fileindex_path,
2594 fileindex_path);
2595 unlink(new_fileindex_path);
2599 * Sleep for a short amount of time to ensure that files modified after
2600 * this program exits have a different time stamp from the one which
2601 * was recorded in the file index.
2603 timeout.tv_sec = 0;
2604 timeout.tv_nsec = 1;
2605 nanosleep(&timeout, NULL);
2606 done:
2607 if (new_index)
2608 fclose(new_index);
2609 free(new_fileindex_path);
2610 return err;
2613 static const struct got_error *
2614 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2615 struct got_object_id **tree_id, const char *wt_relpath,
2616 struct got_commit_object *base_commit, struct got_worktree *worktree,
2617 struct got_repository *repo)
2619 const struct got_error *err = NULL;
2620 struct got_object_id *id = NULL;
2621 char *in_repo_path = NULL;
2622 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2624 *entry_type = GOT_OBJ_TYPE_ANY;
2625 *tree_relpath = NULL;
2626 *tree_id = NULL;
2628 if (wt_relpath[0] == '\0') {
2629 /* Check out all files within the work tree. */
2630 *entry_type = GOT_OBJ_TYPE_TREE;
2631 *tree_relpath = strdup("");
2632 if (*tree_relpath == NULL) {
2633 err = got_error_from_errno("strdup");
2634 goto done;
2636 err = got_object_id_by_path(tree_id, repo, base_commit,
2637 worktree->path_prefix);
2638 if (err)
2639 goto done;
2640 return NULL;
2643 /* Check out a subset of files in the work tree. */
2645 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2646 is_root_wt ? "" : "/", wt_relpath) == -1) {
2647 err = got_error_from_errno("asprintf");
2648 goto done;
2651 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2652 if (err)
2653 goto done;
2655 free(in_repo_path);
2656 in_repo_path = NULL;
2658 err = got_object_get_type(entry_type, repo, id);
2659 if (err)
2660 goto done;
2662 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2663 /* Check out a single file. */
2664 if (strchr(wt_relpath, '/') == NULL) {
2665 /* Check out a single file in work tree's root dir. */
2666 in_repo_path = strdup(worktree->path_prefix);
2667 if (in_repo_path == NULL) {
2668 err = got_error_from_errno("strdup");
2669 goto done;
2671 *tree_relpath = strdup("");
2672 if (*tree_relpath == NULL) {
2673 err = got_error_from_errno("strdup");
2674 goto done;
2676 } else {
2677 /* Check out a single file in a subdirectory. */
2678 err = got_path_dirname(tree_relpath, wt_relpath);
2679 if (err)
2680 return err;
2681 if (asprintf(&in_repo_path, "%s%s%s",
2682 worktree->path_prefix, is_root_wt ? "" : "/",
2683 *tree_relpath) == -1) {
2684 err = got_error_from_errno("asprintf");
2685 goto done;
2688 err = got_object_id_by_path(tree_id, repo,
2689 base_commit, in_repo_path);
2690 } else {
2691 /* Check out all files within a subdirectory. */
2692 *tree_id = got_object_id_dup(id);
2693 if (*tree_id == NULL) {
2694 err = got_error_from_errno("got_object_id_dup");
2695 goto done;
2697 *tree_relpath = strdup(wt_relpath);
2698 if (*tree_relpath == NULL) {
2699 err = got_error_from_errno("strdup");
2700 goto done;
2703 done:
2704 free(id);
2705 free(in_repo_path);
2706 if (err) {
2707 *entry_type = GOT_OBJ_TYPE_ANY;
2708 free(*tree_relpath);
2709 *tree_relpath = NULL;
2710 free(*tree_id);
2711 *tree_id = NULL;
2713 return err;
2716 static const struct got_error *
2717 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2718 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2719 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2720 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2722 const struct got_error *err = NULL;
2723 struct got_commit_object *commit = NULL;
2724 struct got_tree_object *tree = NULL;
2725 struct got_fileindex_diff_tree_cb diff_cb;
2726 struct diff_cb_arg arg;
2728 err = ref_base_commit(worktree, repo);
2729 if (err) {
2730 if (!(err->code == GOT_ERR_ERRNO &&
2731 (errno == EACCES || errno == EROFS)))
2732 goto done;
2733 err = (*progress_cb)(progress_arg,
2734 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2735 if (err)
2736 return err;
2739 err = got_object_open_as_commit(&commit, repo,
2740 worktree->base_commit_id);
2741 if (err)
2742 goto done;
2744 err = got_object_open_as_tree(&tree, repo, tree_id);
2745 if (err)
2746 goto done;
2748 if (entry_name &&
2749 got_object_tree_find_entry(tree, entry_name) == NULL) {
2750 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2751 goto done;
2754 diff_cb.diff_old_new = diff_old_new;
2755 diff_cb.diff_old = diff_old;
2756 diff_cb.diff_new = diff_new;
2757 arg.fileindex = fileindex;
2758 arg.worktree = worktree;
2759 arg.repo = repo;
2760 arg.progress_cb = progress_cb;
2761 arg.progress_arg = progress_arg;
2762 arg.cancel_cb = cancel_cb;
2763 arg.cancel_arg = cancel_arg;
2764 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2765 entry_name, repo, &diff_cb, &arg);
2766 done:
2767 if (tree)
2768 got_object_tree_close(tree);
2769 if (commit)
2770 got_object_commit_close(commit);
2771 return err;
2774 const struct got_error *
2775 got_worktree_checkout_files(struct got_worktree *worktree,
2776 struct got_pathlist_head *paths, struct got_repository *repo,
2777 got_worktree_checkout_cb progress_cb, void *progress_arg,
2778 got_cancel_cb cancel_cb, void *cancel_arg)
2780 const struct got_error *err = NULL, *sync_err, *unlockerr;
2781 struct got_commit_object *commit = NULL;
2782 struct got_tree_object *tree = NULL;
2783 struct got_fileindex *fileindex = NULL;
2784 char *fileindex_path = NULL;
2785 struct got_pathlist_entry *pe;
2786 struct tree_path_data {
2787 STAILQ_ENTRY(tree_path_data) entry;
2788 struct got_object_id *tree_id;
2789 int entry_type;
2790 char *relpath;
2791 char *entry_name;
2792 } *tpd = NULL;
2793 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2795 STAILQ_INIT(&tree_paths);
2797 err = lock_worktree(worktree, LOCK_EX);
2798 if (err)
2799 return err;
2801 err = got_object_open_as_commit(&commit, repo,
2802 worktree->base_commit_id);
2803 if (err)
2804 goto done;
2806 /* Map all specified paths to in-repository trees. */
2807 TAILQ_FOREACH(pe, paths, entry) {
2808 tpd = malloc(sizeof(*tpd));
2809 if (tpd == NULL) {
2810 err = got_error_from_errno("malloc");
2811 goto done;
2814 err = find_tree_entry_for_checkout(&tpd->entry_type,
2815 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2816 worktree, repo);
2817 if (err) {
2818 free(tpd);
2819 goto done;
2822 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2823 err = got_path_basename(&tpd->entry_name, pe->path);
2824 if (err) {
2825 free(tpd->relpath);
2826 free(tpd->tree_id);
2827 free(tpd);
2828 goto done;
2830 } else
2831 tpd->entry_name = NULL;
2833 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2837 * Read the file index.
2838 * Checking out files is supposed to be an idempotent operation.
2839 * If the on-disk file index is incomplete we will try to complete it.
2841 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2842 if (err)
2843 goto done;
2845 tpd = STAILQ_FIRST(&tree_paths);
2846 TAILQ_FOREACH(pe, paths, entry) {
2847 struct bump_base_commit_id_arg bbc_arg;
2849 err = checkout_files(worktree, fileindex, tpd->relpath,
2850 tpd->tree_id, tpd->entry_name, repo,
2851 progress_cb, progress_arg, cancel_cb, cancel_arg);
2852 if (err)
2853 break;
2855 bbc_arg.base_commit_id = worktree->base_commit_id;
2856 bbc_arg.entry_name = tpd->entry_name;
2857 bbc_arg.path = pe->path;
2858 bbc_arg.path_len = pe->path_len;
2859 bbc_arg.progress_cb = progress_cb;
2860 bbc_arg.progress_arg = progress_arg;
2861 err = got_fileindex_for_each_entry_safe(fileindex,
2862 bump_base_commit_id, &bbc_arg);
2863 if (err)
2864 break;
2866 tpd = STAILQ_NEXT(tpd, entry);
2868 sync_err = sync_fileindex(fileindex, fileindex_path);
2869 if (sync_err && err == NULL)
2870 err = sync_err;
2871 done:
2872 free(fileindex_path);
2873 if (tree)
2874 got_object_tree_close(tree);
2875 if (commit)
2876 got_object_commit_close(commit);
2877 if (fileindex)
2878 got_fileindex_free(fileindex);
2879 while (!STAILQ_EMPTY(&tree_paths)) {
2880 tpd = STAILQ_FIRST(&tree_paths);
2881 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2882 free(tpd->relpath);
2883 free(tpd->tree_id);
2884 free(tpd);
2886 unlockerr = lock_worktree(worktree, LOCK_SH);
2887 if (unlockerr && err == NULL)
2888 err = unlockerr;
2889 return err;
2892 static const struct got_error *
2893 add_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2894 struct got_fileindex_entry *ie, const char *ondisk_path,
2895 const char *path2, struct got_blob_object *blob2, mode_t mode2,
2896 int restoring_missing_file, int reverting_versioned_file,
2897 int path_is_unversioned, int allow_bad_symlinks,
2898 struct got_repository *repo,
2899 got_worktree_checkout_cb progress_cb, void *progress_arg)
2901 const struct got_error *err = NULL;
2902 int is_bad_symlink = 0;
2904 if (S_ISLNK(mode2)) {
2905 err = install_symlink(&is_bad_symlink,
2906 worktree, ondisk_path, path2, blob2,
2907 restoring_missing_file,
2908 reverting_versioned_file,
2909 path_is_unversioned, allow_bad_symlinks,
2910 repo, progress_cb, progress_arg);
2911 } else {
2912 err = install_blob(worktree, ondisk_path, path2,
2913 mode2, GOT_DEFAULT_FILE_MODE, blob2,
2914 restoring_missing_file, reverting_versioned_file, 0,
2915 path_is_unversioned, repo, progress_cb, progress_arg);
2917 if (err)
2918 return err;
2919 if (ie == NULL) {
2920 /* Adding an unversioned file. */
2921 err = got_fileindex_entry_alloc(&ie, path2);
2922 if (err)
2923 return err;
2924 err = got_fileindex_entry_update(ie,
2925 worktree->root_fd, path2, NULL, NULL, 1);
2926 if (err) {
2927 got_fileindex_entry_free(ie);
2928 return err;
2930 err = got_fileindex_entry_add(fileindex, ie);
2931 if (err) {
2932 got_fileindex_entry_free(ie);
2933 return err;
2935 } else {
2936 /* Re-adding a locally deleted file. */
2937 err = got_fileindex_entry_update(ie,
2938 worktree->root_fd, path2, ie->blob_sha1,
2939 worktree->base_commit_id->sha1, 0);
2940 if (err)
2941 return err;
2944 if (is_bad_symlink) {
2945 got_fileindex_entry_filetype_set(ie,
2946 GOT_FILEIDX_MODE_BAD_SYMLINK);
2949 return NULL;
2952 struct merge_file_cb_arg {
2953 struct got_worktree *worktree;
2954 struct got_fileindex *fileindex;
2955 got_worktree_checkout_cb progress_cb;
2956 void *progress_arg;
2957 got_cancel_cb cancel_cb;
2958 void *cancel_arg;
2959 const char *label_orig;
2960 struct got_object_id *commit_id2;
2961 int allow_bad_symlinks;
2964 static const struct got_error *
2965 merge_file_cb(void *arg, struct got_blob_object *blob1,
2966 struct got_blob_object *blob2, FILE *f1, FILE *f2,
2967 struct got_object_id *id1, struct got_object_id *id2,
2968 const char *path1, const char *path2,
2969 mode_t mode1, mode_t mode2, struct got_repository *repo)
2971 static const struct got_error *err = NULL;
2972 struct merge_file_cb_arg *a = arg;
2973 struct got_fileindex_entry *ie;
2974 char *ondisk_path = NULL;
2975 struct stat sb;
2976 unsigned char status;
2977 int local_changes_subsumed;
2978 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2979 char *id_str = NULL, *label_deriv2 = NULL;
2981 if (blob1 && blob2) {
2982 ie = got_fileindex_entry_get(a->fileindex, path2,
2983 strlen(path2));
2984 if (ie == NULL)
2985 return (*a->progress_cb)(a->progress_arg,
2986 GOT_STATUS_MISSING, path2);
2988 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2989 path2) == -1)
2990 return got_error_from_errno("asprintf");
2992 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2993 repo);
2994 if (err)
2995 goto done;
2997 if (status == GOT_STATUS_DELETE) {
2998 err = (*a->progress_cb)(a->progress_arg,
2999 GOT_STATUS_MERGE, path2);
3000 goto done;
3002 if (status != GOT_STATUS_NO_CHANGE &&
3003 status != GOT_STATUS_MODIFY &&
3004 status != GOT_STATUS_CONFLICT &&
3005 status != GOT_STATUS_ADD) {
3006 err = (*a->progress_cb)(a->progress_arg, status, path2);
3007 goto done;
3010 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
3011 char *link_target2;
3012 err = got_object_blob_read_to_str(&link_target2, blob2);
3013 if (err)
3014 goto done;
3015 err = merge_symlink(a->worktree, blob1, ondisk_path,
3016 path2, a->label_orig, link_target2, a->commit_id2,
3017 repo, a->progress_cb, a->progress_arg);
3018 free(link_target2);
3019 } else {
3020 int fd;
3022 f_orig = got_opentemp();
3023 if (f_orig == NULL) {
3024 err = got_error_from_errno("got_opentemp");
3025 goto done;
3027 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3028 f_orig, blob1);
3029 if (err)
3030 goto done;
3032 f_deriv2 = got_opentemp();
3033 if (f_deriv2 == NULL)
3034 goto done;
3035 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3036 f_deriv2, blob2);
3037 if (err)
3038 goto done;
3040 fd = open(ondisk_path,
3041 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3042 if (fd == -1) {
3043 err = got_error_from_errno2("open",
3044 ondisk_path);
3045 goto done;
3047 f_deriv = fdopen(fd, "r");
3048 if (f_deriv == NULL) {
3049 err = got_error_from_errno2("fdopen",
3050 ondisk_path);
3051 close(fd);
3052 goto done;
3054 err = got_object_id_str(&id_str, a->commit_id2);
3055 if (err)
3056 goto done;
3057 if (asprintf(&label_deriv2, "%s: commit %s",
3058 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
3059 err = got_error_from_errno("asprintf");
3060 goto done;
3062 err = merge_file(&local_changes_subsumed, a->worktree,
3063 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
3064 mode2, a->label_orig, NULL, label_deriv2,
3065 GOT_DIFF_ALGORITHM_PATIENCE, repo,
3066 a->progress_cb, a->progress_arg);
3068 } else if (blob1) {
3069 ie = got_fileindex_entry_get(a->fileindex, path1,
3070 strlen(path1));
3071 if (ie == NULL)
3072 return (*a->progress_cb)(a->progress_arg,
3073 GOT_STATUS_MISSING, path1);
3075 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3076 path1) == -1)
3077 return got_error_from_errno("asprintf");
3079 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3080 repo);
3081 if (err)
3082 goto done;
3084 switch (status) {
3085 case GOT_STATUS_NO_CHANGE:
3086 err = (*a->progress_cb)(a->progress_arg,
3087 GOT_STATUS_DELETE, path1);
3088 if (err)
3089 goto done;
3090 err = remove_ondisk_file(a->worktree->root_path, path1);
3091 if (err)
3092 goto done;
3093 if (ie)
3094 got_fileindex_entry_mark_deleted_from_disk(ie);
3095 break;
3096 case GOT_STATUS_DELETE:
3097 case GOT_STATUS_MISSING:
3098 err = (*a->progress_cb)(a->progress_arg,
3099 GOT_STATUS_DELETE, path1);
3100 if (err)
3101 goto done;
3102 if (ie)
3103 got_fileindex_entry_mark_deleted_from_disk(ie);
3104 break;
3105 case GOT_STATUS_ADD: {
3106 struct got_object_id *id;
3107 FILE *blob1_f;
3108 off_t blob1_size;
3110 * Delete the added file only if its content already
3111 * exists in the repository.
3113 err = got_object_blob_file_create(&id, &blob1_f,
3114 &blob1_size, path1);
3115 if (err)
3116 goto done;
3117 if (got_object_id_cmp(id, id1) == 0) {
3118 err = (*a->progress_cb)(a->progress_arg,
3119 GOT_STATUS_DELETE, path1);
3120 if (err)
3121 goto done;
3122 err = remove_ondisk_file(a->worktree->root_path,
3123 path1);
3124 if (err)
3125 goto done;
3126 if (ie)
3127 got_fileindex_entry_remove(a->fileindex,
3128 ie);
3129 } else {
3130 err = (*a->progress_cb)(a->progress_arg,
3131 GOT_STATUS_CANNOT_DELETE, path1);
3133 if (fclose(blob1_f) == EOF && err == NULL)
3134 err = got_error_from_errno("fclose");
3135 free(id);
3136 if (err)
3137 goto done;
3138 break;
3140 case GOT_STATUS_MODIFY:
3141 case GOT_STATUS_CONFLICT:
3142 err = (*a->progress_cb)(a->progress_arg,
3143 GOT_STATUS_CANNOT_DELETE, path1);
3144 if (err)
3145 goto done;
3146 break;
3147 case GOT_STATUS_OBSTRUCTED:
3148 err = (*a->progress_cb)(a->progress_arg, status, path1);
3149 if (err)
3150 goto done;
3151 break;
3152 default:
3153 break;
3155 } else if (blob2) {
3156 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3157 path2) == -1)
3158 return got_error_from_errno("asprintf");
3159 ie = got_fileindex_entry_get(a->fileindex, path2,
3160 strlen(path2));
3161 if (ie) {
3162 err = get_file_status(&status, &sb, ie, ondisk_path,
3163 -1, NULL, repo);
3164 if (err)
3165 goto done;
3166 if (status != GOT_STATUS_NO_CHANGE &&
3167 status != GOT_STATUS_MODIFY &&
3168 status != GOT_STATUS_CONFLICT &&
3169 status != GOT_STATUS_ADD &&
3170 status != GOT_STATUS_DELETE) {
3171 err = (*a->progress_cb)(a->progress_arg,
3172 status, path2);
3173 goto done;
3175 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3176 char *link_target2;
3177 err = got_object_blob_read_to_str(&link_target2,
3178 blob2);
3179 if (err)
3180 goto done;
3181 err = merge_symlink(a->worktree, NULL,
3182 ondisk_path, path2, a->label_orig,
3183 link_target2, a->commit_id2, repo,
3184 a->progress_cb, a->progress_arg);
3185 free(link_target2);
3186 } else if (S_ISREG(sb.st_mode)) {
3187 err = merge_blob(&local_changes_subsumed,
3188 a->worktree, NULL, ondisk_path, path2,
3189 sb.st_mode, a->label_orig, blob2,
3190 a->commit_id2, repo, a->progress_cb,
3191 a->progress_arg);
3192 } else if (status != GOT_STATUS_DELETE) {
3193 err = got_error_path(ondisk_path,
3194 GOT_ERR_FILE_OBSTRUCTED);
3196 if (err)
3197 goto done;
3198 if (status == GOT_STATUS_DELETE) {
3199 /* Re-add file with content from new blob. */
3200 err = add_file(a->worktree, a->fileindex, ie,
3201 ondisk_path, path2, blob2, mode2,
3202 0, 0, 0, a->allow_bad_symlinks,
3203 repo, a->progress_cb, a->progress_arg);
3204 if (err)
3205 goto done;
3207 } else {
3208 err = add_file(a->worktree, a->fileindex, NULL,
3209 ondisk_path, path2, blob2, mode2,
3210 0, 0, 1, a->allow_bad_symlinks,
3211 repo, a->progress_cb, a->progress_arg);
3212 if (err)
3213 goto done;
3216 done:
3217 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3218 err = got_error_from_errno("fclose");
3219 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3220 err = got_error_from_errno("fclose");
3221 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3222 err = got_error_from_errno("fclose");
3223 free(id_str);
3224 free(label_deriv2);
3225 free(ondisk_path);
3226 return err;
3229 static const struct got_error *
3230 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3232 struct got_worktree *worktree = arg;
3234 /* Reject merges into a work tree with mixed base commits. */
3235 if (got_fileindex_entry_has_commit(ie) &&
3236 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3237 SHA1_DIGEST_LENGTH) != 0)
3238 return got_error(GOT_ERR_MIXED_COMMITS);
3240 return NULL;
3243 struct check_merge_conflicts_arg {
3244 struct got_worktree *worktree;
3245 struct got_fileindex *fileindex;
3246 struct got_repository *repo;
3249 static const struct got_error *
3250 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3251 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3252 struct got_object_id *id1, struct got_object_id *id2,
3253 const char *path1, const char *path2,
3254 mode_t mode1, mode_t mode2, struct got_repository *repo)
3256 const struct got_error *err = NULL;
3257 struct check_merge_conflicts_arg *a = arg;
3258 unsigned char status;
3259 struct stat sb;
3260 struct got_fileindex_entry *ie;
3261 const char *path = path2 ? path2 : path1;
3262 struct got_object_id *id = id2 ? id2 : id1;
3263 char *ondisk_path;
3265 if (id == NULL)
3266 return NULL;
3268 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3269 if (ie == NULL)
3270 return NULL;
3272 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3273 == -1)
3274 return got_error_from_errno("asprintf");
3276 /* Reject merges into a work tree with conflicted files. */
3277 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3278 free(ondisk_path);
3279 if (err)
3280 return err;
3281 if (status == GOT_STATUS_CONFLICT)
3282 return got_error(GOT_ERR_CONFLICTS);
3284 return NULL;
3287 static const struct got_error *
3288 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3289 const char *fileindex_path, struct got_object_id *commit_id1,
3290 struct got_object_id *commit_id2, struct got_repository *repo,
3291 got_worktree_checkout_cb progress_cb, void *progress_arg,
3292 got_cancel_cb cancel_cb, void *cancel_arg)
3294 const struct got_error *err = NULL, *sync_err;
3295 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3296 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3297 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3298 struct check_merge_conflicts_arg cmc_arg;
3299 struct merge_file_cb_arg arg;
3300 char *label_orig = NULL;
3301 FILE *f1 = NULL, *f2 = NULL;
3302 int fd1 = -1, fd2 = -1;
3304 if (commit_id1) {
3305 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3306 if (err)
3307 goto done;
3308 err = got_object_id_by_path(&tree_id1, repo, commit1,
3309 worktree->path_prefix);
3310 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3311 goto done;
3313 if (tree_id1) {
3314 char *id_str;
3316 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3317 if (err)
3318 goto done;
3320 err = got_object_id_str(&id_str, commit_id1);
3321 if (err)
3322 goto done;
3324 if (asprintf(&label_orig, "%s: commit %s",
3325 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3326 err = got_error_from_errno("asprintf");
3327 free(id_str);
3328 goto done;
3330 free(id_str);
3332 f1 = got_opentemp();
3333 if (f1 == NULL) {
3334 err = got_error_from_errno("got_opentemp");
3335 goto done;
3339 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3340 if (err)
3341 goto done;
3343 err = got_object_id_by_path(&tree_id2, repo, commit2,
3344 worktree->path_prefix);
3345 if (err)
3346 goto done;
3348 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3349 if (err)
3350 goto done;
3352 f2 = got_opentemp();
3353 if (f2 == NULL) {
3354 err = got_error_from_errno("got_opentemp");
3355 goto done;
3358 fd1 = got_opentempfd();
3359 if (fd1 == -1) {
3360 err = got_error_from_errno("got_opentempfd");
3361 goto done;
3364 fd2 = got_opentempfd();
3365 if (fd2 == -1) {
3366 err = got_error_from_errno("got_opentempfd");
3367 goto done;
3370 cmc_arg.worktree = worktree;
3371 cmc_arg.fileindex = fileindex;
3372 cmc_arg.repo = repo;
3373 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3374 check_merge_conflicts, &cmc_arg, 0);
3375 if (err)
3376 goto done;
3378 arg.worktree = worktree;
3379 arg.fileindex = fileindex;
3380 arg.progress_cb = progress_cb;
3381 arg.progress_arg = progress_arg;
3382 arg.cancel_cb = cancel_cb;
3383 arg.cancel_arg = cancel_arg;
3384 arg.label_orig = label_orig;
3385 arg.commit_id2 = commit_id2;
3386 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3387 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3388 merge_file_cb, &arg, 1);
3389 sync_err = sync_fileindex(fileindex, fileindex_path);
3390 if (sync_err && err == NULL)
3391 err = sync_err;
3392 done:
3393 if (commit1)
3394 got_object_commit_close(commit1);
3395 if (commit2)
3396 got_object_commit_close(commit2);
3397 if (tree1)
3398 got_object_tree_close(tree1);
3399 if (tree2)
3400 got_object_tree_close(tree2);
3401 if (f1 && fclose(f1) == EOF && err == NULL)
3402 err = got_error_from_errno("fclose");
3403 if (f2 && fclose(f2) == EOF && err == NULL)
3404 err = got_error_from_errno("fclose");
3405 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3406 err = got_error_from_errno("close");
3407 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3408 err = got_error_from_errno("close");
3409 free(label_orig);
3410 return err;
3413 const struct got_error *
3414 got_worktree_merge_files(struct got_worktree *worktree,
3415 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3416 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3417 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3419 const struct got_error *err, *unlockerr;
3420 char *fileindex_path = NULL;
3421 struct got_fileindex *fileindex = NULL;
3423 err = lock_worktree(worktree, LOCK_EX);
3424 if (err)
3425 return err;
3427 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3428 if (err)
3429 goto done;
3431 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3432 worktree);
3433 if (err)
3434 goto done;
3436 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3437 commit_id2, repo, progress_cb, progress_arg,
3438 cancel_cb, cancel_arg);
3439 done:
3440 if (fileindex)
3441 got_fileindex_free(fileindex);
3442 free(fileindex_path);
3443 unlockerr = lock_worktree(worktree, LOCK_SH);
3444 if (unlockerr && err == NULL)
3445 err = unlockerr;
3446 return err;
3449 struct diff_dir_cb_arg {
3450 struct got_fileindex *fileindex;
3451 struct got_worktree *worktree;
3452 const char *status_path;
3453 size_t status_path_len;
3454 struct got_repository *repo;
3455 got_worktree_status_cb status_cb;
3456 void *status_arg;
3457 got_cancel_cb cancel_cb;
3458 void *cancel_arg;
3459 /* A pathlist containing per-directory pathlists of ignore patterns. */
3460 struct got_pathlist_head *ignores;
3461 int report_unchanged;
3462 int no_ignores;
3465 static const struct got_error *
3466 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3467 int dirfd, const char *de_name,
3468 got_worktree_status_cb status_cb, void *status_arg,
3469 struct got_repository *repo, int report_unchanged)
3471 const struct got_error *err = NULL;
3472 unsigned char status = GOT_STATUS_NO_CHANGE;
3473 unsigned char staged_status;
3474 struct stat sb;
3475 struct got_object_id blob_id, commit_id, staged_blob_id;
3476 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3477 struct got_object_id *staged_blob_idp = NULL;
3479 staged_status = get_staged_status(ie);
3480 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3481 if (err)
3482 return err;
3484 if (status == GOT_STATUS_NO_CHANGE &&
3485 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3486 return NULL;
3488 if (got_fileindex_entry_has_blob(ie))
3489 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3490 if (got_fileindex_entry_has_commit(ie))
3491 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3492 if (staged_status == GOT_STATUS_ADD ||
3493 staged_status == GOT_STATUS_MODIFY) {
3494 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3495 &staged_blob_id, ie);
3498 return (*status_cb)(status_arg, status, staged_status,
3499 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3502 static const struct got_error *
3503 status_old_new(void *arg, struct got_fileindex_entry *ie,
3504 struct dirent *de, const char *parent_path, int dirfd)
3506 const struct got_error *err = NULL;
3507 struct diff_dir_cb_arg *a = arg;
3508 char *abspath;
3510 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3511 return got_error(GOT_ERR_CANCELLED);
3513 if (got_path_cmp(parent_path, a->status_path,
3514 strlen(parent_path), a->status_path_len) != 0 &&
3515 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3516 return NULL;
3518 if (parent_path[0]) {
3519 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3520 parent_path, de->d_name) == -1)
3521 return got_error_from_errno("asprintf");
3522 } else {
3523 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3524 de->d_name) == -1)
3525 return got_error_from_errno("asprintf");
3528 err = report_file_status(ie, abspath, dirfd, de->d_name,
3529 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3530 free(abspath);
3531 return err;
3534 static const struct got_error *
3535 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3537 struct diff_dir_cb_arg *a = arg;
3538 struct got_object_id blob_id, commit_id;
3539 unsigned char status;
3541 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3542 return got_error(GOT_ERR_CANCELLED);
3544 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3545 return NULL;
3547 got_fileindex_entry_get_blob_id(&blob_id, ie);
3548 got_fileindex_entry_get_commit_id(&commit_id, ie);
3549 if (got_fileindex_entry_has_file_on_disk(ie))
3550 status = GOT_STATUS_MISSING;
3551 else
3552 status = GOT_STATUS_DELETE;
3553 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3554 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3557 static void
3558 free_ignores(struct got_pathlist_head *ignores)
3560 struct got_pathlist_entry *pe;
3562 TAILQ_FOREACH(pe, ignores, entry) {
3563 struct got_pathlist_head *ignorelist = pe->data;
3565 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3567 got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3570 static const struct got_error *
3571 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3573 const struct got_error *err = NULL;
3574 struct got_pathlist_entry *pe = NULL;
3575 struct got_pathlist_head *ignorelist;
3576 char *line = NULL, *pattern, *dirpath = NULL;
3577 size_t linesize = 0;
3578 ssize_t linelen;
3580 ignorelist = calloc(1, sizeof(*ignorelist));
3581 if (ignorelist == NULL)
3582 return got_error_from_errno("calloc");
3583 TAILQ_INIT(ignorelist);
3585 while ((linelen = getline(&line, &linesize, f)) != -1) {
3586 if (linelen > 0 && line[linelen - 1] == '\n')
3587 line[linelen - 1] = '\0';
3589 /* Git's ignores may contain comments. */
3590 if (line[0] == '#')
3591 continue;
3593 /* Git's negated patterns are not (yet?) supported. */
3594 if (line[0] == '!')
3595 continue;
3597 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3598 line) == -1) {
3599 err = got_error_from_errno("asprintf");
3600 goto done;
3602 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3603 if (err)
3604 goto done;
3606 if (ferror(f)) {
3607 err = got_error_from_errno("getline");
3608 goto done;
3611 dirpath = strdup(path);
3612 if (dirpath == NULL) {
3613 err = got_error_from_errno("strdup");
3614 goto done;
3616 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3617 done:
3618 free(line);
3619 if (err || pe == NULL) {
3620 free(dirpath);
3621 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3623 return err;
3626 static int
3627 match_path(const char *pattern, size_t pattern_len, const char *path,
3628 int flags)
3630 char buf[PATH_MAX];
3633 * Trailing slashes signify directories.
3634 * Append a * to make such patterns conform to fnmatch rules.
3636 if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3637 if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3638 return FNM_NOMATCH; /* XXX */
3640 return fnmatch(buf, path, flags);
3643 return fnmatch(pattern, path, flags);
3646 static int
3647 match_ignores(struct got_pathlist_head *ignores, const char *path)
3649 struct got_pathlist_entry *pe;
3651 /* Handle patterns which match in all directories. */
3652 TAILQ_FOREACH(pe, ignores, entry) {
3653 struct got_pathlist_head *ignorelist = pe->data;
3654 struct got_pathlist_entry *pi;
3656 TAILQ_FOREACH(pi, ignorelist, entry) {
3657 const char *p;
3659 if (pi->path_len < 3 ||
3660 strncmp(pi->path, "**/", 3) != 0)
3661 continue;
3662 p = path;
3663 while (*p) {
3664 if (match_path(pi->path + 3,
3665 pi->path_len - 3, p,
3666 FNM_PATHNAME | FNM_LEADING_DIR)) {
3667 /* Retry in next directory. */
3668 while (*p && *p != '/')
3669 p++;
3670 while (*p == '/')
3671 p++;
3672 continue;
3674 return 1;
3680 * The ignores pathlist contains ignore lists from children before
3681 * parents, so we can find the most specific ignorelist by walking
3682 * ignores backwards.
3684 pe = TAILQ_LAST(ignores, got_pathlist_head);
3685 while (pe) {
3686 if (got_path_is_child(path, pe->path, pe->path_len)) {
3687 struct got_pathlist_head *ignorelist = pe->data;
3688 struct got_pathlist_entry *pi;
3689 TAILQ_FOREACH(pi, ignorelist, entry) {
3690 int flags = FNM_LEADING_DIR;
3691 if (strstr(pi->path, "/**/") == NULL)
3692 flags |= FNM_PATHNAME;
3693 if (match_path(pi->path, pi->path_len,
3694 path, flags))
3695 continue;
3696 return 1;
3699 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3702 return 0;
3705 static const struct got_error *
3706 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3707 const char *path, int dirfd, const char *ignores_filename)
3709 const struct got_error *err = NULL;
3710 char *ignorespath;
3711 int fd = -1;
3712 FILE *ignoresfile = NULL;
3714 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3715 path[0] ? "/" : "", ignores_filename) == -1)
3716 return got_error_from_errno("asprintf");
3718 if (dirfd != -1) {
3719 fd = openat(dirfd, ignores_filename,
3720 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3721 if (fd == -1) {
3722 if (errno != ENOENT && errno != EACCES)
3723 err = got_error_from_errno2("openat",
3724 ignorespath);
3725 } else {
3726 ignoresfile = fdopen(fd, "r");
3727 if (ignoresfile == NULL)
3728 err = got_error_from_errno2("fdopen",
3729 ignorespath);
3730 else {
3731 fd = -1;
3732 err = read_ignores(ignores, path, ignoresfile);
3735 } else {
3736 ignoresfile = fopen(ignorespath, "re");
3737 if (ignoresfile == NULL) {
3738 if (errno != ENOENT && errno != EACCES)
3739 err = got_error_from_errno2("fopen",
3740 ignorespath);
3741 } else
3742 err = read_ignores(ignores, path, ignoresfile);
3745 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3746 err = got_error_from_errno2("fclose", path);
3747 if (fd != -1 && close(fd) == -1 && err == NULL)
3748 err = got_error_from_errno2("close", path);
3749 free(ignorespath);
3750 return err;
3753 static const struct got_error *
3754 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3755 int dirfd)
3757 const struct got_error *err = NULL;
3758 struct diff_dir_cb_arg *a = arg;
3759 char *path = NULL;
3761 if (ignore != NULL)
3762 *ignore = 0;
3764 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3765 return got_error(GOT_ERR_CANCELLED);
3767 if (parent_path[0]) {
3768 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3769 return got_error_from_errno("asprintf");
3770 } else {
3771 path = de->d_name;
3774 if (de->d_type == DT_DIR) {
3775 if (!a->no_ignores && ignore != NULL &&
3776 match_ignores(a->ignores, path))
3777 *ignore = 1;
3778 } else if (!match_ignores(a->ignores, path) &&
3779 got_path_is_child(path, a->status_path, a->status_path_len))
3780 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3781 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3782 if (parent_path[0])
3783 free(path);
3784 return err;
3787 static const struct got_error *
3788 status_traverse(void *arg, const char *path, int dirfd)
3790 const struct got_error *err = NULL;
3791 struct diff_dir_cb_arg *a = arg;
3793 if (a->no_ignores)
3794 return NULL;
3796 err = add_ignores(a->ignores, a->worktree->root_path,
3797 path, dirfd, ".cvsignore");
3798 if (err)
3799 return err;
3801 err = add_ignores(a->ignores, a->worktree->root_path, path,
3802 dirfd, ".gitignore");
3804 return err;
3807 static const struct got_error *
3808 report_single_file_status(const char *path, const char *ondisk_path,
3809 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3810 void *status_arg, struct got_repository *repo, int report_unchanged,
3811 struct got_pathlist_head *ignores, int no_ignores)
3813 struct got_fileindex_entry *ie;
3814 struct stat sb;
3816 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3817 if (ie)
3818 return report_file_status(ie, ondisk_path, -1, NULL,
3819 status_cb, status_arg, repo, report_unchanged);
3821 if (lstat(ondisk_path, &sb) == -1) {
3822 if (errno != ENOENT)
3823 return got_error_from_errno2("lstat", ondisk_path);
3824 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3825 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3828 if (!no_ignores && match_ignores(ignores, path))
3829 return NULL;
3831 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3832 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3833 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3835 return NULL;
3838 static const struct got_error *
3839 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3840 const char *root_path, const char *path)
3842 const struct got_error *err;
3843 char *parent_path, *next_parent_path = NULL;
3845 err = add_ignores(ignores, root_path, "", -1,
3846 ".cvsignore");
3847 if (err)
3848 return err;
3850 err = add_ignores(ignores, root_path, "", -1,
3851 ".gitignore");
3852 if (err)
3853 return err;
3855 err = got_path_dirname(&parent_path, path);
3856 if (err) {
3857 if (err->code == GOT_ERR_BAD_PATH)
3858 return NULL; /* cannot traverse parent */
3859 return err;
3861 for (;;) {
3862 err = add_ignores(ignores, root_path, parent_path, -1,
3863 ".cvsignore");
3864 if (err)
3865 break;
3866 err = add_ignores(ignores, root_path, parent_path, -1,
3867 ".gitignore");
3868 if (err)
3869 break;
3870 err = got_path_dirname(&next_parent_path, parent_path);
3871 if (err) {
3872 if (err->code == GOT_ERR_BAD_PATH)
3873 err = NULL; /* traversed everything */
3874 break;
3876 if (got_path_is_root_dir(parent_path))
3877 break;
3878 free(parent_path);
3879 parent_path = next_parent_path;
3880 next_parent_path = NULL;
3883 free(parent_path);
3884 free(next_parent_path);
3885 return err;
3888 struct find_missing_children_args {
3889 const char *parent_path;
3890 size_t parent_len;
3891 struct got_pathlist_head *children;
3892 got_cancel_cb cancel_cb;
3893 void *cancel_arg;
3896 static const struct got_error *
3897 find_missing_children(void *arg, struct got_fileindex_entry *ie)
3899 const struct got_error *err = NULL;
3900 struct find_missing_children_args *a = arg;
3902 if (a->cancel_cb) {
3903 err = a->cancel_cb(a->cancel_arg);
3904 if (err)
3905 return err;
3908 if (got_path_is_child(ie->path, a->parent_path, a->parent_len))
3909 err = got_pathlist_append(a->children, ie->path, NULL);
3911 return err;
3914 static const struct got_error *
3915 report_children(struct got_pathlist_head *children,
3916 struct got_worktree *worktree, struct got_fileindex *fileindex,
3917 struct got_repository *repo, int is_root_dir, int report_unchanged,
3918 struct got_pathlist_head *ignores, int no_ignores,
3919 got_worktree_status_cb status_cb, void *status_arg,
3920 got_cancel_cb cancel_cb, void *cancel_arg)
3922 const struct got_error *err = NULL;
3923 struct got_pathlist_entry *pe;
3924 char *ondisk_path = NULL;
3926 TAILQ_FOREACH(pe, children, entry) {
3927 if (cancel_cb) {
3928 err = cancel_cb(cancel_arg);
3929 if (err)
3930 break;
3933 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
3934 !is_root_dir ? "/" : "", pe->path) == -1) {
3935 err = got_error_from_errno("asprintf");
3936 ondisk_path = NULL;
3937 break;
3940 err = report_single_file_status(pe->path, ondisk_path,
3941 fileindex, status_cb, status_arg, repo, report_unchanged,
3942 ignores, no_ignores);
3943 if (err)
3944 break;
3946 free(ondisk_path);
3947 ondisk_path = NULL;
3950 free(ondisk_path);
3951 return err;
3954 static const struct got_error *
3955 worktree_status(struct got_worktree *worktree, const char *path,
3956 struct got_fileindex *fileindex, struct got_repository *repo,
3957 got_worktree_status_cb status_cb, void *status_arg,
3958 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3959 int report_unchanged)
3961 const struct got_error *err = NULL;
3962 int fd = -1;
3963 struct got_fileindex_diff_dir_cb fdiff_cb;
3964 struct diff_dir_cb_arg arg;
3965 char *ondisk_path = NULL;
3966 struct got_pathlist_head ignores, missing_children;
3967 struct got_fileindex_entry *ie;
3969 TAILQ_INIT(&ignores);
3970 TAILQ_INIT(&missing_children);
3972 if (asprintf(&ondisk_path, "%s%s%s",
3973 worktree->root_path, path[0] ? "/" : "", path) == -1)
3974 return got_error_from_errno("asprintf");
3976 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3977 if (ie) {
3978 err = report_single_file_status(path, ondisk_path,
3979 fileindex, status_cb, status_arg, repo,
3980 report_unchanged, &ignores, no_ignores);
3981 goto done;
3982 } else {
3983 struct find_missing_children_args fmca;
3984 fmca.parent_path = path;
3985 fmca.parent_len = strlen(path);
3986 fmca.children = &missing_children;
3987 fmca.cancel_cb = cancel_cb;
3988 fmca.cancel_arg = cancel_arg;
3989 err = got_fileindex_for_each_entry_safe(fileindex,
3990 find_missing_children, &fmca);
3991 if (err)
3992 goto done;
3995 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3996 if (fd == -1) {
3997 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3998 !got_err_open_nofollow_on_symlink())
3999 err = got_error_from_errno2("open", ondisk_path);
4000 else {
4001 if (!no_ignores) {
4002 err = add_ignores_from_parent_paths(&ignores,
4003 worktree->root_path, ondisk_path);
4004 if (err)
4005 goto done;
4007 if (TAILQ_EMPTY(&missing_children)) {
4008 err = report_single_file_status(path,
4009 ondisk_path, fileindex,
4010 status_cb, status_arg, repo,
4011 report_unchanged, &ignores, no_ignores);
4012 if (err)
4013 goto done;
4014 } else {
4015 err = report_children(&missing_children,
4016 worktree, fileindex, repo,
4017 (path[0] == '\0'), report_unchanged,
4018 &ignores, no_ignores,
4019 status_cb, status_arg,
4020 cancel_cb, cancel_arg);
4021 if (err)
4022 goto done;
4025 } else {
4026 fdiff_cb.diff_old_new = status_old_new;
4027 fdiff_cb.diff_old = status_old;
4028 fdiff_cb.diff_new = status_new;
4029 fdiff_cb.diff_traverse = status_traverse;
4030 arg.fileindex = fileindex;
4031 arg.worktree = worktree;
4032 arg.status_path = path;
4033 arg.status_path_len = strlen(path);
4034 arg.repo = repo;
4035 arg.status_cb = status_cb;
4036 arg.status_arg = status_arg;
4037 arg.cancel_cb = cancel_cb;
4038 arg.cancel_arg = cancel_arg;
4039 arg.report_unchanged = report_unchanged;
4040 arg.no_ignores = no_ignores;
4041 if (!no_ignores) {
4042 err = add_ignores_from_parent_paths(&ignores,
4043 worktree->root_path, path);
4044 if (err)
4045 goto done;
4047 arg.ignores = &ignores;
4048 err = got_fileindex_diff_dir(fileindex, fd,
4049 worktree->root_path, path, repo, &fdiff_cb, &arg);
4051 done:
4052 free_ignores(&ignores);
4053 if (fd != -1 && close(fd) == -1 && err == NULL)
4054 err = got_error_from_errno("close");
4055 free(ondisk_path);
4056 return err;
4059 const struct got_error *
4060 got_worktree_status(struct got_worktree *worktree,
4061 struct got_pathlist_head *paths, struct got_repository *repo,
4062 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
4063 got_cancel_cb cancel_cb, void *cancel_arg)
4065 const struct got_error *err = NULL;
4066 char *fileindex_path = NULL;
4067 struct got_fileindex *fileindex = NULL;
4068 struct got_pathlist_entry *pe;
4070 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4071 if (err)
4072 return err;
4074 TAILQ_FOREACH(pe, paths, entry) {
4075 err = worktree_status(worktree, pe->path, fileindex, repo,
4076 status_cb, status_arg, cancel_cb, cancel_arg,
4077 no_ignores, 0);
4078 if (err)
4079 break;
4081 free(fileindex_path);
4082 got_fileindex_free(fileindex);
4083 return err;
4086 const struct got_error *
4087 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
4088 const char *arg)
4090 const struct got_error *err = NULL;
4091 char *resolved = NULL, *cwd = NULL, *path = NULL;
4092 size_t len;
4093 struct stat sb;
4094 char *abspath = NULL;
4095 char canonpath[PATH_MAX];
4097 *wt_path = NULL;
4099 cwd = getcwd(NULL, 0);
4100 if (cwd == NULL)
4101 return got_error_from_errno("getcwd");
4103 if (lstat(arg, &sb) == -1) {
4104 if (errno != ENOENT) {
4105 err = got_error_from_errno2("lstat", arg);
4106 goto done;
4108 sb.st_mode = 0;
4110 if (S_ISLNK(sb.st_mode)) {
4112 * We cannot use realpath(3) with symlinks since we want to
4113 * operate on the symlink itself.
4114 * But we can make the path absolute, assuming it is relative
4115 * to the current working directory, and then canonicalize it.
4117 if (!got_path_is_absolute(arg)) {
4118 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4119 err = got_error_from_errno("asprintf");
4120 goto done;
4124 err = got_canonpath(abspath ? abspath : arg, canonpath,
4125 sizeof(canonpath));
4126 if (err)
4127 goto done;
4128 resolved = strdup(canonpath);
4129 if (resolved == NULL) {
4130 err = got_error_from_errno("strdup");
4131 goto done;
4133 } else {
4134 resolved = realpath(arg, NULL);
4135 if (resolved == NULL) {
4136 if (errno != ENOENT) {
4137 err = got_error_from_errno2("realpath", arg);
4138 goto done;
4140 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4141 err = got_error_from_errno("asprintf");
4142 goto done;
4144 err = got_canonpath(abspath, canonpath,
4145 sizeof(canonpath));
4146 if (err)
4147 goto done;
4148 resolved = strdup(canonpath);
4149 if (resolved == NULL) {
4150 err = got_error_from_errno("strdup");
4151 goto done;
4156 if (strncmp(got_worktree_get_root_path(worktree), resolved,
4157 strlen(got_worktree_get_root_path(worktree)))) {
4158 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
4159 goto done;
4162 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
4163 err = got_path_skip_common_ancestor(&path,
4164 got_worktree_get_root_path(worktree), resolved);
4165 if (err)
4166 goto done;
4167 } else {
4168 path = strdup("");
4169 if (path == NULL) {
4170 err = got_error_from_errno("strdup");
4171 goto done;
4175 /* XXX status walk can't deal with trailing slash! */
4176 len = strlen(path);
4177 while (len > 0 && path[len - 1] == '/') {
4178 path[len - 1] = '\0';
4179 len--;
4181 done:
4182 free(abspath);
4183 free(resolved);
4184 free(cwd);
4185 if (err == NULL)
4186 *wt_path = path;
4187 else
4188 free(path);
4189 return err;
4192 struct schedule_addition_args {
4193 struct got_worktree *worktree;
4194 struct got_fileindex *fileindex;
4195 got_worktree_checkout_cb progress_cb;
4196 void *progress_arg;
4197 struct got_repository *repo;
4200 static const struct got_error *
4201 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4202 const char *relpath, struct got_object_id *blob_id,
4203 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4204 int dirfd, const char *de_name)
4206 struct schedule_addition_args *a = arg;
4207 const struct got_error *err = NULL;
4208 struct got_fileindex_entry *ie;
4209 struct stat sb;
4210 char *ondisk_path;
4212 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4213 relpath) == -1)
4214 return got_error_from_errno("asprintf");
4216 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4217 if (ie) {
4218 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4219 de_name, a->repo);
4220 if (err)
4221 goto done;
4222 /* Re-adding an existing entry is a no-op. */
4223 if (status == GOT_STATUS_ADD)
4224 goto done;
4225 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4226 if (err)
4227 goto done;
4230 if (status != GOT_STATUS_UNVERSIONED) {
4231 if (status == GOT_STATUS_NONEXISTENT)
4232 err = got_error_set_errno(ENOENT, ondisk_path);
4233 else
4234 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4235 goto done;
4238 err = got_fileindex_entry_alloc(&ie, relpath);
4239 if (err)
4240 goto done;
4241 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4242 relpath, NULL, NULL, 1);
4243 if (err) {
4244 got_fileindex_entry_free(ie);
4245 goto done;
4247 err = got_fileindex_entry_add(a->fileindex, ie);
4248 if (err) {
4249 got_fileindex_entry_free(ie);
4250 goto done;
4252 done:
4253 free(ondisk_path);
4254 if (err)
4255 return err;
4256 if (status == GOT_STATUS_ADD)
4257 return NULL;
4258 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4261 const struct got_error *
4262 got_worktree_schedule_add(struct got_worktree *worktree,
4263 struct got_pathlist_head *paths,
4264 got_worktree_checkout_cb progress_cb, void *progress_arg,
4265 struct got_repository *repo, int no_ignores)
4267 struct got_fileindex *fileindex = NULL;
4268 char *fileindex_path = NULL;
4269 const struct got_error *err = NULL, *sync_err, *unlockerr;
4270 struct got_pathlist_entry *pe;
4271 struct schedule_addition_args saa;
4273 err = lock_worktree(worktree, LOCK_EX);
4274 if (err)
4275 return err;
4277 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4278 if (err)
4279 goto done;
4281 saa.worktree = worktree;
4282 saa.fileindex = fileindex;
4283 saa.progress_cb = progress_cb;
4284 saa.progress_arg = progress_arg;
4285 saa.repo = repo;
4287 TAILQ_FOREACH(pe, paths, entry) {
4288 err = worktree_status(worktree, pe->path, fileindex, repo,
4289 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4290 if (err)
4291 break;
4293 sync_err = sync_fileindex(fileindex, fileindex_path);
4294 if (sync_err && err == NULL)
4295 err = sync_err;
4296 done:
4297 free(fileindex_path);
4298 if (fileindex)
4299 got_fileindex_free(fileindex);
4300 unlockerr = lock_worktree(worktree, LOCK_SH);
4301 if (unlockerr && err == NULL)
4302 err = unlockerr;
4303 return err;
4306 struct schedule_deletion_args {
4307 struct got_worktree *worktree;
4308 struct got_fileindex *fileindex;
4309 got_worktree_delete_cb progress_cb;
4310 void *progress_arg;
4311 struct got_repository *repo;
4312 int delete_local_mods;
4313 int keep_on_disk;
4314 int ignore_missing_paths;
4315 const char *status_codes;
4318 static const struct got_error *
4319 schedule_for_deletion(void *arg, unsigned char status,
4320 unsigned char staged_status, const char *relpath,
4321 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4322 struct got_object_id *commit_id, int dirfd, const char *de_name)
4324 struct schedule_deletion_args *a = arg;
4325 const struct got_error *err = NULL;
4326 struct got_fileindex_entry *ie = NULL;
4327 struct stat sb;
4328 char *ondisk_path;
4330 if (status == GOT_STATUS_NONEXISTENT) {
4331 if (a->ignore_missing_paths)
4332 return NULL;
4333 return got_error_set_errno(ENOENT, relpath);
4336 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4337 if (ie == NULL)
4338 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4340 staged_status = get_staged_status(ie);
4341 if (staged_status != GOT_STATUS_NO_CHANGE) {
4342 if (staged_status == GOT_STATUS_DELETE)
4343 return NULL;
4344 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4347 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4348 relpath) == -1)
4349 return got_error_from_errno("asprintf");
4351 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4352 a->repo);
4353 if (err)
4354 goto done;
4356 if (a->status_codes) {
4357 size_t ncodes = strlen(a->status_codes);
4358 int i;
4359 for (i = 0; i < ncodes ; i++) {
4360 if (status == a->status_codes[i])
4361 break;
4363 if (i == ncodes) {
4364 /* Do not delete files in non-matching status. */
4365 free(ondisk_path);
4366 return NULL;
4368 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4369 a->status_codes[i] != GOT_STATUS_MISSING) {
4370 static char msg[64];
4371 snprintf(msg, sizeof(msg),
4372 "invalid status code '%c'", a->status_codes[i]);
4373 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4374 goto done;
4378 if (status != GOT_STATUS_NO_CHANGE) {
4379 if (status == GOT_STATUS_DELETE)
4380 goto done;
4381 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4382 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4383 goto done;
4385 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4386 err = got_error_set_errno(ENOENT, relpath);
4387 goto done;
4389 if (status != GOT_STATUS_MODIFY &&
4390 status != GOT_STATUS_MISSING) {
4391 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4392 goto done;
4396 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4397 size_t root_len;
4399 if (dirfd != -1) {
4400 if (unlinkat(dirfd, de_name, 0) == -1) {
4401 err = got_error_from_errno2("unlinkat",
4402 ondisk_path);
4403 goto done;
4405 } else if (unlink(ondisk_path) == -1) {
4406 err = got_error_from_errno2("unlink", ondisk_path);
4407 goto done;
4410 root_len = strlen(a->worktree->root_path);
4411 do {
4412 char *parent;
4413 err = got_path_dirname(&parent, ondisk_path);
4414 if (err)
4415 goto done;
4416 free(ondisk_path);
4417 ondisk_path = parent;
4418 if (rmdir(ondisk_path) == -1) {
4419 if (errno != ENOTEMPTY)
4420 err = got_error_from_errno2("rmdir",
4421 ondisk_path);
4422 break;
4424 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4425 strlen(ondisk_path), root_len) != 0);
4428 got_fileindex_entry_mark_deleted_from_disk(ie);
4429 done:
4430 free(ondisk_path);
4431 if (err)
4432 return err;
4433 if (status == GOT_STATUS_DELETE)
4434 return NULL;
4435 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4436 staged_status, relpath);
4439 const struct got_error *
4440 got_worktree_schedule_delete(struct got_worktree *worktree,
4441 struct got_pathlist_head *paths, int delete_local_mods,
4442 const char *status_codes,
4443 got_worktree_delete_cb progress_cb, void *progress_arg,
4444 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4446 struct got_fileindex *fileindex = NULL;
4447 char *fileindex_path = NULL;
4448 const struct got_error *err = NULL, *sync_err, *unlockerr;
4449 struct got_pathlist_entry *pe;
4450 struct schedule_deletion_args sda;
4452 err = lock_worktree(worktree, LOCK_EX);
4453 if (err)
4454 return err;
4456 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4457 if (err)
4458 goto done;
4460 sda.worktree = worktree;
4461 sda.fileindex = fileindex;
4462 sda.progress_cb = progress_cb;
4463 sda.progress_arg = progress_arg;
4464 sda.repo = repo;
4465 sda.delete_local_mods = delete_local_mods;
4466 sda.keep_on_disk = keep_on_disk;
4467 sda.ignore_missing_paths = ignore_missing_paths;
4468 sda.status_codes = status_codes;
4470 TAILQ_FOREACH(pe, paths, entry) {
4471 err = worktree_status(worktree, pe->path, fileindex, repo,
4472 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4473 if (err)
4474 break;
4476 sync_err = sync_fileindex(fileindex, fileindex_path);
4477 if (sync_err && err == NULL)
4478 err = sync_err;
4479 done:
4480 free(fileindex_path);
4481 if (fileindex)
4482 got_fileindex_free(fileindex);
4483 unlockerr = lock_worktree(worktree, LOCK_SH);
4484 if (unlockerr && err == NULL)
4485 err = unlockerr;
4486 return err;
4489 static const struct got_error *
4490 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4492 const struct got_error *err = NULL;
4493 char *line = NULL;
4494 size_t linesize = 0, n;
4495 ssize_t linelen;
4497 linelen = getline(&line, &linesize, infile);
4498 if (linelen == -1) {
4499 if (ferror(infile)) {
4500 err = got_error_from_errno("getline");
4501 goto done;
4503 return NULL;
4505 if (outfile) {
4506 n = fwrite(line, 1, linelen, outfile);
4507 if (n != linelen) {
4508 err = got_ferror(outfile, GOT_ERR_IO);
4509 goto done;
4512 if (rejectfile) {
4513 n = fwrite(line, 1, linelen, rejectfile);
4514 if (n != linelen)
4515 err = got_ferror(rejectfile, GOT_ERR_IO);
4517 done:
4518 free(line);
4519 return err;
4522 static const struct got_error *
4523 skip_one_line(FILE *f)
4525 char *line = NULL;
4526 size_t linesize = 0;
4527 ssize_t linelen;
4529 linelen = getline(&line, &linesize, f);
4530 if (linelen == -1) {
4531 if (ferror(f))
4532 return got_error_from_errno("getline");
4533 return NULL;
4535 free(line);
4536 return NULL;
4539 static const struct got_error *
4540 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4541 int start_old, int end_old, int start_new, int end_new,
4542 FILE *outfile, FILE *rejectfile)
4544 const struct got_error *err;
4546 /* Copy old file's lines leading up to patch. */
4547 while (!feof(f1) && *line_cur1 < start_old) {
4548 err = copy_one_line(f1, outfile, NULL);
4549 if (err)
4550 return err;
4551 (*line_cur1)++;
4553 /* Skip new file's lines leading up to patch. */
4554 while (!feof(f2) && *line_cur2 < start_new) {
4555 if (rejectfile)
4556 err = copy_one_line(f2, NULL, rejectfile);
4557 else
4558 err = skip_one_line(f2);
4559 if (err)
4560 return err;
4561 (*line_cur2)++;
4563 /* Copy patched lines. */
4564 while (!feof(f2) && *line_cur2 <= end_new) {
4565 err = copy_one_line(f2, outfile, NULL);
4566 if (err)
4567 return err;
4568 (*line_cur2)++;
4570 /* Skip over old file's replaced lines. */
4571 while (!feof(f1) && *line_cur1 <= end_old) {
4572 if (rejectfile)
4573 err = copy_one_line(f1, NULL, rejectfile);
4574 else
4575 err = skip_one_line(f1);
4576 if (err)
4577 return err;
4578 (*line_cur1)++;
4581 return NULL;
4584 static const struct got_error *
4585 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4586 FILE *outfile, FILE *rejectfile)
4588 const struct got_error *err;
4590 if (outfile) {
4591 /* Copy old file's lines until EOF. */
4592 while (!feof(f1)) {
4593 err = copy_one_line(f1, outfile, NULL);
4594 if (err)
4595 return err;
4596 (*line_cur1)++;
4599 if (rejectfile) {
4600 /* Copy new file's lines until EOF. */
4601 while (!feof(f2)) {
4602 err = copy_one_line(f2, NULL, rejectfile);
4603 if (err)
4604 return err;
4605 (*line_cur2)++;
4609 return NULL;
4612 static const struct got_error *
4613 apply_or_reject_change(int *choice, int *nchunks_used,
4614 struct diff_result *diff_result, int n,
4615 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4616 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4617 got_worktree_patch_cb patch_cb, void *patch_arg)
4619 const struct got_error *err = NULL;
4620 struct diff_chunk_context cc = {};
4621 int start_old, end_old, start_new, end_new;
4622 FILE *hunkfile;
4623 struct diff_output_unidiff_state *diff_state;
4624 struct diff_input_info diff_info;
4625 int rc;
4627 *choice = GOT_PATCH_CHOICE_NONE;
4629 /* Get changed line numbers without context lines for copy_change(). */
4630 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4631 start_old = cc.left.start;
4632 end_old = cc.left.end;
4633 start_new = cc.right.start;
4634 end_new = cc.right.end;
4636 /* Get the same change with context lines for display. */
4637 memset(&cc, 0, sizeof(cc));
4638 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4640 memset(&diff_info, 0, sizeof(diff_info));
4641 diff_info.left_path = relpath;
4642 diff_info.right_path = relpath;
4644 diff_state = diff_output_unidiff_state_alloc();
4645 if (diff_state == NULL)
4646 return got_error_set_errno(ENOMEM,
4647 "diff_output_unidiff_state_alloc");
4649 hunkfile = got_opentemp();
4650 if (hunkfile == NULL) {
4651 err = got_error_from_errno("got_opentemp");
4652 goto done;
4655 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4656 diff_result, &cc);
4657 if (rc != DIFF_RC_OK) {
4658 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4659 goto done;
4662 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4663 err = got_ferror(hunkfile, GOT_ERR_IO);
4664 goto done;
4667 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4668 hunkfile, changeno, nchanges);
4669 if (err)
4670 goto done;
4672 switch (*choice) {
4673 case GOT_PATCH_CHOICE_YES:
4674 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4675 end_old, start_new, end_new, outfile, rejectfile);
4676 break;
4677 case GOT_PATCH_CHOICE_NO:
4678 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4679 end_old, start_new, end_new, rejectfile, outfile);
4680 break;
4681 case GOT_PATCH_CHOICE_QUIT:
4682 break;
4683 default:
4684 err = got_error(GOT_ERR_PATCH_CHOICE);
4685 break;
4687 done:
4688 diff_output_unidiff_state_free(diff_state);
4689 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4690 err = got_error_from_errno("fclose");
4691 return err;
4694 struct revert_file_args {
4695 struct got_worktree *worktree;
4696 struct got_fileindex *fileindex;
4697 got_worktree_checkout_cb progress_cb;
4698 void *progress_arg;
4699 got_worktree_patch_cb patch_cb;
4700 void *patch_arg;
4701 struct got_repository *repo;
4702 int unlink_added_files;
4705 static const struct got_error *
4706 create_patched_content(char **path_outfile, int reverse_patch,
4707 struct got_object_id *blob_id, const char *path2,
4708 int dirfd2, const char *de_name2,
4709 const char *relpath, struct got_repository *repo,
4710 got_worktree_patch_cb patch_cb, void *patch_arg)
4712 const struct got_error *err, *free_err;
4713 struct got_blob_object *blob = NULL;
4714 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4715 int fd = -1, fd2 = -1;
4716 char link_target[PATH_MAX];
4717 ssize_t link_len = 0;
4718 char *path1 = NULL, *id_str = NULL;
4719 struct stat sb2;
4720 struct got_diffreg_result *diffreg_result = NULL;
4721 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4722 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4724 *path_outfile = NULL;
4726 err = got_object_id_str(&id_str, blob_id);
4727 if (err)
4728 return err;
4730 if (dirfd2 != -1) {
4731 fd2 = openat(dirfd2, de_name2,
4732 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4733 if (fd2 == -1) {
4734 if (!got_err_open_nofollow_on_symlink()) {
4735 err = got_error_from_errno2("openat", path2);
4736 goto done;
4738 link_len = readlinkat(dirfd2, de_name2,
4739 link_target, sizeof(link_target));
4740 if (link_len == -1) {
4741 return got_error_from_errno2("readlinkat",
4742 path2);
4744 sb2.st_mode = S_IFLNK;
4745 sb2.st_size = link_len;
4747 } else {
4748 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4749 if (fd2 == -1) {
4750 if (!got_err_open_nofollow_on_symlink()) {
4751 err = got_error_from_errno2("open", path2);
4752 goto done;
4754 link_len = readlink(path2, link_target,
4755 sizeof(link_target));
4756 if (link_len == -1)
4757 return got_error_from_errno2("readlink", path2);
4758 sb2.st_mode = S_IFLNK;
4759 sb2.st_size = link_len;
4762 if (fd2 != -1) {
4763 if (fstat(fd2, &sb2) == -1) {
4764 err = got_error_from_errno2("fstat", path2);
4765 goto done;
4768 f2 = fdopen(fd2, "r");
4769 if (f2 == NULL) {
4770 err = got_error_from_errno2("fdopen", path2);
4771 goto done;
4773 fd2 = -1;
4774 } else {
4775 size_t n;
4776 f2 = got_opentemp();
4777 if (f2 == NULL) {
4778 err = got_error_from_errno2("got_opentemp", path2);
4779 goto done;
4781 n = fwrite(link_target, 1, link_len, f2);
4782 if (n != link_len) {
4783 err = got_ferror(f2, GOT_ERR_IO);
4784 goto done;
4786 if (fflush(f2) == EOF) {
4787 err = got_error_from_errno("fflush");
4788 goto done;
4790 rewind(f2);
4793 fd = got_opentempfd();
4794 if (fd == -1) {
4795 err = got_error_from_errno("got_opentempfd");
4796 goto done;
4799 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4800 if (err)
4801 goto done;
4803 err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4804 if (err)
4805 goto done;
4807 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4808 if (err)
4809 goto done;
4811 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4812 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4813 if (err)
4814 goto done;
4816 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4817 "");
4818 if (err)
4819 goto done;
4821 if (fseek(f1, 0L, SEEK_SET) == -1)
4822 return got_ferror(f1, GOT_ERR_IO);
4823 if (fseek(f2, 0L, SEEK_SET) == -1)
4824 return got_ferror(f2, GOT_ERR_IO);
4826 /* Count the number of actual changes in the diff result. */
4827 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4828 struct diff_chunk_context cc = {};
4829 diff_chunk_context_load_change(&cc, &nchunks_used,
4830 diffreg_result->result, n, 0);
4831 nchanges++;
4833 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4834 int choice;
4835 err = apply_or_reject_change(&choice, &nchunks_used,
4836 diffreg_result->result, n, relpath, f1, f2,
4837 &line_cur1, &line_cur2,
4838 reverse_patch ? NULL : outfile,
4839 reverse_patch ? outfile : NULL,
4840 ++i, nchanges, patch_cb, patch_arg);
4841 if (err)
4842 goto done;
4843 if (choice == GOT_PATCH_CHOICE_YES)
4844 have_content = 1;
4845 else if (choice == GOT_PATCH_CHOICE_QUIT)
4846 break;
4848 if (have_content) {
4849 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4850 reverse_patch ? NULL : outfile,
4851 reverse_patch ? outfile : NULL);
4852 if (err)
4853 goto done;
4855 if (!S_ISLNK(sb2.st_mode)) {
4856 mode_t mode;
4858 mode = apply_umask(sb2.st_mode);
4859 if (fchmod(fileno(outfile), mode) == -1) {
4860 err = got_error_from_errno2("fchmod", path2);
4861 goto done;
4865 done:
4866 free(id_str);
4867 if (fd != -1 && close(fd) == -1 && err == NULL)
4868 err = got_error_from_errno("close");
4869 if (blob)
4870 got_object_blob_close(blob);
4871 free_err = got_diffreg_result_free(diffreg_result);
4872 if (err == NULL)
4873 err = free_err;
4874 if (f1 && fclose(f1) == EOF && err == NULL)
4875 err = got_error_from_errno2("fclose", path1);
4876 if (f2 && fclose(f2) == EOF && err == NULL)
4877 err = got_error_from_errno2("fclose", path2);
4878 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4879 err = got_error_from_errno2("close", path2);
4880 if (outfile && fclose(outfile) == EOF && err == NULL)
4881 err = got_error_from_errno2("fclose", *path_outfile);
4882 if (path1 && unlink(path1) == -1 && err == NULL)
4883 err = got_error_from_errno2("unlink", path1);
4884 if (err || !have_content) {
4885 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4886 err = got_error_from_errno2("unlink", *path_outfile);
4887 free(*path_outfile);
4888 *path_outfile = NULL;
4890 free(path1);
4891 return err;
4894 static const struct got_error *
4895 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4896 const char *relpath, struct got_object_id *blob_id,
4897 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4898 int dirfd, const char *de_name)
4900 struct revert_file_args *a = arg;
4901 const struct got_error *err = NULL;
4902 char *parent_path = NULL;
4903 struct got_fileindex_entry *ie;
4904 struct got_commit_object *base_commit = NULL;
4905 struct got_tree_object *tree = NULL;
4906 struct got_object_id *tree_id = NULL;
4907 const struct got_tree_entry *te = NULL;
4908 char *tree_path = NULL, *te_name;
4909 char *ondisk_path = NULL, *path_content = NULL;
4910 struct got_blob_object *blob = NULL;
4911 int fd = -1;
4913 /* Reverting a staged deletion is a no-op. */
4914 if (status == GOT_STATUS_DELETE &&
4915 staged_status != GOT_STATUS_NO_CHANGE)
4916 return NULL;
4918 if (status == GOT_STATUS_UNVERSIONED)
4919 return (*a->progress_cb)(a->progress_arg,
4920 GOT_STATUS_UNVERSIONED, relpath);
4922 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4923 if (ie == NULL)
4924 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4926 /* Construct in-repository path of tree which contains this blob. */
4927 err = got_path_dirname(&parent_path, ie->path);
4928 if (err) {
4929 if (err->code != GOT_ERR_BAD_PATH)
4930 goto done;
4931 parent_path = strdup("/");
4932 if (parent_path == NULL) {
4933 err = got_error_from_errno("strdup");
4934 goto done;
4937 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4938 tree_path = strdup(parent_path);
4939 if (tree_path == NULL) {
4940 err = got_error_from_errno("strdup");
4941 goto done;
4943 } else {
4944 if (got_path_is_root_dir(parent_path)) {
4945 tree_path = strdup(a->worktree->path_prefix);
4946 if (tree_path == NULL) {
4947 err = got_error_from_errno("strdup");
4948 goto done;
4950 } else {
4951 if (asprintf(&tree_path, "%s/%s",
4952 a->worktree->path_prefix, parent_path) == -1) {
4953 err = got_error_from_errno("asprintf");
4954 goto done;
4959 err = got_object_open_as_commit(&base_commit, a->repo,
4960 a->worktree->base_commit_id);
4961 if (err)
4962 goto done;
4964 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4965 if (err) {
4966 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4967 (status == GOT_STATUS_ADD ||
4968 staged_status == GOT_STATUS_ADD)))
4969 goto done;
4970 } else {
4971 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4972 if (err)
4973 goto done;
4975 err = got_path_basename(&te_name, ie->path);
4976 if (err)
4977 goto done;
4979 te = got_object_tree_find_entry(tree, te_name);
4980 free(te_name);
4981 if (te == NULL && status != GOT_STATUS_ADD &&
4982 staged_status != GOT_STATUS_ADD) {
4983 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4984 goto done;
4988 switch (status) {
4989 case GOT_STATUS_ADD:
4990 if (a->patch_cb) {
4991 int choice = GOT_PATCH_CHOICE_NONE;
4992 err = (*a->patch_cb)(&choice, a->patch_arg,
4993 status, ie->path, NULL, 1, 1);
4994 if (err)
4995 goto done;
4996 if (choice != GOT_PATCH_CHOICE_YES)
4997 break;
4999 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
5000 ie->path);
5001 if (err)
5002 goto done;
5003 got_fileindex_entry_remove(a->fileindex, ie);
5004 if (a->unlink_added_files) {
5005 if (asprintf(&ondisk_path, "%s/%s",
5006 got_worktree_get_root_path(a->worktree),
5007 relpath) == -1) {
5008 err = got_error_from_errno("asprintf");
5009 goto done;
5011 if (unlink(ondisk_path) == -1) {
5012 err = got_error_from_errno2("unlink",
5013 ondisk_path);
5014 break;
5017 break;
5018 case GOT_STATUS_DELETE:
5019 if (a->patch_cb) {
5020 int choice = GOT_PATCH_CHOICE_NONE;
5021 err = (*a->patch_cb)(&choice, a->patch_arg,
5022 status, ie->path, NULL, 1, 1);
5023 if (err)
5024 goto done;
5025 if (choice != GOT_PATCH_CHOICE_YES)
5026 break;
5028 /* fall through */
5029 case GOT_STATUS_MODIFY:
5030 case GOT_STATUS_MODE_CHANGE:
5031 case GOT_STATUS_CONFLICT:
5032 case GOT_STATUS_MISSING: {
5033 struct got_object_id id;
5034 if (staged_status == GOT_STATUS_ADD ||
5035 staged_status == GOT_STATUS_MODIFY)
5036 got_fileindex_entry_get_staged_blob_id(&id, ie);
5037 else
5038 got_fileindex_entry_get_blob_id(&id, ie);
5039 fd = got_opentempfd();
5040 if (fd == -1) {
5041 err = got_error_from_errno("got_opentempfd");
5042 goto done;
5045 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
5046 if (err)
5047 goto done;
5049 if (asprintf(&ondisk_path, "%s/%s",
5050 got_worktree_get_root_path(a->worktree), relpath) == -1) {
5051 err = got_error_from_errno("asprintf");
5052 goto done;
5055 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
5056 status == GOT_STATUS_CONFLICT)) {
5057 int is_bad_symlink = 0;
5058 err = create_patched_content(&path_content, 1, &id,
5059 ondisk_path, dirfd, de_name, ie->path, a->repo,
5060 a->patch_cb, a->patch_arg);
5061 if (err || path_content == NULL)
5062 break;
5063 if (te && S_ISLNK(te->mode)) {
5064 if (unlink(path_content) == -1) {
5065 err = got_error_from_errno2("unlink",
5066 path_content);
5067 break;
5069 err = install_symlink(&is_bad_symlink,
5070 a->worktree, ondisk_path, ie->path,
5071 blob, 0, 1, 0, 0, a->repo,
5072 a->progress_cb, a->progress_arg);
5073 } else {
5074 if (rename(path_content, ondisk_path) == -1) {
5075 err = got_error_from_errno3("rename",
5076 path_content, ondisk_path);
5077 goto done;
5080 } else {
5081 int is_bad_symlink = 0;
5082 if (te && S_ISLNK(te->mode)) {
5083 err = install_symlink(&is_bad_symlink,
5084 a->worktree, ondisk_path, ie->path,
5085 blob, 0, 1, 0, 0, a->repo,
5086 a->progress_cb, a->progress_arg);
5087 } else {
5088 err = install_blob(a->worktree, ondisk_path,
5089 ie->path,
5090 te ? te->mode : GOT_DEFAULT_FILE_MODE,
5091 got_fileindex_perms_to_st(ie), blob,
5092 0, 1, 0, 0, a->repo,
5093 a->progress_cb, a->progress_arg);
5095 if (err)
5096 goto done;
5097 if (status == GOT_STATUS_DELETE ||
5098 status == GOT_STATUS_MODE_CHANGE) {
5099 err = got_fileindex_entry_update(ie,
5100 a->worktree->root_fd, relpath,
5101 blob->id.sha1,
5102 a->worktree->base_commit_id->sha1, 1);
5103 if (err)
5104 goto done;
5106 if (is_bad_symlink) {
5107 got_fileindex_entry_filetype_set(ie,
5108 GOT_FILEIDX_MODE_BAD_SYMLINK);
5111 break;
5113 default:
5114 break;
5116 done:
5117 free(ondisk_path);
5118 free(path_content);
5119 free(parent_path);
5120 free(tree_path);
5121 if (fd != -1 && close(fd) == -1 && err == NULL)
5122 err = got_error_from_errno("close");
5123 if (blob)
5124 got_object_blob_close(blob);
5125 if (tree)
5126 got_object_tree_close(tree);
5127 free(tree_id);
5128 if (base_commit)
5129 got_object_commit_close(base_commit);
5130 return err;
5133 const struct got_error *
5134 got_worktree_revert(struct got_worktree *worktree,
5135 struct got_pathlist_head *paths,
5136 got_worktree_checkout_cb progress_cb, void *progress_arg,
5137 got_worktree_patch_cb patch_cb, void *patch_arg,
5138 struct got_repository *repo)
5140 struct got_fileindex *fileindex = NULL;
5141 char *fileindex_path = NULL;
5142 const struct got_error *err = NULL, *unlockerr = NULL;
5143 const struct got_error *sync_err = NULL;
5144 struct got_pathlist_entry *pe;
5145 struct revert_file_args rfa;
5147 err = lock_worktree(worktree, LOCK_EX);
5148 if (err)
5149 return err;
5151 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5152 if (err)
5153 goto done;
5155 rfa.worktree = worktree;
5156 rfa.fileindex = fileindex;
5157 rfa.progress_cb = progress_cb;
5158 rfa.progress_arg = progress_arg;
5159 rfa.patch_cb = patch_cb;
5160 rfa.patch_arg = patch_arg;
5161 rfa.repo = repo;
5162 rfa.unlink_added_files = 0;
5163 TAILQ_FOREACH(pe, paths, entry) {
5164 err = worktree_status(worktree, pe->path, fileindex, repo,
5165 revert_file, &rfa, NULL, NULL, 1, 0);
5166 if (err)
5167 break;
5169 sync_err = sync_fileindex(fileindex, fileindex_path);
5170 if (sync_err && err == NULL)
5171 err = sync_err;
5172 done:
5173 free(fileindex_path);
5174 if (fileindex)
5175 got_fileindex_free(fileindex);
5176 unlockerr = lock_worktree(worktree, LOCK_SH);
5177 if (unlockerr && err == NULL)
5178 err = unlockerr;
5179 return err;
5182 static void
5183 free_commitable(struct got_commitable *ct)
5185 free(ct->path);
5186 free(ct->in_repo_path);
5187 free(ct->ondisk_path);
5188 free(ct->blob_id);
5189 free(ct->base_blob_id);
5190 free(ct->staged_blob_id);
5191 free(ct->base_commit_id);
5192 free(ct);
5195 struct collect_commitables_arg {
5196 struct got_pathlist_head *commitable_paths;
5197 struct got_repository *repo;
5198 struct got_worktree *worktree;
5199 struct got_fileindex *fileindex;
5200 int have_staged_files;
5201 int allow_bad_symlinks;
5202 int diff_header_shown;
5203 int commit_conflicts;
5204 FILE *diff_outfile;
5205 FILE *f1;
5206 FILE *f2;
5210 * Create a file which contains the target path of a symlink so we can feed
5211 * it as content to the diff engine.
5213 static const struct got_error *
5214 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5215 const char *abspath)
5217 const struct got_error *err = NULL;
5218 char target_path[PATH_MAX];
5219 ssize_t target_len, outlen;
5221 *fd = -1;
5223 if (dirfd != -1) {
5224 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5225 if (target_len == -1)
5226 return got_error_from_errno2("readlinkat", abspath);
5227 } else {
5228 target_len = readlink(abspath, target_path, PATH_MAX);
5229 if (target_len == -1)
5230 return got_error_from_errno2("readlink", abspath);
5233 *fd = got_opentempfd();
5234 if (*fd == -1)
5235 return got_error_from_errno("got_opentempfd");
5237 outlen = write(*fd, target_path, target_len);
5238 if (outlen == -1) {
5239 err = got_error_from_errno("got_opentempfd");
5240 goto done;
5243 if (lseek(*fd, 0, SEEK_SET) == -1) {
5244 err = got_error_from_errno2("lseek", abspath);
5245 goto done;
5247 done:
5248 if (err) {
5249 close(*fd);
5250 *fd = -1;
5252 return err;
5255 static const struct got_error *
5256 append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5257 FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5258 int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5260 const struct got_error *err = NULL;
5261 struct got_blob_object *blob1 = NULL;
5262 int fd = -1, fd1 = -1, fd2 = -1;
5263 FILE *ondisk_file = NULL;
5264 char *label1 = NULL;
5265 struct stat sb;
5266 off_t size1 = 0;
5267 int f2_exists = 0;
5268 char *id_str = NULL;
5270 memset(&sb, 0, sizeof(sb));
5272 if (diff_staged) {
5273 if (ct->staged_status != GOT_STATUS_MODIFY &&
5274 ct->staged_status != GOT_STATUS_ADD &&
5275 ct->staged_status != GOT_STATUS_DELETE)
5276 return NULL;
5277 } else {
5278 if (ct->status != GOT_STATUS_MODIFY &&
5279 ct->status != GOT_STATUS_ADD &&
5280 ct->status != GOT_STATUS_DELETE &&
5281 ct->status != GOT_STATUS_CONFLICT)
5282 return NULL;
5285 err = got_opentemp_truncate(f1);
5286 if (err)
5287 return got_error_from_errno("got_opentemp_truncate");
5288 err = got_opentemp_truncate(f2);
5289 if (err)
5290 return got_error_from_errno("got_opentemp_truncate");
5292 if (!*diff_header_shown) {
5293 err = got_object_id_str(&id_str, worktree->base_commit_id);
5294 if (err)
5295 return err;
5296 fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5297 got_worktree_get_root_path(worktree));
5298 fprintf(diff_outfile, "commit - %s\n", id_str);
5299 fprintf(diff_outfile, "path + %s%s\n",
5300 got_worktree_get_root_path(worktree),
5301 diff_staged ? " (staged changes)" : "");
5302 *diff_header_shown = 1;
5305 if (diff_staged) {
5306 const char *label1 = NULL, *label2 = NULL;
5307 switch (ct->staged_status) {
5308 case GOT_STATUS_MODIFY:
5309 label1 = ct->path;
5310 label2 = ct->path;
5311 break;
5312 case GOT_STATUS_ADD:
5313 label2 = ct->path;
5314 break;
5315 case GOT_STATUS_DELETE:
5316 label1 = ct->path;
5317 break;
5318 default:
5319 return got_error(GOT_ERR_FILE_STATUS);
5321 fd1 = got_opentempfd();
5322 if (fd1 == -1) {
5323 err = got_error_from_errno("got_opentempfd");
5324 goto done;
5326 fd2 = got_opentempfd();
5327 if (fd2 == -1) {
5328 err = got_error_from_errno("got_opentempfd");
5329 goto done;
5331 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5332 fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5333 label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5334 NULL, repo, diff_outfile);
5335 goto done;
5338 fd1 = got_opentempfd();
5339 if (fd1 == -1) {
5340 err = got_error_from_errno("got_opentempfd");
5341 goto done;
5344 if (ct->status != GOT_STATUS_ADD) {
5345 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5346 8192, fd1);
5347 if (err)
5348 goto done;
5351 if (ct->status != GOT_STATUS_DELETE) {
5352 if (dirfd != -1) {
5353 fd = openat(dirfd, de_name,
5354 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5355 if (fd == -1) {
5356 if (!got_err_open_nofollow_on_symlink()) {
5357 err = got_error_from_errno2("openat",
5358 ct->ondisk_path);
5359 goto done;
5361 err = get_symlink_target_file(&fd, dirfd,
5362 de_name, ct->ondisk_path);
5363 if (err)
5364 goto done;
5366 } else {
5367 fd = open(ct->ondisk_path,
5368 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5369 if (fd == -1) {
5370 if (!got_err_open_nofollow_on_symlink()) {
5371 err = got_error_from_errno2("open",
5372 ct->ondisk_path);
5373 goto done;
5375 err = get_symlink_target_file(&fd, dirfd,
5376 de_name, ct->ondisk_path);
5377 if (err)
5378 goto done;
5381 if (fstatat(fd, ct->ondisk_path, &sb,
5382 AT_SYMLINK_NOFOLLOW) == -1) {
5383 err = got_error_from_errno2("fstatat", ct->ondisk_path);
5384 goto done;
5386 ondisk_file = fdopen(fd, "r");
5387 if (ondisk_file == NULL) {
5388 err = got_error_from_errno2("fdopen", ct->ondisk_path);
5389 goto done;
5391 fd = -1;
5392 f2_exists = 1;
5395 if (blob1) {
5396 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5397 f1, blob1);
5398 if (err)
5399 goto done;
5402 err = got_diff_blob_file(blob1, f1, size1, label1,
5403 ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5404 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5405 done:
5406 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5407 err = got_error_from_errno("close");
5408 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5409 err = got_error_from_errno("close");
5410 if (blob1)
5411 got_object_blob_close(blob1);
5412 if (fd != -1 && close(fd) == -1 && err == NULL)
5413 err = got_error_from_errno("close");
5414 if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5415 err = got_error_from_errno("fclose");
5416 return err;
5419 static const struct got_error *
5420 collect_commitables(void *arg, unsigned char status,
5421 unsigned char staged_status, const char *relpath,
5422 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5423 struct got_object_id *commit_id, int dirfd, const char *de_name)
5425 struct collect_commitables_arg *a = arg;
5426 const struct got_error *err = NULL;
5427 struct got_commitable *ct = NULL;
5428 struct got_pathlist_entry *new = NULL;
5429 char *parent_path = NULL, *path = NULL;
5430 struct stat sb;
5432 if (a->have_staged_files) {
5433 if (staged_status != GOT_STATUS_MODIFY &&
5434 staged_status != GOT_STATUS_ADD &&
5435 staged_status != GOT_STATUS_DELETE)
5436 return NULL;
5437 } else {
5438 if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5439 printf("C %s\n", relpath);
5440 return got_error(GOT_ERR_COMMIT_CONFLICT);
5443 if (status != GOT_STATUS_MODIFY &&
5444 status != GOT_STATUS_MODE_CHANGE &&
5445 status != GOT_STATUS_ADD &&
5446 status != GOT_STATUS_DELETE &&
5447 status != GOT_STATUS_CONFLICT)
5448 return NULL;
5451 if (asprintf(&path, "/%s", relpath) == -1) {
5452 err = got_error_from_errno("asprintf");
5453 goto done;
5455 if (strcmp(path, "/") == 0) {
5456 parent_path = strdup("");
5457 if (parent_path == NULL)
5458 return got_error_from_errno("strdup");
5459 } else {
5460 err = got_path_dirname(&parent_path, path);
5461 if (err)
5462 return err;
5465 ct = calloc(1, sizeof(*ct));
5466 if (ct == NULL) {
5467 err = got_error_from_errno("calloc");
5468 goto done;
5471 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5472 relpath) == -1) {
5473 err = got_error_from_errno("asprintf");
5474 goto done;
5477 if (staged_status == GOT_STATUS_ADD ||
5478 staged_status == GOT_STATUS_MODIFY) {
5479 struct got_fileindex_entry *ie;
5480 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5481 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5482 case GOT_FILEIDX_MODE_REGULAR_FILE:
5483 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5484 ct->mode = S_IFREG;
5485 break;
5486 case GOT_FILEIDX_MODE_SYMLINK:
5487 ct->mode = S_IFLNK;
5488 break;
5489 default:
5490 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5491 goto done;
5493 ct->mode |= got_fileindex_entry_perms_get(ie);
5494 } else if (status != GOT_STATUS_DELETE &&
5495 staged_status != GOT_STATUS_DELETE) {
5496 if (dirfd != -1) {
5497 if (fstatat(dirfd, de_name, &sb,
5498 AT_SYMLINK_NOFOLLOW) == -1) {
5499 err = got_error_from_errno2("fstatat",
5500 ct->ondisk_path);
5501 goto done;
5503 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5504 err = got_error_from_errno2("lstat", ct->ondisk_path);
5505 goto done;
5507 ct->mode = sb.st_mode;
5510 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5511 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5512 relpath) == -1) {
5513 err = got_error_from_errno("asprintf");
5514 goto done;
5517 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5518 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5519 int is_bad_symlink;
5520 char target_path[PATH_MAX];
5521 ssize_t target_len;
5522 target_len = readlink(ct->ondisk_path, target_path,
5523 sizeof(target_path));
5524 if (target_len == -1) {
5525 err = got_error_from_errno2("readlink",
5526 ct->ondisk_path);
5527 goto done;
5529 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5530 target_len, ct->ondisk_path, a->worktree->root_path);
5531 if (err)
5532 goto done;
5533 if (is_bad_symlink) {
5534 err = got_error_path(ct->ondisk_path,
5535 GOT_ERR_BAD_SYMLINK);
5536 goto done;
5541 ct->status = status;
5542 ct->staged_status = staged_status;
5543 ct->blob_id = NULL; /* will be filled in when blob gets created */
5544 if (ct->status != GOT_STATUS_ADD &&
5545 ct->staged_status != GOT_STATUS_ADD) {
5546 ct->base_blob_id = got_object_id_dup(blob_id);
5547 if (ct->base_blob_id == NULL) {
5548 err = got_error_from_errno("got_object_id_dup");
5549 goto done;
5551 ct->base_commit_id = got_object_id_dup(commit_id);
5552 if (ct->base_commit_id == NULL) {
5553 err = got_error_from_errno("got_object_id_dup");
5554 goto done;
5557 if (ct->staged_status == GOT_STATUS_ADD ||
5558 ct->staged_status == GOT_STATUS_MODIFY) {
5559 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5560 if (ct->staged_blob_id == NULL) {
5561 err = got_error_from_errno("got_object_id_dup");
5562 goto done;
5565 ct->path = strdup(path);
5566 if (ct->path == NULL) {
5567 err = got_error_from_errno("strdup");
5568 goto done;
5570 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5571 if (err)
5572 goto done;
5574 if (a->diff_outfile && ct && new != NULL) {
5575 err = append_ct_diff(ct, &a->diff_header_shown,
5576 a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5577 a->have_staged_files, a->repo, a->worktree);
5578 if (err)
5579 goto done;
5581 done:
5582 if (ct && (err || new == NULL))
5583 free_commitable(ct);
5584 free(parent_path);
5585 free(path);
5586 return err;
5589 static const struct got_error *write_tree(struct got_object_id **, int *,
5590 struct got_tree_object *, const char *, struct got_pathlist_head *,
5591 got_worktree_status_cb status_cb, void *status_arg,
5592 struct got_repository *);
5594 static const struct got_error *
5595 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5596 struct got_tree_entry *te, const char *parent_path,
5597 struct got_pathlist_head *commitable_paths,
5598 got_worktree_status_cb status_cb, void *status_arg,
5599 struct got_repository *repo)
5601 const struct got_error *err = NULL;
5602 struct got_tree_object *subtree;
5603 char *subpath;
5605 if (asprintf(&subpath, "%s%s%s", parent_path,
5606 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5607 return got_error_from_errno("asprintf");
5609 err = got_object_open_as_tree(&subtree, repo, &te->id);
5610 if (err)
5611 return err;
5613 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5614 commitable_paths, status_cb, status_arg, repo);
5615 got_object_tree_close(subtree);
5616 free(subpath);
5617 return err;
5620 static const struct got_error *
5621 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5623 const struct got_error *err = NULL;
5624 char *ct_parent_path = NULL;
5626 *match = 0;
5628 if (strchr(ct->in_repo_path, '/') == NULL) {
5629 *match = got_path_is_root_dir(path);
5630 return NULL;
5633 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5634 if (err)
5635 return err;
5636 *match = (strcmp(path, ct_parent_path) == 0);
5637 free(ct_parent_path);
5638 return err;
5641 static mode_t
5642 get_ct_file_mode(struct got_commitable *ct)
5644 if (S_ISLNK(ct->mode))
5645 return S_IFLNK;
5647 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5650 static const struct got_error *
5651 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5652 struct got_tree_entry *te, struct got_commitable *ct)
5654 const struct got_error *err = NULL;
5656 *new_te = NULL;
5658 err = got_object_tree_entry_dup(new_te, te);
5659 if (err)
5660 goto done;
5662 (*new_te)->mode = get_ct_file_mode(ct);
5664 if (ct->staged_status == GOT_STATUS_MODIFY)
5665 memcpy(&(*new_te)->id, ct->staged_blob_id,
5666 sizeof((*new_te)->id));
5667 else
5668 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5669 done:
5670 if (err && *new_te) {
5671 free(*new_te);
5672 *new_te = NULL;
5674 return err;
5677 static const struct got_error *
5678 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5679 struct got_commitable *ct)
5681 const struct got_error *err = NULL;
5682 char *ct_name = NULL;
5684 *new_te = NULL;
5686 *new_te = calloc(1, sizeof(**new_te));
5687 if (*new_te == NULL)
5688 return got_error_from_errno("calloc");
5690 err = got_path_basename(&ct_name, ct->path);
5691 if (err)
5692 goto done;
5693 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5694 sizeof((*new_te)->name)) {
5695 err = got_error(GOT_ERR_NO_SPACE);
5696 goto done;
5699 (*new_te)->mode = get_ct_file_mode(ct);
5701 if (ct->staged_status == GOT_STATUS_ADD)
5702 memcpy(&(*new_te)->id, ct->staged_blob_id,
5703 sizeof((*new_te)->id));
5704 else
5705 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5706 done:
5707 free(ct_name);
5708 if (err && *new_te) {
5709 free(*new_te);
5710 *new_te = NULL;
5712 return err;
5715 static const struct got_error *
5716 insert_tree_entry(struct got_tree_entry *new_te,
5717 struct got_pathlist_head *paths)
5719 const struct got_error *err = NULL;
5720 struct got_pathlist_entry *new_pe;
5722 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5723 if (err)
5724 return err;
5725 if (new_pe == NULL)
5726 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5727 return NULL;
5730 static const struct got_error *
5731 report_ct_status(struct got_commitable *ct,
5732 got_worktree_status_cb status_cb, void *status_arg)
5734 const char *ct_path = ct->path;
5735 unsigned char status;
5737 if (status_cb == NULL) /* no commit progress output desired */
5738 return NULL;
5740 while (ct_path[0] == '/')
5741 ct_path++;
5743 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5744 status = ct->staged_status;
5745 else
5746 status = ct->status;
5748 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5749 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5752 static const struct got_error *
5753 match_modified_subtree(int *modified, struct got_tree_entry *te,
5754 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5756 const struct got_error *err = NULL;
5757 struct got_pathlist_entry *pe;
5758 char *te_path;
5760 *modified = 0;
5762 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5763 got_path_is_root_dir(base_tree_path) ? "" : "/",
5764 te->name) == -1)
5765 return got_error_from_errno("asprintf");
5767 TAILQ_FOREACH(pe, commitable_paths, entry) {
5768 struct got_commitable *ct = pe->data;
5769 *modified = got_path_is_child(ct->in_repo_path, te_path,
5770 strlen(te_path));
5771 if (*modified)
5772 break;
5775 free(te_path);
5776 return err;
5779 static const struct got_error *
5780 match_deleted_or_modified_ct(struct got_commitable **ctp,
5781 struct got_tree_entry *te, const char *base_tree_path,
5782 struct got_pathlist_head *commitable_paths)
5784 const struct got_error *err = NULL;
5785 struct got_pathlist_entry *pe;
5787 *ctp = NULL;
5789 TAILQ_FOREACH(pe, commitable_paths, entry) {
5790 struct got_commitable *ct = pe->data;
5791 char *ct_name = NULL;
5792 int path_matches;
5794 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5795 if (ct->status != GOT_STATUS_MODIFY &&
5796 ct->status != GOT_STATUS_MODE_CHANGE &&
5797 ct->status != GOT_STATUS_DELETE &&
5798 ct->status != GOT_STATUS_CONFLICT)
5799 continue;
5800 } else {
5801 if (ct->staged_status != GOT_STATUS_MODIFY &&
5802 ct->staged_status != GOT_STATUS_DELETE)
5803 continue;
5806 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5807 continue;
5809 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5810 if (err)
5811 return err;
5812 if (!path_matches)
5813 continue;
5815 err = got_path_basename(&ct_name, pe->path);
5816 if (err)
5817 return err;
5819 if (strcmp(te->name, ct_name) != 0) {
5820 free(ct_name);
5821 continue;
5823 free(ct_name);
5825 *ctp = ct;
5826 break;
5829 return err;
5832 static const struct got_error *
5833 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5834 const char *child_path, const char *path_base_tree,
5835 struct got_pathlist_head *commitable_paths,
5836 got_worktree_status_cb status_cb, void *status_arg,
5837 struct got_repository *repo)
5839 const struct got_error *err = NULL;
5840 struct got_tree_entry *new_te;
5841 char *subtree_path;
5842 struct got_object_id *id = NULL;
5843 int nentries;
5845 *new_tep = NULL;
5847 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5848 got_path_is_root_dir(path_base_tree) ? "" : "/",
5849 child_path) == -1)
5850 return got_error_from_errno("asprintf");
5852 new_te = calloc(1, sizeof(*new_te));
5853 if (new_te == NULL)
5854 return got_error_from_errno("calloc");
5855 new_te->mode = S_IFDIR;
5857 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5858 sizeof(new_te->name)) {
5859 err = got_error(GOT_ERR_NO_SPACE);
5860 goto done;
5862 err = write_tree(&id, &nentries, NULL, subtree_path,
5863 commitable_paths, status_cb, status_arg, repo);
5864 if (err) {
5865 free(new_te);
5866 goto done;
5868 memcpy(&new_te->id, id, sizeof(new_te->id));
5869 done:
5870 free(id);
5871 free(subtree_path);
5872 if (err == NULL)
5873 *new_tep = new_te;
5874 return err;
5877 static const struct got_error *
5878 write_tree(struct got_object_id **new_tree_id, int *nentries,
5879 struct got_tree_object *base_tree, const char *path_base_tree,
5880 struct got_pathlist_head *commitable_paths,
5881 got_worktree_status_cb status_cb, void *status_arg,
5882 struct got_repository *repo)
5884 const struct got_error *err = NULL;
5885 struct got_pathlist_head paths;
5886 struct got_tree_entry *te, *new_te = NULL;
5887 struct got_pathlist_entry *pe;
5889 TAILQ_INIT(&paths);
5890 *nentries = 0;
5892 /* Insert, and recurse into, newly added entries first. */
5893 TAILQ_FOREACH(pe, commitable_paths, entry) {
5894 struct got_commitable *ct = pe->data;
5895 char *child_path = NULL, *slash;
5897 if ((ct->status != GOT_STATUS_ADD &&
5898 ct->staged_status != GOT_STATUS_ADD) ||
5899 (ct->flags & GOT_COMMITABLE_ADDED))
5900 continue;
5902 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5903 strlen(path_base_tree)))
5904 continue;
5906 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5907 ct->in_repo_path);
5908 if (err)
5909 goto done;
5911 slash = strchr(child_path, '/');
5912 if (slash == NULL) {
5913 err = alloc_added_blob_tree_entry(&new_te, ct);
5914 if (err)
5915 goto done;
5916 err = report_ct_status(ct, status_cb, status_arg);
5917 if (err)
5918 goto done;
5919 ct->flags |= GOT_COMMITABLE_ADDED;
5920 err = insert_tree_entry(new_te, &paths);
5921 if (err)
5922 goto done;
5923 (*nentries)++;
5924 } else {
5925 *slash = '\0'; /* trim trailing path components */
5926 if (base_tree == NULL ||
5927 got_object_tree_find_entry(base_tree, child_path)
5928 == NULL) {
5929 err = make_subtree_for_added_blob(&new_te,
5930 child_path, path_base_tree,
5931 commitable_paths, status_cb, status_arg,
5932 repo);
5933 if (err)
5934 goto done;
5935 err = insert_tree_entry(new_te, &paths);
5936 if (err)
5937 goto done;
5938 (*nentries)++;
5943 if (base_tree) {
5944 int i, nbase_entries;
5945 /* Handle modified and deleted entries. */
5946 nbase_entries = got_object_tree_get_nentries(base_tree);
5947 for (i = 0; i < nbase_entries; i++) {
5948 struct got_commitable *ct = NULL;
5950 te = got_object_tree_get_entry(base_tree, i);
5951 if (got_object_tree_entry_is_submodule(te)) {
5952 /* Entry is a submodule; just copy it. */
5953 err = got_object_tree_entry_dup(&new_te, te);
5954 if (err)
5955 goto done;
5956 err = insert_tree_entry(new_te, &paths);
5957 if (err)
5958 goto done;
5959 (*nentries)++;
5960 continue;
5963 if (S_ISDIR(te->mode)) {
5964 int modified;
5965 err = got_object_tree_entry_dup(&new_te, te);
5966 if (err)
5967 goto done;
5968 err = match_modified_subtree(&modified, te,
5969 path_base_tree, commitable_paths);
5970 if (err)
5971 goto done;
5972 /* Avoid recursion into unmodified subtrees. */
5973 if (modified) {
5974 struct got_object_id *new_id;
5975 int nsubentries;
5976 err = write_subtree(&new_id,
5977 &nsubentries, te,
5978 path_base_tree, commitable_paths,
5979 status_cb, status_arg, repo);
5980 if (err)
5981 goto done;
5982 if (nsubentries == 0) {
5983 /* All entries were deleted. */
5984 free(new_id);
5985 continue;
5987 memcpy(&new_te->id, new_id,
5988 sizeof(new_te->id));
5989 free(new_id);
5991 err = insert_tree_entry(new_te, &paths);
5992 if (err)
5993 goto done;
5994 (*nentries)++;
5995 continue;
5998 err = match_deleted_or_modified_ct(&ct, te,
5999 path_base_tree, commitable_paths);
6000 if (err)
6001 goto done;
6002 if (ct) {
6003 /* NB: Deleted entries get dropped here. */
6004 if (ct->status == GOT_STATUS_MODIFY ||
6005 ct->status == GOT_STATUS_MODE_CHANGE ||
6006 ct->status == GOT_STATUS_CONFLICT ||
6007 ct->staged_status == GOT_STATUS_MODIFY) {
6008 err = alloc_modified_blob_tree_entry(
6009 &new_te, te, ct);
6010 if (err)
6011 goto done;
6012 err = insert_tree_entry(new_te, &paths);
6013 if (err)
6014 goto done;
6015 (*nentries)++;
6017 err = report_ct_status(ct, status_cb,
6018 status_arg);
6019 if (err)
6020 goto done;
6021 } else {
6022 /* Entry is unchanged; just copy it. */
6023 err = got_object_tree_entry_dup(&new_te, te);
6024 if (err)
6025 goto done;
6026 err = insert_tree_entry(new_te, &paths);
6027 if (err)
6028 goto done;
6029 (*nentries)++;
6034 /* Write new list of entries; deleted entries have been dropped. */
6035 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
6036 done:
6037 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
6038 return err;
6041 static const struct got_error *
6042 update_fileindex_after_commit(struct got_worktree *worktree,
6043 struct got_pathlist_head *commitable_paths,
6044 struct got_object_id *new_base_commit_id,
6045 struct got_fileindex *fileindex, int have_staged_files)
6047 const struct got_error *err = NULL;
6048 struct got_pathlist_entry *pe;
6049 char *relpath = NULL;
6051 TAILQ_FOREACH(pe, commitable_paths, entry) {
6052 struct got_fileindex_entry *ie;
6053 struct got_commitable *ct = pe->data;
6055 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6057 err = got_path_skip_common_ancestor(&relpath,
6058 worktree->root_path, ct->ondisk_path);
6059 if (err)
6060 goto done;
6062 if (ie) {
6063 if (ct->status == GOT_STATUS_DELETE ||
6064 ct->staged_status == GOT_STATUS_DELETE) {
6065 got_fileindex_entry_remove(fileindex, ie);
6066 } else if (ct->staged_status == GOT_STATUS_ADD ||
6067 ct->staged_status == GOT_STATUS_MODIFY) {
6068 got_fileindex_entry_stage_set(ie,
6069 GOT_FILEIDX_STAGE_NONE);
6070 got_fileindex_entry_staged_filetype_set(ie, 0);
6072 err = got_fileindex_entry_update(ie,
6073 worktree->root_fd, relpath,
6074 ct->staged_blob_id->sha1,
6075 new_base_commit_id->sha1,
6076 !have_staged_files);
6077 } else
6078 err = got_fileindex_entry_update(ie,
6079 worktree->root_fd, relpath,
6080 ct->blob_id->sha1,
6081 new_base_commit_id->sha1,
6082 !have_staged_files);
6083 } else {
6084 err = got_fileindex_entry_alloc(&ie, pe->path);
6085 if (err)
6086 goto done;
6087 err = got_fileindex_entry_update(ie,
6088 worktree->root_fd, relpath, ct->blob_id->sha1,
6089 new_base_commit_id->sha1, 1);
6090 if (err) {
6091 got_fileindex_entry_free(ie);
6092 goto done;
6094 err = got_fileindex_entry_add(fileindex, ie);
6095 if (err) {
6096 got_fileindex_entry_free(ie);
6097 goto done;
6100 free(relpath);
6101 relpath = NULL;
6103 done:
6104 free(relpath);
6105 return err;
6109 static const struct got_error *
6110 check_out_of_date(const char *in_repo_path, unsigned char status,
6111 unsigned char staged_status, struct got_object_id *base_blob_id,
6112 struct got_object_id *base_commit_id,
6113 struct got_object_id *head_commit_id, struct got_repository *repo,
6114 int ood_errcode)
6116 const struct got_error *err = NULL;
6117 struct got_commit_object *commit = NULL;
6118 struct got_object_id *id = NULL;
6120 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
6121 /* Trivial case: base commit == head commit */
6122 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
6123 return NULL;
6125 * Ensure file content which local changes were based
6126 * on matches file content in the branch head.
6128 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6129 if (err)
6130 goto done;
6131 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6132 if (err) {
6133 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6134 err = got_error(ood_errcode);
6135 goto done;
6136 } else if (got_object_id_cmp(id, base_blob_id) != 0)
6137 err = got_error(ood_errcode);
6138 } else {
6139 /* Require that added files don't exist in the branch head. */
6140 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6141 if (err)
6142 goto done;
6143 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6144 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
6145 goto done;
6146 err = id ? got_error(ood_errcode) : NULL;
6148 done:
6149 free(id);
6150 if (commit)
6151 got_object_commit_close(commit);
6152 return err;
6155 static const struct got_error *
6156 commit_worktree(struct got_object_id **new_commit_id,
6157 struct got_pathlist_head *commitable_paths,
6158 struct got_object_id *head_commit_id,
6159 struct got_object_id *parent_id2,
6160 struct got_worktree *worktree,
6161 const char *author, const char *committer, char *diff_path,
6162 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6163 got_worktree_status_cb status_cb, void *status_arg,
6164 struct got_repository *repo)
6166 const struct got_error *err = NULL, *unlockerr = NULL;
6167 struct got_pathlist_entry *pe;
6168 const char *head_ref_name = NULL;
6169 struct got_commit_object *head_commit = NULL;
6170 struct got_reference *head_ref2 = NULL;
6171 struct got_object_id *head_commit_id2 = NULL;
6172 struct got_tree_object *head_tree = NULL;
6173 struct got_object_id *new_tree_id = NULL;
6174 int nentries, nparents = 0;
6175 struct got_object_id_queue parent_ids;
6176 struct got_object_qid *pid = NULL;
6177 char *logmsg = NULL;
6178 time_t timestamp;
6180 *new_commit_id = NULL;
6182 STAILQ_INIT(&parent_ids);
6184 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6185 if (err)
6186 goto done;
6188 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6189 if (err)
6190 goto done;
6192 if (commit_msg_cb != NULL) {
6193 err = commit_msg_cb(commitable_paths, diff_path,
6194 &logmsg, commit_arg);
6195 if (err)
6196 goto done;
6199 if (logmsg == NULL || strlen(logmsg) == 0) {
6200 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6201 goto done;
6204 /* Create blobs from added and modified files and record their IDs. */
6205 TAILQ_FOREACH(pe, commitable_paths, entry) {
6206 struct got_commitable *ct = pe->data;
6207 char *ondisk_path;
6209 /* Blobs for staged files already exist. */
6210 if (ct->staged_status == GOT_STATUS_ADD ||
6211 ct->staged_status == GOT_STATUS_MODIFY)
6212 continue;
6214 if (ct->status != GOT_STATUS_ADD &&
6215 ct->status != GOT_STATUS_MODIFY &&
6216 ct->status != GOT_STATUS_MODE_CHANGE &&
6217 ct->status != GOT_STATUS_CONFLICT)
6218 continue;
6220 if (asprintf(&ondisk_path, "%s/%s",
6221 worktree->root_path, pe->path) == -1) {
6222 err = got_error_from_errno("asprintf");
6223 goto done;
6225 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6226 free(ondisk_path);
6227 if (err)
6228 goto done;
6231 /* Recursively write new tree objects. */
6232 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6233 commitable_paths, status_cb, status_arg, repo);
6234 if (err)
6235 goto done;
6237 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
6238 if (err)
6239 goto done;
6240 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6241 nparents++;
6242 if (parent_id2) {
6243 err = got_object_qid_alloc(&pid, parent_id2);
6244 if (err)
6245 goto done;
6246 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6247 nparents++;
6249 timestamp = time(NULL);
6250 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6251 nparents, author, timestamp, committer, timestamp, logmsg, repo);
6252 if (logmsg != NULL)
6253 free(logmsg);
6254 if (err)
6255 goto done;
6257 /* Check if a concurrent commit to our branch has occurred. */
6258 head_ref_name = got_worktree_get_head_ref_name(worktree);
6259 if (head_ref_name == NULL) {
6260 err = got_error_from_errno("got_worktree_get_head_ref_name");
6261 goto done;
6263 /* Lock the reference here to prevent concurrent modification. */
6264 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6265 if (err)
6266 goto done;
6267 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6268 if (err)
6269 goto done;
6270 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6271 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6272 goto done;
6274 /* Update branch head in repository. */
6275 err = got_ref_change_ref(head_ref2, *new_commit_id);
6276 if (err)
6277 goto done;
6278 err = got_ref_write(head_ref2, repo);
6279 if (err)
6280 goto done;
6282 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6283 if (err)
6284 goto done;
6286 err = ref_base_commit(worktree, repo);
6287 if (err)
6288 goto done;
6289 done:
6290 got_object_id_queue_free(&parent_ids);
6291 if (head_tree)
6292 got_object_tree_close(head_tree);
6293 if (head_commit)
6294 got_object_commit_close(head_commit);
6295 free(head_commit_id2);
6296 if (head_ref2) {
6297 unlockerr = got_ref_unlock(head_ref2);
6298 if (unlockerr && err == NULL)
6299 err = unlockerr;
6300 got_ref_close(head_ref2);
6302 return err;
6305 static const struct got_error *
6306 check_path_is_commitable(const char *path,
6307 struct got_pathlist_head *commitable_paths)
6309 struct got_pathlist_entry *cpe = NULL;
6310 size_t path_len = strlen(path);
6312 TAILQ_FOREACH(cpe, commitable_paths, entry) {
6313 struct got_commitable *ct = cpe->data;
6314 const char *ct_path = ct->path;
6316 while (ct_path[0] == '/')
6317 ct_path++;
6319 if (strcmp(path, ct_path) == 0 ||
6320 got_path_is_child(ct_path, path, path_len))
6321 break;
6324 if (cpe == NULL)
6325 return got_error_path(path, GOT_ERR_BAD_PATH);
6327 return NULL;
6330 static const struct got_error *
6331 check_staged_file(void *arg, struct got_fileindex_entry *ie)
6333 int *have_staged_files = arg;
6335 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6336 *have_staged_files = 1;
6337 return got_error(GOT_ERR_CANCELLED);
6340 return NULL;
6343 static const struct got_error *
6344 check_non_staged_files(struct got_fileindex *fileindex,
6345 struct got_pathlist_head *paths)
6347 struct got_pathlist_entry *pe;
6348 struct got_fileindex_entry *ie;
6350 TAILQ_FOREACH(pe, paths, entry) {
6351 if (pe->path[0] == '\0')
6352 continue;
6353 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6354 if (ie == NULL)
6355 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6356 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6357 return got_error_path(pe->path,
6358 GOT_ERR_FILE_NOT_STAGED);
6361 return NULL;
6364 const struct got_error *
6365 got_worktree_commit(struct got_object_id **new_commit_id,
6366 struct got_worktree *worktree, struct got_pathlist_head *paths,
6367 const char *author, const char *committer, int allow_bad_symlinks,
6368 int show_diff, int commit_conflicts,
6369 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6370 got_worktree_status_cb status_cb, void *status_arg,
6371 struct got_repository *repo)
6373 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6374 struct got_fileindex *fileindex = NULL;
6375 char *fileindex_path = NULL;
6376 struct got_pathlist_head commitable_paths;
6377 struct collect_commitables_arg cc_arg;
6378 struct got_pathlist_entry *pe;
6379 struct got_reference *head_ref = NULL;
6380 struct got_object_id *head_commit_id = NULL;
6381 char *diff_path = NULL;
6382 int have_staged_files = 0;
6384 *new_commit_id = NULL;
6386 memset(&cc_arg, 0, sizeof(cc_arg));
6387 TAILQ_INIT(&commitable_paths);
6389 err = lock_worktree(worktree, LOCK_EX);
6390 if (err)
6391 goto done;
6393 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6394 if (err)
6395 goto done;
6397 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6398 if (err)
6399 goto done;
6401 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6402 if (err)
6403 goto done;
6405 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6406 &have_staged_files);
6407 if (err && err->code != GOT_ERR_CANCELLED)
6408 goto done;
6409 if (have_staged_files) {
6410 err = check_non_staged_files(fileindex, paths);
6411 if (err)
6412 goto done;
6415 cc_arg.commitable_paths = &commitable_paths;
6416 cc_arg.worktree = worktree;
6417 cc_arg.fileindex = fileindex;
6418 cc_arg.repo = repo;
6419 cc_arg.have_staged_files = have_staged_files;
6420 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6421 cc_arg.diff_header_shown = 0;
6422 cc_arg.commit_conflicts = commit_conflicts;
6423 if (show_diff) {
6424 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6425 GOT_TMPDIR_STR "/got", ".diff");
6426 if (err)
6427 goto done;
6428 cc_arg.f1 = got_opentemp();
6429 if (cc_arg.f1 == NULL) {
6430 err = got_error_from_errno("got_opentemp");
6431 goto done;
6433 cc_arg.f2 = got_opentemp();
6434 if (cc_arg.f2 == NULL) {
6435 err = got_error_from_errno("got_opentemp");
6436 goto done;
6440 TAILQ_FOREACH(pe, paths, entry) {
6441 err = worktree_status(worktree, pe->path, fileindex, repo,
6442 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6443 if (err)
6444 goto done;
6447 if (show_diff) {
6448 if (fflush(cc_arg.diff_outfile) == EOF) {
6449 err = got_error_from_errno("fflush");
6450 goto done;
6454 if (TAILQ_EMPTY(&commitable_paths)) {
6455 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6456 goto done;
6459 TAILQ_FOREACH(pe, paths, entry) {
6460 err = check_path_is_commitable(pe->path, &commitable_paths);
6461 if (err)
6462 goto done;
6465 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6466 struct got_commitable *ct = pe->data;
6467 const char *ct_path = ct->in_repo_path;
6469 while (ct_path[0] == '/')
6470 ct_path++;
6471 err = check_out_of_date(ct_path, ct->status,
6472 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6473 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6474 if (err)
6475 goto done;
6479 err = commit_worktree(new_commit_id, &commitable_paths,
6480 head_commit_id, NULL, worktree, author, committer,
6481 (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6482 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6483 if (err)
6484 goto done;
6486 err = update_fileindex_after_commit(worktree, &commitable_paths,
6487 *new_commit_id, fileindex, have_staged_files);
6488 sync_err = sync_fileindex(fileindex, fileindex_path);
6489 if (sync_err && err == NULL)
6490 err = sync_err;
6491 done:
6492 if (fileindex)
6493 got_fileindex_free(fileindex);
6494 free(fileindex_path);
6495 unlockerr = lock_worktree(worktree, LOCK_SH);
6496 if (unlockerr && err == NULL)
6497 err = unlockerr;
6498 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6499 struct got_commitable *ct = pe->data;
6501 free_commitable(ct);
6503 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6504 if (diff_path && unlink(diff_path) == -1 && err == NULL)
6505 err = got_error_from_errno2("unlink", diff_path);
6506 free(diff_path);
6507 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6508 err == NULL)
6509 err = got_error_from_errno("fclose");
6510 return err;
6513 const char *
6514 got_commitable_get_path(struct got_commitable *ct)
6516 return ct->path;
6519 unsigned int
6520 got_commitable_get_status(struct got_commitable *ct)
6522 return ct->status;
6525 struct check_rebase_ok_arg {
6526 struct got_worktree *worktree;
6527 struct got_repository *repo;
6530 static const struct got_error *
6531 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6533 const struct got_error *err = NULL;
6534 struct check_rebase_ok_arg *a = arg;
6535 unsigned char status;
6536 struct stat sb;
6537 char *ondisk_path;
6539 /* Reject rebase of a work tree with mixed base commits. */
6540 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6541 SHA1_DIGEST_LENGTH))
6542 return got_error(GOT_ERR_MIXED_COMMITS);
6544 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6545 == -1)
6546 return got_error_from_errno("asprintf");
6548 /* Reject rebase of a work tree with modified or staged files. */
6549 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6550 free(ondisk_path);
6551 if (err)
6552 return err;
6554 if (status != GOT_STATUS_NO_CHANGE)
6555 return got_error(GOT_ERR_MODIFIED);
6556 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6557 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6559 return NULL;
6562 const struct got_error *
6563 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6564 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6565 struct got_worktree *worktree, struct got_reference *branch,
6566 struct got_repository *repo)
6568 const struct got_error *err = NULL;
6569 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6570 char *branch_ref_name = NULL;
6571 char *fileindex_path = NULL;
6572 struct check_rebase_ok_arg ok_arg;
6573 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6574 struct got_object_id *wt_branch_tip = NULL;
6576 *new_base_branch_ref = NULL;
6577 *tmp_branch = NULL;
6578 *fileindex = NULL;
6580 err = lock_worktree(worktree, LOCK_EX);
6581 if (err)
6582 return err;
6584 err = open_fileindex(fileindex, &fileindex_path, worktree);
6585 if (err)
6586 goto done;
6588 ok_arg.worktree = worktree;
6589 ok_arg.repo = repo;
6590 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6591 &ok_arg);
6592 if (err)
6593 goto done;
6595 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6596 if (err)
6597 goto done;
6599 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6600 if (err)
6601 goto done;
6603 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6604 if (err)
6605 goto done;
6607 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6608 0);
6609 if (err)
6610 goto done;
6612 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6613 if (err)
6614 goto done;
6615 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6616 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6617 goto done;
6620 err = got_ref_alloc_symref(new_base_branch_ref,
6621 new_base_branch_ref_name, wt_branch);
6622 if (err)
6623 goto done;
6624 err = got_ref_write(*new_base_branch_ref, repo);
6625 if (err)
6626 goto done;
6628 /* TODO Lock original branch's ref while rebasing? */
6630 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6631 if (err)
6632 goto done;
6634 err = got_ref_write(branch_ref, repo);
6635 if (err)
6636 goto done;
6638 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6639 worktree->base_commit_id);
6640 if (err)
6641 goto done;
6642 err = got_ref_write(*tmp_branch, repo);
6643 if (err)
6644 goto done;
6646 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6647 if (err)
6648 goto done;
6649 done:
6650 free(fileindex_path);
6651 free(tmp_branch_name);
6652 free(new_base_branch_ref_name);
6653 free(branch_ref_name);
6654 if (branch_ref)
6655 got_ref_close(branch_ref);
6656 if (wt_branch)
6657 got_ref_close(wt_branch);
6658 free(wt_branch_tip);
6659 if (err) {
6660 if (*new_base_branch_ref) {
6661 got_ref_close(*new_base_branch_ref);
6662 *new_base_branch_ref = NULL;
6664 if (*tmp_branch) {
6665 got_ref_close(*tmp_branch);
6666 *tmp_branch = NULL;
6668 if (*fileindex) {
6669 got_fileindex_free(*fileindex);
6670 *fileindex = NULL;
6672 lock_worktree(worktree, LOCK_SH);
6674 return err;
6677 const struct got_error *
6678 got_worktree_rebase_continue(struct got_object_id **commit_id,
6679 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6680 struct got_reference **branch, struct got_fileindex **fileindex,
6681 struct got_worktree *worktree, struct got_repository *repo)
6683 const struct got_error *err;
6684 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6685 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6686 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6687 char *fileindex_path = NULL;
6688 int have_staged_files = 0;
6690 *commit_id = NULL;
6691 *new_base_branch = NULL;
6692 *tmp_branch = NULL;
6693 *branch = NULL;
6694 *fileindex = NULL;
6696 err = lock_worktree(worktree, LOCK_EX);
6697 if (err)
6698 return err;
6700 err = open_fileindex(fileindex, &fileindex_path, worktree);
6701 if (err)
6702 goto done;
6704 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6705 &have_staged_files);
6706 if (err && err->code != GOT_ERR_CANCELLED)
6707 goto done;
6708 if (have_staged_files) {
6709 err = got_error(GOT_ERR_STAGED_PATHS);
6710 goto done;
6713 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6714 if (err)
6715 goto done;
6717 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6718 if (err)
6719 goto done;
6721 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6722 if (err)
6723 goto done;
6725 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6726 if (err)
6727 goto done;
6729 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6730 if (err)
6731 goto done;
6733 err = got_ref_open(branch, repo,
6734 got_ref_get_symref_target(branch_ref), 0);
6735 if (err)
6736 goto done;
6738 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6739 if (err)
6740 goto done;
6742 err = got_ref_resolve(commit_id, repo, commit_ref);
6743 if (err)
6744 goto done;
6746 err = got_ref_open(new_base_branch, repo,
6747 new_base_branch_ref_name, 0);
6748 if (err)
6749 goto done;
6751 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6752 if (err)
6753 goto done;
6754 done:
6755 free(commit_ref_name);
6756 free(branch_ref_name);
6757 free(fileindex_path);
6758 if (commit_ref)
6759 got_ref_close(commit_ref);
6760 if (branch_ref)
6761 got_ref_close(branch_ref);
6762 if (err) {
6763 free(*commit_id);
6764 *commit_id = NULL;
6765 if (*tmp_branch) {
6766 got_ref_close(*tmp_branch);
6767 *tmp_branch = NULL;
6769 if (*new_base_branch) {
6770 got_ref_close(*new_base_branch);
6771 *new_base_branch = NULL;
6773 if (*branch) {
6774 got_ref_close(*branch);
6775 *branch = NULL;
6777 if (*fileindex) {
6778 got_fileindex_free(*fileindex);
6779 *fileindex = NULL;
6781 lock_worktree(worktree, LOCK_SH);
6783 return err;
6786 const struct got_error *
6787 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6789 const struct got_error *err;
6790 char *tmp_branch_name = NULL;
6792 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6793 if (err)
6794 return err;
6796 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6797 free(tmp_branch_name);
6798 return NULL;
6801 static const struct got_error *
6802 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6803 const char *diff_path, char **logmsg, void *arg)
6805 *logmsg = arg;
6806 return NULL;
6809 static const struct got_error *
6810 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6811 const char *path, struct got_object_id *blob_id,
6812 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6813 int dirfd, const char *de_name)
6815 return NULL;
6818 struct collect_merged_paths_arg {
6819 got_worktree_checkout_cb progress_cb;
6820 void *progress_arg;
6821 struct got_pathlist_head *merged_paths;
6824 static const struct got_error *
6825 collect_merged_paths(void *arg, unsigned char status, const char *path)
6827 const struct got_error *err;
6828 struct collect_merged_paths_arg *a = arg;
6829 char *p;
6830 struct got_pathlist_entry *new;
6832 err = (*a->progress_cb)(a->progress_arg, status, path);
6833 if (err)
6834 return err;
6836 if (status != GOT_STATUS_MERGE &&
6837 status != GOT_STATUS_ADD &&
6838 status != GOT_STATUS_DELETE &&
6839 status != GOT_STATUS_CONFLICT)
6840 return NULL;
6842 p = strdup(path);
6843 if (p == NULL)
6844 return got_error_from_errno("strdup");
6846 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6847 if (err || new == NULL)
6848 free(p);
6849 return err;
6852 static const struct got_error *
6853 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6854 int is_rebase, struct got_repository *repo)
6856 const struct got_error *err;
6857 struct got_reference *commit_ref = NULL;
6859 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6860 if (err) {
6861 if (err->code != GOT_ERR_NOT_REF)
6862 goto done;
6863 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6864 if (err)
6865 goto done;
6866 err = got_ref_write(commit_ref, repo);
6867 if (err)
6868 goto done;
6869 } else if (is_rebase) {
6870 struct got_object_id *stored_id;
6871 int cmp;
6873 err = got_ref_resolve(&stored_id, repo, commit_ref);
6874 if (err)
6875 goto done;
6876 cmp = got_object_id_cmp(commit_id, stored_id);
6877 free(stored_id);
6878 if (cmp != 0) {
6879 err = got_error(GOT_ERR_REBASE_COMMITID);
6880 goto done;
6883 done:
6884 if (commit_ref)
6885 got_ref_close(commit_ref);
6886 return err;
6889 static const struct got_error *
6890 rebase_merge_files(struct got_pathlist_head *merged_paths,
6891 const char *commit_ref_name, struct got_worktree *worktree,
6892 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6893 struct got_object_id *commit_id, struct got_repository *repo,
6894 got_worktree_checkout_cb progress_cb, void *progress_arg,
6895 got_cancel_cb cancel_cb, void *cancel_arg)
6897 const struct got_error *err;
6898 struct got_reference *commit_ref = NULL;
6899 struct collect_merged_paths_arg cmp_arg;
6900 char *fileindex_path;
6902 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6904 err = get_fileindex_path(&fileindex_path, worktree);
6905 if (err)
6906 return err;
6908 cmp_arg.progress_cb = progress_cb;
6909 cmp_arg.progress_arg = progress_arg;
6910 cmp_arg.merged_paths = merged_paths;
6911 err = merge_files(worktree, fileindex, fileindex_path,
6912 parent_commit_id, commit_id, repo, collect_merged_paths,
6913 &cmp_arg, cancel_cb, cancel_arg);
6914 if (commit_ref)
6915 got_ref_close(commit_ref);
6916 return err;
6919 const struct got_error *
6920 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6921 struct got_worktree *worktree, struct got_fileindex *fileindex,
6922 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6923 struct got_repository *repo,
6924 got_worktree_checkout_cb progress_cb, void *progress_arg,
6925 got_cancel_cb cancel_cb, void *cancel_arg)
6927 const struct got_error *err;
6928 char *commit_ref_name;
6930 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6931 if (err)
6932 return err;
6934 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6935 if (err)
6936 goto done;
6938 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6939 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6940 progress_arg, cancel_cb, cancel_arg);
6941 done:
6942 free(commit_ref_name);
6943 return err;
6946 const struct got_error *
6947 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6948 struct got_worktree *worktree, struct got_fileindex *fileindex,
6949 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6950 struct got_repository *repo,
6951 got_worktree_checkout_cb progress_cb, void *progress_arg,
6952 got_cancel_cb cancel_cb, void *cancel_arg)
6954 const struct got_error *err;
6955 char *commit_ref_name;
6957 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6958 if (err)
6959 return err;
6961 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6962 if (err)
6963 goto done;
6965 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6966 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6967 progress_arg, cancel_cb, cancel_arg);
6968 done:
6969 free(commit_ref_name);
6970 return err;
6973 static const struct got_error *
6974 rebase_commit(struct got_object_id **new_commit_id,
6975 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6976 struct got_worktree *worktree, struct got_fileindex *fileindex,
6977 struct got_reference *tmp_branch, const char *committer,
6978 struct got_commit_object *orig_commit, const char *new_logmsg,
6979 int allow_conflict, struct got_repository *repo)
6981 const struct got_error *err, *sync_err;
6982 struct got_pathlist_head commitable_paths;
6983 struct collect_commitables_arg cc_arg;
6984 char *fileindex_path = NULL;
6985 struct got_reference *head_ref = NULL;
6986 struct got_object_id *head_commit_id = NULL;
6987 char *logmsg = NULL;
6989 memset(&cc_arg, 0, sizeof(cc_arg));
6990 TAILQ_INIT(&commitable_paths);
6991 *new_commit_id = NULL;
6993 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6995 err = get_fileindex_path(&fileindex_path, worktree);
6996 if (err)
6997 return err;
6999 cc_arg.commitable_paths = &commitable_paths;
7000 cc_arg.worktree = worktree;
7001 cc_arg.repo = repo;
7002 cc_arg.have_staged_files = 0;
7003 cc_arg.commit_conflicts = allow_conflict;
7005 * If possible get the status of individual files directly to
7006 * avoid crawling the entire work tree once per rebased commit.
7008 * Ideally, merged_paths would contain a list of commitables
7009 * we could use so we could skip worktree_status() entirely.
7010 * However, we would then need carefully keep track of cumulative
7011 * effects of operations such as file additions and deletions
7012 * in 'got histedit -f' (folding multiple commits into one),
7013 * and this extra complexity is not really worth it.
7015 if (merged_paths) {
7016 struct got_pathlist_entry *pe;
7017 TAILQ_FOREACH(pe, merged_paths, entry) {
7018 err = worktree_status(worktree, pe->path, fileindex,
7019 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
7020 0);
7021 if (err)
7022 goto done;
7024 } else {
7025 err = worktree_status(worktree, "", fileindex, repo,
7026 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7027 if (err)
7028 goto done;
7031 if (TAILQ_EMPTY(&commitable_paths)) {
7032 /* No-op change; commit will be elided. */
7033 err = got_ref_delete(commit_ref, repo);
7034 if (err)
7035 goto done;
7036 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
7037 goto done;
7040 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7041 if (err)
7042 goto done;
7044 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7045 if (err)
7046 goto done;
7048 if (new_logmsg) {
7049 logmsg = strdup(new_logmsg);
7050 if (logmsg == NULL) {
7051 err = got_error_from_errno("strdup");
7052 goto done;
7054 } else {
7055 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
7056 if (err)
7057 goto done;
7060 /* NB: commit_worktree will call free(logmsg) */
7061 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
7062 NULL, worktree, got_object_commit_get_author(orig_commit),
7063 committer ? committer :
7064 got_object_commit_get_committer(orig_commit), NULL,
7065 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
7066 if (err)
7067 goto done;
7069 err = got_ref_change_ref(tmp_branch, *new_commit_id);
7070 if (err)
7071 goto done;
7073 err = got_ref_delete(commit_ref, repo);
7074 if (err)
7075 goto done;
7077 err = update_fileindex_after_commit(worktree, &commitable_paths,
7078 *new_commit_id, fileindex, 0);
7079 sync_err = sync_fileindex(fileindex, fileindex_path);
7080 if (sync_err && err == NULL)
7081 err = sync_err;
7082 done:
7083 free(fileindex_path);
7084 free(head_commit_id);
7085 if (head_ref)
7086 got_ref_close(head_ref);
7087 if (err) {
7088 free(*new_commit_id);
7089 *new_commit_id = NULL;
7091 return err;
7094 const struct got_error *
7095 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
7096 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7097 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7098 const char *committer, struct got_commit_object *orig_commit,
7099 struct got_object_id *orig_commit_id, int allow_conflict,
7100 struct got_repository *repo)
7102 const struct got_error *err;
7103 char *commit_ref_name;
7104 struct got_reference *commit_ref = NULL;
7105 struct got_object_id *commit_id = NULL;
7107 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7108 if (err)
7109 return err;
7111 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7112 if (err)
7113 goto done;
7114 err = got_ref_resolve(&commit_id, repo, commit_ref);
7115 if (err)
7116 goto done;
7117 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
7118 err = got_error(GOT_ERR_REBASE_COMMITID);
7119 goto done;
7122 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7123 worktree, fileindex, tmp_branch, committer, orig_commit,
7124 NULL, allow_conflict, repo);
7125 done:
7126 if (commit_ref)
7127 got_ref_close(commit_ref);
7128 free(commit_ref_name);
7129 free(commit_id);
7130 return err;
7133 const struct got_error *
7134 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
7135 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7136 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7137 const char *committer, struct got_commit_object *orig_commit,
7138 struct got_object_id *orig_commit_id, const char *new_logmsg,
7139 int allow_conflict, struct got_repository *repo)
7141 const struct got_error *err;
7142 char *commit_ref_name;
7143 struct got_reference *commit_ref = NULL;
7145 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7146 if (err)
7147 return err;
7149 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7150 if (err)
7151 goto done;
7153 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7154 worktree, fileindex, tmp_branch, committer, orig_commit,
7155 new_logmsg, allow_conflict, repo);
7156 done:
7157 if (commit_ref)
7158 got_ref_close(commit_ref);
7159 free(commit_ref_name);
7160 return err;
7163 const struct got_error *
7164 got_worktree_rebase_postpone(struct got_worktree *worktree,
7165 struct got_fileindex *fileindex)
7167 if (fileindex)
7168 got_fileindex_free(fileindex);
7169 return lock_worktree(worktree, LOCK_SH);
7172 static const struct got_error *
7173 delete_ref(const char *name, struct got_repository *repo)
7175 const struct got_error *err;
7176 struct got_reference *ref;
7178 err = got_ref_open(&ref, repo, name, 0);
7179 if (err) {
7180 if (err->code == GOT_ERR_NOT_REF)
7181 return NULL;
7182 return err;
7185 err = got_ref_delete(ref, repo);
7186 got_ref_close(ref);
7187 return err;
7190 static const struct got_error *
7191 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7193 const struct got_error *err;
7194 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7195 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7197 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7198 if (err)
7199 goto done;
7200 err = delete_ref(tmp_branch_name, repo);
7201 if (err)
7202 goto done;
7204 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7205 if (err)
7206 goto done;
7207 err = delete_ref(new_base_branch_ref_name, repo);
7208 if (err)
7209 goto done;
7211 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7212 if (err)
7213 goto done;
7214 err = delete_ref(branch_ref_name, repo);
7215 if (err)
7216 goto done;
7218 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7219 if (err)
7220 goto done;
7221 err = delete_ref(commit_ref_name, repo);
7222 if (err)
7223 goto done;
7225 done:
7226 free(tmp_branch_name);
7227 free(new_base_branch_ref_name);
7228 free(branch_ref_name);
7229 free(commit_ref_name);
7230 return err;
7233 static const struct got_error *
7234 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7235 struct got_object_id *new_commit_id, struct got_repository *repo)
7237 const struct got_error *err;
7238 struct got_reference *ref = NULL;
7239 struct got_object_id *old_commit_id = NULL;
7240 const char *branch_name = NULL;
7241 char *new_id_str = NULL;
7242 char *refname = NULL;
7244 branch_name = got_ref_get_name(branch);
7245 if (strncmp(branch_name, "refs/heads/", 11) != 0)
7246 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7247 branch_name += 11;
7249 err = got_object_id_str(&new_id_str, new_commit_id);
7250 if (err)
7251 return err;
7253 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7254 new_id_str) == -1) {
7255 err = got_error_from_errno("asprintf");
7256 goto done;
7259 err = got_ref_resolve(&old_commit_id, repo, branch);
7260 if (err)
7261 goto done;
7263 err = got_ref_alloc(&ref, refname, old_commit_id);
7264 if (err)
7265 goto done;
7267 err = got_ref_write(ref, repo);
7268 done:
7269 free(new_id_str);
7270 free(refname);
7271 free(old_commit_id);
7272 if (ref)
7273 got_ref_close(ref);
7274 return err;
7277 const struct got_error *
7278 got_worktree_rebase_complete(struct got_worktree *worktree,
7279 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7280 struct got_reference *rebased_branch, struct got_repository *repo,
7281 int create_backup)
7283 const struct got_error *err, *unlockerr, *sync_err;
7284 struct got_object_id *new_head_commit_id = NULL;
7285 char *fileindex_path = NULL;
7287 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7288 if (err)
7289 return err;
7291 if (create_backup) {
7292 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7293 rebased_branch, new_head_commit_id, repo);
7294 if (err)
7295 goto done;
7298 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7299 if (err)
7300 goto done;
7302 err = got_ref_write(rebased_branch, repo);
7303 if (err)
7304 goto done;
7306 err = got_worktree_set_head_ref(worktree, rebased_branch);
7307 if (err)
7308 goto done;
7310 err = delete_rebase_refs(worktree, repo);
7311 if (err)
7312 goto done;
7314 err = get_fileindex_path(&fileindex_path, worktree);
7315 if (err)
7316 goto done;
7317 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7318 sync_err = sync_fileindex(fileindex, fileindex_path);
7319 if (sync_err && err == NULL)
7320 err = sync_err;
7321 done:
7322 got_fileindex_free(fileindex);
7323 free(fileindex_path);
7324 free(new_head_commit_id);
7325 unlockerr = lock_worktree(worktree, LOCK_SH);
7326 if (unlockerr && err == NULL)
7327 err = unlockerr;
7328 return err;
7331 const struct got_error *
7332 got_worktree_rebase_abort(struct got_worktree *worktree,
7333 struct got_fileindex *fileindex, struct got_repository *repo,
7334 struct got_reference *new_base_branch,
7335 got_worktree_checkout_cb progress_cb, void *progress_arg)
7337 const struct got_error *err, *unlockerr, *sync_err;
7338 struct got_reference *resolved = NULL;
7339 struct got_object_id *commit_id = NULL;
7340 struct got_commit_object *commit = NULL;
7341 char *fileindex_path = NULL;
7342 struct revert_file_args rfa;
7343 struct got_object_id *tree_id = NULL;
7345 err = lock_worktree(worktree, LOCK_EX);
7346 if (err)
7347 return err;
7349 err = got_ref_open(&resolved, repo,
7350 got_ref_get_symref_target(new_base_branch), 0);
7351 if (err)
7352 goto done;
7354 err = got_worktree_set_head_ref(worktree, resolved);
7355 if (err)
7356 goto done;
7359 * XXX commits to the base branch could have happened while
7360 * we were busy rebasing; should we store the original commit ID
7361 * when rebase begins and read it back here?
7363 err = got_ref_resolve(&commit_id, repo, resolved);
7364 if (err)
7365 goto done;
7367 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7368 if (err)
7369 goto done;
7371 err = got_object_open_as_commit(&commit, repo,
7372 worktree->base_commit_id);
7373 if (err)
7374 goto done;
7376 err = got_object_id_by_path(&tree_id, repo, commit,
7377 worktree->path_prefix);
7378 if (err)
7379 goto done;
7381 err = delete_rebase_refs(worktree, repo);
7382 if (err)
7383 goto done;
7385 err = get_fileindex_path(&fileindex_path, worktree);
7386 if (err)
7387 goto done;
7389 rfa.worktree = worktree;
7390 rfa.fileindex = fileindex;
7391 rfa.progress_cb = progress_cb;
7392 rfa.progress_arg = progress_arg;
7393 rfa.patch_cb = NULL;
7394 rfa.patch_arg = NULL;
7395 rfa.repo = repo;
7396 rfa.unlink_added_files = 0;
7397 err = worktree_status(worktree, "", fileindex, repo,
7398 revert_file, &rfa, NULL, NULL, 1, 0);
7399 if (err)
7400 goto sync;
7402 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7403 repo, progress_cb, progress_arg, NULL, NULL);
7404 sync:
7405 sync_err = sync_fileindex(fileindex, fileindex_path);
7406 if (sync_err && err == NULL)
7407 err = sync_err;
7408 done:
7409 got_ref_close(resolved);
7410 free(tree_id);
7411 free(commit_id);
7412 if (commit)
7413 got_object_commit_close(commit);
7414 if (fileindex)
7415 got_fileindex_free(fileindex);
7416 free(fileindex_path);
7418 unlockerr = lock_worktree(worktree, LOCK_SH);
7419 if (unlockerr && err == NULL)
7420 err = unlockerr;
7421 return err;
7424 const struct got_error *
7425 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7426 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7427 struct got_fileindex **fileindex, struct got_worktree *worktree,
7428 struct got_repository *repo)
7430 const struct got_error *err = NULL;
7431 char *tmp_branch_name = NULL;
7432 char *branch_ref_name = NULL;
7433 char *base_commit_ref_name = NULL;
7434 char *fileindex_path = NULL;
7435 struct check_rebase_ok_arg ok_arg;
7436 struct got_reference *wt_branch = NULL;
7437 struct got_reference *base_commit_ref = NULL;
7439 *tmp_branch = NULL;
7440 *branch_ref = NULL;
7441 *base_commit_id = NULL;
7442 *fileindex = NULL;
7444 err = lock_worktree(worktree, LOCK_EX);
7445 if (err)
7446 return err;
7448 err = open_fileindex(fileindex, &fileindex_path, worktree);
7449 if (err)
7450 goto done;
7452 ok_arg.worktree = worktree;
7453 ok_arg.repo = repo;
7454 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7455 &ok_arg);
7456 if (err)
7457 goto done;
7459 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7460 if (err)
7461 goto done;
7463 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7464 if (err)
7465 goto done;
7467 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7468 worktree);
7469 if (err)
7470 goto done;
7472 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7473 0);
7474 if (err)
7475 goto done;
7477 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7478 if (err)
7479 goto done;
7481 err = got_ref_write(*branch_ref, repo);
7482 if (err)
7483 goto done;
7485 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7486 worktree->base_commit_id);
7487 if (err)
7488 goto done;
7489 err = got_ref_write(base_commit_ref, repo);
7490 if (err)
7491 goto done;
7492 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7493 if (*base_commit_id == NULL) {
7494 err = got_error_from_errno("got_object_id_dup");
7495 goto done;
7498 err = got_ref_alloc(tmp_branch, tmp_branch_name,
7499 worktree->base_commit_id);
7500 if (err)
7501 goto done;
7502 err = got_ref_write(*tmp_branch, repo);
7503 if (err)
7504 goto done;
7506 err = got_worktree_set_head_ref(worktree, *tmp_branch);
7507 if (err)
7508 goto done;
7509 done:
7510 free(fileindex_path);
7511 free(tmp_branch_name);
7512 free(branch_ref_name);
7513 free(base_commit_ref_name);
7514 if (wt_branch)
7515 got_ref_close(wt_branch);
7516 if (err) {
7517 if (*branch_ref) {
7518 got_ref_close(*branch_ref);
7519 *branch_ref = NULL;
7521 if (*tmp_branch) {
7522 got_ref_close(*tmp_branch);
7523 *tmp_branch = NULL;
7525 free(*base_commit_id);
7526 if (*fileindex) {
7527 got_fileindex_free(*fileindex);
7528 *fileindex = NULL;
7530 lock_worktree(worktree, LOCK_SH);
7532 return err;
7535 const struct got_error *
7536 got_worktree_histedit_postpone(struct got_worktree *worktree,
7537 struct got_fileindex *fileindex)
7539 if (fileindex)
7540 got_fileindex_free(fileindex);
7541 return lock_worktree(worktree, LOCK_SH);
7544 const struct got_error *
7545 got_worktree_histedit_in_progress(int *in_progress,
7546 struct got_worktree *worktree)
7548 const struct got_error *err;
7549 char *tmp_branch_name = NULL;
7551 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7552 if (err)
7553 return err;
7555 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7556 free(tmp_branch_name);
7557 return NULL;
7560 const struct got_error *
7561 got_worktree_histedit_continue(struct got_object_id **commit_id,
7562 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7563 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7564 struct got_worktree *worktree, struct got_repository *repo)
7566 const struct got_error *err;
7567 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7568 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7569 struct got_reference *commit_ref = NULL;
7570 struct got_reference *base_commit_ref = NULL;
7571 char *fileindex_path = NULL;
7572 int have_staged_files = 0;
7574 *commit_id = NULL;
7575 *tmp_branch = NULL;
7576 *base_commit_id = NULL;
7577 *fileindex = NULL;
7579 err = lock_worktree(worktree, LOCK_EX);
7580 if (err)
7581 return err;
7583 err = open_fileindex(fileindex, &fileindex_path, worktree);
7584 if (err)
7585 goto done;
7587 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7588 &have_staged_files);
7589 if (err && err->code != GOT_ERR_CANCELLED)
7590 goto done;
7591 if (have_staged_files) {
7592 err = got_error(GOT_ERR_STAGED_PATHS);
7593 goto done;
7596 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7597 if (err)
7598 goto done;
7600 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7601 if (err)
7602 goto done;
7604 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7605 if (err)
7606 goto done;
7608 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7609 worktree);
7610 if (err)
7611 goto done;
7613 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7614 if (err)
7615 goto done;
7617 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7618 if (err)
7619 goto done;
7620 err = got_ref_resolve(commit_id, repo, commit_ref);
7621 if (err)
7622 goto done;
7624 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7625 if (err)
7626 goto done;
7627 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7628 if (err)
7629 goto done;
7631 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7632 if (err)
7633 goto done;
7634 done:
7635 free(commit_ref_name);
7636 free(branch_ref_name);
7637 free(fileindex_path);
7638 if (commit_ref)
7639 got_ref_close(commit_ref);
7640 if (base_commit_ref)
7641 got_ref_close(base_commit_ref);
7642 if (err) {
7643 free(*commit_id);
7644 *commit_id = NULL;
7645 free(*base_commit_id);
7646 *base_commit_id = NULL;
7647 if (*tmp_branch) {
7648 got_ref_close(*tmp_branch);
7649 *tmp_branch = NULL;
7651 if (*fileindex) {
7652 got_fileindex_free(*fileindex);
7653 *fileindex = NULL;
7655 lock_worktree(worktree, LOCK_EX);
7657 return err;
7660 static const struct got_error *
7661 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7663 const struct got_error *err;
7664 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7665 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7667 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7668 if (err)
7669 goto done;
7670 err = delete_ref(tmp_branch_name, repo);
7671 if (err)
7672 goto done;
7674 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7675 worktree);
7676 if (err)
7677 goto done;
7678 err = delete_ref(base_commit_ref_name, repo);
7679 if (err)
7680 goto done;
7682 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7683 if (err)
7684 goto done;
7685 err = delete_ref(branch_ref_name, repo);
7686 if (err)
7687 goto done;
7689 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7690 if (err)
7691 goto done;
7692 err = delete_ref(commit_ref_name, repo);
7693 if (err)
7694 goto done;
7695 done:
7696 free(tmp_branch_name);
7697 free(base_commit_ref_name);
7698 free(branch_ref_name);
7699 free(commit_ref_name);
7700 return err;
7703 const struct got_error *
7704 got_worktree_histedit_abort(struct got_worktree *worktree,
7705 struct got_fileindex *fileindex, struct got_repository *repo,
7706 struct got_reference *branch, struct got_object_id *base_commit_id,
7707 got_worktree_checkout_cb progress_cb, void *progress_arg)
7709 const struct got_error *err, *unlockerr, *sync_err;
7710 struct got_reference *resolved = NULL;
7711 char *fileindex_path = NULL;
7712 struct got_commit_object *commit = NULL;
7713 struct got_object_id *tree_id = NULL;
7714 struct revert_file_args rfa;
7716 err = lock_worktree(worktree, LOCK_EX);
7717 if (err)
7718 return err;
7720 err = got_ref_open(&resolved, repo,
7721 got_ref_get_symref_target(branch), 0);
7722 if (err)
7723 goto done;
7725 err = got_worktree_set_head_ref(worktree, resolved);
7726 if (err)
7727 goto done;
7729 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7730 if (err)
7731 goto done;
7733 err = got_object_open_as_commit(&commit, repo,
7734 worktree->base_commit_id);
7735 if (err)
7736 goto done;
7738 err = got_object_id_by_path(&tree_id, repo, commit,
7739 worktree->path_prefix);
7740 if (err)
7741 goto done;
7743 err = delete_histedit_refs(worktree, repo);
7744 if (err)
7745 goto done;
7747 err = get_fileindex_path(&fileindex_path, worktree);
7748 if (err)
7749 goto done;
7751 rfa.worktree = worktree;
7752 rfa.fileindex = fileindex;
7753 rfa.progress_cb = progress_cb;
7754 rfa.progress_arg = progress_arg;
7755 rfa.patch_cb = NULL;
7756 rfa.patch_arg = NULL;
7757 rfa.repo = repo;
7758 rfa.unlink_added_files = 0;
7759 err = worktree_status(worktree, "", fileindex, repo,
7760 revert_file, &rfa, NULL, NULL, 1, 0);
7761 if (err)
7762 goto sync;
7764 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7765 repo, progress_cb, progress_arg, NULL, NULL);
7766 sync:
7767 sync_err = sync_fileindex(fileindex, fileindex_path);
7768 if (sync_err && err == NULL)
7769 err = sync_err;
7770 done:
7771 got_ref_close(resolved);
7772 free(tree_id);
7773 free(fileindex_path);
7775 unlockerr = lock_worktree(worktree, LOCK_SH);
7776 if (unlockerr && err == NULL)
7777 err = unlockerr;
7778 return err;
7781 const struct got_error *
7782 got_worktree_histedit_complete(struct got_worktree *worktree,
7783 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7784 struct got_reference *edited_branch, struct got_repository *repo)
7786 const struct got_error *err, *unlockerr, *sync_err;
7787 struct got_object_id *new_head_commit_id = NULL;
7788 struct got_reference *resolved = NULL;
7789 char *fileindex_path = NULL;
7791 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7792 if (err)
7793 return err;
7795 err = got_ref_open(&resolved, repo,
7796 got_ref_get_symref_target(edited_branch), 0);
7797 if (err)
7798 goto done;
7800 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7801 resolved, new_head_commit_id, repo);
7802 if (err)
7803 goto done;
7805 err = got_ref_change_ref(resolved, new_head_commit_id);
7806 if (err)
7807 goto done;
7809 err = got_ref_write(resolved, repo);
7810 if (err)
7811 goto done;
7813 err = got_worktree_set_head_ref(worktree, resolved);
7814 if (err)
7815 goto done;
7817 err = delete_histedit_refs(worktree, repo);
7818 if (err)
7819 goto done;
7821 err = get_fileindex_path(&fileindex_path, worktree);
7822 if (err)
7823 goto done;
7824 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7825 sync_err = sync_fileindex(fileindex, fileindex_path);
7826 if (sync_err && err == NULL)
7827 err = sync_err;
7828 done:
7829 got_fileindex_free(fileindex);
7830 free(fileindex_path);
7831 free(new_head_commit_id);
7832 unlockerr = lock_worktree(worktree, LOCK_SH);
7833 if (unlockerr && err == NULL)
7834 err = unlockerr;
7835 return err;
7838 const struct got_error *
7839 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7840 struct got_object_id *commit_id, struct got_repository *repo)
7842 const struct got_error *err;
7843 char *commit_ref_name;
7845 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7846 if (err)
7847 return err;
7849 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7850 if (err)
7851 goto done;
7853 err = delete_ref(commit_ref_name, repo);
7854 done:
7855 free(commit_ref_name);
7856 return err;
7859 const struct got_error *
7860 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7861 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7862 struct got_worktree *worktree, const char *refname,
7863 struct got_repository *repo)
7865 const struct got_error *err = NULL;
7866 char *fileindex_path = NULL;
7867 struct check_rebase_ok_arg ok_arg;
7869 *fileindex = NULL;
7870 *branch_ref = NULL;
7871 *base_branch_ref = NULL;
7873 err = lock_worktree(worktree, LOCK_EX);
7874 if (err)
7875 return err;
7877 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7878 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7879 "cannot integrate a branch into itself; "
7880 "update -b or different branch name required");
7881 goto done;
7884 err = open_fileindex(fileindex, &fileindex_path, worktree);
7885 if (err)
7886 goto done;
7888 /* Preconditions are the same as for rebase. */
7889 ok_arg.worktree = worktree;
7890 ok_arg.repo = repo;
7891 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7892 &ok_arg);
7893 if (err)
7894 goto done;
7896 err = got_ref_open(branch_ref, repo, refname, 1);
7897 if (err)
7898 goto done;
7900 err = got_ref_open(base_branch_ref, repo,
7901 got_worktree_get_head_ref_name(worktree), 1);
7902 done:
7903 if (err) {
7904 if (*branch_ref) {
7905 got_ref_close(*branch_ref);
7906 *branch_ref = NULL;
7908 if (*base_branch_ref) {
7909 got_ref_close(*base_branch_ref);
7910 *base_branch_ref = NULL;
7912 if (*fileindex) {
7913 got_fileindex_free(*fileindex);
7914 *fileindex = NULL;
7916 lock_worktree(worktree, LOCK_SH);
7918 return err;
7921 const struct got_error *
7922 got_worktree_integrate_continue(struct got_worktree *worktree,
7923 struct got_fileindex *fileindex, struct got_repository *repo,
7924 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7925 got_worktree_checkout_cb progress_cb, void *progress_arg,
7926 got_cancel_cb cancel_cb, void *cancel_arg)
7928 const struct got_error *err = NULL, *sync_err, *unlockerr;
7929 char *fileindex_path = NULL;
7930 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7931 struct got_commit_object *commit = NULL;
7933 err = get_fileindex_path(&fileindex_path, worktree);
7934 if (err)
7935 goto done;
7937 err = got_ref_resolve(&commit_id, repo, branch_ref);
7938 if (err)
7939 goto done;
7941 err = got_object_open_as_commit(&commit, repo, commit_id);
7942 if (err)
7943 goto done;
7945 err = got_object_id_by_path(&tree_id, repo, commit,
7946 worktree->path_prefix);
7947 if (err)
7948 goto done;
7950 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7951 if (err)
7952 goto done;
7954 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7955 progress_cb, progress_arg, cancel_cb, cancel_arg);
7956 if (err)
7957 goto sync;
7959 err = got_ref_change_ref(base_branch_ref, commit_id);
7960 if (err)
7961 goto sync;
7963 err = got_ref_write(base_branch_ref, repo);
7964 if (err)
7965 goto sync;
7967 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7968 sync:
7969 sync_err = sync_fileindex(fileindex, fileindex_path);
7970 if (sync_err && err == NULL)
7971 err = sync_err;
7973 done:
7974 unlockerr = got_ref_unlock(branch_ref);
7975 if (unlockerr && err == NULL)
7976 err = unlockerr;
7977 got_ref_close(branch_ref);
7979 unlockerr = got_ref_unlock(base_branch_ref);
7980 if (unlockerr && err == NULL)
7981 err = unlockerr;
7982 got_ref_close(base_branch_ref);
7984 got_fileindex_free(fileindex);
7985 free(fileindex_path);
7986 free(tree_id);
7987 if (commit)
7988 got_object_commit_close(commit);
7990 unlockerr = lock_worktree(worktree, LOCK_SH);
7991 if (unlockerr && err == NULL)
7992 err = unlockerr;
7993 return err;
7996 const struct got_error *
7997 got_worktree_integrate_abort(struct got_worktree *worktree,
7998 struct got_fileindex *fileindex, struct got_repository *repo,
7999 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
8001 const struct got_error *err = NULL, *unlockerr = NULL;
8003 got_fileindex_free(fileindex);
8005 err = lock_worktree(worktree, LOCK_SH);
8007 unlockerr = got_ref_unlock(branch_ref);
8008 if (unlockerr && err == NULL)
8009 err = unlockerr;
8010 got_ref_close(branch_ref);
8012 unlockerr = got_ref_unlock(base_branch_ref);
8013 if (unlockerr && err == NULL)
8014 err = unlockerr;
8015 got_ref_close(base_branch_ref);
8017 return err;
8020 const struct got_error *
8021 got_worktree_merge_postpone(struct got_worktree *worktree,
8022 struct got_fileindex *fileindex)
8024 const struct got_error *err, *sync_err;
8025 char *fileindex_path = NULL;
8027 err = get_fileindex_path(&fileindex_path, worktree);
8028 if (err)
8029 goto done;
8031 sync_err = sync_fileindex(fileindex, fileindex_path);
8033 err = lock_worktree(worktree, LOCK_SH);
8034 if (sync_err && err == NULL)
8035 err = sync_err;
8036 done:
8037 got_fileindex_free(fileindex);
8038 free(fileindex_path);
8039 return err;
8042 static const struct got_error *
8043 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
8045 const struct got_error *err;
8046 char *branch_refname = NULL, *commit_refname = NULL;
8048 err = get_merge_branch_ref_name(&branch_refname, worktree);
8049 if (err)
8050 goto done;
8051 err = delete_ref(branch_refname, repo);
8052 if (err)
8053 goto done;
8055 err = get_merge_commit_ref_name(&commit_refname, worktree);
8056 if (err)
8057 goto done;
8058 err = delete_ref(commit_refname, repo);
8059 if (err)
8060 goto done;
8062 done:
8063 free(branch_refname);
8064 free(commit_refname);
8065 return err;
8068 struct merge_commit_msg_arg {
8069 struct got_worktree *worktree;
8070 const char *branch_name;
8073 static const struct got_error *
8074 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
8075 const char *diff_path, char **logmsg, void *arg)
8077 struct merge_commit_msg_arg *a = arg;
8079 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
8080 got_worktree_get_head_ref_name(a->worktree)) == -1)
8081 return got_error_from_errno("asprintf");
8083 return NULL;
8087 const struct got_error *
8088 got_worktree_merge_branch(struct got_worktree *worktree,
8089 struct got_fileindex *fileindex,
8090 struct got_object_id *yca_commit_id,
8091 struct got_object_id *branch_tip,
8092 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
8093 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
8095 const struct got_error *err;
8096 char *fileindex_path = NULL;
8098 err = get_fileindex_path(&fileindex_path, worktree);
8099 if (err)
8100 goto done;
8102 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
8103 worktree);
8104 if (err)
8105 goto done;
8107 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
8108 branch_tip, repo, progress_cb, progress_arg,
8109 cancel_cb, cancel_arg);
8110 done:
8111 free(fileindex_path);
8112 return err;
8115 const struct got_error *
8116 got_worktree_merge_commit(struct got_object_id **new_commit_id,
8117 struct got_worktree *worktree, struct got_fileindex *fileindex,
8118 const char *author, const char *committer, int allow_bad_symlinks,
8119 struct got_object_id *branch_tip, const char *branch_name,
8120 int allow_conflict, struct got_repository *repo,
8121 got_worktree_status_cb status_cb, void *status_arg)
8124 const struct got_error *err = NULL, *sync_err;
8125 struct got_pathlist_head commitable_paths;
8126 struct collect_commitables_arg cc_arg;
8127 struct got_pathlist_entry *pe;
8128 struct got_reference *head_ref = NULL;
8129 struct got_object_id *head_commit_id = NULL;
8130 int have_staged_files = 0;
8131 struct merge_commit_msg_arg mcm_arg;
8132 char *fileindex_path = NULL;
8134 memset(&cc_arg, 0, sizeof(cc_arg));
8135 *new_commit_id = NULL;
8137 TAILQ_INIT(&commitable_paths);
8139 err = get_fileindex_path(&fileindex_path, worktree);
8140 if (err)
8141 goto done;
8143 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
8144 if (err)
8145 goto done;
8147 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8148 if (err)
8149 goto done;
8151 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
8152 &have_staged_files);
8153 if (err && err->code != GOT_ERR_CANCELLED)
8154 goto done;
8155 if (have_staged_files) {
8156 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
8157 goto done;
8160 cc_arg.commitable_paths = &commitable_paths;
8161 cc_arg.worktree = worktree;
8162 cc_arg.fileindex = fileindex;
8163 cc_arg.repo = repo;
8164 cc_arg.have_staged_files = have_staged_files;
8165 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
8166 cc_arg.commit_conflicts = allow_conflict;
8167 err = worktree_status(worktree, "", fileindex, repo,
8168 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8169 if (err)
8170 goto done;
8172 if (TAILQ_EMPTY(&commitable_paths)) {
8173 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
8174 "merge of %s cannot proceed", branch_name);
8175 goto done;
8178 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8179 struct got_commitable *ct = pe->data;
8180 const char *ct_path = ct->in_repo_path;
8182 while (ct_path[0] == '/')
8183 ct_path++;
8184 err = check_out_of_date(ct_path, ct->status,
8185 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
8186 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
8187 if (err)
8188 goto done;
8192 mcm_arg.worktree = worktree;
8193 mcm_arg.branch_name = branch_name;
8194 err = commit_worktree(new_commit_id, &commitable_paths,
8195 head_commit_id, branch_tip, worktree, author, committer, NULL,
8196 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8197 if (err)
8198 goto done;
8200 err = update_fileindex_after_commit(worktree, &commitable_paths,
8201 *new_commit_id, fileindex, have_staged_files);
8202 sync_err = sync_fileindex(fileindex, fileindex_path);
8203 if (sync_err && err == NULL)
8204 err = sync_err;
8205 done:
8206 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8207 struct got_commitable *ct = pe->data;
8209 free_commitable(ct);
8211 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8212 free(fileindex_path);
8213 return err;
8216 const struct got_error *
8217 got_worktree_merge_complete(struct got_worktree *worktree,
8218 struct got_fileindex *fileindex, struct got_repository *repo)
8220 const struct got_error *err, *unlockerr, *sync_err;
8221 char *fileindex_path = NULL;
8223 err = delete_merge_refs(worktree, repo);
8224 if (err)
8225 goto done;
8227 err = get_fileindex_path(&fileindex_path, worktree);
8228 if (err)
8229 goto done;
8230 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8231 sync_err = sync_fileindex(fileindex, fileindex_path);
8232 if (sync_err && err == NULL)
8233 err = sync_err;
8234 done:
8235 got_fileindex_free(fileindex);
8236 free(fileindex_path);
8237 unlockerr = lock_worktree(worktree, LOCK_SH);
8238 if (unlockerr && err == NULL)
8239 err = unlockerr;
8240 return err;
8243 const struct got_error *
8244 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8245 struct got_repository *repo)
8247 const struct got_error *err;
8248 char *branch_refname = NULL;
8249 struct got_reference *branch_ref = NULL;
8251 *in_progress = 0;
8253 err = get_merge_branch_ref_name(&branch_refname, worktree);
8254 if (err)
8255 return err;
8256 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8257 free(branch_refname);
8258 if (err) {
8259 if (err->code != GOT_ERR_NOT_REF)
8260 return err;
8261 } else
8262 *in_progress = 1;
8264 return NULL;
8267 const struct got_error *got_worktree_merge_prepare(
8268 struct got_fileindex **fileindex, struct got_worktree *worktree,
8269 struct got_reference *branch, struct got_repository *repo)
8271 const struct got_error *err = NULL;
8272 char *fileindex_path = NULL;
8273 char *branch_refname = NULL, *commit_refname = NULL;
8274 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
8275 struct got_reference *commit_ref = NULL;
8276 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
8277 struct check_rebase_ok_arg ok_arg;
8279 *fileindex = NULL;
8281 err = lock_worktree(worktree, LOCK_EX);
8282 if (err)
8283 return err;
8285 err = open_fileindex(fileindex, &fileindex_path, worktree);
8286 if (err)
8287 goto done;
8289 /* Preconditions are the same as for rebase. */
8290 ok_arg.worktree = worktree;
8291 ok_arg.repo = repo;
8292 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8293 &ok_arg);
8294 if (err)
8295 goto done;
8297 err = get_merge_branch_ref_name(&branch_refname, worktree);
8298 if (err)
8299 return err;
8301 err = get_merge_commit_ref_name(&commit_refname, worktree);
8302 if (err)
8303 return err;
8305 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8306 0);
8307 if (err)
8308 goto done;
8310 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8311 if (err)
8312 goto done;
8314 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8315 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8316 goto done;
8319 err = got_ref_resolve(&branch_tip, repo, branch);
8320 if (err)
8321 goto done;
8323 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8324 if (err)
8325 goto done;
8326 err = got_ref_write(branch_ref, repo);
8327 if (err)
8328 goto done;
8330 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8331 if (err)
8332 goto done;
8333 err = got_ref_write(commit_ref, repo);
8334 if (err)
8335 goto done;
8337 done:
8338 free(branch_refname);
8339 free(commit_refname);
8340 free(fileindex_path);
8341 if (branch_ref)
8342 got_ref_close(branch_ref);
8343 if (commit_ref)
8344 got_ref_close(commit_ref);
8345 if (wt_branch)
8346 got_ref_close(wt_branch);
8347 free(wt_branch_tip);
8348 if (err) {
8349 if (*fileindex) {
8350 got_fileindex_free(*fileindex);
8351 *fileindex = NULL;
8353 lock_worktree(worktree, LOCK_SH);
8355 return err;
8358 const struct got_error *
8359 got_worktree_merge_continue(char **branch_name,
8360 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8361 struct got_worktree *worktree, struct got_repository *repo)
8363 const struct got_error *err;
8364 char *commit_refname = NULL, *branch_refname = NULL;
8365 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8366 char *fileindex_path = NULL;
8367 int have_staged_files = 0;
8369 *branch_name = NULL;
8370 *branch_tip = NULL;
8371 *fileindex = NULL;
8373 err = lock_worktree(worktree, LOCK_EX);
8374 if (err)
8375 return err;
8377 err = open_fileindex(fileindex, &fileindex_path, worktree);
8378 if (err)
8379 goto done;
8381 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8382 &have_staged_files);
8383 if (err && err->code != GOT_ERR_CANCELLED)
8384 goto done;
8385 if (have_staged_files) {
8386 err = got_error(GOT_ERR_STAGED_PATHS);
8387 goto done;
8390 err = get_merge_branch_ref_name(&branch_refname, worktree);
8391 if (err)
8392 goto done;
8394 err = get_merge_commit_ref_name(&commit_refname, worktree);
8395 if (err)
8396 goto done;
8398 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8399 if (err)
8400 goto done;
8402 if (!got_ref_is_symbolic(branch_ref)) {
8403 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8404 "%s is not a symbolic reference",
8405 got_ref_get_name(branch_ref));
8406 goto done;
8408 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8409 if (*branch_name == NULL) {
8410 err = got_error_from_errno("strdup");
8411 goto done;
8414 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8415 if (err)
8416 goto done;
8418 err = got_ref_resolve(branch_tip, repo, commit_ref);
8419 if (err)
8420 goto done;
8421 done:
8422 free(commit_refname);
8423 free(branch_refname);
8424 free(fileindex_path);
8425 if (commit_ref)
8426 got_ref_close(commit_ref);
8427 if (branch_ref)
8428 got_ref_close(branch_ref);
8429 if (err) {
8430 if (*branch_name) {
8431 free(*branch_name);
8432 *branch_name = NULL;
8434 free(*branch_tip);
8435 *branch_tip = NULL;
8436 if (*fileindex) {
8437 got_fileindex_free(*fileindex);
8438 *fileindex = NULL;
8440 lock_worktree(worktree, LOCK_SH);
8442 return err;
8445 const struct got_error *
8446 got_worktree_merge_abort(struct got_worktree *worktree,
8447 struct got_fileindex *fileindex, struct got_repository *repo,
8448 got_worktree_checkout_cb progress_cb, void *progress_arg)
8450 const struct got_error *err, *unlockerr, *sync_err;
8451 struct got_object_id *commit_id = NULL;
8452 struct got_commit_object *commit = NULL;
8453 char *fileindex_path = NULL;
8454 struct revert_file_args rfa;
8455 struct got_object_id *tree_id = NULL;
8457 err = got_object_open_as_commit(&commit, repo,
8458 worktree->base_commit_id);
8459 if (err)
8460 goto done;
8462 err = got_object_id_by_path(&tree_id, repo, commit,
8463 worktree->path_prefix);
8464 if (err)
8465 goto done;
8467 err = delete_merge_refs(worktree, repo);
8468 if (err)
8469 goto done;
8471 err = get_fileindex_path(&fileindex_path, worktree);
8472 if (err)
8473 goto done;
8475 rfa.worktree = worktree;
8476 rfa.fileindex = fileindex;
8477 rfa.progress_cb = progress_cb;
8478 rfa.progress_arg = progress_arg;
8479 rfa.patch_cb = NULL;
8480 rfa.patch_arg = NULL;
8481 rfa.repo = repo;
8482 rfa.unlink_added_files = 1;
8483 err = worktree_status(worktree, "", fileindex, repo,
8484 revert_file, &rfa, NULL, NULL, 1, 0);
8485 if (err)
8486 goto sync;
8488 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8489 repo, progress_cb, progress_arg, NULL, NULL);
8490 sync:
8491 sync_err = sync_fileindex(fileindex, fileindex_path);
8492 if (sync_err && err == NULL)
8493 err = sync_err;
8494 done:
8495 free(tree_id);
8496 free(commit_id);
8497 if (commit)
8498 got_object_commit_close(commit);
8499 if (fileindex)
8500 got_fileindex_free(fileindex);
8501 free(fileindex_path);
8503 unlockerr = lock_worktree(worktree, LOCK_SH);
8504 if (unlockerr && err == NULL)
8505 err = unlockerr;
8506 return err;
8509 struct check_stage_ok_arg {
8510 struct got_object_id *head_commit_id;
8511 struct got_worktree *worktree;
8512 struct got_fileindex *fileindex;
8513 struct got_repository *repo;
8514 int have_changes;
8517 static const struct got_error *
8518 check_stage_ok(void *arg, unsigned char status,
8519 unsigned char staged_status, const char *relpath,
8520 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8521 struct got_object_id *commit_id, int dirfd, const char *de_name)
8523 struct check_stage_ok_arg *a = arg;
8524 const struct got_error *err = NULL;
8525 struct got_fileindex_entry *ie;
8526 struct got_object_id base_commit_id;
8527 struct got_object_id *base_commit_idp = NULL;
8528 char *in_repo_path = NULL, *p;
8530 if (status == GOT_STATUS_UNVERSIONED ||
8531 status == GOT_STATUS_NO_CHANGE)
8532 return NULL;
8533 if (status == GOT_STATUS_NONEXISTENT)
8534 return got_error_set_errno(ENOENT, relpath);
8536 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8537 if (ie == NULL)
8538 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8540 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8541 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8542 relpath) == -1)
8543 return got_error_from_errno("asprintf");
8545 if (got_fileindex_entry_has_commit(ie)) {
8546 base_commit_idp = got_fileindex_entry_get_commit_id(
8547 &base_commit_id, ie);
8550 if (status == GOT_STATUS_CONFLICT) {
8551 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8552 goto done;
8553 } else if (status != GOT_STATUS_ADD &&
8554 status != GOT_STATUS_MODIFY &&
8555 status != GOT_STATUS_DELETE) {
8556 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8557 goto done;
8560 a->have_changes = 1;
8562 p = in_repo_path;
8563 while (p[0] == '/')
8564 p++;
8565 err = check_out_of_date(p, status, staged_status,
8566 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8567 GOT_ERR_STAGE_OUT_OF_DATE);
8568 done:
8569 free(in_repo_path);
8570 return err;
8573 struct stage_path_arg {
8574 struct got_worktree *worktree;
8575 struct got_fileindex *fileindex;
8576 struct got_repository *repo;
8577 got_worktree_status_cb status_cb;
8578 void *status_arg;
8579 got_worktree_patch_cb patch_cb;
8580 void *patch_arg;
8581 int staged_something;
8582 int allow_bad_symlinks;
8585 static const struct got_error *
8586 stage_path(void *arg, unsigned char status,
8587 unsigned char staged_status, const char *relpath,
8588 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8589 struct got_object_id *commit_id, int dirfd, const char *de_name)
8591 struct stage_path_arg *a = arg;
8592 const struct got_error *err = NULL;
8593 struct got_fileindex_entry *ie;
8594 char *ondisk_path = NULL, *path_content = NULL;
8595 uint32_t stage;
8596 struct got_object_id *new_staged_blob_id = NULL;
8597 struct stat sb;
8599 if (status == GOT_STATUS_UNVERSIONED)
8600 return NULL;
8602 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8603 if (ie == NULL)
8604 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8606 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8607 relpath)== -1)
8608 return got_error_from_errno("asprintf");
8610 switch (status) {
8611 case GOT_STATUS_ADD:
8612 case GOT_STATUS_MODIFY:
8613 /* XXX could sb.st_mode be passed in by our caller? */
8614 if (lstat(ondisk_path, &sb) == -1) {
8615 err = got_error_from_errno2("lstat", ondisk_path);
8616 break;
8618 if (a->patch_cb) {
8619 if (status == GOT_STATUS_ADD) {
8620 int choice = GOT_PATCH_CHOICE_NONE;
8621 err = (*a->patch_cb)(&choice, a->patch_arg,
8622 status, ie->path, NULL, 1, 1);
8623 if (err)
8624 break;
8625 if (choice != GOT_PATCH_CHOICE_YES)
8626 break;
8627 } else {
8628 err = create_patched_content(&path_content, 0,
8629 staged_blob_id ? staged_blob_id : blob_id,
8630 ondisk_path, dirfd, de_name, ie->path,
8631 a->repo, a->patch_cb, a->patch_arg);
8632 if (err || path_content == NULL)
8633 break;
8636 err = got_object_blob_create(&new_staged_blob_id,
8637 path_content ? path_content : ondisk_path, a->repo);
8638 if (err)
8639 break;
8640 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8641 SHA1_DIGEST_LENGTH);
8642 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8643 stage = GOT_FILEIDX_STAGE_ADD;
8644 else
8645 stage = GOT_FILEIDX_STAGE_MODIFY;
8646 got_fileindex_entry_stage_set(ie, stage);
8647 if (S_ISLNK(sb.st_mode)) {
8648 int is_bad_symlink = 0;
8649 if (!a->allow_bad_symlinks) {
8650 char target_path[PATH_MAX];
8651 ssize_t target_len;
8652 target_len = readlink(ondisk_path, target_path,
8653 sizeof(target_path));
8654 if (target_len == -1) {
8655 err = got_error_from_errno2("readlink",
8656 ondisk_path);
8657 break;
8659 err = is_bad_symlink_target(&is_bad_symlink,
8660 target_path, target_len, ondisk_path,
8661 a->worktree->root_path);
8662 if (err)
8663 break;
8664 if (is_bad_symlink) {
8665 err = got_error_path(ondisk_path,
8666 GOT_ERR_BAD_SYMLINK);
8667 break;
8670 if (is_bad_symlink)
8671 got_fileindex_entry_staged_filetype_set(ie,
8672 GOT_FILEIDX_MODE_BAD_SYMLINK);
8673 else
8674 got_fileindex_entry_staged_filetype_set(ie,
8675 GOT_FILEIDX_MODE_SYMLINK);
8676 } else {
8677 got_fileindex_entry_staged_filetype_set(ie,
8678 GOT_FILEIDX_MODE_REGULAR_FILE);
8680 a->staged_something = 1;
8681 if (a->status_cb == NULL)
8682 break;
8683 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8684 get_staged_status(ie), relpath, blob_id,
8685 new_staged_blob_id, NULL, dirfd, de_name);
8686 if (err)
8687 break;
8689 * When staging the reverse of the staged diff,
8690 * implicitly unstage the file.
8692 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8693 sizeof(ie->blob_sha1)) == 0) {
8694 got_fileindex_entry_stage_set(ie,
8695 GOT_FILEIDX_STAGE_NONE);
8697 break;
8698 case GOT_STATUS_DELETE:
8699 if (staged_status == GOT_STATUS_DELETE)
8700 break;
8701 if (a->patch_cb) {
8702 int choice = GOT_PATCH_CHOICE_NONE;
8703 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8704 ie->path, NULL, 1, 1);
8705 if (err)
8706 break;
8707 if (choice == GOT_PATCH_CHOICE_NO)
8708 break;
8709 if (choice != GOT_PATCH_CHOICE_YES) {
8710 err = got_error(GOT_ERR_PATCH_CHOICE);
8711 break;
8714 stage = GOT_FILEIDX_STAGE_DELETE;
8715 got_fileindex_entry_stage_set(ie, stage);
8716 a->staged_something = 1;
8717 if (a->status_cb == NULL)
8718 break;
8719 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8720 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8721 de_name);
8722 break;
8723 case GOT_STATUS_NO_CHANGE:
8724 break;
8725 case GOT_STATUS_CONFLICT:
8726 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8727 break;
8728 case GOT_STATUS_NONEXISTENT:
8729 err = got_error_set_errno(ENOENT, relpath);
8730 break;
8731 default:
8732 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8733 break;
8736 if (path_content && unlink(path_content) == -1 && err == NULL)
8737 err = got_error_from_errno2("unlink", path_content);
8738 free(path_content);
8739 free(ondisk_path);
8740 free(new_staged_blob_id);
8741 return err;
8744 const struct got_error *
8745 got_worktree_stage(struct got_worktree *worktree,
8746 struct got_pathlist_head *paths,
8747 got_worktree_status_cb status_cb, void *status_arg,
8748 got_worktree_patch_cb patch_cb, void *patch_arg,
8749 int allow_bad_symlinks, struct got_repository *repo)
8751 const struct got_error *err = NULL, *sync_err, *unlockerr;
8752 struct got_pathlist_entry *pe;
8753 struct got_fileindex *fileindex = NULL;
8754 char *fileindex_path = NULL;
8755 struct got_reference *head_ref = NULL;
8756 struct got_object_id *head_commit_id = NULL;
8757 struct check_stage_ok_arg oka;
8758 struct stage_path_arg spa;
8760 err = lock_worktree(worktree, LOCK_EX);
8761 if (err)
8762 return err;
8764 err = got_ref_open(&head_ref, repo,
8765 got_worktree_get_head_ref_name(worktree), 0);
8766 if (err)
8767 goto done;
8768 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8769 if (err)
8770 goto done;
8771 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8772 if (err)
8773 goto done;
8775 /* Check pre-conditions before staging anything. */
8776 oka.head_commit_id = head_commit_id;
8777 oka.worktree = worktree;
8778 oka.fileindex = fileindex;
8779 oka.repo = repo;
8780 oka.have_changes = 0;
8781 TAILQ_FOREACH(pe, paths, entry) {
8782 err = worktree_status(worktree, pe->path, fileindex, repo,
8783 check_stage_ok, &oka, NULL, NULL, 1, 0);
8784 if (err)
8785 goto done;
8787 if (!oka.have_changes) {
8788 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8789 goto done;
8792 spa.worktree = worktree;
8793 spa.fileindex = fileindex;
8794 spa.repo = repo;
8795 spa.patch_cb = patch_cb;
8796 spa.patch_arg = patch_arg;
8797 spa.status_cb = status_cb;
8798 spa.status_arg = status_arg;
8799 spa.staged_something = 0;
8800 spa.allow_bad_symlinks = allow_bad_symlinks;
8801 TAILQ_FOREACH(pe, paths, entry) {
8802 err = worktree_status(worktree, pe->path, fileindex, repo,
8803 stage_path, &spa, NULL, NULL, 1, 0);
8804 if (err)
8805 goto done;
8807 if (!spa.staged_something) {
8808 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8809 goto done;
8812 sync_err = sync_fileindex(fileindex, fileindex_path);
8813 if (sync_err && err == NULL)
8814 err = sync_err;
8815 done:
8816 if (head_ref)
8817 got_ref_close(head_ref);
8818 free(head_commit_id);
8819 free(fileindex_path);
8820 if (fileindex)
8821 got_fileindex_free(fileindex);
8822 unlockerr = lock_worktree(worktree, LOCK_SH);
8823 if (unlockerr && err == NULL)
8824 err = unlockerr;
8825 return err;
8828 struct unstage_path_arg {
8829 struct got_worktree *worktree;
8830 struct got_fileindex *fileindex;
8831 struct got_repository *repo;
8832 got_worktree_checkout_cb progress_cb;
8833 void *progress_arg;
8834 got_worktree_patch_cb patch_cb;
8835 void *patch_arg;
8838 static const struct got_error *
8839 create_unstaged_content(char **path_unstaged_content,
8840 char **path_new_staged_content, struct got_object_id *blob_id,
8841 struct got_object_id *staged_blob_id, const char *relpath,
8842 struct got_repository *repo,
8843 got_worktree_patch_cb patch_cb, void *patch_arg)
8845 const struct got_error *err, *free_err;
8846 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8847 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8848 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8849 struct got_diffreg_result *diffreg_result = NULL;
8850 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8851 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8852 int fd1 = -1, fd2 = -1;
8854 *path_unstaged_content = NULL;
8855 *path_new_staged_content = NULL;
8857 err = got_object_id_str(&label1, blob_id);
8858 if (err)
8859 return err;
8861 fd1 = got_opentempfd();
8862 if (fd1 == -1) {
8863 err = got_error_from_errno("got_opentempfd");
8864 goto done;
8866 fd2 = got_opentempfd();
8867 if (fd2 == -1) {
8868 err = got_error_from_errno("got_opentempfd");
8869 goto done;
8872 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8873 if (err)
8874 goto done;
8876 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
8877 if (err)
8878 goto done;
8880 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8881 if (err)
8882 goto done;
8884 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8885 fd2);
8886 if (err)
8887 goto done;
8889 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
8890 if (err)
8891 goto done;
8893 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8894 if (err)
8895 goto done;
8897 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8898 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8899 if (err)
8900 goto done;
8902 err = got_opentemp_named(path_unstaged_content, &outfile,
8903 "got-unstaged-content", "");
8904 if (err)
8905 goto done;
8906 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8907 "got-new-staged-content", "");
8908 if (err)
8909 goto done;
8911 if (fseek(f1, 0L, SEEK_SET) == -1) {
8912 err = got_ferror(f1, GOT_ERR_IO);
8913 goto done;
8915 if (fseek(f2, 0L, SEEK_SET) == -1) {
8916 err = got_ferror(f2, GOT_ERR_IO);
8917 goto done;
8919 /* Count the number of actual changes in the diff result. */
8920 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8921 struct diff_chunk_context cc = {};
8922 diff_chunk_context_load_change(&cc, &nchunks_used,
8923 diffreg_result->result, n, 0);
8924 nchanges++;
8926 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8927 int choice;
8928 err = apply_or_reject_change(&choice, &nchunks_used,
8929 diffreg_result->result, n, relpath, f1, f2,
8930 &line_cur1, &line_cur2,
8931 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8932 if (err)
8933 goto done;
8934 if (choice == GOT_PATCH_CHOICE_YES)
8935 have_content = 1;
8936 else
8937 have_rejected_content = 1;
8938 if (choice == GOT_PATCH_CHOICE_QUIT)
8939 break;
8941 if (have_content || have_rejected_content)
8942 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8943 outfile, rejectfile);
8944 done:
8945 free(label1);
8946 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8947 err = got_error_from_errno("close");
8948 if (blob)
8949 got_object_blob_close(blob);
8950 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8951 err = got_error_from_errno("close");
8952 if (staged_blob)
8953 got_object_blob_close(staged_blob);
8954 free_err = got_diffreg_result_free(diffreg_result);
8955 if (free_err && err == NULL)
8956 err = free_err;
8957 if (f1 && fclose(f1) == EOF && err == NULL)
8958 err = got_error_from_errno2("fclose", path1);
8959 if (f2 && fclose(f2) == EOF && err == NULL)
8960 err = got_error_from_errno2("fclose", path2);
8961 if (outfile && fclose(outfile) == EOF && err == NULL)
8962 err = got_error_from_errno2("fclose", *path_unstaged_content);
8963 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8964 err = got_error_from_errno2("fclose", *path_new_staged_content);
8965 if (path1 && unlink(path1) == -1 && err == NULL)
8966 err = got_error_from_errno2("unlink", path1);
8967 if (path2 && unlink(path2) == -1 && err == NULL)
8968 err = got_error_from_errno2("unlink", path2);
8969 if (err || !have_content) {
8970 if (*path_unstaged_content &&
8971 unlink(*path_unstaged_content) == -1 && err == NULL)
8972 err = got_error_from_errno2("unlink",
8973 *path_unstaged_content);
8974 free(*path_unstaged_content);
8975 *path_unstaged_content = NULL;
8977 if (err || !have_content || !have_rejected_content) {
8978 if (*path_new_staged_content &&
8979 unlink(*path_new_staged_content) == -1 && err == NULL)
8980 err = got_error_from_errno2("unlink",
8981 *path_new_staged_content);
8982 free(*path_new_staged_content);
8983 *path_new_staged_content = NULL;
8985 free(path1);
8986 free(path2);
8987 return err;
8990 static const struct got_error *
8991 unstage_hunks(struct got_object_id *staged_blob_id,
8992 struct got_blob_object *blob_base,
8993 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8994 const char *ondisk_path, const char *label_orig,
8995 struct got_worktree *worktree, struct got_repository *repo,
8996 got_worktree_patch_cb patch_cb, void *patch_arg,
8997 got_worktree_checkout_cb progress_cb, void *progress_arg)
8999 const struct got_error *err = NULL;
9000 char *path_unstaged_content = NULL;
9001 char *path_new_staged_content = NULL;
9002 char *parent = NULL, *base_path = NULL;
9003 char *blob_base_path = NULL;
9004 struct got_object_id *new_staged_blob_id = NULL;
9005 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
9006 struct stat sb;
9008 err = create_unstaged_content(&path_unstaged_content,
9009 &path_new_staged_content, blob_id, staged_blob_id,
9010 ie->path, repo, patch_cb, patch_arg);
9011 if (err)
9012 return err;
9014 if (path_unstaged_content == NULL)
9015 return NULL;
9017 if (path_new_staged_content) {
9018 err = got_object_blob_create(&new_staged_blob_id,
9019 path_new_staged_content, repo);
9020 if (err)
9021 goto done;
9024 f = fopen(path_unstaged_content, "re");
9025 if (f == NULL) {
9026 err = got_error_from_errno2("fopen",
9027 path_unstaged_content);
9028 goto done;
9030 if (fstat(fileno(f), &sb) == -1) {
9031 err = got_error_from_errno2("fstat", path_unstaged_content);
9032 goto done;
9034 if (got_fileindex_entry_staged_filetype_get(ie) ==
9035 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
9036 char link_target[PATH_MAX];
9037 size_t r;
9038 r = fread(link_target, 1, sizeof(link_target), f);
9039 if (r == 0 && ferror(f)) {
9040 err = got_error_from_errno("fread");
9041 goto done;
9043 if (r >= sizeof(link_target)) { /* should not happen */
9044 err = got_error(GOT_ERR_NO_SPACE);
9045 goto done;
9047 link_target[r] = '\0';
9048 err = merge_symlink(worktree, blob_base,
9049 ondisk_path, ie->path, label_orig, link_target,
9050 worktree->base_commit_id, repo, progress_cb,
9051 progress_arg);
9052 } else {
9053 int local_changes_subsumed;
9055 err = got_path_dirname(&parent, ondisk_path);
9056 if (err)
9057 return err;
9059 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
9060 parent) == -1) {
9061 err = got_error_from_errno("asprintf");
9062 base_path = NULL;
9063 goto done;
9066 err = got_opentemp_named(&blob_base_path, &f_base,
9067 base_path, "");
9068 if (err)
9069 goto done;
9070 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
9071 blob_base);
9072 if (err)
9073 goto done;
9076 * In order the run a 3-way merge with a symlink we copy the symlink's
9077 * target path into a temporary file and use that file with diff3.
9079 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9080 err = dump_symlink_target_path_to_file(&f_deriv2,
9081 ondisk_path);
9082 if (err)
9083 goto done;
9084 } else {
9085 int fd;
9086 fd = open(ondisk_path,
9087 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
9088 if (fd == -1) {
9089 err = got_error_from_errno2("open", ondisk_path);
9090 goto done;
9092 f_deriv2 = fdopen(fd, "r");
9093 if (f_deriv2 == NULL) {
9094 err = got_error_from_errno2("fdopen", ondisk_path);
9095 close(fd);
9096 goto done;
9100 err = merge_file(&local_changes_subsumed, worktree,
9101 f_base, f, f_deriv2, ondisk_path, ie->path,
9102 got_fileindex_perms_to_st(ie),
9103 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
9104 repo, progress_cb, progress_arg);
9106 if (err)
9107 goto done;
9109 if (new_staged_blob_id) {
9110 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9111 SHA1_DIGEST_LENGTH);
9112 } else {
9113 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9114 got_fileindex_entry_staged_filetype_set(ie, 0);
9116 done:
9117 free(new_staged_blob_id);
9118 if (path_unstaged_content &&
9119 unlink(path_unstaged_content) == -1 && err == NULL)
9120 err = got_error_from_errno2("unlink", path_unstaged_content);
9121 if (path_new_staged_content &&
9122 unlink(path_new_staged_content) == -1 && err == NULL)
9123 err = got_error_from_errno2("unlink", path_new_staged_content);
9124 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
9125 err = got_error_from_errno2("unlink", blob_base_path);
9126 if (f_base && fclose(f_base) == EOF && err == NULL)
9127 err = got_error_from_errno2("fclose", path_unstaged_content);
9128 if (f && fclose(f) == EOF && err == NULL)
9129 err = got_error_from_errno2("fclose", path_unstaged_content);
9130 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
9131 err = got_error_from_errno2("fclose", ondisk_path);
9132 free(path_unstaged_content);
9133 free(path_new_staged_content);
9134 free(blob_base_path);
9135 free(parent);
9136 free(base_path);
9137 return err;
9140 static const struct got_error *
9141 unstage_path(void *arg, unsigned char status,
9142 unsigned char staged_status, const char *relpath,
9143 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9144 struct got_object_id *commit_id, int dirfd, const char *de_name)
9146 const struct got_error *err = NULL;
9147 struct unstage_path_arg *a = arg;
9148 struct got_fileindex_entry *ie;
9149 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
9150 char *ondisk_path = NULL;
9151 char *id_str = NULL, *label_orig = NULL;
9152 int local_changes_subsumed;
9153 struct stat sb;
9154 int fd1 = -1, fd2 = -1;
9156 if (staged_status != GOT_STATUS_ADD &&
9157 staged_status != GOT_STATUS_MODIFY &&
9158 staged_status != GOT_STATUS_DELETE)
9159 return NULL;
9161 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9162 if (ie == NULL)
9163 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9165 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9166 == -1)
9167 return got_error_from_errno("asprintf");
9169 err = got_object_id_str(&id_str,
9170 commit_id ? commit_id : a->worktree->base_commit_id);
9171 if (err)
9172 goto done;
9173 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9174 id_str) == -1) {
9175 err = got_error_from_errno("asprintf");
9176 goto done;
9179 fd1 = got_opentempfd();
9180 if (fd1 == -1) {
9181 err = got_error_from_errno("got_opentempfd");
9182 goto done;
9184 fd2 = got_opentempfd();
9185 if (fd2 == -1) {
9186 err = got_error_from_errno("got_opentempfd");
9187 goto done;
9190 switch (staged_status) {
9191 case GOT_STATUS_MODIFY:
9192 err = got_object_open_as_blob(&blob_base, a->repo,
9193 blob_id, 8192, fd1);
9194 if (err)
9195 break;
9196 /* fall through */
9197 case GOT_STATUS_ADD:
9198 if (a->patch_cb) {
9199 if (staged_status == GOT_STATUS_ADD) {
9200 int choice = GOT_PATCH_CHOICE_NONE;
9201 err = (*a->patch_cb)(&choice, a->patch_arg,
9202 staged_status, ie->path, NULL, 1, 1);
9203 if (err)
9204 break;
9205 if (choice != GOT_PATCH_CHOICE_YES)
9206 break;
9207 } else {
9208 err = unstage_hunks(staged_blob_id,
9209 blob_base, blob_id, ie, ondisk_path,
9210 label_orig, a->worktree, a->repo,
9211 a->patch_cb, a->patch_arg,
9212 a->progress_cb, a->progress_arg);
9213 break; /* Done with this file. */
9216 err = got_object_open_as_blob(&blob_staged, a->repo,
9217 staged_blob_id, 8192, fd2);
9218 if (err)
9219 break;
9220 switch (got_fileindex_entry_staged_filetype_get(ie)) {
9221 case GOT_FILEIDX_MODE_BAD_SYMLINK:
9222 case GOT_FILEIDX_MODE_REGULAR_FILE:
9223 err = merge_blob(&local_changes_subsumed, a->worktree,
9224 blob_base, ondisk_path, relpath,
9225 got_fileindex_perms_to_st(ie), label_orig,
9226 blob_staged, commit_id ? commit_id :
9227 a->worktree->base_commit_id, a->repo,
9228 a->progress_cb, a->progress_arg);
9229 break;
9230 case GOT_FILEIDX_MODE_SYMLINK:
9231 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9232 char *staged_target;
9233 err = got_object_blob_read_to_str(
9234 &staged_target, blob_staged);
9235 if (err)
9236 goto done;
9237 err = merge_symlink(a->worktree, blob_base,
9238 ondisk_path, relpath, label_orig,
9239 staged_target, commit_id ? commit_id :
9240 a->worktree->base_commit_id,
9241 a->repo, a->progress_cb, a->progress_arg);
9242 free(staged_target);
9243 } else {
9244 err = merge_blob(&local_changes_subsumed,
9245 a->worktree, blob_base, ondisk_path,
9246 relpath, got_fileindex_perms_to_st(ie),
9247 label_orig, blob_staged,
9248 commit_id ? commit_id :
9249 a->worktree->base_commit_id, a->repo,
9250 a->progress_cb, a->progress_arg);
9252 break;
9253 default:
9254 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9255 break;
9257 if (err == NULL) {
9258 got_fileindex_entry_stage_set(ie,
9259 GOT_FILEIDX_STAGE_NONE);
9260 got_fileindex_entry_staged_filetype_set(ie, 0);
9262 break;
9263 case GOT_STATUS_DELETE:
9264 if (a->patch_cb) {
9265 int choice = GOT_PATCH_CHOICE_NONE;
9266 err = (*a->patch_cb)(&choice, a->patch_arg,
9267 staged_status, ie->path, NULL, 1, 1);
9268 if (err)
9269 break;
9270 if (choice == GOT_PATCH_CHOICE_NO)
9271 break;
9272 if (choice != GOT_PATCH_CHOICE_YES) {
9273 err = got_error(GOT_ERR_PATCH_CHOICE);
9274 break;
9277 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9278 got_fileindex_entry_staged_filetype_set(ie, 0);
9279 err = get_file_status(&status, &sb, ie, ondisk_path,
9280 dirfd, de_name, a->repo);
9281 if (err)
9282 break;
9283 err = (*a->progress_cb)(a->progress_arg, status, relpath);
9284 break;
9286 done:
9287 free(ondisk_path);
9288 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9289 err = got_error_from_errno("close");
9290 if (blob_base)
9291 got_object_blob_close(blob_base);
9292 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9293 err = got_error_from_errno("close");
9294 if (blob_staged)
9295 got_object_blob_close(blob_staged);
9296 free(id_str);
9297 free(label_orig);
9298 return err;
9301 const struct got_error *
9302 got_worktree_unstage(struct got_worktree *worktree,
9303 struct got_pathlist_head *paths,
9304 got_worktree_checkout_cb progress_cb, void *progress_arg,
9305 got_worktree_patch_cb patch_cb, void *patch_arg,
9306 struct got_repository *repo)
9308 const struct got_error *err = NULL, *sync_err, *unlockerr;
9309 struct got_pathlist_entry *pe;
9310 struct got_fileindex *fileindex = NULL;
9311 char *fileindex_path = NULL;
9312 struct unstage_path_arg upa;
9314 err = lock_worktree(worktree, LOCK_EX);
9315 if (err)
9316 return err;
9318 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9319 if (err)
9320 goto done;
9322 upa.worktree = worktree;
9323 upa.fileindex = fileindex;
9324 upa.repo = repo;
9325 upa.progress_cb = progress_cb;
9326 upa.progress_arg = progress_arg;
9327 upa.patch_cb = patch_cb;
9328 upa.patch_arg = patch_arg;
9329 TAILQ_FOREACH(pe, paths, entry) {
9330 err = worktree_status(worktree, pe->path, fileindex, repo,
9331 unstage_path, &upa, NULL, NULL, 1, 0);
9332 if (err)
9333 goto done;
9336 sync_err = sync_fileindex(fileindex, fileindex_path);
9337 if (sync_err && err == NULL)
9338 err = sync_err;
9339 done:
9340 free(fileindex_path);
9341 if (fileindex)
9342 got_fileindex_free(fileindex);
9343 unlockerr = lock_worktree(worktree, LOCK_SH);
9344 if (unlockerr && err == NULL)
9345 err = unlockerr;
9346 return err;
9349 struct report_file_info_arg {
9350 struct got_worktree *worktree;
9351 got_worktree_path_info_cb info_cb;
9352 void *info_arg;
9353 struct got_pathlist_head *paths;
9354 got_cancel_cb cancel_cb;
9355 void *cancel_arg;
9358 static const struct got_error *
9359 report_file_info(void *arg, struct got_fileindex_entry *ie)
9361 struct report_file_info_arg *a = arg;
9362 struct got_pathlist_entry *pe;
9363 struct got_object_id blob_id, staged_blob_id, commit_id;
9364 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9365 struct got_object_id *commit_idp = NULL;
9366 int stage;
9368 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9369 return got_error(GOT_ERR_CANCELLED);
9371 TAILQ_FOREACH(pe, a->paths, entry) {
9372 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9373 got_path_is_child(ie->path, pe->path, pe->path_len))
9374 break;
9376 if (pe == NULL) /* not found */
9377 return NULL;
9379 if (got_fileindex_entry_has_blob(ie))
9380 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9381 stage = got_fileindex_entry_stage_get(ie);
9382 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9383 stage == GOT_FILEIDX_STAGE_ADD) {
9384 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9385 &staged_blob_id, ie);
9388 if (got_fileindex_entry_has_commit(ie))
9389 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9391 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9392 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9395 const struct got_error *
9396 got_worktree_path_info(struct got_worktree *worktree,
9397 struct got_pathlist_head *paths,
9398 got_worktree_path_info_cb info_cb, void *info_arg,
9399 got_cancel_cb cancel_cb, void *cancel_arg)
9402 const struct got_error *err = NULL, *unlockerr;
9403 struct got_fileindex *fileindex = NULL;
9404 char *fileindex_path = NULL;
9405 struct report_file_info_arg arg;
9407 err = lock_worktree(worktree, LOCK_SH);
9408 if (err)
9409 return err;
9411 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9412 if (err)
9413 goto done;
9415 arg.worktree = worktree;
9416 arg.info_cb = info_cb;
9417 arg.info_arg = info_arg;
9418 arg.paths = paths;
9419 arg.cancel_cb = cancel_cb;
9420 arg.cancel_arg = cancel_arg;
9421 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9422 &arg);
9423 done:
9424 free(fileindex_path);
9425 if (fileindex)
9426 got_fileindex_free(fileindex);
9427 unlockerr = lock_worktree(worktree, LOCK_UN);
9428 if (unlockerr && err == NULL)
9429 err = unlockerr;
9430 return err;
9433 static const struct got_error *
9434 patch_check_path(const char *p, char **path, unsigned char *status,
9435 unsigned char *staged_status, struct got_fileindex *fileindex,
9436 struct got_worktree *worktree, struct got_repository *repo)
9438 const struct got_error *err;
9439 struct got_fileindex_entry *ie;
9440 struct stat sb;
9441 char *ondisk_path = NULL;
9443 err = got_worktree_resolve_path(path, worktree, p);
9444 if (err)
9445 return err;
9447 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9448 *path[0] ? "/" : "", *path) == -1)
9449 return got_error_from_errno("asprintf");
9451 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9452 if (ie) {
9453 *staged_status = get_staged_status(ie);
9454 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9455 repo);
9456 if (err)
9457 goto done;
9458 } else {
9459 *staged_status = GOT_STATUS_NO_CHANGE;
9460 *status = GOT_STATUS_UNVERSIONED;
9461 if (lstat(ondisk_path, &sb) == -1) {
9462 if (errno != ENOENT) {
9463 err = got_error_from_errno2("lstat",
9464 ondisk_path);
9465 goto done;
9467 *status = GOT_STATUS_NONEXISTENT;
9471 done:
9472 free(ondisk_path);
9473 return err;
9476 static const struct got_error *
9477 patch_can_rm(const char *path, unsigned char status,
9478 unsigned char staged_status)
9480 if (status == GOT_STATUS_NONEXISTENT)
9481 return got_error_set_errno(ENOENT, path);
9482 if (status != GOT_STATUS_NO_CHANGE &&
9483 status != GOT_STATUS_ADD &&
9484 status != GOT_STATUS_MODIFY &&
9485 status != GOT_STATUS_MODE_CHANGE)
9486 return got_error_path(path, GOT_ERR_FILE_STATUS);
9487 if (staged_status == GOT_STATUS_DELETE)
9488 return got_error_path(path, GOT_ERR_FILE_STATUS);
9489 return NULL;
9492 static const struct got_error *
9493 patch_can_add(const char *path, unsigned char status)
9495 if (status != GOT_STATUS_NONEXISTENT)
9496 return got_error_path(path, GOT_ERR_FILE_STATUS);
9497 return NULL;
9500 static const struct got_error *
9501 patch_can_edit(const char *path, unsigned char status,
9502 unsigned char staged_status)
9504 if (status == GOT_STATUS_NONEXISTENT)
9505 return got_error_set_errno(ENOENT, path);
9506 if (status != GOT_STATUS_NO_CHANGE &&
9507 status != GOT_STATUS_ADD &&
9508 status != GOT_STATUS_MODIFY)
9509 return got_error_path(path, GOT_ERR_FILE_STATUS);
9510 if (staged_status == GOT_STATUS_DELETE)
9511 return got_error_path(path, GOT_ERR_FILE_STATUS);
9512 return NULL;
9515 const struct got_error *
9516 got_worktree_patch_prepare(struct got_fileindex **fileindex,
9517 char **fileindex_path, struct got_worktree *worktree)
9519 return open_fileindex(fileindex, fileindex_path, worktree);
9522 const struct got_error *
9523 got_worktree_patch_check_path(const char *old, const char *new,
9524 char **oldpath, char **newpath, struct got_worktree *worktree,
9525 struct got_repository *repo, struct got_fileindex *fileindex)
9527 const struct got_error *err = NULL;
9528 int file_renamed = 0;
9529 unsigned char status_old, staged_status_old;
9530 unsigned char status_new, staged_status_new;
9532 *oldpath = NULL;
9533 *newpath = NULL;
9535 err = patch_check_path(old != NULL ? old : new, oldpath,
9536 &status_old, &staged_status_old, fileindex, worktree, repo);
9537 if (err)
9538 goto done;
9540 err = patch_check_path(new != NULL ? new : old, newpath,
9541 &status_new, &staged_status_new, fileindex, worktree, repo);
9542 if (err)
9543 goto done;
9545 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9546 file_renamed = 1;
9548 if (old != NULL && new == NULL)
9549 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9550 else if (file_renamed) {
9551 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9552 if (err == NULL)
9553 err = patch_can_add(*newpath, status_new);
9554 } else if (old == NULL)
9555 err = patch_can_add(*newpath, status_new);
9556 else
9557 err = patch_can_edit(*newpath, status_new, staged_status_new);
9559 done:
9560 if (err) {
9561 free(*oldpath);
9562 *oldpath = NULL;
9563 free(*newpath);
9564 *newpath = NULL;
9566 return err;
9569 const struct got_error *
9570 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9571 struct got_worktree *worktree, struct got_fileindex *fileindex,
9572 got_worktree_checkout_cb progress_cb, void *progress_arg)
9574 struct schedule_addition_args saa;
9576 memset(&saa, 0, sizeof(saa));
9577 saa.worktree = worktree;
9578 saa.fileindex = fileindex;
9579 saa.progress_cb = progress_cb;
9580 saa.progress_arg = progress_arg;
9581 saa.repo = repo;
9583 return worktree_status(worktree, path, fileindex, repo,
9584 schedule_addition, &saa, NULL, NULL, 1, 0);
9587 const struct got_error *
9588 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9589 struct got_worktree *worktree, struct got_fileindex *fileindex,
9590 got_worktree_delete_cb progress_cb, void *progress_arg)
9592 struct schedule_deletion_args sda;
9594 memset(&sda, 0, sizeof(sda));
9595 sda.worktree = worktree;
9596 sda.fileindex = fileindex;
9597 sda.progress_cb = progress_cb;
9598 sda.progress_arg = progress_arg;
9599 sda.repo = repo;
9600 sda.delete_local_mods = 0;
9601 sda.keep_on_disk = 0;
9602 sda.ignore_missing_paths = 0;
9603 sda.status_codes = NULL;
9605 return worktree_status(worktree, path, fileindex, repo,
9606 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9609 const struct got_error *
9610 got_worktree_patch_complete(struct got_fileindex *fileindex,
9611 const char *fileindex_path)
9613 const struct got_error *err = NULL;
9615 err = sync_fileindex(fileindex, fileindex_path);
9616 got_fileindex_free(fileindex);
9618 return err;