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->hash, base_commit_id->hash, 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;
1517 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1518 static const struct got_error *
1519 get_modified_file_content_status(unsigned char *status, FILE *f)
1521 const struct got_error *err = NULL;
1522 const char *markers[3] = {
1523 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1524 GOT_DIFF_CONFLICT_MARKER_SEP,
1525 GOT_DIFF_CONFLICT_MARKER_END
1527 int i = 0;
1528 char *line = NULL;
1529 size_t linesize = 0;
1530 ssize_t linelen;
1532 while (*status == GOT_STATUS_MODIFY) {
1533 linelen = getline(&line, &linesize, f);
1534 if (linelen == -1) {
1535 if (feof(f))
1536 break;
1537 err = got_ferror(f, GOT_ERR_IO);
1538 break;
1541 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1542 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1543 == 0)
1544 *status = GOT_STATUS_CONFLICT;
1545 else
1546 i++;
1549 free(line);
1551 return err;
1554 static int
1555 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1557 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1558 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1561 static int
1562 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1564 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1565 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1566 ie->mtime_sec == sb->st_mtim.tv_sec &&
1567 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1568 ie->size == (sb->st_size & 0xffffffff) &&
1569 !xbit_differs(ie, sb->st_mode));
1572 static unsigned char
1573 get_staged_status(struct got_fileindex_entry *ie)
1575 switch (got_fileindex_entry_stage_get(ie)) {
1576 case GOT_FILEIDX_STAGE_ADD:
1577 return GOT_STATUS_ADD;
1578 case GOT_FILEIDX_STAGE_DELETE:
1579 return GOT_STATUS_DELETE;
1580 case GOT_FILEIDX_STAGE_MODIFY:
1581 return GOT_STATUS_MODIFY;
1582 default:
1583 return GOT_STATUS_NO_CHANGE;
1587 static const struct got_error *
1588 get_symlink_modification_status(unsigned char *status,
1589 struct got_fileindex_entry *ie, const char *abspath,
1590 int dirfd, const char *de_name, struct got_blob_object *blob)
1592 const struct got_error *err = NULL;
1593 char target_path[PATH_MAX];
1594 char etarget[PATH_MAX];
1595 ssize_t elen;
1596 size_t len, target_len = 0;
1597 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1598 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1600 *status = GOT_STATUS_NO_CHANGE;
1602 /* Blob object content specifies the target path of the link. */
1603 do {
1604 err = got_object_blob_read_block(&len, blob);
1605 if (err)
1606 return err;
1607 if (len + target_len >= sizeof(target_path)) {
1609 * Should not happen. The blob contents were OK
1610 * when this symlink was installed.
1612 return got_error(GOT_ERR_NO_SPACE);
1614 if (len > 0) {
1615 /* Skip blob object header first time around. */
1616 memcpy(target_path + target_len, buf + hdrlen,
1617 len - hdrlen);
1618 target_len += len - hdrlen;
1619 hdrlen = 0;
1621 } while (len != 0);
1622 target_path[target_len] = '\0';
1624 if (dirfd != -1) {
1625 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1626 if (elen == -1)
1627 return got_error_from_errno2("readlinkat", abspath);
1628 } else {
1629 elen = readlink(abspath, etarget, sizeof(etarget));
1630 if (elen == -1)
1631 return got_error_from_errno2("readlink", abspath);
1634 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1635 *status = GOT_STATUS_MODIFY;
1637 return NULL;
1640 static const struct got_error *
1641 get_file_status(unsigned char *status, struct stat *sb,
1642 struct got_fileindex_entry *ie, const char *abspath,
1643 int dirfd, const char *de_name, struct got_repository *repo)
1645 const struct got_error *err = NULL;
1646 struct got_object_id id;
1647 size_t hdrlen;
1648 int fd = -1, fd1 = -1;
1649 FILE *f = NULL;
1650 uint8_t fbuf[8192];
1651 struct got_blob_object *blob = NULL;
1652 size_t flen, blen;
1653 unsigned char staged_status = get_staged_status(ie);
1655 *status = GOT_STATUS_NO_CHANGE;
1656 memset(sb, 0, sizeof(*sb));
1659 * Whenever the caller provides a directory descriptor and a
1660 * directory entry name for the file, use them! This prevents
1661 * race conditions if filesystem paths change beneath our feet.
1663 if (dirfd != -1) {
1664 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1665 if (errno == ENOENT) {
1666 if (got_fileindex_entry_has_file_on_disk(ie))
1667 *status = GOT_STATUS_MISSING;
1668 else
1669 *status = GOT_STATUS_DELETE;
1670 goto done;
1672 err = got_error_from_errno2("fstatat", abspath);
1673 goto done;
1675 } else {
1676 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1677 if (fd == -1 && errno != ENOENT &&
1678 !got_err_open_nofollow_on_symlink())
1679 return got_error_from_errno2("open", abspath);
1680 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1681 if (lstat(abspath, sb) == -1)
1682 return got_error_from_errno2("lstat", abspath);
1683 } else if (fd == -1 || fstat(fd, sb) == -1) {
1684 if (errno == ENOENT) {
1685 if (got_fileindex_entry_has_file_on_disk(ie))
1686 *status = GOT_STATUS_MISSING;
1687 else
1688 *status = GOT_STATUS_DELETE;
1689 goto done;
1691 err = got_error_from_errno2("fstat", abspath);
1692 goto done;
1696 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1697 *status = GOT_STATUS_OBSTRUCTED;
1698 goto done;
1701 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1702 *status = GOT_STATUS_DELETE;
1703 goto done;
1704 } else if (!got_fileindex_entry_has_blob(ie) &&
1705 staged_status != GOT_STATUS_ADD) {
1706 *status = GOT_STATUS_ADD;
1707 goto done;
1710 if (!stat_info_differs(ie, sb))
1711 goto done;
1713 if (S_ISLNK(sb->st_mode) &&
1714 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1715 *status = GOT_STATUS_MODIFY;
1716 goto done;
1719 if (staged_status == GOT_STATUS_MODIFY ||
1720 staged_status == GOT_STATUS_ADD)
1721 got_fileindex_entry_get_staged_blob_id(&id, ie);
1722 else
1723 got_fileindex_entry_get_blob_id(&id, ie);
1725 fd1 = got_opentempfd();
1726 if (fd1 == -1) {
1727 err = got_error_from_errno("got_opentempfd");
1728 goto done;
1730 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1731 if (err)
1732 goto done;
1734 if (S_ISLNK(sb->st_mode)) {
1735 err = get_symlink_modification_status(status, ie,
1736 abspath, dirfd, de_name, blob);
1737 goto done;
1740 if (dirfd != -1) {
1741 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1742 if (fd == -1) {
1743 err = got_error_from_errno2("openat", abspath);
1744 goto done;
1748 f = fdopen(fd, "r");
1749 if (f == NULL) {
1750 err = got_error_from_errno2("fdopen", abspath);
1751 goto done;
1753 fd = -1;
1754 hdrlen = got_object_blob_get_hdrlen(blob);
1755 for (;;) {
1756 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1757 err = got_object_blob_read_block(&blen, blob);
1758 if (err)
1759 goto done;
1760 /* Skip length of blob object header first time around. */
1761 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1762 if (flen == 0 && ferror(f)) {
1763 err = got_error_from_errno("fread");
1764 goto done;
1766 if (blen - hdrlen == 0) {
1767 if (flen != 0)
1768 *status = GOT_STATUS_MODIFY;
1769 break;
1770 } else if (flen == 0) {
1771 if (blen - hdrlen != 0)
1772 *status = GOT_STATUS_MODIFY;
1773 break;
1774 } else if (blen - hdrlen == flen) {
1775 /* Skip blob object header first time around. */
1776 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1777 *status = GOT_STATUS_MODIFY;
1778 break;
1780 } else {
1781 *status = GOT_STATUS_MODIFY;
1782 break;
1784 hdrlen = 0;
1787 if (*status == GOT_STATUS_MODIFY) {
1788 rewind(f);
1789 err = get_modified_file_content_status(status, f);
1790 } else if (xbit_differs(ie, sb->st_mode))
1791 *status = GOT_STATUS_MODE_CHANGE;
1792 done:
1793 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1794 err = got_error_from_errno("close");
1795 if (blob)
1796 got_object_blob_close(blob);
1797 if (f != NULL && fclose(f) == EOF && err == NULL)
1798 err = got_error_from_errno2("fclose", abspath);
1799 if (fd != -1 && close(fd) == -1 && err == NULL)
1800 err = got_error_from_errno2("close", abspath);
1801 return err;
1805 * Update timestamps in the file index if a file is unmodified and
1806 * we had to run a full content comparison to find out.
1808 static const struct got_error *
1809 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1810 struct got_fileindex_entry *ie, struct stat *sb)
1812 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1813 return got_fileindex_entry_update(ie, wt_fd, path,
1814 ie->blob_sha1, ie->commit_sha1, 1);
1816 return NULL;
1819 static const struct got_error *
1820 update_blob(struct got_worktree *worktree,
1821 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1822 struct got_tree_entry *te, const char *path,
1823 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1824 void *progress_arg)
1826 const struct got_error *err = NULL;
1827 struct got_blob_object *blob = NULL;
1828 char *ondisk_path = NULL;
1829 unsigned char status = GOT_STATUS_NO_CHANGE;
1830 struct stat sb;
1831 int fd1 = -1, fd2 = -1;
1833 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1834 return got_error_from_errno("asprintf");
1836 if (ie) {
1837 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1838 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1839 goto done;
1841 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1842 repo);
1843 if (err)
1844 goto done;
1845 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1846 sb.st_mode = got_fileindex_perms_to_st(ie);
1847 } else {
1848 if (stat(ondisk_path, &sb) == -1) {
1849 if (errno != ENOENT) {
1850 err = got_error_from_errno2("stat",
1851 ondisk_path);
1852 goto done;
1854 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1855 status = GOT_STATUS_UNVERSIONED;
1856 } else {
1857 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1858 status = GOT_STATUS_UNVERSIONED;
1859 else
1860 status = GOT_STATUS_OBSTRUCTED;
1864 if (status == GOT_STATUS_OBSTRUCTED) {
1865 if (ie)
1866 got_fileindex_entry_mark_skipped(ie);
1867 err = (*progress_cb)(progress_arg, status, path);
1868 goto done;
1870 if (status == GOT_STATUS_CONFLICT) {
1871 if (ie)
1872 got_fileindex_entry_mark_skipped(ie);
1873 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1874 path);
1875 goto done;
1878 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1879 (S_ISLNK(te->mode) ||
1880 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1882 * This is a regular file or an installed bad symlink.
1883 * If the file index indicates that this file is already
1884 * up-to-date with respect to the repository we can skip
1885 * updating contents of this file.
1887 if (got_fileindex_entry_has_commit(ie) &&
1888 memcmp(ie->commit_sha1, worktree->base_commit_id->hash,
1889 SHA1_DIGEST_LENGTH) == 0) {
1890 /* Same commit. */
1891 err = sync_timestamps(worktree->root_fd,
1892 path, status, ie, &sb);
1893 if (err)
1894 goto done;
1895 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1896 path);
1897 goto done;
1899 if (got_fileindex_entry_has_blob(ie) &&
1900 memcmp(ie->blob_sha1, te->id.hash,
1901 SHA1_DIGEST_LENGTH) == 0) {
1902 /* Different commit but the same blob. */
1903 err = sync_timestamps(worktree->root_fd,
1904 path, status, ie, &sb);
1905 if (err)
1906 goto done;
1907 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1908 path);
1909 goto done;
1913 fd1 = got_opentempfd();
1914 if (fd1 == -1) {
1915 err = got_error_from_errno("got_opentempfd");
1916 goto done;
1918 err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
1919 if (err)
1920 goto done;
1922 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1923 int update_timestamps;
1924 struct got_blob_object *blob2 = NULL;
1925 char *label_orig = NULL;
1926 if (got_fileindex_entry_has_blob(ie)) {
1927 fd2 = got_opentempfd();
1928 if (fd2 == -1) {
1929 err = got_error_from_errno("got_opentempfd");
1930 goto done;
1932 struct got_object_id id2;
1933 got_fileindex_entry_get_blob_id(&id2, ie);
1934 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
1935 fd2);
1936 if (err)
1937 goto done;
1939 if (got_fileindex_entry_has_commit(ie)) {
1940 char id_str[SHA1_DIGEST_STRING_LENGTH];
1941 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1942 sizeof(id_str)) == NULL) {
1943 err = got_error_path(id_str,
1944 GOT_ERR_BAD_OBJ_ID_STR);
1945 goto done;
1947 if (asprintf(&label_orig, "%s: commit %s",
1948 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1949 err = got_error_from_errno("asprintf");
1950 goto done;
1953 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1954 char *link_target;
1955 err = got_object_blob_read_to_str(&link_target, blob);
1956 if (err)
1957 goto done;
1958 err = merge_symlink(worktree, blob2, ondisk_path, path,
1959 label_orig, link_target, worktree->base_commit_id,
1960 repo, progress_cb, progress_arg);
1961 free(link_target);
1962 } else {
1963 err = merge_blob(&update_timestamps, worktree, blob2,
1964 ondisk_path, path, sb.st_mode, label_orig, blob,
1965 worktree->base_commit_id, repo,
1966 progress_cb, progress_arg);
1968 free(label_orig);
1969 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
1970 err = got_error_from_errno("close");
1971 goto done;
1973 if (blob2)
1974 got_object_blob_close(blob2);
1975 if (err)
1976 goto done;
1978 * Do not update timestamps of files with local changes.
1979 * Otherwise, a future status walk would treat them as
1980 * unmodified files again.
1982 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1983 blob->id.hash, worktree->base_commit_id->hash,
1984 update_timestamps);
1985 } else if (status == GOT_STATUS_MODE_CHANGE) {
1986 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1987 blob->id.hash, worktree->base_commit_id->hash, 0);
1988 } else if (status == GOT_STATUS_DELETE) {
1989 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1990 if (err)
1991 goto done;
1992 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1993 blob->id.hash, worktree->base_commit_id->hash, 0);
1994 if (err)
1995 goto done;
1996 } else {
1997 int is_bad_symlink = 0;
1998 if (S_ISLNK(te->mode)) {
1999 err = install_symlink(&is_bad_symlink, worktree,
2000 ondisk_path, path, blob,
2001 status == GOT_STATUS_MISSING, 0,
2002 status == GOT_STATUS_UNVERSIONED, 0,
2003 repo, progress_cb, progress_arg);
2004 } else {
2005 err = install_blob(worktree, ondisk_path, path,
2006 te->mode, sb.st_mode, blob,
2007 status == GOT_STATUS_MISSING, 0, 0,
2008 status == GOT_STATUS_UNVERSIONED, repo,
2009 progress_cb, progress_arg);
2011 if (err)
2012 goto done;
2014 if (ie) {
2015 err = got_fileindex_entry_update(ie,
2016 worktree->root_fd, path, blob->id.hash,
2017 worktree->base_commit_id->hash, 1);
2018 } else {
2019 err = create_fileindex_entry(&ie, fileindex,
2020 worktree->base_commit_id, worktree->root_fd, path,
2021 &blob->id);
2023 if (err)
2024 goto done;
2026 if (is_bad_symlink) {
2027 got_fileindex_entry_filetype_set(ie,
2028 GOT_FILEIDX_MODE_BAD_SYMLINK);
2032 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2033 err = got_error_from_errno("close");
2034 goto done;
2036 got_object_blob_close(blob);
2037 done:
2038 free(ondisk_path);
2039 return err;
2042 static const struct got_error *
2043 remove_ondisk_file(const char *root_path, const char *path)
2045 const struct got_error *err = NULL;
2046 char *ondisk_path = NULL, *parent = NULL;
2048 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2049 return got_error_from_errno("asprintf");
2051 if (unlink(ondisk_path) == -1) {
2052 if (errno != ENOENT)
2053 err = got_error_from_errno2("unlink", ondisk_path);
2054 } else {
2055 size_t root_len = strlen(root_path);
2056 err = got_path_dirname(&parent, ondisk_path);
2057 if (err)
2058 goto done;
2059 while (got_path_cmp(parent, root_path,
2060 strlen(parent), root_len) != 0) {
2061 free(ondisk_path);
2062 ondisk_path = parent;
2063 parent = NULL;
2064 if (rmdir(ondisk_path) == -1) {
2065 if (errno != ENOTEMPTY)
2066 err = got_error_from_errno2("rmdir",
2067 ondisk_path);
2068 break;
2070 err = got_path_dirname(&parent, ondisk_path);
2071 if (err)
2072 break;
2075 done:
2076 free(ondisk_path);
2077 free(parent);
2078 return err;
2081 static const struct got_error *
2082 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2083 struct got_fileindex_entry *ie, struct got_repository *repo,
2084 got_worktree_checkout_cb progress_cb, void *progress_arg)
2086 const struct got_error *err = NULL;
2087 unsigned char status;
2088 struct stat sb;
2089 char *ondisk_path;
2091 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2092 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2094 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2095 == -1)
2096 return got_error_from_errno("asprintf");
2098 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2099 if (err)
2100 goto done;
2102 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2103 char ondisk_target[PATH_MAX];
2104 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2105 sizeof(ondisk_target));
2106 if (ondisk_len == -1) {
2107 err = got_error_from_errno2("readlink", ondisk_path);
2108 goto done;
2110 ondisk_target[ondisk_len] = '\0';
2111 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2112 NULL, NULL, /* XXX pass common ancestor info? */
2113 ondisk_target, ondisk_path);
2114 if (err)
2115 goto done;
2116 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2117 ie->path);
2118 goto done;
2121 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2122 status == GOT_STATUS_ADD) {
2123 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2124 if (err)
2125 goto done;
2127 * Preserve the working file and change the deleted blob's
2128 * entry into a schedule-add entry.
2130 err = got_fileindex_entry_update(ie, worktree->root_fd,
2131 ie->path, NULL, NULL, 0);
2132 } else {
2133 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2134 if (err)
2135 goto done;
2136 if (status == GOT_STATUS_NO_CHANGE) {
2137 err = remove_ondisk_file(worktree->root_path, ie->path);
2138 if (err)
2139 goto done;
2141 got_fileindex_entry_remove(fileindex, ie);
2143 done:
2144 free(ondisk_path);
2145 return err;
2148 struct diff_cb_arg {
2149 struct got_fileindex *fileindex;
2150 struct got_worktree *worktree;
2151 struct got_repository *repo;
2152 got_worktree_checkout_cb progress_cb;
2153 void *progress_arg;
2154 got_cancel_cb cancel_cb;
2155 void *cancel_arg;
2158 static const struct got_error *
2159 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2160 struct got_tree_entry *te, const char *parent_path)
2162 struct diff_cb_arg *a = arg;
2164 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2165 return got_error(GOT_ERR_CANCELLED);
2167 return update_blob(a->worktree, a->fileindex, ie, te,
2168 ie->path, a->repo, a->progress_cb, a->progress_arg);
2171 static const struct got_error *
2172 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2174 struct diff_cb_arg *a = arg;
2176 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2177 return got_error(GOT_ERR_CANCELLED);
2179 return delete_blob(a->worktree, a->fileindex, ie,
2180 a->repo, a->progress_cb, a->progress_arg);
2183 static const struct got_error *
2184 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2186 struct diff_cb_arg *a = arg;
2187 const struct got_error *err;
2188 char *path;
2190 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2191 return got_error(GOT_ERR_CANCELLED);
2193 if (got_object_tree_entry_is_submodule(te))
2194 return NULL;
2196 if (asprintf(&path, "%s%s%s", parent_path,
2197 parent_path[0] ? "/" : "", te->name)
2198 == -1)
2199 return got_error_from_errno("asprintf");
2201 if (S_ISDIR(te->mode))
2202 err = add_dir_on_disk(a->worktree, path);
2203 else
2204 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2205 a->repo, a->progress_cb, a->progress_arg);
2207 free(path);
2208 return err;
2211 const struct got_error *
2212 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2214 uint32_t uuid_status;
2216 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2217 if (uuid_status != uuid_s_ok) {
2218 *uuidstr = NULL;
2219 return got_error_uuid(uuid_status, "uuid_to_string");
2222 return NULL;
2225 static const struct got_error *
2226 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2228 const struct got_error *err = NULL;
2229 char *uuidstr = NULL;
2231 *refname = NULL;
2233 err = got_worktree_get_uuid(&uuidstr, worktree);
2234 if (err)
2235 return err;
2237 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2238 err = got_error_from_errno("asprintf");
2239 *refname = NULL;
2241 free(uuidstr);
2242 return err;
2245 const struct got_error *
2246 got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2247 const char *prefix)
2249 return get_ref_name(refname, worktree, prefix);
2252 const struct got_error *
2253 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2255 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2258 static const struct got_error *
2259 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2261 return get_ref_name(refname, worktree,
2262 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2265 static const struct got_error *
2266 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2268 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2271 static const struct got_error *
2272 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2274 return get_ref_name(refname, worktree,
2275 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2278 static const struct got_error *
2279 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2281 return get_ref_name(refname, worktree,
2282 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2285 static const struct got_error *
2286 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2288 return get_ref_name(refname, worktree,
2289 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2292 static const struct got_error *
2293 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2295 return get_ref_name(refname, worktree,
2296 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2299 static const struct got_error *
2300 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2302 return get_ref_name(refname, worktree,
2303 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2306 static const struct got_error *
2307 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2309 return get_ref_name(refname, worktree,
2310 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2313 const struct got_error *
2314 got_worktree_get_histedit_script_path(char **path,
2315 struct got_worktree *worktree)
2317 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2318 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2319 *path = NULL;
2320 return got_error_from_errno("asprintf");
2322 return NULL;
2325 static const struct got_error *
2326 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2328 return get_ref_name(refname, worktree,
2329 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2332 static const struct got_error *
2333 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2335 return get_ref_name(refname, worktree,
2336 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2340 * Prevent Git's garbage collector from deleting our base commit by
2341 * setting a reference to our base commit's ID.
2343 static const struct got_error *
2344 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2346 const struct got_error *err = NULL;
2347 struct got_reference *ref = NULL;
2348 char *refname;
2350 err = got_worktree_get_base_ref_name(&refname, worktree);
2351 if (err)
2352 return err;
2354 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2355 if (err)
2356 goto done;
2358 err = got_ref_write(ref, repo);
2359 done:
2360 free(refname);
2361 if (ref)
2362 got_ref_close(ref);
2363 return err;
2366 static const struct got_error *
2367 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2369 const struct got_error *err = NULL;
2371 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2372 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2373 err = got_error_from_errno("asprintf");
2374 *fileindex_path = NULL;
2376 return err;
2380 static const struct got_error *
2381 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2382 struct got_worktree *worktree)
2384 const struct got_error *err = NULL;
2385 FILE *index = NULL;
2387 *fileindex_path = NULL;
2388 *fileindex = got_fileindex_alloc();
2389 if (*fileindex == NULL)
2390 return got_error_from_errno("got_fileindex_alloc");
2392 err = get_fileindex_path(fileindex_path, worktree);
2393 if (err)
2394 goto done;
2396 index = fopen(*fileindex_path, "rbe");
2397 if (index == NULL) {
2398 if (errno != ENOENT)
2399 err = got_error_from_errno2("fopen", *fileindex_path);
2400 } else {
2401 err = got_fileindex_read(*fileindex, index);
2402 if (fclose(index) == EOF && err == NULL)
2403 err = got_error_from_errno("fclose");
2405 done:
2406 if (err) {
2407 free(*fileindex_path);
2408 *fileindex_path = NULL;
2409 got_fileindex_free(*fileindex);
2410 *fileindex = NULL;
2412 return err;
2415 struct bump_base_commit_id_arg {
2416 struct got_object_id *base_commit_id;
2417 const char *path;
2418 size_t path_len;
2419 const char *entry_name;
2420 got_worktree_checkout_cb progress_cb;
2421 void *progress_arg;
2424 static const struct got_error *
2425 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2427 const struct got_error *err;
2428 struct bump_base_commit_id_arg *a = arg;
2430 if (a->entry_name) {
2431 if (strcmp(ie->path, a->path) != 0)
2432 return NULL;
2433 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2434 return NULL;
2436 if (got_fileindex_entry_was_skipped(ie))
2437 return NULL;
2439 if (memcmp(ie->commit_sha1, a->base_commit_id->hash,
2440 SHA1_DIGEST_LENGTH) == 0)
2441 return NULL;
2443 if (a->progress_cb) {
2444 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2445 ie->path);
2446 if (err)
2447 return err;
2449 memcpy(ie->commit_sha1, a->base_commit_id->hash, SHA1_DIGEST_LENGTH);
2450 return NULL;
2453 /* Bump base commit ID of all files within an updated part of the work tree. */
2454 static const struct got_error *
2455 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2456 struct got_fileindex *fileindex,
2457 got_worktree_checkout_cb progress_cb, void *progress_arg)
2459 struct bump_base_commit_id_arg bbc_arg;
2461 bbc_arg.base_commit_id = worktree->base_commit_id;
2462 bbc_arg.entry_name = NULL;
2463 bbc_arg.path = "";
2464 bbc_arg.path_len = 0;
2465 bbc_arg.progress_cb = progress_cb;
2466 bbc_arg.progress_arg = progress_arg;
2468 return got_fileindex_for_each_entry_safe(fileindex,
2469 bump_base_commit_id, &bbc_arg);
2472 static const struct got_error *
2473 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2475 const struct got_error *err = NULL;
2476 char *new_fileindex_path = NULL;
2477 FILE *new_index = NULL;
2478 struct timespec timeout;
2480 err = got_opentemp_named(&new_fileindex_path, &new_index,
2481 fileindex_path, "");
2482 if (err)
2483 goto done;
2485 err = got_fileindex_write(fileindex, new_index);
2486 if (err)
2487 goto done;
2489 if (rename(new_fileindex_path, fileindex_path) != 0) {
2490 err = got_error_from_errno3("rename", new_fileindex_path,
2491 fileindex_path);
2492 unlink(new_fileindex_path);
2496 * Sleep for a short amount of time to ensure that files modified after
2497 * this program exits have a different time stamp from the one which
2498 * was recorded in the file index.
2500 timeout.tv_sec = 0;
2501 timeout.tv_nsec = 1;
2502 nanosleep(&timeout, NULL);
2503 done:
2504 if (new_index)
2505 fclose(new_index);
2506 free(new_fileindex_path);
2507 return err;
2510 static const struct got_error *
2511 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2512 struct got_object_id **tree_id, const char *wt_relpath,
2513 struct got_commit_object *base_commit, struct got_worktree *worktree,
2514 struct got_repository *repo)
2516 const struct got_error *err = NULL;
2517 struct got_object_id *id = NULL;
2518 char *in_repo_path = NULL;
2519 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2521 *entry_type = GOT_OBJ_TYPE_ANY;
2522 *tree_relpath = NULL;
2523 *tree_id = NULL;
2525 if (wt_relpath[0] == '\0') {
2526 /* Check out all files within the work tree. */
2527 *entry_type = GOT_OBJ_TYPE_TREE;
2528 *tree_relpath = strdup("");
2529 if (*tree_relpath == NULL) {
2530 err = got_error_from_errno("strdup");
2531 goto done;
2533 err = got_object_id_by_path(tree_id, repo, base_commit,
2534 worktree->path_prefix);
2535 if (err)
2536 goto done;
2537 return NULL;
2540 /* Check out a subset of files in the work tree. */
2542 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2543 is_root_wt ? "" : "/", wt_relpath) == -1) {
2544 err = got_error_from_errno("asprintf");
2545 goto done;
2548 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2549 if (err)
2550 goto done;
2552 free(in_repo_path);
2553 in_repo_path = NULL;
2555 err = got_object_get_type(entry_type, repo, id);
2556 if (err)
2557 goto done;
2559 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2560 /* Check out a single file. */
2561 if (strchr(wt_relpath, '/') == NULL) {
2562 /* Check out a single file in work tree's root dir. */
2563 in_repo_path = strdup(worktree->path_prefix);
2564 if (in_repo_path == NULL) {
2565 err = got_error_from_errno("strdup");
2566 goto done;
2568 *tree_relpath = strdup("");
2569 if (*tree_relpath == NULL) {
2570 err = got_error_from_errno("strdup");
2571 goto done;
2573 } else {
2574 /* Check out a single file in a subdirectory. */
2575 err = got_path_dirname(tree_relpath, wt_relpath);
2576 if (err)
2577 return err;
2578 if (asprintf(&in_repo_path, "%s%s%s",
2579 worktree->path_prefix, is_root_wt ? "" : "/",
2580 *tree_relpath) == -1) {
2581 err = got_error_from_errno("asprintf");
2582 goto done;
2585 err = got_object_id_by_path(tree_id, repo,
2586 base_commit, in_repo_path);
2587 } else {
2588 /* Check out all files within a subdirectory. */
2589 *tree_id = got_object_id_dup(id);
2590 if (*tree_id == NULL) {
2591 err = got_error_from_errno("got_object_id_dup");
2592 goto done;
2594 *tree_relpath = strdup(wt_relpath);
2595 if (*tree_relpath == NULL) {
2596 err = got_error_from_errno("strdup");
2597 goto done;
2600 done:
2601 free(id);
2602 free(in_repo_path);
2603 if (err) {
2604 *entry_type = GOT_OBJ_TYPE_ANY;
2605 free(*tree_relpath);
2606 *tree_relpath = NULL;
2607 free(*tree_id);
2608 *tree_id = NULL;
2610 return err;
2613 static const struct got_error *
2614 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2615 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2616 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2617 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2619 const struct got_error *err = NULL;
2620 struct got_commit_object *commit = NULL;
2621 struct got_tree_object *tree = NULL;
2622 struct got_fileindex_diff_tree_cb diff_cb;
2623 struct diff_cb_arg arg;
2625 err = ref_base_commit(worktree, repo);
2626 if (err) {
2627 if (!(err->code == GOT_ERR_ERRNO &&
2628 (errno == EACCES || errno == EROFS)))
2629 goto done;
2630 err = (*progress_cb)(progress_arg,
2631 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2632 if (err)
2633 return err;
2636 err = got_object_open_as_commit(&commit, repo,
2637 worktree->base_commit_id);
2638 if (err)
2639 goto done;
2641 err = got_object_open_as_tree(&tree, repo, tree_id);
2642 if (err)
2643 goto done;
2645 if (entry_name &&
2646 got_object_tree_find_entry(tree, entry_name) == NULL) {
2647 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2648 goto done;
2651 diff_cb.diff_old_new = diff_old_new;
2652 diff_cb.diff_old = diff_old;
2653 diff_cb.diff_new = diff_new;
2654 arg.fileindex = fileindex;
2655 arg.worktree = worktree;
2656 arg.repo = repo;
2657 arg.progress_cb = progress_cb;
2658 arg.progress_arg = progress_arg;
2659 arg.cancel_cb = cancel_cb;
2660 arg.cancel_arg = cancel_arg;
2661 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2662 entry_name, repo, &diff_cb, &arg);
2663 done:
2664 if (tree)
2665 got_object_tree_close(tree);
2666 if (commit)
2667 got_object_commit_close(commit);
2668 return err;
2671 const struct got_error *
2672 got_worktree_checkout_files(struct got_worktree *worktree,
2673 struct got_pathlist_head *paths, struct got_repository *repo,
2674 got_worktree_checkout_cb progress_cb, void *progress_arg,
2675 got_cancel_cb cancel_cb, void *cancel_arg)
2677 const struct got_error *err = NULL, *sync_err, *unlockerr;
2678 struct got_commit_object *commit = NULL;
2679 struct got_tree_object *tree = NULL;
2680 struct got_fileindex *fileindex = NULL;
2681 char *fileindex_path = NULL;
2682 struct got_pathlist_entry *pe;
2683 struct tree_path_data {
2684 STAILQ_ENTRY(tree_path_data) entry;
2685 struct got_object_id *tree_id;
2686 int entry_type;
2687 char *relpath;
2688 char *entry_name;
2689 } *tpd = NULL;
2690 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2692 STAILQ_INIT(&tree_paths);
2694 err = lock_worktree(worktree, LOCK_EX);
2695 if (err)
2696 return err;
2698 err = got_object_open_as_commit(&commit, repo,
2699 worktree->base_commit_id);
2700 if (err)
2701 goto done;
2703 /* Map all specified paths to in-repository trees. */
2704 TAILQ_FOREACH(pe, paths, entry) {
2705 tpd = malloc(sizeof(*tpd));
2706 if (tpd == NULL) {
2707 err = got_error_from_errno("malloc");
2708 goto done;
2711 err = find_tree_entry_for_checkout(&tpd->entry_type,
2712 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2713 worktree, repo);
2714 if (err) {
2715 free(tpd);
2716 goto done;
2719 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2720 err = got_path_basename(&tpd->entry_name, pe->path);
2721 if (err) {
2722 free(tpd->relpath);
2723 free(tpd->tree_id);
2724 free(tpd);
2725 goto done;
2727 } else
2728 tpd->entry_name = NULL;
2730 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2734 * Read the file index.
2735 * Checking out files is supposed to be an idempotent operation.
2736 * If the on-disk file index is incomplete we will try to complete it.
2738 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2739 if (err)
2740 goto done;
2742 tpd = STAILQ_FIRST(&tree_paths);
2743 TAILQ_FOREACH(pe, paths, entry) {
2744 struct bump_base_commit_id_arg bbc_arg;
2746 err = checkout_files(worktree, fileindex, tpd->relpath,
2747 tpd->tree_id, tpd->entry_name, repo,
2748 progress_cb, progress_arg, cancel_cb, cancel_arg);
2749 if (err)
2750 break;
2752 bbc_arg.base_commit_id = worktree->base_commit_id;
2753 bbc_arg.entry_name = tpd->entry_name;
2754 bbc_arg.path = pe->path;
2755 bbc_arg.path_len = pe->path_len;
2756 bbc_arg.progress_cb = progress_cb;
2757 bbc_arg.progress_arg = progress_arg;
2758 err = got_fileindex_for_each_entry_safe(fileindex,
2759 bump_base_commit_id, &bbc_arg);
2760 if (err)
2761 break;
2763 tpd = STAILQ_NEXT(tpd, entry);
2765 sync_err = sync_fileindex(fileindex, fileindex_path);
2766 if (sync_err && err == NULL)
2767 err = sync_err;
2768 done:
2769 free(fileindex_path);
2770 if (tree)
2771 got_object_tree_close(tree);
2772 if (commit)
2773 got_object_commit_close(commit);
2774 if (fileindex)
2775 got_fileindex_free(fileindex);
2776 while (!STAILQ_EMPTY(&tree_paths)) {
2777 tpd = STAILQ_FIRST(&tree_paths);
2778 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2779 free(tpd->relpath);
2780 free(tpd->tree_id);
2781 free(tpd);
2783 unlockerr = lock_worktree(worktree, LOCK_SH);
2784 if (unlockerr && err == NULL)
2785 err = unlockerr;
2786 return err;
2789 struct merge_file_cb_arg {
2790 struct got_worktree *worktree;
2791 struct got_fileindex *fileindex;
2792 got_worktree_checkout_cb progress_cb;
2793 void *progress_arg;
2794 got_cancel_cb cancel_cb;
2795 void *cancel_arg;
2796 const char *label_orig;
2797 struct got_object_id *commit_id2;
2798 int allow_bad_symlinks;
2801 static const struct got_error *
2802 merge_file_cb(void *arg, struct got_blob_object *blob1,
2803 struct got_blob_object *blob2, FILE *f1, FILE *f2,
2804 struct got_object_id *id1, struct got_object_id *id2,
2805 const char *path1, const char *path2,
2806 mode_t mode1, mode_t mode2, struct got_repository *repo)
2808 static const struct got_error *err = NULL;
2809 struct merge_file_cb_arg *a = arg;
2810 struct got_fileindex_entry *ie;
2811 char *ondisk_path = NULL;
2812 struct stat sb;
2813 unsigned char status;
2814 int local_changes_subsumed;
2815 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2816 char *id_str = NULL, *label_deriv2 = NULL;
2818 if (blob1 && blob2) {
2819 ie = got_fileindex_entry_get(a->fileindex, path2,
2820 strlen(path2));
2821 if (ie == NULL)
2822 return (*a->progress_cb)(a->progress_arg,
2823 GOT_STATUS_MISSING, path2);
2825 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2826 path2) == -1)
2827 return got_error_from_errno("asprintf");
2829 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2830 repo);
2831 if (err)
2832 goto done;
2834 if (status == GOT_STATUS_DELETE) {
2835 err = (*a->progress_cb)(a->progress_arg,
2836 GOT_STATUS_MERGE, path2);
2837 goto done;
2839 if (status != GOT_STATUS_NO_CHANGE &&
2840 status != GOT_STATUS_MODIFY &&
2841 status != GOT_STATUS_CONFLICT &&
2842 status != GOT_STATUS_ADD) {
2843 err = (*a->progress_cb)(a->progress_arg, status, path2);
2844 goto done;
2847 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2848 char *link_target2;
2849 err = got_object_blob_read_to_str(&link_target2, blob2);
2850 if (err)
2851 goto done;
2852 err = merge_symlink(a->worktree, blob1, ondisk_path,
2853 path2, a->label_orig, link_target2, a->commit_id2,
2854 repo, a->progress_cb, a->progress_arg);
2855 free(link_target2);
2856 } else {
2857 int fd;
2859 f_orig = got_opentemp();
2860 if (f_orig == NULL) {
2861 err = got_error_from_errno("got_opentemp");
2862 goto done;
2864 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2865 f_orig, blob1);
2866 if (err)
2867 goto done;
2869 f_deriv2 = got_opentemp();
2870 if (f_deriv2 == NULL)
2871 goto done;
2872 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2873 f_deriv2, blob2);
2874 if (err)
2875 goto done;
2877 fd = open(ondisk_path,
2878 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2879 if (fd == -1) {
2880 err = got_error_from_errno2("open",
2881 ondisk_path);
2882 goto done;
2884 f_deriv = fdopen(fd, "r");
2885 if (f_deriv == NULL) {
2886 err = got_error_from_errno2("fdopen",
2887 ondisk_path);
2888 close(fd);
2889 goto done;
2891 err = got_object_id_str(&id_str, a->commit_id2);
2892 if (err)
2893 goto done;
2894 if (asprintf(&label_deriv2, "%s: commit %s",
2895 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2896 err = got_error_from_errno("asprintf");
2897 goto done;
2899 err = merge_file(&local_changes_subsumed, a->worktree,
2900 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2901 mode2, a->label_orig, NULL, label_deriv2,
2902 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2903 a->progress_cb, a->progress_arg);
2905 } else if (blob1) {
2906 ie = got_fileindex_entry_get(a->fileindex, path1,
2907 strlen(path1));
2908 if (ie == NULL)
2909 return (*a->progress_cb)(a->progress_arg,
2910 GOT_STATUS_MISSING, path1);
2912 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2913 path1) == -1)
2914 return got_error_from_errno("asprintf");
2916 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2917 repo);
2918 if (err)
2919 goto done;
2921 switch (status) {
2922 case GOT_STATUS_NO_CHANGE:
2923 err = (*a->progress_cb)(a->progress_arg,
2924 GOT_STATUS_DELETE, path1);
2925 if (err)
2926 goto done;
2927 err = remove_ondisk_file(a->worktree->root_path, path1);
2928 if (err)
2929 goto done;
2930 if (ie)
2931 got_fileindex_entry_mark_deleted_from_disk(ie);
2932 break;
2933 case GOT_STATUS_DELETE:
2934 case GOT_STATUS_MISSING:
2935 err = (*a->progress_cb)(a->progress_arg,
2936 GOT_STATUS_DELETE, path1);
2937 if (err)
2938 goto done;
2939 if (ie)
2940 got_fileindex_entry_mark_deleted_from_disk(ie);
2941 break;
2942 case GOT_STATUS_ADD: {
2943 struct got_object_id *id;
2944 FILE *blob1_f;
2945 off_t blob1_size;
2947 * Delete the added file only if its content already
2948 * exists in the repository.
2950 err = got_object_blob_file_create(&id, &blob1_f,
2951 &blob1_size, path1);
2952 if (err)
2953 goto done;
2954 if (got_object_id_cmp(id, id1) == 0) {
2955 err = (*a->progress_cb)(a->progress_arg,
2956 GOT_STATUS_DELETE, path1);
2957 if (err)
2958 goto done;
2959 err = remove_ondisk_file(a->worktree->root_path,
2960 path1);
2961 if (err)
2962 goto done;
2963 if (ie)
2964 got_fileindex_entry_remove(a->fileindex,
2965 ie);
2966 } else {
2967 err = (*a->progress_cb)(a->progress_arg,
2968 GOT_STATUS_CANNOT_DELETE, path1);
2970 if (fclose(blob1_f) == EOF && err == NULL)
2971 err = got_error_from_errno("fclose");
2972 free(id);
2973 if (err)
2974 goto done;
2975 break;
2977 case GOT_STATUS_MODIFY:
2978 case GOT_STATUS_CONFLICT:
2979 err = (*a->progress_cb)(a->progress_arg,
2980 GOT_STATUS_CANNOT_DELETE, path1);
2981 if (err)
2982 goto done;
2983 break;
2984 case GOT_STATUS_OBSTRUCTED:
2985 err = (*a->progress_cb)(a->progress_arg, status, path1);
2986 if (err)
2987 goto done;
2988 break;
2989 default:
2990 break;
2992 } else if (blob2) {
2993 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2994 path2) == -1)
2995 return got_error_from_errno("asprintf");
2996 ie = got_fileindex_entry_get(a->fileindex, path2,
2997 strlen(path2));
2998 if (ie) {
2999 err = get_file_status(&status, &sb, ie, ondisk_path,
3000 -1, NULL, repo);
3001 if (err)
3002 goto done;
3003 if (status != GOT_STATUS_NO_CHANGE &&
3004 status != GOT_STATUS_MODIFY &&
3005 status != GOT_STATUS_CONFLICT &&
3006 status != GOT_STATUS_ADD) {
3007 err = (*a->progress_cb)(a->progress_arg,
3008 status, path2);
3009 goto done;
3011 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3012 char *link_target2;
3013 err = got_object_blob_read_to_str(&link_target2,
3014 blob2);
3015 if (err)
3016 goto done;
3017 err = merge_symlink(a->worktree, NULL,
3018 ondisk_path, path2, a->label_orig,
3019 link_target2, a->commit_id2, repo,
3020 a->progress_cb, a->progress_arg);
3021 free(link_target2);
3022 } else if (S_ISREG(sb.st_mode)) {
3023 err = merge_blob(&local_changes_subsumed,
3024 a->worktree, NULL, ondisk_path, path2,
3025 sb.st_mode, a->label_orig, blob2,
3026 a->commit_id2, repo, a->progress_cb,
3027 a->progress_arg);
3028 } else {
3029 err = got_error_path(ondisk_path,
3030 GOT_ERR_FILE_OBSTRUCTED);
3032 if (err)
3033 goto done;
3034 if (status == GOT_STATUS_DELETE) {
3035 err = got_fileindex_entry_update(ie,
3036 a->worktree->root_fd, path2, blob2->id.hash,
3037 a->worktree->base_commit_id->hash, 0);
3038 if (err)
3039 goto done;
3041 } else {
3042 int is_bad_symlink = 0;
3043 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3044 if (S_ISLNK(mode2)) {
3045 err = install_symlink(&is_bad_symlink,
3046 a->worktree, ondisk_path, path2, blob2, 0,
3047 0, 1, a->allow_bad_symlinks, repo,
3048 a->progress_cb, a->progress_arg);
3049 } else {
3050 err = install_blob(a->worktree, ondisk_path, path2,
3051 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3052 a->progress_cb, a->progress_arg);
3054 if (err)
3055 goto done;
3056 err = got_fileindex_entry_alloc(&ie, path2);
3057 if (err)
3058 goto done;
3059 err = got_fileindex_entry_update(ie,
3060 a->worktree->root_fd, path2, NULL, NULL, 1);
3061 if (err) {
3062 got_fileindex_entry_free(ie);
3063 goto done;
3065 err = got_fileindex_entry_add(a->fileindex, ie);
3066 if (err) {
3067 got_fileindex_entry_free(ie);
3068 goto done;
3070 if (is_bad_symlink) {
3071 got_fileindex_entry_filetype_set(ie,
3072 GOT_FILEIDX_MODE_BAD_SYMLINK);
3076 done:
3077 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3078 err = got_error_from_errno("fclose");
3079 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3080 err = got_error_from_errno("fclose");
3081 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3082 err = got_error_from_errno("fclose");
3083 free(id_str);
3084 free(label_deriv2);
3085 free(ondisk_path);
3086 return err;
3089 static const struct got_error *
3090 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3092 struct got_worktree *worktree = arg;
3094 /* Reject merges into a work tree with mixed base commits. */
3095 if (got_fileindex_entry_has_commit(ie) &&
3096 memcmp(ie->commit_sha1, worktree->base_commit_id->hash,
3097 SHA1_DIGEST_LENGTH) != 0)
3098 return got_error(GOT_ERR_MIXED_COMMITS);
3100 return NULL;
3103 struct check_merge_conflicts_arg {
3104 struct got_worktree *worktree;
3105 struct got_fileindex *fileindex;
3106 struct got_repository *repo;
3109 static const struct got_error *
3110 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3111 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3112 struct got_object_id *id1, struct got_object_id *id2,
3113 const char *path1, const char *path2,
3114 mode_t mode1, mode_t mode2, struct got_repository *repo)
3116 const struct got_error *err = NULL;
3117 struct check_merge_conflicts_arg *a = arg;
3118 unsigned char status;
3119 struct stat sb;
3120 struct got_fileindex_entry *ie;
3121 const char *path = path2 ? path2 : path1;
3122 struct got_object_id *id = id2 ? id2 : id1;
3123 char *ondisk_path;
3125 if (id == NULL)
3126 return NULL;
3128 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3129 if (ie == NULL)
3130 return NULL;
3132 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3133 == -1)
3134 return got_error_from_errno("asprintf");
3136 /* Reject merges into a work tree with conflicted files. */
3137 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3138 free(ondisk_path);
3139 if (err)
3140 return err;
3141 if (status == GOT_STATUS_CONFLICT)
3142 return got_error(GOT_ERR_CONFLICTS);
3144 return NULL;
3147 static const struct got_error *
3148 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3149 const char *fileindex_path, struct got_object_id *commit_id1,
3150 struct got_object_id *commit_id2, struct got_repository *repo,
3151 got_worktree_checkout_cb progress_cb, void *progress_arg,
3152 got_cancel_cb cancel_cb, void *cancel_arg)
3154 const struct got_error *err = NULL, *sync_err;
3155 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3156 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3157 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3158 struct check_merge_conflicts_arg cmc_arg;
3159 struct merge_file_cb_arg arg;
3160 char *label_orig = NULL;
3161 FILE *f1 = NULL, *f2 = NULL;
3162 int fd1 = -1, fd2 = -1;
3164 if (commit_id1) {
3165 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3166 if (err)
3167 goto done;
3168 err = got_object_id_by_path(&tree_id1, repo, commit1,
3169 worktree->path_prefix);
3170 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3171 goto done;
3173 if (tree_id1) {
3174 char *id_str;
3176 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3177 if (err)
3178 goto done;
3180 err = got_object_id_str(&id_str, commit_id1);
3181 if (err)
3182 goto done;
3184 if (asprintf(&label_orig, "%s: commit %s",
3185 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3186 err = got_error_from_errno("asprintf");
3187 free(id_str);
3188 goto done;
3190 free(id_str);
3192 f1 = got_opentemp();
3193 if (f1 == NULL) {
3194 err = got_error_from_errno("got_opentemp");
3195 goto done;
3199 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3200 if (err)
3201 goto done;
3203 err = got_object_id_by_path(&tree_id2, repo, commit2,
3204 worktree->path_prefix);
3205 if (err)
3206 goto done;
3208 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3209 if (err)
3210 goto done;
3212 f2 = got_opentemp();
3213 if (f2 == NULL) {
3214 err = got_error_from_errno("got_opentemp");
3215 goto done;
3218 fd1 = got_opentempfd();
3219 if (fd1 == -1) {
3220 err = got_error_from_errno("got_opentempfd");
3221 goto done;
3224 fd2 = got_opentempfd();
3225 if (fd2 == -1) {
3226 err = got_error_from_errno("got_opentempfd");
3227 goto done;
3230 cmc_arg.worktree = worktree;
3231 cmc_arg.fileindex = fileindex;
3232 cmc_arg.repo = repo;
3233 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3234 check_merge_conflicts, &cmc_arg, 0);
3235 if (err)
3236 goto done;
3238 arg.worktree = worktree;
3239 arg.fileindex = fileindex;
3240 arg.progress_cb = progress_cb;
3241 arg.progress_arg = progress_arg;
3242 arg.cancel_cb = cancel_cb;
3243 arg.cancel_arg = cancel_arg;
3244 arg.label_orig = label_orig;
3245 arg.commit_id2 = commit_id2;
3246 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3247 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3248 merge_file_cb, &arg, 1);
3249 sync_err = sync_fileindex(fileindex, fileindex_path);
3250 if (sync_err && err == NULL)
3251 err = sync_err;
3252 done:
3253 if (commit1)
3254 got_object_commit_close(commit1);
3255 if (commit2)
3256 got_object_commit_close(commit2);
3257 if (tree1)
3258 got_object_tree_close(tree1);
3259 if (tree2)
3260 got_object_tree_close(tree2);
3261 if (f1 && fclose(f1) == EOF && err == NULL)
3262 err = got_error_from_errno("fclose");
3263 if (f2 && fclose(f2) == EOF && err == NULL)
3264 err = got_error_from_errno("fclose");
3265 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3266 err = got_error_from_errno("close");
3267 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3268 err = got_error_from_errno("close");
3269 free(label_orig);
3270 return err;
3273 const struct got_error *
3274 got_worktree_merge_files(struct got_worktree *worktree,
3275 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3276 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3277 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3279 const struct got_error *err, *unlockerr;
3280 char *fileindex_path = NULL;
3281 struct got_fileindex *fileindex = NULL;
3283 err = lock_worktree(worktree, LOCK_EX);
3284 if (err)
3285 return err;
3287 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3288 if (err)
3289 goto done;
3291 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3292 worktree);
3293 if (err)
3294 goto done;
3296 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3297 commit_id2, repo, progress_cb, progress_arg,
3298 cancel_cb, cancel_arg);
3299 done:
3300 if (fileindex)
3301 got_fileindex_free(fileindex);
3302 free(fileindex_path);
3303 unlockerr = lock_worktree(worktree, LOCK_SH);
3304 if (unlockerr && err == NULL)
3305 err = unlockerr;
3306 return err;
3309 struct diff_dir_cb_arg {
3310 struct got_fileindex *fileindex;
3311 struct got_worktree *worktree;
3312 const char *status_path;
3313 size_t status_path_len;
3314 struct got_repository *repo;
3315 got_worktree_status_cb status_cb;
3316 void *status_arg;
3317 got_cancel_cb cancel_cb;
3318 void *cancel_arg;
3319 /* A pathlist containing per-directory pathlists of ignore patterns. */
3320 struct got_pathlist_head *ignores;
3321 int report_unchanged;
3322 int no_ignores;
3325 static const struct got_error *
3326 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3327 int dirfd, const char *de_name,
3328 got_worktree_status_cb status_cb, void *status_arg,
3329 struct got_repository *repo, int report_unchanged)
3331 const struct got_error *err = NULL;
3332 unsigned char status = GOT_STATUS_NO_CHANGE;
3333 unsigned char staged_status = get_staged_status(ie);
3334 struct stat sb;
3335 struct got_object_id blob_id, commit_id, staged_blob_id;
3336 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3337 struct got_object_id *staged_blob_idp = NULL;
3339 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3340 if (err)
3341 return err;
3343 if (status == GOT_STATUS_NO_CHANGE &&
3344 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3345 return NULL;
3347 if (got_fileindex_entry_has_blob(ie))
3348 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3349 if (got_fileindex_entry_has_commit(ie))
3350 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3351 if (staged_status == GOT_STATUS_ADD ||
3352 staged_status == GOT_STATUS_MODIFY) {
3353 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3354 &staged_blob_id, ie);
3357 return (*status_cb)(status_arg, status, staged_status,
3358 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3361 static const struct got_error *
3362 status_old_new(void *arg, struct got_fileindex_entry *ie,
3363 struct dirent *de, const char *parent_path, int dirfd)
3365 const struct got_error *err = NULL;
3366 struct diff_dir_cb_arg *a = arg;
3367 char *abspath;
3369 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3370 return got_error(GOT_ERR_CANCELLED);
3372 if (got_path_cmp(parent_path, a->status_path,
3373 strlen(parent_path), a->status_path_len) != 0 &&
3374 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3375 return NULL;
3377 if (parent_path[0]) {
3378 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3379 parent_path, de->d_name) == -1)
3380 return got_error_from_errno("asprintf");
3381 } else {
3382 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3383 de->d_name) == -1)
3384 return got_error_from_errno("asprintf");
3387 err = report_file_status(ie, abspath, dirfd, de->d_name,
3388 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3389 free(abspath);
3390 return err;
3393 static const struct got_error *
3394 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3396 struct diff_dir_cb_arg *a = arg;
3397 struct got_object_id blob_id, commit_id;
3398 unsigned char status;
3400 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3401 return got_error(GOT_ERR_CANCELLED);
3403 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3404 return NULL;
3406 got_fileindex_entry_get_blob_id(&blob_id, ie);
3407 got_fileindex_entry_get_commit_id(&commit_id, ie);
3408 if (got_fileindex_entry_has_file_on_disk(ie))
3409 status = GOT_STATUS_MISSING;
3410 else
3411 status = GOT_STATUS_DELETE;
3412 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3413 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3416 static void
3417 free_ignores(struct got_pathlist_head *ignores)
3419 struct got_pathlist_entry *pe;
3421 TAILQ_FOREACH(pe, ignores, entry) {
3422 struct got_pathlist_head *ignorelist = pe->data;
3424 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3426 got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3429 static const struct got_error *
3430 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3432 const struct got_error *err = NULL;
3433 struct got_pathlist_entry *pe = NULL;
3434 struct got_pathlist_head *ignorelist;
3435 char *line = NULL, *pattern, *dirpath = NULL;
3436 size_t linesize = 0;
3437 ssize_t linelen;
3439 ignorelist = calloc(1, sizeof(*ignorelist));
3440 if (ignorelist == NULL)
3441 return got_error_from_errno("calloc");
3442 TAILQ_INIT(ignorelist);
3444 while ((linelen = getline(&line, &linesize, f)) != -1) {
3445 if (linelen > 0 && line[linelen - 1] == '\n')
3446 line[linelen - 1] = '\0';
3448 /* Git's ignores may contain comments. */
3449 if (line[0] == '#')
3450 continue;
3452 /* Git's negated patterns are not (yet?) supported. */
3453 if (line[0] == '!')
3454 continue;
3456 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3457 line) == -1) {
3458 err = got_error_from_errno("asprintf");
3459 goto done;
3461 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3462 if (err)
3463 goto done;
3465 if (ferror(f)) {
3466 err = got_error_from_errno("getline");
3467 goto done;
3470 dirpath = strdup(path);
3471 if (dirpath == NULL) {
3472 err = got_error_from_errno("strdup");
3473 goto done;
3475 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3476 done:
3477 free(line);
3478 if (err || pe == NULL) {
3479 free(dirpath);
3480 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3482 return err;
3485 static int
3486 match_ignores(struct got_pathlist_head *ignores, const char *path)
3488 struct got_pathlist_entry *pe;
3490 /* Handle patterns which match in all directories. */
3491 TAILQ_FOREACH(pe, ignores, entry) {
3492 struct got_pathlist_head *ignorelist = pe->data;
3493 struct got_pathlist_entry *pi;
3495 TAILQ_FOREACH(pi, ignorelist, entry) {
3496 const char *p, *pattern = pi->path;
3498 if (strncmp(pattern, "**/", 3) != 0)
3499 continue;
3500 pattern += 3;
3501 p = path;
3502 while (*p) {
3503 if (fnmatch(pattern, p,
3504 FNM_PATHNAME | FNM_LEADING_DIR)) {
3505 /* Retry in next directory. */
3506 while (*p && *p != '/')
3507 p++;
3508 while (*p == '/')
3509 p++;
3510 continue;
3512 return 1;
3518 * The ignores pathlist contains ignore lists from children before
3519 * parents, so we can find the most specific ignorelist by walking
3520 * ignores backwards.
3522 pe = TAILQ_LAST(ignores, got_pathlist_head);
3523 while (pe) {
3524 if (got_path_is_child(path, pe->path, pe->path_len)) {
3525 struct got_pathlist_head *ignorelist = pe->data;
3526 struct got_pathlist_entry *pi;
3527 TAILQ_FOREACH(pi, ignorelist, entry) {
3528 const char *pattern = pi->path;
3529 int flags = FNM_LEADING_DIR;
3530 if (strstr(pattern, "/**/") == NULL)
3531 flags |= FNM_PATHNAME;
3532 if (fnmatch(pattern, path, flags))
3533 continue;
3534 return 1;
3537 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3540 return 0;
3543 static const struct got_error *
3544 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3545 const char *path, int dirfd, const char *ignores_filename)
3547 const struct got_error *err = NULL;
3548 char *ignorespath;
3549 int fd = -1;
3550 FILE *ignoresfile = NULL;
3552 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3553 path[0] ? "/" : "", ignores_filename) == -1)
3554 return got_error_from_errno("asprintf");
3556 if (dirfd != -1) {
3557 fd = openat(dirfd, ignores_filename,
3558 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3559 if (fd == -1) {
3560 if (errno != ENOENT && errno != EACCES)
3561 err = got_error_from_errno2("openat",
3562 ignorespath);
3563 } else {
3564 ignoresfile = fdopen(fd, "r");
3565 if (ignoresfile == NULL)
3566 err = got_error_from_errno2("fdopen",
3567 ignorespath);
3568 else {
3569 fd = -1;
3570 err = read_ignores(ignores, path, ignoresfile);
3573 } else {
3574 ignoresfile = fopen(ignorespath, "re");
3575 if (ignoresfile == NULL) {
3576 if (errno != ENOENT && errno != EACCES)
3577 err = got_error_from_errno2("fopen",
3578 ignorespath);
3579 } else
3580 err = read_ignores(ignores, path, ignoresfile);
3583 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3584 err = got_error_from_errno2("fclose", path);
3585 if (fd != -1 && close(fd) == -1 && err == NULL)
3586 err = got_error_from_errno2("close", path);
3587 free(ignorespath);
3588 return err;
3591 static const struct got_error *
3592 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3593 int dirfd)
3595 const struct got_error *err = NULL;
3596 struct diff_dir_cb_arg *a = arg;
3597 char *path = NULL;
3599 if (ignore != NULL)
3600 *ignore = 0;
3602 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3603 return got_error(GOT_ERR_CANCELLED);
3605 if (parent_path[0]) {
3606 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3607 return got_error_from_errno("asprintf");
3608 } else {
3609 path = de->d_name;
3612 if (de->d_type == DT_DIR) {
3613 if (!a->no_ignores && ignore != NULL &&
3614 match_ignores(a->ignores, path))
3615 *ignore = 1;
3616 } else if (!match_ignores(a->ignores, path) &&
3617 got_path_is_child(path, a->status_path, a->status_path_len))
3618 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3619 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3620 if (parent_path[0])
3621 free(path);
3622 return err;
3625 static const struct got_error *
3626 status_traverse(void *arg, const char *path, int dirfd)
3628 const struct got_error *err = NULL;
3629 struct diff_dir_cb_arg *a = arg;
3631 if (a->no_ignores)
3632 return NULL;
3634 err = add_ignores(a->ignores, a->worktree->root_path,
3635 path, dirfd, ".cvsignore");
3636 if (err)
3637 return err;
3639 err = add_ignores(a->ignores, a->worktree->root_path, path,
3640 dirfd, ".gitignore");
3642 return err;
3645 static const struct got_error *
3646 report_single_file_status(const char *path, const char *ondisk_path,
3647 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3648 void *status_arg, struct got_repository *repo, int report_unchanged,
3649 struct got_pathlist_head *ignores, int no_ignores)
3651 struct got_fileindex_entry *ie;
3652 struct stat sb;
3654 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3655 if (ie)
3656 return report_file_status(ie, ondisk_path, -1, NULL,
3657 status_cb, status_arg, repo, report_unchanged);
3659 if (lstat(ondisk_path, &sb) == -1) {
3660 if (errno != ENOENT)
3661 return got_error_from_errno2("lstat", ondisk_path);
3662 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3663 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3666 if (!no_ignores && match_ignores(ignores, path))
3667 return NULL;
3669 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3670 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3671 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3673 return NULL;
3676 static const struct got_error *
3677 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3678 const char *root_path, const char *path)
3680 const struct got_error *err;
3681 char *parent_path, *next_parent_path = NULL;
3683 err = add_ignores(ignores, root_path, "", -1,
3684 ".cvsignore");
3685 if (err)
3686 return err;
3688 err = add_ignores(ignores, root_path, "", -1,
3689 ".gitignore");
3690 if (err)
3691 return err;
3693 err = got_path_dirname(&parent_path, path);
3694 if (err) {
3695 if (err->code == GOT_ERR_BAD_PATH)
3696 return NULL; /* cannot traverse parent */
3697 return err;
3699 for (;;) {
3700 err = add_ignores(ignores, root_path, parent_path, -1,
3701 ".cvsignore");
3702 if (err)
3703 break;
3704 err = add_ignores(ignores, root_path, parent_path, -1,
3705 ".gitignore");
3706 if (err)
3707 break;
3708 err = got_path_dirname(&next_parent_path, parent_path);
3709 if (err) {
3710 if (err->code == GOT_ERR_BAD_PATH)
3711 err = NULL; /* traversed everything */
3712 break;
3714 if (got_path_is_root_dir(parent_path))
3715 break;
3716 free(parent_path);
3717 parent_path = next_parent_path;
3718 next_parent_path = NULL;
3721 free(parent_path);
3722 free(next_parent_path);
3723 return err;
3726 static const struct got_error *
3727 worktree_status(struct got_worktree *worktree, const char *path,
3728 struct got_fileindex *fileindex, struct got_repository *repo,
3729 got_worktree_status_cb status_cb, void *status_arg,
3730 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3731 int report_unchanged)
3733 const struct got_error *err = NULL;
3734 int fd = -1;
3735 struct got_fileindex_diff_dir_cb fdiff_cb;
3736 struct diff_dir_cb_arg arg;
3737 char *ondisk_path = NULL;
3738 struct got_pathlist_head ignores;
3739 struct got_fileindex_entry *ie;
3741 TAILQ_INIT(&ignores);
3743 if (asprintf(&ondisk_path, "%s%s%s",
3744 worktree->root_path, path[0] ? "/" : "", path) == -1)
3745 return got_error_from_errno("asprintf");
3747 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3748 if (ie) {
3749 err = report_single_file_status(path, ondisk_path,
3750 fileindex, status_cb, status_arg, repo,
3751 report_unchanged, &ignores, no_ignores);
3752 goto done;
3755 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3756 if (fd == -1) {
3757 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3758 !got_err_open_nofollow_on_symlink())
3759 err = got_error_from_errno2("open", ondisk_path);
3760 else {
3761 if (!no_ignores) {
3762 err = add_ignores_from_parent_paths(&ignores,
3763 worktree->root_path, ondisk_path);
3764 if (err)
3765 goto done;
3767 err = report_single_file_status(path, ondisk_path,
3768 fileindex, status_cb, status_arg, repo,
3769 report_unchanged, &ignores, no_ignores);
3771 } else {
3772 fdiff_cb.diff_old_new = status_old_new;
3773 fdiff_cb.diff_old = status_old;
3774 fdiff_cb.diff_new = status_new;
3775 fdiff_cb.diff_traverse = status_traverse;
3776 arg.fileindex = fileindex;
3777 arg.worktree = worktree;
3778 arg.status_path = path;
3779 arg.status_path_len = strlen(path);
3780 arg.repo = repo;
3781 arg.status_cb = status_cb;
3782 arg.status_arg = status_arg;
3783 arg.cancel_cb = cancel_cb;
3784 arg.cancel_arg = cancel_arg;
3785 arg.report_unchanged = report_unchanged;
3786 arg.no_ignores = no_ignores;
3787 if (!no_ignores) {
3788 err = add_ignores_from_parent_paths(&ignores,
3789 worktree->root_path, path);
3790 if (err)
3791 goto done;
3793 arg.ignores = &ignores;
3794 err = got_fileindex_diff_dir(fileindex, fd,
3795 worktree->root_path, path, repo, &fdiff_cb, &arg);
3797 done:
3798 free_ignores(&ignores);
3799 if (fd != -1 && close(fd) == -1 && err == NULL)
3800 err = got_error_from_errno("close");
3801 free(ondisk_path);
3802 return err;
3805 const struct got_error *
3806 got_worktree_status(struct got_worktree *worktree,
3807 struct got_pathlist_head *paths, struct got_repository *repo,
3808 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3809 got_cancel_cb cancel_cb, void *cancel_arg)
3811 const struct got_error *err = NULL;
3812 char *fileindex_path = NULL;
3813 struct got_fileindex *fileindex = NULL;
3814 struct got_pathlist_entry *pe;
3816 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3817 if (err)
3818 return err;
3820 TAILQ_FOREACH(pe, paths, entry) {
3821 err = worktree_status(worktree, pe->path, fileindex, repo,
3822 status_cb, status_arg, cancel_cb, cancel_arg,
3823 no_ignores, 0);
3824 if (err)
3825 break;
3827 free(fileindex_path);
3828 got_fileindex_free(fileindex);
3829 return err;
3832 const struct got_error *
3833 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3834 const char *arg)
3836 const struct got_error *err = NULL;
3837 char *resolved = NULL, *cwd = NULL, *path = NULL;
3838 size_t len;
3839 struct stat sb;
3840 char *abspath = NULL;
3841 char canonpath[PATH_MAX];
3843 *wt_path = NULL;
3845 cwd = getcwd(NULL, 0);
3846 if (cwd == NULL)
3847 return got_error_from_errno("getcwd");
3849 if (lstat(arg, &sb) == -1) {
3850 if (errno != ENOENT) {
3851 err = got_error_from_errno2("lstat", arg);
3852 goto done;
3854 sb.st_mode = 0;
3856 if (S_ISLNK(sb.st_mode)) {
3858 * We cannot use realpath(3) with symlinks since we want to
3859 * operate on the symlink itself.
3860 * But we can make the path absolute, assuming it is relative
3861 * to the current working directory, and then canonicalize it.
3863 if (!got_path_is_absolute(arg)) {
3864 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3865 err = got_error_from_errno("asprintf");
3866 goto done;
3870 err = got_canonpath(abspath ? abspath : arg, canonpath,
3871 sizeof(canonpath));
3872 if (err)
3873 goto done;
3874 resolved = strdup(canonpath);
3875 if (resolved == NULL) {
3876 err = got_error_from_errno("strdup");
3877 goto done;
3879 } else {
3880 resolved = realpath(arg, NULL);
3881 if (resolved == NULL) {
3882 if (errno != ENOENT) {
3883 err = got_error_from_errno2("realpath", arg);
3884 goto done;
3886 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3887 err = got_error_from_errno("asprintf");
3888 goto done;
3890 err = got_canonpath(abspath, canonpath,
3891 sizeof(canonpath));
3892 if (err)
3893 goto done;
3894 resolved = strdup(canonpath);
3895 if (resolved == NULL) {
3896 err = got_error_from_errno("strdup");
3897 goto done;
3902 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3903 strlen(got_worktree_get_root_path(worktree)))) {
3904 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3905 goto done;
3908 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3909 err = got_path_skip_common_ancestor(&path,
3910 got_worktree_get_root_path(worktree), resolved);
3911 if (err)
3912 goto done;
3913 } else {
3914 path = strdup("");
3915 if (path == NULL) {
3916 err = got_error_from_errno("strdup");
3917 goto done;
3921 /* XXX status walk can't deal with trailing slash! */
3922 len = strlen(path);
3923 while (len > 0 && path[len - 1] == '/') {
3924 path[len - 1] = '\0';
3925 len--;
3927 done:
3928 free(abspath);
3929 free(resolved);
3930 free(cwd);
3931 if (err == NULL)
3932 *wt_path = path;
3933 else
3934 free(path);
3935 return err;
3938 struct schedule_addition_args {
3939 struct got_worktree *worktree;
3940 struct got_fileindex *fileindex;
3941 got_worktree_checkout_cb progress_cb;
3942 void *progress_arg;
3943 struct got_repository *repo;
3946 static const struct got_error *
3947 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3948 const char *relpath, struct got_object_id *blob_id,
3949 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3950 int dirfd, const char *de_name)
3952 struct schedule_addition_args *a = arg;
3953 const struct got_error *err = NULL;
3954 struct got_fileindex_entry *ie;
3955 struct stat sb;
3956 char *ondisk_path;
3958 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3959 relpath) == -1)
3960 return got_error_from_errno("asprintf");
3962 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3963 if (ie) {
3964 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3965 de_name, a->repo);
3966 if (err)
3967 goto done;
3968 /* Re-adding an existing entry is a no-op. */
3969 if (status == GOT_STATUS_ADD)
3970 goto done;
3971 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3972 if (err)
3973 goto done;
3976 if (status != GOT_STATUS_UNVERSIONED) {
3977 if (status == GOT_STATUS_NONEXISTENT)
3978 err = got_error_set_errno(ENOENT, ondisk_path);
3979 else
3980 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3981 goto done;
3984 err = got_fileindex_entry_alloc(&ie, relpath);
3985 if (err)
3986 goto done;
3987 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3988 relpath, NULL, NULL, 1);
3989 if (err) {
3990 got_fileindex_entry_free(ie);
3991 goto done;
3993 err = got_fileindex_entry_add(a->fileindex, ie);
3994 if (err) {
3995 got_fileindex_entry_free(ie);
3996 goto done;
3998 done:
3999 free(ondisk_path);
4000 if (err)
4001 return err;
4002 if (status == GOT_STATUS_ADD)
4003 return NULL;
4004 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4007 const struct got_error *
4008 got_worktree_schedule_add(struct got_worktree *worktree,
4009 struct got_pathlist_head *paths,
4010 got_worktree_checkout_cb progress_cb, void *progress_arg,
4011 struct got_repository *repo, int no_ignores)
4013 struct got_fileindex *fileindex = NULL;
4014 char *fileindex_path = NULL;
4015 const struct got_error *err = NULL, *sync_err, *unlockerr;
4016 struct got_pathlist_entry *pe;
4017 struct schedule_addition_args saa;
4019 err = lock_worktree(worktree, LOCK_EX);
4020 if (err)
4021 return err;
4023 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4024 if (err)
4025 goto done;
4027 saa.worktree = worktree;
4028 saa.fileindex = fileindex;
4029 saa.progress_cb = progress_cb;
4030 saa.progress_arg = progress_arg;
4031 saa.repo = repo;
4033 TAILQ_FOREACH(pe, paths, entry) {
4034 err = worktree_status(worktree, pe->path, fileindex, repo,
4035 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4036 if (err)
4037 break;
4039 sync_err = sync_fileindex(fileindex, fileindex_path);
4040 if (sync_err && err == NULL)
4041 err = sync_err;
4042 done:
4043 free(fileindex_path);
4044 if (fileindex)
4045 got_fileindex_free(fileindex);
4046 unlockerr = lock_worktree(worktree, LOCK_SH);
4047 if (unlockerr && err == NULL)
4048 err = unlockerr;
4049 return err;
4052 struct schedule_deletion_args {
4053 struct got_worktree *worktree;
4054 struct got_fileindex *fileindex;
4055 got_worktree_delete_cb progress_cb;
4056 void *progress_arg;
4057 struct got_repository *repo;
4058 int delete_local_mods;
4059 int keep_on_disk;
4060 int ignore_missing_paths;
4061 const char *status_codes;
4064 static const struct got_error *
4065 schedule_for_deletion(void *arg, unsigned char status,
4066 unsigned char staged_status, const char *relpath,
4067 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4068 struct got_object_id *commit_id, int dirfd, const char *de_name)
4070 struct schedule_deletion_args *a = arg;
4071 const struct got_error *err = NULL;
4072 struct got_fileindex_entry *ie = NULL;
4073 struct stat sb;
4074 char *ondisk_path;
4076 if (status == GOT_STATUS_NONEXISTENT) {
4077 if (a->ignore_missing_paths)
4078 return NULL;
4079 return got_error_set_errno(ENOENT, relpath);
4082 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4083 if (ie == NULL)
4084 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4086 staged_status = get_staged_status(ie);
4087 if (staged_status != GOT_STATUS_NO_CHANGE) {
4088 if (staged_status == GOT_STATUS_DELETE)
4089 return NULL;
4090 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4093 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4094 relpath) == -1)
4095 return got_error_from_errno("asprintf");
4097 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4098 a->repo);
4099 if (err)
4100 goto done;
4102 if (a->status_codes) {
4103 size_t ncodes = strlen(a->status_codes);
4104 int i;
4105 for (i = 0; i < ncodes ; i++) {
4106 if (status == a->status_codes[i])
4107 break;
4109 if (i == ncodes) {
4110 /* Do not delete files in non-matching status. */
4111 free(ondisk_path);
4112 return NULL;
4114 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4115 a->status_codes[i] != GOT_STATUS_MISSING) {
4116 static char msg[64];
4117 snprintf(msg, sizeof(msg),
4118 "invalid status code '%c'", a->status_codes[i]);
4119 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4120 goto done;
4124 if (status != GOT_STATUS_NO_CHANGE) {
4125 if (status == GOT_STATUS_DELETE)
4126 goto done;
4127 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4128 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4129 goto done;
4131 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4132 err = got_error_set_errno(ENOENT, relpath);
4133 goto done;
4135 if (status != GOT_STATUS_MODIFY &&
4136 status != GOT_STATUS_MISSING) {
4137 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4138 goto done;
4142 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4143 size_t root_len;
4145 if (dirfd != -1) {
4146 if (unlinkat(dirfd, de_name, 0) == -1) {
4147 err = got_error_from_errno2("unlinkat",
4148 ondisk_path);
4149 goto done;
4151 } else if (unlink(ondisk_path) == -1) {
4152 err = got_error_from_errno2("unlink", ondisk_path);
4153 goto done;
4156 root_len = strlen(a->worktree->root_path);
4157 do {
4158 char *parent;
4159 err = got_path_dirname(&parent, ondisk_path);
4160 if (err)
4161 goto done;
4162 free(ondisk_path);
4163 ondisk_path = parent;
4164 if (rmdir(ondisk_path) == -1) {
4165 if (errno != ENOTEMPTY)
4166 err = got_error_from_errno2("rmdir",
4167 ondisk_path);
4168 break;
4170 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4171 strlen(ondisk_path), root_len) != 0);
4174 got_fileindex_entry_mark_deleted_from_disk(ie);
4175 done:
4176 free(ondisk_path);
4177 if (err)
4178 return err;
4179 if (status == GOT_STATUS_DELETE)
4180 return NULL;
4181 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4182 staged_status, relpath);
4185 const struct got_error *
4186 got_worktree_schedule_delete(struct got_worktree *worktree,
4187 struct got_pathlist_head *paths, int delete_local_mods,
4188 const char *status_codes,
4189 got_worktree_delete_cb progress_cb, void *progress_arg,
4190 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4192 struct got_fileindex *fileindex = NULL;
4193 char *fileindex_path = NULL;
4194 const struct got_error *err = NULL, *sync_err, *unlockerr;
4195 struct got_pathlist_entry *pe;
4196 struct schedule_deletion_args sda;
4198 err = lock_worktree(worktree, LOCK_EX);
4199 if (err)
4200 return err;
4202 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4203 if (err)
4204 goto done;
4206 sda.worktree = worktree;
4207 sda.fileindex = fileindex;
4208 sda.progress_cb = progress_cb;
4209 sda.progress_arg = progress_arg;
4210 sda.repo = repo;
4211 sda.delete_local_mods = delete_local_mods;
4212 sda.keep_on_disk = keep_on_disk;
4213 sda.ignore_missing_paths = ignore_missing_paths;
4214 sda.status_codes = status_codes;
4216 TAILQ_FOREACH(pe, paths, entry) {
4217 err = worktree_status(worktree, pe->path, fileindex, repo,
4218 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4219 if (err)
4220 break;
4222 sync_err = sync_fileindex(fileindex, fileindex_path);
4223 if (sync_err && err == NULL)
4224 err = sync_err;
4225 done:
4226 free(fileindex_path);
4227 if (fileindex)
4228 got_fileindex_free(fileindex);
4229 unlockerr = lock_worktree(worktree, LOCK_SH);
4230 if (unlockerr && err == NULL)
4231 err = unlockerr;
4232 return err;
4235 static const struct got_error *
4236 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4238 const struct got_error *err = NULL;
4239 char *line = NULL;
4240 size_t linesize = 0, n;
4241 ssize_t linelen;
4243 linelen = getline(&line, &linesize, infile);
4244 if (linelen == -1) {
4245 if (ferror(infile)) {
4246 err = got_error_from_errno("getline");
4247 goto done;
4249 return NULL;
4251 if (outfile) {
4252 n = fwrite(line, 1, linelen, outfile);
4253 if (n != linelen) {
4254 err = got_ferror(outfile, GOT_ERR_IO);
4255 goto done;
4258 if (rejectfile) {
4259 n = fwrite(line, 1, linelen, rejectfile);
4260 if (n != linelen)
4261 err = got_ferror(rejectfile, GOT_ERR_IO);
4263 done:
4264 free(line);
4265 return err;
4268 static const struct got_error *
4269 skip_one_line(FILE *f)
4271 char *line = NULL;
4272 size_t linesize = 0;
4273 ssize_t linelen;
4275 linelen = getline(&line, &linesize, f);
4276 if (linelen == -1) {
4277 if (ferror(f))
4278 return got_error_from_errno("getline");
4279 return NULL;
4281 free(line);
4282 return NULL;
4285 static const struct got_error *
4286 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4287 int start_old, int end_old, int start_new, int end_new,
4288 FILE *outfile, FILE *rejectfile)
4290 const struct got_error *err;
4292 /* Copy old file's lines leading up to patch. */
4293 while (!feof(f1) && *line_cur1 < start_old) {
4294 err = copy_one_line(f1, outfile, NULL);
4295 if (err)
4296 return err;
4297 (*line_cur1)++;
4299 /* Skip new file's lines leading up to patch. */
4300 while (!feof(f2) && *line_cur2 < start_new) {
4301 if (rejectfile)
4302 err = copy_one_line(f2, NULL, rejectfile);
4303 else
4304 err = skip_one_line(f2);
4305 if (err)
4306 return err;
4307 (*line_cur2)++;
4309 /* Copy patched lines. */
4310 while (!feof(f2) && *line_cur2 <= end_new) {
4311 err = copy_one_line(f2, outfile, NULL);
4312 if (err)
4313 return err;
4314 (*line_cur2)++;
4316 /* Skip over old file's replaced lines. */
4317 while (!feof(f1) && *line_cur1 <= end_old) {
4318 if (rejectfile)
4319 err = copy_one_line(f1, NULL, rejectfile);
4320 else
4321 err = skip_one_line(f1);
4322 if (err)
4323 return err;
4324 (*line_cur1)++;
4327 return NULL;
4330 static const struct got_error *
4331 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4332 FILE *outfile, FILE *rejectfile)
4334 const struct got_error *err;
4336 if (outfile) {
4337 /* Copy old file's lines until EOF. */
4338 while (!feof(f1)) {
4339 err = copy_one_line(f1, outfile, NULL);
4340 if (err)
4341 return err;
4342 (*line_cur1)++;
4345 if (rejectfile) {
4346 /* Copy new file's lines until EOF. */
4347 while (!feof(f2)) {
4348 err = copy_one_line(f2, NULL, rejectfile);
4349 if (err)
4350 return err;
4351 (*line_cur2)++;
4355 return NULL;
4358 static const struct got_error *
4359 apply_or_reject_change(int *choice, int *nchunks_used,
4360 struct diff_result *diff_result, int n,
4361 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4362 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4363 got_worktree_patch_cb patch_cb, void *patch_arg)
4365 const struct got_error *err = NULL;
4366 struct diff_chunk_context cc = {};
4367 int start_old, end_old, start_new, end_new;
4368 FILE *hunkfile;
4369 struct diff_output_unidiff_state *diff_state;
4370 struct diff_input_info diff_info;
4371 int rc;
4373 *choice = GOT_PATCH_CHOICE_NONE;
4375 /* Get changed line numbers without context lines for copy_change(). */
4376 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4377 start_old = cc.left.start;
4378 end_old = cc.left.end;
4379 start_new = cc.right.start;
4380 end_new = cc.right.end;
4382 /* Get the same change with context lines for display. */
4383 memset(&cc, 0, sizeof(cc));
4384 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4386 memset(&diff_info, 0, sizeof(diff_info));
4387 diff_info.left_path = relpath;
4388 diff_info.right_path = relpath;
4390 diff_state = diff_output_unidiff_state_alloc();
4391 if (diff_state == NULL)
4392 return got_error_set_errno(ENOMEM,
4393 "diff_output_unidiff_state_alloc");
4395 hunkfile = got_opentemp();
4396 if (hunkfile == NULL) {
4397 err = got_error_from_errno("got_opentemp");
4398 goto done;
4401 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4402 diff_result, &cc);
4403 if (rc != DIFF_RC_OK) {
4404 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4405 goto done;
4408 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4409 err = got_ferror(hunkfile, GOT_ERR_IO);
4410 goto done;
4413 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4414 hunkfile, changeno, nchanges);
4415 if (err)
4416 goto done;
4418 switch (*choice) {
4419 case GOT_PATCH_CHOICE_YES:
4420 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4421 end_old, start_new, end_new, outfile, rejectfile);
4422 break;
4423 case GOT_PATCH_CHOICE_NO:
4424 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4425 end_old, start_new, end_new, rejectfile, outfile);
4426 break;
4427 case GOT_PATCH_CHOICE_QUIT:
4428 break;
4429 default:
4430 err = got_error(GOT_ERR_PATCH_CHOICE);
4431 break;
4433 done:
4434 diff_output_unidiff_state_free(diff_state);
4435 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4436 err = got_error_from_errno("fclose");
4437 return err;
4440 struct revert_file_args {
4441 struct got_worktree *worktree;
4442 struct got_fileindex *fileindex;
4443 got_worktree_checkout_cb progress_cb;
4444 void *progress_arg;
4445 got_worktree_patch_cb patch_cb;
4446 void *patch_arg;
4447 struct got_repository *repo;
4448 int unlink_added_files;
4451 static const struct got_error *
4452 create_patched_content(char **path_outfile, int reverse_patch,
4453 struct got_object_id *blob_id, const char *path2,
4454 int dirfd2, const char *de_name2,
4455 const char *relpath, struct got_repository *repo,
4456 got_worktree_patch_cb patch_cb, void *patch_arg)
4458 const struct got_error *err, *free_err;
4459 struct got_blob_object *blob = NULL;
4460 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4461 int fd = -1, fd2 = -1;
4462 char link_target[PATH_MAX];
4463 ssize_t link_len = 0;
4464 char *path1 = NULL, *id_str = NULL;
4465 struct stat sb2;
4466 struct got_diffreg_result *diffreg_result = NULL;
4467 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4468 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4470 *path_outfile = NULL;
4472 err = got_object_id_str(&id_str, blob_id);
4473 if (err)
4474 return err;
4476 if (dirfd2 != -1) {
4477 fd2 = openat(dirfd2, de_name2,
4478 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4479 if (fd2 == -1) {
4480 if (!got_err_open_nofollow_on_symlink()) {
4481 err = got_error_from_errno2("openat", path2);
4482 goto done;
4484 link_len = readlinkat(dirfd2, de_name2,
4485 link_target, sizeof(link_target));
4486 if (link_len == -1) {
4487 return got_error_from_errno2("readlinkat",
4488 path2);
4490 sb2.st_mode = S_IFLNK;
4491 sb2.st_size = link_len;
4493 } else {
4494 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4495 if (fd2 == -1) {
4496 if (!got_err_open_nofollow_on_symlink()) {
4497 err = got_error_from_errno2("open", path2);
4498 goto done;
4500 link_len = readlink(path2, link_target,
4501 sizeof(link_target));
4502 if (link_len == -1)
4503 return got_error_from_errno2("readlink", path2);
4504 sb2.st_mode = S_IFLNK;
4505 sb2.st_size = link_len;
4508 if (fd2 != -1) {
4509 if (fstat(fd2, &sb2) == -1) {
4510 err = got_error_from_errno2("fstat", path2);
4511 goto done;
4514 f2 = fdopen(fd2, "r");
4515 if (f2 == NULL) {
4516 err = got_error_from_errno2("fdopen", path2);
4517 goto done;
4519 fd2 = -1;
4520 } else {
4521 size_t n;
4522 f2 = got_opentemp();
4523 if (f2 == NULL) {
4524 err = got_error_from_errno2("got_opentemp", path2);
4525 goto done;
4527 n = fwrite(link_target, 1, link_len, f2);
4528 if (n != link_len) {
4529 err = got_ferror(f2, GOT_ERR_IO);
4530 goto done;
4532 if (fflush(f2) == EOF) {
4533 err = got_error_from_errno("fflush");
4534 goto done;
4536 rewind(f2);
4539 fd = got_opentempfd();
4540 if (fd == -1) {
4541 err = got_error_from_errno("got_opentempfd");
4542 goto done;
4545 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4546 if (err)
4547 goto done;
4549 err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4550 if (err)
4551 goto done;
4553 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4554 if (err)
4555 goto done;
4557 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4558 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4559 if (err)
4560 goto done;
4562 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4563 "");
4564 if (err)
4565 goto done;
4567 if (fseek(f1, 0L, SEEK_SET) == -1)
4568 return got_ferror(f1, GOT_ERR_IO);
4569 if (fseek(f2, 0L, SEEK_SET) == -1)
4570 return got_ferror(f2, GOT_ERR_IO);
4572 /* Count the number of actual changes in the diff result. */
4573 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4574 struct diff_chunk_context cc = {};
4575 diff_chunk_context_load_change(&cc, &nchunks_used,
4576 diffreg_result->result, n, 0);
4577 nchanges++;
4579 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4580 int choice;
4581 err = apply_or_reject_change(&choice, &nchunks_used,
4582 diffreg_result->result, n, relpath, f1, f2,
4583 &line_cur1, &line_cur2,
4584 reverse_patch ? NULL : outfile,
4585 reverse_patch ? outfile : NULL,
4586 ++i, nchanges, patch_cb, patch_arg);
4587 if (err)
4588 goto done;
4589 if (choice == GOT_PATCH_CHOICE_YES)
4590 have_content = 1;
4591 else if (choice == GOT_PATCH_CHOICE_QUIT)
4592 break;
4594 if (have_content) {
4595 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4596 reverse_patch ? NULL : outfile,
4597 reverse_patch ? outfile : NULL);
4598 if (err)
4599 goto done;
4601 if (!S_ISLNK(sb2.st_mode)) {
4602 mode_t mode;
4604 mode = apply_umask(sb2.st_mode);
4605 if (fchmod(fileno(outfile), mode) == -1) {
4606 err = got_error_from_errno2("fchmod", path2);
4607 goto done;
4611 done:
4612 free(id_str);
4613 if (fd != -1 && close(fd) == -1 && err == NULL)
4614 err = got_error_from_errno("close");
4615 if (blob)
4616 got_object_blob_close(blob);
4617 free_err = got_diffreg_result_free(diffreg_result);
4618 if (err == NULL)
4619 err = free_err;
4620 if (f1 && fclose(f1) == EOF && err == NULL)
4621 err = got_error_from_errno2("fclose", path1);
4622 if (f2 && fclose(f2) == EOF && err == NULL)
4623 err = got_error_from_errno2("fclose", path2);
4624 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4625 err = got_error_from_errno2("close", path2);
4626 if (outfile && fclose(outfile) == EOF && err == NULL)
4627 err = got_error_from_errno2("fclose", *path_outfile);
4628 if (path1 && unlink(path1) == -1 && err == NULL)
4629 err = got_error_from_errno2("unlink", path1);
4630 if (err || !have_content) {
4631 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4632 err = got_error_from_errno2("unlink", *path_outfile);
4633 free(*path_outfile);
4634 *path_outfile = NULL;
4636 free(path1);
4637 return err;
4640 static const struct got_error *
4641 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4642 const char *relpath, struct got_object_id *blob_id,
4643 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4644 int dirfd, const char *de_name)
4646 struct revert_file_args *a = arg;
4647 const struct got_error *err = NULL;
4648 char *parent_path = NULL;
4649 struct got_fileindex_entry *ie;
4650 struct got_commit_object *base_commit = NULL;
4651 struct got_tree_object *tree = NULL;
4652 struct got_object_id *tree_id = NULL;
4653 const struct got_tree_entry *te = NULL;
4654 char *tree_path = NULL, *te_name;
4655 char *ondisk_path = NULL, *path_content = NULL;
4656 struct got_blob_object *blob = NULL;
4657 int fd = -1;
4659 /* Reverting a staged deletion is a no-op. */
4660 if (status == GOT_STATUS_DELETE &&
4661 staged_status != GOT_STATUS_NO_CHANGE)
4662 return NULL;
4664 if (status == GOT_STATUS_UNVERSIONED)
4665 return (*a->progress_cb)(a->progress_arg,
4666 GOT_STATUS_UNVERSIONED, relpath);
4668 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4669 if (ie == NULL)
4670 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4672 /* Construct in-repository path of tree which contains this blob. */
4673 err = got_path_dirname(&parent_path, ie->path);
4674 if (err) {
4675 if (err->code != GOT_ERR_BAD_PATH)
4676 goto done;
4677 parent_path = strdup("/");
4678 if (parent_path == NULL) {
4679 err = got_error_from_errno("strdup");
4680 goto done;
4683 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4684 tree_path = strdup(parent_path);
4685 if (tree_path == NULL) {
4686 err = got_error_from_errno("strdup");
4687 goto done;
4689 } else {
4690 if (got_path_is_root_dir(parent_path)) {
4691 tree_path = strdup(a->worktree->path_prefix);
4692 if (tree_path == NULL) {
4693 err = got_error_from_errno("strdup");
4694 goto done;
4696 } else {
4697 if (asprintf(&tree_path, "%s/%s",
4698 a->worktree->path_prefix, parent_path) == -1) {
4699 err = got_error_from_errno("asprintf");
4700 goto done;
4705 err = got_object_open_as_commit(&base_commit, a->repo,
4706 a->worktree->base_commit_id);
4707 if (err)
4708 goto done;
4710 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4711 if (err) {
4712 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4713 (status == GOT_STATUS_ADD ||
4714 staged_status == GOT_STATUS_ADD)))
4715 goto done;
4716 } else {
4717 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4718 if (err)
4719 goto done;
4721 err = got_path_basename(&te_name, ie->path);
4722 if (err)
4723 goto done;
4725 te = got_object_tree_find_entry(tree, te_name);
4726 free(te_name);
4727 if (te == NULL && status != GOT_STATUS_ADD &&
4728 staged_status != GOT_STATUS_ADD) {
4729 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4730 goto done;
4734 switch (status) {
4735 case GOT_STATUS_ADD:
4736 if (a->patch_cb) {
4737 int choice = GOT_PATCH_CHOICE_NONE;
4738 err = (*a->patch_cb)(&choice, a->patch_arg,
4739 status, ie->path, NULL, 1, 1);
4740 if (err)
4741 goto done;
4742 if (choice != GOT_PATCH_CHOICE_YES)
4743 break;
4745 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4746 ie->path);
4747 if (err)
4748 goto done;
4749 got_fileindex_entry_remove(a->fileindex, ie);
4750 if (a->unlink_added_files) {
4751 if (asprintf(&ondisk_path, "%s/%s",
4752 got_worktree_get_root_path(a->worktree),
4753 relpath) == -1) {
4754 err = got_error_from_errno("asprintf");
4755 goto done;
4757 if (unlink(ondisk_path) == -1) {
4758 err = got_error_from_errno2("unlink",
4759 ondisk_path);
4760 break;
4763 break;
4764 case GOT_STATUS_DELETE:
4765 if (a->patch_cb) {
4766 int choice = GOT_PATCH_CHOICE_NONE;
4767 err = (*a->patch_cb)(&choice, a->patch_arg,
4768 status, ie->path, NULL, 1, 1);
4769 if (err)
4770 goto done;
4771 if (choice != GOT_PATCH_CHOICE_YES)
4772 break;
4774 /* fall through */
4775 case GOT_STATUS_MODIFY:
4776 case GOT_STATUS_MODE_CHANGE:
4777 case GOT_STATUS_CONFLICT:
4778 case GOT_STATUS_MISSING: {
4779 struct got_object_id id;
4780 if (staged_status == GOT_STATUS_ADD ||
4781 staged_status == GOT_STATUS_MODIFY)
4782 got_fileindex_entry_get_staged_blob_id(&id, ie);
4783 else
4784 got_fileindex_entry_get_blob_id(&id, ie);
4785 fd = got_opentempfd();
4786 if (fd == -1) {
4787 err = got_error_from_errno("got_opentempfd");
4788 goto done;
4791 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4792 if (err)
4793 goto done;
4795 if (asprintf(&ondisk_path, "%s/%s",
4796 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4797 err = got_error_from_errno("asprintf");
4798 goto done;
4801 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4802 status == GOT_STATUS_CONFLICT)) {
4803 int is_bad_symlink = 0;
4804 err = create_patched_content(&path_content, 1, &id,
4805 ondisk_path, dirfd, de_name, ie->path, a->repo,
4806 a->patch_cb, a->patch_arg);
4807 if (err || path_content == NULL)
4808 break;
4809 if (te && S_ISLNK(te->mode)) {
4810 if (unlink(path_content) == -1) {
4811 err = got_error_from_errno2("unlink",
4812 path_content);
4813 break;
4815 err = install_symlink(&is_bad_symlink,
4816 a->worktree, ondisk_path, ie->path,
4817 blob, 0, 1, 0, 0, a->repo,
4818 a->progress_cb, a->progress_arg);
4819 } else {
4820 if (rename(path_content, ondisk_path) == -1) {
4821 err = got_error_from_errno3("rename",
4822 path_content, ondisk_path);
4823 goto done;
4826 } else {
4827 int is_bad_symlink = 0;
4828 if (te && S_ISLNK(te->mode)) {
4829 err = install_symlink(&is_bad_symlink,
4830 a->worktree, ondisk_path, ie->path,
4831 blob, 0, 1, 0, 0, a->repo,
4832 a->progress_cb, a->progress_arg);
4833 } else {
4834 err = install_blob(a->worktree, ondisk_path,
4835 ie->path,
4836 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4837 got_fileindex_perms_to_st(ie), blob,
4838 0, 1, 0, 0, a->repo,
4839 a->progress_cb, a->progress_arg);
4841 if (err)
4842 goto done;
4843 if (status == GOT_STATUS_DELETE ||
4844 status == GOT_STATUS_MODE_CHANGE) {
4845 err = got_fileindex_entry_update(ie,
4846 a->worktree->root_fd, relpath,
4847 blob->id.hash,
4848 a->worktree->base_commit_id->hash, 1);
4849 if (err)
4850 goto done;
4852 if (is_bad_symlink) {
4853 got_fileindex_entry_filetype_set(ie,
4854 GOT_FILEIDX_MODE_BAD_SYMLINK);
4857 break;
4859 default:
4860 break;
4862 done:
4863 free(ondisk_path);
4864 free(path_content);
4865 free(parent_path);
4866 free(tree_path);
4867 if (fd != -1 && close(fd) == -1 && err == NULL)
4868 err = got_error_from_errno("close");
4869 if (blob)
4870 got_object_blob_close(blob);
4871 if (tree)
4872 got_object_tree_close(tree);
4873 free(tree_id);
4874 if (base_commit)
4875 got_object_commit_close(base_commit);
4876 return err;
4879 const struct got_error *
4880 got_worktree_revert(struct got_worktree *worktree,
4881 struct got_pathlist_head *paths,
4882 got_worktree_checkout_cb progress_cb, void *progress_arg,
4883 got_worktree_patch_cb patch_cb, void *patch_arg,
4884 struct got_repository *repo)
4886 struct got_fileindex *fileindex = NULL;
4887 char *fileindex_path = NULL;
4888 const struct got_error *err = NULL, *unlockerr = NULL;
4889 const struct got_error *sync_err = NULL;
4890 struct got_pathlist_entry *pe;
4891 struct revert_file_args rfa;
4893 err = lock_worktree(worktree, LOCK_EX);
4894 if (err)
4895 return err;
4897 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4898 if (err)
4899 goto done;
4901 rfa.worktree = worktree;
4902 rfa.fileindex = fileindex;
4903 rfa.progress_cb = progress_cb;
4904 rfa.progress_arg = progress_arg;
4905 rfa.patch_cb = patch_cb;
4906 rfa.patch_arg = patch_arg;
4907 rfa.repo = repo;
4908 rfa.unlink_added_files = 0;
4909 TAILQ_FOREACH(pe, paths, entry) {
4910 err = worktree_status(worktree, pe->path, fileindex, repo,
4911 revert_file, &rfa, NULL, NULL, 1, 0);
4912 if (err)
4913 break;
4915 sync_err = sync_fileindex(fileindex, fileindex_path);
4916 if (sync_err && err == NULL)
4917 err = sync_err;
4918 done:
4919 free(fileindex_path);
4920 if (fileindex)
4921 got_fileindex_free(fileindex);
4922 unlockerr = lock_worktree(worktree, LOCK_SH);
4923 if (unlockerr && err == NULL)
4924 err = unlockerr;
4925 return err;
4928 static void
4929 free_commitable(struct got_commitable *ct)
4931 free(ct->path);
4932 free(ct->in_repo_path);
4933 free(ct->ondisk_path);
4934 free(ct->blob_id);
4935 free(ct->base_blob_id);
4936 free(ct->staged_blob_id);
4937 free(ct->base_commit_id);
4938 free(ct);
4941 struct collect_commitables_arg {
4942 struct got_pathlist_head *commitable_paths;
4943 struct got_repository *repo;
4944 struct got_worktree *worktree;
4945 struct got_fileindex *fileindex;
4946 int have_staged_files;
4947 int allow_bad_symlinks;
4948 int diff_header_shown;
4949 FILE *diff_outfile;
4950 FILE *f1;
4951 FILE *f2;
4955 * Create a file which contains the target path of a symlink so we can feed
4956 * it as content to the diff engine.
4958 static const struct got_error *
4959 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4960 const char *abspath)
4962 const struct got_error *err = NULL;
4963 char target_path[PATH_MAX];
4964 ssize_t target_len, outlen;
4966 *fd = -1;
4968 if (dirfd != -1) {
4969 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4970 if (target_len == -1)
4971 return got_error_from_errno2("readlinkat", abspath);
4972 } else {
4973 target_len = readlink(abspath, target_path, PATH_MAX);
4974 if (target_len == -1)
4975 return got_error_from_errno2("readlink", abspath);
4978 *fd = got_opentempfd();
4979 if (*fd == -1)
4980 return got_error_from_errno("got_opentempfd");
4982 outlen = write(*fd, target_path, target_len);
4983 if (outlen == -1) {
4984 err = got_error_from_errno("got_opentempfd");
4985 goto done;
4988 if (lseek(*fd, 0, SEEK_SET) == -1) {
4989 err = got_error_from_errno2("lseek", abspath);
4990 goto done;
4992 done:
4993 if (err) {
4994 close(*fd);
4995 *fd = -1;
4997 return err;
5000 static const struct got_error *
5001 append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5002 FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5003 int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5005 const struct got_error *err = NULL;
5006 struct got_blob_object *blob1 = NULL;
5007 int fd = -1, fd1 = -1, fd2 = -1;
5008 FILE *ondisk_file = NULL;
5009 char *label1 = NULL;
5010 struct stat sb;
5011 off_t size1 = 0;
5012 int f2_exists = 0;
5013 char *id_str = NULL;
5015 memset(&sb, 0, sizeof(sb));
5017 if (diff_staged) {
5018 if (ct->staged_status != GOT_STATUS_MODIFY &&
5019 ct->staged_status != GOT_STATUS_ADD &&
5020 ct->staged_status != GOT_STATUS_DELETE)
5021 return NULL;
5022 } else {
5023 if (ct->status != GOT_STATUS_MODIFY &&
5024 ct->status != GOT_STATUS_ADD &&
5025 ct->status != GOT_STATUS_DELETE)
5026 return NULL;
5029 err = got_opentemp_truncate(f1);
5030 if (err)
5031 return got_error_from_errno("got_opentemp_truncate");
5032 err = got_opentemp_truncate(f2);
5033 if (err)
5034 return got_error_from_errno("got_opentemp_truncate");
5036 if (!*diff_header_shown) {
5037 err = got_object_id_str(&id_str, worktree->base_commit_id);
5038 if (err)
5039 return err;
5040 fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5041 got_worktree_get_root_path(worktree));
5042 fprintf(diff_outfile, "commit - %s\n", id_str);
5043 fprintf(diff_outfile, "path + %s%s\n",
5044 got_worktree_get_root_path(worktree),
5045 diff_staged ? " (staged changes)" : "");
5046 *diff_header_shown = 1;
5049 if (diff_staged) {
5050 const char *label1 = NULL, *label2 = NULL;
5051 switch (ct->staged_status) {
5052 case GOT_STATUS_MODIFY:
5053 label1 = ct->path;
5054 label2 = ct->path;
5055 break;
5056 case GOT_STATUS_ADD:
5057 label2 = ct->path;
5058 break;
5059 case GOT_STATUS_DELETE:
5060 label1 = ct->path;
5061 break;
5062 default:
5063 return got_error(GOT_ERR_FILE_STATUS);
5065 fd1 = got_opentempfd();
5066 if (fd1 == -1) {
5067 err = got_error_from_errno("got_opentempfd");
5068 goto done;
5070 fd2 = got_opentempfd();
5071 if (fd2 == -1) {
5072 err = got_error_from_errno("got_opentempfd");
5073 goto done;
5075 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5076 fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5077 label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5078 NULL, repo, diff_outfile);
5079 goto done;
5082 fd1 = got_opentempfd();
5083 if (fd1 == -1) {
5084 err = got_error_from_errno("got_opentempfd");
5085 goto done;
5088 if (ct->status != GOT_STATUS_ADD) {
5089 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5090 8192, fd1);
5091 if (err)
5092 goto done;
5095 if (ct->status != GOT_STATUS_DELETE) {
5096 if (dirfd != -1) {
5097 fd = openat(dirfd, de_name,
5098 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5099 if (fd == -1) {
5100 if (!got_err_open_nofollow_on_symlink()) {
5101 err = got_error_from_errno2("openat",
5102 ct->ondisk_path);
5103 goto done;
5105 err = get_symlink_target_file(&fd, dirfd,
5106 de_name, ct->ondisk_path);
5107 if (err)
5108 goto done;
5110 } else {
5111 fd = open(ct->ondisk_path,
5112 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5113 if (fd == -1) {
5114 if (!got_err_open_nofollow_on_symlink()) {
5115 err = got_error_from_errno2("open",
5116 ct->ondisk_path);
5117 goto done;
5119 err = get_symlink_target_file(&fd, dirfd,
5120 de_name, ct->ondisk_path);
5121 if (err)
5122 goto done;
5125 if (fstatat(fd, ct->ondisk_path, &sb,
5126 AT_SYMLINK_NOFOLLOW) == -1) {
5127 err = got_error_from_errno2("fstatat", ct->ondisk_path);
5128 goto done;
5130 ondisk_file = fdopen(fd, "r");
5131 if (ondisk_file == NULL) {
5132 err = got_error_from_errno2("fdopen", ct->ondisk_path);
5133 goto done;
5135 fd = -1;
5136 f2_exists = 1;
5139 if (blob1) {
5140 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5141 f1, blob1);
5142 if (err)
5143 goto done;
5146 err = got_diff_blob_file(blob1, f1, size1, label1,
5147 ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5148 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5149 done:
5150 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5151 err = got_error_from_errno("close");
5152 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5153 err = got_error_from_errno("close");
5154 if (blob1)
5155 got_object_blob_close(blob1);
5156 if (fd != -1 && close(fd) == -1 && err == NULL)
5157 err = got_error_from_errno("close");
5158 if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5159 err = got_error_from_errno("fclose");
5160 return err;
5163 static const struct got_error *
5164 collect_commitables(void *arg, unsigned char status,
5165 unsigned char staged_status, const char *relpath,
5166 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5167 struct got_object_id *commit_id, int dirfd, const char *de_name)
5169 struct collect_commitables_arg *a = arg;
5170 const struct got_error *err = NULL;
5171 struct got_commitable *ct = NULL;
5172 struct got_pathlist_entry *new = NULL;
5173 char *parent_path = NULL, *path = NULL;
5174 struct stat sb;
5176 if (a->have_staged_files) {
5177 if (staged_status != GOT_STATUS_MODIFY &&
5178 staged_status != GOT_STATUS_ADD &&
5179 staged_status != GOT_STATUS_DELETE)
5180 return NULL;
5181 } else {
5182 if (status == GOT_STATUS_CONFLICT)
5183 return got_error(GOT_ERR_COMMIT_CONFLICT);
5185 if (status != GOT_STATUS_MODIFY &&
5186 status != GOT_STATUS_MODE_CHANGE &&
5187 status != GOT_STATUS_ADD &&
5188 status != GOT_STATUS_DELETE)
5189 return NULL;
5192 if (asprintf(&path, "/%s", relpath) == -1) {
5193 err = got_error_from_errno("asprintf");
5194 goto done;
5196 if (strcmp(path, "/") == 0) {
5197 parent_path = strdup("");
5198 if (parent_path == NULL)
5199 return got_error_from_errno("strdup");
5200 } else {
5201 err = got_path_dirname(&parent_path, path);
5202 if (err)
5203 return err;
5206 ct = calloc(1, sizeof(*ct));
5207 if (ct == NULL) {
5208 err = got_error_from_errno("calloc");
5209 goto done;
5212 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5213 relpath) == -1) {
5214 err = got_error_from_errno("asprintf");
5215 goto done;
5218 if (staged_status == GOT_STATUS_ADD ||
5219 staged_status == GOT_STATUS_MODIFY) {
5220 struct got_fileindex_entry *ie;
5221 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5222 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5223 case GOT_FILEIDX_MODE_REGULAR_FILE:
5224 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5225 ct->mode = S_IFREG;
5226 break;
5227 case GOT_FILEIDX_MODE_SYMLINK:
5228 ct->mode = S_IFLNK;
5229 break;
5230 default:
5231 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5232 goto done;
5234 ct->mode |= got_fileindex_entry_perms_get(ie);
5235 } else if (status != GOT_STATUS_DELETE &&
5236 staged_status != GOT_STATUS_DELETE) {
5237 if (dirfd != -1) {
5238 if (fstatat(dirfd, de_name, &sb,
5239 AT_SYMLINK_NOFOLLOW) == -1) {
5240 err = got_error_from_errno2("fstatat",
5241 ct->ondisk_path);
5242 goto done;
5244 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5245 err = got_error_from_errno2("lstat", ct->ondisk_path);
5246 goto done;
5248 ct->mode = sb.st_mode;
5251 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5252 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5253 relpath) == -1) {
5254 err = got_error_from_errno("asprintf");
5255 goto done;
5258 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5259 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5260 int is_bad_symlink;
5261 char target_path[PATH_MAX];
5262 ssize_t target_len;
5263 target_len = readlink(ct->ondisk_path, target_path,
5264 sizeof(target_path));
5265 if (target_len == -1) {
5266 err = got_error_from_errno2("readlink",
5267 ct->ondisk_path);
5268 goto done;
5270 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5271 target_len, ct->ondisk_path, a->worktree->root_path);
5272 if (err)
5273 goto done;
5274 if (is_bad_symlink) {
5275 err = got_error_path(ct->ondisk_path,
5276 GOT_ERR_BAD_SYMLINK);
5277 goto done;
5282 ct->status = status;
5283 ct->staged_status = staged_status;
5284 ct->blob_id = NULL; /* will be filled in when blob gets created */
5285 if (ct->status != GOT_STATUS_ADD &&
5286 ct->staged_status != GOT_STATUS_ADD) {
5287 ct->base_blob_id = got_object_id_dup(blob_id);
5288 if (ct->base_blob_id == NULL) {
5289 err = got_error_from_errno("got_object_id_dup");
5290 goto done;
5292 ct->base_commit_id = got_object_id_dup(commit_id);
5293 if (ct->base_commit_id == NULL) {
5294 err = got_error_from_errno("got_object_id_dup");
5295 goto done;
5298 if (ct->staged_status == GOT_STATUS_ADD ||
5299 ct->staged_status == GOT_STATUS_MODIFY) {
5300 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5301 if (ct->staged_blob_id == NULL) {
5302 err = got_error_from_errno("got_object_id_dup");
5303 goto done;
5306 ct->path = strdup(path);
5307 if (ct->path == NULL) {
5308 err = got_error_from_errno("strdup");
5309 goto done;
5311 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5312 if (err)
5313 goto done;
5315 if (a->diff_outfile && ct && new != NULL) {
5316 err = append_ct_diff(ct, &a->diff_header_shown,
5317 a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5318 a->have_staged_files, a->repo, a->worktree);
5319 if (err)
5320 goto done;
5322 done:
5323 if (ct && (err || new == NULL))
5324 free_commitable(ct);
5325 free(parent_path);
5326 free(path);
5327 return err;
5330 static const struct got_error *write_tree(struct got_object_id **, int *,
5331 struct got_tree_object *, const char *, struct got_pathlist_head *,
5332 got_worktree_status_cb status_cb, void *status_arg,
5333 struct got_repository *);
5335 static const struct got_error *
5336 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5337 struct got_tree_entry *te, const char *parent_path,
5338 struct got_pathlist_head *commitable_paths,
5339 got_worktree_status_cb status_cb, void *status_arg,
5340 struct got_repository *repo)
5342 const struct got_error *err = NULL;
5343 struct got_tree_object *subtree;
5344 char *subpath;
5346 if (asprintf(&subpath, "%s%s%s", parent_path,
5347 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5348 return got_error_from_errno("asprintf");
5350 err = got_object_open_as_tree(&subtree, repo, &te->id);
5351 if (err)
5352 return err;
5354 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5355 commitable_paths, status_cb, status_arg, repo);
5356 got_object_tree_close(subtree);
5357 free(subpath);
5358 return err;
5361 static const struct got_error *
5362 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5364 const struct got_error *err = NULL;
5365 char *ct_parent_path = NULL;
5367 *match = 0;
5369 if (strchr(ct->in_repo_path, '/') == NULL) {
5370 *match = got_path_is_root_dir(path);
5371 return NULL;
5374 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5375 if (err)
5376 return err;
5377 *match = (strcmp(path, ct_parent_path) == 0);
5378 free(ct_parent_path);
5379 return err;
5382 static mode_t
5383 get_ct_file_mode(struct got_commitable *ct)
5385 if (S_ISLNK(ct->mode))
5386 return S_IFLNK;
5388 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5391 static const struct got_error *
5392 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5393 struct got_tree_entry *te, struct got_commitable *ct)
5395 const struct got_error *err = NULL;
5397 *new_te = NULL;
5399 err = got_object_tree_entry_dup(new_te, te);
5400 if (err)
5401 goto done;
5403 (*new_te)->mode = get_ct_file_mode(ct);
5405 if (ct->staged_status == GOT_STATUS_MODIFY)
5406 memcpy(&(*new_te)->id, ct->staged_blob_id,
5407 sizeof((*new_te)->id));
5408 else
5409 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5410 done:
5411 if (err && *new_te) {
5412 free(*new_te);
5413 *new_te = NULL;
5415 return err;
5418 static const struct got_error *
5419 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5420 struct got_commitable *ct)
5422 const struct got_error *err = NULL;
5423 char *ct_name = NULL;
5425 *new_te = NULL;
5427 *new_te = calloc(1, sizeof(**new_te));
5428 if (*new_te == NULL)
5429 return got_error_from_errno("calloc");
5431 err = got_path_basename(&ct_name, ct->path);
5432 if (err)
5433 goto done;
5434 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5435 sizeof((*new_te)->name)) {
5436 err = got_error(GOT_ERR_NO_SPACE);
5437 goto done;
5440 (*new_te)->mode = get_ct_file_mode(ct);
5442 if (ct->staged_status == GOT_STATUS_ADD)
5443 memcpy(&(*new_te)->id, ct->staged_blob_id,
5444 sizeof((*new_te)->id));
5445 else
5446 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5447 done:
5448 free(ct_name);
5449 if (err && *new_te) {
5450 free(*new_te);
5451 *new_te = NULL;
5453 return err;
5456 static const struct got_error *
5457 insert_tree_entry(struct got_tree_entry *new_te,
5458 struct got_pathlist_head *paths)
5460 const struct got_error *err = NULL;
5461 struct got_pathlist_entry *new_pe;
5463 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5464 if (err)
5465 return err;
5466 if (new_pe == NULL)
5467 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5468 return NULL;
5471 static const struct got_error *
5472 report_ct_status(struct got_commitable *ct,
5473 got_worktree_status_cb status_cb, void *status_arg)
5475 const char *ct_path = ct->path;
5476 unsigned char status;
5478 if (status_cb == NULL) /* no commit progress output desired */
5479 return NULL;
5481 while (ct_path[0] == '/')
5482 ct_path++;
5484 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5485 status = ct->staged_status;
5486 else
5487 status = ct->status;
5489 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5490 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5493 static const struct got_error *
5494 match_modified_subtree(int *modified, struct got_tree_entry *te,
5495 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5497 const struct got_error *err = NULL;
5498 struct got_pathlist_entry *pe;
5499 char *te_path;
5501 *modified = 0;
5503 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5504 got_path_is_root_dir(base_tree_path) ? "" : "/",
5505 te->name) == -1)
5506 return got_error_from_errno("asprintf");
5508 TAILQ_FOREACH(pe, commitable_paths, entry) {
5509 struct got_commitable *ct = pe->data;
5510 *modified = got_path_is_child(ct->in_repo_path, te_path,
5511 strlen(te_path));
5512 if (*modified)
5513 break;
5516 free(te_path);
5517 return err;
5520 static const struct got_error *
5521 match_deleted_or_modified_ct(struct got_commitable **ctp,
5522 struct got_tree_entry *te, const char *base_tree_path,
5523 struct got_pathlist_head *commitable_paths)
5525 const struct got_error *err = NULL;
5526 struct got_pathlist_entry *pe;
5528 *ctp = NULL;
5530 TAILQ_FOREACH(pe, commitable_paths, entry) {
5531 struct got_commitable *ct = pe->data;
5532 char *ct_name = NULL;
5533 int path_matches;
5535 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5536 if (ct->status != GOT_STATUS_MODIFY &&
5537 ct->status != GOT_STATUS_MODE_CHANGE &&
5538 ct->status != GOT_STATUS_DELETE)
5539 continue;
5540 } else {
5541 if (ct->staged_status != GOT_STATUS_MODIFY &&
5542 ct->staged_status != GOT_STATUS_DELETE)
5543 continue;
5546 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5547 continue;
5549 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5550 if (err)
5551 return err;
5552 if (!path_matches)
5553 continue;
5555 err = got_path_basename(&ct_name, pe->path);
5556 if (err)
5557 return err;
5559 if (strcmp(te->name, ct_name) != 0) {
5560 free(ct_name);
5561 continue;
5563 free(ct_name);
5565 *ctp = ct;
5566 break;
5569 return err;
5572 static const struct got_error *
5573 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5574 const char *child_path, const char *path_base_tree,
5575 struct got_pathlist_head *commitable_paths,
5576 got_worktree_status_cb status_cb, void *status_arg,
5577 struct got_repository *repo)
5579 const struct got_error *err = NULL;
5580 struct got_tree_entry *new_te;
5581 char *subtree_path;
5582 struct got_object_id *id = NULL;
5583 int nentries;
5585 *new_tep = NULL;
5587 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5588 got_path_is_root_dir(path_base_tree) ? "" : "/",
5589 child_path) == -1)
5590 return got_error_from_errno("asprintf");
5592 new_te = calloc(1, sizeof(*new_te));
5593 if (new_te == NULL)
5594 return got_error_from_errno("calloc");
5595 new_te->mode = S_IFDIR;
5597 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5598 sizeof(new_te->name)) {
5599 err = got_error(GOT_ERR_NO_SPACE);
5600 goto done;
5602 err = write_tree(&id, &nentries, NULL, subtree_path,
5603 commitable_paths, status_cb, status_arg, repo);
5604 if (err) {
5605 free(new_te);
5606 goto done;
5608 memcpy(&new_te->id, id, sizeof(new_te->id));
5609 done:
5610 free(id);
5611 free(subtree_path);
5612 if (err == NULL)
5613 *new_tep = new_te;
5614 return err;
5617 static const struct got_error *
5618 write_tree(struct got_object_id **new_tree_id, int *nentries,
5619 struct got_tree_object *base_tree, const char *path_base_tree,
5620 struct got_pathlist_head *commitable_paths,
5621 got_worktree_status_cb status_cb, void *status_arg,
5622 struct got_repository *repo)
5624 const struct got_error *err = NULL;
5625 struct got_pathlist_head paths;
5626 struct got_tree_entry *te, *new_te = NULL;
5627 struct got_pathlist_entry *pe;
5629 TAILQ_INIT(&paths);
5630 *nentries = 0;
5632 /* Insert, and recurse into, newly added entries first. */
5633 TAILQ_FOREACH(pe, commitable_paths, entry) {
5634 struct got_commitable *ct = pe->data;
5635 char *child_path = NULL, *slash;
5637 if ((ct->status != GOT_STATUS_ADD &&
5638 ct->staged_status != GOT_STATUS_ADD) ||
5639 (ct->flags & GOT_COMMITABLE_ADDED))
5640 continue;
5642 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5643 strlen(path_base_tree)))
5644 continue;
5646 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5647 ct->in_repo_path);
5648 if (err)
5649 goto done;
5651 slash = strchr(child_path, '/');
5652 if (slash == NULL) {
5653 err = alloc_added_blob_tree_entry(&new_te, ct);
5654 if (err)
5655 goto done;
5656 err = report_ct_status(ct, status_cb, status_arg);
5657 if (err)
5658 goto done;
5659 ct->flags |= GOT_COMMITABLE_ADDED;
5660 err = insert_tree_entry(new_te, &paths);
5661 if (err)
5662 goto done;
5663 (*nentries)++;
5664 } else {
5665 *slash = '\0'; /* trim trailing path components */
5666 if (base_tree == NULL ||
5667 got_object_tree_find_entry(base_tree, child_path)
5668 == NULL) {
5669 err = make_subtree_for_added_blob(&new_te,
5670 child_path, path_base_tree,
5671 commitable_paths, status_cb, status_arg,
5672 repo);
5673 if (err)
5674 goto done;
5675 err = insert_tree_entry(new_te, &paths);
5676 if (err)
5677 goto done;
5678 (*nentries)++;
5683 if (base_tree) {
5684 int i, nbase_entries;
5685 /* Handle modified and deleted entries. */
5686 nbase_entries = got_object_tree_get_nentries(base_tree);
5687 for (i = 0; i < nbase_entries; i++) {
5688 struct got_commitable *ct = NULL;
5690 te = got_object_tree_get_entry(base_tree, i);
5691 if (got_object_tree_entry_is_submodule(te)) {
5692 /* Entry is a submodule; just copy it. */
5693 err = got_object_tree_entry_dup(&new_te, te);
5694 if (err)
5695 goto done;
5696 err = insert_tree_entry(new_te, &paths);
5697 if (err)
5698 goto done;
5699 (*nentries)++;
5700 continue;
5703 if (S_ISDIR(te->mode)) {
5704 int modified;
5705 err = got_object_tree_entry_dup(&new_te, te);
5706 if (err)
5707 goto done;
5708 err = match_modified_subtree(&modified, te,
5709 path_base_tree, commitable_paths);
5710 if (err)
5711 goto done;
5712 /* Avoid recursion into unmodified subtrees. */
5713 if (modified) {
5714 struct got_object_id *new_id;
5715 int nsubentries;
5716 err = write_subtree(&new_id,
5717 &nsubentries, te,
5718 path_base_tree, commitable_paths,
5719 status_cb, status_arg, repo);
5720 if (err)
5721 goto done;
5722 if (nsubentries == 0) {
5723 /* All entries were deleted. */
5724 free(new_id);
5725 continue;
5727 memcpy(&new_te->id, new_id,
5728 sizeof(new_te->id));
5729 free(new_id);
5731 err = insert_tree_entry(new_te, &paths);
5732 if (err)
5733 goto done;
5734 (*nentries)++;
5735 continue;
5738 err = match_deleted_or_modified_ct(&ct, te,
5739 path_base_tree, commitable_paths);
5740 if (err)
5741 goto done;
5742 if (ct) {
5743 /* NB: Deleted entries get dropped here. */
5744 if (ct->status == GOT_STATUS_MODIFY ||
5745 ct->status == GOT_STATUS_MODE_CHANGE ||
5746 ct->staged_status == GOT_STATUS_MODIFY) {
5747 err = alloc_modified_blob_tree_entry(
5748 &new_te, te, ct);
5749 if (err)
5750 goto done;
5751 err = insert_tree_entry(new_te, &paths);
5752 if (err)
5753 goto done;
5754 (*nentries)++;
5756 err = report_ct_status(ct, status_cb,
5757 status_arg);
5758 if (err)
5759 goto done;
5760 } else {
5761 /* Entry is unchanged; just copy it. */
5762 err = got_object_tree_entry_dup(&new_te, te);
5763 if (err)
5764 goto done;
5765 err = insert_tree_entry(new_te, &paths);
5766 if (err)
5767 goto done;
5768 (*nentries)++;
5773 /* Write new list of entries; deleted entries have been dropped. */
5774 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5775 done:
5776 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
5777 return err;
5780 static const struct got_error *
5781 update_fileindex_after_commit(struct got_worktree *worktree,
5782 struct got_pathlist_head *commitable_paths,
5783 struct got_object_id *new_base_commit_id,
5784 struct got_fileindex *fileindex, int have_staged_files)
5786 const struct got_error *err = NULL;
5787 struct got_pathlist_entry *pe;
5788 char *relpath = NULL;
5790 TAILQ_FOREACH(pe, commitable_paths, entry) {
5791 struct got_fileindex_entry *ie;
5792 struct got_commitable *ct = pe->data;
5794 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5796 err = got_path_skip_common_ancestor(&relpath,
5797 worktree->root_path, ct->ondisk_path);
5798 if (err)
5799 goto done;
5801 if (ie) {
5802 if (ct->status == GOT_STATUS_DELETE ||
5803 ct->staged_status == GOT_STATUS_DELETE) {
5804 got_fileindex_entry_remove(fileindex, ie);
5805 } else if (ct->staged_status == GOT_STATUS_ADD ||
5806 ct->staged_status == GOT_STATUS_MODIFY) {
5807 got_fileindex_entry_stage_set(ie,
5808 GOT_FILEIDX_STAGE_NONE);
5809 got_fileindex_entry_staged_filetype_set(ie, 0);
5811 err = got_fileindex_entry_update(ie,
5812 worktree->root_fd, relpath,
5813 ct->staged_blob_id->hash,
5814 new_base_commit_id->hash,
5815 !have_staged_files);
5816 } else
5817 err = got_fileindex_entry_update(ie,
5818 worktree->root_fd, relpath,
5819 ct->blob_id->hash,
5820 new_base_commit_id->hash,
5821 !have_staged_files);
5822 } else {
5823 err = got_fileindex_entry_alloc(&ie, pe->path);
5824 if (err)
5825 goto done;
5826 err = got_fileindex_entry_update(ie,
5827 worktree->root_fd, relpath, ct->blob_id->hash,
5828 new_base_commit_id->hash, 1);
5829 if (err) {
5830 got_fileindex_entry_free(ie);
5831 goto done;
5833 err = got_fileindex_entry_add(fileindex, ie);
5834 if (err) {
5835 got_fileindex_entry_free(ie);
5836 goto done;
5839 free(relpath);
5840 relpath = NULL;
5842 done:
5843 free(relpath);
5844 return err;
5848 static const struct got_error *
5849 check_out_of_date(const char *in_repo_path, unsigned char status,
5850 unsigned char staged_status, struct got_object_id *base_blob_id,
5851 struct got_object_id *base_commit_id,
5852 struct got_object_id *head_commit_id, struct got_repository *repo,
5853 int ood_errcode)
5855 const struct got_error *err = NULL;
5856 struct got_commit_object *commit = NULL;
5857 struct got_object_id *id = NULL;
5859 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5860 /* Trivial case: base commit == head commit */
5861 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5862 return NULL;
5864 * Ensure file content which local changes were based
5865 * on matches file content in the branch head.
5867 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5868 if (err)
5869 goto done;
5870 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5871 if (err) {
5872 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5873 err = got_error(ood_errcode);
5874 goto done;
5875 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5876 err = got_error(ood_errcode);
5877 } else {
5878 /* Require that added files don't exist in the branch head. */
5879 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5880 if (err)
5881 goto done;
5882 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5883 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5884 goto done;
5885 err = id ? got_error(ood_errcode) : NULL;
5887 done:
5888 free(id);
5889 if (commit)
5890 got_object_commit_close(commit);
5891 return err;
5894 static const struct got_error *
5895 commit_worktree(struct got_object_id **new_commit_id,
5896 struct got_pathlist_head *commitable_paths,
5897 struct got_object_id *head_commit_id,
5898 struct got_object_id *parent_id2,
5899 struct got_worktree *worktree,
5900 const char *author, const char *committer, char *diff_path,
5901 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5902 got_worktree_status_cb status_cb, void *status_arg,
5903 struct got_repository *repo)
5905 const struct got_error *err = NULL, *unlockerr = NULL;
5906 struct got_pathlist_entry *pe;
5907 const char *head_ref_name = NULL;
5908 struct got_commit_object *head_commit = NULL;
5909 struct got_reference *head_ref2 = NULL;
5910 struct got_object_id *head_commit_id2 = NULL;
5911 struct got_tree_object *head_tree = NULL;
5912 struct got_object_id *new_tree_id = NULL;
5913 int nentries, nparents = 0;
5914 struct got_object_id_queue parent_ids;
5915 struct got_object_qid *pid = NULL;
5916 char *logmsg = NULL;
5917 time_t timestamp;
5919 *new_commit_id = NULL;
5921 STAILQ_INIT(&parent_ids);
5923 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5924 if (err)
5925 goto done;
5927 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5928 if (err)
5929 goto done;
5931 if (commit_msg_cb != NULL) {
5932 err = commit_msg_cb(commitable_paths, diff_path,
5933 &logmsg, commit_arg);
5934 if (err)
5935 goto done;
5938 if (logmsg == NULL || strlen(logmsg) == 0) {
5939 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5940 goto done;
5943 /* Create blobs from added and modified files and record their IDs. */
5944 TAILQ_FOREACH(pe, commitable_paths, entry) {
5945 struct got_commitable *ct = pe->data;
5946 char *ondisk_path;
5948 /* Blobs for staged files already exist. */
5949 if (ct->staged_status == GOT_STATUS_ADD ||
5950 ct->staged_status == GOT_STATUS_MODIFY)
5951 continue;
5953 if (ct->status != GOT_STATUS_ADD &&
5954 ct->status != GOT_STATUS_MODIFY &&
5955 ct->status != GOT_STATUS_MODE_CHANGE)
5956 continue;
5958 if (asprintf(&ondisk_path, "%s/%s",
5959 worktree->root_path, pe->path) == -1) {
5960 err = got_error_from_errno("asprintf");
5961 goto done;
5963 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5964 free(ondisk_path);
5965 if (err)
5966 goto done;
5969 /* Recursively write new tree objects. */
5970 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5971 commitable_paths, status_cb, status_arg, repo);
5972 if (err)
5973 goto done;
5975 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5976 if (err)
5977 goto done;
5978 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5979 nparents++;
5980 if (parent_id2) {
5981 err = got_object_qid_alloc(&pid, parent_id2);
5982 if (err)
5983 goto done;
5984 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5985 nparents++;
5987 timestamp = time(NULL);
5988 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5989 nparents, author, timestamp, committer, timestamp, logmsg, repo);
5990 if (logmsg != NULL)
5991 free(logmsg);
5992 if (err)
5993 goto done;
5995 /* Check if a concurrent commit to our branch has occurred. */
5996 head_ref_name = got_worktree_get_head_ref_name(worktree);
5997 if (head_ref_name == NULL) {
5998 err = got_error_from_errno("got_worktree_get_head_ref_name");
5999 goto done;
6001 /* Lock the reference here to prevent concurrent modification. */
6002 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6003 if (err)
6004 goto done;
6005 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6006 if (err)
6007 goto done;
6008 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6009 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6010 goto done;
6012 /* Update branch head in repository. */
6013 err = got_ref_change_ref(head_ref2, *new_commit_id);
6014 if (err)
6015 goto done;
6016 err = got_ref_write(head_ref2, repo);
6017 if (err)
6018 goto done;
6020 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6021 if (err)
6022 goto done;
6024 err = ref_base_commit(worktree, repo);
6025 if (err)
6026 goto done;
6027 done:
6028 got_object_id_queue_free(&parent_ids);
6029 if (head_tree)
6030 got_object_tree_close(head_tree);
6031 if (head_commit)
6032 got_object_commit_close(head_commit);
6033 free(head_commit_id2);
6034 if (head_ref2) {
6035 unlockerr = got_ref_unlock(head_ref2);
6036 if (unlockerr && err == NULL)
6037 err = unlockerr;
6038 got_ref_close(head_ref2);
6040 return err;
6043 static const struct got_error *
6044 check_path_is_commitable(const char *path,
6045 struct got_pathlist_head *commitable_paths)
6047 struct got_pathlist_entry *cpe = NULL;
6048 size_t path_len = strlen(path);
6050 TAILQ_FOREACH(cpe, commitable_paths, entry) {
6051 struct got_commitable *ct = cpe->data;
6052 const char *ct_path = ct->path;
6054 while (ct_path[0] == '/')
6055 ct_path++;
6057 if (strcmp(path, ct_path) == 0 ||
6058 got_path_is_child(ct_path, path, path_len))
6059 break;
6062 if (cpe == NULL)
6063 return got_error_path(path, GOT_ERR_BAD_PATH);
6065 return NULL;
6068 static const struct got_error *
6069 check_staged_file(void *arg, struct got_fileindex_entry *ie)
6071 int *have_staged_files = arg;
6073 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6074 *have_staged_files = 1;
6075 return got_error(GOT_ERR_CANCELLED);
6078 return NULL;
6081 static const struct got_error *
6082 check_non_staged_files(struct got_fileindex *fileindex,
6083 struct got_pathlist_head *paths)
6085 struct got_pathlist_entry *pe;
6086 struct got_fileindex_entry *ie;
6088 TAILQ_FOREACH(pe, paths, entry) {
6089 if (pe->path[0] == '\0')
6090 continue;
6091 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6092 if (ie == NULL)
6093 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6094 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6095 return got_error_path(pe->path,
6096 GOT_ERR_FILE_NOT_STAGED);
6099 return NULL;
6102 const struct got_error *
6103 got_worktree_commit(struct got_object_id **new_commit_id,
6104 struct got_worktree *worktree, struct got_pathlist_head *paths,
6105 const char *author, const char *committer, int allow_bad_symlinks,
6106 int show_diff, got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6107 got_worktree_status_cb status_cb, void *status_arg,
6108 struct got_repository *repo)
6110 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6111 struct got_fileindex *fileindex = NULL;
6112 char *fileindex_path = NULL;
6113 struct got_pathlist_head commitable_paths;
6114 struct collect_commitables_arg cc_arg;
6115 struct got_pathlist_entry *pe;
6116 struct got_reference *head_ref = NULL;
6117 struct got_object_id *head_commit_id = NULL;
6118 char *diff_path = NULL;
6119 int have_staged_files = 0;
6121 *new_commit_id = NULL;
6123 memset(&cc_arg, 0, sizeof(cc_arg));
6124 TAILQ_INIT(&commitable_paths);
6126 err = lock_worktree(worktree, LOCK_EX);
6127 if (err)
6128 goto done;
6130 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6131 if (err)
6132 goto done;
6134 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6135 if (err)
6136 goto done;
6138 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6139 if (err)
6140 goto done;
6142 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6143 &have_staged_files);
6144 if (err && err->code != GOT_ERR_CANCELLED)
6145 goto done;
6146 if (have_staged_files) {
6147 err = check_non_staged_files(fileindex, paths);
6148 if (err)
6149 goto done;
6152 cc_arg.commitable_paths = &commitable_paths;
6153 cc_arg.worktree = worktree;
6154 cc_arg.fileindex = fileindex;
6155 cc_arg.repo = repo;
6156 cc_arg.have_staged_files = have_staged_files;
6157 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6158 cc_arg.diff_header_shown = 0;
6159 if (show_diff) {
6160 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6161 GOT_TMPDIR_STR "/got", ".diff");
6162 if (err)
6163 goto done;
6164 cc_arg.f1 = got_opentemp();
6165 if (cc_arg.f1 == NULL) {
6166 err = got_error_from_errno("got_opentemp");
6167 goto done;
6169 cc_arg.f2 = got_opentemp();
6170 if (cc_arg.f2 == NULL) {
6171 err = got_error_from_errno("got_opentemp");
6172 goto done;
6176 TAILQ_FOREACH(pe, paths, entry) {
6177 err = worktree_status(worktree, pe->path, fileindex, repo,
6178 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6179 if (err)
6180 goto done;
6183 if (show_diff) {
6184 if (fflush(cc_arg.diff_outfile) == EOF) {
6185 err = got_error_from_errno("fflush");
6186 goto done;
6190 if (TAILQ_EMPTY(&commitable_paths)) {
6191 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6192 goto done;
6195 TAILQ_FOREACH(pe, paths, entry) {
6196 err = check_path_is_commitable(pe->path, &commitable_paths);
6197 if (err)
6198 goto done;
6201 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6202 struct got_commitable *ct = pe->data;
6203 const char *ct_path = ct->in_repo_path;
6205 while (ct_path[0] == '/')
6206 ct_path++;
6207 err = check_out_of_date(ct_path, ct->status,
6208 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6209 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6210 if (err)
6211 goto done;
6215 err = commit_worktree(new_commit_id, &commitable_paths,
6216 head_commit_id, NULL, worktree, author, committer,
6217 (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6218 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6219 if (err)
6220 goto done;
6222 err = update_fileindex_after_commit(worktree, &commitable_paths,
6223 *new_commit_id, fileindex, have_staged_files);
6224 sync_err = sync_fileindex(fileindex, fileindex_path);
6225 if (sync_err && err == NULL)
6226 err = sync_err;
6227 done:
6228 if (fileindex)
6229 got_fileindex_free(fileindex);
6230 free(fileindex_path);
6231 unlockerr = lock_worktree(worktree, LOCK_SH);
6232 if (unlockerr && err == NULL)
6233 err = unlockerr;
6234 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6235 struct got_commitable *ct = pe->data;
6237 free_commitable(ct);
6239 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6240 if (diff_path && unlink(diff_path) == -1 && err == NULL)
6241 err = got_error_from_errno2("unlink", diff_path);
6242 free(diff_path);
6243 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6244 err == NULL)
6245 err = got_error_from_errno("fclose");
6246 return err;
6249 const char *
6250 got_commitable_get_path(struct got_commitable *ct)
6252 return ct->path;
6255 unsigned int
6256 got_commitable_get_status(struct got_commitable *ct)
6258 return ct->status;
6261 struct check_rebase_ok_arg {
6262 struct got_worktree *worktree;
6263 struct got_repository *repo;
6266 static const struct got_error *
6267 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6269 const struct got_error *err = NULL;
6270 struct check_rebase_ok_arg *a = arg;
6271 unsigned char status;
6272 struct stat sb;
6273 char *ondisk_path;
6275 /* Reject rebase of a work tree with mixed base commits. */
6276 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->hash,
6277 SHA1_DIGEST_LENGTH))
6278 return got_error(GOT_ERR_MIXED_COMMITS);
6280 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6281 == -1)
6282 return got_error_from_errno("asprintf");
6284 /* Reject rebase of a work tree with modified or staged files. */
6285 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6286 free(ondisk_path);
6287 if (err)
6288 return err;
6290 if (status != GOT_STATUS_NO_CHANGE)
6291 return got_error(GOT_ERR_MODIFIED);
6292 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6293 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6295 return NULL;
6298 const struct got_error *
6299 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6300 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6301 struct got_worktree *worktree, struct got_reference *branch,
6302 struct got_repository *repo)
6304 const struct got_error *err = NULL;
6305 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6306 char *branch_ref_name = NULL;
6307 char *fileindex_path = NULL;
6308 struct check_rebase_ok_arg ok_arg;
6309 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6310 struct got_object_id *wt_branch_tip = NULL;
6312 *new_base_branch_ref = NULL;
6313 *tmp_branch = NULL;
6314 *fileindex = NULL;
6316 err = lock_worktree(worktree, LOCK_EX);
6317 if (err)
6318 return err;
6320 err = open_fileindex(fileindex, &fileindex_path, worktree);
6321 if (err)
6322 goto done;
6324 ok_arg.worktree = worktree;
6325 ok_arg.repo = repo;
6326 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6327 &ok_arg);
6328 if (err)
6329 goto done;
6331 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6332 if (err)
6333 goto done;
6335 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6336 if (err)
6337 goto done;
6339 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6340 if (err)
6341 goto done;
6343 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6344 0);
6345 if (err)
6346 goto done;
6348 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6349 if (err)
6350 goto done;
6351 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6352 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6353 goto done;
6356 err = got_ref_alloc_symref(new_base_branch_ref,
6357 new_base_branch_ref_name, wt_branch);
6358 if (err)
6359 goto done;
6360 err = got_ref_write(*new_base_branch_ref, repo);
6361 if (err)
6362 goto done;
6364 /* TODO Lock original branch's ref while rebasing? */
6366 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6367 if (err)
6368 goto done;
6370 err = got_ref_write(branch_ref, repo);
6371 if (err)
6372 goto done;
6374 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6375 worktree->base_commit_id);
6376 if (err)
6377 goto done;
6378 err = got_ref_write(*tmp_branch, repo);
6379 if (err)
6380 goto done;
6382 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6383 if (err)
6384 goto done;
6385 done:
6386 free(fileindex_path);
6387 free(tmp_branch_name);
6388 free(new_base_branch_ref_name);
6389 free(branch_ref_name);
6390 if (branch_ref)
6391 got_ref_close(branch_ref);
6392 if (wt_branch)
6393 got_ref_close(wt_branch);
6394 free(wt_branch_tip);
6395 if (err) {
6396 if (*new_base_branch_ref) {
6397 got_ref_close(*new_base_branch_ref);
6398 *new_base_branch_ref = NULL;
6400 if (*tmp_branch) {
6401 got_ref_close(*tmp_branch);
6402 *tmp_branch = NULL;
6404 if (*fileindex) {
6405 got_fileindex_free(*fileindex);
6406 *fileindex = NULL;
6408 lock_worktree(worktree, LOCK_SH);
6410 return err;
6413 const struct got_error *
6414 got_worktree_rebase_continue(struct got_object_id **commit_id,
6415 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6416 struct got_reference **branch, struct got_fileindex **fileindex,
6417 struct got_worktree *worktree, struct got_repository *repo)
6419 const struct got_error *err;
6420 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6421 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6422 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6423 char *fileindex_path = NULL;
6424 int have_staged_files = 0;
6426 *commit_id = NULL;
6427 *new_base_branch = NULL;
6428 *tmp_branch = NULL;
6429 *branch = NULL;
6430 *fileindex = NULL;
6432 err = lock_worktree(worktree, LOCK_EX);
6433 if (err)
6434 return err;
6436 err = open_fileindex(fileindex, &fileindex_path, worktree);
6437 if (err)
6438 goto done;
6440 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6441 &have_staged_files);
6442 if (err && err->code != GOT_ERR_CANCELLED)
6443 goto done;
6444 if (have_staged_files) {
6445 err = got_error(GOT_ERR_STAGED_PATHS);
6446 goto done;
6449 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6450 if (err)
6451 goto done;
6453 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6454 if (err)
6455 goto done;
6457 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6458 if (err)
6459 goto done;
6461 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6462 if (err)
6463 goto done;
6465 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6466 if (err)
6467 goto done;
6469 err = got_ref_open(branch, repo,
6470 got_ref_get_symref_target(branch_ref), 0);
6471 if (err)
6472 goto done;
6474 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6475 if (err)
6476 goto done;
6478 err = got_ref_resolve(commit_id, repo, commit_ref);
6479 if (err)
6480 goto done;
6482 err = got_ref_open(new_base_branch, repo,
6483 new_base_branch_ref_name, 0);
6484 if (err)
6485 goto done;
6487 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6488 if (err)
6489 goto done;
6490 done:
6491 free(commit_ref_name);
6492 free(branch_ref_name);
6493 free(fileindex_path);
6494 if (commit_ref)
6495 got_ref_close(commit_ref);
6496 if (branch_ref)
6497 got_ref_close(branch_ref);
6498 if (err) {
6499 free(*commit_id);
6500 *commit_id = NULL;
6501 if (*tmp_branch) {
6502 got_ref_close(*tmp_branch);
6503 *tmp_branch = NULL;
6505 if (*new_base_branch) {
6506 got_ref_close(*new_base_branch);
6507 *new_base_branch = NULL;
6509 if (*branch) {
6510 got_ref_close(*branch);
6511 *branch = NULL;
6513 if (*fileindex) {
6514 got_fileindex_free(*fileindex);
6515 *fileindex = NULL;
6517 lock_worktree(worktree, LOCK_SH);
6519 return err;
6522 const struct got_error *
6523 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6525 const struct got_error *err;
6526 char *tmp_branch_name = NULL;
6528 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6529 if (err)
6530 return err;
6532 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6533 free(tmp_branch_name);
6534 return NULL;
6537 static const struct got_error *
6538 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6539 const char *diff_path, char **logmsg, void *arg)
6541 *logmsg = arg;
6542 return NULL;
6545 static const struct got_error *
6546 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6547 const char *path, struct got_object_id *blob_id,
6548 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6549 int dirfd, const char *de_name)
6551 return NULL;
6554 struct collect_merged_paths_arg {
6555 got_worktree_checkout_cb progress_cb;
6556 void *progress_arg;
6557 struct got_pathlist_head *merged_paths;
6560 static const struct got_error *
6561 collect_merged_paths(void *arg, unsigned char status, const char *path)
6563 const struct got_error *err;
6564 struct collect_merged_paths_arg *a = arg;
6565 char *p;
6566 struct got_pathlist_entry *new;
6568 err = (*a->progress_cb)(a->progress_arg, status, path);
6569 if (err)
6570 return err;
6572 if (status != GOT_STATUS_MERGE &&
6573 status != GOT_STATUS_ADD &&
6574 status != GOT_STATUS_DELETE &&
6575 status != GOT_STATUS_CONFLICT)
6576 return NULL;
6578 p = strdup(path);
6579 if (p == NULL)
6580 return got_error_from_errno("strdup");
6582 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6583 if (err || new == NULL)
6584 free(p);
6585 return err;
6588 static const struct got_error *
6589 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6590 int is_rebase, struct got_repository *repo)
6592 const struct got_error *err;
6593 struct got_reference *commit_ref = NULL;
6595 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6596 if (err) {
6597 if (err->code != GOT_ERR_NOT_REF)
6598 goto done;
6599 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6600 if (err)
6601 goto done;
6602 err = got_ref_write(commit_ref, repo);
6603 if (err)
6604 goto done;
6605 } else if (is_rebase) {
6606 struct got_object_id *stored_id;
6607 int cmp;
6609 err = got_ref_resolve(&stored_id, repo, commit_ref);
6610 if (err)
6611 goto done;
6612 cmp = got_object_id_cmp(commit_id, stored_id);
6613 free(stored_id);
6614 if (cmp != 0) {
6615 err = got_error(GOT_ERR_REBASE_COMMITID);
6616 goto done;
6619 done:
6620 if (commit_ref)
6621 got_ref_close(commit_ref);
6622 return err;
6625 static const struct got_error *
6626 rebase_merge_files(struct got_pathlist_head *merged_paths,
6627 const char *commit_ref_name, struct got_worktree *worktree,
6628 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6629 struct got_object_id *commit_id, struct got_repository *repo,
6630 got_worktree_checkout_cb progress_cb, void *progress_arg,
6631 got_cancel_cb cancel_cb, void *cancel_arg)
6633 const struct got_error *err;
6634 struct got_reference *commit_ref = NULL;
6635 struct collect_merged_paths_arg cmp_arg;
6636 char *fileindex_path;
6638 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6640 err = get_fileindex_path(&fileindex_path, worktree);
6641 if (err)
6642 return err;
6644 cmp_arg.progress_cb = progress_cb;
6645 cmp_arg.progress_arg = progress_arg;
6646 cmp_arg.merged_paths = merged_paths;
6647 err = merge_files(worktree, fileindex, fileindex_path,
6648 parent_commit_id, commit_id, repo, collect_merged_paths,
6649 &cmp_arg, cancel_cb, cancel_arg);
6650 if (commit_ref)
6651 got_ref_close(commit_ref);
6652 return err;
6655 const struct got_error *
6656 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6657 struct got_worktree *worktree, struct got_fileindex *fileindex,
6658 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6659 struct got_repository *repo,
6660 got_worktree_checkout_cb progress_cb, void *progress_arg,
6661 got_cancel_cb cancel_cb, void *cancel_arg)
6663 const struct got_error *err;
6664 char *commit_ref_name;
6666 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6667 if (err)
6668 return err;
6670 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6671 if (err)
6672 goto done;
6674 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6675 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6676 progress_arg, cancel_cb, cancel_arg);
6677 done:
6678 free(commit_ref_name);
6679 return err;
6682 const struct got_error *
6683 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6684 struct got_worktree *worktree, struct got_fileindex *fileindex,
6685 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6686 struct got_repository *repo,
6687 got_worktree_checkout_cb progress_cb, void *progress_arg,
6688 got_cancel_cb cancel_cb, void *cancel_arg)
6690 const struct got_error *err;
6691 char *commit_ref_name;
6693 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6694 if (err)
6695 return err;
6697 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6698 if (err)
6699 goto done;
6701 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6702 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6703 progress_arg, cancel_cb, cancel_arg);
6704 done:
6705 free(commit_ref_name);
6706 return err;
6709 static const struct got_error *
6710 rebase_commit(struct got_object_id **new_commit_id,
6711 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6712 struct got_worktree *worktree, struct got_fileindex *fileindex,
6713 struct got_reference *tmp_branch, const char *committer,
6714 struct got_commit_object *orig_commit, const char *new_logmsg,
6715 struct got_repository *repo)
6717 const struct got_error *err, *sync_err;
6718 struct got_pathlist_head commitable_paths;
6719 struct collect_commitables_arg cc_arg;
6720 char *fileindex_path = NULL;
6721 struct got_reference *head_ref = NULL;
6722 struct got_object_id *head_commit_id = NULL;
6723 char *logmsg = NULL;
6725 memset(&cc_arg, 0, sizeof(cc_arg));
6726 TAILQ_INIT(&commitable_paths);
6727 *new_commit_id = NULL;
6729 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6731 err = get_fileindex_path(&fileindex_path, worktree);
6732 if (err)
6733 return err;
6735 cc_arg.commitable_paths = &commitable_paths;
6736 cc_arg.worktree = worktree;
6737 cc_arg.repo = repo;
6738 cc_arg.have_staged_files = 0;
6740 * If possible get the status of individual files directly to
6741 * avoid crawling the entire work tree once per rebased commit.
6743 * Ideally, merged_paths would contain a list of commitables
6744 * we could use so we could skip worktree_status() entirely.
6745 * However, we would then need carefully keep track of cumulative
6746 * effects of operations such as file additions and deletions
6747 * in 'got histedit -f' (folding multiple commits into one),
6748 * and this extra complexity is not really worth it.
6750 if (merged_paths) {
6751 struct got_pathlist_entry *pe;
6752 TAILQ_FOREACH(pe, merged_paths, entry) {
6753 err = worktree_status(worktree, pe->path, fileindex,
6754 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6755 0);
6756 if (err)
6757 goto done;
6759 } else {
6760 err = worktree_status(worktree, "", fileindex, repo,
6761 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6762 if (err)
6763 goto done;
6766 if (TAILQ_EMPTY(&commitable_paths)) {
6767 /* No-op change; commit will be elided. */
6768 err = got_ref_delete(commit_ref, repo);
6769 if (err)
6770 goto done;
6771 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6772 goto done;
6775 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6776 if (err)
6777 goto done;
6779 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6780 if (err)
6781 goto done;
6783 if (new_logmsg) {
6784 logmsg = strdup(new_logmsg);
6785 if (logmsg == NULL) {
6786 err = got_error_from_errno("strdup");
6787 goto done;
6789 } else {
6790 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6791 if (err)
6792 goto done;
6795 /* NB: commit_worktree will call free(logmsg) */
6796 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6797 NULL, worktree, got_object_commit_get_author(orig_commit),
6798 committer ? committer :
6799 got_object_commit_get_committer(orig_commit), NULL,
6800 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6801 if (err)
6802 goto done;
6804 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6805 if (err)
6806 goto done;
6808 err = got_ref_delete(commit_ref, repo);
6809 if (err)
6810 goto done;
6812 err = update_fileindex_after_commit(worktree, &commitable_paths,
6813 *new_commit_id, fileindex, 0);
6814 sync_err = sync_fileindex(fileindex, fileindex_path);
6815 if (sync_err && err == NULL)
6816 err = sync_err;
6817 done:
6818 free(fileindex_path);
6819 free(head_commit_id);
6820 if (head_ref)
6821 got_ref_close(head_ref);
6822 if (err) {
6823 free(*new_commit_id);
6824 *new_commit_id = NULL;
6826 return err;
6829 const struct got_error *
6830 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6831 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6832 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6833 const char *committer, struct got_commit_object *orig_commit,
6834 struct got_object_id *orig_commit_id, struct got_repository *repo)
6836 const struct got_error *err;
6837 char *commit_ref_name;
6838 struct got_reference *commit_ref = NULL;
6839 struct got_object_id *commit_id = NULL;
6841 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6842 if (err)
6843 return err;
6845 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6846 if (err)
6847 goto done;
6848 err = got_ref_resolve(&commit_id, repo, commit_ref);
6849 if (err)
6850 goto done;
6851 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6852 err = got_error(GOT_ERR_REBASE_COMMITID);
6853 goto done;
6856 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6857 worktree, fileindex, tmp_branch, committer, orig_commit,
6858 NULL, repo);
6859 done:
6860 if (commit_ref)
6861 got_ref_close(commit_ref);
6862 free(commit_ref_name);
6863 free(commit_id);
6864 return err;
6867 const struct got_error *
6868 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6869 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6870 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6871 const char *committer, struct got_commit_object *orig_commit,
6872 struct got_object_id *orig_commit_id, const char *new_logmsg,
6873 struct got_repository *repo)
6875 const struct got_error *err;
6876 char *commit_ref_name;
6877 struct got_reference *commit_ref = NULL;
6879 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6880 if (err)
6881 return err;
6883 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6884 if (err)
6885 goto done;
6887 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6888 worktree, fileindex, tmp_branch, committer, orig_commit,
6889 new_logmsg, repo);
6890 done:
6891 if (commit_ref)
6892 got_ref_close(commit_ref);
6893 free(commit_ref_name);
6894 return err;
6897 const struct got_error *
6898 got_worktree_rebase_postpone(struct got_worktree *worktree,
6899 struct got_fileindex *fileindex)
6901 if (fileindex)
6902 got_fileindex_free(fileindex);
6903 return lock_worktree(worktree, LOCK_SH);
6906 static const struct got_error *
6907 delete_ref(const char *name, struct got_repository *repo)
6909 const struct got_error *err;
6910 struct got_reference *ref;
6912 err = got_ref_open(&ref, repo, name, 0);
6913 if (err) {
6914 if (err->code == GOT_ERR_NOT_REF)
6915 return NULL;
6916 return err;
6919 err = got_ref_delete(ref, repo);
6920 got_ref_close(ref);
6921 return err;
6924 static const struct got_error *
6925 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6927 const struct got_error *err;
6928 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6929 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6931 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6932 if (err)
6933 goto done;
6934 err = delete_ref(tmp_branch_name, repo);
6935 if (err)
6936 goto done;
6938 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6939 if (err)
6940 goto done;
6941 err = delete_ref(new_base_branch_ref_name, repo);
6942 if (err)
6943 goto done;
6945 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6946 if (err)
6947 goto done;
6948 err = delete_ref(branch_ref_name, repo);
6949 if (err)
6950 goto done;
6952 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6953 if (err)
6954 goto done;
6955 err = delete_ref(commit_ref_name, repo);
6956 if (err)
6957 goto done;
6959 done:
6960 free(tmp_branch_name);
6961 free(new_base_branch_ref_name);
6962 free(branch_ref_name);
6963 free(commit_ref_name);
6964 return err;
6967 static const struct got_error *
6968 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6969 struct got_object_id *new_commit_id, struct got_repository *repo)
6971 const struct got_error *err;
6972 struct got_reference *ref = NULL;
6973 struct got_object_id *old_commit_id = NULL;
6974 const char *branch_name = NULL;
6975 char *new_id_str = NULL;
6976 char *refname = NULL;
6978 branch_name = got_ref_get_name(branch);
6979 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6980 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6981 branch_name += 11;
6983 err = got_object_id_str(&new_id_str, new_commit_id);
6984 if (err)
6985 return err;
6987 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6988 new_id_str) == -1) {
6989 err = got_error_from_errno("asprintf");
6990 goto done;
6993 err = got_ref_resolve(&old_commit_id, repo, branch);
6994 if (err)
6995 goto done;
6997 err = got_ref_alloc(&ref, refname, old_commit_id);
6998 if (err)
6999 goto done;
7001 err = got_ref_write(ref, repo);
7002 done:
7003 free(new_id_str);
7004 free(refname);
7005 free(old_commit_id);
7006 if (ref)
7007 got_ref_close(ref);
7008 return err;
7011 const struct got_error *
7012 got_worktree_rebase_complete(struct got_worktree *worktree,
7013 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7014 struct got_reference *rebased_branch, struct got_repository *repo,
7015 int create_backup)
7017 const struct got_error *err, *unlockerr, *sync_err;
7018 struct got_object_id *new_head_commit_id = NULL;
7019 char *fileindex_path = NULL;
7021 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7022 if (err)
7023 return err;
7025 if (create_backup) {
7026 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7027 rebased_branch, new_head_commit_id, repo);
7028 if (err)
7029 goto done;
7032 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7033 if (err)
7034 goto done;
7036 err = got_ref_write(rebased_branch, repo);
7037 if (err)
7038 goto done;
7040 err = got_worktree_set_head_ref(worktree, rebased_branch);
7041 if (err)
7042 goto done;
7044 err = delete_rebase_refs(worktree, repo);
7045 if (err)
7046 goto done;
7048 err = get_fileindex_path(&fileindex_path, worktree);
7049 if (err)
7050 goto done;
7051 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7052 sync_err = sync_fileindex(fileindex, fileindex_path);
7053 if (sync_err && err == NULL)
7054 err = sync_err;
7055 done:
7056 got_fileindex_free(fileindex);
7057 free(fileindex_path);
7058 free(new_head_commit_id);
7059 unlockerr = lock_worktree(worktree, LOCK_SH);
7060 if (unlockerr && err == NULL)
7061 err = unlockerr;
7062 return err;
7065 const struct got_error *
7066 got_worktree_rebase_abort(struct got_worktree *worktree,
7067 struct got_fileindex *fileindex, struct got_repository *repo,
7068 struct got_reference *new_base_branch,
7069 got_worktree_checkout_cb progress_cb, void *progress_arg)
7071 const struct got_error *err, *unlockerr, *sync_err;
7072 struct got_reference *resolved = NULL;
7073 struct got_object_id *commit_id = NULL;
7074 struct got_commit_object *commit = NULL;
7075 char *fileindex_path = NULL;
7076 struct revert_file_args rfa;
7077 struct got_object_id *tree_id = NULL;
7079 err = lock_worktree(worktree, LOCK_EX);
7080 if (err)
7081 return err;
7083 err = got_object_open_as_commit(&commit, repo,
7084 worktree->base_commit_id);
7085 if (err)
7086 goto done;
7088 err = got_ref_open(&resolved, repo,
7089 got_ref_get_symref_target(new_base_branch), 0);
7090 if (err)
7091 goto done;
7093 err = got_worktree_set_head_ref(worktree, resolved);
7094 if (err)
7095 goto done;
7098 * XXX commits to the base branch could have happened while
7099 * we were busy rebasing; should we store the original commit ID
7100 * when rebase begins and read it back here?
7102 err = got_ref_resolve(&commit_id, repo, resolved);
7103 if (err)
7104 goto done;
7106 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7107 if (err)
7108 goto done;
7110 err = got_object_id_by_path(&tree_id, repo, commit,
7111 worktree->path_prefix);
7112 if (err)
7113 goto done;
7115 err = delete_rebase_refs(worktree, repo);
7116 if (err)
7117 goto done;
7119 err = get_fileindex_path(&fileindex_path, worktree);
7120 if (err)
7121 goto done;
7123 rfa.worktree = worktree;
7124 rfa.fileindex = fileindex;
7125 rfa.progress_cb = progress_cb;
7126 rfa.progress_arg = progress_arg;
7127 rfa.patch_cb = NULL;
7128 rfa.patch_arg = NULL;
7129 rfa.repo = repo;
7130 rfa.unlink_added_files = 0;
7131 err = worktree_status(worktree, "", fileindex, repo,
7132 revert_file, &rfa, NULL, NULL, 1, 0);
7133 if (err)
7134 goto sync;
7136 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7137 repo, progress_cb, progress_arg, NULL, NULL);
7138 sync:
7139 sync_err = sync_fileindex(fileindex, fileindex_path);
7140 if (sync_err && err == NULL)
7141 err = sync_err;
7142 done:
7143 got_ref_close(resolved);
7144 free(tree_id);
7145 free(commit_id);
7146 if (commit)
7147 got_object_commit_close(commit);
7148 if (fileindex)
7149 got_fileindex_free(fileindex);
7150 free(fileindex_path);
7152 unlockerr = lock_worktree(worktree, LOCK_SH);
7153 if (unlockerr && err == NULL)
7154 err = unlockerr;
7155 return err;
7158 const struct got_error *
7159 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7160 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7161 struct got_fileindex **fileindex, struct got_worktree *worktree,
7162 struct got_repository *repo)
7164 const struct got_error *err = NULL;
7165 char *tmp_branch_name = NULL;
7166 char *branch_ref_name = NULL;
7167 char *base_commit_ref_name = NULL;
7168 char *fileindex_path = NULL;
7169 struct check_rebase_ok_arg ok_arg;
7170 struct got_reference *wt_branch = NULL;
7171 struct got_reference *base_commit_ref = NULL;
7173 *tmp_branch = NULL;
7174 *branch_ref = NULL;
7175 *base_commit_id = NULL;
7176 *fileindex = NULL;
7178 err = lock_worktree(worktree, LOCK_EX);
7179 if (err)
7180 return err;
7182 err = open_fileindex(fileindex, &fileindex_path, worktree);
7183 if (err)
7184 goto done;
7186 ok_arg.worktree = worktree;
7187 ok_arg.repo = repo;
7188 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7189 &ok_arg);
7190 if (err)
7191 goto done;
7193 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7194 if (err)
7195 goto done;
7197 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7198 if (err)
7199 goto done;
7201 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7202 worktree);
7203 if (err)
7204 goto done;
7206 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7207 0);
7208 if (err)
7209 goto done;
7211 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7212 if (err)
7213 goto done;
7215 err = got_ref_write(*branch_ref, repo);
7216 if (err)
7217 goto done;
7219 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7220 worktree->base_commit_id);
7221 if (err)
7222 goto done;
7223 err = got_ref_write(base_commit_ref, repo);
7224 if (err)
7225 goto done;
7226 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7227 if (*base_commit_id == NULL) {
7228 err = got_error_from_errno("got_object_id_dup");
7229 goto done;
7232 err = got_ref_alloc(tmp_branch, tmp_branch_name,
7233 worktree->base_commit_id);
7234 if (err)
7235 goto done;
7236 err = got_ref_write(*tmp_branch, repo);
7237 if (err)
7238 goto done;
7240 err = got_worktree_set_head_ref(worktree, *tmp_branch);
7241 if (err)
7242 goto done;
7243 done:
7244 free(fileindex_path);
7245 free(tmp_branch_name);
7246 free(branch_ref_name);
7247 free(base_commit_ref_name);
7248 if (wt_branch)
7249 got_ref_close(wt_branch);
7250 if (err) {
7251 if (*branch_ref) {
7252 got_ref_close(*branch_ref);
7253 *branch_ref = NULL;
7255 if (*tmp_branch) {
7256 got_ref_close(*tmp_branch);
7257 *tmp_branch = NULL;
7259 free(*base_commit_id);
7260 if (*fileindex) {
7261 got_fileindex_free(*fileindex);
7262 *fileindex = NULL;
7264 lock_worktree(worktree, LOCK_SH);
7266 return err;
7269 const struct got_error *
7270 got_worktree_histedit_postpone(struct got_worktree *worktree,
7271 struct got_fileindex *fileindex)
7273 if (fileindex)
7274 got_fileindex_free(fileindex);
7275 return lock_worktree(worktree, LOCK_SH);
7278 const struct got_error *
7279 got_worktree_histedit_in_progress(int *in_progress,
7280 struct got_worktree *worktree)
7282 const struct got_error *err;
7283 char *tmp_branch_name = NULL;
7285 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7286 if (err)
7287 return err;
7289 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7290 free(tmp_branch_name);
7291 return NULL;
7294 const struct got_error *
7295 got_worktree_histedit_continue(struct got_object_id **commit_id,
7296 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7297 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7298 struct got_worktree *worktree, struct got_repository *repo)
7300 const struct got_error *err;
7301 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7302 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7303 struct got_reference *commit_ref = NULL;
7304 struct got_reference *base_commit_ref = NULL;
7305 char *fileindex_path = NULL;
7306 int have_staged_files = 0;
7308 *commit_id = NULL;
7309 *tmp_branch = NULL;
7310 *base_commit_id = NULL;
7311 *fileindex = NULL;
7313 err = lock_worktree(worktree, LOCK_EX);
7314 if (err)
7315 return err;
7317 err = open_fileindex(fileindex, &fileindex_path, worktree);
7318 if (err)
7319 goto done;
7321 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7322 &have_staged_files);
7323 if (err && err->code != GOT_ERR_CANCELLED)
7324 goto done;
7325 if (have_staged_files) {
7326 err = got_error(GOT_ERR_STAGED_PATHS);
7327 goto done;
7330 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7331 if (err)
7332 goto done;
7334 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7335 if (err)
7336 goto done;
7338 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7339 if (err)
7340 goto done;
7342 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7343 worktree);
7344 if (err)
7345 goto done;
7347 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7348 if (err)
7349 goto done;
7351 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7352 if (err)
7353 goto done;
7354 err = got_ref_resolve(commit_id, repo, commit_ref);
7355 if (err)
7356 goto done;
7358 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7359 if (err)
7360 goto done;
7361 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7362 if (err)
7363 goto done;
7365 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7366 if (err)
7367 goto done;
7368 done:
7369 free(commit_ref_name);
7370 free(branch_ref_name);
7371 free(fileindex_path);
7372 if (commit_ref)
7373 got_ref_close(commit_ref);
7374 if (base_commit_ref)
7375 got_ref_close(base_commit_ref);
7376 if (err) {
7377 free(*commit_id);
7378 *commit_id = NULL;
7379 free(*base_commit_id);
7380 *base_commit_id = NULL;
7381 if (*tmp_branch) {
7382 got_ref_close(*tmp_branch);
7383 *tmp_branch = NULL;
7385 if (*fileindex) {
7386 got_fileindex_free(*fileindex);
7387 *fileindex = NULL;
7389 lock_worktree(worktree, LOCK_EX);
7391 return err;
7394 static const struct got_error *
7395 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7397 const struct got_error *err;
7398 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7399 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7401 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7402 if (err)
7403 goto done;
7404 err = delete_ref(tmp_branch_name, repo);
7405 if (err)
7406 goto done;
7408 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7409 worktree);
7410 if (err)
7411 goto done;
7412 err = delete_ref(base_commit_ref_name, repo);
7413 if (err)
7414 goto done;
7416 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7417 if (err)
7418 goto done;
7419 err = delete_ref(branch_ref_name, repo);
7420 if (err)
7421 goto done;
7423 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7424 if (err)
7425 goto done;
7426 err = delete_ref(commit_ref_name, repo);
7427 if (err)
7428 goto done;
7429 done:
7430 free(tmp_branch_name);
7431 free(base_commit_ref_name);
7432 free(branch_ref_name);
7433 free(commit_ref_name);
7434 return err;
7437 const struct got_error *
7438 got_worktree_histedit_abort(struct got_worktree *worktree,
7439 struct got_fileindex *fileindex, struct got_repository *repo,
7440 struct got_reference *branch, struct got_object_id *base_commit_id,
7441 got_worktree_checkout_cb progress_cb, void *progress_arg)
7443 const struct got_error *err, *unlockerr, *sync_err;
7444 struct got_reference *resolved = NULL;
7445 char *fileindex_path = NULL;
7446 struct got_commit_object *commit = NULL;
7447 struct got_object_id *tree_id = NULL;
7448 struct revert_file_args rfa;
7450 err = lock_worktree(worktree, LOCK_EX);
7451 if (err)
7452 return err;
7454 err = got_object_open_as_commit(&commit, repo,
7455 worktree->base_commit_id);
7456 if (err)
7457 goto done;
7459 err = got_ref_open(&resolved, repo,
7460 got_ref_get_symref_target(branch), 0);
7461 if (err)
7462 goto done;
7464 err = got_worktree_set_head_ref(worktree, resolved);
7465 if (err)
7466 goto done;
7468 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7469 if (err)
7470 goto done;
7472 err = got_object_id_by_path(&tree_id, repo, commit,
7473 worktree->path_prefix);
7474 if (err)
7475 goto done;
7477 err = delete_histedit_refs(worktree, repo);
7478 if (err)
7479 goto done;
7481 err = get_fileindex_path(&fileindex_path, worktree);
7482 if (err)
7483 goto done;
7485 rfa.worktree = worktree;
7486 rfa.fileindex = fileindex;
7487 rfa.progress_cb = progress_cb;
7488 rfa.progress_arg = progress_arg;
7489 rfa.patch_cb = NULL;
7490 rfa.patch_arg = NULL;
7491 rfa.repo = repo;
7492 rfa.unlink_added_files = 0;
7493 err = worktree_status(worktree, "", fileindex, repo,
7494 revert_file, &rfa, NULL, NULL, 1, 0);
7495 if (err)
7496 goto sync;
7498 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7499 repo, progress_cb, progress_arg, NULL, NULL);
7500 sync:
7501 sync_err = sync_fileindex(fileindex, fileindex_path);
7502 if (sync_err && err == NULL)
7503 err = sync_err;
7504 done:
7505 got_ref_close(resolved);
7506 free(tree_id);
7507 free(fileindex_path);
7509 unlockerr = lock_worktree(worktree, LOCK_SH);
7510 if (unlockerr && err == NULL)
7511 err = unlockerr;
7512 return err;
7515 const struct got_error *
7516 got_worktree_histedit_complete(struct got_worktree *worktree,
7517 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7518 struct got_reference *edited_branch, struct got_repository *repo)
7520 const struct got_error *err, *unlockerr, *sync_err;
7521 struct got_object_id *new_head_commit_id = NULL;
7522 struct got_reference *resolved = NULL;
7523 char *fileindex_path = NULL;
7525 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7526 if (err)
7527 return err;
7529 err = got_ref_open(&resolved, repo,
7530 got_ref_get_symref_target(edited_branch), 0);
7531 if (err)
7532 goto done;
7534 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7535 resolved, new_head_commit_id, repo);
7536 if (err)
7537 goto done;
7539 err = got_ref_change_ref(resolved, new_head_commit_id);
7540 if (err)
7541 goto done;
7543 err = got_ref_write(resolved, repo);
7544 if (err)
7545 goto done;
7547 err = got_worktree_set_head_ref(worktree, resolved);
7548 if (err)
7549 goto done;
7551 err = delete_histedit_refs(worktree, repo);
7552 if (err)
7553 goto done;
7555 err = get_fileindex_path(&fileindex_path, worktree);
7556 if (err)
7557 goto done;
7558 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7559 sync_err = sync_fileindex(fileindex, fileindex_path);
7560 if (sync_err && err == NULL)
7561 err = sync_err;
7562 done:
7563 got_fileindex_free(fileindex);
7564 free(fileindex_path);
7565 free(new_head_commit_id);
7566 unlockerr = lock_worktree(worktree, LOCK_SH);
7567 if (unlockerr && err == NULL)
7568 err = unlockerr;
7569 return err;
7572 const struct got_error *
7573 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7574 struct got_object_id *commit_id, struct got_repository *repo)
7576 const struct got_error *err;
7577 char *commit_ref_name;
7579 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7580 if (err)
7581 return err;
7583 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7584 if (err)
7585 goto done;
7587 err = delete_ref(commit_ref_name, repo);
7588 done:
7589 free(commit_ref_name);
7590 return err;
7593 const struct got_error *
7594 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7595 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7596 struct got_worktree *worktree, const char *refname,
7597 struct got_repository *repo)
7599 const struct got_error *err = NULL;
7600 char *fileindex_path = NULL;
7601 struct check_rebase_ok_arg ok_arg;
7603 *fileindex = NULL;
7604 *branch_ref = NULL;
7605 *base_branch_ref = NULL;
7607 err = lock_worktree(worktree, LOCK_EX);
7608 if (err)
7609 return err;
7611 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7612 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7613 "cannot integrate a branch into itself; "
7614 "update -b or different branch name required");
7615 goto done;
7618 err = open_fileindex(fileindex, &fileindex_path, worktree);
7619 if (err)
7620 goto done;
7622 /* Preconditions are the same as for rebase. */
7623 ok_arg.worktree = worktree;
7624 ok_arg.repo = repo;
7625 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7626 &ok_arg);
7627 if (err)
7628 goto done;
7630 err = got_ref_open(branch_ref, repo, refname, 1);
7631 if (err)
7632 goto done;
7634 err = got_ref_open(base_branch_ref, repo,
7635 got_worktree_get_head_ref_name(worktree), 1);
7636 done:
7637 if (err) {
7638 if (*branch_ref) {
7639 got_ref_close(*branch_ref);
7640 *branch_ref = NULL;
7642 if (*base_branch_ref) {
7643 got_ref_close(*base_branch_ref);
7644 *base_branch_ref = NULL;
7646 if (*fileindex) {
7647 got_fileindex_free(*fileindex);
7648 *fileindex = NULL;
7650 lock_worktree(worktree, LOCK_SH);
7652 return err;
7655 const struct got_error *
7656 got_worktree_integrate_continue(struct got_worktree *worktree,
7657 struct got_fileindex *fileindex, struct got_repository *repo,
7658 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7659 got_worktree_checkout_cb progress_cb, void *progress_arg,
7660 got_cancel_cb cancel_cb, void *cancel_arg)
7662 const struct got_error *err = NULL, *sync_err, *unlockerr;
7663 char *fileindex_path = NULL;
7664 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7665 struct got_commit_object *commit = NULL;
7667 err = get_fileindex_path(&fileindex_path, worktree);
7668 if (err)
7669 goto done;
7671 err = got_ref_resolve(&commit_id, repo, branch_ref);
7672 if (err)
7673 goto done;
7675 err = got_object_open_as_commit(&commit, repo, commit_id);
7676 if (err)
7677 goto done;
7679 err = got_object_id_by_path(&tree_id, repo, commit,
7680 worktree->path_prefix);
7681 if (err)
7682 goto done;
7684 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7685 if (err)
7686 goto done;
7688 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7689 progress_cb, progress_arg, cancel_cb, cancel_arg);
7690 if (err)
7691 goto sync;
7693 err = got_ref_change_ref(base_branch_ref, commit_id);
7694 if (err)
7695 goto sync;
7697 err = got_ref_write(base_branch_ref, repo);
7698 if (err)
7699 goto sync;
7701 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7702 sync:
7703 sync_err = sync_fileindex(fileindex, fileindex_path);
7704 if (sync_err && err == NULL)
7705 err = sync_err;
7707 done:
7708 unlockerr = got_ref_unlock(branch_ref);
7709 if (unlockerr && err == NULL)
7710 err = unlockerr;
7711 got_ref_close(branch_ref);
7713 unlockerr = got_ref_unlock(base_branch_ref);
7714 if (unlockerr && err == NULL)
7715 err = unlockerr;
7716 got_ref_close(base_branch_ref);
7718 got_fileindex_free(fileindex);
7719 free(fileindex_path);
7720 free(tree_id);
7721 if (commit)
7722 got_object_commit_close(commit);
7724 unlockerr = lock_worktree(worktree, LOCK_SH);
7725 if (unlockerr && err == NULL)
7726 err = unlockerr;
7727 return err;
7730 const struct got_error *
7731 got_worktree_integrate_abort(struct got_worktree *worktree,
7732 struct got_fileindex *fileindex, struct got_repository *repo,
7733 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7735 const struct got_error *err = NULL, *unlockerr = NULL;
7737 got_fileindex_free(fileindex);
7739 err = lock_worktree(worktree, LOCK_SH);
7741 unlockerr = got_ref_unlock(branch_ref);
7742 if (unlockerr && err == NULL)
7743 err = unlockerr;
7744 got_ref_close(branch_ref);
7746 unlockerr = got_ref_unlock(base_branch_ref);
7747 if (unlockerr && err == NULL)
7748 err = unlockerr;
7749 got_ref_close(base_branch_ref);
7751 return err;
7754 const struct got_error *
7755 got_worktree_merge_postpone(struct got_worktree *worktree,
7756 struct got_fileindex *fileindex)
7758 const struct got_error *err, *sync_err;
7759 char *fileindex_path = NULL;
7761 err = get_fileindex_path(&fileindex_path, worktree);
7762 if (err)
7763 goto done;
7765 sync_err = sync_fileindex(fileindex, fileindex_path);
7767 err = lock_worktree(worktree, LOCK_SH);
7768 if (sync_err && err == NULL)
7769 err = sync_err;
7770 done:
7771 got_fileindex_free(fileindex);
7772 free(fileindex_path);
7773 return err;
7776 static const struct got_error *
7777 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7779 const struct got_error *err;
7780 char *branch_refname = NULL, *commit_refname = NULL;
7782 err = get_merge_branch_ref_name(&branch_refname, worktree);
7783 if (err)
7784 goto done;
7785 err = delete_ref(branch_refname, repo);
7786 if (err)
7787 goto done;
7789 err = get_merge_commit_ref_name(&commit_refname, worktree);
7790 if (err)
7791 goto done;
7792 err = delete_ref(commit_refname, repo);
7793 if (err)
7794 goto done;
7796 done:
7797 free(branch_refname);
7798 free(commit_refname);
7799 return err;
7802 struct merge_commit_msg_arg {
7803 struct got_worktree *worktree;
7804 const char *branch_name;
7807 static const struct got_error *
7808 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
7809 const char *diff_path, char **logmsg, void *arg)
7811 struct merge_commit_msg_arg *a = arg;
7813 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7814 got_worktree_get_head_ref_name(a->worktree)) == -1)
7815 return got_error_from_errno("asprintf");
7817 return NULL;
7821 const struct got_error *
7822 got_worktree_merge_branch(struct got_worktree *worktree,
7823 struct got_fileindex *fileindex,
7824 struct got_object_id *yca_commit_id,
7825 struct got_object_id *branch_tip,
7826 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7827 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7829 const struct got_error *err;
7830 char *fileindex_path = NULL;
7832 err = get_fileindex_path(&fileindex_path, worktree);
7833 if (err)
7834 goto done;
7836 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7837 worktree);
7838 if (err)
7839 goto done;
7841 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7842 branch_tip, repo, progress_cb, progress_arg,
7843 cancel_cb, cancel_arg);
7844 done:
7845 free(fileindex_path);
7846 return err;
7849 const struct got_error *
7850 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7851 struct got_worktree *worktree, struct got_fileindex *fileindex,
7852 const char *author, const char *committer, int allow_bad_symlinks,
7853 struct got_object_id *branch_tip, const char *branch_name,
7854 struct got_repository *repo,
7855 got_worktree_status_cb status_cb, void *status_arg)
7858 const struct got_error *err = NULL, *sync_err;
7859 struct got_pathlist_head commitable_paths;
7860 struct collect_commitables_arg cc_arg;
7861 struct got_pathlist_entry *pe;
7862 struct got_reference *head_ref = NULL;
7863 struct got_object_id *head_commit_id = NULL;
7864 int have_staged_files = 0;
7865 struct merge_commit_msg_arg mcm_arg;
7866 char *fileindex_path = NULL;
7868 memset(&cc_arg, 0, sizeof(cc_arg));
7869 *new_commit_id = NULL;
7871 TAILQ_INIT(&commitable_paths);
7873 err = get_fileindex_path(&fileindex_path, worktree);
7874 if (err)
7875 goto done;
7877 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7878 if (err)
7879 goto done;
7881 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7882 if (err)
7883 goto done;
7885 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7886 &have_staged_files);
7887 if (err && err->code != GOT_ERR_CANCELLED)
7888 goto done;
7889 if (have_staged_files) {
7890 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7891 goto done;
7894 cc_arg.commitable_paths = &commitable_paths;
7895 cc_arg.worktree = worktree;
7896 cc_arg.fileindex = fileindex;
7897 cc_arg.repo = repo;
7898 cc_arg.have_staged_files = have_staged_files;
7899 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7900 err = worktree_status(worktree, "", fileindex, repo,
7901 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7902 if (err)
7903 goto done;
7905 if (TAILQ_EMPTY(&commitable_paths)) {
7906 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7907 "merge of %s cannot proceed", branch_name);
7908 goto done;
7911 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7912 struct got_commitable *ct = pe->data;
7913 const char *ct_path = ct->in_repo_path;
7915 while (ct_path[0] == '/')
7916 ct_path++;
7917 err = check_out_of_date(ct_path, ct->status,
7918 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7919 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7920 if (err)
7921 goto done;
7925 mcm_arg.worktree = worktree;
7926 mcm_arg.branch_name = branch_name;
7927 err = commit_worktree(new_commit_id, &commitable_paths,
7928 head_commit_id, branch_tip, worktree, author, committer, NULL,
7929 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7930 if (err)
7931 goto done;
7933 err = update_fileindex_after_commit(worktree, &commitable_paths,
7934 *new_commit_id, fileindex, have_staged_files);
7935 sync_err = sync_fileindex(fileindex, fileindex_path);
7936 if (sync_err && err == NULL)
7937 err = sync_err;
7938 done:
7939 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7940 struct got_commitable *ct = pe->data;
7942 free_commitable(ct);
7944 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
7945 free(fileindex_path);
7946 return err;
7949 const struct got_error *
7950 got_worktree_merge_complete(struct got_worktree *worktree,
7951 struct got_fileindex *fileindex, struct got_repository *repo)
7953 const struct got_error *err, *unlockerr, *sync_err;
7954 char *fileindex_path = NULL;
7956 err = delete_merge_refs(worktree, repo);
7957 if (err)
7958 goto done;
7960 err = get_fileindex_path(&fileindex_path, worktree);
7961 if (err)
7962 goto done;
7963 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7964 sync_err = sync_fileindex(fileindex, fileindex_path);
7965 if (sync_err && err == NULL)
7966 err = sync_err;
7967 done:
7968 got_fileindex_free(fileindex);
7969 free(fileindex_path);
7970 unlockerr = lock_worktree(worktree, LOCK_SH);
7971 if (unlockerr && err == NULL)
7972 err = unlockerr;
7973 return err;
7976 const struct got_error *
7977 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7978 struct got_repository *repo)
7980 const struct got_error *err;
7981 char *branch_refname = NULL;
7982 struct got_reference *branch_ref = NULL;
7984 *in_progress = 0;
7986 err = get_merge_branch_ref_name(&branch_refname, worktree);
7987 if (err)
7988 return err;
7989 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7990 free(branch_refname);
7991 if (err) {
7992 if (err->code != GOT_ERR_NOT_REF)
7993 return err;
7994 } else
7995 *in_progress = 1;
7997 return NULL;
8000 const struct got_error *got_worktree_merge_prepare(
8001 struct got_fileindex **fileindex, struct got_worktree *worktree,
8002 struct got_reference *branch, struct got_repository *repo)
8004 const struct got_error *err = NULL;
8005 char *fileindex_path = NULL;
8006 char *branch_refname = NULL, *commit_refname = NULL;
8007 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
8008 struct got_reference *commit_ref = NULL;
8009 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
8010 struct check_rebase_ok_arg ok_arg;
8012 *fileindex = NULL;
8014 err = lock_worktree(worktree, LOCK_EX);
8015 if (err)
8016 return err;
8018 err = open_fileindex(fileindex, &fileindex_path, worktree);
8019 if (err)
8020 goto done;
8022 /* Preconditions are the same as for rebase. */
8023 ok_arg.worktree = worktree;
8024 ok_arg.repo = repo;
8025 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8026 &ok_arg);
8027 if (err)
8028 goto done;
8030 err = get_merge_branch_ref_name(&branch_refname, worktree);
8031 if (err)
8032 return err;
8034 err = get_merge_commit_ref_name(&commit_refname, worktree);
8035 if (err)
8036 return err;
8038 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8039 0);
8040 if (err)
8041 goto done;
8043 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8044 if (err)
8045 goto done;
8047 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8048 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8049 goto done;
8052 err = got_ref_resolve(&branch_tip, repo, branch);
8053 if (err)
8054 goto done;
8056 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8057 if (err)
8058 goto done;
8059 err = got_ref_write(branch_ref, repo);
8060 if (err)
8061 goto done;
8063 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8064 if (err)
8065 goto done;
8066 err = got_ref_write(commit_ref, repo);
8067 if (err)
8068 goto done;
8070 done:
8071 free(branch_refname);
8072 free(commit_refname);
8073 free(fileindex_path);
8074 if (branch_ref)
8075 got_ref_close(branch_ref);
8076 if (commit_ref)
8077 got_ref_close(commit_ref);
8078 if (wt_branch)
8079 got_ref_close(wt_branch);
8080 free(wt_branch_tip);
8081 if (err) {
8082 if (*fileindex) {
8083 got_fileindex_free(*fileindex);
8084 *fileindex = NULL;
8086 lock_worktree(worktree, LOCK_SH);
8088 return err;
8091 const struct got_error *
8092 got_worktree_merge_continue(char **branch_name,
8093 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8094 struct got_worktree *worktree, struct got_repository *repo)
8096 const struct got_error *err;
8097 char *commit_refname = NULL, *branch_refname = NULL;
8098 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8099 char *fileindex_path = NULL;
8100 int have_staged_files = 0;
8102 *branch_name = NULL;
8103 *branch_tip = NULL;
8104 *fileindex = NULL;
8106 err = lock_worktree(worktree, LOCK_EX);
8107 if (err)
8108 return err;
8110 err = open_fileindex(fileindex, &fileindex_path, worktree);
8111 if (err)
8112 goto done;
8114 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8115 &have_staged_files);
8116 if (err && err->code != GOT_ERR_CANCELLED)
8117 goto done;
8118 if (have_staged_files) {
8119 err = got_error(GOT_ERR_STAGED_PATHS);
8120 goto done;
8123 err = get_merge_branch_ref_name(&branch_refname, worktree);
8124 if (err)
8125 goto done;
8127 err = get_merge_commit_ref_name(&commit_refname, worktree);
8128 if (err)
8129 goto done;
8131 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8132 if (err)
8133 goto done;
8135 if (!got_ref_is_symbolic(branch_ref)) {
8136 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8137 "%s is not a symbolic reference",
8138 got_ref_get_name(branch_ref));
8139 goto done;
8141 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8142 if (*branch_name == NULL) {
8143 err = got_error_from_errno("strdup");
8144 goto done;
8147 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8148 if (err)
8149 goto done;
8151 err = got_ref_resolve(branch_tip, repo, commit_ref);
8152 if (err)
8153 goto done;
8154 done:
8155 free(commit_refname);
8156 free(branch_refname);
8157 free(fileindex_path);
8158 if (commit_ref)
8159 got_ref_close(commit_ref);
8160 if (branch_ref)
8161 got_ref_close(branch_ref);
8162 if (err) {
8163 if (*branch_name) {
8164 free(*branch_name);
8165 *branch_name = NULL;
8167 free(*branch_tip);
8168 *branch_tip = NULL;
8169 if (*fileindex) {
8170 got_fileindex_free(*fileindex);
8171 *fileindex = NULL;
8173 lock_worktree(worktree, LOCK_SH);
8175 return err;
8178 const struct got_error *
8179 got_worktree_merge_abort(struct got_worktree *worktree,
8180 struct got_fileindex *fileindex, struct got_repository *repo,
8181 got_worktree_checkout_cb progress_cb, void *progress_arg)
8183 const struct got_error *err, *unlockerr, *sync_err;
8184 struct got_object_id *commit_id = NULL;
8185 struct got_commit_object *commit = NULL;
8186 char *fileindex_path = NULL;
8187 struct revert_file_args rfa;
8188 struct got_object_id *tree_id = NULL;
8190 err = got_object_open_as_commit(&commit, repo,
8191 worktree->base_commit_id);
8192 if (err)
8193 goto done;
8195 err = got_object_id_by_path(&tree_id, repo, commit,
8196 worktree->path_prefix);
8197 if (err)
8198 goto done;
8200 err = delete_merge_refs(worktree, repo);
8201 if (err)
8202 goto done;
8204 err = get_fileindex_path(&fileindex_path, worktree);
8205 if (err)
8206 goto done;
8208 rfa.worktree = worktree;
8209 rfa.fileindex = fileindex;
8210 rfa.progress_cb = progress_cb;
8211 rfa.progress_arg = progress_arg;
8212 rfa.patch_cb = NULL;
8213 rfa.patch_arg = NULL;
8214 rfa.repo = repo;
8215 rfa.unlink_added_files = 1;
8216 err = worktree_status(worktree, "", fileindex, repo,
8217 revert_file, &rfa, NULL, NULL, 1, 0);
8218 if (err)
8219 goto sync;
8221 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8222 repo, progress_cb, progress_arg, NULL, NULL);
8223 sync:
8224 sync_err = sync_fileindex(fileindex, fileindex_path);
8225 if (sync_err && err == NULL)
8226 err = sync_err;
8227 done:
8228 free(tree_id);
8229 free(commit_id);
8230 if (commit)
8231 got_object_commit_close(commit);
8232 if (fileindex)
8233 got_fileindex_free(fileindex);
8234 free(fileindex_path);
8236 unlockerr = lock_worktree(worktree, LOCK_SH);
8237 if (unlockerr && err == NULL)
8238 err = unlockerr;
8239 return err;
8242 struct check_stage_ok_arg {
8243 struct got_object_id *head_commit_id;
8244 struct got_worktree *worktree;
8245 struct got_fileindex *fileindex;
8246 struct got_repository *repo;
8247 int have_changes;
8250 static const struct got_error *
8251 check_stage_ok(void *arg, unsigned char status,
8252 unsigned char staged_status, const char *relpath,
8253 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8254 struct got_object_id *commit_id, int dirfd, const char *de_name)
8256 struct check_stage_ok_arg *a = arg;
8257 const struct got_error *err = NULL;
8258 struct got_fileindex_entry *ie;
8259 struct got_object_id base_commit_id;
8260 struct got_object_id *base_commit_idp = NULL;
8261 char *in_repo_path = NULL, *p;
8263 if (status == GOT_STATUS_UNVERSIONED ||
8264 status == GOT_STATUS_NO_CHANGE)
8265 return NULL;
8266 if (status == GOT_STATUS_NONEXISTENT)
8267 return got_error_set_errno(ENOENT, relpath);
8269 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8270 if (ie == NULL)
8271 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8273 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8274 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8275 relpath) == -1)
8276 return got_error_from_errno("asprintf");
8278 if (got_fileindex_entry_has_commit(ie)) {
8279 base_commit_idp = got_fileindex_entry_get_commit_id(
8280 &base_commit_id, ie);
8283 if (status == GOT_STATUS_CONFLICT) {
8284 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8285 goto done;
8286 } else if (status != GOT_STATUS_ADD &&
8287 status != GOT_STATUS_MODIFY &&
8288 status != GOT_STATUS_DELETE) {
8289 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8290 goto done;
8293 a->have_changes = 1;
8295 p = in_repo_path;
8296 while (p[0] == '/')
8297 p++;
8298 err = check_out_of_date(p, status, staged_status,
8299 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8300 GOT_ERR_STAGE_OUT_OF_DATE);
8301 done:
8302 free(in_repo_path);
8303 return err;
8306 struct stage_path_arg {
8307 struct got_worktree *worktree;
8308 struct got_fileindex *fileindex;
8309 struct got_repository *repo;
8310 got_worktree_status_cb status_cb;
8311 void *status_arg;
8312 got_worktree_patch_cb patch_cb;
8313 void *patch_arg;
8314 int staged_something;
8315 int allow_bad_symlinks;
8318 static const struct got_error *
8319 stage_path(void *arg, unsigned char status,
8320 unsigned char staged_status, const char *relpath,
8321 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8322 struct got_object_id *commit_id, int dirfd, const char *de_name)
8324 struct stage_path_arg *a = arg;
8325 const struct got_error *err = NULL;
8326 struct got_fileindex_entry *ie;
8327 char *ondisk_path = NULL, *path_content = NULL;
8328 uint32_t stage;
8329 struct got_object_id *new_staged_blob_id = NULL;
8330 struct stat sb;
8332 if (status == GOT_STATUS_UNVERSIONED)
8333 return NULL;
8335 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8336 if (ie == NULL)
8337 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8339 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8340 relpath)== -1)
8341 return got_error_from_errno("asprintf");
8343 switch (status) {
8344 case GOT_STATUS_ADD:
8345 case GOT_STATUS_MODIFY:
8346 /* XXX could sb.st_mode be passed in by our caller? */
8347 if (lstat(ondisk_path, &sb) == -1) {
8348 err = got_error_from_errno2("lstat", ondisk_path);
8349 break;
8351 if (a->patch_cb) {
8352 if (status == GOT_STATUS_ADD) {
8353 int choice = GOT_PATCH_CHOICE_NONE;
8354 err = (*a->patch_cb)(&choice, a->patch_arg,
8355 status, ie->path, NULL, 1, 1);
8356 if (err)
8357 break;
8358 if (choice != GOT_PATCH_CHOICE_YES)
8359 break;
8360 } else {
8361 err = create_patched_content(&path_content, 0,
8362 staged_blob_id ? staged_blob_id : blob_id,
8363 ondisk_path, dirfd, de_name, ie->path,
8364 a->repo, a->patch_cb, a->patch_arg);
8365 if (err || path_content == NULL)
8366 break;
8369 err = got_object_blob_create(&new_staged_blob_id,
8370 path_content ? path_content : ondisk_path, a->repo);
8371 if (err)
8372 break;
8373 memcpy(ie->staged_blob_sha1, new_staged_blob_id->hash,
8374 SHA1_DIGEST_LENGTH);
8375 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8376 stage = GOT_FILEIDX_STAGE_ADD;
8377 else
8378 stage = GOT_FILEIDX_STAGE_MODIFY;
8379 got_fileindex_entry_stage_set(ie, stage);
8380 if (S_ISLNK(sb.st_mode)) {
8381 int is_bad_symlink = 0;
8382 if (!a->allow_bad_symlinks) {
8383 char target_path[PATH_MAX];
8384 ssize_t target_len;
8385 target_len = readlink(ondisk_path, target_path,
8386 sizeof(target_path));
8387 if (target_len == -1) {
8388 err = got_error_from_errno2("readlink",
8389 ondisk_path);
8390 break;
8392 err = is_bad_symlink_target(&is_bad_symlink,
8393 target_path, target_len, ondisk_path,
8394 a->worktree->root_path);
8395 if (err)
8396 break;
8397 if (is_bad_symlink) {
8398 err = got_error_path(ondisk_path,
8399 GOT_ERR_BAD_SYMLINK);
8400 break;
8403 if (is_bad_symlink)
8404 got_fileindex_entry_staged_filetype_set(ie,
8405 GOT_FILEIDX_MODE_BAD_SYMLINK);
8406 else
8407 got_fileindex_entry_staged_filetype_set(ie,
8408 GOT_FILEIDX_MODE_SYMLINK);
8409 } else {
8410 got_fileindex_entry_staged_filetype_set(ie,
8411 GOT_FILEIDX_MODE_REGULAR_FILE);
8413 a->staged_something = 1;
8414 if (a->status_cb == NULL)
8415 break;
8416 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8417 get_staged_status(ie), relpath, blob_id,
8418 new_staged_blob_id, NULL, dirfd, de_name);
8419 if (err)
8420 break;
8422 * When staging the reverse of the staged diff,
8423 * implicitly unstage the file.
8425 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8426 sizeof(ie->blob_sha1)) == 0) {
8427 got_fileindex_entry_stage_set(ie,
8428 GOT_FILEIDX_STAGE_NONE);
8430 break;
8431 case GOT_STATUS_DELETE:
8432 if (staged_status == GOT_STATUS_DELETE)
8433 break;
8434 if (a->patch_cb) {
8435 int choice = GOT_PATCH_CHOICE_NONE;
8436 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8437 ie->path, NULL, 1, 1);
8438 if (err)
8439 break;
8440 if (choice == GOT_PATCH_CHOICE_NO)
8441 break;
8442 if (choice != GOT_PATCH_CHOICE_YES) {
8443 err = got_error(GOT_ERR_PATCH_CHOICE);
8444 break;
8447 stage = GOT_FILEIDX_STAGE_DELETE;
8448 got_fileindex_entry_stage_set(ie, stage);
8449 a->staged_something = 1;
8450 if (a->status_cb == NULL)
8451 break;
8452 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8453 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8454 de_name);
8455 break;
8456 case GOT_STATUS_NO_CHANGE:
8457 break;
8458 case GOT_STATUS_CONFLICT:
8459 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8460 break;
8461 case GOT_STATUS_NONEXISTENT:
8462 err = got_error_set_errno(ENOENT, relpath);
8463 break;
8464 default:
8465 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8466 break;
8469 if (path_content && unlink(path_content) == -1 && err == NULL)
8470 err = got_error_from_errno2("unlink", path_content);
8471 free(path_content);
8472 free(ondisk_path);
8473 free(new_staged_blob_id);
8474 return err;
8477 const struct got_error *
8478 got_worktree_stage(struct got_worktree *worktree,
8479 struct got_pathlist_head *paths,
8480 got_worktree_status_cb status_cb, void *status_arg,
8481 got_worktree_patch_cb patch_cb, void *patch_arg,
8482 int allow_bad_symlinks, struct got_repository *repo)
8484 const struct got_error *err = NULL, *sync_err, *unlockerr;
8485 struct got_pathlist_entry *pe;
8486 struct got_fileindex *fileindex = NULL;
8487 char *fileindex_path = NULL;
8488 struct got_reference *head_ref = NULL;
8489 struct got_object_id *head_commit_id = NULL;
8490 struct check_stage_ok_arg oka;
8491 struct stage_path_arg spa;
8493 err = lock_worktree(worktree, LOCK_EX);
8494 if (err)
8495 return err;
8497 err = got_ref_open(&head_ref, repo,
8498 got_worktree_get_head_ref_name(worktree), 0);
8499 if (err)
8500 goto done;
8501 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8502 if (err)
8503 goto done;
8504 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8505 if (err)
8506 goto done;
8508 /* Check pre-conditions before staging anything. */
8509 oka.head_commit_id = head_commit_id;
8510 oka.worktree = worktree;
8511 oka.fileindex = fileindex;
8512 oka.repo = repo;
8513 oka.have_changes = 0;
8514 TAILQ_FOREACH(pe, paths, entry) {
8515 err = worktree_status(worktree, pe->path, fileindex, repo,
8516 check_stage_ok, &oka, NULL, NULL, 1, 0);
8517 if (err)
8518 goto done;
8520 if (!oka.have_changes) {
8521 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8522 goto done;
8525 spa.worktree = worktree;
8526 spa.fileindex = fileindex;
8527 spa.repo = repo;
8528 spa.patch_cb = patch_cb;
8529 spa.patch_arg = patch_arg;
8530 spa.status_cb = status_cb;
8531 spa.status_arg = status_arg;
8532 spa.staged_something = 0;
8533 spa.allow_bad_symlinks = allow_bad_symlinks;
8534 TAILQ_FOREACH(pe, paths, entry) {
8535 err = worktree_status(worktree, pe->path, fileindex, repo,
8536 stage_path, &spa, NULL, NULL, 1, 0);
8537 if (err)
8538 goto done;
8540 if (!spa.staged_something) {
8541 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8542 goto done;
8545 sync_err = sync_fileindex(fileindex, fileindex_path);
8546 if (sync_err && err == NULL)
8547 err = sync_err;
8548 done:
8549 if (head_ref)
8550 got_ref_close(head_ref);
8551 free(head_commit_id);
8552 free(fileindex_path);
8553 if (fileindex)
8554 got_fileindex_free(fileindex);
8555 unlockerr = lock_worktree(worktree, LOCK_SH);
8556 if (unlockerr && err == NULL)
8557 err = unlockerr;
8558 return err;
8561 struct unstage_path_arg {
8562 struct got_worktree *worktree;
8563 struct got_fileindex *fileindex;
8564 struct got_repository *repo;
8565 got_worktree_checkout_cb progress_cb;
8566 void *progress_arg;
8567 got_worktree_patch_cb patch_cb;
8568 void *patch_arg;
8571 static const struct got_error *
8572 create_unstaged_content(char **path_unstaged_content,
8573 char **path_new_staged_content, struct got_object_id *blob_id,
8574 struct got_object_id *staged_blob_id, const char *relpath,
8575 struct got_repository *repo,
8576 got_worktree_patch_cb patch_cb, void *patch_arg)
8578 const struct got_error *err, *free_err;
8579 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8580 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8581 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8582 struct got_diffreg_result *diffreg_result = NULL;
8583 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8584 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8585 int fd1 = -1, fd2 = -1;
8587 *path_unstaged_content = NULL;
8588 *path_new_staged_content = NULL;
8590 err = got_object_id_str(&label1, blob_id);
8591 if (err)
8592 return err;
8594 fd1 = got_opentempfd();
8595 if (fd1 == -1) {
8596 err = got_error_from_errno("got_opentempfd");
8597 goto done;
8599 fd2 = got_opentempfd();
8600 if (fd2 == -1) {
8601 err = got_error_from_errno("got_opentempfd");
8602 goto done;
8605 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8606 if (err)
8607 goto done;
8609 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
8610 if (err)
8611 goto done;
8613 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8614 if (err)
8615 goto done;
8617 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8618 fd2);
8619 if (err)
8620 goto done;
8622 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
8623 if (err)
8624 goto done;
8626 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8627 if (err)
8628 goto done;
8630 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8631 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8632 if (err)
8633 goto done;
8635 err = got_opentemp_named(path_unstaged_content, &outfile,
8636 "got-unstaged-content", "");
8637 if (err)
8638 goto done;
8639 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8640 "got-new-staged-content", "");
8641 if (err)
8642 goto done;
8644 if (fseek(f1, 0L, SEEK_SET) == -1) {
8645 err = got_ferror(f1, GOT_ERR_IO);
8646 goto done;
8648 if (fseek(f2, 0L, SEEK_SET) == -1) {
8649 err = got_ferror(f2, GOT_ERR_IO);
8650 goto done;
8652 /* Count the number of actual changes in the diff result. */
8653 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8654 struct diff_chunk_context cc = {};
8655 diff_chunk_context_load_change(&cc, &nchunks_used,
8656 diffreg_result->result, n, 0);
8657 nchanges++;
8659 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8660 int choice;
8661 err = apply_or_reject_change(&choice, &nchunks_used,
8662 diffreg_result->result, n, relpath, f1, f2,
8663 &line_cur1, &line_cur2,
8664 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8665 if (err)
8666 goto done;
8667 if (choice == GOT_PATCH_CHOICE_YES)
8668 have_content = 1;
8669 else
8670 have_rejected_content = 1;
8671 if (choice == GOT_PATCH_CHOICE_QUIT)
8672 break;
8674 if (have_content || have_rejected_content)
8675 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8676 outfile, rejectfile);
8677 done:
8678 free(label1);
8679 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8680 err = got_error_from_errno("close");
8681 if (blob)
8682 got_object_blob_close(blob);
8683 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8684 err = got_error_from_errno("close");
8685 if (staged_blob)
8686 got_object_blob_close(staged_blob);
8687 free_err = got_diffreg_result_free(diffreg_result);
8688 if (free_err && err == NULL)
8689 err = free_err;
8690 if (f1 && fclose(f1) == EOF && err == NULL)
8691 err = got_error_from_errno2("fclose", path1);
8692 if (f2 && fclose(f2) == EOF && err == NULL)
8693 err = got_error_from_errno2("fclose", path2);
8694 if (outfile && fclose(outfile) == EOF && err == NULL)
8695 err = got_error_from_errno2("fclose", *path_unstaged_content);
8696 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8697 err = got_error_from_errno2("fclose", *path_new_staged_content);
8698 if (path1 && unlink(path1) == -1 && err == NULL)
8699 err = got_error_from_errno2("unlink", path1);
8700 if (path2 && unlink(path2) == -1 && err == NULL)
8701 err = got_error_from_errno2("unlink", path2);
8702 if (err || !have_content) {
8703 if (*path_unstaged_content &&
8704 unlink(*path_unstaged_content) == -1 && err == NULL)
8705 err = got_error_from_errno2("unlink",
8706 *path_unstaged_content);
8707 free(*path_unstaged_content);
8708 *path_unstaged_content = NULL;
8710 if (err || !have_content || !have_rejected_content) {
8711 if (*path_new_staged_content &&
8712 unlink(*path_new_staged_content) == -1 && err == NULL)
8713 err = got_error_from_errno2("unlink",
8714 *path_new_staged_content);
8715 free(*path_new_staged_content);
8716 *path_new_staged_content = NULL;
8718 free(path1);
8719 free(path2);
8720 return err;
8723 static const struct got_error *
8724 unstage_hunks(struct got_object_id *staged_blob_id,
8725 struct got_blob_object *blob_base,
8726 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8727 const char *ondisk_path, const char *label_orig,
8728 struct got_worktree *worktree, struct got_repository *repo,
8729 got_worktree_patch_cb patch_cb, void *patch_arg,
8730 got_worktree_checkout_cb progress_cb, void *progress_arg)
8732 const struct got_error *err = NULL;
8733 char *path_unstaged_content = NULL;
8734 char *path_new_staged_content = NULL;
8735 char *parent = NULL, *base_path = NULL;
8736 char *blob_base_path = NULL;
8737 struct got_object_id *new_staged_blob_id = NULL;
8738 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8739 struct stat sb;
8741 err = create_unstaged_content(&path_unstaged_content,
8742 &path_new_staged_content, blob_id, staged_blob_id,
8743 ie->path, repo, patch_cb, patch_arg);
8744 if (err)
8745 return err;
8747 if (path_unstaged_content == NULL)
8748 return NULL;
8750 if (path_new_staged_content) {
8751 err = got_object_blob_create(&new_staged_blob_id,
8752 path_new_staged_content, repo);
8753 if (err)
8754 goto done;
8757 f = fopen(path_unstaged_content, "re");
8758 if (f == NULL) {
8759 err = got_error_from_errno2("fopen",
8760 path_unstaged_content);
8761 goto done;
8763 if (fstat(fileno(f), &sb) == -1) {
8764 err = got_error_from_errno2("fstat", path_unstaged_content);
8765 goto done;
8767 if (got_fileindex_entry_staged_filetype_get(ie) ==
8768 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8769 char link_target[PATH_MAX];
8770 size_t r;
8771 r = fread(link_target, 1, sizeof(link_target), f);
8772 if (r == 0 && ferror(f)) {
8773 err = got_error_from_errno("fread");
8774 goto done;
8776 if (r >= sizeof(link_target)) { /* should not happen */
8777 err = got_error(GOT_ERR_NO_SPACE);
8778 goto done;
8780 link_target[r] = '\0';
8781 err = merge_symlink(worktree, blob_base,
8782 ondisk_path, ie->path, label_orig, link_target,
8783 worktree->base_commit_id, repo, progress_cb,
8784 progress_arg);
8785 } else {
8786 int local_changes_subsumed;
8788 err = got_path_dirname(&parent, ondisk_path);
8789 if (err)
8790 return err;
8792 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8793 parent) == -1) {
8794 err = got_error_from_errno("asprintf");
8795 base_path = NULL;
8796 goto done;
8799 err = got_opentemp_named(&blob_base_path, &f_base,
8800 base_path, "");
8801 if (err)
8802 goto done;
8803 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8804 blob_base);
8805 if (err)
8806 goto done;
8809 * In order the run a 3-way merge with a symlink we copy the symlink's
8810 * target path into a temporary file and use that file with diff3.
8812 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8813 err = dump_symlink_target_path_to_file(&f_deriv2,
8814 ondisk_path);
8815 if (err)
8816 goto done;
8817 } else {
8818 int fd;
8819 fd = open(ondisk_path,
8820 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8821 if (fd == -1) {
8822 err = got_error_from_errno2("open", ondisk_path);
8823 goto done;
8825 f_deriv2 = fdopen(fd, "r");
8826 if (f_deriv2 == NULL) {
8827 err = got_error_from_errno2("fdopen", ondisk_path);
8828 close(fd);
8829 goto done;
8833 err = merge_file(&local_changes_subsumed, worktree,
8834 f_base, f, f_deriv2, ondisk_path, ie->path,
8835 got_fileindex_perms_to_st(ie),
8836 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8837 repo, progress_cb, progress_arg);
8839 if (err)
8840 goto done;
8842 if (new_staged_blob_id) {
8843 memcpy(ie->staged_blob_sha1, new_staged_blob_id->hash,
8844 SHA1_DIGEST_LENGTH);
8845 } else {
8846 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8847 got_fileindex_entry_staged_filetype_set(ie, 0);
8849 done:
8850 free(new_staged_blob_id);
8851 if (path_unstaged_content &&
8852 unlink(path_unstaged_content) == -1 && err == NULL)
8853 err = got_error_from_errno2("unlink", path_unstaged_content);
8854 if (path_new_staged_content &&
8855 unlink(path_new_staged_content) == -1 && err == NULL)
8856 err = got_error_from_errno2("unlink", path_new_staged_content);
8857 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8858 err = got_error_from_errno2("unlink", blob_base_path);
8859 if (f_base && fclose(f_base) == EOF && err == NULL)
8860 err = got_error_from_errno2("fclose", path_unstaged_content);
8861 if (f && fclose(f) == EOF && err == NULL)
8862 err = got_error_from_errno2("fclose", path_unstaged_content);
8863 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8864 err = got_error_from_errno2("fclose", ondisk_path);
8865 free(path_unstaged_content);
8866 free(path_new_staged_content);
8867 free(blob_base_path);
8868 free(parent);
8869 free(base_path);
8870 return err;
8873 static const struct got_error *
8874 unstage_path(void *arg, unsigned char status,
8875 unsigned char staged_status, const char *relpath,
8876 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8877 struct got_object_id *commit_id, int dirfd, const char *de_name)
8879 const struct got_error *err = NULL;
8880 struct unstage_path_arg *a = arg;
8881 struct got_fileindex_entry *ie;
8882 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8883 char *ondisk_path = NULL;
8884 char *id_str = NULL, *label_orig = NULL;
8885 int local_changes_subsumed;
8886 struct stat sb;
8887 int fd1 = -1, fd2 = -1;
8889 if (staged_status != GOT_STATUS_ADD &&
8890 staged_status != GOT_STATUS_MODIFY &&
8891 staged_status != GOT_STATUS_DELETE)
8892 return NULL;
8894 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8895 if (ie == NULL)
8896 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8898 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8899 == -1)
8900 return got_error_from_errno("asprintf");
8902 err = got_object_id_str(&id_str,
8903 commit_id ? commit_id : a->worktree->base_commit_id);
8904 if (err)
8905 goto done;
8906 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8907 id_str) == -1) {
8908 err = got_error_from_errno("asprintf");
8909 goto done;
8912 fd1 = got_opentempfd();
8913 if (fd1 == -1) {
8914 err = got_error_from_errno("got_opentempfd");
8915 goto done;
8917 fd2 = got_opentempfd();
8918 if (fd2 == -1) {
8919 err = got_error_from_errno("got_opentempfd");
8920 goto done;
8923 switch (staged_status) {
8924 case GOT_STATUS_MODIFY:
8925 err = got_object_open_as_blob(&blob_base, a->repo,
8926 blob_id, 8192, fd1);
8927 if (err)
8928 break;
8929 /* fall through */
8930 case GOT_STATUS_ADD:
8931 if (a->patch_cb) {
8932 if (staged_status == GOT_STATUS_ADD) {
8933 int choice = GOT_PATCH_CHOICE_NONE;
8934 err = (*a->patch_cb)(&choice, a->patch_arg,
8935 staged_status, ie->path, NULL, 1, 1);
8936 if (err)
8937 break;
8938 if (choice != GOT_PATCH_CHOICE_YES)
8939 break;
8940 } else {
8941 err = unstage_hunks(staged_blob_id,
8942 blob_base, blob_id, ie, ondisk_path,
8943 label_orig, a->worktree, a->repo,
8944 a->patch_cb, a->patch_arg,
8945 a->progress_cb, a->progress_arg);
8946 break; /* Done with this file. */
8949 err = got_object_open_as_blob(&blob_staged, a->repo,
8950 staged_blob_id, 8192, fd2);
8951 if (err)
8952 break;
8953 switch (got_fileindex_entry_staged_filetype_get(ie)) {
8954 case GOT_FILEIDX_MODE_BAD_SYMLINK:
8955 case GOT_FILEIDX_MODE_REGULAR_FILE:
8956 err = merge_blob(&local_changes_subsumed, a->worktree,
8957 blob_base, ondisk_path, relpath,
8958 got_fileindex_perms_to_st(ie), label_orig,
8959 blob_staged, commit_id ? commit_id :
8960 a->worktree->base_commit_id, a->repo,
8961 a->progress_cb, a->progress_arg);
8962 break;
8963 case GOT_FILEIDX_MODE_SYMLINK:
8964 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8965 char *staged_target;
8966 err = got_object_blob_read_to_str(
8967 &staged_target, blob_staged);
8968 if (err)
8969 goto done;
8970 err = merge_symlink(a->worktree, blob_base,
8971 ondisk_path, relpath, label_orig,
8972 staged_target, commit_id ? commit_id :
8973 a->worktree->base_commit_id,
8974 a->repo, a->progress_cb, a->progress_arg);
8975 free(staged_target);
8976 } else {
8977 err = merge_blob(&local_changes_subsumed,
8978 a->worktree, blob_base, ondisk_path,
8979 relpath, got_fileindex_perms_to_st(ie),
8980 label_orig, blob_staged,
8981 commit_id ? commit_id :
8982 a->worktree->base_commit_id, a->repo,
8983 a->progress_cb, a->progress_arg);
8985 break;
8986 default:
8987 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8988 break;
8990 if (err == NULL) {
8991 got_fileindex_entry_stage_set(ie,
8992 GOT_FILEIDX_STAGE_NONE);
8993 got_fileindex_entry_staged_filetype_set(ie, 0);
8995 break;
8996 case GOT_STATUS_DELETE:
8997 if (a->patch_cb) {
8998 int choice = GOT_PATCH_CHOICE_NONE;
8999 err = (*a->patch_cb)(&choice, a->patch_arg,
9000 staged_status, ie->path, NULL, 1, 1);
9001 if (err)
9002 break;
9003 if (choice == GOT_PATCH_CHOICE_NO)
9004 break;
9005 if (choice != GOT_PATCH_CHOICE_YES) {
9006 err = got_error(GOT_ERR_PATCH_CHOICE);
9007 break;
9010 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9011 got_fileindex_entry_staged_filetype_set(ie, 0);
9012 err = get_file_status(&status, &sb, ie, ondisk_path,
9013 dirfd, de_name, a->repo);
9014 if (err)
9015 break;
9016 err = (*a->progress_cb)(a->progress_arg, status, relpath);
9017 break;
9019 done:
9020 free(ondisk_path);
9021 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9022 err = got_error_from_errno("close");
9023 if (blob_base)
9024 got_object_blob_close(blob_base);
9025 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9026 err = got_error_from_errno("close");
9027 if (blob_staged)
9028 got_object_blob_close(blob_staged);
9029 free(id_str);
9030 free(label_orig);
9031 return err;
9034 const struct got_error *
9035 got_worktree_unstage(struct got_worktree *worktree,
9036 struct got_pathlist_head *paths,
9037 got_worktree_checkout_cb progress_cb, void *progress_arg,
9038 got_worktree_patch_cb patch_cb, void *patch_arg,
9039 struct got_repository *repo)
9041 const struct got_error *err = NULL, *sync_err, *unlockerr;
9042 struct got_pathlist_entry *pe;
9043 struct got_fileindex *fileindex = NULL;
9044 char *fileindex_path = NULL;
9045 struct unstage_path_arg upa;
9047 err = lock_worktree(worktree, LOCK_EX);
9048 if (err)
9049 return err;
9051 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9052 if (err)
9053 goto done;
9055 upa.worktree = worktree;
9056 upa.fileindex = fileindex;
9057 upa.repo = repo;
9058 upa.progress_cb = progress_cb;
9059 upa.progress_arg = progress_arg;
9060 upa.patch_cb = patch_cb;
9061 upa.patch_arg = patch_arg;
9062 TAILQ_FOREACH(pe, paths, entry) {
9063 err = worktree_status(worktree, pe->path, fileindex, repo,
9064 unstage_path, &upa, NULL, NULL, 1, 0);
9065 if (err)
9066 goto done;
9069 sync_err = sync_fileindex(fileindex, fileindex_path);
9070 if (sync_err && err == NULL)
9071 err = sync_err;
9072 done:
9073 free(fileindex_path);
9074 if (fileindex)
9075 got_fileindex_free(fileindex);
9076 unlockerr = lock_worktree(worktree, LOCK_SH);
9077 if (unlockerr && err == NULL)
9078 err = unlockerr;
9079 return err;
9082 struct report_file_info_arg {
9083 struct got_worktree *worktree;
9084 got_worktree_path_info_cb info_cb;
9085 void *info_arg;
9086 struct got_pathlist_head *paths;
9087 got_cancel_cb cancel_cb;
9088 void *cancel_arg;
9091 static const struct got_error *
9092 report_file_info(void *arg, struct got_fileindex_entry *ie)
9094 struct report_file_info_arg *a = arg;
9095 struct got_pathlist_entry *pe;
9096 struct got_object_id blob_id, staged_blob_id, commit_id;
9097 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9098 struct got_object_id *commit_idp = NULL;
9099 int stage;
9101 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9102 return got_error(GOT_ERR_CANCELLED);
9104 TAILQ_FOREACH(pe, a->paths, entry) {
9105 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9106 got_path_is_child(ie->path, pe->path, pe->path_len))
9107 break;
9109 if (pe == NULL) /* not found */
9110 return NULL;
9112 if (got_fileindex_entry_has_blob(ie))
9113 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9114 stage = got_fileindex_entry_stage_get(ie);
9115 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9116 stage == GOT_FILEIDX_STAGE_ADD) {
9117 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9118 &staged_blob_id, ie);
9121 if (got_fileindex_entry_has_commit(ie))
9122 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9124 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9125 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9128 const struct got_error *
9129 got_worktree_path_info(struct got_worktree *worktree,
9130 struct got_pathlist_head *paths,
9131 got_worktree_path_info_cb info_cb, void *info_arg,
9132 got_cancel_cb cancel_cb, void *cancel_arg)
9135 const struct got_error *err = NULL, *unlockerr;
9136 struct got_fileindex *fileindex = NULL;
9137 char *fileindex_path = NULL;
9138 struct report_file_info_arg arg;
9140 err = lock_worktree(worktree, LOCK_SH);
9141 if (err)
9142 return err;
9144 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9145 if (err)
9146 goto done;
9148 arg.worktree = worktree;
9149 arg.info_cb = info_cb;
9150 arg.info_arg = info_arg;
9151 arg.paths = paths;
9152 arg.cancel_cb = cancel_cb;
9153 arg.cancel_arg = cancel_arg;
9154 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9155 &arg);
9156 done:
9157 free(fileindex_path);
9158 if (fileindex)
9159 got_fileindex_free(fileindex);
9160 unlockerr = lock_worktree(worktree, LOCK_UN);
9161 if (unlockerr && err == NULL)
9162 err = unlockerr;
9163 return err;
9166 static const struct got_error *
9167 patch_check_path(const char *p, char **path, unsigned char *status,
9168 unsigned char *staged_status, struct got_fileindex *fileindex,
9169 struct got_worktree *worktree, struct got_repository *repo)
9171 const struct got_error *err;
9172 struct got_fileindex_entry *ie;
9173 struct stat sb;
9174 char *ondisk_path = NULL;
9176 err = got_worktree_resolve_path(path, worktree, p);
9177 if (err)
9178 return err;
9180 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9181 *path[0] ? "/" : "", *path) == -1)
9182 return got_error_from_errno("asprintf");
9184 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9185 if (ie) {
9186 *staged_status = get_staged_status(ie);
9187 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9188 repo);
9189 if (err)
9190 goto done;
9191 } else {
9192 *staged_status = GOT_STATUS_NO_CHANGE;
9193 *status = GOT_STATUS_UNVERSIONED;
9194 if (lstat(ondisk_path, &sb) == -1) {
9195 if (errno != ENOENT) {
9196 err = got_error_from_errno2("lstat",
9197 ondisk_path);
9198 goto done;
9200 *status = GOT_STATUS_NONEXISTENT;
9204 done:
9205 free(ondisk_path);
9206 return err;
9209 static const struct got_error *
9210 patch_can_rm(const char *path, unsigned char status,
9211 unsigned char staged_status)
9213 if (status == GOT_STATUS_NONEXISTENT)
9214 return got_error_set_errno(ENOENT, path);
9215 if (status != GOT_STATUS_NO_CHANGE &&
9216 status != GOT_STATUS_ADD &&
9217 status != GOT_STATUS_MODIFY &&
9218 status != GOT_STATUS_MODE_CHANGE)
9219 return got_error_path(path, GOT_ERR_FILE_STATUS);
9220 if (staged_status == GOT_STATUS_DELETE)
9221 return got_error_path(path, GOT_ERR_FILE_STATUS);
9222 return NULL;
9225 static const struct got_error *
9226 patch_can_add(const char *path, unsigned char status)
9228 if (status != GOT_STATUS_NONEXISTENT)
9229 return got_error_path(path, GOT_ERR_FILE_STATUS);
9230 return NULL;
9233 static const struct got_error *
9234 patch_can_edit(const char *path, unsigned char status,
9235 unsigned char staged_status)
9237 if (status == GOT_STATUS_NONEXISTENT)
9238 return got_error_set_errno(ENOENT, path);
9239 if (status != GOT_STATUS_NO_CHANGE &&
9240 status != GOT_STATUS_ADD &&
9241 status != GOT_STATUS_MODIFY)
9242 return got_error_path(path, GOT_ERR_FILE_STATUS);
9243 if (staged_status == GOT_STATUS_DELETE)
9244 return got_error_path(path, GOT_ERR_FILE_STATUS);
9245 return NULL;
9248 const struct got_error *
9249 got_worktree_patch_prepare(struct got_fileindex **fileindex,
9250 char **fileindex_path, struct got_worktree *worktree)
9252 return open_fileindex(fileindex, fileindex_path, worktree);
9255 const struct got_error *
9256 got_worktree_patch_check_path(const char *old, const char *new,
9257 char **oldpath, char **newpath, struct got_worktree *worktree,
9258 struct got_repository *repo, struct got_fileindex *fileindex)
9260 const struct got_error *err = NULL;
9261 int file_renamed = 0;
9262 unsigned char status_old, staged_status_old;
9263 unsigned char status_new, staged_status_new;
9265 *oldpath = NULL;
9266 *newpath = NULL;
9268 err = patch_check_path(old != NULL ? old : new, oldpath,
9269 &status_old, &staged_status_old, fileindex, worktree, repo);
9270 if (err)
9271 goto done;
9273 err = patch_check_path(new != NULL ? new : old, newpath,
9274 &status_new, &staged_status_new, fileindex, worktree, repo);
9275 if (err)
9276 goto done;
9278 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9279 file_renamed = 1;
9281 if (old != NULL && new == NULL)
9282 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9283 else if (file_renamed) {
9284 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9285 if (err == NULL)
9286 err = patch_can_add(*newpath, status_new);
9287 } else if (old == NULL)
9288 err = patch_can_add(*newpath, status_new);
9289 else
9290 err = patch_can_edit(*newpath, status_new, staged_status_new);
9292 done:
9293 if (err) {
9294 free(*oldpath);
9295 *oldpath = NULL;
9296 free(*newpath);
9297 *newpath = NULL;
9299 return err;
9302 const struct got_error *
9303 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9304 struct got_worktree *worktree, struct got_fileindex *fileindex,
9305 got_worktree_checkout_cb progress_cb, void *progress_arg)
9307 struct schedule_addition_args saa;
9309 memset(&saa, 0, sizeof(saa));
9310 saa.worktree = worktree;
9311 saa.fileindex = fileindex;
9312 saa.progress_cb = progress_cb;
9313 saa.progress_arg = progress_arg;
9314 saa.repo = repo;
9316 return worktree_status(worktree, path, fileindex, repo,
9317 schedule_addition, &saa, NULL, NULL, 1, 0);
9320 const struct got_error *
9321 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9322 struct got_worktree *worktree, struct got_fileindex *fileindex,
9323 got_worktree_delete_cb progress_cb, void *progress_arg)
9325 struct schedule_deletion_args sda;
9327 memset(&sda, 0, sizeof(sda));
9328 sda.worktree = worktree;
9329 sda.fileindex = fileindex;
9330 sda.progress_cb = progress_cb;
9331 sda.progress_arg = progress_arg;
9332 sda.repo = repo;
9333 sda.delete_local_mods = 0;
9334 sda.keep_on_disk = 0;
9335 sda.ignore_missing_paths = 0;
9336 sda.status_codes = NULL;
9338 return worktree_status(worktree, path, fileindex, repo,
9339 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9342 const struct got_error *
9343 got_worktree_patch_complete(struct got_fileindex *fileindex,
9344 const char *fileindex_path)
9346 const struct got_error *err = NULL;
9348 err = sync_fileindex(fileindex, fileindex_path);
9349 got_fileindex_free(fileindex);
9351 return err;