Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/stat.h>
18 #include <sys/queue.h>
19 #include <sys/tree.h>
21 #include <dirent.h>
22 #include <limits.h>
23 #include <stddef.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <time.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <sha1.h>
32 #include <sha2.h>
33 #include <zlib.h>
34 #include <fnmatch.h>
35 #include <libgen.h>
36 #include <uuid.h>
37 #include <util.h>
39 #include "got_error.h"
40 #include "got_repository.h"
41 #include "got_reference.h"
42 #include "got_object.h"
43 #include "got_path.h"
44 #include "got_cancel.h"
45 #include "got_worktree.h"
46 #include "got_opentemp.h"
47 #include "got_diff.h"
49 #include "got_lib_worktree.h"
50 #include "got_lib_hash.h"
51 #include "got_lib_fileindex.h"
52 #include "got_lib_inflate.h"
53 #include "got_lib_delta.h"
54 #include "got_lib_object.h"
55 #include "got_lib_object_parse.h"
56 #include "got_lib_object_create.h"
57 #include "got_lib_object_idset.h"
58 #include "got_lib_diff.h"
59 #include "got_lib_gotconfig.h"
61 #ifndef MIN
62 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
63 #endif
65 #define GOT_MERGE_LABEL_MERGED "merged change"
66 #define GOT_MERGE_LABEL_BASE "3-way merge base"
68 static mode_t apply_umask(mode_t);
70 static const struct got_error *
71 create_meta_file(const char *path_got, const char *name, const char *content)
72 {
73 const struct got_error *err = NULL;
74 char *path;
76 if (asprintf(&path, "%s/%s", path_got, name) == -1)
77 return got_error_from_errno("asprintf");
79 err = got_path_create_file(path, content);
80 free(path);
81 return err;
82 }
84 static const struct got_error *
85 update_meta_file(const char *path_got, const char *name, const char *content)
86 {
87 const struct got_error *err = NULL;
88 FILE *tmpfile = NULL;
89 char *tmppath = NULL;
90 char *path = NULL;
92 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
93 err = got_error_from_errno("asprintf");
94 path = NULL;
95 goto done;
96 }
98 err = got_opentemp_named(&tmppath, &tmpfile, path, "");
99 if (err)
100 goto done;
102 if (content) {
103 int len = fprintf(tmpfile, "%s\n", content);
104 if (len != strlen(content) + 1) {
105 err = got_error_from_errno2("fprintf", tmppath);
106 goto done;
110 if (rename(tmppath, path) != 0) {
111 err = got_error_from_errno3("rename", tmppath, path);
112 unlink(tmppath);
113 goto done;
116 done:
117 if (fclose(tmpfile) == EOF && err == NULL)
118 err = got_error_from_errno2("fclose", tmppath);
119 free(tmppath);
120 return err;
123 static const struct got_error *
124 write_head_ref(const char *path_got, struct got_reference *head_ref)
126 const struct got_error *err = NULL;
127 char *refstr = NULL;
129 if (got_ref_is_symbolic(head_ref)) {
130 refstr = got_ref_to_str(head_ref);
131 if (refstr == NULL)
132 return got_error_from_errno("got_ref_to_str");
133 } else {
134 refstr = strdup(got_ref_get_name(head_ref));
135 if (refstr == NULL)
136 return got_error_from_errno("strdup");
138 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
139 free(refstr);
140 return err;
143 const struct got_error *
144 got_worktree_init(const char *path, struct got_reference *head_ref,
145 const char *prefix, struct got_repository *repo)
147 const struct got_error *err = NULL;
148 struct got_object_id *commit_id = NULL;
149 uuid_t uuid;
150 uint32_t uuid_status;
151 int obj_type;
152 char *path_got = NULL;
153 char *formatstr = NULL;
154 char *absprefix = NULL;
155 char *basestr = NULL;
156 char *uuidstr = NULL;
158 if (strcmp(path, got_repo_get_path(repo)) == 0) {
159 err = got_error(GOT_ERR_WORKTREE_REPO);
160 goto done;
163 err = got_ref_resolve(&commit_id, repo, head_ref);
164 if (err)
165 return err;
166 err = got_object_get_type(&obj_type, repo, commit_id);
167 if (err)
168 return err;
169 if (obj_type != GOT_OBJ_TYPE_COMMIT)
170 return got_error(GOT_ERR_OBJ_TYPE);
172 if (!got_path_is_absolute(prefix)) {
173 if (asprintf(&absprefix, "/%s", prefix) == -1)
174 return got_error_from_errno("asprintf");
177 /* Create top-level directory (may already exist). */
178 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
179 err = got_error_from_errno2("mkdir", path);
180 goto done;
183 /* Create .got directory (may already exist). */
184 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
185 err = got_error_from_errno("asprintf");
186 goto done;
188 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
189 err = got_error_from_errno2("mkdir", path_got);
190 goto done;
193 /* Create an empty lock file. */
194 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
195 if (err)
196 goto done;
198 /* Create an empty file index. */
199 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
200 if (err)
201 goto done;
203 /* Write the HEAD reference. */
204 err = write_head_ref(path_got, head_ref);
205 if (err)
206 goto done;
208 /* Record our base commit. */
209 err = got_object_id_str(&basestr, commit_id);
210 if (err)
211 goto done;
212 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
213 if (err)
214 goto done;
216 /* Store path to repository. */
217 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
218 got_repo_get_path(repo));
219 if (err)
220 goto done;
222 /* Store in-repository path prefix. */
223 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
224 absprefix ? absprefix : prefix);
225 if (err)
226 goto done;
228 /* Generate UUID. */
229 uuid_create(&uuid, &uuid_status);
230 if (uuid_status != uuid_s_ok) {
231 err = got_error_uuid(uuid_status, "uuid_create");
232 goto done;
234 uuid_to_string(&uuid, &uuidstr, &uuid_status);
235 if (uuid_status != uuid_s_ok) {
236 err = got_error_uuid(uuid_status, "uuid_to_string");
237 goto done;
239 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
240 if (err)
241 goto done;
243 /* Stamp work tree with format file. */
244 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
245 err = got_error_from_errno("asprintf");
246 goto done;
248 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
249 if (err)
250 goto done;
252 done:
253 free(commit_id);
254 free(path_got);
255 free(formatstr);
256 free(absprefix);
257 free(basestr);
258 free(uuidstr);
259 return err;
262 const struct got_error *
263 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
264 const char *path_prefix)
266 char *absprefix = NULL;
268 if (!got_path_is_absolute(path_prefix)) {
269 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
270 return got_error_from_errno("asprintf");
272 *match = (strcmp(absprefix ? absprefix : path_prefix,
273 worktree->path_prefix) == 0);
274 free(absprefix);
275 return NULL;
278 const char *
279 got_worktree_get_head_ref_name(struct got_worktree *worktree)
281 return worktree->head_ref_name;
284 const struct got_error *
285 got_worktree_set_head_ref(struct got_worktree *worktree,
286 struct got_reference *head_ref)
288 const struct got_error *err = NULL;
289 char *path_got = NULL, *head_ref_name = NULL;
291 if (asprintf(&path_got, "%s/%s", worktree->root_path,
292 GOT_WORKTREE_GOT_DIR) == -1) {
293 err = got_error_from_errno("asprintf");
294 path_got = NULL;
295 goto done;
298 head_ref_name = strdup(got_ref_get_name(head_ref));
299 if (head_ref_name == NULL) {
300 err = got_error_from_errno("strdup");
301 goto done;
304 err = write_head_ref(path_got, head_ref);
305 if (err)
306 goto done;
308 free(worktree->head_ref_name);
309 worktree->head_ref_name = head_ref_name;
310 done:
311 free(path_got);
312 if (err)
313 free(head_ref_name);
314 return err;
317 struct got_object_id *
318 got_worktree_get_base_commit_id(struct got_worktree *worktree)
320 return worktree->base_commit_id;
323 const struct got_error *
324 got_worktree_set_base_commit_id(struct got_worktree *worktree,
325 struct got_repository *repo, struct got_object_id *commit_id)
327 const struct got_error *err;
328 struct got_object *obj = NULL;
329 char *id_str = NULL;
330 char *path_got = NULL;
332 if (asprintf(&path_got, "%s/%s", worktree->root_path,
333 GOT_WORKTREE_GOT_DIR) == -1) {
334 err = got_error_from_errno("asprintf");
335 path_got = NULL;
336 goto done;
339 err = got_object_open(&obj, repo, commit_id);
340 if (err)
341 return err;
343 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
344 err = got_error(GOT_ERR_OBJ_TYPE);
345 goto done;
348 /* Record our base commit. */
349 err = got_object_id_str(&id_str, commit_id);
350 if (err)
351 goto done;
352 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
353 if (err)
354 goto done;
356 free(worktree->base_commit_id);
357 worktree->base_commit_id = got_object_id_dup(commit_id);
358 if (worktree->base_commit_id == NULL) {
359 err = got_error_from_errno("got_object_id_dup");
360 goto done;
362 done:
363 if (obj)
364 got_object_close(obj);
365 free(id_str);
366 free(path_got);
367 return err;
370 const struct got_gotconfig *
371 got_worktree_get_gotconfig(struct got_worktree *worktree)
373 return worktree->gotconfig;
376 static const struct got_error *
377 lock_worktree(struct got_worktree *worktree, int operation)
379 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
380 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
381 : got_error_from_errno2("flock",
382 got_worktree_get_root_path(worktree)));
383 return NULL;
386 static const struct got_error *
387 add_dir_on_disk(struct got_worktree *worktree, const char *path)
389 const struct got_error *err = NULL;
390 char *abspath;
392 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
393 return got_error_from_errno("asprintf");
395 err = got_path_mkdir(abspath);
396 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
397 struct stat sb;
398 err = NULL;
399 if (lstat(abspath, &sb) == -1) {
400 err = got_error_from_errno2("lstat", abspath);
401 } else if (!S_ISDIR(sb.st_mode)) {
402 /* TODO directory is obstructed; do something */
403 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
406 free(abspath);
407 return err;
410 static const struct got_error *
411 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
413 const struct got_error *err = NULL;
414 uint8_t fbuf1[8192];
415 uint8_t fbuf2[8192];
416 size_t flen1 = 0, flen2 = 0;
418 *same = 1;
420 for (;;) {
421 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
422 if (flen1 == 0 && ferror(f1)) {
423 err = got_error_from_errno("fread");
424 break;
426 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
427 if (flen2 == 0 && ferror(f2)) {
428 err = got_error_from_errno("fread");
429 break;
431 if (flen1 == 0) {
432 if (flen2 != 0)
433 *same = 0;
434 break;
435 } else if (flen2 == 0) {
436 if (flen1 != 0)
437 *same = 0;
438 break;
439 } else if (flen1 == flen2) {
440 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
441 *same = 0;
442 break;
444 } else {
445 *same = 0;
446 break;
450 return err;
453 static const struct got_error *
454 check_files_equal(int *same, FILE *f1, FILE *f2)
456 struct stat sb;
457 size_t size1, size2;
459 *same = 1;
461 if (fstat(fileno(f1), &sb) != 0)
462 return got_error_from_errno("fstat");
463 size1 = sb.st_size;
465 if (fstat(fileno(f2), &sb) != 0)
466 return got_error_from_errno("fstat");
467 size2 = sb.st_size;
469 if (size1 != size2) {
470 *same = 0;
471 return NULL;
474 if (fseek(f1, 0L, SEEK_SET) == -1)
475 return got_ferror(f1, GOT_ERR_IO);
476 if (fseek(f2, 0L, SEEK_SET) == -1)
477 return got_ferror(f2, GOT_ERR_IO);
479 return check_file_contents_equal(same, f1, f2);
482 static const struct got_error *
483 copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
485 uint8_t fbuf[65536];
486 size_t flen;
487 ssize_t outlen;
489 *outsize = 0;
491 if (fseek(f, 0L, SEEK_SET) == -1)
492 return got_ferror(f, GOT_ERR_IO);
494 for (;;) {
495 flen = fread(fbuf, 1, sizeof(fbuf), f);
496 if (flen == 0) {
497 if (ferror(f))
498 return got_error_from_errno("fread");
499 if (feof(f))
500 break;
502 outlen = write(outfd, fbuf, flen);
503 if (outlen == -1)
504 return got_error_from_errno("write");
505 if (outlen != flen)
506 return got_error(GOT_ERR_IO);
507 *outsize += outlen;
510 return NULL;
513 static const struct got_error *
514 merge_binary_file(int *overlapcnt, int merged_fd,
515 FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
516 const char *label_deriv, const char *label_orig, const char *label_deriv2,
517 const char *ondisk_path)
519 const struct got_error *err = NULL;
520 int same_content, changed_deriv, changed_deriv2;
521 int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
522 off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
523 char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
524 char *base_path_orig = NULL, *base_path_deriv = NULL;
525 char *base_path_deriv2 = NULL;
527 *overlapcnt = 0;
529 err = check_files_equal(&same_content, f_deriv, f_deriv2);
530 if (err)
531 return err;
533 if (same_content)
534 return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
536 err = check_files_equal(&same_content, f_deriv, f_orig);
537 if (err)
538 return err;
539 changed_deriv = !same_content;
540 err = check_files_equal(&same_content, f_deriv2, f_orig);
541 if (err)
542 return err;
543 changed_deriv2 = !same_content;
545 if (changed_deriv && changed_deriv2) {
546 *overlapcnt = 1;
547 if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
548 err = got_error_from_errno("asprintf");
549 goto done;
551 if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
552 err = got_error_from_errno("asprintf");
553 goto done;
555 if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
556 err = got_error_from_errno("asprintf");
557 goto done;
559 err = got_opentemp_named_fd(&path_orig, &fd_orig,
560 base_path_orig, "");
561 if (err)
562 goto done;
563 err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
564 base_path_deriv, "");
565 if (err)
566 goto done;
567 err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
568 base_path_deriv2, "");
569 if (err)
570 goto done;
571 err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
572 if (err)
573 goto done;
574 err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
575 if (err)
576 goto done;
577 err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
578 if (err)
579 goto done;
580 if (dprintf(merged_fd, "Binary files differ and cannot be "
581 "merged automatically:\n") < 0) {
582 err = got_error_from_errno("dprintf");
583 goto done;
585 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
586 GOT_DIFF_CONFLICT_MARKER_BEGIN,
587 label_deriv ? " " : "",
588 label_deriv ? label_deriv : "",
589 path_deriv) < 0) {
590 err = got_error_from_errno("dprintf");
591 goto done;
593 if (size_orig > 0) {
594 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
595 GOT_DIFF_CONFLICT_MARKER_ORIG,
596 label_orig ? " " : "",
597 label_orig ? label_orig : "",
598 path_orig) < 0) {
599 err = got_error_from_errno("dprintf");
600 goto done;
603 if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
604 GOT_DIFF_CONFLICT_MARKER_SEP,
605 path_deriv2,
606 GOT_DIFF_CONFLICT_MARKER_END,
607 label_deriv2 ? " " : "",
608 label_deriv2 ? label_deriv2 : "") < 0) {
609 err = got_error_from_errno("dprintf");
610 goto done;
612 } else if (changed_deriv)
613 err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
614 else if (changed_deriv2)
615 err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
616 done:
617 if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
618 err == NULL)
619 err = got_error_from_errno2("unlink", path_orig);
620 if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
621 err = got_error_from_errno2("close", path_orig);
622 if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
623 err = got_error_from_errno2("close", path_deriv);
624 if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
625 err = got_error_from_errno2("close", path_deriv2);
626 free(path_orig);
627 free(path_deriv);
628 free(path_deriv2);
629 free(base_path_orig);
630 free(base_path_deriv);
631 free(base_path_deriv2);
632 return err;
635 /*
636 * Perform a 3-way merge where the file f_orig acts as the common
637 * ancestor, the file f_deriv acts as the first derived version,
638 * and the file f_deriv2 acts as the second derived version.
639 * The merge result will be written to a new file at ondisk_path; any
640 * existing file at this path will be replaced.
641 */
642 static const struct got_error *
643 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
644 FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
645 const char *path, uint16_t st_mode,
646 const char *label_orig, const char *label_deriv, const char *label_deriv2,
647 enum got_diff_algorithm diff_algo, struct got_repository *repo,
648 got_worktree_checkout_cb progress_cb, void *progress_arg)
650 const struct got_error *err = NULL;
651 int merged_fd = -1;
652 FILE *f_merged = NULL;
653 char *merged_path = NULL, *base_path = NULL;
654 int overlapcnt = 0;
655 char *parent = NULL;
657 *local_changes_subsumed = 0;
659 err = got_path_dirname(&parent, ondisk_path);
660 if (err)
661 return err;
663 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
664 err = got_error_from_errno("asprintf");
665 goto done;
668 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path, "");
669 if (err)
670 goto done;
672 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
673 f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
674 if (err) {
675 if (err->code != GOT_ERR_FILE_BINARY)
676 goto done;
677 err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
678 f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
679 ondisk_path);
680 if (err)
681 goto done;
684 err = (*progress_cb)(progress_arg,
685 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
686 if (err)
687 goto done;
689 if (fsync(merged_fd) != 0) {
690 err = got_error_from_errno("fsync");
691 goto done;
694 f_merged = fdopen(merged_fd, "r");
695 if (f_merged == NULL) {
696 err = got_error_from_errno("fdopen");
697 goto done;
699 merged_fd = -1;
701 /* Check if a clean merge has subsumed all local changes. */
702 if (overlapcnt == 0) {
703 err = check_files_equal(local_changes_subsumed, f_deriv,
704 f_merged);
705 if (err)
706 goto done;
709 if (fchmod(fileno(f_merged), apply_umask(st_mode)) != 0) {
710 err = got_error_from_errno2("fchmod", merged_path);
711 goto done;
714 if (rename(merged_path, ondisk_path) != 0) {
715 err = got_error_from_errno3("rename", merged_path,
716 ondisk_path);
717 goto done;
719 done:
720 if (err) {
721 if (merged_path)
722 unlink(merged_path);
724 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
725 err = got_error_from_errno("close");
726 if (f_merged && fclose(f_merged) == EOF && err == NULL)
727 err = got_error_from_errno("fclose");
728 free(merged_path);
729 free(base_path);
730 free(parent);
731 return err;
734 static const struct got_error *
735 update_symlink(const char *ondisk_path, const char *target_path,
736 size_t target_len)
738 /* This is not atomic but matches what 'ln -sf' does. */
739 if (unlink(ondisk_path) == -1)
740 return got_error_from_errno2("unlink", ondisk_path);
741 if (symlink(target_path, ondisk_path) == -1)
742 return got_error_from_errno3("symlink", target_path,
743 ondisk_path);
744 return NULL;
747 /*
748 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
749 * in the work tree with a file that contains conflict markers and the
750 * conflicting target paths of the original version, a "derived version"
751 * of a symlink from an incoming change, and a local version of the symlink.
753 * The original versions's target path can be NULL if it is not available,
754 * such as if both derived versions added a new symlink at the same path.
756 * The incoming derived symlink target is NULL in case the incoming change
757 * has deleted this symlink.
758 */
759 static const struct got_error *
760 install_symlink_conflict(const char *deriv_target,
761 struct got_object_id *deriv_base_commit_id, const char *orig_target,
762 const char *label_orig, const char *local_target, const char *ondisk_path)
764 const struct got_error *err;
765 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
766 FILE *f = NULL;
768 err = got_object_id_str(&id_str, deriv_base_commit_id);
769 if (err)
770 return got_error_from_errno("asprintf");
772 if (asprintf(&label_deriv, "%s: commit %s",
773 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
774 err = got_error_from_errno("asprintf");
775 goto done;
778 err = got_opentemp_named(&path, &f, "got-symlink-conflict", "");
779 if (err)
780 goto done;
782 if (fchmod(fileno(f), apply_umask(GOT_DEFAULT_FILE_MODE)) == -1) {
783 err = got_error_from_errno2("fchmod", path);
784 goto done;
787 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
788 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
789 deriv_target ? deriv_target : "(symlink was deleted)",
790 orig_target ? label_orig : "",
791 orig_target ? "\n" : "",
792 orig_target ? orig_target : "",
793 orig_target ? "\n" : "",
794 GOT_DIFF_CONFLICT_MARKER_SEP,
795 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
796 err = got_error_from_errno2("fprintf", path);
797 goto done;
800 if (unlink(ondisk_path) == -1) {
801 err = got_error_from_errno2("unlink", ondisk_path);
802 goto done;
804 if (rename(path, ondisk_path) == -1) {
805 err = got_error_from_errno3("rename", path, ondisk_path);
806 goto done;
808 done:
809 if (f != NULL && fclose(f) == EOF && err == NULL)
810 err = got_error_from_errno2("fclose", path);
811 free(path);
812 free(id_str);
813 free(label_deriv);
814 return err;
817 /* forward declaration */
818 static const struct got_error *
819 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
820 const char *, const char *, uint16_t, const char *,
821 struct got_blob_object *, struct got_object_id *,
822 struct got_repository *, got_worktree_checkout_cb, void *);
824 /*
825 * Merge a symlink into the work tree, where blob_orig acts as the common
826 * ancestor, deriv_target is the link target of the first derived version,
827 * and the symlink on disk acts as the second derived version.
828 * Assume that contents of both blobs represent symlinks.
829 */
830 static const struct got_error *
831 merge_symlink(struct got_worktree *worktree,
832 struct got_blob_object *blob_orig, const char *ondisk_path,
833 const char *path, const char *label_orig, const char *deriv_target,
834 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
835 got_worktree_checkout_cb progress_cb, void *progress_arg)
837 const struct got_error *err = NULL;
838 char *ancestor_target = NULL;
839 struct stat sb;
840 ssize_t ondisk_len, deriv_len;
841 char ondisk_target[PATH_MAX];
842 int have_local_change = 0;
843 int have_incoming_change = 0;
845 if (lstat(ondisk_path, &sb) == -1)
846 return got_error_from_errno2("lstat", ondisk_path);
848 ondisk_len = readlink(ondisk_path, ondisk_target,
849 sizeof(ondisk_target));
850 if (ondisk_len == -1) {
851 err = got_error_from_errno2("readlink",
852 ondisk_path);
853 goto done;
855 ondisk_target[ondisk_len] = '\0';
857 if (blob_orig) {
858 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
859 if (err)
860 goto done;
863 if (ancestor_target == NULL ||
864 (ondisk_len != strlen(ancestor_target) ||
865 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
866 have_local_change = 1;
868 deriv_len = strlen(deriv_target);
869 if (ancestor_target == NULL ||
870 (deriv_len != strlen(ancestor_target) ||
871 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
872 have_incoming_change = 1;
874 if (!have_local_change && !have_incoming_change) {
875 if (ancestor_target) {
876 /* Both sides made the same change. */
877 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
878 path);
879 } else if (deriv_len == ondisk_len &&
880 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
881 /* Both sides added the same symlink. */
882 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
883 path);
884 } else {
885 /* Both sides added symlinks which don't match. */
886 err = install_symlink_conflict(deriv_target,
887 deriv_base_commit_id, ancestor_target,
888 label_orig, ondisk_target, ondisk_path);
889 if (err)
890 goto done;
891 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
892 path);
894 } else if (!have_local_change && have_incoming_change) {
895 /* Apply the incoming change. */
896 err = update_symlink(ondisk_path, deriv_target,
897 strlen(deriv_target));
898 if (err)
899 goto done;
900 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
901 } else if (have_local_change && have_incoming_change) {
902 if (deriv_len == ondisk_len &&
903 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
904 /* Both sides made the same change. */
905 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
906 path);
907 } else {
908 err = install_symlink_conflict(deriv_target,
909 deriv_base_commit_id, ancestor_target, label_orig,
910 ondisk_target, ondisk_path);
911 if (err)
912 goto done;
913 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
914 path);
918 done:
919 free(ancestor_target);
920 return err;
923 static const struct got_error *
924 dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
926 const struct got_error *err = NULL;
927 char target_path[PATH_MAX];
928 ssize_t target_len;
929 size_t n;
930 FILE *f;
932 *outfile = NULL;
934 f = got_opentemp();
935 if (f == NULL)
936 return got_error_from_errno("got_opentemp");
937 target_len = readlink(ondisk_path, target_path, sizeof(target_path));
938 if (target_len == -1) {
939 err = got_error_from_errno2("readlink", ondisk_path);
940 goto done;
942 n = fwrite(target_path, 1, target_len, f);
943 if (n != target_len) {
944 err = got_ferror(f, GOT_ERR_IO);
945 goto done;
947 if (fflush(f) == EOF) {
948 err = got_error_from_errno("fflush");
949 goto done;
951 if (fseek(f, 0L, SEEK_SET) == -1) {
952 err = got_ferror(f, GOT_ERR_IO);
953 goto done;
955 done:
956 if (err)
957 fclose(f);
958 else
959 *outfile = f;
960 return err;
963 /*
964 * Perform a 3-way merge where blob_orig acts as the common ancestor,
965 * blob_deriv acts as the first derived version, and the file on disk
966 * acts as the second derived version.
967 */
968 static const struct got_error *
969 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
970 struct got_blob_object *blob_orig, const char *ondisk_path,
971 const char *path, uint16_t st_mode, const char *label_orig,
972 struct got_blob_object *blob_deriv,
973 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
974 got_worktree_checkout_cb progress_cb, void *progress_arg)
976 const struct got_error *err = NULL;
977 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
978 char *blob_orig_path = NULL;
979 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
980 char *label_deriv = NULL, *parent = NULL;
982 *local_changes_subsumed = 0;
984 err = got_path_dirname(&parent, ondisk_path);
985 if (err)
986 return err;
988 if (blob_orig) {
989 if (asprintf(&base_path, "%s/got-merge-blob-orig",
990 parent) == -1) {
991 err = got_error_from_errno("asprintf");
992 base_path = NULL;
993 goto done;
996 err = got_opentemp_named(&blob_orig_path, &f_orig,
997 base_path, "");
998 if (err)
999 goto done;
1000 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1001 blob_orig);
1002 if (err)
1003 goto done;
1004 free(base_path);
1005 } else {
1007 * No common ancestor exists. This is an "add vs add" conflict
1008 * and we simply use an empty ancestor file to make both files
1009 * appear in the merged result in their entirety.
1011 f_orig = got_opentemp();
1012 if (f_orig == NULL) {
1013 err = got_error_from_errno("got_opentemp");
1014 goto done;
1018 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1019 err = got_error_from_errno("asprintf");
1020 base_path = NULL;
1021 goto done;
1024 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path, "");
1025 if (err)
1026 goto done;
1027 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1028 blob_deriv);
1029 if (err)
1030 goto done;
1032 err = got_object_id_str(&id_str, deriv_base_commit_id);
1033 if (err)
1034 goto done;
1035 if (asprintf(&label_deriv, "%s: commit %s",
1036 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1037 err = got_error_from_errno("asprintf");
1038 goto done;
1042 * In order the run a 3-way merge with a symlink we copy the symlink's
1043 * target path into a temporary file and use that file with diff3.
1045 if (S_ISLNK(st_mode)) {
1046 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1047 if (err)
1048 goto done;
1049 } else {
1050 int fd;
1051 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1052 if (fd == -1) {
1053 err = got_error_from_errno2("open", ondisk_path);
1054 goto done;
1056 f_deriv2 = fdopen(fd, "r");
1057 if (f_deriv2 == NULL) {
1058 err = got_error_from_errno2("fdopen", ondisk_path);
1059 close(fd);
1060 goto done;
1064 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1065 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1066 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1067 done:
1068 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1069 err = got_error_from_errno("fclose");
1070 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1071 err = got_error_from_errno("fclose");
1072 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1073 err = got_error_from_errno("fclose");
1074 free(base_path);
1075 if (blob_orig_path) {
1076 unlink(blob_orig_path);
1077 free(blob_orig_path);
1079 if (blob_deriv_path) {
1080 unlink(blob_deriv_path);
1081 free(blob_deriv_path);
1083 free(id_str);
1084 free(label_deriv);
1085 free(parent);
1086 return err;
1089 static const struct got_error *
1090 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1091 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1092 int wt_fd, const char *path, struct got_object_id *blob_id)
1094 const struct got_error *err = NULL;
1095 struct got_fileindex_entry *new_ie;
1097 *new_iep = NULL;
1099 err = got_fileindex_entry_alloc(&new_ie, path);
1100 if (err)
1101 return err;
1103 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1104 blob_id->sha1, base_commit_id->sha1, 1);
1105 if (err)
1106 goto done;
1108 err = got_fileindex_entry_add(fileindex, new_ie);
1109 done:
1110 if (err)
1111 got_fileindex_entry_free(new_ie);
1112 else
1113 *new_iep = new_ie;
1114 return err;
1117 static mode_t
1118 get_ondisk_perms(int executable, mode_t st_mode)
1120 mode_t xbits = S_IXUSR;
1122 if (executable) {
1123 /* Map read bits to execute bits. */
1124 if (st_mode & S_IRGRP)
1125 xbits |= S_IXGRP;
1126 if (st_mode & S_IROTH)
1127 xbits |= S_IXOTH;
1128 return st_mode | xbits;
1131 return st_mode;
1134 static mode_t
1135 apply_umask(mode_t mode)
1137 mode_t um;
1139 um = umask(000);
1140 umask(um);
1141 return mode & ~um;
1144 /* forward declaration */
1145 static const struct got_error *
1146 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1147 const char *path, mode_t te_mode, mode_t st_mode,
1148 struct got_blob_object *blob, int restoring_missing_file,
1149 int reverting_versioned_file, int installing_bad_symlink,
1150 int path_is_unversioned, struct got_repository *repo,
1151 got_worktree_checkout_cb progress_cb, void *progress_arg);
1154 * This function assumes that the provided symlink target points at a
1155 * safe location in the work tree!
1157 static const struct got_error *
1158 replace_existing_symlink(int *did_something, const char *ondisk_path,
1159 const char *target_path, size_t target_len)
1161 const struct got_error *err = NULL;
1162 ssize_t elen;
1163 char etarget[PATH_MAX];
1164 int fd;
1166 *did_something = 0;
1169 * "Bad" symlinks (those pointing outside the work tree or into the
1170 * .got directory) are installed in the work tree as a regular file
1171 * which contains the bad symlink target path.
1172 * The new symlink target has already been checked for safety by our
1173 * caller. If we can successfully open a regular file then we simply
1174 * replace this file with a symlink below.
1176 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1177 if (fd == -1) {
1178 if (!got_err_open_nofollow_on_symlink())
1179 return got_error_from_errno2("open", ondisk_path);
1181 /* We are updating an existing on-disk symlink. */
1182 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1183 if (elen == -1)
1184 return got_error_from_errno2("readlink", ondisk_path);
1186 if (elen == target_len &&
1187 memcmp(etarget, target_path, target_len) == 0)
1188 return NULL; /* nothing to do */
1191 *did_something = 1;
1192 err = update_symlink(ondisk_path, target_path, target_len);
1193 if (fd != -1 && close(fd) == -1 && err == NULL)
1194 err = got_error_from_errno2("close", ondisk_path);
1195 return err;
1198 static const struct got_error *
1199 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1200 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1202 const struct got_error *err = NULL;
1203 char canonpath[PATH_MAX];
1204 char *path_got = NULL;
1206 *is_bad_symlink = 0;
1208 if (target_len >= sizeof(canonpath)) {
1209 *is_bad_symlink = 1;
1210 return NULL;
1214 * We do not use realpath(3) to resolve the symlink's target
1215 * path because we don't want to resolve symlinks recursively.
1216 * Instead we make the path absolute and then canonicalize it.
1217 * Relative symlink target lookup should begin at the directory
1218 * in which the blob object is being installed.
1220 if (!got_path_is_absolute(target_path)) {
1221 char *abspath, *parent;
1222 err = got_path_dirname(&parent, ondisk_path);
1223 if (err)
1224 return err;
1225 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1226 free(parent);
1227 return got_error_from_errno("asprintf");
1229 free(parent);
1230 if (strlen(abspath) >= sizeof(canonpath)) {
1231 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1232 free(abspath);
1233 return err;
1235 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1236 free(abspath);
1237 if (err)
1238 return err;
1239 } else {
1240 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1241 if (err)
1242 return err;
1245 /* Only allow symlinks pointing at paths within the work tree. */
1246 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1247 *is_bad_symlink = 1;
1248 return NULL;
1251 /* Do not allow symlinks pointing into the .got directory. */
1252 if (asprintf(&path_got, "%s/%s", wtroot_path,
1253 GOT_WORKTREE_GOT_DIR) == -1)
1254 return got_error_from_errno("asprintf");
1255 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1256 *is_bad_symlink = 1;
1258 free(path_got);
1259 return NULL;
1262 static const struct got_error *
1263 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1264 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1265 int restoring_missing_file, int reverting_versioned_file,
1266 int path_is_unversioned, int allow_bad_symlinks,
1267 struct got_repository *repo,
1268 got_worktree_checkout_cb progress_cb, void *progress_arg)
1270 const struct got_error *err = NULL;
1271 char target_path[PATH_MAX];
1272 size_t len, target_len = 0;
1273 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1274 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1276 *is_bad_symlink = 0;
1279 * Blob object content specifies the target path of the link.
1280 * If a symbolic link cannot be installed we instead create
1281 * a regular file which contains the link target path stored
1282 * in the blob object.
1284 do {
1285 err = got_object_blob_read_block(&len, blob);
1286 if (err)
1287 return err;
1289 if (len + target_len >= sizeof(target_path)) {
1290 /* Path too long; install as a regular file. */
1291 *is_bad_symlink = 1;
1292 got_object_blob_rewind(blob);
1293 return install_blob(worktree, ondisk_path, path,
1294 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1295 restoring_missing_file, reverting_versioned_file,
1296 1, path_is_unversioned, repo, progress_cb,
1297 progress_arg);
1299 if (len > 0) {
1300 /* Skip blob object header first time around. */
1301 memcpy(target_path + target_len, buf + hdrlen,
1302 len - hdrlen);
1303 target_len += len - hdrlen;
1304 hdrlen = 0;
1306 } while (len != 0);
1307 target_path[target_len] = '\0';
1309 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1310 ondisk_path, worktree->root_path);
1311 if (err)
1312 return err;
1314 if (*is_bad_symlink && !allow_bad_symlinks) {
1315 /* install as a regular file */
1316 got_object_blob_rewind(blob);
1317 err = install_blob(worktree, ondisk_path, path,
1318 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1319 restoring_missing_file, reverting_versioned_file, 1,
1320 path_is_unversioned, repo, progress_cb, progress_arg);
1321 return err;
1324 if (symlink(target_path, ondisk_path) == -1) {
1325 if (errno == EEXIST) {
1326 int symlink_replaced;
1327 if (path_is_unversioned) {
1328 err = (*progress_cb)(progress_arg,
1329 GOT_STATUS_UNVERSIONED, path);
1330 return err;
1332 err = replace_existing_symlink(&symlink_replaced,
1333 ondisk_path, target_path, target_len);
1334 if (err)
1335 return err;
1336 if (progress_cb) {
1337 if (symlink_replaced) {
1338 err = (*progress_cb)(progress_arg,
1339 reverting_versioned_file ?
1340 GOT_STATUS_REVERT :
1341 GOT_STATUS_UPDATE, path);
1342 } else {
1343 err = (*progress_cb)(progress_arg,
1344 GOT_STATUS_EXISTS, path);
1347 return err; /* Nothing else to do. */
1350 if (errno == ENOENT) {
1351 char *parent;
1352 err = got_path_dirname(&parent, ondisk_path);
1353 if (err)
1354 return err;
1355 err = add_dir_on_disk(worktree, parent);
1356 free(parent);
1357 if (err)
1358 return err;
1360 * Retry, and fall through to error handling
1361 * below if this second attempt fails.
1363 if (symlink(target_path, ondisk_path) != -1) {
1364 err = NULL; /* success */
1365 return err;
1369 /* Handle errors from first or second creation attempt. */
1370 if (errno == ENAMETOOLONG) {
1371 /* bad target path; install as a regular file */
1372 *is_bad_symlink = 1;
1373 got_object_blob_rewind(blob);
1374 err = install_blob(worktree, ondisk_path, path,
1375 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1376 restoring_missing_file, reverting_versioned_file, 1,
1377 path_is_unversioned, repo,
1378 progress_cb, progress_arg);
1379 } else if (errno == ENOTDIR) {
1380 err = got_error_path(ondisk_path,
1381 GOT_ERR_FILE_OBSTRUCTED);
1382 } else {
1383 err = got_error_from_errno3("symlink",
1384 target_path, ondisk_path);
1386 } else if (progress_cb)
1387 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1388 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1389 return err;
1392 static const struct got_error *
1393 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1394 const char *path, mode_t te_mode, mode_t st_mode,
1395 struct got_blob_object *blob, int restoring_missing_file,
1396 int reverting_versioned_file, int installing_bad_symlink,
1397 int path_is_unversioned, struct got_repository *repo,
1398 got_worktree_checkout_cb progress_cb, void *progress_arg)
1400 const struct got_error *err = NULL;
1401 int fd = -1;
1402 size_t len, hdrlen;
1403 int update = 0;
1404 char *tmppath = NULL;
1405 mode_t mode;
1407 mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1408 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1409 O_CLOEXEC, mode);
1410 if (fd == -1) {
1411 if (errno == ENOENT || errno == ENOTDIR) {
1412 char *parent;
1413 err = got_path_dirname(&parent, path);
1414 if (err)
1415 return err;
1416 err = add_dir_on_disk(worktree, parent);
1417 if (err && err->code == GOT_ERR_FILE_OBSTRUCTED)
1418 err = got_error_path(path, err->code);
1419 free(parent);
1420 if (err)
1421 return err;
1422 fd = open(ondisk_path,
1423 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1424 mode);
1425 if (fd == -1)
1426 return got_error_from_errno2("open",
1427 ondisk_path);
1428 } else if (errno == EEXIST) {
1429 if (path_is_unversioned) {
1430 err = (*progress_cb)(progress_arg,
1431 GOT_STATUS_UNVERSIONED, path);
1432 goto done;
1434 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1435 !S_ISREG(st_mode) && !installing_bad_symlink) {
1436 /* TODO file is obstructed; do something */
1437 err = got_error_path(ondisk_path,
1438 GOT_ERR_FILE_OBSTRUCTED);
1439 goto done;
1440 } else {
1441 err = got_opentemp_named_fd(&tmppath, &fd,
1442 ondisk_path, "");
1443 if (err)
1444 goto done;
1445 update = 1;
1447 if (fchmod(fd, apply_umask(mode)) == -1) {
1448 err = got_error_from_errno2("fchmod",
1449 tmppath);
1450 goto done;
1453 } else
1454 return got_error_from_errno2("open", ondisk_path);
1457 if (progress_cb) {
1458 if (restoring_missing_file)
1459 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1460 path);
1461 else if (reverting_versioned_file)
1462 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1463 path);
1464 else
1465 err = (*progress_cb)(progress_arg,
1466 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1467 if (err)
1468 goto done;
1471 hdrlen = got_object_blob_get_hdrlen(blob);
1472 do {
1473 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1474 err = got_object_blob_read_block(&len, blob);
1475 if (err)
1476 break;
1477 if (len > 0) {
1478 /* Skip blob object header first time around. */
1479 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1480 if (outlen == -1) {
1481 err = got_error_from_errno("write");
1482 goto done;
1483 } else if (outlen != len - hdrlen) {
1484 err = got_error(GOT_ERR_IO);
1485 goto done;
1487 hdrlen = 0;
1489 } while (len != 0);
1491 if (fsync(fd) != 0) {
1492 err = got_error_from_errno("fsync");
1493 goto done;
1496 if (update) {
1497 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1498 err = got_error_from_errno2("unlink", ondisk_path);
1499 goto done;
1501 if (rename(tmppath, ondisk_path) != 0) {
1502 err = got_error_from_errno3("rename", tmppath,
1503 ondisk_path);
1504 goto done;
1506 free(tmppath);
1507 tmppath = NULL;
1510 done:
1511 if (fd != -1 && close(fd) == -1 && err == NULL)
1512 err = got_error_from_errno("close");
1513 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1514 err = got_error_from_errno2("unlink", tmppath);
1515 free(tmppath);
1516 return err;
1520 * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
1521 * conflict marker is found in newly added lines only.
1523 static const struct got_error *
1524 get_modified_file_content_status(unsigned char *status,
1525 struct got_blob_object *blob, const char *path, struct stat *sb,
1526 FILE *ondisk_file)
1528 const struct got_error *err, *free_err;
1529 const char *markers[3] = {
1530 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1531 GOT_DIFF_CONFLICT_MARKER_SEP,
1532 GOT_DIFF_CONFLICT_MARKER_END
1534 FILE *f1 = NULL;
1535 struct got_diffreg_result *diffreg_result = NULL;
1536 struct diff_result *r;
1537 int nchunks_parsed, n, i = 0, ln = 0;
1538 char *line = NULL;
1539 size_t linesize = 0;
1540 ssize_t linelen;
1542 if (*status != GOT_STATUS_MODIFY)
1543 return NULL;
1545 f1 = got_opentemp();
1546 if (f1 == NULL)
1547 return got_error_from_errno("got_opentemp");
1549 if (blob) {
1550 got_object_blob_rewind(blob);
1551 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
1552 if (err)
1553 goto done;
1556 err = got_diff_files(&diffreg_result, f1, 1, NULL, ondisk_file,
1557 1, NULL, 0, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
1558 if (err)
1559 goto done;
1561 r = diffreg_result->result;
1563 for (n = 0; n < r->chunks.len; n += nchunks_parsed) {
1564 struct diff_chunk *c;
1565 struct diff_chunk_context cc = {};
1566 off_t pos;
1569 * We can optimise a little by advancing straight
1570 * to the next chunk if this one has no added lines.
1572 c = diff_chunk_get(r, n);
1574 if (diff_chunk_type(c) != CHUNK_PLUS) {
1575 nchunks_parsed = 1;
1576 continue; /* removed or unchanged lines */
1579 pos = diff_chunk_get_right_start_pos(c);
1580 if (fseek(ondisk_file, pos, SEEK_SET) == -1) {
1581 err = got_ferror(ondisk_file, GOT_ERR_IO);
1582 goto done;
1585 diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
1586 ln = cc.right.start;
1588 while (ln < cc.right.end) {
1589 linelen = getline(&line, &linesize, ondisk_file);
1590 if (linelen == -1) {
1591 if (feof(ondisk_file))
1592 break;
1593 err = got_ferror(ondisk_file, GOT_ERR_IO);
1594 break;
1597 if (line && strncmp(line, markers[i],
1598 strlen(markers[i])) == 0) {
1599 if (strcmp(markers[i],
1600 GOT_DIFF_CONFLICT_MARKER_END) == 0) {
1601 *status = GOT_STATUS_CONFLICT;
1602 goto done;
1603 } else
1604 i++;
1606 ++ln;
1610 done:
1611 free(line);
1612 if (f1 != NULL && fclose(f1) == EOF && err == NULL)
1613 err = got_error_from_errno("fclose");
1614 free_err = got_diffreg_result_free(diffreg_result);
1615 if (err == NULL)
1616 err = free_err;
1618 return err;
1621 static int
1622 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1624 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1625 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1628 static int
1629 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1631 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1632 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1633 ie->mtime_sec == sb->st_mtim.tv_sec &&
1634 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1635 ie->size == (sb->st_size & 0xffffffff) &&
1636 !xbit_differs(ie, sb->st_mode));
1639 static unsigned char
1640 get_staged_status(struct got_fileindex_entry *ie)
1642 switch (got_fileindex_entry_stage_get(ie)) {
1643 case GOT_FILEIDX_STAGE_ADD:
1644 return GOT_STATUS_ADD;
1645 case GOT_FILEIDX_STAGE_DELETE:
1646 return GOT_STATUS_DELETE;
1647 case GOT_FILEIDX_STAGE_MODIFY:
1648 return GOT_STATUS_MODIFY;
1649 default:
1650 return GOT_STATUS_NO_CHANGE;
1654 static const struct got_error *
1655 get_symlink_modification_status(unsigned char *status,
1656 struct got_fileindex_entry *ie, const char *abspath,
1657 int dirfd, const char *de_name, struct got_blob_object *blob)
1659 const struct got_error *err = NULL;
1660 char target_path[PATH_MAX];
1661 char etarget[PATH_MAX];
1662 ssize_t elen;
1663 size_t len, target_len = 0;
1664 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1665 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1667 *status = GOT_STATUS_NO_CHANGE;
1669 /* Blob object content specifies the target path of the link. */
1670 do {
1671 err = got_object_blob_read_block(&len, blob);
1672 if (err)
1673 return err;
1674 if (len + target_len >= sizeof(target_path)) {
1676 * Should not happen. The blob contents were OK
1677 * when this symlink was installed.
1679 return got_error(GOT_ERR_NO_SPACE);
1681 if (len > 0) {
1682 /* Skip blob object header first time around. */
1683 memcpy(target_path + target_len, buf + hdrlen,
1684 len - hdrlen);
1685 target_len += len - hdrlen;
1686 hdrlen = 0;
1688 } while (len != 0);
1689 target_path[target_len] = '\0';
1691 if (dirfd != -1) {
1692 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1693 if (elen == -1)
1694 return got_error_from_errno2("readlinkat", abspath);
1695 } else {
1696 elen = readlink(abspath, etarget, sizeof(etarget));
1697 if (elen == -1)
1698 return got_error_from_errno2("readlink", abspath);
1701 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1702 *status = GOT_STATUS_MODIFY;
1704 return NULL;
1707 static const struct got_error *
1708 get_file_status(unsigned char *status, struct stat *sb,
1709 struct got_fileindex_entry *ie, const char *abspath,
1710 int dirfd, const char *de_name, struct got_repository *repo)
1712 const struct got_error *err = NULL;
1713 struct got_object_id id;
1714 size_t hdrlen;
1715 int fd = -1, fd1 = -1;
1716 FILE *f = NULL;
1717 uint8_t fbuf[8192];
1718 struct got_blob_object *blob = NULL;
1719 size_t flen, blen;
1720 unsigned char staged_status;
1722 staged_status = get_staged_status(ie);
1723 *status = GOT_STATUS_NO_CHANGE;
1724 memset(sb, 0, sizeof(*sb));
1727 * Whenever the caller provides a directory descriptor and a
1728 * directory entry name for the file, use them! This prevents
1729 * race conditions if filesystem paths change beneath our feet.
1731 if (dirfd != -1) {
1732 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1733 if (errno == ENOENT) {
1734 if (got_fileindex_entry_has_file_on_disk(ie))
1735 *status = GOT_STATUS_MISSING;
1736 else
1737 *status = GOT_STATUS_DELETE;
1738 goto done;
1740 err = got_error_from_errno2("fstatat", abspath);
1741 goto done;
1743 } else {
1744 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1745 if (fd == -1 && errno != ENOENT &&
1746 !got_err_open_nofollow_on_symlink())
1747 return got_error_from_errno2("open", abspath);
1748 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1749 if (lstat(abspath, sb) == -1)
1750 return got_error_from_errno2("lstat", abspath);
1751 } else if (fd == -1 || fstat(fd, sb) == -1) {
1752 if (errno == ENOENT) {
1753 if (got_fileindex_entry_has_file_on_disk(ie))
1754 *status = GOT_STATUS_MISSING;
1755 else
1756 *status = GOT_STATUS_DELETE;
1757 goto done;
1759 err = got_error_from_errno2("fstat", abspath);
1760 goto done;
1764 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1765 *status = GOT_STATUS_OBSTRUCTED;
1766 goto done;
1769 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1770 *status = GOT_STATUS_DELETE;
1771 goto done;
1772 } else if (!got_fileindex_entry_has_blob(ie) &&
1773 staged_status != GOT_STATUS_ADD) {
1774 *status = GOT_STATUS_ADD;
1775 goto done;
1778 if (!stat_info_differs(ie, sb))
1779 goto done;
1781 if (S_ISLNK(sb->st_mode) &&
1782 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1783 *status = GOT_STATUS_MODIFY;
1784 goto done;
1787 if (staged_status == GOT_STATUS_MODIFY ||
1788 staged_status == GOT_STATUS_ADD)
1789 got_fileindex_entry_get_staged_blob_id(&id, ie);
1790 else
1791 got_fileindex_entry_get_blob_id(&id, ie);
1793 fd1 = got_opentempfd();
1794 if (fd1 == -1) {
1795 err = got_error_from_errno("got_opentempfd");
1796 goto done;
1798 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1799 if (err)
1800 goto done;
1802 if (S_ISLNK(sb->st_mode)) {
1803 err = get_symlink_modification_status(status, ie,
1804 abspath, dirfd, de_name, blob);
1805 goto done;
1808 if (dirfd != -1) {
1809 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1810 if (fd == -1) {
1811 err = got_error_from_errno2("openat", abspath);
1812 goto done;
1816 f = fdopen(fd, "r");
1817 if (f == NULL) {
1818 err = got_error_from_errno2("fdopen", abspath);
1819 goto done;
1821 fd = -1;
1822 hdrlen = got_object_blob_get_hdrlen(blob);
1823 for (;;) {
1824 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1825 err = got_object_blob_read_block(&blen, blob);
1826 if (err)
1827 goto done;
1828 /* Skip length of blob object header first time around. */
1829 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1830 if (flen == 0 && ferror(f)) {
1831 err = got_error_from_errno("fread");
1832 goto done;
1834 if (blen - hdrlen == 0) {
1835 if (flen != 0)
1836 *status = GOT_STATUS_MODIFY;
1837 break;
1838 } else if (flen == 0) {
1839 if (blen - hdrlen != 0)
1840 *status = GOT_STATUS_MODIFY;
1841 break;
1842 } else if (blen - hdrlen == flen) {
1843 /* Skip blob object header first time around. */
1844 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1845 *status = GOT_STATUS_MODIFY;
1846 break;
1848 } else {
1849 *status = GOT_STATUS_MODIFY;
1850 break;
1852 hdrlen = 0;
1855 if (*status == GOT_STATUS_MODIFY) {
1856 rewind(f);
1857 err = get_modified_file_content_status(status, blob, ie->path,
1858 sb, f);
1859 } else if (xbit_differs(ie, sb->st_mode))
1860 *status = GOT_STATUS_MODE_CHANGE;
1861 done:
1862 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1863 err = got_error_from_errno("close");
1864 if (blob)
1865 got_object_blob_close(blob);
1866 if (f != NULL && fclose(f) == EOF && err == NULL)
1867 err = got_error_from_errno2("fclose", abspath);
1868 if (fd != -1 && close(fd) == -1 && err == NULL)
1869 err = got_error_from_errno2("close", abspath);
1870 return err;
1874 * Update timestamps in the file index if a file is unmodified and
1875 * we had to run a full content comparison to find out.
1877 static const struct got_error *
1878 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1879 struct got_fileindex_entry *ie, struct stat *sb)
1881 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1882 return got_fileindex_entry_update(ie, wt_fd, path,
1883 ie->blob_sha1, ie->commit_sha1, 1);
1885 return NULL;
1888 static const struct got_error *remove_ondisk_file(const char *, const char *);
1890 static const struct got_error *
1891 update_blob(struct got_worktree *worktree,
1892 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1893 struct got_tree_entry *te, const char *path,
1894 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1895 void *progress_arg)
1897 const struct got_error *err = NULL;
1898 struct got_blob_object *blob = NULL;
1899 char *ondisk_path = NULL;
1900 unsigned char status = GOT_STATUS_NO_CHANGE;
1901 struct stat sb;
1902 int fd1 = -1, fd2 = -1;
1904 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1905 return got_error_from_errno("asprintf");
1907 if (ie) {
1908 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1909 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1910 goto done;
1912 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1913 repo);
1914 if (err)
1915 goto done;
1916 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1917 sb.st_mode = got_fileindex_perms_to_st(ie);
1918 } else {
1919 if (stat(ondisk_path, &sb) == -1) {
1920 if (errno != ENOENT && errno != ENOTDIR) {
1921 err = got_error_from_errno2("stat",
1922 ondisk_path);
1923 goto done;
1925 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1926 status = GOT_STATUS_UNVERSIONED;
1927 } else {
1928 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1929 status = GOT_STATUS_UNVERSIONED;
1930 else
1931 status = GOT_STATUS_OBSTRUCTED;
1935 if (status == GOT_STATUS_OBSTRUCTED) {
1936 if (ie)
1937 got_fileindex_entry_mark_skipped(ie);
1938 err = (*progress_cb)(progress_arg, status, path);
1939 goto done;
1941 if (status == GOT_STATUS_CONFLICT) {
1942 if (ie)
1943 got_fileindex_entry_mark_skipped(ie);
1944 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1945 path);
1946 goto done;
1949 if (S_ISDIR(te->mode)) { /* file changing into a directory */
1950 if (status == GOT_STATUS_UNVERSIONED) {
1951 err = (*progress_cb)(progress_arg, status, path);
1952 } else if (status != GOT_STATUS_NO_CHANGE &&
1953 status != GOT_STATUS_DELETE &&
1954 status != GOT_STATUS_NONEXISTENT &&
1955 status != GOT_STATUS_MISSING) {
1956 err = (*progress_cb)(progress_arg,
1957 GOT_STATUS_CANNOT_DELETE, path);
1958 } else if (ie) {
1959 if (status != GOT_STATUS_DELETE &&
1960 status != GOT_STATUS_NONEXISTENT &&
1961 status != GOT_STATUS_MISSING) {
1962 err = remove_ondisk_file(worktree->root_path,
1963 ie->path);
1964 if (err && !(err->code == GOT_ERR_ERRNO &&
1965 errno == ENOENT))
1966 goto done;
1968 got_fileindex_entry_remove(fileindex, ie);
1969 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE,
1970 ie->path);
1972 goto done; /* nothing else to do */
1975 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1976 (S_ISLNK(te->mode) ||
1977 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1979 * This is a regular file or an installed bad symlink.
1980 * If the file index indicates that this file is already
1981 * up-to-date with respect to the repository we can skip
1982 * updating contents of this file.
1984 if (got_fileindex_entry_has_commit(ie) &&
1985 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1986 SHA1_DIGEST_LENGTH) == 0) {
1987 /* Same commit. */
1988 err = sync_timestamps(worktree->root_fd,
1989 path, status, ie, &sb);
1990 if (err)
1991 goto done;
1992 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1993 path);
1994 goto done;
1996 if (got_fileindex_entry_has_blob(ie) &&
1997 memcmp(ie->blob_sha1, te->id.sha1,
1998 SHA1_DIGEST_LENGTH) == 0) {
1999 /* Different commit but the same blob. */
2000 err = sync_timestamps(worktree->root_fd,
2001 path, status, ie, &sb);
2002 if (err)
2003 goto done;
2004 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2005 path);
2006 goto done;
2010 fd1 = got_opentempfd();
2011 if (fd1 == -1) {
2012 err = got_error_from_errno("got_opentempfd");
2013 goto done;
2015 err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
2016 if (err)
2017 goto done;
2019 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
2020 int update_timestamps;
2021 struct got_blob_object *blob2 = NULL;
2022 char *label_orig = NULL;
2023 if (got_fileindex_entry_has_blob(ie)) {
2024 fd2 = got_opentempfd();
2025 if (fd2 == -1) {
2026 err = got_error_from_errno("got_opentempfd");
2027 goto done;
2029 struct got_object_id id2;
2030 got_fileindex_entry_get_blob_id(&id2, ie);
2031 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
2032 fd2);
2033 if (err)
2034 goto done;
2036 if (got_fileindex_entry_has_commit(ie)) {
2037 char id_str[SHA1_DIGEST_STRING_LENGTH];
2038 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2039 sizeof(id_str)) == NULL) {
2040 err = got_error_path(id_str,
2041 GOT_ERR_BAD_OBJ_ID_STR);
2042 goto done;
2044 if (asprintf(&label_orig, "%s: commit %s",
2045 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2046 err = got_error_from_errno("asprintf");
2047 goto done;
2050 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2051 char *link_target;
2052 err = got_object_blob_read_to_str(&link_target, blob);
2053 if (err)
2054 goto done;
2055 err = merge_symlink(worktree, blob2, ondisk_path, path,
2056 label_orig, link_target, worktree->base_commit_id,
2057 repo, progress_cb, progress_arg);
2058 free(link_target);
2059 } else {
2060 err = merge_blob(&update_timestamps, worktree, blob2,
2061 ondisk_path, path, sb.st_mode, label_orig, blob,
2062 worktree->base_commit_id, repo,
2063 progress_cb, progress_arg);
2065 free(label_orig);
2066 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2067 err = got_error_from_errno("close");
2068 goto done;
2070 if (blob2)
2071 got_object_blob_close(blob2);
2072 if (err)
2073 goto done;
2075 * Do not update timestamps of files with local changes.
2076 * Otherwise, a future status walk would treat them as
2077 * unmodified files again.
2079 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2080 blob->id.sha1, worktree->base_commit_id->sha1,
2081 update_timestamps);
2082 } else if (status == GOT_STATUS_MODE_CHANGE) {
2083 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2084 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2085 } else if (status == GOT_STATUS_DELETE) {
2086 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2087 if (err)
2088 goto done;
2089 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2090 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2091 if (err)
2092 goto done;
2093 } else {
2094 int is_bad_symlink = 0;
2095 if (S_ISLNK(te->mode)) {
2096 err = install_symlink(&is_bad_symlink, worktree,
2097 ondisk_path, path, blob,
2098 status == GOT_STATUS_MISSING, 0,
2099 status == GOT_STATUS_UNVERSIONED, 0,
2100 repo, progress_cb, progress_arg);
2101 } else {
2102 err = install_blob(worktree, ondisk_path, path,
2103 te->mode, sb.st_mode, blob,
2104 status == GOT_STATUS_MISSING, 0, 0,
2105 status == GOT_STATUS_UNVERSIONED, repo,
2106 progress_cb, progress_arg);
2108 if (err)
2109 goto done;
2111 if (ie) {
2112 err = got_fileindex_entry_update(ie,
2113 worktree->root_fd, path, blob->id.sha1,
2114 worktree->base_commit_id->sha1, 1);
2115 } else {
2116 err = create_fileindex_entry(&ie, fileindex,
2117 worktree->base_commit_id, worktree->root_fd, path,
2118 &blob->id);
2120 if (err)
2121 goto done;
2123 if (is_bad_symlink) {
2124 got_fileindex_entry_filetype_set(ie,
2125 GOT_FILEIDX_MODE_BAD_SYMLINK);
2129 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2130 err = got_error_from_errno("close");
2131 goto done;
2133 got_object_blob_close(blob);
2134 done:
2135 free(ondisk_path);
2136 return err;
2139 static const struct got_error *
2140 remove_ondisk_file(const char *root_path, const char *path)
2142 const struct got_error *err = NULL;
2143 char *ondisk_path = NULL, *parent = NULL;
2145 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2146 return got_error_from_errno("asprintf");
2148 if (unlink(ondisk_path) == -1) {
2149 if (errno != ENOENT)
2150 err = got_error_from_errno2("unlink", ondisk_path);
2151 } else {
2152 size_t root_len = strlen(root_path);
2153 err = got_path_dirname(&parent, ondisk_path);
2154 if (err)
2155 goto done;
2156 while (got_path_cmp(parent, root_path,
2157 strlen(parent), root_len) != 0) {
2158 free(ondisk_path);
2159 ondisk_path = parent;
2160 parent = NULL;
2161 if (rmdir(ondisk_path) == -1) {
2162 if (errno != ENOTEMPTY)
2163 err = got_error_from_errno2("rmdir",
2164 ondisk_path);
2165 break;
2167 err = got_path_dirname(&parent, ondisk_path);
2168 if (err)
2169 break;
2172 done:
2173 free(ondisk_path);
2174 free(parent);
2175 return err;
2178 static const struct got_error *
2179 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2180 struct got_fileindex_entry *ie, struct got_repository *repo,
2181 got_worktree_checkout_cb progress_cb, void *progress_arg)
2183 const struct got_error *err = NULL;
2184 unsigned char status;
2185 struct stat sb;
2186 char *ondisk_path;
2188 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2189 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2191 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2192 == -1)
2193 return got_error_from_errno("asprintf");
2195 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2196 if (err)
2197 goto done;
2199 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2200 char ondisk_target[PATH_MAX];
2201 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2202 sizeof(ondisk_target));
2203 if (ondisk_len == -1) {
2204 err = got_error_from_errno2("readlink", ondisk_path);
2205 goto done;
2207 ondisk_target[ondisk_len] = '\0';
2208 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2209 NULL, NULL, /* XXX pass common ancestor info? */
2210 ondisk_target, ondisk_path);
2211 if (err)
2212 goto done;
2213 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2214 ie->path);
2215 goto done;
2218 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2219 status == GOT_STATUS_ADD) {
2220 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2221 if (err)
2222 goto done;
2224 * Preserve the working file and change the deleted blob's
2225 * entry into a schedule-add entry.
2227 err = got_fileindex_entry_update(ie, worktree->root_fd,
2228 ie->path, NULL, NULL, 0);
2229 } else {
2230 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2231 if (err)
2232 goto done;
2233 if (status == GOT_STATUS_NO_CHANGE) {
2234 err = remove_ondisk_file(worktree->root_path, ie->path);
2235 if (err)
2236 goto done;
2238 got_fileindex_entry_remove(fileindex, ie);
2240 done:
2241 free(ondisk_path);
2242 return err;
2245 struct diff_cb_arg {
2246 struct got_fileindex *fileindex;
2247 struct got_worktree *worktree;
2248 struct got_repository *repo;
2249 got_worktree_checkout_cb progress_cb;
2250 void *progress_arg;
2251 got_cancel_cb cancel_cb;
2252 void *cancel_arg;
2255 static const struct got_error *
2256 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2257 struct got_tree_entry *te, const char *parent_path)
2259 struct diff_cb_arg *a = arg;
2261 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2262 return got_error(GOT_ERR_CANCELLED);
2264 return update_blob(a->worktree, a->fileindex, ie, te,
2265 ie->path, a->repo, a->progress_cb, a->progress_arg);
2268 static const struct got_error *
2269 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2271 struct diff_cb_arg *a = arg;
2273 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2274 return got_error(GOT_ERR_CANCELLED);
2276 return delete_blob(a->worktree, a->fileindex, ie,
2277 a->repo, a->progress_cb, a->progress_arg);
2280 static const struct got_error *
2281 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2283 struct diff_cb_arg *a = arg;
2284 const struct got_error *err;
2285 char *path;
2287 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2288 return got_error(GOT_ERR_CANCELLED);
2290 if (got_object_tree_entry_is_submodule(te))
2291 return NULL;
2293 if (asprintf(&path, "%s%s%s", parent_path,
2294 parent_path[0] ? "/" : "", te->name)
2295 == -1)
2296 return got_error_from_errno("asprintf");
2298 if (S_ISDIR(te->mode))
2299 err = add_dir_on_disk(a->worktree, path);
2300 else
2301 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2302 a->repo, a->progress_cb, a->progress_arg);
2304 free(path);
2305 return err;
2308 const struct got_error *
2309 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2311 uint32_t uuid_status;
2313 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2314 if (uuid_status != uuid_s_ok) {
2315 *uuidstr = NULL;
2316 return got_error_uuid(uuid_status, "uuid_to_string");
2319 return NULL;
2322 static const struct got_error *
2323 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2325 const struct got_error *err = NULL;
2326 char *uuidstr = NULL;
2328 *refname = NULL;
2330 err = got_worktree_get_uuid(&uuidstr, worktree);
2331 if (err)
2332 return err;
2334 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2335 err = got_error_from_errno("asprintf");
2336 *refname = NULL;
2338 free(uuidstr);
2339 return err;
2342 const struct got_error *
2343 got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2344 const char *prefix)
2346 return get_ref_name(refname, worktree, prefix);
2349 const struct got_error *
2350 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2352 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2355 static const struct got_error *
2356 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2358 return get_ref_name(refname, worktree,
2359 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2362 static const struct got_error *
2363 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2365 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2368 static const struct got_error *
2369 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2371 return get_ref_name(refname, worktree,
2372 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2375 static const struct got_error *
2376 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2378 return get_ref_name(refname, worktree,
2379 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2382 static const struct got_error *
2383 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2385 return get_ref_name(refname, worktree,
2386 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2389 static const struct got_error *
2390 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2392 return get_ref_name(refname, worktree,
2393 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2396 static const struct got_error *
2397 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2399 return get_ref_name(refname, worktree,
2400 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2403 static const struct got_error *
2404 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2406 return get_ref_name(refname, worktree,
2407 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2410 const struct got_error *
2411 got_worktree_get_histedit_script_path(char **path,
2412 struct got_worktree *worktree)
2414 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2415 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2416 *path = NULL;
2417 return got_error_from_errno("asprintf");
2419 return NULL;
2422 static const struct got_error *
2423 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2425 return get_ref_name(refname, worktree,
2426 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2429 static const struct got_error *
2430 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2432 return get_ref_name(refname, worktree,
2433 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2437 * Prevent Git's garbage collector from deleting our base commit by
2438 * setting a reference to our base commit's ID.
2440 static const struct got_error *
2441 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2443 const struct got_error *err = NULL;
2444 struct got_reference *ref = NULL;
2445 char *refname;
2447 err = got_worktree_get_base_ref_name(&refname, worktree);
2448 if (err)
2449 return err;
2451 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2452 if (err)
2453 goto done;
2455 err = got_ref_write(ref, repo);
2456 done:
2457 free(refname);
2458 if (ref)
2459 got_ref_close(ref);
2460 return err;
2463 static const struct got_error *
2464 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2466 const struct got_error *err = NULL;
2468 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2469 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2470 err = got_error_from_errno("asprintf");
2471 *fileindex_path = NULL;
2473 return err;
2477 static const struct got_error *
2478 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2479 struct got_worktree *worktree)
2481 const struct got_error *err = NULL;
2482 FILE *index = NULL;
2484 *fileindex_path = NULL;
2485 *fileindex = got_fileindex_alloc();
2486 if (*fileindex == NULL)
2487 return got_error_from_errno("got_fileindex_alloc");
2489 err = get_fileindex_path(fileindex_path, worktree);
2490 if (err)
2491 goto done;
2493 index = fopen(*fileindex_path, "rbe");
2494 if (index == NULL) {
2495 if (errno != ENOENT)
2496 err = got_error_from_errno2("fopen", *fileindex_path);
2497 } else {
2498 err = got_fileindex_read(*fileindex, index);
2499 if (fclose(index) == EOF && err == NULL)
2500 err = got_error_from_errno("fclose");
2502 done:
2503 if (err) {
2504 free(*fileindex_path);
2505 *fileindex_path = NULL;
2506 got_fileindex_free(*fileindex);
2507 *fileindex = NULL;
2509 return err;
2512 struct bump_base_commit_id_arg {
2513 struct got_object_id *base_commit_id;
2514 const char *path;
2515 size_t path_len;
2516 const char *entry_name;
2517 got_worktree_checkout_cb progress_cb;
2518 void *progress_arg;
2521 static const struct got_error *
2522 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2524 const struct got_error *err;
2525 struct bump_base_commit_id_arg *a = arg;
2527 if (a->entry_name) {
2528 if (strcmp(ie->path, a->path) != 0)
2529 return NULL;
2530 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2531 return NULL;
2533 if (got_fileindex_entry_was_skipped(ie))
2534 return NULL;
2536 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2537 SHA1_DIGEST_LENGTH) == 0)
2538 return NULL;
2540 if (a->progress_cb) {
2541 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2542 ie->path);
2543 if (err)
2544 return err;
2546 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2547 return NULL;
2550 /* Bump base commit ID of all files within an updated part of the work tree. */
2551 static const struct got_error *
2552 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2553 struct got_fileindex *fileindex,
2554 got_worktree_checkout_cb progress_cb, void *progress_arg)
2556 struct bump_base_commit_id_arg bbc_arg;
2558 bbc_arg.base_commit_id = worktree->base_commit_id;
2559 bbc_arg.entry_name = NULL;
2560 bbc_arg.path = "";
2561 bbc_arg.path_len = 0;
2562 bbc_arg.progress_cb = progress_cb;
2563 bbc_arg.progress_arg = progress_arg;
2565 return got_fileindex_for_each_entry_safe(fileindex,
2566 bump_base_commit_id, &bbc_arg);
2569 static const struct got_error *
2570 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2572 const struct got_error *err = NULL;
2573 char *new_fileindex_path = NULL;
2574 FILE *new_index = NULL;
2575 struct timespec timeout;
2577 err = got_opentemp_named(&new_fileindex_path, &new_index,
2578 fileindex_path, "");
2579 if (err)
2580 goto done;
2582 err = got_fileindex_write(fileindex, new_index);
2583 if (err)
2584 goto done;
2586 if (rename(new_fileindex_path, fileindex_path) != 0) {
2587 err = got_error_from_errno3("rename", new_fileindex_path,
2588 fileindex_path);
2589 unlink(new_fileindex_path);
2593 * Sleep for a short amount of time to ensure that files modified after
2594 * this program exits have a different time stamp from the one which
2595 * was recorded in the file index.
2597 timeout.tv_sec = 0;
2598 timeout.tv_nsec = 1;
2599 nanosleep(&timeout, NULL);
2600 done:
2601 if (new_index)
2602 fclose(new_index);
2603 free(new_fileindex_path);
2604 return err;
2607 static const struct got_error *
2608 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2609 struct got_object_id **tree_id, const char *wt_relpath,
2610 struct got_commit_object *base_commit, struct got_worktree *worktree,
2611 struct got_repository *repo)
2613 const struct got_error *err = NULL;
2614 struct got_object_id *id = NULL;
2615 char *in_repo_path = NULL;
2616 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2618 *entry_type = GOT_OBJ_TYPE_ANY;
2619 *tree_relpath = NULL;
2620 *tree_id = NULL;
2622 if (wt_relpath[0] == '\0') {
2623 /* Check out all files within the work tree. */
2624 *entry_type = GOT_OBJ_TYPE_TREE;
2625 *tree_relpath = strdup("");
2626 if (*tree_relpath == NULL) {
2627 err = got_error_from_errno("strdup");
2628 goto done;
2630 err = got_object_id_by_path(tree_id, repo, base_commit,
2631 worktree->path_prefix);
2632 if (err)
2633 goto done;
2634 return NULL;
2637 /* Check out a subset of files in the work tree. */
2639 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2640 is_root_wt ? "" : "/", wt_relpath) == -1) {
2641 err = got_error_from_errno("asprintf");
2642 goto done;
2645 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2646 if (err)
2647 goto done;
2649 free(in_repo_path);
2650 in_repo_path = NULL;
2652 err = got_object_get_type(entry_type, repo, id);
2653 if (err)
2654 goto done;
2656 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2657 /* Check out a single file. */
2658 if (strchr(wt_relpath, '/') == NULL) {
2659 /* Check out a single file in work tree's root dir. */
2660 in_repo_path = strdup(worktree->path_prefix);
2661 if (in_repo_path == NULL) {
2662 err = got_error_from_errno("strdup");
2663 goto done;
2665 *tree_relpath = strdup("");
2666 if (*tree_relpath == NULL) {
2667 err = got_error_from_errno("strdup");
2668 goto done;
2670 } else {
2671 /* Check out a single file in a subdirectory. */
2672 err = got_path_dirname(tree_relpath, wt_relpath);
2673 if (err)
2674 return err;
2675 if (asprintf(&in_repo_path, "%s%s%s",
2676 worktree->path_prefix, is_root_wt ? "" : "/",
2677 *tree_relpath) == -1) {
2678 err = got_error_from_errno("asprintf");
2679 goto done;
2682 err = got_object_id_by_path(tree_id, repo,
2683 base_commit, in_repo_path);
2684 } else {
2685 /* Check out all files within a subdirectory. */
2686 *tree_id = got_object_id_dup(id);
2687 if (*tree_id == NULL) {
2688 err = got_error_from_errno("got_object_id_dup");
2689 goto done;
2691 *tree_relpath = strdup(wt_relpath);
2692 if (*tree_relpath == NULL) {
2693 err = got_error_from_errno("strdup");
2694 goto done;
2697 done:
2698 free(id);
2699 free(in_repo_path);
2700 if (err) {
2701 *entry_type = GOT_OBJ_TYPE_ANY;
2702 free(*tree_relpath);
2703 *tree_relpath = NULL;
2704 free(*tree_id);
2705 *tree_id = NULL;
2707 return err;
2710 static const struct got_error *
2711 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2712 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2713 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2714 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2716 const struct got_error *err = NULL;
2717 struct got_commit_object *commit = NULL;
2718 struct got_tree_object *tree = NULL;
2719 struct got_fileindex_diff_tree_cb diff_cb;
2720 struct diff_cb_arg arg;
2722 err = ref_base_commit(worktree, repo);
2723 if (err) {
2724 if (!(err->code == GOT_ERR_ERRNO &&
2725 (errno == EACCES || errno == EROFS)))
2726 goto done;
2727 err = (*progress_cb)(progress_arg,
2728 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2729 if (err)
2730 return err;
2733 err = got_object_open_as_commit(&commit, repo,
2734 worktree->base_commit_id);
2735 if (err)
2736 goto done;
2738 err = got_object_open_as_tree(&tree, repo, tree_id);
2739 if (err)
2740 goto done;
2742 if (entry_name &&
2743 got_object_tree_find_entry(tree, entry_name) == NULL) {
2744 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2745 goto done;
2748 diff_cb.diff_old_new = diff_old_new;
2749 diff_cb.diff_old = diff_old;
2750 diff_cb.diff_new = diff_new;
2751 arg.fileindex = fileindex;
2752 arg.worktree = worktree;
2753 arg.repo = repo;
2754 arg.progress_cb = progress_cb;
2755 arg.progress_arg = progress_arg;
2756 arg.cancel_cb = cancel_cb;
2757 arg.cancel_arg = cancel_arg;
2758 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2759 entry_name, repo, &diff_cb, &arg);
2760 done:
2761 if (tree)
2762 got_object_tree_close(tree);
2763 if (commit)
2764 got_object_commit_close(commit);
2765 return err;
2768 const struct got_error *
2769 got_worktree_checkout_files(struct got_worktree *worktree,
2770 struct got_pathlist_head *paths, struct got_repository *repo,
2771 got_worktree_checkout_cb progress_cb, void *progress_arg,
2772 got_cancel_cb cancel_cb, void *cancel_arg)
2774 const struct got_error *err = NULL, *sync_err, *unlockerr;
2775 struct got_commit_object *commit = NULL;
2776 struct got_tree_object *tree = NULL;
2777 struct got_fileindex *fileindex = NULL;
2778 char *fileindex_path = NULL;
2779 struct got_pathlist_entry *pe;
2780 struct tree_path_data {
2781 STAILQ_ENTRY(tree_path_data) entry;
2782 struct got_object_id *tree_id;
2783 int entry_type;
2784 char *relpath;
2785 char *entry_name;
2786 } *tpd = NULL;
2787 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2789 STAILQ_INIT(&tree_paths);
2791 err = lock_worktree(worktree, LOCK_EX);
2792 if (err)
2793 return err;
2795 err = got_object_open_as_commit(&commit, repo,
2796 worktree->base_commit_id);
2797 if (err)
2798 goto done;
2800 /* Map all specified paths to in-repository trees. */
2801 TAILQ_FOREACH(pe, paths, entry) {
2802 tpd = malloc(sizeof(*tpd));
2803 if (tpd == NULL) {
2804 err = got_error_from_errno("malloc");
2805 goto done;
2808 err = find_tree_entry_for_checkout(&tpd->entry_type,
2809 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2810 worktree, repo);
2811 if (err) {
2812 free(tpd);
2813 goto done;
2816 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2817 err = got_path_basename(&tpd->entry_name, pe->path);
2818 if (err) {
2819 free(tpd->relpath);
2820 free(tpd->tree_id);
2821 free(tpd);
2822 goto done;
2824 } else
2825 tpd->entry_name = NULL;
2827 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2831 * Read the file index.
2832 * Checking out files is supposed to be an idempotent operation.
2833 * If the on-disk file index is incomplete we will try to complete it.
2835 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2836 if (err)
2837 goto done;
2839 tpd = STAILQ_FIRST(&tree_paths);
2840 TAILQ_FOREACH(pe, paths, entry) {
2841 struct bump_base_commit_id_arg bbc_arg;
2843 err = checkout_files(worktree, fileindex, tpd->relpath,
2844 tpd->tree_id, tpd->entry_name, repo,
2845 progress_cb, progress_arg, cancel_cb, cancel_arg);
2846 if (err)
2847 break;
2849 bbc_arg.base_commit_id = worktree->base_commit_id;
2850 bbc_arg.entry_name = tpd->entry_name;
2851 bbc_arg.path = pe->path;
2852 bbc_arg.path_len = pe->path_len;
2853 bbc_arg.progress_cb = progress_cb;
2854 bbc_arg.progress_arg = progress_arg;
2855 err = got_fileindex_for_each_entry_safe(fileindex,
2856 bump_base_commit_id, &bbc_arg);
2857 if (err)
2858 break;
2860 tpd = STAILQ_NEXT(tpd, entry);
2862 sync_err = sync_fileindex(fileindex, fileindex_path);
2863 if (sync_err && err == NULL)
2864 err = sync_err;
2865 done:
2866 free(fileindex_path);
2867 if (tree)
2868 got_object_tree_close(tree);
2869 if (commit)
2870 got_object_commit_close(commit);
2871 if (fileindex)
2872 got_fileindex_free(fileindex);
2873 while (!STAILQ_EMPTY(&tree_paths)) {
2874 tpd = STAILQ_FIRST(&tree_paths);
2875 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2876 free(tpd->relpath);
2877 free(tpd->tree_id);
2878 free(tpd);
2880 unlockerr = lock_worktree(worktree, LOCK_SH);
2881 if (unlockerr && err == NULL)
2882 err = unlockerr;
2883 return err;
2886 static const struct got_error *
2887 add_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2888 struct got_fileindex_entry *ie, const char *ondisk_path,
2889 const char *path2, struct got_blob_object *blob2, mode_t mode2,
2890 int restoring_missing_file, int reverting_versioned_file,
2891 int path_is_unversioned, int allow_bad_symlinks,
2892 struct got_repository *repo,
2893 got_worktree_checkout_cb progress_cb, void *progress_arg)
2895 const struct got_error *err = NULL;
2896 int is_bad_symlink = 0;
2898 if (S_ISLNK(mode2)) {
2899 err = install_symlink(&is_bad_symlink,
2900 worktree, ondisk_path, path2, blob2,
2901 restoring_missing_file,
2902 reverting_versioned_file,
2903 path_is_unversioned, allow_bad_symlinks,
2904 repo, progress_cb, progress_arg);
2905 } else {
2906 err = install_blob(worktree, ondisk_path, path2,
2907 mode2, GOT_DEFAULT_FILE_MODE, blob2,
2908 restoring_missing_file, reverting_versioned_file, 0,
2909 path_is_unversioned, repo, progress_cb, progress_arg);
2911 if (err)
2912 return err;
2913 if (ie == NULL) {
2914 /* Adding an unversioned file. */
2915 err = got_fileindex_entry_alloc(&ie, path2);
2916 if (err)
2917 return err;
2918 err = got_fileindex_entry_update(ie,
2919 worktree->root_fd, path2, NULL, NULL, 1);
2920 if (err) {
2921 got_fileindex_entry_free(ie);
2922 return err;
2924 err = got_fileindex_entry_add(fileindex, ie);
2925 if (err) {
2926 got_fileindex_entry_free(ie);
2927 return err;
2929 } else {
2930 /* Re-adding a locally deleted file. */
2931 err = got_fileindex_entry_update(ie,
2932 worktree->root_fd, path2, ie->blob_sha1,
2933 worktree->base_commit_id->sha1, 0);
2934 if (err)
2935 return err;
2938 if (is_bad_symlink) {
2939 got_fileindex_entry_filetype_set(ie,
2940 GOT_FILEIDX_MODE_BAD_SYMLINK);
2943 return NULL;
2946 struct merge_file_cb_arg {
2947 struct got_worktree *worktree;
2948 struct got_fileindex *fileindex;
2949 got_worktree_checkout_cb progress_cb;
2950 void *progress_arg;
2951 got_cancel_cb cancel_cb;
2952 void *cancel_arg;
2953 const char *label_orig;
2954 struct got_object_id *commit_id2;
2955 int allow_bad_symlinks;
2958 static const struct got_error *
2959 merge_file_cb(void *arg, struct got_blob_object *blob1,
2960 struct got_blob_object *blob2, FILE *f1, FILE *f2,
2961 struct got_object_id *id1, struct got_object_id *id2,
2962 const char *path1, const char *path2,
2963 mode_t mode1, mode_t mode2, struct got_repository *repo)
2965 static const struct got_error *err = NULL;
2966 struct merge_file_cb_arg *a = arg;
2967 struct got_fileindex_entry *ie;
2968 char *ondisk_path = NULL;
2969 struct stat sb;
2970 unsigned char status;
2971 int local_changes_subsumed;
2972 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2973 char *id_str = NULL, *label_deriv2 = NULL;
2975 if (blob1 && blob2) {
2976 ie = got_fileindex_entry_get(a->fileindex, path2,
2977 strlen(path2));
2978 if (ie == NULL)
2979 return (*a->progress_cb)(a->progress_arg,
2980 GOT_STATUS_MISSING, path2);
2982 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2983 path2) == -1)
2984 return got_error_from_errno("asprintf");
2986 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2987 repo);
2988 if (err)
2989 goto done;
2991 if (status == GOT_STATUS_DELETE) {
2992 err = (*a->progress_cb)(a->progress_arg,
2993 GOT_STATUS_MERGE, path2);
2994 goto done;
2996 if (status != GOT_STATUS_NO_CHANGE &&
2997 status != GOT_STATUS_MODIFY &&
2998 status != GOT_STATUS_CONFLICT &&
2999 status != GOT_STATUS_ADD) {
3000 err = (*a->progress_cb)(a->progress_arg, status, path2);
3001 goto done;
3004 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
3005 char *link_target2;
3006 err = got_object_blob_read_to_str(&link_target2, blob2);
3007 if (err)
3008 goto done;
3009 err = merge_symlink(a->worktree, blob1, ondisk_path,
3010 path2, a->label_orig, link_target2, a->commit_id2,
3011 repo, a->progress_cb, a->progress_arg);
3012 free(link_target2);
3013 } else {
3014 int fd;
3016 f_orig = got_opentemp();
3017 if (f_orig == NULL) {
3018 err = got_error_from_errno("got_opentemp");
3019 goto done;
3021 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3022 f_orig, blob1);
3023 if (err)
3024 goto done;
3026 f_deriv2 = got_opentemp();
3027 if (f_deriv2 == NULL)
3028 goto done;
3029 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3030 f_deriv2, blob2);
3031 if (err)
3032 goto done;
3034 fd = open(ondisk_path,
3035 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3036 if (fd == -1) {
3037 err = got_error_from_errno2("open",
3038 ondisk_path);
3039 goto done;
3041 f_deriv = fdopen(fd, "r");
3042 if (f_deriv == NULL) {
3043 err = got_error_from_errno2("fdopen",
3044 ondisk_path);
3045 close(fd);
3046 goto done;
3048 err = got_object_id_str(&id_str, a->commit_id2);
3049 if (err)
3050 goto done;
3051 if (asprintf(&label_deriv2, "%s: commit %s",
3052 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
3053 err = got_error_from_errno("asprintf");
3054 goto done;
3056 err = merge_file(&local_changes_subsumed, a->worktree,
3057 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
3058 mode2, a->label_orig, NULL, label_deriv2,
3059 GOT_DIFF_ALGORITHM_PATIENCE, repo,
3060 a->progress_cb, a->progress_arg);
3062 } else if (blob1) {
3063 ie = got_fileindex_entry_get(a->fileindex, path1,
3064 strlen(path1));
3065 if (ie == NULL)
3066 return (*a->progress_cb)(a->progress_arg,
3067 GOT_STATUS_MISSING, path1);
3069 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3070 path1) == -1)
3071 return got_error_from_errno("asprintf");
3073 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3074 repo);
3075 if (err)
3076 goto done;
3078 switch (status) {
3079 case GOT_STATUS_NO_CHANGE:
3080 err = (*a->progress_cb)(a->progress_arg,
3081 GOT_STATUS_DELETE, path1);
3082 if (err)
3083 goto done;
3084 err = remove_ondisk_file(a->worktree->root_path, path1);
3085 if (err)
3086 goto done;
3087 if (ie)
3088 got_fileindex_entry_mark_deleted_from_disk(ie);
3089 break;
3090 case GOT_STATUS_DELETE:
3091 case GOT_STATUS_MISSING:
3092 err = (*a->progress_cb)(a->progress_arg,
3093 GOT_STATUS_DELETE, path1);
3094 if (err)
3095 goto done;
3096 if (ie)
3097 got_fileindex_entry_mark_deleted_from_disk(ie);
3098 break;
3099 case GOT_STATUS_ADD: {
3100 struct got_object_id *id;
3101 FILE *blob1_f;
3102 off_t blob1_size;
3104 * Delete the added file only if its content already
3105 * exists in the repository.
3107 err = got_object_blob_file_create(&id, &blob1_f,
3108 &blob1_size, path1);
3109 if (err)
3110 goto done;
3111 if (got_object_id_cmp(id, id1) == 0) {
3112 err = (*a->progress_cb)(a->progress_arg,
3113 GOT_STATUS_DELETE, path1);
3114 if (err)
3115 goto done;
3116 err = remove_ondisk_file(a->worktree->root_path,
3117 path1);
3118 if (err)
3119 goto done;
3120 if (ie)
3121 got_fileindex_entry_remove(a->fileindex,
3122 ie);
3123 } else {
3124 err = (*a->progress_cb)(a->progress_arg,
3125 GOT_STATUS_CANNOT_DELETE, path1);
3127 if (fclose(blob1_f) == EOF && err == NULL)
3128 err = got_error_from_errno("fclose");
3129 free(id);
3130 if (err)
3131 goto done;
3132 break;
3134 case GOT_STATUS_MODIFY:
3135 case GOT_STATUS_CONFLICT:
3136 err = (*a->progress_cb)(a->progress_arg,
3137 GOT_STATUS_CANNOT_DELETE, path1);
3138 if (err)
3139 goto done;
3140 break;
3141 case GOT_STATUS_OBSTRUCTED:
3142 err = (*a->progress_cb)(a->progress_arg, status, path1);
3143 if (err)
3144 goto done;
3145 break;
3146 default:
3147 break;
3149 } else if (blob2) {
3150 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3151 path2) == -1)
3152 return got_error_from_errno("asprintf");
3153 ie = got_fileindex_entry_get(a->fileindex, path2,
3154 strlen(path2));
3155 if (ie) {
3156 err = get_file_status(&status, &sb, ie, ondisk_path,
3157 -1, NULL, repo);
3158 if (err)
3159 goto done;
3160 if (status != GOT_STATUS_NO_CHANGE &&
3161 status != GOT_STATUS_MODIFY &&
3162 status != GOT_STATUS_CONFLICT &&
3163 status != GOT_STATUS_ADD &&
3164 status != GOT_STATUS_DELETE) {
3165 err = (*a->progress_cb)(a->progress_arg,
3166 status, path2);
3167 goto done;
3169 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3170 char *link_target2;
3171 err = got_object_blob_read_to_str(&link_target2,
3172 blob2);
3173 if (err)
3174 goto done;
3175 err = merge_symlink(a->worktree, NULL,
3176 ondisk_path, path2, a->label_orig,
3177 link_target2, a->commit_id2, repo,
3178 a->progress_cb, a->progress_arg);
3179 free(link_target2);
3180 } else if (S_ISREG(sb.st_mode)) {
3181 err = merge_blob(&local_changes_subsumed,
3182 a->worktree, NULL, ondisk_path, path2,
3183 sb.st_mode, a->label_orig, blob2,
3184 a->commit_id2, repo, a->progress_cb,
3185 a->progress_arg);
3186 } else if (status != GOT_STATUS_DELETE) {
3187 err = got_error_path(ondisk_path,
3188 GOT_ERR_FILE_OBSTRUCTED);
3190 if (err)
3191 goto done;
3192 if (status == GOT_STATUS_DELETE) {
3193 /* Re-add file with content from new blob. */
3194 err = add_file(a->worktree, a->fileindex, ie,
3195 ondisk_path, path2, blob2, mode2,
3196 0, 0, 0, a->allow_bad_symlinks,
3197 repo, a->progress_cb, a->progress_arg);
3198 if (err)
3199 goto done;
3201 } else {
3202 err = add_file(a->worktree, a->fileindex, NULL,
3203 ondisk_path, path2, blob2, mode2,
3204 0, 0, 1, a->allow_bad_symlinks,
3205 repo, a->progress_cb, a->progress_arg);
3206 if (err)
3207 goto done;
3210 done:
3211 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3212 err = got_error_from_errno("fclose");
3213 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3214 err = got_error_from_errno("fclose");
3215 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3216 err = got_error_from_errno("fclose");
3217 free(id_str);
3218 free(label_deriv2);
3219 free(ondisk_path);
3220 return err;
3223 static const struct got_error *
3224 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3226 struct got_worktree *worktree = arg;
3228 /* Reject merges into a work tree with mixed base commits. */
3229 if (got_fileindex_entry_has_commit(ie) &&
3230 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3231 SHA1_DIGEST_LENGTH) != 0)
3232 return got_error(GOT_ERR_MIXED_COMMITS);
3234 return NULL;
3237 struct check_merge_conflicts_arg {
3238 struct got_worktree *worktree;
3239 struct got_fileindex *fileindex;
3240 struct got_repository *repo;
3243 static const struct got_error *
3244 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3245 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3246 struct got_object_id *id1, struct got_object_id *id2,
3247 const char *path1, const char *path2,
3248 mode_t mode1, mode_t mode2, struct got_repository *repo)
3250 const struct got_error *err = NULL;
3251 struct check_merge_conflicts_arg *a = arg;
3252 unsigned char status;
3253 struct stat sb;
3254 struct got_fileindex_entry *ie;
3255 const char *path = path2 ? path2 : path1;
3256 struct got_object_id *id = id2 ? id2 : id1;
3257 char *ondisk_path;
3259 if (id == NULL)
3260 return NULL;
3262 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3263 if (ie == NULL)
3264 return NULL;
3266 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3267 == -1)
3268 return got_error_from_errno("asprintf");
3270 /* Reject merges into a work tree with conflicted files. */
3271 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3272 free(ondisk_path);
3273 if (err)
3274 return err;
3275 if (status == GOT_STATUS_CONFLICT)
3276 return got_error(GOT_ERR_CONFLICTS);
3278 return NULL;
3281 static const struct got_error *
3282 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3283 const char *fileindex_path, struct got_object_id *commit_id1,
3284 struct got_object_id *commit_id2, struct got_repository *repo,
3285 got_worktree_checkout_cb progress_cb, void *progress_arg,
3286 got_cancel_cb cancel_cb, void *cancel_arg)
3288 const struct got_error *err = NULL, *sync_err;
3289 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3290 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3291 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3292 struct check_merge_conflicts_arg cmc_arg;
3293 struct merge_file_cb_arg arg;
3294 char *label_orig = NULL;
3295 FILE *f1 = NULL, *f2 = NULL;
3296 int fd1 = -1, fd2 = -1;
3298 if (commit_id1) {
3299 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3300 if (err)
3301 goto done;
3302 err = got_object_id_by_path(&tree_id1, repo, commit1,
3303 worktree->path_prefix);
3304 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3305 goto done;
3307 if (tree_id1) {
3308 char *id_str;
3310 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3311 if (err)
3312 goto done;
3314 err = got_object_id_str(&id_str, commit_id1);
3315 if (err)
3316 goto done;
3318 if (asprintf(&label_orig, "%s: commit %s",
3319 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3320 err = got_error_from_errno("asprintf");
3321 free(id_str);
3322 goto done;
3324 free(id_str);
3326 f1 = got_opentemp();
3327 if (f1 == NULL) {
3328 err = got_error_from_errno("got_opentemp");
3329 goto done;
3333 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3334 if (err)
3335 goto done;
3337 err = got_object_id_by_path(&tree_id2, repo, commit2,
3338 worktree->path_prefix);
3339 if (err)
3340 goto done;
3342 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3343 if (err)
3344 goto done;
3346 f2 = got_opentemp();
3347 if (f2 == NULL) {
3348 err = got_error_from_errno("got_opentemp");
3349 goto done;
3352 fd1 = got_opentempfd();
3353 if (fd1 == -1) {
3354 err = got_error_from_errno("got_opentempfd");
3355 goto done;
3358 fd2 = got_opentempfd();
3359 if (fd2 == -1) {
3360 err = got_error_from_errno("got_opentempfd");
3361 goto done;
3364 cmc_arg.worktree = worktree;
3365 cmc_arg.fileindex = fileindex;
3366 cmc_arg.repo = repo;
3367 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3368 check_merge_conflicts, &cmc_arg, 0);
3369 if (err)
3370 goto done;
3372 arg.worktree = worktree;
3373 arg.fileindex = fileindex;
3374 arg.progress_cb = progress_cb;
3375 arg.progress_arg = progress_arg;
3376 arg.cancel_cb = cancel_cb;
3377 arg.cancel_arg = cancel_arg;
3378 arg.label_orig = label_orig;
3379 arg.commit_id2 = commit_id2;
3380 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3381 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3382 merge_file_cb, &arg, 1);
3383 sync_err = sync_fileindex(fileindex, fileindex_path);
3384 if (sync_err && err == NULL)
3385 err = sync_err;
3386 done:
3387 if (commit1)
3388 got_object_commit_close(commit1);
3389 if (commit2)
3390 got_object_commit_close(commit2);
3391 if (tree1)
3392 got_object_tree_close(tree1);
3393 if (tree2)
3394 got_object_tree_close(tree2);
3395 if (f1 && fclose(f1) == EOF && err == NULL)
3396 err = got_error_from_errno("fclose");
3397 if (f2 && fclose(f2) == EOF && err == NULL)
3398 err = got_error_from_errno("fclose");
3399 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3400 err = got_error_from_errno("close");
3401 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3402 err = got_error_from_errno("close");
3403 free(label_orig);
3404 return err;
3407 const struct got_error *
3408 got_worktree_merge_files(struct got_worktree *worktree,
3409 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3410 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3411 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3413 const struct got_error *err, *unlockerr;
3414 char *fileindex_path = NULL;
3415 struct got_fileindex *fileindex = NULL;
3417 err = lock_worktree(worktree, LOCK_EX);
3418 if (err)
3419 return err;
3421 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3422 if (err)
3423 goto done;
3425 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3426 worktree);
3427 if (err)
3428 goto done;
3430 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3431 commit_id2, repo, progress_cb, progress_arg,
3432 cancel_cb, cancel_arg);
3433 done:
3434 if (fileindex)
3435 got_fileindex_free(fileindex);
3436 free(fileindex_path);
3437 unlockerr = lock_worktree(worktree, LOCK_SH);
3438 if (unlockerr && err == NULL)
3439 err = unlockerr;
3440 return err;
3443 struct diff_dir_cb_arg {
3444 struct got_fileindex *fileindex;
3445 struct got_worktree *worktree;
3446 const char *status_path;
3447 size_t status_path_len;
3448 struct got_repository *repo;
3449 got_worktree_status_cb status_cb;
3450 void *status_arg;
3451 got_cancel_cb cancel_cb;
3452 void *cancel_arg;
3453 /* A pathlist containing per-directory pathlists of ignore patterns. */
3454 struct got_pathlist_head *ignores;
3455 int report_unchanged;
3456 int no_ignores;
3459 static const struct got_error *
3460 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3461 int dirfd, const char *de_name,
3462 got_worktree_status_cb status_cb, void *status_arg,
3463 struct got_repository *repo, int report_unchanged)
3465 const struct got_error *err = NULL;
3466 unsigned char status = GOT_STATUS_NO_CHANGE;
3467 unsigned char staged_status;
3468 struct stat sb;
3469 struct got_object_id blob_id, commit_id, staged_blob_id;
3470 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3471 struct got_object_id *staged_blob_idp = NULL;
3473 staged_status = get_staged_status(ie);
3474 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3475 if (err)
3476 return err;
3478 if (status == GOT_STATUS_NO_CHANGE &&
3479 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3480 return NULL;
3482 if (got_fileindex_entry_has_blob(ie))
3483 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3484 if (got_fileindex_entry_has_commit(ie))
3485 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3486 if (staged_status == GOT_STATUS_ADD ||
3487 staged_status == GOT_STATUS_MODIFY) {
3488 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3489 &staged_blob_id, ie);
3492 return (*status_cb)(status_arg, status, staged_status,
3493 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3496 static const struct got_error *
3497 status_old_new(void *arg, struct got_fileindex_entry *ie,
3498 struct dirent *de, const char *parent_path, int dirfd)
3500 const struct got_error *err = NULL;
3501 struct diff_dir_cb_arg *a = arg;
3502 char *abspath;
3504 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3505 return got_error(GOT_ERR_CANCELLED);
3507 if (got_path_cmp(parent_path, a->status_path,
3508 strlen(parent_path), a->status_path_len) != 0 &&
3509 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3510 return NULL;
3512 if (parent_path[0]) {
3513 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3514 parent_path, de->d_name) == -1)
3515 return got_error_from_errno("asprintf");
3516 } else {
3517 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3518 de->d_name) == -1)
3519 return got_error_from_errno("asprintf");
3522 err = report_file_status(ie, abspath, dirfd, de->d_name,
3523 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3524 free(abspath);
3525 return err;
3528 static const struct got_error *
3529 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3531 struct diff_dir_cb_arg *a = arg;
3532 struct got_object_id blob_id, commit_id;
3533 unsigned char status;
3535 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3536 return got_error(GOT_ERR_CANCELLED);
3538 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3539 return NULL;
3541 got_fileindex_entry_get_blob_id(&blob_id, ie);
3542 got_fileindex_entry_get_commit_id(&commit_id, ie);
3543 if (got_fileindex_entry_has_file_on_disk(ie))
3544 status = GOT_STATUS_MISSING;
3545 else
3546 status = GOT_STATUS_DELETE;
3547 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3548 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3551 static void
3552 free_ignores(struct got_pathlist_head *ignores)
3554 struct got_pathlist_entry *pe;
3556 TAILQ_FOREACH(pe, ignores, entry) {
3557 struct got_pathlist_head *ignorelist = pe->data;
3559 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3561 got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3564 static const struct got_error *
3565 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3567 const struct got_error *err = NULL;
3568 struct got_pathlist_entry *pe = NULL;
3569 struct got_pathlist_head *ignorelist;
3570 char *line = NULL, *pattern, *dirpath = NULL;
3571 size_t linesize = 0;
3572 ssize_t linelen;
3574 ignorelist = calloc(1, sizeof(*ignorelist));
3575 if (ignorelist == NULL)
3576 return got_error_from_errno("calloc");
3577 TAILQ_INIT(ignorelist);
3579 while ((linelen = getline(&line, &linesize, f)) != -1) {
3580 if (linelen > 0 && line[linelen - 1] == '\n')
3581 line[linelen - 1] = '\0';
3583 /* Git's ignores may contain comments. */
3584 if (line[0] == '#')
3585 continue;
3587 /* Git's negated patterns are not (yet?) supported. */
3588 if (line[0] == '!')
3589 continue;
3591 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3592 line) == -1) {
3593 err = got_error_from_errno("asprintf");
3594 goto done;
3596 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3597 if (err)
3598 goto done;
3600 if (ferror(f)) {
3601 err = got_error_from_errno("getline");
3602 goto done;
3605 dirpath = strdup(path);
3606 if (dirpath == NULL) {
3607 err = got_error_from_errno("strdup");
3608 goto done;
3610 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3611 done:
3612 free(line);
3613 if (err || pe == NULL) {
3614 free(dirpath);
3615 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3617 return err;
3620 static int
3621 match_path(const char *pattern, size_t pattern_len, const char *path,
3622 int flags)
3624 char buf[PATH_MAX];
3627 * Trailing slashes signify directories.
3628 * Append a * to make such patterns conform to fnmatch rules.
3630 if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3631 if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3632 return FNM_NOMATCH; /* XXX */
3634 return fnmatch(buf, path, flags);
3637 return fnmatch(pattern, path, flags);
3640 static int
3641 match_ignores(struct got_pathlist_head *ignores, const char *path)
3643 struct got_pathlist_entry *pe;
3645 /* Handle patterns which match in all directories. */
3646 TAILQ_FOREACH(pe, ignores, entry) {
3647 struct got_pathlist_head *ignorelist = pe->data;
3648 struct got_pathlist_entry *pi;
3650 TAILQ_FOREACH(pi, ignorelist, entry) {
3651 const char *p;
3653 if (pi->path_len < 3 ||
3654 strncmp(pi->path, "**/", 3) != 0)
3655 continue;
3656 p = path;
3657 while (*p) {
3658 if (match_path(pi->path + 3,
3659 pi->path_len - 3, p,
3660 FNM_PATHNAME | FNM_LEADING_DIR)) {
3661 /* Retry in next directory. */
3662 while (*p && *p != '/')
3663 p++;
3664 while (*p == '/')
3665 p++;
3666 continue;
3668 return 1;
3674 * The ignores pathlist contains ignore lists from children before
3675 * parents, so we can find the most specific ignorelist by walking
3676 * ignores backwards.
3678 pe = TAILQ_LAST(ignores, got_pathlist_head);
3679 while (pe) {
3680 if (got_path_is_child(path, pe->path, pe->path_len)) {
3681 struct got_pathlist_head *ignorelist = pe->data;
3682 struct got_pathlist_entry *pi;
3683 TAILQ_FOREACH(pi, ignorelist, entry) {
3684 int flags = FNM_LEADING_DIR;
3685 if (strstr(pi->path, "/**/") == NULL)
3686 flags |= FNM_PATHNAME;
3687 if (match_path(pi->path, pi->path_len,
3688 path, flags))
3689 continue;
3690 return 1;
3693 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3696 return 0;
3699 static const struct got_error *
3700 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3701 const char *path, int dirfd, const char *ignores_filename)
3703 const struct got_error *err = NULL;
3704 char *ignorespath;
3705 int fd = -1;
3706 FILE *ignoresfile = NULL;
3708 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3709 path[0] ? "/" : "", ignores_filename) == -1)
3710 return got_error_from_errno("asprintf");
3712 if (dirfd != -1) {
3713 fd = openat(dirfd, ignores_filename,
3714 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3715 if (fd == -1) {
3716 if (errno != ENOENT && errno != EACCES)
3717 err = got_error_from_errno2("openat",
3718 ignorespath);
3719 } else {
3720 ignoresfile = fdopen(fd, "r");
3721 if (ignoresfile == NULL)
3722 err = got_error_from_errno2("fdopen",
3723 ignorespath);
3724 else {
3725 fd = -1;
3726 err = read_ignores(ignores, path, ignoresfile);
3729 } else {
3730 ignoresfile = fopen(ignorespath, "re");
3731 if (ignoresfile == NULL) {
3732 if (errno != ENOENT && errno != EACCES)
3733 err = got_error_from_errno2("fopen",
3734 ignorespath);
3735 } else
3736 err = read_ignores(ignores, path, ignoresfile);
3739 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3740 err = got_error_from_errno2("fclose", path);
3741 if (fd != -1 && close(fd) == -1 && err == NULL)
3742 err = got_error_from_errno2("close", path);
3743 free(ignorespath);
3744 return err;
3747 static const struct got_error *
3748 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3749 int dirfd)
3751 const struct got_error *err = NULL;
3752 struct diff_dir_cb_arg *a = arg;
3753 char *path = NULL;
3755 if (ignore != NULL)
3756 *ignore = 0;
3758 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3759 return got_error(GOT_ERR_CANCELLED);
3761 if (parent_path[0]) {
3762 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3763 return got_error_from_errno("asprintf");
3764 } else {
3765 path = de->d_name;
3768 if (de->d_type == DT_DIR) {
3769 if (!a->no_ignores && ignore != NULL &&
3770 match_ignores(a->ignores, path))
3771 *ignore = 1;
3772 } else if (!match_ignores(a->ignores, path) &&
3773 got_path_is_child(path, a->status_path, a->status_path_len))
3774 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3775 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3776 if (parent_path[0])
3777 free(path);
3778 return err;
3781 static const struct got_error *
3782 status_traverse(void *arg, const char *path, int dirfd)
3784 const struct got_error *err = NULL;
3785 struct diff_dir_cb_arg *a = arg;
3787 if (a->no_ignores)
3788 return NULL;
3790 err = add_ignores(a->ignores, a->worktree->root_path,
3791 path, dirfd, ".cvsignore");
3792 if (err)
3793 return err;
3795 err = add_ignores(a->ignores, a->worktree->root_path, path,
3796 dirfd, ".gitignore");
3798 return err;
3801 static const struct got_error *
3802 report_single_file_status(const char *path, const char *ondisk_path,
3803 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3804 void *status_arg, struct got_repository *repo, int report_unchanged,
3805 struct got_pathlist_head *ignores, int no_ignores)
3807 struct got_fileindex_entry *ie;
3808 struct stat sb;
3810 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3811 if (ie)
3812 return report_file_status(ie, ondisk_path, -1, NULL,
3813 status_cb, status_arg, repo, report_unchanged);
3815 if (lstat(ondisk_path, &sb) == -1) {
3816 if (errno != ENOENT)
3817 return got_error_from_errno2("lstat", ondisk_path);
3818 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3819 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3822 if (!no_ignores && match_ignores(ignores, path))
3823 return NULL;
3825 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3826 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3827 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3829 return NULL;
3832 static const struct got_error *
3833 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3834 const char *root_path, const char *path)
3836 const struct got_error *err;
3837 char *parent_path, *next_parent_path = NULL;
3839 err = add_ignores(ignores, root_path, "", -1,
3840 ".cvsignore");
3841 if (err)
3842 return err;
3844 err = add_ignores(ignores, root_path, "", -1,
3845 ".gitignore");
3846 if (err)
3847 return err;
3849 err = got_path_dirname(&parent_path, path);
3850 if (err) {
3851 if (err->code == GOT_ERR_BAD_PATH)
3852 return NULL; /* cannot traverse parent */
3853 return err;
3855 for (;;) {
3856 err = add_ignores(ignores, root_path, parent_path, -1,
3857 ".cvsignore");
3858 if (err)
3859 break;
3860 err = add_ignores(ignores, root_path, parent_path, -1,
3861 ".gitignore");
3862 if (err)
3863 break;
3864 err = got_path_dirname(&next_parent_path, parent_path);
3865 if (err) {
3866 if (err->code == GOT_ERR_BAD_PATH)
3867 err = NULL; /* traversed everything */
3868 break;
3870 if (got_path_is_root_dir(parent_path))
3871 break;
3872 free(parent_path);
3873 parent_path = next_parent_path;
3874 next_parent_path = NULL;
3877 free(parent_path);
3878 free(next_parent_path);
3879 return err;
3882 static const struct got_error *
3883 worktree_status(struct got_worktree *worktree, const char *path,
3884 struct got_fileindex *fileindex, struct got_repository *repo,
3885 got_worktree_status_cb status_cb, void *status_arg,
3886 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3887 int report_unchanged)
3889 const struct got_error *err = NULL;
3890 int fd = -1;
3891 struct got_fileindex_diff_dir_cb fdiff_cb;
3892 struct diff_dir_cb_arg arg;
3893 char *ondisk_path = NULL;
3894 struct got_pathlist_head ignores;
3895 struct got_fileindex_entry *ie;
3897 TAILQ_INIT(&ignores);
3899 if (asprintf(&ondisk_path, "%s%s%s",
3900 worktree->root_path, path[0] ? "/" : "", path) == -1)
3901 return got_error_from_errno("asprintf");
3903 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3904 if (ie) {
3905 err = report_single_file_status(path, ondisk_path,
3906 fileindex, status_cb, status_arg, repo,
3907 report_unchanged, &ignores, no_ignores);
3908 goto done;
3911 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3912 if (fd == -1) {
3913 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3914 !got_err_open_nofollow_on_symlink())
3915 err = got_error_from_errno2("open", ondisk_path);
3916 else {
3917 if (!no_ignores) {
3918 err = add_ignores_from_parent_paths(&ignores,
3919 worktree->root_path, ondisk_path);
3920 if (err)
3921 goto done;
3923 err = report_single_file_status(path, ondisk_path,
3924 fileindex, status_cb, status_arg, repo,
3925 report_unchanged, &ignores, no_ignores);
3927 } else {
3928 fdiff_cb.diff_old_new = status_old_new;
3929 fdiff_cb.diff_old = status_old;
3930 fdiff_cb.diff_new = status_new;
3931 fdiff_cb.diff_traverse = status_traverse;
3932 arg.fileindex = fileindex;
3933 arg.worktree = worktree;
3934 arg.status_path = path;
3935 arg.status_path_len = strlen(path);
3936 arg.repo = repo;
3937 arg.status_cb = status_cb;
3938 arg.status_arg = status_arg;
3939 arg.cancel_cb = cancel_cb;
3940 arg.cancel_arg = cancel_arg;
3941 arg.report_unchanged = report_unchanged;
3942 arg.no_ignores = no_ignores;
3943 if (!no_ignores) {
3944 err = add_ignores_from_parent_paths(&ignores,
3945 worktree->root_path, path);
3946 if (err)
3947 goto done;
3949 arg.ignores = &ignores;
3950 err = got_fileindex_diff_dir(fileindex, fd,
3951 worktree->root_path, path, repo, &fdiff_cb, &arg);
3953 done:
3954 free_ignores(&ignores);
3955 if (fd != -1 && close(fd) == -1 && err == NULL)
3956 err = got_error_from_errno("close");
3957 free(ondisk_path);
3958 return err;
3961 const struct got_error *
3962 got_worktree_status(struct got_worktree *worktree,
3963 struct got_pathlist_head *paths, struct got_repository *repo,
3964 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3965 got_cancel_cb cancel_cb, void *cancel_arg)
3967 const struct got_error *err = NULL;
3968 char *fileindex_path = NULL;
3969 struct got_fileindex *fileindex = NULL;
3970 struct got_pathlist_entry *pe;
3972 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3973 if (err)
3974 return err;
3976 TAILQ_FOREACH(pe, paths, entry) {
3977 err = worktree_status(worktree, pe->path, fileindex, repo,
3978 status_cb, status_arg, cancel_cb, cancel_arg,
3979 no_ignores, 0);
3980 if (err)
3981 break;
3983 free(fileindex_path);
3984 got_fileindex_free(fileindex);
3985 return err;
3988 const struct got_error *
3989 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3990 const char *arg)
3992 const struct got_error *err = NULL;
3993 char *resolved = NULL, *cwd = NULL, *path = NULL;
3994 size_t len;
3995 struct stat sb;
3996 char *abspath = NULL;
3997 char canonpath[PATH_MAX];
3999 *wt_path = NULL;
4001 cwd = getcwd(NULL, 0);
4002 if (cwd == NULL)
4003 return got_error_from_errno("getcwd");
4005 if (lstat(arg, &sb) == -1) {
4006 if (errno != ENOENT) {
4007 err = got_error_from_errno2("lstat", arg);
4008 goto done;
4010 sb.st_mode = 0;
4012 if (S_ISLNK(sb.st_mode)) {
4014 * We cannot use realpath(3) with symlinks since we want to
4015 * operate on the symlink itself.
4016 * But we can make the path absolute, assuming it is relative
4017 * to the current working directory, and then canonicalize it.
4019 if (!got_path_is_absolute(arg)) {
4020 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4021 err = got_error_from_errno("asprintf");
4022 goto done;
4026 err = got_canonpath(abspath ? abspath : arg, canonpath,
4027 sizeof(canonpath));
4028 if (err)
4029 goto done;
4030 resolved = strdup(canonpath);
4031 if (resolved == NULL) {
4032 err = got_error_from_errno("strdup");
4033 goto done;
4035 } else {
4036 resolved = realpath(arg, NULL);
4037 if (resolved == NULL) {
4038 if (errno != ENOENT) {
4039 err = got_error_from_errno2("realpath", arg);
4040 goto done;
4042 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4043 err = got_error_from_errno("asprintf");
4044 goto done;
4046 err = got_canonpath(abspath, canonpath,
4047 sizeof(canonpath));
4048 if (err)
4049 goto done;
4050 resolved = strdup(canonpath);
4051 if (resolved == NULL) {
4052 err = got_error_from_errno("strdup");
4053 goto done;
4058 if (strncmp(got_worktree_get_root_path(worktree), resolved,
4059 strlen(got_worktree_get_root_path(worktree)))) {
4060 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
4061 goto done;
4064 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
4065 err = got_path_skip_common_ancestor(&path,
4066 got_worktree_get_root_path(worktree), resolved);
4067 if (err)
4068 goto done;
4069 } else {
4070 path = strdup("");
4071 if (path == NULL) {
4072 err = got_error_from_errno("strdup");
4073 goto done;
4077 /* XXX status walk can't deal with trailing slash! */
4078 len = strlen(path);
4079 while (len > 0 && path[len - 1] == '/') {
4080 path[len - 1] = '\0';
4081 len--;
4083 done:
4084 free(abspath);
4085 free(resolved);
4086 free(cwd);
4087 if (err == NULL)
4088 *wt_path = path;
4089 else
4090 free(path);
4091 return err;
4094 struct schedule_addition_args {
4095 struct got_worktree *worktree;
4096 struct got_fileindex *fileindex;
4097 got_worktree_checkout_cb progress_cb;
4098 void *progress_arg;
4099 struct got_repository *repo;
4102 static const struct got_error *
4103 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4104 const char *relpath, struct got_object_id *blob_id,
4105 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4106 int dirfd, const char *de_name)
4108 struct schedule_addition_args *a = arg;
4109 const struct got_error *err = NULL;
4110 struct got_fileindex_entry *ie;
4111 struct stat sb;
4112 char *ondisk_path;
4114 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4115 relpath) == -1)
4116 return got_error_from_errno("asprintf");
4118 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4119 if (ie) {
4120 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4121 de_name, a->repo);
4122 if (err)
4123 goto done;
4124 /* Re-adding an existing entry is a no-op. */
4125 if (status == GOT_STATUS_ADD)
4126 goto done;
4127 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4128 if (err)
4129 goto done;
4132 if (status != GOT_STATUS_UNVERSIONED) {
4133 if (status == GOT_STATUS_NONEXISTENT)
4134 err = got_error_set_errno(ENOENT, ondisk_path);
4135 else
4136 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4137 goto done;
4140 err = got_fileindex_entry_alloc(&ie, relpath);
4141 if (err)
4142 goto done;
4143 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4144 relpath, NULL, NULL, 1);
4145 if (err) {
4146 got_fileindex_entry_free(ie);
4147 goto done;
4149 err = got_fileindex_entry_add(a->fileindex, ie);
4150 if (err) {
4151 got_fileindex_entry_free(ie);
4152 goto done;
4154 done:
4155 free(ondisk_path);
4156 if (err)
4157 return err;
4158 if (status == GOT_STATUS_ADD)
4159 return NULL;
4160 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4163 const struct got_error *
4164 got_worktree_schedule_add(struct got_worktree *worktree,
4165 struct got_pathlist_head *paths,
4166 got_worktree_checkout_cb progress_cb, void *progress_arg,
4167 struct got_repository *repo, int no_ignores)
4169 struct got_fileindex *fileindex = NULL;
4170 char *fileindex_path = NULL;
4171 const struct got_error *err = NULL, *sync_err, *unlockerr;
4172 struct got_pathlist_entry *pe;
4173 struct schedule_addition_args saa;
4175 err = lock_worktree(worktree, LOCK_EX);
4176 if (err)
4177 return err;
4179 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4180 if (err)
4181 goto done;
4183 saa.worktree = worktree;
4184 saa.fileindex = fileindex;
4185 saa.progress_cb = progress_cb;
4186 saa.progress_arg = progress_arg;
4187 saa.repo = repo;
4189 TAILQ_FOREACH(pe, paths, entry) {
4190 err = worktree_status(worktree, pe->path, fileindex, repo,
4191 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4192 if (err)
4193 break;
4195 sync_err = sync_fileindex(fileindex, fileindex_path);
4196 if (sync_err && err == NULL)
4197 err = sync_err;
4198 done:
4199 free(fileindex_path);
4200 if (fileindex)
4201 got_fileindex_free(fileindex);
4202 unlockerr = lock_worktree(worktree, LOCK_SH);
4203 if (unlockerr && err == NULL)
4204 err = unlockerr;
4205 return err;
4208 struct schedule_deletion_args {
4209 struct got_worktree *worktree;
4210 struct got_fileindex *fileindex;
4211 got_worktree_delete_cb progress_cb;
4212 void *progress_arg;
4213 struct got_repository *repo;
4214 int delete_local_mods;
4215 int keep_on_disk;
4216 int ignore_missing_paths;
4217 const char *status_codes;
4220 static const struct got_error *
4221 schedule_for_deletion(void *arg, unsigned char status,
4222 unsigned char staged_status, const char *relpath,
4223 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4224 struct got_object_id *commit_id, int dirfd, const char *de_name)
4226 struct schedule_deletion_args *a = arg;
4227 const struct got_error *err = NULL;
4228 struct got_fileindex_entry *ie = NULL;
4229 struct stat sb;
4230 char *ondisk_path;
4232 if (status == GOT_STATUS_NONEXISTENT) {
4233 if (a->ignore_missing_paths)
4234 return NULL;
4235 return got_error_set_errno(ENOENT, relpath);
4238 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4239 if (ie == NULL)
4240 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4242 staged_status = get_staged_status(ie);
4243 if (staged_status != GOT_STATUS_NO_CHANGE) {
4244 if (staged_status == GOT_STATUS_DELETE)
4245 return NULL;
4246 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4249 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4250 relpath) == -1)
4251 return got_error_from_errno("asprintf");
4253 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4254 a->repo);
4255 if (err)
4256 goto done;
4258 if (a->status_codes) {
4259 size_t ncodes = strlen(a->status_codes);
4260 int i;
4261 for (i = 0; i < ncodes ; i++) {
4262 if (status == a->status_codes[i])
4263 break;
4265 if (i == ncodes) {
4266 /* Do not delete files in non-matching status. */
4267 free(ondisk_path);
4268 return NULL;
4270 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4271 a->status_codes[i] != GOT_STATUS_MISSING) {
4272 static char msg[64];
4273 snprintf(msg, sizeof(msg),
4274 "invalid status code '%c'", a->status_codes[i]);
4275 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4276 goto done;
4280 if (status != GOT_STATUS_NO_CHANGE) {
4281 if (status == GOT_STATUS_DELETE)
4282 goto done;
4283 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4284 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4285 goto done;
4287 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4288 err = got_error_set_errno(ENOENT, relpath);
4289 goto done;
4291 if (status != GOT_STATUS_MODIFY &&
4292 status != GOT_STATUS_MISSING) {
4293 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4294 goto done;
4298 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4299 size_t root_len;
4301 if (dirfd != -1) {
4302 if (unlinkat(dirfd, de_name, 0) == -1) {
4303 err = got_error_from_errno2("unlinkat",
4304 ondisk_path);
4305 goto done;
4307 } else if (unlink(ondisk_path) == -1) {
4308 err = got_error_from_errno2("unlink", ondisk_path);
4309 goto done;
4312 root_len = strlen(a->worktree->root_path);
4313 do {
4314 char *parent;
4315 err = got_path_dirname(&parent, ondisk_path);
4316 if (err)
4317 goto done;
4318 free(ondisk_path);
4319 ondisk_path = parent;
4320 if (rmdir(ondisk_path) == -1) {
4321 if (errno != ENOTEMPTY)
4322 err = got_error_from_errno2("rmdir",
4323 ondisk_path);
4324 break;
4326 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4327 strlen(ondisk_path), root_len) != 0);
4330 got_fileindex_entry_mark_deleted_from_disk(ie);
4331 done:
4332 free(ondisk_path);
4333 if (err)
4334 return err;
4335 if (status == GOT_STATUS_DELETE)
4336 return NULL;
4337 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4338 staged_status, relpath);
4341 const struct got_error *
4342 got_worktree_schedule_delete(struct got_worktree *worktree,
4343 struct got_pathlist_head *paths, int delete_local_mods,
4344 const char *status_codes,
4345 got_worktree_delete_cb progress_cb, void *progress_arg,
4346 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4348 struct got_fileindex *fileindex = NULL;
4349 char *fileindex_path = NULL;
4350 const struct got_error *err = NULL, *sync_err, *unlockerr;
4351 struct got_pathlist_entry *pe;
4352 struct schedule_deletion_args sda;
4354 err = lock_worktree(worktree, LOCK_EX);
4355 if (err)
4356 return err;
4358 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4359 if (err)
4360 goto done;
4362 sda.worktree = worktree;
4363 sda.fileindex = fileindex;
4364 sda.progress_cb = progress_cb;
4365 sda.progress_arg = progress_arg;
4366 sda.repo = repo;
4367 sda.delete_local_mods = delete_local_mods;
4368 sda.keep_on_disk = keep_on_disk;
4369 sda.ignore_missing_paths = ignore_missing_paths;
4370 sda.status_codes = status_codes;
4372 TAILQ_FOREACH(pe, paths, entry) {
4373 err = worktree_status(worktree, pe->path, fileindex, repo,
4374 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4375 if (err)
4376 break;
4378 sync_err = sync_fileindex(fileindex, fileindex_path);
4379 if (sync_err && err == NULL)
4380 err = sync_err;
4381 done:
4382 free(fileindex_path);
4383 if (fileindex)
4384 got_fileindex_free(fileindex);
4385 unlockerr = lock_worktree(worktree, LOCK_SH);
4386 if (unlockerr && err == NULL)
4387 err = unlockerr;
4388 return err;
4391 static const struct got_error *
4392 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4394 const struct got_error *err = NULL;
4395 char *line = NULL;
4396 size_t linesize = 0, n;
4397 ssize_t linelen;
4399 linelen = getline(&line, &linesize, infile);
4400 if (linelen == -1) {
4401 if (ferror(infile)) {
4402 err = got_error_from_errno("getline");
4403 goto done;
4405 return NULL;
4407 if (outfile) {
4408 n = fwrite(line, 1, linelen, outfile);
4409 if (n != linelen) {
4410 err = got_ferror(outfile, GOT_ERR_IO);
4411 goto done;
4414 if (rejectfile) {
4415 n = fwrite(line, 1, linelen, rejectfile);
4416 if (n != linelen)
4417 err = got_ferror(rejectfile, GOT_ERR_IO);
4419 done:
4420 free(line);
4421 return err;
4424 static const struct got_error *
4425 skip_one_line(FILE *f)
4427 char *line = NULL;
4428 size_t linesize = 0;
4429 ssize_t linelen;
4431 linelen = getline(&line, &linesize, f);
4432 if (linelen == -1) {
4433 if (ferror(f))
4434 return got_error_from_errno("getline");
4435 return NULL;
4437 free(line);
4438 return NULL;
4441 static const struct got_error *
4442 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4443 int start_old, int end_old, int start_new, int end_new,
4444 FILE *outfile, FILE *rejectfile)
4446 const struct got_error *err;
4448 /* Copy old file's lines leading up to patch. */
4449 while (!feof(f1) && *line_cur1 < start_old) {
4450 err = copy_one_line(f1, outfile, NULL);
4451 if (err)
4452 return err;
4453 (*line_cur1)++;
4455 /* Skip new file's lines leading up to patch. */
4456 while (!feof(f2) && *line_cur2 < start_new) {
4457 if (rejectfile)
4458 err = copy_one_line(f2, NULL, rejectfile);
4459 else
4460 err = skip_one_line(f2);
4461 if (err)
4462 return err;
4463 (*line_cur2)++;
4465 /* Copy patched lines. */
4466 while (!feof(f2) && *line_cur2 <= end_new) {
4467 err = copy_one_line(f2, outfile, NULL);
4468 if (err)
4469 return err;
4470 (*line_cur2)++;
4472 /* Skip over old file's replaced lines. */
4473 while (!feof(f1) && *line_cur1 <= end_old) {
4474 if (rejectfile)
4475 err = copy_one_line(f1, NULL, rejectfile);
4476 else
4477 err = skip_one_line(f1);
4478 if (err)
4479 return err;
4480 (*line_cur1)++;
4483 return NULL;
4486 static const struct got_error *
4487 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4488 FILE *outfile, FILE *rejectfile)
4490 const struct got_error *err;
4492 if (outfile) {
4493 /* Copy old file's lines until EOF. */
4494 while (!feof(f1)) {
4495 err = copy_one_line(f1, outfile, NULL);
4496 if (err)
4497 return err;
4498 (*line_cur1)++;
4501 if (rejectfile) {
4502 /* Copy new file's lines until EOF. */
4503 while (!feof(f2)) {
4504 err = copy_one_line(f2, NULL, rejectfile);
4505 if (err)
4506 return err;
4507 (*line_cur2)++;
4511 return NULL;
4514 static const struct got_error *
4515 apply_or_reject_change(int *choice, int *nchunks_used,
4516 struct diff_result *diff_result, int n,
4517 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4518 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4519 got_worktree_patch_cb patch_cb, void *patch_arg)
4521 const struct got_error *err = NULL;
4522 struct diff_chunk_context cc = {};
4523 int start_old, end_old, start_new, end_new;
4524 FILE *hunkfile;
4525 struct diff_output_unidiff_state *diff_state;
4526 struct diff_input_info diff_info;
4527 int rc;
4529 *choice = GOT_PATCH_CHOICE_NONE;
4531 /* Get changed line numbers without context lines for copy_change(). */
4532 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4533 start_old = cc.left.start;
4534 end_old = cc.left.end;
4535 start_new = cc.right.start;
4536 end_new = cc.right.end;
4538 /* Get the same change with context lines for display. */
4539 memset(&cc, 0, sizeof(cc));
4540 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4542 memset(&diff_info, 0, sizeof(diff_info));
4543 diff_info.left_path = relpath;
4544 diff_info.right_path = relpath;
4546 diff_state = diff_output_unidiff_state_alloc();
4547 if (diff_state == NULL)
4548 return got_error_set_errno(ENOMEM,
4549 "diff_output_unidiff_state_alloc");
4551 hunkfile = got_opentemp();
4552 if (hunkfile == NULL) {
4553 err = got_error_from_errno("got_opentemp");
4554 goto done;
4557 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4558 diff_result, &cc);
4559 if (rc != DIFF_RC_OK) {
4560 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4561 goto done;
4564 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4565 err = got_ferror(hunkfile, GOT_ERR_IO);
4566 goto done;
4569 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4570 hunkfile, changeno, nchanges);
4571 if (err)
4572 goto done;
4574 switch (*choice) {
4575 case GOT_PATCH_CHOICE_YES:
4576 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4577 end_old, start_new, end_new, outfile, rejectfile);
4578 break;
4579 case GOT_PATCH_CHOICE_NO:
4580 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4581 end_old, start_new, end_new, rejectfile, outfile);
4582 break;
4583 case GOT_PATCH_CHOICE_QUIT:
4584 break;
4585 default:
4586 err = got_error(GOT_ERR_PATCH_CHOICE);
4587 break;
4589 done:
4590 diff_output_unidiff_state_free(diff_state);
4591 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4592 err = got_error_from_errno("fclose");
4593 return err;
4596 struct revert_file_args {
4597 struct got_worktree *worktree;
4598 struct got_fileindex *fileindex;
4599 got_worktree_checkout_cb progress_cb;
4600 void *progress_arg;
4601 got_worktree_patch_cb patch_cb;
4602 void *patch_arg;
4603 struct got_repository *repo;
4604 int unlink_added_files;
4607 static const struct got_error *
4608 create_patched_content(char **path_outfile, int reverse_patch,
4609 struct got_object_id *blob_id, const char *path2,
4610 int dirfd2, const char *de_name2,
4611 const char *relpath, struct got_repository *repo,
4612 got_worktree_patch_cb patch_cb, void *patch_arg)
4614 const struct got_error *err, *free_err;
4615 struct got_blob_object *blob = NULL;
4616 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4617 int fd = -1, fd2 = -1;
4618 char link_target[PATH_MAX];
4619 ssize_t link_len = 0;
4620 char *path1 = NULL, *id_str = NULL;
4621 struct stat sb2;
4622 struct got_diffreg_result *diffreg_result = NULL;
4623 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4624 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4626 *path_outfile = NULL;
4628 err = got_object_id_str(&id_str, blob_id);
4629 if (err)
4630 return err;
4632 if (dirfd2 != -1) {
4633 fd2 = openat(dirfd2, de_name2,
4634 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4635 if (fd2 == -1) {
4636 if (!got_err_open_nofollow_on_symlink()) {
4637 err = got_error_from_errno2("openat", path2);
4638 goto done;
4640 link_len = readlinkat(dirfd2, de_name2,
4641 link_target, sizeof(link_target));
4642 if (link_len == -1) {
4643 return got_error_from_errno2("readlinkat",
4644 path2);
4646 sb2.st_mode = S_IFLNK;
4647 sb2.st_size = link_len;
4649 } else {
4650 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4651 if (fd2 == -1) {
4652 if (!got_err_open_nofollow_on_symlink()) {
4653 err = got_error_from_errno2("open", path2);
4654 goto done;
4656 link_len = readlink(path2, link_target,
4657 sizeof(link_target));
4658 if (link_len == -1)
4659 return got_error_from_errno2("readlink", path2);
4660 sb2.st_mode = S_IFLNK;
4661 sb2.st_size = link_len;
4664 if (fd2 != -1) {
4665 if (fstat(fd2, &sb2) == -1) {
4666 err = got_error_from_errno2("fstat", path2);
4667 goto done;
4670 f2 = fdopen(fd2, "r");
4671 if (f2 == NULL) {
4672 err = got_error_from_errno2("fdopen", path2);
4673 goto done;
4675 fd2 = -1;
4676 } else {
4677 size_t n;
4678 f2 = got_opentemp();
4679 if (f2 == NULL) {
4680 err = got_error_from_errno2("got_opentemp", path2);
4681 goto done;
4683 n = fwrite(link_target, 1, link_len, f2);
4684 if (n != link_len) {
4685 err = got_ferror(f2, GOT_ERR_IO);
4686 goto done;
4688 if (fflush(f2) == EOF) {
4689 err = got_error_from_errno("fflush");
4690 goto done;
4692 rewind(f2);
4695 fd = got_opentempfd();
4696 if (fd == -1) {
4697 err = got_error_from_errno("got_opentempfd");
4698 goto done;
4701 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4702 if (err)
4703 goto done;
4705 err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4706 if (err)
4707 goto done;
4709 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4710 if (err)
4711 goto done;
4713 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4714 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4715 if (err)
4716 goto done;
4718 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4719 "");
4720 if (err)
4721 goto done;
4723 if (fseek(f1, 0L, SEEK_SET) == -1)
4724 return got_ferror(f1, GOT_ERR_IO);
4725 if (fseek(f2, 0L, SEEK_SET) == -1)
4726 return got_ferror(f2, GOT_ERR_IO);
4728 /* Count the number of actual changes in the diff result. */
4729 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4730 struct diff_chunk_context cc = {};
4731 diff_chunk_context_load_change(&cc, &nchunks_used,
4732 diffreg_result->result, n, 0);
4733 nchanges++;
4735 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4736 int choice;
4737 err = apply_or_reject_change(&choice, &nchunks_used,
4738 diffreg_result->result, n, relpath, f1, f2,
4739 &line_cur1, &line_cur2,
4740 reverse_patch ? NULL : outfile,
4741 reverse_patch ? outfile : NULL,
4742 ++i, nchanges, patch_cb, patch_arg);
4743 if (err)
4744 goto done;
4745 if (choice == GOT_PATCH_CHOICE_YES)
4746 have_content = 1;
4747 else if (choice == GOT_PATCH_CHOICE_QUIT)
4748 break;
4750 if (have_content) {
4751 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4752 reverse_patch ? NULL : outfile,
4753 reverse_patch ? outfile : NULL);
4754 if (err)
4755 goto done;
4757 if (!S_ISLNK(sb2.st_mode)) {
4758 mode_t mode;
4760 mode = apply_umask(sb2.st_mode);
4761 if (fchmod(fileno(outfile), mode) == -1) {
4762 err = got_error_from_errno2("fchmod", path2);
4763 goto done;
4767 done:
4768 free(id_str);
4769 if (fd != -1 && close(fd) == -1 && err == NULL)
4770 err = got_error_from_errno("close");
4771 if (blob)
4772 got_object_blob_close(blob);
4773 free_err = got_diffreg_result_free(diffreg_result);
4774 if (err == NULL)
4775 err = free_err;
4776 if (f1 && fclose(f1) == EOF && err == NULL)
4777 err = got_error_from_errno2("fclose", path1);
4778 if (f2 && fclose(f2) == EOF && err == NULL)
4779 err = got_error_from_errno2("fclose", path2);
4780 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4781 err = got_error_from_errno2("close", path2);
4782 if (outfile && fclose(outfile) == EOF && err == NULL)
4783 err = got_error_from_errno2("fclose", *path_outfile);
4784 if (path1 && unlink(path1) == -1 && err == NULL)
4785 err = got_error_from_errno2("unlink", path1);
4786 if (err || !have_content) {
4787 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4788 err = got_error_from_errno2("unlink", *path_outfile);
4789 free(*path_outfile);
4790 *path_outfile = NULL;
4792 free(path1);
4793 return err;
4796 static const struct got_error *
4797 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4798 const char *relpath, struct got_object_id *blob_id,
4799 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4800 int dirfd, const char *de_name)
4802 struct revert_file_args *a = arg;
4803 const struct got_error *err = NULL;
4804 char *parent_path = NULL;
4805 struct got_fileindex_entry *ie;
4806 struct got_commit_object *base_commit = NULL;
4807 struct got_tree_object *tree = NULL;
4808 struct got_object_id *tree_id = NULL;
4809 const struct got_tree_entry *te = NULL;
4810 char *tree_path = NULL, *te_name;
4811 char *ondisk_path = NULL, *path_content = NULL;
4812 struct got_blob_object *blob = NULL;
4813 int fd = -1;
4815 /* Reverting a staged deletion is a no-op. */
4816 if (status == GOT_STATUS_DELETE &&
4817 staged_status != GOT_STATUS_NO_CHANGE)
4818 return NULL;
4820 if (status == GOT_STATUS_UNVERSIONED)
4821 return (*a->progress_cb)(a->progress_arg,
4822 GOT_STATUS_UNVERSIONED, relpath);
4824 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4825 if (ie == NULL)
4826 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4828 /* Construct in-repository path of tree which contains this blob. */
4829 err = got_path_dirname(&parent_path, ie->path);
4830 if (err) {
4831 if (err->code != GOT_ERR_BAD_PATH)
4832 goto done;
4833 parent_path = strdup("/");
4834 if (parent_path == NULL) {
4835 err = got_error_from_errno("strdup");
4836 goto done;
4839 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4840 tree_path = strdup(parent_path);
4841 if (tree_path == NULL) {
4842 err = got_error_from_errno("strdup");
4843 goto done;
4845 } else {
4846 if (got_path_is_root_dir(parent_path)) {
4847 tree_path = strdup(a->worktree->path_prefix);
4848 if (tree_path == NULL) {
4849 err = got_error_from_errno("strdup");
4850 goto done;
4852 } else {
4853 if (asprintf(&tree_path, "%s/%s",
4854 a->worktree->path_prefix, parent_path) == -1) {
4855 err = got_error_from_errno("asprintf");
4856 goto done;
4861 err = got_object_open_as_commit(&base_commit, a->repo,
4862 a->worktree->base_commit_id);
4863 if (err)
4864 goto done;
4866 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4867 if (err) {
4868 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4869 (status == GOT_STATUS_ADD ||
4870 staged_status == GOT_STATUS_ADD)))
4871 goto done;
4872 } else {
4873 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4874 if (err)
4875 goto done;
4877 err = got_path_basename(&te_name, ie->path);
4878 if (err)
4879 goto done;
4881 te = got_object_tree_find_entry(tree, te_name);
4882 free(te_name);
4883 if (te == NULL && status != GOT_STATUS_ADD &&
4884 staged_status != GOT_STATUS_ADD) {
4885 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4886 goto done;
4890 switch (status) {
4891 case GOT_STATUS_ADD:
4892 if (a->patch_cb) {
4893 int choice = GOT_PATCH_CHOICE_NONE;
4894 err = (*a->patch_cb)(&choice, a->patch_arg,
4895 status, ie->path, NULL, 1, 1);
4896 if (err)
4897 goto done;
4898 if (choice != GOT_PATCH_CHOICE_YES)
4899 break;
4901 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4902 ie->path);
4903 if (err)
4904 goto done;
4905 got_fileindex_entry_remove(a->fileindex, ie);
4906 if (a->unlink_added_files) {
4907 if (asprintf(&ondisk_path, "%s/%s",
4908 got_worktree_get_root_path(a->worktree),
4909 relpath) == -1) {
4910 err = got_error_from_errno("asprintf");
4911 goto done;
4913 if (unlink(ondisk_path) == -1) {
4914 err = got_error_from_errno2("unlink",
4915 ondisk_path);
4916 break;
4919 break;
4920 case GOT_STATUS_DELETE:
4921 if (a->patch_cb) {
4922 int choice = GOT_PATCH_CHOICE_NONE;
4923 err = (*a->patch_cb)(&choice, a->patch_arg,
4924 status, ie->path, NULL, 1, 1);
4925 if (err)
4926 goto done;
4927 if (choice != GOT_PATCH_CHOICE_YES)
4928 break;
4930 /* fall through */
4931 case GOT_STATUS_MODIFY:
4932 case GOT_STATUS_MODE_CHANGE:
4933 case GOT_STATUS_CONFLICT:
4934 case GOT_STATUS_MISSING: {
4935 struct got_object_id id;
4936 if (staged_status == GOT_STATUS_ADD ||
4937 staged_status == GOT_STATUS_MODIFY)
4938 got_fileindex_entry_get_staged_blob_id(&id, ie);
4939 else
4940 got_fileindex_entry_get_blob_id(&id, ie);
4941 fd = got_opentempfd();
4942 if (fd == -1) {
4943 err = got_error_from_errno("got_opentempfd");
4944 goto done;
4947 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4948 if (err)
4949 goto done;
4951 if (asprintf(&ondisk_path, "%s/%s",
4952 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4953 err = got_error_from_errno("asprintf");
4954 goto done;
4957 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4958 status == GOT_STATUS_CONFLICT)) {
4959 int is_bad_symlink = 0;
4960 err = create_patched_content(&path_content, 1, &id,
4961 ondisk_path, dirfd, de_name, ie->path, a->repo,
4962 a->patch_cb, a->patch_arg);
4963 if (err || path_content == NULL)
4964 break;
4965 if (te && S_ISLNK(te->mode)) {
4966 if (unlink(path_content) == -1) {
4967 err = got_error_from_errno2("unlink",
4968 path_content);
4969 break;
4971 err = install_symlink(&is_bad_symlink,
4972 a->worktree, ondisk_path, ie->path,
4973 blob, 0, 1, 0, 0, a->repo,
4974 a->progress_cb, a->progress_arg);
4975 } else {
4976 if (rename(path_content, ondisk_path) == -1) {
4977 err = got_error_from_errno3("rename",
4978 path_content, ondisk_path);
4979 goto done;
4982 } else {
4983 int is_bad_symlink = 0;
4984 if (te && S_ISLNK(te->mode)) {
4985 err = install_symlink(&is_bad_symlink,
4986 a->worktree, ondisk_path, ie->path,
4987 blob, 0, 1, 0, 0, a->repo,
4988 a->progress_cb, a->progress_arg);
4989 } else {
4990 err = install_blob(a->worktree, ondisk_path,
4991 ie->path,
4992 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4993 got_fileindex_perms_to_st(ie), blob,
4994 0, 1, 0, 0, a->repo,
4995 a->progress_cb, a->progress_arg);
4997 if (err)
4998 goto done;
4999 if (status == GOT_STATUS_DELETE ||
5000 status == GOT_STATUS_MODE_CHANGE) {
5001 err = got_fileindex_entry_update(ie,
5002 a->worktree->root_fd, relpath,
5003 blob->id.sha1,
5004 a->worktree->base_commit_id->sha1, 1);
5005 if (err)
5006 goto done;
5008 if (is_bad_symlink) {
5009 got_fileindex_entry_filetype_set(ie,
5010 GOT_FILEIDX_MODE_BAD_SYMLINK);
5013 break;
5015 default:
5016 break;
5018 done:
5019 free(ondisk_path);
5020 free(path_content);
5021 free(parent_path);
5022 free(tree_path);
5023 if (fd != -1 && close(fd) == -1 && err == NULL)
5024 err = got_error_from_errno("close");
5025 if (blob)
5026 got_object_blob_close(blob);
5027 if (tree)
5028 got_object_tree_close(tree);
5029 free(tree_id);
5030 if (base_commit)
5031 got_object_commit_close(base_commit);
5032 return err;
5035 const struct got_error *
5036 got_worktree_revert(struct got_worktree *worktree,
5037 struct got_pathlist_head *paths,
5038 got_worktree_checkout_cb progress_cb, void *progress_arg,
5039 got_worktree_patch_cb patch_cb, void *patch_arg,
5040 struct got_repository *repo)
5042 struct got_fileindex *fileindex = NULL;
5043 char *fileindex_path = NULL;
5044 const struct got_error *err = NULL, *unlockerr = NULL;
5045 const struct got_error *sync_err = NULL;
5046 struct got_pathlist_entry *pe;
5047 struct revert_file_args rfa;
5049 err = lock_worktree(worktree, LOCK_EX);
5050 if (err)
5051 return err;
5053 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5054 if (err)
5055 goto done;
5057 rfa.worktree = worktree;
5058 rfa.fileindex = fileindex;
5059 rfa.progress_cb = progress_cb;
5060 rfa.progress_arg = progress_arg;
5061 rfa.patch_cb = patch_cb;
5062 rfa.patch_arg = patch_arg;
5063 rfa.repo = repo;
5064 rfa.unlink_added_files = 0;
5065 TAILQ_FOREACH(pe, paths, entry) {
5066 err = worktree_status(worktree, pe->path, fileindex, repo,
5067 revert_file, &rfa, NULL, NULL, 1, 0);
5068 if (err)
5069 break;
5071 sync_err = sync_fileindex(fileindex, fileindex_path);
5072 if (sync_err && err == NULL)
5073 err = sync_err;
5074 done:
5075 free(fileindex_path);
5076 if (fileindex)
5077 got_fileindex_free(fileindex);
5078 unlockerr = lock_worktree(worktree, LOCK_SH);
5079 if (unlockerr && err == NULL)
5080 err = unlockerr;
5081 return err;
5084 static void
5085 free_commitable(struct got_commitable *ct)
5087 free(ct->path);
5088 free(ct->in_repo_path);
5089 free(ct->ondisk_path);
5090 free(ct->blob_id);
5091 free(ct->base_blob_id);
5092 free(ct->staged_blob_id);
5093 free(ct->base_commit_id);
5094 free(ct);
5097 struct collect_commitables_arg {
5098 struct got_pathlist_head *commitable_paths;
5099 struct got_repository *repo;
5100 struct got_worktree *worktree;
5101 struct got_fileindex *fileindex;
5102 int have_staged_files;
5103 int allow_bad_symlinks;
5104 int diff_header_shown;
5105 int commit_conflicts;
5106 FILE *diff_outfile;
5107 FILE *f1;
5108 FILE *f2;
5112 * Create a file which contains the target path of a symlink so we can feed
5113 * it as content to the diff engine.
5115 static const struct got_error *
5116 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5117 const char *abspath)
5119 const struct got_error *err = NULL;
5120 char target_path[PATH_MAX];
5121 ssize_t target_len, outlen;
5123 *fd = -1;
5125 if (dirfd != -1) {
5126 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5127 if (target_len == -1)
5128 return got_error_from_errno2("readlinkat", abspath);
5129 } else {
5130 target_len = readlink(abspath, target_path, PATH_MAX);
5131 if (target_len == -1)
5132 return got_error_from_errno2("readlink", abspath);
5135 *fd = got_opentempfd();
5136 if (*fd == -1)
5137 return got_error_from_errno("got_opentempfd");
5139 outlen = write(*fd, target_path, target_len);
5140 if (outlen == -1) {
5141 err = got_error_from_errno("got_opentempfd");
5142 goto done;
5145 if (lseek(*fd, 0, SEEK_SET) == -1) {
5146 err = got_error_from_errno2("lseek", abspath);
5147 goto done;
5149 done:
5150 if (err) {
5151 close(*fd);
5152 *fd = -1;
5154 return err;
5157 static const struct got_error *
5158 append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5159 FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5160 int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5162 const struct got_error *err = NULL;
5163 struct got_blob_object *blob1 = NULL;
5164 int fd = -1, fd1 = -1, fd2 = -1;
5165 FILE *ondisk_file = NULL;
5166 char *label1 = NULL;
5167 struct stat sb;
5168 off_t size1 = 0;
5169 int f2_exists = 0;
5170 char *id_str = NULL;
5172 memset(&sb, 0, sizeof(sb));
5174 if (diff_staged) {
5175 if (ct->staged_status != GOT_STATUS_MODIFY &&
5176 ct->staged_status != GOT_STATUS_ADD &&
5177 ct->staged_status != GOT_STATUS_DELETE)
5178 return NULL;
5179 } else {
5180 if (ct->status != GOT_STATUS_MODIFY &&
5181 ct->status != GOT_STATUS_ADD &&
5182 ct->status != GOT_STATUS_DELETE &&
5183 ct->status != GOT_STATUS_CONFLICT)
5184 return NULL;
5187 err = got_opentemp_truncate(f1);
5188 if (err)
5189 return got_error_from_errno("got_opentemp_truncate");
5190 err = got_opentemp_truncate(f2);
5191 if (err)
5192 return got_error_from_errno("got_opentemp_truncate");
5194 if (!*diff_header_shown) {
5195 err = got_object_id_str(&id_str, worktree->base_commit_id);
5196 if (err)
5197 return err;
5198 fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5199 got_worktree_get_root_path(worktree));
5200 fprintf(diff_outfile, "commit - %s\n", id_str);
5201 fprintf(diff_outfile, "path + %s%s\n",
5202 got_worktree_get_root_path(worktree),
5203 diff_staged ? " (staged changes)" : "");
5204 *diff_header_shown = 1;
5207 if (diff_staged) {
5208 const char *label1 = NULL, *label2 = NULL;
5209 switch (ct->staged_status) {
5210 case GOT_STATUS_MODIFY:
5211 label1 = ct->path;
5212 label2 = ct->path;
5213 break;
5214 case GOT_STATUS_ADD:
5215 label2 = ct->path;
5216 break;
5217 case GOT_STATUS_DELETE:
5218 label1 = ct->path;
5219 break;
5220 default:
5221 return got_error(GOT_ERR_FILE_STATUS);
5223 fd1 = got_opentempfd();
5224 if (fd1 == -1) {
5225 err = got_error_from_errno("got_opentempfd");
5226 goto done;
5228 fd2 = got_opentempfd();
5229 if (fd2 == -1) {
5230 err = got_error_from_errno("got_opentempfd");
5231 goto done;
5233 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5234 fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5235 label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5236 NULL, repo, diff_outfile);
5237 goto done;
5240 fd1 = got_opentempfd();
5241 if (fd1 == -1) {
5242 err = got_error_from_errno("got_opentempfd");
5243 goto done;
5246 if (ct->status != GOT_STATUS_ADD) {
5247 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5248 8192, fd1);
5249 if (err)
5250 goto done;
5253 if (ct->status != GOT_STATUS_DELETE) {
5254 if (dirfd != -1) {
5255 fd = openat(dirfd, de_name,
5256 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5257 if (fd == -1) {
5258 if (!got_err_open_nofollow_on_symlink()) {
5259 err = got_error_from_errno2("openat",
5260 ct->ondisk_path);
5261 goto done;
5263 err = get_symlink_target_file(&fd, dirfd,
5264 de_name, ct->ondisk_path);
5265 if (err)
5266 goto done;
5268 } else {
5269 fd = open(ct->ondisk_path,
5270 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5271 if (fd == -1) {
5272 if (!got_err_open_nofollow_on_symlink()) {
5273 err = got_error_from_errno2("open",
5274 ct->ondisk_path);
5275 goto done;
5277 err = get_symlink_target_file(&fd, dirfd,
5278 de_name, ct->ondisk_path);
5279 if (err)
5280 goto done;
5283 if (fstatat(fd, ct->ondisk_path, &sb,
5284 AT_SYMLINK_NOFOLLOW) == -1) {
5285 err = got_error_from_errno2("fstatat", ct->ondisk_path);
5286 goto done;
5288 ondisk_file = fdopen(fd, "r");
5289 if (ondisk_file == NULL) {
5290 err = got_error_from_errno2("fdopen", ct->ondisk_path);
5291 goto done;
5293 fd = -1;
5294 f2_exists = 1;
5297 if (blob1) {
5298 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5299 f1, blob1);
5300 if (err)
5301 goto done;
5304 err = got_diff_blob_file(blob1, f1, size1, label1,
5305 ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5306 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5307 done:
5308 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5309 err = got_error_from_errno("close");
5310 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5311 err = got_error_from_errno("close");
5312 if (blob1)
5313 got_object_blob_close(blob1);
5314 if (fd != -1 && close(fd) == -1 && err == NULL)
5315 err = got_error_from_errno("close");
5316 if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5317 err = got_error_from_errno("fclose");
5318 return err;
5321 static const struct got_error *
5322 collect_commitables(void *arg, unsigned char status,
5323 unsigned char staged_status, const char *relpath,
5324 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5325 struct got_object_id *commit_id, int dirfd, const char *de_name)
5327 struct collect_commitables_arg *a = arg;
5328 const struct got_error *err = NULL;
5329 struct got_commitable *ct = NULL;
5330 struct got_pathlist_entry *new = NULL;
5331 char *parent_path = NULL, *path = NULL;
5332 struct stat sb;
5334 if (a->have_staged_files) {
5335 if (staged_status != GOT_STATUS_MODIFY &&
5336 staged_status != GOT_STATUS_ADD &&
5337 staged_status != GOT_STATUS_DELETE)
5338 return NULL;
5339 } else {
5340 if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5341 printf("C %s\n", relpath);
5342 return got_error(GOT_ERR_COMMIT_CONFLICT);
5345 if (status != GOT_STATUS_MODIFY &&
5346 status != GOT_STATUS_MODE_CHANGE &&
5347 status != GOT_STATUS_ADD &&
5348 status != GOT_STATUS_DELETE &&
5349 status != GOT_STATUS_CONFLICT)
5350 return NULL;
5353 if (asprintf(&path, "/%s", relpath) == -1) {
5354 err = got_error_from_errno("asprintf");
5355 goto done;
5357 if (strcmp(path, "/") == 0) {
5358 parent_path = strdup("");
5359 if (parent_path == NULL)
5360 return got_error_from_errno("strdup");
5361 } else {
5362 err = got_path_dirname(&parent_path, path);
5363 if (err)
5364 return err;
5367 ct = calloc(1, sizeof(*ct));
5368 if (ct == NULL) {
5369 err = got_error_from_errno("calloc");
5370 goto done;
5373 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5374 relpath) == -1) {
5375 err = got_error_from_errno("asprintf");
5376 goto done;
5379 if (staged_status == GOT_STATUS_ADD ||
5380 staged_status == GOT_STATUS_MODIFY) {
5381 struct got_fileindex_entry *ie;
5382 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5383 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5384 case GOT_FILEIDX_MODE_REGULAR_FILE:
5385 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5386 ct->mode = S_IFREG;
5387 break;
5388 case GOT_FILEIDX_MODE_SYMLINK:
5389 ct->mode = S_IFLNK;
5390 break;
5391 default:
5392 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5393 goto done;
5395 ct->mode |= got_fileindex_entry_perms_get(ie);
5396 } else if (status != GOT_STATUS_DELETE &&
5397 staged_status != GOT_STATUS_DELETE) {
5398 if (dirfd != -1) {
5399 if (fstatat(dirfd, de_name, &sb,
5400 AT_SYMLINK_NOFOLLOW) == -1) {
5401 err = got_error_from_errno2("fstatat",
5402 ct->ondisk_path);
5403 goto done;
5405 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5406 err = got_error_from_errno2("lstat", ct->ondisk_path);
5407 goto done;
5409 ct->mode = sb.st_mode;
5412 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5413 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5414 relpath) == -1) {
5415 err = got_error_from_errno("asprintf");
5416 goto done;
5419 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5420 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5421 int is_bad_symlink;
5422 char target_path[PATH_MAX];
5423 ssize_t target_len;
5424 target_len = readlink(ct->ondisk_path, target_path,
5425 sizeof(target_path));
5426 if (target_len == -1) {
5427 err = got_error_from_errno2("readlink",
5428 ct->ondisk_path);
5429 goto done;
5431 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5432 target_len, ct->ondisk_path, a->worktree->root_path);
5433 if (err)
5434 goto done;
5435 if (is_bad_symlink) {
5436 err = got_error_path(ct->ondisk_path,
5437 GOT_ERR_BAD_SYMLINK);
5438 goto done;
5443 ct->status = status;
5444 ct->staged_status = staged_status;
5445 ct->blob_id = NULL; /* will be filled in when blob gets created */
5446 if (ct->status != GOT_STATUS_ADD &&
5447 ct->staged_status != GOT_STATUS_ADD) {
5448 ct->base_blob_id = got_object_id_dup(blob_id);
5449 if (ct->base_blob_id == NULL) {
5450 err = got_error_from_errno("got_object_id_dup");
5451 goto done;
5453 ct->base_commit_id = got_object_id_dup(commit_id);
5454 if (ct->base_commit_id == NULL) {
5455 err = got_error_from_errno("got_object_id_dup");
5456 goto done;
5459 if (ct->staged_status == GOT_STATUS_ADD ||
5460 ct->staged_status == GOT_STATUS_MODIFY) {
5461 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5462 if (ct->staged_blob_id == NULL) {
5463 err = got_error_from_errno("got_object_id_dup");
5464 goto done;
5467 ct->path = strdup(path);
5468 if (ct->path == NULL) {
5469 err = got_error_from_errno("strdup");
5470 goto done;
5472 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5473 if (err)
5474 goto done;
5476 if (a->diff_outfile && ct && new != NULL) {
5477 err = append_ct_diff(ct, &a->diff_header_shown,
5478 a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5479 a->have_staged_files, a->repo, a->worktree);
5480 if (err)
5481 goto done;
5483 done:
5484 if (ct && (err || new == NULL))
5485 free_commitable(ct);
5486 free(parent_path);
5487 free(path);
5488 return err;
5491 static const struct got_error *write_tree(struct got_object_id **, int *,
5492 struct got_tree_object *, const char *, struct got_pathlist_head *,
5493 got_worktree_status_cb status_cb, void *status_arg,
5494 struct got_repository *);
5496 static const struct got_error *
5497 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5498 struct got_tree_entry *te, const char *parent_path,
5499 struct got_pathlist_head *commitable_paths,
5500 got_worktree_status_cb status_cb, void *status_arg,
5501 struct got_repository *repo)
5503 const struct got_error *err = NULL;
5504 struct got_tree_object *subtree;
5505 char *subpath;
5507 if (asprintf(&subpath, "%s%s%s", parent_path,
5508 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5509 return got_error_from_errno("asprintf");
5511 err = got_object_open_as_tree(&subtree, repo, &te->id);
5512 if (err)
5513 return err;
5515 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5516 commitable_paths, status_cb, status_arg, repo);
5517 got_object_tree_close(subtree);
5518 free(subpath);
5519 return err;
5522 static const struct got_error *
5523 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5525 const struct got_error *err = NULL;
5526 char *ct_parent_path = NULL;
5528 *match = 0;
5530 if (strchr(ct->in_repo_path, '/') == NULL) {
5531 *match = got_path_is_root_dir(path);
5532 return NULL;
5535 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5536 if (err)
5537 return err;
5538 *match = (strcmp(path, ct_parent_path) == 0);
5539 free(ct_parent_path);
5540 return err;
5543 static mode_t
5544 get_ct_file_mode(struct got_commitable *ct)
5546 if (S_ISLNK(ct->mode))
5547 return S_IFLNK;
5549 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5552 static const struct got_error *
5553 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5554 struct got_tree_entry *te, struct got_commitable *ct)
5556 const struct got_error *err = NULL;
5558 *new_te = NULL;
5560 err = got_object_tree_entry_dup(new_te, te);
5561 if (err)
5562 goto done;
5564 (*new_te)->mode = get_ct_file_mode(ct);
5566 if (ct->staged_status == GOT_STATUS_MODIFY)
5567 memcpy(&(*new_te)->id, ct->staged_blob_id,
5568 sizeof((*new_te)->id));
5569 else
5570 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5571 done:
5572 if (err && *new_te) {
5573 free(*new_te);
5574 *new_te = NULL;
5576 return err;
5579 static const struct got_error *
5580 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5581 struct got_commitable *ct)
5583 const struct got_error *err = NULL;
5584 char *ct_name = NULL;
5586 *new_te = NULL;
5588 *new_te = calloc(1, sizeof(**new_te));
5589 if (*new_te == NULL)
5590 return got_error_from_errno("calloc");
5592 err = got_path_basename(&ct_name, ct->path);
5593 if (err)
5594 goto done;
5595 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5596 sizeof((*new_te)->name)) {
5597 err = got_error(GOT_ERR_NO_SPACE);
5598 goto done;
5601 (*new_te)->mode = get_ct_file_mode(ct);
5603 if (ct->staged_status == GOT_STATUS_ADD)
5604 memcpy(&(*new_te)->id, ct->staged_blob_id,
5605 sizeof((*new_te)->id));
5606 else
5607 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5608 done:
5609 free(ct_name);
5610 if (err && *new_te) {
5611 free(*new_te);
5612 *new_te = NULL;
5614 return err;
5617 static const struct got_error *
5618 insert_tree_entry(struct got_tree_entry *new_te,
5619 struct got_pathlist_head *paths)
5621 const struct got_error *err = NULL;
5622 struct got_pathlist_entry *new_pe;
5624 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5625 if (err)
5626 return err;
5627 if (new_pe == NULL)
5628 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5629 return NULL;
5632 static const struct got_error *
5633 report_ct_status(struct got_commitable *ct,
5634 got_worktree_status_cb status_cb, void *status_arg)
5636 const char *ct_path = ct->path;
5637 unsigned char status;
5639 if (status_cb == NULL) /* no commit progress output desired */
5640 return NULL;
5642 while (ct_path[0] == '/')
5643 ct_path++;
5645 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5646 status = ct->staged_status;
5647 else
5648 status = ct->status;
5650 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5651 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5654 static const struct got_error *
5655 match_modified_subtree(int *modified, struct got_tree_entry *te,
5656 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5658 const struct got_error *err = NULL;
5659 struct got_pathlist_entry *pe;
5660 char *te_path;
5662 *modified = 0;
5664 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5665 got_path_is_root_dir(base_tree_path) ? "" : "/",
5666 te->name) == -1)
5667 return got_error_from_errno("asprintf");
5669 TAILQ_FOREACH(pe, commitable_paths, entry) {
5670 struct got_commitable *ct = pe->data;
5671 *modified = got_path_is_child(ct->in_repo_path, te_path,
5672 strlen(te_path));
5673 if (*modified)
5674 break;
5677 free(te_path);
5678 return err;
5681 static const struct got_error *
5682 match_deleted_or_modified_ct(struct got_commitable **ctp,
5683 struct got_tree_entry *te, const char *base_tree_path,
5684 struct got_pathlist_head *commitable_paths)
5686 const struct got_error *err = NULL;
5687 struct got_pathlist_entry *pe;
5689 *ctp = NULL;
5691 TAILQ_FOREACH(pe, commitable_paths, entry) {
5692 struct got_commitable *ct = pe->data;
5693 char *ct_name = NULL;
5694 int path_matches;
5696 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5697 if (ct->status != GOT_STATUS_MODIFY &&
5698 ct->status != GOT_STATUS_MODE_CHANGE &&
5699 ct->status != GOT_STATUS_DELETE &&
5700 ct->status != GOT_STATUS_CONFLICT)
5701 continue;
5702 } else {
5703 if (ct->staged_status != GOT_STATUS_MODIFY &&
5704 ct->staged_status != GOT_STATUS_DELETE)
5705 continue;
5708 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5709 continue;
5711 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5712 if (err)
5713 return err;
5714 if (!path_matches)
5715 continue;
5717 err = got_path_basename(&ct_name, pe->path);
5718 if (err)
5719 return err;
5721 if (strcmp(te->name, ct_name) != 0) {
5722 free(ct_name);
5723 continue;
5725 free(ct_name);
5727 *ctp = ct;
5728 break;
5731 return err;
5734 static const struct got_error *
5735 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5736 const char *child_path, const char *path_base_tree,
5737 struct got_pathlist_head *commitable_paths,
5738 got_worktree_status_cb status_cb, void *status_arg,
5739 struct got_repository *repo)
5741 const struct got_error *err = NULL;
5742 struct got_tree_entry *new_te;
5743 char *subtree_path;
5744 struct got_object_id *id = NULL;
5745 int nentries;
5747 *new_tep = NULL;
5749 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5750 got_path_is_root_dir(path_base_tree) ? "" : "/",
5751 child_path) == -1)
5752 return got_error_from_errno("asprintf");
5754 new_te = calloc(1, sizeof(*new_te));
5755 if (new_te == NULL)
5756 return got_error_from_errno("calloc");
5757 new_te->mode = S_IFDIR;
5759 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5760 sizeof(new_te->name)) {
5761 err = got_error(GOT_ERR_NO_SPACE);
5762 goto done;
5764 err = write_tree(&id, &nentries, NULL, subtree_path,
5765 commitable_paths, status_cb, status_arg, repo);
5766 if (err) {
5767 free(new_te);
5768 goto done;
5770 memcpy(&new_te->id, id, sizeof(new_te->id));
5771 done:
5772 free(id);
5773 free(subtree_path);
5774 if (err == NULL)
5775 *new_tep = new_te;
5776 return err;
5779 static const struct got_error *
5780 write_tree(struct got_object_id **new_tree_id, int *nentries,
5781 struct got_tree_object *base_tree, const char *path_base_tree,
5782 struct got_pathlist_head *commitable_paths,
5783 got_worktree_status_cb status_cb, void *status_arg,
5784 struct got_repository *repo)
5786 const struct got_error *err = NULL;
5787 struct got_pathlist_head paths;
5788 struct got_tree_entry *te, *new_te = NULL;
5789 struct got_pathlist_entry *pe;
5791 TAILQ_INIT(&paths);
5792 *nentries = 0;
5794 /* Insert, and recurse into, newly added entries first. */
5795 TAILQ_FOREACH(pe, commitable_paths, entry) {
5796 struct got_commitable *ct = pe->data;
5797 char *child_path = NULL, *slash;
5799 if ((ct->status != GOT_STATUS_ADD &&
5800 ct->staged_status != GOT_STATUS_ADD) ||
5801 (ct->flags & GOT_COMMITABLE_ADDED))
5802 continue;
5804 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5805 strlen(path_base_tree)))
5806 continue;
5808 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5809 ct->in_repo_path);
5810 if (err)
5811 goto done;
5813 slash = strchr(child_path, '/');
5814 if (slash == NULL) {
5815 err = alloc_added_blob_tree_entry(&new_te, ct);
5816 if (err)
5817 goto done;
5818 err = report_ct_status(ct, status_cb, status_arg);
5819 if (err)
5820 goto done;
5821 ct->flags |= GOT_COMMITABLE_ADDED;
5822 err = insert_tree_entry(new_te, &paths);
5823 if (err)
5824 goto done;
5825 (*nentries)++;
5826 } else {
5827 *slash = '\0'; /* trim trailing path components */
5828 if (base_tree == NULL ||
5829 got_object_tree_find_entry(base_tree, child_path)
5830 == NULL) {
5831 err = make_subtree_for_added_blob(&new_te,
5832 child_path, path_base_tree,
5833 commitable_paths, status_cb, status_arg,
5834 repo);
5835 if (err)
5836 goto done;
5837 err = insert_tree_entry(new_te, &paths);
5838 if (err)
5839 goto done;
5840 (*nentries)++;
5845 if (base_tree) {
5846 int i, nbase_entries;
5847 /* Handle modified and deleted entries. */
5848 nbase_entries = got_object_tree_get_nentries(base_tree);
5849 for (i = 0; i < nbase_entries; i++) {
5850 struct got_commitable *ct = NULL;
5852 te = got_object_tree_get_entry(base_tree, i);
5853 if (got_object_tree_entry_is_submodule(te)) {
5854 /* Entry is a submodule; just copy it. */
5855 err = got_object_tree_entry_dup(&new_te, te);
5856 if (err)
5857 goto done;
5858 err = insert_tree_entry(new_te, &paths);
5859 if (err)
5860 goto done;
5861 (*nentries)++;
5862 continue;
5865 if (S_ISDIR(te->mode)) {
5866 int modified;
5867 err = got_object_tree_entry_dup(&new_te, te);
5868 if (err)
5869 goto done;
5870 err = match_modified_subtree(&modified, te,
5871 path_base_tree, commitable_paths);
5872 if (err)
5873 goto done;
5874 /* Avoid recursion into unmodified subtrees. */
5875 if (modified) {
5876 struct got_object_id *new_id;
5877 int nsubentries;
5878 err = write_subtree(&new_id,
5879 &nsubentries, te,
5880 path_base_tree, commitable_paths,
5881 status_cb, status_arg, repo);
5882 if (err)
5883 goto done;
5884 if (nsubentries == 0) {
5885 /* All entries were deleted. */
5886 free(new_id);
5887 continue;
5889 memcpy(&new_te->id, new_id,
5890 sizeof(new_te->id));
5891 free(new_id);
5893 err = insert_tree_entry(new_te, &paths);
5894 if (err)
5895 goto done;
5896 (*nentries)++;
5897 continue;
5900 err = match_deleted_or_modified_ct(&ct, te,
5901 path_base_tree, commitable_paths);
5902 if (err)
5903 goto done;
5904 if (ct) {
5905 /* NB: Deleted entries get dropped here. */
5906 if (ct->status == GOT_STATUS_MODIFY ||
5907 ct->status == GOT_STATUS_MODE_CHANGE ||
5908 ct->status == GOT_STATUS_CONFLICT ||
5909 ct->staged_status == GOT_STATUS_MODIFY) {
5910 err = alloc_modified_blob_tree_entry(
5911 &new_te, te, ct);
5912 if (err)
5913 goto done;
5914 err = insert_tree_entry(new_te, &paths);
5915 if (err)
5916 goto done;
5917 (*nentries)++;
5919 err = report_ct_status(ct, status_cb,
5920 status_arg);
5921 if (err)
5922 goto done;
5923 } else {
5924 /* Entry is unchanged; just copy it. */
5925 err = got_object_tree_entry_dup(&new_te, te);
5926 if (err)
5927 goto done;
5928 err = insert_tree_entry(new_te, &paths);
5929 if (err)
5930 goto done;
5931 (*nentries)++;
5936 /* Write new list of entries; deleted entries have been dropped. */
5937 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5938 done:
5939 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
5940 return err;
5943 static const struct got_error *
5944 update_fileindex_after_commit(struct got_worktree *worktree,
5945 struct got_pathlist_head *commitable_paths,
5946 struct got_object_id *new_base_commit_id,
5947 struct got_fileindex *fileindex, int have_staged_files)
5949 const struct got_error *err = NULL;
5950 struct got_pathlist_entry *pe;
5951 char *relpath = NULL;
5953 TAILQ_FOREACH(pe, commitable_paths, entry) {
5954 struct got_fileindex_entry *ie;
5955 struct got_commitable *ct = pe->data;
5957 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5959 err = got_path_skip_common_ancestor(&relpath,
5960 worktree->root_path, ct->ondisk_path);
5961 if (err)
5962 goto done;
5964 if (ie) {
5965 if (ct->status == GOT_STATUS_DELETE ||
5966 ct->staged_status == GOT_STATUS_DELETE) {
5967 got_fileindex_entry_remove(fileindex, ie);
5968 } else if (ct->staged_status == GOT_STATUS_ADD ||
5969 ct->staged_status == GOT_STATUS_MODIFY) {
5970 got_fileindex_entry_stage_set(ie,
5971 GOT_FILEIDX_STAGE_NONE);
5972 got_fileindex_entry_staged_filetype_set(ie, 0);
5974 err = got_fileindex_entry_update(ie,
5975 worktree->root_fd, relpath,
5976 ct->staged_blob_id->sha1,
5977 new_base_commit_id->sha1,
5978 !have_staged_files);
5979 } else
5980 err = got_fileindex_entry_update(ie,
5981 worktree->root_fd, relpath,
5982 ct->blob_id->sha1,
5983 new_base_commit_id->sha1,
5984 !have_staged_files);
5985 } else {
5986 err = got_fileindex_entry_alloc(&ie, pe->path);
5987 if (err)
5988 goto done;
5989 err = got_fileindex_entry_update(ie,
5990 worktree->root_fd, relpath, ct->blob_id->sha1,
5991 new_base_commit_id->sha1, 1);
5992 if (err) {
5993 got_fileindex_entry_free(ie);
5994 goto done;
5996 err = got_fileindex_entry_add(fileindex, ie);
5997 if (err) {
5998 got_fileindex_entry_free(ie);
5999 goto done;
6002 free(relpath);
6003 relpath = NULL;
6005 done:
6006 free(relpath);
6007 return err;
6011 static const struct got_error *
6012 check_out_of_date(const char *in_repo_path, unsigned char status,
6013 unsigned char staged_status, struct got_object_id *base_blob_id,
6014 struct got_object_id *base_commit_id,
6015 struct got_object_id *head_commit_id, struct got_repository *repo,
6016 int ood_errcode)
6018 const struct got_error *err = NULL;
6019 struct got_commit_object *commit = NULL;
6020 struct got_object_id *id = NULL;
6022 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
6023 /* Trivial case: base commit == head commit */
6024 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
6025 return NULL;
6027 * Ensure file content which local changes were based
6028 * on matches file content in the branch head.
6030 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6031 if (err)
6032 goto done;
6033 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6034 if (err) {
6035 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6036 err = got_error(ood_errcode);
6037 goto done;
6038 } else if (got_object_id_cmp(id, base_blob_id) != 0)
6039 err = got_error(ood_errcode);
6040 } else {
6041 /* Require that added files don't exist in the branch head. */
6042 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6043 if (err)
6044 goto done;
6045 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6046 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
6047 goto done;
6048 err = id ? got_error(ood_errcode) : NULL;
6050 done:
6051 free(id);
6052 if (commit)
6053 got_object_commit_close(commit);
6054 return err;
6057 static const struct got_error *
6058 commit_worktree(struct got_object_id **new_commit_id,
6059 struct got_pathlist_head *commitable_paths,
6060 struct got_object_id *head_commit_id,
6061 struct got_object_id *parent_id2,
6062 struct got_worktree *worktree,
6063 const char *author, const char *committer, char *diff_path,
6064 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6065 got_worktree_status_cb status_cb, void *status_arg,
6066 struct got_repository *repo)
6068 const struct got_error *err = NULL, *unlockerr = NULL;
6069 struct got_pathlist_entry *pe;
6070 const char *head_ref_name = NULL;
6071 struct got_commit_object *head_commit = NULL;
6072 struct got_reference *head_ref2 = NULL;
6073 struct got_object_id *head_commit_id2 = NULL;
6074 struct got_tree_object *head_tree = NULL;
6075 struct got_object_id *new_tree_id = NULL;
6076 int nentries, nparents = 0;
6077 struct got_object_id_queue parent_ids;
6078 struct got_object_qid *pid = NULL;
6079 char *logmsg = NULL;
6080 time_t timestamp;
6082 *new_commit_id = NULL;
6084 STAILQ_INIT(&parent_ids);
6086 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6087 if (err)
6088 goto done;
6090 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6091 if (err)
6092 goto done;
6094 if (commit_msg_cb != NULL) {
6095 err = commit_msg_cb(commitable_paths, diff_path,
6096 &logmsg, commit_arg);
6097 if (err)
6098 goto done;
6101 if (logmsg == NULL || strlen(logmsg) == 0) {
6102 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6103 goto done;
6106 /* Create blobs from added and modified files and record their IDs. */
6107 TAILQ_FOREACH(pe, commitable_paths, entry) {
6108 struct got_commitable *ct = pe->data;
6109 char *ondisk_path;
6111 /* Blobs for staged files already exist. */
6112 if (ct->staged_status == GOT_STATUS_ADD ||
6113 ct->staged_status == GOT_STATUS_MODIFY)
6114 continue;
6116 if (ct->status != GOT_STATUS_ADD &&
6117 ct->status != GOT_STATUS_MODIFY &&
6118 ct->status != GOT_STATUS_MODE_CHANGE &&
6119 ct->status != GOT_STATUS_CONFLICT)
6120 continue;
6122 if (asprintf(&ondisk_path, "%s/%s",
6123 worktree->root_path, pe->path) == -1) {
6124 err = got_error_from_errno("asprintf");
6125 goto done;
6127 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6128 free(ondisk_path);
6129 if (err)
6130 goto done;
6133 /* Recursively write new tree objects. */
6134 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6135 commitable_paths, status_cb, status_arg, repo);
6136 if (err)
6137 goto done;
6139 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
6140 if (err)
6141 goto done;
6142 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6143 nparents++;
6144 if (parent_id2) {
6145 err = got_object_qid_alloc(&pid, parent_id2);
6146 if (err)
6147 goto done;
6148 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6149 nparents++;
6151 timestamp = time(NULL);
6152 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6153 nparents, author, timestamp, committer, timestamp, logmsg, repo);
6154 if (logmsg != NULL)
6155 free(logmsg);
6156 if (err)
6157 goto done;
6159 /* Check if a concurrent commit to our branch has occurred. */
6160 head_ref_name = got_worktree_get_head_ref_name(worktree);
6161 if (head_ref_name == NULL) {
6162 err = got_error_from_errno("got_worktree_get_head_ref_name");
6163 goto done;
6165 /* Lock the reference here to prevent concurrent modification. */
6166 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6167 if (err)
6168 goto done;
6169 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6170 if (err)
6171 goto done;
6172 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6173 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6174 goto done;
6176 /* Update branch head in repository. */
6177 err = got_ref_change_ref(head_ref2, *new_commit_id);
6178 if (err)
6179 goto done;
6180 err = got_ref_write(head_ref2, repo);
6181 if (err)
6182 goto done;
6184 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6185 if (err)
6186 goto done;
6188 err = ref_base_commit(worktree, repo);
6189 if (err)
6190 goto done;
6191 done:
6192 got_object_id_queue_free(&parent_ids);
6193 if (head_tree)
6194 got_object_tree_close(head_tree);
6195 if (head_commit)
6196 got_object_commit_close(head_commit);
6197 free(head_commit_id2);
6198 if (head_ref2) {
6199 unlockerr = got_ref_unlock(head_ref2);
6200 if (unlockerr && err == NULL)
6201 err = unlockerr;
6202 got_ref_close(head_ref2);
6204 return err;
6207 static const struct got_error *
6208 check_path_is_commitable(const char *path,
6209 struct got_pathlist_head *commitable_paths)
6211 struct got_pathlist_entry *cpe = NULL;
6212 size_t path_len = strlen(path);
6214 TAILQ_FOREACH(cpe, commitable_paths, entry) {
6215 struct got_commitable *ct = cpe->data;
6216 const char *ct_path = ct->path;
6218 while (ct_path[0] == '/')
6219 ct_path++;
6221 if (strcmp(path, ct_path) == 0 ||
6222 got_path_is_child(ct_path, path, path_len))
6223 break;
6226 if (cpe == NULL)
6227 return got_error_path(path, GOT_ERR_BAD_PATH);
6229 return NULL;
6232 static const struct got_error *
6233 check_staged_file(void *arg, struct got_fileindex_entry *ie)
6235 int *have_staged_files = arg;
6237 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6238 *have_staged_files = 1;
6239 return got_error(GOT_ERR_CANCELLED);
6242 return NULL;
6245 static const struct got_error *
6246 check_non_staged_files(struct got_fileindex *fileindex,
6247 struct got_pathlist_head *paths)
6249 struct got_pathlist_entry *pe;
6250 struct got_fileindex_entry *ie;
6252 TAILQ_FOREACH(pe, paths, entry) {
6253 if (pe->path[0] == '\0')
6254 continue;
6255 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6256 if (ie == NULL)
6257 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6258 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6259 return got_error_path(pe->path,
6260 GOT_ERR_FILE_NOT_STAGED);
6263 return NULL;
6266 const struct got_error *
6267 got_worktree_commit(struct got_object_id **new_commit_id,
6268 struct got_worktree *worktree, struct got_pathlist_head *paths,
6269 const char *author, const char *committer, int allow_bad_symlinks,
6270 int show_diff, int commit_conflicts,
6271 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6272 got_worktree_status_cb status_cb, void *status_arg,
6273 struct got_repository *repo)
6275 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6276 struct got_fileindex *fileindex = NULL;
6277 char *fileindex_path = NULL;
6278 struct got_pathlist_head commitable_paths;
6279 struct collect_commitables_arg cc_arg;
6280 struct got_pathlist_entry *pe;
6281 struct got_reference *head_ref = NULL;
6282 struct got_object_id *head_commit_id = NULL;
6283 char *diff_path = NULL;
6284 int have_staged_files = 0;
6286 *new_commit_id = NULL;
6288 memset(&cc_arg, 0, sizeof(cc_arg));
6289 TAILQ_INIT(&commitable_paths);
6291 err = lock_worktree(worktree, LOCK_EX);
6292 if (err)
6293 goto done;
6295 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6296 if (err)
6297 goto done;
6299 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6300 if (err)
6301 goto done;
6303 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6304 if (err)
6305 goto done;
6307 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6308 &have_staged_files);
6309 if (err && err->code != GOT_ERR_CANCELLED)
6310 goto done;
6311 if (have_staged_files) {
6312 err = check_non_staged_files(fileindex, paths);
6313 if (err)
6314 goto done;
6317 cc_arg.commitable_paths = &commitable_paths;
6318 cc_arg.worktree = worktree;
6319 cc_arg.fileindex = fileindex;
6320 cc_arg.repo = repo;
6321 cc_arg.have_staged_files = have_staged_files;
6322 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6323 cc_arg.diff_header_shown = 0;
6324 cc_arg.commit_conflicts = commit_conflicts;
6325 if (show_diff) {
6326 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6327 GOT_TMPDIR_STR "/got", ".diff");
6328 if (err)
6329 goto done;
6330 cc_arg.f1 = got_opentemp();
6331 if (cc_arg.f1 == NULL) {
6332 err = got_error_from_errno("got_opentemp");
6333 goto done;
6335 cc_arg.f2 = got_opentemp();
6336 if (cc_arg.f2 == NULL) {
6337 err = got_error_from_errno("got_opentemp");
6338 goto done;
6342 TAILQ_FOREACH(pe, paths, entry) {
6343 err = worktree_status(worktree, pe->path, fileindex, repo,
6344 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6345 if (err)
6346 goto done;
6349 if (show_diff) {
6350 if (fflush(cc_arg.diff_outfile) == EOF) {
6351 err = got_error_from_errno("fflush");
6352 goto done;
6356 if (TAILQ_EMPTY(&commitable_paths)) {
6357 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6358 goto done;
6361 TAILQ_FOREACH(pe, paths, entry) {
6362 err = check_path_is_commitable(pe->path, &commitable_paths);
6363 if (err)
6364 goto done;
6367 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6368 struct got_commitable *ct = pe->data;
6369 const char *ct_path = ct->in_repo_path;
6371 while (ct_path[0] == '/')
6372 ct_path++;
6373 err = check_out_of_date(ct_path, ct->status,
6374 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6375 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6376 if (err)
6377 goto done;
6381 err = commit_worktree(new_commit_id, &commitable_paths,
6382 head_commit_id, NULL, worktree, author, committer,
6383 (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6384 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6385 if (err)
6386 goto done;
6388 err = update_fileindex_after_commit(worktree, &commitable_paths,
6389 *new_commit_id, fileindex, have_staged_files);
6390 sync_err = sync_fileindex(fileindex, fileindex_path);
6391 if (sync_err && err == NULL)
6392 err = sync_err;
6393 done:
6394 if (fileindex)
6395 got_fileindex_free(fileindex);
6396 free(fileindex_path);
6397 unlockerr = lock_worktree(worktree, LOCK_SH);
6398 if (unlockerr && err == NULL)
6399 err = unlockerr;
6400 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6401 struct got_commitable *ct = pe->data;
6403 free_commitable(ct);
6405 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6406 if (diff_path && unlink(diff_path) == -1 && err == NULL)
6407 err = got_error_from_errno2("unlink", diff_path);
6408 free(diff_path);
6409 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6410 err == NULL)
6411 err = got_error_from_errno("fclose");
6412 return err;
6415 const char *
6416 got_commitable_get_path(struct got_commitable *ct)
6418 return ct->path;
6421 unsigned int
6422 got_commitable_get_status(struct got_commitable *ct)
6424 return ct->status;
6427 struct check_rebase_ok_arg {
6428 struct got_worktree *worktree;
6429 struct got_repository *repo;
6432 static const struct got_error *
6433 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6435 const struct got_error *err = NULL;
6436 struct check_rebase_ok_arg *a = arg;
6437 unsigned char status;
6438 struct stat sb;
6439 char *ondisk_path;
6441 /* Reject rebase of a work tree with mixed base commits. */
6442 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6443 SHA1_DIGEST_LENGTH))
6444 return got_error(GOT_ERR_MIXED_COMMITS);
6446 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6447 == -1)
6448 return got_error_from_errno("asprintf");
6450 /* Reject rebase of a work tree with modified or staged files. */
6451 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6452 free(ondisk_path);
6453 if (err)
6454 return err;
6456 if (status != GOT_STATUS_NO_CHANGE)
6457 return got_error(GOT_ERR_MODIFIED);
6458 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6459 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6461 return NULL;
6464 const struct got_error *
6465 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6466 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6467 struct got_worktree *worktree, struct got_reference *branch,
6468 struct got_repository *repo)
6470 const struct got_error *err = NULL;
6471 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6472 char *branch_ref_name = NULL;
6473 char *fileindex_path = NULL;
6474 struct check_rebase_ok_arg ok_arg;
6475 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6476 struct got_object_id *wt_branch_tip = NULL;
6478 *new_base_branch_ref = NULL;
6479 *tmp_branch = NULL;
6480 *fileindex = NULL;
6482 err = lock_worktree(worktree, LOCK_EX);
6483 if (err)
6484 return err;
6486 err = open_fileindex(fileindex, &fileindex_path, worktree);
6487 if (err)
6488 goto done;
6490 ok_arg.worktree = worktree;
6491 ok_arg.repo = repo;
6492 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6493 &ok_arg);
6494 if (err)
6495 goto done;
6497 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6498 if (err)
6499 goto done;
6501 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6502 if (err)
6503 goto done;
6505 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6506 if (err)
6507 goto done;
6509 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6510 0);
6511 if (err)
6512 goto done;
6514 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6515 if (err)
6516 goto done;
6517 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6518 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6519 goto done;
6522 err = got_ref_alloc_symref(new_base_branch_ref,
6523 new_base_branch_ref_name, wt_branch);
6524 if (err)
6525 goto done;
6526 err = got_ref_write(*new_base_branch_ref, repo);
6527 if (err)
6528 goto done;
6530 /* TODO Lock original branch's ref while rebasing? */
6532 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6533 if (err)
6534 goto done;
6536 err = got_ref_write(branch_ref, repo);
6537 if (err)
6538 goto done;
6540 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6541 worktree->base_commit_id);
6542 if (err)
6543 goto done;
6544 err = got_ref_write(*tmp_branch, repo);
6545 if (err)
6546 goto done;
6548 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6549 if (err)
6550 goto done;
6551 done:
6552 free(fileindex_path);
6553 free(tmp_branch_name);
6554 free(new_base_branch_ref_name);
6555 free(branch_ref_name);
6556 if (branch_ref)
6557 got_ref_close(branch_ref);
6558 if (wt_branch)
6559 got_ref_close(wt_branch);
6560 free(wt_branch_tip);
6561 if (err) {
6562 if (*new_base_branch_ref) {
6563 got_ref_close(*new_base_branch_ref);
6564 *new_base_branch_ref = NULL;
6566 if (*tmp_branch) {
6567 got_ref_close(*tmp_branch);
6568 *tmp_branch = NULL;
6570 if (*fileindex) {
6571 got_fileindex_free(*fileindex);
6572 *fileindex = NULL;
6574 lock_worktree(worktree, LOCK_SH);
6576 return err;
6579 const struct got_error *
6580 got_worktree_rebase_continue(struct got_object_id **commit_id,
6581 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6582 struct got_reference **branch, struct got_fileindex **fileindex,
6583 struct got_worktree *worktree, struct got_repository *repo)
6585 const struct got_error *err;
6586 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6587 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6588 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6589 char *fileindex_path = NULL;
6590 int have_staged_files = 0;
6592 *commit_id = NULL;
6593 *new_base_branch = NULL;
6594 *tmp_branch = NULL;
6595 *branch = NULL;
6596 *fileindex = NULL;
6598 err = lock_worktree(worktree, LOCK_EX);
6599 if (err)
6600 return err;
6602 err = open_fileindex(fileindex, &fileindex_path, worktree);
6603 if (err)
6604 goto done;
6606 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6607 &have_staged_files);
6608 if (err && err->code != GOT_ERR_CANCELLED)
6609 goto done;
6610 if (have_staged_files) {
6611 err = got_error(GOT_ERR_STAGED_PATHS);
6612 goto done;
6615 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6616 if (err)
6617 goto done;
6619 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6620 if (err)
6621 goto done;
6623 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6624 if (err)
6625 goto done;
6627 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6628 if (err)
6629 goto done;
6631 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6632 if (err)
6633 goto done;
6635 err = got_ref_open(branch, repo,
6636 got_ref_get_symref_target(branch_ref), 0);
6637 if (err)
6638 goto done;
6640 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6641 if (err)
6642 goto done;
6644 err = got_ref_resolve(commit_id, repo, commit_ref);
6645 if (err)
6646 goto done;
6648 err = got_ref_open(new_base_branch, repo,
6649 new_base_branch_ref_name, 0);
6650 if (err)
6651 goto done;
6653 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6654 if (err)
6655 goto done;
6656 done:
6657 free(commit_ref_name);
6658 free(branch_ref_name);
6659 free(fileindex_path);
6660 if (commit_ref)
6661 got_ref_close(commit_ref);
6662 if (branch_ref)
6663 got_ref_close(branch_ref);
6664 if (err) {
6665 free(*commit_id);
6666 *commit_id = NULL;
6667 if (*tmp_branch) {
6668 got_ref_close(*tmp_branch);
6669 *tmp_branch = NULL;
6671 if (*new_base_branch) {
6672 got_ref_close(*new_base_branch);
6673 *new_base_branch = NULL;
6675 if (*branch) {
6676 got_ref_close(*branch);
6677 *branch = NULL;
6679 if (*fileindex) {
6680 got_fileindex_free(*fileindex);
6681 *fileindex = NULL;
6683 lock_worktree(worktree, LOCK_SH);
6685 return err;
6688 const struct got_error *
6689 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6691 const struct got_error *err;
6692 char *tmp_branch_name = NULL;
6694 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6695 if (err)
6696 return err;
6698 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6699 free(tmp_branch_name);
6700 return NULL;
6703 static const struct got_error *
6704 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6705 const char *diff_path, char **logmsg, void *arg)
6707 *logmsg = arg;
6708 return NULL;
6711 static const struct got_error *
6712 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6713 const char *path, struct got_object_id *blob_id,
6714 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6715 int dirfd, const char *de_name)
6717 return NULL;
6720 struct collect_merged_paths_arg {
6721 got_worktree_checkout_cb progress_cb;
6722 void *progress_arg;
6723 struct got_pathlist_head *merged_paths;
6726 static const struct got_error *
6727 collect_merged_paths(void *arg, unsigned char status, const char *path)
6729 const struct got_error *err;
6730 struct collect_merged_paths_arg *a = arg;
6731 char *p;
6732 struct got_pathlist_entry *new;
6734 err = (*a->progress_cb)(a->progress_arg, status, path);
6735 if (err)
6736 return err;
6738 if (status != GOT_STATUS_MERGE &&
6739 status != GOT_STATUS_ADD &&
6740 status != GOT_STATUS_DELETE &&
6741 status != GOT_STATUS_CONFLICT)
6742 return NULL;
6744 p = strdup(path);
6745 if (p == NULL)
6746 return got_error_from_errno("strdup");
6748 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6749 if (err || new == NULL)
6750 free(p);
6751 return err;
6754 static const struct got_error *
6755 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6756 int is_rebase, struct got_repository *repo)
6758 const struct got_error *err;
6759 struct got_reference *commit_ref = NULL;
6761 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6762 if (err) {
6763 if (err->code != GOT_ERR_NOT_REF)
6764 goto done;
6765 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6766 if (err)
6767 goto done;
6768 err = got_ref_write(commit_ref, repo);
6769 if (err)
6770 goto done;
6771 } else if (is_rebase) {
6772 struct got_object_id *stored_id;
6773 int cmp;
6775 err = got_ref_resolve(&stored_id, repo, commit_ref);
6776 if (err)
6777 goto done;
6778 cmp = got_object_id_cmp(commit_id, stored_id);
6779 free(stored_id);
6780 if (cmp != 0) {
6781 err = got_error(GOT_ERR_REBASE_COMMITID);
6782 goto done;
6785 done:
6786 if (commit_ref)
6787 got_ref_close(commit_ref);
6788 return err;
6791 static const struct got_error *
6792 rebase_merge_files(struct got_pathlist_head *merged_paths,
6793 const char *commit_ref_name, struct got_worktree *worktree,
6794 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6795 struct got_object_id *commit_id, 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 struct got_reference *commit_ref = NULL;
6801 struct collect_merged_paths_arg cmp_arg;
6802 char *fileindex_path;
6804 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6806 err = get_fileindex_path(&fileindex_path, worktree);
6807 if (err)
6808 return err;
6810 cmp_arg.progress_cb = progress_cb;
6811 cmp_arg.progress_arg = progress_arg;
6812 cmp_arg.merged_paths = merged_paths;
6813 err = merge_files(worktree, fileindex, fileindex_path,
6814 parent_commit_id, commit_id, repo, collect_merged_paths,
6815 &cmp_arg, cancel_cb, cancel_arg);
6816 if (commit_ref)
6817 got_ref_close(commit_ref);
6818 return err;
6821 const struct got_error *
6822 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6823 struct got_worktree *worktree, struct got_fileindex *fileindex,
6824 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6825 struct got_repository *repo,
6826 got_worktree_checkout_cb progress_cb, void *progress_arg,
6827 got_cancel_cb cancel_cb, void *cancel_arg)
6829 const struct got_error *err;
6830 char *commit_ref_name;
6832 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6833 if (err)
6834 return err;
6836 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6837 if (err)
6838 goto done;
6840 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6841 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6842 progress_arg, cancel_cb, cancel_arg);
6843 done:
6844 free(commit_ref_name);
6845 return err;
6848 const struct got_error *
6849 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6850 struct got_worktree *worktree, struct got_fileindex *fileindex,
6851 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6852 struct got_repository *repo,
6853 got_worktree_checkout_cb progress_cb, void *progress_arg,
6854 got_cancel_cb cancel_cb, void *cancel_arg)
6856 const struct got_error *err;
6857 char *commit_ref_name;
6859 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6860 if (err)
6861 return err;
6863 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6864 if (err)
6865 goto done;
6867 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6868 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6869 progress_arg, cancel_cb, cancel_arg);
6870 done:
6871 free(commit_ref_name);
6872 return err;
6875 static const struct got_error *
6876 rebase_commit(struct got_object_id **new_commit_id,
6877 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6878 struct got_worktree *worktree, struct got_fileindex *fileindex,
6879 struct got_reference *tmp_branch, const char *committer,
6880 struct got_commit_object *orig_commit, const char *new_logmsg,
6881 int allow_conflict, struct got_repository *repo)
6883 const struct got_error *err, *sync_err;
6884 struct got_pathlist_head commitable_paths;
6885 struct collect_commitables_arg cc_arg;
6886 char *fileindex_path = NULL;
6887 struct got_reference *head_ref = NULL;
6888 struct got_object_id *head_commit_id = NULL;
6889 char *logmsg = NULL;
6891 memset(&cc_arg, 0, sizeof(cc_arg));
6892 TAILQ_INIT(&commitable_paths);
6893 *new_commit_id = NULL;
6895 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6897 err = get_fileindex_path(&fileindex_path, worktree);
6898 if (err)
6899 return err;
6901 cc_arg.commitable_paths = &commitable_paths;
6902 cc_arg.worktree = worktree;
6903 cc_arg.repo = repo;
6904 cc_arg.have_staged_files = 0;
6905 cc_arg.commit_conflicts = allow_conflict;
6907 * If possible get the status of individual files directly to
6908 * avoid crawling the entire work tree once per rebased commit.
6910 * Ideally, merged_paths would contain a list of commitables
6911 * we could use so we could skip worktree_status() entirely.
6912 * However, we would then need carefully keep track of cumulative
6913 * effects of operations such as file additions and deletions
6914 * in 'got histedit -f' (folding multiple commits into one),
6915 * and this extra complexity is not really worth it.
6917 if (merged_paths) {
6918 struct got_pathlist_entry *pe;
6919 TAILQ_FOREACH(pe, merged_paths, entry) {
6920 err = worktree_status(worktree, pe->path, fileindex,
6921 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6922 0);
6923 if (err)
6924 goto done;
6926 } else {
6927 err = worktree_status(worktree, "", fileindex, repo,
6928 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6929 if (err)
6930 goto done;
6933 if (TAILQ_EMPTY(&commitable_paths)) {
6934 /* No-op change; commit will be elided. */
6935 err = got_ref_delete(commit_ref, repo);
6936 if (err)
6937 goto done;
6938 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6939 goto done;
6942 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6943 if (err)
6944 goto done;
6946 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6947 if (err)
6948 goto done;
6950 if (new_logmsg) {
6951 logmsg = strdup(new_logmsg);
6952 if (logmsg == NULL) {
6953 err = got_error_from_errno("strdup");
6954 goto done;
6956 } else {
6957 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6958 if (err)
6959 goto done;
6962 /* NB: commit_worktree will call free(logmsg) */
6963 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6964 NULL, worktree, got_object_commit_get_author(orig_commit),
6965 committer ? committer :
6966 got_object_commit_get_committer(orig_commit), NULL,
6967 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6968 if (err)
6969 goto done;
6971 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6972 if (err)
6973 goto done;
6975 err = got_ref_delete(commit_ref, repo);
6976 if (err)
6977 goto done;
6979 err = update_fileindex_after_commit(worktree, &commitable_paths,
6980 *new_commit_id, fileindex, 0);
6981 sync_err = sync_fileindex(fileindex, fileindex_path);
6982 if (sync_err && err == NULL)
6983 err = sync_err;
6984 done:
6985 free(fileindex_path);
6986 free(head_commit_id);
6987 if (head_ref)
6988 got_ref_close(head_ref);
6989 if (err) {
6990 free(*new_commit_id);
6991 *new_commit_id = NULL;
6993 return err;
6996 const struct got_error *
6997 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6998 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6999 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7000 const char *committer, struct got_commit_object *orig_commit,
7001 struct got_object_id *orig_commit_id, int allow_conflict,
7002 struct got_repository *repo)
7004 const struct got_error *err;
7005 char *commit_ref_name;
7006 struct got_reference *commit_ref = NULL;
7007 struct got_object_id *commit_id = NULL;
7009 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7010 if (err)
7011 return err;
7013 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7014 if (err)
7015 goto done;
7016 err = got_ref_resolve(&commit_id, repo, commit_ref);
7017 if (err)
7018 goto done;
7019 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
7020 err = got_error(GOT_ERR_REBASE_COMMITID);
7021 goto done;
7024 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7025 worktree, fileindex, tmp_branch, committer, orig_commit,
7026 NULL, allow_conflict, repo);
7027 done:
7028 if (commit_ref)
7029 got_ref_close(commit_ref);
7030 free(commit_ref_name);
7031 free(commit_id);
7032 return err;
7035 const struct got_error *
7036 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
7037 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7038 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7039 const char *committer, struct got_commit_object *orig_commit,
7040 struct got_object_id *orig_commit_id, const char *new_logmsg,
7041 int allow_conflict, struct got_repository *repo)
7043 const struct got_error *err;
7044 char *commit_ref_name;
7045 struct got_reference *commit_ref = NULL;
7047 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7048 if (err)
7049 return err;
7051 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7052 if (err)
7053 goto done;
7055 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7056 worktree, fileindex, tmp_branch, committer, orig_commit,
7057 new_logmsg, allow_conflict, repo);
7058 done:
7059 if (commit_ref)
7060 got_ref_close(commit_ref);
7061 free(commit_ref_name);
7062 return err;
7065 const struct got_error *
7066 got_worktree_rebase_postpone(struct got_worktree *worktree,
7067 struct got_fileindex *fileindex)
7069 if (fileindex)
7070 got_fileindex_free(fileindex);
7071 return lock_worktree(worktree, LOCK_SH);
7074 static const struct got_error *
7075 delete_ref(const char *name, struct got_repository *repo)
7077 const struct got_error *err;
7078 struct got_reference *ref;
7080 err = got_ref_open(&ref, repo, name, 0);
7081 if (err) {
7082 if (err->code == GOT_ERR_NOT_REF)
7083 return NULL;
7084 return err;
7087 err = got_ref_delete(ref, repo);
7088 got_ref_close(ref);
7089 return err;
7092 static const struct got_error *
7093 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7095 const struct got_error *err;
7096 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7097 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7099 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7100 if (err)
7101 goto done;
7102 err = delete_ref(tmp_branch_name, repo);
7103 if (err)
7104 goto done;
7106 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7107 if (err)
7108 goto done;
7109 err = delete_ref(new_base_branch_ref_name, repo);
7110 if (err)
7111 goto done;
7113 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7114 if (err)
7115 goto done;
7116 err = delete_ref(branch_ref_name, repo);
7117 if (err)
7118 goto done;
7120 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7121 if (err)
7122 goto done;
7123 err = delete_ref(commit_ref_name, repo);
7124 if (err)
7125 goto done;
7127 done:
7128 free(tmp_branch_name);
7129 free(new_base_branch_ref_name);
7130 free(branch_ref_name);
7131 free(commit_ref_name);
7132 return err;
7135 static const struct got_error *
7136 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7137 struct got_object_id *new_commit_id, struct got_repository *repo)
7139 const struct got_error *err;
7140 struct got_reference *ref = NULL;
7141 struct got_object_id *old_commit_id = NULL;
7142 const char *branch_name = NULL;
7143 char *new_id_str = NULL;
7144 char *refname = NULL;
7146 branch_name = got_ref_get_name(branch);
7147 if (strncmp(branch_name, "refs/heads/", 11) != 0)
7148 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7149 branch_name += 11;
7151 err = got_object_id_str(&new_id_str, new_commit_id);
7152 if (err)
7153 return err;
7155 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7156 new_id_str) == -1) {
7157 err = got_error_from_errno("asprintf");
7158 goto done;
7161 err = got_ref_resolve(&old_commit_id, repo, branch);
7162 if (err)
7163 goto done;
7165 err = got_ref_alloc(&ref, refname, old_commit_id);
7166 if (err)
7167 goto done;
7169 err = got_ref_write(ref, repo);
7170 done:
7171 free(new_id_str);
7172 free(refname);
7173 free(old_commit_id);
7174 if (ref)
7175 got_ref_close(ref);
7176 return err;
7179 const struct got_error *
7180 got_worktree_rebase_complete(struct got_worktree *worktree,
7181 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7182 struct got_reference *rebased_branch, struct got_repository *repo,
7183 int create_backup)
7185 const struct got_error *err, *unlockerr, *sync_err;
7186 struct got_object_id *new_head_commit_id = NULL;
7187 char *fileindex_path = NULL;
7189 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7190 if (err)
7191 return err;
7193 if (create_backup) {
7194 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7195 rebased_branch, new_head_commit_id, repo);
7196 if (err)
7197 goto done;
7200 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7201 if (err)
7202 goto done;
7204 err = got_ref_write(rebased_branch, repo);
7205 if (err)
7206 goto done;
7208 err = got_worktree_set_head_ref(worktree, rebased_branch);
7209 if (err)
7210 goto done;
7212 err = delete_rebase_refs(worktree, repo);
7213 if (err)
7214 goto done;
7216 err = get_fileindex_path(&fileindex_path, worktree);
7217 if (err)
7218 goto done;
7219 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7220 sync_err = sync_fileindex(fileindex, fileindex_path);
7221 if (sync_err && err == NULL)
7222 err = sync_err;
7223 done:
7224 got_fileindex_free(fileindex);
7225 free(fileindex_path);
7226 free(new_head_commit_id);
7227 unlockerr = lock_worktree(worktree, LOCK_SH);
7228 if (unlockerr && err == NULL)
7229 err = unlockerr;
7230 return err;
7233 const struct got_error *
7234 got_worktree_rebase_abort(struct got_worktree *worktree,
7235 struct got_fileindex *fileindex, struct got_repository *repo,
7236 struct got_reference *new_base_branch,
7237 got_worktree_checkout_cb progress_cb, void *progress_arg)
7239 const struct got_error *err, *unlockerr, *sync_err;
7240 struct got_reference *resolved = NULL;
7241 struct got_object_id *commit_id = NULL;
7242 struct got_commit_object *commit = NULL;
7243 char *fileindex_path = NULL;
7244 struct revert_file_args rfa;
7245 struct got_object_id *tree_id = NULL;
7247 err = lock_worktree(worktree, LOCK_EX);
7248 if (err)
7249 return err;
7251 err = got_object_open_as_commit(&commit, repo,
7252 worktree->base_commit_id);
7253 if (err)
7254 goto done;
7256 err = got_ref_open(&resolved, repo,
7257 got_ref_get_symref_target(new_base_branch), 0);
7258 if (err)
7259 goto done;
7261 err = got_worktree_set_head_ref(worktree, resolved);
7262 if (err)
7263 goto done;
7266 * XXX commits to the base branch could have happened while
7267 * we were busy rebasing; should we store the original commit ID
7268 * when rebase begins and read it back here?
7270 err = got_ref_resolve(&commit_id, repo, resolved);
7271 if (err)
7272 goto done;
7274 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7275 if (err)
7276 goto done;
7278 err = got_object_id_by_path(&tree_id, repo, commit,
7279 worktree->path_prefix);
7280 if (err)
7281 goto done;
7283 err = delete_rebase_refs(worktree, repo);
7284 if (err)
7285 goto done;
7287 err = get_fileindex_path(&fileindex_path, worktree);
7288 if (err)
7289 goto done;
7291 rfa.worktree = worktree;
7292 rfa.fileindex = fileindex;
7293 rfa.progress_cb = progress_cb;
7294 rfa.progress_arg = progress_arg;
7295 rfa.patch_cb = NULL;
7296 rfa.patch_arg = NULL;
7297 rfa.repo = repo;
7298 rfa.unlink_added_files = 0;
7299 err = worktree_status(worktree, "", fileindex, repo,
7300 revert_file, &rfa, NULL, NULL, 1, 0);
7301 if (err)
7302 goto sync;
7304 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7305 repo, progress_cb, progress_arg, NULL, NULL);
7306 sync:
7307 sync_err = sync_fileindex(fileindex, fileindex_path);
7308 if (sync_err && err == NULL)
7309 err = sync_err;
7310 done:
7311 got_ref_close(resolved);
7312 free(tree_id);
7313 free(commit_id);
7314 if (commit)
7315 got_object_commit_close(commit);
7316 if (fileindex)
7317 got_fileindex_free(fileindex);
7318 free(fileindex_path);
7320 unlockerr = lock_worktree(worktree, LOCK_SH);
7321 if (unlockerr && err == NULL)
7322 err = unlockerr;
7323 return err;
7326 const struct got_error *
7327 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7328 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7329 struct got_fileindex **fileindex, struct got_worktree *worktree,
7330 struct got_repository *repo)
7332 const struct got_error *err = NULL;
7333 char *tmp_branch_name = NULL;
7334 char *branch_ref_name = NULL;
7335 char *base_commit_ref_name = NULL;
7336 char *fileindex_path = NULL;
7337 struct check_rebase_ok_arg ok_arg;
7338 struct got_reference *wt_branch = NULL;
7339 struct got_reference *base_commit_ref = NULL;
7341 *tmp_branch = NULL;
7342 *branch_ref = NULL;
7343 *base_commit_id = NULL;
7344 *fileindex = NULL;
7346 err = lock_worktree(worktree, LOCK_EX);
7347 if (err)
7348 return err;
7350 err = open_fileindex(fileindex, &fileindex_path, worktree);
7351 if (err)
7352 goto done;
7354 ok_arg.worktree = worktree;
7355 ok_arg.repo = repo;
7356 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7357 &ok_arg);
7358 if (err)
7359 goto done;
7361 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7362 if (err)
7363 goto done;
7365 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7366 if (err)
7367 goto done;
7369 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7370 worktree);
7371 if (err)
7372 goto done;
7374 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7375 0);
7376 if (err)
7377 goto done;
7379 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7380 if (err)
7381 goto done;
7383 err = got_ref_write(*branch_ref, repo);
7384 if (err)
7385 goto done;
7387 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7388 worktree->base_commit_id);
7389 if (err)
7390 goto done;
7391 err = got_ref_write(base_commit_ref, repo);
7392 if (err)
7393 goto done;
7394 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7395 if (*base_commit_id == NULL) {
7396 err = got_error_from_errno("got_object_id_dup");
7397 goto done;
7400 err = got_ref_alloc(tmp_branch, tmp_branch_name,
7401 worktree->base_commit_id);
7402 if (err)
7403 goto done;
7404 err = got_ref_write(*tmp_branch, repo);
7405 if (err)
7406 goto done;
7408 err = got_worktree_set_head_ref(worktree, *tmp_branch);
7409 if (err)
7410 goto done;
7411 done:
7412 free(fileindex_path);
7413 free(tmp_branch_name);
7414 free(branch_ref_name);
7415 free(base_commit_ref_name);
7416 if (wt_branch)
7417 got_ref_close(wt_branch);
7418 if (err) {
7419 if (*branch_ref) {
7420 got_ref_close(*branch_ref);
7421 *branch_ref = NULL;
7423 if (*tmp_branch) {
7424 got_ref_close(*tmp_branch);
7425 *tmp_branch = NULL;
7427 free(*base_commit_id);
7428 if (*fileindex) {
7429 got_fileindex_free(*fileindex);
7430 *fileindex = NULL;
7432 lock_worktree(worktree, LOCK_SH);
7434 return err;
7437 const struct got_error *
7438 got_worktree_histedit_postpone(struct got_worktree *worktree,
7439 struct got_fileindex *fileindex)
7441 if (fileindex)
7442 got_fileindex_free(fileindex);
7443 return lock_worktree(worktree, LOCK_SH);
7446 const struct got_error *
7447 got_worktree_histedit_in_progress(int *in_progress,
7448 struct got_worktree *worktree)
7450 const struct got_error *err;
7451 char *tmp_branch_name = NULL;
7453 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7454 if (err)
7455 return err;
7457 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7458 free(tmp_branch_name);
7459 return NULL;
7462 const struct got_error *
7463 got_worktree_histedit_continue(struct got_object_id **commit_id,
7464 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7465 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7466 struct got_worktree *worktree, struct got_repository *repo)
7468 const struct got_error *err;
7469 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7470 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7471 struct got_reference *commit_ref = NULL;
7472 struct got_reference *base_commit_ref = NULL;
7473 char *fileindex_path = NULL;
7474 int have_staged_files = 0;
7476 *commit_id = NULL;
7477 *tmp_branch = NULL;
7478 *base_commit_id = NULL;
7479 *fileindex = NULL;
7481 err = lock_worktree(worktree, LOCK_EX);
7482 if (err)
7483 return err;
7485 err = open_fileindex(fileindex, &fileindex_path, worktree);
7486 if (err)
7487 goto done;
7489 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7490 &have_staged_files);
7491 if (err && err->code != GOT_ERR_CANCELLED)
7492 goto done;
7493 if (have_staged_files) {
7494 err = got_error(GOT_ERR_STAGED_PATHS);
7495 goto done;
7498 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7499 if (err)
7500 goto done;
7502 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7503 if (err)
7504 goto done;
7506 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7507 if (err)
7508 goto done;
7510 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7511 worktree);
7512 if (err)
7513 goto done;
7515 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7516 if (err)
7517 goto done;
7519 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7520 if (err)
7521 goto done;
7522 err = got_ref_resolve(commit_id, repo, commit_ref);
7523 if (err)
7524 goto done;
7526 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7527 if (err)
7528 goto done;
7529 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7530 if (err)
7531 goto done;
7533 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7534 if (err)
7535 goto done;
7536 done:
7537 free(commit_ref_name);
7538 free(branch_ref_name);
7539 free(fileindex_path);
7540 if (commit_ref)
7541 got_ref_close(commit_ref);
7542 if (base_commit_ref)
7543 got_ref_close(base_commit_ref);
7544 if (err) {
7545 free(*commit_id);
7546 *commit_id = NULL;
7547 free(*base_commit_id);
7548 *base_commit_id = NULL;
7549 if (*tmp_branch) {
7550 got_ref_close(*tmp_branch);
7551 *tmp_branch = NULL;
7553 if (*fileindex) {
7554 got_fileindex_free(*fileindex);
7555 *fileindex = NULL;
7557 lock_worktree(worktree, LOCK_EX);
7559 return err;
7562 static const struct got_error *
7563 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7565 const struct got_error *err;
7566 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7567 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7569 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7570 if (err)
7571 goto done;
7572 err = delete_ref(tmp_branch_name, repo);
7573 if (err)
7574 goto done;
7576 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7577 worktree);
7578 if (err)
7579 goto done;
7580 err = delete_ref(base_commit_ref_name, repo);
7581 if (err)
7582 goto done;
7584 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7585 if (err)
7586 goto done;
7587 err = delete_ref(branch_ref_name, repo);
7588 if (err)
7589 goto done;
7591 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7592 if (err)
7593 goto done;
7594 err = delete_ref(commit_ref_name, repo);
7595 if (err)
7596 goto done;
7597 done:
7598 free(tmp_branch_name);
7599 free(base_commit_ref_name);
7600 free(branch_ref_name);
7601 free(commit_ref_name);
7602 return err;
7605 const struct got_error *
7606 got_worktree_histedit_abort(struct got_worktree *worktree,
7607 struct got_fileindex *fileindex, struct got_repository *repo,
7608 struct got_reference *branch, struct got_object_id *base_commit_id,
7609 got_worktree_checkout_cb progress_cb, void *progress_arg)
7611 const struct got_error *err, *unlockerr, *sync_err;
7612 struct got_reference *resolved = NULL;
7613 char *fileindex_path = NULL;
7614 struct got_commit_object *commit = NULL;
7615 struct got_object_id *tree_id = NULL;
7616 struct revert_file_args rfa;
7618 err = lock_worktree(worktree, LOCK_EX);
7619 if (err)
7620 return err;
7622 err = got_object_open_as_commit(&commit, repo,
7623 worktree->base_commit_id);
7624 if (err)
7625 goto done;
7627 err = got_ref_open(&resolved, repo,
7628 got_ref_get_symref_target(branch), 0);
7629 if (err)
7630 goto done;
7632 err = got_worktree_set_head_ref(worktree, resolved);
7633 if (err)
7634 goto done;
7636 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7637 if (err)
7638 goto done;
7640 err = got_object_id_by_path(&tree_id, repo, commit,
7641 worktree->path_prefix);
7642 if (err)
7643 goto done;
7645 err = delete_histedit_refs(worktree, repo);
7646 if (err)
7647 goto done;
7649 err = get_fileindex_path(&fileindex_path, worktree);
7650 if (err)
7651 goto done;
7653 rfa.worktree = worktree;
7654 rfa.fileindex = fileindex;
7655 rfa.progress_cb = progress_cb;
7656 rfa.progress_arg = progress_arg;
7657 rfa.patch_cb = NULL;
7658 rfa.patch_arg = NULL;
7659 rfa.repo = repo;
7660 rfa.unlink_added_files = 0;
7661 err = worktree_status(worktree, "", fileindex, repo,
7662 revert_file, &rfa, NULL, NULL, 1, 0);
7663 if (err)
7664 goto sync;
7666 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7667 repo, progress_cb, progress_arg, NULL, NULL);
7668 sync:
7669 sync_err = sync_fileindex(fileindex, fileindex_path);
7670 if (sync_err && err == NULL)
7671 err = sync_err;
7672 done:
7673 got_ref_close(resolved);
7674 free(tree_id);
7675 free(fileindex_path);
7677 unlockerr = lock_worktree(worktree, LOCK_SH);
7678 if (unlockerr && err == NULL)
7679 err = unlockerr;
7680 return err;
7683 const struct got_error *
7684 got_worktree_histedit_complete(struct got_worktree *worktree,
7685 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7686 struct got_reference *edited_branch, struct got_repository *repo)
7688 const struct got_error *err, *unlockerr, *sync_err;
7689 struct got_object_id *new_head_commit_id = NULL;
7690 struct got_reference *resolved = NULL;
7691 char *fileindex_path = NULL;
7693 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7694 if (err)
7695 return err;
7697 err = got_ref_open(&resolved, repo,
7698 got_ref_get_symref_target(edited_branch), 0);
7699 if (err)
7700 goto done;
7702 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7703 resolved, new_head_commit_id, repo);
7704 if (err)
7705 goto done;
7707 err = got_ref_change_ref(resolved, new_head_commit_id);
7708 if (err)
7709 goto done;
7711 err = got_ref_write(resolved, repo);
7712 if (err)
7713 goto done;
7715 err = got_worktree_set_head_ref(worktree, resolved);
7716 if (err)
7717 goto done;
7719 err = delete_histedit_refs(worktree, repo);
7720 if (err)
7721 goto done;
7723 err = get_fileindex_path(&fileindex_path, worktree);
7724 if (err)
7725 goto done;
7726 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7727 sync_err = sync_fileindex(fileindex, fileindex_path);
7728 if (sync_err && err == NULL)
7729 err = sync_err;
7730 done:
7731 got_fileindex_free(fileindex);
7732 free(fileindex_path);
7733 free(new_head_commit_id);
7734 unlockerr = lock_worktree(worktree, LOCK_SH);
7735 if (unlockerr && err == NULL)
7736 err = unlockerr;
7737 return err;
7740 const struct got_error *
7741 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7742 struct got_object_id *commit_id, struct got_repository *repo)
7744 const struct got_error *err;
7745 char *commit_ref_name;
7747 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7748 if (err)
7749 return err;
7751 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7752 if (err)
7753 goto done;
7755 err = delete_ref(commit_ref_name, repo);
7756 done:
7757 free(commit_ref_name);
7758 return err;
7761 const struct got_error *
7762 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7763 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7764 struct got_worktree *worktree, const char *refname,
7765 struct got_repository *repo)
7767 const struct got_error *err = NULL;
7768 char *fileindex_path = NULL;
7769 struct check_rebase_ok_arg ok_arg;
7771 *fileindex = NULL;
7772 *branch_ref = NULL;
7773 *base_branch_ref = NULL;
7775 err = lock_worktree(worktree, LOCK_EX);
7776 if (err)
7777 return err;
7779 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7780 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7781 "cannot integrate a branch into itself; "
7782 "update -b or different branch name required");
7783 goto done;
7786 err = open_fileindex(fileindex, &fileindex_path, worktree);
7787 if (err)
7788 goto done;
7790 /* Preconditions are the same as for rebase. */
7791 ok_arg.worktree = worktree;
7792 ok_arg.repo = repo;
7793 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7794 &ok_arg);
7795 if (err)
7796 goto done;
7798 err = got_ref_open(branch_ref, repo, refname, 1);
7799 if (err)
7800 goto done;
7802 err = got_ref_open(base_branch_ref, repo,
7803 got_worktree_get_head_ref_name(worktree), 1);
7804 done:
7805 if (err) {
7806 if (*branch_ref) {
7807 got_ref_close(*branch_ref);
7808 *branch_ref = NULL;
7810 if (*base_branch_ref) {
7811 got_ref_close(*base_branch_ref);
7812 *base_branch_ref = NULL;
7814 if (*fileindex) {
7815 got_fileindex_free(*fileindex);
7816 *fileindex = NULL;
7818 lock_worktree(worktree, LOCK_SH);
7820 return err;
7823 const struct got_error *
7824 got_worktree_integrate_continue(struct got_worktree *worktree,
7825 struct got_fileindex *fileindex, struct got_repository *repo,
7826 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7827 got_worktree_checkout_cb progress_cb, void *progress_arg,
7828 got_cancel_cb cancel_cb, void *cancel_arg)
7830 const struct got_error *err = NULL, *sync_err, *unlockerr;
7831 char *fileindex_path = NULL;
7832 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7833 struct got_commit_object *commit = NULL;
7835 err = get_fileindex_path(&fileindex_path, worktree);
7836 if (err)
7837 goto done;
7839 err = got_ref_resolve(&commit_id, repo, branch_ref);
7840 if (err)
7841 goto done;
7843 err = got_object_open_as_commit(&commit, repo, commit_id);
7844 if (err)
7845 goto done;
7847 err = got_object_id_by_path(&tree_id, repo, commit,
7848 worktree->path_prefix);
7849 if (err)
7850 goto done;
7852 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7853 if (err)
7854 goto done;
7856 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7857 progress_cb, progress_arg, cancel_cb, cancel_arg);
7858 if (err)
7859 goto sync;
7861 err = got_ref_change_ref(base_branch_ref, commit_id);
7862 if (err)
7863 goto sync;
7865 err = got_ref_write(base_branch_ref, repo);
7866 if (err)
7867 goto sync;
7869 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7870 sync:
7871 sync_err = sync_fileindex(fileindex, fileindex_path);
7872 if (sync_err && err == NULL)
7873 err = sync_err;
7875 done:
7876 unlockerr = got_ref_unlock(branch_ref);
7877 if (unlockerr && err == NULL)
7878 err = unlockerr;
7879 got_ref_close(branch_ref);
7881 unlockerr = got_ref_unlock(base_branch_ref);
7882 if (unlockerr && err == NULL)
7883 err = unlockerr;
7884 got_ref_close(base_branch_ref);
7886 got_fileindex_free(fileindex);
7887 free(fileindex_path);
7888 free(tree_id);
7889 if (commit)
7890 got_object_commit_close(commit);
7892 unlockerr = lock_worktree(worktree, LOCK_SH);
7893 if (unlockerr && err == NULL)
7894 err = unlockerr;
7895 return err;
7898 const struct got_error *
7899 got_worktree_integrate_abort(struct got_worktree *worktree,
7900 struct got_fileindex *fileindex, struct got_repository *repo,
7901 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7903 const struct got_error *err = NULL, *unlockerr = NULL;
7905 got_fileindex_free(fileindex);
7907 err = lock_worktree(worktree, LOCK_SH);
7909 unlockerr = got_ref_unlock(branch_ref);
7910 if (unlockerr && err == NULL)
7911 err = unlockerr;
7912 got_ref_close(branch_ref);
7914 unlockerr = got_ref_unlock(base_branch_ref);
7915 if (unlockerr && err == NULL)
7916 err = unlockerr;
7917 got_ref_close(base_branch_ref);
7919 return err;
7922 const struct got_error *
7923 got_worktree_merge_postpone(struct got_worktree *worktree,
7924 struct got_fileindex *fileindex)
7926 const struct got_error *err, *sync_err;
7927 char *fileindex_path = NULL;
7929 err = get_fileindex_path(&fileindex_path, worktree);
7930 if (err)
7931 goto done;
7933 sync_err = sync_fileindex(fileindex, fileindex_path);
7935 err = lock_worktree(worktree, LOCK_SH);
7936 if (sync_err && err == NULL)
7937 err = sync_err;
7938 done:
7939 got_fileindex_free(fileindex);
7940 free(fileindex_path);
7941 return err;
7944 static const struct got_error *
7945 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7947 const struct got_error *err;
7948 char *branch_refname = NULL, *commit_refname = NULL;
7950 err = get_merge_branch_ref_name(&branch_refname, worktree);
7951 if (err)
7952 goto done;
7953 err = delete_ref(branch_refname, repo);
7954 if (err)
7955 goto done;
7957 err = get_merge_commit_ref_name(&commit_refname, worktree);
7958 if (err)
7959 goto done;
7960 err = delete_ref(commit_refname, repo);
7961 if (err)
7962 goto done;
7964 done:
7965 free(branch_refname);
7966 free(commit_refname);
7967 return err;
7970 struct merge_commit_msg_arg {
7971 struct got_worktree *worktree;
7972 const char *branch_name;
7975 static const struct got_error *
7976 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
7977 const char *diff_path, char **logmsg, void *arg)
7979 struct merge_commit_msg_arg *a = arg;
7981 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7982 got_worktree_get_head_ref_name(a->worktree)) == -1)
7983 return got_error_from_errno("asprintf");
7985 return NULL;
7989 const struct got_error *
7990 got_worktree_merge_branch(struct got_worktree *worktree,
7991 struct got_fileindex *fileindex,
7992 struct got_object_id *yca_commit_id,
7993 struct got_object_id *branch_tip,
7994 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7995 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7997 const struct got_error *err;
7998 char *fileindex_path = NULL;
8000 err = get_fileindex_path(&fileindex_path, worktree);
8001 if (err)
8002 goto done;
8004 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
8005 worktree);
8006 if (err)
8007 goto done;
8009 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
8010 branch_tip, repo, progress_cb, progress_arg,
8011 cancel_cb, cancel_arg);
8012 done:
8013 free(fileindex_path);
8014 return err;
8017 const struct got_error *
8018 got_worktree_merge_commit(struct got_object_id **new_commit_id,
8019 struct got_worktree *worktree, struct got_fileindex *fileindex,
8020 const char *author, const char *committer, int allow_bad_symlinks,
8021 struct got_object_id *branch_tip, const char *branch_name,
8022 int allow_conflict, struct got_repository *repo,
8023 got_worktree_status_cb status_cb, void *status_arg)
8026 const struct got_error *err = NULL, *sync_err;
8027 struct got_pathlist_head commitable_paths;
8028 struct collect_commitables_arg cc_arg;
8029 struct got_pathlist_entry *pe;
8030 struct got_reference *head_ref = NULL;
8031 struct got_object_id *head_commit_id = NULL;
8032 int have_staged_files = 0;
8033 struct merge_commit_msg_arg mcm_arg;
8034 char *fileindex_path = NULL;
8036 memset(&cc_arg, 0, sizeof(cc_arg));
8037 *new_commit_id = NULL;
8039 TAILQ_INIT(&commitable_paths);
8041 err = get_fileindex_path(&fileindex_path, worktree);
8042 if (err)
8043 goto done;
8045 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
8046 if (err)
8047 goto done;
8049 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8050 if (err)
8051 goto done;
8053 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
8054 &have_staged_files);
8055 if (err && err->code != GOT_ERR_CANCELLED)
8056 goto done;
8057 if (have_staged_files) {
8058 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
8059 goto done;
8062 cc_arg.commitable_paths = &commitable_paths;
8063 cc_arg.worktree = worktree;
8064 cc_arg.fileindex = fileindex;
8065 cc_arg.repo = repo;
8066 cc_arg.have_staged_files = have_staged_files;
8067 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
8068 cc_arg.commit_conflicts = allow_conflict;
8069 err = worktree_status(worktree, "", fileindex, repo,
8070 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8071 if (err)
8072 goto done;
8074 if (TAILQ_EMPTY(&commitable_paths)) {
8075 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
8076 "merge of %s cannot proceed", branch_name);
8077 goto done;
8080 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8081 struct got_commitable *ct = pe->data;
8082 const char *ct_path = ct->in_repo_path;
8084 while (ct_path[0] == '/')
8085 ct_path++;
8086 err = check_out_of_date(ct_path, ct->status,
8087 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
8088 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
8089 if (err)
8090 goto done;
8094 mcm_arg.worktree = worktree;
8095 mcm_arg.branch_name = branch_name;
8096 err = commit_worktree(new_commit_id, &commitable_paths,
8097 head_commit_id, branch_tip, worktree, author, committer, NULL,
8098 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8099 if (err)
8100 goto done;
8102 err = update_fileindex_after_commit(worktree, &commitable_paths,
8103 *new_commit_id, fileindex, have_staged_files);
8104 sync_err = sync_fileindex(fileindex, fileindex_path);
8105 if (sync_err && err == NULL)
8106 err = sync_err;
8107 done:
8108 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8109 struct got_commitable *ct = pe->data;
8111 free_commitable(ct);
8113 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8114 free(fileindex_path);
8115 return err;
8118 const struct got_error *
8119 got_worktree_merge_complete(struct got_worktree *worktree,
8120 struct got_fileindex *fileindex, struct got_repository *repo)
8122 const struct got_error *err, *unlockerr, *sync_err;
8123 char *fileindex_path = NULL;
8125 err = delete_merge_refs(worktree, repo);
8126 if (err)
8127 goto done;
8129 err = get_fileindex_path(&fileindex_path, worktree);
8130 if (err)
8131 goto done;
8132 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8133 sync_err = sync_fileindex(fileindex, fileindex_path);
8134 if (sync_err && err == NULL)
8135 err = sync_err;
8136 done:
8137 got_fileindex_free(fileindex);
8138 free(fileindex_path);
8139 unlockerr = lock_worktree(worktree, LOCK_SH);
8140 if (unlockerr && err == NULL)
8141 err = unlockerr;
8142 return err;
8145 const struct got_error *
8146 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8147 struct got_repository *repo)
8149 const struct got_error *err;
8150 char *branch_refname = NULL;
8151 struct got_reference *branch_ref = NULL;
8153 *in_progress = 0;
8155 err = get_merge_branch_ref_name(&branch_refname, worktree);
8156 if (err)
8157 return err;
8158 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8159 free(branch_refname);
8160 if (err) {
8161 if (err->code != GOT_ERR_NOT_REF)
8162 return err;
8163 } else
8164 *in_progress = 1;
8166 return NULL;
8169 const struct got_error *got_worktree_merge_prepare(
8170 struct got_fileindex **fileindex, struct got_worktree *worktree,
8171 struct got_reference *branch, struct got_repository *repo)
8173 const struct got_error *err = NULL;
8174 char *fileindex_path = NULL;
8175 char *branch_refname = NULL, *commit_refname = NULL;
8176 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
8177 struct got_reference *commit_ref = NULL;
8178 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
8179 struct check_rebase_ok_arg ok_arg;
8181 *fileindex = NULL;
8183 err = lock_worktree(worktree, LOCK_EX);
8184 if (err)
8185 return err;
8187 err = open_fileindex(fileindex, &fileindex_path, worktree);
8188 if (err)
8189 goto done;
8191 /* Preconditions are the same as for rebase. */
8192 ok_arg.worktree = worktree;
8193 ok_arg.repo = repo;
8194 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8195 &ok_arg);
8196 if (err)
8197 goto done;
8199 err = get_merge_branch_ref_name(&branch_refname, worktree);
8200 if (err)
8201 return err;
8203 err = get_merge_commit_ref_name(&commit_refname, worktree);
8204 if (err)
8205 return err;
8207 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8208 0);
8209 if (err)
8210 goto done;
8212 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8213 if (err)
8214 goto done;
8216 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8217 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8218 goto done;
8221 err = got_ref_resolve(&branch_tip, repo, branch);
8222 if (err)
8223 goto done;
8225 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8226 if (err)
8227 goto done;
8228 err = got_ref_write(branch_ref, repo);
8229 if (err)
8230 goto done;
8232 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8233 if (err)
8234 goto done;
8235 err = got_ref_write(commit_ref, repo);
8236 if (err)
8237 goto done;
8239 done:
8240 free(branch_refname);
8241 free(commit_refname);
8242 free(fileindex_path);
8243 if (branch_ref)
8244 got_ref_close(branch_ref);
8245 if (commit_ref)
8246 got_ref_close(commit_ref);
8247 if (wt_branch)
8248 got_ref_close(wt_branch);
8249 free(wt_branch_tip);
8250 if (err) {
8251 if (*fileindex) {
8252 got_fileindex_free(*fileindex);
8253 *fileindex = NULL;
8255 lock_worktree(worktree, LOCK_SH);
8257 return err;
8260 const struct got_error *
8261 got_worktree_merge_continue(char **branch_name,
8262 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8263 struct got_worktree *worktree, struct got_repository *repo)
8265 const struct got_error *err;
8266 char *commit_refname = NULL, *branch_refname = NULL;
8267 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8268 char *fileindex_path = NULL;
8269 int have_staged_files = 0;
8271 *branch_name = NULL;
8272 *branch_tip = NULL;
8273 *fileindex = NULL;
8275 err = lock_worktree(worktree, LOCK_EX);
8276 if (err)
8277 return err;
8279 err = open_fileindex(fileindex, &fileindex_path, worktree);
8280 if (err)
8281 goto done;
8283 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8284 &have_staged_files);
8285 if (err && err->code != GOT_ERR_CANCELLED)
8286 goto done;
8287 if (have_staged_files) {
8288 err = got_error(GOT_ERR_STAGED_PATHS);
8289 goto done;
8292 err = get_merge_branch_ref_name(&branch_refname, worktree);
8293 if (err)
8294 goto done;
8296 err = get_merge_commit_ref_name(&commit_refname, worktree);
8297 if (err)
8298 goto done;
8300 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8301 if (err)
8302 goto done;
8304 if (!got_ref_is_symbolic(branch_ref)) {
8305 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8306 "%s is not a symbolic reference",
8307 got_ref_get_name(branch_ref));
8308 goto done;
8310 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8311 if (*branch_name == NULL) {
8312 err = got_error_from_errno("strdup");
8313 goto done;
8316 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8317 if (err)
8318 goto done;
8320 err = got_ref_resolve(branch_tip, repo, commit_ref);
8321 if (err)
8322 goto done;
8323 done:
8324 free(commit_refname);
8325 free(branch_refname);
8326 free(fileindex_path);
8327 if (commit_ref)
8328 got_ref_close(commit_ref);
8329 if (branch_ref)
8330 got_ref_close(branch_ref);
8331 if (err) {
8332 if (*branch_name) {
8333 free(*branch_name);
8334 *branch_name = NULL;
8336 free(*branch_tip);
8337 *branch_tip = NULL;
8338 if (*fileindex) {
8339 got_fileindex_free(*fileindex);
8340 *fileindex = NULL;
8342 lock_worktree(worktree, LOCK_SH);
8344 return err;
8347 const struct got_error *
8348 got_worktree_merge_abort(struct got_worktree *worktree,
8349 struct got_fileindex *fileindex, struct got_repository *repo,
8350 got_worktree_checkout_cb progress_cb, void *progress_arg)
8352 const struct got_error *err, *unlockerr, *sync_err;
8353 struct got_object_id *commit_id = NULL;
8354 struct got_commit_object *commit = NULL;
8355 char *fileindex_path = NULL;
8356 struct revert_file_args rfa;
8357 struct got_object_id *tree_id = NULL;
8359 err = got_object_open_as_commit(&commit, repo,
8360 worktree->base_commit_id);
8361 if (err)
8362 goto done;
8364 err = got_object_id_by_path(&tree_id, repo, commit,
8365 worktree->path_prefix);
8366 if (err)
8367 goto done;
8369 err = delete_merge_refs(worktree, repo);
8370 if (err)
8371 goto done;
8373 err = get_fileindex_path(&fileindex_path, worktree);
8374 if (err)
8375 goto done;
8377 rfa.worktree = worktree;
8378 rfa.fileindex = fileindex;
8379 rfa.progress_cb = progress_cb;
8380 rfa.progress_arg = progress_arg;
8381 rfa.patch_cb = NULL;
8382 rfa.patch_arg = NULL;
8383 rfa.repo = repo;
8384 rfa.unlink_added_files = 1;
8385 err = worktree_status(worktree, "", fileindex, repo,
8386 revert_file, &rfa, NULL, NULL, 1, 0);
8387 if (err)
8388 goto sync;
8390 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8391 repo, progress_cb, progress_arg, NULL, NULL);
8392 sync:
8393 sync_err = sync_fileindex(fileindex, fileindex_path);
8394 if (sync_err && err == NULL)
8395 err = sync_err;
8396 done:
8397 free(tree_id);
8398 free(commit_id);
8399 if (commit)
8400 got_object_commit_close(commit);
8401 if (fileindex)
8402 got_fileindex_free(fileindex);
8403 free(fileindex_path);
8405 unlockerr = lock_worktree(worktree, LOCK_SH);
8406 if (unlockerr && err == NULL)
8407 err = unlockerr;
8408 return err;
8411 struct check_stage_ok_arg {
8412 struct got_object_id *head_commit_id;
8413 struct got_worktree *worktree;
8414 struct got_fileindex *fileindex;
8415 struct got_repository *repo;
8416 int have_changes;
8419 static const struct got_error *
8420 check_stage_ok(void *arg, unsigned char status,
8421 unsigned char staged_status, const char *relpath,
8422 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8423 struct got_object_id *commit_id, int dirfd, const char *de_name)
8425 struct check_stage_ok_arg *a = arg;
8426 const struct got_error *err = NULL;
8427 struct got_fileindex_entry *ie;
8428 struct got_object_id base_commit_id;
8429 struct got_object_id *base_commit_idp = NULL;
8430 char *in_repo_path = NULL, *p;
8432 if (status == GOT_STATUS_UNVERSIONED ||
8433 status == GOT_STATUS_NO_CHANGE)
8434 return NULL;
8435 if (status == GOT_STATUS_NONEXISTENT)
8436 return got_error_set_errno(ENOENT, relpath);
8438 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8439 if (ie == NULL)
8440 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8442 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8443 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8444 relpath) == -1)
8445 return got_error_from_errno("asprintf");
8447 if (got_fileindex_entry_has_commit(ie)) {
8448 base_commit_idp = got_fileindex_entry_get_commit_id(
8449 &base_commit_id, ie);
8452 if (status == GOT_STATUS_CONFLICT) {
8453 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8454 goto done;
8455 } else if (status != GOT_STATUS_ADD &&
8456 status != GOT_STATUS_MODIFY &&
8457 status != GOT_STATUS_DELETE) {
8458 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8459 goto done;
8462 a->have_changes = 1;
8464 p = in_repo_path;
8465 while (p[0] == '/')
8466 p++;
8467 err = check_out_of_date(p, status, staged_status,
8468 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8469 GOT_ERR_STAGE_OUT_OF_DATE);
8470 done:
8471 free(in_repo_path);
8472 return err;
8475 struct stage_path_arg {
8476 struct got_worktree *worktree;
8477 struct got_fileindex *fileindex;
8478 struct got_repository *repo;
8479 got_worktree_status_cb status_cb;
8480 void *status_arg;
8481 got_worktree_patch_cb patch_cb;
8482 void *patch_arg;
8483 int staged_something;
8484 int allow_bad_symlinks;
8487 static const struct got_error *
8488 stage_path(void *arg, unsigned char status,
8489 unsigned char staged_status, const char *relpath,
8490 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8491 struct got_object_id *commit_id, int dirfd, const char *de_name)
8493 struct stage_path_arg *a = arg;
8494 const struct got_error *err = NULL;
8495 struct got_fileindex_entry *ie;
8496 char *ondisk_path = NULL, *path_content = NULL;
8497 uint32_t stage;
8498 struct got_object_id *new_staged_blob_id = NULL;
8499 struct stat sb;
8501 if (status == GOT_STATUS_UNVERSIONED)
8502 return NULL;
8504 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8505 if (ie == NULL)
8506 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8508 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8509 relpath)== -1)
8510 return got_error_from_errno("asprintf");
8512 switch (status) {
8513 case GOT_STATUS_ADD:
8514 case GOT_STATUS_MODIFY:
8515 /* XXX could sb.st_mode be passed in by our caller? */
8516 if (lstat(ondisk_path, &sb) == -1) {
8517 err = got_error_from_errno2("lstat", ondisk_path);
8518 break;
8520 if (a->patch_cb) {
8521 if (status == GOT_STATUS_ADD) {
8522 int choice = GOT_PATCH_CHOICE_NONE;
8523 err = (*a->patch_cb)(&choice, a->patch_arg,
8524 status, ie->path, NULL, 1, 1);
8525 if (err)
8526 break;
8527 if (choice != GOT_PATCH_CHOICE_YES)
8528 break;
8529 } else {
8530 err = create_patched_content(&path_content, 0,
8531 staged_blob_id ? staged_blob_id : blob_id,
8532 ondisk_path, dirfd, de_name, ie->path,
8533 a->repo, a->patch_cb, a->patch_arg);
8534 if (err || path_content == NULL)
8535 break;
8538 err = got_object_blob_create(&new_staged_blob_id,
8539 path_content ? path_content : ondisk_path, a->repo);
8540 if (err)
8541 break;
8542 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8543 SHA1_DIGEST_LENGTH);
8544 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8545 stage = GOT_FILEIDX_STAGE_ADD;
8546 else
8547 stage = GOT_FILEIDX_STAGE_MODIFY;
8548 got_fileindex_entry_stage_set(ie, stage);
8549 if (S_ISLNK(sb.st_mode)) {
8550 int is_bad_symlink = 0;
8551 if (!a->allow_bad_symlinks) {
8552 char target_path[PATH_MAX];
8553 ssize_t target_len;
8554 target_len = readlink(ondisk_path, target_path,
8555 sizeof(target_path));
8556 if (target_len == -1) {
8557 err = got_error_from_errno2("readlink",
8558 ondisk_path);
8559 break;
8561 err = is_bad_symlink_target(&is_bad_symlink,
8562 target_path, target_len, ondisk_path,
8563 a->worktree->root_path);
8564 if (err)
8565 break;
8566 if (is_bad_symlink) {
8567 err = got_error_path(ondisk_path,
8568 GOT_ERR_BAD_SYMLINK);
8569 break;
8572 if (is_bad_symlink)
8573 got_fileindex_entry_staged_filetype_set(ie,
8574 GOT_FILEIDX_MODE_BAD_SYMLINK);
8575 else
8576 got_fileindex_entry_staged_filetype_set(ie,
8577 GOT_FILEIDX_MODE_SYMLINK);
8578 } else {
8579 got_fileindex_entry_staged_filetype_set(ie,
8580 GOT_FILEIDX_MODE_REGULAR_FILE);
8582 a->staged_something = 1;
8583 if (a->status_cb == NULL)
8584 break;
8585 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8586 get_staged_status(ie), relpath, blob_id,
8587 new_staged_blob_id, NULL, dirfd, de_name);
8588 if (err)
8589 break;
8591 * When staging the reverse of the staged diff,
8592 * implicitly unstage the file.
8594 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8595 sizeof(ie->blob_sha1)) == 0) {
8596 got_fileindex_entry_stage_set(ie,
8597 GOT_FILEIDX_STAGE_NONE);
8599 break;
8600 case GOT_STATUS_DELETE:
8601 if (staged_status == GOT_STATUS_DELETE)
8602 break;
8603 if (a->patch_cb) {
8604 int choice = GOT_PATCH_CHOICE_NONE;
8605 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8606 ie->path, NULL, 1, 1);
8607 if (err)
8608 break;
8609 if (choice == GOT_PATCH_CHOICE_NO)
8610 break;
8611 if (choice != GOT_PATCH_CHOICE_YES) {
8612 err = got_error(GOT_ERR_PATCH_CHOICE);
8613 break;
8616 stage = GOT_FILEIDX_STAGE_DELETE;
8617 got_fileindex_entry_stage_set(ie, stage);
8618 a->staged_something = 1;
8619 if (a->status_cb == NULL)
8620 break;
8621 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8622 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8623 de_name);
8624 break;
8625 case GOT_STATUS_NO_CHANGE:
8626 break;
8627 case GOT_STATUS_CONFLICT:
8628 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8629 break;
8630 case GOT_STATUS_NONEXISTENT:
8631 err = got_error_set_errno(ENOENT, relpath);
8632 break;
8633 default:
8634 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8635 break;
8638 if (path_content && unlink(path_content) == -1 && err == NULL)
8639 err = got_error_from_errno2("unlink", path_content);
8640 free(path_content);
8641 free(ondisk_path);
8642 free(new_staged_blob_id);
8643 return err;
8646 const struct got_error *
8647 got_worktree_stage(struct got_worktree *worktree,
8648 struct got_pathlist_head *paths,
8649 got_worktree_status_cb status_cb, void *status_arg,
8650 got_worktree_patch_cb patch_cb, void *patch_arg,
8651 int allow_bad_symlinks, struct got_repository *repo)
8653 const struct got_error *err = NULL, *sync_err, *unlockerr;
8654 struct got_pathlist_entry *pe;
8655 struct got_fileindex *fileindex = NULL;
8656 char *fileindex_path = NULL;
8657 struct got_reference *head_ref = NULL;
8658 struct got_object_id *head_commit_id = NULL;
8659 struct check_stage_ok_arg oka;
8660 struct stage_path_arg spa;
8662 err = lock_worktree(worktree, LOCK_EX);
8663 if (err)
8664 return err;
8666 err = got_ref_open(&head_ref, repo,
8667 got_worktree_get_head_ref_name(worktree), 0);
8668 if (err)
8669 goto done;
8670 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8671 if (err)
8672 goto done;
8673 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8674 if (err)
8675 goto done;
8677 /* Check pre-conditions before staging anything. */
8678 oka.head_commit_id = head_commit_id;
8679 oka.worktree = worktree;
8680 oka.fileindex = fileindex;
8681 oka.repo = repo;
8682 oka.have_changes = 0;
8683 TAILQ_FOREACH(pe, paths, entry) {
8684 err = worktree_status(worktree, pe->path, fileindex, repo,
8685 check_stage_ok, &oka, NULL, NULL, 1, 0);
8686 if (err)
8687 goto done;
8689 if (!oka.have_changes) {
8690 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8691 goto done;
8694 spa.worktree = worktree;
8695 spa.fileindex = fileindex;
8696 spa.repo = repo;
8697 spa.patch_cb = patch_cb;
8698 spa.patch_arg = patch_arg;
8699 spa.status_cb = status_cb;
8700 spa.status_arg = status_arg;
8701 spa.staged_something = 0;
8702 spa.allow_bad_symlinks = allow_bad_symlinks;
8703 TAILQ_FOREACH(pe, paths, entry) {
8704 err = worktree_status(worktree, pe->path, fileindex, repo,
8705 stage_path, &spa, NULL, NULL, 1, 0);
8706 if (err)
8707 goto done;
8709 if (!spa.staged_something) {
8710 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8711 goto done;
8714 sync_err = sync_fileindex(fileindex, fileindex_path);
8715 if (sync_err && err == NULL)
8716 err = sync_err;
8717 done:
8718 if (head_ref)
8719 got_ref_close(head_ref);
8720 free(head_commit_id);
8721 free(fileindex_path);
8722 if (fileindex)
8723 got_fileindex_free(fileindex);
8724 unlockerr = lock_worktree(worktree, LOCK_SH);
8725 if (unlockerr && err == NULL)
8726 err = unlockerr;
8727 return err;
8730 struct unstage_path_arg {
8731 struct got_worktree *worktree;
8732 struct got_fileindex *fileindex;
8733 struct got_repository *repo;
8734 got_worktree_checkout_cb progress_cb;
8735 void *progress_arg;
8736 got_worktree_patch_cb patch_cb;
8737 void *patch_arg;
8740 static const struct got_error *
8741 create_unstaged_content(char **path_unstaged_content,
8742 char **path_new_staged_content, struct got_object_id *blob_id,
8743 struct got_object_id *staged_blob_id, const char *relpath,
8744 struct got_repository *repo,
8745 got_worktree_patch_cb patch_cb, void *patch_arg)
8747 const struct got_error *err, *free_err;
8748 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8749 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8750 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8751 struct got_diffreg_result *diffreg_result = NULL;
8752 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8753 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8754 int fd1 = -1, fd2 = -1;
8756 *path_unstaged_content = NULL;
8757 *path_new_staged_content = NULL;
8759 err = got_object_id_str(&label1, blob_id);
8760 if (err)
8761 return err;
8763 fd1 = got_opentempfd();
8764 if (fd1 == -1) {
8765 err = got_error_from_errno("got_opentempfd");
8766 goto done;
8768 fd2 = got_opentempfd();
8769 if (fd2 == -1) {
8770 err = got_error_from_errno("got_opentempfd");
8771 goto done;
8774 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8775 if (err)
8776 goto done;
8778 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
8779 if (err)
8780 goto done;
8782 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8783 if (err)
8784 goto done;
8786 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8787 fd2);
8788 if (err)
8789 goto done;
8791 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
8792 if (err)
8793 goto done;
8795 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8796 if (err)
8797 goto done;
8799 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8800 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8801 if (err)
8802 goto done;
8804 err = got_opentemp_named(path_unstaged_content, &outfile,
8805 "got-unstaged-content", "");
8806 if (err)
8807 goto done;
8808 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8809 "got-new-staged-content", "");
8810 if (err)
8811 goto done;
8813 if (fseek(f1, 0L, SEEK_SET) == -1) {
8814 err = got_ferror(f1, GOT_ERR_IO);
8815 goto done;
8817 if (fseek(f2, 0L, SEEK_SET) == -1) {
8818 err = got_ferror(f2, GOT_ERR_IO);
8819 goto done;
8821 /* Count the number of actual changes in the diff result. */
8822 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8823 struct diff_chunk_context cc = {};
8824 diff_chunk_context_load_change(&cc, &nchunks_used,
8825 diffreg_result->result, n, 0);
8826 nchanges++;
8828 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8829 int choice;
8830 err = apply_or_reject_change(&choice, &nchunks_used,
8831 diffreg_result->result, n, relpath, f1, f2,
8832 &line_cur1, &line_cur2,
8833 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8834 if (err)
8835 goto done;
8836 if (choice == GOT_PATCH_CHOICE_YES)
8837 have_content = 1;
8838 else
8839 have_rejected_content = 1;
8840 if (choice == GOT_PATCH_CHOICE_QUIT)
8841 break;
8843 if (have_content || have_rejected_content)
8844 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8845 outfile, rejectfile);
8846 done:
8847 free(label1);
8848 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8849 err = got_error_from_errno("close");
8850 if (blob)
8851 got_object_blob_close(blob);
8852 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8853 err = got_error_from_errno("close");
8854 if (staged_blob)
8855 got_object_blob_close(staged_blob);
8856 free_err = got_diffreg_result_free(diffreg_result);
8857 if (free_err && err == NULL)
8858 err = free_err;
8859 if (f1 && fclose(f1) == EOF && err == NULL)
8860 err = got_error_from_errno2("fclose", path1);
8861 if (f2 && fclose(f2) == EOF && err == NULL)
8862 err = got_error_from_errno2("fclose", path2);
8863 if (outfile && fclose(outfile) == EOF && err == NULL)
8864 err = got_error_from_errno2("fclose", *path_unstaged_content);
8865 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8866 err = got_error_from_errno2("fclose", *path_new_staged_content);
8867 if (path1 && unlink(path1) == -1 && err == NULL)
8868 err = got_error_from_errno2("unlink", path1);
8869 if (path2 && unlink(path2) == -1 && err == NULL)
8870 err = got_error_from_errno2("unlink", path2);
8871 if (err || !have_content) {
8872 if (*path_unstaged_content &&
8873 unlink(*path_unstaged_content) == -1 && err == NULL)
8874 err = got_error_from_errno2("unlink",
8875 *path_unstaged_content);
8876 free(*path_unstaged_content);
8877 *path_unstaged_content = NULL;
8879 if (err || !have_content || !have_rejected_content) {
8880 if (*path_new_staged_content &&
8881 unlink(*path_new_staged_content) == -1 && err == NULL)
8882 err = got_error_from_errno2("unlink",
8883 *path_new_staged_content);
8884 free(*path_new_staged_content);
8885 *path_new_staged_content = NULL;
8887 free(path1);
8888 free(path2);
8889 return err;
8892 static const struct got_error *
8893 unstage_hunks(struct got_object_id *staged_blob_id,
8894 struct got_blob_object *blob_base,
8895 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8896 const char *ondisk_path, const char *label_orig,
8897 struct got_worktree *worktree, struct got_repository *repo,
8898 got_worktree_patch_cb patch_cb, void *patch_arg,
8899 got_worktree_checkout_cb progress_cb, void *progress_arg)
8901 const struct got_error *err = NULL;
8902 char *path_unstaged_content = NULL;
8903 char *path_new_staged_content = NULL;
8904 char *parent = NULL, *base_path = NULL;
8905 char *blob_base_path = NULL;
8906 struct got_object_id *new_staged_blob_id = NULL;
8907 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8908 struct stat sb;
8910 err = create_unstaged_content(&path_unstaged_content,
8911 &path_new_staged_content, blob_id, staged_blob_id,
8912 ie->path, repo, patch_cb, patch_arg);
8913 if (err)
8914 return err;
8916 if (path_unstaged_content == NULL)
8917 return NULL;
8919 if (path_new_staged_content) {
8920 err = got_object_blob_create(&new_staged_blob_id,
8921 path_new_staged_content, repo);
8922 if (err)
8923 goto done;
8926 f = fopen(path_unstaged_content, "re");
8927 if (f == NULL) {
8928 err = got_error_from_errno2("fopen",
8929 path_unstaged_content);
8930 goto done;
8932 if (fstat(fileno(f), &sb) == -1) {
8933 err = got_error_from_errno2("fstat", path_unstaged_content);
8934 goto done;
8936 if (got_fileindex_entry_staged_filetype_get(ie) ==
8937 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8938 char link_target[PATH_MAX];
8939 size_t r;
8940 r = fread(link_target, 1, sizeof(link_target), f);
8941 if (r == 0 && ferror(f)) {
8942 err = got_error_from_errno("fread");
8943 goto done;
8945 if (r >= sizeof(link_target)) { /* should not happen */
8946 err = got_error(GOT_ERR_NO_SPACE);
8947 goto done;
8949 link_target[r] = '\0';
8950 err = merge_symlink(worktree, blob_base,
8951 ondisk_path, ie->path, label_orig, link_target,
8952 worktree->base_commit_id, repo, progress_cb,
8953 progress_arg);
8954 } else {
8955 int local_changes_subsumed;
8957 err = got_path_dirname(&parent, ondisk_path);
8958 if (err)
8959 return err;
8961 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8962 parent) == -1) {
8963 err = got_error_from_errno("asprintf");
8964 base_path = NULL;
8965 goto done;
8968 err = got_opentemp_named(&blob_base_path, &f_base,
8969 base_path, "");
8970 if (err)
8971 goto done;
8972 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8973 blob_base);
8974 if (err)
8975 goto done;
8978 * In order the run a 3-way merge with a symlink we copy the symlink's
8979 * target path into a temporary file and use that file with diff3.
8981 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8982 err = dump_symlink_target_path_to_file(&f_deriv2,
8983 ondisk_path);
8984 if (err)
8985 goto done;
8986 } else {
8987 int fd;
8988 fd = open(ondisk_path,
8989 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8990 if (fd == -1) {
8991 err = got_error_from_errno2("open", ondisk_path);
8992 goto done;
8994 f_deriv2 = fdopen(fd, "r");
8995 if (f_deriv2 == NULL) {
8996 err = got_error_from_errno2("fdopen", ondisk_path);
8997 close(fd);
8998 goto done;
9002 err = merge_file(&local_changes_subsumed, worktree,
9003 f_base, f, f_deriv2, ondisk_path, ie->path,
9004 got_fileindex_perms_to_st(ie),
9005 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
9006 repo, progress_cb, progress_arg);
9008 if (err)
9009 goto done;
9011 if (new_staged_blob_id) {
9012 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9013 SHA1_DIGEST_LENGTH);
9014 } else {
9015 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9016 got_fileindex_entry_staged_filetype_set(ie, 0);
9018 done:
9019 free(new_staged_blob_id);
9020 if (path_unstaged_content &&
9021 unlink(path_unstaged_content) == -1 && err == NULL)
9022 err = got_error_from_errno2("unlink", path_unstaged_content);
9023 if (path_new_staged_content &&
9024 unlink(path_new_staged_content) == -1 && err == NULL)
9025 err = got_error_from_errno2("unlink", path_new_staged_content);
9026 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
9027 err = got_error_from_errno2("unlink", blob_base_path);
9028 if (f_base && fclose(f_base) == EOF && err == NULL)
9029 err = got_error_from_errno2("fclose", path_unstaged_content);
9030 if (f && fclose(f) == EOF && err == NULL)
9031 err = got_error_from_errno2("fclose", path_unstaged_content);
9032 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
9033 err = got_error_from_errno2("fclose", ondisk_path);
9034 free(path_unstaged_content);
9035 free(path_new_staged_content);
9036 free(blob_base_path);
9037 free(parent);
9038 free(base_path);
9039 return err;
9042 static const struct got_error *
9043 unstage_path(void *arg, unsigned char status,
9044 unsigned char staged_status, const char *relpath,
9045 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9046 struct got_object_id *commit_id, int dirfd, const char *de_name)
9048 const struct got_error *err = NULL;
9049 struct unstage_path_arg *a = arg;
9050 struct got_fileindex_entry *ie;
9051 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
9052 char *ondisk_path = NULL;
9053 char *id_str = NULL, *label_orig = NULL;
9054 int local_changes_subsumed;
9055 struct stat sb;
9056 int fd1 = -1, fd2 = -1;
9058 if (staged_status != GOT_STATUS_ADD &&
9059 staged_status != GOT_STATUS_MODIFY &&
9060 staged_status != GOT_STATUS_DELETE)
9061 return NULL;
9063 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9064 if (ie == NULL)
9065 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9067 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9068 == -1)
9069 return got_error_from_errno("asprintf");
9071 err = got_object_id_str(&id_str,
9072 commit_id ? commit_id : a->worktree->base_commit_id);
9073 if (err)
9074 goto done;
9075 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9076 id_str) == -1) {
9077 err = got_error_from_errno("asprintf");
9078 goto done;
9081 fd1 = got_opentempfd();
9082 if (fd1 == -1) {
9083 err = got_error_from_errno("got_opentempfd");
9084 goto done;
9086 fd2 = got_opentempfd();
9087 if (fd2 == -1) {
9088 err = got_error_from_errno("got_opentempfd");
9089 goto done;
9092 switch (staged_status) {
9093 case GOT_STATUS_MODIFY:
9094 err = got_object_open_as_blob(&blob_base, a->repo,
9095 blob_id, 8192, fd1);
9096 if (err)
9097 break;
9098 /* fall through */
9099 case GOT_STATUS_ADD:
9100 if (a->patch_cb) {
9101 if (staged_status == GOT_STATUS_ADD) {
9102 int choice = GOT_PATCH_CHOICE_NONE;
9103 err = (*a->patch_cb)(&choice, a->patch_arg,
9104 staged_status, ie->path, NULL, 1, 1);
9105 if (err)
9106 break;
9107 if (choice != GOT_PATCH_CHOICE_YES)
9108 break;
9109 } else {
9110 err = unstage_hunks(staged_blob_id,
9111 blob_base, blob_id, ie, ondisk_path,
9112 label_orig, a->worktree, a->repo,
9113 a->patch_cb, a->patch_arg,
9114 a->progress_cb, a->progress_arg);
9115 break; /* Done with this file. */
9118 err = got_object_open_as_blob(&blob_staged, a->repo,
9119 staged_blob_id, 8192, fd2);
9120 if (err)
9121 break;
9122 switch (got_fileindex_entry_staged_filetype_get(ie)) {
9123 case GOT_FILEIDX_MODE_BAD_SYMLINK:
9124 case GOT_FILEIDX_MODE_REGULAR_FILE:
9125 err = merge_blob(&local_changes_subsumed, a->worktree,
9126 blob_base, ondisk_path, relpath,
9127 got_fileindex_perms_to_st(ie), label_orig,
9128 blob_staged, commit_id ? commit_id :
9129 a->worktree->base_commit_id, a->repo,
9130 a->progress_cb, a->progress_arg);
9131 break;
9132 case GOT_FILEIDX_MODE_SYMLINK:
9133 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9134 char *staged_target;
9135 err = got_object_blob_read_to_str(
9136 &staged_target, blob_staged);
9137 if (err)
9138 goto done;
9139 err = merge_symlink(a->worktree, blob_base,
9140 ondisk_path, relpath, label_orig,
9141 staged_target, commit_id ? commit_id :
9142 a->worktree->base_commit_id,
9143 a->repo, a->progress_cb, a->progress_arg);
9144 free(staged_target);
9145 } else {
9146 err = merge_blob(&local_changes_subsumed,
9147 a->worktree, blob_base, ondisk_path,
9148 relpath, got_fileindex_perms_to_st(ie),
9149 label_orig, blob_staged,
9150 commit_id ? commit_id :
9151 a->worktree->base_commit_id, a->repo,
9152 a->progress_cb, a->progress_arg);
9154 break;
9155 default:
9156 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9157 break;
9159 if (err == NULL) {
9160 got_fileindex_entry_stage_set(ie,
9161 GOT_FILEIDX_STAGE_NONE);
9162 got_fileindex_entry_staged_filetype_set(ie, 0);
9164 break;
9165 case GOT_STATUS_DELETE:
9166 if (a->patch_cb) {
9167 int choice = GOT_PATCH_CHOICE_NONE;
9168 err = (*a->patch_cb)(&choice, a->patch_arg,
9169 staged_status, ie->path, NULL, 1, 1);
9170 if (err)
9171 break;
9172 if (choice == GOT_PATCH_CHOICE_NO)
9173 break;
9174 if (choice != GOT_PATCH_CHOICE_YES) {
9175 err = got_error(GOT_ERR_PATCH_CHOICE);
9176 break;
9179 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9180 got_fileindex_entry_staged_filetype_set(ie, 0);
9181 err = get_file_status(&status, &sb, ie, ondisk_path,
9182 dirfd, de_name, a->repo);
9183 if (err)
9184 break;
9185 err = (*a->progress_cb)(a->progress_arg, status, relpath);
9186 break;
9188 done:
9189 free(ondisk_path);
9190 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9191 err = got_error_from_errno("close");
9192 if (blob_base)
9193 got_object_blob_close(blob_base);
9194 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9195 err = got_error_from_errno("close");
9196 if (blob_staged)
9197 got_object_blob_close(blob_staged);
9198 free(id_str);
9199 free(label_orig);
9200 return err;
9203 const struct got_error *
9204 got_worktree_unstage(struct got_worktree *worktree,
9205 struct got_pathlist_head *paths,
9206 got_worktree_checkout_cb progress_cb, void *progress_arg,
9207 got_worktree_patch_cb patch_cb, void *patch_arg,
9208 struct got_repository *repo)
9210 const struct got_error *err = NULL, *sync_err, *unlockerr;
9211 struct got_pathlist_entry *pe;
9212 struct got_fileindex *fileindex = NULL;
9213 char *fileindex_path = NULL;
9214 struct unstage_path_arg upa;
9216 err = lock_worktree(worktree, LOCK_EX);
9217 if (err)
9218 return err;
9220 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9221 if (err)
9222 goto done;
9224 upa.worktree = worktree;
9225 upa.fileindex = fileindex;
9226 upa.repo = repo;
9227 upa.progress_cb = progress_cb;
9228 upa.progress_arg = progress_arg;
9229 upa.patch_cb = patch_cb;
9230 upa.patch_arg = patch_arg;
9231 TAILQ_FOREACH(pe, paths, entry) {
9232 err = worktree_status(worktree, pe->path, fileindex, repo,
9233 unstage_path, &upa, NULL, NULL, 1, 0);
9234 if (err)
9235 goto done;
9238 sync_err = sync_fileindex(fileindex, fileindex_path);
9239 if (sync_err && err == NULL)
9240 err = sync_err;
9241 done:
9242 free(fileindex_path);
9243 if (fileindex)
9244 got_fileindex_free(fileindex);
9245 unlockerr = lock_worktree(worktree, LOCK_SH);
9246 if (unlockerr && err == NULL)
9247 err = unlockerr;
9248 return err;
9251 struct report_file_info_arg {
9252 struct got_worktree *worktree;
9253 got_worktree_path_info_cb info_cb;
9254 void *info_arg;
9255 struct got_pathlist_head *paths;
9256 got_cancel_cb cancel_cb;
9257 void *cancel_arg;
9260 static const struct got_error *
9261 report_file_info(void *arg, struct got_fileindex_entry *ie)
9263 struct report_file_info_arg *a = arg;
9264 struct got_pathlist_entry *pe;
9265 struct got_object_id blob_id, staged_blob_id, commit_id;
9266 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9267 struct got_object_id *commit_idp = NULL;
9268 int stage;
9270 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9271 return got_error(GOT_ERR_CANCELLED);
9273 TAILQ_FOREACH(pe, a->paths, entry) {
9274 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9275 got_path_is_child(ie->path, pe->path, pe->path_len))
9276 break;
9278 if (pe == NULL) /* not found */
9279 return NULL;
9281 if (got_fileindex_entry_has_blob(ie))
9282 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9283 stage = got_fileindex_entry_stage_get(ie);
9284 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9285 stage == GOT_FILEIDX_STAGE_ADD) {
9286 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9287 &staged_blob_id, ie);
9290 if (got_fileindex_entry_has_commit(ie))
9291 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9293 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9294 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9297 const struct got_error *
9298 got_worktree_path_info(struct got_worktree *worktree,
9299 struct got_pathlist_head *paths,
9300 got_worktree_path_info_cb info_cb, void *info_arg,
9301 got_cancel_cb cancel_cb, void *cancel_arg)
9304 const struct got_error *err = NULL, *unlockerr;
9305 struct got_fileindex *fileindex = NULL;
9306 char *fileindex_path = NULL;
9307 struct report_file_info_arg arg;
9309 err = lock_worktree(worktree, LOCK_SH);
9310 if (err)
9311 return err;
9313 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9314 if (err)
9315 goto done;
9317 arg.worktree = worktree;
9318 arg.info_cb = info_cb;
9319 arg.info_arg = info_arg;
9320 arg.paths = paths;
9321 arg.cancel_cb = cancel_cb;
9322 arg.cancel_arg = cancel_arg;
9323 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9324 &arg);
9325 done:
9326 free(fileindex_path);
9327 if (fileindex)
9328 got_fileindex_free(fileindex);
9329 unlockerr = lock_worktree(worktree, LOCK_UN);
9330 if (unlockerr && err == NULL)
9331 err = unlockerr;
9332 return err;
9335 static const struct got_error *
9336 patch_check_path(const char *p, char **path, unsigned char *status,
9337 unsigned char *staged_status, struct got_fileindex *fileindex,
9338 struct got_worktree *worktree, struct got_repository *repo)
9340 const struct got_error *err;
9341 struct got_fileindex_entry *ie;
9342 struct stat sb;
9343 char *ondisk_path = NULL;
9345 err = got_worktree_resolve_path(path, worktree, p);
9346 if (err)
9347 return err;
9349 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9350 *path[0] ? "/" : "", *path) == -1)
9351 return got_error_from_errno("asprintf");
9353 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9354 if (ie) {
9355 *staged_status = get_staged_status(ie);
9356 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9357 repo);
9358 if (err)
9359 goto done;
9360 } else {
9361 *staged_status = GOT_STATUS_NO_CHANGE;
9362 *status = GOT_STATUS_UNVERSIONED;
9363 if (lstat(ondisk_path, &sb) == -1) {
9364 if (errno != ENOENT) {
9365 err = got_error_from_errno2("lstat",
9366 ondisk_path);
9367 goto done;
9369 *status = GOT_STATUS_NONEXISTENT;
9373 done:
9374 free(ondisk_path);
9375 return err;
9378 static const struct got_error *
9379 patch_can_rm(const char *path, unsigned char status,
9380 unsigned char staged_status)
9382 if (status == GOT_STATUS_NONEXISTENT)
9383 return got_error_set_errno(ENOENT, path);
9384 if (status != GOT_STATUS_NO_CHANGE &&
9385 status != GOT_STATUS_ADD &&
9386 status != GOT_STATUS_MODIFY &&
9387 status != GOT_STATUS_MODE_CHANGE)
9388 return got_error_path(path, GOT_ERR_FILE_STATUS);
9389 if (staged_status == GOT_STATUS_DELETE)
9390 return got_error_path(path, GOT_ERR_FILE_STATUS);
9391 return NULL;
9394 static const struct got_error *
9395 patch_can_add(const char *path, unsigned char status)
9397 if (status != GOT_STATUS_NONEXISTENT)
9398 return got_error_path(path, GOT_ERR_FILE_STATUS);
9399 return NULL;
9402 static const struct got_error *
9403 patch_can_edit(const char *path, unsigned char status,
9404 unsigned char staged_status)
9406 if (status == GOT_STATUS_NONEXISTENT)
9407 return got_error_set_errno(ENOENT, path);
9408 if (status != GOT_STATUS_NO_CHANGE &&
9409 status != GOT_STATUS_ADD &&
9410 status != GOT_STATUS_MODIFY)
9411 return got_error_path(path, GOT_ERR_FILE_STATUS);
9412 if (staged_status == GOT_STATUS_DELETE)
9413 return got_error_path(path, GOT_ERR_FILE_STATUS);
9414 return NULL;
9417 const struct got_error *
9418 got_worktree_patch_prepare(struct got_fileindex **fileindex,
9419 char **fileindex_path, struct got_worktree *worktree)
9421 return open_fileindex(fileindex, fileindex_path, worktree);
9424 const struct got_error *
9425 got_worktree_patch_check_path(const char *old, const char *new,
9426 char **oldpath, char **newpath, struct got_worktree *worktree,
9427 struct got_repository *repo, struct got_fileindex *fileindex)
9429 const struct got_error *err = NULL;
9430 int file_renamed = 0;
9431 unsigned char status_old, staged_status_old;
9432 unsigned char status_new, staged_status_new;
9434 *oldpath = NULL;
9435 *newpath = NULL;
9437 err = patch_check_path(old != NULL ? old : new, oldpath,
9438 &status_old, &staged_status_old, fileindex, worktree, repo);
9439 if (err)
9440 goto done;
9442 err = patch_check_path(new != NULL ? new : old, newpath,
9443 &status_new, &staged_status_new, fileindex, worktree, repo);
9444 if (err)
9445 goto done;
9447 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9448 file_renamed = 1;
9450 if (old != NULL && new == NULL)
9451 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9452 else if (file_renamed) {
9453 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9454 if (err == NULL)
9455 err = patch_can_add(*newpath, status_new);
9456 } else if (old == NULL)
9457 err = patch_can_add(*newpath, status_new);
9458 else
9459 err = patch_can_edit(*newpath, status_new, staged_status_new);
9461 done:
9462 if (err) {
9463 free(*oldpath);
9464 *oldpath = NULL;
9465 free(*newpath);
9466 *newpath = NULL;
9468 return err;
9471 const struct got_error *
9472 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9473 struct got_worktree *worktree, struct got_fileindex *fileindex,
9474 got_worktree_checkout_cb progress_cb, void *progress_arg)
9476 struct schedule_addition_args saa;
9478 memset(&saa, 0, sizeof(saa));
9479 saa.worktree = worktree;
9480 saa.fileindex = fileindex;
9481 saa.progress_cb = progress_cb;
9482 saa.progress_arg = progress_arg;
9483 saa.repo = repo;
9485 return worktree_status(worktree, path, fileindex, repo,
9486 schedule_addition, &saa, NULL, NULL, 1, 0);
9489 const struct got_error *
9490 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9491 struct got_worktree *worktree, struct got_fileindex *fileindex,
9492 got_worktree_delete_cb progress_cb, void *progress_arg)
9494 struct schedule_deletion_args sda;
9496 memset(&sda, 0, sizeof(sda));
9497 sda.worktree = worktree;
9498 sda.fileindex = fileindex;
9499 sda.progress_cb = progress_cb;
9500 sda.progress_arg = progress_arg;
9501 sda.repo = repo;
9502 sda.delete_local_mods = 0;
9503 sda.keep_on_disk = 0;
9504 sda.ignore_missing_paths = 0;
9505 sda.status_codes = NULL;
9507 return worktree_status(worktree, path, fileindex, repo,
9508 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9511 const struct got_error *
9512 got_worktree_patch_complete(struct got_fileindex *fileindex,
9513 const char *fileindex_path)
9515 const struct got_error *err = NULL;
9517 err = sync_fileindex(fileindex, fileindex_path);
9518 got_fileindex_free(fileindex);
9520 return err;