Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/stat.h>
18 #include <sys/queue.h>
19 #include <sys/tree.h>
21 #include <dirent.h>
22 #include <limits.h>
23 #include <stddef.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <time.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <sha1.h>
32 #include <sha2.h>
33 #include <zlib.h>
34 #include <fnmatch.h>
35 #include <libgen.h>
36 #include <uuid.h>
37 #include <util.h>
39 #include "got_error.h"
40 #include "got_repository.h"
41 #include "got_reference.h"
42 #include "got_object.h"
43 #include "got_path.h"
44 #include "got_cancel.h"
45 #include "got_worktree.h"
46 #include "got_opentemp.h"
47 #include "got_diff.h"
49 #include "got_lib_worktree.h"
50 #include "got_lib_hash.h"
51 #include "got_lib_fileindex.h"
52 #include "got_lib_inflate.h"
53 #include "got_lib_delta.h"
54 #include "got_lib_object.h"
55 #include "got_lib_object_parse.h"
56 #include "got_lib_object_create.h"
57 #include "got_lib_object_idset.h"
58 #include "got_lib_diff.h"
59 #include "got_lib_gotconfig.h"
61 #ifndef MIN
62 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
63 #endif
65 #define GOT_MERGE_LABEL_MERGED "merged change"
66 #define GOT_MERGE_LABEL_BASE "3-way merge base"
68 static mode_t apply_umask(mode_t);
70 static const struct got_error *
71 create_meta_file(const char *path_got, const char *name, const char *content)
72 {
73 const struct got_error *err = NULL;
74 char *path;
76 if (asprintf(&path, "%s/%s", path_got, name) == -1)
77 return got_error_from_errno("asprintf");
79 err = got_path_create_file(path, content);
80 free(path);
81 return err;
82 }
84 static const struct got_error *
85 update_meta_file(const char *path_got, const char *name, const char *content)
86 {
87 const struct got_error *err = NULL;
88 FILE *tmpfile = NULL;
89 char *tmppath = NULL;
90 char *path = NULL;
92 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
93 err = got_error_from_errno("asprintf");
94 path = NULL;
95 goto done;
96 }
98 err = got_opentemp_named(&tmppath, &tmpfile, path, "");
99 if (err)
100 goto done;
102 if (content) {
103 int len = fprintf(tmpfile, "%s\n", content);
104 if (len != strlen(content) + 1) {
105 err = got_error_from_errno2("fprintf", tmppath);
106 goto done;
110 if (rename(tmppath, path) != 0) {
111 err = got_error_from_errno3("rename", tmppath, path);
112 unlink(tmppath);
113 goto done;
116 done:
117 if (fclose(tmpfile) == EOF && err == NULL)
118 err = got_error_from_errno2("fclose", tmppath);
119 free(tmppath);
120 return err;
123 static const struct got_error *
124 write_head_ref(const char *path_got, struct got_reference *head_ref)
126 const struct got_error *err = NULL;
127 char *refstr = NULL;
129 if (got_ref_is_symbolic(head_ref)) {
130 refstr = got_ref_to_str(head_ref);
131 if (refstr == NULL)
132 return got_error_from_errno("got_ref_to_str");
133 } else {
134 refstr = strdup(got_ref_get_name(head_ref));
135 if (refstr == NULL)
136 return got_error_from_errno("strdup");
138 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
139 free(refstr);
140 return err;
143 const struct got_error *
144 got_worktree_init(const char *path, struct got_reference *head_ref,
145 const char *prefix, struct got_repository *repo)
147 const struct got_error *err = NULL;
148 struct got_object_id *commit_id = NULL;
149 uuid_t uuid;
150 uint32_t uuid_status;
151 int obj_type;
152 char *path_got = NULL;
153 char *formatstr = NULL;
154 char *absprefix = NULL;
155 char *basestr = NULL;
156 char *uuidstr = NULL;
158 if (strcmp(path, got_repo_get_path(repo)) == 0) {
159 err = got_error(GOT_ERR_WORKTREE_REPO);
160 goto done;
163 err = got_ref_resolve(&commit_id, repo, head_ref);
164 if (err)
165 return err;
166 err = got_object_get_type(&obj_type, repo, commit_id);
167 if (err)
168 return err;
169 if (obj_type != GOT_OBJ_TYPE_COMMIT)
170 return got_error(GOT_ERR_OBJ_TYPE);
172 if (!got_path_is_absolute(prefix)) {
173 if (asprintf(&absprefix, "/%s", prefix) == -1)
174 return got_error_from_errno("asprintf");
177 /* Create top-level directory (may already exist). */
178 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
179 err = got_error_from_errno2("mkdir", path);
180 goto done;
183 /* Create .got directory (may already exist). */
184 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
185 err = got_error_from_errno("asprintf");
186 goto done;
188 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
189 err = got_error_from_errno2("mkdir", path_got);
190 goto done;
193 /* Create an empty lock file. */
194 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
195 if (err)
196 goto done;
198 /* Create an empty file index. */
199 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
200 if (err)
201 goto done;
203 /* Write the HEAD reference. */
204 err = write_head_ref(path_got, head_ref);
205 if (err)
206 goto done;
208 /* Record our base commit. */
209 err = got_object_id_str(&basestr, commit_id);
210 if (err)
211 goto done;
212 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
213 if (err)
214 goto done;
216 /* Store path to repository. */
217 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
218 got_repo_get_path(repo));
219 if (err)
220 goto done;
222 /* Store in-repository path prefix. */
223 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
224 absprefix ? absprefix : prefix);
225 if (err)
226 goto done;
228 /* Generate UUID. */
229 uuid_create(&uuid, &uuid_status);
230 if (uuid_status != uuid_s_ok) {
231 err = got_error_uuid(uuid_status, "uuid_create");
232 goto done;
234 uuid_to_string(&uuid, &uuidstr, &uuid_status);
235 if (uuid_status != uuid_s_ok) {
236 err = got_error_uuid(uuid_status, "uuid_to_string");
237 goto done;
239 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
240 if (err)
241 goto done;
243 /* Stamp work tree with format file. */
244 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
245 err = got_error_from_errno("asprintf");
246 goto done;
248 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
249 if (err)
250 goto done;
252 done:
253 free(commit_id);
254 free(path_got);
255 free(formatstr);
256 free(absprefix);
257 free(basestr);
258 free(uuidstr);
259 return err;
262 const struct got_error *
263 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
264 const char *path_prefix)
266 char *absprefix = NULL;
268 if (!got_path_is_absolute(path_prefix)) {
269 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
270 return got_error_from_errno("asprintf");
272 *match = (strcmp(absprefix ? absprefix : path_prefix,
273 worktree->path_prefix) == 0);
274 free(absprefix);
275 return NULL;
278 const char *
279 got_worktree_get_head_ref_name(struct got_worktree *worktree)
281 return worktree->head_ref_name;
284 const struct got_error *
285 got_worktree_set_head_ref(struct got_worktree *worktree,
286 struct got_reference *head_ref)
288 const struct got_error *err = NULL;
289 char *path_got = NULL, *head_ref_name = NULL;
291 if (asprintf(&path_got, "%s/%s", worktree->root_path,
292 GOT_WORKTREE_GOT_DIR) == -1) {
293 err = got_error_from_errno("asprintf");
294 path_got = NULL;
295 goto done;
298 head_ref_name = strdup(got_ref_get_name(head_ref));
299 if (head_ref_name == NULL) {
300 err = got_error_from_errno("strdup");
301 goto done;
304 err = write_head_ref(path_got, head_ref);
305 if (err)
306 goto done;
308 free(worktree->head_ref_name);
309 worktree->head_ref_name = head_ref_name;
310 done:
311 free(path_got);
312 if (err)
313 free(head_ref_name);
314 return err;
317 struct got_object_id *
318 got_worktree_get_base_commit_id(struct got_worktree *worktree)
320 return worktree->base_commit_id;
323 const struct got_error *
324 got_worktree_set_base_commit_id(struct got_worktree *worktree,
325 struct got_repository *repo, struct got_object_id *commit_id)
327 const struct got_error *err;
328 struct got_object *obj = NULL;
329 char *id_str = NULL;
330 char *path_got = NULL;
332 if (asprintf(&path_got, "%s/%s", worktree->root_path,
333 GOT_WORKTREE_GOT_DIR) == -1) {
334 err = got_error_from_errno("asprintf");
335 path_got = NULL;
336 goto done;
339 err = got_object_open(&obj, repo, commit_id);
340 if (err)
341 return err;
343 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
344 err = got_error(GOT_ERR_OBJ_TYPE);
345 goto done;
348 /* Record our base commit. */
349 err = got_object_id_str(&id_str, commit_id);
350 if (err)
351 goto done;
352 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
353 if (err)
354 goto done;
356 free(worktree->base_commit_id);
357 worktree->base_commit_id = got_object_id_dup(commit_id);
358 if (worktree->base_commit_id == NULL) {
359 err = got_error_from_errno("got_object_id_dup");
360 goto done;
362 done:
363 if (obj)
364 got_object_close(obj);
365 free(id_str);
366 free(path_got);
367 return err;
370 const struct got_gotconfig *
371 got_worktree_get_gotconfig(struct got_worktree *worktree)
373 return worktree->gotconfig;
376 static const struct got_error *
377 lock_worktree(struct got_worktree *worktree, int operation)
379 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
380 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
381 : got_error_from_errno2("flock",
382 got_worktree_get_root_path(worktree)));
383 return NULL;
386 static const struct got_error *
387 add_dir_on_disk(struct got_worktree *worktree, const char *path)
389 const struct got_error *err = NULL;
390 char *abspath;
392 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
393 return got_error_from_errno("asprintf");
395 err = got_path_mkdir(abspath);
396 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
397 struct stat sb;
398 err = NULL;
399 if (lstat(abspath, &sb) == -1) {
400 err = got_error_from_errno2("lstat", abspath);
401 } else if (!S_ISDIR(sb.st_mode)) {
402 /* TODO directory is obstructed; do something */
403 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
406 free(abspath);
407 return err;
410 static const struct got_error *
411 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
413 const struct got_error *err = NULL;
414 uint8_t fbuf1[8192];
415 uint8_t fbuf2[8192];
416 size_t flen1 = 0, flen2 = 0;
418 *same = 1;
420 for (;;) {
421 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
422 if (flen1 == 0 && ferror(f1)) {
423 err = got_error_from_errno("fread");
424 break;
426 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
427 if (flen2 == 0 && ferror(f2)) {
428 err = got_error_from_errno("fread");
429 break;
431 if (flen1 == 0) {
432 if (flen2 != 0)
433 *same = 0;
434 break;
435 } else if (flen2 == 0) {
436 if (flen1 != 0)
437 *same = 0;
438 break;
439 } else if (flen1 == flen2) {
440 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
441 *same = 0;
442 break;
444 } else {
445 *same = 0;
446 break;
450 return err;
453 static const struct got_error *
454 check_files_equal(int *same, FILE *f1, FILE *f2)
456 struct stat sb;
457 size_t size1, size2;
459 *same = 1;
461 if (fstat(fileno(f1), &sb) != 0)
462 return got_error_from_errno("fstat");
463 size1 = sb.st_size;
465 if (fstat(fileno(f2), &sb) != 0)
466 return got_error_from_errno("fstat");
467 size2 = sb.st_size;
469 if (size1 != size2) {
470 *same = 0;
471 return NULL;
474 if (fseek(f1, 0L, SEEK_SET) == -1)
475 return got_ferror(f1, GOT_ERR_IO);
476 if (fseek(f2, 0L, SEEK_SET) == -1)
477 return got_ferror(f2, GOT_ERR_IO);
479 return check_file_contents_equal(same, f1, f2);
482 static const struct got_error *
483 copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
485 uint8_t fbuf[65536];
486 size_t flen;
487 ssize_t outlen;
489 *outsize = 0;
491 if (fseek(f, 0L, SEEK_SET) == -1)
492 return got_ferror(f, GOT_ERR_IO);
494 for (;;) {
495 flen = fread(fbuf, 1, sizeof(fbuf), f);
496 if (flen == 0) {
497 if (ferror(f))
498 return got_error_from_errno("fread");
499 if (feof(f))
500 break;
502 outlen = write(outfd, fbuf, flen);
503 if (outlen == -1)
504 return got_error_from_errno("write");
505 if (outlen != flen)
506 return got_error(GOT_ERR_IO);
507 *outsize += outlen;
510 return NULL;
513 static const struct got_error *
514 merge_binary_file(int *overlapcnt, int merged_fd,
515 FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
516 const char *label_deriv, const char *label_orig, const char *label_deriv2,
517 const char *ondisk_path)
519 const struct got_error *err = NULL;
520 int same_content, changed_deriv, changed_deriv2;
521 int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
522 off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
523 char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
524 char *base_path_orig = NULL, *base_path_deriv = NULL;
525 char *base_path_deriv2 = NULL;
527 *overlapcnt = 0;
529 err = check_files_equal(&same_content, f_deriv, f_deriv2);
530 if (err)
531 return err;
533 if (same_content)
534 return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
536 err = check_files_equal(&same_content, f_deriv, f_orig);
537 if (err)
538 return err;
539 changed_deriv = !same_content;
540 err = check_files_equal(&same_content, f_deriv2, f_orig);
541 if (err)
542 return err;
543 changed_deriv2 = !same_content;
545 if (changed_deriv && changed_deriv2) {
546 *overlapcnt = 1;
547 if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
548 err = got_error_from_errno("asprintf");
549 goto done;
551 if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
552 err = got_error_from_errno("asprintf");
553 goto done;
555 if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
556 err = got_error_from_errno("asprintf");
557 goto done;
559 err = got_opentemp_named_fd(&path_orig, &fd_orig,
560 base_path_orig, "");
561 if (err)
562 goto done;
563 err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
564 base_path_deriv, "");
565 if (err)
566 goto done;
567 err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
568 base_path_deriv2, "");
569 if (err)
570 goto done;
571 err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
572 if (err)
573 goto done;
574 err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
575 if (err)
576 goto done;
577 err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
578 if (err)
579 goto done;
580 if (dprintf(merged_fd, "Binary files differ and cannot be "
581 "merged automatically:\n") < 0) {
582 err = got_error_from_errno("dprintf");
583 goto done;
585 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
586 GOT_DIFF_CONFLICT_MARKER_BEGIN,
587 label_deriv ? " " : "",
588 label_deriv ? label_deriv : "",
589 path_deriv) < 0) {
590 err = got_error_from_errno("dprintf");
591 goto done;
593 if (size_orig > 0) {
594 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
595 GOT_DIFF_CONFLICT_MARKER_ORIG,
596 label_orig ? " " : "",
597 label_orig ? label_orig : "",
598 path_orig) < 0) {
599 err = got_error_from_errno("dprintf");
600 goto done;
603 if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
604 GOT_DIFF_CONFLICT_MARKER_SEP,
605 path_deriv2,
606 GOT_DIFF_CONFLICT_MARKER_END,
607 label_deriv2 ? " " : "",
608 label_deriv2 ? label_deriv2 : "") < 0) {
609 err = got_error_from_errno("dprintf");
610 goto done;
612 } else if (changed_deriv)
613 err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
614 else if (changed_deriv2)
615 err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
616 done:
617 if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
618 err == NULL)
619 err = got_error_from_errno2("unlink", path_orig);
620 if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
621 err = got_error_from_errno2("close", path_orig);
622 if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
623 err = got_error_from_errno2("close", path_deriv);
624 if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
625 err = got_error_from_errno2("close", path_deriv2);
626 free(path_orig);
627 free(path_deriv);
628 free(path_deriv2);
629 free(base_path_orig);
630 free(base_path_deriv);
631 free(base_path_deriv2);
632 return err;
635 /*
636 * Perform a 3-way merge where the file f_orig acts as the common
637 * ancestor, the file f_deriv acts as the first derived version,
638 * and the file f_deriv2 acts as the second derived version.
639 * The merge result will be written to a new file at ondisk_path; any
640 * existing file at this path will be replaced.
641 */
642 static const struct got_error *
643 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
644 FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
645 const char *path, uint16_t st_mode,
646 const char *label_orig, const char *label_deriv, const char *label_deriv2,
647 enum got_diff_algorithm diff_algo, struct got_repository *repo,
648 got_worktree_checkout_cb progress_cb, void *progress_arg)
650 const struct got_error *err = NULL;
651 int merged_fd = -1;
652 FILE *f_merged = NULL;
653 char *merged_path = NULL, *base_path = NULL;
654 int overlapcnt = 0;
655 char *parent = NULL;
657 *local_changes_subsumed = 0;
659 err = got_path_dirname(&parent, ondisk_path);
660 if (err)
661 return err;
663 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
664 err = got_error_from_errno("asprintf");
665 goto done;
668 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path, "");
669 if (err)
670 goto done;
672 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
673 f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
674 if (err) {
675 if (err->code != GOT_ERR_FILE_BINARY)
676 goto done;
677 err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
678 f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
679 ondisk_path);
680 if (err)
681 goto done;
684 err = (*progress_cb)(progress_arg,
685 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
686 if (err)
687 goto done;
689 if (fsync(merged_fd) != 0) {
690 err = got_error_from_errno("fsync");
691 goto done;
694 f_merged = fdopen(merged_fd, "r");
695 if (f_merged == NULL) {
696 err = got_error_from_errno("fdopen");
697 goto done;
699 merged_fd = -1;
701 /* Check if a clean merge has subsumed all local changes. */
702 if (overlapcnt == 0) {
703 err = check_files_equal(local_changes_subsumed, f_deriv,
704 f_merged);
705 if (err)
706 goto done;
709 if (fchmod(fileno(f_merged), apply_umask(st_mode)) != 0) {
710 err = got_error_from_errno2("fchmod", merged_path);
711 goto done;
714 if (rename(merged_path, ondisk_path) != 0) {
715 err = got_error_from_errno3("rename", merged_path,
716 ondisk_path);
717 goto done;
719 done:
720 if (err) {
721 if (merged_path)
722 unlink(merged_path);
724 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
725 err = got_error_from_errno("close");
726 if (f_merged && fclose(f_merged) == EOF && err == NULL)
727 err = got_error_from_errno("fclose");
728 free(merged_path);
729 free(base_path);
730 free(parent);
731 return err;
734 static const struct got_error *
735 update_symlink(const char *ondisk_path, const char *target_path,
736 size_t target_len)
738 /* This is not atomic but matches what 'ln -sf' does. */
739 if (unlink(ondisk_path) == -1)
740 return got_error_from_errno2("unlink", ondisk_path);
741 if (symlink(target_path, ondisk_path) == -1)
742 return got_error_from_errno3("symlink", target_path,
743 ondisk_path);
744 return NULL;
747 /*
748 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
749 * in the work tree with a file that contains conflict markers and the
750 * conflicting target paths of the original version, a "derived version"
751 * of a symlink from an incoming change, and a local version of the symlink.
753 * The original versions's target path can be NULL if it is not available,
754 * such as if both derived versions added a new symlink at the same path.
756 * The incoming derived symlink target is NULL in case the incoming change
757 * has deleted this symlink.
758 */
759 static const struct got_error *
760 install_symlink_conflict(const char *deriv_target,
761 struct got_object_id *deriv_base_commit_id, const char *orig_target,
762 const char *label_orig, const char *local_target, const char *ondisk_path)
764 const struct got_error *err;
765 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
766 FILE *f = NULL;
768 err = got_object_id_str(&id_str, deriv_base_commit_id);
769 if (err)
770 return got_error_from_errno("asprintf");
772 if (asprintf(&label_deriv, "%s: commit %s",
773 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
774 err = got_error_from_errno("asprintf");
775 goto done;
778 err = got_opentemp_named(&path, &f, "got-symlink-conflict", "");
779 if (err)
780 goto done;
782 if (fchmod(fileno(f), apply_umask(GOT_DEFAULT_FILE_MODE)) == -1) {
783 err = got_error_from_errno2("fchmod", path);
784 goto done;
787 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
788 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
789 deriv_target ? deriv_target : "(symlink was deleted)",
790 orig_target ? label_orig : "",
791 orig_target ? "\n" : "",
792 orig_target ? orig_target : "",
793 orig_target ? "\n" : "",
794 GOT_DIFF_CONFLICT_MARKER_SEP,
795 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
796 err = got_error_from_errno2("fprintf", path);
797 goto done;
800 if (unlink(ondisk_path) == -1) {
801 err = got_error_from_errno2("unlink", ondisk_path);
802 goto done;
804 if (rename(path, ondisk_path) == -1) {
805 err = got_error_from_errno3("rename", path, ondisk_path);
806 goto done;
808 done:
809 if (f != NULL && fclose(f) == EOF && err == NULL)
810 err = got_error_from_errno2("fclose", path);
811 free(path);
812 free(id_str);
813 free(label_deriv);
814 return err;
817 /* forward declaration */
818 static const struct got_error *
819 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
820 const char *, const char *, uint16_t, const char *,
821 struct got_blob_object *, struct got_object_id *,
822 struct got_repository *, got_worktree_checkout_cb, void *);
824 /*
825 * Merge a symlink into the work tree, where blob_orig acts as the common
826 * ancestor, deriv_target is the link target of the first derived version,
827 * and the symlink on disk acts as the second derived version.
828 * Assume that contents of both blobs represent symlinks.
829 */
830 static const struct got_error *
831 merge_symlink(struct got_worktree *worktree,
832 struct got_blob_object *blob_orig, const char *ondisk_path,
833 const char *path, const char *label_orig, const char *deriv_target,
834 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
835 got_worktree_checkout_cb progress_cb, void *progress_arg)
837 const struct got_error *err = NULL;
838 char *ancestor_target = NULL;
839 struct stat sb;
840 ssize_t ondisk_len, deriv_len;
841 char ondisk_target[PATH_MAX];
842 int have_local_change = 0;
843 int have_incoming_change = 0;
845 if (lstat(ondisk_path, &sb) == -1)
846 return got_error_from_errno2("lstat", ondisk_path);
848 ondisk_len = readlink(ondisk_path, ondisk_target,
849 sizeof(ondisk_target));
850 if (ondisk_len == -1) {
851 err = got_error_from_errno2("readlink",
852 ondisk_path);
853 goto done;
855 ondisk_target[ondisk_len] = '\0';
857 if (blob_orig) {
858 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
859 if (err)
860 goto done;
863 if (ancestor_target == NULL ||
864 (ondisk_len != strlen(ancestor_target) ||
865 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
866 have_local_change = 1;
868 deriv_len = strlen(deriv_target);
869 if (ancestor_target == NULL ||
870 (deriv_len != strlen(ancestor_target) ||
871 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
872 have_incoming_change = 1;
874 if (!have_local_change && !have_incoming_change) {
875 if (ancestor_target) {
876 /* Both sides made the same change. */
877 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
878 path);
879 } else if (deriv_len == ondisk_len &&
880 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
881 /* Both sides added the same symlink. */
882 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
883 path);
884 } else {
885 /* Both sides added symlinks which don't match. */
886 err = install_symlink_conflict(deriv_target,
887 deriv_base_commit_id, ancestor_target,
888 label_orig, ondisk_target, ondisk_path);
889 if (err)
890 goto done;
891 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
892 path);
894 } else if (!have_local_change && have_incoming_change) {
895 /* Apply the incoming change. */
896 err = update_symlink(ondisk_path, deriv_target,
897 strlen(deriv_target));
898 if (err)
899 goto done;
900 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
901 } else if (have_local_change && have_incoming_change) {
902 if (deriv_len == ondisk_len &&
903 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
904 /* Both sides made the same change. */
905 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
906 path);
907 } else {
908 err = install_symlink_conflict(deriv_target,
909 deriv_base_commit_id, ancestor_target, label_orig,
910 ondisk_target, ondisk_path);
911 if (err)
912 goto done;
913 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
914 path);
918 done:
919 free(ancestor_target);
920 return err;
923 static const struct got_error *
924 dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
926 const struct got_error *err = NULL;
927 char target_path[PATH_MAX];
928 ssize_t target_len;
929 size_t n;
930 FILE *f;
932 *outfile = NULL;
934 f = got_opentemp();
935 if (f == NULL)
936 return got_error_from_errno("got_opentemp");
937 target_len = readlink(ondisk_path, target_path, sizeof(target_path));
938 if (target_len == -1) {
939 err = got_error_from_errno2("readlink", ondisk_path);
940 goto done;
942 n = fwrite(target_path, 1, target_len, f);
943 if (n != target_len) {
944 err = got_ferror(f, GOT_ERR_IO);
945 goto done;
947 if (fflush(f) == EOF) {
948 err = got_error_from_errno("fflush");
949 goto done;
951 if (fseek(f, 0L, SEEK_SET) == -1) {
952 err = got_ferror(f, GOT_ERR_IO);
953 goto done;
955 done:
956 if (err)
957 fclose(f);
958 else
959 *outfile = f;
960 return err;
963 /*
964 * Perform a 3-way merge where blob_orig acts as the common ancestor,
965 * blob_deriv acts as the first derived version, and the file on disk
966 * acts as the second derived version.
967 */
968 static const struct got_error *
969 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
970 struct got_blob_object *blob_orig, const char *ondisk_path,
971 const char *path, uint16_t st_mode, const char *label_orig,
972 struct got_blob_object *blob_deriv,
973 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
974 got_worktree_checkout_cb progress_cb, void *progress_arg)
976 const struct got_error *err = NULL;
977 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
978 char *blob_orig_path = NULL;
979 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
980 char *label_deriv = NULL, *parent = NULL;
982 *local_changes_subsumed = 0;
984 err = got_path_dirname(&parent, ondisk_path);
985 if (err)
986 return err;
988 if (blob_orig) {
989 if (asprintf(&base_path, "%s/got-merge-blob-orig",
990 parent) == -1) {
991 err = got_error_from_errno("asprintf");
992 base_path = NULL;
993 goto done;
996 err = got_opentemp_named(&blob_orig_path, &f_orig,
997 base_path, "");
998 if (err)
999 goto done;
1000 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1001 blob_orig);
1002 if (err)
1003 goto done;
1004 free(base_path);
1005 } else {
1007 * No common ancestor exists. This is an "add vs add" conflict
1008 * and we simply use an empty ancestor file to make both files
1009 * appear in the merged result in their entirety.
1011 f_orig = got_opentemp();
1012 if (f_orig == NULL) {
1013 err = got_error_from_errno("got_opentemp");
1014 goto done;
1018 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1019 err = got_error_from_errno("asprintf");
1020 base_path = NULL;
1021 goto done;
1024 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path, "");
1025 if (err)
1026 goto done;
1027 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1028 blob_deriv);
1029 if (err)
1030 goto done;
1032 err = got_object_id_str(&id_str, deriv_base_commit_id);
1033 if (err)
1034 goto done;
1035 if (asprintf(&label_deriv, "%s: commit %s",
1036 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1037 err = got_error_from_errno("asprintf");
1038 goto done;
1042 * In order the run a 3-way merge with a symlink we copy the symlink's
1043 * target path into a temporary file and use that file with diff3.
1045 if (S_ISLNK(st_mode)) {
1046 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1047 if (err)
1048 goto done;
1049 } else {
1050 int fd;
1051 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1052 if (fd == -1) {
1053 err = got_error_from_errno2("open", ondisk_path);
1054 goto done;
1056 f_deriv2 = fdopen(fd, "r");
1057 if (f_deriv2 == NULL) {
1058 err = got_error_from_errno2("fdopen", ondisk_path);
1059 close(fd);
1060 goto done;
1064 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1065 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1066 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1067 done:
1068 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1069 err = got_error_from_errno("fclose");
1070 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1071 err = got_error_from_errno("fclose");
1072 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1073 err = got_error_from_errno("fclose");
1074 free(base_path);
1075 if (blob_orig_path) {
1076 unlink(blob_orig_path);
1077 free(blob_orig_path);
1079 if (blob_deriv_path) {
1080 unlink(blob_deriv_path);
1081 free(blob_deriv_path);
1083 free(id_str);
1084 free(label_deriv);
1085 free(parent);
1086 return err;
1089 static const struct got_error *
1090 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1091 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1092 int wt_fd, const char *path, struct got_object_id *blob_id)
1094 const struct got_error *err = NULL;
1095 struct got_fileindex_entry *new_ie;
1097 *new_iep = NULL;
1099 err = got_fileindex_entry_alloc(&new_ie, path);
1100 if (err)
1101 return err;
1103 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1104 blob_id->sha1, base_commit_id->sha1, 1);
1105 if (err)
1106 goto done;
1108 err = got_fileindex_entry_add(fileindex, new_ie);
1109 done:
1110 if (err)
1111 got_fileindex_entry_free(new_ie);
1112 else
1113 *new_iep = new_ie;
1114 return err;
1117 static mode_t
1118 get_ondisk_perms(int executable, mode_t st_mode)
1120 mode_t xbits = S_IXUSR;
1122 if (executable) {
1123 /* Map read bits to execute bits. */
1124 if (st_mode & S_IRGRP)
1125 xbits |= S_IXGRP;
1126 if (st_mode & S_IROTH)
1127 xbits |= S_IXOTH;
1128 return st_mode | xbits;
1131 return st_mode;
1134 static mode_t
1135 apply_umask(mode_t mode)
1137 mode_t um;
1139 um = umask(000);
1140 umask(um);
1141 return mode & ~um;
1144 /* forward declaration */
1145 static const struct got_error *
1146 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1147 const char *path, mode_t te_mode, mode_t st_mode,
1148 struct got_blob_object *blob, int restoring_missing_file,
1149 int reverting_versioned_file, int installing_bad_symlink,
1150 int path_is_unversioned, struct got_repository *repo,
1151 got_worktree_checkout_cb progress_cb, void *progress_arg);
1154 * This function assumes that the provided symlink target points at a
1155 * safe location in the work tree!
1157 static const struct got_error *
1158 replace_existing_symlink(int *did_something, const char *ondisk_path,
1159 const char *target_path, size_t target_len)
1161 const struct got_error *err = NULL;
1162 ssize_t elen;
1163 char etarget[PATH_MAX];
1164 int fd;
1166 *did_something = 0;
1169 * "Bad" symlinks (those pointing outside the work tree or into the
1170 * .got directory) are installed in the work tree as a regular file
1171 * which contains the bad symlink target path.
1172 * The new symlink target has already been checked for safety by our
1173 * caller. If we can successfully open a regular file then we simply
1174 * replace this file with a symlink below.
1176 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1177 if (fd == -1) {
1178 if (!got_err_open_nofollow_on_symlink())
1179 return got_error_from_errno2("open", ondisk_path);
1181 /* We are updating an existing on-disk symlink. */
1182 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1183 if (elen == -1)
1184 return got_error_from_errno2("readlink", ondisk_path);
1186 if (elen == target_len &&
1187 memcmp(etarget, target_path, target_len) == 0)
1188 return NULL; /* nothing to do */
1191 *did_something = 1;
1192 err = update_symlink(ondisk_path, target_path, target_len);
1193 if (fd != -1 && close(fd) == -1 && err == NULL)
1194 err = got_error_from_errno2("close", ondisk_path);
1195 return err;
1198 static const struct got_error *
1199 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1200 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1202 const struct got_error *err = NULL;
1203 char canonpath[PATH_MAX];
1204 char *path_got = NULL;
1206 *is_bad_symlink = 0;
1208 if (target_len >= sizeof(canonpath)) {
1209 *is_bad_symlink = 1;
1210 return NULL;
1214 * We do not use realpath(3) to resolve the symlink's target
1215 * path because we don't want to resolve symlinks recursively.
1216 * Instead we make the path absolute and then canonicalize it.
1217 * Relative symlink target lookup should begin at the directory
1218 * in which the blob object is being installed.
1220 if (!got_path_is_absolute(target_path)) {
1221 char *abspath, *parent;
1222 err = got_path_dirname(&parent, ondisk_path);
1223 if (err)
1224 return err;
1225 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1226 free(parent);
1227 return got_error_from_errno("asprintf");
1229 free(parent);
1230 if (strlen(abspath) >= sizeof(canonpath)) {
1231 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1232 free(abspath);
1233 return err;
1235 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1236 free(abspath);
1237 if (err)
1238 return err;
1239 } else {
1240 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1241 if (err)
1242 return err;
1245 /* Only allow symlinks pointing at paths within the work tree. */
1246 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1247 *is_bad_symlink = 1;
1248 return NULL;
1251 /* Do not allow symlinks pointing into the .got directory. */
1252 if (asprintf(&path_got, "%s/%s", wtroot_path,
1253 GOT_WORKTREE_GOT_DIR) == -1)
1254 return got_error_from_errno("asprintf");
1255 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1256 *is_bad_symlink = 1;
1258 free(path_got);
1259 return NULL;
1262 static const struct got_error *
1263 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1264 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1265 int restoring_missing_file, int reverting_versioned_file,
1266 int path_is_unversioned, int allow_bad_symlinks,
1267 struct got_repository *repo,
1268 got_worktree_checkout_cb progress_cb, void *progress_arg)
1270 const struct got_error *err = NULL;
1271 char target_path[PATH_MAX];
1272 size_t len, target_len = 0;
1273 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1274 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1276 *is_bad_symlink = 0;
1279 * Blob object content specifies the target path of the link.
1280 * If a symbolic link cannot be installed we instead create
1281 * a regular file which contains the link target path stored
1282 * in the blob object.
1284 do {
1285 err = got_object_blob_read_block(&len, blob);
1286 if (err)
1287 return err;
1289 if (len + target_len >= sizeof(target_path)) {
1290 /* Path too long; install as a regular file. */
1291 *is_bad_symlink = 1;
1292 got_object_blob_rewind(blob);
1293 return install_blob(worktree, ondisk_path, path,
1294 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1295 restoring_missing_file, reverting_versioned_file,
1296 1, path_is_unversioned, repo, progress_cb,
1297 progress_arg);
1299 if (len > 0) {
1300 /* Skip blob object header first time around. */
1301 memcpy(target_path + target_len, buf + hdrlen,
1302 len - hdrlen);
1303 target_len += len - hdrlen;
1304 hdrlen = 0;
1306 } while (len != 0);
1307 target_path[target_len] = '\0';
1309 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1310 ondisk_path, worktree->root_path);
1311 if (err)
1312 return err;
1314 if (*is_bad_symlink && !allow_bad_symlinks) {
1315 /* install as a regular file */
1316 got_object_blob_rewind(blob);
1317 err = install_blob(worktree, ondisk_path, path,
1318 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1319 restoring_missing_file, reverting_versioned_file, 1,
1320 path_is_unversioned, repo, progress_cb, progress_arg);
1321 return err;
1324 if (symlink(target_path, ondisk_path) == -1) {
1325 if (errno == EEXIST) {
1326 int symlink_replaced;
1327 if (path_is_unversioned) {
1328 err = (*progress_cb)(progress_arg,
1329 GOT_STATUS_UNVERSIONED, path);
1330 return err;
1332 err = replace_existing_symlink(&symlink_replaced,
1333 ondisk_path, target_path, target_len);
1334 if (err)
1335 return err;
1336 if (progress_cb) {
1337 if (symlink_replaced) {
1338 err = (*progress_cb)(progress_arg,
1339 reverting_versioned_file ?
1340 GOT_STATUS_REVERT :
1341 GOT_STATUS_UPDATE, path);
1342 } else {
1343 err = (*progress_cb)(progress_arg,
1344 GOT_STATUS_EXISTS, path);
1347 return err; /* Nothing else to do. */
1350 if (errno == ENOENT) {
1351 char *parent;
1352 err = got_path_dirname(&parent, ondisk_path);
1353 if (err)
1354 return err;
1355 err = add_dir_on_disk(worktree, parent);
1356 free(parent);
1357 if (err)
1358 return err;
1360 * Retry, and fall through to error handling
1361 * below if this second attempt fails.
1363 if (symlink(target_path, ondisk_path) != -1) {
1364 err = NULL; /* success */
1365 return err;
1369 /* Handle errors from first or second creation attempt. */
1370 if (errno == ENAMETOOLONG) {
1371 /* bad target path; install as a regular file */
1372 *is_bad_symlink = 1;
1373 got_object_blob_rewind(blob);
1374 err = install_blob(worktree, ondisk_path, path,
1375 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1376 restoring_missing_file, reverting_versioned_file, 1,
1377 path_is_unversioned, repo,
1378 progress_cb, progress_arg);
1379 } else if (errno == ENOTDIR) {
1380 err = got_error_path(ondisk_path,
1381 GOT_ERR_FILE_OBSTRUCTED);
1382 } else {
1383 err = got_error_from_errno3("symlink",
1384 target_path, ondisk_path);
1386 } else if (progress_cb)
1387 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1388 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1389 return err;
1392 static const struct got_error *
1393 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1394 const char *path, mode_t te_mode, mode_t st_mode,
1395 struct got_blob_object *blob, int restoring_missing_file,
1396 int reverting_versioned_file, int installing_bad_symlink,
1397 int path_is_unversioned, struct got_repository *repo,
1398 got_worktree_checkout_cb progress_cb, void *progress_arg)
1400 const struct got_error *err = NULL;
1401 int fd = -1;
1402 size_t len, hdrlen;
1403 int update = 0;
1404 char *tmppath = NULL;
1405 mode_t mode;
1407 mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1408 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1409 O_CLOEXEC, mode);
1410 if (fd == -1) {
1411 if (errno == ENOENT) {
1412 char *parent;
1413 err = got_path_dirname(&parent, path);
1414 if (err)
1415 return err;
1416 err = add_dir_on_disk(worktree, parent);
1417 free(parent);
1418 if (err)
1419 return err;
1420 fd = open(ondisk_path,
1421 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1422 mode);
1423 if (fd == -1)
1424 return got_error_from_errno2("open",
1425 ondisk_path);
1426 } else if (errno == EEXIST) {
1427 if (path_is_unversioned) {
1428 err = (*progress_cb)(progress_arg,
1429 GOT_STATUS_UNVERSIONED, path);
1430 goto done;
1432 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1433 !S_ISREG(st_mode) && !installing_bad_symlink) {
1434 /* TODO file is obstructed; do something */
1435 err = got_error_path(ondisk_path,
1436 GOT_ERR_FILE_OBSTRUCTED);
1437 goto done;
1438 } else {
1439 err = got_opentemp_named_fd(&tmppath, &fd,
1440 ondisk_path, "");
1441 if (err)
1442 goto done;
1443 update = 1;
1445 if (fchmod(fd, apply_umask(mode)) == -1) {
1446 err = got_error_from_errno2("fchmod",
1447 tmppath);
1448 goto done;
1451 } else
1452 return got_error_from_errno2("open", ondisk_path);
1455 if (progress_cb) {
1456 if (restoring_missing_file)
1457 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1458 path);
1459 else if (reverting_versioned_file)
1460 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1461 path);
1462 else
1463 err = (*progress_cb)(progress_arg,
1464 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1465 if (err)
1466 goto done;
1469 hdrlen = got_object_blob_get_hdrlen(blob);
1470 do {
1471 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1472 err = got_object_blob_read_block(&len, blob);
1473 if (err)
1474 break;
1475 if (len > 0) {
1476 /* Skip blob object header first time around. */
1477 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1478 if (outlen == -1) {
1479 err = got_error_from_errno("write");
1480 goto done;
1481 } else if (outlen != len - hdrlen) {
1482 err = got_error(GOT_ERR_IO);
1483 goto done;
1485 hdrlen = 0;
1487 } while (len != 0);
1489 if (fsync(fd) != 0) {
1490 err = got_error_from_errno("fsync");
1491 goto done;
1494 if (update) {
1495 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1496 err = got_error_from_errno2("unlink", ondisk_path);
1497 goto done;
1499 if (rename(tmppath, ondisk_path) != 0) {
1500 err = got_error_from_errno3("rename", tmppath,
1501 ondisk_path);
1502 goto done;
1504 free(tmppath);
1505 tmppath = NULL;
1508 done:
1509 if (fd != -1 && close(fd) == -1 && err == NULL)
1510 err = got_error_from_errno("close");
1511 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1512 err = got_error_from_errno2("unlink", tmppath);
1513 free(tmppath);
1514 return err;
1518 * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
1519 * conflict marker is found in newly added lines only.
1521 static const struct got_error *
1522 get_modified_file_content_status(unsigned char *status,
1523 struct got_blob_object *blob, const char *path, struct stat *sb,
1524 FILE *ondisk_file)
1526 const struct got_error *err, *free_err;
1527 const char *markers[3] = {
1528 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1529 GOT_DIFF_CONFLICT_MARKER_SEP,
1530 GOT_DIFF_CONFLICT_MARKER_END
1532 FILE *f1 = NULL;
1533 struct got_diffreg_result *diffreg_result = NULL;
1534 struct diff_result *r;
1535 int nchunks_parsed, n, i = 0, ln = 0;
1536 char *line = NULL;
1537 size_t linesize = 0;
1538 ssize_t linelen;
1540 if (*status != GOT_STATUS_MODIFY)
1541 return NULL;
1543 f1 = got_opentemp();
1544 if (f1 == NULL)
1545 return got_error_from_errno("got_opentemp");
1547 if (blob) {
1548 got_object_blob_rewind(blob);
1549 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
1550 if (err)
1551 goto done;
1554 err = got_diff_files(&diffreg_result, f1, 1, NULL, ondisk_file,
1555 1, NULL, 0, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
1556 if (err)
1557 goto done;
1559 r = diffreg_result->result;
1561 for (n = 0; n < r->chunks.len; n += nchunks_parsed) {
1562 struct diff_chunk *c;
1563 struct diff_chunk_context cc = {};
1564 off_t pos;
1567 * We can optimise a little by advancing straight
1568 * to the next chunk if this one has no added lines.
1570 c = diff_chunk_get(r, n);
1572 if (diff_chunk_type(c) != CHUNK_PLUS) {
1573 nchunks_parsed = 1;
1574 continue; /* removed or unchanged lines */
1577 pos = diff_chunk_get_right_start_pos(c);
1578 if (fseek(ondisk_file, pos, SEEK_SET) == -1) {
1579 err = got_ferror(ondisk_file, GOT_ERR_IO);
1580 goto done;
1583 diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
1584 ln = cc.right.start;
1586 while (ln < cc.right.end) {
1587 linelen = getline(&line, &linesize, ondisk_file);
1588 if (linelen == -1) {
1589 if (feof(ondisk_file))
1590 break;
1591 err = got_ferror(ondisk_file, GOT_ERR_IO);
1592 break;
1595 if (line && strncmp(line, markers[i],
1596 strlen(markers[i])) == 0) {
1597 if (strcmp(markers[i],
1598 GOT_DIFF_CONFLICT_MARKER_END) == 0) {
1599 *status = GOT_STATUS_CONFLICT;
1600 goto done;
1601 } else
1602 i++;
1604 ++ln;
1608 done:
1609 free(line);
1610 if (f1 != NULL && fclose(f1) == EOF && err == NULL)
1611 err = got_error_from_errno("fclose");
1612 free_err = got_diffreg_result_free(diffreg_result);
1613 if (err == NULL)
1614 err = free_err;
1616 return err;
1619 static int
1620 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1622 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1623 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1626 static int
1627 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1629 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1630 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1631 ie->mtime_sec == sb->st_mtim.tv_sec &&
1632 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1633 ie->size == (sb->st_size & 0xffffffff) &&
1634 !xbit_differs(ie, sb->st_mode));
1637 static unsigned char
1638 get_staged_status(struct got_fileindex_entry *ie)
1640 switch (got_fileindex_entry_stage_get(ie)) {
1641 case GOT_FILEIDX_STAGE_ADD:
1642 return GOT_STATUS_ADD;
1643 case GOT_FILEIDX_STAGE_DELETE:
1644 return GOT_STATUS_DELETE;
1645 case GOT_FILEIDX_STAGE_MODIFY:
1646 return GOT_STATUS_MODIFY;
1647 default:
1648 return GOT_STATUS_NO_CHANGE;
1652 static const struct got_error *
1653 get_symlink_modification_status(unsigned char *status,
1654 struct got_fileindex_entry *ie, const char *abspath,
1655 int dirfd, const char *de_name, struct got_blob_object *blob)
1657 const struct got_error *err = NULL;
1658 char target_path[PATH_MAX];
1659 char etarget[PATH_MAX];
1660 ssize_t elen;
1661 size_t len, target_len = 0;
1662 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1663 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1665 *status = GOT_STATUS_NO_CHANGE;
1667 /* Blob object content specifies the target path of the link. */
1668 do {
1669 err = got_object_blob_read_block(&len, blob);
1670 if (err)
1671 return err;
1672 if (len + target_len >= sizeof(target_path)) {
1674 * Should not happen. The blob contents were OK
1675 * when this symlink was installed.
1677 return got_error(GOT_ERR_NO_SPACE);
1679 if (len > 0) {
1680 /* Skip blob object header first time around. */
1681 memcpy(target_path + target_len, buf + hdrlen,
1682 len - hdrlen);
1683 target_len += len - hdrlen;
1684 hdrlen = 0;
1686 } while (len != 0);
1687 target_path[target_len] = '\0';
1689 if (dirfd != -1) {
1690 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1691 if (elen == -1)
1692 return got_error_from_errno2("readlinkat", abspath);
1693 } else {
1694 elen = readlink(abspath, etarget, sizeof(etarget));
1695 if (elen == -1)
1696 return got_error_from_errno2("readlink", abspath);
1699 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1700 *status = GOT_STATUS_MODIFY;
1702 return NULL;
1705 static const struct got_error *
1706 get_file_status(unsigned char *status, struct stat *sb,
1707 struct got_fileindex_entry *ie, const char *abspath,
1708 int dirfd, const char *de_name, struct got_repository *repo)
1710 const struct got_error *err = NULL;
1711 struct got_object_id id;
1712 size_t hdrlen;
1713 int fd = -1, fd1 = -1;
1714 FILE *f = NULL;
1715 uint8_t fbuf[8192];
1716 struct got_blob_object *blob = NULL;
1717 size_t flen, blen;
1718 unsigned char staged_status;
1720 staged_status = get_staged_status(ie);
1721 *status = GOT_STATUS_NO_CHANGE;
1722 memset(sb, 0, sizeof(*sb));
1725 * Whenever the caller provides a directory descriptor and a
1726 * directory entry name for the file, use them! This prevents
1727 * race conditions if filesystem paths change beneath our feet.
1729 if (dirfd != -1) {
1730 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1731 if (errno == ENOENT) {
1732 if (got_fileindex_entry_has_file_on_disk(ie))
1733 *status = GOT_STATUS_MISSING;
1734 else
1735 *status = GOT_STATUS_DELETE;
1736 goto done;
1738 err = got_error_from_errno2("fstatat", abspath);
1739 goto done;
1741 } else {
1742 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1743 if (fd == -1 && errno != ENOENT &&
1744 !got_err_open_nofollow_on_symlink())
1745 return got_error_from_errno2("open", abspath);
1746 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1747 if (lstat(abspath, sb) == -1)
1748 return got_error_from_errno2("lstat", abspath);
1749 } else if (fd == -1 || fstat(fd, sb) == -1) {
1750 if (errno == ENOENT) {
1751 if (got_fileindex_entry_has_file_on_disk(ie))
1752 *status = GOT_STATUS_MISSING;
1753 else
1754 *status = GOT_STATUS_DELETE;
1755 goto done;
1757 err = got_error_from_errno2("fstat", abspath);
1758 goto done;
1762 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1763 *status = GOT_STATUS_OBSTRUCTED;
1764 goto done;
1767 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1768 *status = GOT_STATUS_DELETE;
1769 goto done;
1770 } else if (!got_fileindex_entry_has_blob(ie) &&
1771 staged_status != GOT_STATUS_ADD) {
1772 *status = GOT_STATUS_ADD;
1773 goto done;
1776 if (!stat_info_differs(ie, sb))
1777 goto done;
1779 if (S_ISLNK(sb->st_mode) &&
1780 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1781 *status = GOT_STATUS_MODIFY;
1782 goto done;
1785 if (staged_status == GOT_STATUS_MODIFY ||
1786 staged_status == GOT_STATUS_ADD)
1787 got_fileindex_entry_get_staged_blob_id(&id, ie);
1788 else
1789 got_fileindex_entry_get_blob_id(&id, ie);
1791 fd1 = got_opentempfd();
1792 if (fd1 == -1) {
1793 err = got_error_from_errno("got_opentempfd");
1794 goto done;
1796 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1797 if (err)
1798 goto done;
1800 if (S_ISLNK(sb->st_mode)) {
1801 err = get_symlink_modification_status(status, ie,
1802 abspath, dirfd, de_name, blob);
1803 goto done;
1806 if (dirfd != -1) {
1807 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1808 if (fd == -1) {
1809 err = got_error_from_errno2("openat", abspath);
1810 goto done;
1814 f = fdopen(fd, "r");
1815 if (f == NULL) {
1816 err = got_error_from_errno2("fdopen", abspath);
1817 goto done;
1819 fd = -1;
1820 hdrlen = got_object_blob_get_hdrlen(blob);
1821 for (;;) {
1822 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1823 err = got_object_blob_read_block(&blen, blob);
1824 if (err)
1825 goto done;
1826 /* Skip length of blob object header first time around. */
1827 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1828 if (flen == 0 && ferror(f)) {
1829 err = got_error_from_errno("fread");
1830 goto done;
1832 if (blen - hdrlen == 0) {
1833 if (flen != 0)
1834 *status = GOT_STATUS_MODIFY;
1835 break;
1836 } else if (flen == 0) {
1837 if (blen - hdrlen != 0)
1838 *status = GOT_STATUS_MODIFY;
1839 break;
1840 } else if (blen - hdrlen == flen) {
1841 /* Skip blob object header first time around. */
1842 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1843 *status = GOT_STATUS_MODIFY;
1844 break;
1846 } else {
1847 *status = GOT_STATUS_MODIFY;
1848 break;
1850 hdrlen = 0;
1853 if (*status == GOT_STATUS_MODIFY) {
1854 rewind(f);
1855 err = get_modified_file_content_status(status, blob, ie->path,
1856 sb, f);
1857 } else if (xbit_differs(ie, sb->st_mode))
1858 *status = GOT_STATUS_MODE_CHANGE;
1859 done:
1860 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1861 err = got_error_from_errno("close");
1862 if (blob)
1863 got_object_blob_close(blob);
1864 if (f != NULL && fclose(f) == EOF && err == NULL)
1865 err = got_error_from_errno2("fclose", abspath);
1866 if (fd != -1 && close(fd) == -1 && err == NULL)
1867 err = got_error_from_errno2("close", abspath);
1868 return err;
1872 * Update timestamps in the file index if a file is unmodified and
1873 * we had to run a full content comparison to find out.
1875 static const struct got_error *
1876 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1877 struct got_fileindex_entry *ie, struct stat *sb)
1879 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1880 return got_fileindex_entry_update(ie, wt_fd, path,
1881 ie->blob_sha1, ie->commit_sha1, 1);
1883 return NULL;
1886 static const struct got_error *
1887 update_blob(struct got_worktree *worktree,
1888 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1889 struct got_tree_entry *te, const char *path,
1890 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1891 void *progress_arg)
1893 const struct got_error *err = NULL;
1894 struct got_blob_object *blob = NULL;
1895 char *ondisk_path = NULL;
1896 unsigned char status = GOT_STATUS_NO_CHANGE;
1897 struct stat sb;
1898 int fd1 = -1, fd2 = -1;
1900 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1901 return got_error_from_errno("asprintf");
1903 if (ie) {
1904 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1905 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1906 goto done;
1908 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1909 repo);
1910 if (err)
1911 goto done;
1912 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1913 sb.st_mode = got_fileindex_perms_to_st(ie);
1914 } else {
1915 if (stat(ondisk_path, &sb) == -1) {
1916 if (errno != ENOENT) {
1917 err = got_error_from_errno2("stat",
1918 ondisk_path);
1919 goto done;
1921 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1922 status = GOT_STATUS_UNVERSIONED;
1923 } else {
1924 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1925 status = GOT_STATUS_UNVERSIONED;
1926 else
1927 status = GOT_STATUS_OBSTRUCTED;
1931 if (status == GOT_STATUS_OBSTRUCTED) {
1932 if (ie)
1933 got_fileindex_entry_mark_skipped(ie);
1934 err = (*progress_cb)(progress_arg, status, path);
1935 goto done;
1937 if (status == GOT_STATUS_CONFLICT) {
1938 if (ie)
1939 got_fileindex_entry_mark_skipped(ie);
1940 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1941 path);
1942 goto done;
1945 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1946 (S_ISLNK(te->mode) ||
1947 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1949 * This is a regular file or an installed bad symlink.
1950 * If the file index indicates that this file is already
1951 * up-to-date with respect to the repository we can skip
1952 * updating contents of this file.
1954 if (got_fileindex_entry_has_commit(ie) &&
1955 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1956 SHA1_DIGEST_LENGTH) == 0) {
1957 /* Same commit. */
1958 err = sync_timestamps(worktree->root_fd,
1959 path, status, ie, &sb);
1960 if (err)
1961 goto done;
1962 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1963 path);
1964 goto done;
1966 if (got_fileindex_entry_has_blob(ie) &&
1967 memcmp(ie->blob_sha1, te->id.sha1,
1968 SHA1_DIGEST_LENGTH) == 0) {
1969 /* Different commit but the same blob. */
1970 err = sync_timestamps(worktree->root_fd,
1971 path, status, ie, &sb);
1972 if (err)
1973 goto done;
1974 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1975 path);
1976 goto done;
1980 fd1 = got_opentempfd();
1981 if (fd1 == -1) {
1982 err = got_error_from_errno("got_opentempfd");
1983 goto done;
1985 err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
1986 if (err)
1987 goto done;
1989 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1990 int update_timestamps;
1991 struct got_blob_object *blob2 = NULL;
1992 char *label_orig = NULL;
1993 if (got_fileindex_entry_has_blob(ie)) {
1994 fd2 = got_opentempfd();
1995 if (fd2 == -1) {
1996 err = got_error_from_errno("got_opentempfd");
1997 goto done;
1999 struct got_object_id id2;
2000 got_fileindex_entry_get_blob_id(&id2, ie);
2001 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
2002 fd2);
2003 if (err)
2004 goto done;
2006 if (got_fileindex_entry_has_commit(ie)) {
2007 char id_str[SHA1_DIGEST_STRING_LENGTH];
2008 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2009 sizeof(id_str)) == NULL) {
2010 err = got_error_path(id_str,
2011 GOT_ERR_BAD_OBJ_ID_STR);
2012 goto done;
2014 if (asprintf(&label_orig, "%s: commit %s",
2015 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2016 err = got_error_from_errno("asprintf");
2017 goto done;
2020 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2021 char *link_target;
2022 err = got_object_blob_read_to_str(&link_target, blob);
2023 if (err)
2024 goto done;
2025 err = merge_symlink(worktree, blob2, ondisk_path, path,
2026 label_orig, link_target, worktree->base_commit_id,
2027 repo, progress_cb, progress_arg);
2028 free(link_target);
2029 } else {
2030 err = merge_blob(&update_timestamps, worktree, blob2,
2031 ondisk_path, path, sb.st_mode, label_orig, blob,
2032 worktree->base_commit_id, repo,
2033 progress_cb, progress_arg);
2035 free(label_orig);
2036 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2037 err = got_error_from_errno("close");
2038 goto done;
2040 if (blob2)
2041 got_object_blob_close(blob2);
2042 if (err)
2043 goto done;
2045 * Do not update timestamps of files with local changes.
2046 * Otherwise, a future status walk would treat them as
2047 * unmodified files again.
2049 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2050 blob->id.sha1, worktree->base_commit_id->sha1,
2051 update_timestamps);
2052 } else if (status == GOT_STATUS_MODE_CHANGE) {
2053 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2054 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2055 } else if (status == GOT_STATUS_DELETE) {
2056 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2057 if (err)
2058 goto done;
2059 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2060 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2061 if (err)
2062 goto done;
2063 } else {
2064 int is_bad_symlink = 0;
2065 if (S_ISLNK(te->mode)) {
2066 err = install_symlink(&is_bad_symlink, worktree,
2067 ondisk_path, path, blob,
2068 status == GOT_STATUS_MISSING, 0,
2069 status == GOT_STATUS_UNVERSIONED, 0,
2070 repo, progress_cb, progress_arg);
2071 } else {
2072 err = install_blob(worktree, ondisk_path, path,
2073 te->mode, sb.st_mode, blob,
2074 status == GOT_STATUS_MISSING, 0, 0,
2075 status == GOT_STATUS_UNVERSIONED, repo,
2076 progress_cb, progress_arg);
2078 if (err)
2079 goto done;
2081 if (ie) {
2082 err = got_fileindex_entry_update(ie,
2083 worktree->root_fd, path, blob->id.sha1,
2084 worktree->base_commit_id->sha1, 1);
2085 } else {
2086 err = create_fileindex_entry(&ie, fileindex,
2087 worktree->base_commit_id, worktree->root_fd, path,
2088 &blob->id);
2090 if (err)
2091 goto done;
2093 if (is_bad_symlink) {
2094 got_fileindex_entry_filetype_set(ie,
2095 GOT_FILEIDX_MODE_BAD_SYMLINK);
2099 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2100 err = got_error_from_errno("close");
2101 goto done;
2103 got_object_blob_close(blob);
2104 done:
2105 free(ondisk_path);
2106 return err;
2109 static const struct got_error *
2110 remove_ondisk_file(const char *root_path, const char *path)
2112 const struct got_error *err = NULL;
2113 char *ondisk_path = NULL, *parent = NULL;
2115 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2116 return got_error_from_errno("asprintf");
2118 if (unlink(ondisk_path) == -1) {
2119 if (errno != ENOENT)
2120 err = got_error_from_errno2("unlink", ondisk_path);
2121 } else {
2122 size_t root_len = strlen(root_path);
2123 err = got_path_dirname(&parent, ondisk_path);
2124 if (err)
2125 goto done;
2126 while (got_path_cmp(parent, root_path,
2127 strlen(parent), root_len) != 0) {
2128 free(ondisk_path);
2129 ondisk_path = parent;
2130 parent = NULL;
2131 if (rmdir(ondisk_path) == -1) {
2132 if (errno != ENOTEMPTY)
2133 err = got_error_from_errno2("rmdir",
2134 ondisk_path);
2135 break;
2137 err = got_path_dirname(&parent, ondisk_path);
2138 if (err)
2139 break;
2142 done:
2143 free(ondisk_path);
2144 free(parent);
2145 return err;
2148 static const struct got_error *
2149 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2150 struct got_fileindex_entry *ie, struct got_repository *repo,
2151 got_worktree_checkout_cb progress_cb, void *progress_arg)
2153 const struct got_error *err = NULL;
2154 unsigned char status;
2155 struct stat sb;
2156 char *ondisk_path;
2158 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2159 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2161 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2162 == -1)
2163 return got_error_from_errno("asprintf");
2165 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2166 if (err)
2167 goto done;
2169 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2170 char ondisk_target[PATH_MAX];
2171 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2172 sizeof(ondisk_target));
2173 if (ondisk_len == -1) {
2174 err = got_error_from_errno2("readlink", ondisk_path);
2175 goto done;
2177 ondisk_target[ondisk_len] = '\0';
2178 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2179 NULL, NULL, /* XXX pass common ancestor info? */
2180 ondisk_target, ondisk_path);
2181 if (err)
2182 goto done;
2183 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2184 ie->path);
2185 goto done;
2188 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2189 status == GOT_STATUS_ADD) {
2190 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2191 if (err)
2192 goto done;
2194 * Preserve the working file and change the deleted blob's
2195 * entry into a schedule-add entry.
2197 err = got_fileindex_entry_update(ie, worktree->root_fd,
2198 ie->path, NULL, NULL, 0);
2199 } else {
2200 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2201 if (err)
2202 goto done;
2203 if (status == GOT_STATUS_NO_CHANGE) {
2204 err = remove_ondisk_file(worktree->root_path, ie->path);
2205 if (err)
2206 goto done;
2208 got_fileindex_entry_remove(fileindex, ie);
2210 done:
2211 free(ondisk_path);
2212 return err;
2215 struct diff_cb_arg {
2216 struct got_fileindex *fileindex;
2217 struct got_worktree *worktree;
2218 struct got_repository *repo;
2219 got_worktree_checkout_cb progress_cb;
2220 void *progress_arg;
2221 got_cancel_cb cancel_cb;
2222 void *cancel_arg;
2225 static const struct got_error *
2226 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2227 struct got_tree_entry *te, const char *parent_path)
2229 struct diff_cb_arg *a = arg;
2231 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2232 return got_error(GOT_ERR_CANCELLED);
2234 return update_blob(a->worktree, a->fileindex, ie, te,
2235 ie->path, a->repo, a->progress_cb, a->progress_arg);
2238 static const struct got_error *
2239 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2241 struct diff_cb_arg *a = arg;
2243 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2244 return got_error(GOT_ERR_CANCELLED);
2246 return delete_blob(a->worktree, a->fileindex, ie,
2247 a->repo, a->progress_cb, a->progress_arg);
2250 static const struct got_error *
2251 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2253 struct diff_cb_arg *a = arg;
2254 const struct got_error *err;
2255 char *path;
2257 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2258 return got_error(GOT_ERR_CANCELLED);
2260 if (got_object_tree_entry_is_submodule(te))
2261 return NULL;
2263 if (asprintf(&path, "%s%s%s", parent_path,
2264 parent_path[0] ? "/" : "", te->name)
2265 == -1)
2266 return got_error_from_errno("asprintf");
2268 if (S_ISDIR(te->mode))
2269 err = add_dir_on_disk(a->worktree, path);
2270 else
2271 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2272 a->repo, a->progress_cb, a->progress_arg);
2274 free(path);
2275 return err;
2278 const struct got_error *
2279 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2281 uint32_t uuid_status;
2283 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2284 if (uuid_status != uuid_s_ok) {
2285 *uuidstr = NULL;
2286 return got_error_uuid(uuid_status, "uuid_to_string");
2289 return NULL;
2292 static const struct got_error *
2293 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2295 const struct got_error *err = NULL;
2296 char *uuidstr = NULL;
2298 *refname = NULL;
2300 err = got_worktree_get_uuid(&uuidstr, worktree);
2301 if (err)
2302 return err;
2304 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2305 err = got_error_from_errno("asprintf");
2306 *refname = NULL;
2308 free(uuidstr);
2309 return err;
2312 const struct got_error *
2313 got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2314 const char *prefix)
2316 return get_ref_name(refname, worktree, prefix);
2319 const struct got_error *
2320 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2322 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2325 static const struct got_error *
2326 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2328 return get_ref_name(refname, worktree,
2329 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2332 static const struct got_error *
2333 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2335 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2338 static const struct got_error *
2339 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2341 return get_ref_name(refname, worktree,
2342 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2345 static const struct got_error *
2346 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2348 return get_ref_name(refname, worktree,
2349 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2352 static const struct got_error *
2353 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2355 return get_ref_name(refname, worktree,
2356 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2359 static const struct got_error *
2360 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2362 return get_ref_name(refname, worktree,
2363 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2366 static const struct got_error *
2367 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2369 return get_ref_name(refname, worktree,
2370 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2373 static const struct got_error *
2374 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2376 return get_ref_name(refname, worktree,
2377 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2380 const struct got_error *
2381 got_worktree_get_histedit_script_path(char **path,
2382 struct got_worktree *worktree)
2384 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2385 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2386 *path = NULL;
2387 return got_error_from_errno("asprintf");
2389 return NULL;
2392 static const struct got_error *
2393 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2395 return get_ref_name(refname, worktree,
2396 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2399 static const struct got_error *
2400 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2402 return get_ref_name(refname, worktree,
2403 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2407 * Prevent Git's garbage collector from deleting our base commit by
2408 * setting a reference to our base commit's ID.
2410 static const struct got_error *
2411 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2413 const struct got_error *err = NULL;
2414 struct got_reference *ref = NULL;
2415 char *refname;
2417 err = got_worktree_get_base_ref_name(&refname, worktree);
2418 if (err)
2419 return err;
2421 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2422 if (err)
2423 goto done;
2425 err = got_ref_write(ref, repo);
2426 done:
2427 free(refname);
2428 if (ref)
2429 got_ref_close(ref);
2430 return err;
2433 static const struct got_error *
2434 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2436 const struct got_error *err = NULL;
2438 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2439 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2440 err = got_error_from_errno("asprintf");
2441 *fileindex_path = NULL;
2443 return err;
2447 static const struct got_error *
2448 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2449 struct got_worktree *worktree)
2451 const struct got_error *err = NULL;
2452 FILE *index = NULL;
2454 *fileindex_path = NULL;
2455 *fileindex = got_fileindex_alloc();
2456 if (*fileindex == NULL)
2457 return got_error_from_errno("got_fileindex_alloc");
2459 err = get_fileindex_path(fileindex_path, worktree);
2460 if (err)
2461 goto done;
2463 index = fopen(*fileindex_path, "rbe");
2464 if (index == NULL) {
2465 if (errno != ENOENT)
2466 err = got_error_from_errno2("fopen", *fileindex_path);
2467 } else {
2468 err = got_fileindex_read(*fileindex, index);
2469 if (fclose(index) == EOF && err == NULL)
2470 err = got_error_from_errno("fclose");
2472 done:
2473 if (err) {
2474 free(*fileindex_path);
2475 *fileindex_path = NULL;
2476 got_fileindex_free(*fileindex);
2477 *fileindex = NULL;
2479 return err;
2482 struct bump_base_commit_id_arg {
2483 struct got_object_id *base_commit_id;
2484 const char *path;
2485 size_t path_len;
2486 const char *entry_name;
2487 got_worktree_checkout_cb progress_cb;
2488 void *progress_arg;
2491 static const struct got_error *
2492 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2494 const struct got_error *err;
2495 struct bump_base_commit_id_arg *a = arg;
2497 if (a->entry_name) {
2498 if (strcmp(ie->path, a->path) != 0)
2499 return NULL;
2500 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2501 return NULL;
2503 if (got_fileindex_entry_was_skipped(ie))
2504 return NULL;
2506 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2507 SHA1_DIGEST_LENGTH) == 0)
2508 return NULL;
2510 if (a->progress_cb) {
2511 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2512 ie->path);
2513 if (err)
2514 return err;
2516 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2517 return NULL;
2520 /* Bump base commit ID of all files within an updated part of the work tree. */
2521 static const struct got_error *
2522 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2523 struct got_fileindex *fileindex,
2524 got_worktree_checkout_cb progress_cb, void *progress_arg)
2526 struct bump_base_commit_id_arg bbc_arg;
2528 bbc_arg.base_commit_id = worktree->base_commit_id;
2529 bbc_arg.entry_name = NULL;
2530 bbc_arg.path = "";
2531 bbc_arg.path_len = 0;
2532 bbc_arg.progress_cb = progress_cb;
2533 bbc_arg.progress_arg = progress_arg;
2535 return got_fileindex_for_each_entry_safe(fileindex,
2536 bump_base_commit_id, &bbc_arg);
2539 static const struct got_error *
2540 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2542 const struct got_error *err = NULL;
2543 char *new_fileindex_path = NULL;
2544 FILE *new_index = NULL;
2545 struct timespec timeout;
2547 err = got_opentemp_named(&new_fileindex_path, &new_index,
2548 fileindex_path, "");
2549 if (err)
2550 goto done;
2552 err = got_fileindex_write(fileindex, new_index);
2553 if (err)
2554 goto done;
2556 if (rename(new_fileindex_path, fileindex_path) != 0) {
2557 err = got_error_from_errno3("rename", new_fileindex_path,
2558 fileindex_path);
2559 unlink(new_fileindex_path);
2563 * Sleep for a short amount of time to ensure that files modified after
2564 * this program exits have a different time stamp from the one which
2565 * was recorded in the file index.
2567 timeout.tv_sec = 0;
2568 timeout.tv_nsec = 1;
2569 nanosleep(&timeout, NULL);
2570 done:
2571 if (new_index)
2572 fclose(new_index);
2573 free(new_fileindex_path);
2574 return err;
2577 static const struct got_error *
2578 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2579 struct got_object_id **tree_id, const char *wt_relpath,
2580 struct got_commit_object *base_commit, struct got_worktree *worktree,
2581 struct got_repository *repo)
2583 const struct got_error *err = NULL;
2584 struct got_object_id *id = NULL;
2585 char *in_repo_path = NULL;
2586 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2588 *entry_type = GOT_OBJ_TYPE_ANY;
2589 *tree_relpath = NULL;
2590 *tree_id = NULL;
2592 if (wt_relpath[0] == '\0') {
2593 /* Check out all files within the work tree. */
2594 *entry_type = GOT_OBJ_TYPE_TREE;
2595 *tree_relpath = strdup("");
2596 if (*tree_relpath == NULL) {
2597 err = got_error_from_errno("strdup");
2598 goto done;
2600 err = got_object_id_by_path(tree_id, repo, base_commit,
2601 worktree->path_prefix);
2602 if (err)
2603 goto done;
2604 return NULL;
2607 /* Check out a subset of files in the work tree. */
2609 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2610 is_root_wt ? "" : "/", wt_relpath) == -1) {
2611 err = got_error_from_errno("asprintf");
2612 goto done;
2615 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2616 if (err)
2617 goto done;
2619 free(in_repo_path);
2620 in_repo_path = NULL;
2622 err = got_object_get_type(entry_type, repo, id);
2623 if (err)
2624 goto done;
2626 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2627 /* Check out a single file. */
2628 if (strchr(wt_relpath, '/') == NULL) {
2629 /* Check out a single file in work tree's root dir. */
2630 in_repo_path = strdup(worktree->path_prefix);
2631 if (in_repo_path == NULL) {
2632 err = got_error_from_errno("strdup");
2633 goto done;
2635 *tree_relpath = strdup("");
2636 if (*tree_relpath == NULL) {
2637 err = got_error_from_errno("strdup");
2638 goto done;
2640 } else {
2641 /* Check out a single file in a subdirectory. */
2642 err = got_path_dirname(tree_relpath, wt_relpath);
2643 if (err)
2644 return err;
2645 if (asprintf(&in_repo_path, "%s%s%s",
2646 worktree->path_prefix, is_root_wt ? "" : "/",
2647 *tree_relpath) == -1) {
2648 err = got_error_from_errno("asprintf");
2649 goto done;
2652 err = got_object_id_by_path(tree_id, repo,
2653 base_commit, in_repo_path);
2654 } else {
2655 /* Check out all files within a subdirectory. */
2656 *tree_id = got_object_id_dup(id);
2657 if (*tree_id == NULL) {
2658 err = got_error_from_errno("got_object_id_dup");
2659 goto done;
2661 *tree_relpath = strdup(wt_relpath);
2662 if (*tree_relpath == NULL) {
2663 err = got_error_from_errno("strdup");
2664 goto done;
2667 done:
2668 free(id);
2669 free(in_repo_path);
2670 if (err) {
2671 *entry_type = GOT_OBJ_TYPE_ANY;
2672 free(*tree_relpath);
2673 *tree_relpath = NULL;
2674 free(*tree_id);
2675 *tree_id = NULL;
2677 return err;
2680 static const struct got_error *
2681 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2682 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2683 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2684 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2686 const struct got_error *err = NULL;
2687 struct got_commit_object *commit = NULL;
2688 struct got_tree_object *tree = NULL;
2689 struct got_fileindex_diff_tree_cb diff_cb;
2690 struct diff_cb_arg arg;
2692 err = ref_base_commit(worktree, repo);
2693 if (err) {
2694 if (!(err->code == GOT_ERR_ERRNO &&
2695 (errno == EACCES || errno == EROFS)))
2696 goto done;
2697 err = (*progress_cb)(progress_arg,
2698 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2699 if (err)
2700 return err;
2703 err = got_object_open_as_commit(&commit, repo,
2704 worktree->base_commit_id);
2705 if (err)
2706 goto done;
2708 err = got_object_open_as_tree(&tree, repo, tree_id);
2709 if (err)
2710 goto done;
2712 if (entry_name &&
2713 got_object_tree_find_entry(tree, entry_name) == NULL) {
2714 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2715 goto done;
2718 diff_cb.diff_old_new = diff_old_new;
2719 diff_cb.diff_old = diff_old;
2720 diff_cb.diff_new = diff_new;
2721 arg.fileindex = fileindex;
2722 arg.worktree = worktree;
2723 arg.repo = repo;
2724 arg.progress_cb = progress_cb;
2725 arg.progress_arg = progress_arg;
2726 arg.cancel_cb = cancel_cb;
2727 arg.cancel_arg = cancel_arg;
2728 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2729 entry_name, repo, &diff_cb, &arg);
2730 done:
2731 if (tree)
2732 got_object_tree_close(tree);
2733 if (commit)
2734 got_object_commit_close(commit);
2735 return err;
2738 const struct got_error *
2739 got_worktree_checkout_files(struct got_worktree *worktree,
2740 struct got_pathlist_head *paths, struct got_repository *repo,
2741 got_worktree_checkout_cb progress_cb, void *progress_arg,
2742 got_cancel_cb cancel_cb, void *cancel_arg)
2744 const struct got_error *err = NULL, *sync_err, *unlockerr;
2745 struct got_commit_object *commit = NULL;
2746 struct got_tree_object *tree = NULL;
2747 struct got_fileindex *fileindex = NULL;
2748 char *fileindex_path = NULL;
2749 struct got_pathlist_entry *pe;
2750 struct tree_path_data {
2751 STAILQ_ENTRY(tree_path_data) entry;
2752 struct got_object_id *tree_id;
2753 int entry_type;
2754 char *relpath;
2755 char *entry_name;
2756 } *tpd = NULL;
2757 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2759 STAILQ_INIT(&tree_paths);
2761 err = lock_worktree(worktree, LOCK_EX);
2762 if (err)
2763 return err;
2765 err = got_object_open_as_commit(&commit, repo,
2766 worktree->base_commit_id);
2767 if (err)
2768 goto done;
2770 /* Map all specified paths to in-repository trees. */
2771 TAILQ_FOREACH(pe, paths, entry) {
2772 tpd = malloc(sizeof(*tpd));
2773 if (tpd == NULL) {
2774 err = got_error_from_errno("malloc");
2775 goto done;
2778 err = find_tree_entry_for_checkout(&tpd->entry_type,
2779 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2780 worktree, repo);
2781 if (err) {
2782 free(tpd);
2783 goto done;
2786 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2787 err = got_path_basename(&tpd->entry_name, pe->path);
2788 if (err) {
2789 free(tpd->relpath);
2790 free(tpd->tree_id);
2791 free(tpd);
2792 goto done;
2794 } else
2795 tpd->entry_name = NULL;
2797 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2801 * Read the file index.
2802 * Checking out files is supposed to be an idempotent operation.
2803 * If the on-disk file index is incomplete we will try to complete it.
2805 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2806 if (err)
2807 goto done;
2809 tpd = STAILQ_FIRST(&tree_paths);
2810 TAILQ_FOREACH(pe, paths, entry) {
2811 struct bump_base_commit_id_arg bbc_arg;
2813 err = checkout_files(worktree, fileindex, tpd->relpath,
2814 tpd->tree_id, tpd->entry_name, repo,
2815 progress_cb, progress_arg, cancel_cb, cancel_arg);
2816 if (err)
2817 break;
2819 bbc_arg.base_commit_id = worktree->base_commit_id;
2820 bbc_arg.entry_name = tpd->entry_name;
2821 bbc_arg.path = pe->path;
2822 bbc_arg.path_len = pe->path_len;
2823 bbc_arg.progress_cb = progress_cb;
2824 bbc_arg.progress_arg = progress_arg;
2825 err = got_fileindex_for_each_entry_safe(fileindex,
2826 bump_base_commit_id, &bbc_arg);
2827 if (err)
2828 break;
2830 tpd = STAILQ_NEXT(tpd, entry);
2832 sync_err = sync_fileindex(fileindex, fileindex_path);
2833 if (sync_err && err == NULL)
2834 err = sync_err;
2835 done:
2836 free(fileindex_path);
2837 if (tree)
2838 got_object_tree_close(tree);
2839 if (commit)
2840 got_object_commit_close(commit);
2841 if (fileindex)
2842 got_fileindex_free(fileindex);
2843 while (!STAILQ_EMPTY(&tree_paths)) {
2844 tpd = STAILQ_FIRST(&tree_paths);
2845 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2846 free(tpd->relpath);
2847 free(tpd->tree_id);
2848 free(tpd);
2850 unlockerr = lock_worktree(worktree, LOCK_SH);
2851 if (unlockerr && err == NULL)
2852 err = unlockerr;
2853 return err;
2856 static const struct got_error *
2857 add_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2858 struct got_fileindex_entry *ie, const char *ondisk_path,
2859 const char *path2, struct got_blob_object *blob2, mode_t mode2,
2860 int restoring_missing_file, int reverting_versioned_file,
2861 int path_is_unversioned, int allow_bad_symlinks,
2862 struct got_repository *repo,
2863 got_worktree_checkout_cb progress_cb, void *progress_arg)
2865 const struct got_error *err = NULL;
2866 int is_bad_symlink = 0;
2868 if (S_ISLNK(mode2)) {
2869 err = install_symlink(&is_bad_symlink,
2870 worktree, ondisk_path, path2, blob2,
2871 restoring_missing_file,
2872 reverting_versioned_file,
2873 path_is_unversioned, allow_bad_symlinks,
2874 repo, progress_cb, progress_arg);
2875 } else {
2876 err = install_blob(worktree, ondisk_path, path2,
2877 mode2, GOT_DEFAULT_FILE_MODE, blob2,
2878 restoring_missing_file, reverting_versioned_file, 0,
2879 path_is_unversioned, repo, progress_cb, progress_arg);
2881 if (err)
2882 return err;
2883 if (ie == NULL) {
2884 /* Adding an unversioned file. */
2885 err = got_fileindex_entry_alloc(&ie, path2);
2886 if (err)
2887 return err;
2888 err = got_fileindex_entry_update(ie,
2889 worktree->root_fd, path2, NULL, NULL, 1);
2890 if (err) {
2891 got_fileindex_entry_free(ie);
2892 return err;
2894 err = got_fileindex_entry_add(fileindex, ie);
2895 if (err) {
2896 got_fileindex_entry_free(ie);
2897 return err;
2899 } else {
2900 /* Re-adding a locally deleted file. */
2901 err = got_fileindex_entry_update(ie,
2902 worktree->root_fd, path2, ie->blob_sha1,
2903 worktree->base_commit_id->sha1, 0);
2904 if (err)
2905 return err;
2908 if (is_bad_symlink) {
2909 got_fileindex_entry_filetype_set(ie,
2910 GOT_FILEIDX_MODE_BAD_SYMLINK);
2913 return NULL;
2916 struct merge_file_cb_arg {
2917 struct got_worktree *worktree;
2918 struct got_fileindex *fileindex;
2919 got_worktree_checkout_cb progress_cb;
2920 void *progress_arg;
2921 got_cancel_cb cancel_cb;
2922 void *cancel_arg;
2923 const char *label_orig;
2924 struct got_object_id *commit_id2;
2925 int allow_bad_symlinks;
2928 static const struct got_error *
2929 merge_file_cb(void *arg, struct got_blob_object *blob1,
2930 struct got_blob_object *blob2, FILE *f1, FILE *f2,
2931 struct got_object_id *id1, struct got_object_id *id2,
2932 const char *path1, const char *path2,
2933 mode_t mode1, mode_t mode2, struct got_repository *repo)
2935 static const struct got_error *err = NULL;
2936 struct merge_file_cb_arg *a = arg;
2937 struct got_fileindex_entry *ie;
2938 char *ondisk_path = NULL;
2939 struct stat sb;
2940 unsigned char status;
2941 int local_changes_subsumed;
2942 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2943 char *id_str = NULL, *label_deriv2 = NULL;
2945 if (blob1 && blob2) {
2946 ie = got_fileindex_entry_get(a->fileindex, path2,
2947 strlen(path2));
2948 if (ie == NULL)
2949 return (*a->progress_cb)(a->progress_arg,
2950 GOT_STATUS_MISSING, path2);
2952 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2953 path2) == -1)
2954 return got_error_from_errno("asprintf");
2956 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2957 repo);
2958 if (err)
2959 goto done;
2961 if (status == GOT_STATUS_DELETE) {
2962 err = (*a->progress_cb)(a->progress_arg,
2963 GOT_STATUS_MERGE, path2);
2964 goto done;
2966 if (status != GOT_STATUS_NO_CHANGE &&
2967 status != GOT_STATUS_MODIFY &&
2968 status != GOT_STATUS_CONFLICT &&
2969 status != GOT_STATUS_ADD) {
2970 err = (*a->progress_cb)(a->progress_arg, status, path2);
2971 goto done;
2974 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2975 char *link_target2;
2976 err = got_object_blob_read_to_str(&link_target2, blob2);
2977 if (err)
2978 goto done;
2979 err = merge_symlink(a->worktree, blob1, ondisk_path,
2980 path2, a->label_orig, link_target2, a->commit_id2,
2981 repo, a->progress_cb, a->progress_arg);
2982 free(link_target2);
2983 } else {
2984 int fd;
2986 f_orig = got_opentemp();
2987 if (f_orig == NULL) {
2988 err = got_error_from_errno("got_opentemp");
2989 goto done;
2991 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2992 f_orig, blob1);
2993 if (err)
2994 goto done;
2996 f_deriv2 = got_opentemp();
2997 if (f_deriv2 == NULL)
2998 goto done;
2999 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3000 f_deriv2, blob2);
3001 if (err)
3002 goto done;
3004 fd = open(ondisk_path,
3005 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3006 if (fd == -1) {
3007 err = got_error_from_errno2("open",
3008 ondisk_path);
3009 goto done;
3011 f_deriv = fdopen(fd, "r");
3012 if (f_deriv == NULL) {
3013 err = got_error_from_errno2("fdopen",
3014 ondisk_path);
3015 close(fd);
3016 goto done;
3018 err = got_object_id_str(&id_str, a->commit_id2);
3019 if (err)
3020 goto done;
3021 if (asprintf(&label_deriv2, "%s: commit %s",
3022 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
3023 err = got_error_from_errno("asprintf");
3024 goto done;
3026 err = merge_file(&local_changes_subsumed, a->worktree,
3027 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
3028 mode2, a->label_orig, NULL, label_deriv2,
3029 GOT_DIFF_ALGORITHM_PATIENCE, repo,
3030 a->progress_cb, a->progress_arg);
3032 } else if (blob1) {
3033 ie = got_fileindex_entry_get(a->fileindex, path1,
3034 strlen(path1));
3035 if (ie == NULL)
3036 return (*a->progress_cb)(a->progress_arg,
3037 GOT_STATUS_MISSING, path1);
3039 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3040 path1) == -1)
3041 return got_error_from_errno("asprintf");
3043 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3044 repo);
3045 if (err)
3046 goto done;
3048 switch (status) {
3049 case GOT_STATUS_NO_CHANGE:
3050 err = (*a->progress_cb)(a->progress_arg,
3051 GOT_STATUS_DELETE, path1);
3052 if (err)
3053 goto done;
3054 err = remove_ondisk_file(a->worktree->root_path, path1);
3055 if (err)
3056 goto done;
3057 if (ie)
3058 got_fileindex_entry_mark_deleted_from_disk(ie);
3059 break;
3060 case GOT_STATUS_DELETE:
3061 case GOT_STATUS_MISSING:
3062 err = (*a->progress_cb)(a->progress_arg,
3063 GOT_STATUS_DELETE, path1);
3064 if (err)
3065 goto done;
3066 if (ie)
3067 got_fileindex_entry_mark_deleted_from_disk(ie);
3068 break;
3069 case GOT_STATUS_ADD: {
3070 struct got_object_id *id;
3071 FILE *blob1_f;
3072 off_t blob1_size;
3074 * Delete the added file only if its content already
3075 * exists in the repository.
3077 err = got_object_blob_file_create(&id, &blob1_f,
3078 &blob1_size, path1);
3079 if (err)
3080 goto done;
3081 if (got_object_id_cmp(id, id1) == 0) {
3082 err = (*a->progress_cb)(a->progress_arg,
3083 GOT_STATUS_DELETE, path1);
3084 if (err)
3085 goto done;
3086 err = remove_ondisk_file(a->worktree->root_path,
3087 path1);
3088 if (err)
3089 goto done;
3090 if (ie)
3091 got_fileindex_entry_remove(a->fileindex,
3092 ie);
3093 } else {
3094 err = (*a->progress_cb)(a->progress_arg,
3095 GOT_STATUS_CANNOT_DELETE, path1);
3097 if (fclose(blob1_f) == EOF && err == NULL)
3098 err = got_error_from_errno("fclose");
3099 free(id);
3100 if (err)
3101 goto done;
3102 break;
3104 case GOT_STATUS_MODIFY:
3105 case GOT_STATUS_CONFLICT:
3106 err = (*a->progress_cb)(a->progress_arg,
3107 GOT_STATUS_CANNOT_DELETE, path1);
3108 if (err)
3109 goto done;
3110 break;
3111 case GOT_STATUS_OBSTRUCTED:
3112 err = (*a->progress_cb)(a->progress_arg, status, path1);
3113 if (err)
3114 goto done;
3115 break;
3116 default:
3117 break;
3119 } else if (blob2) {
3120 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3121 path2) == -1)
3122 return got_error_from_errno("asprintf");
3123 ie = got_fileindex_entry_get(a->fileindex, path2,
3124 strlen(path2));
3125 if (ie) {
3126 err = get_file_status(&status, &sb, ie, ondisk_path,
3127 -1, NULL, repo);
3128 if (err)
3129 goto done;
3130 if (status != GOT_STATUS_NO_CHANGE &&
3131 status != GOT_STATUS_MODIFY &&
3132 status != GOT_STATUS_CONFLICT &&
3133 status != GOT_STATUS_ADD &&
3134 status != GOT_STATUS_DELETE) {
3135 err = (*a->progress_cb)(a->progress_arg,
3136 status, path2);
3137 goto done;
3139 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3140 char *link_target2;
3141 err = got_object_blob_read_to_str(&link_target2,
3142 blob2);
3143 if (err)
3144 goto done;
3145 err = merge_symlink(a->worktree, NULL,
3146 ondisk_path, path2, a->label_orig,
3147 link_target2, a->commit_id2, repo,
3148 a->progress_cb, a->progress_arg);
3149 free(link_target2);
3150 } else if (S_ISREG(sb.st_mode)) {
3151 err = merge_blob(&local_changes_subsumed,
3152 a->worktree, NULL, ondisk_path, path2,
3153 sb.st_mode, a->label_orig, blob2,
3154 a->commit_id2, repo, a->progress_cb,
3155 a->progress_arg);
3156 } else if (status != GOT_STATUS_DELETE) {
3157 err = got_error_path(ondisk_path,
3158 GOT_ERR_FILE_OBSTRUCTED);
3160 if (err)
3161 goto done;
3162 if (status == GOT_STATUS_DELETE) {
3163 /* Re-add file with content from new blob. */
3164 err = add_file(a->worktree, a->fileindex, ie,
3165 ondisk_path, path2, blob2, mode2,
3166 0, 0, 0, a->allow_bad_symlinks,
3167 repo, a->progress_cb, a->progress_arg);
3168 if (err)
3169 goto done;
3171 } else {
3172 err = add_file(a->worktree, a->fileindex, NULL,
3173 ondisk_path, path2, blob2, mode2,
3174 0, 0, 1, a->allow_bad_symlinks,
3175 repo, a->progress_cb, a->progress_arg);
3176 if (err)
3177 goto done;
3180 done:
3181 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3182 err = got_error_from_errno("fclose");
3183 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3184 err = got_error_from_errno("fclose");
3185 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3186 err = got_error_from_errno("fclose");
3187 free(id_str);
3188 free(label_deriv2);
3189 free(ondisk_path);
3190 return err;
3193 static const struct got_error *
3194 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3196 struct got_worktree *worktree = arg;
3198 /* Reject merges into a work tree with mixed base commits. */
3199 if (got_fileindex_entry_has_commit(ie) &&
3200 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3201 SHA1_DIGEST_LENGTH) != 0)
3202 return got_error(GOT_ERR_MIXED_COMMITS);
3204 return NULL;
3207 struct check_merge_conflicts_arg {
3208 struct got_worktree *worktree;
3209 struct got_fileindex *fileindex;
3210 struct got_repository *repo;
3213 static const struct got_error *
3214 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3215 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3216 struct got_object_id *id1, struct got_object_id *id2,
3217 const char *path1, const char *path2,
3218 mode_t mode1, mode_t mode2, struct got_repository *repo)
3220 const struct got_error *err = NULL;
3221 struct check_merge_conflicts_arg *a = arg;
3222 unsigned char status;
3223 struct stat sb;
3224 struct got_fileindex_entry *ie;
3225 const char *path = path2 ? path2 : path1;
3226 struct got_object_id *id = id2 ? id2 : id1;
3227 char *ondisk_path;
3229 if (id == NULL)
3230 return NULL;
3232 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3233 if (ie == NULL)
3234 return NULL;
3236 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3237 == -1)
3238 return got_error_from_errno("asprintf");
3240 /* Reject merges into a work tree with conflicted files. */
3241 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3242 free(ondisk_path);
3243 if (err)
3244 return err;
3245 if (status == GOT_STATUS_CONFLICT)
3246 return got_error(GOT_ERR_CONFLICTS);
3248 return NULL;
3251 static const struct got_error *
3252 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3253 const char *fileindex_path, struct got_object_id *commit_id1,
3254 struct got_object_id *commit_id2, struct got_repository *repo,
3255 got_worktree_checkout_cb progress_cb, void *progress_arg,
3256 got_cancel_cb cancel_cb, void *cancel_arg)
3258 const struct got_error *err = NULL, *sync_err;
3259 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3260 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3261 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3262 struct check_merge_conflicts_arg cmc_arg;
3263 struct merge_file_cb_arg arg;
3264 char *label_orig = NULL;
3265 FILE *f1 = NULL, *f2 = NULL;
3266 int fd1 = -1, fd2 = -1;
3268 if (commit_id1) {
3269 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3270 if (err)
3271 goto done;
3272 err = got_object_id_by_path(&tree_id1, repo, commit1,
3273 worktree->path_prefix);
3274 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3275 goto done;
3277 if (tree_id1) {
3278 char *id_str;
3280 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3281 if (err)
3282 goto done;
3284 err = got_object_id_str(&id_str, commit_id1);
3285 if (err)
3286 goto done;
3288 if (asprintf(&label_orig, "%s: commit %s",
3289 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3290 err = got_error_from_errno("asprintf");
3291 free(id_str);
3292 goto done;
3294 free(id_str);
3296 f1 = got_opentemp();
3297 if (f1 == NULL) {
3298 err = got_error_from_errno("got_opentemp");
3299 goto done;
3303 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3304 if (err)
3305 goto done;
3307 err = got_object_id_by_path(&tree_id2, repo, commit2,
3308 worktree->path_prefix);
3309 if (err)
3310 goto done;
3312 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3313 if (err)
3314 goto done;
3316 f2 = got_opentemp();
3317 if (f2 == NULL) {
3318 err = got_error_from_errno("got_opentemp");
3319 goto done;
3322 fd1 = got_opentempfd();
3323 if (fd1 == -1) {
3324 err = got_error_from_errno("got_opentempfd");
3325 goto done;
3328 fd2 = got_opentempfd();
3329 if (fd2 == -1) {
3330 err = got_error_from_errno("got_opentempfd");
3331 goto done;
3334 cmc_arg.worktree = worktree;
3335 cmc_arg.fileindex = fileindex;
3336 cmc_arg.repo = repo;
3337 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3338 check_merge_conflicts, &cmc_arg, 0);
3339 if (err)
3340 goto done;
3342 arg.worktree = worktree;
3343 arg.fileindex = fileindex;
3344 arg.progress_cb = progress_cb;
3345 arg.progress_arg = progress_arg;
3346 arg.cancel_cb = cancel_cb;
3347 arg.cancel_arg = cancel_arg;
3348 arg.label_orig = label_orig;
3349 arg.commit_id2 = commit_id2;
3350 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3351 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3352 merge_file_cb, &arg, 1);
3353 sync_err = sync_fileindex(fileindex, fileindex_path);
3354 if (sync_err && err == NULL)
3355 err = sync_err;
3356 done:
3357 if (commit1)
3358 got_object_commit_close(commit1);
3359 if (commit2)
3360 got_object_commit_close(commit2);
3361 if (tree1)
3362 got_object_tree_close(tree1);
3363 if (tree2)
3364 got_object_tree_close(tree2);
3365 if (f1 && fclose(f1) == EOF && err == NULL)
3366 err = got_error_from_errno("fclose");
3367 if (f2 && fclose(f2) == EOF && err == NULL)
3368 err = got_error_from_errno("fclose");
3369 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3370 err = got_error_from_errno("close");
3371 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3372 err = got_error_from_errno("close");
3373 free(label_orig);
3374 return err;
3377 const struct got_error *
3378 got_worktree_merge_files(struct got_worktree *worktree,
3379 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3380 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3381 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3383 const struct got_error *err, *unlockerr;
3384 char *fileindex_path = NULL;
3385 struct got_fileindex *fileindex = NULL;
3387 err = lock_worktree(worktree, LOCK_EX);
3388 if (err)
3389 return err;
3391 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3392 if (err)
3393 goto done;
3395 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3396 worktree);
3397 if (err)
3398 goto done;
3400 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3401 commit_id2, repo, progress_cb, progress_arg,
3402 cancel_cb, cancel_arg);
3403 done:
3404 if (fileindex)
3405 got_fileindex_free(fileindex);
3406 free(fileindex_path);
3407 unlockerr = lock_worktree(worktree, LOCK_SH);
3408 if (unlockerr && err == NULL)
3409 err = unlockerr;
3410 return err;
3413 struct diff_dir_cb_arg {
3414 struct got_fileindex *fileindex;
3415 struct got_worktree *worktree;
3416 const char *status_path;
3417 size_t status_path_len;
3418 struct got_repository *repo;
3419 got_worktree_status_cb status_cb;
3420 void *status_arg;
3421 got_cancel_cb cancel_cb;
3422 void *cancel_arg;
3423 /* A pathlist containing per-directory pathlists of ignore patterns. */
3424 struct got_pathlist_head *ignores;
3425 int report_unchanged;
3426 int no_ignores;
3429 static const struct got_error *
3430 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3431 int dirfd, const char *de_name,
3432 got_worktree_status_cb status_cb, void *status_arg,
3433 struct got_repository *repo, int report_unchanged)
3435 const struct got_error *err = NULL;
3436 unsigned char status = GOT_STATUS_NO_CHANGE;
3437 unsigned char staged_status;
3438 struct stat sb;
3439 struct got_object_id blob_id, commit_id, staged_blob_id;
3440 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3441 struct got_object_id *staged_blob_idp = NULL;
3443 staged_status = get_staged_status(ie);
3444 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3445 if (err)
3446 return err;
3448 if (status == GOT_STATUS_NO_CHANGE &&
3449 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3450 return NULL;
3452 if (got_fileindex_entry_has_blob(ie))
3453 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3454 if (got_fileindex_entry_has_commit(ie))
3455 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3456 if (staged_status == GOT_STATUS_ADD ||
3457 staged_status == GOT_STATUS_MODIFY) {
3458 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3459 &staged_blob_id, ie);
3462 return (*status_cb)(status_arg, status, staged_status,
3463 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3466 static const struct got_error *
3467 status_old_new(void *arg, struct got_fileindex_entry *ie,
3468 struct dirent *de, const char *parent_path, int dirfd)
3470 const struct got_error *err = NULL;
3471 struct diff_dir_cb_arg *a = arg;
3472 char *abspath;
3474 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3475 return got_error(GOT_ERR_CANCELLED);
3477 if (got_path_cmp(parent_path, a->status_path,
3478 strlen(parent_path), a->status_path_len) != 0 &&
3479 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3480 return NULL;
3482 if (parent_path[0]) {
3483 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3484 parent_path, de->d_name) == -1)
3485 return got_error_from_errno("asprintf");
3486 } else {
3487 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3488 de->d_name) == -1)
3489 return got_error_from_errno("asprintf");
3492 err = report_file_status(ie, abspath, dirfd, de->d_name,
3493 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3494 free(abspath);
3495 return err;
3498 static const struct got_error *
3499 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3501 struct diff_dir_cb_arg *a = arg;
3502 struct got_object_id blob_id, commit_id;
3503 unsigned char status;
3505 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3506 return got_error(GOT_ERR_CANCELLED);
3508 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3509 return NULL;
3511 got_fileindex_entry_get_blob_id(&blob_id, ie);
3512 got_fileindex_entry_get_commit_id(&commit_id, ie);
3513 if (got_fileindex_entry_has_file_on_disk(ie))
3514 status = GOT_STATUS_MISSING;
3515 else
3516 status = GOT_STATUS_DELETE;
3517 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3518 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3521 static void
3522 free_ignores(struct got_pathlist_head *ignores)
3524 struct got_pathlist_entry *pe;
3526 TAILQ_FOREACH(pe, ignores, entry) {
3527 struct got_pathlist_head *ignorelist = pe->data;
3529 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3531 got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3534 static const struct got_error *
3535 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3537 const struct got_error *err = NULL;
3538 struct got_pathlist_entry *pe = NULL;
3539 struct got_pathlist_head *ignorelist;
3540 char *line = NULL, *pattern, *dirpath = NULL;
3541 size_t linesize = 0;
3542 ssize_t linelen;
3544 ignorelist = calloc(1, sizeof(*ignorelist));
3545 if (ignorelist == NULL)
3546 return got_error_from_errno("calloc");
3547 TAILQ_INIT(ignorelist);
3549 while ((linelen = getline(&line, &linesize, f)) != -1) {
3550 if (linelen > 0 && line[linelen - 1] == '\n')
3551 line[linelen - 1] = '\0';
3553 /* Git's ignores may contain comments. */
3554 if (line[0] == '#')
3555 continue;
3557 /* Git's negated patterns are not (yet?) supported. */
3558 if (line[0] == '!')
3559 continue;
3561 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3562 line) == -1) {
3563 err = got_error_from_errno("asprintf");
3564 goto done;
3566 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3567 if (err)
3568 goto done;
3570 if (ferror(f)) {
3571 err = got_error_from_errno("getline");
3572 goto done;
3575 dirpath = strdup(path);
3576 if (dirpath == NULL) {
3577 err = got_error_from_errno("strdup");
3578 goto done;
3580 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3581 done:
3582 free(line);
3583 if (err || pe == NULL) {
3584 free(dirpath);
3585 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3587 return err;
3590 static int
3591 match_path(const char *pattern, size_t pattern_len, const char *path,
3592 int flags)
3594 char buf[PATH_MAX];
3597 * Trailing slashes signify directories.
3598 * Append a * to make such patterns conform to fnmatch rules.
3600 if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3601 if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3602 return FNM_NOMATCH; /* XXX */
3604 return fnmatch(buf, path, flags);
3607 return fnmatch(pattern, path, flags);
3610 static int
3611 match_ignores(struct got_pathlist_head *ignores, const char *path)
3613 struct got_pathlist_entry *pe;
3615 /* Handle patterns which match in all directories. */
3616 TAILQ_FOREACH(pe, ignores, entry) {
3617 struct got_pathlist_head *ignorelist = pe->data;
3618 struct got_pathlist_entry *pi;
3620 TAILQ_FOREACH(pi, ignorelist, entry) {
3621 const char *p;
3623 if (pi->path_len < 3 ||
3624 strncmp(pi->path, "**/", 3) != 0)
3625 continue;
3626 p = path;
3627 while (*p) {
3628 if (match_path(pi->path + 3,
3629 pi->path_len - 3, p,
3630 FNM_PATHNAME | FNM_LEADING_DIR)) {
3631 /* Retry in next directory. */
3632 while (*p && *p != '/')
3633 p++;
3634 while (*p == '/')
3635 p++;
3636 continue;
3638 return 1;
3644 * The ignores pathlist contains ignore lists from children before
3645 * parents, so we can find the most specific ignorelist by walking
3646 * ignores backwards.
3648 pe = TAILQ_LAST(ignores, got_pathlist_head);
3649 while (pe) {
3650 if (got_path_is_child(path, pe->path, pe->path_len)) {
3651 struct got_pathlist_head *ignorelist = pe->data;
3652 struct got_pathlist_entry *pi;
3653 TAILQ_FOREACH(pi, ignorelist, entry) {
3654 int flags = FNM_LEADING_DIR;
3655 if (strstr(pi->path, "/**/") == NULL)
3656 flags |= FNM_PATHNAME;
3657 if (match_path(pi->path, pi->path_len,
3658 path, flags))
3659 continue;
3660 return 1;
3663 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3666 return 0;
3669 static const struct got_error *
3670 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3671 const char *path, int dirfd, const char *ignores_filename)
3673 const struct got_error *err = NULL;
3674 char *ignorespath;
3675 int fd = -1;
3676 FILE *ignoresfile = NULL;
3678 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3679 path[0] ? "/" : "", ignores_filename) == -1)
3680 return got_error_from_errno("asprintf");
3682 if (dirfd != -1) {
3683 fd = openat(dirfd, ignores_filename,
3684 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3685 if (fd == -1) {
3686 if (errno != ENOENT && errno != EACCES)
3687 err = got_error_from_errno2("openat",
3688 ignorespath);
3689 } else {
3690 ignoresfile = fdopen(fd, "r");
3691 if (ignoresfile == NULL)
3692 err = got_error_from_errno2("fdopen",
3693 ignorespath);
3694 else {
3695 fd = -1;
3696 err = read_ignores(ignores, path, ignoresfile);
3699 } else {
3700 ignoresfile = fopen(ignorespath, "re");
3701 if (ignoresfile == NULL) {
3702 if (errno != ENOENT && errno != EACCES)
3703 err = got_error_from_errno2("fopen",
3704 ignorespath);
3705 } else
3706 err = read_ignores(ignores, path, ignoresfile);
3709 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3710 err = got_error_from_errno2("fclose", path);
3711 if (fd != -1 && close(fd) == -1 && err == NULL)
3712 err = got_error_from_errno2("close", path);
3713 free(ignorespath);
3714 return err;
3717 static const struct got_error *
3718 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3719 int dirfd)
3721 const struct got_error *err = NULL;
3722 struct diff_dir_cb_arg *a = arg;
3723 char *path = NULL;
3725 if (ignore != NULL)
3726 *ignore = 0;
3728 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3729 return got_error(GOT_ERR_CANCELLED);
3731 if (parent_path[0]) {
3732 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3733 return got_error_from_errno("asprintf");
3734 } else {
3735 path = de->d_name;
3738 if (de->d_type == DT_DIR) {
3739 if (!a->no_ignores && ignore != NULL &&
3740 match_ignores(a->ignores, path))
3741 *ignore = 1;
3742 } else if (!match_ignores(a->ignores, path) &&
3743 got_path_is_child(path, a->status_path, a->status_path_len))
3744 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3745 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3746 if (parent_path[0])
3747 free(path);
3748 return err;
3751 static const struct got_error *
3752 status_traverse(void *arg, const char *path, int dirfd)
3754 const struct got_error *err = NULL;
3755 struct diff_dir_cb_arg *a = arg;
3757 if (a->no_ignores)
3758 return NULL;
3760 err = add_ignores(a->ignores, a->worktree->root_path,
3761 path, dirfd, ".cvsignore");
3762 if (err)
3763 return err;
3765 err = add_ignores(a->ignores, a->worktree->root_path, path,
3766 dirfd, ".gitignore");
3768 return err;
3771 static const struct got_error *
3772 report_single_file_status(const char *path, const char *ondisk_path,
3773 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3774 void *status_arg, struct got_repository *repo, int report_unchanged,
3775 struct got_pathlist_head *ignores, int no_ignores)
3777 struct got_fileindex_entry *ie;
3778 struct stat sb;
3780 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3781 if (ie)
3782 return report_file_status(ie, ondisk_path, -1, NULL,
3783 status_cb, status_arg, repo, report_unchanged);
3785 if (lstat(ondisk_path, &sb) == -1) {
3786 if (errno != ENOENT)
3787 return got_error_from_errno2("lstat", ondisk_path);
3788 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3789 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3792 if (!no_ignores && match_ignores(ignores, path))
3793 return NULL;
3795 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3796 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3797 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3799 return NULL;
3802 static const struct got_error *
3803 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3804 const char *root_path, const char *path)
3806 const struct got_error *err;
3807 char *parent_path, *next_parent_path = NULL;
3809 err = add_ignores(ignores, root_path, "", -1,
3810 ".cvsignore");
3811 if (err)
3812 return err;
3814 err = add_ignores(ignores, root_path, "", -1,
3815 ".gitignore");
3816 if (err)
3817 return err;
3819 err = got_path_dirname(&parent_path, path);
3820 if (err) {
3821 if (err->code == GOT_ERR_BAD_PATH)
3822 return NULL; /* cannot traverse parent */
3823 return err;
3825 for (;;) {
3826 err = add_ignores(ignores, root_path, parent_path, -1,
3827 ".cvsignore");
3828 if (err)
3829 break;
3830 err = add_ignores(ignores, root_path, parent_path, -1,
3831 ".gitignore");
3832 if (err)
3833 break;
3834 err = got_path_dirname(&next_parent_path, parent_path);
3835 if (err) {
3836 if (err->code == GOT_ERR_BAD_PATH)
3837 err = NULL; /* traversed everything */
3838 break;
3840 if (got_path_is_root_dir(parent_path))
3841 break;
3842 free(parent_path);
3843 parent_path = next_parent_path;
3844 next_parent_path = NULL;
3847 free(parent_path);
3848 free(next_parent_path);
3849 return err;
3852 static const struct got_error *
3853 worktree_status(struct got_worktree *worktree, const char *path,
3854 struct got_fileindex *fileindex, struct got_repository *repo,
3855 got_worktree_status_cb status_cb, void *status_arg,
3856 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3857 int report_unchanged)
3859 const struct got_error *err = NULL;
3860 int fd = -1;
3861 struct got_fileindex_diff_dir_cb fdiff_cb;
3862 struct diff_dir_cb_arg arg;
3863 char *ondisk_path = NULL;
3864 struct got_pathlist_head ignores;
3865 struct got_fileindex_entry *ie;
3867 TAILQ_INIT(&ignores);
3869 if (asprintf(&ondisk_path, "%s%s%s",
3870 worktree->root_path, path[0] ? "/" : "", path) == -1)
3871 return got_error_from_errno("asprintf");
3873 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3874 if (ie) {
3875 err = report_single_file_status(path, ondisk_path,
3876 fileindex, status_cb, status_arg, repo,
3877 report_unchanged, &ignores, no_ignores);
3878 goto done;
3881 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3882 if (fd == -1) {
3883 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3884 !got_err_open_nofollow_on_symlink())
3885 err = got_error_from_errno2("open", ondisk_path);
3886 else {
3887 if (!no_ignores) {
3888 err = add_ignores_from_parent_paths(&ignores,
3889 worktree->root_path, ondisk_path);
3890 if (err)
3891 goto done;
3893 err = report_single_file_status(path, ondisk_path,
3894 fileindex, status_cb, status_arg, repo,
3895 report_unchanged, &ignores, no_ignores);
3897 } else {
3898 fdiff_cb.diff_old_new = status_old_new;
3899 fdiff_cb.diff_old = status_old;
3900 fdiff_cb.diff_new = status_new;
3901 fdiff_cb.diff_traverse = status_traverse;
3902 arg.fileindex = fileindex;
3903 arg.worktree = worktree;
3904 arg.status_path = path;
3905 arg.status_path_len = strlen(path);
3906 arg.repo = repo;
3907 arg.status_cb = status_cb;
3908 arg.status_arg = status_arg;
3909 arg.cancel_cb = cancel_cb;
3910 arg.cancel_arg = cancel_arg;
3911 arg.report_unchanged = report_unchanged;
3912 arg.no_ignores = no_ignores;
3913 if (!no_ignores) {
3914 err = add_ignores_from_parent_paths(&ignores,
3915 worktree->root_path, path);
3916 if (err)
3917 goto done;
3919 arg.ignores = &ignores;
3920 err = got_fileindex_diff_dir(fileindex, fd,
3921 worktree->root_path, path, repo, &fdiff_cb, &arg);
3923 done:
3924 free_ignores(&ignores);
3925 if (fd != -1 && close(fd) == -1 && err == NULL)
3926 err = got_error_from_errno("close");
3927 free(ondisk_path);
3928 return err;
3931 const struct got_error *
3932 got_worktree_status(struct got_worktree *worktree,
3933 struct got_pathlist_head *paths, struct got_repository *repo,
3934 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3935 got_cancel_cb cancel_cb, void *cancel_arg)
3937 const struct got_error *err = NULL;
3938 char *fileindex_path = NULL;
3939 struct got_fileindex *fileindex = NULL;
3940 struct got_pathlist_entry *pe;
3942 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3943 if (err)
3944 return err;
3946 TAILQ_FOREACH(pe, paths, entry) {
3947 err = worktree_status(worktree, pe->path, fileindex, repo,
3948 status_cb, status_arg, cancel_cb, cancel_arg,
3949 no_ignores, 0);
3950 if (err)
3951 break;
3953 free(fileindex_path);
3954 got_fileindex_free(fileindex);
3955 return err;
3958 const struct got_error *
3959 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3960 const char *arg)
3962 const struct got_error *err = NULL;
3963 char *resolved = NULL, *cwd = NULL, *path = NULL;
3964 size_t len;
3965 struct stat sb;
3966 char *abspath = NULL;
3967 char canonpath[PATH_MAX];
3969 *wt_path = NULL;
3971 cwd = getcwd(NULL, 0);
3972 if (cwd == NULL)
3973 return got_error_from_errno("getcwd");
3975 if (lstat(arg, &sb) == -1) {
3976 if (errno != ENOENT) {
3977 err = got_error_from_errno2("lstat", arg);
3978 goto done;
3980 sb.st_mode = 0;
3982 if (S_ISLNK(sb.st_mode)) {
3984 * We cannot use realpath(3) with symlinks since we want to
3985 * operate on the symlink itself.
3986 * But we can make the path absolute, assuming it is relative
3987 * to the current working directory, and then canonicalize it.
3989 if (!got_path_is_absolute(arg)) {
3990 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3991 err = got_error_from_errno("asprintf");
3992 goto done;
3996 err = got_canonpath(abspath ? abspath : arg, canonpath,
3997 sizeof(canonpath));
3998 if (err)
3999 goto done;
4000 resolved = strdup(canonpath);
4001 if (resolved == NULL) {
4002 err = got_error_from_errno("strdup");
4003 goto done;
4005 } else {
4006 resolved = realpath(arg, NULL);
4007 if (resolved == NULL) {
4008 if (errno != ENOENT) {
4009 err = got_error_from_errno2("realpath", arg);
4010 goto done;
4012 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4013 err = got_error_from_errno("asprintf");
4014 goto done;
4016 err = got_canonpath(abspath, canonpath,
4017 sizeof(canonpath));
4018 if (err)
4019 goto done;
4020 resolved = strdup(canonpath);
4021 if (resolved == NULL) {
4022 err = got_error_from_errno("strdup");
4023 goto done;
4028 if (strncmp(got_worktree_get_root_path(worktree), resolved,
4029 strlen(got_worktree_get_root_path(worktree)))) {
4030 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
4031 goto done;
4034 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
4035 err = got_path_skip_common_ancestor(&path,
4036 got_worktree_get_root_path(worktree), resolved);
4037 if (err)
4038 goto done;
4039 } else {
4040 path = strdup("");
4041 if (path == NULL) {
4042 err = got_error_from_errno("strdup");
4043 goto done;
4047 /* XXX status walk can't deal with trailing slash! */
4048 len = strlen(path);
4049 while (len > 0 && path[len - 1] == '/') {
4050 path[len - 1] = '\0';
4051 len--;
4053 done:
4054 free(abspath);
4055 free(resolved);
4056 free(cwd);
4057 if (err == NULL)
4058 *wt_path = path;
4059 else
4060 free(path);
4061 return err;
4064 struct schedule_addition_args {
4065 struct got_worktree *worktree;
4066 struct got_fileindex *fileindex;
4067 got_worktree_checkout_cb progress_cb;
4068 void *progress_arg;
4069 struct got_repository *repo;
4072 static const struct got_error *
4073 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4074 const char *relpath, struct got_object_id *blob_id,
4075 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4076 int dirfd, const char *de_name)
4078 struct schedule_addition_args *a = arg;
4079 const struct got_error *err = NULL;
4080 struct got_fileindex_entry *ie;
4081 struct stat sb;
4082 char *ondisk_path;
4084 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4085 relpath) == -1)
4086 return got_error_from_errno("asprintf");
4088 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4089 if (ie) {
4090 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4091 de_name, a->repo);
4092 if (err)
4093 goto done;
4094 /* Re-adding an existing entry is a no-op. */
4095 if (status == GOT_STATUS_ADD)
4096 goto done;
4097 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4098 if (err)
4099 goto done;
4102 if (status != GOT_STATUS_UNVERSIONED) {
4103 if (status == GOT_STATUS_NONEXISTENT)
4104 err = got_error_set_errno(ENOENT, ondisk_path);
4105 else
4106 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4107 goto done;
4110 err = got_fileindex_entry_alloc(&ie, relpath);
4111 if (err)
4112 goto done;
4113 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4114 relpath, NULL, NULL, 1);
4115 if (err) {
4116 got_fileindex_entry_free(ie);
4117 goto done;
4119 err = got_fileindex_entry_add(a->fileindex, ie);
4120 if (err) {
4121 got_fileindex_entry_free(ie);
4122 goto done;
4124 done:
4125 free(ondisk_path);
4126 if (err)
4127 return err;
4128 if (status == GOT_STATUS_ADD)
4129 return NULL;
4130 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4133 const struct got_error *
4134 got_worktree_schedule_add(struct got_worktree *worktree,
4135 struct got_pathlist_head *paths,
4136 got_worktree_checkout_cb progress_cb, void *progress_arg,
4137 struct got_repository *repo, int no_ignores)
4139 struct got_fileindex *fileindex = NULL;
4140 char *fileindex_path = NULL;
4141 const struct got_error *err = NULL, *sync_err, *unlockerr;
4142 struct got_pathlist_entry *pe;
4143 struct schedule_addition_args saa;
4145 err = lock_worktree(worktree, LOCK_EX);
4146 if (err)
4147 return err;
4149 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4150 if (err)
4151 goto done;
4153 saa.worktree = worktree;
4154 saa.fileindex = fileindex;
4155 saa.progress_cb = progress_cb;
4156 saa.progress_arg = progress_arg;
4157 saa.repo = repo;
4159 TAILQ_FOREACH(pe, paths, entry) {
4160 err = worktree_status(worktree, pe->path, fileindex, repo,
4161 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4162 if (err)
4163 break;
4165 sync_err = sync_fileindex(fileindex, fileindex_path);
4166 if (sync_err && err == NULL)
4167 err = sync_err;
4168 done:
4169 free(fileindex_path);
4170 if (fileindex)
4171 got_fileindex_free(fileindex);
4172 unlockerr = lock_worktree(worktree, LOCK_SH);
4173 if (unlockerr && err == NULL)
4174 err = unlockerr;
4175 return err;
4178 struct schedule_deletion_args {
4179 struct got_worktree *worktree;
4180 struct got_fileindex *fileindex;
4181 got_worktree_delete_cb progress_cb;
4182 void *progress_arg;
4183 struct got_repository *repo;
4184 int delete_local_mods;
4185 int keep_on_disk;
4186 int ignore_missing_paths;
4187 const char *status_codes;
4190 static const struct got_error *
4191 schedule_for_deletion(void *arg, unsigned char status,
4192 unsigned char staged_status, const char *relpath,
4193 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4194 struct got_object_id *commit_id, int dirfd, const char *de_name)
4196 struct schedule_deletion_args *a = arg;
4197 const struct got_error *err = NULL;
4198 struct got_fileindex_entry *ie = NULL;
4199 struct stat sb;
4200 char *ondisk_path;
4202 if (status == GOT_STATUS_NONEXISTENT) {
4203 if (a->ignore_missing_paths)
4204 return NULL;
4205 return got_error_set_errno(ENOENT, relpath);
4208 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4209 if (ie == NULL)
4210 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4212 staged_status = get_staged_status(ie);
4213 if (staged_status != GOT_STATUS_NO_CHANGE) {
4214 if (staged_status == GOT_STATUS_DELETE)
4215 return NULL;
4216 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4219 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4220 relpath) == -1)
4221 return got_error_from_errno("asprintf");
4223 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4224 a->repo);
4225 if (err)
4226 goto done;
4228 if (a->status_codes) {
4229 size_t ncodes = strlen(a->status_codes);
4230 int i;
4231 for (i = 0; i < ncodes ; i++) {
4232 if (status == a->status_codes[i])
4233 break;
4235 if (i == ncodes) {
4236 /* Do not delete files in non-matching status. */
4237 free(ondisk_path);
4238 return NULL;
4240 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4241 a->status_codes[i] != GOT_STATUS_MISSING) {
4242 static char msg[64];
4243 snprintf(msg, sizeof(msg),
4244 "invalid status code '%c'", a->status_codes[i]);
4245 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4246 goto done;
4250 if (status != GOT_STATUS_NO_CHANGE) {
4251 if (status == GOT_STATUS_DELETE)
4252 goto done;
4253 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4254 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4255 goto done;
4257 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4258 err = got_error_set_errno(ENOENT, relpath);
4259 goto done;
4261 if (status != GOT_STATUS_MODIFY &&
4262 status != GOT_STATUS_MISSING) {
4263 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4264 goto done;
4268 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4269 size_t root_len;
4271 if (dirfd != -1) {
4272 if (unlinkat(dirfd, de_name, 0) == -1) {
4273 err = got_error_from_errno2("unlinkat",
4274 ondisk_path);
4275 goto done;
4277 } else if (unlink(ondisk_path) == -1) {
4278 err = got_error_from_errno2("unlink", ondisk_path);
4279 goto done;
4282 root_len = strlen(a->worktree->root_path);
4283 do {
4284 char *parent;
4285 err = got_path_dirname(&parent, ondisk_path);
4286 if (err)
4287 goto done;
4288 free(ondisk_path);
4289 ondisk_path = parent;
4290 if (rmdir(ondisk_path) == -1) {
4291 if (errno != ENOTEMPTY)
4292 err = got_error_from_errno2("rmdir",
4293 ondisk_path);
4294 break;
4296 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4297 strlen(ondisk_path), root_len) != 0);
4300 got_fileindex_entry_mark_deleted_from_disk(ie);
4301 done:
4302 free(ondisk_path);
4303 if (err)
4304 return err;
4305 if (status == GOT_STATUS_DELETE)
4306 return NULL;
4307 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4308 staged_status, relpath);
4311 const struct got_error *
4312 got_worktree_schedule_delete(struct got_worktree *worktree,
4313 struct got_pathlist_head *paths, int delete_local_mods,
4314 const char *status_codes,
4315 got_worktree_delete_cb progress_cb, void *progress_arg,
4316 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4318 struct got_fileindex *fileindex = NULL;
4319 char *fileindex_path = NULL;
4320 const struct got_error *err = NULL, *sync_err, *unlockerr;
4321 struct got_pathlist_entry *pe;
4322 struct schedule_deletion_args sda;
4324 err = lock_worktree(worktree, LOCK_EX);
4325 if (err)
4326 return err;
4328 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4329 if (err)
4330 goto done;
4332 sda.worktree = worktree;
4333 sda.fileindex = fileindex;
4334 sda.progress_cb = progress_cb;
4335 sda.progress_arg = progress_arg;
4336 sda.repo = repo;
4337 sda.delete_local_mods = delete_local_mods;
4338 sda.keep_on_disk = keep_on_disk;
4339 sda.ignore_missing_paths = ignore_missing_paths;
4340 sda.status_codes = status_codes;
4342 TAILQ_FOREACH(pe, paths, entry) {
4343 err = worktree_status(worktree, pe->path, fileindex, repo,
4344 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4345 if (err)
4346 break;
4348 sync_err = sync_fileindex(fileindex, fileindex_path);
4349 if (sync_err && err == NULL)
4350 err = sync_err;
4351 done:
4352 free(fileindex_path);
4353 if (fileindex)
4354 got_fileindex_free(fileindex);
4355 unlockerr = lock_worktree(worktree, LOCK_SH);
4356 if (unlockerr && err == NULL)
4357 err = unlockerr;
4358 return err;
4361 static const struct got_error *
4362 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4364 const struct got_error *err = NULL;
4365 char *line = NULL;
4366 size_t linesize = 0, n;
4367 ssize_t linelen;
4369 linelen = getline(&line, &linesize, infile);
4370 if (linelen == -1) {
4371 if (ferror(infile)) {
4372 err = got_error_from_errno("getline");
4373 goto done;
4375 return NULL;
4377 if (outfile) {
4378 n = fwrite(line, 1, linelen, outfile);
4379 if (n != linelen) {
4380 err = got_ferror(outfile, GOT_ERR_IO);
4381 goto done;
4384 if (rejectfile) {
4385 n = fwrite(line, 1, linelen, rejectfile);
4386 if (n != linelen)
4387 err = got_ferror(rejectfile, GOT_ERR_IO);
4389 done:
4390 free(line);
4391 return err;
4394 static const struct got_error *
4395 skip_one_line(FILE *f)
4397 char *line = NULL;
4398 size_t linesize = 0;
4399 ssize_t linelen;
4401 linelen = getline(&line, &linesize, f);
4402 if (linelen == -1) {
4403 if (ferror(f))
4404 return got_error_from_errno("getline");
4405 return NULL;
4407 free(line);
4408 return NULL;
4411 static const struct got_error *
4412 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4413 int start_old, int end_old, int start_new, int end_new,
4414 FILE *outfile, FILE *rejectfile)
4416 const struct got_error *err;
4418 /* Copy old file's lines leading up to patch. */
4419 while (!feof(f1) && *line_cur1 < start_old) {
4420 err = copy_one_line(f1, outfile, NULL);
4421 if (err)
4422 return err;
4423 (*line_cur1)++;
4425 /* Skip new file's lines leading up to patch. */
4426 while (!feof(f2) && *line_cur2 < start_new) {
4427 if (rejectfile)
4428 err = copy_one_line(f2, NULL, rejectfile);
4429 else
4430 err = skip_one_line(f2);
4431 if (err)
4432 return err;
4433 (*line_cur2)++;
4435 /* Copy patched lines. */
4436 while (!feof(f2) && *line_cur2 <= end_new) {
4437 err = copy_one_line(f2, outfile, NULL);
4438 if (err)
4439 return err;
4440 (*line_cur2)++;
4442 /* Skip over old file's replaced lines. */
4443 while (!feof(f1) && *line_cur1 <= end_old) {
4444 if (rejectfile)
4445 err = copy_one_line(f1, NULL, rejectfile);
4446 else
4447 err = skip_one_line(f1);
4448 if (err)
4449 return err;
4450 (*line_cur1)++;
4453 return NULL;
4456 static const struct got_error *
4457 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4458 FILE *outfile, FILE *rejectfile)
4460 const struct got_error *err;
4462 if (outfile) {
4463 /* Copy old file's lines until EOF. */
4464 while (!feof(f1)) {
4465 err = copy_one_line(f1, outfile, NULL);
4466 if (err)
4467 return err;
4468 (*line_cur1)++;
4471 if (rejectfile) {
4472 /* Copy new file's lines until EOF. */
4473 while (!feof(f2)) {
4474 err = copy_one_line(f2, NULL, rejectfile);
4475 if (err)
4476 return err;
4477 (*line_cur2)++;
4481 return NULL;
4484 static const struct got_error *
4485 apply_or_reject_change(int *choice, int *nchunks_used,
4486 struct diff_result *diff_result, int n,
4487 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4488 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4489 got_worktree_patch_cb patch_cb, void *patch_arg)
4491 const struct got_error *err = NULL;
4492 struct diff_chunk_context cc = {};
4493 int start_old, end_old, start_new, end_new;
4494 FILE *hunkfile;
4495 struct diff_output_unidiff_state *diff_state;
4496 struct diff_input_info diff_info;
4497 int rc;
4499 *choice = GOT_PATCH_CHOICE_NONE;
4501 /* Get changed line numbers without context lines for copy_change(). */
4502 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4503 start_old = cc.left.start;
4504 end_old = cc.left.end;
4505 start_new = cc.right.start;
4506 end_new = cc.right.end;
4508 /* Get the same change with context lines for display. */
4509 memset(&cc, 0, sizeof(cc));
4510 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4512 memset(&diff_info, 0, sizeof(diff_info));
4513 diff_info.left_path = relpath;
4514 diff_info.right_path = relpath;
4516 diff_state = diff_output_unidiff_state_alloc();
4517 if (diff_state == NULL)
4518 return got_error_set_errno(ENOMEM,
4519 "diff_output_unidiff_state_alloc");
4521 hunkfile = got_opentemp();
4522 if (hunkfile == NULL) {
4523 err = got_error_from_errno("got_opentemp");
4524 goto done;
4527 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4528 diff_result, &cc);
4529 if (rc != DIFF_RC_OK) {
4530 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4531 goto done;
4534 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4535 err = got_ferror(hunkfile, GOT_ERR_IO);
4536 goto done;
4539 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4540 hunkfile, changeno, nchanges);
4541 if (err)
4542 goto done;
4544 switch (*choice) {
4545 case GOT_PATCH_CHOICE_YES:
4546 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4547 end_old, start_new, end_new, outfile, rejectfile);
4548 break;
4549 case GOT_PATCH_CHOICE_NO:
4550 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4551 end_old, start_new, end_new, rejectfile, outfile);
4552 break;
4553 case GOT_PATCH_CHOICE_QUIT:
4554 break;
4555 default:
4556 err = got_error(GOT_ERR_PATCH_CHOICE);
4557 break;
4559 done:
4560 diff_output_unidiff_state_free(diff_state);
4561 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4562 err = got_error_from_errno("fclose");
4563 return err;
4566 struct revert_file_args {
4567 struct got_worktree *worktree;
4568 struct got_fileindex *fileindex;
4569 got_worktree_checkout_cb progress_cb;
4570 void *progress_arg;
4571 got_worktree_patch_cb patch_cb;
4572 void *patch_arg;
4573 struct got_repository *repo;
4574 int unlink_added_files;
4577 static const struct got_error *
4578 create_patched_content(char **path_outfile, int reverse_patch,
4579 struct got_object_id *blob_id, const char *path2,
4580 int dirfd2, const char *de_name2,
4581 const char *relpath, struct got_repository *repo,
4582 got_worktree_patch_cb patch_cb, void *patch_arg)
4584 const struct got_error *err, *free_err;
4585 struct got_blob_object *blob = NULL;
4586 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4587 int fd = -1, fd2 = -1;
4588 char link_target[PATH_MAX];
4589 ssize_t link_len = 0;
4590 char *path1 = NULL, *id_str = NULL;
4591 struct stat sb2;
4592 struct got_diffreg_result *diffreg_result = NULL;
4593 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4594 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4596 *path_outfile = NULL;
4598 err = got_object_id_str(&id_str, blob_id);
4599 if (err)
4600 return err;
4602 if (dirfd2 != -1) {
4603 fd2 = openat(dirfd2, de_name2,
4604 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4605 if (fd2 == -1) {
4606 if (!got_err_open_nofollow_on_symlink()) {
4607 err = got_error_from_errno2("openat", path2);
4608 goto done;
4610 link_len = readlinkat(dirfd2, de_name2,
4611 link_target, sizeof(link_target));
4612 if (link_len == -1) {
4613 return got_error_from_errno2("readlinkat",
4614 path2);
4616 sb2.st_mode = S_IFLNK;
4617 sb2.st_size = link_len;
4619 } else {
4620 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4621 if (fd2 == -1) {
4622 if (!got_err_open_nofollow_on_symlink()) {
4623 err = got_error_from_errno2("open", path2);
4624 goto done;
4626 link_len = readlink(path2, link_target,
4627 sizeof(link_target));
4628 if (link_len == -1)
4629 return got_error_from_errno2("readlink", path2);
4630 sb2.st_mode = S_IFLNK;
4631 sb2.st_size = link_len;
4634 if (fd2 != -1) {
4635 if (fstat(fd2, &sb2) == -1) {
4636 err = got_error_from_errno2("fstat", path2);
4637 goto done;
4640 f2 = fdopen(fd2, "r");
4641 if (f2 == NULL) {
4642 err = got_error_from_errno2("fdopen", path2);
4643 goto done;
4645 fd2 = -1;
4646 } else {
4647 size_t n;
4648 f2 = got_opentemp();
4649 if (f2 == NULL) {
4650 err = got_error_from_errno2("got_opentemp", path2);
4651 goto done;
4653 n = fwrite(link_target, 1, link_len, f2);
4654 if (n != link_len) {
4655 err = got_ferror(f2, GOT_ERR_IO);
4656 goto done;
4658 if (fflush(f2) == EOF) {
4659 err = got_error_from_errno("fflush");
4660 goto done;
4662 rewind(f2);
4665 fd = got_opentempfd();
4666 if (fd == -1) {
4667 err = got_error_from_errno("got_opentempfd");
4668 goto done;
4671 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4672 if (err)
4673 goto done;
4675 err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4676 if (err)
4677 goto done;
4679 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4680 if (err)
4681 goto done;
4683 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4684 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4685 if (err)
4686 goto done;
4688 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4689 "");
4690 if (err)
4691 goto done;
4693 if (fseek(f1, 0L, SEEK_SET) == -1)
4694 return got_ferror(f1, GOT_ERR_IO);
4695 if (fseek(f2, 0L, SEEK_SET) == -1)
4696 return got_ferror(f2, GOT_ERR_IO);
4698 /* Count the number of actual changes in the diff result. */
4699 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4700 struct diff_chunk_context cc = {};
4701 diff_chunk_context_load_change(&cc, &nchunks_used,
4702 diffreg_result->result, n, 0);
4703 nchanges++;
4705 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4706 int choice;
4707 err = apply_or_reject_change(&choice, &nchunks_used,
4708 diffreg_result->result, n, relpath, f1, f2,
4709 &line_cur1, &line_cur2,
4710 reverse_patch ? NULL : outfile,
4711 reverse_patch ? outfile : NULL,
4712 ++i, nchanges, patch_cb, patch_arg);
4713 if (err)
4714 goto done;
4715 if (choice == GOT_PATCH_CHOICE_YES)
4716 have_content = 1;
4717 else if (choice == GOT_PATCH_CHOICE_QUIT)
4718 break;
4720 if (have_content) {
4721 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4722 reverse_patch ? NULL : outfile,
4723 reverse_patch ? outfile : NULL);
4724 if (err)
4725 goto done;
4727 if (!S_ISLNK(sb2.st_mode)) {
4728 mode_t mode;
4730 mode = apply_umask(sb2.st_mode);
4731 if (fchmod(fileno(outfile), mode) == -1) {
4732 err = got_error_from_errno2("fchmod", path2);
4733 goto done;
4737 done:
4738 free(id_str);
4739 if (fd != -1 && close(fd) == -1 && err == NULL)
4740 err = got_error_from_errno("close");
4741 if (blob)
4742 got_object_blob_close(blob);
4743 free_err = got_diffreg_result_free(diffreg_result);
4744 if (err == NULL)
4745 err = free_err;
4746 if (f1 && fclose(f1) == EOF && err == NULL)
4747 err = got_error_from_errno2("fclose", path1);
4748 if (f2 && fclose(f2) == EOF && err == NULL)
4749 err = got_error_from_errno2("fclose", path2);
4750 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4751 err = got_error_from_errno2("close", path2);
4752 if (outfile && fclose(outfile) == EOF && err == NULL)
4753 err = got_error_from_errno2("fclose", *path_outfile);
4754 if (path1 && unlink(path1) == -1 && err == NULL)
4755 err = got_error_from_errno2("unlink", path1);
4756 if (err || !have_content) {
4757 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4758 err = got_error_from_errno2("unlink", *path_outfile);
4759 free(*path_outfile);
4760 *path_outfile = NULL;
4762 free(path1);
4763 return err;
4766 static const struct got_error *
4767 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4768 const char *relpath, struct got_object_id *blob_id,
4769 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4770 int dirfd, const char *de_name)
4772 struct revert_file_args *a = arg;
4773 const struct got_error *err = NULL;
4774 char *parent_path = NULL;
4775 struct got_fileindex_entry *ie;
4776 struct got_commit_object *base_commit = NULL;
4777 struct got_tree_object *tree = NULL;
4778 struct got_object_id *tree_id = NULL;
4779 const struct got_tree_entry *te = NULL;
4780 char *tree_path = NULL, *te_name;
4781 char *ondisk_path = NULL, *path_content = NULL;
4782 struct got_blob_object *blob = NULL;
4783 int fd = -1;
4785 /* Reverting a staged deletion is a no-op. */
4786 if (status == GOT_STATUS_DELETE &&
4787 staged_status != GOT_STATUS_NO_CHANGE)
4788 return NULL;
4790 if (status == GOT_STATUS_UNVERSIONED)
4791 return (*a->progress_cb)(a->progress_arg,
4792 GOT_STATUS_UNVERSIONED, relpath);
4794 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4795 if (ie == NULL)
4796 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4798 /* Construct in-repository path of tree which contains this blob. */
4799 err = got_path_dirname(&parent_path, ie->path);
4800 if (err) {
4801 if (err->code != GOT_ERR_BAD_PATH)
4802 goto done;
4803 parent_path = strdup("/");
4804 if (parent_path == NULL) {
4805 err = got_error_from_errno("strdup");
4806 goto done;
4809 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4810 tree_path = strdup(parent_path);
4811 if (tree_path == NULL) {
4812 err = got_error_from_errno("strdup");
4813 goto done;
4815 } else {
4816 if (got_path_is_root_dir(parent_path)) {
4817 tree_path = strdup(a->worktree->path_prefix);
4818 if (tree_path == NULL) {
4819 err = got_error_from_errno("strdup");
4820 goto done;
4822 } else {
4823 if (asprintf(&tree_path, "%s/%s",
4824 a->worktree->path_prefix, parent_path) == -1) {
4825 err = got_error_from_errno("asprintf");
4826 goto done;
4831 err = got_object_open_as_commit(&base_commit, a->repo,
4832 a->worktree->base_commit_id);
4833 if (err)
4834 goto done;
4836 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4837 if (err) {
4838 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4839 (status == GOT_STATUS_ADD ||
4840 staged_status == GOT_STATUS_ADD)))
4841 goto done;
4842 } else {
4843 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4844 if (err)
4845 goto done;
4847 err = got_path_basename(&te_name, ie->path);
4848 if (err)
4849 goto done;
4851 te = got_object_tree_find_entry(tree, te_name);
4852 free(te_name);
4853 if (te == NULL && status != GOT_STATUS_ADD &&
4854 staged_status != GOT_STATUS_ADD) {
4855 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4856 goto done;
4860 switch (status) {
4861 case GOT_STATUS_ADD:
4862 if (a->patch_cb) {
4863 int choice = GOT_PATCH_CHOICE_NONE;
4864 err = (*a->patch_cb)(&choice, a->patch_arg,
4865 status, ie->path, NULL, 1, 1);
4866 if (err)
4867 goto done;
4868 if (choice != GOT_PATCH_CHOICE_YES)
4869 break;
4871 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4872 ie->path);
4873 if (err)
4874 goto done;
4875 got_fileindex_entry_remove(a->fileindex, ie);
4876 if (a->unlink_added_files) {
4877 if (asprintf(&ondisk_path, "%s/%s",
4878 got_worktree_get_root_path(a->worktree),
4879 relpath) == -1) {
4880 err = got_error_from_errno("asprintf");
4881 goto done;
4883 if (unlink(ondisk_path) == -1) {
4884 err = got_error_from_errno2("unlink",
4885 ondisk_path);
4886 break;
4889 break;
4890 case GOT_STATUS_DELETE:
4891 if (a->patch_cb) {
4892 int choice = GOT_PATCH_CHOICE_NONE;
4893 err = (*a->patch_cb)(&choice, a->patch_arg,
4894 status, ie->path, NULL, 1, 1);
4895 if (err)
4896 goto done;
4897 if (choice != GOT_PATCH_CHOICE_YES)
4898 break;
4900 /* fall through */
4901 case GOT_STATUS_MODIFY:
4902 case GOT_STATUS_MODE_CHANGE:
4903 case GOT_STATUS_CONFLICT:
4904 case GOT_STATUS_MISSING: {
4905 struct got_object_id id;
4906 if (staged_status == GOT_STATUS_ADD ||
4907 staged_status == GOT_STATUS_MODIFY)
4908 got_fileindex_entry_get_staged_blob_id(&id, ie);
4909 else
4910 got_fileindex_entry_get_blob_id(&id, ie);
4911 fd = got_opentempfd();
4912 if (fd == -1) {
4913 err = got_error_from_errno("got_opentempfd");
4914 goto done;
4917 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4918 if (err)
4919 goto done;
4921 if (asprintf(&ondisk_path, "%s/%s",
4922 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4923 err = got_error_from_errno("asprintf");
4924 goto done;
4927 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4928 status == GOT_STATUS_CONFLICT)) {
4929 int is_bad_symlink = 0;
4930 err = create_patched_content(&path_content, 1, &id,
4931 ondisk_path, dirfd, de_name, ie->path, a->repo,
4932 a->patch_cb, a->patch_arg);
4933 if (err || path_content == NULL)
4934 break;
4935 if (te && S_ISLNK(te->mode)) {
4936 if (unlink(path_content) == -1) {
4937 err = got_error_from_errno2("unlink",
4938 path_content);
4939 break;
4941 err = install_symlink(&is_bad_symlink,
4942 a->worktree, ondisk_path, ie->path,
4943 blob, 0, 1, 0, 0, a->repo,
4944 a->progress_cb, a->progress_arg);
4945 } else {
4946 if (rename(path_content, ondisk_path) == -1) {
4947 err = got_error_from_errno3("rename",
4948 path_content, ondisk_path);
4949 goto done;
4952 } else {
4953 int is_bad_symlink = 0;
4954 if (te && S_ISLNK(te->mode)) {
4955 err = install_symlink(&is_bad_symlink,
4956 a->worktree, ondisk_path, ie->path,
4957 blob, 0, 1, 0, 0, a->repo,
4958 a->progress_cb, a->progress_arg);
4959 } else {
4960 err = install_blob(a->worktree, ondisk_path,
4961 ie->path,
4962 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4963 got_fileindex_perms_to_st(ie), blob,
4964 0, 1, 0, 0, a->repo,
4965 a->progress_cb, a->progress_arg);
4967 if (err)
4968 goto done;
4969 if (status == GOT_STATUS_DELETE ||
4970 status == GOT_STATUS_MODE_CHANGE) {
4971 err = got_fileindex_entry_update(ie,
4972 a->worktree->root_fd, relpath,
4973 blob->id.sha1,
4974 a->worktree->base_commit_id->sha1, 1);
4975 if (err)
4976 goto done;
4978 if (is_bad_symlink) {
4979 got_fileindex_entry_filetype_set(ie,
4980 GOT_FILEIDX_MODE_BAD_SYMLINK);
4983 break;
4985 default:
4986 break;
4988 done:
4989 free(ondisk_path);
4990 free(path_content);
4991 free(parent_path);
4992 free(tree_path);
4993 if (fd != -1 && close(fd) == -1 && err == NULL)
4994 err = got_error_from_errno("close");
4995 if (blob)
4996 got_object_blob_close(blob);
4997 if (tree)
4998 got_object_tree_close(tree);
4999 free(tree_id);
5000 if (base_commit)
5001 got_object_commit_close(base_commit);
5002 return err;
5005 const struct got_error *
5006 got_worktree_revert(struct got_worktree *worktree,
5007 struct got_pathlist_head *paths,
5008 got_worktree_checkout_cb progress_cb, void *progress_arg,
5009 got_worktree_patch_cb patch_cb, void *patch_arg,
5010 struct got_repository *repo)
5012 struct got_fileindex *fileindex = NULL;
5013 char *fileindex_path = NULL;
5014 const struct got_error *err = NULL, *unlockerr = NULL;
5015 const struct got_error *sync_err = NULL;
5016 struct got_pathlist_entry *pe;
5017 struct revert_file_args rfa;
5019 err = lock_worktree(worktree, LOCK_EX);
5020 if (err)
5021 return err;
5023 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5024 if (err)
5025 goto done;
5027 rfa.worktree = worktree;
5028 rfa.fileindex = fileindex;
5029 rfa.progress_cb = progress_cb;
5030 rfa.progress_arg = progress_arg;
5031 rfa.patch_cb = patch_cb;
5032 rfa.patch_arg = patch_arg;
5033 rfa.repo = repo;
5034 rfa.unlink_added_files = 0;
5035 TAILQ_FOREACH(pe, paths, entry) {
5036 err = worktree_status(worktree, pe->path, fileindex, repo,
5037 revert_file, &rfa, NULL, NULL, 1, 0);
5038 if (err)
5039 break;
5041 sync_err = sync_fileindex(fileindex, fileindex_path);
5042 if (sync_err && err == NULL)
5043 err = sync_err;
5044 done:
5045 free(fileindex_path);
5046 if (fileindex)
5047 got_fileindex_free(fileindex);
5048 unlockerr = lock_worktree(worktree, LOCK_SH);
5049 if (unlockerr && err == NULL)
5050 err = unlockerr;
5051 return err;
5054 static void
5055 free_commitable(struct got_commitable *ct)
5057 free(ct->path);
5058 free(ct->in_repo_path);
5059 free(ct->ondisk_path);
5060 free(ct->blob_id);
5061 free(ct->base_blob_id);
5062 free(ct->staged_blob_id);
5063 free(ct->base_commit_id);
5064 free(ct);
5067 struct collect_commitables_arg {
5068 struct got_pathlist_head *commitable_paths;
5069 struct got_repository *repo;
5070 struct got_worktree *worktree;
5071 struct got_fileindex *fileindex;
5072 int have_staged_files;
5073 int allow_bad_symlinks;
5074 int diff_header_shown;
5075 int commit_conflicts;
5076 FILE *diff_outfile;
5077 FILE *f1;
5078 FILE *f2;
5082 * Create a file which contains the target path of a symlink so we can feed
5083 * it as content to the diff engine.
5085 static const struct got_error *
5086 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5087 const char *abspath)
5089 const struct got_error *err = NULL;
5090 char target_path[PATH_MAX];
5091 ssize_t target_len, outlen;
5093 *fd = -1;
5095 if (dirfd != -1) {
5096 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5097 if (target_len == -1)
5098 return got_error_from_errno2("readlinkat", abspath);
5099 } else {
5100 target_len = readlink(abspath, target_path, PATH_MAX);
5101 if (target_len == -1)
5102 return got_error_from_errno2("readlink", abspath);
5105 *fd = got_opentempfd();
5106 if (*fd == -1)
5107 return got_error_from_errno("got_opentempfd");
5109 outlen = write(*fd, target_path, target_len);
5110 if (outlen == -1) {
5111 err = got_error_from_errno("got_opentempfd");
5112 goto done;
5115 if (lseek(*fd, 0, SEEK_SET) == -1) {
5116 err = got_error_from_errno2("lseek", abspath);
5117 goto done;
5119 done:
5120 if (err) {
5121 close(*fd);
5122 *fd = -1;
5124 return err;
5127 static const struct got_error *
5128 append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5129 FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5130 int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5132 const struct got_error *err = NULL;
5133 struct got_blob_object *blob1 = NULL;
5134 int fd = -1, fd1 = -1, fd2 = -1;
5135 FILE *ondisk_file = NULL;
5136 char *label1 = NULL;
5137 struct stat sb;
5138 off_t size1 = 0;
5139 int f2_exists = 0;
5140 char *id_str = NULL;
5142 memset(&sb, 0, sizeof(sb));
5144 if (diff_staged) {
5145 if (ct->staged_status != GOT_STATUS_MODIFY &&
5146 ct->staged_status != GOT_STATUS_ADD &&
5147 ct->staged_status != GOT_STATUS_DELETE)
5148 return NULL;
5149 } else {
5150 if (ct->status != GOT_STATUS_MODIFY &&
5151 ct->status != GOT_STATUS_ADD &&
5152 ct->status != GOT_STATUS_DELETE &&
5153 ct->status != GOT_STATUS_CONFLICT)
5154 return NULL;
5157 err = got_opentemp_truncate(f1);
5158 if (err)
5159 return got_error_from_errno("got_opentemp_truncate");
5160 err = got_opentemp_truncate(f2);
5161 if (err)
5162 return got_error_from_errno("got_opentemp_truncate");
5164 if (!*diff_header_shown) {
5165 err = got_object_id_str(&id_str, worktree->base_commit_id);
5166 if (err)
5167 return err;
5168 fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5169 got_worktree_get_root_path(worktree));
5170 fprintf(diff_outfile, "commit - %s\n", id_str);
5171 fprintf(diff_outfile, "path + %s%s\n",
5172 got_worktree_get_root_path(worktree),
5173 diff_staged ? " (staged changes)" : "");
5174 *diff_header_shown = 1;
5177 if (diff_staged) {
5178 const char *label1 = NULL, *label2 = NULL;
5179 switch (ct->staged_status) {
5180 case GOT_STATUS_MODIFY:
5181 label1 = ct->path;
5182 label2 = ct->path;
5183 break;
5184 case GOT_STATUS_ADD:
5185 label2 = ct->path;
5186 break;
5187 case GOT_STATUS_DELETE:
5188 label1 = ct->path;
5189 break;
5190 default:
5191 return got_error(GOT_ERR_FILE_STATUS);
5193 fd1 = got_opentempfd();
5194 if (fd1 == -1) {
5195 err = got_error_from_errno("got_opentempfd");
5196 goto done;
5198 fd2 = got_opentempfd();
5199 if (fd2 == -1) {
5200 err = got_error_from_errno("got_opentempfd");
5201 goto done;
5203 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5204 fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5205 label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5206 NULL, repo, diff_outfile);
5207 goto done;
5210 fd1 = got_opentempfd();
5211 if (fd1 == -1) {
5212 err = got_error_from_errno("got_opentempfd");
5213 goto done;
5216 if (ct->status != GOT_STATUS_ADD) {
5217 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5218 8192, fd1);
5219 if (err)
5220 goto done;
5223 if (ct->status != GOT_STATUS_DELETE) {
5224 if (dirfd != -1) {
5225 fd = openat(dirfd, de_name,
5226 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5227 if (fd == -1) {
5228 if (!got_err_open_nofollow_on_symlink()) {
5229 err = got_error_from_errno2("openat",
5230 ct->ondisk_path);
5231 goto done;
5233 err = get_symlink_target_file(&fd, dirfd,
5234 de_name, ct->ondisk_path);
5235 if (err)
5236 goto done;
5238 } else {
5239 fd = open(ct->ondisk_path,
5240 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5241 if (fd == -1) {
5242 if (!got_err_open_nofollow_on_symlink()) {
5243 err = got_error_from_errno2("open",
5244 ct->ondisk_path);
5245 goto done;
5247 err = get_symlink_target_file(&fd, dirfd,
5248 de_name, ct->ondisk_path);
5249 if (err)
5250 goto done;
5253 if (fstatat(fd, ct->ondisk_path, &sb,
5254 AT_SYMLINK_NOFOLLOW) == -1) {
5255 err = got_error_from_errno2("fstatat", ct->ondisk_path);
5256 goto done;
5258 ondisk_file = fdopen(fd, "r");
5259 if (ondisk_file == NULL) {
5260 err = got_error_from_errno2("fdopen", ct->ondisk_path);
5261 goto done;
5263 fd = -1;
5264 f2_exists = 1;
5267 if (blob1) {
5268 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5269 f1, blob1);
5270 if (err)
5271 goto done;
5274 err = got_diff_blob_file(blob1, f1, size1, label1,
5275 ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5276 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5277 done:
5278 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5279 err = got_error_from_errno("close");
5280 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5281 err = got_error_from_errno("close");
5282 if (blob1)
5283 got_object_blob_close(blob1);
5284 if (fd != -1 && close(fd) == -1 && err == NULL)
5285 err = got_error_from_errno("close");
5286 if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5287 err = got_error_from_errno("fclose");
5288 return err;
5291 static const struct got_error *
5292 collect_commitables(void *arg, unsigned char status,
5293 unsigned char staged_status, const char *relpath,
5294 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5295 struct got_object_id *commit_id, int dirfd, const char *de_name)
5297 struct collect_commitables_arg *a = arg;
5298 const struct got_error *err = NULL;
5299 struct got_commitable *ct = NULL;
5300 struct got_pathlist_entry *new = NULL;
5301 char *parent_path = NULL, *path = NULL;
5302 struct stat sb;
5304 if (a->have_staged_files) {
5305 if (staged_status != GOT_STATUS_MODIFY &&
5306 staged_status != GOT_STATUS_ADD &&
5307 staged_status != GOT_STATUS_DELETE)
5308 return NULL;
5309 } else {
5310 if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5311 printf("C %s\n", relpath);
5312 return got_error(GOT_ERR_COMMIT_CONFLICT);
5315 if (status != GOT_STATUS_MODIFY &&
5316 status != GOT_STATUS_MODE_CHANGE &&
5317 status != GOT_STATUS_ADD &&
5318 status != GOT_STATUS_DELETE &&
5319 status != GOT_STATUS_CONFLICT)
5320 return NULL;
5323 if (asprintf(&path, "/%s", relpath) == -1) {
5324 err = got_error_from_errno("asprintf");
5325 goto done;
5327 if (strcmp(path, "/") == 0) {
5328 parent_path = strdup("");
5329 if (parent_path == NULL)
5330 return got_error_from_errno("strdup");
5331 } else {
5332 err = got_path_dirname(&parent_path, path);
5333 if (err)
5334 return err;
5337 ct = calloc(1, sizeof(*ct));
5338 if (ct == NULL) {
5339 err = got_error_from_errno("calloc");
5340 goto done;
5343 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5344 relpath) == -1) {
5345 err = got_error_from_errno("asprintf");
5346 goto done;
5349 if (staged_status == GOT_STATUS_ADD ||
5350 staged_status == GOT_STATUS_MODIFY) {
5351 struct got_fileindex_entry *ie;
5352 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5353 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5354 case GOT_FILEIDX_MODE_REGULAR_FILE:
5355 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5356 ct->mode = S_IFREG;
5357 break;
5358 case GOT_FILEIDX_MODE_SYMLINK:
5359 ct->mode = S_IFLNK;
5360 break;
5361 default:
5362 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5363 goto done;
5365 ct->mode |= got_fileindex_entry_perms_get(ie);
5366 } else if (status != GOT_STATUS_DELETE &&
5367 staged_status != GOT_STATUS_DELETE) {
5368 if (dirfd != -1) {
5369 if (fstatat(dirfd, de_name, &sb,
5370 AT_SYMLINK_NOFOLLOW) == -1) {
5371 err = got_error_from_errno2("fstatat",
5372 ct->ondisk_path);
5373 goto done;
5375 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5376 err = got_error_from_errno2("lstat", ct->ondisk_path);
5377 goto done;
5379 ct->mode = sb.st_mode;
5382 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5383 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5384 relpath) == -1) {
5385 err = got_error_from_errno("asprintf");
5386 goto done;
5389 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5390 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5391 int is_bad_symlink;
5392 char target_path[PATH_MAX];
5393 ssize_t target_len;
5394 target_len = readlink(ct->ondisk_path, target_path,
5395 sizeof(target_path));
5396 if (target_len == -1) {
5397 err = got_error_from_errno2("readlink",
5398 ct->ondisk_path);
5399 goto done;
5401 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5402 target_len, ct->ondisk_path, a->worktree->root_path);
5403 if (err)
5404 goto done;
5405 if (is_bad_symlink) {
5406 err = got_error_path(ct->ondisk_path,
5407 GOT_ERR_BAD_SYMLINK);
5408 goto done;
5413 ct->status = status;
5414 ct->staged_status = staged_status;
5415 ct->blob_id = NULL; /* will be filled in when blob gets created */
5416 if (ct->status != GOT_STATUS_ADD &&
5417 ct->staged_status != GOT_STATUS_ADD) {
5418 ct->base_blob_id = got_object_id_dup(blob_id);
5419 if (ct->base_blob_id == NULL) {
5420 err = got_error_from_errno("got_object_id_dup");
5421 goto done;
5423 ct->base_commit_id = got_object_id_dup(commit_id);
5424 if (ct->base_commit_id == NULL) {
5425 err = got_error_from_errno("got_object_id_dup");
5426 goto done;
5429 if (ct->staged_status == GOT_STATUS_ADD ||
5430 ct->staged_status == GOT_STATUS_MODIFY) {
5431 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5432 if (ct->staged_blob_id == NULL) {
5433 err = got_error_from_errno("got_object_id_dup");
5434 goto done;
5437 ct->path = strdup(path);
5438 if (ct->path == NULL) {
5439 err = got_error_from_errno("strdup");
5440 goto done;
5442 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5443 if (err)
5444 goto done;
5446 if (a->diff_outfile && ct && new != NULL) {
5447 err = append_ct_diff(ct, &a->diff_header_shown,
5448 a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5449 a->have_staged_files, a->repo, a->worktree);
5450 if (err)
5451 goto done;
5453 done:
5454 if (ct && (err || new == NULL))
5455 free_commitable(ct);
5456 free(parent_path);
5457 free(path);
5458 return err;
5461 static const struct got_error *write_tree(struct got_object_id **, int *,
5462 struct got_tree_object *, const char *, struct got_pathlist_head *,
5463 got_worktree_status_cb status_cb, void *status_arg,
5464 struct got_repository *);
5466 static const struct got_error *
5467 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5468 struct got_tree_entry *te, const char *parent_path,
5469 struct got_pathlist_head *commitable_paths,
5470 got_worktree_status_cb status_cb, void *status_arg,
5471 struct got_repository *repo)
5473 const struct got_error *err = NULL;
5474 struct got_tree_object *subtree;
5475 char *subpath;
5477 if (asprintf(&subpath, "%s%s%s", parent_path,
5478 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5479 return got_error_from_errno("asprintf");
5481 err = got_object_open_as_tree(&subtree, repo, &te->id);
5482 if (err)
5483 return err;
5485 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5486 commitable_paths, status_cb, status_arg, repo);
5487 got_object_tree_close(subtree);
5488 free(subpath);
5489 return err;
5492 static const struct got_error *
5493 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5495 const struct got_error *err = NULL;
5496 char *ct_parent_path = NULL;
5498 *match = 0;
5500 if (strchr(ct->in_repo_path, '/') == NULL) {
5501 *match = got_path_is_root_dir(path);
5502 return NULL;
5505 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5506 if (err)
5507 return err;
5508 *match = (strcmp(path, ct_parent_path) == 0);
5509 free(ct_parent_path);
5510 return err;
5513 static mode_t
5514 get_ct_file_mode(struct got_commitable *ct)
5516 if (S_ISLNK(ct->mode))
5517 return S_IFLNK;
5519 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5522 static const struct got_error *
5523 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5524 struct got_tree_entry *te, struct got_commitable *ct)
5526 const struct got_error *err = NULL;
5528 *new_te = NULL;
5530 err = got_object_tree_entry_dup(new_te, te);
5531 if (err)
5532 goto done;
5534 (*new_te)->mode = get_ct_file_mode(ct);
5536 if (ct->staged_status == GOT_STATUS_MODIFY)
5537 memcpy(&(*new_te)->id, ct->staged_blob_id,
5538 sizeof((*new_te)->id));
5539 else
5540 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5541 done:
5542 if (err && *new_te) {
5543 free(*new_te);
5544 *new_te = NULL;
5546 return err;
5549 static const struct got_error *
5550 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5551 struct got_commitable *ct)
5553 const struct got_error *err = NULL;
5554 char *ct_name = NULL;
5556 *new_te = NULL;
5558 *new_te = calloc(1, sizeof(**new_te));
5559 if (*new_te == NULL)
5560 return got_error_from_errno("calloc");
5562 err = got_path_basename(&ct_name, ct->path);
5563 if (err)
5564 goto done;
5565 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5566 sizeof((*new_te)->name)) {
5567 err = got_error(GOT_ERR_NO_SPACE);
5568 goto done;
5571 (*new_te)->mode = get_ct_file_mode(ct);
5573 if (ct->staged_status == GOT_STATUS_ADD)
5574 memcpy(&(*new_te)->id, ct->staged_blob_id,
5575 sizeof((*new_te)->id));
5576 else
5577 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5578 done:
5579 free(ct_name);
5580 if (err && *new_te) {
5581 free(*new_te);
5582 *new_te = NULL;
5584 return err;
5587 static const struct got_error *
5588 insert_tree_entry(struct got_tree_entry *new_te,
5589 struct got_pathlist_head *paths)
5591 const struct got_error *err = NULL;
5592 struct got_pathlist_entry *new_pe;
5594 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5595 if (err)
5596 return err;
5597 if (new_pe == NULL)
5598 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5599 return NULL;
5602 static const struct got_error *
5603 report_ct_status(struct got_commitable *ct,
5604 got_worktree_status_cb status_cb, void *status_arg)
5606 const char *ct_path = ct->path;
5607 unsigned char status;
5609 if (status_cb == NULL) /* no commit progress output desired */
5610 return NULL;
5612 while (ct_path[0] == '/')
5613 ct_path++;
5615 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5616 status = ct->staged_status;
5617 else
5618 status = ct->status;
5620 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5621 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5624 static const struct got_error *
5625 match_modified_subtree(int *modified, struct got_tree_entry *te,
5626 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5628 const struct got_error *err = NULL;
5629 struct got_pathlist_entry *pe;
5630 char *te_path;
5632 *modified = 0;
5634 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5635 got_path_is_root_dir(base_tree_path) ? "" : "/",
5636 te->name) == -1)
5637 return got_error_from_errno("asprintf");
5639 TAILQ_FOREACH(pe, commitable_paths, entry) {
5640 struct got_commitable *ct = pe->data;
5641 *modified = got_path_is_child(ct->in_repo_path, te_path,
5642 strlen(te_path));
5643 if (*modified)
5644 break;
5647 free(te_path);
5648 return err;
5651 static const struct got_error *
5652 match_deleted_or_modified_ct(struct got_commitable **ctp,
5653 struct got_tree_entry *te, const char *base_tree_path,
5654 struct got_pathlist_head *commitable_paths)
5656 const struct got_error *err = NULL;
5657 struct got_pathlist_entry *pe;
5659 *ctp = NULL;
5661 TAILQ_FOREACH(pe, commitable_paths, entry) {
5662 struct got_commitable *ct = pe->data;
5663 char *ct_name = NULL;
5664 int path_matches;
5666 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5667 if (ct->status != GOT_STATUS_MODIFY &&
5668 ct->status != GOT_STATUS_MODE_CHANGE &&
5669 ct->status != GOT_STATUS_DELETE &&
5670 ct->status != GOT_STATUS_CONFLICT)
5671 continue;
5672 } else {
5673 if (ct->staged_status != GOT_STATUS_MODIFY &&
5674 ct->staged_status != GOT_STATUS_DELETE)
5675 continue;
5678 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5679 continue;
5681 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5682 if (err)
5683 return err;
5684 if (!path_matches)
5685 continue;
5687 err = got_path_basename(&ct_name, pe->path);
5688 if (err)
5689 return err;
5691 if (strcmp(te->name, ct_name) != 0) {
5692 free(ct_name);
5693 continue;
5695 free(ct_name);
5697 *ctp = ct;
5698 break;
5701 return err;
5704 static const struct got_error *
5705 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5706 const char *child_path, const char *path_base_tree,
5707 struct got_pathlist_head *commitable_paths,
5708 got_worktree_status_cb status_cb, void *status_arg,
5709 struct got_repository *repo)
5711 const struct got_error *err = NULL;
5712 struct got_tree_entry *new_te;
5713 char *subtree_path;
5714 struct got_object_id *id = NULL;
5715 int nentries;
5717 *new_tep = NULL;
5719 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5720 got_path_is_root_dir(path_base_tree) ? "" : "/",
5721 child_path) == -1)
5722 return got_error_from_errno("asprintf");
5724 new_te = calloc(1, sizeof(*new_te));
5725 if (new_te == NULL)
5726 return got_error_from_errno("calloc");
5727 new_te->mode = S_IFDIR;
5729 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5730 sizeof(new_te->name)) {
5731 err = got_error(GOT_ERR_NO_SPACE);
5732 goto done;
5734 err = write_tree(&id, &nentries, NULL, subtree_path,
5735 commitable_paths, status_cb, status_arg, repo);
5736 if (err) {
5737 free(new_te);
5738 goto done;
5740 memcpy(&new_te->id, id, sizeof(new_te->id));
5741 done:
5742 free(id);
5743 free(subtree_path);
5744 if (err == NULL)
5745 *new_tep = new_te;
5746 return err;
5749 static const struct got_error *
5750 write_tree(struct got_object_id **new_tree_id, int *nentries,
5751 struct got_tree_object *base_tree, const char *path_base_tree,
5752 struct got_pathlist_head *commitable_paths,
5753 got_worktree_status_cb status_cb, void *status_arg,
5754 struct got_repository *repo)
5756 const struct got_error *err = NULL;
5757 struct got_pathlist_head paths;
5758 struct got_tree_entry *te, *new_te = NULL;
5759 struct got_pathlist_entry *pe;
5761 TAILQ_INIT(&paths);
5762 *nentries = 0;
5764 /* Insert, and recurse into, newly added entries first. */
5765 TAILQ_FOREACH(pe, commitable_paths, entry) {
5766 struct got_commitable *ct = pe->data;
5767 char *child_path = NULL, *slash;
5769 if ((ct->status != GOT_STATUS_ADD &&
5770 ct->staged_status != GOT_STATUS_ADD) ||
5771 (ct->flags & GOT_COMMITABLE_ADDED))
5772 continue;
5774 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5775 strlen(path_base_tree)))
5776 continue;
5778 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5779 ct->in_repo_path);
5780 if (err)
5781 goto done;
5783 slash = strchr(child_path, '/');
5784 if (slash == NULL) {
5785 err = alloc_added_blob_tree_entry(&new_te, ct);
5786 if (err)
5787 goto done;
5788 err = report_ct_status(ct, status_cb, status_arg);
5789 if (err)
5790 goto done;
5791 ct->flags |= GOT_COMMITABLE_ADDED;
5792 err = insert_tree_entry(new_te, &paths);
5793 if (err)
5794 goto done;
5795 (*nentries)++;
5796 } else {
5797 *slash = '\0'; /* trim trailing path components */
5798 if (base_tree == NULL ||
5799 got_object_tree_find_entry(base_tree, child_path)
5800 == NULL) {
5801 err = make_subtree_for_added_blob(&new_te,
5802 child_path, path_base_tree,
5803 commitable_paths, status_cb, status_arg,
5804 repo);
5805 if (err)
5806 goto done;
5807 err = insert_tree_entry(new_te, &paths);
5808 if (err)
5809 goto done;
5810 (*nentries)++;
5815 if (base_tree) {
5816 int i, nbase_entries;
5817 /* Handle modified and deleted entries. */
5818 nbase_entries = got_object_tree_get_nentries(base_tree);
5819 for (i = 0; i < nbase_entries; i++) {
5820 struct got_commitable *ct = NULL;
5822 te = got_object_tree_get_entry(base_tree, i);
5823 if (got_object_tree_entry_is_submodule(te)) {
5824 /* Entry is a submodule; just copy it. */
5825 err = got_object_tree_entry_dup(&new_te, te);
5826 if (err)
5827 goto done;
5828 err = insert_tree_entry(new_te, &paths);
5829 if (err)
5830 goto done;
5831 (*nentries)++;
5832 continue;
5835 if (S_ISDIR(te->mode)) {
5836 int modified;
5837 err = got_object_tree_entry_dup(&new_te, te);
5838 if (err)
5839 goto done;
5840 err = match_modified_subtree(&modified, te,
5841 path_base_tree, commitable_paths);
5842 if (err)
5843 goto done;
5844 /* Avoid recursion into unmodified subtrees. */
5845 if (modified) {
5846 struct got_object_id *new_id;
5847 int nsubentries;
5848 err = write_subtree(&new_id,
5849 &nsubentries, te,
5850 path_base_tree, commitable_paths,
5851 status_cb, status_arg, repo);
5852 if (err)
5853 goto done;
5854 if (nsubentries == 0) {
5855 /* All entries were deleted. */
5856 free(new_id);
5857 continue;
5859 memcpy(&new_te->id, new_id,
5860 sizeof(new_te->id));
5861 free(new_id);
5863 err = insert_tree_entry(new_te, &paths);
5864 if (err)
5865 goto done;
5866 (*nentries)++;
5867 continue;
5870 err = match_deleted_or_modified_ct(&ct, te,
5871 path_base_tree, commitable_paths);
5872 if (err)
5873 goto done;
5874 if (ct) {
5875 /* NB: Deleted entries get dropped here. */
5876 if (ct->status == GOT_STATUS_MODIFY ||
5877 ct->status == GOT_STATUS_MODE_CHANGE ||
5878 ct->status == GOT_STATUS_CONFLICT ||
5879 ct->staged_status == GOT_STATUS_MODIFY) {
5880 err = alloc_modified_blob_tree_entry(
5881 &new_te, te, ct);
5882 if (err)
5883 goto done;
5884 err = insert_tree_entry(new_te, &paths);
5885 if (err)
5886 goto done;
5887 (*nentries)++;
5889 err = report_ct_status(ct, status_cb,
5890 status_arg);
5891 if (err)
5892 goto done;
5893 } else {
5894 /* Entry is unchanged; just copy it. */
5895 err = got_object_tree_entry_dup(&new_te, te);
5896 if (err)
5897 goto done;
5898 err = insert_tree_entry(new_te, &paths);
5899 if (err)
5900 goto done;
5901 (*nentries)++;
5906 /* Write new list of entries; deleted entries have been dropped. */
5907 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5908 done:
5909 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
5910 return err;
5913 static const struct got_error *
5914 update_fileindex_after_commit(struct got_worktree *worktree,
5915 struct got_pathlist_head *commitable_paths,
5916 struct got_object_id *new_base_commit_id,
5917 struct got_fileindex *fileindex, int have_staged_files)
5919 const struct got_error *err = NULL;
5920 struct got_pathlist_entry *pe;
5921 char *relpath = NULL;
5923 TAILQ_FOREACH(pe, commitable_paths, entry) {
5924 struct got_fileindex_entry *ie;
5925 struct got_commitable *ct = pe->data;
5927 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5929 err = got_path_skip_common_ancestor(&relpath,
5930 worktree->root_path, ct->ondisk_path);
5931 if (err)
5932 goto done;
5934 if (ie) {
5935 if (ct->status == GOT_STATUS_DELETE ||
5936 ct->staged_status == GOT_STATUS_DELETE) {
5937 got_fileindex_entry_remove(fileindex, ie);
5938 } else if (ct->staged_status == GOT_STATUS_ADD ||
5939 ct->staged_status == GOT_STATUS_MODIFY) {
5940 got_fileindex_entry_stage_set(ie,
5941 GOT_FILEIDX_STAGE_NONE);
5942 got_fileindex_entry_staged_filetype_set(ie, 0);
5944 err = got_fileindex_entry_update(ie,
5945 worktree->root_fd, relpath,
5946 ct->staged_blob_id->sha1,
5947 new_base_commit_id->sha1,
5948 !have_staged_files);
5949 } else
5950 err = got_fileindex_entry_update(ie,
5951 worktree->root_fd, relpath,
5952 ct->blob_id->sha1,
5953 new_base_commit_id->sha1,
5954 !have_staged_files);
5955 } else {
5956 err = got_fileindex_entry_alloc(&ie, pe->path);
5957 if (err)
5958 goto done;
5959 err = got_fileindex_entry_update(ie,
5960 worktree->root_fd, relpath, ct->blob_id->sha1,
5961 new_base_commit_id->sha1, 1);
5962 if (err) {
5963 got_fileindex_entry_free(ie);
5964 goto done;
5966 err = got_fileindex_entry_add(fileindex, ie);
5967 if (err) {
5968 got_fileindex_entry_free(ie);
5969 goto done;
5972 free(relpath);
5973 relpath = NULL;
5975 done:
5976 free(relpath);
5977 return err;
5981 static const struct got_error *
5982 check_out_of_date(const char *in_repo_path, unsigned char status,
5983 unsigned char staged_status, struct got_object_id *base_blob_id,
5984 struct got_object_id *base_commit_id,
5985 struct got_object_id *head_commit_id, struct got_repository *repo,
5986 int ood_errcode)
5988 const struct got_error *err = NULL;
5989 struct got_commit_object *commit = NULL;
5990 struct got_object_id *id = NULL;
5992 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5993 /* Trivial case: base commit == head commit */
5994 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5995 return NULL;
5997 * Ensure file content which local changes were based
5998 * on matches file content in the branch head.
6000 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6001 if (err)
6002 goto done;
6003 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6004 if (err) {
6005 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6006 err = got_error(ood_errcode);
6007 goto done;
6008 } else if (got_object_id_cmp(id, base_blob_id) != 0)
6009 err = got_error(ood_errcode);
6010 } else {
6011 /* Require that added files don't exist in the branch head. */
6012 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6013 if (err)
6014 goto done;
6015 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6016 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
6017 goto done;
6018 err = id ? got_error(ood_errcode) : NULL;
6020 done:
6021 free(id);
6022 if (commit)
6023 got_object_commit_close(commit);
6024 return err;
6027 static const struct got_error *
6028 commit_worktree(struct got_object_id **new_commit_id,
6029 struct got_pathlist_head *commitable_paths,
6030 struct got_object_id *head_commit_id,
6031 struct got_object_id *parent_id2,
6032 struct got_worktree *worktree,
6033 const char *author, const char *committer, char *diff_path,
6034 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6035 got_worktree_status_cb status_cb, void *status_arg,
6036 struct got_repository *repo)
6038 const struct got_error *err = NULL, *unlockerr = NULL;
6039 struct got_pathlist_entry *pe;
6040 const char *head_ref_name = NULL;
6041 struct got_commit_object *head_commit = NULL;
6042 struct got_reference *head_ref2 = NULL;
6043 struct got_object_id *head_commit_id2 = NULL;
6044 struct got_tree_object *head_tree = NULL;
6045 struct got_object_id *new_tree_id = NULL;
6046 int nentries, nparents = 0;
6047 struct got_object_id_queue parent_ids;
6048 struct got_object_qid *pid = NULL;
6049 char *logmsg = NULL;
6050 time_t timestamp;
6052 *new_commit_id = NULL;
6054 STAILQ_INIT(&parent_ids);
6056 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6057 if (err)
6058 goto done;
6060 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6061 if (err)
6062 goto done;
6064 if (commit_msg_cb != NULL) {
6065 err = commit_msg_cb(commitable_paths, diff_path,
6066 &logmsg, commit_arg);
6067 if (err)
6068 goto done;
6071 if (logmsg == NULL || strlen(logmsg) == 0) {
6072 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6073 goto done;
6076 /* Create blobs from added and modified files and record their IDs. */
6077 TAILQ_FOREACH(pe, commitable_paths, entry) {
6078 struct got_commitable *ct = pe->data;
6079 char *ondisk_path;
6081 /* Blobs for staged files already exist. */
6082 if (ct->staged_status == GOT_STATUS_ADD ||
6083 ct->staged_status == GOT_STATUS_MODIFY)
6084 continue;
6086 if (ct->status != GOT_STATUS_ADD &&
6087 ct->status != GOT_STATUS_MODIFY &&
6088 ct->status != GOT_STATUS_MODE_CHANGE &&
6089 ct->status != GOT_STATUS_CONFLICT)
6090 continue;
6092 if (asprintf(&ondisk_path, "%s/%s",
6093 worktree->root_path, pe->path) == -1) {
6094 err = got_error_from_errno("asprintf");
6095 goto done;
6097 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6098 free(ondisk_path);
6099 if (err)
6100 goto done;
6103 /* Recursively write new tree objects. */
6104 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6105 commitable_paths, status_cb, status_arg, repo);
6106 if (err)
6107 goto done;
6109 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
6110 if (err)
6111 goto done;
6112 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6113 nparents++;
6114 if (parent_id2) {
6115 err = got_object_qid_alloc(&pid, parent_id2);
6116 if (err)
6117 goto done;
6118 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6119 nparents++;
6121 timestamp = time(NULL);
6122 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6123 nparents, author, timestamp, committer, timestamp, logmsg, repo);
6124 if (logmsg != NULL)
6125 free(logmsg);
6126 if (err)
6127 goto done;
6129 /* Check if a concurrent commit to our branch has occurred. */
6130 head_ref_name = got_worktree_get_head_ref_name(worktree);
6131 if (head_ref_name == NULL) {
6132 err = got_error_from_errno("got_worktree_get_head_ref_name");
6133 goto done;
6135 /* Lock the reference here to prevent concurrent modification. */
6136 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6137 if (err)
6138 goto done;
6139 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6140 if (err)
6141 goto done;
6142 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6143 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6144 goto done;
6146 /* Update branch head in repository. */
6147 err = got_ref_change_ref(head_ref2, *new_commit_id);
6148 if (err)
6149 goto done;
6150 err = got_ref_write(head_ref2, repo);
6151 if (err)
6152 goto done;
6154 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6155 if (err)
6156 goto done;
6158 err = ref_base_commit(worktree, repo);
6159 if (err)
6160 goto done;
6161 done:
6162 got_object_id_queue_free(&parent_ids);
6163 if (head_tree)
6164 got_object_tree_close(head_tree);
6165 if (head_commit)
6166 got_object_commit_close(head_commit);
6167 free(head_commit_id2);
6168 if (head_ref2) {
6169 unlockerr = got_ref_unlock(head_ref2);
6170 if (unlockerr && err == NULL)
6171 err = unlockerr;
6172 got_ref_close(head_ref2);
6174 return err;
6177 static const struct got_error *
6178 check_path_is_commitable(const char *path,
6179 struct got_pathlist_head *commitable_paths)
6181 struct got_pathlist_entry *cpe = NULL;
6182 size_t path_len = strlen(path);
6184 TAILQ_FOREACH(cpe, commitable_paths, entry) {
6185 struct got_commitable *ct = cpe->data;
6186 const char *ct_path = ct->path;
6188 while (ct_path[0] == '/')
6189 ct_path++;
6191 if (strcmp(path, ct_path) == 0 ||
6192 got_path_is_child(ct_path, path, path_len))
6193 break;
6196 if (cpe == NULL)
6197 return got_error_path(path, GOT_ERR_BAD_PATH);
6199 return NULL;
6202 static const struct got_error *
6203 check_staged_file(void *arg, struct got_fileindex_entry *ie)
6205 int *have_staged_files = arg;
6207 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6208 *have_staged_files = 1;
6209 return got_error(GOT_ERR_CANCELLED);
6212 return NULL;
6215 static const struct got_error *
6216 check_non_staged_files(struct got_fileindex *fileindex,
6217 struct got_pathlist_head *paths)
6219 struct got_pathlist_entry *pe;
6220 struct got_fileindex_entry *ie;
6222 TAILQ_FOREACH(pe, paths, entry) {
6223 if (pe->path[0] == '\0')
6224 continue;
6225 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6226 if (ie == NULL)
6227 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6228 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6229 return got_error_path(pe->path,
6230 GOT_ERR_FILE_NOT_STAGED);
6233 return NULL;
6236 const struct got_error *
6237 got_worktree_commit(struct got_object_id **new_commit_id,
6238 struct got_worktree *worktree, struct got_pathlist_head *paths,
6239 const char *author, const char *committer, int allow_bad_symlinks,
6240 int show_diff, int commit_conflicts,
6241 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6242 got_worktree_status_cb status_cb, void *status_arg,
6243 struct got_repository *repo)
6245 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6246 struct got_fileindex *fileindex = NULL;
6247 char *fileindex_path = NULL;
6248 struct got_pathlist_head commitable_paths;
6249 struct collect_commitables_arg cc_arg;
6250 struct got_pathlist_entry *pe;
6251 struct got_reference *head_ref = NULL;
6252 struct got_object_id *head_commit_id = NULL;
6253 char *diff_path = NULL;
6254 int have_staged_files = 0;
6256 *new_commit_id = NULL;
6258 memset(&cc_arg, 0, sizeof(cc_arg));
6259 TAILQ_INIT(&commitable_paths);
6261 err = lock_worktree(worktree, LOCK_EX);
6262 if (err)
6263 goto done;
6265 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6266 if (err)
6267 goto done;
6269 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6270 if (err)
6271 goto done;
6273 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6274 if (err)
6275 goto done;
6277 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6278 &have_staged_files);
6279 if (err && err->code != GOT_ERR_CANCELLED)
6280 goto done;
6281 if (have_staged_files) {
6282 err = check_non_staged_files(fileindex, paths);
6283 if (err)
6284 goto done;
6287 cc_arg.commitable_paths = &commitable_paths;
6288 cc_arg.worktree = worktree;
6289 cc_arg.fileindex = fileindex;
6290 cc_arg.repo = repo;
6291 cc_arg.have_staged_files = have_staged_files;
6292 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6293 cc_arg.diff_header_shown = 0;
6294 cc_arg.commit_conflicts = commit_conflicts;
6295 if (show_diff) {
6296 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6297 GOT_TMPDIR_STR "/got", ".diff");
6298 if (err)
6299 goto done;
6300 cc_arg.f1 = got_opentemp();
6301 if (cc_arg.f1 == NULL) {
6302 err = got_error_from_errno("got_opentemp");
6303 goto done;
6305 cc_arg.f2 = got_opentemp();
6306 if (cc_arg.f2 == NULL) {
6307 err = got_error_from_errno("got_opentemp");
6308 goto done;
6312 TAILQ_FOREACH(pe, paths, entry) {
6313 err = worktree_status(worktree, pe->path, fileindex, repo,
6314 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6315 if (err)
6316 goto done;
6319 if (show_diff) {
6320 if (fflush(cc_arg.diff_outfile) == EOF) {
6321 err = got_error_from_errno("fflush");
6322 goto done;
6326 if (TAILQ_EMPTY(&commitable_paths)) {
6327 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6328 goto done;
6331 TAILQ_FOREACH(pe, paths, entry) {
6332 err = check_path_is_commitable(pe->path, &commitable_paths);
6333 if (err)
6334 goto done;
6337 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6338 struct got_commitable *ct = pe->data;
6339 const char *ct_path = ct->in_repo_path;
6341 while (ct_path[0] == '/')
6342 ct_path++;
6343 err = check_out_of_date(ct_path, ct->status,
6344 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6345 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6346 if (err)
6347 goto done;
6351 err = commit_worktree(new_commit_id, &commitable_paths,
6352 head_commit_id, NULL, worktree, author, committer,
6353 (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6354 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6355 if (err)
6356 goto done;
6358 err = update_fileindex_after_commit(worktree, &commitable_paths,
6359 *new_commit_id, fileindex, have_staged_files);
6360 sync_err = sync_fileindex(fileindex, fileindex_path);
6361 if (sync_err && err == NULL)
6362 err = sync_err;
6363 done:
6364 if (fileindex)
6365 got_fileindex_free(fileindex);
6366 free(fileindex_path);
6367 unlockerr = lock_worktree(worktree, LOCK_SH);
6368 if (unlockerr && err == NULL)
6369 err = unlockerr;
6370 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6371 struct got_commitable *ct = pe->data;
6373 free_commitable(ct);
6375 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6376 if (diff_path && unlink(diff_path) == -1 && err == NULL)
6377 err = got_error_from_errno2("unlink", diff_path);
6378 free(diff_path);
6379 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6380 err == NULL)
6381 err = got_error_from_errno("fclose");
6382 return err;
6385 const char *
6386 got_commitable_get_path(struct got_commitable *ct)
6388 return ct->path;
6391 unsigned int
6392 got_commitable_get_status(struct got_commitable *ct)
6394 return ct->status;
6397 struct check_rebase_ok_arg {
6398 struct got_worktree *worktree;
6399 struct got_repository *repo;
6402 static const struct got_error *
6403 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6405 const struct got_error *err = NULL;
6406 struct check_rebase_ok_arg *a = arg;
6407 unsigned char status;
6408 struct stat sb;
6409 char *ondisk_path;
6411 /* Reject rebase of a work tree with mixed base commits. */
6412 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6413 SHA1_DIGEST_LENGTH))
6414 return got_error(GOT_ERR_MIXED_COMMITS);
6416 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6417 == -1)
6418 return got_error_from_errno("asprintf");
6420 /* Reject rebase of a work tree with modified or staged files. */
6421 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6422 free(ondisk_path);
6423 if (err)
6424 return err;
6426 if (status != GOT_STATUS_NO_CHANGE)
6427 return got_error(GOT_ERR_MODIFIED);
6428 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6429 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6431 return NULL;
6434 const struct got_error *
6435 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6436 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6437 struct got_worktree *worktree, struct got_reference *branch,
6438 struct got_repository *repo)
6440 const struct got_error *err = NULL;
6441 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6442 char *branch_ref_name = NULL;
6443 char *fileindex_path = NULL;
6444 struct check_rebase_ok_arg ok_arg;
6445 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6446 struct got_object_id *wt_branch_tip = NULL;
6448 *new_base_branch_ref = NULL;
6449 *tmp_branch = NULL;
6450 *fileindex = NULL;
6452 err = lock_worktree(worktree, LOCK_EX);
6453 if (err)
6454 return err;
6456 err = open_fileindex(fileindex, &fileindex_path, worktree);
6457 if (err)
6458 goto done;
6460 ok_arg.worktree = worktree;
6461 ok_arg.repo = repo;
6462 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6463 &ok_arg);
6464 if (err)
6465 goto done;
6467 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6468 if (err)
6469 goto done;
6471 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6472 if (err)
6473 goto done;
6475 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6476 if (err)
6477 goto done;
6479 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6480 0);
6481 if (err)
6482 goto done;
6484 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6485 if (err)
6486 goto done;
6487 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6488 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6489 goto done;
6492 err = got_ref_alloc_symref(new_base_branch_ref,
6493 new_base_branch_ref_name, wt_branch);
6494 if (err)
6495 goto done;
6496 err = got_ref_write(*new_base_branch_ref, repo);
6497 if (err)
6498 goto done;
6500 /* TODO Lock original branch's ref while rebasing? */
6502 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6503 if (err)
6504 goto done;
6506 err = got_ref_write(branch_ref, repo);
6507 if (err)
6508 goto done;
6510 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6511 worktree->base_commit_id);
6512 if (err)
6513 goto done;
6514 err = got_ref_write(*tmp_branch, repo);
6515 if (err)
6516 goto done;
6518 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6519 if (err)
6520 goto done;
6521 done:
6522 free(fileindex_path);
6523 free(tmp_branch_name);
6524 free(new_base_branch_ref_name);
6525 free(branch_ref_name);
6526 if (branch_ref)
6527 got_ref_close(branch_ref);
6528 if (wt_branch)
6529 got_ref_close(wt_branch);
6530 free(wt_branch_tip);
6531 if (err) {
6532 if (*new_base_branch_ref) {
6533 got_ref_close(*new_base_branch_ref);
6534 *new_base_branch_ref = NULL;
6536 if (*tmp_branch) {
6537 got_ref_close(*tmp_branch);
6538 *tmp_branch = NULL;
6540 if (*fileindex) {
6541 got_fileindex_free(*fileindex);
6542 *fileindex = NULL;
6544 lock_worktree(worktree, LOCK_SH);
6546 return err;
6549 const struct got_error *
6550 got_worktree_rebase_continue(struct got_object_id **commit_id,
6551 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6552 struct got_reference **branch, struct got_fileindex **fileindex,
6553 struct got_worktree *worktree, struct got_repository *repo)
6555 const struct got_error *err;
6556 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6557 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6558 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6559 char *fileindex_path = NULL;
6560 int have_staged_files = 0;
6562 *commit_id = NULL;
6563 *new_base_branch = NULL;
6564 *tmp_branch = NULL;
6565 *branch = NULL;
6566 *fileindex = NULL;
6568 err = lock_worktree(worktree, LOCK_EX);
6569 if (err)
6570 return err;
6572 err = open_fileindex(fileindex, &fileindex_path, worktree);
6573 if (err)
6574 goto done;
6576 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6577 &have_staged_files);
6578 if (err && err->code != GOT_ERR_CANCELLED)
6579 goto done;
6580 if (have_staged_files) {
6581 err = got_error(GOT_ERR_STAGED_PATHS);
6582 goto done;
6585 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6586 if (err)
6587 goto done;
6589 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6590 if (err)
6591 goto done;
6593 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6594 if (err)
6595 goto done;
6597 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6598 if (err)
6599 goto done;
6601 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6602 if (err)
6603 goto done;
6605 err = got_ref_open(branch, repo,
6606 got_ref_get_symref_target(branch_ref), 0);
6607 if (err)
6608 goto done;
6610 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6611 if (err)
6612 goto done;
6614 err = got_ref_resolve(commit_id, repo, commit_ref);
6615 if (err)
6616 goto done;
6618 err = got_ref_open(new_base_branch, repo,
6619 new_base_branch_ref_name, 0);
6620 if (err)
6621 goto done;
6623 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6624 if (err)
6625 goto done;
6626 done:
6627 free(commit_ref_name);
6628 free(branch_ref_name);
6629 free(fileindex_path);
6630 if (commit_ref)
6631 got_ref_close(commit_ref);
6632 if (branch_ref)
6633 got_ref_close(branch_ref);
6634 if (err) {
6635 free(*commit_id);
6636 *commit_id = NULL;
6637 if (*tmp_branch) {
6638 got_ref_close(*tmp_branch);
6639 *tmp_branch = NULL;
6641 if (*new_base_branch) {
6642 got_ref_close(*new_base_branch);
6643 *new_base_branch = NULL;
6645 if (*branch) {
6646 got_ref_close(*branch);
6647 *branch = NULL;
6649 if (*fileindex) {
6650 got_fileindex_free(*fileindex);
6651 *fileindex = NULL;
6653 lock_worktree(worktree, LOCK_SH);
6655 return err;
6658 const struct got_error *
6659 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6661 const struct got_error *err;
6662 char *tmp_branch_name = NULL;
6664 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6665 if (err)
6666 return err;
6668 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6669 free(tmp_branch_name);
6670 return NULL;
6673 static const struct got_error *
6674 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6675 const char *diff_path, char **logmsg, void *arg)
6677 *logmsg = arg;
6678 return NULL;
6681 static const struct got_error *
6682 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6683 const char *path, struct got_object_id *blob_id,
6684 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6685 int dirfd, const char *de_name)
6687 return NULL;
6690 struct collect_merged_paths_arg {
6691 got_worktree_checkout_cb progress_cb;
6692 void *progress_arg;
6693 struct got_pathlist_head *merged_paths;
6696 static const struct got_error *
6697 collect_merged_paths(void *arg, unsigned char status, const char *path)
6699 const struct got_error *err;
6700 struct collect_merged_paths_arg *a = arg;
6701 char *p;
6702 struct got_pathlist_entry *new;
6704 err = (*a->progress_cb)(a->progress_arg, status, path);
6705 if (err)
6706 return err;
6708 if (status != GOT_STATUS_MERGE &&
6709 status != GOT_STATUS_ADD &&
6710 status != GOT_STATUS_DELETE &&
6711 status != GOT_STATUS_CONFLICT)
6712 return NULL;
6714 p = strdup(path);
6715 if (p == NULL)
6716 return got_error_from_errno("strdup");
6718 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6719 if (err || new == NULL)
6720 free(p);
6721 return err;
6724 static const struct got_error *
6725 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6726 int is_rebase, struct got_repository *repo)
6728 const struct got_error *err;
6729 struct got_reference *commit_ref = NULL;
6731 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6732 if (err) {
6733 if (err->code != GOT_ERR_NOT_REF)
6734 goto done;
6735 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6736 if (err)
6737 goto done;
6738 err = got_ref_write(commit_ref, repo);
6739 if (err)
6740 goto done;
6741 } else if (is_rebase) {
6742 struct got_object_id *stored_id;
6743 int cmp;
6745 err = got_ref_resolve(&stored_id, repo, commit_ref);
6746 if (err)
6747 goto done;
6748 cmp = got_object_id_cmp(commit_id, stored_id);
6749 free(stored_id);
6750 if (cmp != 0) {
6751 err = got_error(GOT_ERR_REBASE_COMMITID);
6752 goto done;
6755 done:
6756 if (commit_ref)
6757 got_ref_close(commit_ref);
6758 return err;
6761 static const struct got_error *
6762 rebase_merge_files(struct got_pathlist_head *merged_paths,
6763 const char *commit_ref_name, struct got_worktree *worktree,
6764 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6765 struct got_object_id *commit_id, struct got_repository *repo,
6766 got_worktree_checkout_cb progress_cb, void *progress_arg,
6767 got_cancel_cb cancel_cb, void *cancel_arg)
6769 const struct got_error *err;
6770 struct got_reference *commit_ref = NULL;
6771 struct collect_merged_paths_arg cmp_arg;
6772 char *fileindex_path;
6774 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6776 err = get_fileindex_path(&fileindex_path, worktree);
6777 if (err)
6778 return err;
6780 cmp_arg.progress_cb = progress_cb;
6781 cmp_arg.progress_arg = progress_arg;
6782 cmp_arg.merged_paths = merged_paths;
6783 err = merge_files(worktree, fileindex, fileindex_path,
6784 parent_commit_id, commit_id, repo, collect_merged_paths,
6785 &cmp_arg, cancel_cb, cancel_arg);
6786 if (commit_ref)
6787 got_ref_close(commit_ref);
6788 return err;
6791 const struct got_error *
6792 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6793 struct got_worktree *worktree, struct got_fileindex *fileindex,
6794 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6795 struct got_repository *repo,
6796 got_worktree_checkout_cb progress_cb, void *progress_arg,
6797 got_cancel_cb cancel_cb, void *cancel_arg)
6799 const struct got_error *err;
6800 char *commit_ref_name;
6802 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6803 if (err)
6804 return err;
6806 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6807 if (err)
6808 goto done;
6810 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6811 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6812 progress_arg, cancel_cb, cancel_arg);
6813 done:
6814 free(commit_ref_name);
6815 return err;
6818 const struct got_error *
6819 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6820 struct got_worktree *worktree, struct got_fileindex *fileindex,
6821 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6822 struct got_repository *repo,
6823 got_worktree_checkout_cb progress_cb, void *progress_arg,
6824 got_cancel_cb cancel_cb, void *cancel_arg)
6826 const struct got_error *err;
6827 char *commit_ref_name;
6829 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6830 if (err)
6831 return err;
6833 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6834 if (err)
6835 goto done;
6837 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6838 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6839 progress_arg, cancel_cb, cancel_arg);
6840 done:
6841 free(commit_ref_name);
6842 return err;
6845 static const struct got_error *
6846 rebase_commit(struct got_object_id **new_commit_id,
6847 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6848 struct got_worktree *worktree, struct got_fileindex *fileindex,
6849 struct got_reference *tmp_branch, const char *committer,
6850 struct got_commit_object *orig_commit, const char *new_logmsg,
6851 int allow_conflict, struct got_repository *repo)
6853 const struct got_error *err, *sync_err;
6854 struct got_pathlist_head commitable_paths;
6855 struct collect_commitables_arg cc_arg;
6856 char *fileindex_path = NULL;
6857 struct got_reference *head_ref = NULL;
6858 struct got_object_id *head_commit_id = NULL;
6859 char *logmsg = NULL;
6861 memset(&cc_arg, 0, sizeof(cc_arg));
6862 TAILQ_INIT(&commitable_paths);
6863 *new_commit_id = NULL;
6865 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6867 err = get_fileindex_path(&fileindex_path, worktree);
6868 if (err)
6869 return err;
6871 cc_arg.commitable_paths = &commitable_paths;
6872 cc_arg.worktree = worktree;
6873 cc_arg.repo = repo;
6874 cc_arg.have_staged_files = 0;
6875 cc_arg.commit_conflicts = allow_conflict;
6877 * If possible get the status of individual files directly to
6878 * avoid crawling the entire work tree once per rebased commit.
6880 * Ideally, merged_paths would contain a list of commitables
6881 * we could use so we could skip worktree_status() entirely.
6882 * However, we would then need carefully keep track of cumulative
6883 * effects of operations such as file additions and deletions
6884 * in 'got histedit -f' (folding multiple commits into one),
6885 * and this extra complexity is not really worth it.
6887 if (merged_paths) {
6888 struct got_pathlist_entry *pe;
6889 TAILQ_FOREACH(pe, merged_paths, entry) {
6890 err = worktree_status(worktree, pe->path, fileindex,
6891 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6892 0);
6893 if (err)
6894 goto done;
6896 } else {
6897 err = worktree_status(worktree, "", fileindex, repo,
6898 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6899 if (err)
6900 goto done;
6903 if (TAILQ_EMPTY(&commitable_paths)) {
6904 /* No-op change; commit will be elided. */
6905 err = got_ref_delete(commit_ref, repo);
6906 if (err)
6907 goto done;
6908 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6909 goto done;
6912 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6913 if (err)
6914 goto done;
6916 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6917 if (err)
6918 goto done;
6920 if (new_logmsg) {
6921 logmsg = strdup(new_logmsg);
6922 if (logmsg == NULL) {
6923 err = got_error_from_errno("strdup");
6924 goto done;
6926 } else {
6927 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6928 if (err)
6929 goto done;
6932 /* NB: commit_worktree will call free(logmsg) */
6933 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6934 NULL, worktree, got_object_commit_get_author(orig_commit),
6935 committer ? committer :
6936 got_object_commit_get_committer(orig_commit), NULL,
6937 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6938 if (err)
6939 goto done;
6941 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6942 if (err)
6943 goto done;
6945 err = got_ref_delete(commit_ref, repo);
6946 if (err)
6947 goto done;
6949 err = update_fileindex_after_commit(worktree, &commitable_paths,
6950 *new_commit_id, fileindex, 0);
6951 sync_err = sync_fileindex(fileindex, fileindex_path);
6952 if (sync_err && err == NULL)
6953 err = sync_err;
6954 done:
6955 free(fileindex_path);
6956 free(head_commit_id);
6957 if (head_ref)
6958 got_ref_close(head_ref);
6959 if (err) {
6960 free(*new_commit_id);
6961 *new_commit_id = NULL;
6963 return err;
6966 const struct got_error *
6967 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6968 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6969 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6970 const char *committer, struct got_commit_object *orig_commit,
6971 struct got_object_id *orig_commit_id, int allow_conflict,
6972 struct got_repository *repo)
6974 const struct got_error *err;
6975 char *commit_ref_name;
6976 struct got_reference *commit_ref = NULL;
6977 struct got_object_id *commit_id = NULL;
6979 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6980 if (err)
6981 return err;
6983 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6984 if (err)
6985 goto done;
6986 err = got_ref_resolve(&commit_id, repo, commit_ref);
6987 if (err)
6988 goto done;
6989 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6990 err = got_error(GOT_ERR_REBASE_COMMITID);
6991 goto done;
6994 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6995 worktree, fileindex, tmp_branch, committer, orig_commit,
6996 NULL, allow_conflict, repo);
6997 done:
6998 if (commit_ref)
6999 got_ref_close(commit_ref);
7000 free(commit_ref_name);
7001 free(commit_id);
7002 return err;
7005 const struct got_error *
7006 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
7007 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7008 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7009 const char *committer, struct got_commit_object *orig_commit,
7010 struct got_object_id *orig_commit_id, const char *new_logmsg,
7011 int allow_conflict, struct got_repository *repo)
7013 const struct got_error *err;
7014 char *commit_ref_name;
7015 struct got_reference *commit_ref = NULL;
7017 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7018 if (err)
7019 return err;
7021 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7022 if (err)
7023 goto done;
7025 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7026 worktree, fileindex, tmp_branch, committer, orig_commit,
7027 new_logmsg, allow_conflict, repo);
7028 done:
7029 if (commit_ref)
7030 got_ref_close(commit_ref);
7031 free(commit_ref_name);
7032 return err;
7035 const struct got_error *
7036 got_worktree_rebase_postpone(struct got_worktree *worktree,
7037 struct got_fileindex *fileindex)
7039 if (fileindex)
7040 got_fileindex_free(fileindex);
7041 return lock_worktree(worktree, LOCK_SH);
7044 static const struct got_error *
7045 delete_ref(const char *name, struct got_repository *repo)
7047 const struct got_error *err;
7048 struct got_reference *ref;
7050 err = got_ref_open(&ref, repo, name, 0);
7051 if (err) {
7052 if (err->code == GOT_ERR_NOT_REF)
7053 return NULL;
7054 return err;
7057 err = got_ref_delete(ref, repo);
7058 got_ref_close(ref);
7059 return err;
7062 static const struct got_error *
7063 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7065 const struct got_error *err;
7066 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7067 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7069 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7070 if (err)
7071 goto done;
7072 err = delete_ref(tmp_branch_name, repo);
7073 if (err)
7074 goto done;
7076 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7077 if (err)
7078 goto done;
7079 err = delete_ref(new_base_branch_ref_name, repo);
7080 if (err)
7081 goto done;
7083 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7084 if (err)
7085 goto done;
7086 err = delete_ref(branch_ref_name, repo);
7087 if (err)
7088 goto done;
7090 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7091 if (err)
7092 goto done;
7093 err = delete_ref(commit_ref_name, repo);
7094 if (err)
7095 goto done;
7097 done:
7098 free(tmp_branch_name);
7099 free(new_base_branch_ref_name);
7100 free(branch_ref_name);
7101 free(commit_ref_name);
7102 return err;
7105 static const struct got_error *
7106 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7107 struct got_object_id *new_commit_id, struct got_repository *repo)
7109 const struct got_error *err;
7110 struct got_reference *ref = NULL;
7111 struct got_object_id *old_commit_id = NULL;
7112 const char *branch_name = NULL;
7113 char *new_id_str = NULL;
7114 char *refname = NULL;
7116 branch_name = got_ref_get_name(branch);
7117 if (strncmp(branch_name, "refs/heads/", 11) != 0)
7118 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7119 branch_name += 11;
7121 err = got_object_id_str(&new_id_str, new_commit_id);
7122 if (err)
7123 return err;
7125 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7126 new_id_str) == -1) {
7127 err = got_error_from_errno("asprintf");
7128 goto done;
7131 err = got_ref_resolve(&old_commit_id, repo, branch);
7132 if (err)
7133 goto done;
7135 err = got_ref_alloc(&ref, refname, old_commit_id);
7136 if (err)
7137 goto done;
7139 err = got_ref_write(ref, repo);
7140 done:
7141 free(new_id_str);
7142 free(refname);
7143 free(old_commit_id);
7144 if (ref)
7145 got_ref_close(ref);
7146 return err;
7149 const struct got_error *
7150 got_worktree_rebase_complete(struct got_worktree *worktree,
7151 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7152 struct got_reference *rebased_branch, struct got_repository *repo,
7153 int create_backup)
7155 const struct got_error *err, *unlockerr, *sync_err;
7156 struct got_object_id *new_head_commit_id = NULL;
7157 char *fileindex_path = NULL;
7159 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7160 if (err)
7161 return err;
7163 if (create_backup) {
7164 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7165 rebased_branch, new_head_commit_id, repo);
7166 if (err)
7167 goto done;
7170 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7171 if (err)
7172 goto done;
7174 err = got_ref_write(rebased_branch, repo);
7175 if (err)
7176 goto done;
7178 err = got_worktree_set_head_ref(worktree, rebased_branch);
7179 if (err)
7180 goto done;
7182 err = delete_rebase_refs(worktree, repo);
7183 if (err)
7184 goto done;
7186 err = get_fileindex_path(&fileindex_path, worktree);
7187 if (err)
7188 goto done;
7189 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7190 sync_err = sync_fileindex(fileindex, fileindex_path);
7191 if (sync_err && err == NULL)
7192 err = sync_err;
7193 done:
7194 got_fileindex_free(fileindex);
7195 free(fileindex_path);
7196 free(new_head_commit_id);
7197 unlockerr = lock_worktree(worktree, LOCK_SH);
7198 if (unlockerr && err == NULL)
7199 err = unlockerr;
7200 return err;
7203 const struct got_error *
7204 got_worktree_rebase_abort(struct got_worktree *worktree,
7205 struct got_fileindex *fileindex, struct got_repository *repo,
7206 struct got_reference *new_base_branch,
7207 got_worktree_checkout_cb progress_cb, void *progress_arg)
7209 const struct got_error *err, *unlockerr, *sync_err;
7210 struct got_reference *resolved = NULL;
7211 struct got_object_id *commit_id = NULL;
7212 struct got_commit_object *commit = NULL;
7213 char *fileindex_path = NULL;
7214 struct revert_file_args rfa;
7215 struct got_object_id *tree_id = NULL;
7217 err = lock_worktree(worktree, LOCK_EX);
7218 if (err)
7219 return err;
7221 err = got_object_open_as_commit(&commit, repo,
7222 worktree->base_commit_id);
7223 if (err)
7224 goto done;
7226 err = got_ref_open(&resolved, repo,
7227 got_ref_get_symref_target(new_base_branch), 0);
7228 if (err)
7229 goto done;
7231 err = got_worktree_set_head_ref(worktree, resolved);
7232 if (err)
7233 goto done;
7236 * XXX commits to the base branch could have happened while
7237 * we were busy rebasing; should we store the original commit ID
7238 * when rebase begins and read it back here?
7240 err = got_ref_resolve(&commit_id, repo, resolved);
7241 if (err)
7242 goto done;
7244 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7245 if (err)
7246 goto done;
7248 err = got_object_id_by_path(&tree_id, repo, commit,
7249 worktree->path_prefix);
7250 if (err)
7251 goto done;
7253 err = delete_rebase_refs(worktree, repo);
7254 if (err)
7255 goto done;
7257 err = get_fileindex_path(&fileindex_path, worktree);
7258 if (err)
7259 goto done;
7261 rfa.worktree = worktree;
7262 rfa.fileindex = fileindex;
7263 rfa.progress_cb = progress_cb;
7264 rfa.progress_arg = progress_arg;
7265 rfa.patch_cb = NULL;
7266 rfa.patch_arg = NULL;
7267 rfa.repo = repo;
7268 rfa.unlink_added_files = 0;
7269 err = worktree_status(worktree, "", fileindex, repo,
7270 revert_file, &rfa, NULL, NULL, 1, 0);
7271 if (err)
7272 goto sync;
7274 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7275 repo, progress_cb, progress_arg, NULL, NULL);
7276 sync:
7277 sync_err = sync_fileindex(fileindex, fileindex_path);
7278 if (sync_err && err == NULL)
7279 err = sync_err;
7280 done:
7281 got_ref_close(resolved);
7282 free(tree_id);
7283 free(commit_id);
7284 if (commit)
7285 got_object_commit_close(commit);
7286 if (fileindex)
7287 got_fileindex_free(fileindex);
7288 free(fileindex_path);
7290 unlockerr = lock_worktree(worktree, LOCK_SH);
7291 if (unlockerr && err == NULL)
7292 err = unlockerr;
7293 return err;
7296 const struct got_error *
7297 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7298 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7299 struct got_fileindex **fileindex, struct got_worktree *worktree,
7300 struct got_repository *repo)
7302 const struct got_error *err = NULL;
7303 char *tmp_branch_name = NULL;
7304 char *branch_ref_name = NULL;
7305 char *base_commit_ref_name = NULL;
7306 char *fileindex_path = NULL;
7307 struct check_rebase_ok_arg ok_arg;
7308 struct got_reference *wt_branch = NULL;
7309 struct got_reference *base_commit_ref = NULL;
7311 *tmp_branch = NULL;
7312 *branch_ref = NULL;
7313 *base_commit_id = NULL;
7314 *fileindex = NULL;
7316 err = lock_worktree(worktree, LOCK_EX);
7317 if (err)
7318 return err;
7320 err = open_fileindex(fileindex, &fileindex_path, worktree);
7321 if (err)
7322 goto done;
7324 ok_arg.worktree = worktree;
7325 ok_arg.repo = repo;
7326 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7327 &ok_arg);
7328 if (err)
7329 goto done;
7331 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7332 if (err)
7333 goto done;
7335 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7336 if (err)
7337 goto done;
7339 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7340 worktree);
7341 if (err)
7342 goto done;
7344 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7345 0);
7346 if (err)
7347 goto done;
7349 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7350 if (err)
7351 goto done;
7353 err = got_ref_write(*branch_ref, repo);
7354 if (err)
7355 goto done;
7357 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7358 worktree->base_commit_id);
7359 if (err)
7360 goto done;
7361 err = got_ref_write(base_commit_ref, repo);
7362 if (err)
7363 goto done;
7364 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7365 if (*base_commit_id == NULL) {
7366 err = got_error_from_errno("got_object_id_dup");
7367 goto done;
7370 err = got_ref_alloc(tmp_branch, tmp_branch_name,
7371 worktree->base_commit_id);
7372 if (err)
7373 goto done;
7374 err = got_ref_write(*tmp_branch, repo);
7375 if (err)
7376 goto done;
7378 err = got_worktree_set_head_ref(worktree, *tmp_branch);
7379 if (err)
7380 goto done;
7381 done:
7382 free(fileindex_path);
7383 free(tmp_branch_name);
7384 free(branch_ref_name);
7385 free(base_commit_ref_name);
7386 if (wt_branch)
7387 got_ref_close(wt_branch);
7388 if (err) {
7389 if (*branch_ref) {
7390 got_ref_close(*branch_ref);
7391 *branch_ref = NULL;
7393 if (*tmp_branch) {
7394 got_ref_close(*tmp_branch);
7395 *tmp_branch = NULL;
7397 free(*base_commit_id);
7398 if (*fileindex) {
7399 got_fileindex_free(*fileindex);
7400 *fileindex = NULL;
7402 lock_worktree(worktree, LOCK_SH);
7404 return err;
7407 const struct got_error *
7408 got_worktree_histedit_postpone(struct got_worktree *worktree,
7409 struct got_fileindex *fileindex)
7411 if (fileindex)
7412 got_fileindex_free(fileindex);
7413 return lock_worktree(worktree, LOCK_SH);
7416 const struct got_error *
7417 got_worktree_histedit_in_progress(int *in_progress,
7418 struct got_worktree *worktree)
7420 const struct got_error *err;
7421 char *tmp_branch_name = NULL;
7423 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7424 if (err)
7425 return err;
7427 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7428 free(tmp_branch_name);
7429 return NULL;
7432 const struct got_error *
7433 got_worktree_histedit_continue(struct got_object_id **commit_id,
7434 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7435 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7436 struct got_worktree *worktree, struct got_repository *repo)
7438 const struct got_error *err;
7439 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7440 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7441 struct got_reference *commit_ref = NULL;
7442 struct got_reference *base_commit_ref = NULL;
7443 char *fileindex_path = NULL;
7444 int have_staged_files = 0;
7446 *commit_id = NULL;
7447 *tmp_branch = NULL;
7448 *base_commit_id = NULL;
7449 *fileindex = NULL;
7451 err = lock_worktree(worktree, LOCK_EX);
7452 if (err)
7453 return err;
7455 err = open_fileindex(fileindex, &fileindex_path, worktree);
7456 if (err)
7457 goto done;
7459 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7460 &have_staged_files);
7461 if (err && err->code != GOT_ERR_CANCELLED)
7462 goto done;
7463 if (have_staged_files) {
7464 err = got_error(GOT_ERR_STAGED_PATHS);
7465 goto done;
7468 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7469 if (err)
7470 goto done;
7472 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7473 if (err)
7474 goto done;
7476 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7477 if (err)
7478 goto done;
7480 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7481 worktree);
7482 if (err)
7483 goto done;
7485 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7486 if (err)
7487 goto done;
7489 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7490 if (err)
7491 goto done;
7492 err = got_ref_resolve(commit_id, repo, commit_ref);
7493 if (err)
7494 goto done;
7496 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7497 if (err)
7498 goto done;
7499 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7500 if (err)
7501 goto done;
7503 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7504 if (err)
7505 goto done;
7506 done:
7507 free(commit_ref_name);
7508 free(branch_ref_name);
7509 free(fileindex_path);
7510 if (commit_ref)
7511 got_ref_close(commit_ref);
7512 if (base_commit_ref)
7513 got_ref_close(base_commit_ref);
7514 if (err) {
7515 free(*commit_id);
7516 *commit_id = NULL;
7517 free(*base_commit_id);
7518 *base_commit_id = NULL;
7519 if (*tmp_branch) {
7520 got_ref_close(*tmp_branch);
7521 *tmp_branch = NULL;
7523 if (*fileindex) {
7524 got_fileindex_free(*fileindex);
7525 *fileindex = NULL;
7527 lock_worktree(worktree, LOCK_EX);
7529 return err;
7532 static const struct got_error *
7533 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7535 const struct got_error *err;
7536 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7537 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7539 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7540 if (err)
7541 goto done;
7542 err = delete_ref(tmp_branch_name, repo);
7543 if (err)
7544 goto done;
7546 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7547 worktree);
7548 if (err)
7549 goto done;
7550 err = delete_ref(base_commit_ref_name, repo);
7551 if (err)
7552 goto done;
7554 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7555 if (err)
7556 goto done;
7557 err = delete_ref(branch_ref_name, repo);
7558 if (err)
7559 goto done;
7561 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7562 if (err)
7563 goto done;
7564 err = delete_ref(commit_ref_name, repo);
7565 if (err)
7566 goto done;
7567 done:
7568 free(tmp_branch_name);
7569 free(base_commit_ref_name);
7570 free(branch_ref_name);
7571 free(commit_ref_name);
7572 return err;
7575 const struct got_error *
7576 got_worktree_histedit_abort(struct got_worktree *worktree,
7577 struct got_fileindex *fileindex, struct got_repository *repo,
7578 struct got_reference *branch, struct got_object_id *base_commit_id,
7579 got_worktree_checkout_cb progress_cb, void *progress_arg)
7581 const struct got_error *err, *unlockerr, *sync_err;
7582 struct got_reference *resolved = NULL;
7583 char *fileindex_path = NULL;
7584 struct got_commit_object *commit = NULL;
7585 struct got_object_id *tree_id = NULL;
7586 struct revert_file_args rfa;
7588 err = lock_worktree(worktree, LOCK_EX);
7589 if (err)
7590 return err;
7592 err = got_object_open_as_commit(&commit, repo,
7593 worktree->base_commit_id);
7594 if (err)
7595 goto done;
7597 err = got_ref_open(&resolved, repo,
7598 got_ref_get_symref_target(branch), 0);
7599 if (err)
7600 goto done;
7602 err = got_worktree_set_head_ref(worktree, resolved);
7603 if (err)
7604 goto done;
7606 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7607 if (err)
7608 goto done;
7610 err = got_object_id_by_path(&tree_id, repo, commit,
7611 worktree->path_prefix);
7612 if (err)
7613 goto done;
7615 err = delete_histedit_refs(worktree, repo);
7616 if (err)
7617 goto done;
7619 err = get_fileindex_path(&fileindex_path, worktree);
7620 if (err)
7621 goto done;
7623 rfa.worktree = worktree;
7624 rfa.fileindex = fileindex;
7625 rfa.progress_cb = progress_cb;
7626 rfa.progress_arg = progress_arg;
7627 rfa.patch_cb = NULL;
7628 rfa.patch_arg = NULL;
7629 rfa.repo = repo;
7630 rfa.unlink_added_files = 0;
7631 err = worktree_status(worktree, "", fileindex, repo,
7632 revert_file, &rfa, NULL, NULL, 1, 0);
7633 if (err)
7634 goto sync;
7636 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7637 repo, progress_cb, progress_arg, NULL, NULL);
7638 sync:
7639 sync_err = sync_fileindex(fileindex, fileindex_path);
7640 if (sync_err && err == NULL)
7641 err = sync_err;
7642 done:
7643 got_ref_close(resolved);
7644 free(tree_id);
7645 free(fileindex_path);
7647 unlockerr = lock_worktree(worktree, LOCK_SH);
7648 if (unlockerr && err == NULL)
7649 err = unlockerr;
7650 return err;
7653 const struct got_error *
7654 got_worktree_histedit_complete(struct got_worktree *worktree,
7655 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7656 struct got_reference *edited_branch, struct got_repository *repo)
7658 const struct got_error *err, *unlockerr, *sync_err;
7659 struct got_object_id *new_head_commit_id = NULL;
7660 struct got_reference *resolved = NULL;
7661 char *fileindex_path = NULL;
7663 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7664 if (err)
7665 return err;
7667 err = got_ref_open(&resolved, repo,
7668 got_ref_get_symref_target(edited_branch), 0);
7669 if (err)
7670 goto done;
7672 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7673 resolved, new_head_commit_id, repo);
7674 if (err)
7675 goto done;
7677 err = got_ref_change_ref(resolved, new_head_commit_id);
7678 if (err)
7679 goto done;
7681 err = got_ref_write(resolved, repo);
7682 if (err)
7683 goto done;
7685 err = got_worktree_set_head_ref(worktree, resolved);
7686 if (err)
7687 goto done;
7689 err = delete_histedit_refs(worktree, repo);
7690 if (err)
7691 goto done;
7693 err = get_fileindex_path(&fileindex_path, worktree);
7694 if (err)
7695 goto done;
7696 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7697 sync_err = sync_fileindex(fileindex, fileindex_path);
7698 if (sync_err && err == NULL)
7699 err = sync_err;
7700 done:
7701 got_fileindex_free(fileindex);
7702 free(fileindex_path);
7703 free(new_head_commit_id);
7704 unlockerr = lock_worktree(worktree, LOCK_SH);
7705 if (unlockerr && err == NULL)
7706 err = unlockerr;
7707 return err;
7710 const struct got_error *
7711 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7712 struct got_object_id *commit_id, struct got_repository *repo)
7714 const struct got_error *err;
7715 char *commit_ref_name;
7717 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7718 if (err)
7719 return err;
7721 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7722 if (err)
7723 goto done;
7725 err = delete_ref(commit_ref_name, repo);
7726 done:
7727 free(commit_ref_name);
7728 return err;
7731 const struct got_error *
7732 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7733 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7734 struct got_worktree *worktree, const char *refname,
7735 struct got_repository *repo)
7737 const struct got_error *err = NULL;
7738 char *fileindex_path = NULL;
7739 struct check_rebase_ok_arg ok_arg;
7741 *fileindex = NULL;
7742 *branch_ref = NULL;
7743 *base_branch_ref = NULL;
7745 err = lock_worktree(worktree, LOCK_EX);
7746 if (err)
7747 return err;
7749 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7750 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7751 "cannot integrate a branch into itself; "
7752 "update -b or different branch name required");
7753 goto done;
7756 err = open_fileindex(fileindex, &fileindex_path, worktree);
7757 if (err)
7758 goto done;
7760 /* Preconditions are the same as for rebase. */
7761 ok_arg.worktree = worktree;
7762 ok_arg.repo = repo;
7763 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7764 &ok_arg);
7765 if (err)
7766 goto done;
7768 err = got_ref_open(branch_ref, repo, refname, 1);
7769 if (err)
7770 goto done;
7772 err = got_ref_open(base_branch_ref, repo,
7773 got_worktree_get_head_ref_name(worktree), 1);
7774 done:
7775 if (err) {
7776 if (*branch_ref) {
7777 got_ref_close(*branch_ref);
7778 *branch_ref = NULL;
7780 if (*base_branch_ref) {
7781 got_ref_close(*base_branch_ref);
7782 *base_branch_ref = NULL;
7784 if (*fileindex) {
7785 got_fileindex_free(*fileindex);
7786 *fileindex = NULL;
7788 lock_worktree(worktree, LOCK_SH);
7790 return err;
7793 const struct got_error *
7794 got_worktree_integrate_continue(struct got_worktree *worktree,
7795 struct got_fileindex *fileindex, struct got_repository *repo,
7796 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7797 got_worktree_checkout_cb progress_cb, void *progress_arg,
7798 got_cancel_cb cancel_cb, void *cancel_arg)
7800 const struct got_error *err = NULL, *sync_err, *unlockerr;
7801 char *fileindex_path = NULL;
7802 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7803 struct got_commit_object *commit = NULL;
7805 err = get_fileindex_path(&fileindex_path, worktree);
7806 if (err)
7807 goto done;
7809 err = got_ref_resolve(&commit_id, repo, branch_ref);
7810 if (err)
7811 goto done;
7813 err = got_object_open_as_commit(&commit, repo, commit_id);
7814 if (err)
7815 goto done;
7817 err = got_object_id_by_path(&tree_id, repo, commit,
7818 worktree->path_prefix);
7819 if (err)
7820 goto done;
7822 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7823 if (err)
7824 goto done;
7826 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7827 progress_cb, progress_arg, cancel_cb, cancel_arg);
7828 if (err)
7829 goto sync;
7831 err = got_ref_change_ref(base_branch_ref, commit_id);
7832 if (err)
7833 goto sync;
7835 err = got_ref_write(base_branch_ref, repo);
7836 if (err)
7837 goto sync;
7839 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7840 sync:
7841 sync_err = sync_fileindex(fileindex, fileindex_path);
7842 if (sync_err && err == NULL)
7843 err = sync_err;
7845 done:
7846 unlockerr = got_ref_unlock(branch_ref);
7847 if (unlockerr && err == NULL)
7848 err = unlockerr;
7849 got_ref_close(branch_ref);
7851 unlockerr = got_ref_unlock(base_branch_ref);
7852 if (unlockerr && err == NULL)
7853 err = unlockerr;
7854 got_ref_close(base_branch_ref);
7856 got_fileindex_free(fileindex);
7857 free(fileindex_path);
7858 free(tree_id);
7859 if (commit)
7860 got_object_commit_close(commit);
7862 unlockerr = lock_worktree(worktree, LOCK_SH);
7863 if (unlockerr && err == NULL)
7864 err = unlockerr;
7865 return err;
7868 const struct got_error *
7869 got_worktree_integrate_abort(struct got_worktree *worktree,
7870 struct got_fileindex *fileindex, struct got_repository *repo,
7871 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7873 const struct got_error *err = NULL, *unlockerr = NULL;
7875 got_fileindex_free(fileindex);
7877 err = lock_worktree(worktree, LOCK_SH);
7879 unlockerr = got_ref_unlock(branch_ref);
7880 if (unlockerr && err == NULL)
7881 err = unlockerr;
7882 got_ref_close(branch_ref);
7884 unlockerr = got_ref_unlock(base_branch_ref);
7885 if (unlockerr && err == NULL)
7886 err = unlockerr;
7887 got_ref_close(base_branch_ref);
7889 return err;
7892 const struct got_error *
7893 got_worktree_merge_postpone(struct got_worktree *worktree,
7894 struct got_fileindex *fileindex)
7896 const struct got_error *err, *sync_err;
7897 char *fileindex_path = NULL;
7899 err = get_fileindex_path(&fileindex_path, worktree);
7900 if (err)
7901 goto done;
7903 sync_err = sync_fileindex(fileindex, fileindex_path);
7905 err = lock_worktree(worktree, LOCK_SH);
7906 if (sync_err && err == NULL)
7907 err = sync_err;
7908 done:
7909 got_fileindex_free(fileindex);
7910 free(fileindex_path);
7911 return err;
7914 static const struct got_error *
7915 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7917 const struct got_error *err;
7918 char *branch_refname = NULL, *commit_refname = NULL;
7920 err = get_merge_branch_ref_name(&branch_refname, worktree);
7921 if (err)
7922 goto done;
7923 err = delete_ref(branch_refname, repo);
7924 if (err)
7925 goto done;
7927 err = get_merge_commit_ref_name(&commit_refname, worktree);
7928 if (err)
7929 goto done;
7930 err = delete_ref(commit_refname, repo);
7931 if (err)
7932 goto done;
7934 done:
7935 free(branch_refname);
7936 free(commit_refname);
7937 return err;
7940 struct merge_commit_msg_arg {
7941 struct got_worktree *worktree;
7942 const char *branch_name;
7945 static const struct got_error *
7946 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
7947 const char *diff_path, char **logmsg, void *arg)
7949 struct merge_commit_msg_arg *a = arg;
7951 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7952 got_worktree_get_head_ref_name(a->worktree)) == -1)
7953 return got_error_from_errno("asprintf");
7955 return NULL;
7959 const struct got_error *
7960 got_worktree_merge_branch(struct got_worktree *worktree,
7961 struct got_fileindex *fileindex,
7962 struct got_object_id *yca_commit_id,
7963 struct got_object_id *branch_tip,
7964 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7965 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7967 const struct got_error *err;
7968 char *fileindex_path = NULL;
7970 err = get_fileindex_path(&fileindex_path, worktree);
7971 if (err)
7972 goto done;
7974 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7975 worktree);
7976 if (err)
7977 goto done;
7979 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7980 branch_tip, repo, progress_cb, progress_arg,
7981 cancel_cb, cancel_arg);
7982 done:
7983 free(fileindex_path);
7984 return err;
7987 const struct got_error *
7988 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7989 struct got_worktree *worktree, struct got_fileindex *fileindex,
7990 const char *author, const char *committer, int allow_bad_symlinks,
7991 struct got_object_id *branch_tip, const char *branch_name,
7992 int allow_conflict, struct got_repository *repo,
7993 got_worktree_status_cb status_cb, void *status_arg)
7996 const struct got_error *err = NULL, *sync_err;
7997 struct got_pathlist_head commitable_paths;
7998 struct collect_commitables_arg cc_arg;
7999 struct got_pathlist_entry *pe;
8000 struct got_reference *head_ref = NULL;
8001 struct got_object_id *head_commit_id = NULL;
8002 int have_staged_files = 0;
8003 struct merge_commit_msg_arg mcm_arg;
8004 char *fileindex_path = NULL;
8006 memset(&cc_arg, 0, sizeof(cc_arg));
8007 *new_commit_id = NULL;
8009 TAILQ_INIT(&commitable_paths);
8011 err = get_fileindex_path(&fileindex_path, worktree);
8012 if (err)
8013 goto done;
8015 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
8016 if (err)
8017 goto done;
8019 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8020 if (err)
8021 goto done;
8023 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
8024 &have_staged_files);
8025 if (err && err->code != GOT_ERR_CANCELLED)
8026 goto done;
8027 if (have_staged_files) {
8028 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
8029 goto done;
8032 cc_arg.commitable_paths = &commitable_paths;
8033 cc_arg.worktree = worktree;
8034 cc_arg.fileindex = fileindex;
8035 cc_arg.repo = repo;
8036 cc_arg.have_staged_files = have_staged_files;
8037 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
8038 cc_arg.commit_conflicts = allow_conflict;
8039 err = worktree_status(worktree, "", fileindex, repo,
8040 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8041 if (err)
8042 goto done;
8044 if (TAILQ_EMPTY(&commitable_paths)) {
8045 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
8046 "merge of %s cannot proceed", branch_name);
8047 goto done;
8050 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8051 struct got_commitable *ct = pe->data;
8052 const char *ct_path = ct->in_repo_path;
8054 while (ct_path[0] == '/')
8055 ct_path++;
8056 err = check_out_of_date(ct_path, ct->status,
8057 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
8058 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
8059 if (err)
8060 goto done;
8064 mcm_arg.worktree = worktree;
8065 mcm_arg.branch_name = branch_name;
8066 err = commit_worktree(new_commit_id, &commitable_paths,
8067 head_commit_id, branch_tip, worktree, author, committer, NULL,
8068 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8069 if (err)
8070 goto done;
8072 err = update_fileindex_after_commit(worktree, &commitable_paths,
8073 *new_commit_id, fileindex, have_staged_files);
8074 sync_err = sync_fileindex(fileindex, fileindex_path);
8075 if (sync_err && err == NULL)
8076 err = sync_err;
8077 done:
8078 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8079 struct got_commitable *ct = pe->data;
8081 free_commitable(ct);
8083 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8084 free(fileindex_path);
8085 return err;
8088 const struct got_error *
8089 got_worktree_merge_complete(struct got_worktree *worktree,
8090 struct got_fileindex *fileindex, struct got_repository *repo)
8092 const struct got_error *err, *unlockerr, *sync_err;
8093 char *fileindex_path = NULL;
8095 err = delete_merge_refs(worktree, repo);
8096 if (err)
8097 goto done;
8099 err = get_fileindex_path(&fileindex_path, worktree);
8100 if (err)
8101 goto done;
8102 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8103 sync_err = sync_fileindex(fileindex, fileindex_path);
8104 if (sync_err && err == NULL)
8105 err = sync_err;
8106 done:
8107 got_fileindex_free(fileindex);
8108 free(fileindex_path);
8109 unlockerr = lock_worktree(worktree, LOCK_SH);
8110 if (unlockerr && err == NULL)
8111 err = unlockerr;
8112 return err;
8115 const struct got_error *
8116 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8117 struct got_repository *repo)
8119 const struct got_error *err;
8120 char *branch_refname = NULL;
8121 struct got_reference *branch_ref = NULL;
8123 *in_progress = 0;
8125 err = get_merge_branch_ref_name(&branch_refname, worktree);
8126 if (err)
8127 return err;
8128 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8129 free(branch_refname);
8130 if (err) {
8131 if (err->code != GOT_ERR_NOT_REF)
8132 return err;
8133 } else
8134 *in_progress = 1;
8136 return NULL;
8139 const struct got_error *got_worktree_merge_prepare(
8140 struct got_fileindex **fileindex, struct got_worktree *worktree,
8141 struct got_reference *branch, struct got_repository *repo)
8143 const struct got_error *err = NULL;
8144 char *fileindex_path = NULL;
8145 char *branch_refname = NULL, *commit_refname = NULL;
8146 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
8147 struct got_reference *commit_ref = NULL;
8148 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
8149 struct check_rebase_ok_arg ok_arg;
8151 *fileindex = NULL;
8153 err = lock_worktree(worktree, LOCK_EX);
8154 if (err)
8155 return err;
8157 err = open_fileindex(fileindex, &fileindex_path, worktree);
8158 if (err)
8159 goto done;
8161 /* Preconditions are the same as for rebase. */
8162 ok_arg.worktree = worktree;
8163 ok_arg.repo = repo;
8164 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8165 &ok_arg);
8166 if (err)
8167 goto done;
8169 err = get_merge_branch_ref_name(&branch_refname, worktree);
8170 if (err)
8171 return err;
8173 err = get_merge_commit_ref_name(&commit_refname, worktree);
8174 if (err)
8175 return err;
8177 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8178 0);
8179 if (err)
8180 goto done;
8182 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8183 if (err)
8184 goto done;
8186 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8187 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8188 goto done;
8191 err = got_ref_resolve(&branch_tip, repo, branch);
8192 if (err)
8193 goto done;
8195 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8196 if (err)
8197 goto done;
8198 err = got_ref_write(branch_ref, repo);
8199 if (err)
8200 goto done;
8202 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8203 if (err)
8204 goto done;
8205 err = got_ref_write(commit_ref, repo);
8206 if (err)
8207 goto done;
8209 done:
8210 free(branch_refname);
8211 free(commit_refname);
8212 free(fileindex_path);
8213 if (branch_ref)
8214 got_ref_close(branch_ref);
8215 if (commit_ref)
8216 got_ref_close(commit_ref);
8217 if (wt_branch)
8218 got_ref_close(wt_branch);
8219 free(wt_branch_tip);
8220 if (err) {
8221 if (*fileindex) {
8222 got_fileindex_free(*fileindex);
8223 *fileindex = NULL;
8225 lock_worktree(worktree, LOCK_SH);
8227 return err;
8230 const struct got_error *
8231 got_worktree_merge_continue(char **branch_name,
8232 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8233 struct got_worktree *worktree, struct got_repository *repo)
8235 const struct got_error *err;
8236 char *commit_refname = NULL, *branch_refname = NULL;
8237 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8238 char *fileindex_path = NULL;
8239 int have_staged_files = 0;
8241 *branch_name = NULL;
8242 *branch_tip = NULL;
8243 *fileindex = NULL;
8245 err = lock_worktree(worktree, LOCK_EX);
8246 if (err)
8247 return err;
8249 err = open_fileindex(fileindex, &fileindex_path, worktree);
8250 if (err)
8251 goto done;
8253 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8254 &have_staged_files);
8255 if (err && err->code != GOT_ERR_CANCELLED)
8256 goto done;
8257 if (have_staged_files) {
8258 err = got_error(GOT_ERR_STAGED_PATHS);
8259 goto done;
8262 err = get_merge_branch_ref_name(&branch_refname, worktree);
8263 if (err)
8264 goto done;
8266 err = get_merge_commit_ref_name(&commit_refname, worktree);
8267 if (err)
8268 goto done;
8270 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8271 if (err)
8272 goto done;
8274 if (!got_ref_is_symbolic(branch_ref)) {
8275 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8276 "%s is not a symbolic reference",
8277 got_ref_get_name(branch_ref));
8278 goto done;
8280 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8281 if (*branch_name == NULL) {
8282 err = got_error_from_errno("strdup");
8283 goto done;
8286 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8287 if (err)
8288 goto done;
8290 err = got_ref_resolve(branch_tip, repo, commit_ref);
8291 if (err)
8292 goto done;
8293 done:
8294 free(commit_refname);
8295 free(branch_refname);
8296 free(fileindex_path);
8297 if (commit_ref)
8298 got_ref_close(commit_ref);
8299 if (branch_ref)
8300 got_ref_close(branch_ref);
8301 if (err) {
8302 if (*branch_name) {
8303 free(*branch_name);
8304 *branch_name = NULL;
8306 free(*branch_tip);
8307 *branch_tip = NULL;
8308 if (*fileindex) {
8309 got_fileindex_free(*fileindex);
8310 *fileindex = NULL;
8312 lock_worktree(worktree, LOCK_SH);
8314 return err;
8317 const struct got_error *
8318 got_worktree_merge_abort(struct got_worktree *worktree,
8319 struct got_fileindex *fileindex, struct got_repository *repo,
8320 got_worktree_checkout_cb progress_cb, void *progress_arg)
8322 const struct got_error *err, *unlockerr, *sync_err;
8323 struct got_object_id *commit_id = NULL;
8324 struct got_commit_object *commit = NULL;
8325 char *fileindex_path = NULL;
8326 struct revert_file_args rfa;
8327 struct got_object_id *tree_id = NULL;
8329 err = got_object_open_as_commit(&commit, repo,
8330 worktree->base_commit_id);
8331 if (err)
8332 goto done;
8334 err = got_object_id_by_path(&tree_id, repo, commit,
8335 worktree->path_prefix);
8336 if (err)
8337 goto done;
8339 err = delete_merge_refs(worktree, repo);
8340 if (err)
8341 goto done;
8343 err = get_fileindex_path(&fileindex_path, worktree);
8344 if (err)
8345 goto done;
8347 rfa.worktree = worktree;
8348 rfa.fileindex = fileindex;
8349 rfa.progress_cb = progress_cb;
8350 rfa.progress_arg = progress_arg;
8351 rfa.patch_cb = NULL;
8352 rfa.patch_arg = NULL;
8353 rfa.repo = repo;
8354 rfa.unlink_added_files = 1;
8355 err = worktree_status(worktree, "", fileindex, repo,
8356 revert_file, &rfa, NULL, NULL, 1, 0);
8357 if (err)
8358 goto sync;
8360 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8361 repo, progress_cb, progress_arg, NULL, NULL);
8362 sync:
8363 sync_err = sync_fileindex(fileindex, fileindex_path);
8364 if (sync_err && err == NULL)
8365 err = sync_err;
8366 done:
8367 free(tree_id);
8368 free(commit_id);
8369 if (commit)
8370 got_object_commit_close(commit);
8371 if (fileindex)
8372 got_fileindex_free(fileindex);
8373 free(fileindex_path);
8375 unlockerr = lock_worktree(worktree, LOCK_SH);
8376 if (unlockerr && err == NULL)
8377 err = unlockerr;
8378 return err;
8381 struct check_stage_ok_arg {
8382 struct got_object_id *head_commit_id;
8383 struct got_worktree *worktree;
8384 struct got_fileindex *fileindex;
8385 struct got_repository *repo;
8386 int have_changes;
8389 static const struct got_error *
8390 check_stage_ok(void *arg, unsigned char status,
8391 unsigned char staged_status, const char *relpath,
8392 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8393 struct got_object_id *commit_id, int dirfd, const char *de_name)
8395 struct check_stage_ok_arg *a = arg;
8396 const struct got_error *err = NULL;
8397 struct got_fileindex_entry *ie;
8398 struct got_object_id base_commit_id;
8399 struct got_object_id *base_commit_idp = NULL;
8400 char *in_repo_path = NULL, *p;
8402 if (status == GOT_STATUS_UNVERSIONED ||
8403 status == GOT_STATUS_NO_CHANGE)
8404 return NULL;
8405 if (status == GOT_STATUS_NONEXISTENT)
8406 return got_error_set_errno(ENOENT, relpath);
8408 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8409 if (ie == NULL)
8410 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8412 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8413 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8414 relpath) == -1)
8415 return got_error_from_errno("asprintf");
8417 if (got_fileindex_entry_has_commit(ie)) {
8418 base_commit_idp = got_fileindex_entry_get_commit_id(
8419 &base_commit_id, ie);
8422 if (status == GOT_STATUS_CONFLICT) {
8423 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8424 goto done;
8425 } else if (status != GOT_STATUS_ADD &&
8426 status != GOT_STATUS_MODIFY &&
8427 status != GOT_STATUS_DELETE) {
8428 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8429 goto done;
8432 a->have_changes = 1;
8434 p = in_repo_path;
8435 while (p[0] == '/')
8436 p++;
8437 err = check_out_of_date(p, status, staged_status,
8438 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8439 GOT_ERR_STAGE_OUT_OF_DATE);
8440 done:
8441 free(in_repo_path);
8442 return err;
8445 struct stage_path_arg {
8446 struct got_worktree *worktree;
8447 struct got_fileindex *fileindex;
8448 struct got_repository *repo;
8449 got_worktree_status_cb status_cb;
8450 void *status_arg;
8451 got_worktree_patch_cb patch_cb;
8452 void *patch_arg;
8453 int staged_something;
8454 int allow_bad_symlinks;
8457 static const struct got_error *
8458 stage_path(void *arg, unsigned char status,
8459 unsigned char staged_status, const char *relpath,
8460 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8461 struct got_object_id *commit_id, int dirfd, const char *de_name)
8463 struct stage_path_arg *a = arg;
8464 const struct got_error *err = NULL;
8465 struct got_fileindex_entry *ie;
8466 char *ondisk_path = NULL, *path_content = NULL;
8467 uint32_t stage;
8468 struct got_object_id *new_staged_blob_id = NULL;
8469 struct stat sb;
8471 if (status == GOT_STATUS_UNVERSIONED)
8472 return NULL;
8474 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8475 if (ie == NULL)
8476 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8478 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8479 relpath)== -1)
8480 return got_error_from_errno("asprintf");
8482 switch (status) {
8483 case GOT_STATUS_ADD:
8484 case GOT_STATUS_MODIFY:
8485 /* XXX could sb.st_mode be passed in by our caller? */
8486 if (lstat(ondisk_path, &sb) == -1) {
8487 err = got_error_from_errno2("lstat", ondisk_path);
8488 break;
8490 if (a->patch_cb) {
8491 if (status == GOT_STATUS_ADD) {
8492 int choice = GOT_PATCH_CHOICE_NONE;
8493 err = (*a->patch_cb)(&choice, a->patch_arg,
8494 status, ie->path, NULL, 1, 1);
8495 if (err)
8496 break;
8497 if (choice != GOT_PATCH_CHOICE_YES)
8498 break;
8499 } else {
8500 err = create_patched_content(&path_content, 0,
8501 staged_blob_id ? staged_blob_id : blob_id,
8502 ondisk_path, dirfd, de_name, ie->path,
8503 a->repo, a->patch_cb, a->patch_arg);
8504 if (err || path_content == NULL)
8505 break;
8508 err = got_object_blob_create(&new_staged_blob_id,
8509 path_content ? path_content : ondisk_path, a->repo);
8510 if (err)
8511 break;
8512 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8513 SHA1_DIGEST_LENGTH);
8514 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8515 stage = GOT_FILEIDX_STAGE_ADD;
8516 else
8517 stage = GOT_FILEIDX_STAGE_MODIFY;
8518 got_fileindex_entry_stage_set(ie, stage);
8519 if (S_ISLNK(sb.st_mode)) {
8520 int is_bad_symlink = 0;
8521 if (!a->allow_bad_symlinks) {
8522 char target_path[PATH_MAX];
8523 ssize_t target_len;
8524 target_len = readlink(ondisk_path, target_path,
8525 sizeof(target_path));
8526 if (target_len == -1) {
8527 err = got_error_from_errno2("readlink",
8528 ondisk_path);
8529 break;
8531 err = is_bad_symlink_target(&is_bad_symlink,
8532 target_path, target_len, ondisk_path,
8533 a->worktree->root_path);
8534 if (err)
8535 break;
8536 if (is_bad_symlink) {
8537 err = got_error_path(ondisk_path,
8538 GOT_ERR_BAD_SYMLINK);
8539 break;
8542 if (is_bad_symlink)
8543 got_fileindex_entry_staged_filetype_set(ie,
8544 GOT_FILEIDX_MODE_BAD_SYMLINK);
8545 else
8546 got_fileindex_entry_staged_filetype_set(ie,
8547 GOT_FILEIDX_MODE_SYMLINK);
8548 } else {
8549 got_fileindex_entry_staged_filetype_set(ie,
8550 GOT_FILEIDX_MODE_REGULAR_FILE);
8552 a->staged_something = 1;
8553 if (a->status_cb == NULL)
8554 break;
8555 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8556 get_staged_status(ie), relpath, blob_id,
8557 new_staged_blob_id, NULL, dirfd, de_name);
8558 if (err)
8559 break;
8561 * When staging the reverse of the staged diff,
8562 * implicitly unstage the file.
8564 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8565 sizeof(ie->blob_sha1)) == 0) {
8566 got_fileindex_entry_stage_set(ie,
8567 GOT_FILEIDX_STAGE_NONE);
8569 break;
8570 case GOT_STATUS_DELETE:
8571 if (staged_status == GOT_STATUS_DELETE)
8572 break;
8573 if (a->patch_cb) {
8574 int choice = GOT_PATCH_CHOICE_NONE;
8575 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8576 ie->path, NULL, 1, 1);
8577 if (err)
8578 break;
8579 if (choice == GOT_PATCH_CHOICE_NO)
8580 break;
8581 if (choice != GOT_PATCH_CHOICE_YES) {
8582 err = got_error(GOT_ERR_PATCH_CHOICE);
8583 break;
8586 stage = GOT_FILEIDX_STAGE_DELETE;
8587 got_fileindex_entry_stage_set(ie, stage);
8588 a->staged_something = 1;
8589 if (a->status_cb == NULL)
8590 break;
8591 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8592 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8593 de_name);
8594 break;
8595 case GOT_STATUS_NO_CHANGE:
8596 break;
8597 case GOT_STATUS_CONFLICT:
8598 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8599 break;
8600 case GOT_STATUS_NONEXISTENT:
8601 err = got_error_set_errno(ENOENT, relpath);
8602 break;
8603 default:
8604 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8605 break;
8608 if (path_content && unlink(path_content) == -1 && err == NULL)
8609 err = got_error_from_errno2("unlink", path_content);
8610 free(path_content);
8611 free(ondisk_path);
8612 free(new_staged_blob_id);
8613 return err;
8616 const struct got_error *
8617 got_worktree_stage(struct got_worktree *worktree,
8618 struct got_pathlist_head *paths,
8619 got_worktree_status_cb status_cb, void *status_arg,
8620 got_worktree_patch_cb patch_cb, void *patch_arg,
8621 int allow_bad_symlinks, struct got_repository *repo)
8623 const struct got_error *err = NULL, *sync_err, *unlockerr;
8624 struct got_pathlist_entry *pe;
8625 struct got_fileindex *fileindex = NULL;
8626 char *fileindex_path = NULL;
8627 struct got_reference *head_ref = NULL;
8628 struct got_object_id *head_commit_id = NULL;
8629 struct check_stage_ok_arg oka;
8630 struct stage_path_arg spa;
8632 err = lock_worktree(worktree, LOCK_EX);
8633 if (err)
8634 return err;
8636 err = got_ref_open(&head_ref, repo,
8637 got_worktree_get_head_ref_name(worktree), 0);
8638 if (err)
8639 goto done;
8640 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8641 if (err)
8642 goto done;
8643 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8644 if (err)
8645 goto done;
8647 /* Check pre-conditions before staging anything. */
8648 oka.head_commit_id = head_commit_id;
8649 oka.worktree = worktree;
8650 oka.fileindex = fileindex;
8651 oka.repo = repo;
8652 oka.have_changes = 0;
8653 TAILQ_FOREACH(pe, paths, entry) {
8654 err = worktree_status(worktree, pe->path, fileindex, repo,
8655 check_stage_ok, &oka, NULL, NULL, 1, 0);
8656 if (err)
8657 goto done;
8659 if (!oka.have_changes) {
8660 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8661 goto done;
8664 spa.worktree = worktree;
8665 spa.fileindex = fileindex;
8666 spa.repo = repo;
8667 spa.patch_cb = patch_cb;
8668 spa.patch_arg = patch_arg;
8669 spa.status_cb = status_cb;
8670 spa.status_arg = status_arg;
8671 spa.staged_something = 0;
8672 spa.allow_bad_symlinks = allow_bad_symlinks;
8673 TAILQ_FOREACH(pe, paths, entry) {
8674 err = worktree_status(worktree, pe->path, fileindex, repo,
8675 stage_path, &spa, NULL, NULL, 1, 0);
8676 if (err)
8677 goto done;
8679 if (!spa.staged_something) {
8680 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8681 goto done;
8684 sync_err = sync_fileindex(fileindex, fileindex_path);
8685 if (sync_err && err == NULL)
8686 err = sync_err;
8687 done:
8688 if (head_ref)
8689 got_ref_close(head_ref);
8690 free(head_commit_id);
8691 free(fileindex_path);
8692 if (fileindex)
8693 got_fileindex_free(fileindex);
8694 unlockerr = lock_worktree(worktree, LOCK_SH);
8695 if (unlockerr && err == NULL)
8696 err = unlockerr;
8697 return err;
8700 struct unstage_path_arg {
8701 struct got_worktree *worktree;
8702 struct got_fileindex *fileindex;
8703 struct got_repository *repo;
8704 got_worktree_checkout_cb progress_cb;
8705 void *progress_arg;
8706 got_worktree_patch_cb patch_cb;
8707 void *patch_arg;
8710 static const struct got_error *
8711 create_unstaged_content(char **path_unstaged_content,
8712 char **path_new_staged_content, struct got_object_id *blob_id,
8713 struct got_object_id *staged_blob_id, const char *relpath,
8714 struct got_repository *repo,
8715 got_worktree_patch_cb patch_cb, void *patch_arg)
8717 const struct got_error *err, *free_err;
8718 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8719 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8720 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8721 struct got_diffreg_result *diffreg_result = NULL;
8722 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8723 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8724 int fd1 = -1, fd2 = -1;
8726 *path_unstaged_content = NULL;
8727 *path_new_staged_content = NULL;
8729 err = got_object_id_str(&label1, blob_id);
8730 if (err)
8731 return err;
8733 fd1 = got_opentempfd();
8734 if (fd1 == -1) {
8735 err = got_error_from_errno("got_opentempfd");
8736 goto done;
8738 fd2 = got_opentempfd();
8739 if (fd2 == -1) {
8740 err = got_error_from_errno("got_opentempfd");
8741 goto done;
8744 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8745 if (err)
8746 goto done;
8748 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
8749 if (err)
8750 goto done;
8752 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8753 if (err)
8754 goto done;
8756 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8757 fd2);
8758 if (err)
8759 goto done;
8761 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
8762 if (err)
8763 goto done;
8765 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8766 if (err)
8767 goto done;
8769 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8770 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8771 if (err)
8772 goto done;
8774 err = got_opentemp_named(path_unstaged_content, &outfile,
8775 "got-unstaged-content", "");
8776 if (err)
8777 goto done;
8778 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8779 "got-new-staged-content", "");
8780 if (err)
8781 goto done;
8783 if (fseek(f1, 0L, SEEK_SET) == -1) {
8784 err = got_ferror(f1, GOT_ERR_IO);
8785 goto done;
8787 if (fseek(f2, 0L, SEEK_SET) == -1) {
8788 err = got_ferror(f2, GOT_ERR_IO);
8789 goto done;
8791 /* Count the number of actual changes in the diff result. */
8792 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8793 struct diff_chunk_context cc = {};
8794 diff_chunk_context_load_change(&cc, &nchunks_used,
8795 diffreg_result->result, n, 0);
8796 nchanges++;
8798 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8799 int choice;
8800 err = apply_or_reject_change(&choice, &nchunks_used,
8801 diffreg_result->result, n, relpath, f1, f2,
8802 &line_cur1, &line_cur2,
8803 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8804 if (err)
8805 goto done;
8806 if (choice == GOT_PATCH_CHOICE_YES)
8807 have_content = 1;
8808 else
8809 have_rejected_content = 1;
8810 if (choice == GOT_PATCH_CHOICE_QUIT)
8811 break;
8813 if (have_content || have_rejected_content)
8814 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8815 outfile, rejectfile);
8816 done:
8817 free(label1);
8818 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8819 err = got_error_from_errno("close");
8820 if (blob)
8821 got_object_blob_close(blob);
8822 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8823 err = got_error_from_errno("close");
8824 if (staged_blob)
8825 got_object_blob_close(staged_blob);
8826 free_err = got_diffreg_result_free(diffreg_result);
8827 if (free_err && err == NULL)
8828 err = free_err;
8829 if (f1 && fclose(f1) == EOF && err == NULL)
8830 err = got_error_from_errno2("fclose", path1);
8831 if (f2 && fclose(f2) == EOF && err == NULL)
8832 err = got_error_from_errno2("fclose", path2);
8833 if (outfile && fclose(outfile) == EOF && err == NULL)
8834 err = got_error_from_errno2("fclose", *path_unstaged_content);
8835 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8836 err = got_error_from_errno2("fclose", *path_new_staged_content);
8837 if (path1 && unlink(path1) == -1 && err == NULL)
8838 err = got_error_from_errno2("unlink", path1);
8839 if (path2 && unlink(path2) == -1 && err == NULL)
8840 err = got_error_from_errno2("unlink", path2);
8841 if (err || !have_content) {
8842 if (*path_unstaged_content &&
8843 unlink(*path_unstaged_content) == -1 && err == NULL)
8844 err = got_error_from_errno2("unlink",
8845 *path_unstaged_content);
8846 free(*path_unstaged_content);
8847 *path_unstaged_content = NULL;
8849 if (err || !have_content || !have_rejected_content) {
8850 if (*path_new_staged_content &&
8851 unlink(*path_new_staged_content) == -1 && err == NULL)
8852 err = got_error_from_errno2("unlink",
8853 *path_new_staged_content);
8854 free(*path_new_staged_content);
8855 *path_new_staged_content = NULL;
8857 free(path1);
8858 free(path2);
8859 return err;
8862 static const struct got_error *
8863 unstage_hunks(struct got_object_id *staged_blob_id,
8864 struct got_blob_object *blob_base,
8865 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8866 const char *ondisk_path, const char *label_orig,
8867 struct got_worktree *worktree, struct got_repository *repo,
8868 got_worktree_patch_cb patch_cb, void *patch_arg,
8869 got_worktree_checkout_cb progress_cb, void *progress_arg)
8871 const struct got_error *err = NULL;
8872 char *path_unstaged_content = NULL;
8873 char *path_new_staged_content = NULL;
8874 char *parent = NULL, *base_path = NULL;
8875 char *blob_base_path = NULL;
8876 struct got_object_id *new_staged_blob_id = NULL;
8877 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8878 struct stat sb;
8880 err = create_unstaged_content(&path_unstaged_content,
8881 &path_new_staged_content, blob_id, staged_blob_id,
8882 ie->path, repo, patch_cb, patch_arg);
8883 if (err)
8884 return err;
8886 if (path_unstaged_content == NULL)
8887 return NULL;
8889 if (path_new_staged_content) {
8890 err = got_object_blob_create(&new_staged_blob_id,
8891 path_new_staged_content, repo);
8892 if (err)
8893 goto done;
8896 f = fopen(path_unstaged_content, "re");
8897 if (f == NULL) {
8898 err = got_error_from_errno2("fopen",
8899 path_unstaged_content);
8900 goto done;
8902 if (fstat(fileno(f), &sb) == -1) {
8903 err = got_error_from_errno2("fstat", path_unstaged_content);
8904 goto done;
8906 if (got_fileindex_entry_staged_filetype_get(ie) ==
8907 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8908 char link_target[PATH_MAX];
8909 size_t r;
8910 r = fread(link_target, 1, sizeof(link_target), f);
8911 if (r == 0 && ferror(f)) {
8912 err = got_error_from_errno("fread");
8913 goto done;
8915 if (r >= sizeof(link_target)) { /* should not happen */
8916 err = got_error(GOT_ERR_NO_SPACE);
8917 goto done;
8919 link_target[r] = '\0';
8920 err = merge_symlink(worktree, blob_base,
8921 ondisk_path, ie->path, label_orig, link_target,
8922 worktree->base_commit_id, repo, progress_cb,
8923 progress_arg);
8924 } else {
8925 int local_changes_subsumed;
8927 err = got_path_dirname(&parent, ondisk_path);
8928 if (err)
8929 return err;
8931 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8932 parent) == -1) {
8933 err = got_error_from_errno("asprintf");
8934 base_path = NULL;
8935 goto done;
8938 err = got_opentemp_named(&blob_base_path, &f_base,
8939 base_path, "");
8940 if (err)
8941 goto done;
8942 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8943 blob_base);
8944 if (err)
8945 goto done;
8948 * In order the run a 3-way merge with a symlink we copy the symlink's
8949 * target path into a temporary file and use that file with diff3.
8951 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8952 err = dump_symlink_target_path_to_file(&f_deriv2,
8953 ondisk_path);
8954 if (err)
8955 goto done;
8956 } else {
8957 int fd;
8958 fd = open(ondisk_path,
8959 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8960 if (fd == -1) {
8961 err = got_error_from_errno2("open", ondisk_path);
8962 goto done;
8964 f_deriv2 = fdopen(fd, "r");
8965 if (f_deriv2 == NULL) {
8966 err = got_error_from_errno2("fdopen", ondisk_path);
8967 close(fd);
8968 goto done;
8972 err = merge_file(&local_changes_subsumed, worktree,
8973 f_base, f, f_deriv2, ondisk_path, ie->path,
8974 got_fileindex_perms_to_st(ie),
8975 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8976 repo, progress_cb, progress_arg);
8978 if (err)
8979 goto done;
8981 if (new_staged_blob_id) {
8982 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8983 SHA1_DIGEST_LENGTH);
8984 } else {
8985 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8986 got_fileindex_entry_staged_filetype_set(ie, 0);
8988 done:
8989 free(new_staged_blob_id);
8990 if (path_unstaged_content &&
8991 unlink(path_unstaged_content) == -1 && err == NULL)
8992 err = got_error_from_errno2("unlink", path_unstaged_content);
8993 if (path_new_staged_content &&
8994 unlink(path_new_staged_content) == -1 && err == NULL)
8995 err = got_error_from_errno2("unlink", path_new_staged_content);
8996 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8997 err = got_error_from_errno2("unlink", blob_base_path);
8998 if (f_base && fclose(f_base) == EOF && err == NULL)
8999 err = got_error_from_errno2("fclose", path_unstaged_content);
9000 if (f && fclose(f) == EOF && err == NULL)
9001 err = got_error_from_errno2("fclose", path_unstaged_content);
9002 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
9003 err = got_error_from_errno2("fclose", ondisk_path);
9004 free(path_unstaged_content);
9005 free(path_new_staged_content);
9006 free(blob_base_path);
9007 free(parent);
9008 free(base_path);
9009 return err;
9012 static const struct got_error *
9013 unstage_path(void *arg, unsigned char status,
9014 unsigned char staged_status, const char *relpath,
9015 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9016 struct got_object_id *commit_id, int dirfd, const char *de_name)
9018 const struct got_error *err = NULL;
9019 struct unstage_path_arg *a = arg;
9020 struct got_fileindex_entry *ie;
9021 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
9022 char *ondisk_path = NULL;
9023 char *id_str = NULL, *label_orig = NULL;
9024 int local_changes_subsumed;
9025 struct stat sb;
9026 int fd1 = -1, fd2 = -1;
9028 if (staged_status != GOT_STATUS_ADD &&
9029 staged_status != GOT_STATUS_MODIFY &&
9030 staged_status != GOT_STATUS_DELETE)
9031 return NULL;
9033 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9034 if (ie == NULL)
9035 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9037 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9038 == -1)
9039 return got_error_from_errno("asprintf");
9041 err = got_object_id_str(&id_str,
9042 commit_id ? commit_id : a->worktree->base_commit_id);
9043 if (err)
9044 goto done;
9045 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9046 id_str) == -1) {
9047 err = got_error_from_errno("asprintf");
9048 goto done;
9051 fd1 = got_opentempfd();
9052 if (fd1 == -1) {
9053 err = got_error_from_errno("got_opentempfd");
9054 goto done;
9056 fd2 = got_opentempfd();
9057 if (fd2 == -1) {
9058 err = got_error_from_errno("got_opentempfd");
9059 goto done;
9062 switch (staged_status) {
9063 case GOT_STATUS_MODIFY:
9064 err = got_object_open_as_blob(&blob_base, a->repo,
9065 blob_id, 8192, fd1);
9066 if (err)
9067 break;
9068 /* fall through */
9069 case GOT_STATUS_ADD:
9070 if (a->patch_cb) {
9071 if (staged_status == GOT_STATUS_ADD) {
9072 int choice = GOT_PATCH_CHOICE_NONE;
9073 err = (*a->patch_cb)(&choice, a->patch_arg,
9074 staged_status, ie->path, NULL, 1, 1);
9075 if (err)
9076 break;
9077 if (choice != GOT_PATCH_CHOICE_YES)
9078 break;
9079 } else {
9080 err = unstage_hunks(staged_blob_id,
9081 blob_base, blob_id, ie, ondisk_path,
9082 label_orig, a->worktree, a->repo,
9083 a->patch_cb, a->patch_arg,
9084 a->progress_cb, a->progress_arg);
9085 break; /* Done with this file. */
9088 err = got_object_open_as_blob(&blob_staged, a->repo,
9089 staged_blob_id, 8192, fd2);
9090 if (err)
9091 break;
9092 switch (got_fileindex_entry_staged_filetype_get(ie)) {
9093 case GOT_FILEIDX_MODE_BAD_SYMLINK:
9094 case GOT_FILEIDX_MODE_REGULAR_FILE:
9095 err = merge_blob(&local_changes_subsumed, a->worktree,
9096 blob_base, ondisk_path, relpath,
9097 got_fileindex_perms_to_st(ie), label_orig,
9098 blob_staged, commit_id ? commit_id :
9099 a->worktree->base_commit_id, a->repo,
9100 a->progress_cb, a->progress_arg);
9101 break;
9102 case GOT_FILEIDX_MODE_SYMLINK:
9103 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9104 char *staged_target;
9105 err = got_object_blob_read_to_str(
9106 &staged_target, blob_staged);
9107 if (err)
9108 goto done;
9109 err = merge_symlink(a->worktree, blob_base,
9110 ondisk_path, relpath, label_orig,
9111 staged_target, commit_id ? commit_id :
9112 a->worktree->base_commit_id,
9113 a->repo, a->progress_cb, a->progress_arg);
9114 free(staged_target);
9115 } else {
9116 err = merge_blob(&local_changes_subsumed,
9117 a->worktree, blob_base, ondisk_path,
9118 relpath, got_fileindex_perms_to_st(ie),
9119 label_orig, blob_staged,
9120 commit_id ? commit_id :
9121 a->worktree->base_commit_id, a->repo,
9122 a->progress_cb, a->progress_arg);
9124 break;
9125 default:
9126 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9127 break;
9129 if (err == NULL) {
9130 got_fileindex_entry_stage_set(ie,
9131 GOT_FILEIDX_STAGE_NONE);
9132 got_fileindex_entry_staged_filetype_set(ie, 0);
9134 break;
9135 case GOT_STATUS_DELETE:
9136 if (a->patch_cb) {
9137 int choice = GOT_PATCH_CHOICE_NONE;
9138 err = (*a->patch_cb)(&choice, a->patch_arg,
9139 staged_status, ie->path, NULL, 1, 1);
9140 if (err)
9141 break;
9142 if (choice == GOT_PATCH_CHOICE_NO)
9143 break;
9144 if (choice != GOT_PATCH_CHOICE_YES) {
9145 err = got_error(GOT_ERR_PATCH_CHOICE);
9146 break;
9149 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9150 got_fileindex_entry_staged_filetype_set(ie, 0);
9151 err = get_file_status(&status, &sb, ie, ondisk_path,
9152 dirfd, de_name, a->repo);
9153 if (err)
9154 break;
9155 err = (*a->progress_cb)(a->progress_arg, status, relpath);
9156 break;
9158 done:
9159 free(ondisk_path);
9160 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9161 err = got_error_from_errno("close");
9162 if (blob_base)
9163 got_object_blob_close(blob_base);
9164 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9165 err = got_error_from_errno("close");
9166 if (blob_staged)
9167 got_object_blob_close(blob_staged);
9168 free(id_str);
9169 free(label_orig);
9170 return err;
9173 const struct got_error *
9174 got_worktree_unstage(struct got_worktree *worktree,
9175 struct got_pathlist_head *paths,
9176 got_worktree_checkout_cb progress_cb, void *progress_arg,
9177 got_worktree_patch_cb patch_cb, void *patch_arg,
9178 struct got_repository *repo)
9180 const struct got_error *err = NULL, *sync_err, *unlockerr;
9181 struct got_pathlist_entry *pe;
9182 struct got_fileindex *fileindex = NULL;
9183 char *fileindex_path = NULL;
9184 struct unstage_path_arg upa;
9186 err = lock_worktree(worktree, LOCK_EX);
9187 if (err)
9188 return err;
9190 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9191 if (err)
9192 goto done;
9194 upa.worktree = worktree;
9195 upa.fileindex = fileindex;
9196 upa.repo = repo;
9197 upa.progress_cb = progress_cb;
9198 upa.progress_arg = progress_arg;
9199 upa.patch_cb = patch_cb;
9200 upa.patch_arg = patch_arg;
9201 TAILQ_FOREACH(pe, paths, entry) {
9202 err = worktree_status(worktree, pe->path, fileindex, repo,
9203 unstage_path, &upa, NULL, NULL, 1, 0);
9204 if (err)
9205 goto done;
9208 sync_err = sync_fileindex(fileindex, fileindex_path);
9209 if (sync_err && err == NULL)
9210 err = sync_err;
9211 done:
9212 free(fileindex_path);
9213 if (fileindex)
9214 got_fileindex_free(fileindex);
9215 unlockerr = lock_worktree(worktree, LOCK_SH);
9216 if (unlockerr && err == NULL)
9217 err = unlockerr;
9218 return err;
9221 struct report_file_info_arg {
9222 struct got_worktree *worktree;
9223 got_worktree_path_info_cb info_cb;
9224 void *info_arg;
9225 struct got_pathlist_head *paths;
9226 got_cancel_cb cancel_cb;
9227 void *cancel_arg;
9230 static const struct got_error *
9231 report_file_info(void *arg, struct got_fileindex_entry *ie)
9233 struct report_file_info_arg *a = arg;
9234 struct got_pathlist_entry *pe;
9235 struct got_object_id blob_id, staged_blob_id, commit_id;
9236 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9237 struct got_object_id *commit_idp = NULL;
9238 int stage;
9240 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9241 return got_error(GOT_ERR_CANCELLED);
9243 TAILQ_FOREACH(pe, a->paths, entry) {
9244 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9245 got_path_is_child(ie->path, pe->path, pe->path_len))
9246 break;
9248 if (pe == NULL) /* not found */
9249 return NULL;
9251 if (got_fileindex_entry_has_blob(ie))
9252 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9253 stage = got_fileindex_entry_stage_get(ie);
9254 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9255 stage == GOT_FILEIDX_STAGE_ADD) {
9256 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9257 &staged_blob_id, ie);
9260 if (got_fileindex_entry_has_commit(ie))
9261 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9263 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9264 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9267 const struct got_error *
9268 got_worktree_path_info(struct got_worktree *worktree,
9269 struct got_pathlist_head *paths,
9270 got_worktree_path_info_cb info_cb, void *info_arg,
9271 got_cancel_cb cancel_cb, void *cancel_arg)
9274 const struct got_error *err = NULL, *unlockerr;
9275 struct got_fileindex *fileindex = NULL;
9276 char *fileindex_path = NULL;
9277 struct report_file_info_arg arg;
9279 err = lock_worktree(worktree, LOCK_SH);
9280 if (err)
9281 return err;
9283 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9284 if (err)
9285 goto done;
9287 arg.worktree = worktree;
9288 arg.info_cb = info_cb;
9289 arg.info_arg = info_arg;
9290 arg.paths = paths;
9291 arg.cancel_cb = cancel_cb;
9292 arg.cancel_arg = cancel_arg;
9293 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9294 &arg);
9295 done:
9296 free(fileindex_path);
9297 if (fileindex)
9298 got_fileindex_free(fileindex);
9299 unlockerr = lock_worktree(worktree, LOCK_UN);
9300 if (unlockerr && err == NULL)
9301 err = unlockerr;
9302 return err;
9305 static const struct got_error *
9306 patch_check_path(const char *p, char **path, unsigned char *status,
9307 unsigned char *staged_status, struct got_fileindex *fileindex,
9308 struct got_worktree *worktree, struct got_repository *repo)
9310 const struct got_error *err;
9311 struct got_fileindex_entry *ie;
9312 struct stat sb;
9313 char *ondisk_path = NULL;
9315 err = got_worktree_resolve_path(path, worktree, p);
9316 if (err)
9317 return err;
9319 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9320 *path[0] ? "/" : "", *path) == -1)
9321 return got_error_from_errno("asprintf");
9323 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9324 if (ie) {
9325 *staged_status = get_staged_status(ie);
9326 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9327 repo);
9328 if (err)
9329 goto done;
9330 } else {
9331 *staged_status = GOT_STATUS_NO_CHANGE;
9332 *status = GOT_STATUS_UNVERSIONED;
9333 if (lstat(ondisk_path, &sb) == -1) {
9334 if (errno != ENOENT) {
9335 err = got_error_from_errno2("lstat",
9336 ondisk_path);
9337 goto done;
9339 *status = GOT_STATUS_NONEXISTENT;
9343 done:
9344 free(ondisk_path);
9345 return err;
9348 static const struct got_error *
9349 patch_can_rm(const char *path, unsigned char status,
9350 unsigned char staged_status)
9352 if (status == GOT_STATUS_NONEXISTENT)
9353 return got_error_set_errno(ENOENT, path);
9354 if (status != GOT_STATUS_NO_CHANGE &&
9355 status != GOT_STATUS_ADD &&
9356 status != GOT_STATUS_MODIFY &&
9357 status != GOT_STATUS_MODE_CHANGE)
9358 return got_error_path(path, GOT_ERR_FILE_STATUS);
9359 if (staged_status == GOT_STATUS_DELETE)
9360 return got_error_path(path, GOT_ERR_FILE_STATUS);
9361 return NULL;
9364 static const struct got_error *
9365 patch_can_add(const char *path, unsigned char status)
9367 if (status != GOT_STATUS_NONEXISTENT)
9368 return got_error_path(path, GOT_ERR_FILE_STATUS);
9369 return NULL;
9372 static const struct got_error *
9373 patch_can_edit(const char *path, unsigned char status,
9374 unsigned char staged_status)
9376 if (status == GOT_STATUS_NONEXISTENT)
9377 return got_error_set_errno(ENOENT, path);
9378 if (status != GOT_STATUS_NO_CHANGE &&
9379 status != GOT_STATUS_ADD &&
9380 status != GOT_STATUS_MODIFY)
9381 return got_error_path(path, GOT_ERR_FILE_STATUS);
9382 if (staged_status == GOT_STATUS_DELETE)
9383 return got_error_path(path, GOT_ERR_FILE_STATUS);
9384 return NULL;
9387 const struct got_error *
9388 got_worktree_patch_prepare(struct got_fileindex **fileindex,
9389 char **fileindex_path, struct got_worktree *worktree)
9391 return open_fileindex(fileindex, fileindex_path, worktree);
9394 const struct got_error *
9395 got_worktree_patch_check_path(const char *old, const char *new,
9396 char **oldpath, char **newpath, struct got_worktree *worktree,
9397 struct got_repository *repo, struct got_fileindex *fileindex)
9399 const struct got_error *err = NULL;
9400 int file_renamed = 0;
9401 unsigned char status_old, staged_status_old;
9402 unsigned char status_new, staged_status_new;
9404 *oldpath = NULL;
9405 *newpath = NULL;
9407 err = patch_check_path(old != NULL ? old : new, oldpath,
9408 &status_old, &staged_status_old, fileindex, worktree, repo);
9409 if (err)
9410 goto done;
9412 err = patch_check_path(new != NULL ? new : old, newpath,
9413 &status_new, &staged_status_new, fileindex, worktree, repo);
9414 if (err)
9415 goto done;
9417 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9418 file_renamed = 1;
9420 if (old != NULL && new == NULL)
9421 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9422 else if (file_renamed) {
9423 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9424 if (err == NULL)
9425 err = patch_can_add(*newpath, status_new);
9426 } else if (old == NULL)
9427 err = patch_can_add(*newpath, status_new);
9428 else
9429 err = patch_can_edit(*newpath, status_new, staged_status_new);
9431 done:
9432 if (err) {
9433 free(*oldpath);
9434 *oldpath = NULL;
9435 free(*newpath);
9436 *newpath = NULL;
9438 return err;
9441 const struct got_error *
9442 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9443 struct got_worktree *worktree, struct got_fileindex *fileindex,
9444 got_worktree_checkout_cb progress_cb, void *progress_arg)
9446 struct schedule_addition_args saa;
9448 memset(&saa, 0, sizeof(saa));
9449 saa.worktree = worktree;
9450 saa.fileindex = fileindex;
9451 saa.progress_cb = progress_cb;
9452 saa.progress_arg = progress_arg;
9453 saa.repo = repo;
9455 return worktree_status(worktree, path, fileindex, repo,
9456 schedule_addition, &saa, NULL, NULL, 1, 0);
9459 const struct got_error *
9460 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9461 struct got_worktree *worktree, struct got_fileindex *fileindex,
9462 got_worktree_delete_cb progress_cb, void *progress_arg)
9464 struct schedule_deletion_args sda;
9466 memset(&sda, 0, sizeof(sda));
9467 sda.worktree = worktree;
9468 sda.fileindex = fileindex;
9469 sda.progress_cb = progress_cb;
9470 sda.progress_arg = progress_arg;
9471 sda.repo = repo;
9472 sda.delete_local_mods = 0;
9473 sda.keep_on_disk = 0;
9474 sda.ignore_missing_paths = 0;
9475 sda.status_codes = NULL;
9477 return worktree_status(worktree, path, fileindex, repo,
9478 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9481 const struct got_error *
9482 got_worktree_patch_complete(struct got_fileindex *fileindex,
9483 const char *fileindex_path)
9485 const struct got_error *err = NULL;
9487 err = sync_fileindex(fileindex, fileindex_path);
9488 got_fileindex_free(fileindex);
9490 return err;