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) {
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 free(parent);
1418 if (err)
1419 return err;
1420 fd = open(ondisk_path,
1421 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1422 mode);
1423 if (fd == -1)
1424 return got_error_from_errno2("open",
1425 ondisk_path);
1426 } else if (errno == EEXIST) {
1427 if (path_is_unversioned) {
1428 err = (*progress_cb)(progress_arg,
1429 GOT_STATUS_UNVERSIONED, path);
1430 goto done;
1432 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1433 !S_ISREG(st_mode) && !installing_bad_symlink) {
1434 /* TODO file is obstructed; do something */
1435 err = got_error_path(ondisk_path,
1436 GOT_ERR_FILE_OBSTRUCTED);
1437 goto done;
1438 } else {
1439 err = got_opentemp_named_fd(&tmppath, &fd,
1440 ondisk_path, "");
1441 if (err)
1442 goto done;
1443 update = 1;
1445 if (fchmod(fd, apply_umask(mode)) == -1) {
1446 err = got_error_from_errno2("fchmod",
1447 tmppath);
1448 goto done;
1451 } else
1452 return got_error_from_errno2("open", ondisk_path);
1455 if (progress_cb) {
1456 if (restoring_missing_file)
1457 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1458 path);
1459 else if (reverting_versioned_file)
1460 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1461 path);
1462 else
1463 err = (*progress_cb)(progress_arg,
1464 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1465 if (err)
1466 goto done;
1469 hdrlen = got_object_blob_get_hdrlen(blob);
1470 do {
1471 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1472 err = got_object_blob_read_block(&len, blob);
1473 if (err)
1474 break;
1475 if (len > 0) {
1476 /* Skip blob object header first time around. */
1477 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1478 if (outlen == -1) {
1479 err = got_error_from_errno("write");
1480 goto done;
1481 } else if (outlen != len - hdrlen) {
1482 err = got_error(GOT_ERR_IO);
1483 goto done;
1485 hdrlen = 0;
1487 } while (len != 0);
1489 if (fsync(fd) != 0) {
1490 err = got_error_from_errno("fsync");
1491 goto done;
1494 if (update) {
1495 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1496 err = got_error_from_errno2("unlink", ondisk_path);
1497 goto done;
1499 if (rename(tmppath, ondisk_path) != 0) {
1500 err = got_error_from_errno3("rename", tmppath,
1501 ondisk_path);
1502 goto done;
1504 free(tmppath);
1505 tmppath = NULL;
1508 done:
1509 if (fd != -1 && close(fd) == -1 && err == NULL)
1510 err = got_error_from_errno("close");
1511 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1512 err = got_error_from_errno2("unlink", tmppath);
1513 free(tmppath);
1514 return err;
1518 * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
1519 * conflict marker is found in newly added lines only.
1521 static const struct got_error *
1522 get_modified_file_content_status(unsigned char *status,
1523 struct got_blob_object *blob, const char *path, struct stat *sb,
1524 FILE *ondisk_file)
1526 const struct got_error *err, *free_err;
1527 const char *markers[3] = {
1528 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1529 GOT_DIFF_CONFLICT_MARKER_SEP,
1530 GOT_DIFF_CONFLICT_MARKER_END
1532 FILE *f1 = NULL;
1533 struct got_diffreg_result *diffreg_result = NULL;
1534 struct diff_result *r;
1535 int nchunks_parsed, n, i = 0, ln = 0;
1536 char *line = NULL;
1537 size_t linesize = 0;
1538 ssize_t linelen;
1540 if (*status != GOT_STATUS_MODIFY)
1541 return NULL;
1543 f1 = got_opentemp();
1544 if (f1 == NULL)
1545 return got_error_from_errno("got_opentemp");
1547 if (blob) {
1548 got_object_blob_rewind(blob);
1549 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
1550 if (err)
1551 goto done;
1554 err = got_diff_files(&diffreg_result, f1, 1, NULL, ondisk_file,
1555 1, NULL, 0, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
1556 if (err)
1557 goto done;
1559 r = diffreg_result->result;
1561 for (n = 0; n < r->chunks.len; n += nchunks_parsed) {
1562 struct diff_chunk *c;
1563 struct diff_chunk_context cc = {};
1564 off_t pos;
1567 * We can optimise a little by advancing straight
1568 * to the next chunk if this one has no added lines.
1570 c = diff_chunk_get(r, n);
1572 if (diff_chunk_type(c) != CHUNK_PLUS) {
1573 nchunks_parsed = 1;
1574 continue; /* removed or unchanged lines */
1577 pos = diff_chunk_get_right_start_pos(c);
1578 if (fseek(ondisk_file, pos, SEEK_SET) == -1) {
1579 err = got_ferror(ondisk_file, GOT_ERR_IO);
1580 goto done;
1583 diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
1584 ln = cc.right.start;
1586 while (ln < cc.right.end) {
1587 linelen = getline(&line, &linesize, ondisk_file);
1588 if (linelen == -1) {
1589 if (feof(ondisk_file))
1590 break;
1591 err = got_ferror(ondisk_file, GOT_ERR_IO);
1592 break;
1595 if (line && strncmp(line, markers[i],
1596 strlen(markers[i])) == 0) {
1597 if (strcmp(markers[i],
1598 GOT_DIFF_CONFLICT_MARKER_END) == 0) {
1599 *status = GOT_STATUS_CONFLICT;
1600 goto done;
1601 } else
1602 i++;
1604 ++ln;
1608 done:
1609 free(line);
1610 if (f1 != NULL && fclose(f1) == EOF && err == NULL)
1611 err = got_error_from_errno("fclose");
1612 free_err = got_diffreg_result_free(diffreg_result);
1613 if (err == NULL)
1614 err = free_err;
1616 return err;
1619 static int
1620 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1622 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1623 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1626 static int
1627 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1629 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1630 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1631 ie->mtime_sec == sb->st_mtim.tv_sec &&
1632 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1633 ie->size == (sb->st_size & 0xffffffff) &&
1634 !xbit_differs(ie, sb->st_mode));
1637 static unsigned char
1638 get_staged_status(struct got_fileindex_entry *ie)
1640 switch (got_fileindex_entry_stage_get(ie)) {
1641 case GOT_FILEIDX_STAGE_ADD:
1642 return GOT_STATUS_ADD;
1643 case GOT_FILEIDX_STAGE_DELETE:
1644 return GOT_STATUS_DELETE;
1645 case GOT_FILEIDX_STAGE_MODIFY:
1646 return GOT_STATUS_MODIFY;
1647 default:
1648 return GOT_STATUS_NO_CHANGE;
1652 static const struct got_error *
1653 get_symlink_modification_status(unsigned char *status,
1654 struct got_fileindex_entry *ie, const char *abspath,
1655 int dirfd, const char *de_name, struct got_blob_object *blob)
1657 const struct got_error *err = NULL;
1658 char target_path[PATH_MAX];
1659 char etarget[PATH_MAX];
1660 ssize_t elen;
1661 size_t len, target_len = 0;
1662 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1663 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1665 *status = GOT_STATUS_NO_CHANGE;
1667 /* Blob object content specifies the target path of the link. */
1668 do {
1669 err = got_object_blob_read_block(&len, blob);
1670 if (err)
1671 return err;
1672 if (len + target_len >= sizeof(target_path)) {
1674 * Should not happen. The blob contents were OK
1675 * when this symlink was installed.
1677 return got_error(GOT_ERR_NO_SPACE);
1679 if (len > 0) {
1680 /* Skip blob object header first time around. */
1681 memcpy(target_path + target_len, buf + hdrlen,
1682 len - hdrlen);
1683 target_len += len - hdrlen;
1684 hdrlen = 0;
1686 } while (len != 0);
1687 target_path[target_len] = '\0';
1689 if (dirfd != -1) {
1690 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1691 if (elen == -1)
1692 return got_error_from_errno2("readlinkat", abspath);
1693 } else {
1694 elen = readlink(abspath, etarget, sizeof(etarget));
1695 if (elen == -1)
1696 return got_error_from_errno2("readlink", abspath);
1699 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1700 *status = GOT_STATUS_MODIFY;
1702 return NULL;
1705 static const struct got_error *
1706 get_file_status(unsigned char *status, struct stat *sb,
1707 struct got_fileindex_entry *ie, const char *abspath,
1708 int dirfd, const char *de_name, struct got_repository *repo)
1710 const struct got_error *err = NULL;
1711 struct got_object_id id;
1712 size_t hdrlen;
1713 int fd = -1, fd1 = -1;
1714 FILE *f = NULL;
1715 uint8_t fbuf[8192];
1716 struct got_blob_object *blob = NULL;
1717 size_t flen, blen;
1718 unsigned char staged_status;
1720 staged_status = get_staged_status(ie);
1721 *status = GOT_STATUS_NO_CHANGE;
1722 memset(sb, 0, sizeof(*sb));
1725 * Whenever the caller provides a directory descriptor and a
1726 * directory entry name for the file, use them! This prevents
1727 * race conditions if filesystem paths change beneath our feet.
1729 if (dirfd != -1) {
1730 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1731 if (errno == ENOENT) {
1732 if (got_fileindex_entry_has_file_on_disk(ie))
1733 *status = GOT_STATUS_MISSING;
1734 else
1735 *status = GOT_STATUS_DELETE;
1736 goto done;
1738 err = got_error_from_errno2("fstatat", abspath);
1739 goto done;
1741 } else {
1742 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1743 if (fd == -1 && errno != ENOENT &&
1744 !got_err_open_nofollow_on_symlink())
1745 return got_error_from_errno2("open", abspath);
1746 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1747 if (lstat(abspath, sb) == -1)
1748 return got_error_from_errno2("lstat", abspath);
1749 } else if (fd == -1 || fstat(fd, sb) == -1) {
1750 if (errno == ENOENT) {
1751 if (got_fileindex_entry_has_file_on_disk(ie))
1752 *status = GOT_STATUS_MISSING;
1753 else
1754 *status = GOT_STATUS_DELETE;
1755 goto done;
1757 err = got_error_from_errno2("fstat", abspath);
1758 goto done;
1762 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1763 *status = GOT_STATUS_OBSTRUCTED;
1764 goto done;
1767 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1768 *status = GOT_STATUS_DELETE;
1769 goto done;
1770 } else if (!got_fileindex_entry_has_blob(ie) &&
1771 staged_status != GOT_STATUS_ADD) {
1772 *status = GOT_STATUS_ADD;
1773 goto done;
1776 if (!stat_info_differs(ie, sb))
1777 goto done;
1779 if (S_ISLNK(sb->st_mode) &&
1780 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1781 *status = GOT_STATUS_MODIFY;
1782 goto done;
1785 if (staged_status == GOT_STATUS_MODIFY ||
1786 staged_status == GOT_STATUS_ADD)
1787 got_fileindex_entry_get_staged_blob_id(&id, ie);
1788 else
1789 got_fileindex_entry_get_blob_id(&id, ie);
1791 fd1 = got_opentempfd();
1792 if (fd1 == -1) {
1793 err = got_error_from_errno("got_opentempfd");
1794 goto done;
1796 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1797 if (err)
1798 goto done;
1800 if (S_ISLNK(sb->st_mode)) {
1801 err = get_symlink_modification_status(status, ie,
1802 abspath, dirfd, de_name, blob);
1803 goto done;
1806 if (dirfd != -1) {
1807 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1808 if (fd == -1) {
1809 err = got_error_from_errno2("openat", abspath);
1810 goto done;
1814 f = fdopen(fd, "r");
1815 if (f == NULL) {
1816 err = got_error_from_errno2("fdopen", abspath);
1817 goto done;
1819 fd = -1;
1820 hdrlen = got_object_blob_get_hdrlen(blob);
1821 for (;;) {
1822 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1823 err = got_object_blob_read_block(&blen, blob);
1824 if (err)
1825 goto done;
1826 /* Skip length of blob object header first time around. */
1827 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1828 if (flen == 0 && ferror(f)) {
1829 err = got_error_from_errno("fread");
1830 goto done;
1832 if (blen - hdrlen == 0) {
1833 if (flen != 0)
1834 *status = GOT_STATUS_MODIFY;
1835 break;
1836 } else if (flen == 0) {
1837 if (blen - hdrlen != 0)
1838 *status = GOT_STATUS_MODIFY;
1839 break;
1840 } else if (blen - hdrlen == flen) {
1841 /* Skip blob object header first time around. */
1842 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1843 *status = GOT_STATUS_MODIFY;
1844 break;
1846 } else {
1847 *status = GOT_STATUS_MODIFY;
1848 break;
1850 hdrlen = 0;
1853 if (*status == GOT_STATUS_MODIFY) {
1854 rewind(f);
1855 err = get_modified_file_content_status(status, blob, ie->path,
1856 sb, f);
1857 } else if (xbit_differs(ie, sb->st_mode))
1858 *status = GOT_STATUS_MODE_CHANGE;
1859 done:
1860 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1861 err = got_error_from_errno("close");
1862 if (blob)
1863 got_object_blob_close(blob);
1864 if (f != NULL && fclose(f) == EOF && err == NULL)
1865 err = got_error_from_errno2("fclose", abspath);
1866 if (fd != -1 && close(fd) == -1 && err == NULL)
1867 err = got_error_from_errno2("close", abspath);
1868 return err;
1872 * Update timestamps in the file index if a file is unmodified and
1873 * we had to run a full content comparison to find out.
1875 static const struct got_error *
1876 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1877 struct got_fileindex_entry *ie, struct stat *sb)
1879 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1880 return got_fileindex_entry_update(ie, wt_fd, path,
1881 ie->blob_sha1, ie->commit_sha1, 1);
1883 return NULL;
1886 static const struct got_error *
1887 update_blob(struct got_worktree *worktree,
1888 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1889 struct got_tree_entry *te, const char *path,
1890 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1891 void *progress_arg)
1893 const struct got_error *err = NULL;
1894 struct got_blob_object *blob = NULL;
1895 char *ondisk_path = NULL;
1896 unsigned char status = GOT_STATUS_NO_CHANGE;
1897 struct stat sb;
1898 int fd1 = -1, fd2 = -1;
1900 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1901 return got_error_from_errno("asprintf");
1903 if (ie) {
1904 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1905 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1906 goto done;
1908 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1909 repo);
1910 if (err)
1911 goto done;
1912 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1913 sb.st_mode = got_fileindex_perms_to_st(ie);
1914 } else {
1915 if (stat(ondisk_path, &sb) == -1) {
1916 if (errno != ENOENT) {
1917 err = got_error_from_errno2("stat",
1918 ondisk_path);
1919 goto done;
1921 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1922 status = GOT_STATUS_UNVERSIONED;
1923 } else {
1924 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1925 status = GOT_STATUS_UNVERSIONED;
1926 else
1927 status = GOT_STATUS_OBSTRUCTED;
1931 if (status == GOT_STATUS_OBSTRUCTED) {
1932 if (ie)
1933 got_fileindex_entry_mark_skipped(ie);
1934 err = (*progress_cb)(progress_arg, status, path);
1935 goto done;
1937 if (status == GOT_STATUS_CONFLICT) {
1938 if (ie)
1939 got_fileindex_entry_mark_skipped(ie);
1940 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1941 path);
1942 goto done;
1945 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1946 (S_ISLNK(te->mode) ||
1947 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1949 * This is a regular file or an installed bad symlink.
1950 * If the file index indicates that this file is already
1951 * up-to-date with respect to the repository we can skip
1952 * updating contents of this file.
1954 if (got_fileindex_entry_has_commit(ie) &&
1955 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1956 SHA1_DIGEST_LENGTH) == 0) {
1957 /* Same commit. */
1958 err = sync_timestamps(worktree->root_fd,
1959 path, status, ie, &sb);
1960 if (err)
1961 goto done;
1962 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1963 path);
1964 goto done;
1966 if (got_fileindex_entry_has_blob(ie) &&
1967 memcmp(ie->blob_sha1, te->id.sha1,
1968 SHA1_DIGEST_LENGTH) == 0) {
1969 /* Different commit but the same blob. */
1970 err = sync_timestamps(worktree->root_fd,
1971 path, status, ie, &sb);
1972 if (err)
1973 goto done;
1974 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1975 path);
1976 goto done;
1980 fd1 = got_opentempfd();
1981 if (fd1 == -1) {
1982 err = got_error_from_errno("got_opentempfd");
1983 goto done;
1985 err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
1986 if (err)
1987 goto done;
1989 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1990 int update_timestamps;
1991 struct got_blob_object *blob2 = NULL;
1992 char *label_orig = NULL;
1993 if (got_fileindex_entry_has_blob(ie)) {
1994 fd2 = got_opentempfd();
1995 if (fd2 == -1) {
1996 err = got_error_from_errno("got_opentempfd");
1997 goto done;
1999 struct got_object_id id2;
2000 got_fileindex_entry_get_blob_id(&id2, ie);
2001 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
2002 fd2);
2003 if (err)
2004 goto done;
2006 if (got_fileindex_entry_has_commit(ie)) {
2007 char id_str[SHA1_DIGEST_STRING_LENGTH];
2008 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2009 sizeof(id_str)) == NULL) {
2010 err = got_error_path(id_str,
2011 GOT_ERR_BAD_OBJ_ID_STR);
2012 goto done;
2014 if (asprintf(&label_orig, "%s: commit %s",
2015 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2016 err = got_error_from_errno("asprintf");
2017 goto done;
2020 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2021 char *link_target;
2022 err = got_object_blob_read_to_str(&link_target, blob);
2023 if (err)
2024 goto done;
2025 err = merge_symlink(worktree, blob2, ondisk_path, path,
2026 label_orig, link_target, worktree->base_commit_id,
2027 repo, progress_cb, progress_arg);
2028 free(link_target);
2029 } else {
2030 err = merge_blob(&update_timestamps, worktree, blob2,
2031 ondisk_path, path, sb.st_mode, label_orig, blob,
2032 worktree->base_commit_id, repo,
2033 progress_cb, progress_arg);
2035 free(label_orig);
2036 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2037 err = got_error_from_errno("close");
2038 goto done;
2040 if (blob2)
2041 got_object_blob_close(blob2);
2042 if (err)
2043 goto done;
2045 * Do not update timestamps of files with local changes.
2046 * Otherwise, a future status walk would treat them as
2047 * unmodified files again.
2049 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2050 blob->id.sha1, worktree->base_commit_id->sha1,
2051 update_timestamps);
2052 } else if (status == GOT_STATUS_MODE_CHANGE) {
2053 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2054 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2055 } else if (status == GOT_STATUS_DELETE) {
2056 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2057 if (err)
2058 goto done;
2059 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2060 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2061 if (err)
2062 goto done;
2063 } else {
2064 int is_bad_symlink = 0;
2065 if (S_ISLNK(te->mode)) {
2066 err = install_symlink(&is_bad_symlink, worktree,
2067 ondisk_path, path, blob,
2068 status == GOT_STATUS_MISSING, 0,
2069 status == GOT_STATUS_UNVERSIONED, 0,
2070 repo, progress_cb, progress_arg);
2071 } else {
2072 err = install_blob(worktree, ondisk_path, path,
2073 te->mode, sb.st_mode, blob,
2074 status == GOT_STATUS_MISSING, 0, 0,
2075 status == GOT_STATUS_UNVERSIONED, repo,
2076 progress_cb, progress_arg);
2078 if (err)
2079 goto done;
2081 if (ie) {
2082 err = got_fileindex_entry_update(ie,
2083 worktree->root_fd, path, blob->id.sha1,
2084 worktree->base_commit_id->sha1, 1);
2085 } else {
2086 err = create_fileindex_entry(&ie, fileindex,
2087 worktree->base_commit_id, worktree->root_fd, path,
2088 &blob->id);
2090 if (err)
2091 goto done;
2093 if (is_bad_symlink) {
2094 got_fileindex_entry_filetype_set(ie,
2095 GOT_FILEIDX_MODE_BAD_SYMLINK);
2099 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2100 err = got_error_from_errno("close");
2101 goto done;
2103 got_object_blob_close(blob);
2104 done:
2105 free(ondisk_path);
2106 return err;
2109 static const struct got_error *
2110 remove_ondisk_file(const char *root_path, const char *path)
2112 const struct got_error *err = NULL;
2113 char *ondisk_path = NULL, *parent = NULL;
2115 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2116 return got_error_from_errno("asprintf");
2118 if (unlink(ondisk_path) == -1) {
2119 if (errno != ENOENT)
2120 err = got_error_from_errno2("unlink", ondisk_path);
2121 } else {
2122 size_t root_len = strlen(root_path);
2123 err = got_path_dirname(&parent, ondisk_path);
2124 if (err)
2125 goto done;
2126 while (got_path_cmp(parent, root_path,
2127 strlen(parent), root_len) != 0) {
2128 free(ondisk_path);
2129 ondisk_path = parent;
2130 parent = NULL;
2131 if (rmdir(ondisk_path) == -1) {
2132 if (errno != ENOTEMPTY)
2133 err = got_error_from_errno2("rmdir",
2134 ondisk_path);
2135 break;
2137 err = got_path_dirname(&parent, ondisk_path);
2138 if (err)
2139 break;
2142 done:
2143 free(ondisk_path);
2144 free(parent);
2145 return err;
2148 static const struct got_error *
2149 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2150 struct got_fileindex_entry *ie, struct got_repository *repo,
2151 got_worktree_checkout_cb progress_cb, void *progress_arg)
2153 const struct got_error *err = NULL;
2154 unsigned char status;
2155 struct stat sb;
2156 char *ondisk_path;
2158 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2159 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2161 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2162 == -1)
2163 return got_error_from_errno("asprintf");
2165 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2166 if (err)
2167 goto done;
2169 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2170 char ondisk_target[PATH_MAX];
2171 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2172 sizeof(ondisk_target));
2173 if (ondisk_len == -1) {
2174 err = got_error_from_errno2("readlink", ondisk_path);
2175 goto done;
2177 ondisk_target[ondisk_len] = '\0';
2178 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2179 NULL, NULL, /* XXX pass common ancestor info? */
2180 ondisk_target, ondisk_path);
2181 if (err)
2182 goto done;
2183 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2184 ie->path);
2185 goto done;
2188 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2189 status == GOT_STATUS_ADD) {
2190 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2191 if (err)
2192 goto done;
2194 * Preserve the working file and change the deleted blob's
2195 * entry into a schedule-add entry.
2197 err = got_fileindex_entry_update(ie, worktree->root_fd,
2198 ie->path, NULL, NULL, 0);
2199 } else {
2200 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2201 if (err)
2202 goto done;
2203 if (status == GOT_STATUS_NO_CHANGE) {
2204 err = remove_ondisk_file(worktree->root_path, ie->path);
2205 if (err)
2206 goto done;
2208 got_fileindex_entry_remove(fileindex, ie);
2210 done:
2211 free(ondisk_path);
2212 return err;
2215 struct diff_cb_arg {
2216 struct got_fileindex *fileindex;
2217 struct got_worktree *worktree;
2218 struct got_repository *repo;
2219 got_worktree_checkout_cb progress_cb;
2220 void *progress_arg;
2221 got_cancel_cb cancel_cb;
2222 void *cancel_arg;
2225 static const struct got_error *
2226 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2227 struct got_tree_entry *te, const char *parent_path)
2229 struct diff_cb_arg *a = arg;
2231 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2232 return got_error(GOT_ERR_CANCELLED);
2234 return update_blob(a->worktree, a->fileindex, ie, te,
2235 ie->path, a->repo, a->progress_cb, a->progress_arg);
2238 static const struct got_error *
2239 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2241 struct diff_cb_arg *a = arg;
2243 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2244 return got_error(GOT_ERR_CANCELLED);
2246 return delete_blob(a->worktree, a->fileindex, ie,
2247 a->repo, a->progress_cb, a->progress_arg);
2250 static const struct got_error *
2251 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2253 struct diff_cb_arg *a = arg;
2254 const struct got_error *err;
2255 char *path;
2257 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2258 return got_error(GOT_ERR_CANCELLED);
2260 if (got_object_tree_entry_is_submodule(te))
2261 return NULL;
2263 if (asprintf(&path, "%s%s%s", parent_path,
2264 parent_path[0] ? "/" : "", te->name)
2265 == -1)
2266 return got_error_from_errno("asprintf");
2268 if (S_ISDIR(te->mode))
2269 err = add_dir_on_disk(a->worktree, path);
2270 else
2271 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2272 a->repo, a->progress_cb, a->progress_arg);
2274 free(path);
2275 return err;
2278 const struct got_error *
2279 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2281 uint32_t uuid_status;
2283 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2284 if (uuid_status != uuid_s_ok) {
2285 *uuidstr = NULL;
2286 return got_error_uuid(uuid_status, "uuid_to_string");
2289 return NULL;
2292 static const struct got_error *
2293 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2295 const struct got_error *err = NULL;
2296 char *uuidstr = NULL;
2298 *refname = NULL;
2300 err = got_worktree_get_uuid(&uuidstr, worktree);
2301 if (err)
2302 return err;
2304 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2305 err = got_error_from_errno("asprintf");
2306 *refname = NULL;
2308 free(uuidstr);
2309 return err;
2312 const struct got_error *
2313 got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2314 const char *prefix)
2316 return get_ref_name(refname, worktree, prefix);
2319 const struct got_error *
2320 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2322 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2325 static const struct got_error *
2326 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2328 return get_ref_name(refname, worktree,
2329 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2332 static const struct got_error *
2333 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2335 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2338 static const struct got_error *
2339 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2341 return get_ref_name(refname, worktree,
2342 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2345 static const struct got_error *
2346 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2348 return get_ref_name(refname, worktree,
2349 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2352 static const struct got_error *
2353 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2355 return get_ref_name(refname, worktree,
2356 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2359 static const struct got_error *
2360 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2362 return get_ref_name(refname, worktree,
2363 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2366 static const struct got_error *
2367 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2369 return get_ref_name(refname, worktree,
2370 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2373 static const struct got_error *
2374 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2376 return get_ref_name(refname, worktree,
2377 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2380 const struct got_error *
2381 got_worktree_get_histedit_script_path(char **path,
2382 struct got_worktree *worktree)
2384 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2385 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2386 *path = NULL;
2387 return got_error_from_errno("asprintf");
2389 return NULL;
2392 static const struct got_error *
2393 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2395 return get_ref_name(refname, worktree,
2396 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2399 static const struct got_error *
2400 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2402 return get_ref_name(refname, worktree,
2403 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2407 * Prevent Git's garbage collector from deleting our base commit by
2408 * setting a reference to our base commit's ID.
2410 static const struct got_error *
2411 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2413 const struct got_error *err = NULL;
2414 struct got_reference *ref = NULL;
2415 char *refname;
2417 err = got_worktree_get_base_ref_name(&refname, worktree);
2418 if (err)
2419 return err;
2421 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2422 if (err)
2423 goto done;
2425 err = got_ref_write(ref, repo);
2426 done:
2427 free(refname);
2428 if (ref)
2429 got_ref_close(ref);
2430 return err;
2433 static const struct got_error *
2434 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2436 const struct got_error *err = NULL;
2438 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2439 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2440 err = got_error_from_errno("asprintf");
2441 *fileindex_path = NULL;
2443 return err;
2447 static const struct got_error *
2448 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2449 struct got_worktree *worktree)
2451 const struct got_error *err = NULL;
2452 FILE *index = NULL;
2454 *fileindex_path = NULL;
2455 *fileindex = got_fileindex_alloc();
2456 if (*fileindex == NULL)
2457 return got_error_from_errno("got_fileindex_alloc");
2459 err = get_fileindex_path(fileindex_path, worktree);
2460 if (err)
2461 goto done;
2463 index = fopen(*fileindex_path, "rbe");
2464 if (index == NULL) {
2465 if (errno != ENOENT)
2466 err = got_error_from_errno2("fopen", *fileindex_path);
2467 } else {
2468 err = got_fileindex_read(*fileindex, index);
2469 if (fclose(index) == EOF && err == NULL)
2470 err = got_error_from_errno("fclose");
2472 done:
2473 if (err) {
2474 free(*fileindex_path);
2475 *fileindex_path = NULL;
2476 got_fileindex_free(*fileindex);
2477 *fileindex = NULL;
2479 return err;
2482 struct bump_base_commit_id_arg {
2483 struct got_object_id *base_commit_id;
2484 const char *path;
2485 size_t path_len;
2486 const char *entry_name;
2487 got_worktree_checkout_cb progress_cb;
2488 void *progress_arg;
2491 static const struct got_error *
2492 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2494 const struct got_error *err;
2495 struct bump_base_commit_id_arg *a = arg;
2497 if (a->entry_name) {
2498 if (strcmp(ie->path, a->path) != 0)
2499 return NULL;
2500 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2501 return NULL;
2503 if (got_fileindex_entry_was_skipped(ie))
2504 return NULL;
2506 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2507 SHA1_DIGEST_LENGTH) == 0)
2508 return NULL;
2510 if (a->progress_cb) {
2511 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2512 ie->path);
2513 if (err)
2514 return err;
2516 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2517 return NULL;
2520 /* Bump base commit ID of all files within an updated part of the work tree. */
2521 static const struct got_error *
2522 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2523 struct got_fileindex *fileindex,
2524 got_worktree_checkout_cb progress_cb, void *progress_arg)
2526 struct bump_base_commit_id_arg bbc_arg;
2528 bbc_arg.base_commit_id = worktree->base_commit_id;
2529 bbc_arg.entry_name = NULL;
2530 bbc_arg.path = "";
2531 bbc_arg.path_len = 0;
2532 bbc_arg.progress_cb = progress_cb;
2533 bbc_arg.progress_arg = progress_arg;
2535 return got_fileindex_for_each_entry_safe(fileindex,
2536 bump_base_commit_id, &bbc_arg);
2539 static const struct got_error *
2540 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2542 const struct got_error *err = NULL;
2543 char *new_fileindex_path = NULL;
2544 FILE *new_index = NULL;
2545 struct timespec timeout;
2547 err = got_opentemp_named(&new_fileindex_path, &new_index,
2548 fileindex_path, "");
2549 if (err)
2550 goto done;
2552 err = got_fileindex_write(fileindex, new_index);
2553 if (err)
2554 goto done;
2556 if (rename(new_fileindex_path, fileindex_path) != 0) {
2557 err = got_error_from_errno3("rename", new_fileindex_path,
2558 fileindex_path);
2559 unlink(new_fileindex_path);
2563 * Sleep for a short amount of time to ensure that files modified after
2564 * this program exits have a different time stamp from the one which
2565 * was recorded in the file index.
2567 timeout.tv_sec = 0;
2568 timeout.tv_nsec = 1;
2569 nanosleep(&timeout, NULL);
2570 done:
2571 if (new_index)
2572 fclose(new_index);
2573 free(new_fileindex_path);
2574 return err;
2577 static const struct got_error *
2578 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2579 struct got_object_id **tree_id, const char *wt_relpath,
2580 struct got_commit_object *base_commit, struct got_worktree *worktree,
2581 struct got_repository *repo)
2583 const struct got_error *err = NULL;
2584 struct got_object_id *id = NULL;
2585 char *in_repo_path = NULL;
2586 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2588 *entry_type = GOT_OBJ_TYPE_ANY;
2589 *tree_relpath = NULL;
2590 *tree_id = NULL;
2592 if (wt_relpath[0] == '\0') {
2593 /* Check out all files within the work tree. */
2594 *entry_type = GOT_OBJ_TYPE_TREE;
2595 *tree_relpath = strdup("");
2596 if (*tree_relpath == NULL) {
2597 err = got_error_from_errno("strdup");
2598 goto done;
2600 err = got_object_id_by_path(tree_id, repo, base_commit,
2601 worktree->path_prefix);
2602 if (err)
2603 goto done;
2604 return NULL;
2607 /* Check out a subset of files in the work tree. */
2609 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2610 is_root_wt ? "" : "/", wt_relpath) == -1) {
2611 err = got_error_from_errno("asprintf");
2612 goto done;
2615 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2616 if (err)
2617 goto done;
2619 free(in_repo_path);
2620 in_repo_path = NULL;
2622 err = got_object_get_type(entry_type, repo, id);
2623 if (err)
2624 goto done;
2626 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2627 /* Check out a single file. */
2628 if (strchr(wt_relpath, '/') == NULL) {
2629 /* Check out a single file in work tree's root dir. */
2630 in_repo_path = strdup(worktree->path_prefix);
2631 if (in_repo_path == NULL) {
2632 err = got_error_from_errno("strdup");
2633 goto done;
2635 *tree_relpath = strdup("");
2636 if (*tree_relpath == NULL) {
2637 err = got_error_from_errno("strdup");
2638 goto done;
2640 } else {
2641 /* Check out a single file in a subdirectory. */
2642 err = got_path_dirname(tree_relpath, wt_relpath);
2643 if (err)
2644 return err;
2645 if (asprintf(&in_repo_path, "%s%s%s",
2646 worktree->path_prefix, is_root_wt ? "" : "/",
2647 *tree_relpath) == -1) {
2648 err = got_error_from_errno("asprintf");
2649 goto done;
2652 err = got_object_id_by_path(tree_id, repo,
2653 base_commit, in_repo_path);
2654 } else {
2655 /* Check out all files within a subdirectory. */
2656 *tree_id = got_object_id_dup(id);
2657 if (*tree_id == NULL) {
2658 err = got_error_from_errno("got_object_id_dup");
2659 goto done;
2661 *tree_relpath = strdup(wt_relpath);
2662 if (*tree_relpath == NULL) {
2663 err = got_error_from_errno("strdup");
2664 goto done;
2667 done:
2668 free(id);
2669 free(in_repo_path);
2670 if (err) {
2671 *entry_type = GOT_OBJ_TYPE_ANY;
2672 free(*tree_relpath);
2673 *tree_relpath = NULL;
2674 free(*tree_id);
2675 *tree_id = NULL;
2677 return err;
2680 static const struct got_error *
2681 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2682 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2683 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2684 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2686 const struct got_error *err = NULL;
2687 struct got_commit_object *commit = NULL;
2688 struct got_tree_object *tree = NULL;
2689 struct got_fileindex_diff_tree_cb diff_cb;
2690 struct diff_cb_arg arg;
2692 err = ref_base_commit(worktree, repo);
2693 if (err) {
2694 if (!(err->code == GOT_ERR_ERRNO &&
2695 (errno == EACCES || errno == EROFS)))
2696 goto done;
2697 err = (*progress_cb)(progress_arg,
2698 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2699 if (err)
2700 return err;
2703 err = got_object_open_as_commit(&commit, repo,
2704 worktree->base_commit_id);
2705 if (err)
2706 goto done;
2708 err = got_object_open_as_tree(&tree, repo, tree_id);
2709 if (err)
2710 goto done;
2712 if (entry_name &&
2713 got_object_tree_find_entry(tree, entry_name) == NULL) {
2714 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2715 goto done;
2718 diff_cb.diff_old_new = diff_old_new;
2719 diff_cb.diff_old = diff_old;
2720 diff_cb.diff_new = diff_new;
2721 arg.fileindex = fileindex;
2722 arg.worktree = worktree;
2723 arg.repo = repo;
2724 arg.progress_cb = progress_cb;
2725 arg.progress_arg = progress_arg;
2726 arg.cancel_cb = cancel_cb;
2727 arg.cancel_arg = cancel_arg;
2728 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2729 entry_name, repo, &diff_cb, &arg);
2730 done:
2731 if (tree)
2732 got_object_tree_close(tree);
2733 if (commit)
2734 got_object_commit_close(commit);
2735 return err;
2738 const struct got_error *
2739 got_worktree_checkout_files(struct got_worktree *worktree,
2740 struct got_pathlist_head *paths, struct got_repository *repo,
2741 got_worktree_checkout_cb progress_cb, void *progress_arg,
2742 got_cancel_cb cancel_cb, void *cancel_arg)
2744 const struct got_error *err = NULL, *sync_err, *unlockerr;
2745 struct got_commit_object *commit = NULL;
2746 struct got_tree_object *tree = NULL;
2747 struct got_fileindex *fileindex = NULL;
2748 char *fileindex_path = NULL;
2749 struct got_pathlist_entry *pe;
2750 struct tree_path_data {
2751 STAILQ_ENTRY(tree_path_data) entry;
2752 struct got_object_id *tree_id;
2753 int entry_type;
2754 char *relpath;
2755 char *entry_name;
2756 } *tpd = NULL;
2757 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2759 STAILQ_INIT(&tree_paths);
2761 err = lock_worktree(worktree, LOCK_EX);
2762 if (err)
2763 return err;
2765 err = got_object_open_as_commit(&commit, repo,
2766 worktree->base_commit_id);
2767 if (err)
2768 goto done;
2770 /* Map all specified paths to in-repository trees. */
2771 TAILQ_FOREACH(pe, paths, entry) {
2772 tpd = malloc(sizeof(*tpd));
2773 if (tpd == NULL) {
2774 err = got_error_from_errno("malloc");
2775 goto done;
2778 err = find_tree_entry_for_checkout(&tpd->entry_type,
2779 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2780 worktree, repo);
2781 if (err) {
2782 free(tpd);
2783 goto done;
2786 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2787 err = got_path_basename(&tpd->entry_name, pe->path);
2788 if (err) {
2789 free(tpd->relpath);
2790 free(tpd->tree_id);
2791 free(tpd);
2792 goto done;
2794 } else
2795 tpd->entry_name = NULL;
2797 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2801 * Read the file index.
2802 * Checking out files is supposed to be an idempotent operation.
2803 * If the on-disk file index is incomplete we will try to complete it.
2805 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2806 if (err)
2807 goto done;
2809 tpd = STAILQ_FIRST(&tree_paths);
2810 TAILQ_FOREACH(pe, paths, entry) {
2811 struct bump_base_commit_id_arg bbc_arg;
2813 err = checkout_files(worktree, fileindex, tpd->relpath,
2814 tpd->tree_id, tpd->entry_name, repo,
2815 progress_cb, progress_arg, cancel_cb, cancel_arg);
2816 if (err)
2817 break;
2819 bbc_arg.base_commit_id = worktree->base_commit_id;
2820 bbc_arg.entry_name = tpd->entry_name;
2821 bbc_arg.path = pe->path;
2822 bbc_arg.path_len = pe->path_len;
2823 bbc_arg.progress_cb = progress_cb;
2824 bbc_arg.progress_arg = progress_arg;
2825 err = got_fileindex_for_each_entry_safe(fileindex,
2826 bump_base_commit_id, &bbc_arg);
2827 if (err)
2828 break;
2830 tpd = STAILQ_NEXT(tpd, entry);
2832 sync_err = sync_fileindex(fileindex, fileindex_path);
2833 if (sync_err && err == NULL)
2834 err = sync_err;
2835 done:
2836 free(fileindex_path);
2837 if (tree)
2838 got_object_tree_close(tree);
2839 if (commit)
2840 got_object_commit_close(commit);
2841 if (fileindex)
2842 got_fileindex_free(fileindex);
2843 while (!STAILQ_EMPTY(&tree_paths)) {
2844 tpd = STAILQ_FIRST(&tree_paths);
2845 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2846 free(tpd->relpath);
2847 free(tpd->tree_id);
2848 free(tpd);
2850 unlockerr = lock_worktree(worktree, LOCK_SH);
2851 if (unlockerr && err == NULL)
2852 err = unlockerr;
2853 return err;
2856 struct merge_file_cb_arg {
2857 struct got_worktree *worktree;
2858 struct got_fileindex *fileindex;
2859 got_worktree_checkout_cb progress_cb;
2860 void *progress_arg;
2861 got_cancel_cb cancel_cb;
2862 void *cancel_arg;
2863 const char *label_orig;
2864 struct got_object_id *commit_id2;
2865 int allow_bad_symlinks;
2868 static const struct got_error *
2869 merge_file_cb(void *arg, struct got_blob_object *blob1,
2870 struct got_blob_object *blob2, FILE *f1, FILE *f2,
2871 struct got_object_id *id1, struct got_object_id *id2,
2872 const char *path1, const char *path2,
2873 mode_t mode1, mode_t mode2, struct got_repository *repo)
2875 static const struct got_error *err = NULL;
2876 struct merge_file_cb_arg *a = arg;
2877 struct got_fileindex_entry *ie;
2878 char *ondisk_path = NULL;
2879 struct stat sb;
2880 unsigned char status;
2881 int local_changes_subsumed;
2882 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2883 char *id_str = NULL, *label_deriv2 = NULL;
2885 if (blob1 && blob2) {
2886 ie = got_fileindex_entry_get(a->fileindex, path2,
2887 strlen(path2));
2888 if (ie == NULL)
2889 return (*a->progress_cb)(a->progress_arg,
2890 GOT_STATUS_MISSING, path2);
2892 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2893 path2) == -1)
2894 return got_error_from_errno("asprintf");
2896 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2897 repo);
2898 if (err)
2899 goto done;
2901 if (status == GOT_STATUS_DELETE) {
2902 err = (*a->progress_cb)(a->progress_arg,
2903 GOT_STATUS_MERGE, path2);
2904 goto done;
2906 if (status != GOT_STATUS_NO_CHANGE &&
2907 status != GOT_STATUS_MODIFY &&
2908 status != GOT_STATUS_CONFLICT &&
2909 status != GOT_STATUS_ADD) {
2910 err = (*a->progress_cb)(a->progress_arg, status, path2);
2911 goto done;
2914 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2915 char *link_target2;
2916 err = got_object_blob_read_to_str(&link_target2, blob2);
2917 if (err)
2918 goto done;
2919 err = merge_symlink(a->worktree, blob1, ondisk_path,
2920 path2, a->label_orig, link_target2, a->commit_id2,
2921 repo, a->progress_cb, a->progress_arg);
2922 free(link_target2);
2923 } else {
2924 int fd;
2926 f_orig = got_opentemp();
2927 if (f_orig == NULL) {
2928 err = got_error_from_errno("got_opentemp");
2929 goto done;
2931 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2932 f_orig, blob1);
2933 if (err)
2934 goto done;
2936 f_deriv2 = got_opentemp();
2937 if (f_deriv2 == NULL)
2938 goto done;
2939 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2940 f_deriv2, blob2);
2941 if (err)
2942 goto done;
2944 fd = open(ondisk_path,
2945 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2946 if (fd == -1) {
2947 err = got_error_from_errno2("open",
2948 ondisk_path);
2949 goto done;
2951 f_deriv = fdopen(fd, "r");
2952 if (f_deriv == NULL) {
2953 err = got_error_from_errno2("fdopen",
2954 ondisk_path);
2955 close(fd);
2956 goto done;
2958 err = got_object_id_str(&id_str, a->commit_id2);
2959 if (err)
2960 goto done;
2961 if (asprintf(&label_deriv2, "%s: commit %s",
2962 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2963 err = got_error_from_errno("asprintf");
2964 goto done;
2966 err = merge_file(&local_changes_subsumed, a->worktree,
2967 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2968 mode2, a->label_orig, NULL, label_deriv2,
2969 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2970 a->progress_cb, a->progress_arg);
2972 } else if (blob1) {
2973 ie = got_fileindex_entry_get(a->fileindex, path1,
2974 strlen(path1));
2975 if (ie == NULL)
2976 return (*a->progress_cb)(a->progress_arg,
2977 GOT_STATUS_MISSING, path1);
2979 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2980 path1) == -1)
2981 return got_error_from_errno("asprintf");
2983 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2984 repo);
2985 if (err)
2986 goto done;
2988 switch (status) {
2989 case GOT_STATUS_NO_CHANGE:
2990 err = (*a->progress_cb)(a->progress_arg,
2991 GOT_STATUS_DELETE, path1);
2992 if (err)
2993 goto done;
2994 err = remove_ondisk_file(a->worktree->root_path, path1);
2995 if (err)
2996 goto done;
2997 if (ie)
2998 got_fileindex_entry_mark_deleted_from_disk(ie);
2999 break;
3000 case GOT_STATUS_DELETE:
3001 case GOT_STATUS_MISSING:
3002 err = (*a->progress_cb)(a->progress_arg,
3003 GOT_STATUS_DELETE, path1);
3004 if (err)
3005 goto done;
3006 if (ie)
3007 got_fileindex_entry_mark_deleted_from_disk(ie);
3008 break;
3009 case GOT_STATUS_ADD: {
3010 struct got_object_id *id;
3011 FILE *blob1_f;
3012 off_t blob1_size;
3014 * Delete the added file only if its content already
3015 * exists in the repository.
3017 err = got_object_blob_file_create(&id, &blob1_f,
3018 &blob1_size, path1);
3019 if (err)
3020 goto done;
3021 if (got_object_id_cmp(id, id1) == 0) {
3022 err = (*a->progress_cb)(a->progress_arg,
3023 GOT_STATUS_DELETE, path1);
3024 if (err)
3025 goto done;
3026 err = remove_ondisk_file(a->worktree->root_path,
3027 path1);
3028 if (err)
3029 goto done;
3030 if (ie)
3031 got_fileindex_entry_remove(a->fileindex,
3032 ie);
3033 } else {
3034 err = (*a->progress_cb)(a->progress_arg,
3035 GOT_STATUS_CANNOT_DELETE, path1);
3037 if (fclose(blob1_f) == EOF && err == NULL)
3038 err = got_error_from_errno("fclose");
3039 free(id);
3040 if (err)
3041 goto done;
3042 break;
3044 case GOT_STATUS_MODIFY:
3045 case GOT_STATUS_CONFLICT:
3046 err = (*a->progress_cb)(a->progress_arg,
3047 GOT_STATUS_CANNOT_DELETE, path1);
3048 if (err)
3049 goto done;
3050 break;
3051 case GOT_STATUS_OBSTRUCTED:
3052 err = (*a->progress_cb)(a->progress_arg, status, path1);
3053 if (err)
3054 goto done;
3055 break;
3056 default:
3057 break;
3059 } else if (blob2) {
3060 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3061 path2) == -1)
3062 return got_error_from_errno("asprintf");
3063 ie = got_fileindex_entry_get(a->fileindex, path2,
3064 strlen(path2));
3065 if (ie) {
3066 err = get_file_status(&status, &sb, ie, ondisk_path,
3067 -1, NULL, repo);
3068 if (err)
3069 goto done;
3070 if (status != GOT_STATUS_NO_CHANGE &&
3071 status != GOT_STATUS_MODIFY &&
3072 status != GOT_STATUS_CONFLICT &&
3073 status != GOT_STATUS_ADD) {
3074 err = (*a->progress_cb)(a->progress_arg,
3075 status, path2);
3076 goto done;
3078 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3079 char *link_target2;
3080 err = got_object_blob_read_to_str(&link_target2,
3081 blob2);
3082 if (err)
3083 goto done;
3084 err = merge_symlink(a->worktree, NULL,
3085 ondisk_path, path2, a->label_orig,
3086 link_target2, a->commit_id2, repo,
3087 a->progress_cb, a->progress_arg);
3088 free(link_target2);
3089 } else if (S_ISREG(sb.st_mode)) {
3090 err = merge_blob(&local_changes_subsumed,
3091 a->worktree, NULL, ondisk_path, path2,
3092 sb.st_mode, a->label_orig, blob2,
3093 a->commit_id2, repo, a->progress_cb,
3094 a->progress_arg);
3095 } else {
3096 err = got_error_path(ondisk_path,
3097 GOT_ERR_FILE_OBSTRUCTED);
3099 if (err)
3100 goto done;
3101 if (status == GOT_STATUS_DELETE) {
3102 err = got_fileindex_entry_update(ie,
3103 a->worktree->root_fd, path2, blob2->id.sha1,
3104 a->worktree->base_commit_id->sha1, 0);
3105 if (err)
3106 goto done;
3108 } else {
3109 int is_bad_symlink = 0;
3110 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3111 if (S_ISLNK(mode2)) {
3112 err = install_symlink(&is_bad_symlink,
3113 a->worktree, ondisk_path, path2, blob2, 0,
3114 0, 1, a->allow_bad_symlinks, repo,
3115 a->progress_cb, a->progress_arg);
3116 } else {
3117 err = install_blob(a->worktree, ondisk_path, path2,
3118 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3119 a->progress_cb, a->progress_arg);
3121 if (err)
3122 goto done;
3123 err = got_fileindex_entry_alloc(&ie, path2);
3124 if (err)
3125 goto done;
3126 err = got_fileindex_entry_update(ie,
3127 a->worktree->root_fd, path2, NULL, NULL, 1);
3128 if (err) {
3129 got_fileindex_entry_free(ie);
3130 goto done;
3132 err = got_fileindex_entry_add(a->fileindex, ie);
3133 if (err) {
3134 got_fileindex_entry_free(ie);
3135 goto done;
3137 if (is_bad_symlink) {
3138 got_fileindex_entry_filetype_set(ie,
3139 GOT_FILEIDX_MODE_BAD_SYMLINK);
3143 done:
3144 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3145 err = got_error_from_errno("fclose");
3146 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3147 err = got_error_from_errno("fclose");
3148 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3149 err = got_error_from_errno("fclose");
3150 free(id_str);
3151 free(label_deriv2);
3152 free(ondisk_path);
3153 return err;
3156 static const struct got_error *
3157 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3159 struct got_worktree *worktree = arg;
3161 /* Reject merges into a work tree with mixed base commits. */
3162 if (got_fileindex_entry_has_commit(ie) &&
3163 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3164 SHA1_DIGEST_LENGTH) != 0)
3165 return got_error(GOT_ERR_MIXED_COMMITS);
3167 return NULL;
3170 struct check_merge_conflicts_arg {
3171 struct got_worktree *worktree;
3172 struct got_fileindex *fileindex;
3173 struct got_repository *repo;
3176 static const struct got_error *
3177 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3178 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3179 struct got_object_id *id1, struct got_object_id *id2,
3180 const char *path1, const char *path2,
3181 mode_t mode1, mode_t mode2, struct got_repository *repo)
3183 const struct got_error *err = NULL;
3184 struct check_merge_conflicts_arg *a = arg;
3185 unsigned char status;
3186 struct stat sb;
3187 struct got_fileindex_entry *ie;
3188 const char *path = path2 ? path2 : path1;
3189 struct got_object_id *id = id2 ? id2 : id1;
3190 char *ondisk_path;
3192 if (id == NULL)
3193 return NULL;
3195 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3196 if (ie == NULL)
3197 return NULL;
3199 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3200 == -1)
3201 return got_error_from_errno("asprintf");
3203 /* Reject merges into a work tree with conflicted files. */
3204 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3205 free(ondisk_path);
3206 if (err)
3207 return err;
3208 if (status == GOT_STATUS_CONFLICT)
3209 return got_error(GOT_ERR_CONFLICTS);
3211 return NULL;
3214 static const struct got_error *
3215 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3216 const char *fileindex_path, struct got_object_id *commit_id1,
3217 struct got_object_id *commit_id2, struct got_repository *repo,
3218 got_worktree_checkout_cb progress_cb, void *progress_arg,
3219 got_cancel_cb cancel_cb, void *cancel_arg)
3221 const struct got_error *err = NULL, *sync_err;
3222 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3223 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3224 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3225 struct check_merge_conflicts_arg cmc_arg;
3226 struct merge_file_cb_arg arg;
3227 char *label_orig = NULL;
3228 FILE *f1 = NULL, *f2 = NULL;
3229 int fd1 = -1, fd2 = -1;
3231 if (commit_id1) {
3232 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3233 if (err)
3234 goto done;
3235 err = got_object_id_by_path(&tree_id1, repo, commit1,
3236 worktree->path_prefix);
3237 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3238 goto done;
3240 if (tree_id1) {
3241 char *id_str;
3243 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3244 if (err)
3245 goto done;
3247 err = got_object_id_str(&id_str, commit_id1);
3248 if (err)
3249 goto done;
3251 if (asprintf(&label_orig, "%s: commit %s",
3252 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3253 err = got_error_from_errno("asprintf");
3254 free(id_str);
3255 goto done;
3257 free(id_str);
3259 f1 = got_opentemp();
3260 if (f1 == NULL) {
3261 err = got_error_from_errno("got_opentemp");
3262 goto done;
3266 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3267 if (err)
3268 goto done;
3270 err = got_object_id_by_path(&tree_id2, repo, commit2,
3271 worktree->path_prefix);
3272 if (err)
3273 goto done;
3275 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3276 if (err)
3277 goto done;
3279 f2 = got_opentemp();
3280 if (f2 == NULL) {
3281 err = got_error_from_errno("got_opentemp");
3282 goto done;
3285 fd1 = got_opentempfd();
3286 if (fd1 == -1) {
3287 err = got_error_from_errno("got_opentempfd");
3288 goto done;
3291 fd2 = got_opentempfd();
3292 if (fd2 == -1) {
3293 err = got_error_from_errno("got_opentempfd");
3294 goto done;
3297 cmc_arg.worktree = worktree;
3298 cmc_arg.fileindex = fileindex;
3299 cmc_arg.repo = repo;
3300 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3301 check_merge_conflicts, &cmc_arg, 0);
3302 if (err)
3303 goto done;
3305 arg.worktree = worktree;
3306 arg.fileindex = fileindex;
3307 arg.progress_cb = progress_cb;
3308 arg.progress_arg = progress_arg;
3309 arg.cancel_cb = cancel_cb;
3310 arg.cancel_arg = cancel_arg;
3311 arg.label_orig = label_orig;
3312 arg.commit_id2 = commit_id2;
3313 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3314 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3315 merge_file_cb, &arg, 1);
3316 sync_err = sync_fileindex(fileindex, fileindex_path);
3317 if (sync_err && err == NULL)
3318 err = sync_err;
3319 done:
3320 if (commit1)
3321 got_object_commit_close(commit1);
3322 if (commit2)
3323 got_object_commit_close(commit2);
3324 if (tree1)
3325 got_object_tree_close(tree1);
3326 if (tree2)
3327 got_object_tree_close(tree2);
3328 if (f1 && fclose(f1) == EOF && err == NULL)
3329 err = got_error_from_errno("fclose");
3330 if (f2 && fclose(f2) == EOF && err == NULL)
3331 err = got_error_from_errno("fclose");
3332 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3333 err = got_error_from_errno("close");
3334 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3335 err = got_error_from_errno("close");
3336 free(label_orig);
3337 return err;
3340 const struct got_error *
3341 got_worktree_merge_files(struct got_worktree *worktree,
3342 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3343 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3344 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3346 const struct got_error *err, *unlockerr;
3347 char *fileindex_path = NULL;
3348 struct got_fileindex *fileindex = NULL;
3350 err = lock_worktree(worktree, LOCK_EX);
3351 if (err)
3352 return err;
3354 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3355 if (err)
3356 goto done;
3358 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3359 worktree);
3360 if (err)
3361 goto done;
3363 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3364 commit_id2, repo, progress_cb, progress_arg,
3365 cancel_cb, cancel_arg);
3366 done:
3367 if (fileindex)
3368 got_fileindex_free(fileindex);
3369 free(fileindex_path);
3370 unlockerr = lock_worktree(worktree, LOCK_SH);
3371 if (unlockerr && err == NULL)
3372 err = unlockerr;
3373 return err;
3376 struct diff_dir_cb_arg {
3377 struct got_fileindex *fileindex;
3378 struct got_worktree *worktree;
3379 const char *status_path;
3380 size_t status_path_len;
3381 struct got_repository *repo;
3382 got_worktree_status_cb status_cb;
3383 void *status_arg;
3384 got_cancel_cb cancel_cb;
3385 void *cancel_arg;
3386 /* A pathlist containing per-directory pathlists of ignore patterns. */
3387 struct got_pathlist_head *ignores;
3388 int report_unchanged;
3389 int no_ignores;
3392 static const struct got_error *
3393 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3394 int dirfd, const char *de_name,
3395 got_worktree_status_cb status_cb, void *status_arg,
3396 struct got_repository *repo, int report_unchanged)
3398 const struct got_error *err = NULL;
3399 unsigned char status = GOT_STATUS_NO_CHANGE;
3400 unsigned char staged_status;
3401 struct stat sb;
3402 struct got_object_id blob_id, commit_id, staged_blob_id;
3403 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3404 struct got_object_id *staged_blob_idp = NULL;
3406 staged_status = get_staged_status(ie);
3407 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3408 if (err)
3409 return err;
3411 if (status == GOT_STATUS_NO_CHANGE &&
3412 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3413 return NULL;
3415 if (got_fileindex_entry_has_blob(ie))
3416 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3417 if (got_fileindex_entry_has_commit(ie))
3418 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3419 if (staged_status == GOT_STATUS_ADD ||
3420 staged_status == GOT_STATUS_MODIFY) {
3421 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3422 &staged_blob_id, ie);
3425 return (*status_cb)(status_arg, status, staged_status,
3426 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3429 static const struct got_error *
3430 status_old_new(void *arg, struct got_fileindex_entry *ie,
3431 struct dirent *de, const char *parent_path, int dirfd)
3433 const struct got_error *err = NULL;
3434 struct diff_dir_cb_arg *a = arg;
3435 char *abspath;
3437 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3438 return got_error(GOT_ERR_CANCELLED);
3440 if (got_path_cmp(parent_path, a->status_path,
3441 strlen(parent_path), a->status_path_len) != 0 &&
3442 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3443 return NULL;
3445 if (parent_path[0]) {
3446 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3447 parent_path, de->d_name) == -1)
3448 return got_error_from_errno("asprintf");
3449 } else {
3450 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3451 de->d_name) == -1)
3452 return got_error_from_errno("asprintf");
3455 err = report_file_status(ie, abspath, dirfd, de->d_name,
3456 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3457 free(abspath);
3458 return err;
3461 static const struct got_error *
3462 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3464 struct diff_dir_cb_arg *a = arg;
3465 struct got_object_id blob_id, commit_id;
3466 unsigned char status;
3468 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3469 return got_error(GOT_ERR_CANCELLED);
3471 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3472 return NULL;
3474 got_fileindex_entry_get_blob_id(&blob_id, ie);
3475 got_fileindex_entry_get_commit_id(&commit_id, ie);
3476 if (got_fileindex_entry_has_file_on_disk(ie))
3477 status = GOT_STATUS_MISSING;
3478 else
3479 status = GOT_STATUS_DELETE;
3480 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3481 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3484 static void
3485 free_ignores(struct got_pathlist_head *ignores)
3487 struct got_pathlist_entry *pe;
3489 TAILQ_FOREACH(pe, ignores, entry) {
3490 struct got_pathlist_head *ignorelist = pe->data;
3492 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3494 got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3497 static const struct got_error *
3498 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3500 const struct got_error *err = NULL;
3501 struct got_pathlist_entry *pe = NULL;
3502 struct got_pathlist_head *ignorelist;
3503 char *line = NULL, *pattern, *dirpath = NULL;
3504 size_t linesize = 0;
3505 ssize_t linelen;
3507 ignorelist = calloc(1, sizeof(*ignorelist));
3508 if (ignorelist == NULL)
3509 return got_error_from_errno("calloc");
3510 TAILQ_INIT(ignorelist);
3512 while ((linelen = getline(&line, &linesize, f)) != -1) {
3513 if (linelen > 0 && line[linelen - 1] == '\n')
3514 line[linelen - 1] = '\0';
3516 /* Git's ignores may contain comments. */
3517 if (line[0] == '#')
3518 continue;
3520 /* Git's negated patterns are not (yet?) supported. */
3521 if (line[0] == '!')
3522 continue;
3524 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3525 line) == -1) {
3526 err = got_error_from_errno("asprintf");
3527 goto done;
3529 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3530 if (err)
3531 goto done;
3533 if (ferror(f)) {
3534 err = got_error_from_errno("getline");
3535 goto done;
3538 dirpath = strdup(path);
3539 if (dirpath == NULL) {
3540 err = got_error_from_errno("strdup");
3541 goto done;
3543 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3544 done:
3545 free(line);
3546 if (err || pe == NULL) {
3547 free(dirpath);
3548 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3550 return err;
3553 static int
3554 match_path(const char *pattern, size_t pattern_len, const char *path,
3555 int flags)
3557 char buf[PATH_MAX];
3560 * Trailing slashes signify directories.
3561 * Append a * to make such patterns conform to fnmatch rules.
3563 if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3564 if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3565 return FNM_NOMATCH; /* XXX */
3567 return fnmatch(buf, path, flags);
3570 return fnmatch(pattern, path, flags);
3573 static int
3574 match_ignores(struct got_pathlist_head *ignores, const char *path)
3576 struct got_pathlist_entry *pe;
3578 /* Handle patterns which match in all directories. */
3579 TAILQ_FOREACH(pe, ignores, entry) {
3580 struct got_pathlist_head *ignorelist = pe->data;
3581 struct got_pathlist_entry *pi;
3583 TAILQ_FOREACH(pi, ignorelist, entry) {
3584 const char *p;
3586 if (pi->path_len < 3 ||
3587 strncmp(pi->path, "**/", 3) != 0)
3588 continue;
3589 p = path;
3590 while (*p) {
3591 if (match_path(pi->path + 3,
3592 pi->path_len - 3, p,
3593 FNM_PATHNAME | FNM_LEADING_DIR)) {
3594 /* Retry in next directory. */
3595 while (*p && *p != '/')
3596 p++;
3597 while (*p == '/')
3598 p++;
3599 continue;
3601 return 1;
3607 * The ignores pathlist contains ignore lists from children before
3608 * parents, so we can find the most specific ignorelist by walking
3609 * ignores backwards.
3611 pe = TAILQ_LAST(ignores, got_pathlist_head);
3612 while (pe) {
3613 if (got_path_is_child(path, pe->path, pe->path_len)) {
3614 struct got_pathlist_head *ignorelist = pe->data;
3615 struct got_pathlist_entry *pi;
3616 TAILQ_FOREACH(pi, ignorelist, entry) {
3617 int flags = FNM_LEADING_DIR;
3618 if (strstr(pi->path, "/**/") == NULL)
3619 flags |= FNM_PATHNAME;
3620 if (match_path(pi->path, pi->path_len,
3621 path, flags))
3622 continue;
3623 return 1;
3626 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3629 return 0;
3632 static const struct got_error *
3633 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3634 const char *path, int dirfd, const char *ignores_filename)
3636 const struct got_error *err = NULL;
3637 char *ignorespath;
3638 int fd = -1;
3639 FILE *ignoresfile = NULL;
3641 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3642 path[0] ? "/" : "", ignores_filename) == -1)
3643 return got_error_from_errno("asprintf");
3645 if (dirfd != -1) {
3646 fd = openat(dirfd, ignores_filename,
3647 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3648 if (fd == -1) {
3649 if (errno != ENOENT && errno != EACCES)
3650 err = got_error_from_errno2("openat",
3651 ignorespath);
3652 } else {
3653 ignoresfile = fdopen(fd, "r");
3654 if (ignoresfile == NULL)
3655 err = got_error_from_errno2("fdopen",
3656 ignorespath);
3657 else {
3658 fd = -1;
3659 err = read_ignores(ignores, path, ignoresfile);
3662 } else {
3663 ignoresfile = fopen(ignorespath, "re");
3664 if (ignoresfile == NULL) {
3665 if (errno != ENOENT && errno != EACCES)
3666 err = got_error_from_errno2("fopen",
3667 ignorespath);
3668 } else
3669 err = read_ignores(ignores, path, ignoresfile);
3672 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3673 err = got_error_from_errno2("fclose", path);
3674 if (fd != -1 && close(fd) == -1 && err == NULL)
3675 err = got_error_from_errno2("close", path);
3676 free(ignorespath);
3677 return err;
3680 static const struct got_error *
3681 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3682 int dirfd)
3684 const struct got_error *err = NULL;
3685 struct diff_dir_cb_arg *a = arg;
3686 char *path = NULL;
3688 if (ignore != NULL)
3689 *ignore = 0;
3691 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3692 return got_error(GOT_ERR_CANCELLED);
3694 if (parent_path[0]) {
3695 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3696 return got_error_from_errno("asprintf");
3697 } else {
3698 path = de->d_name;
3701 if (de->d_type == DT_DIR) {
3702 if (!a->no_ignores && ignore != NULL &&
3703 match_ignores(a->ignores, path))
3704 *ignore = 1;
3705 } else if (!match_ignores(a->ignores, path) &&
3706 got_path_is_child(path, a->status_path, a->status_path_len))
3707 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3708 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3709 if (parent_path[0])
3710 free(path);
3711 return err;
3714 static const struct got_error *
3715 status_traverse(void *arg, const char *path, int dirfd)
3717 const struct got_error *err = NULL;
3718 struct diff_dir_cb_arg *a = arg;
3720 if (a->no_ignores)
3721 return NULL;
3723 err = add_ignores(a->ignores, a->worktree->root_path,
3724 path, dirfd, ".cvsignore");
3725 if (err)
3726 return err;
3728 err = add_ignores(a->ignores, a->worktree->root_path, path,
3729 dirfd, ".gitignore");
3731 return err;
3734 static const struct got_error *
3735 report_single_file_status(const char *path, const char *ondisk_path,
3736 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3737 void *status_arg, struct got_repository *repo, int report_unchanged,
3738 struct got_pathlist_head *ignores, int no_ignores)
3740 struct got_fileindex_entry *ie;
3741 struct stat sb;
3743 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3744 if (ie)
3745 return report_file_status(ie, ondisk_path, -1, NULL,
3746 status_cb, status_arg, repo, report_unchanged);
3748 if (lstat(ondisk_path, &sb) == -1) {
3749 if (errno != ENOENT)
3750 return got_error_from_errno2("lstat", ondisk_path);
3751 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3752 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3755 if (!no_ignores && match_ignores(ignores, path))
3756 return NULL;
3758 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3759 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3760 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3762 return NULL;
3765 static const struct got_error *
3766 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3767 const char *root_path, const char *path)
3769 const struct got_error *err;
3770 char *parent_path, *next_parent_path = NULL;
3772 err = add_ignores(ignores, root_path, "", -1,
3773 ".cvsignore");
3774 if (err)
3775 return err;
3777 err = add_ignores(ignores, root_path, "", -1,
3778 ".gitignore");
3779 if (err)
3780 return err;
3782 err = got_path_dirname(&parent_path, path);
3783 if (err) {
3784 if (err->code == GOT_ERR_BAD_PATH)
3785 return NULL; /* cannot traverse parent */
3786 return err;
3788 for (;;) {
3789 err = add_ignores(ignores, root_path, parent_path, -1,
3790 ".cvsignore");
3791 if (err)
3792 break;
3793 err = add_ignores(ignores, root_path, parent_path, -1,
3794 ".gitignore");
3795 if (err)
3796 break;
3797 err = got_path_dirname(&next_parent_path, parent_path);
3798 if (err) {
3799 if (err->code == GOT_ERR_BAD_PATH)
3800 err = NULL; /* traversed everything */
3801 break;
3803 if (got_path_is_root_dir(parent_path))
3804 break;
3805 free(parent_path);
3806 parent_path = next_parent_path;
3807 next_parent_path = NULL;
3810 free(parent_path);
3811 free(next_parent_path);
3812 return err;
3815 static const struct got_error *
3816 worktree_status(struct got_worktree *worktree, const char *path,
3817 struct got_fileindex *fileindex, struct got_repository *repo,
3818 got_worktree_status_cb status_cb, void *status_arg,
3819 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3820 int report_unchanged)
3822 const struct got_error *err = NULL;
3823 int fd = -1;
3824 struct got_fileindex_diff_dir_cb fdiff_cb;
3825 struct diff_dir_cb_arg arg;
3826 char *ondisk_path = NULL;
3827 struct got_pathlist_head ignores;
3828 struct got_fileindex_entry *ie;
3830 TAILQ_INIT(&ignores);
3832 if (asprintf(&ondisk_path, "%s%s%s",
3833 worktree->root_path, path[0] ? "/" : "", path) == -1)
3834 return got_error_from_errno("asprintf");
3836 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3837 if (ie) {
3838 err = report_single_file_status(path, ondisk_path,
3839 fileindex, status_cb, status_arg, repo,
3840 report_unchanged, &ignores, no_ignores);
3841 goto done;
3844 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3845 if (fd == -1) {
3846 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3847 !got_err_open_nofollow_on_symlink())
3848 err = got_error_from_errno2("open", ondisk_path);
3849 else {
3850 if (!no_ignores) {
3851 err = add_ignores_from_parent_paths(&ignores,
3852 worktree->root_path, ondisk_path);
3853 if (err)
3854 goto done;
3856 err = report_single_file_status(path, ondisk_path,
3857 fileindex, status_cb, status_arg, repo,
3858 report_unchanged, &ignores, no_ignores);
3860 } else {
3861 fdiff_cb.diff_old_new = status_old_new;
3862 fdiff_cb.diff_old = status_old;
3863 fdiff_cb.diff_new = status_new;
3864 fdiff_cb.diff_traverse = status_traverse;
3865 arg.fileindex = fileindex;
3866 arg.worktree = worktree;
3867 arg.status_path = path;
3868 arg.status_path_len = strlen(path);
3869 arg.repo = repo;
3870 arg.status_cb = status_cb;
3871 arg.status_arg = status_arg;
3872 arg.cancel_cb = cancel_cb;
3873 arg.cancel_arg = cancel_arg;
3874 arg.report_unchanged = report_unchanged;
3875 arg.no_ignores = no_ignores;
3876 if (!no_ignores) {
3877 err = add_ignores_from_parent_paths(&ignores,
3878 worktree->root_path, path);
3879 if (err)
3880 goto done;
3882 arg.ignores = &ignores;
3883 err = got_fileindex_diff_dir(fileindex, fd,
3884 worktree->root_path, path, repo, &fdiff_cb, &arg);
3886 done:
3887 free_ignores(&ignores);
3888 if (fd != -1 && close(fd) == -1 && err == NULL)
3889 err = got_error_from_errno("close");
3890 free(ondisk_path);
3891 return err;
3894 const struct got_error *
3895 got_worktree_status(struct got_worktree *worktree,
3896 struct got_pathlist_head *paths, struct got_repository *repo,
3897 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3898 got_cancel_cb cancel_cb, void *cancel_arg)
3900 const struct got_error *err = NULL;
3901 char *fileindex_path = NULL;
3902 struct got_fileindex *fileindex = NULL;
3903 struct got_pathlist_entry *pe;
3905 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3906 if (err)
3907 return err;
3909 TAILQ_FOREACH(pe, paths, entry) {
3910 err = worktree_status(worktree, pe->path, fileindex, repo,
3911 status_cb, status_arg, cancel_cb, cancel_arg,
3912 no_ignores, 0);
3913 if (err)
3914 break;
3916 free(fileindex_path);
3917 got_fileindex_free(fileindex);
3918 return err;
3921 const struct got_error *
3922 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3923 const char *arg)
3925 const struct got_error *err = NULL;
3926 char *resolved = NULL, *cwd = NULL, *path = NULL;
3927 size_t len;
3928 struct stat sb;
3929 char *abspath = NULL;
3930 char canonpath[PATH_MAX];
3932 *wt_path = NULL;
3934 cwd = getcwd(NULL, 0);
3935 if (cwd == NULL)
3936 return got_error_from_errno("getcwd");
3938 if (lstat(arg, &sb) == -1) {
3939 if (errno != ENOENT) {
3940 err = got_error_from_errno2("lstat", arg);
3941 goto done;
3943 sb.st_mode = 0;
3945 if (S_ISLNK(sb.st_mode)) {
3947 * We cannot use realpath(3) with symlinks since we want to
3948 * operate on the symlink itself.
3949 * But we can make the path absolute, assuming it is relative
3950 * to the current working directory, and then canonicalize it.
3952 if (!got_path_is_absolute(arg)) {
3953 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3954 err = got_error_from_errno("asprintf");
3955 goto done;
3959 err = got_canonpath(abspath ? abspath : arg, canonpath,
3960 sizeof(canonpath));
3961 if (err)
3962 goto done;
3963 resolved = strdup(canonpath);
3964 if (resolved == NULL) {
3965 err = got_error_from_errno("strdup");
3966 goto done;
3968 } else {
3969 resolved = realpath(arg, NULL);
3970 if (resolved == NULL) {
3971 if (errno != ENOENT) {
3972 err = got_error_from_errno2("realpath", arg);
3973 goto done;
3975 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3976 err = got_error_from_errno("asprintf");
3977 goto done;
3979 err = got_canonpath(abspath, canonpath,
3980 sizeof(canonpath));
3981 if (err)
3982 goto done;
3983 resolved = strdup(canonpath);
3984 if (resolved == NULL) {
3985 err = got_error_from_errno("strdup");
3986 goto done;
3991 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3992 strlen(got_worktree_get_root_path(worktree)))) {
3993 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3994 goto done;
3997 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3998 err = got_path_skip_common_ancestor(&path,
3999 got_worktree_get_root_path(worktree), resolved);
4000 if (err)
4001 goto done;
4002 } else {
4003 path = strdup("");
4004 if (path == NULL) {
4005 err = got_error_from_errno("strdup");
4006 goto done;
4010 /* XXX status walk can't deal with trailing slash! */
4011 len = strlen(path);
4012 while (len > 0 && path[len - 1] == '/') {
4013 path[len - 1] = '\0';
4014 len--;
4016 done:
4017 free(abspath);
4018 free(resolved);
4019 free(cwd);
4020 if (err == NULL)
4021 *wt_path = path;
4022 else
4023 free(path);
4024 return err;
4027 struct schedule_addition_args {
4028 struct got_worktree *worktree;
4029 struct got_fileindex *fileindex;
4030 got_worktree_checkout_cb progress_cb;
4031 void *progress_arg;
4032 struct got_repository *repo;
4035 static const struct got_error *
4036 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4037 const char *relpath, struct got_object_id *blob_id,
4038 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4039 int dirfd, const char *de_name)
4041 struct schedule_addition_args *a = arg;
4042 const struct got_error *err = NULL;
4043 struct got_fileindex_entry *ie;
4044 struct stat sb;
4045 char *ondisk_path;
4047 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4048 relpath) == -1)
4049 return got_error_from_errno("asprintf");
4051 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4052 if (ie) {
4053 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4054 de_name, a->repo);
4055 if (err)
4056 goto done;
4057 /* Re-adding an existing entry is a no-op. */
4058 if (status == GOT_STATUS_ADD)
4059 goto done;
4060 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4061 if (err)
4062 goto done;
4065 if (status != GOT_STATUS_UNVERSIONED) {
4066 if (status == GOT_STATUS_NONEXISTENT)
4067 err = got_error_set_errno(ENOENT, ondisk_path);
4068 else
4069 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4070 goto done;
4073 err = got_fileindex_entry_alloc(&ie, relpath);
4074 if (err)
4075 goto done;
4076 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4077 relpath, NULL, NULL, 1);
4078 if (err) {
4079 got_fileindex_entry_free(ie);
4080 goto done;
4082 err = got_fileindex_entry_add(a->fileindex, ie);
4083 if (err) {
4084 got_fileindex_entry_free(ie);
4085 goto done;
4087 done:
4088 free(ondisk_path);
4089 if (err)
4090 return err;
4091 if (status == GOT_STATUS_ADD)
4092 return NULL;
4093 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4096 const struct got_error *
4097 got_worktree_schedule_add(struct got_worktree *worktree,
4098 struct got_pathlist_head *paths,
4099 got_worktree_checkout_cb progress_cb, void *progress_arg,
4100 struct got_repository *repo, int no_ignores)
4102 struct got_fileindex *fileindex = NULL;
4103 char *fileindex_path = NULL;
4104 const struct got_error *err = NULL, *sync_err, *unlockerr;
4105 struct got_pathlist_entry *pe;
4106 struct schedule_addition_args saa;
4108 err = lock_worktree(worktree, LOCK_EX);
4109 if (err)
4110 return err;
4112 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4113 if (err)
4114 goto done;
4116 saa.worktree = worktree;
4117 saa.fileindex = fileindex;
4118 saa.progress_cb = progress_cb;
4119 saa.progress_arg = progress_arg;
4120 saa.repo = repo;
4122 TAILQ_FOREACH(pe, paths, entry) {
4123 err = worktree_status(worktree, pe->path, fileindex, repo,
4124 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4125 if (err)
4126 break;
4128 sync_err = sync_fileindex(fileindex, fileindex_path);
4129 if (sync_err && err == NULL)
4130 err = sync_err;
4131 done:
4132 free(fileindex_path);
4133 if (fileindex)
4134 got_fileindex_free(fileindex);
4135 unlockerr = lock_worktree(worktree, LOCK_SH);
4136 if (unlockerr && err == NULL)
4137 err = unlockerr;
4138 return err;
4141 struct schedule_deletion_args {
4142 struct got_worktree *worktree;
4143 struct got_fileindex *fileindex;
4144 got_worktree_delete_cb progress_cb;
4145 void *progress_arg;
4146 struct got_repository *repo;
4147 int delete_local_mods;
4148 int keep_on_disk;
4149 int ignore_missing_paths;
4150 const char *status_codes;
4153 static const struct got_error *
4154 schedule_for_deletion(void *arg, unsigned char status,
4155 unsigned char staged_status, const char *relpath,
4156 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4157 struct got_object_id *commit_id, int dirfd, const char *de_name)
4159 struct schedule_deletion_args *a = arg;
4160 const struct got_error *err = NULL;
4161 struct got_fileindex_entry *ie = NULL;
4162 struct stat sb;
4163 char *ondisk_path;
4165 if (status == GOT_STATUS_NONEXISTENT) {
4166 if (a->ignore_missing_paths)
4167 return NULL;
4168 return got_error_set_errno(ENOENT, relpath);
4171 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4172 if (ie == NULL)
4173 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4175 staged_status = get_staged_status(ie);
4176 if (staged_status != GOT_STATUS_NO_CHANGE) {
4177 if (staged_status == GOT_STATUS_DELETE)
4178 return NULL;
4179 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4182 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4183 relpath) == -1)
4184 return got_error_from_errno("asprintf");
4186 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4187 a->repo);
4188 if (err)
4189 goto done;
4191 if (a->status_codes) {
4192 size_t ncodes = strlen(a->status_codes);
4193 int i;
4194 for (i = 0; i < ncodes ; i++) {
4195 if (status == a->status_codes[i])
4196 break;
4198 if (i == ncodes) {
4199 /* Do not delete files in non-matching status. */
4200 free(ondisk_path);
4201 return NULL;
4203 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4204 a->status_codes[i] != GOT_STATUS_MISSING) {
4205 static char msg[64];
4206 snprintf(msg, sizeof(msg),
4207 "invalid status code '%c'", a->status_codes[i]);
4208 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4209 goto done;
4213 if (status != GOT_STATUS_NO_CHANGE) {
4214 if (status == GOT_STATUS_DELETE)
4215 goto done;
4216 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4217 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4218 goto done;
4220 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4221 err = got_error_set_errno(ENOENT, relpath);
4222 goto done;
4224 if (status != GOT_STATUS_MODIFY &&
4225 status != GOT_STATUS_MISSING) {
4226 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4227 goto done;
4231 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4232 size_t root_len;
4234 if (dirfd != -1) {
4235 if (unlinkat(dirfd, de_name, 0) == -1) {
4236 err = got_error_from_errno2("unlinkat",
4237 ondisk_path);
4238 goto done;
4240 } else if (unlink(ondisk_path) == -1) {
4241 err = got_error_from_errno2("unlink", ondisk_path);
4242 goto done;
4245 root_len = strlen(a->worktree->root_path);
4246 do {
4247 char *parent;
4248 err = got_path_dirname(&parent, ondisk_path);
4249 if (err)
4250 goto done;
4251 free(ondisk_path);
4252 ondisk_path = parent;
4253 if (rmdir(ondisk_path) == -1) {
4254 if (errno != ENOTEMPTY)
4255 err = got_error_from_errno2("rmdir",
4256 ondisk_path);
4257 break;
4259 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4260 strlen(ondisk_path), root_len) != 0);
4263 got_fileindex_entry_mark_deleted_from_disk(ie);
4264 done:
4265 free(ondisk_path);
4266 if (err)
4267 return err;
4268 if (status == GOT_STATUS_DELETE)
4269 return NULL;
4270 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4271 staged_status, relpath);
4274 const struct got_error *
4275 got_worktree_schedule_delete(struct got_worktree *worktree,
4276 struct got_pathlist_head *paths, int delete_local_mods,
4277 const char *status_codes,
4278 got_worktree_delete_cb progress_cb, void *progress_arg,
4279 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4281 struct got_fileindex *fileindex = NULL;
4282 char *fileindex_path = NULL;
4283 const struct got_error *err = NULL, *sync_err, *unlockerr;
4284 struct got_pathlist_entry *pe;
4285 struct schedule_deletion_args sda;
4287 err = lock_worktree(worktree, LOCK_EX);
4288 if (err)
4289 return err;
4291 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4292 if (err)
4293 goto done;
4295 sda.worktree = worktree;
4296 sda.fileindex = fileindex;
4297 sda.progress_cb = progress_cb;
4298 sda.progress_arg = progress_arg;
4299 sda.repo = repo;
4300 sda.delete_local_mods = delete_local_mods;
4301 sda.keep_on_disk = keep_on_disk;
4302 sda.ignore_missing_paths = ignore_missing_paths;
4303 sda.status_codes = status_codes;
4305 TAILQ_FOREACH(pe, paths, entry) {
4306 err = worktree_status(worktree, pe->path, fileindex, repo,
4307 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4308 if (err)
4309 break;
4311 sync_err = sync_fileindex(fileindex, fileindex_path);
4312 if (sync_err && err == NULL)
4313 err = sync_err;
4314 done:
4315 free(fileindex_path);
4316 if (fileindex)
4317 got_fileindex_free(fileindex);
4318 unlockerr = lock_worktree(worktree, LOCK_SH);
4319 if (unlockerr && err == NULL)
4320 err = unlockerr;
4321 return err;
4324 static const struct got_error *
4325 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4327 const struct got_error *err = NULL;
4328 char *line = NULL;
4329 size_t linesize = 0, n;
4330 ssize_t linelen;
4332 linelen = getline(&line, &linesize, infile);
4333 if (linelen == -1) {
4334 if (ferror(infile)) {
4335 err = got_error_from_errno("getline");
4336 goto done;
4338 return NULL;
4340 if (outfile) {
4341 n = fwrite(line, 1, linelen, outfile);
4342 if (n != linelen) {
4343 err = got_ferror(outfile, GOT_ERR_IO);
4344 goto done;
4347 if (rejectfile) {
4348 n = fwrite(line, 1, linelen, rejectfile);
4349 if (n != linelen)
4350 err = got_ferror(rejectfile, GOT_ERR_IO);
4352 done:
4353 free(line);
4354 return err;
4357 static const struct got_error *
4358 skip_one_line(FILE *f)
4360 char *line = NULL;
4361 size_t linesize = 0;
4362 ssize_t linelen;
4364 linelen = getline(&line, &linesize, f);
4365 if (linelen == -1) {
4366 if (ferror(f))
4367 return got_error_from_errno("getline");
4368 return NULL;
4370 free(line);
4371 return NULL;
4374 static const struct got_error *
4375 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4376 int start_old, int end_old, int start_new, int end_new,
4377 FILE *outfile, FILE *rejectfile)
4379 const struct got_error *err;
4381 /* Copy old file's lines leading up to patch. */
4382 while (!feof(f1) && *line_cur1 < start_old) {
4383 err = copy_one_line(f1, outfile, NULL);
4384 if (err)
4385 return err;
4386 (*line_cur1)++;
4388 /* Skip new file's lines leading up to patch. */
4389 while (!feof(f2) && *line_cur2 < start_new) {
4390 if (rejectfile)
4391 err = copy_one_line(f2, NULL, rejectfile);
4392 else
4393 err = skip_one_line(f2);
4394 if (err)
4395 return err;
4396 (*line_cur2)++;
4398 /* Copy patched lines. */
4399 while (!feof(f2) && *line_cur2 <= end_new) {
4400 err = copy_one_line(f2, outfile, NULL);
4401 if (err)
4402 return err;
4403 (*line_cur2)++;
4405 /* Skip over old file's replaced lines. */
4406 while (!feof(f1) && *line_cur1 <= end_old) {
4407 if (rejectfile)
4408 err = copy_one_line(f1, NULL, rejectfile);
4409 else
4410 err = skip_one_line(f1);
4411 if (err)
4412 return err;
4413 (*line_cur1)++;
4416 return NULL;
4419 static const struct got_error *
4420 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4421 FILE *outfile, FILE *rejectfile)
4423 const struct got_error *err;
4425 if (outfile) {
4426 /* Copy old file's lines until EOF. */
4427 while (!feof(f1)) {
4428 err = copy_one_line(f1, outfile, NULL);
4429 if (err)
4430 return err;
4431 (*line_cur1)++;
4434 if (rejectfile) {
4435 /* Copy new file's lines until EOF. */
4436 while (!feof(f2)) {
4437 err = copy_one_line(f2, NULL, rejectfile);
4438 if (err)
4439 return err;
4440 (*line_cur2)++;
4444 return NULL;
4447 static const struct got_error *
4448 apply_or_reject_change(int *choice, int *nchunks_used,
4449 struct diff_result *diff_result, int n,
4450 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4451 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4452 got_worktree_patch_cb patch_cb, void *patch_arg)
4454 const struct got_error *err = NULL;
4455 struct diff_chunk_context cc = {};
4456 int start_old, end_old, start_new, end_new;
4457 FILE *hunkfile;
4458 struct diff_output_unidiff_state *diff_state;
4459 struct diff_input_info diff_info;
4460 int rc;
4462 *choice = GOT_PATCH_CHOICE_NONE;
4464 /* Get changed line numbers without context lines for copy_change(). */
4465 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4466 start_old = cc.left.start;
4467 end_old = cc.left.end;
4468 start_new = cc.right.start;
4469 end_new = cc.right.end;
4471 /* Get the same change with context lines for display. */
4472 memset(&cc, 0, sizeof(cc));
4473 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4475 memset(&diff_info, 0, sizeof(diff_info));
4476 diff_info.left_path = relpath;
4477 diff_info.right_path = relpath;
4479 diff_state = diff_output_unidiff_state_alloc();
4480 if (diff_state == NULL)
4481 return got_error_set_errno(ENOMEM,
4482 "diff_output_unidiff_state_alloc");
4484 hunkfile = got_opentemp();
4485 if (hunkfile == NULL) {
4486 err = got_error_from_errno("got_opentemp");
4487 goto done;
4490 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4491 diff_result, &cc);
4492 if (rc != DIFF_RC_OK) {
4493 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4494 goto done;
4497 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4498 err = got_ferror(hunkfile, GOT_ERR_IO);
4499 goto done;
4502 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4503 hunkfile, changeno, nchanges);
4504 if (err)
4505 goto done;
4507 switch (*choice) {
4508 case GOT_PATCH_CHOICE_YES:
4509 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4510 end_old, start_new, end_new, outfile, rejectfile);
4511 break;
4512 case GOT_PATCH_CHOICE_NO:
4513 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4514 end_old, start_new, end_new, rejectfile, outfile);
4515 break;
4516 case GOT_PATCH_CHOICE_QUIT:
4517 break;
4518 default:
4519 err = got_error(GOT_ERR_PATCH_CHOICE);
4520 break;
4522 done:
4523 diff_output_unidiff_state_free(diff_state);
4524 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4525 err = got_error_from_errno("fclose");
4526 return err;
4529 struct revert_file_args {
4530 struct got_worktree *worktree;
4531 struct got_fileindex *fileindex;
4532 got_worktree_checkout_cb progress_cb;
4533 void *progress_arg;
4534 got_worktree_patch_cb patch_cb;
4535 void *patch_arg;
4536 struct got_repository *repo;
4537 int unlink_added_files;
4540 static const struct got_error *
4541 create_patched_content(char **path_outfile, int reverse_patch,
4542 struct got_object_id *blob_id, const char *path2,
4543 int dirfd2, const char *de_name2,
4544 const char *relpath, struct got_repository *repo,
4545 got_worktree_patch_cb patch_cb, void *patch_arg)
4547 const struct got_error *err, *free_err;
4548 struct got_blob_object *blob = NULL;
4549 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4550 int fd = -1, fd2 = -1;
4551 char link_target[PATH_MAX];
4552 ssize_t link_len = 0;
4553 char *path1 = NULL, *id_str = NULL;
4554 struct stat sb2;
4555 struct got_diffreg_result *diffreg_result = NULL;
4556 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4557 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4559 *path_outfile = NULL;
4561 err = got_object_id_str(&id_str, blob_id);
4562 if (err)
4563 return err;
4565 if (dirfd2 != -1) {
4566 fd2 = openat(dirfd2, de_name2,
4567 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4568 if (fd2 == -1) {
4569 if (!got_err_open_nofollow_on_symlink()) {
4570 err = got_error_from_errno2("openat", path2);
4571 goto done;
4573 link_len = readlinkat(dirfd2, de_name2,
4574 link_target, sizeof(link_target));
4575 if (link_len == -1) {
4576 return got_error_from_errno2("readlinkat",
4577 path2);
4579 sb2.st_mode = S_IFLNK;
4580 sb2.st_size = link_len;
4582 } else {
4583 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4584 if (fd2 == -1) {
4585 if (!got_err_open_nofollow_on_symlink()) {
4586 err = got_error_from_errno2("open", path2);
4587 goto done;
4589 link_len = readlink(path2, link_target,
4590 sizeof(link_target));
4591 if (link_len == -1)
4592 return got_error_from_errno2("readlink", path2);
4593 sb2.st_mode = S_IFLNK;
4594 sb2.st_size = link_len;
4597 if (fd2 != -1) {
4598 if (fstat(fd2, &sb2) == -1) {
4599 err = got_error_from_errno2("fstat", path2);
4600 goto done;
4603 f2 = fdopen(fd2, "r");
4604 if (f2 == NULL) {
4605 err = got_error_from_errno2("fdopen", path2);
4606 goto done;
4608 fd2 = -1;
4609 } else {
4610 size_t n;
4611 f2 = got_opentemp();
4612 if (f2 == NULL) {
4613 err = got_error_from_errno2("got_opentemp", path2);
4614 goto done;
4616 n = fwrite(link_target, 1, link_len, f2);
4617 if (n != link_len) {
4618 err = got_ferror(f2, GOT_ERR_IO);
4619 goto done;
4621 if (fflush(f2) == EOF) {
4622 err = got_error_from_errno("fflush");
4623 goto done;
4625 rewind(f2);
4628 fd = got_opentempfd();
4629 if (fd == -1) {
4630 err = got_error_from_errno("got_opentempfd");
4631 goto done;
4634 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4635 if (err)
4636 goto done;
4638 err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4639 if (err)
4640 goto done;
4642 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4643 if (err)
4644 goto done;
4646 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4647 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4648 if (err)
4649 goto done;
4651 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4652 "");
4653 if (err)
4654 goto done;
4656 if (fseek(f1, 0L, SEEK_SET) == -1)
4657 return got_ferror(f1, GOT_ERR_IO);
4658 if (fseek(f2, 0L, SEEK_SET) == -1)
4659 return got_ferror(f2, GOT_ERR_IO);
4661 /* Count the number of actual changes in the diff result. */
4662 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4663 struct diff_chunk_context cc = {};
4664 diff_chunk_context_load_change(&cc, &nchunks_used,
4665 diffreg_result->result, n, 0);
4666 nchanges++;
4668 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4669 int choice;
4670 err = apply_or_reject_change(&choice, &nchunks_used,
4671 diffreg_result->result, n, relpath, f1, f2,
4672 &line_cur1, &line_cur2,
4673 reverse_patch ? NULL : outfile,
4674 reverse_patch ? outfile : NULL,
4675 ++i, nchanges, patch_cb, patch_arg);
4676 if (err)
4677 goto done;
4678 if (choice == GOT_PATCH_CHOICE_YES)
4679 have_content = 1;
4680 else if (choice == GOT_PATCH_CHOICE_QUIT)
4681 break;
4683 if (have_content) {
4684 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4685 reverse_patch ? NULL : outfile,
4686 reverse_patch ? outfile : NULL);
4687 if (err)
4688 goto done;
4690 if (!S_ISLNK(sb2.st_mode)) {
4691 mode_t mode;
4693 mode = apply_umask(sb2.st_mode);
4694 if (fchmod(fileno(outfile), mode) == -1) {
4695 err = got_error_from_errno2("fchmod", path2);
4696 goto done;
4700 done:
4701 free(id_str);
4702 if (fd != -1 && close(fd) == -1 && err == NULL)
4703 err = got_error_from_errno("close");
4704 if (blob)
4705 got_object_blob_close(blob);
4706 free_err = got_diffreg_result_free(diffreg_result);
4707 if (err == NULL)
4708 err = free_err;
4709 if (f1 && fclose(f1) == EOF && err == NULL)
4710 err = got_error_from_errno2("fclose", path1);
4711 if (f2 && fclose(f2) == EOF && err == NULL)
4712 err = got_error_from_errno2("fclose", path2);
4713 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4714 err = got_error_from_errno2("close", path2);
4715 if (outfile && fclose(outfile) == EOF && err == NULL)
4716 err = got_error_from_errno2("fclose", *path_outfile);
4717 if (path1 && unlink(path1) == -1 && err == NULL)
4718 err = got_error_from_errno2("unlink", path1);
4719 if (err || !have_content) {
4720 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4721 err = got_error_from_errno2("unlink", *path_outfile);
4722 free(*path_outfile);
4723 *path_outfile = NULL;
4725 free(path1);
4726 return err;
4729 static const struct got_error *
4730 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4731 const char *relpath, struct got_object_id *blob_id,
4732 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4733 int dirfd, const char *de_name)
4735 struct revert_file_args *a = arg;
4736 const struct got_error *err = NULL;
4737 char *parent_path = NULL;
4738 struct got_fileindex_entry *ie;
4739 struct got_commit_object *base_commit = NULL;
4740 struct got_tree_object *tree = NULL;
4741 struct got_object_id *tree_id = NULL;
4742 const struct got_tree_entry *te = NULL;
4743 char *tree_path = NULL, *te_name;
4744 char *ondisk_path = NULL, *path_content = NULL;
4745 struct got_blob_object *blob = NULL;
4746 int fd = -1;
4748 /* Reverting a staged deletion is a no-op. */
4749 if (status == GOT_STATUS_DELETE &&
4750 staged_status != GOT_STATUS_NO_CHANGE)
4751 return NULL;
4753 if (status == GOT_STATUS_UNVERSIONED)
4754 return (*a->progress_cb)(a->progress_arg,
4755 GOT_STATUS_UNVERSIONED, relpath);
4757 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4758 if (ie == NULL)
4759 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4761 /* Construct in-repository path of tree which contains this blob. */
4762 err = got_path_dirname(&parent_path, ie->path);
4763 if (err) {
4764 if (err->code != GOT_ERR_BAD_PATH)
4765 goto done;
4766 parent_path = strdup("/");
4767 if (parent_path == NULL) {
4768 err = got_error_from_errno("strdup");
4769 goto done;
4772 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4773 tree_path = strdup(parent_path);
4774 if (tree_path == NULL) {
4775 err = got_error_from_errno("strdup");
4776 goto done;
4778 } else {
4779 if (got_path_is_root_dir(parent_path)) {
4780 tree_path = strdup(a->worktree->path_prefix);
4781 if (tree_path == NULL) {
4782 err = got_error_from_errno("strdup");
4783 goto done;
4785 } else {
4786 if (asprintf(&tree_path, "%s/%s",
4787 a->worktree->path_prefix, parent_path) == -1) {
4788 err = got_error_from_errno("asprintf");
4789 goto done;
4794 err = got_object_open_as_commit(&base_commit, a->repo,
4795 a->worktree->base_commit_id);
4796 if (err)
4797 goto done;
4799 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4800 if (err) {
4801 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4802 (status == GOT_STATUS_ADD ||
4803 staged_status == GOT_STATUS_ADD)))
4804 goto done;
4805 } else {
4806 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4807 if (err)
4808 goto done;
4810 err = got_path_basename(&te_name, ie->path);
4811 if (err)
4812 goto done;
4814 te = got_object_tree_find_entry(tree, te_name);
4815 free(te_name);
4816 if (te == NULL && status != GOT_STATUS_ADD &&
4817 staged_status != GOT_STATUS_ADD) {
4818 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4819 goto done;
4823 switch (status) {
4824 case GOT_STATUS_ADD:
4825 if (a->patch_cb) {
4826 int choice = GOT_PATCH_CHOICE_NONE;
4827 err = (*a->patch_cb)(&choice, a->patch_arg,
4828 status, ie->path, NULL, 1, 1);
4829 if (err)
4830 goto done;
4831 if (choice != GOT_PATCH_CHOICE_YES)
4832 break;
4834 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4835 ie->path);
4836 if (err)
4837 goto done;
4838 got_fileindex_entry_remove(a->fileindex, ie);
4839 if (a->unlink_added_files) {
4840 if (asprintf(&ondisk_path, "%s/%s",
4841 got_worktree_get_root_path(a->worktree),
4842 relpath) == -1) {
4843 err = got_error_from_errno("asprintf");
4844 goto done;
4846 if (unlink(ondisk_path) == -1) {
4847 err = got_error_from_errno2("unlink",
4848 ondisk_path);
4849 break;
4852 break;
4853 case GOT_STATUS_DELETE:
4854 if (a->patch_cb) {
4855 int choice = GOT_PATCH_CHOICE_NONE;
4856 err = (*a->patch_cb)(&choice, a->patch_arg,
4857 status, ie->path, NULL, 1, 1);
4858 if (err)
4859 goto done;
4860 if (choice != GOT_PATCH_CHOICE_YES)
4861 break;
4863 /* fall through */
4864 case GOT_STATUS_MODIFY:
4865 case GOT_STATUS_MODE_CHANGE:
4866 case GOT_STATUS_CONFLICT:
4867 case GOT_STATUS_MISSING: {
4868 struct got_object_id id;
4869 if (staged_status == GOT_STATUS_ADD ||
4870 staged_status == GOT_STATUS_MODIFY)
4871 got_fileindex_entry_get_staged_blob_id(&id, ie);
4872 else
4873 got_fileindex_entry_get_blob_id(&id, ie);
4874 fd = got_opentempfd();
4875 if (fd == -1) {
4876 err = got_error_from_errno("got_opentempfd");
4877 goto done;
4880 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4881 if (err)
4882 goto done;
4884 if (asprintf(&ondisk_path, "%s/%s",
4885 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4886 err = got_error_from_errno("asprintf");
4887 goto done;
4890 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4891 status == GOT_STATUS_CONFLICT)) {
4892 int is_bad_symlink = 0;
4893 err = create_patched_content(&path_content, 1, &id,
4894 ondisk_path, dirfd, de_name, ie->path, a->repo,
4895 a->patch_cb, a->patch_arg);
4896 if (err || path_content == NULL)
4897 break;
4898 if (te && S_ISLNK(te->mode)) {
4899 if (unlink(path_content) == -1) {
4900 err = got_error_from_errno2("unlink",
4901 path_content);
4902 break;
4904 err = install_symlink(&is_bad_symlink,
4905 a->worktree, ondisk_path, ie->path,
4906 blob, 0, 1, 0, 0, a->repo,
4907 a->progress_cb, a->progress_arg);
4908 } else {
4909 if (rename(path_content, ondisk_path) == -1) {
4910 err = got_error_from_errno3("rename",
4911 path_content, ondisk_path);
4912 goto done;
4915 } else {
4916 int is_bad_symlink = 0;
4917 if (te && S_ISLNK(te->mode)) {
4918 err = install_symlink(&is_bad_symlink,
4919 a->worktree, ondisk_path, ie->path,
4920 blob, 0, 1, 0, 0, a->repo,
4921 a->progress_cb, a->progress_arg);
4922 } else {
4923 err = install_blob(a->worktree, ondisk_path,
4924 ie->path,
4925 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4926 got_fileindex_perms_to_st(ie), blob,
4927 0, 1, 0, 0, a->repo,
4928 a->progress_cb, a->progress_arg);
4930 if (err)
4931 goto done;
4932 if (status == GOT_STATUS_DELETE ||
4933 status == GOT_STATUS_MODE_CHANGE) {
4934 err = got_fileindex_entry_update(ie,
4935 a->worktree->root_fd, relpath,
4936 blob->id.sha1,
4937 a->worktree->base_commit_id->sha1, 1);
4938 if (err)
4939 goto done;
4941 if (is_bad_symlink) {
4942 got_fileindex_entry_filetype_set(ie,
4943 GOT_FILEIDX_MODE_BAD_SYMLINK);
4946 break;
4948 default:
4949 break;
4951 done:
4952 free(ondisk_path);
4953 free(path_content);
4954 free(parent_path);
4955 free(tree_path);
4956 if (fd != -1 && close(fd) == -1 && err == NULL)
4957 err = got_error_from_errno("close");
4958 if (blob)
4959 got_object_blob_close(blob);
4960 if (tree)
4961 got_object_tree_close(tree);
4962 free(tree_id);
4963 if (base_commit)
4964 got_object_commit_close(base_commit);
4965 return err;
4968 const struct got_error *
4969 got_worktree_revert(struct got_worktree *worktree,
4970 struct got_pathlist_head *paths,
4971 got_worktree_checkout_cb progress_cb, void *progress_arg,
4972 got_worktree_patch_cb patch_cb, void *patch_arg,
4973 struct got_repository *repo)
4975 struct got_fileindex *fileindex = NULL;
4976 char *fileindex_path = NULL;
4977 const struct got_error *err = NULL, *unlockerr = NULL;
4978 const struct got_error *sync_err = NULL;
4979 struct got_pathlist_entry *pe;
4980 struct revert_file_args rfa;
4982 err = lock_worktree(worktree, LOCK_EX);
4983 if (err)
4984 return err;
4986 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4987 if (err)
4988 goto done;
4990 rfa.worktree = worktree;
4991 rfa.fileindex = fileindex;
4992 rfa.progress_cb = progress_cb;
4993 rfa.progress_arg = progress_arg;
4994 rfa.patch_cb = patch_cb;
4995 rfa.patch_arg = patch_arg;
4996 rfa.repo = repo;
4997 rfa.unlink_added_files = 0;
4998 TAILQ_FOREACH(pe, paths, entry) {
4999 err = worktree_status(worktree, pe->path, fileindex, repo,
5000 revert_file, &rfa, NULL, NULL, 1, 0);
5001 if (err)
5002 break;
5004 sync_err = sync_fileindex(fileindex, fileindex_path);
5005 if (sync_err && err == NULL)
5006 err = sync_err;
5007 done:
5008 free(fileindex_path);
5009 if (fileindex)
5010 got_fileindex_free(fileindex);
5011 unlockerr = lock_worktree(worktree, LOCK_SH);
5012 if (unlockerr && err == NULL)
5013 err = unlockerr;
5014 return err;
5017 static void
5018 free_commitable(struct got_commitable *ct)
5020 free(ct->path);
5021 free(ct->in_repo_path);
5022 free(ct->ondisk_path);
5023 free(ct->blob_id);
5024 free(ct->base_blob_id);
5025 free(ct->staged_blob_id);
5026 free(ct->base_commit_id);
5027 free(ct);
5030 struct collect_commitables_arg {
5031 struct got_pathlist_head *commitable_paths;
5032 struct got_repository *repo;
5033 struct got_worktree *worktree;
5034 struct got_fileindex *fileindex;
5035 int have_staged_files;
5036 int allow_bad_symlinks;
5037 int diff_header_shown;
5038 int commit_conflicts;
5039 FILE *diff_outfile;
5040 FILE *f1;
5041 FILE *f2;
5045 * Create a file which contains the target path of a symlink so we can feed
5046 * it as content to the diff engine.
5048 static const struct got_error *
5049 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5050 const char *abspath)
5052 const struct got_error *err = NULL;
5053 char target_path[PATH_MAX];
5054 ssize_t target_len, outlen;
5056 *fd = -1;
5058 if (dirfd != -1) {
5059 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5060 if (target_len == -1)
5061 return got_error_from_errno2("readlinkat", abspath);
5062 } else {
5063 target_len = readlink(abspath, target_path, PATH_MAX);
5064 if (target_len == -1)
5065 return got_error_from_errno2("readlink", abspath);
5068 *fd = got_opentempfd();
5069 if (*fd == -1)
5070 return got_error_from_errno("got_opentempfd");
5072 outlen = write(*fd, target_path, target_len);
5073 if (outlen == -1) {
5074 err = got_error_from_errno("got_opentempfd");
5075 goto done;
5078 if (lseek(*fd, 0, SEEK_SET) == -1) {
5079 err = got_error_from_errno2("lseek", abspath);
5080 goto done;
5082 done:
5083 if (err) {
5084 close(*fd);
5085 *fd = -1;
5087 return err;
5090 static const struct got_error *
5091 append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5092 FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5093 int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5095 const struct got_error *err = NULL;
5096 struct got_blob_object *blob1 = NULL;
5097 int fd = -1, fd1 = -1, fd2 = -1;
5098 FILE *ondisk_file = NULL;
5099 char *label1 = NULL;
5100 struct stat sb;
5101 off_t size1 = 0;
5102 int f2_exists = 0;
5103 char *id_str = NULL;
5105 memset(&sb, 0, sizeof(sb));
5107 if (diff_staged) {
5108 if (ct->staged_status != GOT_STATUS_MODIFY &&
5109 ct->staged_status != GOT_STATUS_ADD &&
5110 ct->staged_status != GOT_STATUS_DELETE)
5111 return NULL;
5112 } else {
5113 if (ct->status != GOT_STATUS_MODIFY &&
5114 ct->status != GOT_STATUS_ADD &&
5115 ct->status != GOT_STATUS_DELETE &&
5116 ct->status != GOT_STATUS_CONFLICT)
5117 return NULL;
5120 err = got_opentemp_truncate(f1);
5121 if (err)
5122 return got_error_from_errno("got_opentemp_truncate");
5123 err = got_opentemp_truncate(f2);
5124 if (err)
5125 return got_error_from_errno("got_opentemp_truncate");
5127 if (!*diff_header_shown) {
5128 err = got_object_id_str(&id_str, worktree->base_commit_id);
5129 if (err)
5130 return err;
5131 fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5132 got_worktree_get_root_path(worktree));
5133 fprintf(diff_outfile, "commit - %s\n", id_str);
5134 fprintf(diff_outfile, "path + %s%s\n",
5135 got_worktree_get_root_path(worktree),
5136 diff_staged ? " (staged changes)" : "");
5137 *diff_header_shown = 1;
5140 if (diff_staged) {
5141 const char *label1 = NULL, *label2 = NULL;
5142 switch (ct->staged_status) {
5143 case GOT_STATUS_MODIFY:
5144 label1 = ct->path;
5145 label2 = ct->path;
5146 break;
5147 case GOT_STATUS_ADD:
5148 label2 = ct->path;
5149 break;
5150 case GOT_STATUS_DELETE:
5151 label1 = ct->path;
5152 break;
5153 default:
5154 return got_error(GOT_ERR_FILE_STATUS);
5156 fd1 = got_opentempfd();
5157 if (fd1 == -1) {
5158 err = got_error_from_errno("got_opentempfd");
5159 goto done;
5161 fd2 = got_opentempfd();
5162 if (fd2 == -1) {
5163 err = got_error_from_errno("got_opentempfd");
5164 goto done;
5166 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5167 fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5168 label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5169 NULL, repo, diff_outfile);
5170 goto done;
5173 fd1 = got_opentempfd();
5174 if (fd1 == -1) {
5175 err = got_error_from_errno("got_opentempfd");
5176 goto done;
5179 if (ct->status != GOT_STATUS_ADD) {
5180 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5181 8192, fd1);
5182 if (err)
5183 goto done;
5186 if (ct->status != GOT_STATUS_DELETE) {
5187 if (dirfd != -1) {
5188 fd = openat(dirfd, de_name,
5189 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5190 if (fd == -1) {
5191 if (!got_err_open_nofollow_on_symlink()) {
5192 err = got_error_from_errno2("openat",
5193 ct->ondisk_path);
5194 goto done;
5196 err = get_symlink_target_file(&fd, dirfd,
5197 de_name, ct->ondisk_path);
5198 if (err)
5199 goto done;
5201 } else {
5202 fd = open(ct->ondisk_path,
5203 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5204 if (fd == -1) {
5205 if (!got_err_open_nofollow_on_symlink()) {
5206 err = got_error_from_errno2("open",
5207 ct->ondisk_path);
5208 goto done;
5210 err = get_symlink_target_file(&fd, dirfd,
5211 de_name, ct->ondisk_path);
5212 if (err)
5213 goto done;
5216 if (fstatat(fd, ct->ondisk_path, &sb,
5217 AT_SYMLINK_NOFOLLOW) == -1) {
5218 err = got_error_from_errno2("fstatat", ct->ondisk_path);
5219 goto done;
5221 ondisk_file = fdopen(fd, "r");
5222 if (ondisk_file == NULL) {
5223 err = got_error_from_errno2("fdopen", ct->ondisk_path);
5224 goto done;
5226 fd = -1;
5227 f2_exists = 1;
5230 if (blob1) {
5231 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5232 f1, blob1);
5233 if (err)
5234 goto done;
5237 err = got_diff_blob_file(blob1, f1, size1, label1,
5238 ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5239 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5240 done:
5241 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5242 err = got_error_from_errno("close");
5243 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5244 err = got_error_from_errno("close");
5245 if (blob1)
5246 got_object_blob_close(blob1);
5247 if (fd != -1 && close(fd) == -1 && err == NULL)
5248 err = got_error_from_errno("close");
5249 if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5250 err = got_error_from_errno("fclose");
5251 return err;
5254 static const struct got_error *
5255 collect_commitables(void *arg, unsigned char status,
5256 unsigned char staged_status, const char *relpath,
5257 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5258 struct got_object_id *commit_id, int dirfd, const char *de_name)
5260 struct collect_commitables_arg *a = arg;
5261 const struct got_error *err = NULL;
5262 struct got_commitable *ct = NULL;
5263 struct got_pathlist_entry *new = NULL;
5264 char *parent_path = NULL, *path = NULL;
5265 struct stat sb;
5267 if (a->have_staged_files) {
5268 if (staged_status != GOT_STATUS_MODIFY &&
5269 staged_status != GOT_STATUS_ADD &&
5270 staged_status != GOT_STATUS_DELETE)
5271 return NULL;
5272 } else {
5273 if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5274 printf("C %s\n", relpath);
5275 return got_error(GOT_ERR_COMMIT_CONFLICT);
5278 if (status != GOT_STATUS_MODIFY &&
5279 status != GOT_STATUS_MODE_CHANGE &&
5280 status != GOT_STATUS_ADD &&
5281 status != GOT_STATUS_DELETE &&
5282 status != GOT_STATUS_CONFLICT)
5283 return NULL;
5286 if (asprintf(&path, "/%s", relpath) == -1) {
5287 err = got_error_from_errno("asprintf");
5288 goto done;
5290 if (strcmp(path, "/") == 0) {
5291 parent_path = strdup("");
5292 if (parent_path == NULL)
5293 return got_error_from_errno("strdup");
5294 } else {
5295 err = got_path_dirname(&parent_path, path);
5296 if (err)
5297 return err;
5300 ct = calloc(1, sizeof(*ct));
5301 if (ct == NULL) {
5302 err = got_error_from_errno("calloc");
5303 goto done;
5306 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5307 relpath) == -1) {
5308 err = got_error_from_errno("asprintf");
5309 goto done;
5312 if (staged_status == GOT_STATUS_ADD ||
5313 staged_status == GOT_STATUS_MODIFY) {
5314 struct got_fileindex_entry *ie;
5315 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5316 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5317 case GOT_FILEIDX_MODE_REGULAR_FILE:
5318 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5319 ct->mode = S_IFREG;
5320 break;
5321 case GOT_FILEIDX_MODE_SYMLINK:
5322 ct->mode = S_IFLNK;
5323 break;
5324 default:
5325 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5326 goto done;
5328 ct->mode |= got_fileindex_entry_perms_get(ie);
5329 } else if (status != GOT_STATUS_DELETE &&
5330 staged_status != GOT_STATUS_DELETE) {
5331 if (dirfd != -1) {
5332 if (fstatat(dirfd, de_name, &sb,
5333 AT_SYMLINK_NOFOLLOW) == -1) {
5334 err = got_error_from_errno2("fstatat",
5335 ct->ondisk_path);
5336 goto done;
5338 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5339 err = got_error_from_errno2("lstat", ct->ondisk_path);
5340 goto done;
5342 ct->mode = sb.st_mode;
5345 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5346 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5347 relpath) == -1) {
5348 err = got_error_from_errno("asprintf");
5349 goto done;
5352 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5353 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5354 int is_bad_symlink;
5355 char target_path[PATH_MAX];
5356 ssize_t target_len;
5357 target_len = readlink(ct->ondisk_path, target_path,
5358 sizeof(target_path));
5359 if (target_len == -1) {
5360 err = got_error_from_errno2("readlink",
5361 ct->ondisk_path);
5362 goto done;
5364 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5365 target_len, ct->ondisk_path, a->worktree->root_path);
5366 if (err)
5367 goto done;
5368 if (is_bad_symlink) {
5369 err = got_error_path(ct->ondisk_path,
5370 GOT_ERR_BAD_SYMLINK);
5371 goto done;
5376 ct->status = status;
5377 ct->staged_status = staged_status;
5378 ct->blob_id = NULL; /* will be filled in when blob gets created */
5379 if (ct->status != GOT_STATUS_ADD &&
5380 ct->staged_status != GOT_STATUS_ADD) {
5381 ct->base_blob_id = got_object_id_dup(blob_id);
5382 if (ct->base_blob_id == NULL) {
5383 err = got_error_from_errno("got_object_id_dup");
5384 goto done;
5386 ct->base_commit_id = got_object_id_dup(commit_id);
5387 if (ct->base_commit_id == NULL) {
5388 err = got_error_from_errno("got_object_id_dup");
5389 goto done;
5392 if (ct->staged_status == GOT_STATUS_ADD ||
5393 ct->staged_status == GOT_STATUS_MODIFY) {
5394 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5395 if (ct->staged_blob_id == NULL) {
5396 err = got_error_from_errno("got_object_id_dup");
5397 goto done;
5400 ct->path = strdup(path);
5401 if (ct->path == NULL) {
5402 err = got_error_from_errno("strdup");
5403 goto done;
5405 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5406 if (err)
5407 goto done;
5409 if (a->diff_outfile && ct && new != NULL) {
5410 err = append_ct_diff(ct, &a->diff_header_shown,
5411 a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5412 a->have_staged_files, a->repo, a->worktree);
5413 if (err)
5414 goto done;
5416 done:
5417 if (ct && (err || new == NULL))
5418 free_commitable(ct);
5419 free(parent_path);
5420 free(path);
5421 return err;
5424 static const struct got_error *write_tree(struct got_object_id **, int *,
5425 struct got_tree_object *, const char *, struct got_pathlist_head *,
5426 got_worktree_status_cb status_cb, void *status_arg,
5427 struct got_repository *);
5429 static const struct got_error *
5430 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5431 struct got_tree_entry *te, const char *parent_path,
5432 struct got_pathlist_head *commitable_paths,
5433 got_worktree_status_cb status_cb, void *status_arg,
5434 struct got_repository *repo)
5436 const struct got_error *err = NULL;
5437 struct got_tree_object *subtree;
5438 char *subpath;
5440 if (asprintf(&subpath, "%s%s%s", parent_path,
5441 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5442 return got_error_from_errno("asprintf");
5444 err = got_object_open_as_tree(&subtree, repo, &te->id);
5445 if (err)
5446 return err;
5448 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5449 commitable_paths, status_cb, status_arg, repo);
5450 got_object_tree_close(subtree);
5451 free(subpath);
5452 return err;
5455 static const struct got_error *
5456 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5458 const struct got_error *err = NULL;
5459 char *ct_parent_path = NULL;
5461 *match = 0;
5463 if (strchr(ct->in_repo_path, '/') == NULL) {
5464 *match = got_path_is_root_dir(path);
5465 return NULL;
5468 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5469 if (err)
5470 return err;
5471 *match = (strcmp(path, ct_parent_path) == 0);
5472 free(ct_parent_path);
5473 return err;
5476 static mode_t
5477 get_ct_file_mode(struct got_commitable *ct)
5479 if (S_ISLNK(ct->mode))
5480 return S_IFLNK;
5482 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5485 static const struct got_error *
5486 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5487 struct got_tree_entry *te, struct got_commitable *ct)
5489 const struct got_error *err = NULL;
5491 *new_te = NULL;
5493 err = got_object_tree_entry_dup(new_te, te);
5494 if (err)
5495 goto done;
5497 (*new_te)->mode = get_ct_file_mode(ct);
5499 if (ct->staged_status == GOT_STATUS_MODIFY)
5500 memcpy(&(*new_te)->id, ct->staged_blob_id,
5501 sizeof((*new_te)->id));
5502 else
5503 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5504 done:
5505 if (err && *new_te) {
5506 free(*new_te);
5507 *new_te = NULL;
5509 return err;
5512 static const struct got_error *
5513 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5514 struct got_commitable *ct)
5516 const struct got_error *err = NULL;
5517 char *ct_name = NULL;
5519 *new_te = NULL;
5521 *new_te = calloc(1, sizeof(**new_te));
5522 if (*new_te == NULL)
5523 return got_error_from_errno("calloc");
5525 err = got_path_basename(&ct_name, ct->path);
5526 if (err)
5527 goto done;
5528 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5529 sizeof((*new_te)->name)) {
5530 err = got_error(GOT_ERR_NO_SPACE);
5531 goto done;
5534 (*new_te)->mode = get_ct_file_mode(ct);
5536 if (ct->staged_status == GOT_STATUS_ADD)
5537 memcpy(&(*new_te)->id, ct->staged_blob_id,
5538 sizeof((*new_te)->id));
5539 else
5540 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5541 done:
5542 free(ct_name);
5543 if (err && *new_te) {
5544 free(*new_te);
5545 *new_te = NULL;
5547 return err;
5550 static const struct got_error *
5551 insert_tree_entry(struct got_tree_entry *new_te,
5552 struct got_pathlist_head *paths)
5554 const struct got_error *err = NULL;
5555 struct got_pathlist_entry *new_pe;
5557 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5558 if (err)
5559 return err;
5560 if (new_pe == NULL)
5561 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5562 return NULL;
5565 static const struct got_error *
5566 report_ct_status(struct got_commitable *ct,
5567 got_worktree_status_cb status_cb, void *status_arg)
5569 const char *ct_path = ct->path;
5570 unsigned char status;
5572 if (status_cb == NULL) /* no commit progress output desired */
5573 return NULL;
5575 while (ct_path[0] == '/')
5576 ct_path++;
5578 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5579 status = ct->staged_status;
5580 else
5581 status = ct->status;
5583 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5584 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5587 static const struct got_error *
5588 match_modified_subtree(int *modified, struct got_tree_entry *te,
5589 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5591 const struct got_error *err = NULL;
5592 struct got_pathlist_entry *pe;
5593 char *te_path;
5595 *modified = 0;
5597 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5598 got_path_is_root_dir(base_tree_path) ? "" : "/",
5599 te->name) == -1)
5600 return got_error_from_errno("asprintf");
5602 TAILQ_FOREACH(pe, commitable_paths, entry) {
5603 struct got_commitable *ct = pe->data;
5604 *modified = got_path_is_child(ct->in_repo_path, te_path,
5605 strlen(te_path));
5606 if (*modified)
5607 break;
5610 free(te_path);
5611 return err;
5614 static const struct got_error *
5615 match_deleted_or_modified_ct(struct got_commitable **ctp,
5616 struct got_tree_entry *te, const char *base_tree_path,
5617 struct got_pathlist_head *commitable_paths)
5619 const struct got_error *err = NULL;
5620 struct got_pathlist_entry *pe;
5622 *ctp = NULL;
5624 TAILQ_FOREACH(pe, commitable_paths, entry) {
5625 struct got_commitable *ct = pe->data;
5626 char *ct_name = NULL;
5627 int path_matches;
5629 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5630 if (ct->status != GOT_STATUS_MODIFY &&
5631 ct->status != GOT_STATUS_MODE_CHANGE &&
5632 ct->status != GOT_STATUS_DELETE &&
5633 ct->status != GOT_STATUS_CONFLICT)
5634 continue;
5635 } else {
5636 if (ct->staged_status != GOT_STATUS_MODIFY &&
5637 ct->staged_status != GOT_STATUS_DELETE)
5638 continue;
5641 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5642 continue;
5644 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5645 if (err)
5646 return err;
5647 if (!path_matches)
5648 continue;
5650 err = got_path_basename(&ct_name, pe->path);
5651 if (err)
5652 return err;
5654 if (strcmp(te->name, ct_name) != 0) {
5655 free(ct_name);
5656 continue;
5658 free(ct_name);
5660 *ctp = ct;
5661 break;
5664 return err;
5667 static const struct got_error *
5668 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5669 const char *child_path, const char *path_base_tree,
5670 struct got_pathlist_head *commitable_paths,
5671 got_worktree_status_cb status_cb, void *status_arg,
5672 struct got_repository *repo)
5674 const struct got_error *err = NULL;
5675 struct got_tree_entry *new_te;
5676 char *subtree_path;
5677 struct got_object_id *id = NULL;
5678 int nentries;
5680 *new_tep = NULL;
5682 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5683 got_path_is_root_dir(path_base_tree) ? "" : "/",
5684 child_path) == -1)
5685 return got_error_from_errno("asprintf");
5687 new_te = calloc(1, sizeof(*new_te));
5688 if (new_te == NULL)
5689 return got_error_from_errno("calloc");
5690 new_te->mode = S_IFDIR;
5692 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5693 sizeof(new_te->name)) {
5694 err = got_error(GOT_ERR_NO_SPACE);
5695 goto done;
5697 err = write_tree(&id, &nentries, NULL, subtree_path,
5698 commitable_paths, status_cb, status_arg, repo);
5699 if (err) {
5700 free(new_te);
5701 goto done;
5703 memcpy(&new_te->id, id, sizeof(new_te->id));
5704 done:
5705 free(id);
5706 free(subtree_path);
5707 if (err == NULL)
5708 *new_tep = new_te;
5709 return err;
5712 static const struct got_error *
5713 write_tree(struct got_object_id **new_tree_id, int *nentries,
5714 struct got_tree_object *base_tree, const char *path_base_tree,
5715 struct got_pathlist_head *commitable_paths,
5716 got_worktree_status_cb status_cb, void *status_arg,
5717 struct got_repository *repo)
5719 const struct got_error *err = NULL;
5720 struct got_pathlist_head paths;
5721 struct got_tree_entry *te, *new_te = NULL;
5722 struct got_pathlist_entry *pe;
5724 TAILQ_INIT(&paths);
5725 *nentries = 0;
5727 /* Insert, and recurse into, newly added entries first. */
5728 TAILQ_FOREACH(pe, commitable_paths, entry) {
5729 struct got_commitable *ct = pe->data;
5730 char *child_path = NULL, *slash;
5732 if ((ct->status != GOT_STATUS_ADD &&
5733 ct->staged_status != GOT_STATUS_ADD) ||
5734 (ct->flags & GOT_COMMITABLE_ADDED))
5735 continue;
5737 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5738 strlen(path_base_tree)))
5739 continue;
5741 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5742 ct->in_repo_path);
5743 if (err)
5744 goto done;
5746 slash = strchr(child_path, '/');
5747 if (slash == NULL) {
5748 err = alloc_added_blob_tree_entry(&new_te, ct);
5749 if (err)
5750 goto done;
5751 err = report_ct_status(ct, status_cb, status_arg);
5752 if (err)
5753 goto done;
5754 ct->flags |= GOT_COMMITABLE_ADDED;
5755 err = insert_tree_entry(new_te, &paths);
5756 if (err)
5757 goto done;
5758 (*nentries)++;
5759 } else {
5760 *slash = '\0'; /* trim trailing path components */
5761 if (base_tree == NULL ||
5762 got_object_tree_find_entry(base_tree, child_path)
5763 == NULL) {
5764 err = make_subtree_for_added_blob(&new_te,
5765 child_path, path_base_tree,
5766 commitable_paths, status_cb, status_arg,
5767 repo);
5768 if (err)
5769 goto done;
5770 err = insert_tree_entry(new_te, &paths);
5771 if (err)
5772 goto done;
5773 (*nentries)++;
5778 if (base_tree) {
5779 int i, nbase_entries;
5780 /* Handle modified and deleted entries. */
5781 nbase_entries = got_object_tree_get_nentries(base_tree);
5782 for (i = 0; i < nbase_entries; i++) {
5783 struct got_commitable *ct = NULL;
5785 te = got_object_tree_get_entry(base_tree, i);
5786 if (got_object_tree_entry_is_submodule(te)) {
5787 /* Entry is a submodule; just copy it. */
5788 err = got_object_tree_entry_dup(&new_te, te);
5789 if (err)
5790 goto done;
5791 err = insert_tree_entry(new_te, &paths);
5792 if (err)
5793 goto done;
5794 (*nentries)++;
5795 continue;
5798 if (S_ISDIR(te->mode)) {
5799 int modified;
5800 err = got_object_tree_entry_dup(&new_te, te);
5801 if (err)
5802 goto done;
5803 err = match_modified_subtree(&modified, te,
5804 path_base_tree, commitable_paths);
5805 if (err)
5806 goto done;
5807 /* Avoid recursion into unmodified subtrees. */
5808 if (modified) {
5809 struct got_object_id *new_id;
5810 int nsubentries;
5811 err = write_subtree(&new_id,
5812 &nsubentries, te,
5813 path_base_tree, commitable_paths,
5814 status_cb, status_arg, repo);
5815 if (err)
5816 goto done;
5817 if (nsubentries == 0) {
5818 /* All entries were deleted. */
5819 free(new_id);
5820 continue;
5822 memcpy(&new_te->id, new_id,
5823 sizeof(new_te->id));
5824 free(new_id);
5826 err = insert_tree_entry(new_te, &paths);
5827 if (err)
5828 goto done;
5829 (*nentries)++;
5830 continue;
5833 err = match_deleted_or_modified_ct(&ct, te,
5834 path_base_tree, commitable_paths);
5835 if (err)
5836 goto done;
5837 if (ct) {
5838 /* NB: Deleted entries get dropped here. */
5839 if (ct->status == GOT_STATUS_MODIFY ||
5840 ct->status == GOT_STATUS_MODE_CHANGE ||
5841 ct->status == GOT_STATUS_CONFLICT ||
5842 ct->staged_status == GOT_STATUS_MODIFY) {
5843 err = alloc_modified_blob_tree_entry(
5844 &new_te, te, ct);
5845 if (err)
5846 goto done;
5847 err = insert_tree_entry(new_te, &paths);
5848 if (err)
5849 goto done;
5850 (*nentries)++;
5852 err = report_ct_status(ct, status_cb,
5853 status_arg);
5854 if (err)
5855 goto done;
5856 } else {
5857 /* Entry is unchanged; just copy it. */
5858 err = got_object_tree_entry_dup(&new_te, te);
5859 if (err)
5860 goto done;
5861 err = insert_tree_entry(new_te, &paths);
5862 if (err)
5863 goto done;
5864 (*nentries)++;
5869 /* Write new list of entries; deleted entries have been dropped. */
5870 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5871 done:
5872 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
5873 return err;
5876 static const struct got_error *
5877 update_fileindex_after_commit(struct got_worktree *worktree,
5878 struct got_pathlist_head *commitable_paths,
5879 struct got_object_id *new_base_commit_id,
5880 struct got_fileindex *fileindex, int have_staged_files)
5882 const struct got_error *err = NULL;
5883 struct got_pathlist_entry *pe;
5884 char *relpath = NULL;
5886 TAILQ_FOREACH(pe, commitable_paths, entry) {
5887 struct got_fileindex_entry *ie;
5888 struct got_commitable *ct = pe->data;
5890 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5892 err = got_path_skip_common_ancestor(&relpath,
5893 worktree->root_path, ct->ondisk_path);
5894 if (err)
5895 goto done;
5897 if (ie) {
5898 if (ct->status == GOT_STATUS_DELETE ||
5899 ct->staged_status == GOT_STATUS_DELETE) {
5900 got_fileindex_entry_remove(fileindex, ie);
5901 } else if (ct->staged_status == GOT_STATUS_ADD ||
5902 ct->staged_status == GOT_STATUS_MODIFY) {
5903 got_fileindex_entry_stage_set(ie,
5904 GOT_FILEIDX_STAGE_NONE);
5905 got_fileindex_entry_staged_filetype_set(ie, 0);
5907 err = got_fileindex_entry_update(ie,
5908 worktree->root_fd, relpath,
5909 ct->staged_blob_id->sha1,
5910 new_base_commit_id->sha1,
5911 !have_staged_files);
5912 } else
5913 err = got_fileindex_entry_update(ie,
5914 worktree->root_fd, relpath,
5915 ct->blob_id->sha1,
5916 new_base_commit_id->sha1,
5917 !have_staged_files);
5918 } else {
5919 err = got_fileindex_entry_alloc(&ie, pe->path);
5920 if (err)
5921 goto done;
5922 err = got_fileindex_entry_update(ie,
5923 worktree->root_fd, relpath, ct->blob_id->sha1,
5924 new_base_commit_id->sha1, 1);
5925 if (err) {
5926 got_fileindex_entry_free(ie);
5927 goto done;
5929 err = got_fileindex_entry_add(fileindex, ie);
5930 if (err) {
5931 got_fileindex_entry_free(ie);
5932 goto done;
5935 free(relpath);
5936 relpath = NULL;
5938 done:
5939 free(relpath);
5940 return err;
5944 static const struct got_error *
5945 check_out_of_date(const char *in_repo_path, unsigned char status,
5946 unsigned char staged_status, struct got_object_id *base_blob_id,
5947 struct got_object_id *base_commit_id,
5948 struct got_object_id *head_commit_id, struct got_repository *repo,
5949 int ood_errcode)
5951 const struct got_error *err = NULL;
5952 struct got_commit_object *commit = NULL;
5953 struct got_object_id *id = NULL;
5955 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5956 /* Trivial case: base commit == head commit */
5957 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5958 return NULL;
5960 * Ensure file content which local changes were based
5961 * on matches file content in the branch head.
5963 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5964 if (err)
5965 goto done;
5966 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5967 if (err) {
5968 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5969 err = got_error(ood_errcode);
5970 goto done;
5971 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5972 err = got_error(ood_errcode);
5973 } else {
5974 /* Require that added files don't exist in the branch head. */
5975 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5976 if (err)
5977 goto done;
5978 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5979 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5980 goto done;
5981 err = id ? got_error(ood_errcode) : NULL;
5983 done:
5984 free(id);
5985 if (commit)
5986 got_object_commit_close(commit);
5987 return err;
5990 static const struct got_error *
5991 commit_worktree(struct got_object_id **new_commit_id,
5992 struct got_pathlist_head *commitable_paths,
5993 struct got_object_id *head_commit_id,
5994 struct got_object_id *parent_id2,
5995 struct got_worktree *worktree,
5996 const char *author, const char *committer, char *diff_path,
5997 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5998 got_worktree_status_cb status_cb, void *status_arg,
5999 struct got_repository *repo)
6001 const struct got_error *err = NULL, *unlockerr = NULL;
6002 struct got_pathlist_entry *pe;
6003 const char *head_ref_name = NULL;
6004 struct got_commit_object *head_commit = NULL;
6005 struct got_reference *head_ref2 = NULL;
6006 struct got_object_id *head_commit_id2 = NULL;
6007 struct got_tree_object *head_tree = NULL;
6008 struct got_object_id *new_tree_id = NULL;
6009 int nentries, nparents = 0;
6010 struct got_object_id_queue parent_ids;
6011 struct got_object_qid *pid = NULL;
6012 char *logmsg = NULL;
6013 time_t timestamp;
6015 *new_commit_id = NULL;
6017 STAILQ_INIT(&parent_ids);
6019 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6020 if (err)
6021 goto done;
6023 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6024 if (err)
6025 goto done;
6027 if (commit_msg_cb != NULL) {
6028 err = commit_msg_cb(commitable_paths, diff_path,
6029 &logmsg, commit_arg);
6030 if (err)
6031 goto done;
6034 if (logmsg == NULL || strlen(logmsg) == 0) {
6035 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6036 goto done;
6039 /* Create blobs from added and modified files and record their IDs. */
6040 TAILQ_FOREACH(pe, commitable_paths, entry) {
6041 struct got_commitable *ct = pe->data;
6042 char *ondisk_path;
6044 /* Blobs for staged files already exist. */
6045 if (ct->staged_status == GOT_STATUS_ADD ||
6046 ct->staged_status == GOT_STATUS_MODIFY)
6047 continue;
6049 if (ct->status != GOT_STATUS_ADD &&
6050 ct->status != GOT_STATUS_MODIFY &&
6051 ct->status != GOT_STATUS_MODE_CHANGE &&
6052 ct->status != GOT_STATUS_CONFLICT)
6053 continue;
6055 if (asprintf(&ondisk_path, "%s/%s",
6056 worktree->root_path, pe->path) == -1) {
6057 err = got_error_from_errno("asprintf");
6058 goto done;
6060 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6061 free(ondisk_path);
6062 if (err)
6063 goto done;
6066 /* Recursively write new tree objects. */
6067 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6068 commitable_paths, status_cb, status_arg, repo);
6069 if (err)
6070 goto done;
6072 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
6073 if (err)
6074 goto done;
6075 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6076 nparents++;
6077 if (parent_id2) {
6078 err = got_object_qid_alloc(&pid, parent_id2);
6079 if (err)
6080 goto done;
6081 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6082 nparents++;
6084 timestamp = time(NULL);
6085 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6086 nparents, author, timestamp, committer, timestamp, logmsg, repo);
6087 if (logmsg != NULL)
6088 free(logmsg);
6089 if (err)
6090 goto done;
6092 /* Check if a concurrent commit to our branch has occurred. */
6093 head_ref_name = got_worktree_get_head_ref_name(worktree);
6094 if (head_ref_name == NULL) {
6095 err = got_error_from_errno("got_worktree_get_head_ref_name");
6096 goto done;
6098 /* Lock the reference here to prevent concurrent modification. */
6099 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6100 if (err)
6101 goto done;
6102 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6103 if (err)
6104 goto done;
6105 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6106 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6107 goto done;
6109 /* Update branch head in repository. */
6110 err = got_ref_change_ref(head_ref2, *new_commit_id);
6111 if (err)
6112 goto done;
6113 err = got_ref_write(head_ref2, repo);
6114 if (err)
6115 goto done;
6117 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6118 if (err)
6119 goto done;
6121 err = ref_base_commit(worktree, repo);
6122 if (err)
6123 goto done;
6124 done:
6125 got_object_id_queue_free(&parent_ids);
6126 if (head_tree)
6127 got_object_tree_close(head_tree);
6128 if (head_commit)
6129 got_object_commit_close(head_commit);
6130 free(head_commit_id2);
6131 if (head_ref2) {
6132 unlockerr = got_ref_unlock(head_ref2);
6133 if (unlockerr && err == NULL)
6134 err = unlockerr;
6135 got_ref_close(head_ref2);
6137 return err;
6140 static const struct got_error *
6141 check_path_is_commitable(const char *path,
6142 struct got_pathlist_head *commitable_paths)
6144 struct got_pathlist_entry *cpe = NULL;
6145 size_t path_len = strlen(path);
6147 TAILQ_FOREACH(cpe, commitable_paths, entry) {
6148 struct got_commitable *ct = cpe->data;
6149 const char *ct_path = ct->path;
6151 while (ct_path[0] == '/')
6152 ct_path++;
6154 if (strcmp(path, ct_path) == 0 ||
6155 got_path_is_child(ct_path, path, path_len))
6156 break;
6159 if (cpe == NULL)
6160 return got_error_path(path, GOT_ERR_BAD_PATH);
6162 return NULL;
6165 static const struct got_error *
6166 check_staged_file(void *arg, struct got_fileindex_entry *ie)
6168 int *have_staged_files = arg;
6170 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6171 *have_staged_files = 1;
6172 return got_error(GOT_ERR_CANCELLED);
6175 return NULL;
6178 static const struct got_error *
6179 check_non_staged_files(struct got_fileindex *fileindex,
6180 struct got_pathlist_head *paths)
6182 struct got_pathlist_entry *pe;
6183 struct got_fileindex_entry *ie;
6185 TAILQ_FOREACH(pe, paths, entry) {
6186 if (pe->path[0] == '\0')
6187 continue;
6188 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6189 if (ie == NULL)
6190 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6191 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6192 return got_error_path(pe->path,
6193 GOT_ERR_FILE_NOT_STAGED);
6196 return NULL;
6199 const struct got_error *
6200 got_worktree_commit(struct got_object_id **new_commit_id,
6201 struct got_worktree *worktree, struct got_pathlist_head *paths,
6202 const char *author, const char *committer, int allow_bad_symlinks,
6203 int show_diff, int commit_conflicts,
6204 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6205 got_worktree_status_cb status_cb, void *status_arg,
6206 struct got_repository *repo)
6208 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6209 struct got_fileindex *fileindex = NULL;
6210 char *fileindex_path = NULL;
6211 struct got_pathlist_head commitable_paths;
6212 struct collect_commitables_arg cc_arg;
6213 struct got_pathlist_entry *pe;
6214 struct got_reference *head_ref = NULL;
6215 struct got_object_id *head_commit_id = NULL;
6216 char *diff_path = NULL;
6217 int have_staged_files = 0;
6219 *new_commit_id = NULL;
6221 memset(&cc_arg, 0, sizeof(cc_arg));
6222 TAILQ_INIT(&commitable_paths);
6224 err = lock_worktree(worktree, LOCK_EX);
6225 if (err)
6226 goto done;
6228 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6229 if (err)
6230 goto done;
6232 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6233 if (err)
6234 goto done;
6236 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6237 if (err)
6238 goto done;
6240 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6241 &have_staged_files);
6242 if (err && err->code != GOT_ERR_CANCELLED)
6243 goto done;
6244 if (have_staged_files) {
6245 err = check_non_staged_files(fileindex, paths);
6246 if (err)
6247 goto done;
6250 cc_arg.commitable_paths = &commitable_paths;
6251 cc_arg.worktree = worktree;
6252 cc_arg.fileindex = fileindex;
6253 cc_arg.repo = repo;
6254 cc_arg.have_staged_files = have_staged_files;
6255 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6256 cc_arg.diff_header_shown = 0;
6257 cc_arg.commit_conflicts = commit_conflicts;
6258 if (show_diff) {
6259 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6260 GOT_TMPDIR_STR "/got", ".diff");
6261 if (err)
6262 goto done;
6263 cc_arg.f1 = got_opentemp();
6264 if (cc_arg.f1 == NULL) {
6265 err = got_error_from_errno("got_opentemp");
6266 goto done;
6268 cc_arg.f2 = got_opentemp();
6269 if (cc_arg.f2 == NULL) {
6270 err = got_error_from_errno("got_opentemp");
6271 goto done;
6275 TAILQ_FOREACH(pe, paths, entry) {
6276 err = worktree_status(worktree, pe->path, fileindex, repo,
6277 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6278 if (err)
6279 goto done;
6282 if (show_diff) {
6283 if (fflush(cc_arg.diff_outfile) == EOF) {
6284 err = got_error_from_errno("fflush");
6285 goto done;
6289 if (TAILQ_EMPTY(&commitable_paths)) {
6290 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6291 goto done;
6294 TAILQ_FOREACH(pe, paths, entry) {
6295 err = check_path_is_commitable(pe->path, &commitable_paths);
6296 if (err)
6297 goto done;
6300 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6301 struct got_commitable *ct = pe->data;
6302 const char *ct_path = ct->in_repo_path;
6304 while (ct_path[0] == '/')
6305 ct_path++;
6306 err = check_out_of_date(ct_path, ct->status,
6307 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6308 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6309 if (err)
6310 goto done;
6314 err = commit_worktree(new_commit_id, &commitable_paths,
6315 head_commit_id, NULL, worktree, author, committer,
6316 (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6317 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6318 if (err)
6319 goto done;
6321 err = update_fileindex_after_commit(worktree, &commitable_paths,
6322 *new_commit_id, fileindex, have_staged_files);
6323 sync_err = sync_fileindex(fileindex, fileindex_path);
6324 if (sync_err && err == NULL)
6325 err = sync_err;
6326 done:
6327 if (fileindex)
6328 got_fileindex_free(fileindex);
6329 free(fileindex_path);
6330 unlockerr = lock_worktree(worktree, LOCK_SH);
6331 if (unlockerr && err == NULL)
6332 err = unlockerr;
6333 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6334 struct got_commitable *ct = pe->data;
6336 free_commitable(ct);
6338 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6339 if (diff_path && unlink(diff_path) == -1 && err == NULL)
6340 err = got_error_from_errno2("unlink", diff_path);
6341 free(diff_path);
6342 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6343 err == NULL)
6344 err = got_error_from_errno("fclose");
6345 return err;
6348 const char *
6349 got_commitable_get_path(struct got_commitable *ct)
6351 return ct->path;
6354 unsigned int
6355 got_commitable_get_status(struct got_commitable *ct)
6357 return ct->status;
6360 struct check_rebase_ok_arg {
6361 struct got_worktree *worktree;
6362 struct got_repository *repo;
6365 static const struct got_error *
6366 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6368 const struct got_error *err = NULL;
6369 struct check_rebase_ok_arg *a = arg;
6370 unsigned char status;
6371 struct stat sb;
6372 char *ondisk_path;
6374 /* Reject rebase of a work tree with mixed base commits. */
6375 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6376 SHA1_DIGEST_LENGTH))
6377 return got_error(GOT_ERR_MIXED_COMMITS);
6379 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6380 == -1)
6381 return got_error_from_errno("asprintf");
6383 /* Reject rebase of a work tree with modified or staged files. */
6384 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6385 free(ondisk_path);
6386 if (err)
6387 return err;
6389 if (status != GOT_STATUS_NO_CHANGE)
6390 return got_error(GOT_ERR_MODIFIED);
6391 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6392 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6394 return NULL;
6397 const struct got_error *
6398 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6399 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6400 struct got_worktree *worktree, struct got_reference *branch,
6401 struct got_repository *repo)
6403 const struct got_error *err = NULL;
6404 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6405 char *branch_ref_name = NULL;
6406 char *fileindex_path = NULL;
6407 struct check_rebase_ok_arg ok_arg;
6408 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6409 struct got_object_id *wt_branch_tip = NULL;
6411 *new_base_branch_ref = NULL;
6412 *tmp_branch = NULL;
6413 *fileindex = NULL;
6415 err = lock_worktree(worktree, LOCK_EX);
6416 if (err)
6417 return err;
6419 err = open_fileindex(fileindex, &fileindex_path, worktree);
6420 if (err)
6421 goto done;
6423 ok_arg.worktree = worktree;
6424 ok_arg.repo = repo;
6425 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6426 &ok_arg);
6427 if (err)
6428 goto done;
6430 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6431 if (err)
6432 goto done;
6434 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6435 if (err)
6436 goto done;
6438 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6439 if (err)
6440 goto done;
6442 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6443 0);
6444 if (err)
6445 goto done;
6447 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6448 if (err)
6449 goto done;
6450 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6451 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6452 goto done;
6455 err = got_ref_alloc_symref(new_base_branch_ref,
6456 new_base_branch_ref_name, wt_branch);
6457 if (err)
6458 goto done;
6459 err = got_ref_write(*new_base_branch_ref, repo);
6460 if (err)
6461 goto done;
6463 /* TODO Lock original branch's ref while rebasing? */
6465 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6466 if (err)
6467 goto done;
6469 err = got_ref_write(branch_ref, repo);
6470 if (err)
6471 goto done;
6473 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6474 worktree->base_commit_id);
6475 if (err)
6476 goto done;
6477 err = got_ref_write(*tmp_branch, repo);
6478 if (err)
6479 goto done;
6481 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6482 if (err)
6483 goto done;
6484 done:
6485 free(fileindex_path);
6486 free(tmp_branch_name);
6487 free(new_base_branch_ref_name);
6488 free(branch_ref_name);
6489 if (branch_ref)
6490 got_ref_close(branch_ref);
6491 if (wt_branch)
6492 got_ref_close(wt_branch);
6493 free(wt_branch_tip);
6494 if (err) {
6495 if (*new_base_branch_ref) {
6496 got_ref_close(*new_base_branch_ref);
6497 *new_base_branch_ref = NULL;
6499 if (*tmp_branch) {
6500 got_ref_close(*tmp_branch);
6501 *tmp_branch = NULL;
6503 if (*fileindex) {
6504 got_fileindex_free(*fileindex);
6505 *fileindex = NULL;
6507 lock_worktree(worktree, LOCK_SH);
6509 return err;
6512 const struct got_error *
6513 got_worktree_rebase_continue(struct got_object_id **commit_id,
6514 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6515 struct got_reference **branch, struct got_fileindex **fileindex,
6516 struct got_worktree *worktree, struct got_repository *repo)
6518 const struct got_error *err;
6519 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6520 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6521 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6522 char *fileindex_path = NULL;
6523 int have_staged_files = 0;
6525 *commit_id = NULL;
6526 *new_base_branch = NULL;
6527 *tmp_branch = NULL;
6528 *branch = NULL;
6529 *fileindex = NULL;
6531 err = lock_worktree(worktree, LOCK_EX);
6532 if (err)
6533 return err;
6535 err = open_fileindex(fileindex, &fileindex_path, worktree);
6536 if (err)
6537 goto done;
6539 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6540 &have_staged_files);
6541 if (err && err->code != GOT_ERR_CANCELLED)
6542 goto done;
6543 if (have_staged_files) {
6544 err = got_error(GOT_ERR_STAGED_PATHS);
6545 goto done;
6548 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6549 if (err)
6550 goto done;
6552 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6553 if (err)
6554 goto done;
6556 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6557 if (err)
6558 goto done;
6560 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6561 if (err)
6562 goto done;
6564 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6565 if (err)
6566 goto done;
6568 err = got_ref_open(branch, repo,
6569 got_ref_get_symref_target(branch_ref), 0);
6570 if (err)
6571 goto done;
6573 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6574 if (err)
6575 goto done;
6577 err = got_ref_resolve(commit_id, repo, commit_ref);
6578 if (err)
6579 goto done;
6581 err = got_ref_open(new_base_branch, repo,
6582 new_base_branch_ref_name, 0);
6583 if (err)
6584 goto done;
6586 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6587 if (err)
6588 goto done;
6589 done:
6590 free(commit_ref_name);
6591 free(branch_ref_name);
6592 free(fileindex_path);
6593 if (commit_ref)
6594 got_ref_close(commit_ref);
6595 if (branch_ref)
6596 got_ref_close(branch_ref);
6597 if (err) {
6598 free(*commit_id);
6599 *commit_id = NULL;
6600 if (*tmp_branch) {
6601 got_ref_close(*tmp_branch);
6602 *tmp_branch = NULL;
6604 if (*new_base_branch) {
6605 got_ref_close(*new_base_branch);
6606 *new_base_branch = NULL;
6608 if (*branch) {
6609 got_ref_close(*branch);
6610 *branch = NULL;
6612 if (*fileindex) {
6613 got_fileindex_free(*fileindex);
6614 *fileindex = NULL;
6616 lock_worktree(worktree, LOCK_SH);
6618 return err;
6621 const struct got_error *
6622 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6624 const struct got_error *err;
6625 char *tmp_branch_name = NULL;
6627 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6628 if (err)
6629 return err;
6631 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6632 free(tmp_branch_name);
6633 return NULL;
6636 static const struct got_error *
6637 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6638 const char *diff_path, char **logmsg, void *arg)
6640 *logmsg = arg;
6641 return NULL;
6644 static const struct got_error *
6645 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6646 const char *path, struct got_object_id *blob_id,
6647 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6648 int dirfd, const char *de_name)
6650 return NULL;
6653 struct collect_merged_paths_arg {
6654 got_worktree_checkout_cb progress_cb;
6655 void *progress_arg;
6656 struct got_pathlist_head *merged_paths;
6659 static const struct got_error *
6660 collect_merged_paths(void *arg, unsigned char status, const char *path)
6662 const struct got_error *err;
6663 struct collect_merged_paths_arg *a = arg;
6664 char *p;
6665 struct got_pathlist_entry *new;
6667 err = (*a->progress_cb)(a->progress_arg, status, path);
6668 if (err)
6669 return err;
6671 if (status != GOT_STATUS_MERGE &&
6672 status != GOT_STATUS_ADD &&
6673 status != GOT_STATUS_DELETE &&
6674 status != GOT_STATUS_CONFLICT)
6675 return NULL;
6677 p = strdup(path);
6678 if (p == NULL)
6679 return got_error_from_errno("strdup");
6681 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6682 if (err || new == NULL)
6683 free(p);
6684 return err;
6687 static const struct got_error *
6688 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6689 int is_rebase, struct got_repository *repo)
6691 const struct got_error *err;
6692 struct got_reference *commit_ref = NULL;
6694 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6695 if (err) {
6696 if (err->code != GOT_ERR_NOT_REF)
6697 goto done;
6698 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6699 if (err)
6700 goto done;
6701 err = got_ref_write(commit_ref, repo);
6702 if (err)
6703 goto done;
6704 } else if (is_rebase) {
6705 struct got_object_id *stored_id;
6706 int cmp;
6708 err = got_ref_resolve(&stored_id, repo, commit_ref);
6709 if (err)
6710 goto done;
6711 cmp = got_object_id_cmp(commit_id, stored_id);
6712 free(stored_id);
6713 if (cmp != 0) {
6714 err = got_error(GOT_ERR_REBASE_COMMITID);
6715 goto done;
6718 done:
6719 if (commit_ref)
6720 got_ref_close(commit_ref);
6721 return err;
6724 static const struct got_error *
6725 rebase_merge_files(struct got_pathlist_head *merged_paths,
6726 const char *commit_ref_name, struct got_worktree *worktree,
6727 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6728 struct got_object_id *commit_id, struct got_repository *repo,
6729 got_worktree_checkout_cb progress_cb, void *progress_arg,
6730 got_cancel_cb cancel_cb, void *cancel_arg)
6732 const struct got_error *err;
6733 struct got_reference *commit_ref = NULL;
6734 struct collect_merged_paths_arg cmp_arg;
6735 char *fileindex_path;
6737 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6739 err = get_fileindex_path(&fileindex_path, worktree);
6740 if (err)
6741 return err;
6743 cmp_arg.progress_cb = progress_cb;
6744 cmp_arg.progress_arg = progress_arg;
6745 cmp_arg.merged_paths = merged_paths;
6746 err = merge_files(worktree, fileindex, fileindex_path,
6747 parent_commit_id, commit_id, repo, collect_merged_paths,
6748 &cmp_arg, cancel_cb, cancel_arg);
6749 if (commit_ref)
6750 got_ref_close(commit_ref);
6751 return err;
6754 const struct got_error *
6755 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6756 struct got_worktree *worktree, struct got_fileindex *fileindex,
6757 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6758 struct got_repository *repo,
6759 got_worktree_checkout_cb progress_cb, void *progress_arg,
6760 got_cancel_cb cancel_cb, void *cancel_arg)
6762 const struct got_error *err;
6763 char *commit_ref_name;
6765 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6766 if (err)
6767 return err;
6769 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6770 if (err)
6771 goto done;
6773 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6774 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6775 progress_arg, cancel_cb, cancel_arg);
6776 done:
6777 free(commit_ref_name);
6778 return err;
6781 const struct got_error *
6782 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6783 struct got_worktree *worktree, struct got_fileindex *fileindex,
6784 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6785 struct got_repository *repo,
6786 got_worktree_checkout_cb progress_cb, void *progress_arg,
6787 got_cancel_cb cancel_cb, void *cancel_arg)
6789 const struct got_error *err;
6790 char *commit_ref_name;
6792 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6793 if (err)
6794 return err;
6796 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6797 if (err)
6798 goto done;
6800 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6801 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6802 progress_arg, cancel_cb, cancel_arg);
6803 done:
6804 free(commit_ref_name);
6805 return err;
6808 static const struct got_error *
6809 rebase_commit(struct got_object_id **new_commit_id,
6810 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6811 struct got_worktree *worktree, struct got_fileindex *fileindex,
6812 struct got_reference *tmp_branch, const char *committer,
6813 struct got_commit_object *orig_commit, const char *new_logmsg,
6814 int allow_conflict, struct got_repository *repo)
6816 const struct got_error *err, *sync_err;
6817 struct got_pathlist_head commitable_paths;
6818 struct collect_commitables_arg cc_arg;
6819 char *fileindex_path = NULL;
6820 struct got_reference *head_ref = NULL;
6821 struct got_object_id *head_commit_id = NULL;
6822 char *logmsg = NULL;
6824 memset(&cc_arg, 0, sizeof(cc_arg));
6825 TAILQ_INIT(&commitable_paths);
6826 *new_commit_id = NULL;
6828 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6830 err = get_fileindex_path(&fileindex_path, worktree);
6831 if (err)
6832 return err;
6834 cc_arg.commitable_paths = &commitable_paths;
6835 cc_arg.worktree = worktree;
6836 cc_arg.repo = repo;
6837 cc_arg.have_staged_files = 0;
6838 cc_arg.commit_conflicts = allow_conflict;
6840 * If possible get the status of individual files directly to
6841 * avoid crawling the entire work tree once per rebased commit.
6843 * Ideally, merged_paths would contain a list of commitables
6844 * we could use so we could skip worktree_status() entirely.
6845 * However, we would then need carefully keep track of cumulative
6846 * effects of operations such as file additions and deletions
6847 * in 'got histedit -f' (folding multiple commits into one),
6848 * and this extra complexity is not really worth it.
6850 if (merged_paths) {
6851 struct got_pathlist_entry *pe;
6852 TAILQ_FOREACH(pe, merged_paths, entry) {
6853 err = worktree_status(worktree, pe->path, fileindex,
6854 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6855 0);
6856 if (err)
6857 goto done;
6859 } else {
6860 err = worktree_status(worktree, "", fileindex, repo,
6861 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6862 if (err)
6863 goto done;
6866 if (TAILQ_EMPTY(&commitable_paths)) {
6867 /* No-op change; commit will be elided. */
6868 err = got_ref_delete(commit_ref, repo);
6869 if (err)
6870 goto done;
6871 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6872 goto done;
6875 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6876 if (err)
6877 goto done;
6879 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6880 if (err)
6881 goto done;
6883 if (new_logmsg) {
6884 logmsg = strdup(new_logmsg);
6885 if (logmsg == NULL) {
6886 err = got_error_from_errno("strdup");
6887 goto done;
6889 } else {
6890 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6891 if (err)
6892 goto done;
6895 /* NB: commit_worktree will call free(logmsg) */
6896 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6897 NULL, worktree, got_object_commit_get_author(orig_commit),
6898 committer ? committer :
6899 got_object_commit_get_committer(orig_commit), NULL,
6900 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6901 if (err)
6902 goto done;
6904 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6905 if (err)
6906 goto done;
6908 err = got_ref_delete(commit_ref, repo);
6909 if (err)
6910 goto done;
6912 err = update_fileindex_after_commit(worktree, &commitable_paths,
6913 *new_commit_id, fileindex, 0);
6914 sync_err = sync_fileindex(fileindex, fileindex_path);
6915 if (sync_err && err == NULL)
6916 err = sync_err;
6917 done:
6918 free(fileindex_path);
6919 free(head_commit_id);
6920 if (head_ref)
6921 got_ref_close(head_ref);
6922 if (err) {
6923 free(*new_commit_id);
6924 *new_commit_id = NULL;
6926 return err;
6929 const struct got_error *
6930 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6931 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6932 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6933 const char *committer, struct got_commit_object *orig_commit,
6934 struct got_object_id *orig_commit_id, int allow_conflict,
6935 struct got_repository *repo)
6937 const struct got_error *err;
6938 char *commit_ref_name;
6939 struct got_reference *commit_ref = NULL;
6940 struct got_object_id *commit_id = NULL;
6942 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6943 if (err)
6944 return err;
6946 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6947 if (err)
6948 goto done;
6949 err = got_ref_resolve(&commit_id, repo, commit_ref);
6950 if (err)
6951 goto done;
6952 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6953 err = got_error(GOT_ERR_REBASE_COMMITID);
6954 goto done;
6957 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6958 worktree, fileindex, tmp_branch, committer, orig_commit,
6959 NULL, allow_conflict, repo);
6960 done:
6961 if (commit_ref)
6962 got_ref_close(commit_ref);
6963 free(commit_ref_name);
6964 free(commit_id);
6965 return err;
6968 const struct got_error *
6969 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6970 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6971 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6972 const char *committer, struct got_commit_object *orig_commit,
6973 struct got_object_id *orig_commit_id, const char *new_logmsg,
6974 int allow_conflict, struct got_repository *repo)
6976 const struct got_error *err;
6977 char *commit_ref_name;
6978 struct got_reference *commit_ref = NULL;
6980 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6981 if (err)
6982 return err;
6984 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6985 if (err)
6986 goto done;
6988 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6989 worktree, fileindex, tmp_branch, committer, orig_commit,
6990 new_logmsg, allow_conflict, repo);
6991 done:
6992 if (commit_ref)
6993 got_ref_close(commit_ref);
6994 free(commit_ref_name);
6995 return err;
6998 const struct got_error *
6999 got_worktree_rebase_postpone(struct got_worktree *worktree,
7000 struct got_fileindex *fileindex)
7002 if (fileindex)
7003 got_fileindex_free(fileindex);
7004 return lock_worktree(worktree, LOCK_SH);
7007 static const struct got_error *
7008 delete_ref(const char *name, struct got_repository *repo)
7010 const struct got_error *err;
7011 struct got_reference *ref;
7013 err = got_ref_open(&ref, repo, name, 0);
7014 if (err) {
7015 if (err->code == GOT_ERR_NOT_REF)
7016 return NULL;
7017 return err;
7020 err = got_ref_delete(ref, repo);
7021 got_ref_close(ref);
7022 return err;
7025 static const struct got_error *
7026 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7028 const struct got_error *err;
7029 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7030 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7032 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7033 if (err)
7034 goto done;
7035 err = delete_ref(tmp_branch_name, repo);
7036 if (err)
7037 goto done;
7039 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7040 if (err)
7041 goto done;
7042 err = delete_ref(new_base_branch_ref_name, repo);
7043 if (err)
7044 goto done;
7046 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7047 if (err)
7048 goto done;
7049 err = delete_ref(branch_ref_name, repo);
7050 if (err)
7051 goto done;
7053 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7054 if (err)
7055 goto done;
7056 err = delete_ref(commit_ref_name, repo);
7057 if (err)
7058 goto done;
7060 done:
7061 free(tmp_branch_name);
7062 free(new_base_branch_ref_name);
7063 free(branch_ref_name);
7064 free(commit_ref_name);
7065 return err;
7068 static const struct got_error *
7069 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7070 struct got_object_id *new_commit_id, struct got_repository *repo)
7072 const struct got_error *err;
7073 struct got_reference *ref = NULL;
7074 struct got_object_id *old_commit_id = NULL;
7075 const char *branch_name = NULL;
7076 char *new_id_str = NULL;
7077 char *refname = NULL;
7079 branch_name = got_ref_get_name(branch);
7080 if (strncmp(branch_name, "refs/heads/", 11) != 0)
7081 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7082 branch_name += 11;
7084 err = got_object_id_str(&new_id_str, new_commit_id);
7085 if (err)
7086 return err;
7088 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7089 new_id_str) == -1) {
7090 err = got_error_from_errno("asprintf");
7091 goto done;
7094 err = got_ref_resolve(&old_commit_id, repo, branch);
7095 if (err)
7096 goto done;
7098 err = got_ref_alloc(&ref, refname, old_commit_id);
7099 if (err)
7100 goto done;
7102 err = got_ref_write(ref, repo);
7103 done:
7104 free(new_id_str);
7105 free(refname);
7106 free(old_commit_id);
7107 if (ref)
7108 got_ref_close(ref);
7109 return err;
7112 const struct got_error *
7113 got_worktree_rebase_complete(struct got_worktree *worktree,
7114 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7115 struct got_reference *rebased_branch, struct got_repository *repo,
7116 int create_backup)
7118 const struct got_error *err, *unlockerr, *sync_err;
7119 struct got_object_id *new_head_commit_id = NULL;
7120 char *fileindex_path = NULL;
7122 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7123 if (err)
7124 return err;
7126 if (create_backup) {
7127 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7128 rebased_branch, new_head_commit_id, repo);
7129 if (err)
7130 goto done;
7133 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7134 if (err)
7135 goto done;
7137 err = got_ref_write(rebased_branch, repo);
7138 if (err)
7139 goto done;
7141 err = got_worktree_set_head_ref(worktree, rebased_branch);
7142 if (err)
7143 goto done;
7145 err = delete_rebase_refs(worktree, repo);
7146 if (err)
7147 goto done;
7149 err = get_fileindex_path(&fileindex_path, worktree);
7150 if (err)
7151 goto done;
7152 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7153 sync_err = sync_fileindex(fileindex, fileindex_path);
7154 if (sync_err && err == NULL)
7155 err = sync_err;
7156 done:
7157 got_fileindex_free(fileindex);
7158 free(fileindex_path);
7159 free(new_head_commit_id);
7160 unlockerr = lock_worktree(worktree, LOCK_SH);
7161 if (unlockerr && err == NULL)
7162 err = unlockerr;
7163 return err;
7166 const struct got_error *
7167 got_worktree_rebase_abort(struct got_worktree *worktree,
7168 struct got_fileindex *fileindex, struct got_repository *repo,
7169 struct got_reference *new_base_branch,
7170 got_worktree_checkout_cb progress_cb, void *progress_arg)
7172 const struct got_error *err, *unlockerr, *sync_err;
7173 struct got_reference *resolved = NULL;
7174 struct got_object_id *commit_id = NULL;
7175 struct got_commit_object *commit = NULL;
7176 char *fileindex_path = NULL;
7177 struct revert_file_args rfa;
7178 struct got_object_id *tree_id = NULL;
7180 err = lock_worktree(worktree, LOCK_EX);
7181 if (err)
7182 return err;
7184 err = got_object_open_as_commit(&commit, repo,
7185 worktree->base_commit_id);
7186 if (err)
7187 goto done;
7189 err = got_ref_open(&resolved, repo,
7190 got_ref_get_symref_target(new_base_branch), 0);
7191 if (err)
7192 goto done;
7194 err = got_worktree_set_head_ref(worktree, resolved);
7195 if (err)
7196 goto done;
7199 * XXX commits to the base branch could have happened while
7200 * we were busy rebasing; should we store the original commit ID
7201 * when rebase begins and read it back here?
7203 err = got_ref_resolve(&commit_id, repo, resolved);
7204 if (err)
7205 goto done;
7207 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7208 if (err)
7209 goto done;
7211 err = got_object_id_by_path(&tree_id, repo, commit,
7212 worktree->path_prefix);
7213 if (err)
7214 goto done;
7216 err = delete_rebase_refs(worktree, repo);
7217 if (err)
7218 goto done;
7220 err = get_fileindex_path(&fileindex_path, worktree);
7221 if (err)
7222 goto done;
7224 rfa.worktree = worktree;
7225 rfa.fileindex = fileindex;
7226 rfa.progress_cb = progress_cb;
7227 rfa.progress_arg = progress_arg;
7228 rfa.patch_cb = NULL;
7229 rfa.patch_arg = NULL;
7230 rfa.repo = repo;
7231 rfa.unlink_added_files = 0;
7232 err = worktree_status(worktree, "", fileindex, repo,
7233 revert_file, &rfa, NULL, NULL, 1, 0);
7234 if (err)
7235 goto sync;
7237 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7238 repo, progress_cb, progress_arg, NULL, NULL);
7239 sync:
7240 sync_err = sync_fileindex(fileindex, fileindex_path);
7241 if (sync_err && err == NULL)
7242 err = sync_err;
7243 done:
7244 got_ref_close(resolved);
7245 free(tree_id);
7246 free(commit_id);
7247 if (commit)
7248 got_object_commit_close(commit);
7249 if (fileindex)
7250 got_fileindex_free(fileindex);
7251 free(fileindex_path);
7253 unlockerr = lock_worktree(worktree, LOCK_SH);
7254 if (unlockerr && err == NULL)
7255 err = unlockerr;
7256 return err;
7259 const struct got_error *
7260 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7261 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7262 struct got_fileindex **fileindex, struct got_worktree *worktree,
7263 struct got_repository *repo)
7265 const struct got_error *err = NULL;
7266 char *tmp_branch_name = NULL;
7267 char *branch_ref_name = NULL;
7268 char *base_commit_ref_name = NULL;
7269 char *fileindex_path = NULL;
7270 struct check_rebase_ok_arg ok_arg;
7271 struct got_reference *wt_branch = NULL;
7272 struct got_reference *base_commit_ref = NULL;
7274 *tmp_branch = NULL;
7275 *branch_ref = NULL;
7276 *base_commit_id = NULL;
7277 *fileindex = NULL;
7279 err = lock_worktree(worktree, LOCK_EX);
7280 if (err)
7281 return err;
7283 err = open_fileindex(fileindex, &fileindex_path, worktree);
7284 if (err)
7285 goto done;
7287 ok_arg.worktree = worktree;
7288 ok_arg.repo = repo;
7289 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7290 &ok_arg);
7291 if (err)
7292 goto done;
7294 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7295 if (err)
7296 goto done;
7298 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7299 if (err)
7300 goto done;
7302 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7303 worktree);
7304 if (err)
7305 goto done;
7307 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7308 0);
7309 if (err)
7310 goto done;
7312 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7313 if (err)
7314 goto done;
7316 err = got_ref_write(*branch_ref, repo);
7317 if (err)
7318 goto done;
7320 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7321 worktree->base_commit_id);
7322 if (err)
7323 goto done;
7324 err = got_ref_write(base_commit_ref, repo);
7325 if (err)
7326 goto done;
7327 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7328 if (*base_commit_id == NULL) {
7329 err = got_error_from_errno("got_object_id_dup");
7330 goto done;
7333 err = got_ref_alloc(tmp_branch, tmp_branch_name,
7334 worktree->base_commit_id);
7335 if (err)
7336 goto done;
7337 err = got_ref_write(*tmp_branch, repo);
7338 if (err)
7339 goto done;
7341 err = got_worktree_set_head_ref(worktree, *tmp_branch);
7342 if (err)
7343 goto done;
7344 done:
7345 free(fileindex_path);
7346 free(tmp_branch_name);
7347 free(branch_ref_name);
7348 free(base_commit_ref_name);
7349 if (wt_branch)
7350 got_ref_close(wt_branch);
7351 if (err) {
7352 if (*branch_ref) {
7353 got_ref_close(*branch_ref);
7354 *branch_ref = NULL;
7356 if (*tmp_branch) {
7357 got_ref_close(*tmp_branch);
7358 *tmp_branch = NULL;
7360 free(*base_commit_id);
7361 if (*fileindex) {
7362 got_fileindex_free(*fileindex);
7363 *fileindex = NULL;
7365 lock_worktree(worktree, LOCK_SH);
7367 return err;
7370 const struct got_error *
7371 got_worktree_histedit_postpone(struct got_worktree *worktree,
7372 struct got_fileindex *fileindex)
7374 if (fileindex)
7375 got_fileindex_free(fileindex);
7376 return lock_worktree(worktree, LOCK_SH);
7379 const struct got_error *
7380 got_worktree_histedit_in_progress(int *in_progress,
7381 struct got_worktree *worktree)
7383 const struct got_error *err;
7384 char *tmp_branch_name = NULL;
7386 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7387 if (err)
7388 return err;
7390 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7391 free(tmp_branch_name);
7392 return NULL;
7395 const struct got_error *
7396 got_worktree_histedit_continue(struct got_object_id **commit_id,
7397 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7398 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7399 struct got_worktree *worktree, struct got_repository *repo)
7401 const struct got_error *err;
7402 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7403 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7404 struct got_reference *commit_ref = NULL;
7405 struct got_reference *base_commit_ref = NULL;
7406 char *fileindex_path = NULL;
7407 int have_staged_files = 0;
7409 *commit_id = NULL;
7410 *tmp_branch = NULL;
7411 *base_commit_id = NULL;
7412 *fileindex = NULL;
7414 err = lock_worktree(worktree, LOCK_EX);
7415 if (err)
7416 return err;
7418 err = open_fileindex(fileindex, &fileindex_path, worktree);
7419 if (err)
7420 goto done;
7422 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7423 &have_staged_files);
7424 if (err && err->code != GOT_ERR_CANCELLED)
7425 goto done;
7426 if (have_staged_files) {
7427 err = got_error(GOT_ERR_STAGED_PATHS);
7428 goto done;
7431 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7432 if (err)
7433 goto done;
7435 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7436 if (err)
7437 goto done;
7439 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7440 if (err)
7441 goto done;
7443 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7444 worktree);
7445 if (err)
7446 goto done;
7448 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7449 if (err)
7450 goto done;
7452 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7453 if (err)
7454 goto done;
7455 err = got_ref_resolve(commit_id, repo, commit_ref);
7456 if (err)
7457 goto done;
7459 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7460 if (err)
7461 goto done;
7462 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7463 if (err)
7464 goto done;
7466 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7467 if (err)
7468 goto done;
7469 done:
7470 free(commit_ref_name);
7471 free(branch_ref_name);
7472 free(fileindex_path);
7473 if (commit_ref)
7474 got_ref_close(commit_ref);
7475 if (base_commit_ref)
7476 got_ref_close(base_commit_ref);
7477 if (err) {
7478 free(*commit_id);
7479 *commit_id = NULL;
7480 free(*base_commit_id);
7481 *base_commit_id = NULL;
7482 if (*tmp_branch) {
7483 got_ref_close(*tmp_branch);
7484 *tmp_branch = NULL;
7486 if (*fileindex) {
7487 got_fileindex_free(*fileindex);
7488 *fileindex = NULL;
7490 lock_worktree(worktree, LOCK_EX);
7492 return err;
7495 static const struct got_error *
7496 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7498 const struct got_error *err;
7499 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7500 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7502 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7503 if (err)
7504 goto done;
7505 err = delete_ref(tmp_branch_name, repo);
7506 if (err)
7507 goto done;
7509 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7510 worktree);
7511 if (err)
7512 goto done;
7513 err = delete_ref(base_commit_ref_name, repo);
7514 if (err)
7515 goto done;
7517 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7518 if (err)
7519 goto done;
7520 err = delete_ref(branch_ref_name, repo);
7521 if (err)
7522 goto done;
7524 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7525 if (err)
7526 goto done;
7527 err = delete_ref(commit_ref_name, repo);
7528 if (err)
7529 goto done;
7530 done:
7531 free(tmp_branch_name);
7532 free(base_commit_ref_name);
7533 free(branch_ref_name);
7534 free(commit_ref_name);
7535 return err;
7538 const struct got_error *
7539 got_worktree_histedit_abort(struct got_worktree *worktree,
7540 struct got_fileindex *fileindex, struct got_repository *repo,
7541 struct got_reference *branch, struct got_object_id *base_commit_id,
7542 got_worktree_checkout_cb progress_cb, void *progress_arg)
7544 const struct got_error *err, *unlockerr, *sync_err;
7545 struct got_reference *resolved = NULL;
7546 char *fileindex_path = NULL;
7547 struct got_commit_object *commit = NULL;
7548 struct got_object_id *tree_id = NULL;
7549 struct revert_file_args rfa;
7551 err = lock_worktree(worktree, LOCK_EX);
7552 if (err)
7553 return err;
7555 err = got_object_open_as_commit(&commit, repo,
7556 worktree->base_commit_id);
7557 if (err)
7558 goto done;
7560 err = got_ref_open(&resolved, repo,
7561 got_ref_get_symref_target(branch), 0);
7562 if (err)
7563 goto done;
7565 err = got_worktree_set_head_ref(worktree, resolved);
7566 if (err)
7567 goto done;
7569 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7570 if (err)
7571 goto done;
7573 err = got_object_id_by_path(&tree_id, repo, commit,
7574 worktree->path_prefix);
7575 if (err)
7576 goto done;
7578 err = delete_histedit_refs(worktree, repo);
7579 if (err)
7580 goto done;
7582 err = get_fileindex_path(&fileindex_path, worktree);
7583 if (err)
7584 goto done;
7586 rfa.worktree = worktree;
7587 rfa.fileindex = fileindex;
7588 rfa.progress_cb = progress_cb;
7589 rfa.progress_arg = progress_arg;
7590 rfa.patch_cb = NULL;
7591 rfa.patch_arg = NULL;
7592 rfa.repo = repo;
7593 rfa.unlink_added_files = 0;
7594 err = worktree_status(worktree, "", fileindex, repo,
7595 revert_file, &rfa, NULL, NULL, 1, 0);
7596 if (err)
7597 goto sync;
7599 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7600 repo, progress_cb, progress_arg, NULL, NULL);
7601 sync:
7602 sync_err = sync_fileindex(fileindex, fileindex_path);
7603 if (sync_err && err == NULL)
7604 err = sync_err;
7605 done:
7606 got_ref_close(resolved);
7607 free(tree_id);
7608 free(fileindex_path);
7610 unlockerr = lock_worktree(worktree, LOCK_SH);
7611 if (unlockerr && err == NULL)
7612 err = unlockerr;
7613 return err;
7616 const struct got_error *
7617 got_worktree_histedit_complete(struct got_worktree *worktree,
7618 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7619 struct got_reference *edited_branch, struct got_repository *repo)
7621 const struct got_error *err, *unlockerr, *sync_err;
7622 struct got_object_id *new_head_commit_id = NULL;
7623 struct got_reference *resolved = NULL;
7624 char *fileindex_path = NULL;
7626 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7627 if (err)
7628 return err;
7630 err = got_ref_open(&resolved, repo,
7631 got_ref_get_symref_target(edited_branch), 0);
7632 if (err)
7633 goto done;
7635 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7636 resolved, new_head_commit_id, repo);
7637 if (err)
7638 goto done;
7640 err = got_ref_change_ref(resolved, new_head_commit_id);
7641 if (err)
7642 goto done;
7644 err = got_ref_write(resolved, repo);
7645 if (err)
7646 goto done;
7648 err = got_worktree_set_head_ref(worktree, resolved);
7649 if (err)
7650 goto done;
7652 err = delete_histedit_refs(worktree, repo);
7653 if (err)
7654 goto done;
7656 err = get_fileindex_path(&fileindex_path, worktree);
7657 if (err)
7658 goto done;
7659 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7660 sync_err = sync_fileindex(fileindex, fileindex_path);
7661 if (sync_err && err == NULL)
7662 err = sync_err;
7663 done:
7664 got_fileindex_free(fileindex);
7665 free(fileindex_path);
7666 free(new_head_commit_id);
7667 unlockerr = lock_worktree(worktree, LOCK_SH);
7668 if (unlockerr && err == NULL)
7669 err = unlockerr;
7670 return err;
7673 const struct got_error *
7674 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7675 struct got_object_id *commit_id, struct got_repository *repo)
7677 const struct got_error *err;
7678 char *commit_ref_name;
7680 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7681 if (err)
7682 return err;
7684 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7685 if (err)
7686 goto done;
7688 err = delete_ref(commit_ref_name, repo);
7689 done:
7690 free(commit_ref_name);
7691 return err;
7694 const struct got_error *
7695 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7696 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7697 struct got_worktree *worktree, const char *refname,
7698 struct got_repository *repo)
7700 const struct got_error *err = NULL;
7701 char *fileindex_path = NULL;
7702 struct check_rebase_ok_arg ok_arg;
7704 *fileindex = NULL;
7705 *branch_ref = NULL;
7706 *base_branch_ref = NULL;
7708 err = lock_worktree(worktree, LOCK_EX);
7709 if (err)
7710 return err;
7712 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7713 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7714 "cannot integrate a branch into itself; "
7715 "update -b or different branch name required");
7716 goto done;
7719 err = open_fileindex(fileindex, &fileindex_path, worktree);
7720 if (err)
7721 goto done;
7723 /* Preconditions are the same as for rebase. */
7724 ok_arg.worktree = worktree;
7725 ok_arg.repo = repo;
7726 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7727 &ok_arg);
7728 if (err)
7729 goto done;
7731 err = got_ref_open(branch_ref, repo, refname, 1);
7732 if (err)
7733 goto done;
7735 err = got_ref_open(base_branch_ref, repo,
7736 got_worktree_get_head_ref_name(worktree), 1);
7737 done:
7738 if (err) {
7739 if (*branch_ref) {
7740 got_ref_close(*branch_ref);
7741 *branch_ref = NULL;
7743 if (*base_branch_ref) {
7744 got_ref_close(*base_branch_ref);
7745 *base_branch_ref = NULL;
7747 if (*fileindex) {
7748 got_fileindex_free(*fileindex);
7749 *fileindex = NULL;
7751 lock_worktree(worktree, LOCK_SH);
7753 return err;
7756 const struct got_error *
7757 got_worktree_integrate_continue(struct got_worktree *worktree,
7758 struct got_fileindex *fileindex, struct got_repository *repo,
7759 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7760 got_worktree_checkout_cb progress_cb, void *progress_arg,
7761 got_cancel_cb cancel_cb, void *cancel_arg)
7763 const struct got_error *err = NULL, *sync_err, *unlockerr;
7764 char *fileindex_path = NULL;
7765 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7766 struct got_commit_object *commit = NULL;
7768 err = get_fileindex_path(&fileindex_path, worktree);
7769 if (err)
7770 goto done;
7772 err = got_ref_resolve(&commit_id, repo, branch_ref);
7773 if (err)
7774 goto done;
7776 err = got_object_open_as_commit(&commit, repo, commit_id);
7777 if (err)
7778 goto done;
7780 err = got_object_id_by_path(&tree_id, repo, commit,
7781 worktree->path_prefix);
7782 if (err)
7783 goto done;
7785 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7786 if (err)
7787 goto done;
7789 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7790 progress_cb, progress_arg, cancel_cb, cancel_arg);
7791 if (err)
7792 goto sync;
7794 err = got_ref_change_ref(base_branch_ref, commit_id);
7795 if (err)
7796 goto sync;
7798 err = got_ref_write(base_branch_ref, repo);
7799 if (err)
7800 goto sync;
7802 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7803 sync:
7804 sync_err = sync_fileindex(fileindex, fileindex_path);
7805 if (sync_err && err == NULL)
7806 err = sync_err;
7808 done:
7809 unlockerr = got_ref_unlock(branch_ref);
7810 if (unlockerr && err == NULL)
7811 err = unlockerr;
7812 got_ref_close(branch_ref);
7814 unlockerr = got_ref_unlock(base_branch_ref);
7815 if (unlockerr && err == NULL)
7816 err = unlockerr;
7817 got_ref_close(base_branch_ref);
7819 got_fileindex_free(fileindex);
7820 free(fileindex_path);
7821 free(tree_id);
7822 if (commit)
7823 got_object_commit_close(commit);
7825 unlockerr = lock_worktree(worktree, LOCK_SH);
7826 if (unlockerr && err == NULL)
7827 err = unlockerr;
7828 return err;
7831 const struct got_error *
7832 got_worktree_integrate_abort(struct got_worktree *worktree,
7833 struct got_fileindex *fileindex, struct got_repository *repo,
7834 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7836 const struct got_error *err = NULL, *unlockerr = NULL;
7838 got_fileindex_free(fileindex);
7840 err = lock_worktree(worktree, LOCK_SH);
7842 unlockerr = got_ref_unlock(branch_ref);
7843 if (unlockerr && err == NULL)
7844 err = unlockerr;
7845 got_ref_close(branch_ref);
7847 unlockerr = got_ref_unlock(base_branch_ref);
7848 if (unlockerr && err == NULL)
7849 err = unlockerr;
7850 got_ref_close(base_branch_ref);
7852 return err;
7855 const struct got_error *
7856 got_worktree_merge_postpone(struct got_worktree *worktree,
7857 struct got_fileindex *fileindex)
7859 const struct got_error *err, *sync_err;
7860 char *fileindex_path = NULL;
7862 err = get_fileindex_path(&fileindex_path, worktree);
7863 if (err)
7864 goto done;
7866 sync_err = sync_fileindex(fileindex, fileindex_path);
7868 err = lock_worktree(worktree, LOCK_SH);
7869 if (sync_err && err == NULL)
7870 err = sync_err;
7871 done:
7872 got_fileindex_free(fileindex);
7873 free(fileindex_path);
7874 return err;
7877 static const struct got_error *
7878 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7880 const struct got_error *err;
7881 char *branch_refname = NULL, *commit_refname = NULL;
7883 err = get_merge_branch_ref_name(&branch_refname, worktree);
7884 if (err)
7885 goto done;
7886 err = delete_ref(branch_refname, repo);
7887 if (err)
7888 goto done;
7890 err = get_merge_commit_ref_name(&commit_refname, worktree);
7891 if (err)
7892 goto done;
7893 err = delete_ref(commit_refname, repo);
7894 if (err)
7895 goto done;
7897 done:
7898 free(branch_refname);
7899 free(commit_refname);
7900 return err;
7903 struct merge_commit_msg_arg {
7904 struct got_worktree *worktree;
7905 const char *branch_name;
7908 static const struct got_error *
7909 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
7910 const char *diff_path, char **logmsg, void *arg)
7912 struct merge_commit_msg_arg *a = arg;
7914 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7915 got_worktree_get_head_ref_name(a->worktree)) == -1)
7916 return got_error_from_errno("asprintf");
7918 return NULL;
7922 const struct got_error *
7923 got_worktree_merge_branch(struct got_worktree *worktree,
7924 struct got_fileindex *fileindex,
7925 struct got_object_id *yca_commit_id,
7926 struct got_object_id *branch_tip,
7927 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7928 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7930 const struct got_error *err;
7931 char *fileindex_path = NULL;
7933 err = get_fileindex_path(&fileindex_path, worktree);
7934 if (err)
7935 goto done;
7937 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7938 worktree);
7939 if (err)
7940 goto done;
7942 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7943 branch_tip, repo, progress_cb, progress_arg,
7944 cancel_cb, cancel_arg);
7945 done:
7946 free(fileindex_path);
7947 return err;
7950 const struct got_error *
7951 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7952 struct got_worktree *worktree, struct got_fileindex *fileindex,
7953 const char *author, const char *committer, int allow_bad_symlinks,
7954 struct got_object_id *branch_tip, const char *branch_name,
7955 int allow_conflict, struct got_repository *repo,
7956 got_worktree_status_cb status_cb, void *status_arg)
7959 const struct got_error *err = NULL, *sync_err;
7960 struct got_pathlist_head commitable_paths;
7961 struct collect_commitables_arg cc_arg;
7962 struct got_pathlist_entry *pe;
7963 struct got_reference *head_ref = NULL;
7964 struct got_object_id *head_commit_id = NULL;
7965 int have_staged_files = 0;
7966 struct merge_commit_msg_arg mcm_arg;
7967 char *fileindex_path = NULL;
7969 memset(&cc_arg, 0, sizeof(cc_arg));
7970 *new_commit_id = NULL;
7972 TAILQ_INIT(&commitable_paths);
7974 err = get_fileindex_path(&fileindex_path, worktree);
7975 if (err)
7976 goto done;
7978 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7979 if (err)
7980 goto done;
7982 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7983 if (err)
7984 goto done;
7986 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7987 &have_staged_files);
7988 if (err && err->code != GOT_ERR_CANCELLED)
7989 goto done;
7990 if (have_staged_files) {
7991 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7992 goto done;
7995 cc_arg.commitable_paths = &commitable_paths;
7996 cc_arg.worktree = worktree;
7997 cc_arg.fileindex = fileindex;
7998 cc_arg.repo = repo;
7999 cc_arg.have_staged_files = have_staged_files;
8000 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
8001 cc_arg.commit_conflicts = allow_conflict;
8002 err = worktree_status(worktree, "", fileindex, repo,
8003 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8004 if (err)
8005 goto done;
8007 if (TAILQ_EMPTY(&commitable_paths)) {
8008 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
8009 "merge of %s cannot proceed", branch_name);
8010 goto done;
8013 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8014 struct got_commitable *ct = pe->data;
8015 const char *ct_path = ct->in_repo_path;
8017 while (ct_path[0] == '/')
8018 ct_path++;
8019 err = check_out_of_date(ct_path, ct->status,
8020 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
8021 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
8022 if (err)
8023 goto done;
8027 mcm_arg.worktree = worktree;
8028 mcm_arg.branch_name = branch_name;
8029 err = commit_worktree(new_commit_id, &commitable_paths,
8030 head_commit_id, branch_tip, worktree, author, committer, NULL,
8031 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8032 if (err)
8033 goto done;
8035 err = update_fileindex_after_commit(worktree, &commitable_paths,
8036 *new_commit_id, fileindex, have_staged_files);
8037 sync_err = sync_fileindex(fileindex, fileindex_path);
8038 if (sync_err && err == NULL)
8039 err = sync_err;
8040 done:
8041 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8042 struct got_commitable *ct = pe->data;
8044 free_commitable(ct);
8046 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8047 free(fileindex_path);
8048 return err;
8051 const struct got_error *
8052 got_worktree_merge_complete(struct got_worktree *worktree,
8053 struct got_fileindex *fileindex, struct got_repository *repo)
8055 const struct got_error *err, *unlockerr, *sync_err;
8056 char *fileindex_path = NULL;
8058 err = delete_merge_refs(worktree, repo);
8059 if (err)
8060 goto done;
8062 err = get_fileindex_path(&fileindex_path, worktree);
8063 if (err)
8064 goto done;
8065 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8066 sync_err = sync_fileindex(fileindex, fileindex_path);
8067 if (sync_err && err == NULL)
8068 err = sync_err;
8069 done:
8070 got_fileindex_free(fileindex);
8071 free(fileindex_path);
8072 unlockerr = lock_worktree(worktree, LOCK_SH);
8073 if (unlockerr && err == NULL)
8074 err = unlockerr;
8075 return err;
8078 const struct got_error *
8079 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8080 struct got_repository *repo)
8082 const struct got_error *err;
8083 char *branch_refname = NULL;
8084 struct got_reference *branch_ref = NULL;
8086 *in_progress = 0;
8088 err = get_merge_branch_ref_name(&branch_refname, worktree);
8089 if (err)
8090 return err;
8091 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8092 free(branch_refname);
8093 if (err) {
8094 if (err->code != GOT_ERR_NOT_REF)
8095 return err;
8096 } else
8097 *in_progress = 1;
8099 return NULL;
8102 const struct got_error *got_worktree_merge_prepare(
8103 struct got_fileindex **fileindex, struct got_worktree *worktree,
8104 struct got_reference *branch, struct got_repository *repo)
8106 const struct got_error *err = NULL;
8107 char *fileindex_path = NULL;
8108 char *branch_refname = NULL, *commit_refname = NULL;
8109 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
8110 struct got_reference *commit_ref = NULL;
8111 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
8112 struct check_rebase_ok_arg ok_arg;
8114 *fileindex = NULL;
8116 err = lock_worktree(worktree, LOCK_EX);
8117 if (err)
8118 return err;
8120 err = open_fileindex(fileindex, &fileindex_path, worktree);
8121 if (err)
8122 goto done;
8124 /* Preconditions are the same as for rebase. */
8125 ok_arg.worktree = worktree;
8126 ok_arg.repo = repo;
8127 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8128 &ok_arg);
8129 if (err)
8130 goto done;
8132 err = get_merge_branch_ref_name(&branch_refname, worktree);
8133 if (err)
8134 return err;
8136 err = get_merge_commit_ref_name(&commit_refname, worktree);
8137 if (err)
8138 return err;
8140 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8141 0);
8142 if (err)
8143 goto done;
8145 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8146 if (err)
8147 goto done;
8149 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8150 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8151 goto done;
8154 err = got_ref_resolve(&branch_tip, repo, branch);
8155 if (err)
8156 goto done;
8158 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8159 if (err)
8160 goto done;
8161 err = got_ref_write(branch_ref, repo);
8162 if (err)
8163 goto done;
8165 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8166 if (err)
8167 goto done;
8168 err = got_ref_write(commit_ref, repo);
8169 if (err)
8170 goto done;
8172 done:
8173 free(branch_refname);
8174 free(commit_refname);
8175 free(fileindex_path);
8176 if (branch_ref)
8177 got_ref_close(branch_ref);
8178 if (commit_ref)
8179 got_ref_close(commit_ref);
8180 if (wt_branch)
8181 got_ref_close(wt_branch);
8182 free(wt_branch_tip);
8183 if (err) {
8184 if (*fileindex) {
8185 got_fileindex_free(*fileindex);
8186 *fileindex = NULL;
8188 lock_worktree(worktree, LOCK_SH);
8190 return err;
8193 const struct got_error *
8194 got_worktree_merge_continue(char **branch_name,
8195 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8196 struct got_worktree *worktree, struct got_repository *repo)
8198 const struct got_error *err;
8199 char *commit_refname = NULL, *branch_refname = NULL;
8200 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8201 char *fileindex_path = NULL;
8202 int have_staged_files = 0;
8204 *branch_name = NULL;
8205 *branch_tip = NULL;
8206 *fileindex = NULL;
8208 err = lock_worktree(worktree, LOCK_EX);
8209 if (err)
8210 return err;
8212 err = open_fileindex(fileindex, &fileindex_path, worktree);
8213 if (err)
8214 goto done;
8216 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8217 &have_staged_files);
8218 if (err && err->code != GOT_ERR_CANCELLED)
8219 goto done;
8220 if (have_staged_files) {
8221 err = got_error(GOT_ERR_STAGED_PATHS);
8222 goto done;
8225 err = get_merge_branch_ref_name(&branch_refname, worktree);
8226 if (err)
8227 goto done;
8229 err = get_merge_commit_ref_name(&commit_refname, worktree);
8230 if (err)
8231 goto done;
8233 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8234 if (err)
8235 goto done;
8237 if (!got_ref_is_symbolic(branch_ref)) {
8238 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8239 "%s is not a symbolic reference",
8240 got_ref_get_name(branch_ref));
8241 goto done;
8243 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8244 if (*branch_name == NULL) {
8245 err = got_error_from_errno("strdup");
8246 goto done;
8249 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8250 if (err)
8251 goto done;
8253 err = got_ref_resolve(branch_tip, repo, commit_ref);
8254 if (err)
8255 goto done;
8256 done:
8257 free(commit_refname);
8258 free(branch_refname);
8259 free(fileindex_path);
8260 if (commit_ref)
8261 got_ref_close(commit_ref);
8262 if (branch_ref)
8263 got_ref_close(branch_ref);
8264 if (err) {
8265 if (*branch_name) {
8266 free(*branch_name);
8267 *branch_name = NULL;
8269 free(*branch_tip);
8270 *branch_tip = NULL;
8271 if (*fileindex) {
8272 got_fileindex_free(*fileindex);
8273 *fileindex = NULL;
8275 lock_worktree(worktree, LOCK_SH);
8277 return err;
8280 const struct got_error *
8281 got_worktree_merge_abort(struct got_worktree *worktree,
8282 struct got_fileindex *fileindex, struct got_repository *repo,
8283 got_worktree_checkout_cb progress_cb, void *progress_arg)
8285 const struct got_error *err, *unlockerr, *sync_err;
8286 struct got_object_id *commit_id = NULL;
8287 struct got_commit_object *commit = NULL;
8288 char *fileindex_path = NULL;
8289 struct revert_file_args rfa;
8290 struct got_object_id *tree_id = NULL;
8292 err = got_object_open_as_commit(&commit, repo,
8293 worktree->base_commit_id);
8294 if (err)
8295 goto done;
8297 err = got_object_id_by_path(&tree_id, repo, commit,
8298 worktree->path_prefix);
8299 if (err)
8300 goto done;
8302 err = delete_merge_refs(worktree, repo);
8303 if (err)
8304 goto done;
8306 err = get_fileindex_path(&fileindex_path, worktree);
8307 if (err)
8308 goto done;
8310 rfa.worktree = worktree;
8311 rfa.fileindex = fileindex;
8312 rfa.progress_cb = progress_cb;
8313 rfa.progress_arg = progress_arg;
8314 rfa.patch_cb = NULL;
8315 rfa.patch_arg = NULL;
8316 rfa.repo = repo;
8317 rfa.unlink_added_files = 1;
8318 err = worktree_status(worktree, "", fileindex, repo,
8319 revert_file, &rfa, NULL, NULL, 1, 0);
8320 if (err)
8321 goto sync;
8323 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8324 repo, progress_cb, progress_arg, NULL, NULL);
8325 sync:
8326 sync_err = sync_fileindex(fileindex, fileindex_path);
8327 if (sync_err && err == NULL)
8328 err = sync_err;
8329 done:
8330 free(tree_id);
8331 free(commit_id);
8332 if (commit)
8333 got_object_commit_close(commit);
8334 if (fileindex)
8335 got_fileindex_free(fileindex);
8336 free(fileindex_path);
8338 unlockerr = lock_worktree(worktree, LOCK_SH);
8339 if (unlockerr && err == NULL)
8340 err = unlockerr;
8341 return err;
8344 struct check_stage_ok_arg {
8345 struct got_object_id *head_commit_id;
8346 struct got_worktree *worktree;
8347 struct got_fileindex *fileindex;
8348 struct got_repository *repo;
8349 int have_changes;
8352 static const struct got_error *
8353 check_stage_ok(void *arg, unsigned char status,
8354 unsigned char staged_status, const char *relpath,
8355 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8356 struct got_object_id *commit_id, int dirfd, const char *de_name)
8358 struct check_stage_ok_arg *a = arg;
8359 const struct got_error *err = NULL;
8360 struct got_fileindex_entry *ie;
8361 struct got_object_id base_commit_id;
8362 struct got_object_id *base_commit_idp = NULL;
8363 char *in_repo_path = NULL, *p;
8365 if (status == GOT_STATUS_UNVERSIONED ||
8366 status == GOT_STATUS_NO_CHANGE)
8367 return NULL;
8368 if (status == GOT_STATUS_NONEXISTENT)
8369 return got_error_set_errno(ENOENT, relpath);
8371 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8372 if (ie == NULL)
8373 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8375 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8376 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8377 relpath) == -1)
8378 return got_error_from_errno("asprintf");
8380 if (got_fileindex_entry_has_commit(ie)) {
8381 base_commit_idp = got_fileindex_entry_get_commit_id(
8382 &base_commit_id, ie);
8385 if (status == GOT_STATUS_CONFLICT) {
8386 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8387 goto done;
8388 } else if (status != GOT_STATUS_ADD &&
8389 status != GOT_STATUS_MODIFY &&
8390 status != GOT_STATUS_DELETE) {
8391 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8392 goto done;
8395 a->have_changes = 1;
8397 p = in_repo_path;
8398 while (p[0] == '/')
8399 p++;
8400 err = check_out_of_date(p, status, staged_status,
8401 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8402 GOT_ERR_STAGE_OUT_OF_DATE);
8403 done:
8404 free(in_repo_path);
8405 return err;
8408 struct stage_path_arg {
8409 struct got_worktree *worktree;
8410 struct got_fileindex *fileindex;
8411 struct got_repository *repo;
8412 got_worktree_status_cb status_cb;
8413 void *status_arg;
8414 got_worktree_patch_cb patch_cb;
8415 void *patch_arg;
8416 int staged_something;
8417 int allow_bad_symlinks;
8420 static const struct got_error *
8421 stage_path(void *arg, unsigned char status,
8422 unsigned char staged_status, const char *relpath,
8423 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8424 struct got_object_id *commit_id, int dirfd, const char *de_name)
8426 struct stage_path_arg *a = arg;
8427 const struct got_error *err = NULL;
8428 struct got_fileindex_entry *ie;
8429 char *ondisk_path = NULL, *path_content = NULL;
8430 uint32_t stage;
8431 struct got_object_id *new_staged_blob_id = NULL;
8432 struct stat sb;
8434 if (status == GOT_STATUS_UNVERSIONED)
8435 return NULL;
8437 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8438 if (ie == NULL)
8439 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8441 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8442 relpath)== -1)
8443 return got_error_from_errno("asprintf");
8445 switch (status) {
8446 case GOT_STATUS_ADD:
8447 case GOT_STATUS_MODIFY:
8448 /* XXX could sb.st_mode be passed in by our caller? */
8449 if (lstat(ondisk_path, &sb) == -1) {
8450 err = got_error_from_errno2("lstat", ondisk_path);
8451 break;
8453 if (a->patch_cb) {
8454 if (status == GOT_STATUS_ADD) {
8455 int choice = GOT_PATCH_CHOICE_NONE;
8456 err = (*a->patch_cb)(&choice, a->patch_arg,
8457 status, ie->path, NULL, 1, 1);
8458 if (err)
8459 break;
8460 if (choice != GOT_PATCH_CHOICE_YES)
8461 break;
8462 } else {
8463 err = create_patched_content(&path_content, 0,
8464 staged_blob_id ? staged_blob_id : blob_id,
8465 ondisk_path, dirfd, de_name, ie->path,
8466 a->repo, a->patch_cb, a->patch_arg);
8467 if (err || path_content == NULL)
8468 break;
8471 err = got_object_blob_create(&new_staged_blob_id,
8472 path_content ? path_content : ondisk_path, a->repo);
8473 if (err)
8474 break;
8475 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8476 SHA1_DIGEST_LENGTH);
8477 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8478 stage = GOT_FILEIDX_STAGE_ADD;
8479 else
8480 stage = GOT_FILEIDX_STAGE_MODIFY;
8481 got_fileindex_entry_stage_set(ie, stage);
8482 if (S_ISLNK(sb.st_mode)) {
8483 int is_bad_symlink = 0;
8484 if (!a->allow_bad_symlinks) {
8485 char target_path[PATH_MAX];
8486 ssize_t target_len;
8487 target_len = readlink(ondisk_path, target_path,
8488 sizeof(target_path));
8489 if (target_len == -1) {
8490 err = got_error_from_errno2("readlink",
8491 ondisk_path);
8492 break;
8494 err = is_bad_symlink_target(&is_bad_symlink,
8495 target_path, target_len, ondisk_path,
8496 a->worktree->root_path);
8497 if (err)
8498 break;
8499 if (is_bad_symlink) {
8500 err = got_error_path(ondisk_path,
8501 GOT_ERR_BAD_SYMLINK);
8502 break;
8505 if (is_bad_symlink)
8506 got_fileindex_entry_staged_filetype_set(ie,
8507 GOT_FILEIDX_MODE_BAD_SYMLINK);
8508 else
8509 got_fileindex_entry_staged_filetype_set(ie,
8510 GOT_FILEIDX_MODE_SYMLINK);
8511 } else {
8512 got_fileindex_entry_staged_filetype_set(ie,
8513 GOT_FILEIDX_MODE_REGULAR_FILE);
8515 a->staged_something = 1;
8516 if (a->status_cb == NULL)
8517 break;
8518 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8519 get_staged_status(ie), relpath, blob_id,
8520 new_staged_blob_id, NULL, dirfd, de_name);
8521 if (err)
8522 break;
8524 * When staging the reverse of the staged diff,
8525 * implicitly unstage the file.
8527 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8528 sizeof(ie->blob_sha1)) == 0) {
8529 got_fileindex_entry_stage_set(ie,
8530 GOT_FILEIDX_STAGE_NONE);
8532 break;
8533 case GOT_STATUS_DELETE:
8534 if (staged_status == GOT_STATUS_DELETE)
8535 break;
8536 if (a->patch_cb) {
8537 int choice = GOT_PATCH_CHOICE_NONE;
8538 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8539 ie->path, NULL, 1, 1);
8540 if (err)
8541 break;
8542 if (choice == GOT_PATCH_CHOICE_NO)
8543 break;
8544 if (choice != GOT_PATCH_CHOICE_YES) {
8545 err = got_error(GOT_ERR_PATCH_CHOICE);
8546 break;
8549 stage = GOT_FILEIDX_STAGE_DELETE;
8550 got_fileindex_entry_stage_set(ie, stage);
8551 a->staged_something = 1;
8552 if (a->status_cb == NULL)
8553 break;
8554 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8555 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8556 de_name);
8557 break;
8558 case GOT_STATUS_NO_CHANGE:
8559 break;
8560 case GOT_STATUS_CONFLICT:
8561 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8562 break;
8563 case GOT_STATUS_NONEXISTENT:
8564 err = got_error_set_errno(ENOENT, relpath);
8565 break;
8566 default:
8567 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8568 break;
8571 if (path_content && unlink(path_content) == -1 && err == NULL)
8572 err = got_error_from_errno2("unlink", path_content);
8573 free(path_content);
8574 free(ondisk_path);
8575 free(new_staged_blob_id);
8576 return err;
8579 const struct got_error *
8580 got_worktree_stage(struct got_worktree *worktree,
8581 struct got_pathlist_head *paths,
8582 got_worktree_status_cb status_cb, void *status_arg,
8583 got_worktree_patch_cb patch_cb, void *patch_arg,
8584 int allow_bad_symlinks, struct got_repository *repo)
8586 const struct got_error *err = NULL, *sync_err, *unlockerr;
8587 struct got_pathlist_entry *pe;
8588 struct got_fileindex *fileindex = NULL;
8589 char *fileindex_path = NULL;
8590 struct got_reference *head_ref = NULL;
8591 struct got_object_id *head_commit_id = NULL;
8592 struct check_stage_ok_arg oka;
8593 struct stage_path_arg spa;
8595 err = lock_worktree(worktree, LOCK_EX);
8596 if (err)
8597 return err;
8599 err = got_ref_open(&head_ref, repo,
8600 got_worktree_get_head_ref_name(worktree), 0);
8601 if (err)
8602 goto done;
8603 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8604 if (err)
8605 goto done;
8606 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8607 if (err)
8608 goto done;
8610 /* Check pre-conditions before staging anything. */
8611 oka.head_commit_id = head_commit_id;
8612 oka.worktree = worktree;
8613 oka.fileindex = fileindex;
8614 oka.repo = repo;
8615 oka.have_changes = 0;
8616 TAILQ_FOREACH(pe, paths, entry) {
8617 err = worktree_status(worktree, pe->path, fileindex, repo,
8618 check_stage_ok, &oka, NULL, NULL, 1, 0);
8619 if (err)
8620 goto done;
8622 if (!oka.have_changes) {
8623 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8624 goto done;
8627 spa.worktree = worktree;
8628 spa.fileindex = fileindex;
8629 spa.repo = repo;
8630 spa.patch_cb = patch_cb;
8631 spa.patch_arg = patch_arg;
8632 spa.status_cb = status_cb;
8633 spa.status_arg = status_arg;
8634 spa.staged_something = 0;
8635 spa.allow_bad_symlinks = allow_bad_symlinks;
8636 TAILQ_FOREACH(pe, paths, entry) {
8637 err = worktree_status(worktree, pe->path, fileindex, repo,
8638 stage_path, &spa, NULL, NULL, 1, 0);
8639 if (err)
8640 goto done;
8642 if (!spa.staged_something) {
8643 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8644 goto done;
8647 sync_err = sync_fileindex(fileindex, fileindex_path);
8648 if (sync_err && err == NULL)
8649 err = sync_err;
8650 done:
8651 if (head_ref)
8652 got_ref_close(head_ref);
8653 free(head_commit_id);
8654 free(fileindex_path);
8655 if (fileindex)
8656 got_fileindex_free(fileindex);
8657 unlockerr = lock_worktree(worktree, LOCK_SH);
8658 if (unlockerr && err == NULL)
8659 err = unlockerr;
8660 return err;
8663 struct unstage_path_arg {
8664 struct got_worktree *worktree;
8665 struct got_fileindex *fileindex;
8666 struct got_repository *repo;
8667 got_worktree_checkout_cb progress_cb;
8668 void *progress_arg;
8669 got_worktree_patch_cb patch_cb;
8670 void *patch_arg;
8673 static const struct got_error *
8674 create_unstaged_content(char **path_unstaged_content,
8675 char **path_new_staged_content, struct got_object_id *blob_id,
8676 struct got_object_id *staged_blob_id, const char *relpath,
8677 struct got_repository *repo,
8678 got_worktree_patch_cb patch_cb, void *patch_arg)
8680 const struct got_error *err, *free_err;
8681 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8682 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8683 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8684 struct got_diffreg_result *diffreg_result = NULL;
8685 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8686 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8687 int fd1 = -1, fd2 = -1;
8689 *path_unstaged_content = NULL;
8690 *path_new_staged_content = NULL;
8692 err = got_object_id_str(&label1, blob_id);
8693 if (err)
8694 return err;
8696 fd1 = got_opentempfd();
8697 if (fd1 == -1) {
8698 err = got_error_from_errno("got_opentempfd");
8699 goto done;
8701 fd2 = got_opentempfd();
8702 if (fd2 == -1) {
8703 err = got_error_from_errno("got_opentempfd");
8704 goto done;
8707 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8708 if (err)
8709 goto done;
8711 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
8712 if (err)
8713 goto done;
8715 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8716 if (err)
8717 goto done;
8719 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8720 fd2);
8721 if (err)
8722 goto done;
8724 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
8725 if (err)
8726 goto done;
8728 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8729 if (err)
8730 goto done;
8732 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8733 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8734 if (err)
8735 goto done;
8737 err = got_opentemp_named(path_unstaged_content, &outfile,
8738 "got-unstaged-content", "");
8739 if (err)
8740 goto done;
8741 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8742 "got-new-staged-content", "");
8743 if (err)
8744 goto done;
8746 if (fseek(f1, 0L, SEEK_SET) == -1) {
8747 err = got_ferror(f1, GOT_ERR_IO);
8748 goto done;
8750 if (fseek(f2, 0L, SEEK_SET) == -1) {
8751 err = got_ferror(f2, GOT_ERR_IO);
8752 goto done;
8754 /* Count the number of actual changes in the diff result. */
8755 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8756 struct diff_chunk_context cc = {};
8757 diff_chunk_context_load_change(&cc, &nchunks_used,
8758 diffreg_result->result, n, 0);
8759 nchanges++;
8761 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8762 int choice;
8763 err = apply_or_reject_change(&choice, &nchunks_used,
8764 diffreg_result->result, n, relpath, f1, f2,
8765 &line_cur1, &line_cur2,
8766 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8767 if (err)
8768 goto done;
8769 if (choice == GOT_PATCH_CHOICE_YES)
8770 have_content = 1;
8771 else
8772 have_rejected_content = 1;
8773 if (choice == GOT_PATCH_CHOICE_QUIT)
8774 break;
8776 if (have_content || have_rejected_content)
8777 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8778 outfile, rejectfile);
8779 done:
8780 free(label1);
8781 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8782 err = got_error_from_errno("close");
8783 if (blob)
8784 got_object_blob_close(blob);
8785 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8786 err = got_error_from_errno("close");
8787 if (staged_blob)
8788 got_object_blob_close(staged_blob);
8789 free_err = got_diffreg_result_free(diffreg_result);
8790 if (free_err && err == NULL)
8791 err = free_err;
8792 if (f1 && fclose(f1) == EOF && err == NULL)
8793 err = got_error_from_errno2("fclose", path1);
8794 if (f2 && fclose(f2) == EOF && err == NULL)
8795 err = got_error_from_errno2("fclose", path2);
8796 if (outfile && fclose(outfile) == EOF && err == NULL)
8797 err = got_error_from_errno2("fclose", *path_unstaged_content);
8798 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8799 err = got_error_from_errno2("fclose", *path_new_staged_content);
8800 if (path1 && unlink(path1) == -1 && err == NULL)
8801 err = got_error_from_errno2("unlink", path1);
8802 if (path2 && unlink(path2) == -1 && err == NULL)
8803 err = got_error_from_errno2("unlink", path2);
8804 if (err || !have_content) {
8805 if (*path_unstaged_content &&
8806 unlink(*path_unstaged_content) == -1 && err == NULL)
8807 err = got_error_from_errno2("unlink",
8808 *path_unstaged_content);
8809 free(*path_unstaged_content);
8810 *path_unstaged_content = NULL;
8812 if (err || !have_content || !have_rejected_content) {
8813 if (*path_new_staged_content &&
8814 unlink(*path_new_staged_content) == -1 && err == NULL)
8815 err = got_error_from_errno2("unlink",
8816 *path_new_staged_content);
8817 free(*path_new_staged_content);
8818 *path_new_staged_content = NULL;
8820 free(path1);
8821 free(path2);
8822 return err;
8825 static const struct got_error *
8826 unstage_hunks(struct got_object_id *staged_blob_id,
8827 struct got_blob_object *blob_base,
8828 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8829 const char *ondisk_path, const char *label_orig,
8830 struct got_worktree *worktree, struct got_repository *repo,
8831 got_worktree_patch_cb patch_cb, void *patch_arg,
8832 got_worktree_checkout_cb progress_cb, void *progress_arg)
8834 const struct got_error *err = NULL;
8835 char *path_unstaged_content = NULL;
8836 char *path_new_staged_content = NULL;
8837 char *parent = NULL, *base_path = NULL;
8838 char *blob_base_path = NULL;
8839 struct got_object_id *new_staged_blob_id = NULL;
8840 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8841 struct stat sb;
8843 err = create_unstaged_content(&path_unstaged_content,
8844 &path_new_staged_content, blob_id, staged_blob_id,
8845 ie->path, repo, patch_cb, patch_arg);
8846 if (err)
8847 return err;
8849 if (path_unstaged_content == NULL)
8850 return NULL;
8852 if (path_new_staged_content) {
8853 err = got_object_blob_create(&new_staged_blob_id,
8854 path_new_staged_content, repo);
8855 if (err)
8856 goto done;
8859 f = fopen(path_unstaged_content, "re");
8860 if (f == NULL) {
8861 err = got_error_from_errno2("fopen",
8862 path_unstaged_content);
8863 goto done;
8865 if (fstat(fileno(f), &sb) == -1) {
8866 err = got_error_from_errno2("fstat", path_unstaged_content);
8867 goto done;
8869 if (got_fileindex_entry_staged_filetype_get(ie) ==
8870 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8871 char link_target[PATH_MAX];
8872 size_t r;
8873 r = fread(link_target, 1, sizeof(link_target), f);
8874 if (r == 0 && ferror(f)) {
8875 err = got_error_from_errno("fread");
8876 goto done;
8878 if (r >= sizeof(link_target)) { /* should not happen */
8879 err = got_error(GOT_ERR_NO_SPACE);
8880 goto done;
8882 link_target[r] = '\0';
8883 err = merge_symlink(worktree, blob_base,
8884 ondisk_path, ie->path, label_orig, link_target,
8885 worktree->base_commit_id, repo, progress_cb,
8886 progress_arg);
8887 } else {
8888 int local_changes_subsumed;
8890 err = got_path_dirname(&parent, ondisk_path);
8891 if (err)
8892 return err;
8894 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8895 parent) == -1) {
8896 err = got_error_from_errno("asprintf");
8897 base_path = NULL;
8898 goto done;
8901 err = got_opentemp_named(&blob_base_path, &f_base,
8902 base_path, "");
8903 if (err)
8904 goto done;
8905 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8906 blob_base);
8907 if (err)
8908 goto done;
8911 * In order the run a 3-way merge with a symlink we copy the symlink's
8912 * target path into a temporary file and use that file with diff3.
8914 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8915 err = dump_symlink_target_path_to_file(&f_deriv2,
8916 ondisk_path);
8917 if (err)
8918 goto done;
8919 } else {
8920 int fd;
8921 fd = open(ondisk_path,
8922 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8923 if (fd == -1) {
8924 err = got_error_from_errno2("open", ondisk_path);
8925 goto done;
8927 f_deriv2 = fdopen(fd, "r");
8928 if (f_deriv2 == NULL) {
8929 err = got_error_from_errno2("fdopen", ondisk_path);
8930 close(fd);
8931 goto done;
8935 err = merge_file(&local_changes_subsumed, worktree,
8936 f_base, f, f_deriv2, ondisk_path, ie->path,
8937 got_fileindex_perms_to_st(ie),
8938 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8939 repo, progress_cb, progress_arg);
8941 if (err)
8942 goto done;
8944 if (new_staged_blob_id) {
8945 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8946 SHA1_DIGEST_LENGTH);
8947 } else {
8948 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8949 got_fileindex_entry_staged_filetype_set(ie, 0);
8951 done:
8952 free(new_staged_blob_id);
8953 if (path_unstaged_content &&
8954 unlink(path_unstaged_content) == -1 && err == NULL)
8955 err = got_error_from_errno2("unlink", path_unstaged_content);
8956 if (path_new_staged_content &&
8957 unlink(path_new_staged_content) == -1 && err == NULL)
8958 err = got_error_from_errno2("unlink", path_new_staged_content);
8959 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8960 err = got_error_from_errno2("unlink", blob_base_path);
8961 if (f_base && fclose(f_base) == EOF && err == NULL)
8962 err = got_error_from_errno2("fclose", path_unstaged_content);
8963 if (f && fclose(f) == EOF && err == NULL)
8964 err = got_error_from_errno2("fclose", path_unstaged_content);
8965 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8966 err = got_error_from_errno2("fclose", ondisk_path);
8967 free(path_unstaged_content);
8968 free(path_new_staged_content);
8969 free(blob_base_path);
8970 free(parent);
8971 free(base_path);
8972 return err;
8975 static const struct got_error *
8976 unstage_path(void *arg, unsigned char status,
8977 unsigned char staged_status, const char *relpath,
8978 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8979 struct got_object_id *commit_id, int dirfd, const char *de_name)
8981 const struct got_error *err = NULL;
8982 struct unstage_path_arg *a = arg;
8983 struct got_fileindex_entry *ie;
8984 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8985 char *ondisk_path = NULL;
8986 char *id_str = NULL, *label_orig = NULL;
8987 int local_changes_subsumed;
8988 struct stat sb;
8989 int fd1 = -1, fd2 = -1;
8991 if (staged_status != GOT_STATUS_ADD &&
8992 staged_status != GOT_STATUS_MODIFY &&
8993 staged_status != GOT_STATUS_DELETE)
8994 return NULL;
8996 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8997 if (ie == NULL)
8998 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9000 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9001 == -1)
9002 return got_error_from_errno("asprintf");
9004 err = got_object_id_str(&id_str,
9005 commit_id ? commit_id : a->worktree->base_commit_id);
9006 if (err)
9007 goto done;
9008 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9009 id_str) == -1) {
9010 err = got_error_from_errno("asprintf");
9011 goto done;
9014 fd1 = got_opentempfd();
9015 if (fd1 == -1) {
9016 err = got_error_from_errno("got_opentempfd");
9017 goto done;
9019 fd2 = got_opentempfd();
9020 if (fd2 == -1) {
9021 err = got_error_from_errno("got_opentempfd");
9022 goto done;
9025 switch (staged_status) {
9026 case GOT_STATUS_MODIFY:
9027 err = got_object_open_as_blob(&blob_base, a->repo,
9028 blob_id, 8192, fd1);
9029 if (err)
9030 break;
9031 /* fall through */
9032 case GOT_STATUS_ADD:
9033 if (a->patch_cb) {
9034 if (staged_status == GOT_STATUS_ADD) {
9035 int choice = GOT_PATCH_CHOICE_NONE;
9036 err = (*a->patch_cb)(&choice, a->patch_arg,
9037 staged_status, ie->path, NULL, 1, 1);
9038 if (err)
9039 break;
9040 if (choice != GOT_PATCH_CHOICE_YES)
9041 break;
9042 } else {
9043 err = unstage_hunks(staged_blob_id,
9044 blob_base, blob_id, ie, ondisk_path,
9045 label_orig, a->worktree, a->repo,
9046 a->patch_cb, a->patch_arg,
9047 a->progress_cb, a->progress_arg);
9048 break; /* Done with this file. */
9051 err = got_object_open_as_blob(&blob_staged, a->repo,
9052 staged_blob_id, 8192, fd2);
9053 if (err)
9054 break;
9055 switch (got_fileindex_entry_staged_filetype_get(ie)) {
9056 case GOT_FILEIDX_MODE_BAD_SYMLINK:
9057 case GOT_FILEIDX_MODE_REGULAR_FILE:
9058 err = merge_blob(&local_changes_subsumed, a->worktree,
9059 blob_base, ondisk_path, relpath,
9060 got_fileindex_perms_to_st(ie), label_orig,
9061 blob_staged, commit_id ? commit_id :
9062 a->worktree->base_commit_id, a->repo,
9063 a->progress_cb, a->progress_arg);
9064 break;
9065 case GOT_FILEIDX_MODE_SYMLINK:
9066 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9067 char *staged_target;
9068 err = got_object_blob_read_to_str(
9069 &staged_target, blob_staged);
9070 if (err)
9071 goto done;
9072 err = merge_symlink(a->worktree, blob_base,
9073 ondisk_path, relpath, label_orig,
9074 staged_target, commit_id ? commit_id :
9075 a->worktree->base_commit_id,
9076 a->repo, a->progress_cb, a->progress_arg);
9077 free(staged_target);
9078 } else {
9079 err = merge_blob(&local_changes_subsumed,
9080 a->worktree, blob_base, ondisk_path,
9081 relpath, got_fileindex_perms_to_st(ie),
9082 label_orig, blob_staged,
9083 commit_id ? commit_id :
9084 a->worktree->base_commit_id, a->repo,
9085 a->progress_cb, a->progress_arg);
9087 break;
9088 default:
9089 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9090 break;
9092 if (err == NULL) {
9093 got_fileindex_entry_stage_set(ie,
9094 GOT_FILEIDX_STAGE_NONE);
9095 got_fileindex_entry_staged_filetype_set(ie, 0);
9097 break;
9098 case GOT_STATUS_DELETE:
9099 if (a->patch_cb) {
9100 int choice = GOT_PATCH_CHOICE_NONE;
9101 err = (*a->patch_cb)(&choice, a->patch_arg,
9102 staged_status, ie->path, NULL, 1, 1);
9103 if (err)
9104 break;
9105 if (choice == GOT_PATCH_CHOICE_NO)
9106 break;
9107 if (choice != GOT_PATCH_CHOICE_YES) {
9108 err = got_error(GOT_ERR_PATCH_CHOICE);
9109 break;
9112 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9113 got_fileindex_entry_staged_filetype_set(ie, 0);
9114 err = get_file_status(&status, &sb, ie, ondisk_path,
9115 dirfd, de_name, a->repo);
9116 if (err)
9117 break;
9118 err = (*a->progress_cb)(a->progress_arg, status, relpath);
9119 break;
9121 done:
9122 free(ondisk_path);
9123 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9124 err = got_error_from_errno("close");
9125 if (blob_base)
9126 got_object_blob_close(blob_base);
9127 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9128 err = got_error_from_errno("close");
9129 if (blob_staged)
9130 got_object_blob_close(blob_staged);
9131 free(id_str);
9132 free(label_orig);
9133 return err;
9136 const struct got_error *
9137 got_worktree_unstage(struct got_worktree *worktree,
9138 struct got_pathlist_head *paths,
9139 got_worktree_checkout_cb progress_cb, void *progress_arg,
9140 got_worktree_patch_cb patch_cb, void *patch_arg,
9141 struct got_repository *repo)
9143 const struct got_error *err = NULL, *sync_err, *unlockerr;
9144 struct got_pathlist_entry *pe;
9145 struct got_fileindex *fileindex = NULL;
9146 char *fileindex_path = NULL;
9147 struct unstage_path_arg upa;
9149 err = lock_worktree(worktree, LOCK_EX);
9150 if (err)
9151 return err;
9153 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9154 if (err)
9155 goto done;
9157 upa.worktree = worktree;
9158 upa.fileindex = fileindex;
9159 upa.repo = repo;
9160 upa.progress_cb = progress_cb;
9161 upa.progress_arg = progress_arg;
9162 upa.patch_cb = patch_cb;
9163 upa.patch_arg = patch_arg;
9164 TAILQ_FOREACH(pe, paths, entry) {
9165 err = worktree_status(worktree, pe->path, fileindex, repo,
9166 unstage_path, &upa, NULL, NULL, 1, 0);
9167 if (err)
9168 goto done;
9171 sync_err = sync_fileindex(fileindex, fileindex_path);
9172 if (sync_err && err == NULL)
9173 err = sync_err;
9174 done:
9175 free(fileindex_path);
9176 if (fileindex)
9177 got_fileindex_free(fileindex);
9178 unlockerr = lock_worktree(worktree, LOCK_SH);
9179 if (unlockerr && err == NULL)
9180 err = unlockerr;
9181 return err;
9184 struct report_file_info_arg {
9185 struct got_worktree *worktree;
9186 got_worktree_path_info_cb info_cb;
9187 void *info_arg;
9188 struct got_pathlist_head *paths;
9189 got_cancel_cb cancel_cb;
9190 void *cancel_arg;
9193 static const struct got_error *
9194 report_file_info(void *arg, struct got_fileindex_entry *ie)
9196 struct report_file_info_arg *a = arg;
9197 struct got_pathlist_entry *pe;
9198 struct got_object_id blob_id, staged_blob_id, commit_id;
9199 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9200 struct got_object_id *commit_idp = NULL;
9201 int stage;
9203 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9204 return got_error(GOT_ERR_CANCELLED);
9206 TAILQ_FOREACH(pe, a->paths, entry) {
9207 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9208 got_path_is_child(ie->path, pe->path, pe->path_len))
9209 break;
9211 if (pe == NULL) /* not found */
9212 return NULL;
9214 if (got_fileindex_entry_has_blob(ie))
9215 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9216 stage = got_fileindex_entry_stage_get(ie);
9217 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9218 stage == GOT_FILEIDX_STAGE_ADD) {
9219 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9220 &staged_blob_id, ie);
9223 if (got_fileindex_entry_has_commit(ie))
9224 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9226 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9227 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9230 const struct got_error *
9231 got_worktree_path_info(struct got_worktree *worktree,
9232 struct got_pathlist_head *paths,
9233 got_worktree_path_info_cb info_cb, void *info_arg,
9234 got_cancel_cb cancel_cb, void *cancel_arg)
9237 const struct got_error *err = NULL, *unlockerr;
9238 struct got_fileindex *fileindex = NULL;
9239 char *fileindex_path = NULL;
9240 struct report_file_info_arg arg;
9242 err = lock_worktree(worktree, LOCK_SH);
9243 if (err)
9244 return err;
9246 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9247 if (err)
9248 goto done;
9250 arg.worktree = worktree;
9251 arg.info_cb = info_cb;
9252 arg.info_arg = info_arg;
9253 arg.paths = paths;
9254 arg.cancel_cb = cancel_cb;
9255 arg.cancel_arg = cancel_arg;
9256 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9257 &arg);
9258 done:
9259 free(fileindex_path);
9260 if (fileindex)
9261 got_fileindex_free(fileindex);
9262 unlockerr = lock_worktree(worktree, LOCK_UN);
9263 if (unlockerr && err == NULL)
9264 err = unlockerr;
9265 return err;
9268 static const struct got_error *
9269 patch_check_path(const char *p, char **path, unsigned char *status,
9270 unsigned char *staged_status, struct got_fileindex *fileindex,
9271 struct got_worktree *worktree, struct got_repository *repo)
9273 const struct got_error *err;
9274 struct got_fileindex_entry *ie;
9275 struct stat sb;
9276 char *ondisk_path = NULL;
9278 err = got_worktree_resolve_path(path, worktree, p);
9279 if (err)
9280 return err;
9282 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9283 *path[0] ? "/" : "", *path) == -1)
9284 return got_error_from_errno("asprintf");
9286 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9287 if (ie) {
9288 *staged_status = get_staged_status(ie);
9289 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9290 repo);
9291 if (err)
9292 goto done;
9293 } else {
9294 *staged_status = GOT_STATUS_NO_CHANGE;
9295 *status = GOT_STATUS_UNVERSIONED;
9296 if (lstat(ondisk_path, &sb) == -1) {
9297 if (errno != ENOENT) {
9298 err = got_error_from_errno2("lstat",
9299 ondisk_path);
9300 goto done;
9302 *status = GOT_STATUS_NONEXISTENT;
9306 done:
9307 free(ondisk_path);
9308 return err;
9311 static const struct got_error *
9312 patch_can_rm(const char *path, unsigned char status,
9313 unsigned char staged_status)
9315 if (status == GOT_STATUS_NONEXISTENT)
9316 return got_error_set_errno(ENOENT, path);
9317 if (status != GOT_STATUS_NO_CHANGE &&
9318 status != GOT_STATUS_ADD &&
9319 status != GOT_STATUS_MODIFY &&
9320 status != GOT_STATUS_MODE_CHANGE)
9321 return got_error_path(path, GOT_ERR_FILE_STATUS);
9322 if (staged_status == GOT_STATUS_DELETE)
9323 return got_error_path(path, GOT_ERR_FILE_STATUS);
9324 return NULL;
9327 static const struct got_error *
9328 patch_can_add(const char *path, unsigned char status)
9330 if (status != GOT_STATUS_NONEXISTENT)
9331 return got_error_path(path, GOT_ERR_FILE_STATUS);
9332 return NULL;
9335 static const struct got_error *
9336 patch_can_edit(const char *path, unsigned char status,
9337 unsigned char staged_status)
9339 if (status == GOT_STATUS_NONEXISTENT)
9340 return got_error_set_errno(ENOENT, path);
9341 if (status != GOT_STATUS_NO_CHANGE &&
9342 status != GOT_STATUS_ADD &&
9343 status != GOT_STATUS_MODIFY)
9344 return got_error_path(path, GOT_ERR_FILE_STATUS);
9345 if (staged_status == GOT_STATUS_DELETE)
9346 return got_error_path(path, GOT_ERR_FILE_STATUS);
9347 return NULL;
9350 const struct got_error *
9351 got_worktree_patch_prepare(struct got_fileindex **fileindex,
9352 char **fileindex_path, struct got_worktree *worktree)
9354 return open_fileindex(fileindex, fileindex_path, worktree);
9357 const struct got_error *
9358 got_worktree_patch_check_path(const char *old, const char *new,
9359 char **oldpath, char **newpath, struct got_worktree *worktree,
9360 struct got_repository *repo, struct got_fileindex *fileindex)
9362 const struct got_error *err = NULL;
9363 int file_renamed = 0;
9364 unsigned char status_old, staged_status_old;
9365 unsigned char status_new, staged_status_new;
9367 *oldpath = NULL;
9368 *newpath = NULL;
9370 err = patch_check_path(old != NULL ? old : new, oldpath,
9371 &status_old, &staged_status_old, fileindex, worktree, repo);
9372 if (err)
9373 goto done;
9375 err = patch_check_path(new != NULL ? new : old, newpath,
9376 &status_new, &staged_status_new, fileindex, worktree, repo);
9377 if (err)
9378 goto done;
9380 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9381 file_renamed = 1;
9383 if (old != NULL && new == NULL)
9384 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9385 else if (file_renamed) {
9386 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9387 if (err == NULL)
9388 err = patch_can_add(*newpath, status_new);
9389 } else if (old == NULL)
9390 err = patch_can_add(*newpath, status_new);
9391 else
9392 err = patch_can_edit(*newpath, status_new, staged_status_new);
9394 done:
9395 if (err) {
9396 free(*oldpath);
9397 *oldpath = NULL;
9398 free(*newpath);
9399 *newpath = NULL;
9401 return err;
9404 const struct got_error *
9405 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9406 struct got_worktree *worktree, struct got_fileindex *fileindex,
9407 got_worktree_checkout_cb progress_cb, void *progress_arg)
9409 struct schedule_addition_args saa;
9411 memset(&saa, 0, sizeof(saa));
9412 saa.worktree = worktree;
9413 saa.fileindex = fileindex;
9414 saa.progress_cb = progress_cb;
9415 saa.progress_arg = progress_arg;
9416 saa.repo = repo;
9418 return worktree_status(worktree, path, fileindex, repo,
9419 schedule_addition, &saa, NULL, NULL, 1, 0);
9422 const struct got_error *
9423 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9424 struct got_worktree *worktree, struct got_fileindex *fileindex,
9425 got_worktree_delete_cb progress_cb, void *progress_arg)
9427 struct schedule_deletion_args sda;
9429 memset(&sda, 0, sizeof(sda));
9430 sda.worktree = worktree;
9431 sda.fileindex = fileindex;
9432 sda.progress_cb = progress_cb;
9433 sda.progress_arg = progress_arg;
9434 sda.repo = repo;
9435 sda.delete_local_mods = 0;
9436 sda.keep_on_disk = 0;
9437 sda.ignore_missing_paths = 0;
9438 sda.status_codes = NULL;
9440 return worktree_status(worktree, path, fileindex, repo,
9441 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9444 const struct got_error *
9445 got_worktree_patch_complete(struct got_fileindex *fileindex,
9446 const char *fileindex_path)
9448 const struct got_error *err = NULL;
9450 err = sync_fileindex(fileindex, fileindex_path);
9451 got_fileindex_free(fileindex);
9453 return err;