Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/stat.h>
18 #include <sys/queue.h>
19 #include <sys/tree.h>
21 #include <dirent.h>
22 #include <limits.h>
23 #include <stddef.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <time.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <sha1.h>
32 #include <sha2.h>
33 #include <zlib.h>
34 #include <fnmatch.h>
35 #include <libgen.h>
36 #include <uuid.h>
37 #include <util.h>
39 #include "got_error.h"
40 #include "got_repository.h"
41 #include "got_reference.h"
42 #include "got_object.h"
43 #include "got_path.h"
44 #include "got_cancel.h"
45 #include "got_worktree.h"
46 #include "got_opentemp.h"
47 #include "got_diff.h"
49 #include "got_lib_worktree.h"
50 #include "got_lib_hash.h"
51 #include "got_lib_fileindex.h"
52 #include "got_lib_inflate.h"
53 #include "got_lib_delta.h"
54 #include "got_lib_object.h"
55 #include "got_lib_object_parse.h"
56 #include "got_lib_object_create.h"
57 #include "got_lib_object_idset.h"
58 #include "got_lib_diff.h"
59 #include "got_lib_gotconfig.h"
61 #ifndef MIN
62 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
63 #endif
65 #define GOT_MERGE_LABEL_MERGED "merged change"
66 #define GOT_MERGE_LABEL_BASE "3-way merge base"
68 static mode_t apply_umask(mode_t);
70 static const struct got_error *
71 create_meta_file(const char *path_got, const char *name, const char *content)
72 {
73 const struct got_error *err = NULL;
74 char *path;
76 if (asprintf(&path, "%s/%s", path_got, name) == -1)
77 return got_error_from_errno("asprintf");
79 err = got_path_create_file(path, content);
80 free(path);
81 return err;
82 }
84 static const struct got_error *
85 update_meta_file(const char *path_got, const char *name, const char *content)
86 {
87 const struct got_error *err = NULL;
88 FILE *tmpfile = NULL;
89 char *tmppath = NULL;
90 char *path = NULL;
92 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
93 err = got_error_from_errno("asprintf");
94 path = NULL;
95 goto done;
96 }
98 err = got_opentemp_named(&tmppath, &tmpfile, path, "");
99 if (err)
100 goto done;
102 if (content) {
103 int len = fprintf(tmpfile, "%s\n", content);
104 if (len != strlen(content) + 1) {
105 err = got_error_from_errno2("fprintf", tmppath);
106 goto done;
110 if (rename(tmppath, path) != 0) {
111 err = got_error_from_errno3("rename", tmppath, path);
112 unlink(tmppath);
113 goto done;
116 done:
117 if (fclose(tmpfile) == EOF && err == NULL)
118 err = got_error_from_errno2("fclose", tmppath);
119 free(tmppath);
120 return err;
123 static const struct got_error *
124 write_head_ref(const char *path_got, struct got_reference *head_ref)
126 const struct got_error *err = NULL;
127 char *refstr = NULL;
129 if (got_ref_is_symbolic(head_ref)) {
130 refstr = got_ref_to_str(head_ref);
131 if (refstr == NULL)
132 return got_error_from_errno("got_ref_to_str");
133 } else {
134 refstr = strdup(got_ref_get_name(head_ref));
135 if (refstr == NULL)
136 return got_error_from_errno("strdup");
138 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
139 free(refstr);
140 return err;
143 const struct got_error *
144 got_worktree_init(const char *path, struct got_reference *head_ref,
145 const char *prefix, struct got_repository *repo)
147 const struct got_error *err = NULL;
148 struct got_object_id *commit_id = NULL;
149 uuid_t uuid;
150 uint32_t uuid_status;
151 int obj_type;
152 char *path_got = NULL;
153 char *formatstr = NULL;
154 char *absprefix = NULL;
155 char *basestr = NULL;
156 char *uuidstr = NULL;
158 if (strcmp(path, got_repo_get_path(repo)) == 0) {
159 err = got_error(GOT_ERR_WORKTREE_REPO);
160 goto done;
163 err = got_ref_resolve(&commit_id, repo, head_ref);
164 if (err)
165 return err;
166 err = got_object_get_type(&obj_type, repo, commit_id);
167 if (err)
168 return err;
169 if (obj_type != GOT_OBJ_TYPE_COMMIT)
170 return got_error(GOT_ERR_OBJ_TYPE);
172 if (!got_path_is_absolute(prefix)) {
173 if (asprintf(&absprefix, "/%s", prefix) == -1)
174 return got_error_from_errno("asprintf");
177 /* Create top-level directory (may already exist). */
178 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
179 err = got_error_from_errno2("mkdir", path);
180 goto done;
183 /* Create .got directory (may already exist). */
184 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
185 err = got_error_from_errno("asprintf");
186 goto done;
188 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
189 err = got_error_from_errno2("mkdir", path_got);
190 goto done;
193 /* Create an empty lock file. */
194 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
195 if (err)
196 goto done;
198 /* Create an empty file index. */
199 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
200 if (err)
201 goto done;
203 /* Write the HEAD reference. */
204 err = write_head_ref(path_got, head_ref);
205 if (err)
206 goto done;
208 /* Record our base commit. */
209 err = got_object_id_str(&basestr, commit_id);
210 if (err)
211 goto done;
212 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
213 if (err)
214 goto done;
216 /* Store path to repository. */
217 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
218 got_repo_get_path(repo));
219 if (err)
220 goto done;
222 /* Store in-repository path prefix. */
223 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
224 absprefix ? absprefix : prefix);
225 if (err)
226 goto done;
228 /* Generate UUID. */
229 uuid_create(&uuid, &uuid_status);
230 if (uuid_status != uuid_s_ok) {
231 err = got_error_uuid(uuid_status, "uuid_create");
232 goto done;
234 uuid_to_string(&uuid, &uuidstr, &uuid_status);
235 if (uuid_status != uuid_s_ok) {
236 err = got_error_uuid(uuid_status, "uuid_to_string");
237 goto done;
239 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
240 if (err)
241 goto done;
243 /* Stamp work tree with format file. */
244 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
245 err = got_error_from_errno("asprintf");
246 goto done;
248 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
249 if (err)
250 goto done;
252 done:
253 free(commit_id);
254 free(path_got);
255 free(formatstr);
256 free(absprefix);
257 free(basestr);
258 free(uuidstr);
259 return err;
262 const struct got_error *
263 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
264 const char *path_prefix)
266 char *absprefix = NULL;
268 if (!got_path_is_absolute(path_prefix)) {
269 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
270 return got_error_from_errno("asprintf");
272 *match = (strcmp(absprefix ? absprefix : path_prefix,
273 worktree->path_prefix) == 0);
274 free(absprefix);
275 return NULL;
278 const char *
279 got_worktree_get_head_ref_name(struct got_worktree *worktree)
281 return worktree->head_ref_name;
284 const struct got_error *
285 got_worktree_set_head_ref(struct got_worktree *worktree,
286 struct got_reference *head_ref)
288 const struct got_error *err = NULL;
289 char *path_got = NULL, *head_ref_name = NULL;
291 if (asprintf(&path_got, "%s/%s", worktree->root_path,
292 GOT_WORKTREE_GOT_DIR) == -1) {
293 err = got_error_from_errno("asprintf");
294 path_got = NULL;
295 goto done;
298 head_ref_name = strdup(got_ref_get_name(head_ref));
299 if (head_ref_name == NULL) {
300 err = got_error_from_errno("strdup");
301 goto done;
304 err = write_head_ref(path_got, head_ref);
305 if (err)
306 goto done;
308 free(worktree->head_ref_name);
309 worktree->head_ref_name = head_ref_name;
310 done:
311 free(path_got);
312 if (err)
313 free(head_ref_name);
314 return err;
317 struct got_object_id *
318 got_worktree_get_base_commit_id(struct got_worktree *worktree)
320 return worktree->base_commit_id;
323 const struct got_error *
324 got_worktree_set_base_commit_id(struct got_worktree *worktree,
325 struct got_repository *repo, struct got_object_id *commit_id)
327 const struct got_error *err;
328 struct got_object *obj = NULL;
329 char *id_str = NULL;
330 char *path_got = NULL;
332 if (asprintf(&path_got, "%s/%s", worktree->root_path,
333 GOT_WORKTREE_GOT_DIR) == -1) {
334 err = got_error_from_errno("asprintf");
335 path_got = NULL;
336 goto done;
339 err = got_object_open(&obj, repo, commit_id);
340 if (err)
341 return err;
343 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
344 err = got_error(GOT_ERR_OBJ_TYPE);
345 goto done;
348 /* Record our base commit. */
349 err = got_object_id_str(&id_str, commit_id);
350 if (err)
351 goto done;
352 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
353 if (err)
354 goto done;
356 free(worktree->base_commit_id);
357 worktree->base_commit_id = got_object_id_dup(commit_id);
358 if (worktree->base_commit_id == NULL) {
359 err = got_error_from_errno("got_object_id_dup");
360 goto done;
362 done:
363 if (obj)
364 got_object_close(obj);
365 free(id_str);
366 free(path_got);
367 return err;
370 const struct got_gotconfig *
371 got_worktree_get_gotconfig(struct got_worktree *worktree)
373 return worktree->gotconfig;
376 static const struct got_error *
377 lock_worktree(struct got_worktree *worktree, int operation)
379 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
380 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
381 : got_error_from_errno2("flock",
382 got_worktree_get_root_path(worktree)));
383 return NULL;
386 static const struct got_error *
387 add_dir_on_disk(struct got_worktree *worktree, const char *path)
389 const struct got_error *err = NULL;
390 char *abspath;
392 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
393 return got_error_from_errno("asprintf");
395 err = got_path_mkdir(abspath);
396 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
397 struct stat sb;
398 err = NULL;
399 if (lstat(abspath, &sb) == -1) {
400 err = got_error_from_errno2("lstat", abspath);
401 } else if (!S_ISDIR(sb.st_mode)) {
402 /* TODO directory is obstructed; do something */
403 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
406 free(abspath);
407 return err;
410 static const struct got_error *
411 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
413 const struct got_error *err = NULL;
414 uint8_t fbuf1[8192];
415 uint8_t fbuf2[8192];
416 size_t flen1 = 0, flen2 = 0;
418 *same = 1;
420 for (;;) {
421 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
422 if (flen1 == 0 && ferror(f1)) {
423 err = got_error_from_errno("fread");
424 break;
426 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
427 if (flen2 == 0 && ferror(f2)) {
428 err = got_error_from_errno("fread");
429 break;
431 if (flen1 == 0) {
432 if (flen2 != 0)
433 *same = 0;
434 break;
435 } else if (flen2 == 0) {
436 if (flen1 != 0)
437 *same = 0;
438 break;
439 } else if (flen1 == flen2) {
440 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
441 *same = 0;
442 break;
444 } else {
445 *same = 0;
446 break;
450 return err;
453 static const struct got_error *
454 check_files_equal(int *same, FILE *f1, FILE *f2)
456 struct stat sb;
457 size_t size1, size2;
459 *same = 1;
461 if (fstat(fileno(f1), &sb) != 0)
462 return got_error_from_errno("fstat");
463 size1 = sb.st_size;
465 if (fstat(fileno(f2), &sb) != 0)
466 return got_error_from_errno("fstat");
467 size2 = sb.st_size;
469 if (size1 != size2) {
470 *same = 0;
471 return NULL;
474 if (fseek(f1, 0L, SEEK_SET) == -1)
475 return got_ferror(f1, GOT_ERR_IO);
476 if (fseek(f2, 0L, SEEK_SET) == -1)
477 return got_ferror(f2, GOT_ERR_IO);
479 return check_file_contents_equal(same, f1, f2);
482 static const struct got_error *
483 copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
485 uint8_t fbuf[65536];
486 size_t flen;
487 ssize_t outlen;
489 *outsize = 0;
491 if (fseek(f, 0L, SEEK_SET) == -1)
492 return got_ferror(f, GOT_ERR_IO);
494 for (;;) {
495 flen = fread(fbuf, 1, sizeof(fbuf), f);
496 if (flen == 0) {
497 if (ferror(f))
498 return got_error_from_errno("fread");
499 if (feof(f))
500 break;
502 outlen = write(outfd, fbuf, flen);
503 if (outlen == -1)
504 return got_error_from_errno("write");
505 if (outlen != flen)
506 return got_error(GOT_ERR_IO);
507 *outsize += outlen;
510 return NULL;
513 static const struct got_error *
514 merge_binary_file(int *overlapcnt, int merged_fd,
515 FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
516 const char *label_deriv, const char *label_orig, const char *label_deriv2,
517 const char *ondisk_path)
519 const struct got_error *err = NULL;
520 int same_content, changed_deriv, changed_deriv2;
521 int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
522 off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
523 char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
524 char *base_path_orig = NULL, *base_path_deriv = NULL;
525 char *base_path_deriv2 = NULL;
527 *overlapcnt = 0;
529 err = check_files_equal(&same_content, f_deriv, f_deriv2);
530 if (err)
531 return err;
533 if (same_content)
534 return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
536 err = check_files_equal(&same_content, f_deriv, f_orig);
537 if (err)
538 return err;
539 changed_deriv = !same_content;
540 err = check_files_equal(&same_content, f_deriv2, f_orig);
541 if (err)
542 return err;
543 changed_deriv2 = !same_content;
545 if (changed_deriv && changed_deriv2) {
546 *overlapcnt = 1;
547 if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
548 err = got_error_from_errno("asprintf");
549 goto done;
551 if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
552 err = got_error_from_errno("asprintf");
553 goto done;
555 if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
556 err = got_error_from_errno("asprintf");
557 goto done;
559 err = got_opentemp_named_fd(&path_orig, &fd_orig,
560 base_path_orig, "");
561 if (err)
562 goto done;
563 err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
564 base_path_deriv, "");
565 if (err)
566 goto done;
567 err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
568 base_path_deriv2, "");
569 if (err)
570 goto done;
571 err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
572 if (err)
573 goto done;
574 err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
575 if (err)
576 goto done;
577 err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
578 if (err)
579 goto done;
580 if (dprintf(merged_fd, "Binary files differ and cannot be "
581 "merged automatically:\n") < 0) {
582 err = got_error_from_errno("dprintf");
583 goto done;
585 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
586 GOT_DIFF_CONFLICT_MARKER_BEGIN,
587 label_deriv ? " " : "",
588 label_deriv ? label_deriv : "",
589 path_deriv) < 0) {
590 err = got_error_from_errno("dprintf");
591 goto done;
593 if (size_orig > 0) {
594 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
595 GOT_DIFF_CONFLICT_MARKER_ORIG,
596 label_orig ? " " : "",
597 label_orig ? label_orig : "",
598 path_orig) < 0) {
599 err = got_error_from_errno("dprintf");
600 goto done;
603 if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
604 GOT_DIFF_CONFLICT_MARKER_SEP,
605 path_deriv2,
606 GOT_DIFF_CONFLICT_MARKER_END,
607 label_deriv2 ? " " : "",
608 label_deriv2 ? label_deriv2 : "") < 0) {
609 err = got_error_from_errno("dprintf");
610 goto done;
612 } else if (changed_deriv)
613 err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
614 else if (changed_deriv2)
615 err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
616 done:
617 if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
618 err == NULL)
619 err = got_error_from_errno2("unlink", path_orig);
620 if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
621 err = got_error_from_errno2("close", path_orig);
622 if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
623 err = got_error_from_errno2("close", path_deriv);
624 if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
625 err = got_error_from_errno2("close", path_deriv2);
626 free(path_orig);
627 free(path_deriv);
628 free(path_deriv2);
629 free(base_path_orig);
630 free(base_path_deriv);
631 free(base_path_deriv2);
632 return err;
635 /*
636 * Perform a 3-way merge where the file f_orig acts as the common
637 * ancestor, the file f_deriv acts as the first derived version,
638 * and the file f_deriv2 acts as the second derived version.
639 * The merge result will be written to a new file at ondisk_path; any
640 * existing file at this path will be replaced.
641 */
642 static const struct got_error *
643 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
644 FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
645 const char *path, uint16_t st_mode,
646 const char *label_orig, const char *label_deriv, const char *label_deriv2,
647 enum got_diff_algorithm diff_algo, struct got_repository *repo,
648 got_worktree_checkout_cb progress_cb, void *progress_arg)
650 const struct got_error *err = NULL;
651 int merged_fd = -1;
652 FILE *f_merged = NULL;
653 char *merged_path = NULL, *base_path = NULL;
654 int overlapcnt = 0;
655 char *parent = NULL;
657 *local_changes_subsumed = 0;
659 err = got_path_dirname(&parent, ondisk_path);
660 if (err)
661 return err;
663 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
664 err = got_error_from_errno("asprintf");
665 goto done;
668 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path, "");
669 if (err)
670 goto done;
672 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
673 f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
674 if (err) {
675 if (err->code != GOT_ERR_FILE_BINARY)
676 goto done;
677 err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
678 f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
679 ondisk_path);
680 if (err)
681 goto done;
684 err = (*progress_cb)(progress_arg,
685 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
686 if (err)
687 goto done;
689 if (fsync(merged_fd) != 0) {
690 err = got_error_from_errno("fsync");
691 goto done;
694 f_merged = fdopen(merged_fd, "r");
695 if (f_merged == NULL) {
696 err = got_error_from_errno("fdopen");
697 goto done;
699 merged_fd = -1;
701 /* Check if a clean merge has subsumed all local changes. */
702 if (overlapcnt == 0) {
703 err = check_files_equal(local_changes_subsumed, f_deriv,
704 f_merged);
705 if (err)
706 goto done;
709 if (fchmod(fileno(f_merged), apply_umask(st_mode)) != 0) {
710 err = got_error_from_errno2("fchmod", merged_path);
711 goto done;
714 if (rename(merged_path, ondisk_path) != 0) {
715 err = got_error_from_errno3("rename", merged_path,
716 ondisk_path);
717 goto done;
719 done:
720 if (err) {
721 if (merged_path)
722 unlink(merged_path);
724 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
725 err = got_error_from_errno("close");
726 if (f_merged && fclose(f_merged) == EOF && err == NULL)
727 err = got_error_from_errno("fclose");
728 free(merged_path);
729 free(base_path);
730 free(parent);
731 return err;
734 static const struct got_error *
735 update_symlink(const char *ondisk_path, const char *target_path,
736 size_t target_len)
738 /* This is not atomic but matches what 'ln -sf' does. */
739 if (unlink(ondisk_path) == -1)
740 return got_error_from_errno2("unlink", ondisk_path);
741 if (symlink(target_path, ondisk_path) == -1)
742 return got_error_from_errno3("symlink", target_path,
743 ondisk_path);
744 return NULL;
747 /*
748 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
749 * in the work tree with a file that contains conflict markers and the
750 * conflicting target paths of the original version, a "derived version"
751 * of a symlink from an incoming change, and a local version of the symlink.
753 * The original versions's target path can be NULL if it is not available,
754 * such as if both derived versions added a new symlink at the same path.
756 * The incoming derived symlink target is NULL in case the incoming change
757 * has deleted this symlink.
758 */
759 static const struct got_error *
760 install_symlink_conflict(const char *deriv_target,
761 struct got_object_id *deriv_base_commit_id, const char *orig_target,
762 const char *label_orig, const char *local_target, const char *ondisk_path)
764 const struct got_error *err;
765 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
766 FILE *f = NULL;
768 err = got_object_id_str(&id_str, deriv_base_commit_id);
769 if (err)
770 return got_error_from_errno("asprintf");
772 if (asprintf(&label_deriv, "%s: commit %s",
773 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
774 err = got_error_from_errno("asprintf");
775 goto done;
778 err = got_opentemp_named(&path, &f, "got-symlink-conflict", "");
779 if (err)
780 goto done;
782 if (fchmod(fileno(f), apply_umask(GOT_DEFAULT_FILE_MODE)) == -1) {
783 err = got_error_from_errno2("fchmod", path);
784 goto done;
787 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
788 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
789 deriv_target ? deriv_target : "(symlink was deleted)",
790 orig_target ? label_orig : "",
791 orig_target ? "\n" : "",
792 orig_target ? orig_target : "",
793 orig_target ? "\n" : "",
794 GOT_DIFF_CONFLICT_MARKER_SEP,
795 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
796 err = got_error_from_errno2("fprintf", path);
797 goto done;
800 if (unlink(ondisk_path) == -1) {
801 err = got_error_from_errno2("unlink", ondisk_path);
802 goto done;
804 if (rename(path, ondisk_path) == -1) {
805 err = got_error_from_errno3("rename", path, ondisk_path);
806 goto done;
808 done:
809 if (f != NULL && fclose(f) == EOF && err == NULL)
810 err = got_error_from_errno2("fclose", path);
811 free(path);
812 free(id_str);
813 free(label_deriv);
814 return err;
817 /* forward declaration */
818 static const struct got_error *
819 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
820 const char *, const char *, uint16_t, const char *,
821 struct got_blob_object *, struct got_object_id *,
822 struct got_repository *, got_worktree_checkout_cb, void *);
824 /*
825 * Merge a symlink into the work tree, where blob_orig acts as the common
826 * ancestor, deriv_target is the link target of the first derived version,
827 * and the symlink on disk acts as the second derived version.
828 * Assume that contents of both blobs represent symlinks.
829 */
830 static const struct got_error *
831 merge_symlink(struct got_worktree *worktree,
832 struct got_blob_object *blob_orig, const char *ondisk_path,
833 const char *path, const char *label_orig, const char *deriv_target,
834 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
835 got_worktree_checkout_cb progress_cb, void *progress_arg)
837 const struct got_error *err = NULL;
838 char *ancestor_target = NULL;
839 struct stat sb;
840 ssize_t ondisk_len, deriv_len;
841 char ondisk_target[PATH_MAX];
842 int have_local_change = 0;
843 int have_incoming_change = 0;
845 if (lstat(ondisk_path, &sb) == -1)
846 return got_error_from_errno2("lstat", ondisk_path);
848 ondisk_len = readlink(ondisk_path, ondisk_target,
849 sizeof(ondisk_target));
850 if (ondisk_len == -1) {
851 err = got_error_from_errno2("readlink",
852 ondisk_path);
853 goto done;
855 ondisk_target[ondisk_len] = '\0';
857 if (blob_orig) {
858 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
859 if (err)
860 goto done;
863 if (ancestor_target == NULL ||
864 (ondisk_len != strlen(ancestor_target) ||
865 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
866 have_local_change = 1;
868 deriv_len = strlen(deriv_target);
869 if (ancestor_target == NULL ||
870 (deriv_len != strlen(ancestor_target) ||
871 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
872 have_incoming_change = 1;
874 if (!have_local_change && !have_incoming_change) {
875 if (ancestor_target) {
876 /* Both sides made the same change. */
877 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
878 path);
879 } else if (deriv_len == ondisk_len &&
880 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
881 /* Both sides added the same symlink. */
882 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
883 path);
884 } else {
885 /* Both sides added symlinks which don't match. */
886 err = install_symlink_conflict(deriv_target,
887 deriv_base_commit_id, ancestor_target,
888 label_orig, ondisk_target, ondisk_path);
889 if (err)
890 goto done;
891 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
892 path);
894 } else if (!have_local_change && have_incoming_change) {
895 /* Apply the incoming change. */
896 err = update_symlink(ondisk_path, deriv_target,
897 strlen(deriv_target));
898 if (err)
899 goto done;
900 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
901 } else if (have_local_change && have_incoming_change) {
902 if (deriv_len == ondisk_len &&
903 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
904 /* Both sides made the same change. */
905 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
906 path);
907 } else {
908 err = install_symlink_conflict(deriv_target,
909 deriv_base_commit_id, ancestor_target, label_orig,
910 ondisk_target, ondisk_path);
911 if (err)
912 goto done;
913 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
914 path);
918 done:
919 free(ancestor_target);
920 return err;
923 static const struct got_error *
924 dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
926 const struct got_error *err = NULL;
927 char target_path[PATH_MAX];
928 ssize_t target_len;
929 size_t n;
930 FILE *f;
932 *outfile = NULL;
934 f = got_opentemp();
935 if (f == NULL)
936 return got_error_from_errno("got_opentemp");
937 target_len = readlink(ondisk_path, target_path, sizeof(target_path));
938 if (target_len == -1) {
939 err = got_error_from_errno2("readlink", ondisk_path);
940 goto done;
942 n = fwrite(target_path, 1, target_len, f);
943 if (n != target_len) {
944 err = got_ferror(f, GOT_ERR_IO);
945 goto done;
947 if (fflush(f) == EOF) {
948 err = got_error_from_errno("fflush");
949 goto done;
951 if (fseek(f, 0L, SEEK_SET) == -1) {
952 err = got_ferror(f, GOT_ERR_IO);
953 goto done;
955 done:
956 if (err)
957 fclose(f);
958 else
959 *outfile = f;
960 return err;
963 /*
964 * Perform a 3-way merge where blob_orig acts as the common ancestor,
965 * blob_deriv acts as the first derived version, and the file on disk
966 * acts as the second derived version.
967 */
968 static const struct got_error *
969 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
970 struct got_blob_object *blob_orig, const char *ondisk_path,
971 const char *path, uint16_t st_mode, const char *label_orig,
972 struct got_blob_object *blob_deriv,
973 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
974 got_worktree_checkout_cb progress_cb, void *progress_arg)
976 const struct got_error *err = NULL;
977 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
978 char *blob_orig_path = NULL;
979 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
980 char *label_deriv = NULL, *parent = NULL;
982 *local_changes_subsumed = 0;
984 err = got_path_dirname(&parent, ondisk_path);
985 if (err)
986 return err;
988 if (blob_orig) {
989 if (asprintf(&base_path, "%s/got-merge-blob-orig",
990 parent) == -1) {
991 err = got_error_from_errno("asprintf");
992 base_path = NULL;
993 goto done;
996 err = got_opentemp_named(&blob_orig_path, &f_orig,
997 base_path, "");
998 if (err)
999 goto done;
1000 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1001 blob_orig);
1002 if (err)
1003 goto done;
1004 free(base_path);
1005 } else {
1007 * No common ancestor exists. This is an "add vs add" conflict
1008 * and we simply use an empty ancestor file to make both files
1009 * appear in the merged result in their entirety.
1011 f_orig = got_opentemp();
1012 if (f_orig == NULL) {
1013 err = got_error_from_errno("got_opentemp");
1014 goto done;
1018 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1019 err = got_error_from_errno("asprintf");
1020 base_path = NULL;
1021 goto done;
1024 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path, "");
1025 if (err)
1026 goto done;
1027 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1028 blob_deriv);
1029 if (err)
1030 goto done;
1032 err = got_object_id_str(&id_str, deriv_base_commit_id);
1033 if (err)
1034 goto done;
1035 if (asprintf(&label_deriv, "%s: commit %s",
1036 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1037 err = got_error_from_errno("asprintf");
1038 goto done;
1042 * In order the run a 3-way merge with a symlink we copy the symlink's
1043 * target path into a temporary file and use that file with diff3.
1045 if (S_ISLNK(st_mode)) {
1046 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1047 if (err)
1048 goto done;
1049 } else {
1050 int fd;
1051 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1052 if (fd == -1) {
1053 err = got_error_from_errno2("open", ondisk_path);
1054 goto done;
1056 f_deriv2 = fdopen(fd, "r");
1057 if (f_deriv2 == NULL) {
1058 err = got_error_from_errno2("fdopen", ondisk_path);
1059 close(fd);
1060 goto done;
1064 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1065 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1066 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1067 done:
1068 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1069 err = got_error_from_errno("fclose");
1070 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1071 err = got_error_from_errno("fclose");
1072 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1073 err = got_error_from_errno("fclose");
1074 free(base_path);
1075 if (blob_orig_path) {
1076 unlink(blob_orig_path);
1077 free(blob_orig_path);
1079 if (blob_deriv_path) {
1080 unlink(blob_deriv_path);
1081 free(blob_deriv_path);
1083 free(id_str);
1084 free(label_deriv);
1085 free(parent);
1086 return err;
1089 static const struct got_error *
1090 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1091 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1092 int wt_fd, const char *path, struct got_object_id *blob_id)
1094 const struct got_error *err = NULL;
1095 struct got_fileindex_entry *new_ie;
1097 *new_iep = NULL;
1099 err = got_fileindex_entry_alloc(&new_ie, path);
1100 if (err)
1101 return err;
1103 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1104 blob_id->hash, base_commit_id->hash, 1);
1105 if (err)
1106 goto done;
1108 err = got_fileindex_entry_add(fileindex, new_ie);
1109 done:
1110 if (err)
1111 got_fileindex_entry_free(new_ie);
1112 else
1113 *new_iep = new_ie;
1114 return err;
1117 static mode_t
1118 get_ondisk_perms(int executable, mode_t st_mode)
1120 mode_t xbits = S_IXUSR;
1122 if (executable) {
1123 /* Map read bits to execute bits. */
1124 if (st_mode & S_IRGRP)
1125 xbits |= S_IXGRP;
1126 if (st_mode & S_IROTH)
1127 xbits |= S_IXOTH;
1128 return st_mode | xbits;
1131 return st_mode;
1134 static mode_t
1135 apply_umask(mode_t mode)
1137 mode_t um;
1139 um = umask(000);
1140 umask(um);
1141 return mode & ~um;
1144 /* forward declaration */
1145 static const struct got_error *
1146 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1147 const char *path, mode_t te_mode, mode_t st_mode,
1148 struct got_blob_object *blob, int restoring_missing_file,
1149 int reverting_versioned_file, int installing_bad_symlink,
1150 int path_is_unversioned, struct got_repository *repo,
1151 got_worktree_checkout_cb progress_cb, void *progress_arg);
1154 * This function assumes that the provided symlink target points at a
1155 * safe location in the work tree!
1157 static const struct got_error *
1158 replace_existing_symlink(int *did_something, const char *ondisk_path,
1159 const char *target_path, size_t target_len)
1161 const struct got_error *err = NULL;
1162 ssize_t elen;
1163 char etarget[PATH_MAX];
1164 int fd;
1166 *did_something = 0;
1169 * "Bad" symlinks (those pointing outside the work tree or into the
1170 * .got directory) are installed in the work tree as a regular file
1171 * which contains the bad symlink target path.
1172 * The new symlink target has already been checked for safety by our
1173 * caller. If we can successfully open a regular file then we simply
1174 * replace this file with a symlink below.
1176 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1177 if (fd == -1) {
1178 if (!got_err_open_nofollow_on_symlink())
1179 return got_error_from_errno2("open", ondisk_path);
1181 /* We are updating an existing on-disk symlink. */
1182 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1183 if (elen == -1)
1184 return got_error_from_errno2("readlink", ondisk_path);
1186 if (elen == target_len &&
1187 memcmp(etarget, target_path, target_len) == 0)
1188 return NULL; /* nothing to do */
1191 *did_something = 1;
1192 err = update_symlink(ondisk_path, target_path, target_len);
1193 if (fd != -1 && close(fd) == -1 && err == NULL)
1194 err = got_error_from_errno2("close", ondisk_path);
1195 return err;
1198 static const struct got_error *
1199 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1200 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1202 const struct got_error *err = NULL;
1203 char canonpath[PATH_MAX];
1204 char *path_got = NULL;
1206 *is_bad_symlink = 0;
1208 if (target_len >= sizeof(canonpath)) {
1209 *is_bad_symlink = 1;
1210 return NULL;
1214 * We do not use realpath(3) to resolve the symlink's target
1215 * path because we don't want to resolve symlinks recursively.
1216 * Instead we make the path absolute and then canonicalize it.
1217 * Relative symlink target lookup should begin at the directory
1218 * in which the blob object is being installed.
1220 if (!got_path_is_absolute(target_path)) {
1221 char *abspath, *parent;
1222 err = got_path_dirname(&parent, ondisk_path);
1223 if (err)
1224 return err;
1225 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1226 free(parent);
1227 return got_error_from_errno("asprintf");
1229 free(parent);
1230 if (strlen(abspath) >= sizeof(canonpath)) {
1231 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1232 free(abspath);
1233 return err;
1235 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1236 free(abspath);
1237 if (err)
1238 return err;
1239 } else {
1240 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1241 if (err)
1242 return err;
1245 /* Only allow symlinks pointing at paths within the work tree. */
1246 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1247 *is_bad_symlink = 1;
1248 return NULL;
1251 /* Do not allow symlinks pointing into the .got directory. */
1252 if (asprintf(&path_got, "%s/%s", wtroot_path,
1253 GOT_WORKTREE_GOT_DIR) == -1)
1254 return got_error_from_errno("asprintf");
1255 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1256 *is_bad_symlink = 1;
1258 free(path_got);
1259 return NULL;
1262 static const struct got_error *
1263 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1264 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1265 int restoring_missing_file, int reverting_versioned_file,
1266 int path_is_unversioned, int allow_bad_symlinks,
1267 struct got_repository *repo,
1268 got_worktree_checkout_cb progress_cb, void *progress_arg)
1270 const struct got_error *err = NULL;
1271 char target_path[PATH_MAX];
1272 size_t len, target_len = 0;
1273 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1274 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1276 *is_bad_symlink = 0;
1279 * Blob object content specifies the target path of the link.
1280 * If a symbolic link cannot be installed we instead create
1281 * a regular file which contains the link target path stored
1282 * in the blob object.
1284 do {
1285 err = got_object_blob_read_block(&len, blob);
1286 if (err)
1287 return err;
1289 if (len + target_len >= sizeof(target_path)) {
1290 /* Path too long; install as a regular file. */
1291 *is_bad_symlink = 1;
1292 got_object_blob_rewind(blob);
1293 return install_blob(worktree, ondisk_path, path,
1294 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1295 restoring_missing_file, reverting_versioned_file,
1296 1, path_is_unversioned, repo, progress_cb,
1297 progress_arg);
1299 if (len > 0) {
1300 /* Skip blob object header first time around. */
1301 memcpy(target_path + target_len, buf + hdrlen,
1302 len - hdrlen);
1303 target_len += len - hdrlen;
1304 hdrlen = 0;
1306 } while (len != 0);
1307 target_path[target_len] = '\0';
1309 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1310 ondisk_path, worktree->root_path);
1311 if (err)
1312 return err;
1314 if (*is_bad_symlink && !allow_bad_symlinks) {
1315 /* install as a regular file */
1316 got_object_blob_rewind(blob);
1317 err = install_blob(worktree, ondisk_path, path,
1318 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1319 restoring_missing_file, reverting_versioned_file, 1,
1320 path_is_unversioned, repo, progress_cb, progress_arg);
1321 return err;
1324 if (symlink(target_path, ondisk_path) == -1) {
1325 if (errno == EEXIST) {
1326 int symlink_replaced;
1327 if (path_is_unversioned) {
1328 err = (*progress_cb)(progress_arg,
1329 GOT_STATUS_UNVERSIONED, path);
1330 return err;
1332 err = replace_existing_symlink(&symlink_replaced,
1333 ondisk_path, target_path, target_len);
1334 if (err)
1335 return err;
1336 if (progress_cb) {
1337 if (symlink_replaced) {
1338 err = (*progress_cb)(progress_arg,
1339 reverting_versioned_file ?
1340 GOT_STATUS_REVERT :
1341 GOT_STATUS_UPDATE, path);
1342 } else {
1343 err = (*progress_cb)(progress_arg,
1344 GOT_STATUS_EXISTS, path);
1347 return err; /* Nothing else to do. */
1350 if (errno == ENOENT) {
1351 char *parent;
1352 err = got_path_dirname(&parent, ondisk_path);
1353 if (err)
1354 return err;
1355 err = add_dir_on_disk(worktree, parent);
1356 free(parent);
1357 if (err)
1358 return err;
1360 * Retry, and fall through to error handling
1361 * below if this second attempt fails.
1363 if (symlink(target_path, ondisk_path) != -1) {
1364 err = NULL; /* success */
1365 return err;
1369 /* Handle errors from first or second creation attempt. */
1370 if (errno == ENAMETOOLONG) {
1371 /* bad target path; install as a regular file */
1372 *is_bad_symlink = 1;
1373 got_object_blob_rewind(blob);
1374 err = install_blob(worktree, ondisk_path, path,
1375 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1376 restoring_missing_file, reverting_versioned_file, 1,
1377 path_is_unversioned, repo,
1378 progress_cb, progress_arg);
1379 } else if (errno == ENOTDIR) {
1380 err = got_error_path(ondisk_path,
1381 GOT_ERR_FILE_OBSTRUCTED);
1382 } else {
1383 err = got_error_from_errno3("symlink",
1384 target_path, ondisk_path);
1386 } else if (progress_cb)
1387 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1388 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1389 return err;
1392 static const struct got_error *
1393 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1394 const char *path, mode_t te_mode, mode_t st_mode,
1395 struct got_blob_object *blob, int restoring_missing_file,
1396 int reverting_versioned_file, int installing_bad_symlink,
1397 int path_is_unversioned, struct got_repository *repo,
1398 got_worktree_checkout_cb progress_cb, void *progress_arg)
1400 const struct got_error *err = NULL;
1401 int fd = -1;
1402 size_t len, hdrlen;
1403 int update = 0;
1404 char *tmppath = NULL;
1405 mode_t mode;
1407 mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1408 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1409 O_CLOEXEC, mode);
1410 if (fd == -1) {
1411 if (errno == ENOENT) {
1412 char *parent;
1413 err = got_path_dirname(&parent, path);
1414 if (err)
1415 return err;
1416 err = add_dir_on_disk(worktree, parent);
1417 free(parent);
1418 if (err)
1419 return err;
1420 fd = open(ondisk_path,
1421 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1422 mode);
1423 if (fd == -1)
1424 return got_error_from_errno2("open",
1425 ondisk_path);
1426 } else if (errno == EEXIST) {
1427 if (path_is_unversioned) {
1428 err = (*progress_cb)(progress_arg,
1429 GOT_STATUS_UNVERSIONED, path);
1430 goto done;
1432 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1433 !S_ISREG(st_mode) && !installing_bad_symlink) {
1434 /* TODO file is obstructed; do something */
1435 err = got_error_path(ondisk_path,
1436 GOT_ERR_FILE_OBSTRUCTED);
1437 goto done;
1438 } else {
1439 err = got_opentemp_named_fd(&tmppath, &fd,
1440 ondisk_path, "");
1441 if (err)
1442 goto done;
1443 update = 1;
1445 if (fchmod(fd, apply_umask(mode)) == -1) {
1446 err = got_error_from_errno2("fchmod",
1447 tmppath);
1448 goto done;
1451 } else
1452 return got_error_from_errno2("open", ondisk_path);
1455 if (progress_cb) {
1456 if (restoring_missing_file)
1457 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1458 path);
1459 else if (reverting_versioned_file)
1460 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1461 path);
1462 else
1463 err = (*progress_cb)(progress_arg,
1464 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1465 if (err)
1466 goto done;
1469 hdrlen = got_object_blob_get_hdrlen(blob);
1470 do {
1471 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1472 err = got_object_blob_read_block(&len, blob);
1473 if (err)
1474 break;
1475 if (len > 0) {
1476 /* Skip blob object header first time around. */
1477 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1478 if (outlen == -1) {
1479 err = got_error_from_errno("write");
1480 goto done;
1481 } else if (outlen != len - hdrlen) {
1482 err = got_error(GOT_ERR_IO);
1483 goto done;
1485 hdrlen = 0;
1487 } while (len != 0);
1489 if (fsync(fd) != 0) {
1490 err = got_error_from_errno("fsync");
1491 goto done;
1494 if (update) {
1495 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1496 err = got_error_from_errno2("unlink", ondisk_path);
1497 goto done;
1499 if (rename(tmppath, ondisk_path) != 0) {
1500 err = got_error_from_errno3("rename", tmppath,
1501 ondisk_path);
1502 goto done;
1504 free(tmppath);
1505 tmppath = NULL;
1508 done:
1509 if (fd != -1 && close(fd) == -1 && err == NULL)
1510 err = got_error_from_errno("close");
1511 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1512 err = got_error_from_errno2("unlink", tmppath);
1513 free(tmppath);
1514 return err;
1517 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1518 static const struct got_error *
1519 get_modified_file_content_status(unsigned char *status, FILE *f)
1521 const struct got_error *err = NULL;
1522 const char *markers[3] = {
1523 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1524 GOT_DIFF_CONFLICT_MARKER_SEP,
1525 GOT_DIFF_CONFLICT_MARKER_END
1527 int i = 0;
1528 char *line = NULL;
1529 size_t linesize = 0;
1530 ssize_t linelen;
1532 while (*status == GOT_STATUS_MODIFY) {
1533 linelen = getline(&line, &linesize, f);
1534 if (linelen == -1) {
1535 if (feof(f))
1536 break;
1537 err = got_ferror(f, GOT_ERR_IO);
1538 break;
1541 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1542 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1543 == 0)
1544 *status = GOT_STATUS_CONFLICT;
1545 else
1546 i++;
1549 free(line);
1551 return err;
1554 static int
1555 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1557 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1558 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1561 static int
1562 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1564 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1565 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1566 ie->mtime_sec == sb->st_mtim.tv_sec &&
1567 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1568 ie->size == (sb->st_size & 0xffffffff) &&
1569 !xbit_differs(ie, sb->st_mode));
1572 static unsigned char
1573 get_staged_status(struct got_fileindex_entry *ie)
1575 switch (got_fileindex_entry_stage_get(ie)) {
1576 case GOT_FILEIDX_STAGE_ADD:
1577 return GOT_STATUS_ADD;
1578 case GOT_FILEIDX_STAGE_DELETE:
1579 return GOT_STATUS_DELETE;
1580 case GOT_FILEIDX_STAGE_MODIFY:
1581 return GOT_STATUS_MODIFY;
1582 default:
1583 return GOT_STATUS_NO_CHANGE;
1587 static const struct got_error *
1588 get_symlink_modification_status(unsigned char *status,
1589 struct got_fileindex_entry *ie, const char *abspath,
1590 int dirfd, const char *de_name, struct got_blob_object *blob)
1592 const struct got_error *err = NULL;
1593 char target_path[PATH_MAX];
1594 char etarget[PATH_MAX];
1595 ssize_t elen;
1596 size_t len, target_len = 0;
1597 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1598 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1600 *status = GOT_STATUS_NO_CHANGE;
1602 /* Blob object content specifies the target path of the link. */
1603 do {
1604 err = got_object_blob_read_block(&len, blob);
1605 if (err)
1606 return err;
1607 if (len + target_len >= sizeof(target_path)) {
1609 * Should not happen. The blob contents were OK
1610 * when this symlink was installed.
1612 return got_error(GOT_ERR_NO_SPACE);
1614 if (len > 0) {
1615 /* Skip blob object header first time around. */
1616 memcpy(target_path + target_len, buf + hdrlen,
1617 len - hdrlen);
1618 target_len += len - hdrlen;
1619 hdrlen = 0;
1621 } while (len != 0);
1622 target_path[target_len] = '\0';
1624 if (dirfd != -1) {
1625 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1626 if (elen == -1)
1627 return got_error_from_errno2("readlinkat", abspath);
1628 } else {
1629 elen = readlink(abspath, etarget, sizeof(etarget));
1630 if (elen == -1)
1631 return got_error_from_errno2("readlink", abspath);
1634 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1635 *status = GOT_STATUS_MODIFY;
1637 return NULL;
1640 static const struct got_error *
1641 get_file_status(unsigned char *status, struct stat *sb,
1642 struct got_fileindex_entry *ie, const char *abspath,
1643 int dirfd, const char *de_name, struct got_repository *repo)
1645 const struct got_error *err = NULL;
1646 struct got_object_id id;
1647 size_t hdrlen;
1648 int fd = -1, fd1 = -1;
1649 FILE *f = NULL;
1650 uint8_t fbuf[8192];
1651 struct got_blob_object *blob = NULL;
1652 size_t flen, blen;
1653 unsigned char staged_status = get_staged_status(ie);
1655 *status = GOT_STATUS_NO_CHANGE;
1656 memset(sb, 0, sizeof(*sb));
1659 * Whenever the caller provides a directory descriptor and a
1660 * directory entry name for the file, use them! This prevents
1661 * race conditions if filesystem paths change beneath our feet.
1663 if (dirfd != -1) {
1664 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1665 if (errno == ENOENT) {
1666 if (got_fileindex_entry_has_file_on_disk(ie))
1667 *status = GOT_STATUS_MISSING;
1668 else
1669 *status = GOT_STATUS_DELETE;
1670 goto done;
1672 err = got_error_from_errno2("fstatat", abspath);
1673 goto done;
1675 } else {
1676 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1677 if (fd == -1 && errno != ENOENT &&
1678 !got_err_open_nofollow_on_symlink())
1679 return got_error_from_errno2("open", abspath);
1680 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1681 if (lstat(abspath, sb) == -1)
1682 return got_error_from_errno2("lstat", abspath);
1683 } else if (fd == -1 || fstat(fd, sb) == -1) {
1684 if (errno == ENOENT) {
1685 if (got_fileindex_entry_has_file_on_disk(ie))
1686 *status = GOT_STATUS_MISSING;
1687 else
1688 *status = GOT_STATUS_DELETE;
1689 goto done;
1691 err = got_error_from_errno2("fstat", abspath);
1692 goto done;
1696 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1697 *status = GOT_STATUS_OBSTRUCTED;
1698 goto done;
1701 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1702 *status = GOT_STATUS_DELETE;
1703 goto done;
1704 } else if (!got_fileindex_entry_has_blob(ie) &&
1705 staged_status != GOT_STATUS_ADD) {
1706 *status = GOT_STATUS_ADD;
1707 goto done;
1710 if (!stat_info_differs(ie, sb))
1711 goto done;
1713 if (S_ISLNK(sb->st_mode) &&
1714 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1715 *status = GOT_STATUS_MODIFY;
1716 goto done;
1719 if (staged_status == GOT_STATUS_MODIFY ||
1720 staged_status == GOT_STATUS_ADD)
1721 memcpy(id.hash, ie->staged_blob_hash, sizeof(id.hash));
1722 else
1723 memcpy(id.hash, ie->blob_hash, sizeof(id.hash));
1724 id.algo = got_repo_get_hash_algorithm(repo);
1726 fd1 = got_opentempfd();
1727 if (fd1 == -1) {
1728 err = got_error_from_errno("got_opentempfd");
1729 goto done;
1731 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1732 if (err)
1733 goto done;
1735 if (S_ISLNK(sb->st_mode)) {
1736 err = get_symlink_modification_status(status, ie,
1737 abspath, dirfd, de_name, blob);
1738 goto done;
1741 if (dirfd != -1) {
1742 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1743 if (fd == -1) {
1744 err = got_error_from_errno2("openat", abspath);
1745 goto done;
1749 f = fdopen(fd, "r");
1750 if (f == NULL) {
1751 err = got_error_from_errno2("fdopen", abspath);
1752 goto done;
1754 fd = -1;
1755 hdrlen = got_object_blob_get_hdrlen(blob);
1756 for (;;) {
1757 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1758 err = got_object_blob_read_block(&blen, blob);
1759 if (err)
1760 goto done;
1761 /* Skip length of blob object header first time around. */
1762 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1763 if (flen == 0 && ferror(f)) {
1764 err = got_error_from_errno("fread");
1765 goto done;
1767 if (blen - hdrlen == 0) {
1768 if (flen != 0)
1769 *status = GOT_STATUS_MODIFY;
1770 break;
1771 } else if (flen == 0) {
1772 if (blen - hdrlen != 0)
1773 *status = GOT_STATUS_MODIFY;
1774 break;
1775 } else if (blen - hdrlen == flen) {
1776 /* Skip blob object header first time around. */
1777 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1778 *status = GOT_STATUS_MODIFY;
1779 break;
1781 } else {
1782 *status = GOT_STATUS_MODIFY;
1783 break;
1785 hdrlen = 0;
1788 if (*status == GOT_STATUS_MODIFY) {
1789 rewind(f);
1790 err = get_modified_file_content_status(status, f);
1791 } else if (xbit_differs(ie, sb->st_mode))
1792 *status = GOT_STATUS_MODE_CHANGE;
1793 done:
1794 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1795 err = got_error_from_errno("close");
1796 if (blob)
1797 got_object_blob_close(blob);
1798 if (f != NULL && fclose(f) == EOF && err == NULL)
1799 err = got_error_from_errno2("fclose", abspath);
1800 if (fd != -1 && close(fd) == -1 && err == NULL)
1801 err = got_error_from_errno2("close", abspath);
1802 return err;
1806 * Update timestamps in the file index if a file is unmodified and
1807 * we had to run a full content comparison to find out.
1809 static const struct got_error *
1810 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1811 struct got_fileindex_entry *ie, struct stat *sb)
1813 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1814 return got_fileindex_entry_update(ie, wt_fd, path,
1815 ie->blob_hash, ie->commit_hash, 1);
1817 return NULL;
1820 static const struct got_error *
1821 update_blob(struct got_worktree *worktree,
1822 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1823 struct got_tree_entry *te, const char *path,
1824 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1825 void *progress_arg)
1827 const struct got_error *err = NULL;
1828 struct got_blob_object *blob = NULL;
1829 char *ondisk_path = NULL;
1830 unsigned char status = GOT_STATUS_NO_CHANGE;
1831 struct stat sb;
1832 int fd1 = -1, fd2 = -1;
1834 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1835 return got_error_from_errno("asprintf");
1837 if (ie) {
1838 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1839 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1840 goto done;
1842 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1843 repo);
1844 if (err)
1845 goto done;
1846 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1847 sb.st_mode = got_fileindex_perms_to_st(ie);
1848 } else {
1849 if (stat(ondisk_path, &sb) == -1) {
1850 if (errno != ENOENT) {
1851 err = got_error_from_errno2("stat",
1852 ondisk_path);
1853 goto done;
1855 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1856 status = GOT_STATUS_UNVERSIONED;
1857 } else {
1858 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1859 status = GOT_STATUS_UNVERSIONED;
1860 else
1861 status = GOT_STATUS_OBSTRUCTED;
1865 if (status == GOT_STATUS_OBSTRUCTED) {
1866 if (ie)
1867 got_fileindex_entry_mark_skipped(ie);
1868 err = (*progress_cb)(progress_arg, status, path);
1869 goto done;
1871 if (status == GOT_STATUS_CONFLICT) {
1872 if (ie)
1873 got_fileindex_entry_mark_skipped(ie);
1874 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1875 path);
1876 goto done;
1879 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1880 (S_ISLNK(te->mode) ||
1881 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1883 * This is a regular file or an installed bad symlink.
1884 * If the file index indicates that this file is already
1885 * up-to-date with respect to the repository we can skip
1886 * updating contents of this file.
1888 if (got_fileindex_entry_has_commit(ie) &&
1889 memcmp(ie->commit_hash, worktree->base_commit_id->hash,
1890 SHA1_DIGEST_LENGTH) == 0) {
1891 /* Same commit. */
1892 err = sync_timestamps(worktree->root_fd,
1893 path, status, ie, &sb);
1894 if (err)
1895 goto done;
1896 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1897 path);
1898 goto done;
1900 if (got_fileindex_entry_has_blob(ie) &&
1901 memcmp(ie->blob_hash, te->id.hash,
1902 SHA1_DIGEST_LENGTH) == 0) {
1903 /* Different commit but the same blob. */
1904 err = sync_timestamps(worktree->root_fd,
1905 path, status, ie, &sb);
1906 if (err)
1907 goto done;
1908 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1909 path);
1910 goto done;
1914 fd1 = got_opentempfd();
1915 if (fd1 == -1) {
1916 err = got_error_from_errno("got_opentempfd");
1917 goto done;
1919 err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
1920 if (err)
1921 goto done;
1923 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1924 int update_timestamps;
1925 struct got_blob_object *blob2 = NULL;
1926 char *label_orig = NULL;
1927 if (got_fileindex_entry_has_blob(ie)) {
1928 fd2 = got_opentempfd();
1929 if (fd2 == -1) {
1930 err = got_error_from_errno("got_opentempfd");
1931 goto done;
1933 struct got_object_id id2;
1934 memcpy(id2.hash, ie->blob_hash, SHA1_DIGEST_LENGTH);
1935 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
1936 fd2);
1937 if (err)
1938 goto done;
1940 if (got_fileindex_entry_has_commit(ie)) {
1941 char id_str[SHA1_DIGEST_STRING_LENGTH];
1942 if (got_sha1_digest_to_str(ie->commit_hash, id_str,
1943 sizeof(id_str)) == NULL) {
1944 err = got_error_path(id_str,
1945 GOT_ERR_BAD_OBJ_ID_STR);
1946 goto done;
1948 if (asprintf(&label_orig, "%s: commit %s",
1949 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1950 err = got_error_from_errno("asprintf");
1951 goto done;
1954 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1955 char *link_target;
1956 err = got_object_blob_read_to_str(&link_target, blob);
1957 if (err)
1958 goto done;
1959 err = merge_symlink(worktree, blob2, ondisk_path, path,
1960 label_orig, link_target, worktree->base_commit_id,
1961 repo, progress_cb, progress_arg);
1962 free(link_target);
1963 } else {
1964 err = merge_blob(&update_timestamps, worktree, blob2,
1965 ondisk_path, path, sb.st_mode, label_orig, blob,
1966 worktree->base_commit_id, repo,
1967 progress_cb, progress_arg);
1969 free(label_orig);
1970 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
1971 err = got_error_from_errno("close");
1972 goto done;
1974 if (blob2)
1975 got_object_blob_close(blob2);
1976 if (err)
1977 goto done;
1979 * Do not update timestamps of files with local changes.
1980 * Otherwise, a future status walk would treat them as
1981 * unmodified files again.
1983 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1984 blob->id.hash, worktree->base_commit_id->hash,
1985 update_timestamps);
1986 } else if (status == GOT_STATUS_MODE_CHANGE) {
1987 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1988 blob->id.hash, worktree->base_commit_id->hash, 0);
1989 } else if (status == GOT_STATUS_DELETE) {
1990 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1991 if (err)
1992 goto done;
1993 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1994 blob->id.hash, worktree->base_commit_id->hash, 0);
1995 if (err)
1996 goto done;
1997 } else {
1998 int is_bad_symlink = 0;
1999 if (S_ISLNK(te->mode)) {
2000 err = install_symlink(&is_bad_symlink, worktree,
2001 ondisk_path, path, blob,
2002 status == GOT_STATUS_MISSING, 0,
2003 status == GOT_STATUS_UNVERSIONED, 0,
2004 repo, progress_cb, progress_arg);
2005 } else {
2006 err = install_blob(worktree, ondisk_path, path,
2007 te->mode, sb.st_mode, blob,
2008 status == GOT_STATUS_MISSING, 0, 0,
2009 status == GOT_STATUS_UNVERSIONED, repo,
2010 progress_cb, progress_arg);
2012 if (err)
2013 goto done;
2015 if (ie) {
2016 err = got_fileindex_entry_update(ie,
2017 worktree->root_fd, path, blob->id.hash,
2018 worktree->base_commit_id->hash, 1);
2019 } else {
2020 err = create_fileindex_entry(&ie, fileindex,
2021 worktree->base_commit_id, worktree->root_fd, path,
2022 &blob->id);
2024 if (err)
2025 goto done;
2027 if (is_bad_symlink) {
2028 got_fileindex_entry_filetype_set(ie,
2029 GOT_FILEIDX_MODE_BAD_SYMLINK);
2033 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2034 err = got_error_from_errno("close");
2035 goto done;
2037 got_object_blob_close(blob);
2038 done:
2039 free(ondisk_path);
2040 return err;
2043 static const struct got_error *
2044 remove_ondisk_file(const char *root_path, const char *path)
2046 const struct got_error *err = NULL;
2047 char *ondisk_path = NULL, *parent = NULL;
2049 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2050 return got_error_from_errno("asprintf");
2052 if (unlink(ondisk_path) == -1) {
2053 if (errno != ENOENT)
2054 err = got_error_from_errno2("unlink", ondisk_path);
2055 } else {
2056 size_t root_len = strlen(root_path);
2057 err = got_path_dirname(&parent, ondisk_path);
2058 if (err)
2059 goto done;
2060 while (got_path_cmp(parent, root_path,
2061 strlen(parent), root_len) != 0) {
2062 free(ondisk_path);
2063 ondisk_path = parent;
2064 parent = NULL;
2065 if (rmdir(ondisk_path) == -1) {
2066 if (errno != ENOTEMPTY)
2067 err = got_error_from_errno2("rmdir",
2068 ondisk_path);
2069 break;
2071 err = got_path_dirname(&parent, ondisk_path);
2072 if (err)
2073 break;
2076 done:
2077 free(ondisk_path);
2078 free(parent);
2079 return err;
2082 static const struct got_error *
2083 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2084 struct got_fileindex_entry *ie, struct got_repository *repo,
2085 got_worktree_checkout_cb progress_cb, void *progress_arg)
2087 const struct got_error *err = NULL;
2088 unsigned char status;
2089 struct stat sb;
2090 char *ondisk_path;
2092 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2093 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2095 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2096 == -1)
2097 return got_error_from_errno("asprintf");
2099 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2100 if (err)
2101 goto done;
2103 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2104 char ondisk_target[PATH_MAX];
2105 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2106 sizeof(ondisk_target));
2107 if (ondisk_len == -1) {
2108 err = got_error_from_errno2("readlink", ondisk_path);
2109 goto done;
2111 ondisk_target[ondisk_len] = '\0';
2112 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2113 NULL, NULL, /* XXX pass common ancestor info? */
2114 ondisk_target, ondisk_path);
2115 if (err)
2116 goto done;
2117 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2118 ie->path);
2119 goto done;
2122 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2123 status == GOT_STATUS_ADD) {
2124 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2125 if (err)
2126 goto done;
2128 * Preserve the working file and change the deleted blob's
2129 * entry into a schedule-add entry.
2131 err = got_fileindex_entry_update(ie, worktree->root_fd,
2132 ie->path, NULL, NULL, 0);
2133 } else {
2134 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2135 if (err)
2136 goto done;
2137 if (status == GOT_STATUS_NO_CHANGE) {
2138 err = remove_ondisk_file(worktree->root_path, ie->path);
2139 if (err)
2140 goto done;
2142 got_fileindex_entry_remove(fileindex, ie);
2144 done:
2145 free(ondisk_path);
2146 return err;
2149 struct diff_cb_arg {
2150 struct got_fileindex *fileindex;
2151 struct got_worktree *worktree;
2152 struct got_repository *repo;
2153 got_worktree_checkout_cb progress_cb;
2154 void *progress_arg;
2155 got_cancel_cb cancel_cb;
2156 void *cancel_arg;
2159 static const struct got_error *
2160 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2161 struct got_tree_entry *te, const char *parent_path)
2163 struct diff_cb_arg *a = arg;
2165 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2166 return got_error(GOT_ERR_CANCELLED);
2168 return update_blob(a->worktree, a->fileindex, ie, te,
2169 ie->path, a->repo, a->progress_cb, a->progress_arg);
2172 static const struct got_error *
2173 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2175 struct diff_cb_arg *a = arg;
2177 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2178 return got_error(GOT_ERR_CANCELLED);
2180 return delete_blob(a->worktree, a->fileindex, ie,
2181 a->repo, a->progress_cb, a->progress_arg);
2184 static const struct got_error *
2185 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2187 struct diff_cb_arg *a = arg;
2188 const struct got_error *err;
2189 char *path;
2191 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2192 return got_error(GOT_ERR_CANCELLED);
2194 if (got_object_tree_entry_is_submodule(te))
2195 return NULL;
2197 if (asprintf(&path, "%s%s%s", parent_path,
2198 parent_path[0] ? "/" : "", te->name)
2199 == -1)
2200 return got_error_from_errno("asprintf");
2202 if (S_ISDIR(te->mode))
2203 err = add_dir_on_disk(a->worktree, path);
2204 else
2205 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2206 a->repo, a->progress_cb, a->progress_arg);
2208 free(path);
2209 return err;
2212 const struct got_error *
2213 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2215 uint32_t uuid_status;
2217 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2218 if (uuid_status != uuid_s_ok) {
2219 *uuidstr = NULL;
2220 return got_error_uuid(uuid_status, "uuid_to_string");
2223 return NULL;
2226 static const struct got_error *
2227 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2229 const struct got_error *err = NULL;
2230 char *uuidstr = NULL;
2232 *refname = NULL;
2234 err = got_worktree_get_uuid(&uuidstr, worktree);
2235 if (err)
2236 return err;
2238 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2239 err = got_error_from_errno("asprintf");
2240 *refname = NULL;
2242 free(uuidstr);
2243 return err;
2246 const struct got_error *
2247 got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2248 const char *prefix)
2250 return get_ref_name(refname, worktree, prefix);
2253 const struct got_error *
2254 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2256 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2259 static const struct got_error *
2260 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2262 return get_ref_name(refname, worktree,
2263 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2266 static const struct got_error *
2267 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2269 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2272 static const struct got_error *
2273 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2275 return get_ref_name(refname, worktree,
2276 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2279 static const struct got_error *
2280 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2282 return get_ref_name(refname, worktree,
2283 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2286 static const struct got_error *
2287 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2289 return get_ref_name(refname, worktree,
2290 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2293 static const struct got_error *
2294 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2296 return get_ref_name(refname, worktree,
2297 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2300 static const struct got_error *
2301 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2303 return get_ref_name(refname, worktree,
2304 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2307 static const struct got_error *
2308 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2310 return get_ref_name(refname, worktree,
2311 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2314 const struct got_error *
2315 got_worktree_get_histedit_script_path(char **path,
2316 struct got_worktree *worktree)
2318 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2319 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2320 *path = NULL;
2321 return got_error_from_errno("asprintf");
2323 return NULL;
2326 static const struct got_error *
2327 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2329 return get_ref_name(refname, worktree,
2330 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2333 static const struct got_error *
2334 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2336 return get_ref_name(refname, worktree,
2337 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2341 * Prevent Git's garbage collector from deleting our base commit by
2342 * setting a reference to our base commit's ID.
2344 static const struct got_error *
2345 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2347 const struct got_error *err = NULL;
2348 struct got_reference *ref = NULL;
2349 char *refname;
2351 err = got_worktree_get_base_ref_name(&refname, worktree);
2352 if (err)
2353 return err;
2355 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2356 if (err)
2357 goto done;
2359 err = got_ref_write(ref, repo);
2360 done:
2361 free(refname);
2362 if (ref)
2363 got_ref_close(ref);
2364 return err;
2367 static const struct got_error *
2368 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2370 const struct got_error *err = NULL;
2372 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2373 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2374 err = got_error_from_errno("asprintf");
2375 *fileindex_path = NULL;
2377 return err;
2381 static const struct got_error *
2382 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2383 struct got_worktree *worktree)
2385 const struct got_error *err = NULL;
2386 FILE *index = NULL;
2388 *fileindex_path = NULL;
2389 *fileindex = got_fileindex_alloc();
2390 if (*fileindex == NULL)
2391 return got_error_from_errno("got_fileindex_alloc");
2393 err = get_fileindex_path(fileindex_path, worktree);
2394 if (err)
2395 goto done;
2397 index = fopen(*fileindex_path, "rbe");
2398 if (index == NULL) {
2399 if (errno != ENOENT)
2400 err = got_error_from_errno2("fopen", *fileindex_path);
2401 } else {
2402 err = got_fileindex_read(*fileindex, index);
2403 if (fclose(index) == EOF && err == NULL)
2404 err = got_error_from_errno("fclose");
2406 done:
2407 if (err) {
2408 free(*fileindex_path);
2409 *fileindex_path = NULL;
2410 got_fileindex_free(*fileindex);
2411 *fileindex = NULL;
2413 return err;
2416 struct bump_base_commit_id_arg {
2417 struct got_object_id *base_commit_id;
2418 const char *path;
2419 size_t path_len;
2420 const char *entry_name;
2421 got_worktree_checkout_cb progress_cb;
2422 void *progress_arg;
2425 /* Bump base commit ID of all files within an updated part of the work tree. */
2426 static const struct got_error *
2427 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2429 const struct got_error *err;
2430 struct bump_base_commit_id_arg *a = arg;
2432 if (a->entry_name) {
2433 if (strcmp(ie->path, a->path) != 0)
2434 return NULL;
2435 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2436 return NULL;
2438 if (got_fileindex_entry_was_skipped(ie))
2439 return NULL;
2441 if (memcmp(ie->commit_hash, a->base_commit_id->hash,
2442 SHA1_DIGEST_LENGTH) == 0)
2443 return NULL;
2445 if (a->progress_cb) {
2446 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2447 ie->path);
2448 if (err)
2449 return err;
2451 memcpy(ie->commit_hash, a->base_commit_id->hash, SHA1_DIGEST_LENGTH);
2452 return NULL;
2455 static const struct got_error *
2456 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2457 struct got_fileindex *fileindex,
2458 got_worktree_checkout_cb progress_cb, void *progress_arg)
2460 struct bump_base_commit_id_arg bbc_arg;
2462 bbc_arg.base_commit_id = worktree->base_commit_id;
2463 bbc_arg.entry_name = NULL;
2464 bbc_arg.path = "";
2465 bbc_arg.path_len = 0;
2466 bbc_arg.progress_cb = progress_cb;
2467 bbc_arg.progress_arg = progress_arg;
2469 return got_fileindex_for_each_entry_safe(fileindex,
2470 bump_base_commit_id, &bbc_arg);
2473 static const struct got_error *
2474 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2476 const struct got_error *err = NULL;
2477 char *new_fileindex_path = NULL;
2478 FILE *new_index = NULL;
2479 struct timespec timeout;
2481 err = got_opentemp_named(&new_fileindex_path, &new_index,
2482 fileindex_path, "");
2483 if (err)
2484 goto done;
2486 err = got_fileindex_write(fileindex, new_index);
2487 if (err)
2488 goto done;
2490 if (rename(new_fileindex_path, fileindex_path) != 0) {
2491 err = got_error_from_errno3("rename", new_fileindex_path,
2492 fileindex_path);
2493 unlink(new_fileindex_path);
2497 * Sleep for a short amount of time to ensure that files modified after
2498 * this program exits have a different time stamp from the one which
2499 * was recorded in the file index.
2501 timeout.tv_sec = 0;
2502 timeout.tv_nsec = 1;
2503 nanosleep(&timeout, NULL);
2504 done:
2505 if (new_index)
2506 fclose(new_index);
2507 free(new_fileindex_path);
2508 return err;
2511 static const struct got_error *
2512 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2513 struct got_object_id **tree_id, const char *wt_relpath,
2514 struct got_commit_object *base_commit, struct got_worktree *worktree,
2515 struct got_repository *repo)
2517 const struct got_error *err = NULL;
2518 struct got_object_id *id = NULL;
2519 char *in_repo_path = NULL;
2520 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2522 *entry_type = GOT_OBJ_TYPE_ANY;
2523 *tree_relpath = NULL;
2524 *tree_id = NULL;
2526 if (wt_relpath[0] == '\0') {
2527 /* Check out all files within the work tree. */
2528 *entry_type = GOT_OBJ_TYPE_TREE;
2529 *tree_relpath = strdup("");
2530 if (*tree_relpath == NULL) {
2531 err = got_error_from_errno("strdup");
2532 goto done;
2534 err = got_object_id_by_path(tree_id, repo, base_commit,
2535 worktree->path_prefix);
2536 if (err)
2537 goto done;
2538 return NULL;
2541 /* Check out a subset of files in the work tree. */
2543 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2544 is_root_wt ? "" : "/", wt_relpath) == -1) {
2545 err = got_error_from_errno("asprintf");
2546 goto done;
2549 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2550 if (err)
2551 goto done;
2553 free(in_repo_path);
2554 in_repo_path = NULL;
2556 err = got_object_get_type(entry_type, repo, id);
2557 if (err)
2558 goto done;
2560 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2561 /* Check out a single file. */
2562 if (strchr(wt_relpath, '/') == NULL) {
2563 /* Check out a single file in work tree's root dir. */
2564 in_repo_path = strdup(worktree->path_prefix);
2565 if (in_repo_path == NULL) {
2566 err = got_error_from_errno("strdup");
2567 goto done;
2569 *tree_relpath = strdup("");
2570 if (*tree_relpath == NULL) {
2571 err = got_error_from_errno("strdup");
2572 goto done;
2574 } else {
2575 /* Check out a single file in a subdirectory. */
2576 err = got_path_dirname(tree_relpath, wt_relpath);
2577 if (err)
2578 return err;
2579 if (asprintf(&in_repo_path, "%s%s%s",
2580 worktree->path_prefix, is_root_wt ? "" : "/",
2581 *tree_relpath) == -1) {
2582 err = got_error_from_errno("asprintf");
2583 goto done;
2586 err = got_object_id_by_path(tree_id, repo,
2587 base_commit, in_repo_path);
2588 } else {
2589 /* Check out all files within a subdirectory. */
2590 *tree_id = got_object_id_dup(id);
2591 if (*tree_id == NULL) {
2592 err = got_error_from_errno("got_object_id_dup");
2593 goto done;
2595 *tree_relpath = strdup(wt_relpath);
2596 if (*tree_relpath == NULL) {
2597 err = got_error_from_errno("strdup");
2598 goto done;
2601 done:
2602 free(id);
2603 free(in_repo_path);
2604 if (err) {
2605 *entry_type = GOT_OBJ_TYPE_ANY;
2606 free(*tree_relpath);
2607 *tree_relpath = NULL;
2608 free(*tree_id);
2609 *tree_id = NULL;
2611 return err;
2614 static const struct got_error *
2615 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2616 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2617 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2618 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2620 const struct got_error *err = NULL;
2621 struct got_commit_object *commit = NULL;
2622 struct got_tree_object *tree = NULL;
2623 struct got_fileindex_diff_tree_cb diff_cb;
2624 struct diff_cb_arg arg;
2626 err = ref_base_commit(worktree, repo);
2627 if (err) {
2628 if (!(err->code == GOT_ERR_ERRNO &&
2629 (errno == EACCES || errno == EROFS)))
2630 goto done;
2631 err = (*progress_cb)(progress_arg,
2632 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2633 if (err)
2634 return err;
2637 err = got_object_open_as_commit(&commit, repo,
2638 worktree->base_commit_id);
2639 if (err)
2640 goto done;
2642 err = got_object_open_as_tree(&tree, repo, tree_id);
2643 if (err)
2644 goto done;
2646 if (entry_name &&
2647 got_object_tree_find_entry(tree, entry_name) == NULL) {
2648 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2649 goto done;
2652 diff_cb.diff_old_new = diff_old_new;
2653 diff_cb.diff_old = diff_old;
2654 diff_cb.diff_new = diff_new;
2655 arg.fileindex = fileindex;
2656 arg.worktree = worktree;
2657 arg.repo = repo;
2658 arg.progress_cb = progress_cb;
2659 arg.progress_arg = progress_arg;
2660 arg.cancel_cb = cancel_cb;
2661 arg.cancel_arg = cancel_arg;
2662 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2663 entry_name, repo, &diff_cb, &arg);
2664 done:
2665 if (tree)
2666 got_object_tree_close(tree);
2667 if (commit)
2668 got_object_commit_close(commit);
2669 return err;
2672 const struct got_error *
2673 got_worktree_checkout_files(struct got_worktree *worktree,
2674 struct got_pathlist_head *paths, struct got_repository *repo,
2675 got_worktree_checkout_cb progress_cb, void *progress_arg,
2676 got_cancel_cb cancel_cb, void *cancel_arg)
2678 const struct got_error *err = NULL, *sync_err, *unlockerr;
2679 struct got_commit_object *commit = NULL;
2680 struct got_tree_object *tree = NULL;
2681 struct got_fileindex *fileindex = NULL;
2682 char *fileindex_path = NULL;
2683 struct got_pathlist_entry *pe;
2684 struct tree_path_data {
2685 STAILQ_ENTRY(tree_path_data) entry;
2686 struct got_object_id *tree_id;
2687 int entry_type;
2688 char *relpath;
2689 char *entry_name;
2690 } *tpd = NULL;
2691 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2693 STAILQ_INIT(&tree_paths);
2695 err = lock_worktree(worktree, LOCK_EX);
2696 if (err)
2697 return err;
2699 err = got_object_open_as_commit(&commit, repo,
2700 worktree->base_commit_id);
2701 if (err)
2702 goto done;
2704 /* Map all specified paths to in-repository trees. */
2705 TAILQ_FOREACH(pe, paths, entry) {
2706 tpd = malloc(sizeof(*tpd));
2707 if (tpd == NULL) {
2708 err = got_error_from_errno("malloc");
2709 goto done;
2712 err = find_tree_entry_for_checkout(&tpd->entry_type,
2713 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2714 worktree, repo);
2715 if (err) {
2716 free(tpd);
2717 goto done;
2720 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2721 err = got_path_basename(&tpd->entry_name, pe->path);
2722 if (err) {
2723 free(tpd->relpath);
2724 free(tpd->tree_id);
2725 free(tpd);
2726 goto done;
2728 } else
2729 tpd->entry_name = NULL;
2731 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2735 * Read the file index.
2736 * Checking out files is supposed to be an idempotent operation.
2737 * If the on-disk file index is incomplete we will try to complete it.
2739 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2740 if (err)
2741 goto done;
2743 tpd = STAILQ_FIRST(&tree_paths);
2744 TAILQ_FOREACH(pe, paths, entry) {
2745 struct bump_base_commit_id_arg bbc_arg;
2747 err = checkout_files(worktree, fileindex, tpd->relpath,
2748 tpd->tree_id, tpd->entry_name, repo,
2749 progress_cb, progress_arg, cancel_cb, cancel_arg);
2750 if (err)
2751 break;
2753 bbc_arg.base_commit_id = worktree->base_commit_id;
2754 bbc_arg.entry_name = tpd->entry_name;
2755 bbc_arg.path = pe->path;
2756 bbc_arg.path_len = pe->path_len;
2757 bbc_arg.progress_cb = progress_cb;
2758 bbc_arg.progress_arg = progress_arg;
2759 err = got_fileindex_for_each_entry_safe(fileindex,
2760 bump_base_commit_id, &bbc_arg);
2761 if (err)
2762 break;
2764 tpd = STAILQ_NEXT(tpd, entry);
2766 sync_err = sync_fileindex(fileindex, fileindex_path);
2767 if (sync_err && err == NULL)
2768 err = sync_err;
2769 done:
2770 free(fileindex_path);
2771 if (tree)
2772 got_object_tree_close(tree);
2773 if (commit)
2774 got_object_commit_close(commit);
2775 if (fileindex)
2776 got_fileindex_free(fileindex);
2777 while (!STAILQ_EMPTY(&tree_paths)) {
2778 tpd = STAILQ_FIRST(&tree_paths);
2779 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2780 free(tpd->relpath);
2781 free(tpd->tree_id);
2782 free(tpd);
2784 unlockerr = lock_worktree(worktree, LOCK_SH);
2785 if (unlockerr && err == NULL)
2786 err = unlockerr;
2787 return err;
2790 struct merge_file_cb_arg {
2791 struct got_worktree *worktree;
2792 struct got_fileindex *fileindex;
2793 got_worktree_checkout_cb progress_cb;
2794 void *progress_arg;
2795 got_cancel_cb cancel_cb;
2796 void *cancel_arg;
2797 const char *label_orig;
2798 struct got_object_id *commit_id2;
2799 int allow_bad_symlinks;
2802 static const struct got_error *
2803 merge_file_cb(void *arg, struct got_blob_object *blob1,
2804 struct got_blob_object *blob2, FILE *f1, FILE *f2,
2805 struct got_object_id *id1, struct got_object_id *id2,
2806 const char *path1, const char *path2,
2807 mode_t mode1, mode_t mode2, struct got_repository *repo)
2809 static const struct got_error *err = NULL;
2810 struct merge_file_cb_arg *a = arg;
2811 struct got_fileindex_entry *ie;
2812 char *ondisk_path = NULL;
2813 struct stat sb;
2814 unsigned char status;
2815 int local_changes_subsumed;
2816 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2817 char *id_str = NULL, *label_deriv2 = NULL;
2819 if (blob1 && blob2) {
2820 ie = got_fileindex_entry_get(a->fileindex, path2,
2821 strlen(path2));
2822 if (ie == NULL)
2823 return (*a->progress_cb)(a->progress_arg,
2824 GOT_STATUS_MISSING, path2);
2826 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2827 path2) == -1)
2828 return got_error_from_errno("asprintf");
2830 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2831 repo);
2832 if (err)
2833 goto done;
2835 if (status == GOT_STATUS_DELETE) {
2836 err = (*a->progress_cb)(a->progress_arg,
2837 GOT_STATUS_MERGE, path2);
2838 goto done;
2840 if (status != GOT_STATUS_NO_CHANGE &&
2841 status != GOT_STATUS_MODIFY &&
2842 status != GOT_STATUS_CONFLICT &&
2843 status != GOT_STATUS_ADD) {
2844 err = (*a->progress_cb)(a->progress_arg, status, path2);
2845 goto done;
2848 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2849 char *link_target2;
2850 err = got_object_blob_read_to_str(&link_target2, blob2);
2851 if (err)
2852 goto done;
2853 err = merge_symlink(a->worktree, blob1, ondisk_path,
2854 path2, a->label_orig, link_target2, a->commit_id2,
2855 repo, a->progress_cb, a->progress_arg);
2856 free(link_target2);
2857 } else {
2858 int fd;
2860 f_orig = got_opentemp();
2861 if (f_orig == NULL) {
2862 err = got_error_from_errno("got_opentemp");
2863 goto done;
2865 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2866 f_orig, blob1);
2867 if (err)
2868 goto done;
2870 f_deriv2 = got_opentemp();
2871 if (f_deriv2 == NULL)
2872 goto done;
2873 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2874 f_deriv2, blob2);
2875 if (err)
2876 goto done;
2878 fd = open(ondisk_path,
2879 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2880 if (fd == -1) {
2881 err = got_error_from_errno2("open",
2882 ondisk_path);
2883 goto done;
2885 f_deriv = fdopen(fd, "r");
2886 if (f_deriv == NULL) {
2887 err = got_error_from_errno2("fdopen",
2888 ondisk_path);
2889 close(fd);
2890 goto done;
2892 err = got_object_id_str(&id_str, a->commit_id2);
2893 if (err)
2894 goto done;
2895 if (asprintf(&label_deriv2, "%s: commit %s",
2896 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2897 err = got_error_from_errno("asprintf");
2898 goto done;
2900 err = merge_file(&local_changes_subsumed, a->worktree,
2901 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2902 mode2, a->label_orig, NULL, label_deriv2,
2903 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2904 a->progress_cb, a->progress_arg);
2906 } else if (blob1) {
2907 ie = got_fileindex_entry_get(a->fileindex, path1,
2908 strlen(path1));
2909 if (ie == NULL)
2910 return (*a->progress_cb)(a->progress_arg,
2911 GOT_STATUS_MISSING, path1);
2913 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2914 path1) == -1)
2915 return got_error_from_errno("asprintf");
2917 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2918 repo);
2919 if (err)
2920 goto done;
2922 switch (status) {
2923 case GOT_STATUS_NO_CHANGE:
2924 err = (*a->progress_cb)(a->progress_arg,
2925 GOT_STATUS_DELETE, path1);
2926 if (err)
2927 goto done;
2928 err = remove_ondisk_file(a->worktree->root_path, path1);
2929 if (err)
2930 goto done;
2931 if (ie)
2932 got_fileindex_entry_mark_deleted_from_disk(ie);
2933 break;
2934 case GOT_STATUS_DELETE:
2935 case GOT_STATUS_MISSING:
2936 err = (*a->progress_cb)(a->progress_arg,
2937 GOT_STATUS_DELETE, path1);
2938 if (err)
2939 goto done;
2940 if (ie)
2941 got_fileindex_entry_mark_deleted_from_disk(ie);
2942 break;
2943 case GOT_STATUS_ADD: {
2944 struct got_object_id *id;
2945 FILE *blob1_f;
2946 off_t blob1_size;
2948 * Delete the added file only if its content already
2949 * exists in the repository.
2951 err = got_object_blob_file_create(&id, &blob1_f,
2952 &blob1_size, path1);
2953 if (err)
2954 goto done;
2955 if (got_object_id_cmp(id, id1) == 0) {
2956 err = (*a->progress_cb)(a->progress_arg,
2957 GOT_STATUS_DELETE, path1);
2958 if (err)
2959 goto done;
2960 err = remove_ondisk_file(a->worktree->root_path,
2961 path1);
2962 if (err)
2963 goto done;
2964 if (ie)
2965 got_fileindex_entry_remove(a->fileindex,
2966 ie);
2967 } else {
2968 err = (*a->progress_cb)(a->progress_arg,
2969 GOT_STATUS_CANNOT_DELETE, path1);
2971 if (fclose(blob1_f) == EOF && err == NULL)
2972 err = got_error_from_errno("fclose");
2973 free(id);
2974 if (err)
2975 goto done;
2976 break;
2978 case GOT_STATUS_MODIFY:
2979 case GOT_STATUS_CONFLICT:
2980 err = (*a->progress_cb)(a->progress_arg,
2981 GOT_STATUS_CANNOT_DELETE, path1);
2982 if (err)
2983 goto done;
2984 break;
2985 case GOT_STATUS_OBSTRUCTED:
2986 err = (*a->progress_cb)(a->progress_arg, status, path1);
2987 if (err)
2988 goto done;
2989 break;
2990 default:
2991 break;
2993 } else if (blob2) {
2994 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2995 path2) == -1)
2996 return got_error_from_errno("asprintf");
2997 ie = got_fileindex_entry_get(a->fileindex, path2,
2998 strlen(path2));
2999 if (ie) {
3000 err = get_file_status(&status, &sb, ie, ondisk_path,
3001 -1, NULL, repo);
3002 if (err)
3003 goto done;
3004 if (status != GOT_STATUS_NO_CHANGE &&
3005 status != GOT_STATUS_MODIFY &&
3006 status != GOT_STATUS_CONFLICT &&
3007 status != GOT_STATUS_ADD) {
3008 err = (*a->progress_cb)(a->progress_arg,
3009 status, path2);
3010 goto done;
3012 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3013 char *link_target2;
3014 err = got_object_blob_read_to_str(&link_target2,
3015 blob2);
3016 if (err)
3017 goto done;
3018 err = merge_symlink(a->worktree, NULL,
3019 ondisk_path, path2, a->label_orig,
3020 link_target2, a->commit_id2, repo,
3021 a->progress_cb, a->progress_arg);
3022 free(link_target2);
3023 } else if (S_ISREG(sb.st_mode)) {
3024 err = merge_blob(&local_changes_subsumed,
3025 a->worktree, NULL, ondisk_path, path2,
3026 sb.st_mode, a->label_orig, blob2,
3027 a->commit_id2, repo, a->progress_cb,
3028 a->progress_arg);
3029 } else {
3030 err = got_error_path(ondisk_path,
3031 GOT_ERR_FILE_OBSTRUCTED);
3033 if (err)
3034 goto done;
3035 if (status == GOT_STATUS_DELETE) {
3036 err = got_fileindex_entry_update(ie,
3037 a->worktree->root_fd, path2, blob2->id.hash,
3038 a->worktree->base_commit_id->hash, 0);
3039 if (err)
3040 goto done;
3042 } else {
3043 int is_bad_symlink = 0;
3044 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3045 if (S_ISLNK(mode2)) {
3046 err = install_symlink(&is_bad_symlink,
3047 a->worktree, ondisk_path, path2, blob2, 0,
3048 0, 1, a->allow_bad_symlinks, repo,
3049 a->progress_cb, a->progress_arg);
3050 } else {
3051 err = install_blob(a->worktree, ondisk_path, path2,
3052 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3053 a->progress_cb, a->progress_arg);
3055 if (err)
3056 goto done;
3057 err = got_fileindex_entry_alloc(&ie, path2);
3058 if (err)
3059 goto done;
3060 err = got_fileindex_entry_update(ie,
3061 a->worktree->root_fd, path2, NULL, NULL, 1);
3062 if (err) {
3063 got_fileindex_entry_free(ie);
3064 goto done;
3066 err = got_fileindex_entry_add(a->fileindex, ie);
3067 if (err) {
3068 got_fileindex_entry_free(ie);
3069 goto done;
3071 if (is_bad_symlink) {
3072 got_fileindex_entry_filetype_set(ie,
3073 GOT_FILEIDX_MODE_BAD_SYMLINK);
3077 done:
3078 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3079 err = got_error_from_errno("fclose");
3080 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3081 err = got_error_from_errno("fclose");
3082 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3083 err = got_error_from_errno("fclose");
3084 free(id_str);
3085 free(label_deriv2);
3086 free(ondisk_path);
3087 return err;
3090 static const struct got_error *
3091 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3093 struct got_worktree *worktree = arg;
3095 /* Reject merges into a work tree with mixed base commits. */
3096 if (got_fileindex_entry_has_commit(ie) &&
3097 memcmp(ie->commit_hash, worktree->base_commit_id->hash,
3098 SHA1_DIGEST_LENGTH) != 0)
3099 return got_error(GOT_ERR_MIXED_COMMITS);
3101 return NULL;
3104 struct check_merge_conflicts_arg {
3105 struct got_worktree *worktree;
3106 struct got_fileindex *fileindex;
3107 struct got_repository *repo;
3110 static const struct got_error *
3111 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3112 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3113 struct got_object_id *id1, struct got_object_id *id2,
3114 const char *path1, const char *path2,
3115 mode_t mode1, mode_t mode2, struct got_repository *repo)
3117 const struct got_error *err = NULL;
3118 struct check_merge_conflicts_arg *a = arg;
3119 unsigned char status;
3120 struct stat sb;
3121 struct got_fileindex_entry *ie;
3122 const char *path = path2 ? path2 : path1;
3123 struct got_object_id *id = id2 ? id2 : id1;
3124 char *ondisk_path;
3126 if (id == NULL)
3127 return NULL;
3129 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3130 if (ie == NULL)
3131 return NULL;
3133 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3134 == -1)
3135 return got_error_from_errno("asprintf");
3137 /* Reject merges into a work tree with conflicted files. */
3138 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3139 free(ondisk_path);
3140 if (err)
3141 return err;
3142 if (status == GOT_STATUS_CONFLICT)
3143 return got_error(GOT_ERR_CONFLICTS);
3145 return NULL;
3148 static const struct got_error *
3149 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3150 const char *fileindex_path, struct got_object_id *commit_id1,
3151 struct got_object_id *commit_id2, struct got_repository *repo,
3152 got_worktree_checkout_cb progress_cb, void *progress_arg,
3153 got_cancel_cb cancel_cb, void *cancel_arg)
3155 const struct got_error *err = NULL, *sync_err;
3156 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3157 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3158 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3159 struct check_merge_conflicts_arg cmc_arg;
3160 struct merge_file_cb_arg arg;
3161 char *label_orig = NULL;
3162 FILE *f1 = NULL, *f2 = NULL;
3163 int fd1 = -1, fd2 = -1;
3165 if (commit_id1) {
3166 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3167 if (err)
3168 goto done;
3169 err = got_object_id_by_path(&tree_id1, repo, commit1,
3170 worktree->path_prefix);
3171 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3172 goto done;
3174 if (tree_id1) {
3175 char *id_str;
3177 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3178 if (err)
3179 goto done;
3181 err = got_object_id_str(&id_str, commit_id1);
3182 if (err)
3183 goto done;
3185 if (asprintf(&label_orig, "%s: commit %s",
3186 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3187 err = got_error_from_errno("asprintf");
3188 free(id_str);
3189 goto done;
3191 free(id_str);
3193 f1 = got_opentemp();
3194 if (f1 == NULL) {
3195 err = got_error_from_errno("got_opentemp");
3196 goto done;
3200 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3201 if (err)
3202 goto done;
3204 err = got_object_id_by_path(&tree_id2, repo, commit2,
3205 worktree->path_prefix);
3206 if (err)
3207 goto done;
3209 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3210 if (err)
3211 goto done;
3213 f2 = got_opentemp();
3214 if (f2 == NULL) {
3215 err = got_error_from_errno("got_opentemp");
3216 goto done;
3219 fd1 = got_opentempfd();
3220 if (fd1 == -1) {
3221 err = got_error_from_errno("got_opentempfd");
3222 goto done;
3225 fd2 = got_opentempfd();
3226 if (fd2 == -1) {
3227 err = got_error_from_errno("got_opentempfd");
3228 goto done;
3231 cmc_arg.worktree = worktree;
3232 cmc_arg.fileindex = fileindex;
3233 cmc_arg.repo = repo;
3234 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3235 check_merge_conflicts, &cmc_arg, 0);
3236 if (err)
3237 goto done;
3239 arg.worktree = worktree;
3240 arg.fileindex = fileindex;
3241 arg.progress_cb = progress_cb;
3242 arg.progress_arg = progress_arg;
3243 arg.cancel_cb = cancel_cb;
3244 arg.cancel_arg = cancel_arg;
3245 arg.label_orig = label_orig;
3246 arg.commit_id2 = commit_id2;
3247 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3248 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3249 merge_file_cb, &arg, 1);
3250 sync_err = sync_fileindex(fileindex, fileindex_path);
3251 if (sync_err && err == NULL)
3252 err = sync_err;
3253 done:
3254 if (commit1)
3255 got_object_commit_close(commit1);
3256 if (commit2)
3257 got_object_commit_close(commit2);
3258 if (tree1)
3259 got_object_tree_close(tree1);
3260 if (tree2)
3261 got_object_tree_close(tree2);
3262 if (f1 && fclose(f1) == EOF && err == NULL)
3263 err = got_error_from_errno("fclose");
3264 if (f2 && fclose(f2) == EOF && err == NULL)
3265 err = got_error_from_errno("fclose");
3266 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3267 err = got_error_from_errno("close");
3268 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3269 err = got_error_from_errno("close");
3270 free(label_orig);
3271 return err;
3274 const struct got_error *
3275 got_worktree_merge_files(struct got_worktree *worktree,
3276 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3277 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3278 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3280 const struct got_error *err, *unlockerr;
3281 char *fileindex_path = NULL;
3282 struct got_fileindex *fileindex = NULL;
3284 err = lock_worktree(worktree, LOCK_EX);
3285 if (err)
3286 return err;
3288 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3289 if (err)
3290 goto done;
3292 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3293 worktree);
3294 if (err)
3295 goto done;
3297 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3298 commit_id2, repo, progress_cb, progress_arg,
3299 cancel_cb, cancel_arg);
3300 done:
3301 if (fileindex)
3302 got_fileindex_free(fileindex);
3303 free(fileindex_path);
3304 unlockerr = lock_worktree(worktree, LOCK_SH);
3305 if (unlockerr && err == NULL)
3306 err = unlockerr;
3307 return err;
3310 struct diff_dir_cb_arg {
3311 struct got_fileindex *fileindex;
3312 struct got_worktree *worktree;
3313 const char *status_path;
3314 size_t status_path_len;
3315 struct got_repository *repo;
3316 got_worktree_status_cb status_cb;
3317 void *status_arg;
3318 got_cancel_cb cancel_cb;
3319 void *cancel_arg;
3320 /* A pathlist containing per-directory pathlists of ignore patterns. */
3321 struct got_pathlist_head *ignores;
3322 int report_unchanged;
3323 int no_ignores;
3326 static const struct got_error *
3327 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3328 int dirfd, const char *de_name,
3329 got_worktree_status_cb status_cb, void *status_arg,
3330 struct got_repository *repo, int report_unchanged)
3332 const struct got_error *err = NULL;
3333 unsigned char status = GOT_STATUS_NO_CHANGE;
3334 unsigned char staged_status = get_staged_status(ie);
3335 struct stat sb;
3336 struct got_object_id blob_id, commit_id, staged_blob_id;
3337 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3338 struct got_object_id *staged_blob_idp = NULL;
3340 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3341 if (err)
3342 return err;
3344 if (status == GOT_STATUS_NO_CHANGE &&
3345 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3346 return NULL;
3348 if (got_fileindex_entry_has_blob(ie)) {
3349 memcpy(blob_id.hash, ie->blob_hash, SHA1_DIGEST_LENGTH);
3350 blob_idp = &blob_id;
3352 if (got_fileindex_entry_has_commit(ie)) {
3353 memcpy(commit_id.hash, ie->commit_hash, SHA1_DIGEST_LENGTH);
3354 commit_idp = &commit_id;
3356 if (staged_status == GOT_STATUS_ADD ||
3357 staged_status == GOT_STATUS_MODIFY) {
3358 memcpy(staged_blob_id.hash, ie->staged_blob_hash,
3359 SHA1_DIGEST_LENGTH);
3360 staged_blob_idp = &staged_blob_id;
3363 return (*status_cb)(status_arg, status, staged_status,
3364 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3367 static const struct got_error *
3368 status_old_new(void *arg, struct got_fileindex_entry *ie,
3369 struct dirent *de, const char *parent_path, int dirfd)
3371 const struct got_error *err = NULL;
3372 struct diff_dir_cb_arg *a = arg;
3373 char *abspath;
3375 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3376 return got_error(GOT_ERR_CANCELLED);
3378 if (got_path_cmp(parent_path, a->status_path,
3379 strlen(parent_path), a->status_path_len) != 0 &&
3380 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3381 return NULL;
3383 if (parent_path[0]) {
3384 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3385 parent_path, de->d_name) == -1)
3386 return got_error_from_errno("asprintf");
3387 } else {
3388 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3389 de->d_name) == -1)
3390 return got_error_from_errno("asprintf");
3393 err = report_file_status(ie, abspath, dirfd, de->d_name,
3394 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3395 free(abspath);
3396 return err;
3399 static const struct got_error *
3400 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3402 struct diff_dir_cb_arg *a = arg;
3403 struct got_object_id blob_id, commit_id;
3404 unsigned char status;
3406 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3407 return got_error(GOT_ERR_CANCELLED);
3409 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3410 return NULL;
3412 memcpy(blob_id.hash, ie->blob_hash, SHA1_DIGEST_LENGTH);
3413 memcpy(commit_id.hash, ie->commit_hash, SHA1_DIGEST_LENGTH);
3414 if (got_fileindex_entry_has_file_on_disk(ie))
3415 status = GOT_STATUS_MISSING;
3416 else
3417 status = GOT_STATUS_DELETE;
3418 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3419 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3422 static void
3423 free_ignores(struct got_pathlist_head *ignores)
3425 struct got_pathlist_entry *pe;
3427 TAILQ_FOREACH(pe, ignores, entry) {
3428 struct got_pathlist_head *ignorelist = pe->data;
3430 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3432 got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3435 static const struct got_error *
3436 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3438 const struct got_error *err = NULL;
3439 struct got_pathlist_entry *pe = NULL;
3440 struct got_pathlist_head *ignorelist;
3441 char *line = NULL, *pattern, *dirpath = NULL;
3442 size_t linesize = 0;
3443 ssize_t linelen;
3445 ignorelist = calloc(1, sizeof(*ignorelist));
3446 if (ignorelist == NULL)
3447 return got_error_from_errno("calloc");
3448 TAILQ_INIT(ignorelist);
3450 while ((linelen = getline(&line, &linesize, f)) != -1) {
3451 if (linelen > 0 && line[linelen - 1] == '\n')
3452 line[linelen - 1] = '\0';
3454 /* Git's ignores may contain comments. */
3455 if (line[0] == '#')
3456 continue;
3458 /* Git's negated patterns are not (yet?) supported. */
3459 if (line[0] == '!')
3460 continue;
3462 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3463 line) == -1) {
3464 err = got_error_from_errno("asprintf");
3465 goto done;
3467 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3468 if (err)
3469 goto done;
3471 if (ferror(f)) {
3472 err = got_error_from_errno("getline");
3473 goto done;
3476 dirpath = strdup(path);
3477 if (dirpath == NULL) {
3478 err = got_error_from_errno("strdup");
3479 goto done;
3481 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3482 done:
3483 free(line);
3484 if (err || pe == NULL) {
3485 free(dirpath);
3486 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3488 return err;
3491 static int
3492 match_ignores(struct got_pathlist_head *ignores, const char *path)
3494 struct got_pathlist_entry *pe;
3496 /* Handle patterns which match in all directories. */
3497 TAILQ_FOREACH(pe, ignores, entry) {
3498 struct got_pathlist_head *ignorelist = pe->data;
3499 struct got_pathlist_entry *pi;
3501 TAILQ_FOREACH(pi, ignorelist, entry) {
3502 const char *p, *pattern = pi->path;
3504 if (strncmp(pattern, "**/", 3) != 0)
3505 continue;
3506 pattern += 3;
3507 p = path;
3508 while (*p) {
3509 if (fnmatch(pattern, p,
3510 FNM_PATHNAME | FNM_LEADING_DIR)) {
3511 /* Retry in next directory. */
3512 while (*p && *p != '/')
3513 p++;
3514 while (*p == '/')
3515 p++;
3516 continue;
3518 return 1;
3524 * The ignores pathlist contains ignore lists from children before
3525 * parents, so we can find the most specific ignorelist by walking
3526 * ignores backwards.
3528 pe = TAILQ_LAST(ignores, got_pathlist_head);
3529 while (pe) {
3530 if (got_path_is_child(path, pe->path, pe->path_len)) {
3531 struct got_pathlist_head *ignorelist = pe->data;
3532 struct got_pathlist_entry *pi;
3533 TAILQ_FOREACH(pi, ignorelist, entry) {
3534 const char *pattern = pi->path;
3535 int flags = FNM_LEADING_DIR;
3536 if (strstr(pattern, "/**/") == NULL)
3537 flags |= FNM_PATHNAME;
3538 if (fnmatch(pattern, path, flags))
3539 continue;
3540 return 1;
3543 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3546 return 0;
3549 static const struct got_error *
3550 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3551 const char *path, int dirfd, const char *ignores_filename)
3553 const struct got_error *err = NULL;
3554 char *ignorespath;
3555 int fd = -1;
3556 FILE *ignoresfile = NULL;
3558 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3559 path[0] ? "/" : "", ignores_filename) == -1)
3560 return got_error_from_errno("asprintf");
3562 if (dirfd != -1) {
3563 fd = openat(dirfd, ignores_filename,
3564 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3565 if (fd == -1) {
3566 if (errno != ENOENT && errno != EACCES)
3567 err = got_error_from_errno2("openat",
3568 ignorespath);
3569 } else {
3570 ignoresfile = fdopen(fd, "r");
3571 if (ignoresfile == NULL)
3572 err = got_error_from_errno2("fdopen",
3573 ignorespath);
3574 else {
3575 fd = -1;
3576 err = read_ignores(ignores, path, ignoresfile);
3579 } else {
3580 ignoresfile = fopen(ignorespath, "re");
3581 if (ignoresfile == NULL) {
3582 if (errno != ENOENT && errno != EACCES)
3583 err = got_error_from_errno2("fopen",
3584 ignorespath);
3585 } else
3586 err = read_ignores(ignores, path, ignoresfile);
3589 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3590 err = got_error_from_errno2("fclose", path);
3591 if (fd != -1 && close(fd) == -1 && err == NULL)
3592 err = got_error_from_errno2("close", path);
3593 free(ignorespath);
3594 return err;
3597 static const struct got_error *
3598 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3599 int dirfd)
3601 const struct got_error *err = NULL;
3602 struct diff_dir_cb_arg *a = arg;
3603 char *path = NULL;
3605 if (ignore != NULL)
3606 *ignore = 0;
3608 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3609 return got_error(GOT_ERR_CANCELLED);
3611 if (parent_path[0]) {
3612 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3613 return got_error_from_errno("asprintf");
3614 } else {
3615 path = de->d_name;
3618 if (de->d_type == DT_DIR) {
3619 if (!a->no_ignores && ignore != NULL &&
3620 match_ignores(a->ignores, path))
3621 *ignore = 1;
3622 } else if (!match_ignores(a->ignores, path) &&
3623 got_path_is_child(path, a->status_path, a->status_path_len))
3624 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3625 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3626 if (parent_path[0])
3627 free(path);
3628 return err;
3631 static const struct got_error *
3632 status_traverse(void *arg, const char *path, int dirfd)
3634 const struct got_error *err = NULL;
3635 struct diff_dir_cb_arg *a = arg;
3637 if (a->no_ignores)
3638 return NULL;
3640 err = add_ignores(a->ignores, a->worktree->root_path,
3641 path, dirfd, ".cvsignore");
3642 if (err)
3643 return err;
3645 err = add_ignores(a->ignores, a->worktree->root_path, path,
3646 dirfd, ".gitignore");
3648 return err;
3651 static const struct got_error *
3652 report_single_file_status(const char *path, const char *ondisk_path,
3653 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3654 void *status_arg, struct got_repository *repo, int report_unchanged,
3655 struct got_pathlist_head *ignores, int no_ignores)
3657 struct got_fileindex_entry *ie;
3658 struct stat sb;
3660 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3661 if (ie)
3662 return report_file_status(ie, ondisk_path, -1, NULL,
3663 status_cb, status_arg, repo, report_unchanged);
3665 if (lstat(ondisk_path, &sb) == -1) {
3666 if (errno != ENOENT)
3667 return got_error_from_errno2("lstat", ondisk_path);
3668 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3669 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3672 if (!no_ignores && match_ignores(ignores, path))
3673 return NULL;
3675 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3676 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3677 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3679 return NULL;
3682 static const struct got_error *
3683 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3684 const char *root_path, const char *path)
3686 const struct got_error *err;
3687 char *parent_path, *next_parent_path = NULL;
3689 err = add_ignores(ignores, root_path, "", -1,
3690 ".cvsignore");
3691 if (err)
3692 return err;
3694 err = add_ignores(ignores, root_path, "", -1,
3695 ".gitignore");
3696 if (err)
3697 return err;
3699 err = got_path_dirname(&parent_path, path);
3700 if (err) {
3701 if (err->code == GOT_ERR_BAD_PATH)
3702 return NULL; /* cannot traverse parent */
3703 return err;
3705 for (;;) {
3706 err = add_ignores(ignores, root_path, parent_path, -1,
3707 ".cvsignore");
3708 if (err)
3709 break;
3710 err = add_ignores(ignores, root_path, parent_path, -1,
3711 ".gitignore");
3712 if (err)
3713 break;
3714 err = got_path_dirname(&next_parent_path, parent_path);
3715 if (err) {
3716 if (err->code == GOT_ERR_BAD_PATH)
3717 err = NULL; /* traversed everything */
3718 break;
3720 if (got_path_is_root_dir(parent_path))
3721 break;
3722 free(parent_path);
3723 parent_path = next_parent_path;
3724 next_parent_path = NULL;
3727 free(parent_path);
3728 free(next_parent_path);
3729 return err;
3732 static const struct got_error *
3733 worktree_status(struct got_worktree *worktree, const char *path,
3734 struct got_fileindex *fileindex, struct got_repository *repo,
3735 got_worktree_status_cb status_cb, void *status_arg,
3736 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3737 int report_unchanged)
3739 const struct got_error *err = NULL;
3740 int fd = -1;
3741 struct got_fileindex_diff_dir_cb fdiff_cb;
3742 struct diff_dir_cb_arg arg;
3743 char *ondisk_path = NULL;
3744 struct got_pathlist_head ignores;
3745 struct got_fileindex_entry *ie;
3747 TAILQ_INIT(&ignores);
3749 if (asprintf(&ondisk_path, "%s%s%s",
3750 worktree->root_path, path[0] ? "/" : "", path) == -1)
3751 return got_error_from_errno("asprintf");
3753 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3754 if (ie) {
3755 err = report_single_file_status(path, ondisk_path,
3756 fileindex, status_cb, status_arg, repo,
3757 report_unchanged, &ignores, no_ignores);
3758 goto done;
3761 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3762 if (fd == -1) {
3763 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3764 !got_err_open_nofollow_on_symlink())
3765 err = got_error_from_errno2("open", ondisk_path);
3766 else {
3767 if (!no_ignores) {
3768 err = add_ignores_from_parent_paths(&ignores,
3769 worktree->root_path, ondisk_path);
3770 if (err)
3771 goto done;
3773 err = report_single_file_status(path, ondisk_path,
3774 fileindex, status_cb, status_arg, repo,
3775 report_unchanged, &ignores, no_ignores);
3777 } else {
3778 fdiff_cb.diff_old_new = status_old_new;
3779 fdiff_cb.diff_old = status_old;
3780 fdiff_cb.diff_new = status_new;
3781 fdiff_cb.diff_traverse = status_traverse;
3782 arg.fileindex = fileindex;
3783 arg.worktree = worktree;
3784 arg.status_path = path;
3785 arg.status_path_len = strlen(path);
3786 arg.repo = repo;
3787 arg.status_cb = status_cb;
3788 arg.status_arg = status_arg;
3789 arg.cancel_cb = cancel_cb;
3790 arg.cancel_arg = cancel_arg;
3791 arg.report_unchanged = report_unchanged;
3792 arg.no_ignores = no_ignores;
3793 if (!no_ignores) {
3794 err = add_ignores_from_parent_paths(&ignores,
3795 worktree->root_path, path);
3796 if (err)
3797 goto done;
3799 arg.ignores = &ignores;
3800 err = got_fileindex_diff_dir(fileindex, fd,
3801 worktree->root_path, path, repo, &fdiff_cb, &arg);
3803 done:
3804 free_ignores(&ignores);
3805 if (fd != -1 && close(fd) == -1 && err == NULL)
3806 err = got_error_from_errno("close");
3807 free(ondisk_path);
3808 return err;
3811 const struct got_error *
3812 got_worktree_status(struct got_worktree *worktree,
3813 struct got_pathlist_head *paths, struct got_repository *repo,
3814 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3815 got_cancel_cb cancel_cb, void *cancel_arg)
3817 const struct got_error *err = NULL;
3818 char *fileindex_path = NULL;
3819 struct got_fileindex *fileindex = NULL;
3820 struct got_pathlist_entry *pe;
3822 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3823 if (err)
3824 return err;
3826 TAILQ_FOREACH(pe, paths, entry) {
3827 err = worktree_status(worktree, pe->path, fileindex, repo,
3828 status_cb, status_arg, cancel_cb, cancel_arg,
3829 no_ignores, 0);
3830 if (err)
3831 break;
3833 free(fileindex_path);
3834 got_fileindex_free(fileindex);
3835 return err;
3838 const struct got_error *
3839 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3840 const char *arg)
3842 const struct got_error *err = NULL;
3843 char *resolved = NULL, *cwd = NULL, *path = NULL;
3844 size_t len;
3845 struct stat sb;
3846 char *abspath = NULL;
3847 char canonpath[PATH_MAX];
3849 *wt_path = NULL;
3851 cwd = getcwd(NULL, 0);
3852 if (cwd == NULL)
3853 return got_error_from_errno("getcwd");
3855 if (lstat(arg, &sb) == -1) {
3856 if (errno != ENOENT) {
3857 err = got_error_from_errno2("lstat", arg);
3858 goto done;
3860 sb.st_mode = 0;
3862 if (S_ISLNK(sb.st_mode)) {
3864 * We cannot use realpath(3) with symlinks since we want to
3865 * operate on the symlink itself.
3866 * But we can make the path absolute, assuming it is relative
3867 * to the current working directory, and then canonicalize it.
3869 if (!got_path_is_absolute(arg)) {
3870 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3871 err = got_error_from_errno("asprintf");
3872 goto done;
3876 err = got_canonpath(abspath ? abspath : arg, canonpath,
3877 sizeof(canonpath));
3878 if (err)
3879 goto done;
3880 resolved = strdup(canonpath);
3881 if (resolved == NULL) {
3882 err = got_error_from_errno("strdup");
3883 goto done;
3885 } else {
3886 resolved = realpath(arg, NULL);
3887 if (resolved == NULL) {
3888 if (errno != ENOENT) {
3889 err = got_error_from_errno2("realpath", arg);
3890 goto done;
3892 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3893 err = got_error_from_errno("asprintf");
3894 goto done;
3896 err = got_canonpath(abspath, canonpath,
3897 sizeof(canonpath));
3898 if (err)
3899 goto done;
3900 resolved = strdup(canonpath);
3901 if (resolved == NULL) {
3902 err = got_error_from_errno("strdup");
3903 goto done;
3908 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3909 strlen(got_worktree_get_root_path(worktree)))) {
3910 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3911 goto done;
3914 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3915 err = got_path_skip_common_ancestor(&path,
3916 got_worktree_get_root_path(worktree), resolved);
3917 if (err)
3918 goto done;
3919 } else {
3920 path = strdup("");
3921 if (path == NULL) {
3922 err = got_error_from_errno("strdup");
3923 goto done;
3927 /* XXX status walk can't deal with trailing slash! */
3928 len = strlen(path);
3929 while (len > 0 && path[len - 1] == '/') {
3930 path[len - 1] = '\0';
3931 len--;
3933 done:
3934 free(abspath);
3935 free(resolved);
3936 free(cwd);
3937 if (err == NULL)
3938 *wt_path = path;
3939 else
3940 free(path);
3941 return err;
3944 struct schedule_addition_args {
3945 struct got_worktree *worktree;
3946 struct got_fileindex *fileindex;
3947 got_worktree_checkout_cb progress_cb;
3948 void *progress_arg;
3949 struct got_repository *repo;
3952 static const struct got_error *
3953 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3954 const char *relpath, struct got_object_id *blob_id,
3955 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3956 int dirfd, const char *de_name)
3958 struct schedule_addition_args *a = arg;
3959 const struct got_error *err = NULL;
3960 struct got_fileindex_entry *ie;
3961 struct stat sb;
3962 char *ondisk_path;
3964 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3965 relpath) == -1)
3966 return got_error_from_errno("asprintf");
3968 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3969 if (ie) {
3970 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3971 de_name, a->repo);
3972 if (err)
3973 goto done;
3974 /* Re-adding an existing entry is a no-op. */
3975 if (status == GOT_STATUS_ADD)
3976 goto done;
3977 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3978 if (err)
3979 goto done;
3982 if (status != GOT_STATUS_UNVERSIONED) {
3983 if (status == GOT_STATUS_NONEXISTENT)
3984 err = got_error_set_errno(ENOENT, ondisk_path);
3985 else
3986 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3987 goto done;
3990 err = got_fileindex_entry_alloc(&ie, relpath);
3991 if (err)
3992 goto done;
3993 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3994 relpath, NULL, NULL, 1);
3995 if (err) {
3996 got_fileindex_entry_free(ie);
3997 goto done;
3999 err = got_fileindex_entry_add(a->fileindex, ie);
4000 if (err) {
4001 got_fileindex_entry_free(ie);
4002 goto done;
4004 done:
4005 free(ondisk_path);
4006 if (err)
4007 return err;
4008 if (status == GOT_STATUS_ADD)
4009 return NULL;
4010 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4013 const struct got_error *
4014 got_worktree_schedule_add(struct got_worktree *worktree,
4015 struct got_pathlist_head *paths,
4016 got_worktree_checkout_cb progress_cb, void *progress_arg,
4017 struct got_repository *repo, int no_ignores)
4019 struct got_fileindex *fileindex = NULL;
4020 char *fileindex_path = NULL;
4021 const struct got_error *err = NULL, *sync_err, *unlockerr;
4022 struct got_pathlist_entry *pe;
4023 struct schedule_addition_args saa;
4025 err = lock_worktree(worktree, LOCK_EX);
4026 if (err)
4027 return err;
4029 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4030 if (err)
4031 goto done;
4033 saa.worktree = worktree;
4034 saa.fileindex = fileindex;
4035 saa.progress_cb = progress_cb;
4036 saa.progress_arg = progress_arg;
4037 saa.repo = repo;
4039 TAILQ_FOREACH(pe, paths, entry) {
4040 err = worktree_status(worktree, pe->path, fileindex, repo,
4041 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4042 if (err)
4043 break;
4045 sync_err = sync_fileindex(fileindex, fileindex_path);
4046 if (sync_err && err == NULL)
4047 err = sync_err;
4048 done:
4049 free(fileindex_path);
4050 if (fileindex)
4051 got_fileindex_free(fileindex);
4052 unlockerr = lock_worktree(worktree, LOCK_SH);
4053 if (unlockerr && err == NULL)
4054 err = unlockerr;
4055 return err;
4058 struct schedule_deletion_args {
4059 struct got_worktree *worktree;
4060 struct got_fileindex *fileindex;
4061 got_worktree_delete_cb progress_cb;
4062 void *progress_arg;
4063 struct got_repository *repo;
4064 int delete_local_mods;
4065 int keep_on_disk;
4066 int ignore_missing_paths;
4067 const char *status_codes;
4070 static const struct got_error *
4071 schedule_for_deletion(void *arg, unsigned char status,
4072 unsigned char staged_status, const char *relpath,
4073 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4074 struct got_object_id *commit_id, int dirfd, const char *de_name)
4076 struct schedule_deletion_args *a = arg;
4077 const struct got_error *err = NULL;
4078 struct got_fileindex_entry *ie = NULL;
4079 struct stat sb;
4080 char *ondisk_path;
4082 if (status == GOT_STATUS_NONEXISTENT) {
4083 if (a->ignore_missing_paths)
4084 return NULL;
4085 return got_error_set_errno(ENOENT, relpath);
4088 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4089 if (ie == NULL)
4090 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4092 staged_status = get_staged_status(ie);
4093 if (staged_status != GOT_STATUS_NO_CHANGE) {
4094 if (staged_status == GOT_STATUS_DELETE)
4095 return NULL;
4096 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4099 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4100 relpath) == -1)
4101 return got_error_from_errno("asprintf");
4103 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4104 a->repo);
4105 if (err)
4106 goto done;
4108 if (a->status_codes) {
4109 size_t ncodes = strlen(a->status_codes);
4110 int i;
4111 for (i = 0; i < ncodes ; i++) {
4112 if (status == a->status_codes[i])
4113 break;
4115 if (i == ncodes) {
4116 /* Do not delete files in non-matching status. */
4117 free(ondisk_path);
4118 return NULL;
4120 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4121 a->status_codes[i] != GOT_STATUS_MISSING) {
4122 static char msg[64];
4123 snprintf(msg, sizeof(msg),
4124 "invalid status code '%c'", a->status_codes[i]);
4125 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4126 goto done;
4130 if (status != GOT_STATUS_NO_CHANGE) {
4131 if (status == GOT_STATUS_DELETE)
4132 goto done;
4133 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4134 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4135 goto done;
4137 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4138 err = got_error_set_errno(ENOENT, relpath);
4139 goto done;
4141 if (status != GOT_STATUS_MODIFY &&
4142 status != GOT_STATUS_MISSING) {
4143 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4144 goto done;
4148 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4149 size_t root_len;
4151 if (dirfd != -1) {
4152 if (unlinkat(dirfd, de_name, 0) == -1) {
4153 err = got_error_from_errno2("unlinkat",
4154 ondisk_path);
4155 goto done;
4157 } else if (unlink(ondisk_path) == -1) {
4158 err = got_error_from_errno2("unlink", ondisk_path);
4159 goto done;
4162 root_len = strlen(a->worktree->root_path);
4163 do {
4164 char *parent;
4165 err = got_path_dirname(&parent, ondisk_path);
4166 if (err)
4167 goto done;
4168 free(ondisk_path);
4169 ondisk_path = parent;
4170 if (rmdir(ondisk_path) == -1) {
4171 if (errno != ENOTEMPTY)
4172 err = got_error_from_errno2("rmdir",
4173 ondisk_path);
4174 break;
4176 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4177 strlen(ondisk_path), root_len) != 0);
4180 got_fileindex_entry_mark_deleted_from_disk(ie);
4181 done:
4182 free(ondisk_path);
4183 if (err)
4184 return err;
4185 if (status == GOT_STATUS_DELETE)
4186 return NULL;
4187 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4188 staged_status, relpath);
4191 const struct got_error *
4192 got_worktree_schedule_delete(struct got_worktree *worktree,
4193 struct got_pathlist_head *paths, int delete_local_mods,
4194 const char *status_codes,
4195 got_worktree_delete_cb progress_cb, void *progress_arg,
4196 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4198 struct got_fileindex *fileindex = NULL;
4199 char *fileindex_path = NULL;
4200 const struct got_error *err = NULL, *sync_err, *unlockerr;
4201 struct got_pathlist_entry *pe;
4202 struct schedule_deletion_args sda;
4204 err = lock_worktree(worktree, LOCK_EX);
4205 if (err)
4206 return err;
4208 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4209 if (err)
4210 goto done;
4212 sda.worktree = worktree;
4213 sda.fileindex = fileindex;
4214 sda.progress_cb = progress_cb;
4215 sda.progress_arg = progress_arg;
4216 sda.repo = repo;
4217 sda.delete_local_mods = delete_local_mods;
4218 sda.keep_on_disk = keep_on_disk;
4219 sda.ignore_missing_paths = ignore_missing_paths;
4220 sda.status_codes = status_codes;
4222 TAILQ_FOREACH(pe, paths, entry) {
4223 err = worktree_status(worktree, pe->path, fileindex, repo,
4224 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4225 if (err)
4226 break;
4228 sync_err = sync_fileindex(fileindex, fileindex_path);
4229 if (sync_err && err == NULL)
4230 err = sync_err;
4231 done:
4232 free(fileindex_path);
4233 if (fileindex)
4234 got_fileindex_free(fileindex);
4235 unlockerr = lock_worktree(worktree, LOCK_SH);
4236 if (unlockerr && err == NULL)
4237 err = unlockerr;
4238 return err;
4241 static const struct got_error *
4242 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4244 const struct got_error *err = NULL;
4245 char *line = NULL;
4246 size_t linesize = 0, n;
4247 ssize_t linelen;
4249 linelen = getline(&line, &linesize, infile);
4250 if (linelen == -1) {
4251 if (ferror(infile)) {
4252 err = got_error_from_errno("getline");
4253 goto done;
4255 return NULL;
4257 if (outfile) {
4258 n = fwrite(line, 1, linelen, outfile);
4259 if (n != linelen) {
4260 err = got_ferror(outfile, GOT_ERR_IO);
4261 goto done;
4264 if (rejectfile) {
4265 n = fwrite(line, 1, linelen, rejectfile);
4266 if (n != linelen)
4267 err = got_ferror(rejectfile, GOT_ERR_IO);
4269 done:
4270 free(line);
4271 return err;
4274 static const struct got_error *
4275 skip_one_line(FILE *f)
4277 char *line = NULL;
4278 size_t linesize = 0;
4279 ssize_t linelen;
4281 linelen = getline(&line, &linesize, f);
4282 if (linelen == -1) {
4283 if (ferror(f))
4284 return got_error_from_errno("getline");
4285 return NULL;
4287 free(line);
4288 return NULL;
4291 static const struct got_error *
4292 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4293 int start_old, int end_old, int start_new, int end_new,
4294 FILE *outfile, FILE *rejectfile)
4296 const struct got_error *err;
4298 /* Copy old file's lines leading up to patch. */
4299 while (!feof(f1) && *line_cur1 < start_old) {
4300 err = copy_one_line(f1, outfile, NULL);
4301 if (err)
4302 return err;
4303 (*line_cur1)++;
4305 /* Skip new file's lines leading up to patch. */
4306 while (!feof(f2) && *line_cur2 < start_new) {
4307 if (rejectfile)
4308 err = copy_one_line(f2, NULL, rejectfile);
4309 else
4310 err = skip_one_line(f2);
4311 if (err)
4312 return err;
4313 (*line_cur2)++;
4315 /* Copy patched lines. */
4316 while (!feof(f2) && *line_cur2 <= end_new) {
4317 err = copy_one_line(f2, outfile, NULL);
4318 if (err)
4319 return err;
4320 (*line_cur2)++;
4322 /* Skip over old file's replaced lines. */
4323 while (!feof(f1) && *line_cur1 <= end_old) {
4324 if (rejectfile)
4325 err = copy_one_line(f1, NULL, rejectfile);
4326 else
4327 err = skip_one_line(f1);
4328 if (err)
4329 return err;
4330 (*line_cur1)++;
4333 return NULL;
4336 static const struct got_error *
4337 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4338 FILE *outfile, FILE *rejectfile)
4340 const struct got_error *err;
4342 if (outfile) {
4343 /* Copy old file's lines until EOF. */
4344 while (!feof(f1)) {
4345 err = copy_one_line(f1, outfile, NULL);
4346 if (err)
4347 return err;
4348 (*line_cur1)++;
4351 if (rejectfile) {
4352 /* Copy new file's lines until EOF. */
4353 while (!feof(f2)) {
4354 err = copy_one_line(f2, NULL, rejectfile);
4355 if (err)
4356 return err;
4357 (*line_cur2)++;
4361 return NULL;
4364 static const struct got_error *
4365 apply_or_reject_change(int *choice, int *nchunks_used,
4366 struct diff_result *diff_result, int n,
4367 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4368 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4369 got_worktree_patch_cb patch_cb, void *patch_arg)
4371 const struct got_error *err = NULL;
4372 struct diff_chunk_context cc = {};
4373 int start_old, end_old, start_new, end_new;
4374 FILE *hunkfile;
4375 struct diff_output_unidiff_state *diff_state;
4376 struct diff_input_info diff_info;
4377 int rc;
4379 *choice = GOT_PATCH_CHOICE_NONE;
4381 /* Get changed line numbers without context lines for copy_change(). */
4382 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4383 start_old = cc.left.start;
4384 end_old = cc.left.end;
4385 start_new = cc.right.start;
4386 end_new = cc.right.end;
4388 /* Get the same change with context lines for display. */
4389 memset(&cc, 0, sizeof(cc));
4390 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4392 memset(&diff_info, 0, sizeof(diff_info));
4393 diff_info.left_path = relpath;
4394 diff_info.right_path = relpath;
4396 diff_state = diff_output_unidiff_state_alloc();
4397 if (diff_state == NULL)
4398 return got_error_set_errno(ENOMEM,
4399 "diff_output_unidiff_state_alloc");
4401 hunkfile = got_opentemp();
4402 if (hunkfile == NULL) {
4403 err = got_error_from_errno("got_opentemp");
4404 goto done;
4407 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4408 diff_result, &cc);
4409 if (rc != DIFF_RC_OK) {
4410 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4411 goto done;
4414 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4415 err = got_ferror(hunkfile, GOT_ERR_IO);
4416 goto done;
4419 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4420 hunkfile, changeno, nchanges);
4421 if (err)
4422 goto done;
4424 switch (*choice) {
4425 case GOT_PATCH_CHOICE_YES:
4426 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4427 end_old, start_new, end_new, outfile, rejectfile);
4428 break;
4429 case GOT_PATCH_CHOICE_NO:
4430 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4431 end_old, start_new, end_new, rejectfile, outfile);
4432 break;
4433 case GOT_PATCH_CHOICE_QUIT:
4434 break;
4435 default:
4436 err = got_error(GOT_ERR_PATCH_CHOICE);
4437 break;
4439 done:
4440 diff_output_unidiff_state_free(diff_state);
4441 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4442 err = got_error_from_errno("fclose");
4443 return err;
4446 struct revert_file_args {
4447 struct got_worktree *worktree;
4448 struct got_fileindex *fileindex;
4449 got_worktree_checkout_cb progress_cb;
4450 void *progress_arg;
4451 got_worktree_patch_cb patch_cb;
4452 void *patch_arg;
4453 struct got_repository *repo;
4454 int unlink_added_files;
4457 static const struct got_error *
4458 create_patched_content(char **path_outfile, int reverse_patch,
4459 struct got_object_id *blob_id, const char *path2,
4460 int dirfd2, const char *de_name2,
4461 const char *relpath, struct got_repository *repo,
4462 got_worktree_patch_cb patch_cb, void *patch_arg)
4464 const struct got_error *err, *free_err;
4465 struct got_blob_object *blob = NULL;
4466 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4467 int fd = -1, fd2 = -1;
4468 char link_target[PATH_MAX];
4469 ssize_t link_len = 0;
4470 char *path1 = NULL, *id_str = NULL;
4471 struct stat sb2;
4472 struct got_diffreg_result *diffreg_result = NULL;
4473 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4474 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4476 *path_outfile = NULL;
4478 err = got_object_id_str(&id_str, blob_id);
4479 if (err)
4480 return err;
4482 if (dirfd2 != -1) {
4483 fd2 = openat(dirfd2, de_name2,
4484 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4485 if (fd2 == -1) {
4486 if (!got_err_open_nofollow_on_symlink()) {
4487 err = got_error_from_errno2("openat", path2);
4488 goto done;
4490 link_len = readlinkat(dirfd2, de_name2,
4491 link_target, sizeof(link_target));
4492 if (link_len == -1) {
4493 return got_error_from_errno2("readlinkat",
4494 path2);
4496 sb2.st_mode = S_IFLNK;
4497 sb2.st_size = link_len;
4499 } else {
4500 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4501 if (fd2 == -1) {
4502 if (!got_err_open_nofollow_on_symlink()) {
4503 err = got_error_from_errno2("open", path2);
4504 goto done;
4506 link_len = readlink(path2, link_target,
4507 sizeof(link_target));
4508 if (link_len == -1)
4509 return got_error_from_errno2("readlink", path2);
4510 sb2.st_mode = S_IFLNK;
4511 sb2.st_size = link_len;
4514 if (fd2 != -1) {
4515 if (fstat(fd2, &sb2) == -1) {
4516 err = got_error_from_errno2("fstat", path2);
4517 goto done;
4520 f2 = fdopen(fd2, "r");
4521 if (f2 == NULL) {
4522 err = got_error_from_errno2("fdopen", path2);
4523 goto done;
4525 fd2 = -1;
4526 } else {
4527 size_t n;
4528 f2 = got_opentemp();
4529 if (f2 == NULL) {
4530 err = got_error_from_errno2("got_opentemp", path2);
4531 goto done;
4533 n = fwrite(link_target, 1, link_len, f2);
4534 if (n != link_len) {
4535 err = got_ferror(f2, GOT_ERR_IO);
4536 goto done;
4538 if (fflush(f2) == EOF) {
4539 err = got_error_from_errno("fflush");
4540 goto done;
4542 rewind(f2);
4545 fd = got_opentempfd();
4546 if (fd == -1) {
4547 err = got_error_from_errno("got_opentempfd");
4548 goto done;
4551 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4552 if (err)
4553 goto done;
4555 err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4556 if (err)
4557 goto done;
4559 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4560 if (err)
4561 goto done;
4563 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4564 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4565 if (err)
4566 goto done;
4568 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4569 "");
4570 if (err)
4571 goto done;
4573 if (fseek(f1, 0L, SEEK_SET) == -1)
4574 return got_ferror(f1, GOT_ERR_IO);
4575 if (fseek(f2, 0L, SEEK_SET) == -1)
4576 return got_ferror(f2, GOT_ERR_IO);
4578 /* Count the number of actual changes in the diff result. */
4579 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4580 struct diff_chunk_context cc = {};
4581 diff_chunk_context_load_change(&cc, &nchunks_used,
4582 diffreg_result->result, n, 0);
4583 nchanges++;
4585 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4586 int choice;
4587 err = apply_or_reject_change(&choice, &nchunks_used,
4588 diffreg_result->result, n, relpath, f1, f2,
4589 &line_cur1, &line_cur2,
4590 reverse_patch ? NULL : outfile,
4591 reverse_patch ? outfile : NULL,
4592 ++i, nchanges, patch_cb, patch_arg);
4593 if (err)
4594 goto done;
4595 if (choice == GOT_PATCH_CHOICE_YES)
4596 have_content = 1;
4597 else if (choice == GOT_PATCH_CHOICE_QUIT)
4598 break;
4600 if (have_content) {
4601 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4602 reverse_patch ? NULL : outfile,
4603 reverse_patch ? outfile : NULL);
4604 if (err)
4605 goto done;
4607 if (!S_ISLNK(sb2.st_mode)) {
4608 mode_t mode;
4610 mode = apply_umask(sb2.st_mode);
4611 if (fchmod(fileno(outfile), mode) == -1) {
4612 err = got_error_from_errno2("fchmod", path2);
4613 goto done;
4617 done:
4618 free(id_str);
4619 if (fd != -1 && close(fd) == -1 && err == NULL)
4620 err = got_error_from_errno("close");
4621 if (blob)
4622 got_object_blob_close(blob);
4623 free_err = got_diffreg_result_free(diffreg_result);
4624 if (err == NULL)
4625 err = free_err;
4626 if (f1 && fclose(f1) == EOF && err == NULL)
4627 err = got_error_from_errno2("fclose", path1);
4628 if (f2 && fclose(f2) == EOF && err == NULL)
4629 err = got_error_from_errno2("fclose", path2);
4630 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4631 err = got_error_from_errno2("close", path2);
4632 if (outfile && fclose(outfile) == EOF && err == NULL)
4633 err = got_error_from_errno2("fclose", *path_outfile);
4634 if (path1 && unlink(path1) == -1 && err == NULL)
4635 err = got_error_from_errno2("unlink", path1);
4636 if (err || !have_content) {
4637 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4638 err = got_error_from_errno2("unlink", *path_outfile);
4639 free(*path_outfile);
4640 *path_outfile = NULL;
4642 free(path1);
4643 return err;
4646 static const struct got_error *
4647 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4648 const char *relpath, struct got_object_id *blob_id,
4649 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4650 int dirfd, const char *de_name)
4652 struct revert_file_args *a = arg;
4653 const struct got_error *err = NULL;
4654 char *parent_path = NULL;
4655 struct got_fileindex_entry *ie;
4656 struct got_commit_object *base_commit = NULL;
4657 struct got_tree_object *tree = NULL;
4658 struct got_object_id *tree_id = NULL;
4659 const struct got_tree_entry *te = NULL;
4660 char *tree_path = NULL, *te_name;
4661 char *ondisk_path = NULL, *path_content = NULL;
4662 struct got_blob_object *blob = NULL;
4663 int fd = -1;
4665 /* Reverting a staged deletion is a no-op. */
4666 if (status == GOT_STATUS_DELETE &&
4667 staged_status != GOT_STATUS_NO_CHANGE)
4668 return NULL;
4670 if (status == GOT_STATUS_UNVERSIONED)
4671 return (*a->progress_cb)(a->progress_arg,
4672 GOT_STATUS_UNVERSIONED, relpath);
4674 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4675 if (ie == NULL)
4676 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4678 /* Construct in-repository path of tree which contains this blob. */
4679 err = got_path_dirname(&parent_path, ie->path);
4680 if (err) {
4681 if (err->code != GOT_ERR_BAD_PATH)
4682 goto done;
4683 parent_path = strdup("/");
4684 if (parent_path == NULL) {
4685 err = got_error_from_errno("strdup");
4686 goto done;
4689 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4690 tree_path = strdup(parent_path);
4691 if (tree_path == NULL) {
4692 err = got_error_from_errno("strdup");
4693 goto done;
4695 } else {
4696 if (got_path_is_root_dir(parent_path)) {
4697 tree_path = strdup(a->worktree->path_prefix);
4698 if (tree_path == NULL) {
4699 err = got_error_from_errno("strdup");
4700 goto done;
4702 } else {
4703 if (asprintf(&tree_path, "%s/%s",
4704 a->worktree->path_prefix, parent_path) == -1) {
4705 err = got_error_from_errno("asprintf");
4706 goto done;
4711 err = got_object_open_as_commit(&base_commit, a->repo,
4712 a->worktree->base_commit_id);
4713 if (err)
4714 goto done;
4716 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4717 if (err) {
4718 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4719 (status == GOT_STATUS_ADD ||
4720 staged_status == GOT_STATUS_ADD)))
4721 goto done;
4722 } else {
4723 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4724 if (err)
4725 goto done;
4727 err = got_path_basename(&te_name, ie->path);
4728 if (err)
4729 goto done;
4731 te = got_object_tree_find_entry(tree, te_name);
4732 free(te_name);
4733 if (te == NULL && status != GOT_STATUS_ADD &&
4734 staged_status != GOT_STATUS_ADD) {
4735 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4736 goto done;
4740 switch (status) {
4741 case GOT_STATUS_ADD:
4742 if (a->patch_cb) {
4743 int choice = GOT_PATCH_CHOICE_NONE;
4744 err = (*a->patch_cb)(&choice, a->patch_arg,
4745 status, ie->path, NULL, 1, 1);
4746 if (err)
4747 goto done;
4748 if (choice != GOT_PATCH_CHOICE_YES)
4749 break;
4751 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4752 ie->path);
4753 if (err)
4754 goto done;
4755 got_fileindex_entry_remove(a->fileindex, ie);
4756 if (a->unlink_added_files) {
4757 if (asprintf(&ondisk_path, "%s/%s",
4758 got_worktree_get_root_path(a->worktree),
4759 relpath) == -1) {
4760 err = got_error_from_errno("asprintf");
4761 goto done;
4763 if (unlink(ondisk_path) == -1) {
4764 err = got_error_from_errno2("unlink",
4765 ondisk_path);
4766 break;
4769 break;
4770 case GOT_STATUS_DELETE:
4771 if (a->patch_cb) {
4772 int choice = GOT_PATCH_CHOICE_NONE;
4773 err = (*a->patch_cb)(&choice, a->patch_arg,
4774 status, ie->path, NULL, 1, 1);
4775 if (err)
4776 goto done;
4777 if (choice != GOT_PATCH_CHOICE_YES)
4778 break;
4780 /* fall through */
4781 case GOT_STATUS_MODIFY:
4782 case GOT_STATUS_MODE_CHANGE:
4783 case GOT_STATUS_CONFLICT:
4784 case GOT_STATUS_MISSING: {
4785 struct got_object_id id;
4786 if (staged_status == GOT_STATUS_ADD ||
4787 staged_status == GOT_STATUS_MODIFY) {
4788 memcpy(id.hash, ie->staged_blob_hash,
4789 SHA1_DIGEST_LENGTH);
4790 } else
4791 memcpy(id.hash, ie->blob_hash,
4792 SHA1_DIGEST_LENGTH);
4793 fd = got_opentempfd();
4794 if (fd == -1) {
4795 err = got_error_from_errno("got_opentempfd");
4796 goto done;
4799 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4800 if (err)
4801 goto done;
4803 if (asprintf(&ondisk_path, "%s/%s",
4804 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4805 err = got_error_from_errno("asprintf");
4806 goto done;
4809 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4810 status == GOT_STATUS_CONFLICT)) {
4811 int is_bad_symlink = 0;
4812 err = create_patched_content(&path_content, 1, &id,
4813 ondisk_path, dirfd, de_name, ie->path, a->repo,
4814 a->patch_cb, a->patch_arg);
4815 if (err || path_content == NULL)
4816 break;
4817 if (te && S_ISLNK(te->mode)) {
4818 if (unlink(path_content) == -1) {
4819 err = got_error_from_errno2("unlink",
4820 path_content);
4821 break;
4823 err = install_symlink(&is_bad_symlink,
4824 a->worktree, ondisk_path, ie->path,
4825 blob, 0, 1, 0, 0, a->repo,
4826 a->progress_cb, a->progress_arg);
4827 } else {
4828 if (rename(path_content, ondisk_path) == -1) {
4829 err = got_error_from_errno3("rename",
4830 path_content, ondisk_path);
4831 goto done;
4834 } else {
4835 int is_bad_symlink = 0;
4836 if (te && S_ISLNK(te->mode)) {
4837 err = install_symlink(&is_bad_symlink,
4838 a->worktree, ondisk_path, ie->path,
4839 blob, 0, 1, 0, 0, a->repo,
4840 a->progress_cb, a->progress_arg);
4841 } else {
4842 err = install_blob(a->worktree, ondisk_path,
4843 ie->path,
4844 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4845 got_fileindex_perms_to_st(ie), blob,
4846 0, 1, 0, 0, a->repo,
4847 a->progress_cb, a->progress_arg);
4849 if (err)
4850 goto done;
4851 if (status == GOT_STATUS_DELETE ||
4852 status == GOT_STATUS_MODE_CHANGE) {
4853 err = got_fileindex_entry_update(ie,
4854 a->worktree->root_fd, relpath,
4855 blob->id.hash,
4856 a->worktree->base_commit_id->hash, 1);
4857 if (err)
4858 goto done;
4860 if (is_bad_symlink) {
4861 got_fileindex_entry_filetype_set(ie,
4862 GOT_FILEIDX_MODE_BAD_SYMLINK);
4865 break;
4867 default:
4868 break;
4870 done:
4871 free(ondisk_path);
4872 free(path_content);
4873 free(parent_path);
4874 free(tree_path);
4875 if (fd != -1 && close(fd) == -1 && err == NULL)
4876 err = got_error_from_errno("close");
4877 if (blob)
4878 got_object_blob_close(blob);
4879 if (tree)
4880 got_object_tree_close(tree);
4881 free(tree_id);
4882 if (base_commit)
4883 got_object_commit_close(base_commit);
4884 return err;
4887 const struct got_error *
4888 got_worktree_revert(struct got_worktree *worktree,
4889 struct got_pathlist_head *paths,
4890 got_worktree_checkout_cb progress_cb, void *progress_arg,
4891 got_worktree_patch_cb patch_cb, void *patch_arg,
4892 struct got_repository *repo)
4894 struct got_fileindex *fileindex = NULL;
4895 char *fileindex_path = NULL;
4896 const struct got_error *err = NULL, *unlockerr = NULL;
4897 const struct got_error *sync_err = NULL;
4898 struct got_pathlist_entry *pe;
4899 struct revert_file_args rfa;
4901 err = lock_worktree(worktree, LOCK_EX);
4902 if (err)
4903 return err;
4905 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4906 if (err)
4907 goto done;
4909 rfa.worktree = worktree;
4910 rfa.fileindex = fileindex;
4911 rfa.progress_cb = progress_cb;
4912 rfa.progress_arg = progress_arg;
4913 rfa.patch_cb = patch_cb;
4914 rfa.patch_arg = patch_arg;
4915 rfa.repo = repo;
4916 rfa.unlink_added_files = 0;
4917 TAILQ_FOREACH(pe, paths, entry) {
4918 err = worktree_status(worktree, pe->path, fileindex, repo,
4919 revert_file, &rfa, NULL, NULL, 1, 0);
4920 if (err)
4921 break;
4923 sync_err = sync_fileindex(fileindex, fileindex_path);
4924 if (sync_err && err == NULL)
4925 err = sync_err;
4926 done:
4927 free(fileindex_path);
4928 if (fileindex)
4929 got_fileindex_free(fileindex);
4930 unlockerr = lock_worktree(worktree, LOCK_SH);
4931 if (unlockerr && err == NULL)
4932 err = unlockerr;
4933 return err;
4936 static void
4937 free_commitable(struct got_commitable *ct)
4939 free(ct->path);
4940 free(ct->in_repo_path);
4941 free(ct->ondisk_path);
4942 free(ct->blob_id);
4943 free(ct->base_blob_id);
4944 free(ct->staged_blob_id);
4945 free(ct->base_commit_id);
4946 free(ct);
4949 struct collect_commitables_arg {
4950 struct got_pathlist_head *commitable_paths;
4951 struct got_repository *repo;
4952 struct got_worktree *worktree;
4953 struct got_fileindex *fileindex;
4954 int have_staged_files;
4955 int allow_bad_symlinks;
4956 int diff_header_shown;
4957 FILE *diff_outfile;
4958 FILE *f1;
4959 FILE *f2;
4963 * Create a file which contains the target path of a symlink so we can feed
4964 * it as content to the diff engine.
4966 static const struct got_error *
4967 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4968 const char *abspath)
4970 const struct got_error *err = NULL;
4971 char target_path[PATH_MAX];
4972 ssize_t target_len, outlen;
4974 *fd = -1;
4976 if (dirfd != -1) {
4977 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4978 if (target_len == -1)
4979 return got_error_from_errno2("readlinkat", abspath);
4980 } else {
4981 target_len = readlink(abspath, target_path, PATH_MAX);
4982 if (target_len == -1)
4983 return got_error_from_errno2("readlink", abspath);
4986 *fd = got_opentempfd();
4987 if (*fd == -1)
4988 return got_error_from_errno("got_opentempfd");
4990 outlen = write(*fd, target_path, target_len);
4991 if (outlen == -1) {
4992 err = got_error_from_errno("got_opentempfd");
4993 goto done;
4996 if (lseek(*fd, 0, SEEK_SET) == -1) {
4997 err = got_error_from_errno2("lseek", abspath);
4998 goto done;
5000 done:
5001 if (err) {
5002 close(*fd);
5003 *fd = -1;
5005 return err;
5008 static const struct got_error *
5009 append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5010 FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5011 int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5013 const struct got_error *err = NULL;
5014 struct got_blob_object *blob1 = NULL;
5015 int fd = -1, fd1 = -1, fd2 = -1;
5016 FILE *ondisk_file = NULL;
5017 char *label1 = NULL;
5018 struct stat sb;
5019 off_t size1 = 0;
5020 int f2_exists = 0;
5021 char *id_str = NULL;
5023 memset(&sb, 0, sizeof(sb));
5025 if (diff_staged) {
5026 if (ct->staged_status != GOT_STATUS_MODIFY &&
5027 ct->staged_status != GOT_STATUS_ADD &&
5028 ct->staged_status != GOT_STATUS_DELETE)
5029 return NULL;
5030 } else {
5031 if (ct->status != GOT_STATUS_MODIFY &&
5032 ct->status != GOT_STATUS_ADD &&
5033 ct->status != GOT_STATUS_DELETE)
5034 return NULL;
5037 err = got_opentemp_truncate(f1);
5038 if (err)
5039 return got_error_from_errno("got_opentemp_truncate");
5040 err = got_opentemp_truncate(f2);
5041 if (err)
5042 return got_error_from_errno("got_opentemp_truncate");
5044 if (!*diff_header_shown) {
5045 err = got_object_id_str(&id_str, worktree->base_commit_id);
5046 if (err)
5047 return err;
5048 fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5049 got_worktree_get_root_path(worktree));
5050 fprintf(diff_outfile, "commit - %s\n", id_str);
5051 fprintf(diff_outfile, "path + %s%s\n",
5052 got_worktree_get_root_path(worktree),
5053 diff_staged ? " (staged changes)" : "");
5054 *diff_header_shown = 1;
5057 if (diff_staged) {
5058 const char *label1 = NULL, *label2 = NULL;
5059 switch (ct->staged_status) {
5060 case GOT_STATUS_MODIFY:
5061 label1 = ct->path;
5062 label2 = ct->path;
5063 break;
5064 case GOT_STATUS_ADD:
5065 label2 = ct->path;
5066 break;
5067 case GOT_STATUS_DELETE:
5068 label1 = ct->path;
5069 break;
5070 default:
5071 return got_error(GOT_ERR_FILE_STATUS);
5073 fd1 = got_opentempfd();
5074 if (fd1 == -1) {
5075 err = got_error_from_errno("got_opentempfd");
5076 goto done;
5078 fd2 = got_opentempfd();
5079 if (fd2 == -1) {
5080 err = got_error_from_errno("got_opentempfd");
5081 goto done;
5083 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5084 fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5085 label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5086 NULL, repo, diff_outfile);
5087 goto done;
5090 fd1 = got_opentempfd();
5091 if (fd1 == -1) {
5092 err = got_error_from_errno("got_opentempfd");
5093 goto done;
5096 if (ct->status != GOT_STATUS_ADD) {
5097 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5098 8192, fd1);
5099 if (err)
5100 goto done;
5103 if (ct->status != GOT_STATUS_DELETE) {
5104 if (dirfd != -1) {
5105 fd = openat(dirfd, de_name,
5106 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5107 if (fd == -1) {
5108 if (!got_err_open_nofollow_on_symlink()) {
5109 err = got_error_from_errno2("openat",
5110 ct->ondisk_path);
5111 goto done;
5113 err = get_symlink_target_file(&fd, dirfd,
5114 de_name, ct->ondisk_path);
5115 if (err)
5116 goto done;
5118 } else {
5119 fd = open(ct->ondisk_path,
5120 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5121 if (fd == -1) {
5122 if (!got_err_open_nofollow_on_symlink()) {
5123 err = got_error_from_errno2("open",
5124 ct->ondisk_path);
5125 goto done;
5127 err = get_symlink_target_file(&fd, dirfd,
5128 de_name, ct->ondisk_path);
5129 if (err)
5130 goto done;
5133 if (fstatat(fd, ct->ondisk_path, &sb,
5134 AT_SYMLINK_NOFOLLOW) == -1) {
5135 err = got_error_from_errno2("fstatat", ct->ondisk_path);
5136 goto done;
5138 ondisk_file = fdopen(fd, "r");
5139 if (ondisk_file == NULL) {
5140 err = got_error_from_errno2("fdopen", ct->ondisk_path);
5141 goto done;
5143 fd = -1;
5144 f2_exists = 1;
5147 if (blob1) {
5148 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5149 f1, blob1);
5150 if (err)
5151 goto done;
5154 err = got_diff_blob_file(blob1, f1, size1, label1,
5155 ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5156 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5157 done:
5158 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5159 err = got_error_from_errno("close");
5160 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5161 err = got_error_from_errno("close");
5162 if (blob1)
5163 got_object_blob_close(blob1);
5164 if (fd != -1 && close(fd) == -1 && err == NULL)
5165 err = got_error_from_errno("close");
5166 if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5167 err = got_error_from_errno("fclose");
5168 return err;
5171 static const struct got_error *
5172 collect_commitables(void *arg, unsigned char status,
5173 unsigned char staged_status, const char *relpath,
5174 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5175 struct got_object_id *commit_id, int dirfd, const char *de_name)
5177 struct collect_commitables_arg *a = arg;
5178 const struct got_error *err = NULL;
5179 struct got_commitable *ct = NULL;
5180 struct got_pathlist_entry *new = NULL;
5181 char *parent_path = NULL, *path = NULL;
5182 struct stat sb;
5184 if (a->have_staged_files) {
5185 if (staged_status != GOT_STATUS_MODIFY &&
5186 staged_status != GOT_STATUS_ADD &&
5187 staged_status != GOT_STATUS_DELETE)
5188 return NULL;
5189 } else {
5190 if (status == GOT_STATUS_CONFLICT)
5191 return got_error(GOT_ERR_COMMIT_CONFLICT);
5193 if (status != GOT_STATUS_MODIFY &&
5194 status != GOT_STATUS_MODE_CHANGE &&
5195 status != GOT_STATUS_ADD &&
5196 status != GOT_STATUS_DELETE)
5197 return NULL;
5200 if (asprintf(&path, "/%s", relpath) == -1) {
5201 err = got_error_from_errno("asprintf");
5202 goto done;
5204 if (strcmp(path, "/") == 0) {
5205 parent_path = strdup("");
5206 if (parent_path == NULL)
5207 return got_error_from_errno("strdup");
5208 } else {
5209 err = got_path_dirname(&parent_path, path);
5210 if (err)
5211 return err;
5214 ct = calloc(1, sizeof(*ct));
5215 if (ct == NULL) {
5216 err = got_error_from_errno("calloc");
5217 goto done;
5220 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5221 relpath) == -1) {
5222 err = got_error_from_errno("asprintf");
5223 goto done;
5226 if (staged_status == GOT_STATUS_ADD ||
5227 staged_status == GOT_STATUS_MODIFY) {
5228 struct got_fileindex_entry *ie;
5229 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5230 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5231 case GOT_FILEIDX_MODE_REGULAR_FILE:
5232 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5233 ct->mode = S_IFREG;
5234 break;
5235 case GOT_FILEIDX_MODE_SYMLINK:
5236 ct->mode = S_IFLNK;
5237 break;
5238 default:
5239 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5240 goto done;
5242 ct->mode |= got_fileindex_entry_perms_get(ie);
5243 } else if (status != GOT_STATUS_DELETE &&
5244 staged_status != GOT_STATUS_DELETE) {
5245 if (dirfd != -1) {
5246 if (fstatat(dirfd, de_name, &sb,
5247 AT_SYMLINK_NOFOLLOW) == -1) {
5248 err = got_error_from_errno2("fstatat",
5249 ct->ondisk_path);
5250 goto done;
5252 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5253 err = got_error_from_errno2("lstat", ct->ondisk_path);
5254 goto done;
5256 ct->mode = sb.st_mode;
5259 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5260 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5261 relpath) == -1) {
5262 err = got_error_from_errno("asprintf");
5263 goto done;
5266 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5267 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5268 int is_bad_symlink;
5269 char target_path[PATH_MAX];
5270 ssize_t target_len;
5271 target_len = readlink(ct->ondisk_path, target_path,
5272 sizeof(target_path));
5273 if (target_len == -1) {
5274 err = got_error_from_errno2("readlink",
5275 ct->ondisk_path);
5276 goto done;
5278 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5279 target_len, ct->ondisk_path, a->worktree->root_path);
5280 if (err)
5281 goto done;
5282 if (is_bad_symlink) {
5283 err = got_error_path(ct->ondisk_path,
5284 GOT_ERR_BAD_SYMLINK);
5285 goto done;
5290 ct->status = status;
5291 ct->staged_status = staged_status;
5292 ct->blob_id = NULL; /* will be filled in when blob gets created */
5293 if (ct->status != GOT_STATUS_ADD &&
5294 ct->staged_status != GOT_STATUS_ADD) {
5295 ct->base_blob_id = got_object_id_dup(blob_id);
5296 if (ct->base_blob_id == NULL) {
5297 err = got_error_from_errno("got_object_id_dup");
5298 goto done;
5300 ct->base_commit_id = got_object_id_dup(commit_id);
5301 if (ct->base_commit_id == NULL) {
5302 err = got_error_from_errno("got_object_id_dup");
5303 goto done;
5306 if (ct->staged_status == GOT_STATUS_ADD ||
5307 ct->staged_status == GOT_STATUS_MODIFY) {
5308 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5309 if (ct->staged_blob_id == NULL) {
5310 err = got_error_from_errno("got_object_id_dup");
5311 goto done;
5314 ct->path = strdup(path);
5315 if (ct->path == NULL) {
5316 err = got_error_from_errno("strdup");
5317 goto done;
5319 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5320 if (err)
5321 goto done;
5323 if (a->diff_outfile && ct && new != NULL) {
5324 err = append_ct_diff(ct, &a->diff_header_shown,
5325 a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5326 a->have_staged_files, a->repo, a->worktree);
5327 if (err)
5328 goto done;
5330 done:
5331 if (ct && (err || new == NULL))
5332 free_commitable(ct);
5333 free(parent_path);
5334 free(path);
5335 return err;
5338 static const struct got_error *write_tree(struct got_object_id **, int *,
5339 struct got_tree_object *, const char *, struct got_pathlist_head *,
5340 got_worktree_status_cb status_cb, void *status_arg,
5341 struct got_repository *);
5343 static const struct got_error *
5344 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5345 struct got_tree_entry *te, const char *parent_path,
5346 struct got_pathlist_head *commitable_paths,
5347 got_worktree_status_cb status_cb, void *status_arg,
5348 struct got_repository *repo)
5350 const struct got_error *err = NULL;
5351 struct got_tree_object *subtree;
5352 char *subpath;
5354 if (asprintf(&subpath, "%s%s%s", parent_path,
5355 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5356 return got_error_from_errno("asprintf");
5358 err = got_object_open_as_tree(&subtree, repo, &te->id);
5359 if (err)
5360 return err;
5362 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5363 commitable_paths, status_cb, status_arg, repo);
5364 got_object_tree_close(subtree);
5365 free(subpath);
5366 return err;
5369 static const struct got_error *
5370 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5372 const struct got_error *err = NULL;
5373 char *ct_parent_path = NULL;
5375 *match = 0;
5377 if (strchr(ct->in_repo_path, '/') == NULL) {
5378 *match = got_path_is_root_dir(path);
5379 return NULL;
5382 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5383 if (err)
5384 return err;
5385 *match = (strcmp(path, ct_parent_path) == 0);
5386 free(ct_parent_path);
5387 return err;
5390 static mode_t
5391 get_ct_file_mode(struct got_commitable *ct)
5393 if (S_ISLNK(ct->mode))
5394 return S_IFLNK;
5396 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5399 static const struct got_error *
5400 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5401 struct got_tree_entry *te, struct got_commitable *ct)
5403 const struct got_error *err = NULL;
5405 *new_te = NULL;
5407 err = got_object_tree_entry_dup(new_te, te);
5408 if (err)
5409 goto done;
5411 (*new_te)->mode = get_ct_file_mode(ct);
5413 if (ct->staged_status == GOT_STATUS_MODIFY)
5414 memcpy(&(*new_te)->id, ct->staged_blob_id,
5415 sizeof((*new_te)->id));
5416 else
5417 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5418 done:
5419 if (err && *new_te) {
5420 free(*new_te);
5421 *new_te = NULL;
5423 return err;
5426 static const struct got_error *
5427 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5428 struct got_commitable *ct)
5430 const struct got_error *err = NULL;
5431 char *ct_name = NULL;
5433 *new_te = NULL;
5435 *new_te = calloc(1, sizeof(**new_te));
5436 if (*new_te == NULL)
5437 return got_error_from_errno("calloc");
5439 err = got_path_basename(&ct_name, ct->path);
5440 if (err)
5441 goto done;
5442 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5443 sizeof((*new_te)->name)) {
5444 err = got_error(GOT_ERR_NO_SPACE);
5445 goto done;
5448 (*new_te)->mode = get_ct_file_mode(ct);
5450 if (ct->staged_status == GOT_STATUS_ADD)
5451 memcpy(&(*new_te)->id, ct->staged_blob_id,
5452 sizeof((*new_te)->id));
5453 else
5454 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5455 done:
5456 free(ct_name);
5457 if (err && *new_te) {
5458 free(*new_te);
5459 *new_te = NULL;
5461 return err;
5464 static const struct got_error *
5465 insert_tree_entry(struct got_tree_entry *new_te,
5466 struct got_pathlist_head *paths)
5468 const struct got_error *err = NULL;
5469 struct got_pathlist_entry *new_pe;
5471 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5472 if (err)
5473 return err;
5474 if (new_pe == NULL)
5475 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5476 return NULL;
5479 static const struct got_error *
5480 report_ct_status(struct got_commitable *ct,
5481 got_worktree_status_cb status_cb, void *status_arg)
5483 const char *ct_path = ct->path;
5484 unsigned char status;
5486 if (status_cb == NULL) /* no commit progress output desired */
5487 return NULL;
5489 while (ct_path[0] == '/')
5490 ct_path++;
5492 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5493 status = ct->staged_status;
5494 else
5495 status = ct->status;
5497 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5498 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5501 static const struct got_error *
5502 match_modified_subtree(int *modified, struct got_tree_entry *te,
5503 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5505 const struct got_error *err = NULL;
5506 struct got_pathlist_entry *pe;
5507 char *te_path;
5509 *modified = 0;
5511 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5512 got_path_is_root_dir(base_tree_path) ? "" : "/",
5513 te->name) == -1)
5514 return got_error_from_errno("asprintf");
5516 TAILQ_FOREACH(pe, commitable_paths, entry) {
5517 struct got_commitable *ct = pe->data;
5518 *modified = got_path_is_child(ct->in_repo_path, te_path,
5519 strlen(te_path));
5520 if (*modified)
5521 break;
5524 free(te_path);
5525 return err;
5528 static const struct got_error *
5529 match_deleted_or_modified_ct(struct got_commitable **ctp,
5530 struct got_tree_entry *te, const char *base_tree_path,
5531 struct got_pathlist_head *commitable_paths)
5533 const struct got_error *err = NULL;
5534 struct got_pathlist_entry *pe;
5536 *ctp = NULL;
5538 TAILQ_FOREACH(pe, commitable_paths, entry) {
5539 struct got_commitable *ct = pe->data;
5540 char *ct_name = NULL;
5541 int path_matches;
5543 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5544 if (ct->status != GOT_STATUS_MODIFY &&
5545 ct->status != GOT_STATUS_MODE_CHANGE &&
5546 ct->status != GOT_STATUS_DELETE)
5547 continue;
5548 } else {
5549 if (ct->staged_status != GOT_STATUS_MODIFY &&
5550 ct->staged_status != GOT_STATUS_DELETE)
5551 continue;
5554 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5555 continue;
5557 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5558 if (err)
5559 return err;
5560 if (!path_matches)
5561 continue;
5563 err = got_path_basename(&ct_name, pe->path);
5564 if (err)
5565 return err;
5567 if (strcmp(te->name, ct_name) != 0) {
5568 free(ct_name);
5569 continue;
5571 free(ct_name);
5573 *ctp = ct;
5574 break;
5577 return err;
5580 static const struct got_error *
5581 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5582 const char *child_path, const char *path_base_tree,
5583 struct got_pathlist_head *commitable_paths,
5584 got_worktree_status_cb status_cb, void *status_arg,
5585 struct got_repository *repo)
5587 const struct got_error *err = NULL;
5588 struct got_tree_entry *new_te;
5589 char *subtree_path;
5590 struct got_object_id *id = NULL;
5591 int nentries;
5593 *new_tep = NULL;
5595 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5596 got_path_is_root_dir(path_base_tree) ? "" : "/",
5597 child_path) == -1)
5598 return got_error_from_errno("asprintf");
5600 new_te = calloc(1, sizeof(*new_te));
5601 if (new_te == NULL)
5602 return got_error_from_errno("calloc");
5603 new_te->mode = S_IFDIR;
5605 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5606 sizeof(new_te->name)) {
5607 err = got_error(GOT_ERR_NO_SPACE);
5608 goto done;
5610 err = write_tree(&id, &nentries, NULL, subtree_path,
5611 commitable_paths, status_cb, status_arg, repo);
5612 if (err) {
5613 free(new_te);
5614 goto done;
5616 memcpy(&new_te->id, id, sizeof(new_te->id));
5617 done:
5618 free(id);
5619 free(subtree_path);
5620 if (err == NULL)
5621 *new_tep = new_te;
5622 return err;
5625 static const struct got_error *
5626 write_tree(struct got_object_id **new_tree_id, int *nentries,
5627 struct got_tree_object *base_tree, const char *path_base_tree,
5628 struct got_pathlist_head *commitable_paths,
5629 got_worktree_status_cb status_cb, void *status_arg,
5630 struct got_repository *repo)
5632 const struct got_error *err = NULL;
5633 struct got_pathlist_head paths;
5634 struct got_tree_entry *te, *new_te = NULL;
5635 struct got_pathlist_entry *pe;
5637 TAILQ_INIT(&paths);
5638 *nentries = 0;
5640 /* Insert, and recurse into, newly added entries first. */
5641 TAILQ_FOREACH(pe, commitable_paths, entry) {
5642 struct got_commitable *ct = pe->data;
5643 char *child_path = NULL, *slash;
5645 if ((ct->status != GOT_STATUS_ADD &&
5646 ct->staged_status != GOT_STATUS_ADD) ||
5647 (ct->flags & GOT_COMMITABLE_ADDED))
5648 continue;
5650 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5651 strlen(path_base_tree)))
5652 continue;
5654 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5655 ct->in_repo_path);
5656 if (err)
5657 goto done;
5659 slash = strchr(child_path, '/');
5660 if (slash == NULL) {
5661 err = alloc_added_blob_tree_entry(&new_te, ct);
5662 if (err)
5663 goto done;
5664 err = report_ct_status(ct, status_cb, status_arg);
5665 if (err)
5666 goto done;
5667 ct->flags |= GOT_COMMITABLE_ADDED;
5668 err = insert_tree_entry(new_te, &paths);
5669 if (err)
5670 goto done;
5671 (*nentries)++;
5672 } else {
5673 *slash = '\0'; /* trim trailing path components */
5674 if (base_tree == NULL ||
5675 got_object_tree_find_entry(base_tree, child_path)
5676 == NULL) {
5677 err = make_subtree_for_added_blob(&new_te,
5678 child_path, path_base_tree,
5679 commitable_paths, status_cb, status_arg,
5680 repo);
5681 if (err)
5682 goto done;
5683 err = insert_tree_entry(new_te, &paths);
5684 if (err)
5685 goto done;
5686 (*nentries)++;
5691 if (base_tree) {
5692 int i, nbase_entries;
5693 /* Handle modified and deleted entries. */
5694 nbase_entries = got_object_tree_get_nentries(base_tree);
5695 for (i = 0; i < nbase_entries; i++) {
5696 struct got_commitable *ct = NULL;
5698 te = got_object_tree_get_entry(base_tree, i);
5699 if (got_object_tree_entry_is_submodule(te)) {
5700 /* Entry is a submodule; just copy it. */
5701 err = got_object_tree_entry_dup(&new_te, te);
5702 if (err)
5703 goto done;
5704 err = insert_tree_entry(new_te, &paths);
5705 if (err)
5706 goto done;
5707 (*nentries)++;
5708 continue;
5711 if (S_ISDIR(te->mode)) {
5712 int modified;
5713 err = got_object_tree_entry_dup(&new_te, te);
5714 if (err)
5715 goto done;
5716 err = match_modified_subtree(&modified, te,
5717 path_base_tree, commitable_paths);
5718 if (err)
5719 goto done;
5720 /* Avoid recursion into unmodified subtrees. */
5721 if (modified) {
5722 struct got_object_id *new_id;
5723 int nsubentries;
5724 err = write_subtree(&new_id,
5725 &nsubentries, te,
5726 path_base_tree, commitable_paths,
5727 status_cb, status_arg, repo);
5728 if (err)
5729 goto done;
5730 if (nsubentries == 0) {
5731 /* All entries were deleted. */
5732 free(new_id);
5733 continue;
5735 memcpy(&new_te->id, new_id,
5736 sizeof(new_te->id));
5737 free(new_id);
5739 err = insert_tree_entry(new_te, &paths);
5740 if (err)
5741 goto done;
5742 (*nentries)++;
5743 continue;
5746 err = match_deleted_or_modified_ct(&ct, te,
5747 path_base_tree, commitable_paths);
5748 if (err)
5749 goto done;
5750 if (ct) {
5751 /* NB: Deleted entries get dropped here. */
5752 if (ct->status == GOT_STATUS_MODIFY ||
5753 ct->status == GOT_STATUS_MODE_CHANGE ||
5754 ct->staged_status == GOT_STATUS_MODIFY) {
5755 err = alloc_modified_blob_tree_entry(
5756 &new_te, te, ct);
5757 if (err)
5758 goto done;
5759 err = insert_tree_entry(new_te, &paths);
5760 if (err)
5761 goto done;
5762 (*nentries)++;
5764 err = report_ct_status(ct, status_cb,
5765 status_arg);
5766 if (err)
5767 goto done;
5768 } else {
5769 /* Entry is unchanged; just copy it. */
5770 err = got_object_tree_entry_dup(&new_te, te);
5771 if (err)
5772 goto done;
5773 err = insert_tree_entry(new_te, &paths);
5774 if (err)
5775 goto done;
5776 (*nentries)++;
5781 /* Write new list of entries; deleted entries have been dropped. */
5782 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5783 done:
5784 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
5785 return err;
5788 static const struct got_error *
5789 update_fileindex_after_commit(struct got_worktree *worktree,
5790 struct got_pathlist_head *commitable_paths,
5791 struct got_object_id *new_base_commit_id,
5792 struct got_fileindex *fileindex, int have_staged_files)
5794 const struct got_error *err = NULL;
5795 struct got_pathlist_entry *pe;
5796 char *relpath = NULL;
5798 TAILQ_FOREACH(pe, commitable_paths, entry) {
5799 struct got_fileindex_entry *ie;
5800 struct got_commitable *ct = pe->data;
5802 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5804 err = got_path_skip_common_ancestor(&relpath,
5805 worktree->root_path, ct->ondisk_path);
5806 if (err)
5807 goto done;
5809 if (ie) {
5810 if (ct->status == GOT_STATUS_DELETE ||
5811 ct->staged_status == GOT_STATUS_DELETE) {
5812 got_fileindex_entry_remove(fileindex, ie);
5813 } else if (ct->staged_status == GOT_STATUS_ADD ||
5814 ct->staged_status == GOT_STATUS_MODIFY) {
5815 got_fileindex_entry_stage_set(ie,
5816 GOT_FILEIDX_STAGE_NONE);
5817 got_fileindex_entry_staged_filetype_set(ie, 0);
5819 err = got_fileindex_entry_update(ie,
5820 worktree->root_fd, relpath,
5821 ct->staged_blob_id->hash,
5822 new_base_commit_id->hash,
5823 !have_staged_files);
5824 } else
5825 err = got_fileindex_entry_update(ie,
5826 worktree->root_fd, relpath,
5827 ct->blob_id->hash,
5828 new_base_commit_id->hash,
5829 !have_staged_files);
5830 } else {
5831 err = got_fileindex_entry_alloc(&ie, pe->path);
5832 if (err)
5833 goto done;
5834 err = got_fileindex_entry_update(ie,
5835 worktree->root_fd, relpath, ct->blob_id->hash,
5836 new_base_commit_id->hash, 1);
5837 if (err) {
5838 got_fileindex_entry_free(ie);
5839 goto done;
5841 err = got_fileindex_entry_add(fileindex, ie);
5842 if (err) {
5843 got_fileindex_entry_free(ie);
5844 goto done;
5847 free(relpath);
5848 relpath = NULL;
5850 done:
5851 free(relpath);
5852 return err;
5856 static const struct got_error *
5857 check_out_of_date(const char *in_repo_path, unsigned char status,
5858 unsigned char staged_status, struct got_object_id *base_blob_id,
5859 struct got_object_id *base_commit_id,
5860 struct got_object_id *head_commit_id, struct got_repository *repo,
5861 int ood_errcode)
5863 const struct got_error *err = NULL;
5864 struct got_commit_object *commit = NULL;
5865 struct got_object_id *id = NULL;
5867 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5868 /* Trivial case: base commit == head commit */
5869 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5870 return NULL;
5872 * Ensure file content which local changes were based
5873 * on matches file content in the branch head.
5875 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5876 if (err)
5877 goto done;
5878 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5879 if (err) {
5880 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5881 err = got_error(ood_errcode);
5882 goto done;
5883 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5884 err = got_error(ood_errcode);
5885 } else {
5886 /* Require that added files don't exist in the branch head. */
5887 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5888 if (err)
5889 goto done;
5890 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5891 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5892 goto done;
5893 err = id ? got_error(ood_errcode) : NULL;
5895 done:
5896 free(id);
5897 if (commit)
5898 got_object_commit_close(commit);
5899 return err;
5902 static const struct got_error *
5903 commit_worktree(struct got_object_id **new_commit_id,
5904 struct got_pathlist_head *commitable_paths,
5905 struct got_object_id *head_commit_id,
5906 struct got_object_id *parent_id2,
5907 struct got_worktree *worktree,
5908 const char *author, const char *committer, char *diff_path,
5909 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5910 got_worktree_status_cb status_cb, void *status_arg,
5911 struct got_repository *repo)
5913 const struct got_error *err = NULL, *unlockerr = NULL;
5914 struct got_pathlist_entry *pe;
5915 const char *head_ref_name = NULL;
5916 struct got_commit_object *head_commit = NULL;
5917 struct got_reference *head_ref2 = NULL;
5918 struct got_object_id *head_commit_id2 = NULL;
5919 struct got_tree_object *head_tree = NULL;
5920 struct got_object_id *new_tree_id = NULL;
5921 int nentries, nparents = 0;
5922 struct got_object_id_queue parent_ids;
5923 struct got_object_qid *pid = NULL;
5924 char *logmsg = NULL;
5925 time_t timestamp;
5927 *new_commit_id = NULL;
5929 STAILQ_INIT(&parent_ids);
5931 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5932 if (err)
5933 goto done;
5935 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5936 if (err)
5937 goto done;
5939 if (commit_msg_cb != NULL) {
5940 err = commit_msg_cb(commitable_paths, diff_path,
5941 &logmsg, commit_arg);
5942 if (err)
5943 goto done;
5946 if (logmsg == NULL || strlen(logmsg) == 0) {
5947 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5948 goto done;
5951 /* Create blobs from added and modified files and record their IDs. */
5952 TAILQ_FOREACH(pe, commitable_paths, entry) {
5953 struct got_commitable *ct = pe->data;
5954 char *ondisk_path;
5956 /* Blobs for staged files already exist. */
5957 if (ct->staged_status == GOT_STATUS_ADD ||
5958 ct->staged_status == GOT_STATUS_MODIFY)
5959 continue;
5961 if (ct->status != GOT_STATUS_ADD &&
5962 ct->status != GOT_STATUS_MODIFY &&
5963 ct->status != GOT_STATUS_MODE_CHANGE)
5964 continue;
5966 if (asprintf(&ondisk_path, "%s/%s",
5967 worktree->root_path, pe->path) == -1) {
5968 err = got_error_from_errno("asprintf");
5969 goto done;
5971 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5972 free(ondisk_path);
5973 if (err)
5974 goto done;
5977 /* Recursively write new tree objects. */
5978 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5979 commitable_paths, status_cb, status_arg, repo);
5980 if (err)
5981 goto done;
5983 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5984 if (err)
5985 goto done;
5986 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5987 nparents++;
5988 if (parent_id2) {
5989 err = got_object_qid_alloc(&pid, parent_id2);
5990 if (err)
5991 goto done;
5992 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5993 nparents++;
5995 timestamp = time(NULL);
5996 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5997 nparents, author, timestamp, committer, timestamp, logmsg, repo);
5998 if (logmsg != NULL)
5999 free(logmsg);
6000 if (err)
6001 goto done;
6003 /* Check if a concurrent commit to our branch has occurred. */
6004 head_ref_name = got_worktree_get_head_ref_name(worktree);
6005 if (head_ref_name == NULL) {
6006 err = got_error_from_errno("got_worktree_get_head_ref_name");
6007 goto done;
6009 /* Lock the reference here to prevent concurrent modification. */
6010 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6011 if (err)
6012 goto done;
6013 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6014 if (err)
6015 goto done;
6016 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6017 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6018 goto done;
6020 /* Update branch head in repository. */
6021 err = got_ref_change_ref(head_ref2, *new_commit_id);
6022 if (err)
6023 goto done;
6024 err = got_ref_write(head_ref2, repo);
6025 if (err)
6026 goto done;
6028 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6029 if (err)
6030 goto done;
6032 err = ref_base_commit(worktree, repo);
6033 if (err)
6034 goto done;
6035 done:
6036 got_object_id_queue_free(&parent_ids);
6037 if (head_tree)
6038 got_object_tree_close(head_tree);
6039 if (head_commit)
6040 got_object_commit_close(head_commit);
6041 free(head_commit_id2);
6042 if (head_ref2) {
6043 unlockerr = got_ref_unlock(head_ref2);
6044 if (unlockerr && err == NULL)
6045 err = unlockerr;
6046 got_ref_close(head_ref2);
6048 return err;
6051 static const struct got_error *
6052 check_path_is_commitable(const char *path,
6053 struct got_pathlist_head *commitable_paths)
6055 struct got_pathlist_entry *cpe = NULL;
6056 size_t path_len = strlen(path);
6058 TAILQ_FOREACH(cpe, commitable_paths, entry) {
6059 struct got_commitable *ct = cpe->data;
6060 const char *ct_path = ct->path;
6062 while (ct_path[0] == '/')
6063 ct_path++;
6065 if (strcmp(path, ct_path) == 0 ||
6066 got_path_is_child(ct_path, path, path_len))
6067 break;
6070 if (cpe == NULL)
6071 return got_error_path(path, GOT_ERR_BAD_PATH);
6073 return NULL;
6076 static const struct got_error *
6077 check_staged_file(void *arg, struct got_fileindex_entry *ie)
6079 int *have_staged_files = arg;
6081 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6082 *have_staged_files = 1;
6083 return got_error(GOT_ERR_CANCELLED);
6086 return NULL;
6089 static const struct got_error *
6090 check_non_staged_files(struct got_fileindex *fileindex,
6091 struct got_pathlist_head *paths)
6093 struct got_pathlist_entry *pe;
6094 struct got_fileindex_entry *ie;
6096 TAILQ_FOREACH(pe, paths, entry) {
6097 if (pe->path[0] == '\0')
6098 continue;
6099 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6100 if (ie == NULL)
6101 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6102 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6103 return got_error_path(pe->path,
6104 GOT_ERR_FILE_NOT_STAGED);
6107 return NULL;
6110 const struct got_error *
6111 got_worktree_commit(struct got_object_id **new_commit_id,
6112 struct got_worktree *worktree, struct got_pathlist_head *paths,
6113 const char *author, const char *committer, int allow_bad_symlinks,
6114 int show_diff, got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6115 got_worktree_status_cb status_cb, void *status_arg,
6116 struct got_repository *repo)
6118 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6119 struct got_fileindex *fileindex = NULL;
6120 char *fileindex_path = NULL;
6121 struct got_pathlist_head commitable_paths;
6122 struct collect_commitables_arg cc_arg;
6123 struct got_pathlist_entry *pe;
6124 struct got_reference *head_ref = NULL;
6125 struct got_object_id *head_commit_id = NULL;
6126 char *diff_path = NULL;
6127 int have_staged_files = 0;
6129 *new_commit_id = NULL;
6131 memset(&cc_arg, 0, sizeof(cc_arg));
6132 TAILQ_INIT(&commitable_paths);
6134 err = lock_worktree(worktree, LOCK_EX);
6135 if (err)
6136 goto done;
6138 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6139 if (err)
6140 goto done;
6142 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6143 if (err)
6144 goto done;
6146 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6147 if (err)
6148 goto done;
6150 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6151 &have_staged_files);
6152 if (err && err->code != GOT_ERR_CANCELLED)
6153 goto done;
6154 if (have_staged_files) {
6155 err = check_non_staged_files(fileindex, paths);
6156 if (err)
6157 goto done;
6160 cc_arg.commitable_paths = &commitable_paths;
6161 cc_arg.worktree = worktree;
6162 cc_arg.fileindex = fileindex;
6163 cc_arg.repo = repo;
6164 cc_arg.have_staged_files = have_staged_files;
6165 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6166 cc_arg.diff_header_shown = 0;
6167 if (show_diff) {
6168 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6169 GOT_TMPDIR_STR "/got", ".diff");
6170 if (err)
6171 goto done;
6172 cc_arg.f1 = got_opentemp();
6173 if (cc_arg.f1 == NULL) {
6174 err = got_error_from_errno("got_opentemp");
6175 goto done;
6177 cc_arg.f2 = got_opentemp();
6178 if (cc_arg.f2 == NULL) {
6179 err = got_error_from_errno("got_opentemp");
6180 goto done;
6184 TAILQ_FOREACH(pe, paths, entry) {
6185 err = worktree_status(worktree, pe->path, fileindex, repo,
6186 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6187 if (err)
6188 goto done;
6191 if (show_diff) {
6192 if (fflush(cc_arg.diff_outfile) == EOF) {
6193 err = got_error_from_errno("fflush");
6194 goto done;
6198 if (TAILQ_EMPTY(&commitable_paths)) {
6199 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6200 goto done;
6203 TAILQ_FOREACH(pe, paths, entry) {
6204 err = check_path_is_commitable(pe->path, &commitable_paths);
6205 if (err)
6206 goto done;
6209 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6210 struct got_commitable *ct = pe->data;
6211 const char *ct_path = ct->in_repo_path;
6213 while (ct_path[0] == '/')
6214 ct_path++;
6215 err = check_out_of_date(ct_path, ct->status,
6216 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6217 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6218 if (err)
6219 goto done;
6223 err = commit_worktree(new_commit_id, &commitable_paths,
6224 head_commit_id, NULL, worktree, author, committer,
6225 (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6226 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6227 if (err)
6228 goto done;
6230 err = update_fileindex_after_commit(worktree, &commitable_paths,
6231 *new_commit_id, fileindex, have_staged_files);
6232 sync_err = sync_fileindex(fileindex, fileindex_path);
6233 if (sync_err && err == NULL)
6234 err = sync_err;
6235 done:
6236 if (fileindex)
6237 got_fileindex_free(fileindex);
6238 free(fileindex_path);
6239 unlockerr = lock_worktree(worktree, LOCK_SH);
6240 if (unlockerr && err == NULL)
6241 err = unlockerr;
6242 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6243 struct got_commitable *ct = pe->data;
6245 free_commitable(ct);
6247 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6248 if (diff_path && unlink(diff_path) == -1 && err == NULL)
6249 err = got_error_from_errno2("unlink", diff_path);
6250 free(diff_path);
6251 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6252 err == NULL)
6253 err = got_error_from_errno("fclose");
6254 return err;
6257 const char *
6258 got_commitable_get_path(struct got_commitable *ct)
6260 return ct->path;
6263 unsigned int
6264 got_commitable_get_status(struct got_commitable *ct)
6266 return ct->status;
6269 struct check_rebase_ok_arg {
6270 struct got_worktree *worktree;
6271 struct got_repository *repo;
6274 static const struct got_error *
6275 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6277 const struct got_error *err = NULL;
6278 struct check_rebase_ok_arg *a = arg;
6279 unsigned char status;
6280 struct stat sb;
6281 char *ondisk_path;
6283 /* Reject rebase of a work tree with mixed base commits. */
6284 if (memcmp(ie->commit_hash, a->worktree->base_commit_id->hash,
6285 SHA1_DIGEST_LENGTH))
6286 return got_error(GOT_ERR_MIXED_COMMITS);
6288 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6289 == -1)
6290 return got_error_from_errno("asprintf");
6292 /* Reject rebase of a work tree with modified or staged files. */
6293 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6294 free(ondisk_path);
6295 if (err)
6296 return err;
6298 if (status != GOT_STATUS_NO_CHANGE)
6299 return got_error(GOT_ERR_MODIFIED);
6300 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6301 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6303 return NULL;
6306 const struct got_error *
6307 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6308 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6309 struct got_worktree *worktree, struct got_reference *branch,
6310 struct got_repository *repo)
6312 const struct got_error *err = NULL;
6313 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6314 char *branch_ref_name = NULL;
6315 char *fileindex_path = NULL;
6316 struct check_rebase_ok_arg ok_arg;
6317 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6318 struct got_object_id *wt_branch_tip = NULL;
6320 *new_base_branch_ref = NULL;
6321 *tmp_branch = NULL;
6322 *fileindex = NULL;
6324 err = lock_worktree(worktree, LOCK_EX);
6325 if (err)
6326 return err;
6328 err = open_fileindex(fileindex, &fileindex_path, worktree);
6329 if (err)
6330 goto done;
6332 ok_arg.worktree = worktree;
6333 ok_arg.repo = repo;
6334 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6335 &ok_arg);
6336 if (err)
6337 goto done;
6339 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6340 if (err)
6341 goto done;
6343 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6344 if (err)
6345 goto done;
6347 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6348 if (err)
6349 goto done;
6351 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6352 0);
6353 if (err)
6354 goto done;
6356 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6357 if (err)
6358 goto done;
6359 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6360 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6361 goto done;
6364 err = got_ref_alloc_symref(new_base_branch_ref,
6365 new_base_branch_ref_name, wt_branch);
6366 if (err)
6367 goto done;
6368 err = got_ref_write(*new_base_branch_ref, repo);
6369 if (err)
6370 goto done;
6372 /* TODO Lock original branch's ref while rebasing? */
6374 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6375 if (err)
6376 goto done;
6378 err = got_ref_write(branch_ref, repo);
6379 if (err)
6380 goto done;
6382 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6383 worktree->base_commit_id);
6384 if (err)
6385 goto done;
6386 err = got_ref_write(*tmp_branch, repo);
6387 if (err)
6388 goto done;
6390 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6391 if (err)
6392 goto done;
6393 done:
6394 free(fileindex_path);
6395 free(tmp_branch_name);
6396 free(new_base_branch_ref_name);
6397 free(branch_ref_name);
6398 if (branch_ref)
6399 got_ref_close(branch_ref);
6400 if (wt_branch)
6401 got_ref_close(wt_branch);
6402 free(wt_branch_tip);
6403 if (err) {
6404 if (*new_base_branch_ref) {
6405 got_ref_close(*new_base_branch_ref);
6406 *new_base_branch_ref = NULL;
6408 if (*tmp_branch) {
6409 got_ref_close(*tmp_branch);
6410 *tmp_branch = NULL;
6412 if (*fileindex) {
6413 got_fileindex_free(*fileindex);
6414 *fileindex = NULL;
6416 lock_worktree(worktree, LOCK_SH);
6418 return err;
6421 const struct got_error *
6422 got_worktree_rebase_continue(struct got_object_id **commit_id,
6423 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6424 struct got_reference **branch, struct got_fileindex **fileindex,
6425 struct got_worktree *worktree, struct got_repository *repo)
6427 const struct got_error *err;
6428 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6429 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6430 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6431 char *fileindex_path = NULL;
6432 int have_staged_files = 0;
6434 *commit_id = NULL;
6435 *new_base_branch = NULL;
6436 *tmp_branch = NULL;
6437 *branch = NULL;
6438 *fileindex = NULL;
6440 err = lock_worktree(worktree, LOCK_EX);
6441 if (err)
6442 return err;
6444 err = open_fileindex(fileindex, &fileindex_path, worktree);
6445 if (err)
6446 goto done;
6448 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6449 &have_staged_files);
6450 if (err && err->code != GOT_ERR_CANCELLED)
6451 goto done;
6452 if (have_staged_files) {
6453 err = got_error(GOT_ERR_STAGED_PATHS);
6454 goto done;
6457 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6458 if (err)
6459 goto done;
6461 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6462 if (err)
6463 goto done;
6465 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6466 if (err)
6467 goto done;
6469 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6470 if (err)
6471 goto done;
6473 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6474 if (err)
6475 goto done;
6477 err = got_ref_open(branch, repo,
6478 got_ref_get_symref_target(branch_ref), 0);
6479 if (err)
6480 goto done;
6482 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6483 if (err)
6484 goto done;
6486 err = got_ref_resolve(commit_id, repo, commit_ref);
6487 if (err)
6488 goto done;
6490 err = got_ref_open(new_base_branch, repo,
6491 new_base_branch_ref_name, 0);
6492 if (err)
6493 goto done;
6495 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6496 if (err)
6497 goto done;
6498 done:
6499 free(commit_ref_name);
6500 free(branch_ref_name);
6501 free(fileindex_path);
6502 if (commit_ref)
6503 got_ref_close(commit_ref);
6504 if (branch_ref)
6505 got_ref_close(branch_ref);
6506 if (err) {
6507 free(*commit_id);
6508 *commit_id = NULL;
6509 if (*tmp_branch) {
6510 got_ref_close(*tmp_branch);
6511 *tmp_branch = NULL;
6513 if (*new_base_branch) {
6514 got_ref_close(*new_base_branch);
6515 *new_base_branch = NULL;
6517 if (*branch) {
6518 got_ref_close(*branch);
6519 *branch = NULL;
6521 if (*fileindex) {
6522 got_fileindex_free(*fileindex);
6523 *fileindex = NULL;
6525 lock_worktree(worktree, LOCK_SH);
6527 return err;
6530 const struct got_error *
6531 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6533 const struct got_error *err;
6534 char *tmp_branch_name = NULL;
6536 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6537 if (err)
6538 return err;
6540 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6541 free(tmp_branch_name);
6542 return NULL;
6545 static const struct got_error *
6546 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6547 const char *diff_path, char **logmsg, void *arg)
6549 *logmsg = arg;
6550 return NULL;
6553 static const struct got_error *
6554 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6555 const char *path, struct got_object_id *blob_id,
6556 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6557 int dirfd, const char *de_name)
6559 return NULL;
6562 struct collect_merged_paths_arg {
6563 got_worktree_checkout_cb progress_cb;
6564 void *progress_arg;
6565 struct got_pathlist_head *merged_paths;
6568 static const struct got_error *
6569 collect_merged_paths(void *arg, unsigned char status, const char *path)
6571 const struct got_error *err;
6572 struct collect_merged_paths_arg *a = arg;
6573 char *p;
6574 struct got_pathlist_entry *new;
6576 err = (*a->progress_cb)(a->progress_arg, status, path);
6577 if (err)
6578 return err;
6580 if (status != GOT_STATUS_MERGE &&
6581 status != GOT_STATUS_ADD &&
6582 status != GOT_STATUS_DELETE &&
6583 status != GOT_STATUS_CONFLICT)
6584 return NULL;
6586 p = strdup(path);
6587 if (p == NULL)
6588 return got_error_from_errno("strdup");
6590 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6591 if (err || new == NULL)
6592 free(p);
6593 return err;
6596 static const struct got_error *
6597 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6598 int is_rebase, struct got_repository *repo)
6600 const struct got_error *err;
6601 struct got_reference *commit_ref = NULL;
6603 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6604 if (err) {
6605 if (err->code != GOT_ERR_NOT_REF)
6606 goto done;
6607 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6608 if (err)
6609 goto done;
6610 err = got_ref_write(commit_ref, repo);
6611 if (err)
6612 goto done;
6613 } else if (is_rebase) {
6614 struct got_object_id *stored_id;
6615 int cmp;
6617 err = got_ref_resolve(&stored_id, repo, commit_ref);
6618 if (err)
6619 goto done;
6620 cmp = got_object_id_cmp(commit_id, stored_id);
6621 free(stored_id);
6622 if (cmp != 0) {
6623 err = got_error(GOT_ERR_REBASE_COMMITID);
6624 goto done;
6627 done:
6628 if (commit_ref)
6629 got_ref_close(commit_ref);
6630 return err;
6633 static const struct got_error *
6634 rebase_merge_files(struct got_pathlist_head *merged_paths,
6635 const char *commit_ref_name, struct got_worktree *worktree,
6636 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6637 struct got_object_id *commit_id, struct got_repository *repo,
6638 got_worktree_checkout_cb progress_cb, void *progress_arg,
6639 got_cancel_cb cancel_cb, void *cancel_arg)
6641 const struct got_error *err;
6642 struct got_reference *commit_ref = NULL;
6643 struct collect_merged_paths_arg cmp_arg;
6644 char *fileindex_path;
6646 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6648 err = get_fileindex_path(&fileindex_path, worktree);
6649 if (err)
6650 return err;
6652 cmp_arg.progress_cb = progress_cb;
6653 cmp_arg.progress_arg = progress_arg;
6654 cmp_arg.merged_paths = merged_paths;
6655 err = merge_files(worktree, fileindex, fileindex_path,
6656 parent_commit_id, commit_id, repo, collect_merged_paths,
6657 &cmp_arg, cancel_cb, cancel_arg);
6658 if (commit_ref)
6659 got_ref_close(commit_ref);
6660 return err;
6663 const struct got_error *
6664 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6665 struct got_worktree *worktree, struct got_fileindex *fileindex,
6666 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6667 struct got_repository *repo,
6668 got_worktree_checkout_cb progress_cb, void *progress_arg,
6669 got_cancel_cb cancel_cb, void *cancel_arg)
6671 const struct got_error *err;
6672 char *commit_ref_name;
6674 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6675 if (err)
6676 return err;
6678 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6679 if (err)
6680 goto done;
6682 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6683 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6684 progress_arg, cancel_cb, cancel_arg);
6685 done:
6686 free(commit_ref_name);
6687 return err;
6690 const struct got_error *
6691 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6692 struct got_worktree *worktree, struct got_fileindex *fileindex,
6693 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6694 struct got_repository *repo,
6695 got_worktree_checkout_cb progress_cb, void *progress_arg,
6696 got_cancel_cb cancel_cb, void *cancel_arg)
6698 const struct got_error *err;
6699 char *commit_ref_name;
6701 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6702 if (err)
6703 return err;
6705 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6706 if (err)
6707 goto done;
6709 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6710 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6711 progress_arg, cancel_cb, cancel_arg);
6712 done:
6713 free(commit_ref_name);
6714 return err;
6717 static const struct got_error *
6718 rebase_commit(struct got_object_id **new_commit_id,
6719 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6720 struct got_worktree *worktree, struct got_fileindex *fileindex,
6721 struct got_reference *tmp_branch, const char *committer,
6722 struct got_commit_object *orig_commit, const char *new_logmsg,
6723 struct got_repository *repo)
6725 const struct got_error *err, *sync_err;
6726 struct got_pathlist_head commitable_paths;
6727 struct collect_commitables_arg cc_arg;
6728 char *fileindex_path = NULL;
6729 struct got_reference *head_ref = NULL;
6730 struct got_object_id *head_commit_id = NULL;
6731 char *logmsg = NULL;
6733 memset(&cc_arg, 0, sizeof(cc_arg));
6734 TAILQ_INIT(&commitable_paths);
6735 *new_commit_id = NULL;
6737 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6739 err = get_fileindex_path(&fileindex_path, worktree);
6740 if (err)
6741 return err;
6743 cc_arg.commitable_paths = &commitable_paths;
6744 cc_arg.worktree = worktree;
6745 cc_arg.repo = repo;
6746 cc_arg.have_staged_files = 0;
6748 * If possible get the status of individual files directly to
6749 * avoid crawling the entire work tree once per rebased commit.
6751 * Ideally, merged_paths would contain a list of commitables
6752 * we could use so we could skip worktree_status() entirely.
6753 * However, we would then need carefully keep track of cumulative
6754 * effects of operations such as file additions and deletions
6755 * in 'got histedit -f' (folding multiple commits into one),
6756 * and this extra complexity is not really worth it.
6758 if (merged_paths) {
6759 struct got_pathlist_entry *pe;
6760 TAILQ_FOREACH(pe, merged_paths, entry) {
6761 err = worktree_status(worktree, pe->path, fileindex,
6762 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6763 0);
6764 if (err)
6765 goto done;
6767 } else {
6768 err = worktree_status(worktree, "", fileindex, repo,
6769 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6770 if (err)
6771 goto done;
6774 if (TAILQ_EMPTY(&commitable_paths)) {
6775 /* No-op change; commit will be elided. */
6776 err = got_ref_delete(commit_ref, repo);
6777 if (err)
6778 goto done;
6779 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6780 goto done;
6783 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6784 if (err)
6785 goto done;
6787 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6788 if (err)
6789 goto done;
6791 if (new_logmsg) {
6792 logmsg = strdup(new_logmsg);
6793 if (logmsg == NULL) {
6794 err = got_error_from_errno("strdup");
6795 goto done;
6797 } else {
6798 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6799 if (err)
6800 goto done;
6803 /* NB: commit_worktree will call free(logmsg) */
6804 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6805 NULL, worktree, got_object_commit_get_author(orig_commit),
6806 committer ? committer :
6807 got_object_commit_get_committer(orig_commit), NULL,
6808 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6809 if (err)
6810 goto done;
6812 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6813 if (err)
6814 goto done;
6816 err = got_ref_delete(commit_ref, repo);
6817 if (err)
6818 goto done;
6820 err = update_fileindex_after_commit(worktree, &commitable_paths,
6821 *new_commit_id, fileindex, 0);
6822 sync_err = sync_fileindex(fileindex, fileindex_path);
6823 if (sync_err && err == NULL)
6824 err = sync_err;
6825 done:
6826 free(fileindex_path);
6827 free(head_commit_id);
6828 if (head_ref)
6829 got_ref_close(head_ref);
6830 if (err) {
6831 free(*new_commit_id);
6832 *new_commit_id = NULL;
6834 return err;
6837 const struct got_error *
6838 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6839 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6840 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6841 const char *committer, struct got_commit_object *orig_commit,
6842 struct got_object_id *orig_commit_id, struct got_repository *repo)
6844 const struct got_error *err;
6845 char *commit_ref_name;
6846 struct got_reference *commit_ref = NULL;
6847 struct got_object_id *commit_id = NULL;
6849 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6850 if (err)
6851 return err;
6853 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6854 if (err)
6855 goto done;
6856 err = got_ref_resolve(&commit_id, repo, commit_ref);
6857 if (err)
6858 goto done;
6859 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6860 err = got_error(GOT_ERR_REBASE_COMMITID);
6861 goto done;
6864 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6865 worktree, fileindex, tmp_branch, committer, orig_commit,
6866 NULL, repo);
6867 done:
6868 if (commit_ref)
6869 got_ref_close(commit_ref);
6870 free(commit_ref_name);
6871 free(commit_id);
6872 return err;
6875 const struct got_error *
6876 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6877 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6878 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6879 const char *committer, struct got_commit_object *orig_commit,
6880 struct got_object_id *orig_commit_id, const char *new_logmsg,
6881 struct got_repository *repo)
6883 const struct got_error *err;
6884 char *commit_ref_name;
6885 struct got_reference *commit_ref = NULL;
6887 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6888 if (err)
6889 return err;
6891 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6892 if (err)
6893 goto done;
6895 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6896 worktree, fileindex, tmp_branch, committer, orig_commit,
6897 new_logmsg, repo);
6898 done:
6899 if (commit_ref)
6900 got_ref_close(commit_ref);
6901 free(commit_ref_name);
6902 return err;
6905 const struct got_error *
6906 got_worktree_rebase_postpone(struct got_worktree *worktree,
6907 struct got_fileindex *fileindex)
6909 if (fileindex)
6910 got_fileindex_free(fileindex);
6911 return lock_worktree(worktree, LOCK_SH);
6914 static const struct got_error *
6915 delete_ref(const char *name, struct got_repository *repo)
6917 const struct got_error *err;
6918 struct got_reference *ref;
6920 err = got_ref_open(&ref, repo, name, 0);
6921 if (err) {
6922 if (err->code == GOT_ERR_NOT_REF)
6923 return NULL;
6924 return err;
6927 err = got_ref_delete(ref, repo);
6928 got_ref_close(ref);
6929 return err;
6932 static const struct got_error *
6933 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6935 const struct got_error *err;
6936 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6937 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6939 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6940 if (err)
6941 goto done;
6942 err = delete_ref(tmp_branch_name, repo);
6943 if (err)
6944 goto done;
6946 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6947 if (err)
6948 goto done;
6949 err = delete_ref(new_base_branch_ref_name, repo);
6950 if (err)
6951 goto done;
6953 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6954 if (err)
6955 goto done;
6956 err = delete_ref(branch_ref_name, repo);
6957 if (err)
6958 goto done;
6960 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6961 if (err)
6962 goto done;
6963 err = delete_ref(commit_ref_name, repo);
6964 if (err)
6965 goto done;
6967 done:
6968 free(tmp_branch_name);
6969 free(new_base_branch_ref_name);
6970 free(branch_ref_name);
6971 free(commit_ref_name);
6972 return err;
6975 static const struct got_error *
6976 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6977 struct got_object_id *new_commit_id, struct got_repository *repo)
6979 const struct got_error *err;
6980 struct got_reference *ref = NULL;
6981 struct got_object_id *old_commit_id = NULL;
6982 const char *branch_name = NULL;
6983 char *new_id_str = NULL;
6984 char *refname = NULL;
6986 branch_name = got_ref_get_name(branch);
6987 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6988 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6989 branch_name += 11;
6991 err = got_object_id_str(&new_id_str, new_commit_id);
6992 if (err)
6993 return err;
6995 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6996 new_id_str) == -1) {
6997 err = got_error_from_errno("asprintf");
6998 goto done;
7001 err = got_ref_resolve(&old_commit_id, repo, branch);
7002 if (err)
7003 goto done;
7005 err = got_ref_alloc(&ref, refname, old_commit_id);
7006 if (err)
7007 goto done;
7009 err = got_ref_write(ref, repo);
7010 done:
7011 free(new_id_str);
7012 free(refname);
7013 free(old_commit_id);
7014 if (ref)
7015 got_ref_close(ref);
7016 return err;
7019 const struct got_error *
7020 got_worktree_rebase_complete(struct got_worktree *worktree,
7021 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7022 struct got_reference *rebased_branch, struct got_repository *repo,
7023 int create_backup)
7025 const struct got_error *err, *unlockerr, *sync_err;
7026 struct got_object_id *new_head_commit_id = NULL;
7027 char *fileindex_path = NULL;
7029 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7030 if (err)
7031 return err;
7033 if (create_backup) {
7034 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7035 rebased_branch, new_head_commit_id, repo);
7036 if (err)
7037 goto done;
7040 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7041 if (err)
7042 goto done;
7044 err = got_ref_write(rebased_branch, repo);
7045 if (err)
7046 goto done;
7048 err = got_worktree_set_head_ref(worktree, rebased_branch);
7049 if (err)
7050 goto done;
7052 err = delete_rebase_refs(worktree, repo);
7053 if (err)
7054 goto done;
7056 err = get_fileindex_path(&fileindex_path, worktree);
7057 if (err)
7058 goto done;
7059 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7060 sync_err = sync_fileindex(fileindex, fileindex_path);
7061 if (sync_err && err == NULL)
7062 err = sync_err;
7063 done:
7064 got_fileindex_free(fileindex);
7065 free(fileindex_path);
7066 free(new_head_commit_id);
7067 unlockerr = lock_worktree(worktree, LOCK_SH);
7068 if (unlockerr && err == NULL)
7069 err = unlockerr;
7070 return err;
7073 const struct got_error *
7074 got_worktree_rebase_abort(struct got_worktree *worktree,
7075 struct got_fileindex *fileindex, struct got_repository *repo,
7076 struct got_reference *new_base_branch,
7077 got_worktree_checkout_cb progress_cb, void *progress_arg)
7079 const struct got_error *err, *unlockerr, *sync_err;
7080 struct got_reference *resolved = NULL;
7081 struct got_object_id *commit_id = NULL;
7082 struct got_commit_object *commit = NULL;
7083 char *fileindex_path = NULL;
7084 struct revert_file_args rfa;
7085 struct got_object_id *tree_id = NULL;
7087 err = lock_worktree(worktree, LOCK_EX);
7088 if (err)
7089 return err;
7091 err = got_object_open_as_commit(&commit, repo,
7092 worktree->base_commit_id);
7093 if (err)
7094 goto done;
7096 err = got_ref_open(&resolved, repo,
7097 got_ref_get_symref_target(new_base_branch), 0);
7098 if (err)
7099 goto done;
7101 err = got_worktree_set_head_ref(worktree, resolved);
7102 if (err)
7103 goto done;
7106 * XXX commits to the base branch could have happened while
7107 * we were busy rebasing; should we store the original commit ID
7108 * when rebase begins and read it back here?
7110 err = got_ref_resolve(&commit_id, repo, resolved);
7111 if (err)
7112 goto done;
7114 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7115 if (err)
7116 goto done;
7118 err = got_object_id_by_path(&tree_id, repo, commit,
7119 worktree->path_prefix);
7120 if (err)
7121 goto done;
7123 err = delete_rebase_refs(worktree, repo);
7124 if (err)
7125 goto done;
7127 err = get_fileindex_path(&fileindex_path, worktree);
7128 if (err)
7129 goto done;
7131 rfa.worktree = worktree;
7132 rfa.fileindex = fileindex;
7133 rfa.progress_cb = progress_cb;
7134 rfa.progress_arg = progress_arg;
7135 rfa.patch_cb = NULL;
7136 rfa.patch_arg = NULL;
7137 rfa.repo = repo;
7138 rfa.unlink_added_files = 0;
7139 err = worktree_status(worktree, "", fileindex, repo,
7140 revert_file, &rfa, NULL, NULL, 1, 0);
7141 if (err)
7142 goto sync;
7144 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7145 repo, progress_cb, progress_arg, NULL, NULL);
7146 sync:
7147 sync_err = sync_fileindex(fileindex, fileindex_path);
7148 if (sync_err && err == NULL)
7149 err = sync_err;
7150 done:
7151 got_ref_close(resolved);
7152 free(tree_id);
7153 free(commit_id);
7154 if (commit)
7155 got_object_commit_close(commit);
7156 if (fileindex)
7157 got_fileindex_free(fileindex);
7158 free(fileindex_path);
7160 unlockerr = lock_worktree(worktree, LOCK_SH);
7161 if (unlockerr && err == NULL)
7162 err = unlockerr;
7163 return err;
7166 const struct got_error *
7167 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7168 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7169 struct got_fileindex **fileindex, struct got_worktree *worktree,
7170 struct got_repository *repo)
7172 const struct got_error *err = NULL;
7173 char *tmp_branch_name = NULL;
7174 char *branch_ref_name = NULL;
7175 char *base_commit_ref_name = NULL;
7176 char *fileindex_path = NULL;
7177 struct check_rebase_ok_arg ok_arg;
7178 struct got_reference *wt_branch = NULL;
7179 struct got_reference *base_commit_ref = NULL;
7181 *tmp_branch = NULL;
7182 *branch_ref = NULL;
7183 *base_commit_id = NULL;
7184 *fileindex = NULL;
7186 err = lock_worktree(worktree, LOCK_EX);
7187 if (err)
7188 return err;
7190 err = open_fileindex(fileindex, &fileindex_path, worktree);
7191 if (err)
7192 goto done;
7194 ok_arg.worktree = worktree;
7195 ok_arg.repo = repo;
7196 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7197 &ok_arg);
7198 if (err)
7199 goto done;
7201 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7202 if (err)
7203 goto done;
7205 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7206 if (err)
7207 goto done;
7209 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7210 worktree);
7211 if (err)
7212 goto done;
7214 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7215 0);
7216 if (err)
7217 goto done;
7219 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7220 if (err)
7221 goto done;
7223 err = got_ref_write(*branch_ref, repo);
7224 if (err)
7225 goto done;
7227 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7228 worktree->base_commit_id);
7229 if (err)
7230 goto done;
7231 err = got_ref_write(base_commit_ref, repo);
7232 if (err)
7233 goto done;
7234 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7235 if (*base_commit_id == NULL) {
7236 err = got_error_from_errno("got_object_id_dup");
7237 goto done;
7240 err = got_ref_alloc(tmp_branch, tmp_branch_name,
7241 worktree->base_commit_id);
7242 if (err)
7243 goto done;
7244 err = got_ref_write(*tmp_branch, repo);
7245 if (err)
7246 goto done;
7248 err = got_worktree_set_head_ref(worktree, *tmp_branch);
7249 if (err)
7250 goto done;
7251 done:
7252 free(fileindex_path);
7253 free(tmp_branch_name);
7254 free(branch_ref_name);
7255 free(base_commit_ref_name);
7256 if (wt_branch)
7257 got_ref_close(wt_branch);
7258 if (err) {
7259 if (*branch_ref) {
7260 got_ref_close(*branch_ref);
7261 *branch_ref = NULL;
7263 if (*tmp_branch) {
7264 got_ref_close(*tmp_branch);
7265 *tmp_branch = NULL;
7267 free(*base_commit_id);
7268 if (*fileindex) {
7269 got_fileindex_free(*fileindex);
7270 *fileindex = NULL;
7272 lock_worktree(worktree, LOCK_SH);
7274 return err;
7277 const struct got_error *
7278 got_worktree_histedit_postpone(struct got_worktree *worktree,
7279 struct got_fileindex *fileindex)
7281 if (fileindex)
7282 got_fileindex_free(fileindex);
7283 return lock_worktree(worktree, LOCK_SH);
7286 const struct got_error *
7287 got_worktree_histedit_in_progress(int *in_progress,
7288 struct got_worktree *worktree)
7290 const struct got_error *err;
7291 char *tmp_branch_name = NULL;
7293 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7294 if (err)
7295 return err;
7297 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7298 free(tmp_branch_name);
7299 return NULL;
7302 const struct got_error *
7303 got_worktree_histedit_continue(struct got_object_id **commit_id,
7304 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7305 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7306 struct got_worktree *worktree, struct got_repository *repo)
7308 const struct got_error *err;
7309 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7310 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7311 struct got_reference *commit_ref = NULL;
7312 struct got_reference *base_commit_ref = NULL;
7313 char *fileindex_path = NULL;
7314 int have_staged_files = 0;
7316 *commit_id = NULL;
7317 *tmp_branch = NULL;
7318 *base_commit_id = NULL;
7319 *fileindex = NULL;
7321 err = lock_worktree(worktree, LOCK_EX);
7322 if (err)
7323 return err;
7325 err = open_fileindex(fileindex, &fileindex_path, worktree);
7326 if (err)
7327 goto done;
7329 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7330 &have_staged_files);
7331 if (err && err->code != GOT_ERR_CANCELLED)
7332 goto done;
7333 if (have_staged_files) {
7334 err = got_error(GOT_ERR_STAGED_PATHS);
7335 goto done;
7338 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7339 if (err)
7340 goto done;
7342 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7343 if (err)
7344 goto done;
7346 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7347 if (err)
7348 goto done;
7350 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7351 worktree);
7352 if (err)
7353 goto done;
7355 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7356 if (err)
7357 goto done;
7359 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7360 if (err)
7361 goto done;
7362 err = got_ref_resolve(commit_id, repo, commit_ref);
7363 if (err)
7364 goto done;
7366 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7367 if (err)
7368 goto done;
7369 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7370 if (err)
7371 goto done;
7373 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7374 if (err)
7375 goto done;
7376 done:
7377 free(commit_ref_name);
7378 free(branch_ref_name);
7379 free(fileindex_path);
7380 if (commit_ref)
7381 got_ref_close(commit_ref);
7382 if (base_commit_ref)
7383 got_ref_close(base_commit_ref);
7384 if (err) {
7385 free(*commit_id);
7386 *commit_id = NULL;
7387 free(*base_commit_id);
7388 *base_commit_id = NULL;
7389 if (*tmp_branch) {
7390 got_ref_close(*tmp_branch);
7391 *tmp_branch = NULL;
7393 if (*fileindex) {
7394 got_fileindex_free(*fileindex);
7395 *fileindex = NULL;
7397 lock_worktree(worktree, LOCK_EX);
7399 return err;
7402 static const struct got_error *
7403 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7405 const struct got_error *err;
7406 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7407 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7409 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7410 if (err)
7411 goto done;
7412 err = delete_ref(tmp_branch_name, repo);
7413 if (err)
7414 goto done;
7416 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7417 worktree);
7418 if (err)
7419 goto done;
7420 err = delete_ref(base_commit_ref_name, repo);
7421 if (err)
7422 goto done;
7424 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7425 if (err)
7426 goto done;
7427 err = delete_ref(branch_ref_name, repo);
7428 if (err)
7429 goto done;
7431 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7432 if (err)
7433 goto done;
7434 err = delete_ref(commit_ref_name, repo);
7435 if (err)
7436 goto done;
7437 done:
7438 free(tmp_branch_name);
7439 free(base_commit_ref_name);
7440 free(branch_ref_name);
7441 free(commit_ref_name);
7442 return err;
7445 const struct got_error *
7446 got_worktree_histedit_abort(struct got_worktree *worktree,
7447 struct got_fileindex *fileindex, struct got_repository *repo,
7448 struct got_reference *branch, struct got_object_id *base_commit_id,
7449 got_worktree_checkout_cb progress_cb, void *progress_arg)
7451 const struct got_error *err, *unlockerr, *sync_err;
7452 struct got_reference *resolved = NULL;
7453 char *fileindex_path = NULL;
7454 struct got_commit_object *commit = NULL;
7455 struct got_object_id *tree_id = NULL;
7456 struct revert_file_args rfa;
7458 err = lock_worktree(worktree, LOCK_EX);
7459 if (err)
7460 return err;
7462 err = got_object_open_as_commit(&commit, repo,
7463 worktree->base_commit_id);
7464 if (err)
7465 goto done;
7467 err = got_ref_open(&resolved, repo,
7468 got_ref_get_symref_target(branch), 0);
7469 if (err)
7470 goto done;
7472 err = got_worktree_set_head_ref(worktree, resolved);
7473 if (err)
7474 goto done;
7476 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7477 if (err)
7478 goto done;
7480 err = got_object_id_by_path(&tree_id, repo, commit,
7481 worktree->path_prefix);
7482 if (err)
7483 goto done;
7485 err = delete_histedit_refs(worktree, repo);
7486 if (err)
7487 goto done;
7489 err = get_fileindex_path(&fileindex_path, worktree);
7490 if (err)
7491 goto done;
7493 rfa.worktree = worktree;
7494 rfa.fileindex = fileindex;
7495 rfa.progress_cb = progress_cb;
7496 rfa.progress_arg = progress_arg;
7497 rfa.patch_cb = NULL;
7498 rfa.patch_arg = NULL;
7499 rfa.repo = repo;
7500 rfa.unlink_added_files = 0;
7501 err = worktree_status(worktree, "", fileindex, repo,
7502 revert_file, &rfa, NULL, NULL, 1, 0);
7503 if (err)
7504 goto sync;
7506 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7507 repo, progress_cb, progress_arg, NULL, NULL);
7508 sync:
7509 sync_err = sync_fileindex(fileindex, fileindex_path);
7510 if (sync_err && err == NULL)
7511 err = sync_err;
7512 done:
7513 got_ref_close(resolved);
7514 free(tree_id);
7515 free(fileindex_path);
7517 unlockerr = lock_worktree(worktree, LOCK_SH);
7518 if (unlockerr && err == NULL)
7519 err = unlockerr;
7520 return err;
7523 const struct got_error *
7524 got_worktree_histedit_complete(struct got_worktree *worktree,
7525 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7526 struct got_reference *edited_branch, struct got_repository *repo)
7528 const struct got_error *err, *unlockerr, *sync_err;
7529 struct got_object_id *new_head_commit_id = NULL;
7530 struct got_reference *resolved = NULL;
7531 char *fileindex_path = NULL;
7533 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7534 if (err)
7535 return err;
7537 err = got_ref_open(&resolved, repo,
7538 got_ref_get_symref_target(edited_branch), 0);
7539 if (err)
7540 goto done;
7542 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7543 resolved, new_head_commit_id, repo);
7544 if (err)
7545 goto done;
7547 err = got_ref_change_ref(resolved, new_head_commit_id);
7548 if (err)
7549 goto done;
7551 err = got_ref_write(resolved, repo);
7552 if (err)
7553 goto done;
7555 err = got_worktree_set_head_ref(worktree, resolved);
7556 if (err)
7557 goto done;
7559 err = delete_histedit_refs(worktree, repo);
7560 if (err)
7561 goto done;
7563 err = get_fileindex_path(&fileindex_path, worktree);
7564 if (err)
7565 goto done;
7566 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7567 sync_err = sync_fileindex(fileindex, fileindex_path);
7568 if (sync_err && err == NULL)
7569 err = sync_err;
7570 done:
7571 got_fileindex_free(fileindex);
7572 free(fileindex_path);
7573 free(new_head_commit_id);
7574 unlockerr = lock_worktree(worktree, LOCK_SH);
7575 if (unlockerr && err == NULL)
7576 err = unlockerr;
7577 return err;
7580 const struct got_error *
7581 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7582 struct got_object_id *commit_id, struct got_repository *repo)
7584 const struct got_error *err;
7585 char *commit_ref_name;
7587 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7588 if (err)
7589 return err;
7591 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7592 if (err)
7593 goto done;
7595 err = delete_ref(commit_ref_name, repo);
7596 done:
7597 free(commit_ref_name);
7598 return err;
7601 const struct got_error *
7602 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7603 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7604 struct got_worktree *worktree, const char *refname,
7605 struct got_repository *repo)
7607 const struct got_error *err = NULL;
7608 char *fileindex_path = NULL;
7609 struct check_rebase_ok_arg ok_arg;
7611 *fileindex = NULL;
7612 *branch_ref = NULL;
7613 *base_branch_ref = NULL;
7615 err = lock_worktree(worktree, LOCK_EX);
7616 if (err)
7617 return err;
7619 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7620 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7621 "cannot integrate a branch into itself; "
7622 "update -b or different branch name required");
7623 goto done;
7626 err = open_fileindex(fileindex, &fileindex_path, worktree);
7627 if (err)
7628 goto done;
7630 /* Preconditions are the same as for rebase. */
7631 ok_arg.worktree = worktree;
7632 ok_arg.repo = repo;
7633 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7634 &ok_arg);
7635 if (err)
7636 goto done;
7638 err = got_ref_open(branch_ref, repo, refname, 1);
7639 if (err)
7640 goto done;
7642 err = got_ref_open(base_branch_ref, repo,
7643 got_worktree_get_head_ref_name(worktree), 1);
7644 done:
7645 if (err) {
7646 if (*branch_ref) {
7647 got_ref_close(*branch_ref);
7648 *branch_ref = NULL;
7650 if (*base_branch_ref) {
7651 got_ref_close(*base_branch_ref);
7652 *base_branch_ref = NULL;
7654 if (*fileindex) {
7655 got_fileindex_free(*fileindex);
7656 *fileindex = NULL;
7658 lock_worktree(worktree, LOCK_SH);
7660 return err;
7663 const struct got_error *
7664 got_worktree_integrate_continue(struct got_worktree *worktree,
7665 struct got_fileindex *fileindex, struct got_repository *repo,
7666 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7667 got_worktree_checkout_cb progress_cb, void *progress_arg,
7668 got_cancel_cb cancel_cb, void *cancel_arg)
7670 const struct got_error *err = NULL, *sync_err, *unlockerr;
7671 char *fileindex_path = NULL;
7672 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7673 struct got_commit_object *commit = NULL;
7675 err = get_fileindex_path(&fileindex_path, worktree);
7676 if (err)
7677 goto done;
7679 err = got_ref_resolve(&commit_id, repo, branch_ref);
7680 if (err)
7681 goto done;
7683 err = got_object_open_as_commit(&commit, repo, commit_id);
7684 if (err)
7685 goto done;
7687 err = got_object_id_by_path(&tree_id, repo, commit,
7688 worktree->path_prefix);
7689 if (err)
7690 goto done;
7692 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7693 if (err)
7694 goto done;
7696 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7697 progress_cb, progress_arg, cancel_cb, cancel_arg);
7698 if (err)
7699 goto sync;
7701 err = got_ref_change_ref(base_branch_ref, commit_id);
7702 if (err)
7703 goto sync;
7705 err = got_ref_write(base_branch_ref, repo);
7706 if (err)
7707 goto sync;
7709 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7710 sync:
7711 sync_err = sync_fileindex(fileindex, fileindex_path);
7712 if (sync_err && err == NULL)
7713 err = sync_err;
7715 done:
7716 unlockerr = got_ref_unlock(branch_ref);
7717 if (unlockerr && err == NULL)
7718 err = unlockerr;
7719 got_ref_close(branch_ref);
7721 unlockerr = got_ref_unlock(base_branch_ref);
7722 if (unlockerr && err == NULL)
7723 err = unlockerr;
7724 got_ref_close(base_branch_ref);
7726 got_fileindex_free(fileindex);
7727 free(fileindex_path);
7728 free(tree_id);
7729 if (commit)
7730 got_object_commit_close(commit);
7732 unlockerr = lock_worktree(worktree, LOCK_SH);
7733 if (unlockerr && err == NULL)
7734 err = unlockerr;
7735 return err;
7738 const struct got_error *
7739 got_worktree_integrate_abort(struct got_worktree *worktree,
7740 struct got_fileindex *fileindex, struct got_repository *repo,
7741 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7743 const struct got_error *err = NULL, *unlockerr = NULL;
7745 got_fileindex_free(fileindex);
7747 err = lock_worktree(worktree, LOCK_SH);
7749 unlockerr = got_ref_unlock(branch_ref);
7750 if (unlockerr && err == NULL)
7751 err = unlockerr;
7752 got_ref_close(branch_ref);
7754 unlockerr = got_ref_unlock(base_branch_ref);
7755 if (unlockerr && err == NULL)
7756 err = unlockerr;
7757 got_ref_close(base_branch_ref);
7759 return err;
7762 const struct got_error *
7763 got_worktree_merge_postpone(struct got_worktree *worktree,
7764 struct got_fileindex *fileindex)
7766 const struct got_error *err, *sync_err;
7767 char *fileindex_path = NULL;
7769 err = get_fileindex_path(&fileindex_path, worktree);
7770 if (err)
7771 goto done;
7773 sync_err = sync_fileindex(fileindex, fileindex_path);
7775 err = lock_worktree(worktree, LOCK_SH);
7776 if (sync_err && err == NULL)
7777 err = sync_err;
7778 done:
7779 got_fileindex_free(fileindex);
7780 free(fileindex_path);
7781 return err;
7784 static const struct got_error *
7785 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7787 const struct got_error *err;
7788 char *branch_refname = NULL, *commit_refname = NULL;
7790 err = get_merge_branch_ref_name(&branch_refname, worktree);
7791 if (err)
7792 goto done;
7793 err = delete_ref(branch_refname, repo);
7794 if (err)
7795 goto done;
7797 err = get_merge_commit_ref_name(&commit_refname, worktree);
7798 if (err)
7799 goto done;
7800 err = delete_ref(commit_refname, repo);
7801 if (err)
7802 goto done;
7804 done:
7805 free(branch_refname);
7806 free(commit_refname);
7807 return err;
7810 struct merge_commit_msg_arg {
7811 struct got_worktree *worktree;
7812 const char *branch_name;
7815 static const struct got_error *
7816 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
7817 const char *diff_path, char **logmsg, void *arg)
7819 struct merge_commit_msg_arg *a = arg;
7821 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7822 got_worktree_get_head_ref_name(a->worktree)) == -1)
7823 return got_error_from_errno("asprintf");
7825 return NULL;
7829 const struct got_error *
7830 got_worktree_merge_branch(struct got_worktree *worktree,
7831 struct got_fileindex *fileindex,
7832 struct got_object_id *yca_commit_id,
7833 struct got_object_id *branch_tip,
7834 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7835 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7837 const struct got_error *err;
7838 char *fileindex_path = NULL;
7840 err = get_fileindex_path(&fileindex_path, worktree);
7841 if (err)
7842 goto done;
7844 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7845 worktree);
7846 if (err)
7847 goto done;
7849 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7850 branch_tip, repo, progress_cb, progress_arg,
7851 cancel_cb, cancel_arg);
7852 done:
7853 free(fileindex_path);
7854 return err;
7857 const struct got_error *
7858 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7859 struct got_worktree *worktree, struct got_fileindex *fileindex,
7860 const char *author, const char *committer, int allow_bad_symlinks,
7861 struct got_object_id *branch_tip, const char *branch_name,
7862 struct got_repository *repo,
7863 got_worktree_status_cb status_cb, void *status_arg)
7866 const struct got_error *err = NULL, *sync_err;
7867 struct got_pathlist_head commitable_paths;
7868 struct collect_commitables_arg cc_arg;
7869 struct got_pathlist_entry *pe;
7870 struct got_reference *head_ref = NULL;
7871 struct got_object_id *head_commit_id = NULL;
7872 int have_staged_files = 0;
7873 struct merge_commit_msg_arg mcm_arg;
7874 char *fileindex_path = NULL;
7876 memset(&cc_arg, 0, sizeof(cc_arg));
7877 *new_commit_id = NULL;
7879 TAILQ_INIT(&commitable_paths);
7881 err = get_fileindex_path(&fileindex_path, worktree);
7882 if (err)
7883 goto done;
7885 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7886 if (err)
7887 goto done;
7889 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7890 if (err)
7891 goto done;
7893 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7894 &have_staged_files);
7895 if (err && err->code != GOT_ERR_CANCELLED)
7896 goto done;
7897 if (have_staged_files) {
7898 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7899 goto done;
7902 cc_arg.commitable_paths = &commitable_paths;
7903 cc_arg.worktree = worktree;
7904 cc_arg.fileindex = fileindex;
7905 cc_arg.repo = repo;
7906 cc_arg.have_staged_files = have_staged_files;
7907 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7908 err = worktree_status(worktree, "", fileindex, repo,
7909 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7910 if (err)
7911 goto done;
7913 if (TAILQ_EMPTY(&commitable_paths)) {
7914 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7915 "merge of %s cannot proceed", branch_name);
7916 goto done;
7919 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7920 struct got_commitable *ct = pe->data;
7921 const char *ct_path = ct->in_repo_path;
7923 while (ct_path[0] == '/')
7924 ct_path++;
7925 err = check_out_of_date(ct_path, ct->status,
7926 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7927 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7928 if (err)
7929 goto done;
7933 mcm_arg.worktree = worktree;
7934 mcm_arg.branch_name = branch_name;
7935 err = commit_worktree(new_commit_id, &commitable_paths,
7936 head_commit_id, branch_tip, worktree, author, committer, NULL,
7937 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7938 if (err)
7939 goto done;
7941 err = update_fileindex_after_commit(worktree, &commitable_paths,
7942 *new_commit_id, fileindex, have_staged_files);
7943 sync_err = sync_fileindex(fileindex, fileindex_path);
7944 if (sync_err && err == NULL)
7945 err = sync_err;
7946 done:
7947 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7948 struct got_commitable *ct = pe->data;
7950 free_commitable(ct);
7952 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
7953 free(fileindex_path);
7954 return err;
7957 const struct got_error *
7958 got_worktree_merge_complete(struct got_worktree *worktree,
7959 struct got_fileindex *fileindex, struct got_repository *repo)
7961 const struct got_error *err, *unlockerr, *sync_err;
7962 char *fileindex_path = NULL;
7964 err = delete_merge_refs(worktree, repo);
7965 if (err)
7966 goto done;
7968 err = get_fileindex_path(&fileindex_path, worktree);
7969 if (err)
7970 goto done;
7971 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7972 sync_err = sync_fileindex(fileindex, fileindex_path);
7973 if (sync_err && err == NULL)
7974 err = sync_err;
7975 done:
7976 got_fileindex_free(fileindex);
7977 free(fileindex_path);
7978 unlockerr = lock_worktree(worktree, LOCK_SH);
7979 if (unlockerr && err == NULL)
7980 err = unlockerr;
7981 return err;
7984 const struct got_error *
7985 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7986 struct got_repository *repo)
7988 const struct got_error *err;
7989 char *branch_refname = NULL;
7990 struct got_reference *branch_ref = NULL;
7992 *in_progress = 0;
7994 err = get_merge_branch_ref_name(&branch_refname, worktree);
7995 if (err)
7996 return err;
7997 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7998 free(branch_refname);
7999 if (err) {
8000 if (err->code != GOT_ERR_NOT_REF)
8001 return err;
8002 } else
8003 *in_progress = 1;
8005 return NULL;
8008 const struct got_error *got_worktree_merge_prepare(
8009 struct got_fileindex **fileindex, struct got_worktree *worktree,
8010 struct got_reference *branch, struct got_repository *repo)
8012 const struct got_error *err = NULL;
8013 char *fileindex_path = NULL;
8014 char *branch_refname = NULL, *commit_refname = NULL;
8015 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
8016 struct got_reference *commit_ref = NULL;
8017 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
8018 struct check_rebase_ok_arg ok_arg;
8020 *fileindex = NULL;
8022 err = lock_worktree(worktree, LOCK_EX);
8023 if (err)
8024 return err;
8026 err = open_fileindex(fileindex, &fileindex_path, worktree);
8027 if (err)
8028 goto done;
8030 /* Preconditions are the same as for rebase. */
8031 ok_arg.worktree = worktree;
8032 ok_arg.repo = repo;
8033 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8034 &ok_arg);
8035 if (err)
8036 goto done;
8038 err = get_merge_branch_ref_name(&branch_refname, worktree);
8039 if (err)
8040 return err;
8042 err = get_merge_commit_ref_name(&commit_refname, worktree);
8043 if (err)
8044 return err;
8046 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8047 0);
8048 if (err)
8049 goto done;
8051 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8052 if (err)
8053 goto done;
8055 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8056 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8057 goto done;
8060 err = got_ref_resolve(&branch_tip, repo, branch);
8061 if (err)
8062 goto done;
8064 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8065 if (err)
8066 goto done;
8067 err = got_ref_write(branch_ref, repo);
8068 if (err)
8069 goto done;
8071 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8072 if (err)
8073 goto done;
8074 err = got_ref_write(commit_ref, repo);
8075 if (err)
8076 goto done;
8078 done:
8079 free(branch_refname);
8080 free(commit_refname);
8081 free(fileindex_path);
8082 if (branch_ref)
8083 got_ref_close(branch_ref);
8084 if (commit_ref)
8085 got_ref_close(commit_ref);
8086 if (wt_branch)
8087 got_ref_close(wt_branch);
8088 free(wt_branch_tip);
8089 if (err) {
8090 if (*fileindex) {
8091 got_fileindex_free(*fileindex);
8092 *fileindex = NULL;
8094 lock_worktree(worktree, LOCK_SH);
8096 return err;
8099 const struct got_error *
8100 got_worktree_merge_continue(char **branch_name,
8101 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8102 struct got_worktree *worktree, struct got_repository *repo)
8104 const struct got_error *err;
8105 char *commit_refname = NULL, *branch_refname = NULL;
8106 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8107 char *fileindex_path = NULL;
8108 int have_staged_files = 0;
8110 *branch_name = NULL;
8111 *branch_tip = NULL;
8112 *fileindex = NULL;
8114 err = lock_worktree(worktree, LOCK_EX);
8115 if (err)
8116 return err;
8118 err = open_fileindex(fileindex, &fileindex_path, worktree);
8119 if (err)
8120 goto done;
8122 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8123 &have_staged_files);
8124 if (err && err->code != GOT_ERR_CANCELLED)
8125 goto done;
8126 if (have_staged_files) {
8127 err = got_error(GOT_ERR_STAGED_PATHS);
8128 goto done;
8131 err = get_merge_branch_ref_name(&branch_refname, worktree);
8132 if (err)
8133 goto done;
8135 err = get_merge_commit_ref_name(&commit_refname, worktree);
8136 if (err)
8137 goto done;
8139 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8140 if (err)
8141 goto done;
8143 if (!got_ref_is_symbolic(branch_ref)) {
8144 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8145 "%s is not a symbolic reference",
8146 got_ref_get_name(branch_ref));
8147 goto done;
8149 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8150 if (*branch_name == NULL) {
8151 err = got_error_from_errno("strdup");
8152 goto done;
8155 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8156 if (err)
8157 goto done;
8159 err = got_ref_resolve(branch_tip, repo, commit_ref);
8160 if (err)
8161 goto done;
8162 done:
8163 free(commit_refname);
8164 free(branch_refname);
8165 free(fileindex_path);
8166 if (commit_ref)
8167 got_ref_close(commit_ref);
8168 if (branch_ref)
8169 got_ref_close(branch_ref);
8170 if (err) {
8171 if (*branch_name) {
8172 free(*branch_name);
8173 *branch_name = NULL;
8175 free(*branch_tip);
8176 *branch_tip = NULL;
8177 if (*fileindex) {
8178 got_fileindex_free(*fileindex);
8179 *fileindex = NULL;
8181 lock_worktree(worktree, LOCK_SH);
8183 return err;
8186 const struct got_error *
8187 got_worktree_merge_abort(struct got_worktree *worktree,
8188 struct got_fileindex *fileindex, struct got_repository *repo,
8189 got_worktree_checkout_cb progress_cb, void *progress_arg)
8191 const struct got_error *err, *unlockerr, *sync_err;
8192 struct got_object_id *commit_id = NULL;
8193 struct got_commit_object *commit = NULL;
8194 char *fileindex_path = NULL;
8195 struct revert_file_args rfa;
8196 struct got_object_id *tree_id = NULL;
8198 err = got_object_open_as_commit(&commit, repo,
8199 worktree->base_commit_id);
8200 if (err)
8201 goto done;
8203 err = got_object_id_by_path(&tree_id, repo, commit,
8204 worktree->path_prefix);
8205 if (err)
8206 goto done;
8208 err = delete_merge_refs(worktree, repo);
8209 if (err)
8210 goto done;
8212 err = get_fileindex_path(&fileindex_path, worktree);
8213 if (err)
8214 goto done;
8216 rfa.worktree = worktree;
8217 rfa.fileindex = fileindex;
8218 rfa.progress_cb = progress_cb;
8219 rfa.progress_arg = progress_arg;
8220 rfa.patch_cb = NULL;
8221 rfa.patch_arg = NULL;
8222 rfa.repo = repo;
8223 rfa.unlink_added_files = 1;
8224 err = worktree_status(worktree, "", fileindex, repo,
8225 revert_file, &rfa, NULL, NULL, 1, 0);
8226 if (err)
8227 goto sync;
8229 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8230 repo, progress_cb, progress_arg, NULL, NULL);
8231 sync:
8232 sync_err = sync_fileindex(fileindex, fileindex_path);
8233 if (sync_err && err == NULL)
8234 err = sync_err;
8235 done:
8236 free(tree_id);
8237 free(commit_id);
8238 if (commit)
8239 got_object_commit_close(commit);
8240 if (fileindex)
8241 got_fileindex_free(fileindex);
8242 free(fileindex_path);
8244 unlockerr = lock_worktree(worktree, LOCK_SH);
8245 if (unlockerr && err == NULL)
8246 err = unlockerr;
8247 return err;
8250 struct check_stage_ok_arg {
8251 struct got_object_id *head_commit_id;
8252 struct got_worktree *worktree;
8253 struct got_fileindex *fileindex;
8254 struct got_repository *repo;
8255 int have_changes;
8258 static const struct got_error *
8259 check_stage_ok(void *arg, unsigned char status,
8260 unsigned char staged_status, const char *relpath,
8261 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8262 struct got_object_id *commit_id, int dirfd, const char *de_name)
8264 struct check_stage_ok_arg *a = arg;
8265 const struct got_error *err = NULL;
8266 struct got_fileindex_entry *ie;
8267 struct got_object_id base_commit_id;
8268 struct got_object_id *base_commit_idp = NULL;
8269 char *in_repo_path = NULL, *p;
8271 if (status == GOT_STATUS_UNVERSIONED ||
8272 status == GOT_STATUS_NO_CHANGE)
8273 return NULL;
8274 if (status == GOT_STATUS_NONEXISTENT)
8275 return got_error_set_errno(ENOENT, relpath);
8277 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8278 if (ie == NULL)
8279 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8281 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8282 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8283 relpath) == -1)
8284 return got_error_from_errno("asprintf");
8286 if (got_fileindex_entry_has_commit(ie)) {
8287 memcpy(base_commit_id.hash, ie->commit_hash,
8288 SHA1_DIGEST_LENGTH);
8289 base_commit_idp = &base_commit_id;
8292 if (status == GOT_STATUS_CONFLICT) {
8293 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8294 goto done;
8295 } else if (status != GOT_STATUS_ADD &&
8296 status != GOT_STATUS_MODIFY &&
8297 status != GOT_STATUS_DELETE) {
8298 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8299 goto done;
8302 a->have_changes = 1;
8304 p = in_repo_path;
8305 while (p[0] == '/')
8306 p++;
8307 err = check_out_of_date(p, status, staged_status,
8308 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8309 GOT_ERR_STAGE_OUT_OF_DATE);
8310 done:
8311 free(in_repo_path);
8312 return err;
8315 struct stage_path_arg {
8316 struct got_worktree *worktree;
8317 struct got_fileindex *fileindex;
8318 struct got_repository *repo;
8319 got_worktree_status_cb status_cb;
8320 void *status_arg;
8321 got_worktree_patch_cb patch_cb;
8322 void *patch_arg;
8323 int staged_something;
8324 int allow_bad_symlinks;
8327 static const struct got_error *
8328 stage_path(void *arg, unsigned char status,
8329 unsigned char staged_status, const char *relpath,
8330 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8331 struct got_object_id *commit_id, int dirfd, const char *de_name)
8333 struct stage_path_arg *a = arg;
8334 const struct got_error *err = NULL;
8335 struct got_fileindex_entry *ie;
8336 char *ondisk_path = NULL, *path_content = NULL;
8337 uint32_t stage;
8338 struct got_object_id *new_staged_blob_id = NULL;
8339 struct stat sb;
8341 if (status == GOT_STATUS_UNVERSIONED)
8342 return NULL;
8344 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8345 if (ie == NULL)
8346 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8348 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8349 relpath)== -1)
8350 return got_error_from_errno("asprintf");
8352 switch (status) {
8353 case GOT_STATUS_ADD:
8354 case GOT_STATUS_MODIFY:
8355 /* XXX could sb.st_mode be passed in by our caller? */
8356 if (lstat(ondisk_path, &sb) == -1) {
8357 err = got_error_from_errno2("lstat", ondisk_path);
8358 break;
8360 if (a->patch_cb) {
8361 if (status == GOT_STATUS_ADD) {
8362 int choice = GOT_PATCH_CHOICE_NONE;
8363 err = (*a->patch_cb)(&choice, a->patch_arg,
8364 status, ie->path, NULL, 1, 1);
8365 if (err)
8366 break;
8367 if (choice != GOT_PATCH_CHOICE_YES)
8368 break;
8369 } else {
8370 err = create_patched_content(&path_content, 0,
8371 staged_blob_id ? staged_blob_id : blob_id,
8372 ondisk_path, dirfd, de_name, ie->path,
8373 a->repo, a->patch_cb, a->patch_arg);
8374 if (err || path_content == NULL)
8375 break;
8378 err = got_object_blob_create(&new_staged_blob_id,
8379 path_content ? path_content : ondisk_path, a->repo);
8380 if (err)
8381 break;
8382 memcpy(ie->staged_blob_hash, new_staged_blob_id->hash,
8383 SHA1_DIGEST_LENGTH);
8384 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8385 stage = GOT_FILEIDX_STAGE_ADD;
8386 else
8387 stage = GOT_FILEIDX_STAGE_MODIFY;
8388 got_fileindex_entry_stage_set(ie, stage);
8389 if (S_ISLNK(sb.st_mode)) {
8390 int is_bad_symlink = 0;
8391 if (!a->allow_bad_symlinks) {
8392 char target_path[PATH_MAX];
8393 ssize_t target_len;
8394 target_len = readlink(ondisk_path, target_path,
8395 sizeof(target_path));
8396 if (target_len == -1) {
8397 err = got_error_from_errno2("readlink",
8398 ondisk_path);
8399 break;
8401 err = is_bad_symlink_target(&is_bad_symlink,
8402 target_path, target_len, ondisk_path,
8403 a->worktree->root_path);
8404 if (err)
8405 break;
8406 if (is_bad_symlink) {
8407 err = got_error_path(ondisk_path,
8408 GOT_ERR_BAD_SYMLINK);
8409 break;
8412 if (is_bad_symlink)
8413 got_fileindex_entry_staged_filetype_set(ie,
8414 GOT_FILEIDX_MODE_BAD_SYMLINK);
8415 else
8416 got_fileindex_entry_staged_filetype_set(ie,
8417 GOT_FILEIDX_MODE_SYMLINK);
8418 } else {
8419 got_fileindex_entry_staged_filetype_set(ie,
8420 GOT_FILEIDX_MODE_REGULAR_FILE);
8422 a->staged_something = 1;
8423 if (a->status_cb == NULL)
8424 break;
8425 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8426 get_staged_status(ie), relpath, blob_id,
8427 new_staged_blob_id, NULL, dirfd, de_name);
8428 if (err)
8429 break;
8431 * When staging the reverse of the staged diff,
8432 * implicitly unstage the file.
8434 if (memcmp(ie->staged_blob_hash, ie->blob_hash,
8435 sizeof(ie->blob_hash)) == 0) {
8436 got_fileindex_entry_stage_set(ie,
8437 GOT_FILEIDX_STAGE_NONE);
8439 break;
8440 case GOT_STATUS_DELETE:
8441 if (staged_status == GOT_STATUS_DELETE)
8442 break;
8443 if (a->patch_cb) {
8444 int choice = GOT_PATCH_CHOICE_NONE;
8445 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8446 ie->path, NULL, 1, 1);
8447 if (err)
8448 break;
8449 if (choice == GOT_PATCH_CHOICE_NO)
8450 break;
8451 if (choice != GOT_PATCH_CHOICE_YES) {
8452 err = got_error(GOT_ERR_PATCH_CHOICE);
8453 break;
8456 stage = GOT_FILEIDX_STAGE_DELETE;
8457 got_fileindex_entry_stage_set(ie, stage);
8458 a->staged_something = 1;
8459 if (a->status_cb == NULL)
8460 break;
8461 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8462 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8463 de_name);
8464 break;
8465 case GOT_STATUS_NO_CHANGE:
8466 break;
8467 case GOT_STATUS_CONFLICT:
8468 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8469 break;
8470 case GOT_STATUS_NONEXISTENT:
8471 err = got_error_set_errno(ENOENT, relpath);
8472 break;
8473 default:
8474 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8475 break;
8478 if (path_content && unlink(path_content) == -1 && err == NULL)
8479 err = got_error_from_errno2("unlink", path_content);
8480 free(path_content);
8481 free(ondisk_path);
8482 free(new_staged_blob_id);
8483 return err;
8486 const struct got_error *
8487 got_worktree_stage(struct got_worktree *worktree,
8488 struct got_pathlist_head *paths,
8489 got_worktree_status_cb status_cb, void *status_arg,
8490 got_worktree_patch_cb patch_cb, void *patch_arg,
8491 int allow_bad_symlinks, struct got_repository *repo)
8493 const struct got_error *err = NULL, *sync_err, *unlockerr;
8494 struct got_pathlist_entry *pe;
8495 struct got_fileindex *fileindex = NULL;
8496 char *fileindex_path = NULL;
8497 struct got_reference *head_ref = NULL;
8498 struct got_object_id *head_commit_id = NULL;
8499 struct check_stage_ok_arg oka;
8500 struct stage_path_arg spa;
8502 err = lock_worktree(worktree, LOCK_EX);
8503 if (err)
8504 return err;
8506 err = got_ref_open(&head_ref, repo,
8507 got_worktree_get_head_ref_name(worktree), 0);
8508 if (err)
8509 goto done;
8510 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8511 if (err)
8512 goto done;
8513 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8514 if (err)
8515 goto done;
8517 /* Check pre-conditions before staging anything. */
8518 oka.head_commit_id = head_commit_id;
8519 oka.worktree = worktree;
8520 oka.fileindex = fileindex;
8521 oka.repo = repo;
8522 oka.have_changes = 0;
8523 TAILQ_FOREACH(pe, paths, entry) {
8524 err = worktree_status(worktree, pe->path, fileindex, repo,
8525 check_stage_ok, &oka, NULL, NULL, 1, 0);
8526 if (err)
8527 goto done;
8529 if (!oka.have_changes) {
8530 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8531 goto done;
8534 spa.worktree = worktree;
8535 spa.fileindex = fileindex;
8536 spa.repo = repo;
8537 spa.patch_cb = patch_cb;
8538 spa.patch_arg = patch_arg;
8539 spa.status_cb = status_cb;
8540 spa.status_arg = status_arg;
8541 spa.staged_something = 0;
8542 spa.allow_bad_symlinks = allow_bad_symlinks;
8543 TAILQ_FOREACH(pe, paths, entry) {
8544 err = worktree_status(worktree, pe->path, fileindex, repo,
8545 stage_path, &spa, NULL, NULL, 1, 0);
8546 if (err)
8547 goto done;
8549 if (!spa.staged_something) {
8550 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8551 goto done;
8554 sync_err = sync_fileindex(fileindex, fileindex_path);
8555 if (sync_err && err == NULL)
8556 err = sync_err;
8557 done:
8558 if (head_ref)
8559 got_ref_close(head_ref);
8560 free(head_commit_id);
8561 free(fileindex_path);
8562 if (fileindex)
8563 got_fileindex_free(fileindex);
8564 unlockerr = lock_worktree(worktree, LOCK_SH);
8565 if (unlockerr && err == NULL)
8566 err = unlockerr;
8567 return err;
8570 struct unstage_path_arg {
8571 struct got_worktree *worktree;
8572 struct got_fileindex *fileindex;
8573 struct got_repository *repo;
8574 got_worktree_checkout_cb progress_cb;
8575 void *progress_arg;
8576 got_worktree_patch_cb patch_cb;
8577 void *patch_arg;
8580 static const struct got_error *
8581 create_unstaged_content(char **path_unstaged_content,
8582 char **path_new_staged_content, struct got_object_id *blob_id,
8583 struct got_object_id *staged_blob_id, const char *relpath,
8584 struct got_repository *repo,
8585 got_worktree_patch_cb patch_cb, void *patch_arg)
8587 const struct got_error *err, *free_err;
8588 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8589 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8590 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8591 struct got_diffreg_result *diffreg_result = NULL;
8592 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8593 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8594 int fd1 = -1, fd2 = -1;
8596 *path_unstaged_content = NULL;
8597 *path_new_staged_content = NULL;
8599 err = got_object_id_str(&label1, blob_id);
8600 if (err)
8601 return err;
8603 fd1 = got_opentempfd();
8604 if (fd1 == -1) {
8605 err = got_error_from_errno("got_opentempfd");
8606 goto done;
8608 fd2 = got_opentempfd();
8609 if (fd2 == -1) {
8610 err = got_error_from_errno("got_opentempfd");
8611 goto done;
8614 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8615 if (err)
8616 goto done;
8618 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
8619 if (err)
8620 goto done;
8622 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8623 if (err)
8624 goto done;
8626 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8627 fd2);
8628 if (err)
8629 goto done;
8631 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
8632 if (err)
8633 goto done;
8635 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8636 if (err)
8637 goto done;
8639 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8640 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8641 if (err)
8642 goto done;
8644 err = got_opentemp_named(path_unstaged_content, &outfile,
8645 "got-unstaged-content", "");
8646 if (err)
8647 goto done;
8648 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8649 "got-new-staged-content", "");
8650 if (err)
8651 goto done;
8653 if (fseek(f1, 0L, SEEK_SET) == -1) {
8654 err = got_ferror(f1, GOT_ERR_IO);
8655 goto done;
8657 if (fseek(f2, 0L, SEEK_SET) == -1) {
8658 err = got_ferror(f2, GOT_ERR_IO);
8659 goto done;
8661 /* Count the number of actual changes in the diff result. */
8662 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8663 struct diff_chunk_context cc = {};
8664 diff_chunk_context_load_change(&cc, &nchunks_used,
8665 diffreg_result->result, n, 0);
8666 nchanges++;
8668 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8669 int choice;
8670 err = apply_or_reject_change(&choice, &nchunks_used,
8671 diffreg_result->result, n, relpath, f1, f2,
8672 &line_cur1, &line_cur2,
8673 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8674 if (err)
8675 goto done;
8676 if (choice == GOT_PATCH_CHOICE_YES)
8677 have_content = 1;
8678 else
8679 have_rejected_content = 1;
8680 if (choice == GOT_PATCH_CHOICE_QUIT)
8681 break;
8683 if (have_content || have_rejected_content)
8684 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8685 outfile, rejectfile);
8686 done:
8687 free(label1);
8688 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8689 err = got_error_from_errno("close");
8690 if (blob)
8691 got_object_blob_close(blob);
8692 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8693 err = got_error_from_errno("close");
8694 if (staged_blob)
8695 got_object_blob_close(staged_blob);
8696 free_err = got_diffreg_result_free(diffreg_result);
8697 if (free_err && err == NULL)
8698 err = free_err;
8699 if (f1 && fclose(f1) == EOF && err == NULL)
8700 err = got_error_from_errno2("fclose", path1);
8701 if (f2 && fclose(f2) == EOF && err == NULL)
8702 err = got_error_from_errno2("fclose", path2);
8703 if (outfile && fclose(outfile) == EOF && err == NULL)
8704 err = got_error_from_errno2("fclose", *path_unstaged_content);
8705 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8706 err = got_error_from_errno2("fclose", *path_new_staged_content);
8707 if (path1 && unlink(path1) == -1 && err == NULL)
8708 err = got_error_from_errno2("unlink", path1);
8709 if (path2 && unlink(path2) == -1 && err == NULL)
8710 err = got_error_from_errno2("unlink", path2);
8711 if (err || !have_content) {
8712 if (*path_unstaged_content &&
8713 unlink(*path_unstaged_content) == -1 && err == NULL)
8714 err = got_error_from_errno2("unlink",
8715 *path_unstaged_content);
8716 free(*path_unstaged_content);
8717 *path_unstaged_content = NULL;
8719 if (err || !have_content || !have_rejected_content) {
8720 if (*path_new_staged_content &&
8721 unlink(*path_new_staged_content) == -1 && err == NULL)
8722 err = got_error_from_errno2("unlink",
8723 *path_new_staged_content);
8724 free(*path_new_staged_content);
8725 *path_new_staged_content = NULL;
8727 free(path1);
8728 free(path2);
8729 return err;
8732 static const struct got_error *
8733 unstage_hunks(struct got_object_id *staged_blob_id,
8734 struct got_blob_object *blob_base,
8735 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8736 const char *ondisk_path, const char *label_orig,
8737 struct got_worktree *worktree, struct got_repository *repo,
8738 got_worktree_patch_cb patch_cb, void *patch_arg,
8739 got_worktree_checkout_cb progress_cb, void *progress_arg)
8741 const struct got_error *err = NULL;
8742 char *path_unstaged_content = NULL;
8743 char *path_new_staged_content = NULL;
8744 char *parent = NULL, *base_path = NULL;
8745 char *blob_base_path = NULL;
8746 struct got_object_id *new_staged_blob_id = NULL;
8747 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8748 struct stat sb;
8750 err = create_unstaged_content(&path_unstaged_content,
8751 &path_new_staged_content, blob_id, staged_blob_id,
8752 ie->path, repo, patch_cb, patch_arg);
8753 if (err)
8754 return err;
8756 if (path_unstaged_content == NULL)
8757 return NULL;
8759 if (path_new_staged_content) {
8760 err = got_object_blob_create(&new_staged_blob_id,
8761 path_new_staged_content, repo);
8762 if (err)
8763 goto done;
8766 f = fopen(path_unstaged_content, "re");
8767 if (f == NULL) {
8768 err = got_error_from_errno2("fopen",
8769 path_unstaged_content);
8770 goto done;
8772 if (fstat(fileno(f), &sb) == -1) {
8773 err = got_error_from_errno2("fstat", path_unstaged_content);
8774 goto done;
8776 if (got_fileindex_entry_staged_filetype_get(ie) ==
8777 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8778 char link_target[PATH_MAX];
8779 size_t r;
8780 r = fread(link_target, 1, sizeof(link_target), f);
8781 if (r == 0 && ferror(f)) {
8782 err = got_error_from_errno("fread");
8783 goto done;
8785 if (r >= sizeof(link_target)) { /* should not happen */
8786 err = got_error(GOT_ERR_NO_SPACE);
8787 goto done;
8789 link_target[r] = '\0';
8790 err = merge_symlink(worktree, blob_base,
8791 ondisk_path, ie->path, label_orig, link_target,
8792 worktree->base_commit_id, repo, progress_cb,
8793 progress_arg);
8794 } else {
8795 int local_changes_subsumed;
8797 err = got_path_dirname(&parent, ondisk_path);
8798 if (err)
8799 return err;
8801 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8802 parent) == -1) {
8803 err = got_error_from_errno("asprintf");
8804 base_path = NULL;
8805 goto done;
8808 err = got_opentemp_named(&blob_base_path, &f_base,
8809 base_path, "");
8810 if (err)
8811 goto done;
8812 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8813 blob_base);
8814 if (err)
8815 goto done;
8818 * In order the run a 3-way merge with a symlink we copy the symlink's
8819 * target path into a temporary file and use that file with diff3.
8821 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8822 err = dump_symlink_target_path_to_file(&f_deriv2,
8823 ondisk_path);
8824 if (err)
8825 goto done;
8826 } else {
8827 int fd;
8828 fd = open(ondisk_path,
8829 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8830 if (fd == -1) {
8831 err = got_error_from_errno2("open", ondisk_path);
8832 goto done;
8834 f_deriv2 = fdopen(fd, "r");
8835 if (f_deriv2 == NULL) {
8836 err = got_error_from_errno2("fdopen", ondisk_path);
8837 close(fd);
8838 goto done;
8842 err = merge_file(&local_changes_subsumed, worktree,
8843 f_base, f, f_deriv2, ondisk_path, ie->path,
8844 got_fileindex_perms_to_st(ie),
8845 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8846 repo, progress_cb, progress_arg);
8848 if (err)
8849 goto done;
8851 if (new_staged_blob_id) {
8852 memcpy(ie->staged_blob_hash, new_staged_blob_id->hash,
8853 SHA1_DIGEST_LENGTH);
8854 } else {
8855 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8856 got_fileindex_entry_staged_filetype_set(ie, 0);
8858 done:
8859 free(new_staged_blob_id);
8860 if (path_unstaged_content &&
8861 unlink(path_unstaged_content) == -1 && err == NULL)
8862 err = got_error_from_errno2("unlink", path_unstaged_content);
8863 if (path_new_staged_content &&
8864 unlink(path_new_staged_content) == -1 && err == NULL)
8865 err = got_error_from_errno2("unlink", path_new_staged_content);
8866 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8867 err = got_error_from_errno2("unlink", blob_base_path);
8868 if (f_base && fclose(f_base) == EOF && err == NULL)
8869 err = got_error_from_errno2("fclose", path_unstaged_content);
8870 if (f && fclose(f) == EOF && err == NULL)
8871 err = got_error_from_errno2("fclose", path_unstaged_content);
8872 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8873 err = got_error_from_errno2("fclose", ondisk_path);
8874 free(path_unstaged_content);
8875 free(path_new_staged_content);
8876 free(blob_base_path);
8877 free(parent);
8878 free(base_path);
8879 return err;
8882 static const struct got_error *
8883 unstage_path(void *arg, unsigned char status,
8884 unsigned char staged_status, const char *relpath,
8885 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8886 struct got_object_id *commit_id, int dirfd, const char *de_name)
8888 const struct got_error *err = NULL;
8889 struct unstage_path_arg *a = arg;
8890 struct got_fileindex_entry *ie;
8891 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8892 char *ondisk_path = NULL;
8893 char *id_str = NULL, *label_orig = NULL;
8894 int local_changes_subsumed;
8895 struct stat sb;
8896 int fd1 = -1, fd2 = -1;
8898 if (staged_status != GOT_STATUS_ADD &&
8899 staged_status != GOT_STATUS_MODIFY &&
8900 staged_status != GOT_STATUS_DELETE)
8901 return NULL;
8903 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8904 if (ie == NULL)
8905 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8907 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8908 == -1)
8909 return got_error_from_errno("asprintf");
8911 err = got_object_id_str(&id_str,
8912 commit_id ? commit_id : a->worktree->base_commit_id);
8913 if (err)
8914 goto done;
8915 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8916 id_str) == -1) {
8917 err = got_error_from_errno("asprintf");
8918 goto done;
8921 fd1 = got_opentempfd();
8922 if (fd1 == -1) {
8923 err = got_error_from_errno("got_opentempfd");
8924 goto done;
8926 fd2 = got_opentempfd();
8927 if (fd2 == -1) {
8928 err = got_error_from_errno("got_opentempfd");
8929 goto done;
8932 switch (staged_status) {
8933 case GOT_STATUS_MODIFY:
8934 err = got_object_open_as_blob(&blob_base, a->repo,
8935 blob_id, 8192, fd1);
8936 if (err)
8937 break;
8938 /* fall through */
8939 case GOT_STATUS_ADD:
8940 if (a->patch_cb) {
8941 if (staged_status == GOT_STATUS_ADD) {
8942 int choice = GOT_PATCH_CHOICE_NONE;
8943 err = (*a->patch_cb)(&choice, a->patch_arg,
8944 staged_status, ie->path, NULL, 1, 1);
8945 if (err)
8946 break;
8947 if (choice != GOT_PATCH_CHOICE_YES)
8948 break;
8949 } else {
8950 err = unstage_hunks(staged_blob_id,
8951 blob_base, blob_id, ie, ondisk_path,
8952 label_orig, a->worktree, a->repo,
8953 a->patch_cb, a->patch_arg,
8954 a->progress_cb, a->progress_arg);
8955 break; /* Done with this file. */
8958 err = got_object_open_as_blob(&blob_staged, a->repo,
8959 staged_blob_id, 8192, fd2);
8960 if (err)
8961 break;
8962 switch (got_fileindex_entry_staged_filetype_get(ie)) {
8963 case GOT_FILEIDX_MODE_BAD_SYMLINK:
8964 case GOT_FILEIDX_MODE_REGULAR_FILE:
8965 err = merge_blob(&local_changes_subsumed, a->worktree,
8966 blob_base, ondisk_path, relpath,
8967 got_fileindex_perms_to_st(ie), label_orig,
8968 blob_staged, commit_id ? commit_id :
8969 a->worktree->base_commit_id, a->repo,
8970 a->progress_cb, a->progress_arg);
8971 break;
8972 case GOT_FILEIDX_MODE_SYMLINK:
8973 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8974 char *staged_target;
8975 err = got_object_blob_read_to_str(
8976 &staged_target, blob_staged);
8977 if (err)
8978 goto done;
8979 err = merge_symlink(a->worktree, blob_base,
8980 ondisk_path, relpath, label_orig,
8981 staged_target, commit_id ? commit_id :
8982 a->worktree->base_commit_id,
8983 a->repo, a->progress_cb, a->progress_arg);
8984 free(staged_target);
8985 } else {
8986 err = merge_blob(&local_changes_subsumed,
8987 a->worktree, blob_base, ondisk_path,
8988 relpath, got_fileindex_perms_to_st(ie),
8989 label_orig, blob_staged,
8990 commit_id ? commit_id :
8991 a->worktree->base_commit_id, a->repo,
8992 a->progress_cb, a->progress_arg);
8994 break;
8995 default:
8996 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8997 break;
8999 if (err == NULL) {
9000 got_fileindex_entry_stage_set(ie,
9001 GOT_FILEIDX_STAGE_NONE);
9002 got_fileindex_entry_staged_filetype_set(ie, 0);
9004 break;
9005 case GOT_STATUS_DELETE:
9006 if (a->patch_cb) {
9007 int choice = GOT_PATCH_CHOICE_NONE;
9008 err = (*a->patch_cb)(&choice, a->patch_arg,
9009 staged_status, ie->path, NULL, 1, 1);
9010 if (err)
9011 break;
9012 if (choice == GOT_PATCH_CHOICE_NO)
9013 break;
9014 if (choice != GOT_PATCH_CHOICE_YES) {
9015 err = got_error(GOT_ERR_PATCH_CHOICE);
9016 break;
9019 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9020 got_fileindex_entry_staged_filetype_set(ie, 0);
9021 err = get_file_status(&status, &sb, ie, ondisk_path,
9022 dirfd, de_name, a->repo);
9023 if (err)
9024 break;
9025 err = (*a->progress_cb)(a->progress_arg, status, relpath);
9026 break;
9028 done:
9029 free(ondisk_path);
9030 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9031 err = got_error_from_errno("close");
9032 if (blob_base)
9033 got_object_blob_close(blob_base);
9034 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9035 err = got_error_from_errno("close");
9036 if (blob_staged)
9037 got_object_blob_close(blob_staged);
9038 free(id_str);
9039 free(label_orig);
9040 return err;
9043 const struct got_error *
9044 got_worktree_unstage(struct got_worktree *worktree,
9045 struct got_pathlist_head *paths,
9046 got_worktree_checkout_cb progress_cb, void *progress_arg,
9047 got_worktree_patch_cb patch_cb, void *patch_arg,
9048 struct got_repository *repo)
9050 const struct got_error *err = NULL, *sync_err, *unlockerr;
9051 struct got_pathlist_entry *pe;
9052 struct got_fileindex *fileindex = NULL;
9053 char *fileindex_path = NULL;
9054 struct unstage_path_arg upa;
9056 err = lock_worktree(worktree, LOCK_EX);
9057 if (err)
9058 return err;
9060 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9061 if (err)
9062 goto done;
9064 upa.worktree = worktree;
9065 upa.fileindex = fileindex;
9066 upa.repo = repo;
9067 upa.progress_cb = progress_cb;
9068 upa.progress_arg = progress_arg;
9069 upa.patch_cb = patch_cb;
9070 upa.patch_arg = patch_arg;
9071 TAILQ_FOREACH(pe, paths, entry) {
9072 err = worktree_status(worktree, pe->path, fileindex, repo,
9073 unstage_path, &upa, NULL, NULL, 1, 0);
9074 if (err)
9075 goto done;
9078 sync_err = sync_fileindex(fileindex, fileindex_path);
9079 if (sync_err && err == NULL)
9080 err = sync_err;
9081 done:
9082 free(fileindex_path);
9083 if (fileindex)
9084 got_fileindex_free(fileindex);
9085 unlockerr = lock_worktree(worktree, LOCK_SH);
9086 if (unlockerr && err == NULL)
9087 err = unlockerr;
9088 return err;
9091 struct report_file_info_arg {
9092 struct got_worktree *worktree;
9093 got_worktree_path_info_cb info_cb;
9094 void *info_arg;
9095 struct got_pathlist_head *paths;
9096 got_cancel_cb cancel_cb;
9097 void *cancel_arg;
9100 static const struct got_error *
9101 report_file_info(void *arg, struct got_fileindex_entry *ie)
9103 struct report_file_info_arg *a = arg;
9104 struct got_pathlist_entry *pe;
9105 struct got_object_id blob_id, staged_blob_id, commit_id;
9106 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9107 struct got_object_id *commit_idp = NULL;
9108 int stage;
9110 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9111 return got_error(GOT_ERR_CANCELLED);
9113 TAILQ_FOREACH(pe, a->paths, entry) {
9114 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9115 got_path_is_child(ie->path, pe->path, pe->path_len))
9116 break;
9118 if (pe == NULL) /* not found */
9119 return NULL;
9121 if (got_fileindex_entry_has_blob(ie)) {
9122 memcpy(blob_id.hash, ie->blob_hash, SHA1_DIGEST_LENGTH);
9123 blob_idp = &blob_id;
9125 stage = got_fileindex_entry_stage_get(ie);
9126 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9127 stage == GOT_FILEIDX_STAGE_ADD) {
9128 memcpy(staged_blob_id.hash, ie->staged_blob_hash,
9129 SHA1_DIGEST_LENGTH);
9130 staged_blob_idp = &staged_blob_id;
9133 if (got_fileindex_entry_has_commit(ie)) {
9134 memcpy(commit_id.hash, ie->commit_hash, SHA1_DIGEST_LENGTH);
9135 commit_idp = &commit_id;
9138 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9139 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9142 const struct got_error *
9143 got_worktree_path_info(struct got_worktree *worktree,
9144 struct got_pathlist_head *paths,
9145 got_worktree_path_info_cb info_cb, void *info_arg,
9146 got_cancel_cb cancel_cb, void *cancel_arg)
9149 const struct got_error *err = NULL, *unlockerr;
9150 struct got_fileindex *fileindex = NULL;
9151 char *fileindex_path = NULL;
9152 struct report_file_info_arg arg;
9154 err = lock_worktree(worktree, LOCK_SH);
9155 if (err)
9156 return err;
9158 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9159 if (err)
9160 goto done;
9162 arg.worktree = worktree;
9163 arg.info_cb = info_cb;
9164 arg.info_arg = info_arg;
9165 arg.paths = paths;
9166 arg.cancel_cb = cancel_cb;
9167 arg.cancel_arg = cancel_arg;
9168 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9169 &arg);
9170 done:
9171 free(fileindex_path);
9172 if (fileindex)
9173 got_fileindex_free(fileindex);
9174 unlockerr = lock_worktree(worktree, LOCK_UN);
9175 if (unlockerr && err == NULL)
9176 err = unlockerr;
9177 return err;
9180 static const struct got_error *
9181 patch_check_path(const char *p, char **path, unsigned char *status,
9182 unsigned char *staged_status, struct got_fileindex *fileindex,
9183 struct got_worktree *worktree, struct got_repository *repo)
9185 const struct got_error *err;
9186 struct got_fileindex_entry *ie;
9187 struct stat sb;
9188 char *ondisk_path = NULL;
9190 err = got_worktree_resolve_path(path, worktree, p);
9191 if (err)
9192 return err;
9194 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9195 *path[0] ? "/" : "", *path) == -1)
9196 return got_error_from_errno("asprintf");
9198 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9199 if (ie) {
9200 *staged_status = get_staged_status(ie);
9201 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9202 repo);
9203 if (err)
9204 goto done;
9205 } else {
9206 *staged_status = GOT_STATUS_NO_CHANGE;
9207 *status = GOT_STATUS_UNVERSIONED;
9208 if (lstat(ondisk_path, &sb) == -1) {
9209 if (errno != ENOENT) {
9210 err = got_error_from_errno2("lstat",
9211 ondisk_path);
9212 goto done;
9214 *status = GOT_STATUS_NONEXISTENT;
9218 done:
9219 free(ondisk_path);
9220 return err;
9223 static const struct got_error *
9224 patch_can_rm(const char *path, unsigned char status,
9225 unsigned char staged_status)
9227 if (status == GOT_STATUS_NONEXISTENT)
9228 return got_error_set_errno(ENOENT, path);
9229 if (status != GOT_STATUS_NO_CHANGE &&
9230 status != GOT_STATUS_ADD &&
9231 status != GOT_STATUS_MODIFY &&
9232 status != GOT_STATUS_MODE_CHANGE)
9233 return got_error_path(path, GOT_ERR_FILE_STATUS);
9234 if (staged_status == GOT_STATUS_DELETE)
9235 return got_error_path(path, GOT_ERR_FILE_STATUS);
9236 return NULL;
9239 static const struct got_error *
9240 patch_can_add(const char *path, unsigned char status)
9242 if (status != GOT_STATUS_NONEXISTENT)
9243 return got_error_path(path, GOT_ERR_FILE_STATUS);
9244 return NULL;
9247 static const struct got_error *
9248 patch_can_edit(const char *path, unsigned char status,
9249 unsigned char staged_status)
9251 if (status == GOT_STATUS_NONEXISTENT)
9252 return got_error_set_errno(ENOENT, path);
9253 if (status != GOT_STATUS_NO_CHANGE &&
9254 status != GOT_STATUS_ADD &&
9255 status != GOT_STATUS_MODIFY)
9256 return got_error_path(path, GOT_ERR_FILE_STATUS);
9257 if (staged_status == GOT_STATUS_DELETE)
9258 return got_error_path(path, GOT_ERR_FILE_STATUS);
9259 return NULL;
9262 const struct got_error *
9263 got_worktree_patch_prepare(struct got_fileindex **fileindex,
9264 char **fileindex_path, struct got_worktree *worktree)
9266 return open_fileindex(fileindex, fileindex_path, worktree);
9269 const struct got_error *
9270 got_worktree_patch_check_path(const char *old, const char *new,
9271 char **oldpath, char **newpath, struct got_worktree *worktree,
9272 struct got_repository *repo, struct got_fileindex *fileindex)
9274 const struct got_error *err = NULL;
9275 int file_renamed = 0;
9276 unsigned char status_old, staged_status_old;
9277 unsigned char status_new, staged_status_new;
9279 *oldpath = NULL;
9280 *newpath = NULL;
9282 err = patch_check_path(old != NULL ? old : new, oldpath,
9283 &status_old, &staged_status_old, fileindex, worktree, repo);
9284 if (err)
9285 goto done;
9287 err = patch_check_path(new != NULL ? new : old, newpath,
9288 &status_new, &staged_status_new, fileindex, worktree, repo);
9289 if (err)
9290 goto done;
9292 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9293 file_renamed = 1;
9295 if (old != NULL && new == NULL)
9296 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9297 else if (file_renamed) {
9298 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9299 if (err == NULL)
9300 err = patch_can_add(*newpath, status_new);
9301 } else if (old == NULL)
9302 err = patch_can_add(*newpath, status_new);
9303 else
9304 err = patch_can_edit(*newpath, status_new, staged_status_new);
9306 done:
9307 if (err) {
9308 free(*oldpath);
9309 *oldpath = NULL;
9310 free(*newpath);
9311 *newpath = NULL;
9313 return err;
9316 const struct got_error *
9317 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9318 struct got_worktree *worktree, struct got_fileindex *fileindex,
9319 got_worktree_checkout_cb progress_cb, void *progress_arg)
9321 struct schedule_addition_args saa;
9323 memset(&saa, 0, sizeof(saa));
9324 saa.worktree = worktree;
9325 saa.fileindex = fileindex;
9326 saa.progress_cb = progress_cb;
9327 saa.progress_arg = progress_arg;
9328 saa.repo = repo;
9330 return worktree_status(worktree, path, fileindex, repo,
9331 schedule_addition, &saa, NULL, NULL, 1, 0);
9334 const struct got_error *
9335 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9336 struct got_worktree *worktree, struct got_fileindex *fileindex,
9337 got_worktree_delete_cb progress_cb, void *progress_arg)
9339 struct schedule_deletion_args sda;
9341 memset(&sda, 0, sizeof(sda));
9342 sda.worktree = worktree;
9343 sda.fileindex = fileindex;
9344 sda.progress_cb = progress_cb;
9345 sda.progress_arg = progress_arg;
9346 sda.repo = repo;
9347 sda.delete_local_mods = 0;
9348 sda.keep_on_disk = 0;
9349 sda.ignore_missing_paths = 0;
9350 sda.status_codes = NULL;
9352 return worktree_status(worktree, path, fileindex, repo,
9353 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9356 const struct got_error *
9357 got_worktree_patch_complete(struct got_fileindex *fileindex,
9358 const char *fileindex_path)
9360 const struct got_error *err = NULL;
9362 err = sync_fileindex(fileindex, fileindex_path);
9363 got_fileindex_free(fileindex);
9365 return err;