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 static const struct got_error *
3889 worktree_status(struct got_worktree *worktree, const char *path,
3890 struct got_fileindex *fileindex, struct got_repository *repo,
3891 got_worktree_status_cb status_cb, void *status_arg,
3892 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3893 int report_unchanged)
3895 const struct got_error *err = NULL;
3896 int fd = -1;
3897 struct got_fileindex_diff_dir_cb fdiff_cb;
3898 struct diff_dir_cb_arg arg;
3899 char *ondisk_path = NULL;
3900 struct got_pathlist_head ignores;
3901 struct got_fileindex_entry *ie;
3903 TAILQ_INIT(&ignores);
3905 if (asprintf(&ondisk_path, "%s%s%s",
3906 worktree->root_path, path[0] ? "/" : "", path) == -1)
3907 return got_error_from_errno("asprintf");
3909 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3910 if (ie) {
3911 err = report_single_file_status(path, ondisk_path,
3912 fileindex, status_cb, status_arg, repo,
3913 report_unchanged, &ignores, no_ignores);
3914 goto done;
3917 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3918 if (fd == -1) {
3919 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3920 !got_err_open_nofollow_on_symlink())
3921 err = got_error_from_errno2("open", ondisk_path);
3922 else {
3923 if (!no_ignores) {
3924 err = add_ignores_from_parent_paths(&ignores,
3925 worktree->root_path, ondisk_path);
3926 if (err)
3927 goto done;
3929 err = report_single_file_status(path, ondisk_path,
3930 fileindex, status_cb, status_arg, repo,
3931 report_unchanged, &ignores, no_ignores);
3933 } else {
3934 fdiff_cb.diff_old_new = status_old_new;
3935 fdiff_cb.diff_old = status_old;
3936 fdiff_cb.diff_new = status_new;
3937 fdiff_cb.diff_traverse = status_traverse;
3938 arg.fileindex = fileindex;
3939 arg.worktree = worktree;
3940 arg.status_path = path;
3941 arg.status_path_len = strlen(path);
3942 arg.repo = repo;
3943 arg.status_cb = status_cb;
3944 arg.status_arg = status_arg;
3945 arg.cancel_cb = cancel_cb;
3946 arg.cancel_arg = cancel_arg;
3947 arg.report_unchanged = report_unchanged;
3948 arg.no_ignores = no_ignores;
3949 if (!no_ignores) {
3950 err = add_ignores_from_parent_paths(&ignores,
3951 worktree->root_path, path);
3952 if (err)
3953 goto done;
3955 arg.ignores = &ignores;
3956 err = got_fileindex_diff_dir(fileindex, fd,
3957 worktree->root_path, path, repo, &fdiff_cb, &arg);
3959 done:
3960 free_ignores(&ignores);
3961 if (fd != -1 && close(fd) == -1 && err == NULL)
3962 err = got_error_from_errno("close");
3963 free(ondisk_path);
3964 return err;
3967 const struct got_error *
3968 got_worktree_status(struct got_worktree *worktree,
3969 struct got_pathlist_head *paths, struct got_repository *repo,
3970 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3971 got_cancel_cb cancel_cb, void *cancel_arg)
3973 const struct got_error *err = NULL;
3974 char *fileindex_path = NULL;
3975 struct got_fileindex *fileindex = NULL;
3976 struct got_pathlist_entry *pe;
3978 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3979 if (err)
3980 return err;
3982 TAILQ_FOREACH(pe, paths, entry) {
3983 err = worktree_status(worktree, pe->path, fileindex, repo,
3984 status_cb, status_arg, cancel_cb, cancel_arg,
3985 no_ignores, 0);
3986 if (err)
3987 break;
3989 free(fileindex_path);
3990 got_fileindex_free(fileindex);
3991 return err;
3994 const struct got_error *
3995 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3996 const char *arg)
3998 const struct got_error *err = NULL;
3999 char *resolved = NULL, *cwd = NULL, *path = NULL;
4000 size_t len;
4001 struct stat sb;
4002 char *abspath = NULL;
4003 char canonpath[PATH_MAX];
4005 *wt_path = NULL;
4007 cwd = getcwd(NULL, 0);
4008 if (cwd == NULL)
4009 return got_error_from_errno("getcwd");
4011 if (lstat(arg, &sb) == -1) {
4012 if (errno != ENOENT) {
4013 err = got_error_from_errno2("lstat", arg);
4014 goto done;
4016 sb.st_mode = 0;
4018 if (S_ISLNK(sb.st_mode)) {
4020 * We cannot use realpath(3) with symlinks since we want to
4021 * operate on the symlink itself.
4022 * But we can make the path absolute, assuming it is relative
4023 * to the current working directory, and then canonicalize it.
4025 if (!got_path_is_absolute(arg)) {
4026 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4027 err = got_error_from_errno("asprintf");
4028 goto done;
4032 err = got_canonpath(abspath ? abspath : arg, canonpath,
4033 sizeof(canonpath));
4034 if (err)
4035 goto done;
4036 resolved = strdup(canonpath);
4037 if (resolved == NULL) {
4038 err = got_error_from_errno("strdup");
4039 goto done;
4041 } else {
4042 resolved = realpath(arg, NULL);
4043 if (resolved == NULL) {
4044 if (errno != ENOENT) {
4045 err = got_error_from_errno2("realpath", arg);
4046 goto done;
4048 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4049 err = got_error_from_errno("asprintf");
4050 goto done;
4052 err = got_canonpath(abspath, canonpath,
4053 sizeof(canonpath));
4054 if (err)
4055 goto done;
4056 resolved = strdup(canonpath);
4057 if (resolved == NULL) {
4058 err = got_error_from_errno("strdup");
4059 goto done;
4064 if (strncmp(got_worktree_get_root_path(worktree), resolved,
4065 strlen(got_worktree_get_root_path(worktree)))) {
4066 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
4067 goto done;
4070 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
4071 err = got_path_skip_common_ancestor(&path,
4072 got_worktree_get_root_path(worktree), resolved);
4073 if (err)
4074 goto done;
4075 } else {
4076 path = strdup("");
4077 if (path == NULL) {
4078 err = got_error_from_errno("strdup");
4079 goto done;
4083 /* XXX status walk can't deal with trailing slash! */
4084 len = strlen(path);
4085 while (len > 0 && path[len - 1] == '/') {
4086 path[len - 1] = '\0';
4087 len--;
4089 done:
4090 free(abspath);
4091 free(resolved);
4092 free(cwd);
4093 if (err == NULL)
4094 *wt_path = path;
4095 else
4096 free(path);
4097 return err;
4100 struct schedule_addition_args {
4101 struct got_worktree *worktree;
4102 struct got_fileindex *fileindex;
4103 got_worktree_checkout_cb progress_cb;
4104 void *progress_arg;
4105 struct got_repository *repo;
4108 static const struct got_error *
4109 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4110 const char *relpath, struct got_object_id *blob_id,
4111 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4112 int dirfd, const char *de_name)
4114 struct schedule_addition_args *a = arg;
4115 const struct got_error *err = NULL;
4116 struct got_fileindex_entry *ie;
4117 struct stat sb;
4118 char *ondisk_path;
4120 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4121 relpath) == -1)
4122 return got_error_from_errno("asprintf");
4124 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4125 if (ie) {
4126 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4127 de_name, a->repo);
4128 if (err)
4129 goto done;
4130 /* Re-adding an existing entry is a no-op. */
4131 if (status == GOT_STATUS_ADD)
4132 goto done;
4133 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4134 if (err)
4135 goto done;
4138 if (status != GOT_STATUS_UNVERSIONED) {
4139 if (status == GOT_STATUS_NONEXISTENT)
4140 err = got_error_set_errno(ENOENT, ondisk_path);
4141 else
4142 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4143 goto done;
4146 err = got_fileindex_entry_alloc(&ie, relpath);
4147 if (err)
4148 goto done;
4149 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4150 relpath, NULL, NULL, 1);
4151 if (err) {
4152 got_fileindex_entry_free(ie);
4153 goto done;
4155 err = got_fileindex_entry_add(a->fileindex, ie);
4156 if (err) {
4157 got_fileindex_entry_free(ie);
4158 goto done;
4160 done:
4161 free(ondisk_path);
4162 if (err)
4163 return err;
4164 if (status == GOT_STATUS_ADD)
4165 return NULL;
4166 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4169 const struct got_error *
4170 got_worktree_schedule_add(struct got_worktree *worktree,
4171 struct got_pathlist_head *paths,
4172 got_worktree_checkout_cb progress_cb, void *progress_arg,
4173 struct got_repository *repo, int no_ignores)
4175 struct got_fileindex *fileindex = NULL;
4176 char *fileindex_path = NULL;
4177 const struct got_error *err = NULL, *sync_err, *unlockerr;
4178 struct got_pathlist_entry *pe;
4179 struct schedule_addition_args saa;
4181 err = lock_worktree(worktree, LOCK_EX);
4182 if (err)
4183 return err;
4185 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4186 if (err)
4187 goto done;
4189 saa.worktree = worktree;
4190 saa.fileindex = fileindex;
4191 saa.progress_cb = progress_cb;
4192 saa.progress_arg = progress_arg;
4193 saa.repo = repo;
4195 TAILQ_FOREACH(pe, paths, entry) {
4196 err = worktree_status(worktree, pe->path, fileindex, repo,
4197 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4198 if (err)
4199 break;
4201 sync_err = sync_fileindex(fileindex, fileindex_path);
4202 if (sync_err && err == NULL)
4203 err = sync_err;
4204 done:
4205 free(fileindex_path);
4206 if (fileindex)
4207 got_fileindex_free(fileindex);
4208 unlockerr = lock_worktree(worktree, LOCK_SH);
4209 if (unlockerr && err == NULL)
4210 err = unlockerr;
4211 return err;
4214 struct schedule_deletion_args {
4215 struct got_worktree *worktree;
4216 struct got_fileindex *fileindex;
4217 got_worktree_delete_cb progress_cb;
4218 void *progress_arg;
4219 struct got_repository *repo;
4220 int delete_local_mods;
4221 int keep_on_disk;
4222 int ignore_missing_paths;
4223 const char *status_codes;
4226 static const struct got_error *
4227 schedule_for_deletion(void *arg, unsigned char status,
4228 unsigned char staged_status, const char *relpath,
4229 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4230 struct got_object_id *commit_id, int dirfd, const char *de_name)
4232 struct schedule_deletion_args *a = arg;
4233 const struct got_error *err = NULL;
4234 struct got_fileindex_entry *ie = NULL;
4235 struct stat sb;
4236 char *ondisk_path;
4238 if (status == GOT_STATUS_NONEXISTENT) {
4239 if (a->ignore_missing_paths)
4240 return NULL;
4241 return got_error_set_errno(ENOENT, relpath);
4244 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4245 if (ie == NULL)
4246 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4248 staged_status = get_staged_status(ie);
4249 if (staged_status != GOT_STATUS_NO_CHANGE) {
4250 if (staged_status == GOT_STATUS_DELETE)
4251 return NULL;
4252 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4255 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4256 relpath) == -1)
4257 return got_error_from_errno("asprintf");
4259 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4260 a->repo);
4261 if (err)
4262 goto done;
4264 if (a->status_codes) {
4265 size_t ncodes = strlen(a->status_codes);
4266 int i;
4267 for (i = 0; i < ncodes ; i++) {
4268 if (status == a->status_codes[i])
4269 break;
4271 if (i == ncodes) {
4272 /* Do not delete files in non-matching status. */
4273 free(ondisk_path);
4274 return NULL;
4276 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4277 a->status_codes[i] != GOT_STATUS_MISSING) {
4278 static char msg[64];
4279 snprintf(msg, sizeof(msg),
4280 "invalid status code '%c'", a->status_codes[i]);
4281 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4282 goto done;
4286 if (status != GOT_STATUS_NO_CHANGE) {
4287 if (status == GOT_STATUS_DELETE)
4288 goto done;
4289 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4290 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4291 goto done;
4293 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4294 err = got_error_set_errno(ENOENT, relpath);
4295 goto done;
4297 if (status != GOT_STATUS_MODIFY &&
4298 status != GOT_STATUS_MISSING) {
4299 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4300 goto done;
4304 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4305 size_t root_len;
4307 if (dirfd != -1) {
4308 if (unlinkat(dirfd, de_name, 0) == -1) {
4309 err = got_error_from_errno2("unlinkat",
4310 ondisk_path);
4311 goto done;
4313 } else if (unlink(ondisk_path) == -1) {
4314 err = got_error_from_errno2("unlink", ondisk_path);
4315 goto done;
4318 root_len = strlen(a->worktree->root_path);
4319 do {
4320 char *parent;
4321 err = got_path_dirname(&parent, ondisk_path);
4322 if (err)
4323 goto done;
4324 free(ondisk_path);
4325 ondisk_path = parent;
4326 if (rmdir(ondisk_path) == -1) {
4327 if (errno != ENOTEMPTY)
4328 err = got_error_from_errno2("rmdir",
4329 ondisk_path);
4330 break;
4332 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4333 strlen(ondisk_path), root_len) != 0);
4336 got_fileindex_entry_mark_deleted_from_disk(ie);
4337 done:
4338 free(ondisk_path);
4339 if (err)
4340 return err;
4341 if (status == GOT_STATUS_DELETE)
4342 return NULL;
4343 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4344 staged_status, relpath);
4347 const struct got_error *
4348 got_worktree_schedule_delete(struct got_worktree *worktree,
4349 struct got_pathlist_head *paths, int delete_local_mods,
4350 const char *status_codes,
4351 got_worktree_delete_cb progress_cb, void *progress_arg,
4352 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4354 struct got_fileindex *fileindex = NULL;
4355 char *fileindex_path = NULL;
4356 const struct got_error *err = NULL, *sync_err, *unlockerr;
4357 struct got_pathlist_entry *pe;
4358 struct schedule_deletion_args sda;
4360 err = lock_worktree(worktree, LOCK_EX);
4361 if (err)
4362 return err;
4364 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4365 if (err)
4366 goto done;
4368 sda.worktree = worktree;
4369 sda.fileindex = fileindex;
4370 sda.progress_cb = progress_cb;
4371 sda.progress_arg = progress_arg;
4372 sda.repo = repo;
4373 sda.delete_local_mods = delete_local_mods;
4374 sda.keep_on_disk = keep_on_disk;
4375 sda.ignore_missing_paths = ignore_missing_paths;
4376 sda.status_codes = status_codes;
4378 TAILQ_FOREACH(pe, paths, entry) {
4379 err = worktree_status(worktree, pe->path, fileindex, repo,
4380 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4381 if (err)
4382 break;
4384 sync_err = sync_fileindex(fileindex, fileindex_path);
4385 if (sync_err && err == NULL)
4386 err = sync_err;
4387 done:
4388 free(fileindex_path);
4389 if (fileindex)
4390 got_fileindex_free(fileindex);
4391 unlockerr = lock_worktree(worktree, LOCK_SH);
4392 if (unlockerr && err == NULL)
4393 err = unlockerr;
4394 return err;
4397 static const struct got_error *
4398 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4400 const struct got_error *err = NULL;
4401 char *line = NULL;
4402 size_t linesize = 0, n;
4403 ssize_t linelen;
4405 linelen = getline(&line, &linesize, infile);
4406 if (linelen == -1) {
4407 if (ferror(infile)) {
4408 err = got_error_from_errno("getline");
4409 goto done;
4411 return NULL;
4413 if (outfile) {
4414 n = fwrite(line, 1, linelen, outfile);
4415 if (n != linelen) {
4416 err = got_ferror(outfile, GOT_ERR_IO);
4417 goto done;
4420 if (rejectfile) {
4421 n = fwrite(line, 1, linelen, rejectfile);
4422 if (n != linelen)
4423 err = got_ferror(rejectfile, GOT_ERR_IO);
4425 done:
4426 free(line);
4427 return err;
4430 static const struct got_error *
4431 skip_one_line(FILE *f)
4433 char *line = NULL;
4434 size_t linesize = 0;
4435 ssize_t linelen;
4437 linelen = getline(&line, &linesize, f);
4438 if (linelen == -1) {
4439 if (ferror(f))
4440 return got_error_from_errno("getline");
4441 return NULL;
4443 free(line);
4444 return NULL;
4447 static const struct got_error *
4448 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4449 int start_old, int end_old, int start_new, int end_new,
4450 FILE *outfile, FILE *rejectfile)
4452 const struct got_error *err;
4454 /* Copy old file's lines leading up to patch. */
4455 while (!feof(f1) && *line_cur1 < start_old) {
4456 err = copy_one_line(f1, outfile, NULL);
4457 if (err)
4458 return err;
4459 (*line_cur1)++;
4461 /* Skip new file's lines leading up to patch. */
4462 while (!feof(f2) && *line_cur2 < start_new) {
4463 if (rejectfile)
4464 err = copy_one_line(f2, NULL, rejectfile);
4465 else
4466 err = skip_one_line(f2);
4467 if (err)
4468 return err;
4469 (*line_cur2)++;
4471 /* Copy patched lines. */
4472 while (!feof(f2) && *line_cur2 <= end_new) {
4473 err = copy_one_line(f2, outfile, NULL);
4474 if (err)
4475 return err;
4476 (*line_cur2)++;
4478 /* Skip over old file's replaced lines. */
4479 while (!feof(f1) && *line_cur1 <= end_old) {
4480 if (rejectfile)
4481 err = copy_one_line(f1, NULL, rejectfile);
4482 else
4483 err = skip_one_line(f1);
4484 if (err)
4485 return err;
4486 (*line_cur1)++;
4489 return NULL;
4492 static const struct got_error *
4493 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4494 FILE *outfile, FILE *rejectfile)
4496 const struct got_error *err;
4498 if (outfile) {
4499 /* Copy old file's lines until EOF. */
4500 while (!feof(f1)) {
4501 err = copy_one_line(f1, outfile, NULL);
4502 if (err)
4503 return err;
4504 (*line_cur1)++;
4507 if (rejectfile) {
4508 /* Copy new file's lines until EOF. */
4509 while (!feof(f2)) {
4510 err = copy_one_line(f2, NULL, rejectfile);
4511 if (err)
4512 return err;
4513 (*line_cur2)++;
4517 return NULL;
4520 static const struct got_error *
4521 apply_or_reject_change(int *choice, int *nchunks_used,
4522 struct diff_result *diff_result, int n,
4523 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4524 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4525 got_worktree_patch_cb patch_cb, void *patch_arg)
4527 const struct got_error *err = NULL;
4528 struct diff_chunk_context cc = {};
4529 int start_old, end_old, start_new, end_new;
4530 FILE *hunkfile;
4531 struct diff_output_unidiff_state *diff_state;
4532 struct diff_input_info diff_info;
4533 int rc;
4535 *choice = GOT_PATCH_CHOICE_NONE;
4537 /* Get changed line numbers without context lines for copy_change(). */
4538 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4539 start_old = cc.left.start;
4540 end_old = cc.left.end;
4541 start_new = cc.right.start;
4542 end_new = cc.right.end;
4544 /* Get the same change with context lines for display. */
4545 memset(&cc, 0, sizeof(cc));
4546 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4548 memset(&diff_info, 0, sizeof(diff_info));
4549 diff_info.left_path = relpath;
4550 diff_info.right_path = relpath;
4552 diff_state = diff_output_unidiff_state_alloc();
4553 if (diff_state == NULL)
4554 return got_error_set_errno(ENOMEM,
4555 "diff_output_unidiff_state_alloc");
4557 hunkfile = got_opentemp();
4558 if (hunkfile == NULL) {
4559 err = got_error_from_errno("got_opentemp");
4560 goto done;
4563 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4564 diff_result, &cc);
4565 if (rc != DIFF_RC_OK) {
4566 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4567 goto done;
4570 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4571 err = got_ferror(hunkfile, GOT_ERR_IO);
4572 goto done;
4575 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4576 hunkfile, changeno, nchanges);
4577 if (err)
4578 goto done;
4580 switch (*choice) {
4581 case GOT_PATCH_CHOICE_YES:
4582 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4583 end_old, start_new, end_new, outfile, rejectfile);
4584 break;
4585 case GOT_PATCH_CHOICE_NO:
4586 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4587 end_old, start_new, end_new, rejectfile, outfile);
4588 break;
4589 case GOT_PATCH_CHOICE_QUIT:
4590 break;
4591 default:
4592 err = got_error(GOT_ERR_PATCH_CHOICE);
4593 break;
4595 done:
4596 diff_output_unidiff_state_free(diff_state);
4597 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4598 err = got_error_from_errno("fclose");
4599 return err;
4602 struct revert_file_args {
4603 struct got_worktree *worktree;
4604 struct got_fileindex *fileindex;
4605 got_worktree_checkout_cb progress_cb;
4606 void *progress_arg;
4607 got_worktree_patch_cb patch_cb;
4608 void *patch_arg;
4609 struct got_repository *repo;
4610 int unlink_added_files;
4613 static const struct got_error *
4614 create_patched_content(char **path_outfile, int reverse_patch,
4615 struct got_object_id *blob_id, const char *path2,
4616 int dirfd2, const char *de_name2,
4617 const char *relpath, struct got_repository *repo,
4618 got_worktree_patch_cb patch_cb, void *patch_arg)
4620 const struct got_error *err, *free_err;
4621 struct got_blob_object *blob = NULL;
4622 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4623 int fd = -1, fd2 = -1;
4624 char link_target[PATH_MAX];
4625 ssize_t link_len = 0;
4626 char *path1 = NULL, *id_str = NULL;
4627 struct stat sb2;
4628 struct got_diffreg_result *diffreg_result = NULL;
4629 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4630 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4632 *path_outfile = NULL;
4634 err = got_object_id_str(&id_str, blob_id);
4635 if (err)
4636 return err;
4638 if (dirfd2 != -1) {
4639 fd2 = openat(dirfd2, de_name2,
4640 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4641 if (fd2 == -1) {
4642 if (!got_err_open_nofollow_on_symlink()) {
4643 err = got_error_from_errno2("openat", path2);
4644 goto done;
4646 link_len = readlinkat(dirfd2, de_name2,
4647 link_target, sizeof(link_target));
4648 if (link_len == -1) {
4649 return got_error_from_errno2("readlinkat",
4650 path2);
4652 sb2.st_mode = S_IFLNK;
4653 sb2.st_size = link_len;
4655 } else {
4656 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4657 if (fd2 == -1) {
4658 if (!got_err_open_nofollow_on_symlink()) {
4659 err = got_error_from_errno2("open", path2);
4660 goto done;
4662 link_len = readlink(path2, link_target,
4663 sizeof(link_target));
4664 if (link_len == -1)
4665 return got_error_from_errno2("readlink", path2);
4666 sb2.st_mode = S_IFLNK;
4667 sb2.st_size = link_len;
4670 if (fd2 != -1) {
4671 if (fstat(fd2, &sb2) == -1) {
4672 err = got_error_from_errno2("fstat", path2);
4673 goto done;
4676 f2 = fdopen(fd2, "r");
4677 if (f2 == NULL) {
4678 err = got_error_from_errno2("fdopen", path2);
4679 goto done;
4681 fd2 = -1;
4682 } else {
4683 size_t n;
4684 f2 = got_opentemp();
4685 if (f2 == NULL) {
4686 err = got_error_from_errno2("got_opentemp", path2);
4687 goto done;
4689 n = fwrite(link_target, 1, link_len, f2);
4690 if (n != link_len) {
4691 err = got_ferror(f2, GOT_ERR_IO);
4692 goto done;
4694 if (fflush(f2) == EOF) {
4695 err = got_error_from_errno("fflush");
4696 goto done;
4698 rewind(f2);
4701 fd = got_opentempfd();
4702 if (fd == -1) {
4703 err = got_error_from_errno("got_opentempfd");
4704 goto done;
4707 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4708 if (err)
4709 goto done;
4711 err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4712 if (err)
4713 goto done;
4715 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4716 if (err)
4717 goto done;
4719 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4720 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4721 if (err)
4722 goto done;
4724 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4725 "");
4726 if (err)
4727 goto done;
4729 if (fseek(f1, 0L, SEEK_SET) == -1)
4730 return got_ferror(f1, GOT_ERR_IO);
4731 if (fseek(f2, 0L, SEEK_SET) == -1)
4732 return got_ferror(f2, GOT_ERR_IO);
4734 /* Count the number of actual changes in the diff result. */
4735 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4736 struct diff_chunk_context cc = {};
4737 diff_chunk_context_load_change(&cc, &nchunks_used,
4738 diffreg_result->result, n, 0);
4739 nchanges++;
4741 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4742 int choice;
4743 err = apply_or_reject_change(&choice, &nchunks_used,
4744 diffreg_result->result, n, relpath, f1, f2,
4745 &line_cur1, &line_cur2,
4746 reverse_patch ? NULL : outfile,
4747 reverse_patch ? outfile : NULL,
4748 ++i, nchanges, patch_cb, patch_arg);
4749 if (err)
4750 goto done;
4751 if (choice == GOT_PATCH_CHOICE_YES)
4752 have_content = 1;
4753 else if (choice == GOT_PATCH_CHOICE_QUIT)
4754 break;
4756 if (have_content) {
4757 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4758 reverse_patch ? NULL : outfile,
4759 reverse_patch ? outfile : NULL);
4760 if (err)
4761 goto done;
4763 if (!S_ISLNK(sb2.st_mode)) {
4764 mode_t mode;
4766 mode = apply_umask(sb2.st_mode);
4767 if (fchmod(fileno(outfile), mode) == -1) {
4768 err = got_error_from_errno2("fchmod", path2);
4769 goto done;
4773 done:
4774 free(id_str);
4775 if (fd != -1 && close(fd) == -1 && err == NULL)
4776 err = got_error_from_errno("close");
4777 if (blob)
4778 got_object_blob_close(blob);
4779 free_err = got_diffreg_result_free(diffreg_result);
4780 if (err == NULL)
4781 err = free_err;
4782 if (f1 && fclose(f1) == EOF && err == NULL)
4783 err = got_error_from_errno2("fclose", path1);
4784 if (f2 && fclose(f2) == EOF && err == NULL)
4785 err = got_error_from_errno2("fclose", path2);
4786 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4787 err = got_error_from_errno2("close", path2);
4788 if (outfile && fclose(outfile) == EOF && err == NULL)
4789 err = got_error_from_errno2("fclose", *path_outfile);
4790 if (path1 && unlink(path1) == -1 && err == NULL)
4791 err = got_error_from_errno2("unlink", path1);
4792 if (err || !have_content) {
4793 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4794 err = got_error_from_errno2("unlink", *path_outfile);
4795 free(*path_outfile);
4796 *path_outfile = NULL;
4798 free(path1);
4799 return err;
4802 static const struct got_error *
4803 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4804 const char *relpath, struct got_object_id *blob_id,
4805 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4806 int dirfd, const char *de_name)
4808 struct revert_file_args *a = arg;
4809 const struct got_error *err = NULL;
4810 char *parent_path = NULL;
4811 struct got_fileindex_entry *ie;
4812 struct got_commit_object *base_commit = NULL;
4813 struct got_tree_object *tree = NULL;
4814 struct got_object_id *tree_id = NULL;
4815 const struct got_tree_entry *te = NULL;
4816 char *tree_path = NULL, *te_name;
4817 char *ondisk_path = NULL, *path_content = NULL;
4818 struct got_blob_object *blob = NULL;
4819 int fd = -1;
4821 /* Reverting a staged deletion is a no-op. */
4822 if (status == GOT_STATUS_DELETE &&
4823 staged_status != GOT_STATUS_NO_CHANGE)
4824 return NULL;
4826 if (status == GOT_STATUS_UNVERSIONED)
4827 return (*a->progress_cb)(a->progress_arg,
4828 GOT_STATUS_UNVERSIONED, relpath);
4830 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4831 if (ie == NULL)
4832 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4834 /* Construct in-repository path of tree which contains this blob. */
4835 err = got_path_dirname(&parent_path, ie->path);
4836 if (err) {
4837 if (err->code != GOT_ERR_BAD_PATH)
4838 goto done;
4839 parent_path = strdup("/");
4840 if (parent_path == NULL) {
4841 err = got_error_from_errno("strdup");
4842 goto done;
4845 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4846 tree_path = strdup(parent_path);
4847 if (tree_path == NULL) {
4848 err = got_error_from_errno("strdup");
4849 goto done;
4851 } else {
4852 if (got_path_is_root_dir(parent_path)) {
4853 tree_path = strdup(a->worktree->path_prefix);
4854 if (tree_path == NULL) {
4855 err = got_error_from_errno("strdup");
4856 goto done;
4858 } else {
4859 if (asprintf(&tree_path, "%s/%s",
4860 a->worktree->path_prefix, parent_path) == -1) {
4861 err = got_error_from_errno("asprintf");
4862 goto done;
4867 err = got_object_open_as_commit(&base_commit, a->repo,
4868 a->worktree->base_commit_id);
4869 if (err)
4870 goto done;
4872 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4873 if (err) {
4874 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4875 (status == GOT_STATUS_ADD ||
4876 staged_status == GOT_STATUS_ADD)))
4877 goto done;
4878 } else {
4879 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4880 if (err)
4881 goto done;
4883 err = got_path_basename(&te_name, ie->path);
4884 if (err)
4885 goto done;
4887 te = got_object_tree_find_entry(tree, te_name);
4888 free(te_name);
4889 if (te == NULL && status != GOT_STATUS_ADD &&
4890 staged_status != GOT_STATUS_ADD) {
4891 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4892 goto done;
4896 switch (status) {
4897 case GOT_STATUS_ADD:
4898 if (a->patch_cb) {
4899 int choice = GOT_PATCH_CHOICE_NONE;
4900 err = (*a->patch_cb)(&choice, a->patch_arg,
4901 status, ie->path, NULL, 1, 1);
4902 if (err)
4903 goto done;
4904 if (choice != GOT_PATCH_CHOICE_YES)
4905 break;
4907 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4908 ie->path);
4909 if (err)
4910 goto done;
4911 got_fileindex_entry_remove(a->fileindex, ie);
4912 if (a->unlink_added_files) {
4913 if (asprintf(&ondisk_path, "%s/%s",
4914 got_worktree_get_root_path(a->worktree),
4915 relpath) == -1) {
4916 err = got_error_from_errno("asprintf");
4917 goto done;
4919 if (unlink(ondisk_path) == -1) {
4920 err = got_error_from_errno2("unlink",
4921 ondisk_path);
4922 break;
4925 break;
4926 case GOT_STATUS_DELETE:
4927 if (a->patch_cb) {
4928 int choice = GOT_PATCH_CHOICE_NONE;
4929 err = (*a->patch_cb)(&choice, a->patch_arg,
4930 status, ie->path, NULL, 1, 1);
4931 if (err)
4932 goto done;
4933 if (choice != GOT_PATCH_CHOICE_YES)
4934 break;
4936 /* fall through */
4937 case GOT_STATUS_MODIFY:
4938 case GOT_STATUS_MODE_CHANGE:
4939 case GOT_STATUS_CONFLICT:
4940 case GOT_STATUS_MISSING: {
4941 struct got_object_id id;
4942 if (staged_status == GOT_STATUS_ADD ||
4943 staged_status == GOT_STATUS_MODIFY)
4944 got_fileindex_entry_get_staged_blob_id(&id, ie);
4945 else
4946 got_fileindex_entry_get_blob_id(&id, ie);
4947 fd = got_opentempfd();
4948 if (fd == -1) {
4949 err = got_error_from_errno("got_opentempfd");
4950 goto done;
4953 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4954 if (err)
4955 goto done;
4957 if (asprintf(&ondisk_path, "%s/%s",
4958 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4959 err = got_error_from_errno("asprintf");
4960 goto done;
4963 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4964 status == GOT_STATUS_CONFLICT)) {
4965 int is_bad_symlink = 0;
4966 err = create_patched_content(&path_content, 1, &id,
4967 ondisk_path, dirfd, de_name, ie->path, a->repo,
4968 a->patch_cb, a->patch_arg);
4969 if (err || path_content == NULL)
4970 break;
4971 if (te && S_ISLNK(te->mode)) {
4972 if (unlink(path_content) == -1) {
4973 err = got_error_from_errno2("unlink",
4974 path_content);
4975 break;
4977 err = install_symlink(&is_bad_symlink,
4978 a->worktree, ondisk_path, ie->path,
4979 blob, 0, 1, 0, 0, a->repo,
4980 a->progress_cb, a->progress_arg);
4981 } else {
4982 if (rename(path_content, ondisk_path) == -1) {
4983 err = got_error_from_errno3("rename",
4984 path_content, ondisk_path);
4985 goto done;
4988 } else {
4989 int is_bad_symlink = 0;
4990 if (te && S_ISLNK(te->mode)) {
4991 err = install_symlink(&is_bad_symlink,
4992 a->worktree, ondisk_path, ie->path,
4993 blob, 0, 1, 0, 0, a->repo,
4994 a->progress_cb, a->progress_arg);
4995 } else {
4996 err = install_blob(a->worktree, ondisk_path,
4997 ie->path,
4998 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4999 got_fileindex_perms_to_st(ie), blob,
5000 0, 1, 0, 0, a->repo,
5001 a->progress_cb, a->progress_arg);
5003 if (err)
5004 goto done;
5005 if (status == GOT_STATUS_DELETE ||
5006 status == GOT_STATUS_MODE_CHANGE) {
5007 err = got_fileindex_entry_update(ie,
5008 a->worktree->root_fd, relpath,
5009 blob->id.sha1,
5010 a->worktree->base_commit_id->sha1, 1);
5011 if (err)
5012 goto done;
5014 if (is_bad_symlink) {
5015 got_fileindex_entry_filetype_set(ie,
5016 GOT_FILEIDX_MODE_BAD_SYMLINK);
5019 break;
5021 default:
5022 break;
5024 done:
5025 free(ondisk_path);
5026 free(path_content);
5027 free(parent_path);
5028 free(tree_path);
5029 if (fd != -1 && close(fd) == -1 && err == NULL)
5030 err = got_error_from_errno("close");
5031 if (blob)
5032 got_object_blob_close(blob);
5033 if (tree)
5034 got_object_tree_close(tree);
5035 free(tree_id);
5036 if (base_commit)
5037 got_object_commit_close(base_commit);
5038 return err;
5041 const struct got_error *
5042 got_worktree_revert(struct got_worktree *worktree,
5043 struct got_pathlist_head *paths,
5044 got_worktree_checkout_cb progress_cb, void *progress_arg,
5045 got_worktree_patch_cb patch_cb, void *patch_arg,
5046 struct got_repository *repo)
5048 struct got_fileindex *fileindex = NULL;
5049 char *fileindex_path = NULL;
5050 const struct got_error *err = NULL, *unlockerr = NULL;
5051 const struct got_error *sync_err = NULL;
5052 struct got_pathlist_entry *pe;
5053 struct revert_file_args rfa;
5055 err = lock_worktree(worktree, LOCK_EX);
5056 if (err)
5057 return err;
5059 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5060 if (err)
5061 goto done;
5063 rfa.worktree = worktree;
5064 rfa.fileindex = fileindex;
5065 rfa.progress_cb = progress_cb;
5066 rfa.progress_arg = progress_arg;
5067 rfa.patch_cb = patch_cb;
5068 rfa.patch_arg = patch_arg;
5069 rfa.repo = repo;
5070 rfa.unlink_added_files = 0;
5071 TAILQ_FOREACH(pe, paths, entry) {
5072 err = worktree_status(worktree, pe->path, fileindex, repo,
5073 revert_file, &rfa, NULL, NULL, 1, 0);
5074 if (err)
5075 break;
5077 sync_err = sync_fileindex(fileindex, fileindex_path);
5078 if (sync_err && err == NULL)
5079 err = sync_err;
5080 done:
5081 free(fileindex_path);
5082 if (fileindex)
5083 got_fileindex_free(fileindex);
5084 unlockerr = lock_worktree(worktree, LOCK_SH);
5085 if (unlockerr && err == NULL)
5086 err = unlockerr;
5087 return err;
5090 static void
5091 free_commitable(struct got_commitable *ct)
5093 free(ct->path);
5094 free(ct->in_repo_path);
5095 free(ct->ondisk_path);
5096 free(ct->blob_id);
5097 free(ct->base_blob_id);
5098 free(ct->staged_blob_id);
5099 free(ct->base_commit_id);
5100 free(ct);
5103 struct collect_commitables_arg {
5104 struct got_pathlist_head *commitable_paths;
5105 struct got_repository *repo;
5106 struct got_worktree *worktree;
5107 struct got_fileindex *fileindex;
5108 int have_staged_files;
5109 int allow_bad_symlinks;
5110 int diff_header_shown;
5111 int commit_conflicts;
5112 FILE *diff_outfile;
5113 FILE *f1;
5114 FILE *f2;
5118 * Create a file which contains the target path of a symlink so we can feed
5119 * it as content to the diff engine.
5121 static const struct got_error *
5122 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5123 const char *abspath)
5125 const struct got_error *err = NULL;
5126 char target_path[PATH_MAX];
5127 ssize_t target_len, outlen;
5129 *fd = -1;
5131 if (dirfd != -1) {
5132 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5133 if (target_len == -1)
5134 return got_error_from_errno2("readlinkat", abspath);
5135 } else {
5136 target_len = readlink(abspath, target_path, PATH_MAX);
5137 if (target_len == -1)
5138 return got_error_from_errno2("readlink", abspath);
5141 *fd = got_opentempfd();
5142 if (*fd == -1)
5143 return got_error_from_errno("got_opentempfd");
5145 outlen = write(*fd, target_path, target_len);
5146 if (outlen == -1) {
5147 err = got_error_from_errno("got_opentempfd");
5148 goto done;
5151 if (lseek(*fd, 0, SEEK_SET) == -1) {
5152 err = got_error_from_errno2("lseek", abspath);
5153 goto done;
5155 done:
5156 if (err) {
5157 close(*fd);
5158 *fd = -1;
5160 return err;
5163 static const struct got_error *
5164 append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5165 FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5166 int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5168 const struct got_error *err = NULL;
5169 struct got_blob_object *blob1 = NULL;
5170 int fd = -1, fd1 = -1, fd2 = -1;
5171 FILE *ondisk_file = NULL;
5172 char *label1 = NULL;
5173 struct stat sb;
5174 off_t size1 = 0;
5175 int f2_exists = 0;
5176 char *id_str = NULL;
5178 memset(&sb, 0, sizeof(sb));
5180 if (diff_staged) {
5181 if (ct->staged_status != GOT_STATUS_MODIFY &&
5182 ct->staged_status != GOT_STATUS_ADD &&
5183 ct->staged_status != GOT_STATUS_DELETE)
5184 return NULL;
5185 } else {
5186 if (ct->status != GOT_STATUS_MODIFY &&
5187 ct->status != GOT_STATUS_ADD &&
5188 ct->status != GOT_STATUS_DELETE &&
5189 ct->status != GOT_STATUS_CONFLICT)
5190 return NULL;
5193 err = got_opentemp_truncate(f1);
5194 if (err)
5195 return got_error_from_errno("got_opentemp_truncate");
5196 err = got_opentemp_truncate(f2);
5197 if (err)
5198 return got_error_from_errno("got_opentemp_truncate");
5200 if (!*diff_header_shown) {
5201 err = got_object_id_str(&id_str, worktree->base_commit_id);
5202 if (err)
5203 return err;
5204 fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5205 got_worktree_get_root_path(worktree));
5206 fprintf(diff_outfile, "commit - %s\n", id_str);
5207 fprintf(diff_outfile, "path + %s%s\n",
5208 got_worktree_get_root_path(worktree),
5209 diff_staged ? " (staged changes)" : "");
5210 *diff_header_shown = 1;
5213 if (diff_staged) {
5214 const char *label1 = NULL, *label2 = NULL;
5215 switch (ct->staged_status) {
5216 case GOT_STATUS_MODIFY:
5217 label1 = ct->path;
5218 label2 = ct->path;
5219 break;
5220 case GOT_STATUS_ADD:
5221 label2 = ct->path;
5222 break;
5223 case GOT_STATUS_DELETE:
5224 label1 = ct->path;
5225 break;
5226 default:
5227 return got_error(GOT_ERR_FILE_STATUS);
5229 fd1 = got_opentempfd();
5230 if (fd1 == -1) {
5231 err = got_error_from_errno("got_opentempfd");
5232 goto done;
5234 fd2 = got_opentempfd();
5235 if (fd2 == -1) {
5236 err = got_error_from_errno("got_opentempfd");
5237 goto done;
5239 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5240 fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5241 label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5242 NULL, repo, diff_outfile);
5243 goto done;
5246 fd1 = got_opentempfd();
5247 if (fd1 == -1) {
5248 err = got_error_from_errno("got_opentempfd");
5249 goto done;
5252 if (ct->status != GOT_STATUS_ADD) {
5253 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5254 8192, fd1);
5255 if (err)
5256 goto done;
5259 if (ct->status != GOT_STATUS_DELETE) {
5260 if (dirfd != -1) {
5261 fd = openat(dirfd, de_name,
5262 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5263 if (fd == -1) {
5264 if (!got_err_open_nofollow_on_symlink()) {
5265 err = got_error_from_errno2("openat",
5266 ct->ondisk_path);
5267 goto done;
5269 err = get_symlink_target_file(&fd, dirfd,
5270 de_name, ct->ondisk_path);
5271 if (err)
5272 goto done;
5274 } else {
5275 fd = open(ct->ondisk_path,
5276 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5277 if (fd == -1) {
5278 if (!got_err_open_nofollow_on_symlink()) {
5279 err = got_error_from_errno2("open",
5280 ct->ondisk_path);
5281 goto done;
5283 err = get_symlink_target_file(&fd, dirfd,
5284 de_name, ct->ondisk_path);
5285 if (err)
5286 goto done;
5289 if (fstatat(fd, ct->ondisk_path, &sb,
5290 AT_SYMLINK_NOFOLLOW) == -1) {
5291 err = got_error_from_errno2("fstatat", ct->ondisk_path);
5292 goto done;
5294 ondisk_file = fdopen(fd, "r");
5295 if (ondisk_file == NULL) {
5296 err = got_error_from_errno2("fdopen", ct->ondisk_path);
5297 goto done;
5299 fd = -1;
5300 f2_exists = 1;
5303 if (blob1) {
5304 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5305 f1, blob1);
5306 if (err)
5307 goto done;
5310 err = got_diff_blob_file(blob1, f1, size1, label1,
5311 ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5312 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5313 done:
5314 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5315 err = got_error_from_errno("close");
5316 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5317 err = got_error_from_errno("close");
5318 if (blob1)
5319 got_object_blob_close(blob1);
5320 if (fd != -1 && close(fd) == -1 && err == NULL)
5321 err = got_error_from_errno("close");
5322 if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5323 err = got_error_from_errno("fclose");
5324 return err;
5327 static const struct got_error *
5328 collect_commitables(void *arg, unsigned char status,
5329 unsigned char staged_status, const char *relpath,
5330 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5331 struct got_object_id *commit_id, int dirfd, const char *de_name)
5333 struct collect_commitables_arg *a = arg;
5334 const struct got_error *err = NULL;
5335 struct got_commitable *ct = NULL;
5336 struct got_pathlist_entry *new = NULL;
5337 char *parent_path = NULL, *path = NULL;
5338 struct stat sb;
5340 if (a->have_staged_files) {
5341 if (staged_status != GOT_STATUS_MODIFY &&
5342 staged_status != GOT_STATUS_ADD &&
5343 staged_status != GOT_STATUS_DELETE)
5344 return NULL;
5345 } else {
5346 if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5347 printf("C %s\n", relpath);
5348 return got_error(GOT_ERR_COMMIT_CONFLICT);
5351 if (status != GOT_STATUS_MODIFY &&
5352 status != GOT_STATUS_MODE_CHANGE &&
5353 status != GOT_STATUS_ADD &&
5354 status != GOT_STATUS_DELETE &&
5355 status != GOT_STATUS_CONFLICT)
5356 return NULL;
5359 if (asprintf(&path, "/%s", relpath) == -1) {
5360 err = got_error_from_errno("asprintf");
5361 goto done;
5363 if (strcmp(path, "/") == 0) {
5364 parent_path = strdup("");
5365 if (parent_path == NULL)
5366 return got_error_from_errno("strdup");
5367 } else {
5368 err = got_path_dirname(&parent_path, path);
5369 if (err)
5370 return err;
5373 ct = calloc(1, sizeof(*ct));
5374 if (ct == NULL) {
5375 err = got_error_from_errno("calloc");
5376 goto done;
5379 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5380 relpath) == -1) {
5381 err = got_error_from_errno("asprintf");
5382 goto done;
5385 if (staged_status == GOT_STATUS_ADD ||
5386 staged_status == GOT_STATUS_MODIFY) {
5387 struct got_fileindex_entry *ie;
5388 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5389 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5390 case GOT_FILEIDX_MODE_REGULAR_FILE:
5391 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5392 ct->mode = S_IFREG;
5393 break;
5394 case GOT_FILEIDX_MODE_SYMLINK:
5395 ct->mode = S_IFLNK;
5396 break;
5397 default:
5398 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5399 goto done;
5401 ct->mode |= got_fileindex_entry_perms_get(ie);
5402 } else if (status != GOT_STATUS_DELETE &&
5403 staged_status != GOT_STATUS_DELETE) {
5404 if (dirfd != -1) {
5405 if (fstatat(dirfd, de_name, &sb,
5406 AT_SYMLINK_NOFOLLOW) == -1) {
5407 err = got_error_from_errno2("fstatat",
5408 ct->ondisk_path);
5409 goto done;
5411 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5412 err = got_error_from_errno2("lstat", ct->ondisk_path);
5413 goto done;
5415 ct->mode = sb.st_mode;
5418 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5419 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5420 relpath) == -1) {
5421 err = got_error_from_errno("asprintf");
5422 goto done;
5425 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5426 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5427 int is_bad_symlink;
5428 char target_path[PATH_MAX];
5429 ssize_t target_len;
5430 target_len = readlink(ct->ondisk_path, target_path,
5431 sizeof(target_path));
5432 if (target_len == -1) {
5433 err = got_error_from_errno2("readlink",
5434 ct->ondisk_path);
5435 goto done;
5437 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5438 target_len, ct->ondisk_path, a->worktree->root_path);
5439 if (err)
5440 goto done;
5441 if (is_bad_symlink) {
5442 err = got_error_path(ct->ondisk_path,
5443 GOT_ERR_BAD_SYMLINK);
5444 goto done;
5449 ct->status = status;
5450 ct->staged_status = staged_status;
5451 ct->blob_id = NULL; /* will be filled in when blob gets created */
5452 if (ct->status != GOT_STATUS_ADD &&
5453 ct->staged_status != GOT_STATUS_ADD) {
5454 ct->base_blob_id = got_object_id_dup(blob_id);
5455 if (ct->base_blob_id == NULL) {
5456 err = got_error_from_errno("got_object_id_dup");
5457 goto done;
5459 ct->base_commit_id = got_object_id_dup(commit_id);
5460 if (ct->base_commit_id == NULL) {
5461 err = got_error_from_errno("got_object_id_dup");
5462 goto done;
5465 if (ct->staged_status == GOT_STATUS_ADD ||
5466 ct->staged_status == GOT_STATUS_MODIFY) {
5467 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5468 if (ct->staged_blob_id == NULL) {
5469 err = got_error_from_errno("got_object_id_dup");
5470 goto done;
5473 ct->path = strdup(path);
5474 if (ct->path == NULL) {
5475 err = got_error_from_errno("strdup");
5476 goto done;
5478 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5479 if (err)
5480 goto done;
5482 if (a->diff_outfile && ct && new != NULL) {
5483 err = append_ct_diff(ct, &a->diff_header_shown,
5484 a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5485 a->have_staged_files, a->repo, a->worktree);
5486 if (err)
5487 goto done;
5489 done:
5490 if (ct && (err || new == NULL))
5491 free_commitable(ct);
5492 free(parent_path);
5493 free(path);
5494 return err;
5497 static const struct got_error *write_tree(struct got_object_id **, int *,
5498 struct got_tree_object *, const char *, struct got_pathlist_head *,
5499 got_worktree_status_cb status_cb, void *status_arg,
5500 struct got_repository *);
5502 static const struct got_error *
5503 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5504 struct got_tree_entry *te, const char *parent_path,
5505 struct got_pathlist_head *commitable_paths,
5506 got_worktree_status_cb status_cb, void *status_arg,
5507 struct got_repository *repo)
5509 const struct got_error *err = NULL;
5510 struct got_tree_object *subtree;
5511 char *subpath;
5513 if (asprintf(&subpath, "%s%s%s", parent_path,
5514 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5515 return got_error_from_errno("asprintf");
5517 err = got_object_open_as_tree(&subtree, repo, &te->id);
5518 if (err)
5519 return err;
5521 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5522 commitable_paths, status_cb, status_arg, repo);
5523 got_object_tree_close(subtree);
5524 free(subpath);
5525 return err;
5528 static const struct got_error *
5529 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5531 const struct got_error *err = NULL;
5532 char *ct_parent_path = NULL;
5534 *match = 0;
5536 if (strchr(ct->in_repo_path, '/') == NULL) {
5537 *match = got_path_is_root_dir(path);
5538 return NULL;
5541 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5542 if (err)
5543 return err;
5544 *match = (strcmp(path, ct_parent_path) == 0);
5545 free(ct_parent_path);
5546 return err;
5549 static mode_t
5550 get_ct_file_mode(struct got_commitable *ct)
5552 if (S_ISLNK(ct->mode))
5553 return S_IFLNK;
5555 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5558 static const struct got_error *
5559 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5560 struct got_tree_entry *te, struct got_commitable *ct)
5562 const struct got_error *err = NULL;
5564 *new_te = NULL;
5566 err = got_object_tree_entry_dup(new_te, te);
5567 if (err)
5568 goto done;
5570 (*new_te)->mode = get_ct_file_mode(ct);
5572 if (ct->staged_status == GOT_STATUS_MODIFY)
5573 memcpy(&(*new_te)->id, ct->staged_blob_id,
5574 sizeof((*new_te)->id));
5575 else
5576 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5577 done:
5578 if (err && *new_te) {
5579 free(*new_te);
5580 *new_te = NULL;
5582 return err;
5585 static const struct got_error *
5586 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5587 struct got_commitable *ct)
5589 const struct got_error *err = NULL;
5590 char *ct_name = NULL;
5592 *new_te = NULL;
5594 *new_te = calloc(1, sizeof(**new_te));
5595 if (*new_te == NULL)
5596 return got_error_from_errno("calloc");
5598 err = got_path_basename(&ct_name, ct->path);
5599 if (err)
5600 goto done;
5601 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5602 sizeof((*new_te)->name)) {
5603 err = got_error(GOT_ERR_NO_SPACE);
5604 goto done;
5607 (*new_te)->mode = get_ct_file_mode(ct);
5609 if (ct->staged_status == GOT_STATUS_ADD)
5610 memcpy(&(*new_te)->id, ct->staged_blob_id,
5611 sizeof((*new_te)->id));
5612 else
5613 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5614 done:
5615 free(ct_name);
5616 if (err && *new_te) {
5617 free(*new_te);
5618 *new_te = NULL;
5620 return err;
5623 static const struct got_error *
5624 insert_tree_entry(struct got_tree_entry *new_te,
5625 struct got_pathlist_head *paths)
5627 const struct got_error *err = NULL;
5628 struct got_pathlist_entry *new_pe;
5630 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5631 if (err)
5632 return err;
5633 if (new_pe == NULL)
5634 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5635 return NULL;
5638 static const struct got_error *
5639 report_ct_status(struct got_commitable *ct,
5640 got_worktree_status_cb status_cb, void *status_arg)
5642 const char *ct_path = ct->path;
5643 unsigned char status;
5645 if (status_cb == NULL) /* no commit progress output desired */
5646 return NULL;
5648 while (ct_path[0] == '/')
5649 ct_path++;
5651 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5652 status = ct->staged_status;
5653 else
5654 status = ct->status;
5656 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5657 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5660 static const struct got_error *
5661 match_modified_subtree(int *modified, struct got_tree_entry *te,
5662 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5664 const struct got_error *err = NULL;
5665 struct got_pathlist_entry *pe;
5666 char *te_path;
5668 *modified = 0;
5670 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5671 got_path_is_root_dir(base_tree_path) ? "" : "/",
5672 te->name) == -1)
5673 return got_error_from_errno("asprintf");
5675 TAILQ_FOREACH(pe, commitable_paths, entry) {
5676 struct got_commitable *ct = pe->data;
5677 *modified = got_path_is_child(ct->in_repo_path, te_path,
5678 strlen(te_path));
5679 if (*modified)
5680 break;
5683 free(te_path);
5684 return err;
5687 static const struct got_error *
5688 match_deleted_or_modified_ct(struct got_commitable **ctp,
5689 struct got_tree_entry *te, const char *base_tree_path,
5690 struct got_pathlist_head *commitable_paths)
5692 const struct got_error *err = NULL;
5693 struct got_pathlist_entry *pe;
5695 *ctp = NULL;
5697 TAILQ_FOREACH(pe, commitable_paths, entry) {
5698 struct got_commitable *ct = pe->data;
5699 char *ct_name = NULL;
5700 int path_matches;
5702 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5703 if (ct->status != GOT_STATUS_MODIFY &&
5704 ct->status != GOT_STATUS_MODE_CHANGE &&
5705 ct->status != GOT_STATUS_DELETE &&
5706 ct->status != GOT_STATUS_CONFLICT)
5707 continue;
5708 } else {
5709 if (ct->staged_status != GOT_STATUS_MODIFY &&
5710 ct->staged_status != GOT_STATUS_DELETE)
5711 continue;
5714 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5715 continue;
5717 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5718 if (err)
5719 return err;
5720 if (!path_matches)
5721 continue;
5723 err = got_path_basename(&ct_name, pe->path);
5724 if (err)
5725 return err;
5727 if (strcmp(te->name, ct_name) != 0) {
5728 free(ct_name);
5729 continue;
5731 free(ct_name);
5733 *ctp = ct;
5734 break;
5737 return err;
5740 static const struct got_error *
5741 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5742 const char *child_path, const char *path_base_tree,
5743 struct got_pathlist_head *commitable_paths,
5744 got_worktree_status_cb status_cb, void *status_arg,
5745 struct got_repository *repo)
5747 const struct got_error *err = NULL;
5748 struct got_tree_entry *new_te;
5749 char *subtree_path;
5750 struct got_object_id *id = NULL;
5751 int nentries;
5753 *new_tep = NULL;
5755 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5756 got_path_is_root_dir(path_base_tree) ? "" : "/",
5757 child_path) == -1)
5758 return got_error_from_errno("asprintf");
5760 new_te = calloc(1, sizeof(*new_te));
5761 if (new_te == NULL)
5762 return got_error_from_errno("calloc");
5763 new_te->mode = S_IFDIR;
5765 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5766 sizeof(new_te->name)) {
5767 err = got_error(GOT_ERR_NO_SPACE);
5768 goto done;
5770 err = write_tree(&id, &nentries, NULL, subtree_path,
5771 commitable_paths, status_cb, status_arg, repo);
5772 if (err) {
5773 free(new_te);
5774 goto done;
5776 memcpy(&new_te->id, id, sizeof(new_te->id));
5777 done:
5778 free(id);
5779 free(subtree_path);
5780 if (err == NULL)
5781 *new_tep = new_te;
5782 return err;
5785 static const struct got_error *
5786 write_tree(struct got_object_id **new_tree_id, int *nentries,
5787 struct got_tree_object *base_tree, const char *path_base_tree,
5788 struct got_pathlist_head *commitable_paths,
5789 got_worktree_status_cb status_cb, void *status_arg,
5790 struct got_repository *repo)
5792 const struct got_error *err = NULL;
5793 struct got_pathlist_head paths;
5794 struct got_tree_entry *te, *new_te = NULL;
5795 struct got_pathlist_entry *pe;
5797 TAILQ_INIT(&paths);
5798 *nentries = 0;
5800 /* Insert, and recurse into, newly added entries first. */
5801 TAILQ_FOREACH(pe, commitable_paths, entry) {
5802 struct got_commitable *ct = pe->data;
5803 char *child_path = NULL, *slash;
5805 if ((ct->status != GOT_STATUS_ADD &&
5806 ct->staged_status != GOT_STATUS_ADD) ||
5807 (ct->flags & GOT_COMMITABLE_ADDED))
5808 continue;
5810 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5811 strlen(path_base_tree)))
5812 continue;
5814 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5815 ct->in_repo_path);
5816 if (err)
5817 goto done;
5819 slash = strchr(child_path, '/');
5820 if (slash == NULL) {
5821 err = alloc_added_blob_tree_entry(&new_te, ct);
5822 if (err)
5823 goto done;
5824 err = report_ct_status(ct, status_cb, status_arg);
5825 if (err)
5826 goto done;
5827 ct->flags |= GOT_COMMITABLE_ADDED;
5828 err = insert_tree_entry(new_te, &paths);
5829 if (err)
5830 goto done;
5831 (*nentries)++;
5832 } else {
5833 *slash = '\0'; /* trim trailing path components */
5834 if (base_tree == NULL ||
5835 got_object_tree_find_entry(base_tree, child_path)
5836 == NULL) {
5837 err = make_subtree_for_added_blob(&new_te,
5838 child_path, path_base_tree,
5839 commitable_paths, status_cb, status_arg,
5840 repo);
5841 if (err)
5842 goto done;
5843 err = insert_tree_entry(new_te, &paths);
5844 if (err)
5845 goto done;
5846 (*nentries)++;
5851 if (base_tree) {
5852 int i, nbase_entries;
5853 /* Handle modified and deleted entries. */
5854 nbase_entries = got_object_tree_get_nentries(base_tree);
5855 for (i = 0; i < nbase_entries; i++) {
5856 struct got_commitable *ct = NULL;
5858 te = got_object_tree_get_entry(base_tree, i);
5859 if (got_object_tree_entry_is_submodule(te)) {
5860 /* Entry is a submodule; just copy it. */
5861 err = got_object_tree_entry_dup(&new_te, te);
5862 if (err)
5863 goto done;
5864 err = insert_tree_entry(new_te, &paths);
5865 if (err)
5866 goto done;
5867 (*nentries)++;
5868 continue;
5871 if (S_ISDIR(te->mode)) {
5872 int modified;
5873 err = got_object_tree_entry_dup(&new_te, te);
5874 if (err)
5875 goto done;
5876 err = match_modified_subtree(&modified, te,
5877 path_base_tree, commitable_paths);
5878 if (err)
5879 goto done;
5880 /* Avoid recursion into unmodified subtrees. */
5881 if (modified) {
5882 struct got_object_id *new_id;
5883 int nsubentries;
5884 err = write_subtree(&new_id,
5885 &nsubentries, te,
5886 path_base_tree, commitable_paths,
5887 status_cb, status_arg, repo);
5888 if (err)
5889 goto done;
5890 if (nsubentries == 0) {
5891 /* All entries were deleted. */
5892 free(new_id);
5893 continue;
5895 memcpy(&new_te->id, new_id,
5896 sizeof(new_te->id));
5897 free(new_id);
5899 err = insert_tree_entry(new_te, &paths);
5900 if (err)
5901 goto done;
5902 (*nentries)++;
5903 continue;
5906 err = match_deleted_or_modified_ct(&ct, te,
5907 path_base_tree, commitable_paths);
5908 if (err)
5909 goto done;
5910 if (ct) {
5911 /* NB: Deleted entries get dropped here. */
5912 if (ct->status == GOT_STATUS_MODIFY ||
5913 ct->status == GOT_STATUS_MODE_CHANGE ||
5914 ct->status == GOT_STATUS_CONFLICT ||
5915 ct->staged_status == GOT_STATUS_MODIFY) {
5916 err = alloc_modified_blob_tree_entry(
5917 &new_te, te, ct);
5918 if (err)
5919 goto done;
5920 err = insert_tree_entry(new_te, &paths);
5921 if (err)
5922 goto done;
5923 (*nentries)++;
5925 err = report_ct_status(ct, status_cb,
5926 status_arg);
5927 if (err)
5928 goto done;
5929 } else {
5930 /* Entry is unchanged; just copy it. */
5931 err = got_object_tree_entry_dup(&new_te, te);
5932 if (err)
5933 goto done;
5934 err = insert_tree_entry(new_te, &paths);
5935 if (err)
5936 goto done;
5937 (*nentries)++;
5942 /* Write new list of entries; deleted entries have been dropped. */
5943 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5944 done:
5945 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
5946 return err;
5949 static const struct got_error *
5950 update_fileindex_after_commit(struct got_worktree *worktree,
5951 struct got_pathlist_head *commitable_paths,
5952 struct got_object_id *new_base_commit_id,
5953 struct got_fileindex *fileindex, int have_staged_files)
5955 const struct got_error *err = NULL;
5956 struct got_pathlist_entry *pe;
5957 char *relpath = NULL;
5959 TAILQ_FOREACH(pe, commitable_paths, entry) {
5960 struct got_fileindex_entry *ie;
5961 struct got_commitable *ct = pe->data;
5963 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5965 err = got_path_skip_common_ancestor(&relpath,
5966 worktree->root_path, ct->ondisk_path);
5967 if (err)
5968 goto done;
5970 if (ie) {
5971 if (ct->status == GOT_STATUS_DELETE ||
5972 ct->staged_status == GOT_STATUS_DELETE) {
5973 got_fileindex_entry_remove(fileindex, ie);
5974 } else if (ct->staged_status == GOT_STATUS_ADD ||
5975 ct->staged_status == GOT_STATUS_MODIFY) {
5976 got_fileindex_entry_stage_set(ie,
5977 GOT_FILEIDX_STAGE_NONE);
5978 got_fileindex_entry_staged_filetype_set(ie, 0);
5980 err = got_fileindex_entry_update(ie,
5981 worktree->root_fd, relpath,
5982 ct->staged_blob_id->sha1,
5983 new_base_commit_id->sha1,
5984 !have_staged_files);
5985 } else
5986 err = got_fileindex_entry_update(ie,
5987 worktree->root_fd, relpath,
5988 ct->blob_id->sha1,
5989 new_base_commit_id->sha1,
5990 !have_staged_files);
5991 } else {
5992 err = got_fileindex_entry_alloc(&ie, pe->path);
5993 if (err)
5994 goto done;
5995 err = got_fileindex_entry_update(ie,
5996 worktree->root_fd, relpath, ct->blob_id->sha1,
5997 new_base_commit_id->sha1, 1);
5998 if (err) {
5999 got_fileindex_entry_free(ie);
6000 goto done;
6002 err = got_fileindex_entry_add(fileindex, ie);
6003 if (err) {
6004 got_fileindex_entry_free(ie);
6005 goto done;
6008 free(relpath);
6009 relpath = NULL;
6011 done:
6012 free(relpath);
6013 return err;
6017 static const struct got_error *
6018 check_out_of_date(const char *in_repo_path, unsigned char status,
6019 unsigned char staged_status, struct got_object_id *base_blob_id,
6020 struct got_object_id *base_commit_id,
6021 struct got_object_id *head_commit_id, struct got_repository *repo,
6022 int ood_errcode)
6024 const struct got_error *err = NULL;
6025 struct got_commit_object *commit = NULL;
6026 struct got_object_id *id = NULL;
6028 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
6029 /* Trivial case: base commit == head commit */
6030 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
6031 return NULL;
6033 * Ensure file content which local changes were based
6034 * on matches file content in the branch head.
6036 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6037 if (err)
6038 goto done;
6039 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6040 if (err) {
6041 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6042 err = got_error(ood_errcode);
6043 goto done;
6044 } else if (got_object_id_cmp(id, base_blob_id) != 0)
6045 err = got_error(ood_errcode);
6046 } else {
6047 /* Require that added files don't exist in the branch head. */
6048 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6049 if (err)
6050 goto done;
6051 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6052 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
6053 goto done;
6054 err = id ? got_error(ood_errcode) : NULL;
6056 done:
6057 free(id);
6058 if (commit)
6059 got_object_commit_close(commit);
6060 return err;
6063 static const struct got_error *
6064 commit_worktree(struct got_object_id **new_commit_id,
6065 struct got_pathlist_head *commitable_paths,
6066 struct got_object_id *head_commit_id,
6067 struct got_object_id *parent_id2,
6068 struct got_worktree *worktree,
6069 const char *author, const char *committer, char *diff_path,
6070 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6071 got_worktree_status_cb status_cb, void *status_arg,
6072 struct got_repository *repo)
6074 const struct got_error *err = NULL, *unlockerr = NULL;
6075 struct got_pathlist_entry *pe;
6076 const char *head_ref_name = NULL;
6077 struct got_commit_object *head_commit = NULL;
6078 struct got_reference *head_ref2 = NULL;
6079 struct got_object_id *head_commit_id2 = NULL;
6080 struct got_tree_object *head_tree = NULL;
6081 struct got_object_id *new_tree_id = NULL;
6082 int nentries, nparents = 0;
6083 struct got_object_id_queue parent_ids;
6084 struct got_object_qid *pid = NULL;
6085 char *logmsg = NULL;
6086 time_t timestamp;
6088 *new_commit_id = NULL;
6090 STAILQ_INIT(&parent_ids);
6092 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6093 if (err)
6094 goto done;
6096 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6097 if (err)
6098 goto done;
6100 if (commit_msg_cb != NULL) {
6101 err = commit_msg_cb(commitable_paths, diff_path,
6102 &logmsg, commit_arg);
6103 if (err)
6104 goto done;
6107 if (logmsg == NULL || strlen(logmsg) == 0) {
6108 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6109 goto done;
6112 /* Create blobs from added and modified files and record their IDs. */
6113 TAILQ_FOREACH(pe, commitable_paths, entry) {
6114 struct got_commitable *ct = pe->data;
6115 char *ondisk_path;
6117 /* Blobs for staged files already exist. */
6118 if (ct->staged_status == GOT_STATUS_ADD ||
6119 ct->staged_status == GOT_STATUS_MODIFY)
6120 continue;
6122 if (ct->status != GOT_STATUS_ADD &&
6123 ct->status != GOT_STATUS_MODIFY &&
6124 ct->status != GOT_STATUS_MODE_CHANGE &&
6125 ct->status != GOT_STATUS_CONFLICT)
6126 continue;
6128 if (asprintf(&ondisk_path, "%s/%s",
6129 worktree->root_path, pe->path) == -1) {
6130 err = got_error_from_errno("asprintf");
6131 goto done;
6133 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6134 free(ondisk_path);
6135 if (err)
6136 goto done;
6139 /* Recursively write new tree objects. */
6140 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6141 commitable_paths, status_cb, status_arg, repo);
6142 if (err)
6143 goto done;
6145 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
6146 if (err)
6147 goto done;
6148 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6149 nparents++;
6150 if (parent_id2) {
6151 err = got_object_qid_alloc(&pid, parent_id2);
6152 if (err)
6153 goto done;
6154 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6155 nparents++;
6157 timestamp = time(NULL);
6158 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6159 nparents, author, timestamp, committer, timestamp, logmsg, repo);
6160 if (logmsg != NULL)
6161 free(logmsg);
6162 if (err)
6163 goto done;
6165 /* Check if a concurrent commit to our branch has occurred. */
6166 head_ref_name = got_worktree_get_head_ref_name(worktree);
6167 if (head_ref_name == NULL) {
6168 err = got_error_from_errno("got_worktree_get_head_ref_name");
6169 goto done;
6171 /* Lock the reference here to prevent concurrent modification. */
6172 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6173 if (err)
6174 goto done;
6175 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6176 if (err)
6177 goto done;
6178 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6179 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6180 goto done;
6182 /* Update branch head in repository. */
6183 err = got_ref_change_ref(head_ref2, *new_commit_id);
6184 if (err)
6185 goto done;
6186 err = got_ref_write(head_ref2, repo);
6187 if (err)
6188 goto done;
6190 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6191 if (err)
6192 goto done;
6194 err = ref_base_commit(worktree, repo);
6195 if (err)
6196 goto done;
6197 done:
6198 got_object_id_queue_free(&parent_ids);
6199 if (head_tree)
6200 got_object_tree_close(head_tree);
6201 if (head_commit)
6202 got_object_commit_close(head_commit);
6203 free(head_commit_id2);
6204 if (head_ref2) {
6205 unlockerr = got_ref_unlock(head_ref2);
6206 if (unlockerr && err == NULL)
6207 err = unlockerr;
6208 got_ref_close(head_ref2);
6210 return err;
6213 static const struct got_error *
6214 check_path_is_commitable(const char *path,
6215 struct got_pathlist_head *commitable_paths)
6217 struct got_pathlist_entry *cpe = NULL;
6218 size_t path_len = strlen(path);
6220 TAILQ_FOREACH(cpe, commitable_paths, entry) {
6221 struct got_commitable *ct = cpe->data;
6222 const char *ct_path = ct->path;
6224 while (ct_path[0] == '/')
6225 ct_path++;
6227 if (strcmp(path, ct_path) == 0 ||
6228 got_path_is_child(ct_path, path, path_len))
6229 break;
6232 if (cpe == NULL)
6233 return got_error_path(path, GOT_ERR_BAD_PATH);
6235 return NULL;
6238 static const struct got_error *
6239 check_staged_file(void *arg, struct got_fileindex_entry *ie)
6241 int *have_staged_files = arg;
6243 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6244 *have_staged_files = 1;
6245 return got_error(GOT_ERR_CANCELLED);
6248 return NULL;
6251 static const struct got_error *
6252 check_non_staged_files(struct got_fileindex *fileindex,
6253 struct got_pathlist_head *paths)
6255 struct got_pathlist_entry *pe;
6256 struct got_fileindex_entry *ie;
6258 TAILQ_FOREACH(pe, paths, entry) {
6259 if (pe->path[0] == '\0')
6260 continue;
6261 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6262 if (ie == NULL)
6263 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6264 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6265 return got_error_path(pe->path,
6266 GOT_ERR_FILE_NOT_STAGED);
6269 return NULL;
6272 const struct got_error *
6273 got_worktree_commit(struct got_object_id **new_commit_id,
6274 struct got_worktree *worktree, struct got_pathlist_head *paths,
6275 const char *author, const char *committer, int allow_bad_symlinks,
6276 int show_diff, int commit_conflicts,
6277 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6278 got_worktree_status_cb status_cb, void *status_arg,
6279 struct got_repository *repo)
6281 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6282 struct got_fileindex *fileindex = NULL;
6283 char *fileindex_path = NULL;
6284 struct got_pathlist_head commitable_paths;
6285 struct collect_commitables_arg cc_arg;
6286 struct got_pathlist_entry *pe;
6287 struct got_reference *head_ref = NULL;
6288 struct got_object_id *head_commit_id = NULL;
6289 char *diff_path = NULL;
6290 int have_staged_files = 0;
6292 *new_commit_id = NULL;
6294 memset(&cc_arg, 0, sizeof(cc_arg));
6295 TAILQ_INIT(&commitable_paths);
6297 err = lock_worktree(worktree, LOCK_EX);
6298 if (err)
6299 goto done;
6301 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6302 if (err)
6303 goto done;
6305 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6306 if (err)
6307 goto done;
6309 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6310 if (err)
6311 goto done;
6313 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6314 &have_staged_files);
6315 if (err && err->code != GOT_ERR_CANCELLED)
6316 goto done;
6317 if (have_staged_files) {
6318 err = check_non_staged_files(fileindex, paths);
6319 if (err)
6320 goto done;
6323 cc_arg.commitable_paths = &commitable_paths;
6324 cc_arg.worktree = worktree;
6325 cc_arg.fileindex = fileindex;
6326 cc_arg.repo = repo;
6327 cc_arg.have_staged_files = have_staged_files;
6328 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6329 cc_arg.diff_header_shown = 0;
6330 cc_arg.commit_conflicts = commit_conflicts;
6331 if (show_diff) {
6332 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6333 GOT_TMPDIR_STR "/got", ".diff");
6334 if (err)
6335 goto done;
6336 cc_arg.f1 = got_opentemp();
6337 if (cc_arg.f1 == NULL) {
6338 err = got_error_from_errno("got_opentemp");
6339 goto done;
6341 cc_arg.f2 = got_opentemp();
6342 if (cc_arg.f2 == NULL) {
6343 err = got_error_from_errno("got_opentemp");
6344 goto done;
6348 TAILQ_FOREACH(pe, paths, entry) {
6349 err = worktree_status(worktree, pe->path, fileindex, repo,
6350 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6351 if (err)
6352 goto done;
6355 if (show_diff) {
6356 if (fflush(cc_arg.diff_outfile) == EOF) {
6357 err = got_error_from_errno("fflush");
6358 goto done;
6362 if (TAILQ_EMPTY(&commitable_paths)) {
6363 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6364 goto done;
6367 TAILQ_FOREACH(pe, paths, entry) {
6368 err = check_path_is_commitable(pe->path, &commitable_paths);
6369 if (err)
6370 goto done;
6373 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6374 struct got_commitable *ct = pe->data;
6375 const char *ct_path = ct->in_repo_path;
6377 while (ct_path[0] == '/')
6378 ct_path++;
6379 err = check_out_of_date(ct_path, ct->status,
6380 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6381 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6382 if (err)
6383 goto done;
6387 err = commit_worktree(new_commit_id, &commitable_paths,
6388 head_commit_id, NULL, worktree, author, committer,
6389 (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6390 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6391 if (err)
6392 goto done;
6394 err = update_fileindex_after_commit(worktree, &commitable_paths,
6395 *new_commit_id, fileindex, have_staged_files);
6396 sync_err = sync_fileindex(fileindex, fileindex_path);
6397 if (sync_err && err == NULL)
6398 err = sync_err;
6399 done:
6400 if (fileindex)
6401 got_fileindex_free(fileindex);
6402 free(fileindex_path);
6403 unlockerr = lock_worktree(worktree, LOCK_SH);
6404 if (unlockerr && err == NULL)
6405 err = unlockerr;
6406 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6407 struct got_commitable *ct = pe->data;
6409 free_commitable(ct);
6411 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6412 if (diff_path && unlink(diff_path) == -1 && err == NULL)
6413 err = got_error_from_errno2("unlink", diff_path);
6414 free(diff_path);
6415 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6416 err == NULL)
6417 err = got_error_from_errno("fclose");
6418 return err;
6421 const char *
6422 got_commitable_get_path(struct got_commitable *ct)
6424 return ct->path;
6427 unsigned int
6428 got_commitable_get_status(struct got_commitable *ct)
6430 return ct->status;
6433 struct check_rebase_ok_arg {
6434 struct got_worktree *worktree;
6435 struct got_repository *repo;
6438 static const struct got_error *
6439 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6441 const struct got_error *err = NULL;
6442 struct check_rebase_ok_arg *a = arg;
6443 unsigned char status;
6444 struct stat sb;
6445 char *ondisk_path;
6447 /* Reject rebase of a work tree with mixed base commits. */
6448 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6449 SHA1_DIGEST_LENGTH))
6450 return got_error(GOT_ERR_MIXED_COMMITS);
6452 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6453 == -1)
6454 return got_error_from_errno("asprintf");
6456 /* Reject rebase of a work tree with modified or staged files. */
6457 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6458 free(ondisk_path);
6459 if (err)
6460 return err;
6462 if (status != GOT_STATUS_NO_CHANGE)
6463 return got_error(GOT_ERR_MODIFIED);
6464 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6465 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6467 return NULL;
6470 const struct got_error *
6471 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6472 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6473 struct got_worktree *worktree, struct got_reference *branch,
6474 struct got_repository *repo)
6476 const struct got_error *err = NULL;
6477 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6478 char *branch_ref_name = NULL;
6479 char *fileindex_path = NULL;
6480 struct check_rebase_ok_arg ok_arg;
6481 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6482 struct got_object_id *wt_branch_tip = NULL;
6484 *new_base_branch_ref = NULL;
6485 *tmp_branch = NULL;
6486 *fileindex = NULL;
6488 err = lock_worktree(worktree, LOCK_EX);
6489 if (err)
6490 return err;
6492 err = open_fileindex(fileindex, &fileindex_path, worktree);
6493 if (err)
6494 goto done;
6496 ok_arg.worktree = worktree;
6497 ok_arg.repo = repo;
6498 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6499 &ok_arg);
6500 if (err)
6501 goto done;
6503 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6504 if (err)
6505 goto done;
6507 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6508 if (err)
6509 goto done;
6511 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6512 if (err)
6513 goto done;
6515 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6516 0);
6517 if (err)
6518 goto done;
6520 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6521 if (err)
6522 goto done;
6523 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6524 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6525 goto done;
6528 err = got_ref_alloc_symref(new_base_branch_ref,
6529 new_base_branch_ref_name, wt_branch);
6530 if (err)
6531 goto done;
6532 err = got_ref_write(*new_base_branch_ref, repo);
6533 if (err)
6534 goto done;
6536 /* TODO Lock original branch's ref while rebasing? */
6538 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6539 if (err)
6540 goto done;
6542 err = got_ref_write(branch_ref, repo);
6543 if (err)
6544 goto done;
6546 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6547 worktree->base_commit_id);
6548 if (err)
6549 goto done;
6550 err = got_ref_write(*tmp_branch, repo);
6551 if (err)
6552 goto done;
6554 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6555 if (err)
6556 goto done;
6557 done:
6558 free(fileindex_path);
6559 free(tmp_branch_name);
6560 free(new_base_branch_ref_name);
6561 free(branch_ref_name);
6562 if (branch_ref)
6563 got_ref_close(branch_ref);
6564 if (wt_branch)
6565 got_ref_close(wt_branch);
6566 free(wt_branch_tip);
6567 if (err) {
6568 if (*new_base_branch_ref) {
6569 got_ref_close(*new_base_branch_ref);
6570 *new_base_branch_ref = NULL;
6572 if (*tmp_branch) {
6573 got_ref_close(*tmp_branch);
6574 *tmp_branch = NULL;
6576 if (*fileindex) {
6577 got_fileindex_free(*fileindex);
6578 *fileindex = NULL;
6580 lock_worktree(worktree, LOCK_SH);
6582 return err;
6585 const struct got_error *
6586 got_worktree_rebase_continue(struct got_object_id **commit_id,
6587 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6588 struct got_reference **branch, struct got_fileindex **fileindex,
6589 struct got_worktree *worktree, struct got_repository *repo)
6591 const struct got_error *err;
6592 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6593 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6594 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6595 char *fileindex_path = NULL;
6596 int have_staged_files = 0;
6598 *commit_id = NULL;
6599 *new_base_branch = NULL;
6600 *tmp_branch = NULL;
6601 *branch = NULL;
6602 *fileindex = NULL;
6604 err = lock_worktree(worktree, LOCK_EX);
6605 if (err)
6606 return err;
6608 err = open_fileindex(fileindex, &fileindex_path, worktree);
6609 if (err)
6610 goto done;
6612 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6613 &have_staged_files);
6614 if (err && err->code != GOT_ERR_CANCELLED)
6615 goto done;
6616 if (have_staged_files) {
6617 err = got_error(GOT_ERR_STAGED_PATHS);
6618 goto done;
6621 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6622 if (err)
6623 goto done;
6625 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6626 if (err)
6627 goto done;
6629 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6630 if (err)
6631 goto done;
6633 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6634 if (err)
6635 goto done;
6637 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6638 if (err)
6639 goto done;
6641 err = got_ref_open(branch, repo,
6642 got_ref_get_symref_target(branch_ref), 0);
6643 if (err)
6644 goto done;
6646 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6647 if (err)
6648 goto done;
6650 err = got_ref_resolve(commit_id, repo, commit_ref);
6651 if (err)
6652 goto done;
6654 err = got_ref_open(new_base_branch, repo,
6655 new_base_branch_ref_name, 0);
6656 if (err)
6657 goto done;
6659 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6660 if (err)
6661 goto done;
6662 done:
6663 free(commit_ref_name);
6664 free(branch_ref_name);
6665 free(fileindex_path);
6666 if (commit_ref)
6667 got_ref_close(commit_ref);
6668 if (branch_ref)
6669 got_ref_close(branch_ref);
6670 if (err) {
6671 free(*commit_id);
6672 *commit_id = NULL;
6673 if (*tmp_branch) {
6674 got_ref_close(*tmp_branch);
6675 *tmp_branch = NULL;
6677 if (*new_base_branch) {
6678 got_ref_close(*new_base_branch);
6679 *new_base_branch = NULL;
6681 if (*branch) {
6682 got_ref_close(*branch);
6683 *branch = NULL;
6685 if (*fileindex) {
6686 got_fileindex_free(*fileindex);
6687 *fileindex = NULL;
6689 lock_worktree(worktree, LOCK_SH);
6691 return err;
6694 const struct got_error *
6695 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6697 const struct got_error *err;
6698 char *tmp_branch_name = NULL;
6700 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6701 if (err)
6702 return err;
6704 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6705 free(tmp_branch_name);
6706 return NULL;
6709 static const struct got_error *
6710 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6711 const char *diff_path, char **logmsg, void *arg)
6713 *logmsg = arg;
6714 return NULL;
6717 static const struct got_error *
6718 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6719 const char *path, struct got_object_id *blob_id,
6720 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6721 int dirfd, const char *de_name)
6723 return NULL;
6726 struct collect_merged_paths_arg {
6727 got_worktree_checkout_cb progress_cb;
6728 void *progress_arg;
6729 struct got_pathlist_head *merged_paths;
6732 static const struct got_error *
6733 collect_merged_paths(void *arg, unsigned char status, const char *path)
6735 const struct got_error *err;
6736 struct collect_merged_paths_arg *a = arg;
6737 char *p;
6738 struct got_pathlist_entry *new;
6740 err = (*a->progress_cb)(a->progress_arg, status, path);
6741 if (err)
6742 return err;
6744 if (status != GOT_STATUS_MERGE &&
6745 status != GOT_STATUS_ADD &&
6746 status != GOT_STATUS_DELETE &&
6747 status != GOT_STATUS_CONFLICT)
6748 return NULL;
6750 p = strdup(path);
6751 if (p == NULL)
6752 return got_error_from_errno("strdup");
6754 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6755 if (err || new == NULL)
6756 free(p);
6757 return err;
6760 static const struct got_error *
6761 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6762 int is_rebase, struct got_repository *repo)
6764 const struct got_error *err;
6765 struct got_reference *commit_ref = NULL;
6767 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6768 if (err) {
6769 if (err->code != GOT_ERR_NOT_REF)
6770 goto done;
6771 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6772 if (err)
6773 goto done;
6774 err = got_ref_write(commit_ref, repo);
6775 if (err)
6776 goto done;
6777 } else if (is_rebase) {
6778 struct got_object_id *stored_id;
6779 int cmp;
6781 err = got_ref_resolve(&stored_id, repo, commit_ref);
6782 if (err)
6783 goto done;
6784 cmp = got_object_id_cmp(commit_id, stored_id);
6785 free(stored_id);
6786 if (cmp != 0) {
6787 err = got_error(GOT_ERR_REBASE_COMMITID);
6788 goto done;
6791 done:
6792 if (commit_ref)
6793 got_ref_close(commit_ref);
6794 return err;
6797 static const struct got_error *
6798 rebase_merge_files(struct got_pathlist_head *merged_paths,
6799 const char *commit_ref_name, struct got_worktree *worktree,
6800 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6801 struct got_object_id *commit_id, struct got_repository *repo,
6802 got_worktree_checkout_cb progress_cb, void *progress_arg,
6803 got_cancel_cb cancel_cb, void *cancel_arg)
6805 const struct got_error *err;
6806 struct got_reference *commit_ref = NULL;
6807 struct collect_merged_paths_arg cmp_arg;
6808 char *fileindex_path;
6810 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6812 err = get_fileindex_path(&fileindex_path, worktree);
6813 if (err)
6814 return err;
6816 cmp_arg.progress_cb = progress_cb;
6817 cmp_arg.progress_arg = progress_arg;
6818 cmp_arg.merged_paths = merged_paths;
6819 err = merge_files(worktree, fileindex, fileindex_path,
6820 parent_commit_id, commit_id, repo, collect_merged_paths,
6821 &cmp_arg, cancel_cb, cancel_arg);
6822 if (commit_ref)
6823 got_ref_close(commit_ref);
6824 return err;
6827 const struct got_error *
6828 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6829 struct got_worktree *worktree, struct got_fileindex *fileindex,
6830 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6831 struct got_repository *repo,
6832 got_worktree_checkout_cb progress_cb, void *progress_arg,
6833 got_cancel_cb cancel_cb, void *cancel_arg)
6835 const struct got_error *err;
6836 char *commit_ref_name;
6838 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6839 if (err)
6840 return err;
6842 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6843 if (err)
6844 goto done;
6846 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6847 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6848 progress_arg, cancel_cb, cancel_arg);
6849 done:
6850 free(commit_ref_name);
6851 return err;
6854 const struct got_error *
6855 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6856 struct got_worktree *worktree, struct got_fileindex *fileindex,
6857 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6858 struct got_repository *repo,
6859 got_worktree_checkout_cb progress_cb, void *progress_arg,
6860 got_cancel_cb cancel_cb, void *cancel_arg)
6862 const struct got_error *err;
6863 char *commit_ref_name;
6865 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6866 if (err)
6867 return err;
6869 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6870 if (err)
6871 goto done;
6873 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6874 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6875 progress_arg, cancel_cb, cancel_arg);
6876 done:
6877 free(commit_ref_name);
6878 return err;
6881 static const struct got_error *
6882 rebase_commit(struct got_object_id **new_commit_id,
6883 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6884 struct got_worktree *worktree, struct got_fileindex *fileindex,
6885 struct got_reference *tmp_branch, const char *committer,
6886 struct got_commit_object *orig_commit, const char *new_logmsg,
6887 int allow_conflict, struct got_repository *repo)
6889 const struct got_error *err, *sync_err;
6890 struct got_pathlist_head commitable_paths;
6891 struct collect_commitables_arg cc_arg;
6892 char *fileindex_path = NULL;
6893 struct got_reference *head_ref = NULL;
6894 struct got_object_id *head_commit_id = NULL;
6895 char *logmsg = NULL;
6897 memset(&cc_arg, 0, sizeof(cc_arg));
6898 TAILQ_INIT(&commitable_paths);
6899 *new_commit_id = NULL;
6901 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6903 err = get_fileindex_path(&fileindex_path, worktree);
6904 if (err)
6905 return err;
6907 cc_arg.commitable_paths = &commitable_paths;
6908 cc_arg.worktree = worktree;
6909 cc_arg.repo = repo;
6910 cc_arg.have_staged_files = 0;
6911 cc_arg.commit_conflicts = allow_conflict;
6913 * If possible get the status of individual files directly to
6914 * avoid crawling the entire work tree once per rebased commit.
6916 * Ideally, merged_paths would contain a list of commitables
6917 * we could use so we could skip worktree_status() entirely.
6918 * However, we would then need carefully keep track of cumulative
6919 * effects of operations such as file additions and deletions
6920 * in 'got histedit -f' (folding multiple commits into one),
6921 * and this extra complexity is not really worth it.
6923 if (merged_paths) {
6924 struct got_pathlist_entry *pe;
6925 TAILQ_FOREACH(pe, merged_paths, entry) {
6926 err = worktree_status(worktree, pe->path, fileindex,
6927 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6928 0);
6929 if (err)
6930 goto done;
6932 } else {
6933 err = worktree_status(worktree, "", fileindex, repo,
6934 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6935 if (err)
6936 goto done;
6939 if (TAILQ_EMPTY(&commitable_paths)) {
6940 /* No-op change; commit will be elided. */
6941 err = got_ref_delete(commit_ref, repo);
6942 if (err)
6943 goto done;
6944 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6945 goto done;
6948 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6949 if (err)
6950 goto done;
6952 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6953 if (err)
6954 goto done;
6956 if (new_logmsg) {
6957 logmsg = strdup(new_logmsg);
6958 if (logmsg == NULL) {
6959 err = got_error_from_errno("strdup");
6960 goto done;
6962 } else {
6963 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6964 if (err)
6965 goto done;
6968 /* NB: commit_worktree will call free(logmsg) */
6969 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6970 NULL, worktree, got_object_commit_get_author(orig_commit),
6971 committer ? committer :
6972 got_object_commit_get_committer(orig_commit), NULL,
6973 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6974 if (err)
6975 goto done;
6977 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6978 if (err)
6979 goto done;
6981 err = got_ref_delete(commit_ref, repo);
6982 if (err)
6983 goto done;
6985 err = update_fileindex_after_commit(worktree, &commitable_paths,
6986 *new_commit_id, fileindex, 0);
6987 sync_err = sync_fileindex(fileindex, fileindex_path);
6988 if (sync_err && err == NULL)
6989 err = sync_err;
6990 done:
6991 free(fileindex_path);
6992 free(head_commit_id);
6993 if (head_ref)
6994 got_ref_close(head_ref);
6995 if (err) {
6996 free(*new_commit_id);
6997 *new_commit_id = NULL;
6999 return err;
7002 const struct got_error *
7003 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
7004 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7005 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7006 const char *committer, struct got_commit_object *orig_commit,
7007 struct got_object_id *orig_commit_id, int allow_conflict,
7008 struct got_repository *repo)
7010 const struct got_error *err;
7011 char *commit_ref_name;
7012 struct got_reference *commit_ref = NULL;
7013 struct got_object_id *commit_id = NULL;
7015 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7016 if (err)
7017 return err;
7019 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7020 if (err)
7021 goto done;
7022 err = got_ref_resolve(&commit_id, repo, commit_ref);
7023 if (err)
7024 goto done;
7025 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
7026 err = got_error(GOT_ERR_REBASE_COMMITID);
7027 goto done;
7030 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7031 worktree, fileindex, tmp_branch, committer, orig_commit,
7032 NULL, allow_conflict, repo);
7033 done:
7034 if (commit_ref)
7035 got_ref_close(commit_ref);
7036 free(commit_ref_name);
7037 free(commit_id);
7038 return err;
7041 const struct got_error *
7042 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
7043 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7044 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7045 const char *committer, struct got_commit_object *orig_commit,
7046 struct got_object_id *orig_commit_id, const char *new_logmsg,
7047 int allow_conflict, struct got_repository *repo)
7049 const struct got_error *err;
7050 char *commit_ref_name;
7051 struct got_reference *commit_ref = NULL;
7053 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7054 if (err)
7055 return err;
7057 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7058 if (err)
7059 goto done;
7061 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7062 worktree, fileindex, tmp_branch, committer, orig_commit,
7063 new_logmsg, allow_conflict, repo);
7064 done:
7065 if (commit_ref)
7066 got_ref_close(commit_ref);
7067 free(commit_ref_name);
7068 return err;
7071 const struct got_error *
7072 got_worktree_rebase_postpone(struct got_worktree *worktree,
7073 struct got_fileindex *fileindex)
7075 if (fileindex)
7076 got_fileindex_free(fileindex);
7077 return lock_worktree(worktree, LOCK_SH);
7080 static const struct got_error *
7081 delete_ref(const char *name, struct got_repository *repo)
7083 const struct got_error *err;
7084 struct got_reference *ref;
7086 err = got_ref_open(&ref, repo, name, 0);
7087 if (err) {
7088 if (err->code == GOT_ERR_NOT_REF)
7089 return NULL;
7090 return err;
7093 err = got_ref_delete(ref, repo);
7094 got_ref_close(ref);
7095 return err;
7098 static const struct got_error *
7099 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7101 const struct got_error *err;
7102 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7103 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7105 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7106 if (err)
7107 goto done;
7108 err = delete_ref(tmp_branch_name, repo);
7109 if (err)
7110 goto done;
7112 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7113 if (err)
7114 goto done;
7115 err = delete_ref(new_base_branch_ref_name, repo);
7116 if (err)
7117 goto done;
7119 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7120 if (err)
7121 goto done;
7122 err = delete_ref(branch_ref_name, repo);
7123 if (err)
7124 goto done;
7126 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7127 if (err)
7128 goto done;
7129 err = delete_ref(commit_ref_name, repo);
7130 if (err)
7131 goto done;
7133 done:
7134 free(tmp_branch_name);
7135 free(new_base_branch_ref_name);
7136 free(branch_ref_name);
7137 free(commit_ref_name);
7138 return err;
7141 static const struct got_error *
7142 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7143 struct got_object_id *new_commit_id, struct got_repository *repo)
7145 const struct got_error *err;
7146 struct got_reference *ref = NULL;
7147 struct got_object_id *old_commit_id = NULL;
7148 const char *branch_name = NULL;
7149 char *new_id_str = NULL;
7150 char *refname = NULL;
7152 branch_name = got_ref_get_name(branch);
7153 if (strncmp(branch_name, "refs/heads/", 11) != 0)
7154 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7155 branch_name += 11;
7157 err = got_object_id_str(&new_id_str, new_commit_id);
7158 if (err)
7159 return err;
7161 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7162 new_id_str) == -1) {
7163 err = got_error_from_errno("asprintf");
7164 goto done;
7167 err = got_ref_resolve(&old_commit_id, repo, branch);
7168 if (err)
7169 goto done;
7171 err = got_ref_alloc(&ref, refname, old_commit_id);
7172 if (err)
7173 goto done;
7175 err = got_ref_write(ref, repo);
7176 done:
7177 free(new_id_str);
7178 free(refname);
7179 free(old_commit_id);
7180 if (ref)
7181 got_ref_close(ref);
7182 return err;
7185 const struct got_error *
7186 got_worktree_rebase_complete(struct got_worktree *worktree,
7187 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7188 struct got_reference *rebased_branch, struct got_repository *repo,
7189 int create_backup)
7191 const struct got_error *err, *unlockerr, *sync_err;
7192 struct got_object_id *new_head_commit_id = NULL;
7193 char *fileindex_path = NULL;
7195 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7196 if (err)
7197 return err;
7199 if (create_backup) {
7200 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7201 rebased_branch, new_head_commit_id, repo);
7202 if (err)
7203 goto done;
7206 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7207 if (err)
7208 goto done;
7210 err = got_ref_write(rebased_branch, repo);
7211 if (err)
7212 goto done;
7214 err = got_worktree_set_head_ref(worktree, rebased_branch);
7215 if (err)
7216 goto done;
7218 err = delete_rebase_refs(worktree, repo);
7219 if (err)
7220 goto done;
7222 err = get_fileindex_path(&fileindex_path, worktree);
7223 if (err)
7224 goto done;
7225 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7226 sync_err = sync_fileindex(fileindex, fileindex_path);
7227 if (sync_err && err == NULL)
7228 err = sync_err;
7229 done:
7230 got_fileindex_free(fileindex);
7231 free(fileindex_path);
7232 free(new_head_commit_id);
7233 unlockerr = lock_worktree(worktree, LOCK_SH);
7234 if (unlockerr && err == NULL)
7235 err = unlockerr;
7236 return err;
7239 const struct got_error *
7240 got_worktree_rebase_abort(struct got_worktree *worktree,
7241 struct got_fileindex *fileindex, struct got_repository *repo,
7242 struct got_reference *new_base_branch,
7243 got_worktree_checkout_cb progress_cb, void *progress_arg)
7245 const struct got_error *err, *unlockerr, *sync_err;
7246 struct got_reference *resolved = NULL;
7247 struct got_object_id *commit_id = NULL;
7248 struct got_commit_object *commit = NULL;
7249 char *fileindex_path = NULL;
7250 struct revert_file_args rfa;
7251 struct got_object_id *tree_id = NULL;
7253 err = lock_worktree(worktree, LOCK_EX);
7254 if (err)
7255 return err;
7257 err = got_ref_open(&resolved, repo,
7258 got_ref_get_symref_target(new_base_branch), 0);
7259 if (err)
7260 goto done;
7262 err = got_worktree_set_head_ref(worktree, resolved);
7263 if (err)
7264 goto done;
7267 * XXX commits to the base branch could have happened while
7268 * we were busy rebasing; should we store the original commit ID
7269 * when rebase begins and read it back here?
7271 err = got_ref_resolve(&commit_id, repo, resolved);
7272 if (err)
7273 goto done;
7275 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7276 if (err)
7277 goto done;
7279 err = got_object_open_as_commit(&commit, repo,
7280 worktree->base_commit_id);
7281 if (err)
7282 goto done;
7284 err = got_object_id_by_path(&tree_id, repo, commit,
7285 worktree->path_prefix);
7286 if (err)
7287 goto done;
7289 err = delete_rebase_refs(worktree, repo);
7290 if (err)
7291 goto done;
7293 err = get_fileindex_path(&fileindex_path, worktree);
7294 if (err)
7295 goto done;
7297 rfa.worktree = worktree;
7298 rfa.fileindex = fileindex;
7299 rfa.progress_cb = progress_cb;
7300 rfa.progress_arg = progress_arg;
7301 rfa.patch_cb = NULL;
7302 rfa.patch_arg = NULL;
7303 rfa.repo = repo;
7304 rfa.unlink_added_files = 0;
7305 err = worktree_status(worktree, "", fileindex, repo,
7306 revert_file, &rfa, NULL, NULL, 1, 0);
7307 if (err)
7308 goto sync;
7310 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7311 repo, progress_cb, progress_arg, NULL, NULL);
7312 sync:
7313 sync_err = sync_fileindex(fileindex, fileindex_path);
7314 if (sync_err && err == NULL)
7315 err = sync_err;
7316 done:
7317 got_ref_close(resolved);
7318 free(tree_id);
7319 free(commit_id);
7320 if (commit)
7321 got_object_commit_close(commit);
7322 if (fileindex)
7323 got_fileindex_free(fileindex);
7324 free(fileindex_path);
7326 unlockerr = lock_worktree(worktree, LOCK_SH);
7327 if (unlockerr && err == NULL)
7328 err = unlockerr;
7329 return err;
7332 const struct got_error *
7333 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7334 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7335 struct got_fileindex **fileindex, struct got_worktree *worktree,
7336 struct got_repository *repo)
7338 const struct got_error *err = NULL;
7339 char *tmp_branch_name = NULL;
7340 char *branch_ref_name = NULL;
7341 char *base_commit_ref_name = NULL;
7342 char *fileindex_path = NULL;
7343 struct check_rebase_ok_arg ok_arg;
7344 struct got_reference *wt_branch = NULL;
7345 struct got_reference *base_commit_ref = NULL;
7347 *tmp_branch = NULL;
7348 *branch_ref = NULL;
7349 *base_commit_id = NULL;
7350 *fileindex = NULL;
7352 err = lock_worktree(worktree, LOCK_EX);
7353 if (err)
7354 return err;
7356 err = open_fileindex(fileindex, &fileindex_path, worktree);
7357 if (err)
7358 goto done;
7360 ok_arg.worktree = worktree;
7361 ok_arg.repo = repo;
7362 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7363 &ok_arg);
7364 if (err)
7365 goto done;
7367 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7368 if (err)
7369 goto done;
7371 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7372 if (err)
7373 goto done;
7375 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7376 worktree);
7377 if (err)
7378 goto done;
7380 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7381 0);
7382 if (err)
7383 goto done;
7385 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7386 if (err)
7387 goto done;
7389 err = got_ref_write(*branch_ref, repo);
7390 if (err)
7391 goto done;
7393 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7394 worktree->base_commit_id);
7395 if (err)
7396 goto done;
7397 err = got_ref_write(base_commit_ref, repo);
7398 if (err)
7399 goto done;
7400 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7401 if (*base_commit_id == NULL) {
7402 err = got_error_from_errno("got_object_id_dup");
7403 goto done;
7406 err = got_ref_alloc(tmp_branch, tmp_branch_name,
7407 worktree->base_commit_id);
7408 if (err)
7409 goto done;
7410 err = got_ref_write(*tmp_branch, repo);
7411 if (err)
7412 goto done;
7414 err = got_worktree_set_head_ref(worktree, *tmp_branch);
7415 if (err)
7416 goto done;
7417 done:
7418 free(fileindex_path);
7419 free(tmp_branch_name);
7420 free(branch_ref_name);
7421 free(base_commit_ref_name);
7422 if (wt_branch)
7423 got_ref_close(wt_branch);
7424 if (err) {
7425 if (*branch_ref) {
7426 got_ref_close(*branch_ref);
7427 *branch_ref = NULL;
7429 if (*tmp_branch) {
7430 got_ref_close(*tmp_branch);
7431 *tmp_branch = NULL;
7433 free(*base_commit_id);
7434 if (*fileindex) {
7435 got_fileindex_free(*fileindex);
7436 *fileindex = NULL;
7438 lock_worktree(worktree, LOCK_SH);
7440 return err;
7443 const struct got_error *
7444 got_worktree_histedit_postpone(struct got_worktree *worktree,
7445 struct got_fileindex *fileindex)
7447 if (fileindex)
7448 got_fileindex_free(fileindex);
7449 return lock_worktree(worktree, LOCK_SH);
7452 const struct got_error *
7453 got_worktree_histedit_in_progress(int *in_progress,
7454 struct got_worktree *worktree)
7456 const struct got_error *err;
7457 char *tmp_branch_name = NULL;
7459 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7460 if (err)
7461 return err;
7463 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7464 free(tmp_branch_name);
7465 return NULL;
7468 const struct got_error *
7469 got_worktree_histedit_continue(struct got_object_id **commit_id,
7470 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7471 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7472 struct got_worktree *worktree, struct got_repository *repo)
7474 const struct got_error *err;
7475 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7476 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7477 struct got_reference *commit_ref = NULL;
7478 struct got_reference *base_commit_ref = NULL;
7479 char *fileindex_path = NULL;
7480 int have_staged_files = 0;
7482 *commit_id = NULL;
7483 *tmp_branch = NULL;
7484 *base_commit_id = NULL;
7485 *fileindex = NULL;
7487 err = lock_worktree(worktree, LOCK_EX);
7488 if (err)
7489 return err;
7491 err = open_fileindex(fileindex, &fileindex_path, worktree);
7492 if (err)
7493 goto done;
7495 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7496 &have_staged_files);
7497 if (err && err->code != GOT_ERR_CANCELLED)
7498 goto done;
7499 if (have_staged_files) {
7500 err = got_error(GOT_ERR_STAGED_PATHS);
7501 goto done;
7504 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7505 if (err)
7506 goto done;
7508 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7509 if (err)
7510 goto done;
7512 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7513 if (err)
7514 goto done;
7516 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7517 worktree);
7518 if (err)
7519 goto done;
7521 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7522 if (err)
7523 goto done;
7525 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7526 if (err)
7527 goto done;
7528 err = got_ref_resolve(commit_id, repo, commit_ref);
7529 if (err)
7530 goto done;
7532 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7533 if (err)
7534 goto done;
7535 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7536 if (err)
7537 goto done;
7539 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7540 if (err)
7541 goto done;
7542 done:
7543 free(commit_ref_name);
7544 free(branch_ref_name);
7545 free(fileindex_path);
7546 if (commit_ref)
7547 got_ref_close(commit_ref);
7548 if (base_commit_ref)
7549 got_ref_close(base_commit_ref);
7550 if (err) {
7551 free(*commit_id);
7552 *commit_id = NULL;
7553 free(*base_commit_id);
7554 *base_commit_id = NULL;
7555 if (*tmp_branch) {
7556 got_ref_close(*tmp_branch);
7557 *tmp_branch = NULL;
7559 if (*fileindex) {
7560 got_fileindex_free(*fileindex);
7561 *fileindex = NULL;
7563 lock_worktree(worktree, LOCK_EX);
7565 return err;
7568 static const struct got_error *
7569 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7571 const struct got_error *err;
7572 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7573 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7575 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7576 if (err)
7577 goto done;
7578 err = delete_ref(tmp_branch_name, repo);
7579 if (err)
7580 goto done;
7582 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7583 worktree);
7584 if (err)
7585 goto done;
7586 err = delete_ref(base_commit_ref_name, repo);
7587 if (err)
7588 goto done;
7590 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7591 if (err)
7592 goto done;
7593 err = delete_ref(branch_ref_name, repo);
7594 if (err)
7595 goto done;
7597 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7598 if (err)
7599 goto done;
7600 err = delete_ref(commit_ref_name, repo);
7601 if (err)
7602 goto done;
7603 done:
7604 free(tmp_branch_name);
7605 free(base_commit_ref_name);
7606 free(branch_ref_name);
7607 free(commit_ref_name);
7608 return err;
7611 const struct got_error *
7612 got_worktree_histedit_abort(struct got_worktree *worktree,
7613 struct got_fileindex *fileindex, struct got_repository *repo,
7614 struct got_reference *branch, struct got_object_id *base_commit_id,
7615 got_worktree_checkout_cb progress_cb, void *progress_arg)
7617 const struct got_error *err, *unlockerr, *sync_err;
7618 struct got_reference *resolved = NULL;
7619 char *fileindex_path = NULL;
7620 struct got_commit_object *commit = NULL;
7621 struct got_object_id *tree_id = NULL;
7622 struct revert_file_args rfa;
7624 err = lock_worktree(worktree, LOCK_EX);
7625 if (err)
7626 return err;
7628 err = got_ref_open(&resolved, repo,
7629 got_ref_get_symref_target(branch), 0);
7630 if (err)
7631 goto done;
7633 err = got_worktree_set_head_ref(worktree, resolved);
7634 if (err)
7635 goto done;
7637 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7638 if (err)
7639 goto done;
7641 err = got_object_open_as_commit(&commit, repo,
7642 worktree->base_commit_id);
7643 if (err)
7644 goto done;
7646 err = got_object_id_by_path(&tree_id, repo, commit,
7647 worktree->path_prefix);
7648 if (err)
7649 goto done;
7651 err = delete_histedit_refs(worktree, repo);
7652 if (err)
7653 goto done;
7655 err = get_fileindex_path(&fileindex_path, worktree);
7656 if (err)
7657 goto done;
7659 rfa.worktree = worktree;
7660 rfa.fileindex = fileindex;
7661 rfa.progress_cb = progress_cb;
7662 rfa.progress_arg = progress_arg;
7663 rfa.patch_cb = NULL;
7664 rfa.patch_arg = NULL;
7665 rfa.repo = repo;
7666 rfa.unlink_added_files = 0;
7667 err = worktree_status(worktree, "", fileindex, repo,
7668 revert_file, &rfa, NULL, NULL, 1, 0);
7669 if (err)
7670 goto sync;
7672 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7673 repo, progress_cb, progress_arg, NULL, NULL);
7674 sync:
7675 sync_err = sync_fileindex(fileindex, fileindex_path);
7676 if (sync_err && err == NULL)
7677 err = sync_err;
7678 done:
7679 got_ref_close(resolved);
7680 free(tree_id);
7681 free(fileindex_path);
7683 unlockerr = lock_worktree(worktree, LOCK_SH);
7684 if (unlockerr && err == NULL)
7685 err = unlockerr;
7686 return err;
7689 const struct got_error *
7690 got_worktree_histedit_complete(struct got_worktree *worktree,
7691 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7692 struct got_reference *edited_branch, struct got_repository *repo)
7694 const struct got_error *err, *unlockerr, *sync_err;
7695 struct got_object_id *new_head_commit_id = NULL;
7696 struct got_reference *resolved = NULL;
7697 char *fileindex_path = NULL;
7699 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7700 if (err)
7701 return err;
7703 err = got_ref_open(&resolved, repo,
7704 got_ref_get_symref_target(edited_branch), 0);
7705 if (err)
7706 goto done;
7708 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7709 resolved, new_head_commit_id, repo);
7710 if (err)
7711 goto done;
7713 err = got_ref_change_ref(resolved, new_head_commit_id);
7714 if (err)
7715 goto done;
7717 err = got_ref_write(resolved, repo);
7718 if (err)
7719 goto done;
7721 err = got_worktree_set_head_ref(worktree, resolved);
7722 if (err)
7723 goto done;
7725 err = delete_histedit_refs(worktree, repo);
7726 if (err)
7727 goto done;
7729 err = get_fileindex_path(&fileindex_path, worktree);
7730 if (err)
7731 goto done;
7732 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7733 sync_err = sync_fileindex(fileindex, fileindex_path);
7734 if (sync_err && err == NULL)
7735 err = sync_err;
7736 done:
7737 got_fileindex_free(fileindex);
7738 free(fileindex_path);
7739 free(new_head_commit_id);
7740 unlockerr = lock_worktree(worktree, LOCK_SH);
7741 if (unlockerr && err == NULL)
7742 err = unlockerr;
7743 return err;
7746 const struct got_error *
7747 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7748 struct got_object_id *commit_id, struct got_repository *repo)
7750 const struct got_error *err;
7751 char *commit_ref_name;
7753 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7754 if (err)
7755 return err;
7757 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7758 if (err)
7759 goto done;
7761 err = delete_ref(commit_ref_name, repo);
7762 done:
7763 free(commit_ref_name);
7764 return err;
7767 const struct got_error *
7768 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7769 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7770 struct got_worktree *worktree, const char *refname,
7771 struct got_repository *repo)
7773 const struct got_error *err = NULL;
7774 char *fileindex_path = NULL;
7775 struct check_rebase_ok_arg ok_arg;
7777 *fileindex = NULL;
7778 *branch_ref = NULL;
7779 *base_branch_ref = NULL;
7781 err = lock_worktree(worktree, LOCK_EX);
7782 if (err)
7783 return err;
7785 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7786 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7787 "cannot integrate a branch into itself; "
7788 "update -b or different branch name required");
7789 goto done;
7792 err = open_fileindex(fileindex, &fileindex_path, worktree);
7793 if (err)
7794 goto done;
7796 /* Preconditions are the same as for rebase. */
7797 ok_arg.worktree = worktree;
7798 ok_arg.repo = repo;
7799 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7800 &ok_arg);
7801 if (err)
7802 goto done;
7804 err = got_ref_open(branch_ref, repo, refname, 1);
7805 if (err)
7806 goto done;
7808 err = got_ref_open(base_branch_ref, repo,
7809 got_worktree_get_head_ref_name(worktree), 1);
7810 done:
7811 if (err) {
7812 if (*branch_ref) {
7813 got_ref_close(*branch_ref);
7814 *branch_ref = NULL;
7816 if (*base_branch_ref) {
7817 got_ref_close(*base_branch_ref);
7818 *base_branch_ref = NULL;
7820 if (*fileindex) {
7821 got_fileindex_free(*fileindex);
7822 *fileindex = NULL;
7824 lock_worktree(worktree, LOCK_SH);
7826 return err;
7829 const struct got_error *
7830 got_worktree_integrate_continue(struct got_worktree *worktree,
7831 struct got_fileindex *fileindex, struct got_repository *repo,
7832 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7833 got_worktree_checkout_cb progress_cb, void *progress_arg,
7834 got_cancel_cb cancel_cb, void *cancel_arg)
7836 const struct got_error *err = NULL, *sync_err, *unlockerr;
7837 char *fileindex_path = NULL;
7838 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7839 struct got_commit_object *commit = NULL;
7841 err = get_fileindex_path(&fileindex_path, worktree);
7842 if (err)
7843 goto done;
7845 err = got_ref_resolve(&commit_id, repo, branch_ref);
7846 if (err)
7847 goto done;
7849 err = got_object_open_as_commit(&commit, repo, commit_id);
7850 if (err)
7851 goto done;
7853 err = got_object_id_by_path(&tree_id, repo, commit,
7854 worktree->path_prefix);
7855 if (err)
7856 goto done;
7858 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7859 if (err)
7860 goto done;
7862 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7863 progress_cb, progress_arg, cancel_cb, cancel_arg);
7864 if (err)
7865 goto sync;
7867 err = got_ref_change_ref(base_branch_ref, commit_id);
7868 if (err)
7869 goto sync;
7871 err = got_ref_write(base_branch_ref, repo);
7872 if (err)
7873 goto sync;
7875 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7876 sync:
7877 sync_err = sync_fileindex(fileindex, fileindex_path);
7878 if (sync_err && err == NULL)
7879 err = sync_err;
7881 done:
7882 unlockerr = got_ref_unlock(branch_ref);
7883 if (unlockerr && err == NULL)
7884 err = unlockerr;
7885 got_ref_close(branch_ref);
7887 unlockerr = got_ref_unlock(base_branch_ref);
7888 if (unlockerr && err == NULL)
7889 err = unlockerr;
7890 got_ref_close(base_branch_ref);
7892 got_fileindex_free(fileindex);
7893 free(fileindex_path);
7894 free(tree_id);
7895 if (commit)
7896 got_object_commit_close(commit);
7898 unlockerr = lock_worktree(worktree, LOCK_SH);
7899 if (unlockerr && err == NULL)
7900 err = unlockerr;
7901 return err;
7904 const struct got_error *
7905 got_worktree_integrate_abort(struct got_worktree *worktree,
7906 struct got_fileindex *fileindex, struct got_repository *repo,
7907 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7909 const struct got_error *err = NULL, *unlockerr = NULL;
7911 got_fileindex_free(fileindex);
7913 err = lock_worktree(worktree, LOCK_SH);
7915 unlockerr = got_ref_unlock(branch_ref);
7916 if (unlockerr && err == NULL)
7917 err = unlockerr;
7918 got_ref_close(branch_ref);
7920 unlockerr = got_ref_unlock(base_branch_ref);
7921 if (unlockerr && err == NULL)
7922 err = unlockerr;
7923 got_ref_close(base_branch_ref);
7925 return err;
7928 const struct got_error *
7929 got_worktree_merge_postpone(struct got_worktree *worktree,
7930 struct got_fileindex *fileindex)
7932 const struct got_error *err, *sync_err;
7933 char *fileindex_path = NULL;
7935 err = get_fileindex_path(&fileindex_path, worktree);
7936 if (err)
7937 goto done;
7939 sync_err = sync_fileindex(fileindex, fileindex_path);
7941 err = lock_worktree(worktree, LOCK_SH);
7942 if (sync_err && err == NULL)
7943 err = sync_err;
7944 done:
7945 got_fileindex_free(fileindex);
7946 free(fileindex_path);
7947 return err;
7950 static const struct got_error *
7951 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7953 const struct got_error *err;
7954 char *branch_refname = NULL, *commit_refname = NULL;
7956 err = get_merge_branch_ref_name(&branch_refname, worktree);
7957 if (err)
7958 goto done;
7959 err = delete_ref(branch_refname, repo);
7960 if (err)
7961 goto done;
7963 err = get_merge_commit_ref_name(&commit_refname, worktree);
7964 if (err)
7965 goto done;
7966 err = delete_ref(commit_refname, repo);
7967 if (err)
7968 goto done;
7970 done:
7971 free(branch_refname);
7972 free(commit_refname);
7973 return err;
7976 struct merge_commit_msg_arg {
7977 struct got_worktree *worktree;
7978 const char *branch_name;
7981 static const struct got_error *
7982 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
7983 const char *diff_path, char **logmsg, void *arg)
7985 struct merge_commit_msg_arg *a = arg;
7987 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7988 got_worktree_get_head_ref_name(a->worktree)) == -1)
7989 return got_error_from_errno("asprintf");
7991 return NULL;
7995 const struct got_error *
7996 got_worktree_merge_branch(struct got_worktree *worktree,
7997 struct got_fileindex *fileindex,
7998 struct got_object_id *yca_commit_id,
7999 struct got_object_id *branch_tip,
8000 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
8001 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
8003 const struct got_error *err;
8004 char *fileindex_path = NULL;
8006 err = get_fileindex_path(&fileindex_path, worktree);
8007 if (err)
8008 goto done;
8010 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
8011 worktree);
8012 if (err)
8013 goto done;
8015 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
8016 branch_tip, repo, progress_cb, progress_arg,
8017 cancel_cb, cancel_arg);
8018 done:
8019 free(fileindex_path);
8020 return err;
8023 const struct got_error *
8024 got_worktree_merge_commit(struct got_object_id **new_commit_id,
8025 struct got_worktree *worktree, struct got_fileindex *fileindex,
8026 const char *author, const char *committer, int allow_bad_symlinks,
8027 struct got_object_id *branch_tip, const char *branch_name,
8028 int allow_conflict, struct got_repository *repo,
8029 got_worktree_status_cb status_cb, void *status_arg)
8032 const struct got_error *err = NULL, *sync_err;
8033 struct got_pathlist_head commitable_paths;
8034 struct collect_commitables_arg cc_arg;
8035 struct got_pathlist_entry *pe;
8036 struct got_reference *head_ref = NULL;
8037 struct got_object_id *head_commit_id = NULL;
8038 int have_staged_files = 0;
8039 struct merge_commit_msg_arg mcm_arg;
8040 char *fileindex_path = NULL;
8042 memset(&cc_arg, 0, sizeof(cc_arg));
8043 *new_commit_id = NULL;
8045 TAILQ_INIT(&commitable_paths);
8047 err = get_fileindex_path(&fileindex_path, worktree);
8048 if (err)
8049 goto done;
8051 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
8052 if (err)
8053 goto done;
8055 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8056 if (err)
8057 goto done;
8059 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
8060 &have_staged_files);
8061 if (err && err->code != GOT_ERR_CANCELLED)
8062 goto done;
8063 if (have_staged_files) {
8064 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
8065 goto done;
8068 cc_arg.commitable_paths = &commitable_paths;
8069 cc_arg.worktree = worktree;
8070 cc_arg.fileindex = fileindex;
8071 cc_arg.repo = repo;
8072 cc_arg.have_staged_files = have_staged_files;
8073 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
8074 cc_arg.commit_conflicts = allow_conflict;
8075 err = worktree_status(worktree, "", fileindex, repo,
8076 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8077 if (err)
8078 goto done;
8080 if (TAILQ_EMPTY(&commitable_paths)) {
8081 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
8082 "merge of %s cannot proceed", branch_name);
8083 goto done;
8086 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8087 struct got_commitable *ct = pe->data;
8088 const char *ct_path = ct->in_repo_path;
8090 while (ct_path[0] == '/')
8091 ct_path++;
8092 err = check_out_of_date(ct_path, ct->status,
8093 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
8094 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
8095 if (err)
8096 goto done;
8100 mcm_arg.worktree = worktree;
8101 mcm_arg.branch_name = branch_name;
8102 err = commit_worktree(new_commit_id, &commitable_paths,
8103 head_commit_id, branch_tip, worktree, author, committer, NULL,
8104 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8105 if (err)
8106 goto done;
8108 err = update_fileindex_after_commit(worktree, &commitable_paths,
8109 *new_commit_id, fileindex, have_staged_files);
8110 sync_err = sync_fileindex(fileindex, fileindex_path);
8111 if (sync_err && err == NULL)
8112 err = sync_err;
8113 done:
8114 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8115 struct got_commitable *ct = pe->data;
8117 free_commitable(ct);
8119 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8120 free(fileindex_path);
8121 return err;
8124 const struct got_error *
8125 got_worktree_merge_complete(struct got_worktree *worktree,
8126 struct got_fileindex *fileindex, struct got_repository *repo)
8128 const struct got_error *err, *unlockerr, *sync_err;
8129 char *fileindex_path = NULL;
8131 err = delete_merge_refs(worktree, repo);
8132 if (err)
8133 goto done;
8135 err = get_fileindex_path(&fileindex_path, worktree);
8136 if (err)
8137 goto done;
8138 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8139 sync_err = sync_fileindex(fileindex, fileindex_path);
8140 if (sync_err && err == NULL)
8141 err = sync_err;
8142 done:
8143 got_fileindex_free(fileindex);
8144 free(fileindex_path);
8145 unlockerr = lock_worktree(worktree, LOCK_SH);
8146 if (unlockerr && err == NULL)
8147 err = unlockerr;
8148 return err;
8151 const struct got_error *
8152 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8153 struct got_repository *repo)
8155 const struct got_error *err;
8156 char *branch_refname = NULL;
8157 struct got_reference *branch_ref = NULL;
8159 *in_progress = 0;
8161 err = get_merge_branch_ref_name(&branch_refname, worktree);
8162 if (err)
8163 return err;
8164 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8165 free(branch_refname);
8166 if (err) {
8167 if (err->code != GOT_ERR_NOT_REF)
8168 return err;
8169 } else
8170 *in_progress = 1;
8172 return NULL;
8175 const struct got_error *got_worktree_merge_prepare(
8176 struct got_fileindex **fileindex, struct got_worktree *worktree,
8177 struct got_reference *branch, struct got_repository *repo)
8179 const struct got_error *err = NULL;
8180 char *fileindex_path = NULL;
8181 char *branch_refname = NULL, *commit_refname = NULL;
8182 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
8183 struct got_reference *commit_ref = NULL;
8184 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
8185 struct check_rebase_ok_arg ok_arg;
8187 *fileindex = NULL;
8189 err = lock_worktree(worktree, LOCK_EX);
8190 if (err)
8191 return err;
8193 err = open_fileindex(fileindex, &fileindex_path, worktree);
8194 if (err)
8195 goto done;
8197 /* Preconditions are the same as for rebase. */
8198 ok_arg.worktree = worktree;
8199 ok_arg.repo = repo;
8200 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8201 &ok_arg);
8202 if (err)
8203 goto done;
8205 err = get_merge_branch_ref_name(&branch_refname, worktree);
8206 if (err)
8207 return err;
8209 err = get_merge_commit_ref_name(&commit_refname, worktree);
8210 if (err)
8211 return err;
8213 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8214 0);
8215 if (err)
8216 goto done;
8218 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8219 if (err)
8220 goto done;
8222 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8223 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8224 goto done;
8227 err = got_ref_resolve(&branch_tip, repo, branch);
8228 if (err)
8229 goto done;
8231 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8232 if (err)
8233 goto done;
8234 err = got_ref_write(branch_ref, repo);
8235 if (err)
8236 goto done;
8238 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8239 if (err)
8240 goto done;
8241 err = got_ref_write(commit_ref, repo);
8242 if (err)
8243 goto done;
8245 done:
8246 free(branch_refname);
8247 free(commit_refname);
8248 free(fileindex_path);
8249 if (branch_ref)
8250 got_ref_close(branch_ref);
8251 if (commit_ref)
8252 got_ref_close(commit_ref);
8253 if (wt_branch)
8254 got_ref_close(wt_branch);
8255 free(wt_branch_tip);
8256 if (err) {
8257 if (*fileindex) {
8258 got_fileindex_free(*fileindex);
8259 *fileindex = NULL;
8261 lock_worktree(worktree, LOCK_SH);
8263 return err;
8266 const struct got_error *
8267 got_worktree_merge_continue(char **branch_name,
8268 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8269 struct got_worktree *worktree, struct got_repository *repo)
8271 const struct got_error *err;
8272 char *commit_refname = NULL, *branch_refname = NULL;
8273 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8274 char *fileindex_path = NULL;
8275 int have_staged_files = 0;
8277 *branch_name = NULL;
8278 *branch_tip = NULL;
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 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8290 &have_staged_files);
8291 if (err && err->code != GOT_ERR_CANCELLED)
8292 goto done;
8293 if (have_staged_files) {
8294 err = got_error(GOT_ERR_STAGED_PATHS);
8295 goto done;
8298 err = get_merge_branch_ref_name(&branch_refname, worktree);
8299 if (err)
8300 goto done;
8302 err = get_merge_commit_ref_name(&commit_refname, worktree);
8303 if (err)
8304 goto done;
8306 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8307 if (err)
8308 goto done;
8310 if (!got_ref_is_symbolic(branch_ref)) {
8311 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8312 "%s is not a symbolic reference",
8313 got_ref_get_name(branch_ref));
8314 goto done;
8316 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8317 if (*branch_name == NULL) {
8318 err = got_error_from_errno("strdup");
8319 goto done;
8322 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8323 if (err)
8324 goto done;
8326 err = got_ref_resolve(branch_tip, repo, commit_ref);
8327 if (err)
8328 goto done;
8329 done:
8330 free(commit_refname);
8331 free(branch_refname);
8332 free(fileindex_path);
8333 if (commit_ref)
8334 got_ref_close(commit_ref);
8335 if (branch_ref)
8336 got_ref_close(branch_ref);
8337 if (err) {
8338 if (*branch_name) {
8339 free(*branch_name);
8340 *branch_name = NULL;
8342 free(*branch_tip);
8343 *branch_tip = NULL;
8344 if (*fileindex) {
8345 got_fileindex_free(*fileindex);
8346 *fileindex = NULL;
8348 lock_worktree(worktree, LOCK_SH);
8350 return err;
8353 const struct got_error *
8354 got_worktree_merge_abort(struct got_worktree *worktree,
8355 struct got_fileindex *fileindex, struct got_repository *repo,
8356 got_worktree_checkout_cb progress_cb, void *progress_arg)
8358 const struct got_error *err, *unlockerr, *sync_err;
8359 struct got_object_id *commit_id = NULL;
8360 struct got_commit_object *commit = NULL;
8361 char *fileindex_path = NULL;
8362 struct revert_file_args rfa;
8363 struct got_object_id *tree_id = NULL;
8365 err = got_object_open_as_commit(&commit, repo,
8366 worktree->base_commit_id);
8367 if (err)
8368 goto done;
8370 err = got_object_id_by_path(&tree_id, repo, commit,
8371 worktree->path_prefix);
8372 if (err)
8373 goto done;
8375 err = delete_merge_refs(worktree, repo);
8376 if (err)
8377 goto done;
8379 err = get_fileindex_path(&fileindex_path, worktree);
8380 if (err)
8381 goto done;
8383 rfa.worktree = worktree;
8384 rfa.fileindex = fileindex;
8385 rfa.progress_cb = progress_cb;
8386 rfa.progress_arg = progress_arg;
8387 rfa.patch_cb = NULL;
8388 rfa.patch_arg = NULL;
8389 rfa.repo = repo;
8390 rfa.unlink_added_files = 1;
8391 err = worktree_status(worktree, "", fileindex, repo,
8392 revert_file, &rfa, NULL, NULL, 1, 0);
8393 if (err)
8394 goto sync;
8396 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8397 repo, progress_cb, progress_arg, NULL, NULL);
8398 sync:
8399 sync_err = sync_fileindex(fileindex, fileindex_path);
8400 if (sync_err && err == NULL)
8401 err = sync_err;
8402 done:
8403 free(tree_id);
8404 free(commit_id);
8405 if (commit)
8406 got_object_commit_close(commit);
8407 if (fileindex)
8408 got_fileindex_free(fileindex);
8409 free(fileindex_path);
8411 unlockerr = lock_worktree(worktree, LOCK_SH);
8412 if (unlockerr && err == NULL)
8413 err = unlockerr;
8414 return err;
8417 struct check_stage_ok_arg {
8418 struct got_object_id *head_commit_id;
8419 struct got_worktree *worktree;
8420 struct got_fileindex *fileindex;
8421 struct got_repository *repo;
8422 int have_changes;
8425 static const struct got_error *
8426 check_stage_ok(void *arg, unsigned char status,
8427 unsigned char staged_status, const char *relpath,
8428 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8429 struct got_object_id *commit_id, int dirfd, const char *de_name)
8431 struct check_stage_ok_arg *a = arg;
8432 const struct got_error *err = NULL;
8433 struct got_fileindex_entry *ie;
8434 struct got_object_id base_commit_id;
8435 struct got_object_id *base_commit_idp = NULL;
8436 char *in_repo_path = NULL, *p;
8438 if (status == GOT_STATUS_UNVERSIONED ||
8439 status == GOT_STATUS_NO_CHANGE)
8440 return NULL;
8441 if (status == GOT_STATUS_NONEXISTENT)
8442 return got_error_set_errno(ENOENT, relpath);
8444 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8445 if (ie == NULL)
8446 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8448 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8449 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8450 relpath) == -1)
8451 return got_error_from_errno("asprintf");
8453 if (got_fileindex_entry_has_commit(ie)) {
8454 base_commit_idp = got_fileindex_entry_get_commit_id(
8455 &base_commit_id, ie);
8458 if (status == GOT_STATUS_CONFLICT) {
8459 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8460 goto done;
8461 } else if (status != GOT_STATUS_ADD &&
8462 status != GOT_STATUS_MODIFY &&
8463 status != GOT_STATUS_DELETE) {
8464 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8465 goto done;
8468 a->have_changes = 1;
8470 p = in_repo_path;
8471 while (p[0] == '/')
8472 p++;
8473 err = check_out_of_date(p, status, staged_status,
8474 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8475 GOT_ERR_STAGE_OUT_OF_DATE);
8476 done:
8477 free(in_repo_path);
8478 return err;
8481 struct stage_path_arg {
8482 struct got_worktree *worktree;
8483 struct got_fileindex *fileindex;
8484 struct got_repository *repo;
8485 got_worktree_status_cb status_cb;
8486 void *status_arg;
8487 got_worktree_patch_cb patch_cb;
8488 void *patch_arg;
8489 int staged_something;
8490 int allow_bad_symlinks;
8493 static const struct got_error *
8494 stage_path(void *arg, unsigned char status,
8495 unsigned char staged_status, const char *relpath,
8496 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8497 struct got_object_id *commit_id, int dirfd, const char *de_name)
8499 struct stage_path_arg *a = arg;
8500 const struct got_error *err = NULL;
8501 struct got_fileindex_entry *ie;
8502 char *ondisk_path = NULL, *path_content = NULL;
8503 uint32_t stage;
8504 struct got_object_id *new_staged_blob_id = NULL;
8505 struct stat sb;
8507 if (status == GOT_STATUS_UNVERSIONED)
8508 return NULL;
8510 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8511 if (ie == NULL)
8512 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8514 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8515 relpath)== -1)
8516 return got_error_from_errno("asprintf");
8518 switch (status) {
8519 case GOT_STATUS_ADD:
8520 case GOT_STATUS_MODIFY:
8521 /* XXX could sb.st_mode be passed in by our caller? */
8522 if (lstat(ondisk_path, &sb) == -1) {
8523 err = got_error_from_errno2("lstat", ondisk_path);
8524 break;
8526 if (a->patch_cb) {
8527 if (status == GOT_STATUS_ADD) {
8528 int choice = GOT_PATCH_CHOICE_NONE;
8529 err = (*a->patch_cb)(&choice, a->patch_arg,
8530 status, ie->path, NULL, 1, 1);
8531 if (err)
8532 break;
8533 if (choice != GOT_PATCH_CHOICE_YES)
8534 break;
8535 } else {
8536 err = create_patched_content(&path_content, 0,
8537 staged_blob_id ? staged_blob_id : blob_id,
8538 ondisk_path, dirfd, de_name, ie->path,
8539 a->repo, a->patch_cb, a->patch_arg);
8540 if (err || path_content == NULL)
8541 break;
8544 err = got_object_blob_create(&new_staged_blob_id,
8545 path_content ? path_content : ondisk_path, a->repo);
8546 if (err)
8547 break;
8548 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8549 SHA1_DIGEST_LENGTH);
8550 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8551 stage = GOT_FILEIDX_STAGE_ADD;
8552 else
8553 stage = GOT_FILEIDX_STAGE_MODIFY;
8554 got_fileindex_entry_stage_set(ie, stage);
8555 if (S_ISLNK(sb.st_mode)) {
8556 int is_bad_symlink = 0;
8557 if (!a->allow_bad_symlinks) {
8558 char target_path[PATH_MAX];
8559 ssize_t target_len;
8560 target_len = readlink(ondisk_path, target_path,
8561 sizeof(target_path));
8562 if (target_len == -1) {
8563 err = got_error_from_errno2("readlink",
8564 ondisk_path);
8565 break;
8567 err = is_bad_symlink_target(&is_bad_symlink,
8568 target_path, target_len, ondisk_path,
8569 a->worktree->root_path);
8570 if (err)
8571 break;
8572 if (is_bad_symlink) {
8573 err = got_error_path(ondisk_path,
8574 GOT_ERR_BAD_SYMLINK);
8575 break;
8578 if (is_bad_symlink)
8579 got_fileindex_entry_staged_filetype_set(ie,
8580 GOT_FILEIDX_MODE_BAD_SYMLINK);
8581 else
8582 got_fileindex_entry_staged_filetype_set(ie,
8583 GOT_FILEIDX_MODE_SYMLINK);
8584 } else {
8585 got_fileindex_entry_staged_filetype_set(ie,
8586 GOT_FILEIDX_MODE_REGULAR_FILE);
8588 a->staged_something = 1;
8589 if (a->status_cb == NULL)
8590 break;
8591 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8592 get_staged_status(ie), relpath, blob_id,
8593 new_staged_blob_id, NULL, dirfd, de_name);
8594 if (err)
8595 break;
8597 * When staging the reverse of the staged diff,
8598 * implicitly unstage the file.
8600 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8601 sizeof(ie->blob_sha1)) == 0) {
8602 got_fileindex_entry_stage_set(ie,
8603 GOT_FILEIDX_STAGE_NONE);
8605 break;
8606 case GOT_STATUS_DELETE:
8607 if (staged_status == GOT_STATUS_DELETE)
8608 break;
8609 if (a->patch_cb) {
8610 int choice = GOT_PATCH_CHOICE_NONE;
8611 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8612 ie->path, NULL, 1, 1);
8613 if (err)
8614 break;
8615 if (choice == GOT_PATCH_CHOICE_NO)
8616 break;
8617 if (choice != GOT_PATCH_CHOICE_YES) {
8618 err = got_error(GOT_ERR_PATCH_CHOICE);
8619 break;
8622 stage = GOT_FILEIDX_STAGE_DELETE;
8623 got_fileindex_entry_stage_set(ie, stage);
8624 a->staged_something = 1;
8625 if (a->status_cb == NULL)
8626 break;
8627 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8628 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8629 de_name);
8630 break;
8631 case GOT_STATUS_NO_CHANGE:
8632 break;
8633 case GOT_STATUS_CONFLICT:
8634 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8635 break;
8636 case GOT_STATUS_NONEXISTENT:
8637 err = got_error_set_errno(ENOENT, relpath);
8638 break;
8639 default:
8640 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8641 break;
8644 if (path_content && unlink(path_content) == -1 && err == NULL)
8645 err = got_error_from_errno2("unlink", path_content);
8646 free(path_content);
8647 free(ondisk_path);
8648 free(new_staged_blob_id);
8649 return err;
8652 const struct got_error *
8653 got_worktree_stage(struct got_worktree *worktree,
8654 struct got_pathlist_head *paths,
8655 got_worktree_status_cb status_cb, void *status_arg,
8656 got_worktree_patch_cb patch_cb, void *patch_arg,
8657 int allow_bad_symlinks, struct got_repository *repo)
8659 const struct got_error *err = NULL, *sync_err, *unlockerr;
8660 struct got_pathlist_entry *pe;
8661 struct got_fileindex *fileindex = NULL;
8662 char *fileindex_path = NULL;
8663 struct got_reference *head_ref = NULL;
8664 struct got_object_id *head_commit_id = NULL;
8665 struct check_stage_ok_arg oka;
8666 struct stage_path_arg spa;
8668 err = lock_worktree(worktree, LOCK_EX);
8669 if (err)
8670 return err;
8672 err = got_ref_open(&head_ref, repo,
8673 got_worktree_get_head_ref_name(worktree), 0);
8674 if (err)
8675 goto done;
8676 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8677 if (err)
8678 goto done;
8679 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8680 if (err)
8681 goto done;
8683 /* Check pre-conditions before staging anything. */
8684 oka.head_commit_id = head_commit_id;
8685 oka.worktree = worktree;
8686 oka.fileindex = fileindex;
8687 oka.repo = repo;
8688 oka.have_changes = 0;
8689 TAILQ_FOREACH(pe, paths, entry) {
8690 err = worktree_status(worktree, pe->path, fileindex, repo,
8691 check_stage_ok, &oka, NULL, NULL, 1, 0);
8692 if (err)
8693 goto done;
8695 if (!oka.have_changes) {
8696 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8697 goto done;
8700 spa.worktree = worktree;
8701 spa.fileindex = fileindex;
8702 spa.repo = repo;
8703 spa.patch_cb = patch_cb;
8704 spa.patch_arg = patch_arg;
8705 spa.status_cb = status_cb;
8706 spa.status_arg = status_arg;
8707 spa.staged_something = 0;
8708 spa.allow_bad_symlinks = allow_bad_symlinks;
8709 TAILQ_FOREACH(pe, paths, entry) {
8710 err = worktree_status(worktree, pe->path, fileindex, repo,
8711 stage_path, &spa, NULL, NULL, 1, 0);
8712 if (err)
8713 goto done;
8715 if (!spa.staged_something) {
8716 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8717 goto done;
8720 sync_err = sync_fileindex(fileindex, fileindex_path);
8721 if (sync_err && err == NULL)
8722 err = sync_err;
8723 done:
8724 if (head_ref)
8725 got_ref_close(head_ref);
8726 free(head_commit_id);
8727 free(fileindex_path);
8728 if (fileindex)
8729 got_fileindex_free(fileindex);
8730 unlockerr = lock_worktree(worktree, LOCK_SH);
8731 if (unlockerr && err == NULL)
8732 err = unlockerr;
8733 return err;
8736 struct unstage_path_arg {
8737 struct got_worktree *worktree;
8738 struct got_fileindex *fileindex;
8739 struct got_repository *repo;
8740 got_worktree_checkout_cb progress_cb;
8741 void *progress_arg;
8742 got_worktree_patch_cb patch_cb;
8743 void *patch_arg;
8746 static const struct got_error *
8747 create_unstaged_content(char **path_unstaged_content,
8748 char **path_new_staged_content, struct got_object_id *blob_id,
8749 struct got_object_id *staged_blob_id, const char *relpath,
8750 struct got_repository *repo,
8751 got_worktree_patch_cb patch_cb, void *patch_arg)
8753 const struct got_error *err, *free_err;
8754 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8755 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8756 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8757 struct got_diffreg_result *diffreg_result = NULL;
8758 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8759 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8760 int fd1 = -1, fd2 = -1;
8762 *path_unstaged_content = NULL;
8763 *path_new_staged_content = NULL;
8765 err = got_object_id_str(&label1, blob_id);
8766 if (err)
8767 return err;
8769 fd1 = got_opentempfd();
8770 if (fd1 == -1) {
8771 err = got_error_from_errno("got_opentempfd");
8772 goto done;
8774 fd2 = got_opentempfd();
8775 if (fd2 == -1) {
8776 err = got_error_from_errno("got_opentempfd");
8777 goto done;
8780 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8781 if (err)
8782 goto done;
8784 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
8785 if (err)
8786 goto done;
8788 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8789 if (err)
8790 goto done;
8792 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8793 fd2);
8794 if (err)
8795 goto done;
8797 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
8798 if (err)
8799 goto done;
8801 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8802 if (err)
8803 goto done;
8805 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8806 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8807 if (err)
8808 goto done;
8810 err = got_opentemp_named(path_unstaged_content, &outfile,
8811 "got-unstaged-content", "");
8812 if (err)
8813 goto done;
8814 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8815 "got-new-staged-content", "");
8816 if (err)
8817 goto done;
8819 if (fseek(f1, 0L, SEEK_SET) == -1) {
8820 err = got_ferror(f1, GOT_ERR_IO);
8821 goto done;
8823 if (fseek(f2, 0L, SEEK_SET) == -1) {
8824 err = got_ferror(f2, GOT_ERR_IO);
8825 goto done;
8827 /* Count the number of actual changes in the diff result. */
8828 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8829 struct diff_chunk_context cc = {};
8830 diff_chunk_context_load_change(&cc, &nchunks_used,
8831 diffreg_result->result, n, 0);
8832 nchanges++;
8834 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8835 int choice;
8836 err = apply_or_reject_change(&choice, &nchunks_used,
8837 diffreg_result->result, n, relpath, f1, f2,
8838 &line_cur1, &line_cur2,
8839 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8840 if (err)
8841 goto done;
8842 if (choice == GOT_PATCH_CHOICE_YES)
8843 have_content = 1;
8844 else
8845 have_rejected_content = 1;
8846 if (choice == GOT_PATCH_CHOICE_QUIT)
8847 break;
8849 if (have_content || have_rejected_content)
8850 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8851 outfile, rejectfile);
8852 done:
8853 free(label1);
8854 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8855 err = got_error_from_errno("close");
8856 if (blob)
8857 got_object_blob_close(blob);
8858 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8859 err = got_error_from_errno("close");
8860 if (staged_blob)
8861 got_object_blob_close(staged_blob);
8862 free_err = got_diffreg_result_free(diffreg_result);
8863 if (free_err && err == NULL)
8864 err = free_err;
8865 if (f1 && fclose(f1) == EOF && err == NULL)
8866 err = got_error_from_errno2("fclose", path1);
8867 if (f2 && fclose(f2) == EOF && err == NULL)
8868 err = got_error_from_errno2("fclose", path2);
8869 if (outfile && fclose(outfile) == EOF && err == NULL)
8870 err = got_error_from_errno2("fclose", *path_unstaged_content);
8871 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8872 err = got_error_from_errno2("fclose", *path_new_staged_content);
8873 if (path1 && unlink(path1) == -1 && err == NULL)
8874 err = got_error_from_errno2("unlink", path1);
8875 if (path2 && unlink(path2) == -1 && err == NULL)
8876 err = got_error_from_errno2("unlink", path2);
8877 if (err || !have_content) {
8878 if (*path_unstaged_content &&
8879 unlink(*path_unstaged_content) == -1 && err == NULL)
8880 err = got_error_from_errno2("unlink",
8881 *path_unstaged_content);
8882 free(*path_unstaged_content);
8883 *path_unstaged_content = NULL;
8885 if (err || !have_content || !have_rejected_content) {
8886 if (*path_new_staged_content &&
8887 unlink(*path_new_staged_content) == -1 && err == NULL)
8888 err = got_error_from_errno2("unlink",
8889 *path_new_staged_content);
8890 free(*path_new_staged_content);
8891 *path_new_staged_content = NULL;
8893 free(path1);
8894 free(path2);
8895 return err;
8898 static const struct got_error *
8899 unstage_hunks(struct got_object_id *staged_blob_id,
8900 struct got_blob_object *blob_base,
8901 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8902 const char *ondisk_path, const char *label_orig,
8903 struct got_worktree *worktree, struct got_repository *repo,
8904 got_worktree_patch_cb patch_cb, void *patch_arg,
8905 got_worktree_checkout_cb progress_cb, void *progress_arg)
8907 const struct got_error *err = NULL;
8908 char *path_unstaged_content = NULL;
8909 char *path_new_staged_content = NULL;
8910 char *parent = NULL, *base_path = NULL;
8911 char *blob_base_path = NULL;
8912 struct got_object_id *new_staged_blob_id = NULL;
8913 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8914 struct stat sb;
8916 err = create_unstaged_content(&path_unstaged_content,
8917 &path_new_staged_content, blob_id, staged_blob_id,
8918 ie->path, repo, patch_cb, patch_arg);
8919 if (err)
8920 return err;
8922 if (path_unstaged_content == NULL)
8923 return NULL;
8925 if (path_new_staged_content) {
8926 err = got_object_blob_create(&new_staged_blob_id,
8927 path_new_staged_content, repo);
8928 if (err)
8929 goto done;
8932 f = fopen(path_unstaged_content, "re");
8933 if (f == NULL) {
8934 err = got_error_from_errno2("fopen",
8935 path_unstaged_content);
8936 goto done;
8938 if (fstat(fileno(f), &sb) == -1) {
8939 err = got_error_from_errno2("fstat", path_unstaged_content);
8940 goto done;
8942 if (got_fileindex_entry_staged_filetype_get(ie) ==
8943 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8944 char link_target[PATH_MAX];
8945 size_t r;
8946 r = fread(link_target, 1, sizeof(link_target), f);
8947 if (r == 0 && ferror(f)) {
8948 err = got_error_from_errno("fread");
8949 goto done;
8951 if (r >= sizeof(link_target)) { /* should not happen */
8952 err = got_error(GOT_ERR_NO_SPACE);
8953 goto done;
8955 link_target[r] = '\0';
8956 err = merge_symlink(worktree, blob_base,
8957 ondisk_path, ie->path, label_orig, link_target,
8958 worktree->base_commit_id, repo, progress_cb,
8959 progress_arg);
8960 } else {
8961 int local_changes_subsumed;
8963 err = got_path_dirname(&parent, ondisk_path);
8964 if (err)
8965 return err;
8967 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8968 parent) == -1) {
8969 err = got_error_from_errno("asprintf");
8970 base_path = NULL;
8971 goto done;
8974 err = got_opentemp_named(&blob_base_path, &f_base,
8975 base_path, "");
8976 if (err)
8977 goto done;
8978 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8979 blob_base);
8980 if (err)
8981 goto done;
8984 * In order the run a 3-way merge with a symlink we copy the symlink's
8985 * target path into a temporary file and use that file with diff3.
8987 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8988 err = dump_symlink_target_path_to_file(&f_deriv2,
8989 ondisk_path);
8990 if (err)
8991 goto done;
8992 } else {
8993 int fd;
8994 fd = open(ondisk_path,
8995 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8996 if (fd == -1) {
8997 err = got_error_from_errno2("open", ondisk_path);
8998 goto done;
9000 f_deriv2 = fdopen(fd, "r");
9001 if (f_deriv2 == NULL) {
9002 err = got_error_from_errno2("fdopen", ondisk_path);
9003 close(fd);
9004 goto done;
9008 err = merge_file(&local_changes_subsumed, worktree,
9009 f_base, f, f_deriv2, ondisk_path, ie->path,
9010 got_fileindex_perms_to_st(ie),
9011 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
9012 repo, progress_cb, progress_arg);
9014 if (err)
9015 goto done;
9017 if (new_staged_blob_id) {
9018 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9019 SHA1_DIGEST_LENGTH);
9020 } else {
9021 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9022 got_fileindex_entry_staged_filetype_set(ie, 0);
9024 done:
9025 free(new_staged_blob_id);
9026 if (path_unstaged_content &&
9027 unlink(path_unstaged_content) == -1 && err == NULL)
9028 err = got_error_from_errno2("unlink", path_unstaged_content);
9029 if (path_new_staged_content &&
9030 unlink(path_new_staged_content) == -1 && err == NULL)
9031 err = got_error_from_errno2("unlink", path_new_staged_content);
9032 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
9033 err = got_error_from_errno2("unlink", blob_base_path);
9034 if (f_base && fclose(f_base) == EOF && err == NULL)
9035 err = got_error_from_errno2("fclose", path_unstaged_content);
9036 if (f && fclose(f) == EOF && err == NULL)
9037 err = got_error_from_errno2("fclose", path_unstaged_content);
9038 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
9039 err = got_error_from_errno2("fclose", ondisk_path);
9040 free(path_unstaged_content);
9041 free(path_new_staged_content);
9042 free(blob_base_path);
9043 free(parent);
9044 free(base_path);
9045 return err;
9048 static const struct got_error *
9049 unstage_path(void *arg, unsigned char status,
9050 unsigned char staged_status, const char *relpath,
9051 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9052 struct got_object_id *commit_id, int dirfd, const char *de_name)
9054 const struct got_error *err = NULL;
9055 struct unstage_path_arg *a = arg;
9056 struct got_fileindex_entry *ie;
9057 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
9058 char *ondisk_path = NULL;
9059 char *id_str = NULL, *label_orig = NULL;
9060 int local_changes_subsumed;
9061 struct stat sb;
9062 int fd1 = -1, fd2 = -1;
9064 if (staged_status != GOT_STATUS_ADD &&
9065 staged_status != GOT_STATUS_MODIFY &&
9066 staged_status != GOT_STATUS_DELETE)
9067 return NULL;
9069 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9070 if (ie == NULL)
9071 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9073 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9074 == -1)
9075 return got_error_from_errno("asprintf");
9077 err = got_object_id_str(&id_str,
9078 commit_id ? commit_id : a->worktree->base_commit_id);
9079 if (err)
9080 goto done;
9081 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9082 id_str) == -1) {
9083 err = got_error_from_errno("asprintf");
9084 goto done;
9087 fd1 = got_opentempfd();
9088 if (fd1 == -1) {
9089 err = got_error_from_errno("got_opentempfd");
9090 goto done;
9092 fd2 = got_opentempfd();
9093 if (fd2 == -1) {
9094 err = got_error_from_errno("got_opentempfd");
9095 goto done;
9098 switch (staged_status) {
9099 case GOT_STATUS_MODIFY:
9100 err = got_object_open_as_blob(&blob_base, a->repo,
9101 blob_id, 8192, fd1);
9102 if (err)
9103 break;
9104 /* fall through */
9105 case GOT_STATUS_ADD:
9106 if (a->patch_cb) {
9107 if (staged_status == GOT_STATUS_ADD) {
9108 int choice = GOT_PATCH_CHOICE_NONE;
9109 err = (*a->patch_cb)(&choice, a->patch_arg,
9110 staged_status, ie->path, NULL, 1, 1);
9111 if (err)
9112 break;
9113 if (choice != GOT_PATCH_CHOICE_YES)
9114 break;
9115 } else {
9116 err = unstage_hunks(staged_blob_id,
9117 blob_base, blob_id, ie, ondisk_path,
9118 label_orig, a->worktree, a->repo,
9119 a->patch_cb, a->patch_arg,
9120 a->progress_cb, a->progress_arg);
9121 break; /* Done with this file. */
9124 err = got_object_open_as_blob(&blob_staged, a->repo,
9125 staged_blob_id, 8192, fd2);
9126 if (err)
9127 break;
9128 switch (got_fileindex_entry_staged_filetype_get(ie)) {
9129 case GOT_FILEIDX_MODE_BAD_SYMLINK:
9130 case GOT_FILEIDX_MODE_REGULAR_FILE:
9131 err = merge_blob(&local_changes_subsumed, a->worktree,
9132 blob_base, ondisk_path, relpath,
9133 got_fileindex_perms_to_st(ie), label_orig,
9134 blob_staged, commit_id ? commit_id :
9135 a->worktree->base_commit_id, a->repo,
9136 a->progress_cb, a->progress_arg);
9137 break;
9138 case GOT_FILEIDX_MODE_SYMLINK:
9139 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9140 char *staged_target;
9141 err = got_object_blob_read_to_str(
9142 &staged_target, blob_staged);
9143 if (err)
9144 goto done;
9145 err = merge_symlink(a->worktree, blob_base,
9146 ondisk_path, relpath, label_orig,
9147 staged_target, commit_id ? commit_id :
9148 a->worktree->base_commit_id,
9149 a->repo, a->progress_cb, a->progress_arg);
9150 free(staged_target);
9151 } else {
9152 err = merge_blob(&local_changes_subsumed,
9153 a->worktree, blob_base, ondisk_path,
9154 relpath, got_fileindex_perms_to_st(ie),
9155 label_orig, blob_staged,
9156 commit_id ? commit_id :
9157 a->worktree->base_commit_id, a->repo,
9158 a->progress_cb, a->progress_arg);
9160 break;
9161 default:
9162 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9163 break;
9165 if (err == NULL) {
9166 got_fileindex_entry_stage_set(ie,
9167 GOT_FILEIDX_STAGE_NONE);
9168 got_fileindex_entry_staged_filetype_set(ie, 0);
9170 break;
9171 case GOT_STATUS_DELETE:
9172 if (a->patch_cb) {
9173 int choice = GOT_PATCH_CHOICE_NONE;
9174 err = (*a->patch_cb)(&choice, a->patch_arg,
9175 staged_status, ie->path, NULL, 1, 1);
9176 if (err)
9177 break;
9178 if (choice == GOT_PATCH_CHOICE_NO)
9179 break;
9180 if (choice != GOT_PATCH_CHOICE_YES) {
9181 err = got_error(GOT_ERR_PATCH_CHOICE);
9182 break;
9185 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9186 got_fileindex_entry_staged_filetype_set(ie, 0);
9187 err = get_file_status(&status, &sb, ie, ondisk_path,
9188 dirfd, de_name, a->repo);
9189 if (err)
9190 break;
9191 err = (*a->progress_cb)(a->progress_arg, status, relpath);
9192 break;
9194 done:
9195 free(ondisk_path);
9196 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9197 err = got_error_from_errno("close");
9198 if (blob_base)
9199 got_object_blob_close(blob_base);
9200 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9201 err = got_error_from_errno("close");
9202 if (blob_staged)
9203 got_object_blob_close(blob_staged);
9204 free(id_str);
9205 free(label_orig);
9206 return err;
9209 const struct got_error *
9210 got_worktree_unstage(struct got_worktree *worktree,
9211 struct got_pathlist_head *paths,
9212 got_worktree_checkout_cb progress_cb, void *progress_arg,
9213 got_worktree_patch_cb patch_cb, void *patch_arg,
9214 struct got_repository *repo)
9216 const struct got_error *err = NULL, *sync_err, *unlockerr;
9217 struct got_pathlist_entry *pe;
9218 struct got_fileindex *fileindex = NULL;
9219 char *fileindex_path = NULL;
9220 struct unstage_path_arg upa;
9222 err = lock_worktree(worktree, LOCK_EX);
9223 if (err)
9224 return err;
9226 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9227 if (err)
9228 goto done;
9230 upa.worktree = worktree;
9231 upa.fileindex = fileindex;
9232 upa.repo = repo;
9233 upa.progress_cb = progress_cb;
9234 upa.progress_arg = progress_arg;
9235 upa.patch_cb = patch_cb;
9236 upa.patch_arg = patch_arg;
9237 TAILQ_FOREACH(pe, paths, entry) {
9238 err = worktree_status(worktree, pe->path, fileindex, repo,
9239 unstage_path, &upa, NULL, NULL, 1, 0);
9240 if (err)
9241 goto done;
9244 sync_err = sync_fileindex(fileindex, fileindex_path);
9245 if (sync_err && err == NULL)
9246 err = sync_err;
9247 done:
9248 free(fileindex_path);
9249 if (fileindex)
9250 got_fileindex_free(fileindex);
9251 unlockerr = lock_worktree(worktree, LOCK_SH);
9252 if (unlockerr && err == NULL)
9253 err = unlockerr;
9254 return err;
9257 struct report_file_info_arg {
9258 struct got_worktree *worktree;
9259 got_worktree_path_info_cb info_cb;
9260 void *info_arg;
9261 struct got_pathlist_head *paths;
9262 got_cancel_cb cancel_cb;
9263 void *cancel_arg;
9266 static const struct got_error *
9267 report_file_info(void *arg, struct got_fileindex_entry *ie)
9269 struct report_file_info_arg *a = arg;
9270 struct got_pathlist_entry *pe;
9271 struct got_object_id blob_id, staged_blob_id, commit_id;
9272 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9273 struct got_object_id *commit_idp = NULL;
9274 int stage;
9276 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9277 return got_error(GOT_ERR_CANCELLED);
9279 TAILQ_FOREACH(pe, a->paths, entry) {
9280 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9281 got_path_is_child(ie->path, pe->path, pe->path_len))
9282 break;
9284 if (pe == NULL) /* not found */
9285 return NULL;
9287 if (got_fileindex_entry_has_blob(ie))
9288 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9289 stage = got_fileindex_entry_stage_get(ie);
9290 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9291 stage == GOT_FILEIDX_STAGE_ADD) {
9292 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9293 &staged_blob_id, ie);
9296 if (got_fileindex_entry_has_commit(ie))
9297 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9299 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9300 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9303 const struct got_error *
9304 got_worktree_path_info(struct got_worktree *worktree,
9305 struct got_pathlist_head *paths,
9306 got_worktree_path_info_cb info_cb, void *info_arg,
9307 got_cancel_cb cancel_cb, void *cancel_arg)
9310 const struct got_error *err = NULL, *unlockerr;
9311 struct got_fileindex *fileindex = NULL;
9312 char *fileindex_path = NULL;
9313 struct report_file_info_arg arg;
9315 err = lock_worktree(worktree, LOCK_SH);
9316 if (err)
9317 return err;
9319 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9320 if (err)
9321 goto done;
9323 arg.worktree = worktree;
9324 arg.info_cb = info_cb;
9325 arg.info_arg = info_arg;
9326 arg.paths = paths;
9327 arg.cancel_cb = cancel_cb;
9328 arg.cancel_arg = cancel_arg;
9329 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9330 &arg);
9331 done:
9332 free(fileindex_path);
9333 if (fileindex)
9334 got_fileindex_free(fileindex);
9335 unlockerr = lock_worktree(worktree, LOCK_UN);
9336 if (unlockerr && err == NULL)
9337 err = unlockerr;
9338 return err;
9341 static const struct got_error *
9342 patch_check_path(const char *p, char **path, unsigned char *status,
9343 unsigned char *staged_status, struct got_fileindex *fileindex,
9344 struct got_worktree *worktree, struct got_repository *repo)
9346 const struct got_error *err;
9347 struct got_fileindex_entry *ie;
9348 struct stat sb;
9349 char *ondisk_path = NULL;
9351 err = got_worktree_resolve_path(path, worktree, p);
9352 if (err)
9353 return err;
9355 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9356 *path[0] ? "/" : "", *path) == -1)
9357 return got_error_from_errno("asprintf");
9359 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9360 if (ie) {
9361 *staged_status = get_staged_status(ie);
9362 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9363 repo);
9364 if (err)
9365 goto done;
9366 } else {
9367 *staged_status = GOT_STATUS_NO_CHANGE;
9368 *status = GOT_STATUS_UNVERSIONED;
9369 if (lstat(ondisk_path, &sb) == -1) {
9370 if (errno != ENOENT) {
9371 err = got_error_from_errno2("lstat",
9372 ondisk_path);
9373 goto done;
9375 *status = GOT_STATUS_NONEXISTENT;
9379 done:
9380 free(ondisk_path);
9381 return err;
9384 static const struct got_error *
9385 patch_can_rm(const char *path, unsigned char status,
9386 unsigned char staged_status)
9388 if (status == GOT_STATUS_NONEXISTENT)
9389 return got_error_set_errno(ENOENT, path);
9390 if (status != GOT_STATUS_NO_CHANGE &&
9391 status != GOT_STATUS_ADD &&
9392 status != GOT_STATUS_MODIFY &&
9393 status != GOT_STATUS_MODE_CHANGE)
9394 return got_error_path(path, GOT_ERR_FILE_STATUS);
9395 if (staged_status == GOT_STATUS_DELETE)
9396 return got_error_path(path, GOT_ERR_FILE_STATUS);
9397 return NULL;
9400 static const struct got_error *
9401 patch_can_add(const char *path, unsigned char status)
9403 if (status != GOT_STATUS_NONEXISTENT)
9404 return got_error_path(path, GOT_ERR_FILE_STATUS);
9405 return NULL;
9408 static const struct got_error *
9409 patch_can_edit(const char *path, unsigned char status,
9410 unsigned char staged_status)
9412 if (status == GOT_STATUS_NONEXISTENT)
9413 return got_error_set_errno(ENOENT, path);
9414 if (status != GOT_STATUS_NO_CHANGE &&
9415 status != GOT_STATUS_ADD &&
9416 status != GOT_STATUS_MODIFY)
9417 return got_error_path(path, GOT_ERR_FILE_STATUS);
9418 if (staged_status == GOT_STATUS_DELETE)
9419 return got_error_path(path, GOT_ERR_FILE_STATUS);
9420 return NULL;
9423 const struct got_error *
9424 got_worktree_patch_prepare(struct got_fileindex **fileindex,
9425 char **fileindex_path, struct got_worktree *worktree)
9427 return open_fileindex(fileindex, fileindex_path, worktree);
9430 const struct got_error *
9431 got_worktree_patch_check_path(const char *old, const char *new,
9432 char **oldpath, char **newpath, struct got_worktree *worktree,
9433 struct got_repository *repo, struct got_fileindex *fileindex)
9435 const struct got_error *err = NULL;
9436 int file_renamed = 0;
9437 unsigned char status_old, staged_status_old;
9438 unsigned char status_new, staged_status_new;
9440 *oldpath = NULL;
9441 *newpath = NULL;
9443 err = patch_check_path(old != NULL ? old : new, oldpath,
9444 &status_old, &staged_status_old, fileindex, worktree, repo);
9445 if (err)
9446 goto done;
9448 err = patch_check_path(new != NULL ? new : old, newpath,
9449 &status_new, &staged_status_new, fileindex, worktree, repo);
9450 if (err)
9451 goto done;
9453 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9454 file_renamed = 1;
9456 if (old != NULL && new == NULL)
9457 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9458 else if (file_renamed) {
9459 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9460 if (err == NULL)
9461 err = patch_can_add(*newpath, status_new);
9462 } else if (old == NULL)
9463 err = patch_can_add(*newpath, status_new);
9464 else
9465 err = patch_can_edit(*newpath, status_new, staged_status_new);
9467 done:
9468 if (err) {
9469 free(*oldpath);
9470 *oldpath = NULL;
9471 free(*newpath);
9472 *newpath = NULL;
9474 return err;
9477 const struct got_error *
9478 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9479 struct got_worktree *worktree, struct got_fileindex *fileindex,
9480 got_worktree_checkout_cb progress_cb, void *progress_arg)
9482 struct schedule_addition_args saa;
9484 memset(&saa, 0, sizeof(saa));
9485 saa.worktree = worktree;
9486 saa.fileindex = fileindex;
9487 saa.progress_cb = progress_cb;
9488 saa.progress_arg = progress_arg;
9489 saa.repo = repo;
9491 return worktree_status(worktree, path, fileindex, repo,
9492 schedule_addition, &saa, NULL, NULL, 1, 0);
9495 const struct got_error *
9496 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9497 struct got_worktree *worktree, struct got_fileindex *fileindex,
9498 got_worktree_delete_cb progress_cb, void *progress_arg)
9500 struct schedule_deletion_args sda;
9502 memset(&sda, 0, sizeof(sda));
9503 sda.worktree = worktree;
9504 sda.fileindex = fileindex;
9505 sda.progress_cb = progress_cb;
9506 sda.progress_arg = progress_arg;
9507 sda.repo = repo;
9508 sda.delete_local_mods = 0;
9509 sda.keep_on_disk = 0;
9510 sda.ignore_missing_paths = 0;
9511 sda.status_codes = NULL;
9513 return worktree_status(worktree, path, fileindex, repo,
9514 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9517 const struct got_error *
9518 got_worktree_patch_complete(struct got_fileindex *fileindex,
9519 const char *fileindex_path)
9521 const struct got_error *err = NULL;
9523 err = sync_fileindex(fileindex, fileindex_path);
9524 got_fileindex_free(fileindex);
9526 return err;