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));
1725 fd1 = got_opentempfd();
1726 if (fd1 == -1) {
1727 err = got_error_from_errno("got_opentempfd");
1728 goto done;
1730 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1731 if (err)
1732 goto done;
1734 if (S_ISLNK(sb->st_mode)) {
1735 err = get_symlink_modification_status(status, ie,
1736 abspath, dirfd, de_name, blob);
1737 goto done;
1740 if (dirfd != -1) {
1741 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1742 if (fd == -1) {
1743 err = got_error_from_errno2("openat", abspath);
1744 goto done;
1748 f = fdopen(fd, "r");
1749 if (f == NULL) {
1750 err = got_error_from_errno2("fdopen", abspath);
1751 goto done;
1753 fd = -1;
1754 hdrlen = got_object_blob_get_hdrlen(blob);
1755 for (;;) {
1756 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1757 err = got_object_blob_read_block(&blen, blob);
1758 if (err)
1759 goto done;
1760 /* Skip length of blob object header first time around. */
1761 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1762 if (flen == 0 && ferror(f)) {
1763 err = got_error_from_errno("fread");
1764 goto done;
1766 if (blen - hdrlen == 0) {
1767 if (flen != 0)
1768 *status = GOT_STATUS_MODIFY;
1769 break;
1770 } else if (flen == 0) {
1771 if (blen - hdrlen != 0)
1772 *status = GOT_STATUS_MODIFY;
1773 break;
1774 } else if (blen - hdrlen == flen) {
1775 /* Skip blob object header first time around. */
1776 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1777 *status = GOT_STATUS_MODIFY;
1778 break;
1780 } else {
1781 *status = GOT_STATUS_MODIFY;
1782 break;
1784 hdrlen = 0;
1787 if (*status == GOT_STATUS_MODIFY) {
1788 rewind(f);
1789 err = get_modified_file_content_status(status, f);
1790 } else if (xbit_differs(ie, sb->st_mode))
1791 *status = GOT_STATUS_MODE_CHANGE;
1792 done:
1793 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1794 err = got_error_from_errno("close");
1795 if (blob)
1796 got_object_blob_close(blob);
1797 if (f != NULL && fclose(f) == EOF && err == NULL)
1798 err = got_error_from_errno2("fclose", abspath);
1799 if (fd != -1 && close(fd) == -1 && err == NULL)
1800 err = got_error_from_errno2("close", abspath);
1801 return err;
1805 * Update timestamps in the file index if a file is unmodified and
1806 * we had to run a full content comparison to find out.
1808 static const struct got_error *
1809 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1810 struct got_fileindex_entry *ie, struct stat *sb)
1812 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1813 return got_fileindex_entry_update(ie, wt_fd, path,
1814 ie->blob_hash, ie->commit_hash, 1);
1816 return NULL;
1819 static const struct got_error *
1820 update_blob(struct got_worktree *worktree,
1821 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1822 struct got_tree_entry *te, const char *path,
1823 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1824 void *progress_arg)
1826 const struct got_error *err = NULL;
1827 struct got_blob_object *blob = NULL;
1828 char *ondisk_path = NULL;
1829 unsigned char status = GOT_STATUS_NO_CHANGE;
1830 struct stat sb;
1831 int fd1 = -1, fd2 = -1;
1833 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1834 return got_error_from_errno("asprintf");
1836 if (ie) {
1837 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1838 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1839 goto done;
1841 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1842 repo);
1843 if (err)
1844 goto done;
1845 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1846 sb.st_mode = got_fileindex_perms_to_st(ie);
1847 } else {
1848 if (stat(ondisk_path, &sb) == -1) {
1849 if (errno != ENOENT) {
1850 err = got_error_from_errno2("stat",
1851 ondisk_path);
1852 goto done;
1854 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1855 status = GOT_STATUS_UNVERSIONED;
1856 } else {
1857 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1858 status = GOT_STATUS_UNVERSIONED;
1859 else
1860 status = GOT_STATUS_OBSTRUCTED;
1864 if (status == GOT_STATUS_OBSTRUCTED) {
1865 if (ie)
1866 got_fileindex_entry_mark_skipped(ie);
1867 err = (*progress_cb)(progress_arg, status, path);
1868 goto done;
1870 if (status == GOT_STATUS_CONFLICT) {
1871 if (ie)
1872 got_fileindex_entry_mark_skipped(ie);
1873 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1874 path);
1875 goto done;
1878 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1879 (S_ISLNK(te->mode) ||
1880 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1882 * This is a regular file or an installed bad symlink.
1883 * If the file index indicates that this file is already
1884 * up-to-date with respect to the repository we can skip
1885 * updating contents of this file.
1887 if (got_fileindex_entry_has_commit(ie) &&
1888 memcmp(ie->commit_hash, worktree->base_commit_id->hash,
1889 SHA1_DIGEST_LENGTH) == 0) {
1890 /* Same commit. */
1891 err = sync_timestamps(worktree->root_fd,
1892 path, status, ie, &sb);
1893 if (err)
1894 goto done;
1895 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1896 path);
1897 goto done;
1899 if (got_fileindex_entry_has_blob(ie) &&
1900 memcmp(ie->blob_hash, te->id.hash,
1901 SHA1_DIGEST_LENGTH) == 0) {
1902 /* Different commit but the same blob. */
1903 err = sync_timestamps(worktree->root_fd,
1904 path, status, ie, &sb);
1905 if (err)
1906 goto done;
1907 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1908 path);
1909 goto done;
1913 fd1 = got_opentempfd();
1914 if (fd1 == -1) {
1915 err = got_error_from_errno("got_opentempfd");
1916 goto done;
1918 err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
1919 if (err)
1920 goto done;
1922 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1923 int update_timestamps;
1924 struct got_blob_object *blob2 = NULL;
1925 char *label_orig = NULL;
1926 if (got_fileindex_entry_has_blob(ie)) {
1927 fd2 = got_opentempfd();
1928 if (fd2 == -1) {
1929 err = got_error_from_errno("got_opentempfd");
1930 goto done;
1932 struct got_object_id id2;
1933 memcpy(id2.hash, ie->blob_hash, SHA1_DIGEST_LENGTH);
1934 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
1935 fd2);
1936 if (err)
1937 goto done;
1939 if (got_fileindex_entry_has_commit(ie)) {
1940 char id_str[SHA1_DIGEST_STRING_LENGTH];
1941 if (got_sha1_digest_to_str(ie->commit_hash, id_str,
1942 sizeof(id_str)) == NULL) {
1943 err = got_error_path(id_str,
1944 GOT_ERR_BAD_OBJ_ID_STR);
1945 goto done;
1947 if (asprintf(&label_orig, "%s: commit %s",
1948 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1949 err = got_error_from_errno("asprintf");
1950 goto done;
1953 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1954 char *link_target;
1955 err = got_object_blob_read_to_str(&link_target, blob);
1956 if (err)
1957 goto done;
1958 err = merge_symlink(worktree, blob2, ondisk_path, path,
1959 label_orig, link_target, worktree->base_commit_id,
1960 repo, progress_cb, progress_arg);
1961 free(link_target);
1962 } else {
1963 err = merge_blob(&update_timestamps, worktree, blob2,
1964 ondisk_path, path, sb.st_mode, label_orig, blob,
1965 worktree->base_commit_id, repo,
1966 progress_cb, progress_arg);
1968 free(label_orig);
1969 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
1970 err = got_error_from_errno("close");
1971 goto done;
1973 if (blob2)
1974 got_object_blob_close(blob2);
1975 if (err)
1976 goto done;
1978 * Do not update timestamps of files with local changes.
1979 * Otherwise, a future status walk would treat them as
1980 * unmodified files again.
1982 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1983 blob->id.hash, worktree->base_commit_id->hash,
1984 update_timestamps);
1985 } else if (status == GOT_STATUS_MODE_CHANGE) {
1986 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1987 blob->id.hash, worktree->base_commit_id->hash, 0);
1988 } else if (status == GOT_STATUS_DELETE) {
1989 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1990 if (err)
1991 goto done;
1992 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1993 blob->id.hash, worktree->base_commit_id->hash, 0);
1994 if (err)
1995 goto done;
1996 } else {
1997 int is_bad_symlink = 0;
1998 if (S_ISLNK(te->mode)) {
1999 err = install_symlink(&is_bad_symlink, worktree,
2000 ondisk_path, path, blob,
2001 status == GOT_STATUS_MISSING, 0,
2002 status == GOT_STATUS_UNVERSIONED, 0,
2003 repo, progress_cb, progress_arg);
2004 } else {
2005 err = install_blob(worktree, ondisk_path, path,
2006 te->mode, sb.st_mode, blob,
2007 status == GOT_STATUS_MISSING, 0, 0,
2008 status == GOT_STATUS_UNVERSIONED, repo,
2009 progress_cb, progress_arg);
2011 if (err)
2012 goto done;
2014 if (ie) {
2015 err = got_fileindex_entry_update(ie,
2016 worktree->root_fd, path, blob->id.hash,
2017 worktree->base_commit_id->hash, 1);
2018 } else {
2019 err = create_fileindex_entry(&ie, fileindex,
2020 worktree->base_commit_id, worktree->root_fd, path,
2021 &blob->id);
2023 if (err)
2024 goto done;
2026 if (is_bad_symlink) {
2027 got_fileindex_entry_filetype_set(ie,
2028 GOT_FILEIDX_MODE_BAD_SYMLINK);
2032 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2033 err = got_error_from_errno("close");
2034 goto done;
2036 got_object_blob_close(blob);
2037 done:
2038 free(ondisk_path);
2039 return err;
2042 static const struct got_error *
2043 remove_ondisk_file(const char *root_path, const char *path)
2045 const struct got_error *err = NULL;
2046 char *ondisk_path = NULL, *parent = NULL;
2048 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2049 return got_error_from_errno("asprintf");
2051 if (unlink(ondisk_path) == -1) {
2052 if (errno != ENOENT)
2053 err = got_error_from_errno2("unlink", ondisk_path);
2054 } else {
2055 size_t root_len = strlen(root_path);
2056 err = got_path_dirname(&parent, ondisk_path);
2057 if (err)
2058 goto done;
2059 while (got_path_cmp(parent, root_path,
2060 strlen(parent), root_len) != 0) {
2061 free(ondisk_path);
2062 ondisk_path = parent;
2063 parent = NULL;
2064 if (rmdir(ondisk_path) == -1) {
2065 if (errno != ENOTEMPTY)
2066 err = got_error_from_errno2("rmdir",
2067 ondisk_path);
2068 break;
2070 err = got_path_dirname(&parent, ondisk_path);
2071 if (err)
2072 break;
2075 done:
2076 free(ondisk_path);
2077 free(parent);
2078 return err;
2081 static const struct got_error *
2082 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2083 struct got_fileindex_entry *ie, struct got_repository *repo,
2084 got_worktree_checkout_cb progress_cb, void *progress_arg)
2086 const struct got_error *err = NULL;
2087 unsigned char status;
2088 struct stat sb;
2089 char *ondisk_path;
2091 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2092 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2094 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2095 == -1)
2096 return got_error_from_errno("asprintf");
2098 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2099 if (err)
2100 goto done;
2102 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2103 char ondisk_target[PATH_MAX];
2104 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2105 sizeof(ondisk_target));
2106 if (ondisk_len == -1) {
2107 err = got_error_from_errno2("readlink", ondisk_path);
2108 goto done;
2110 ondisk_target[ondisk_len] = '\0';
2111 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2112 NULL, NULL, /* XXX pass common ancestor info? */
2113 ondisk_target, ondisk_path);
2114 if (err)
2115 goto done;
2116 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2117 ie->path);
2118 goto done;
2121 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2122 status == GOT_STATUS_ADD) {
2123 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2124 if (err)
2125 goto done;
2127 * Preserve the working file and change the deleted blob's
2128 * entry into a schedule-add entry.
2130 err = got_fileindex_entry_update(ie, worktree->root_fd,
2131 ie->path, NULL, NULL, 0);
2132 } else {
2133 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2134 if (err)
2135 goto done;
2136 if (status == GOT_STATUS_NO_CHANGE) {
2137 err = remove_ondisk_file(worktree->root_path, ie->path);
2138 if (err)
2139 goto done;
2141 got_fileindex_entry_remove(fileindex, ie);
2143 done:
2144 free(ondisk_path);
2145 return err;
2148 struct diff_cb_arg {
2149 struct got_fileindex *fileindex;
2150 struct got_worktree *worktree;
2151 struct got_repository *repo;
2152 got_worktree_checkout_cb progress_cb;
2153 void *progress_arg;
2154 got_cancel_cb cancel_cb;
2155 void *cancel_arg;
2158 static const struct got_error *
2159 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2160 struct got_tree_entry *te, const char *parent_path)
2162 struct diff_cb_arg *a = arg;
2164 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2165 return got_error(GOT_ERR_CANCELLED);
2167 return update_blob(a->worktree, a->fileindex, ie, te,
2168 ie->path, a->repo, a->progress_cb, a->progress_arg);
2171 static const struct got_error *
2172 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2174 struct diff_cb_arg *a = arg;
2176 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2177 return got_error(GOT_ERR_CANCELLED);
2179 return delete_blob(a->worktree, a->fileindex, ie,
2180 a->repo, a->progress_cb, a->progress_arg);
2183 static const struct got_error *
2184 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2186 struct diff_cb_arg *a = arg;
2187 const struct got_error *err;
2188 char *path;
2190 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2191 return got_error(GOT_ERR_CANCELLED);
2193 if (got_object_tree_entry_is_submodule(te))
2194 return NULL;
2196 if (asprintf(&path, "%s%s%s", parent_path,
2197 parent_path[0] ? "/" : "", te->name)
2198 == -1)
2199 return got_error_from_errno("asprintf");
2201 if (S_ISDIR(te->mode))
2202 err = add_dir_on_disk(a->worktree, path);
2203 else
2204 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2205 a->repo, a->progress_cb, a->progress_arg);
2207 free(path);
2208 return err;
2211 const struct got_error *
2212 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2214 uint32_t uuid_status;
2216 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2217 if (uuid_status != uuid_s_ok) {
2218 *uuidstr = NULL;
2219 return got_error_uuid(uuid_status, "uuid_to_string");
2222 return NULL;
2225 static const struct got_error *
2226 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2228 const struct got_error *err = NULL;
2229 char *uuidstr = NULL;
2231 *refname = NULL;
2233 err = got_worktree_get_uuid(&uuidstr, worktree);
2234 if (err)
2235 return err;
2237 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2238 err = got_error_from_errno("asprintf");
2239 *refname = NULL;
2241 free(uuidstr);
2242 return err;
2245 const struct got_error *
2246 got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2247 const char *prefix)
2249 return get_ref_name(refname, worktree, prefix);
2252 const struct got_error *
2253 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2255 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2258 static const struct got_error *
2259 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2261 return get_ref_name(refname, worktree,
2262 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2265 static const struct got_error *
2266 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2268 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2271 static const struct got_error *
2272 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2274 return get_ref_name(refname, worktree,
2275 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2278 static const struct got_error *
2279 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2281 return get_ref_name(refname, worktree,
2282 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2285 static const struct got_error *
2286 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2288 return get_ref_name(refname, worktree,
2289 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2292 static const struct got_error *
2293 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2295 return get_ref_name(refname, worktree,
2296 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2299 static const struct got_error *
2300 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2302 return get_ref_name(refname, worktree,
2303 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2306 static const struct got_error *
2307 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2309 return get_ref_name(refname, worktree,
2310 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2313 const struct got_error *
2314 got_worktree_get_histedit_script_path(char **path,
2315 struct got_worktree *worktree)
2317 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2318 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2319 *path = NULL;
2320 return got_error_from_errno("asprintf");
2322 return NULL;
2325 static const struct got_error *
2326 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2328 return get_ref_name(refname, worktree,
2329 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2332 static const struct got_error *
2333 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2335 return get_ref_name(refname, worktree,
2336 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2340 * Prevent Git's garbage collector from deleting our base commit by
2341 * setting a reference to our base commit's ID.
2343 static const struct got_error *
2344 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2346 const struct got_error *err = NULL;
2347 struct got_reference *ref = NULL;
2348 char *refname;
2350 err = got_worktree_get_base_ref_name(&refname, worktree);
2351 if (err)
2352 return err;
2354 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2355 if (err)
2356 goto done;
2358 err = got_ref_write(ref, repo);
2359 done:
2360 free(refname);
2361 if (ref)
2362 got_ref_close(ref);
2363 return err;
2366 static const struct got_error *
2367 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2369 const struct got_error *err = NULL;
2371 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2372 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2373 err = got_error_from_errno("asprintf");
2374 *fileindex_path = NULL;
2376 return err;
2380 static const struct got_error *
2381 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2382 struct got_worktree *worktree)
2384 const struct got_error *err = NULL;
2385 FILE *index = NULL;
2387 *fileindex_path = NULL;
2388 *fileindex = got_fileindex_alloc();
2389 if (*fileindex == NULL)
2390 return got_error_from_errno("got_fileindex_alloc");
2392 err = get_fileindex_path(fileindex_path, worktree);
2393 if (err)
2394 goto done;
2396 index = fopen(*fileindex_path, "rbe");
2397 if (index == NULL) {
2398 if (errno != ENOENT)
2399 err = got_error_from_errno2("fopen", *fileindex_path);
2400 } else {
2401 err = got_fileindex_read(*fileindex, index);
2402 if (fclose(index) == EOF && err == NULL)
2403 err = got_error_from_errno("fclose");
2405 done:
2406 if (err) {
2407 free(*fileindex_path);
2408 *fileindex_path = NULL;
2409 got_fileindex_free(*fileindex);
2410 *fileindex = NULL;
2412 return err;
2415 struct bump_base_commit_id_arg {
2416 struct got_object_id *base_commit_id;
2417 const char *path;
2418 size_t path_len;
2419 const char *entry_name;
2420 got_worktree_checkout_cb progress_cb;
2421 void *progress_arg;
2424 /* Bump base commit ID of all files within an updated part of the work tree. */
2425 static const struct got_error *
2426 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2428 const struct got_error *err;
2429 struct bump_base_commit_id_arg *a = arg;
2431 if (a->entry_name) {
2432 if (strcmp(ie->path, a->path) != 0)
2433 return NULL;
2434 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2435 return NULL;
2437 if (got_fileindex_entry_was_skipped(ie))
2438 return NULL;
2440 if (memcmp(ie->commit_hash, a->base_commit_id->hash,
2441 SHA1_DIGEST_LENGTH) == 0)
2442 return NULL;
2444 if (a->progress_cb) {
2445 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2446 ie->path);
2447 if (err)
2448 return err;
2450 memcpy(ie->commit_hash, a->base_commit_id->hash, SHA1_DIGEST_LENGTH);
2451 return NULL;
2454 static const struct got_error *
2455 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2456 struct got_fileindex *fileindex,
2457 got_worktree_checkout_cb progress_cb, void *progress_arg)
2459 struct bump_base_commit_id_arg bbc_arg;
2461 bbc_arg.base_commit_id = worktree->base_commit_id;
2462 bbc_arg.entry_name = NULL;
2463 bbc_arg.path = "";
2464 bbc_arg.path_len = 0;
2465 bbc_arg.progress_cb = progress_cb;
2466 bbc_arg.progress_arg = progress_arg;
2468 return got_fileindex_for_each_entry_safe(fileindex,
2469 bump_base_commit_id, &bbc_arg);
2472 static const struct got_error *
2473 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2475 const struct got_error *err = NULL;
2476 char *new_fileindex_path = NULL;
2477 FILE *new_index = NULL;
2478 struct timespec timeout;
2480 err = got_opentemp_named(&new_fileindex_path, &new_index,
2481 fileindex_path, "");
2482 if (err)
2483 goto done;
2485 err = got_fileindex_write(fileindex, new_index);
2486 if (err)
2487 goto done;
2489 if (rename(new_fileindex_path, fileindex_path) != 0) {
2490 err = got_error_from_errno3("rename", new_fileindex_path,
2491 fileindex_path);
2492 unlink(new_fileindex_path);
2496 * Sleep for a short amount of time to ensure that files modified after
2497 * this program exits have a different time stamp from the one which
2498 * was recorded in the file index.
2500 timeout.tv_sec = 0;
2501 timeout.tv_nsec = 1;
2502 nanosleep(&timeout, NULL);
2503 done:
2504 if (new_index)
2505 fclose(new_index);
2506 free(new_fileindex_path);
2507 return err;
2510 static const struct got_error *
2511 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2512 struct got_object_id **tree_id, const char *wt_relpath,
2513 struct got_commit_object *base_commit, struct got_worktree *worktree,
2514 struct got_repository *repo)
2516 const struct got_error *err = NULL;
2517 struct got_object_id *id = NULL;
2518 char *in_repo_path = NULL;
2519 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2521 *entry_type = GOT_OBJ_TYPE_ANY;
2522 *tree_relpath = NULL;
2523 *tree_id = NULL;
2525 if (wt_relpath[0] == '\0') {
2526 /* Check out all files within the work tree. */
2527 *entry_type = GOT_OBJ_TYPE_TREE;
2528 *tree_relpath = strdup("");
2529 if (*tree_relpath == NULL) {
2530 err = got_error_from_errno("strdup");
2531 goto done;
2533 err = got_object_id_by_path(tree_id, repo, base_commit,
2534 worktree->path_prefix);
2535 if (err)
2536 goto done;
2537 return NULL;
2540 /* Check out a subset of files in the work tree. */
2542 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2543 is_root_wt ? "" : "/", wt_relpath) == -1) {
2544 err = got_error_from_errno("asprintf");
2545 goto done;
2548 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2549 if (err)
2550 goto done;
2552 free(in_repo_path);
2553 in_repo_path = NULL;
2555 err = got_object_get_type(entry_type, repo, id);
2556 if (err)
2557 goto done;
2559 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2560 /* Check out a single file. */
2561 if (strchr(wt_relpath, '/') == NULL) {
2562 /* Check out a single file in work tree's root dir. */
2563 in_repo_path = strdup(worktree->path_prefix);
2564 if (in_repo_path == NULL) {
2565 err = got_error_from_errno("strdup");
2566 goto done;
2568 *tree_relpath = strdup("");
2569 if (*tree_relpath == NULL) {
2570 err = got_error_from_errno("strdup");
2571 goto done;
2573 } else {
2574 /* Check out a single file in a subdirectory. */
2575 err = got_path_dirname(tree_relpath, wt_relpath);
2576 if (err)
2577 return err;
2578 if (asprintf(&in_repo_path, "%s%s%s",
2579 worktree->path_prefix, is_root_wt ? "" : "/",
2580 *tree_relpath) == -1) {
2581 err = got_error_from_errno("asprintf");
2582 goto done;
2585 err = got_object_id_by_path(tree_id, repo,
2586 base_commit, in_repo_path);
2587 } else {
2588 /* Check out all files within a subdirectory. */
2589 *tree_id = got_object_id_dup(id);
2590 if (*tree_id == NULL) {
2591 err = got_error_from_errno("got_object_id_dup");
2592 goto done;
2594 *tree_relpath = strdup(wt_relpath);
2595 if (*tree_relpath == NULL) {
2596 err = got_error_from_errno("strdup");
2597 goto done;
2600 done:
2601 free(id);
2602 free(in_repo_path);
2603 if (err) {
2604 *entry_type = GOT_OBJ_TYPE_ANY;
2605 free(*tree_relpath);
2606 *tree_relpath = NULL;
2607 free(*tree_id);
2608 *tree_id = NULL;
2610 return err;
2613 static const struct got_error *
2614 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2615 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2616 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2617 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2619 const struct got_error *err = NULL;
2620 struct got_commit_object *commit = NULL;
2621 struct got_tree_object *tree = NULL;
2622 struct got_fileindex_diff_tree_cb diff_cb;
2623 struct diff_cb_arg arg;
2625 err = ref_base_commit(worktree, repo);
2626 if (err) {
2627 if (!(err->code == GOT_ERR_ERRNO &&
2628 (errno == EACCES || errno == EROFS)))
2629 goto done;
2630 err = (*progress_cb)(progress_arg,
2631 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2632 if (err)
2633 return err;
2636 err = got_object_open_as_commit(&commit, repo,
2637 worktree->base_commit_id);
2638 if (err)
2639 goto done;
2641 err = got_object_open_as_tree(&tree, repo, tree_id);
2642 if (err)
2643 goto done;
2645 if (entry_name &&
2646 got_object_tree_find_entry(tree, entry_name) == NULL) {
2647 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2648 goto done;
2651 diff_cb.diff_old_new = diff_old_new;
2652 diff_cb.diff_old = diff_old;
2653 diff_cb.diff_new = diff_new;
2654 arg.fileindex = fileindex;
2655 arg.worktree = worktree;
2656 arg.repo = repo;
2657 arg.progress_cb = progress_cb;
2658 arg.progress_arg = progress_arg;
2659 arg.cancel_cb = cancel_cb;
2660 arg.cancel_arg = cancel_arg;
2661 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2662 entry_name, repo, &diff_cb, &arg);
2663 done:
2664 if (tree)
2665 got_object_tree_close(tree);
2666 if (commit)
2667 got_object_commit_close(commit);
2668 return err;
2671 const struct got_error *
2672 got_worktree_checkout_files(struct got_worktree *worktree,
2673 struct got_pathlist_head *paths, struct got_repository *repo,
2674 got_worktree_checkout_cb progress_cb, void *progress_arg,
2675 got_cancel_cb cancel_cb, void *cancel_arg)
2677 const struct got_error *err = NULL, *sync_err, *unlockerr;
2678 struct got_commit_object *commit = NULL;
2679 struct got_tree_object *tree = NULL;
2680 struct got_fileindex *fileindex = NULL;
2681 char *fileindex_path = NULL;
2682 struct got_pathlist_entry *pe;
2683 struct tree_path_data {
2684 STAILQ_ENTRY(tree_path_data) entry;
2685 struct got_object_id *tree_id;
2686 int entry_type;
2687 char *relpath;
2688 char *entry_name;
2689 } *tpd = NULL;
2690 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2692 STAILQ_INIT(&tree_paths);
2694 err = lock_worktree(worktree, LOCK_EX);
2695 if (err)
2696 return err;
2698 err = got_object_open_as_commit(&commit, repo,
2699 worktree->base_commit_id);
2700 if (err)
2701 goto done;
2703 /* Map all specified paths to in-repository trees. */
2704 TAILQ_FOREACH(pe, paths, entry) {
2705 tpd = malloc(sizeof(*tpd));
2706 if (tpd == NULL) {
2707 err = got_error_from_errno("malloc");
2708 goto done;
2711 err = find_tree_entry_for_checkout(&tpd->entry_type,
2712 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2713 worktree, repo);
2714 if (err) {
2715 free(tpd);
2716 goto done;
2719 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2720 err = got_path_basename(&tpd->entry_name, pe->path);
2721 if (err) {
2722 free(tpd->relpath);
2723 free(tpd->tree_id);
2724 free(tpd);
2725 goto done;
2727 } else
2728 tpd->entry_name = NULL;
2730 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2734 * Read the file index.
2735 * Checking out files is supposed to be an idempotent operation.
2736 * If the on-disk file index is incomplete we will try to complete it.
2738 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2739 if (err)
2740 goto done;
2742 tpd = STAILQ_FIRST(&tree_paths);
2743 TAILQ_FOREACH(pe, paths, entry) {
2744 struct bump_base_commit_id_arg bbc_arg;
2746 err = checkout_files(worktree, fileindex, tpd->relpath,
2747 tpd->tree_id, tpd->entry_name, repo,
2748 progress_cb, progress_arg, cancel_cb, cancel_arg);
2749 if (err)
2750 break;
2752 bbc_arg.base_commit_id = worktree->base_commit_id;
2753 bbc_arg.entry_name = tpd->entry_name;
2754 bbc_arg.path = pe->path;
2755 bbc_arg.path_len = pe->path_len;
2756 bbc_arg.progress_cb = progress_cb;
2757 bbc_arg.progress_arg = progress_arg;
2758 err = got_fileindex_for_each_entry_safe(fileindex,
2759 bump_base_commit_id, &bbc_arg);
2760 if (err)
2761 break;
2763 tpd = STAILQ_NEXT(tpd, entry);
2765 sync_err = sync_fileindex(fileindex, fileindex_path);
2766 if (sync_err && err == NULL)
2767 err = sync_err;
2768 done:
2769 free(fileindex_path);
2770 if (tree)
2771 got_object_tree_close(tree);
2772 if (commit)
2773 got_object_commit_close(commit);
2774 if (fileindex)
2775 got_fileindex_free(fileindex);
2776 while (!STAILQ_EMPTY(&tree_paths)) {
2777 tpd = STAILQ_FIRST(&tree_paths);
2778 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2779 free(tpd->relpath);
2780 free(tpd->tree_id);
2781 free(tpd);
2783 unlockerr = lock_worktree(worktree, LOCK_SH);
2784 if (unlockerr && err == NULL)
2785 err = unlockerr;
2786 return err;
2789 struct merge_file_cb_arg {
2790 struct got_worktree *worktree;
2791 struct got_fileindex *fileindex;
2792 got_worktree_checkout_cb progress_cb;
2793 void *progress_arg;
2794 got_cancel_cb cancel_cb;
2795 void *cancel_arg;
2796 const char *label_orig;
2797 struct got_object_id *commit_id2;
2798 int allow_bad_symlinks;
2801 static const struct got_error *
2802 merge_file_cb(void *arg, struct got_blob_object *blob1,
2803 struct got_blob_object *blob2, FILE *f1, FILE *f2,
2804 struct got_object_id *id1, struct got_object_id *id2,
2805 const char *path1, const char *path2,
2806 mode_t mode1, mode_t mode2, struct got_repository *repo)
2808 static const struct got_error *err = NULL;
2809 struct merge_file_cb_arg *a = arg;
2810 struct got_fileindex_entry *ie;
2811 char *ondisk_path = NULL;
2812 struct stat sb;
2813 unsigned char status;
2814 int local_changes_subsumed;
2815 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2816 char *id_str = NULL, *label_deriv2 = NULL;
2818 if (blob1 && blob2) {
2819 ie = got_fileindex_entry_get(a->fileindex, path2,
2820 strlen(path2));
2821 if (ie == NULL)
2822 return (*a->progress_cb)(a->progress_arg,
2823 GOT_STATUS_MISSING, path2);
2825 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2826 path2) == -1)
2827 return got_error_from_errno("asprintf");
2829 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2830 repo);
2831 if (err)
2832 goto done;
2834 if (status == GOT_STATUS_DELETE) {
2835 err = (*a->progress_cb)(a->progress_arg,
2836 GOT_STATUS_MERGE, path2);
2837 goto done;
2839 if (status != GOT_STATUS_NO_CHANGE &&
2840 status != GOT_STATUS_MODIFY &&
2841 status != GOT_STATUS_CONFLICT &&
2842 status != GOT_STATUS_ADD) {
2843 err = (*a->progress_cb)(a->progress_arg, status, path2);
2844 goto done;
2847 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2848 char *link_target2;
2849 err = got_object_blob_read_to_str(&link_target2, blob2);
2850 if (err)
2851 goto done;
2852 err = merge_symlink(a->worktree, blob1, ondisk_path,
2853 path2, a->label_orig, link_target2, a->commit_id2,
2854 repo, a->progress_cb, a->progress_arg);
2855 free(link_target2);
2856 } else {
2857 int fd;
2859 f_orig = got_opentemp();
2860 if (f_orig == NULL) {
2861 err = got_error_from_errno("got_opentemp");
2862 goto done;
2864 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2865 f_orig, blob1);
2866 if (err)
2867 goto done;
2869 f_deriv2 = got_opentemp();
2870 if (f_deriv2 == NULL)
2871 goto done;
2872 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2873 f_deriv2, blob2);
2874 if (err)
2875 goto done;
2877 fd = open(ondisk_path,
2878 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2879 if (fd == -1) {
2880 err = got_error_from_errno2("open",
2881 ondisk_path);
2882 goto done;
2884 f_deriv = fdopen(fd, "r");
2885 if (f_deriv == NULL) {
2886 err = got_error_from_errno2("fdopen",
2887 ondisk_path);
2888 close(fd);
2889 goto done;
2891 err = got_object_id_str(&id_str, a->commit_id2);
2892 if (err)
2893 goto done;
2894 if (asprintf(&label_deriv2, "%s: commit %s",
2895 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2896 err = got_error_from_errno("asprintf");
2897 goto done;
2899 err = merge_file(&local_changes_subsumed, a->worktree,
2900 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2901 mode2, a->label_orig, NULL, label_deriv2,
2902 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2903 a->progress_cb, a->progress_arg);
2905 } else if (blob1) {
2906 ie = got_fileindex_entry_get(a->fileindex, path1,
2907 strlen(path1));
2908 if (ie == NULL)
2909 return (*a->progress_cb)(a->progress_arg,
2910 GOT_STATUS_MISSING, path1);
2912 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2913 path1) == -1)
2914 return got_error_from_errno("asprintf");
2916 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2917 repo);
2918 if (err)
2919 goto done;
2921 switch (status) {
2922 case GOT_STATUS_NO_CHANGE:
2923 err = (*a->progress_cb)(a->progress_arg,
2924 GOT_STATUS_DELETE, path1);
2925 if (err)
2926 goto done;
2927 err = remove_ondisk_file(a->worktree->root_path, path1);
2928 if (err)
2929 goto done;
2930 if (ie)
2931 got_fileindex_entry_mark_deleted_from_disk(ie);
2932 break;
2933 case GOT_STATUS_DELETE:
2934 case GOT_STATUS_MISSING:
2935 err = (*a->progress_cb)(a->progress_arg,
2936 GOT_STATUS_DELETE, path1);
2937 if (err)
2938 goto done;
2939 if (ie)
2940 got_fileindex_entry_mark_deleted_from_disk(ie);
2941 break;
2942 case GOT_STATUS_ADD: {
2943 struct got_object_id *id;
2944 FILE *blob1_f;
2945 off_t blob1_size;
2947 * Delete the added file only if its content already
2948 * exists in the repository.
2950 err = got_object_blob_file_create(&id, &blob1_f,
2951 &blob1_size, path1);
2952 if (err)
2953 goto done;
2954 if (got_object_id_cmp(id, id1) == 0) {
2955 err = (*a->progress_cb)(a->progress_arg,
2956 GOT_STATUS_DELETE, path1);
2957 if (err)
2958 goto done;
2959 err = remove_ondisk_file(a->worktree->root_path,
2960 path1);
2961 if (err)
2962 goto done;
2963 if (ie)
2964 got_fileindex_entry_remove(a->fileindex,
2965 ie);
2966 } else {
2967 err = (*a->progress_cb)(a->progress_arg,
2968 GOT_STATUS_CANNOT_DELETE, path1);
2970 if (fclose(blob1_f) == EOF && err == NULL)
2971 err = got_error_from_errno("fclose");
2972 free(id);
2973 if (err)
2974 goto done;
2975 break;
2977 case GOT_STATUS_MODIFY:
2978 case GOT_STATUS_CONFLICT:
2979 err = (*a->progress_cb)(a->progress_arg,
2980 GOT_STATUS_CANNOT_DELETE, path1);
2981 if (err)
2982 goto done;
2983 break;
2984 case GOT_STATUS_OBSTRUCTED:
2985 err = (*a->progress_cb)(a->progress_arg, status, path1);
2986 if (err)
2987 goto done;
2988 break;
2989 default:
2990 break;
2992 } else if (blob2) {
2993 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2994 path2) == -1)
2995 return got_error_from_errno("asprintf");
2996 ie = got_fileindex_entry_get(a->fileindex, path2,
2997 strlen(path2));
2998 if (ie) {
2999 err = get_file_status(&status, &sb, ie, ondisk_path,
3000 -1, NULL, repo);
3001 if (err)
3002 goto done;
3003 if (status != GOT_STATUS_NO_CHANGE &&
3004 status != GOT_STATUS_MODIFY &&
3005 status != GOT_STATUS_CONFLICT &&
3006 status != GOT_STATUS_ADD) {
3007 err = (*a->progress_cb)(a->progress_arg,
3008 status, path2);
3009 goto done;
3011 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3012 char *link_target2;
3013 err = got_object_blob_read_to_str(&link_target2,
3014 blob2);
3015 if (err)
3016 goto done;
3017 err = merge_symlink(a->worktree, NULL,
3018 ondisk_path, path2, a->label_orig,
3019 link_target2, a->commit_id2, repo,
3020 a->progress_cb, a->progress_arg);
3021 free(link_target2);
3022 } else if (S_ISREG(sb.st_mode)) {
3023 err = merge_blob(&local_changes_subsumed,
3024 a->worktree, NULL, ondisk_path, path2,
3025 sb.st_mode, a->label_orig, blob2,
3026 a->commit_id2, repo, a->progress_cb,
3027 a->progress_arg);
3028 } else {
3029 err = got_error_path(ondisk_path,
3030 GOT_ERR_FILE_OBSTRUCTED);
3032 if (err)
3033 goto done;
3034 if (status == GOT_STATUS_DELETE) {
3035 err = got_fileindex_entry_update(ie,
3036 a->worktree->root_fd, path2, blob2->id.hash,
3037 a->worktree->base_commit_id->hash, 0);
3038 if (err)
3039 goto done;
3041 } else {
3042 int is_bad_symlink = 0;
3043 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3044 if (S_ISLNK(mode2)) {
3045 err = install_symlink(&is_bad_symlink,
3046 a->worktree, ondisk_path, path2, blob2, 0,
3047 0, 1, a->allow_bad_symlinks, repo,
3048 a->progress_cb, a->progress_arg);
3049 } else {
3050 err = install_blob(a->worktree, ondisk_path, path2,
3051 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3052 a->progress_cb, a->progress_arg);
3054 if (err)
3055 goto done;
3056 err = got_fileindex_entry_alloc(&ie, path2);
3057 if (err)
3058 goto done;
3059 err = got_fileindex_entry_update(ie,
3060 a->worktree->root_fd, path2, NULL, NULL, 1);
3061 if (err) {
3062 got_fileindex_entry_free(ie);
3063 goto done;
3065 err = got_fileindex_entry_add(a->fileindex, ie);
3066 if (err) {
3067 got_fileindex_entry_free(ie);
3068 goto done;
3070 if (is_bad_symlink) {
3071 got_fileindex_entry_filetype_set(ie,
3072 GOT_FILEIDX_MODE_BAD_SYMLINK);
3076 done:
3077 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3078 err = got_error_from_errno("fclose");
3079 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3080 err = got_error_from_errno("fclose");
3081 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3082 err = got_error_from_errno("fclose");
3083 free(id_str);
3084 free(label_deriv2);
3085 free(ondisk_path);
3086 return err;
3089 static const struct got_error *
3090 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3092 struct got_worktree *worktree = arg;
3094 /* Reject merges into a work tree with mixed base commits. */
3095 if (got_fileindex_entry_has_commit(ie) &&
3096 memcmp(ie->commit_hash, worktree->base_commit_id->hash,
3097 SHA1_DIGEST_LENGTH) != 0)
3098 return got_error(GOT_ERR_MIXED_COMMITS);
3100 return NULL;
3103 struct check_merge_conflicts_arg {
3104 struct got_worktree *worktree;
3105 struct got_fileindex *fileindex;
3106 struct got_repository *repo;
3109 static const struct got_error *
3110 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3111 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3112 struct got_object_id *id1, struct got_object_id *id2,
3113 const char *path1, const char *path2,
3114 mode_t mode1, mode_t mode2, struct got_repository *repo)
3116 const struct got_error *err = NULL;
3117 struct check_merge_conflicts_arg *a = arg;
3118 unsigned char status;
3119 struct stat sb;
3120 struct got_fileindex_entry *ie;
3121 const char *path = path2 ? path2 : path1;
3122 struct got_object_id *id = id2 ? id2 : id1;
3123 char *ondisk_path;
3125 if (id == NULL)
3126 return NULL;
3128 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3129 if (ie == NULL)
3130 return NULL;
3132 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3133 == -1)
3134 return got_error_from_errno("asprintf");
3136 /* Reject merges into a work tree with conflicted files. */
3137 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3138 free(ondisk_path);
3139 if (err)
3140 return err;
3141 if (status == GOT_STATUS_CONFLICT)
3142 return got_error(GOT_ERR_CONFLICTS);
3144 return NULL;
3147 static const struct got_error *
3148 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3149 const char *fileindex_path, struct got_object_id *commit_id1,
3150 struct got_object_id *commit_id2, struct got_repository *repo,
3151 got_worktree_checkout_cb progress_cb, void *progress_arg,
3152 got_cancel_cb cancel_cb, void *cancel_arg)
3154 const struct got_error *err = NULL, *sync_err;
3155 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3156 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3157 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3158 struct check_merge_conflicts_arg cmc_arg;
3159 struct merge_file_cb_arg arg;
3160 char *label_orig = NULL;
3161 FILE *f1 = NULL, *f2 = NULL;
3162 int fd1 = -1, fd2 = -1;
3164 if (commit_id1) {
3165 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3166 if (err)
3167 goto done;
3168 err = got_object_id_by_path(&tree_id1, repo, commit1,
3169 worktree->path_prefix);
3170 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3171 goto done;
3173 if (tree_id1) {
3174 char *id_str;
3176 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3177 if (err)
3178 goto done;
3180 err = got_object_id_str(&id_str, commit_id1);
3181 if (err)
3182 goto done;
3184 if (asprintf(&label_orig, "%s: commit %s",
3185 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3186 err = got_error_from_errno("asprintf");
3187 free(id_str);
3188 goto done;
3190 free(id_str);
3192 f1 = got_opentemp();
3193 if (f1 == NULL) {
3194 err = got_error_from_errno("got_opentemp");
3195 goto done;
3199 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3200 if (err)
3201 goto done;
3203 err = got_object_id_by_path(&tree_id2, repo, commit2,
3204 worktree->path_prefix);
3205 if (err)
3206 goto done;
3208 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3209 if (err)
3210 goto done;
3212 f2 = got_opentemp();
3213 if (f2 == NULL) {
3214 err = got_error_from_errno("got_opentemp");
3215 goto done;
3218 fd1 = got_opentempfd();
3219 if (fd1 == -1) {
3220 err = got_error_from_errno("got_opentempfd");
3221 goto done;
3224 fd2 = got_opentempfd();
3225 if (fd2 == -1) {
3226 err = got_error_from_errno("got_opentempfd");
3227 goto done;
3230 cmc_arg.worktree = worktree;
3231 cmc_arg.fileindex = fileindex;
3232 cmc_arg.repo = repo;
3233 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3234 check_merge_conflicts, &cmc_arg, 0);
3235 if (err)
3236 goto done;
3238 arg.worktree = worktree;
3239 arg.fileindex = fileindex;
3240 arg.progress_cb = progress_cb;
3241 arg.progress_arg = progress_arg;
3242 arg.cancel_cb = cancel_cb;
3243 arg.cancel_arg = cancel_arg;
3244 arg.label_orig = label_orig;
3245 arg.commit_id2 = commit_id2;
3246 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3247 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3248 merge_file_cb, &arg, 1);
3249 sync_err = sync_fileindex(fileindex, fileindex_path);
3250 if (sync_err && err == NULL)
3251 err = sync_err;
3252 done:
3253 if (commit1)
3254 got_object_commit_close(commit1);
3255 if (commit2)
3256 got_object_commit_close(commit2);
3257 if (tree1)
3258 got_object_tree_close(tree1);
3259 if (tree2)
3260 got_object_tree_close(tree2);
3261 if (f1 && fclose(f1) == EOF && err == NULL)
3262 err = got_error_from_errno("fclose");
3263 if (f2 && fclose(f2) == EOF && err == NULL)
3264 err = got_error_from_errno("fclose");
3265 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3266 err = got_error_from_errno("close");
3267 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3268 err = got_error_from_errno("close");
3269 free(label_orig);
3270 return err;
3273 const struct got_error *
3274 got_worktree_merge_files(struct got_worktree *worktree,
3275 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3276 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3277 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3279 const struct got_error *err, *unlockerr;
3280 char *fileindex_path = NULL;
3281 struct got_fileindex *fileindex = NULL;
3283 err = lock_worktree(worktree, LOCK_EX);
3284 if (err)
3285 return err;
3287 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3288 if (err)
3289 goto done;
3291 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3292 worktree);
3293 if (err)
3294 goto done;
3296 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3297 commit_id2, repo, progress_cb, progress_arg,
3298 cancel_cb, cancel_arg);
3299 done:
3300 if (fileindex)
3301 got_fileindex_free(fileindex);
3302 free(fileindex_path);
3303 unlockerr = lock_worktree(worktree, LOCK_SH);
3304 if (unlockerr && err == NULL)
3305 err = unlockerr;
3306 return err;
3309 struct diff_dir_cb_arg {
3310 struct got_fileindex *fileindex;
3311 struct got_worktree *worktree;
3312 const char *status_path;
3313 size_t status_path_len;
3314 struct got_repository *repo;
3315 got_worktree_status_cb status_cb;
3316 void *status_arg;
3317 got_cancel_cb cancel_cb;
3318 void *cancel_arg;
3319 /* A pathlist containing per-directory pathlists of ignore patterns. */
3320 struct got_pathlist_head *ignores;
3321 int report_unchanged;
3322 int no_ignores;
3325 static const struct got_error *
3326 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3327 int dirfd, const char *de_name,
3328 got_worktree_status_cb status_cb, void *status_arg,
3329 struct got_repository *repo, int report_unchanged)
3331 const struct got_error *err = NULL;
3332 unsigned char status = GOT_STATUS_NO_CHANGE;
3333 unsigned char staged_status = get_staged_status(ie);
3334 struct stat sb;
3335 struct got_object_id blob_id, commit_id, staged_blob_id;
3336 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3337 struct got_object_id *staged_blob_idp = NULL;
3339 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3340 if (err)
3341 return err;
3343 if (status == GOT_STATUS_NO_CHANGE &&
3344 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3345 return NULL;
3347 if (got_fileindex_entry_has_blob(ie)) {
3348 memcpy(blob_id.hash, ie->blob_hash, SHA1_DIGEST_LENGTH);
3349 blob_idp = &blob_id;
3351 if (got_fileindex_entry_has_commit(ie)) {
3352 memcpy(commit_id.hash, ie->commit_hash, SHA1_DIGEST_LENGTH);
3353 commit_idp = &commit_id;
3355 if (staged_status == GOT_STATUS_ADD ||
3356 staged_status == GOT_STATUS_MODIFY) {
3357 memcpy(staged_blob_id.hash, ie->staged_blob_hash,
3358 SHA1_DIGEST_LENGTH);
3359 staged_blob_idp = &staged_blob_id;
3362 return (*status_cb)(status_arg, status, staged_status,
3363 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3366 static const struct got_error *
3367 status_old_new(void *arg, struct got_fileindex_entry *ie,
3368 struct dirent *de, const char *parent_path, int dirfd)
3370 const struct got_error *err = NULL;
3371 struct diff_dir_cb_arg *a = arg;
3372 char *abspath;
3374 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3375 return got_error(GOT_ERR_CANCELLED);
3377 if (got_path_cmp(parent_path, a->status_path,
3378 strlen(parent_path), a->status_path_len) != 0 &&
3379 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3380 return NULL;
3382 if (parent_path[0]) {
3383 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3384 parent_path, de->d_name) == -1)
3385 return got_error_from_errno("asprintf");
3386 } else {
3387 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3388 de->d_name) == -1)
3389 return got_error_from_errno("asprintf");
3392 err = report_file_status(ie, abspath, dirfd, de->d_name,
3393 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3394 free(abspath);
3395 return err;
3398 static const struct got_error *
3399 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3401 struct diff_dir_cb_arg *a = arg;
3402 struct got_object_id blob_id, commit_id;
3403 unsigned char status;
3405 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3406 return got_error(GOT_ERR_CANCELLED);
3408 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3409 return NULL;
3411 memcpy(blob_id.hash, ie->blob_hash, SHA1_DIGEST_LENGTH);
3412 memcpy(commit_id.hash, ie->commit_hash, SHA1_DIGEST_LENGTH);
3413 if (got_fileindex_entry_has_file_on_disk(ie))
3414 status = GOT_STATUS_MISSING;
3415 else
3416 status = GOT_STATUS_DELETE;
3417 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3418 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3421 static void
3422 free_ignores(struct got_pathlist_head *ignores)
3424 struct got_pathlist_entry *pe;
3426 TAILQ_FOREACH(pe, ignores, entry) {
3427 struct got_pathlist_head *ignorelist = pe->data;
3429 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3431 got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3434 static const struct got_error *
3435 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3437 const struct got_error *err = NULL;
3438 struct got_pathlist_entry *pe = NULL;
3439 struct got_pathlist_head *ignorelist;
3440 char *line = NULL, *pattern, *dirpath = NULL;
3441 size_t linesize = 0;
3442 ssize_t linelen;
3444 ignorelist = calloc(1, sizeof(*ignorelist));
3445 if (ignorelist == NULL)
3446 return got_error_from_errno("calloc");
3447 TAILQ_INIT(ignorelist);
3449 while ((linelen = getline(&line, &linesize, f)) != -1) {
3450 if (linelen > 0 && line[linelen - 1] == '\n')
3451 line[linelen - 1] = '\0';
3453 /* Git's ignores may contain comments. */
3454 if (line[0] == '#')
3455 continue;
3457 /* Git's negated patterns are not (yet?) supported. */
3458 if (line[0] == '!')
3459 continue;
3461 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3462 line) == -1) {
3463 err = got_error_from_errno("asprintf");
3464 goto done;
3466 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3467 if (err)
3468 goto done;
3470 if (ferror(f)) {
3471 err = got_error_from_errno("getline");
3472 goto done;
3475 dirpath = strdup(path);
3476 if (dirpath == NULL) {
3477 err = got_error_from_errno("strdup");
3478 goto done;
3480 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3481 done:
3482 free(line);
3483 if (err || pe == NULL) {
3484 free(dirpath);
3485 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3487 return err;
3490 static int
3491 match_ignores(struct got_pathlist_head *ignores, const char *path)
3493 struct got_pathlist_entry *pe;
3495 /* Handle patterns which match in all directories. */
3496 TAILQ_FOREACH(pe, ignores, entry) {
3497 struct got_pathlist_head *ignorelist = pe->data;
3498 struct got_pathlist_entry *pi;
3500 TAILQ_FOREACH(pi, ignorelist, entry) {
3501 const char *p, *pattern = pi->path;
3503 if (strncmp(pattern, "**/", 3) != 0)
3504 continue;
3505 pattern += 3;
3506 p = path;
3507 while (*p) {
3508 if (fnmatch(pattern, p,
3509 FNM_PATHNAME | FNM_LEADING_DIR)) {
3510 /* Retry in next directory. */
3511 while (*p && *p != '/')
3512 p++;
3513 while (*p == '/')
3514 p++;
3515 continue;
3517 return 1;
3523 * The ignores pathlist contains ignore lists from children before
3524 * parents, so we can find the most specific ignorelist by walking
3525 * ignores backwards.
3527 pe = TAILQ_LAST(ignores, got_pathlist_head);
3528 while (pe) {
3529 if (got_path_is_child(path, pe->path, pe->path_len)) {
3530 struct got_pathlist_head *ignorelist = pe->data;
3531 struct got_pathlist_entry *pi;
3532 TAILQ_FOREACH(pi, ignorelist, entry) {
3533 const char *pattern = pi->path;
3534 int flags = FNM_LEADING_DIR;
3535 if (strstr(pattern, "/**/") == NULL)
3536 flags |= FNM_PATHNAME;
3537 if (fnmatch(pattern, path, flags))
3538 continue;
3539 return 1;
3542 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3545 return 0;
3548 static const struct got_error *
3549 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3550 const char *path, int dirfd, const char *ignores_filename)
3552 const struct got_error *err = NULL;
3553 char *ignorespath;
3554 int fd = -1;
3555 FILE *ignoresfile = NULL;
3557 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3558 path[0] ? "/" : "", ignores_filename) == -1)
3559 return got_error_from_errno("asprintf");
3561 if (dirfd != -1) {
3562 fd = openat(dirfd, ignores_filename,
3563 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3564 if (fd == -1) {
3565 if (errno != ENOENT && errno != EACCES)
3566 err = got_error_from_errno2("openat",
3567 ignorespath);
3568 } else {
3569 ignoresfile = fdopen(fd, "r");
3570 if (ignoresfile == NULL)
3571 err = got_error_from_errno2("fdopen",
3572 ignorespath);
3573 else {
3574 fd = -1;
3575 err = read_ignores(ignores, path, ignoresfile);
3578 } else {
3579 ignoresfile = fopen(ignorespath, "re");
3580 if (ignoresfile == NULL) {
3581 if (errno != ENOENT && errno != EACCES)
3582 err = got_error_from_errno2("fopen",
3583 ignorespath);
3584 } else
3585 err = read_ignores(ignores, path, ignoresfile);
3588 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3589 err = got_error_from_errno2("fclose", path);
3590 if (fd != -1 && close(fd) == -1 && err == NULL)
3591 err = got_error_from_errno2("close", path);
3592 free(ignorespath);
3593 return err;
3596 static const struct got_error *
3597 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3598 int dirfd)
3600 const struct got_error *err = NULL;
3601 struct diff_dir_cb_arg *a = arg;
3602 char *path = NULL;
3604 if (ignore != NULL)
3605 *ignore = 0;
3607 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3608 return got_error(GOT_ERR_CANCELLED);
3610 if (parent_path[0]) {
3611 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3612 return got_error_from_errno("asprintf");
3613 } else {
3614 path = de->d_name;
3617 if (de->d_type == DT_DIR) {
3618 if (!a->no_ignores && ignore != NULL &&
3619 match_ignores(a->ignores, path))
3620 *ignore = 1;
3621 } else if (!match_ignores(a->ignores, path) &&
3622 got_path_is_child(path, a->status_path, a->status_path_len))
3623 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3624 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3625 if (parent_path[0])
3626 free(path);
3627 return err;
3630 static const struct got_error *
3631 status_traverse(void *arg, const char *path, int dirfd)
3633 const struct got_error *err = NULL;
3634 struct diff_dir_cb_arg *a = arg;
3636 if (a->no_ignores)
3637 return NULL;
3639 err = add_ignores(a->ignores, a->worktree->root_path,
3640 path, dirfd, ".cvsignore");
3641 if (err)
3642 return err;
3644 err = add_ignores(a->ignores, a->worktree->root_path, path,
3645 dirfd, ".gitignore");
3647 return err;
3650 static const struct got_error *
3651 report_single_file_status(const char *path, const char *ondisk_path,
3652 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3653 void *status_arg, struct got_repository *repo, int report_unchanged,
3654 struct got_pathlist_head *ignores, int no_ignores)
3656 struct got_fileindex_entry *ie;
3657 struct stat sb;
3659 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3660 if (ie)
3661 return report_file_status(ie, ondisk_path, -1, NULL,
3662 status_cb, status_arg, repo, report_unchanged);
3664 if (lstat(ondisk_path, &sb) == -1) {
3665 if (errno != ENOENT)
3666 return got_error_from_errno2("lstat", ondisk_path);
3667 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3668 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3671 if (!no_ignores && match_ignores(ignores, path))
3672 return NULL;
3674 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3675 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3676 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3678 return NULL;
3681 static const struct got_error *
3682 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3683 const char *root_path, const char *path)
3685 const struct got_error *err;
3686 char *parent_path, *next_parent_path = NULL;
3688 err = add_ignores(ignores, root_path, "", -1,
3689 ".cvsignore");
3690 if (err)
3691 return err;
3693 err = add_ignores(ignores, root_path, "", -1,
3694 ".gitignore");
3695 if (err)
3696 return err;
3698 err = got_path_dirname(&parent_path, path);
3699 if (err) {
3700 if (err->code == GOT_ERR_BAD_PATH)
3701 return NULL; /* cannot traverse parent */
3702 return err;
3704 for (;;) {
3705 err = add_ignores(ignores, root_path, parent_path, -1,
3706 ".cvsignore");
3707 if (err)
3708 break;
3709 err = add_ignores(ignores, root_path, parent_path, -1,
3710 ".gitignore");
3711 if (err)
3712 break;
3713 err = got_path_dirname(&next_parent_path, parent_path);
3714 if (err) {
3715 if (err->code == GOT_ERR_BAD_PATH)
3716 err = NULL; /* traversed everything */
3717 break;
3719 if (got_path_is_root_dir(parent_path))
3720 break;
3721 free(parent_path);
3722 parent_path = next_parent_path;
3723 next_parent_path = NULL;
3726 free(parent_path);
3727 free(next_parent_path);
3728 return err;
3731 static const struct got_error *
3732 worktree_status(struct got_worktree *worktree, const char *path,
3733 struct got_fileindex *fileindex, struct got_repository *repo,
3734 got_worktree_status_cb status_cb, void *status_arg,
3735 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3736 int report_unchanged)
3738 const struct got_error *err = NULL;
3739 int fd = -1;
3740 struct got_fileindex_diff_dir_cb fdiff_cb;
3741 struct diff_dir_cb_arg arg;
3742 char *ondisk_path = NULL;
3743 struct got_pathlist_head ignores;
3744 struct got_fileindex_entry *ie;
3746 TAILQ_INIT(&ignores);
3748 if (asprintf(&ondisk_path, "%s%s%s",
3749 worktree->root_path, path[0] ? "/" : "", path) == -1)
3750 return got_error_from_errno("asprintf");
3752 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3753 if (ie) {
3754 err = report_single_file_status(path, ondisk_path,
3755 fileindex, status_cb, status_arg, repo,
3756 report_unchanged, &ignores, no_ignores);
3757 goto done;
3760 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3761 if (fd == -1) {
3762 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3763 !got_err_open_nofollow_on_symlink())
3764 err = got_error_from_errno2("open", ondisk_path);
3765 else {
3766 if (!no_ignores) {
3767 err = add_ignores_from_parent_paths(&ignores,
3768 worktree->root_path, ondisk_path);
3769 if (err)
3770 goto done;
3772 err = report_single_file_status(path, ondisk_path,
3773 fileindex, status_cb, status_arg, repo,
3774 report_unchanged, &ignores, no_ignores);
3776 } else {
3777 fdiff_cb.diff_old_new = status_old_new;
3778 fdiff_cb.diff_old = status_old;
3779 fdiff_cb.diff_new = status_new;
3780 fdiff_cb.diff_traverse = status_traverse;
3781 arg.fileindex = fileindex;
3782 arg.worktree = worktree;
3783 arg.status_path = path;
3784 arg.status_path_len = strlen(path);
3785 arg.repo = repo;
3786 arg.status_cb = status_cb;
3787 arg.status_arg = status_arg;
3788 arg.cancel_cb = cancel_cb;
3789 arg.cancel_arg = cancel_arg;
3790 arg.report_unchanged = report_unchanged;
3791 arg.no_ignores = no_ignores;
3792 if (!no_ignores) {
3793 err = add_ignores_from_parent_paths(&ignores,
3794 worktree->root_path, path);
3795 if (err)
3796 goto done;
3798 arg.ignores = &ignores;
3799 err = got_fileindex_diff_dir(fileindex, fd,
3800 worktree->root_path, path, repo, &fdiff_cb, &arg);
3802 done:
3803 free_ignores(&ignores);
3804 if (fd != -1 && close(fd) == -1 && err == NULL)
3805 err = got_error_from_errno("close");
3806 free(ondisk_path);
3807 return err;
3810 const struct got_error *
3811 got_worktree_status(struct got_worktree *worktree,
3812 struct got_pathlist_head *paths, struct got_repository *repo,
3813 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3814 got_cancel_cb cancel_cb, void *cancel_arg)
3816 const struct got_error *err = NULL;
3817 char *fileindex_path = NULL;
3818 struct got_fileindex *fileindex = NULL;
3819 struct got_pathlist_entry *pe;
3821 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3822 if (err)
3823 return err;
3825 TAILQ_FOREACH(pe, paths, entry) {
3826 err = worktree_status(worktree, pe->path, fileindex, repo,
3827 status_cb, status_arg, cancel_cb, cancel_arg,
3828 no_ignores, 0);
3829 if (err)
3830 break;
3832 free(fileindex_path);
3833 got_fileindex_free(fileindex);
3834 return err;
3837 const struct got_error *
3838 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3839 const char *arg)
3841 const struct got_error *err = NULL;
3842 char *resolved = NULL, *cwd = NULL, *path = NULL;
3843 size_t len;
3844 struct stat sb;
3845 char *abspath = NULL;
3846 char canonpath[PATH_MAX];
3848 *wt_path = NULL;
3850 cwd = getcwd(NULL, 0);
3851 if (cwd == NULL)
3852 return got_error_from_errno("getcwd");
3854 if (lstat(arg, &sb) == -1) {
3855 if (errno != ENOENT) {
3856 err = got_error_from_errno2("lstat", arg);
3857 goto done;
3859 sb.st_mode = 0;
3861 if (S_ISLNK(sb.st_mode)) {
3863 * We cannot use realpath(3) with symlinks since we want to
3864 * operate on the symlink itself.
3865 * But we can make the path absolute, assuming it is relative
3866 * to the current working directory, and then canonicalize it.
3868 if (!got_path_is_absolute(arg)) {
3869 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3870 err = got_error_from_errno("asprintf");
3871 goto done;
3875 err = got_canonpath(abspath ? abspath : arg, canonpath,
3876 sizeof(canonpath));
3877 if (err)
3878 goto done;
3879 resolved = strdup(canonpath);
3880 if (resolved == NULL) {
3881 err = got_error_from_errno("strdup");
3882 goto done;
3884 } else {
3885 resolved = realpath(arg, NULL);
3886 if (resolved == NULL) {
3887 if (errno != ENOENT) {
3888 err = got_error_from_errno2("realpath", arg);
3889 goto done;
3891 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3892 err = got_error_from_errno("asprintf");
3893 goto done;
3895 err = got_canonpath(abspath, canonpath,
3896 sizeof(canonpath));
3897 if (err)
3898 goto done;
3899 resolved = strdup(canonpath);
3900 if (resolved == NULL) {
3901 err = got_error_from_errno("strdup");
3902 goto done;
3907 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3908 strlen(got_worktree_get_root_path(worktree)))) {
3909 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3910 goto done;
3913 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3914 err = got_path_skip_common_ancestor(&path,
3915 got_worktree_get_root_path(worktree), resolved);
3916 if (err)
3917 goto done;
3918 } else {
3919 path = strdup("");
3920 if (path == NULL) {
3921 err = got_error_from_errno("strdup");
3922 goto done;
3926 /* XXX status walk can't deal with trailing slash! */
3927 len = strlen(path);
3928 while (len > 0 && path[len - 1] == '/') {
3929 path[len - 1] = '\0';
3930 len--;
3932 done:
3933 free(abspath);
3934 free(resolved);
3935 free(cwd);
3936 if (err == NULL)
3937 *wt_path = path;
3938 else
3939 free(path);
3940 return err;
3943 struct schedule_addition_args {
3944 struct got_worktree *worktree;
3945 struct got_fileindex *fileindex;
3946 got_worktree_checkout_cb progress_cb;
3947 void *progress_arg;
3948 struct got_repository *repo;
3951 static const struct got_error *
3952 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3953 const char *relpath, struct got_object_id *blob_id,
3954 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3955 int dirfd, const char *de_name)
3957 struct schedule_addition_args *a = arg;
3958 const struct got_error *err = NULL;
3959 struct got_fileindex_entry *ie;
3960 struct stat sb;
3961 char *ondisk_path;
3963 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3964 relpath) == -1)
3965 return got_error_from_errno("asprintf");
3967 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3968 if (ie) {
3969 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3970 de_name, a->repo);
3971 if (err)
3972 goto done;
3973 /* Re-adding an existing entry is a no-op. */
3974 if (status == GOT_STATUS_ADD)
3975 goto done;
3976 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3977 if (err)
3978 goto done;
3981 if (status != GOT_STATUS_UNVERSIONED) {
3982 if (status == GOT_STATUS_NONEXISTENT)
3983 err = got_error_set_errno(ENOENT, ondisk_path);
3984 else
3985 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3986 goto done;
3989 err = got_fileindex_entry_alloc(&ie, relpath);
3990 if (err)
3991 goto done;
3992 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3993 relpath, NULL, NULL, 1);
3994 if (err) {
3995 got_fileindex_entry_free(ie);
3996 goto done;
3998 err = got_fileindex_entry_add(a->fileindex, ie);
3999 if (err) {
4000 got_fileindex_entry_free(ie);
4001 goto done;
4003 done:
4004 free(ondisk_path);
4005 if (err)
4006 return err;
4007 if (status == GOT_STATUS_ADD)
4008 return NULL;
4009 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4012 const struct got_error *
4013 got_worktree_schedule_add(struct got_worktree *worktree,
4014 struct got_pathlist_head *paths,
4015 got_worktree_checkout_cb progress_cb, void *progress_arg,
4016 struct got_repository *repo, int no_ignores)
4018 struct got_fileindex *fileindex = NULL;
4019 char *fileindex_path = NULL;
4020 const struct got_error *err = NULL, *sync_err, *unlockerr;
4021 struct got_pathlist_entry *pe;
4022 struct schedule_addition_args saa;
4024 err = lock_worktree(worktree, LOCK_EX);
4025 if (err)
4026 return err;
4028 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4029 if (err)
4030 goto done;
4032 saa.worktree = worktree;
4033 saa.fileindex = fileindex;
4034 saa.progress_cb = progress_cb;
4035 saa.progress_arg = progress_arg;
4036 saa.repo = repo;
4038 TAILQ_FOREACH(pe, paths, entry) {
4039 err = worktree_status(worktree, pe->path, fileindex, repo,
4040 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4041 if (err)
4042 break;
4044 sync_err = sync_fileindex(fileindex, fileindex_path);
4045 if (sync_err && err == NULL)
4046 err = sync_err;
4047 done:
4048 free(fileindex_path);
4049 if (fileindex)
4050 got_fileindex_free(fileindex);
4051 unlockerr = lock_worktree(worktree, LOCK_SH);
4052 if (unlockerr && err == NULL)
4053 err = unlockerr;
4054 return err;
4057 struct schedule_deletion_args {
4058 struct got_worktree *worktree;
4059 struct got_fileindex *fileindex;
4060 got_worktree_delete_cb progress_cb;
4061 void *progress_arg;
4062 struct got_repository *repo;
4063 int delete_local_mods;
4064 int keep_on_disk;
4065 int ignore_missing_paths;
4066 const char *status_codes;
4069 static const struct got_error *
4070 schedule_for_deletion(void *arg, unsigned char status,
4071 unsigned char staged_status, const char *relpath,
4072 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4073 struct got_object_id *commit_id, int dirfd, const char *de_name)
4075 struct schedule_deletion_args *a = arg;
4076 const struct got_error *err = NULL;
4077 struct got_fileindex_entry *ie = NULL;
4078 struct stat sb;
4079 char *ondisk_path;
4081 if (status == GOT_STATUS_NONEXISTENT) {
4082 if (a->ignore_missing_paths)
4083 return NULL;
4084 return got_error_set_errno(ENOENT, relpath);
4087 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4088 if (ie == NULL)
4089 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4091 staged_status = get_staged_status(ie);
4092 if (staged_status != GOT_STATUS_NO_CHANGE) {
4093 if (staged_status == GOT_STATUS_DELETE)
4094 return NULL;
4095 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4098 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4099 relpath) == -1)
4100 return got_error_from_errno("asprintf");
4102 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4103 a->repo);
4104 if (err)
4105 goto done;
4107 if (a->status_codes) {
4108 size_t ncodes = strlen(a->status_codes);
4109 int i;
4110 for (i = 0; i < ncodes ; i++) {
4111 if (status == a->status_codes[i])
4112 break;
4114 if (i == ncodes) {
4115 /* Do not delete files in non-matching status. */
4116 free(ondisk_path);
4117 return NULL;
4119 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4120 a->status_codes[i] != GOT_STATUS_MISSING) {
4121 static char msg[64];
4122 snprintf(msg, sizeof(msg),
4123 "invalid status code '%c'", a->status_codes[i]);
4124 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4125 goto done;
4129 if (status != GOT_STATUS_NO_CHANGE) {
4130 if (status == GOT_STATUS_DELETE)
4131 goto done;
4132 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4133 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4134 goto done;
4136 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4137 err = got_error_set_errno(ENOENT, relpath);
4138 goto done;
4140 if (status != GOT_STATUS_MODIFY &&
4141 status != GOT_STATUS_MISSING) {
4142 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4143 goto done;
4147 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4148 size_t root_len;
4150 if (dirfd != -1) {
4151 if (unlinkat(dirfd, de_name, 0) == -1) {
4152 err = got_error_from_errno2("unlinkat",
4153 ondisk_path);
4154 goto done;
4156 } else if (unlink(ondisk_path) == -1) {
4157 err = got_error_from_errno2("unlink", ondisk_path);
4158 goto done;
4161 root_len = strlen(a->worktree->root_path);
4162 do {
4163 char *parent;
4164 err = got_path_dirname(&parent, ondisk_path);
4165 if (err)
4166 goto done;
4167 free(ondisk_path);
4168 ondisk_path = parent;
4169 if (rmdir(ondisk_path) == -1) {
4170 if (errno != ENOTEMPTY)
4171 err = got_error_from_errno2("rmdir",
4172 ondisk_path);
4173 break;
4175 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4176 strlen(ondisk_path), root_len) != 0);
4179 got_fileindex_entry_mark_deleted_from_disk(ie);
4180 done:
4181 free(ondisk_path);
4182 if (err)
4183 return err;
4184 if (status == GOT_STATUS_DELETE)
4185 return NULL;
4186 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4187 staged_status, relpath);
4190 const struct got_error *
4191 got_worktree_schedule_delete(struct got_worktree *worktree,
4192 struct got_pathlist_head *paths, int delete_local_mods,
4193 const char *status_codes,
4194 got_worktree_delete_cb progress_cb, void *progress_arg,
4195 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4197 struct got_fileindex *fileindex = NULL;
4198 char *fileindex_path = NULL;
4199 const struct got_error *err = NULL, *sync_err, *unlockerr;
4200 struct got_pathlist_entry *pe;
4201 struct schedule_deletion_args sda;
4203 err = lock_worktree(worktree, LOCK_EX);
4204 if (err)
4205 return err;
4207 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4208 if (err)
4209 goto done;
4211 sda.worktree = worktree;
4212 sda.fileindex = fileindex;
4213 sda.progress_cb = progress_cb;
4214 sda.progress_arg = progress_arg;
4215 sda.repo = repo;
4216 sda.delete_local_mods = delete_local_mods;
4217 sda.keep_on_disk = keep_on_disk;
4218 sda.ignore_missing_paths = ignore_missing_paths;
4219 sda.status_codes = status_codes;
4221 TAILQ_FOREACH(pe, paths, entry) {
4222 err = worktree_status(worktree, pe->path, fileindex, repo,
4223 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4224 if (err)
4225 break;
4227 sync_err = sync_fileindex(fileindex, fileindex_path);
4228 if (sync_err && err == NULL)
4229 err = sync_err;
4230 done:
4231 free(fileindex_path);
4232 if (fileindex)
4233 got_fileindex_free(fileindex);
4234 unlockerr = lock_worktree(worktree, LOCK_SH);
4235 if (unlockerr && err == NULL)
4236 err = unlockerr;
4237 return err;
4240 static const struct got_error *
4241 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4243 const struct got_error *err = NULL;
4244 char *line = NULL;
4245 size_t linesize = 0, n;
4246 ssize_t linelen;
4248 linelen = getline(&line, &linesize, infile);
4249 if (linelen == -1) {
4250 if (ferror(infile)) {
4251 err = got_error_from_errno("getline");
4252 goto done;
4254 return NULL;
4256 if (outfile) {
4257 n = fwrite(line, 1, linelen, outfile);
4258 if (n != linelen) {
4259 err = got_ferror(outfile, GOT_ERR_IO);
4260 goto done;
4263 if (rejectfile) {
4264 n = fwrite(line, 1, linelen, rejectfile);
4265 if (n != linelen)
4266 err = got_ferror(rejectfile, GOT_ERR_IO);
4268 done:
4269 free(line);
4270 return err;
4273 static const struct got_error *
4274 skip_one_line(FILE *f)
4276 char *line = NULL;
4277 size_t linesize = 0;
4278 ssize_t linelen;
4280 linelen = getline(&line, &linesize, f);
4281 if (linelen == -1) {
4282 if (ferror(f))
4283 return got_error_from_errno("getline");
4284 return NULL;
4286 free(line);
4287 return NULL;
4290 static const struct got_error *
4291 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4292 int start_old, int end_old, int start_new, int end_new,
4293 FILE *outfile, FILE *rejectfile)
4295 const struct got_error *err;
4297 /* Copy old file's lines leading up to patch. */
4298 while (!feof(f1) && *line_cur1 < start_old) {
4299 err = copy_one_line(f1, outfile, NULL);
4300 if (err)
4301 return err;
4302 (*line_cur1)++;
4304 /* Skip new file's lines leading up to patch. */
4305 while (!feof(f2) && *line_cur2 < start_new) {
4306 if (rejectfile)
4307 err = copy_one_line(f2, NULL, rejectfile);
4308 else
4309 err = skip_one_line(f2);
4310 if (err)
4311 return err;
4312 (*line_cur2)++;
4314 /* Copy patched lines. */
4315 while (!feof(f2) && *line_cur2 <= end_new) {
4316 err = copy_one_line(f2, outfile, NULL);
4317 if (err)
4318 return err;
4319 (*line_cur2)++;
4321 /* Skip over old file's replaced lines. */
4322 while (!feof(f1) && *line_cur1 <= end_old) {
4323 if (rejectfile)
4324 err = copy_one_line(f1, NULL, rejectfile);
4325 else
4326 err = skip_one_line(f1);
4327 if (err)
4328 return err;
4329 (*line_cur1)++;
4332 return NULL;
4335 static const struct got_error *
4336 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4337 FILE *outfile, FILE *rejectfile)
4339 const struct got_error *err;
4341 if (outfile) {
4342 /* Copy old file's lines until EOF. */
4343 while (!feof(f1)) {
4344 err = copy_one_line(f1, outfile, NULL);
4345 if (err)
4346 return err;
4347 (*line_cur1)++;
4350 if (rejectfile) {
4351 /* Copy new file's lines until EOF. */
4352 while (!feof(f2)) {
4353 err = copy_one_line(f2, NULL, rejectfile);
4354 if (err)
4355 return err;
4356 (*line_cur2)++;
4360 return NULL;
4363 static const struct got_error *
4364 apply_or_reject_change(int *choice, int *nchunks_used,
4365 struct diff_result *diff_result, int n,
4366 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4367 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4368 got_worktree_patch_cb patch_cb, void *patch_arg)
4370 const struct got_error *err = NULL;
4371 struct diff_chunk_context cc = {};
4372 int start_old, end_old, start_new, end_new;
4373 FILE *hunkfile;
4374 struct diff_output_unidiff_state *diff_state;
4375 struct diff_input_info diff_info;
4376 int rc;
4378 *choice = GOT_PATCH_CHOICE_NONE;
4380 /* Get changed line numbers without context lines for copy_change(). */
4381 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4382 start_old = cc.left.start;
4383 end_old = cc.left.end;
4384 start_new = cc.right.start;
4385 end_new = cc.right.end;
4387 /* Get the same change with context lines for display. */
4388 memset(&cc, 0, sizeof(cc));
4389 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4391 memset(&diff_info, 0, sizeof(diff_info));
4392 diff_info.left_path = relpath;
4393 diff_info.right_path = relpath;
4395 diff_state = diff_output_unidiff_state_alloc();
4396 if (diff_state == NULL)
4397 return got_error_set_errno(ENOMEM,
4398 "diff_output_unidiff_state_alloc");
4400 hunkfile = got_opentemp();
4401 if (hunkfile == NULL) {
4402 err = got_error_from_errno("got_opentemp");
4403 goto done;
4406 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4407 diff_result, &cc);
4408 if (rc != DIFF_RC_OK) {
4409 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4410 goto done;
4413 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4414 err = got_ferror(hunkfile, GOT_ERR_IO);
4415 goto done;
4418 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4419 hunkfile, changeno, nchanges);
4420 if (err)
4421 goto done;
4423 switch (*choice) {
4424 case GOT_PATCH_CHOICE_YES:
4425 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4426 end_old, start_new, end_new, outfile, rejectfile);
4427 break;
4428 case GOT_PATCH_CHOICE_NO:
4429 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4430 end_old, start_new, end_new, rejectfile, outfile);
4431 break;
4432 case GOT_PATCH_CHOICE_QUIT:
4433 break;
4434 default:
4435 err = got_error(GOT_ERR_PATCH_CHOICE);
4436 break;
4438 done:
4439 diff_output_unidiff_state_free(diff_state);
4440 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4441 err = got_error_from_errno("fclose");
4442 return err;
4445 struct revert_file_args {
4446 struct got_worktree *worktree;
4447 struct got_fileindex *fileindex;
4448 got_worktree_checkout_cb progress_cb;
4449 void *progress_arg;
4450 got_worktree_patch_cb patch_cb;
4451 void *patch_arg;
4452 struct got_repository *repo;
4453 int unlink_added_files;
4456 static const struct got_error *
4457 create_patched_content(char **path_outfile, int reverse_patch,
4458 struct got_object_id *blob_id, const char *path2,
4459 int dirfd2, const char *de_name2,
4460 const char *relpath, struct got_repository *repo,
4461 got_worktree_patch_cb patch_cb, void *patch_arg)
4463 const struct got_error *err, *free_err;
4464 struct got_blob_object *blob = NULL;
4465 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4466 int fd = -1, fd2 = -1;
4467 char link_target[PATH_MAX];
4468 ssize_t link_len = 0;
4469 char *path1 = NULL, *id_str = NULL;
4470 struct stat sb2;
4471 struct got_diffreg_result *diffreg_result = NULL;
4472 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4473 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4475 *path_outfile = NULL;
4477 err = got_object_id_str(&id_str, blob_id);
4478 if (err)
4479 return err;
4481 if (dirfd2 != -1) {
4482 fd2 = openat(dirfd2, de_name2,
4483 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4484 if (fd2 == -1) {
4485 if (!got_err_open_nofollow_on_symlink()) {
4486 err = got_error_from_errno2("openat", path2);
4487 goto done;
4489 link_len = readlinkat(dirfd2, de_name2,
4490 link_target, sizeof(link_target));
4491 if (link_len == -1) {
4492 return got_error_from_errno2("readlinkat",
4493 path2);
4495 sb2.st_mode = S_IFLNK;
4496 sb2.st_size = link_len;
4498 } else {
4499 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4500 if (fd2 == -1) {
4501 if (!got_err_open_nofollow_on_symlink()) {
4502 err = got_error_from_errno2("open", path2);
4503 goto done;
4505 link_len = readlink(path2, link_target,
4506 sizeof(link_target));
4507 if (link_len == -1)
4508 return got_error_from_errno2("readlink", path2);
4509 sb2.st_mode = S_IFLNK;
4510 sb2.st_size = link_len;
4513 if (fd2 != -1) {
4514 if (fstat(fd2, &sb2) == -1) {
4515 err = got_error_from_errno2("fstat", path2);
4516 goto done;
4519 f2 = fdopen(fd2, "r");
4520 if (f2 == NULL) {
4521 err = got_error_from_errno2("fdopen", path2);
4522 goto done;
4524 fd2 = -1;
4525 } else {
4526 size_t n;
4527 f2 = got_opentemp();
4528 if (f2 == NULL) {
4529 err = got_error_from_errno2("got_opentemp", path2);
4530 goto done;
4532 n = fwrite(link_target, 1, link_len, f2);
4533 if (n != link_len) {
4534 err = got_ferror(f2, GOT_ERR_IO);
4535 goto done;
4537 if (fflush(f2) == EOF) {
4538 err = got_error_from_errno("fflush");
4539 goto done;
4541 rewind(f2);
4544 fd = got_opentempfd();
4545 if (fd == -1) {
4546 err = got_error_from_errno("got_opentempfd");
4547 goto done;
4550 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4551 if (err)
4552 goto done;
4554 err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4555 if (err)
4556 goto done;
4558 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4559 if (err)
4560 goto done;
4562 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4563 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4564 if (err)
4565 goto done;
4567 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4568 "");
4569 if (err)
4570 goto done;
4572 if (fseek(f1, 0L, SEEK_SET) == -1)
4573 return got_ferror(f1, GOT_ERR_IO);
4574 if (fseek(f2, 0L, SEEK_SET) == -1)
4575 return got_ferror(f2, GOT_ERR_IO);
4577 /* Count the number of actual changes in the diff result. */
4578 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4579 struct diff_chunk_context cc = {};
4580 diff_chunk_context_load_change(&cc, &nchunks_used,
4581 diffreg_result->result, n, 0);
4582 nchanges++;
4584 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4585 int choice;
4586 err = apply_or_reject_change(&choice, &nchunks_used,
4587 diffreg_result->result, n, relpath, f1, f2,
4588 &line_cur1, &line_cur2,
4589 reverse_patch ? NULL : outfile,
4590 reverse_patch ? outfile : NULL,
4591 ++i, nchanges, patch_cb, patch_arg);
4592 if (err)
4593 goto done;
4594 if (choice == GOT_PATCH_CHOICE_YES)
4595 have_content = 1;
4596 else if (choice == GOT_PATCH_CHOICE_QUIT)
4597 break;
4599 if (have_content) {
4600 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4601 reverse_patch ? NULL : outfile,
4602 reverse_patch ? outfile : NULL);
4603 if (err)
4604 goto done;
4606 if (!S_ISLNK(sb2.st_mode)) {
4607 mode_t mode;
4609 mode = apply_umask(sb2.st_mode);
4610 if (fchmod(fileno(outfile), mode) == -1) {
4611 err = got_error_from_errno2("fchmod", path2);
4612 goto done;
4616 done:
4617 free(id_str);
4618 if (fd != -1 && close(fd) == -1 && err == NULL)
4619 err = got_error_from_errno("close");
4620 if (blob)
4621 got_object_blob_close(blob);
4622 free_err = got_diffreg_result_free(diffreg_result);
4623 if (err == NULL)
4624 err = free_err;
4625 if (f1 && fclose(f1) == EOF && err == NULL)
4626 err = got_error_from_errno2("fclose", path1);
4627 if (f2 && fclose(f2) == EOF && err == NULL)
4628 err = got_error_from_errno2("fclose", path2);
4629 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4630 err = got_error_from_errno2("close", path2);
4631 if (outfile && fclose(outfile) == EOF && err == NULL)
4632 err = got_error_from_errno2("fclose", *path_outfile);
4633 if (path1 && unlink(path1) == -1 && err == NULL)
4634 err = got_error_from_errno2("unlink", path1);
4635 if (err || !have_content) {
4636 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4637 err = got_error_from_errno2("unlink", *path_outfile);
4638 free(*path_outfile);
4639 *path_outfile = NULL;
4641 free(path1);
4642 return err;
4645 static const struct got_error *
4646 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4647 const char *relpath, struct got_object_id *blob_id,
4648 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4649 int dirfd, const char *de_name)
4651 struct revert_file_args *a = arg;
4652 const struct got_error *err = NULL;
4653 char *parent_path = NULL;
4654 struct got_fileindex_entry *ie;
4655 struct got_commit_object *base_commit = NULL;
4656 struct got_tree_object *tree = NULL;
4657 struct got_object_id *tree_id = NULL;
4658 const struct got_tree_entry *te = NULL;
4659 char *tree_path = NULL, *te_name;
4660 char *ondisk_path = NULL, *path_content = NULL;
4661 struct got_blob_object *blob = NULL;
4662 int fd = -1;
4664 /* Reverting a staged deletion is a no-op. */
4665 if (status == GOT_STATUS_DELETE &&
4666 staged_status != GOT_STATUS_NO_CHANGE)
4667 return NULL;
4669 if (status == GOT_STATUS_UNVERSIONED)
4670 return (*a->progress_cb)(a->progress_arg,
4671 GOT_STATUS_UNVERSIONED, relpath);
4673 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4674 if (ie == NULL)
4675 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4677 /* Construct in-repository path of tree which contains this blob. */
4678 err = got_path_dirname(&parent_path, ie->path);
4679 if (err) {
4680 if (err->code != GOT_ERR_BAD_PATH)
4681 goto done;
4682 parent_path = strdup("/");
4683 if (parent_path == NULL) {
4684 err = got_error_from_errno("strdup");
4685 goto done;
4688 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4689 tree_path = strdup(parent_path);
4690 if (tree_path == NULL) {
4691 err = got_error_from_errno("strdup");
4692 goto done;
4694 } else {
4695 if (got_path_is_root_dir(parent_path)) {
4696 tree_path = strdup(a->worktree->path_prefix);
4697 if (tree_path == NULL) {
4698 err = got_error_from_errno("strdup");
4699 goto done;
4701 } else {
4702 if (asprintf(&tree_path, "%s/%s",
4703 a->worktree->path_prefix, parent_path) == -1) {
4704 err = got_error_from_errno("asprintf");
4705 goto done;
4710 err = got_object_open_as_commit(&base_commit, a->repo,
4711 a->worktree->base_commit_id);
4712 if (err)
4713 goto done;
4715 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4716 if (err) {
4717 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4718 (status == GOT_STATUS_ADD ||
4719 staged_status == GOT_STATUS_ADD)))
4720 goto done;
4721 } else {
4722 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4723 if (err)
4724 goto done;
4726 err = got_path_basename(&te_name, ie->path);
4727 if (err)
4728 goto done;
4730 te = got_object_tree_find_entry(tree, te_name);
4731 free(te_name);
4732 if (te == NULL && status != GOT_STATUS_ADD &&
4733 staged_status != GOT_STATUS_ADD) {
4734 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4735 goto done;
4739 switch (status) {
4740 case GOT_STATUS_ADD:
4741 if (a->patch_cb) {
4742 int choice = GOT_PATCH_CHOICE_NONE;
4743 err = (*a->patch_cb)(&choice, a->patch_arg,
4744 status, ie->path, NULL, 1, 1);
4745 if (err)
4746 goto done;
4747 if (choice != GOT_PATCH_CHOICE_YES)
4748 break;
4750 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4751 ie->path);
4752 if (err)
4753 goto done;
4754 got_fileindex_entry_remove(a->fileindex, ie);
4755 if (a->unlink_added_files) {
4756 if (asprintf(&ondisk_path, "%s/%s",
4757 got_worktree_get_root_path(a->worktree),
4758 relpath) == -1) {
4759 err = got_error_from_errno("asprintf");
4760 goto done;
4762 if (unlink(ondisk_path) == -1) {
4763 err = got_error_from_errno2("unlink",
4764 ondisk_path);
4765 break;
4768 break;
4769 case GOT_STATUS_DELETE:
4770 if (a->patch_cb) {
4771 int choice = GOT_PATCH_CHOICE_NONE;
4772 err = (*a->patch_cb)(&choice, a->patch_arg,
4773 status, ie->path, NULL, 1, 1);
4774 if (err)
4775 goto done;
4776 if (choice != GOT_PATCH_CHOICE_YES)
4777 break;
4779 /* fall through */
4780 case GOT_STATUS_MODIFY:
4781 case GOT_STATUS_MODE_CHANGE:
4782 case GOT_STATUS_CONFLICT:
4783 case GOT_STATUS_MISSING: {
4784 struct got_object_id id;
4785 if (staged_status == GOT_STATUS_ADD ||
4786 staged_status == GOT_STATUS_MODIFY) {
4787 memcpy(id.hash, ie->staged_blob_hash,
4788 SHA1_DIGEST_LENGTH);
4789 } else
4790 memcpy(id.hash, ie->blob_hash,
4791 SHA1_DIGEST_LENGTH);
4792 fd = got_opentempfd();
4793 if (fd == -1) {
4794 err = got_error_from_errno("got_opentempfd");
4795 goto done;
4798 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4799 if (err)
4800 goto done;
4802 if (asprintf(&ondisk_path, "%s/%s",
4803 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4804 err = got_error_from_errno("asprintf");
4805 goto done;
4808 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4809 status == GOT_STATUS_CONFLICT)) {
4810 int is_bad_symlink = 0;
4811 err = create_patched_content(&path_content, 1, &id,
4812 ondisk_path, dirfd, de_name, ie->path, a->repo,
4813 a->patch_cb, a->patch_arg);
4814 if (err || path_content == NULL)
4815 break;
4816 if (te && S_ISLNK(te->mode)) {
4817 if (unlink(path_content) == -1) {
4818 err = got_error_from_errno2("unlink",
4819 path_content);
4820 break;
4822 err = install_symlink(&is_bad_symlink,
4823 a->worktree, ondisk_path, ie->path,
4824 blob, 0, 1, 0, 0, a->repo,
4825 a->progress_cb, a->progress_arg);
4826 } else {
4827 if (rename(path_content, ondisk_path) == -1) {
4828 err = got_error_from_errno3("rename",
4829 path_content, ondisk_path);
4830 goto done;
4833 } else {
4834 int is_bad_symlink = 0;
4835 if (te && S_ISLNK(te->mode)) {
4836 err = install_symlink(&is_bad_symlink,
4837 a->worktree, ondisk_path, ie->path,
4838 blob, 0, 1, 0, 0, a->repo,
4839 a->progress_cb, a->progress_arg);
4840 } else {
4841 err = install_blob(a->worktree, ondisk_path,
4842 ie->path,
4843 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4844 got_fileindex_perms_to_st(ie), blob,
4845 0, 1, 0, 0, a->repo,
4846 a->progress_cb, a->progress_arg);
4848 if (err)
4849 goto done;
4850 if (status == GOT_STATUS_DELETE ||
4851 status == GOT_STATUS_MODE_CHANGE) {
4852 err = got_fileindex_entry_update(ie,
4853 a->worktree->root_fd, relpath,
4854 blob->id.hash,
4855 a->worktree->base_commit_id->hash, 1);
4856 if (err)
4857 goto done;
4859 if (is_bad_symlink) {
4860 got_fileindex_entry_filetype_set(ie,
4861 GOT_FILEIDX_MODE_BAD_SYMLINK);
4864 break;
4866 default:
4867 break;
4869 done:
4870 free(ondisk_path);
4871 free(path_content);
4872 free(parent_path);
4873 free(tree_path);
4874 if (fd != -1 && close(fd) == -1 && err == NULL)
4875 err = got_error_from_errno("close");
4876 if (blob)
4877 got_object_blob_close(blob);
4878 if (tree)
4879 got_object_tree_close(tree);
4880 free(tree_id);
4881 if (base_commit)
4882 got_object_commit_close(base_commit);
4883 return err;
4886 const struct got_error *
4887 got_worktree_revert(struct got_worktree *worktree,
4888 struct got_pathlist_head *paths,
4889 got_worktree_checkout_cb progress_cb, void *progress_arg,
4890 got_worktree_patch_cb patch_cb, void *patch_arg,
4891 struct got_repository *repo)
4893 struct got_fileindex *fileindex = NULL;
4894 char *fileindex_path = NULL;
4895 const struct got_error *err = NULL, *unlockerr = NULL;
4896 const struct got_error *sync_err = NULL;
4897 struct got_pathlist_entry *pe;
4898 struct revert_file_args rfa;
4900 err = lock_worktree(worktree, LOCK_EX);
4901 if (err)
4902 return err;
4904 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4905 if (err)
4906 goto done;
4908 rfa.worktree = worktree;
4909 rfa.fileindex = fileindex;
4910 rfa.progress_cb = progress_cb;
4911 rfa.progress_arg = progress_arg;
4912 rfa.patch_cb = patch_cb;
4913 rfa.patch_arg = patch_arg;
4914 rfa.repo = repo;
4915 rfa.unlink_added_files = 0;
4916 TAILQ_FOREACH(pe, paths, entry) {
4917 err = worktree_status(worktree, pe->path, fileindex, repo,
4918 revert_file, &rfa, NULL, NULL, 1, 0);
4919 if (err)
4920 break;
4922 sync_err = sync_fileindex(fileindex, fileindex_path);
4923 if (sync_err && err == NULL)
4924 err = sync_err;
4925 done:
4926 free(fileindex_path);
4927 if (fileindex)
4928 got_fileindex_free(fileindex);
4929 unlockerr = lock_worktree(worktree, LOCK_SH);
4930 if (unlockerr && err == NULL)
4931 err = unlockerr;
4932 return err;
4935 static void
4936 free_commitable(struct got_commitable *ct)
4938 free(ct->path);
4939 free(ct->in_repo_path);
4940 free(ct->ondisk_path);
4941 free(ct->blob_id);
4942 free(ct->base_blob_id);
4943 free(ct->staged_blob_id);
4944 free(ct->base_commit_id);
4945 free(ct);
4948 struct collect_commitables_arg {
4949 struct got_pathlist_head *commitable_paths;
4950 struct got_repository *repo;
4951 struct got_worktree *worktree;
4952 struct got_fileindex *fileindex;
4953 int have_staged_files;
4954 int allow_bad_symlinks;
4955 int diff_header_shown;
4956 FILE *diff_outfile;
4957 FILE *f1;
4958 FILE *f2;
4962 * Create a file which contains the target path of a symlink so we can feed
4963 * it as content to the diff engine.
4965 static const struct got_error *
4966 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4967 const char *abspath)
4969 const struct got_error *err = NULL;
4970 char target_path[PATH_MAX];
4971 ssize_t target_len, outlen;
4973 *fd = -1;
4975 if (dirfd != -1) {
4976 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4977 if (target_len == -1)
4978 return got_error_from_errno2("readlinkat", abspath);
4979 } else {
4980 target_len = readlink(abspath, target_path, PATH_MAX);
4981 if (target_len == -1)
4982 return got_error_from_errno2("readlink", abspath);
4985 *fd = got_opentempfd();
4986 if (*fd == -1)
4987 return got_error_from_errno("got_opentempfd");
4989 outlen = write(*fd, target_path, target_len);
4990 if (outlen == -1) {
4991 err = got_error_from_errno("got_opentempfd");
4992 goto done;
4995 if (lseek(*fd, 0, SEEK_SET) == -1) {
4996 err = got_error_from_errno2("lseek", abspath);
4997 goto done;
4999 done:
5000 if (err) {
5001 close(*fd);
5002 *fd = -1;
5004 return err;
5007 static const struct got_error *
5008 append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5009 FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5010 int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5012 const struct got_error *err = NULL;
5013 struct got_blob_object *blob1 = NULL;
5014 int fd = -1, fd1 = -1, fd2 = -1;
5015 FILE *ondisk_file = NULL;
5016 char *label1 = NULL;
5017 struct stat sb;
5018 off_t size1 = 0;
5019 int f2_exists = 0;
5020 char *id_str = NULL;
5022 memset(&sb, 0, sizeof(sb));
5024 if (diff_staged) {
5025 if (ct->staged_status != GOT_STATUS_MODIFY &&
5026 ct->staged_status != GOT_STATUS_ADD &&
5027 ct->staged_status != GOT_STATUS_DELETE)
5028 return NULL;
5029 } else {
5030 if (ct->status != GOT_STATUS_MODIFY &&
5031 ct->status != GOT_STATUS_ADD &&
5032 ct->status != GOT_STATUS_DELETE)
5033 return NULL;
5036 err = got_opentemp_truncate(f1);
5037 if (err)
5038 return got_error_from_errno("got_opentemp_truncate");
5039 err = got_opentemp_truncate(f2);
5040 if (err)
5041 return got_error_from_errno("got_opentemp_truncate");
5043 if (!*diff_header_shown) {
5044 err = got_object_id_str(&id_str, worktree->base_commit_id);
5045 if (err)
5046 return err;
5047 fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5048 got_worktree_get_root_path(worktree));
5049 fprintf(diff_outfile, "commit - %s\n", id_str);
5050 fprintf(diff_outfile, "path + %s%s\n",
5051 got_worktree_get_root_path(worktree),
5052 diff_staged ? " (staged changes)" : "");
5053 *diff_header_shown = 1;
5056 if (diff_staged) {
5057 const char *label1 = NULL, *label2 = NULL;
5058 switch (ct->staged_status) {
5059 case GOT_STATUS_MODIFY:
5060 label1 = ct->path;
5061 label2 = ct->path;
5062 break;
5063 case GOT_STATUS_ADD:
5064 label2 = ct->path;
5065 break;
5066 case GOT_STATUS_DELETE:
5067 label1 = ct->path;
5068 break;
5069 default:
5070 return got_error(GOT_ERR_FILE_STATUS);
5072 fd1 = got_opentempfd();
5073 if (fd1 == -1) {
5074 err = got_error_from_errno("got_opentempfd");
5075 goto done;
5077 fd2 = got_opentempfd();
5078 if (fd2 == -1) {
5079 err = got_error_from_errno("got_opentempfd");
5080 goto done;
5082 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5083 fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5084 label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5085 NULL, repo, diff_outfile);
5086 goto done;
5089 fd1 = got_opentempfd();
5090 if (fd1 == -1) {
5091 err = got_error_from_errno("got_opentempfd");
5092 goto done;
5095 if (ct->status != GOT_STATUS_ADD) {
5096 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5097 8192, fd1);
5098 if (err)
5099 goto done;
5102 if (ct->status != GOT_STATUS_DELETE) {
5103 if (dirfd != -1) {
5104 fd = openat(dirfd, de_name,
5105 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5106 if (fd == -1) {
5107 if (!got_err_open_nofollow_on_symlink()) {
5108 err = got_error_from_errno2("openat",
5109 ct->ondisk_path);
5110 goto done;
5112 err = get_symlink_target_file(&fd, dirfd,
5113 de_name, ct->ondisk_path);
5114 if (err)
5115 goto done;
5117 } else {
5118 fd = open(ct->ondisk_path,
5119 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5120 if (fd == -1) {
5121 if (!got_err_open_nofollow_on_symlink()) {
5122 err = got_error_from_errno2("open",
5123 ct->ondisk_path);
5124 goto done;
5126 err = get_symlink_target_file(&fd, dirfd,
5127 de_name, ct->ondisk_path);
5128 if (err)
5129 goto done;
5132 if (fstatat(fd, ct->ondisk_path, &sb,
5133 AT_SYMLINK_NOFOLLOW) == -1) {
5134 err = got_error_from_errno2("fstatat", ct->ondisk_path);
5135 goto done;
5137 ondisk_file = fdopen(fd, "r");
5138 if (ondisk_file == NULL) {
5139 err = got_error_from_errno2("fdopen", ct->ondisk_path);
5140 goto done;
5142 fd = -1;
5143 f2_exists = 1;
5146 if (blob1) {
5147 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5148 f1, blob1);
5149 if (err)
5150 goto done;
5153 err = got_diff_blob_file(blob1, f1, size1, label1,
5154 ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5155 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5156 done:
5157 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5158 err = got_error_from_errno("close");
5159 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5160 err = got_error_from_errno("close");
5161 if (blob1)
5162 got_object_blob_close(blob1);
5163 if (fd != -1 && close(fd) == -1 && err == NULL)
5164 err = got_error_from_errno("close");
5165 if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5166 err = got_error_from_errno("fclose");
5167 return err;
5170 static const struct got_error *
5171 collect_commitables(void *arg, unsigned char status,
5172 unsigned char staged_status, const char *relpath,
5173 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5174 struct got_object_id *commit_id, int dirfd, const char *de_name)
5176 struct collect_commitables_arg *a = arg;
5177 const struct got_error *err = NULL;
5178 struct got_commitable *ct = NULL;
5179 struct got_pathlist_entry *new = NULL;
5180 char *parent_path = NULL, *path = NULL;
5181 struct stat sb;
5183 if (a->have_staged_files) {
5184 if (staged_status != GOT_STATUS_MODIFY &&
5185 staged_status != GOT_STATUS_ADD &&
5186 staged_status != GOT_STATUS_DELETE)
5187 return NULL;
5188 } else {
5189 if (status == GOT_STATUS_CONFLICT)
5190 return got_error(GOT_ERR_COMMIT_CONFLICT);
5192 if (status != GOT_STATUS_MODIFY &&
5193 status != GOT_STATUS_MODE_CHANGE &&
5194 status != GOT_STATUS_ADD &&
5195 status != GOT_STATUS_DELETE)
5196 return NULL;
5199 if (asprintf(&path, "/%s", relpath) == -1) {
5200 err = got_error_from_errno("asprintf");
5201 goto done;
5203 if (strcmp(path, "/") == 0) {
5204 parent_path = strdup("");
5205 if (parent_path == NULL)
5206 return got_error_from_errno("strdup");
5207 } else {
5208 err = got_path_dirname(&parent_path, path);
5209 if (err)
5210 return err;
5213 ct = calloc(1, sizeof(*ct));
5214 if (ct == NULL) {
5215 err = got_error_from_errno("calloc");
5216 goto done;
5219 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5220 relpath) == -1) {
5221 err = got_error_from_errno("asprintf");
5222 goto done;
5225 if (staged_status == GOT_STATUS_ADD ||
5226 staged_status == GOT_STATUS_MODIFY) {
5227 struct got_fileindex_entry *ie;
5228 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5229 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5230 case GOT_FILEIDX_MODE_REGULAR_FILE:
5231 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5232 ct->mode = S_IFREG;
5233 break;
5234 case GOT_FILEIDX_MODE_SYMLINK:
5235 ct->mode = S_IFLNK;
5236 break;
5237 default:
5238 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5239 goto done;
5241 ct->mode |= got_fileindex_entry_perms_get(ie);
5242 } else if (status != GOT_STATUS_DELETE &&
5243 staged_status != GOT_STATUS_DELETE) {
5244 if (dirfd != -1) {
5245 if (fstatat(dirfd, de_name, &sb,
5246 AT_SYMLINK_NOFOLLOW) == -1) {
5247 err = got_error_from_errno2("fstatat",
5248 ct->ondisk_path);
5249 goto done;
5251 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5252 err = got_error_from_errno2("lstat", ct->ondisk_path);
5253 goto done;
5255 ct->mode = sb.st_mode;
5258 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5259 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5260 relpath) == -1) {
5261 err = got_error_from_errno("asprintf");
5262 goto done;
5265 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5266 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5267 int is_bad_symlink;
5268 char target_path[PATH_MAX];
5269 ssize_t target_len;
5270 target_len = readlink(ct->ondisk_path, target_path,
5271 sizeof(target_path));
5272 if (target_len == -1) {
5273 err = got_error_from_errno2("readlink",
5274 ct->ondisk_path);
5275 goto done;
5277 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5278 target_len, ct->ondisk_path, a->worktree->root_path);
5279 if (err)
5280 goto done;
5281 if (is_bad_symlink) {
5282 err = got_error_path(ct->ondisk_path,
5283 GOT_ERR_BAD_SYMLINK);
5284 goto done;
5289 ct->status = status;
5290 ct->staged_status = staged_status;
5291 ct->blob_id = NULL; /* will be filled in when blob gets created */
5292 if (ct->status != GOT_STATUS_ADD &&
5293 ct->staged_status != GOT_STATUS_ADD) {
5294 ct->base_blob_id = got_object_id_dup(blob_id);
5295 if (ct->base_blob_id == NULL) {
5296 err = got_error_from_errno("got_object_id_dup");
5297 goto done;
5299 ct->base_commit_id = got_object_id_dup(commit_id);
5300 if (ct->base_commit_id == NULL) {
5301 err = got_error_from_errno("got_object_id_dup");
5302 goto done;
5305 if (ct->staged_status == GOT_STATUS_ADD ||
5306 ct->staged_status == GOT_STATUS_MODIFY) {
5307 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5308 if (ct->staged_blob_id == NULL) {
5309 err = got_error_from_errno("got_object_id_dup");
5310 goto done;
5313 ct->path = strdup(path);
5314 if (ct->path == NULL) {
5315 err = got_error_from_errno("strdup");
5316 goto done;
5318 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5319 if (err)
5320 goto done;
5322 if (a->diff_outfile && ct && new != NULL) {
5323 err = append_ct_diff(ct, &a->diff_header_shown,
5324 a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5325 a->have_staged_files, a->repo, a->worktree);
5326 if (err)
5327 goto done;
5329 done:
5330 if (ct && (err || new == NULL))
5331 free_commitable(ct);
5332 free(parent_path);
5333 free(path);
5334 return err;
5337 static const struct got_error *write_tree(struct got_object_id **, int *,
5338 struct got_tree_object *, const char *, struct got_pathlist_head *,
5339 got_worktree_status_cb status_cb, void *status_arg,
5340 struct got_repository *);
5342 static const struct got_error *
5343 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5344 struct got_tree_entry *te, const char *parent_path,
5345 struct got_pathlist_head *commitable_paths,
5346 got_worktree_status_cb status_cb, void *status_arg,
5347 struct got_repository *repo)
5349 const struct got_error *err = NULL;
5350 struct got_tree_object *subtree;
5351 char *subpath;
5353 if (asprintf(&subpath, "%s%s%s", parent_path,
5354 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5355 return got_error_from_errno("asprintf");
5357 err = got_object_open_as_tree(&subtree, repo, &te->id);
5358 if (err)
5359 return err;
5361 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5362 commitable_paths, status_cb, status_arg, repo);
5363 got_object_tree_close(subtree);
5364 free(subpath);
5365 return err;
5368 static const struct got_error *
5369 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5371 const struct got_error *err = NULL;
5372 char *ct_parent_path = NULL;
5374 *match = 0;
5376 if (strchr(ct->in_repo_path, '/') == NULL) {
5377 *match = got_path_is_root_dir(path);
5378 return NULL;
5381 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5382 if (err)
5383 return err;
5384 *match = (strcmp(path, ct_parent_path) == 0);
5385 free(ct_parent_path);
5386 return err;
5389 static mode_t
5390 get_ct_file_mode(struct got_commitable *ct)
5392 if (S_ISLNK(ct->mode))
5393 return S_IFLNK;
5395 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5398 static const struct got_error *
5399 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5400 struct got_tree_entry *te, struct got_commitable *ct)
5402 const struct got_error *err = NULL;
5404 *new_te = NULL;
5406 err = got_object_tree_entry_dup(new_te, te);
5407 if (err)
5408 goto done;
5410 (*new_te)->mode = get_ct_file_mode(ct);
5412 if (ct->staged_status == GOT_STATUS_MODIFY)
5413 memcpy(&(*new_te)->id, ct->staged_blob_id,
5414 sizeof((*new_te)->id));
5415 else
5416 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5417 done:
5418 if (err && *new_te) {
5419 free(*new_te);
5420 *new_te = NULL;
5422 return err;
5425 static const struct got_error *
5426 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5427 struct got_commitable *ct)
5429 const struct got_error *err = NULL;
5430 char *ct_name = NULL;
5432 *new_te = NULL;
5434 *new_te = calloc(1, sizeof(**new_te));
5435 if (*new_te == NULL)
5436 return got_error_from_errno("calloc");
5438 err = got_path_basename(&ct_name, ct->path);
5439 if (err)
5440 goto done;
5441 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5442 sizeof((*new_te)->name)) {
5443 err = got_error(GOT_ERR_NO_SPACE);
5444 goto done;
5447 (*new_te)->mode = get_ct_file_mode(ct);
5449 if (ct->staged_status == GOT_STATUS_ADD)
5450 memcpy(&(*new_te)->id, ct->staged_blob_id,
5451 sizeof((*new_te)->id));
5452 else
5453 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5454 done:
5455 free(ct_name);
5456 if (err && *new_te) {
5457 free(*new_te);
5458 *new_te = NULL;
5460 return err;
5463 static const struct got_error *
5464 insert_tree_entry(struct got_tree_entry *new_te,
5465 struct got_pathlist_head *paths)
5467 const struct got_error *err = NULL;
5468 struct got_pathlist_entry *new_pe;
5470 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5471 if (err)
5472 return err;
5473 if (new_pe == NULL)
5474 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5475 return NULL;
5478 static const struct got_error *
5479 report_ct_status(struct got_commitable *ct,
5480 got_worktree_status_cb status_cb, void *status_arg)
5482 const char *ct_path = ct->path;
5483 unsigned char status;
5485 if (status_cb == NULL) /* no commit progress output desired */
5486 return NULL;
5488 while (ct_path[0] == '/')
5489 ct_path++;
5491 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5492 status = ct->staged_status;
5493 else
5494 status = ct->status;
5496 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5497 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5500 static const struct got_error *
5501 match_modified_subtree(int *modified, struct got_tree_entry *te,
5502 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5504 const struct got_error *err = NULL;
5505 struct got_pathlist_entry *pe;
5506 char *te_path;
5508 *modified = 0;
5510 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5511 got_path_is_root_dir(base_tree_path) ? "" : "/",
5512 te->name) == -1)
5513 return got_error_from_errno("asprintf");
5515 TAILQ_FOREACH(pe, commitable_paths, entry) {
5516 struct got_commitable *ct = pe->data;
5517 *modified = got_path_is_child(ct->in_repo_path, te_path,
5518 strlen(te_path));
5519 if (*modified)
5520 break;
5523 free(te_path);
5524 return err;
5527 static const struct got_error *
5528 match_deleted_or_modified_ct(struct got_commitable **ctp,
5529 struct got_tree_entry *te, const char *base_tree_path,
5530 struct got_pathlist_head *commitable_paths)
5532 const struct got_error *err = NULL;
5533 struct got_pathlist_entry *pe;
5535 *ctp = NULL;
5537 TAILQ_FOREACH(pe, commitable_paths, entry) {
5538 struct got_commitable *ct = pe->data;
5539 char *ct_name = NULL;
5540 int path_matches;
5542 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5543 if (ct->status != GOT_STATUS_MODIFY &&
5544 ct->status != GOT_STATUS_MODE_CHANGE &&
5545 ct->status != GOT_STATUS_DELETE)
5546 continue;
5547 } else {
5548 if (ct->staged_status != GOT_STATUS_MODIFY &&
5549 ct->staged_status != GOT_STATUS_DELETE)
5550 continue;
5553 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5554 continue;
5556 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5557 if (err)
5558 return err;
5559 if (!path_matches)
5560 continue;
5562 err = got_path_basename(&ct_name, pe->path);
5563 if (err)
5564 return err;
5566 if (strcmp(te->name, ct_name) != 0) {
5567 free(ct_name);
5568 continue;
5570 free(ct_name);
5572 *ctp = ct;
5573 break;
5576 return err;
5579 static const struct got_error *
5580 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5581 const char *child_path, const char *path_base_tree,
5582 struct got_pathlist_head *commitable_paths,
5583 got_worktree_status_cb status_cb, void *status_arg,
5584 struct got_repository *repo)
5586 const struct got_error *err = NULL;
5587 struct got_tree_entry *new_te;
5588 char *subtree_path;
5589 struct got_object_id *id = NULL;
5590 int nentries;
5592 *new_tep = NULL;
5594 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5595 got_path_is_root_dir(path_base_tree) ? "" : "/",
5596 child_path) == -1)
5597 return got_error_from_errno("asprintf");
5599 new_te = calloc(1, sizeof(*new_te));
5600 if (new_te == NULL)
5601 return got_error_from_errno("calloc");
5602 new_te->mode = S_IFDIR;
5604 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5605 sizeof(new_te->name)) {
5606 err = got_error(GOT_ERR_NO_SPACE);
5607 goto done;
5609 err = write_tree(&id, &nentries, NULL, subtree_path,
5610 commitable_paths, status_cb, status_arg, repo);
5611 if (err) {
5612 free(new_te);
5613 goto done;
5615 memcpy(&new_te->id, id, sizeof(new_te->id));
5616 done:
5617 free(id);
5618 free(subtree_path);
5619 if (err == NULL)
5620 *new_tep = new_te;
5621 return err;
5624 static const struct got_error *
5625 write_tree(struct got_object_id **new_tree_id, int *nentries,
5626 struct got_tree_object *base_tree, const char *path_base_tree,
5627 struct got_pathlist_head *commitable_paths,
5628 got_worktree_status_cb status_cb, void *status_arg,
5629 struct got_repository *repo)
5631 const struct got_error *err = NULL;
5632 struct got_pathlist_head paths;
5633 struct got_tree_entry *te, *new_te = NULL;
5634 struct got_pathlist_entry *pe;
5636 TAILQ_INIT(&paths);
5637 *nentries = 0;
5639 /* Insert, and recurse into, newly added entries first. */
5640 TAILQ_FOREACH(pe, commitable_paths, entry) {
5641 struct got_commitable *ct = pe->data;
5642 char *child_path = NULL, *slash;
5644 if ((ct->status != GOT_STATUS_ADD &&
5645 ct->staged_status != GOT_STATUS_ADD) ||
5646 (ct->flags & GOT_COMMITABLE_ADDED))
5647 continue;
5649 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5650 strlen(path_base_tree)))
5651 continue;
5653 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5654 ct->in_repo_path);
5655 if (err)
5656 goto done;
5658 slash = strchr(child_path, '/');
5659 if (slash == NULL) {
5660 err = alloc_added_blob_tree_entry(&new_te, ct);
5661 if (err)
5662 goto done;
5663 err = report_ct_status(ct, status_cb, status_arg);
5664 if (err)
5665 goto done;
5666 ct->flags |= GOT_COMMITABLE_ADDED;
5667 err = insert_tree_entry(new_te, &paths);
5668 if (err)
5669 goto done;
5670 (*nentries)++;
5671 } else {
5672 *slash = '\0'; /* trim trailing path components */
5673 if (base_tree == NULL ||
5674 got_object_tree_find_entry(base_tree, child_path)
5675 == NULL) {
5676 err = make_subtree_for_added_blob(&new_te,
5677 child_path, path_base_tree,
5678 commitable_paths, status_cb, status_arg,
5679 repo);
5680 if (err)
5681 goto done;
5682 err = insert_tree_entry(new_te, &paths);
5683 if (err)
5684 goto done;
5685 (*nentries)++;
5690 if (base_tree) {
5691 int i, nbase_entries;
5692 /* Handle modified and deleted entries. */
5693 nbase_entries = got_object_tree_get_nentries(base_tree);
5694 for (i = 0; i < nbase_entries; i++) {
5695 struct got_commitable *ct = NULL;
5697 te = got_object_tree_get_entry(base_tree, i);
5698 if (got_object_tree_entry_is_submodule(te)) {
5699 /* Entry is a submodule; just copy it. */
5700 err = got_object_tree_entry_dup(&new_te, te);
5701 if (err)
5702 goto done;
5703 err = insert_tree_entry(new_te, &paths);
5704 if (err)
5705 goto done;
5706 (*nentries)++;
5707 continue;
5710 if (S_ISDIR(te->mode)) {
5711 int modified;
5712 err = got_object_tree_entry_dup(&new_te, te);
5713 if (err)
5714 goto done;
5715 err = match_modified_subtree(&modified, te,
5716 path_base_tree, commitable_paths);
5717 if (err)
5718 goto done;
5719 /* Avoid recursion into unmodified subtrees. */
5720 if (modified) {
5721 struct got_object_id *new_id;
5722 int nsubentries;
5723 err = write_subtree(&new_id,
5724 &nsubentries, te,
5725 path_base_tree, commitable_paths,
5726 status_cb, status_arg, repo);
5727 if (err)
5728 goto done;
5729 if (nsubentries == 0) {
5730 /* All entries were deleted. */
5731 free(new_id);
5732 continue;
5734 memcpy(&new_te->id, new_id,
5735 sizeof(new_te->id));
5736 free(new_id);
5738 err = insert_tree_entry(new_te, &paths);
5739 if (err)
5740 goto done;
5741 (*nentries)++;
5742 continue;
5745 err = match_deleted_or_modified_ct(&ct, te,
5746 path_base_tree, commitable_paths);
5747 if (err)
5748 goto done;
5749 if (ct) {
5750 /* NB: Deleted entries get dropped here. */
5751 if (ct->status == GOT_STATUS_MODIFY ||
5752 ct->status == GOT_STATUS_MODE_CHANGE ||
5753 ct->staged_status == GOT_STATUS_MODIFY) {
5754 err = alloc_modified_blob_tree_entry(
5755 &new_te, te, ct);
5756 if (err)
5757 goto done;
5758 err = insert_tree_entry(new_te, &paths);
5759 if (err)
5760 goto done;
5761 (*nentries)++;
5763 err = report_ct_status(ct, status_cb,
5764 status_arg);
5765 if (err)
5766 goto done;
5767 } else {
5768 /* Entry is unchanged; just copy it. */
5769 err = got_object_tree_entry_dup(&new_te, te);
5770 if (err)
5771 goto done;
5772 err = insert_tree_entry(new_te, &paths);
5773 if (err)
5774 goto done;
5775 (*nentries)++;
5780 /* Write new list of entries; deleted entries have been dropped. */
5781 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5782 done:
5783 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
5784 return err;
5787 static const struct got_error *
5788 update_fileindex_after_commit(struct got_worktree *worktree,
5789 struct got_pathlist_head *commitable_paths,
5790 struct got_object_id *new_base_commit_id,
5791 struct got_fileindex *fileindex, int have_staged_files)
5793 const struct got_error *err = NULL;
5794 struct got_pathlist_entry *pe;
5795 char *relpath = NULL;
5797 TAILQ_FOREACH(pe, commitable_paths, entry) {
5798 struct got_fileindex_entry *ie;
5799 struct got_commitable *ct = pe->data;
5801 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5803 err = got_path_skip_common_ancestor(&relpath,
5804 worktree->root_path, ct->ondisk_path);
5805 if (err)
5806 goto done;
5808 if (ie) {
5809 if (ct->status == GOT_STATUS_DELETE ||
5810 ct->staged_status == GOT_STATUS_DELETE) {
5811 got_fileindex_entry_remove(fileindex, ie);
5812 } else if (ct->staged_status == GOT_STATUS_ADD ||
5813 ct->staged_status == GOT_STATUS_MODIFY) {
5814 got_fileindex_entry_stage_set(ie,
5815 GOT_FILEIDX_STAGE_NONE);
5816 got_fileindex_entry_staged_filetype_set(ie, 0);
5818 err = got_fileindex_entry_update(ie,
5819 worktree->root_fd, relpath,
5820 ct->staged_blob_id->hash,
5821 new_base_commit_id->hash,
5822 !have_staged_files);
5823 } else
5824 err = got_fileindex_entry_update(ie,
5825 worktree->root_fd, relpath,
5826 ct->blob_id->hash,
5827 new_base_commit_id->hash,
5828 !have_staged_files);
5829 } else {
5830 err = got_fileindex_entry_alloc(&ie, pe->path);
5831 if (err)
5832 goto done;
5833 err = got_fileindex_entry_update(ie,
5834 worktree->root_fd, relpath, ct->blob_id->hash,
5835 new_base_commit_id->hash, 1);
5836 if (err) {
5837 got_fileindex_entry_free(ie);
5838 goto done;
5840 err = got_fileindex_entry_add(fileindex, ie);
5841 if (err) {
5842 got_fileindex_entry_free(ie);
5843 goto done;
5846 free(relpath);
5847 relpath = NULL;
5849 done:
5850 free(relpath);
5851 return err;
5855 static const struct got_error *
5856 check_out_of_date(const char *in_repo_path, unsigned char status,
5857 unsigned char staged_status, struct got_object_id *base_blob_id,
5858 struct got_object_id *base_commit_id,
5859 struct got_object_id *head_commit_id, struct got_repository *repo,
5860 int ood_errcode)
5862 const struct got_error *err = NULL;
5863 struct got_commit_object *commit = NULL;
5864 struct got_object_id *id = NULL;
5866 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5867 /* Trivial case: base commit == head commit */
5868 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5869 return NULL;
5871 * Ensure file content which local changes were based
5872 * on matches file content in the branch head.
5874 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5875 if (err)
5876 goto done;
5877 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5878 if (err) {
5879 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5880 err = got_error(ood_errcode);
5881 goto done;
5882 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5883 err = got_error(ood_errcode);
5884 } else {
5885 /* Require that added files don't exist in the branch head. */
5886 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5887 if (err)
5888 goto done;
5889 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5890 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5891 goto done;
5892 err = id ? got_error(ood_errcode) : NULL;
5894 done:
5895 free(id);
5896 if (commit)
5897 got_object_commit_close(commit);
5898 return err;
5901 static const struct got_error *
5902 commit_worktree(struct got_object_id **new_commit_id,
5903 struct got_pathlist_head *commitable_paths,
5904 struct got_object_id *head_commit_id,
5905 struct got_object_id *parent_id2,
5906 struct got_worktree *worktree,
5907 const char *author, const char *committer, char *diff_path,
5908 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5909 got_worktree_status_cb status_cb, void *status_arg,
5910 struct got_repository *repo)
5912 const struct got_error *err = NULL, *unlockerr = NULL;
5913 struct got_pathlist_entry *pe;
5914 const char *head_ref_name = NULL;
5915 struct got_commit_object *head_commit = NULL;
5916 struct got_reference *head_ref2 = NULL;
5917 struct got_object_id *head_commit_id2 = NULL;
5918 struct got_tree_object *head_tree = NULL;
5919 struct got_object_id *new_tree_id = NULL;
5920 int nentries, nparents = 0;
5921 struct got_object_id_queue parent_ids;
5922 struct got_object_qid *pid = NULL;
5923 char *logmsg = NULL;
5924 time_t timestamp;
5926 *new_commit_id = NULL;
5928 STAILQ_INIT(&parent_ids);
5930 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5931 if (err)
5932 goto done;
5934 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5935 if (err)
5936 goto done;
5938 if (commit_msg_cb != NULL) {
5939 err = commit_msg_cb(commitable_paths, diff_path,
5940 &logmsg, commit_arg);
5941 if (err)
5942 goto done;
5945 if (logmsg == NULL || strlen(logmsg) == 0) {
5946 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5947 goto done;
5950 /* Create blobs from added and modified files and record their IDs. */
5951 TAILQ_FOREACH(pe, commitable_paths, entry) {
5952 struct got_commitable *ct = pe->data;
5953 char *ondisk_path;
5955 /* Blobs for staged files already exist. */
5956 if (ct->staged_status == GOT_STATUS_ADD ||
5957 ct->staged_status == GOT_STATUS_MODIFY)
5958 continue;
5960 if (ct->status != GOT_STATUS_ADD &&
5961 ct->status != GOT_STATUS_MODIFY &&
5962 ct->status != GOT_STATUS_MODE_CHANGE)
5963 continue;
5965 if (asprintf(&ondisk_path, "%s/%s",
5966 worktree->root_path, pe->path) == -1) {
5967 err = got_error_from_errno("asprintf");
5968 goto done;
5970 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5971 free(ondisk_path);
5972 if (err)
5973 goto done;
5976 /* Recursively write new tree objects. */
5977 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5978 commitable_paths, status_cb, status_arg, repo);
5979 if (err)
5980 goto done;
5982 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5983 if (err)
5984 goto done;
5985 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5986 nparents++;
5987 if (parent_id2) {
5988 err = got_object_qid_alloc(&pid, parent_id2);
5989 if (err)
5990 goto done;
5991 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5992 nparents++;
5994 timestamp = time(NULL);
5995 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5996 nparents, author, timestamp, committer, timestamp, logmsg, repo);
5997 if (logmsg != NULL)
5998 free(logmsg);
5999 if (err)
6000 goto done;
6002 /* Check if a concurrent commit to our branch has occurred. */
6003 head_ref_name = got_worktree_get_head_ref_name(worktree);
6004 if (head_ref_name == NULL) {
6005 err = got_error_from_errno("got_worktree_get_head_ref_name");
6006 goto done;
6008 /* Lock the reference here to prevent concurrent modification. */
6009 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6010 if (err)
6011 goto done;
6012 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6013 if (err)
6014 goto done;
6015 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6016 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6017 goto done;
6019 /* Update branch head in repository. */
6020 err = got_ref_change_ref(head_ref2, *new_commit_id);
6021 if (err)
6022 goto done;
6023 err = got_ref_write(head_ref2, repo);
6024 if (err)
6025 goto done;
6027 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6028 if (err)
6029 goto done;
6031 err = ref_base_commit(worktree, repo);
6032 if (err)
6033 goto done;
6034 done:
6035 got_object_id_queue_free(&parent_ids);
6036 if (head_tree)
6037 got_object_tree_close(head_tree);
6038 if (head_commit)
6039 got_object_commit_close(head_commit);
6040 free(head_commit_id2);
6041 if (head_ref2) {
6042 unlockerr = got_ref_unlock(head_ref2);
6043 if (unlockerr && err == NULL)
6044 err = unlockerr;
6045 got_ref_close(head_ref2);
6047 return err;
6050 static const struct got_error *
6051 check_path_is_commitable(const char *path,
6052 struct got_pathlist_head *commitable_paths)
6054 struct got_pathlist_entry *cpe = NULL;
6055 size_t path_len = strlen(path);
6057 TAILQ_FOREACH(cpe, commitable_paths, entry) {
6058 struct got_commitable *ct = cpe->data;
6059 const char *ct_path = ct->path;
6061 while (ct_path[0] == '/')
6062 ct_path++;
6064 if (strcmp(path, ct_path) == 0 ||
6065 got_path_is_child(ct_path, path, path_len))
6066 break;
6069 if (cpe == NULL)
6070 return got_error_path(path, GOT_ERR_BAD_PATH);
6072 return NULL;
6075 static const struct got_error *
6076 check_staged_file(void *arg, struct got_fileindex_entry *ie)
6078 int *have_staged_files = arg;
6080 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6081 *have_staged_files = 1;
6082 return got_error(GOT_ERR_CANCELLED);
6085 return NULL;
6088 static const struct got_error *
6089 check_non_staged_files(struct got_fileindex *fileindex,
6090 struct got_pathlist_head *paths)
6092 struct got_pathlist_entry *pe;
6093 struct got_fileindex_entry *ie;
6095 TAILQ_FOREACH(pe, paths, entry) {
6096 if (pe->path[0] == '\0')
6097 continue;
6098 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6099 if (ie == NULL)
6100 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6101 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6102 return got_error_path(pe->path,
6103 GOT_ERR_FILE_NOT_STAGED);
6106 return NULL;
6109 const struct got_error *
6110 got_worktree_commit(struct got_object_id **new_commit_id,
6111 struct got_worktree *worktree, struct got_pathlist_head *paths,
6112 const char *author, const char *committer, int allow_bad_symlinks,
6113 int show_diff, got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6114 got_worktree_status_cb status_cb, void *status_arg,
6115 struct got_repository *repo)
6117 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6118 struct got_fileindex *fileindex = NULL;
6119 char *fileindex_path = NULL;
6120 struct got_pathlist_head commitable_paths;
6121 struct collect_commitables_arg cc_arg;
6122 struct got_pathlist_entry *pe;
6123 struct got_reference *head_ref = NULL;
6124 struct got_object_id *head_commit_id = NULL;
6125 char *diff_path = NULL;
6126 int have_staged_files = 0;
6128 *new_commit_id = NULL;
6130 memset(&cc_arg, 0, sizeof(cc_arg));
6131 TAILQ_INIT(&commitable_paths);
6133 err = lock_worktree(worktree, LOCK_EX);
6134 if (err)
6135 goto done;
6137 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6138 if (err)
6139 goto done;
6141 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6142 if (err)
6143 goto done;
6145 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6146 if (err)
6147 goto done;
6149 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6150 &have_staged_files);
6151 if (err && err->code != GOT_ERR_CANCELLED)
6152 goto done;
6153 if (have_staged_files) {
6154 err = check_non_staged_files(fileindex, paths);
6155 if (err)
6156 goto done;
6159 cc_arg.commitable_paths = &commitable_paths;
6160 cc_arg.worktree = worktree;
6161 cc_arg.fileindex = fileindex;
6162 cc_arg.repo = repo;
6163 cc_arg.have_staged_files = have_staged_files;
6164 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6165 cc_arg.diff_header_shown = 0;
6166 if (show_diff) {
6167 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6168 GOT_TMPDIR_STR "/got", ".diff");
6169 if (err)
6170 goto done;
6171 cc_arg.f1 = got_opentemp();
6172 if (cc_arg.f1 == NULL) {
6173 err = got_error_from_errno("got_opentemp");
6174 goto done;
6176 cc_arg.f2 = got_opentemp();
6177 if (cc_arg.f2 == NULL) {
6178 err = got_error_from_errno("got_opentemp");
6179 goto done;
6183 TAILQ_FOREACH(pe, paths, entry) {
6184 err = worktree_status(worktree, pe->path, fileindex, repo,
6185 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6186 if (err)
6187 goto done;
6190 if (show_diff) {
6191 if (fflush(cc_arg.diff_outfile) == EOF) {
6192 err = got_error_from_errno("fflush");
6193 goto done;
6197 if (TAILQ_EMPTY(&commitable_paths)) {
6198 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6199 goto done;
6202 TAILQ_FOREACH(pe, paths, entry) {
6203 err = check_path_is_commitable(pe->path, &commitable_paths);
6204 if (err)
6205 goto done;
6208 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6209 struct got_commitable *ct = pe->data;
6210 const char *ct_path = ct->in_repo_path;
6212 while (ct_path[0] == '/')
6213 ct_path++;
6214 err = check_out_of_date(ct_path, ct->status,
6215 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6216 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6217 if (err)
6218 goto done;
6222 err = commit_worktree(new_commit_id, &commitable_paths,
6223 head_commit_id, NULL, worktree, author, committer,
6224 (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6225 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6226 if (err)
6227 goto done;
6229 err = update_fileindex_after_commit(worktree, &commitable_paths,
6230 *new_commit_id, fileindex, have_staged_files);
6231 sync_err = sync_fileindex(fileindex, fileindex_path);
6232 if (sync_err && err == NULL)
6233 err = sync_err;
6234 done:
6235 if (fileindex)
6236 got_fileindex_free(fileindex);
6237 free(fileindex_path);
6238 unlockerr = lock_worktree(worktree, LOCK_SH);
6239 if (unlockerr && err == NULL)
6240 err = unlockerr;
6241 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6242 struct got_commitable *ct = pe->data;
6244 free_commitable(ct);
6246 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6247 if (diff_path && unlink(diff_path) == -1 && err == NULL)
6248 err = got_error_from_errno2("unlink", diff_path);
6249 free(diff_path);
6250 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6251 err == NULL)
6252 err = got_error_from_errno("fclose");
6253 return err;
6256 const char *
6257 got_commitable_get_path(struct got_commitable *ct)
6259 return ct->path;
6262 unsigned int
6263 got_commitable_get_status(struct got_commitable *ct)
6265 return ct->status;
6268 struct check_rebase_ok_arg {
6269 struct got_worktree *worktree;
6270 struct got_repository *repo;
6273 static const struct got_error *
6274 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6276 const struct got_error *err = NULL;
6277 struct check_rebase_ok_arg *a = arg;
6278 unsigned char status;
6279 struct stat sb;
6280 char *ondisk_path;
6282 /* Reject rebase of a work tree with mixed base commits. */
6283 if (memcmp(ie->commit_hash, a->worktree->base_commit_id->hash,
6284 SHA1_DIGEST_LENGTH))
6285 return got_error(GOT_ERR_MIXED_COMMITS);
6287 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6288 == -1)
6289 return got_error_from_errno("asprintf");
6291 /* Reject rebase of a work tree with modified or staged files. */
6292 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6293 free(ondisk_path);
6294 if (err)
6295 return err;
6297 if (status != GOT_STATUS_NO_CHANGE)
6298 return got_error(GOT_ERR_MODIFIED);
6299 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6300 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6302 return NULL;
6305 const struct got_error *
6306 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6307 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6308 struct got_worktree *worktree, struct got_reference *branch,
6309 struct got_repository *repo)
6311 const struct got_error *err = NULL;
6312 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6313 char *branch_ref_name = NULL;
6314 char *fileindex_path = NULL;
6315 struct check_rebase_ok_arg ok_arg;
6316 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6317 struct got_object_id *wt_branch_tip = NULL;
6319 *new_base_branch_ref = NULL;
6320 *tmp_branch = NULL;
6321 *fileindex = NULL;
6323 err = lock_worktree(worktree, LOCK_EX);
6324 if (err)
6325 return err;
6327 err = open_fileindex(fileindex, &fileindex_path, worktree);
6328 if (err)
6329 goto done;
6331 ok_arg.worktree = worktree;
6332 ok_arg.repo = repo;
6333 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6334 &ok_arg);
6335 if (err)
6336 goto done;
6338 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6339 if (err)
6340 goto done;
6342 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6343 if (err)
6344 goto done;
6346 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6347 if (err)
6348 goto done;
6350 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6351 0);
6352 if (err)
6353 goto done;
6355 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6356 if (err)
6357 goto done;
6358 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6359 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6360 goto done;
6363 err = got_ref_alloc_symref(new_base_branch_ref,
6364 new_base_branch_ref_name, wt_branch);
6365 if (err)
6366 goto done;
6367 err = got_ref_write(*new_base_branch_ref, repo);
6368 if (err)
6369 goto done;
6371 /* TODO Lock original branch's ref while rebasing? */
6373 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6374 if (err)
6375 goto done;
6377 err = got_ref_write(branch_ref, repo);
6378 if (err)
6379 goto done;
6381 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6382 worktree->base_commit_id);
6383 if (err)
6384 goto done;
6385 err = got_ref_write(*tmp_branch, repo);
6386 if (err)
6387 goto done;
6389 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6390 if (err)
6391 goto done;
6392 done:
6393 free(fileindex_path);
6394 free(tmp_branch_name);
6395 free(new_base_branch_ref_name);
6396 free(branch_ref_name);
6397 if (branch_ref)
6398 got_ref_close(branch_ref);
6399 if (wt_branch)
6400 got_ref_close(wt_branch);
6401 free(wt_branch_tip);
6402 if (err) {
6403 if (*new_base_branch_ref) {
6404 got_ref_close(*new_base_branch_ref);
6405 *new_base_branch_ref = NULL;
6407 if (*tmp_branch) {
6408 got_ref_close(*tmp_branch);
6409 *tmp_branch = NULL;
6411 if (*fileindex) {
6412 got_fileindex_free(*fileindex);
6413 *fileindex = NULL;
6415 lock_worktree(worktree, LOCK_SH);
6417 return err;
6420 const struct got_error *
6421 got_worktree_rebase_continue(struct got_object_id **commit_id,
6422 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6423 struct got_reference **branch, struct got_fileindex **fileindex,
6424 struct got_worktree *worktree, struct got_repository *repo)
6426 const struct got_error *err;
6427 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6428 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6429 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6430 char *fileindex_path = NULL;
6431 int have_staged_files = 0;
6433 *commit_id = NULL;
6434 *new_base_branch = NULL;
6435 *tmp_branch = NULL;
6436 *branch = NULL;
6437 *fileindex = NULL;
6439 err = lock_worktree(worktree, LOCK_EX);
6440 if (err)
6441 return err;
6443 err = open_fileindex(fileindex, &fileindex_path, worktree);
6444 if (err)
6445 goto done;
6447 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6448 &have_staged_files);
6449 if (err && err->code != GOT_ERR_CANCELLED)
6450 goto done;
6451 if (have_staged_files) {
6452 err = got_error(GOT_ERR_STAGED_PATHS);
6453 goto done;
6456 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6457 if (err)
6458 goto done;
6460 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6461 if (err)
6462 goto done;
6464 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6465 if (err)
6466 goto done;
6468 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6469 if (err)
6470 goto done;
6472 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6473 if (err)
6474 goto done;
6476 err = got_ref_open(branch, repo,
6477 got_ref_get_symref_target(branch_ref), 0);
6478 if (err)
6479 goto done;
6481 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6482 if (err)
6483 goto done;
6485 err = got_ref_resolve(commit_id, repo, commit_ref);
6486 if (err)
6487 goto done;
6489 err = got_ref_open(new_base_branch, repo,
6490 new_base_branch_ref_name, 0);
6491 if (err)
6492 goto done;
6494 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6495 if (err)
6496 goto done;
6497 done:
6498 free(commit_ref_name);
6499 free(branch_ref_name);
6500 free(fileindex_path);
6501 if (commit_ref)
6502 got_ref_close(commit_ref);
6503 if (branch_ref)
6504 got_ref_close(branch_ref);
6505 if (err) {
6506 free(*commit_id);
6507 *commit_id = NULL;
6508 if (*tmp_branch) {
6509 got_ref_close(*tmp_branch);
6510 *tmp_branch = NULL;
6512 if (*new_base_branch) {
6513 got_ref_close(*new_base_branch);
6514 *new_base_branch = NULL;
6516 if (*branch) {
6517 got_ref_close(*branch);
6518 *branch = NULL;
6520 if (*fileindex) {
6521 got_fileindex_free(*fileindex);
6522 *fileindex = NULL;
6524 lock_worktree(worktree, LOCK_SH);
6526 return err;
6529 const struct got_error *
6530 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6532 const struct got_error *err;
6533 char *tmp_branch_name = NULL;
6535 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6536 if (err)
6537 return err;
6539 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6540 free(tmp_branch_name);
6541 return NULL;
6544 static const struct got_error *
6545 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6546 const char *diff_path, char **logmsg, void *arg)
6548 *logmsg = arg;
6549 return NULL;
6552 static const struct got_error *
6553 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6554 const char *path, struct got_object_id *blob_id,
6555 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6556 int dirfd, const char *de_name)
6558 return NULL;
6561 struct collect_merged_paths_arg {
6562 got_worktree_checkout_cb progress_cb;
6563 void *progress_arg;
6564 struct got_pathlist_head *merged_paths;
6567 static const struct got_error *
6568 collect_merged_paths(void *arg, unsigned char status, const char *path)
6570 const struct got_error *err;
6571 struct collect_merged_paths_arg *a = arg;
6572 char *p;
6573 struct got_pathlist_entry *new;
6575 err = (*a->progress_cb)(a->progress_arg, status, path);
6576 if (err)
6577 return err;
6579 if (status != GOT_STATUS_MERGE &&
6580 status != GOT_STATUS_ADD &&
6581 status != GOT_STATUS_DELETE &&
6582 status != GOT_STATUS_CONFLICT)
6583 return NULL;
6585 p = strdup(path);
6586 if (p == NULL)
6587 return got_error_from_errno("strdup");
6589 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6590 if (err || new == NULL)
6591 free(p);
6592 return err;
6595 static const struct got_error *
6596 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6597 int is_rebase, struct got_repository *repo)
6599 const struct got_error *err;
6600 struct got_reference *commit_ref = NULL;
6602 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6603 if (err) {
6604 if (err->code != GOT_ERR_NOT_REF)
6605 goto done;
6606 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6607 if (err)
6608 goto done;
6609 err = got_ref_write(commit_ref, repo);
6610 if (err)
6611 goto done;
6612 } else if (is_rebase) {
6613 struct got_object_id *stored_id;
6614 int cmp;
6616 err = got_ref_resolve(&stored_id, repo, commit_ref);
6617 if (err)
6618 goto done;
6619 cmp = got_object_id_cmp(commit_id, stored_id);
6620 free(stored_id);
6621 if (cmp != 0) {
6622 err = got_error(GOT_ERR_REBASE_COMMITID);
6623 goto done;
6626 done:
6627 if (commit_ref)
6628 got_ref_close(commit_ref);
6629 return err;
6632 static const struct got_error *
6633 rebase_merge_files(struct got_pathlist_head *merged_paths,
6634 const char *commit_ref_name, struct got_worktree *worktree,
6635 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6636 struct got_object_id *commit_id, struct got_repository *repo,
6637 got_worktree_checkout_cb progress_cb, void *progress_arg,
6638 got_cancel_cb cancel_cb, void *cancel_arg)
6640 const struct got_error *err;
6641 struct got_reference *commit_ref = NULL;
6642 struct collect_merged_paths_arg cmp_arg;
6643 char *fileindex_path;
6645 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6647 err = get_fileindex_path(&fileindex_path, worktree);
6648 if (err)
6649 return err;
6651 cmp_arg.progress_cb = progress_cb;
6652 cmp_arg.progress_arg = progress_arg;
6653 cmp_arg.merged_paths = merged_paths;
6654 err = merge_files(worktree, fileindex, fileindex_path,
6655 parent_commit_id, commit_id, repo, collect_merged_paths,
6656 &cmp_arg, cancel_cb, cancel_arg);
6657 if (commit_ref)
6658 got_ref_close(commit_ref);
6659 return err;
6662 const struct got_error *
6663 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6664 struct got_worktree *worktree, struct got_fileindex *fileindex,
6665 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6666 struct got_repository *repo,
6667 got_worktree_checkout_cb progress_cb, void *progress_arg,
6668 got_cancel_cb cancel_cb, void *cancel_arg)
6670 const struct got_error *err;
6671 char *commit_ref_name;
6673 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6674 if (err)
6675 return err;
6677 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6678 if (err)
6679 goto done;
6681 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6682 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6683 progress_arg, cancel_cb, cancel_arg);
6684 done:
6685 free(commit_ref_name);
6686 return err;
6689 const struct got_error *
6690 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6691 struct got_worktree *worktree, struct got_fileindex *fileindex,
6692 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6693 struct got_repository *repo,
6694 got_worktree_checkout_cb progress_cb, void *progress_arg,
6695 got_cancel_cb cancel_cb, void *cancel_arg)
6697 const struct got_error *err;
6698 char *commit_ref_name;
6700 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6701 if (err)
6702 return err;
6704 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6705 if (err)
6706 goto done;
6708 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6709 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6710 progress_arg, cancel_cb, cancel_arg);
6711 done:
6712 free(commit_ref_name);
6713 return err;
6716 static const struct got_error *
6717 rebase_commit(struct got_object_id **new_commit_id,
6718 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6719 struct got_worktree *worktree, struct got_fileindex *fileindex,
6720 struct got_reference *tmp_branch, const char *committer,
6721 struct got_commit_object *orig_commit, const char *new_logmsg,
6722 struct got_repository *repo)
6724 const struct got_error *err, *sync_err;
6725 struct got_pathlist_head commitable_paths;
6726 struct collect_commitables_arg cc_arg;
6727 char *fileindex_path = NULL;
6728 struct got_reference *head_ref = NULL;
6729 struct got_object_id *head_commit_id = NULL;
6730 char *logmsg = NULL;
6732 memset(&cc_arg, 0, sizeof(cc_arg));
6733 TAILQ_INIT(&commitable_paths);
6734 *new_commit_id = NULL;
6736 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6738 err = get_fileindex_path(&fileindex_path, worktree);
6739 if (err)
6740 return err;
6742 cc_arg.commitable_paths = &commitable_paths;
6743 cc_arg.worktree = worktree;
6744 cc_arg.repo = repo;
6745 cc_arg.have_staged_files = 0;
6747 * If possible get the status of individual files directly to
6748 * avoid crawling the entire work tree once per rebased commit.
6750 * Ideally, merged_paths would contain a list of commitables
6751 * we could use so we could skip worktree_status() entirely.
6752 * However, we would then need carefully keep track of cumulative
6753 * effects of operations such as file additions and deletions
6754 * in 'got histedit -f' (folding multiple commits into one),
6755 * and this extra complexity is not really worth it.
6757 if (merged_paths) {
6758 struct got_pathlist_entry *pe;
6759 TAILQ_FOREACH(pe, merged_paths, entry) {
6760 err = worktree_status(worktree, pe->path, fileindex,
6761 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6762 0);
6763 if (err)
6764 goto done;
6766 } else {
6767 err = worktree_status(worktree, "", fileindex, repo,
6768 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6769 if (err)
6770 goto done;
6773 if (TAILQ_EMPTY(&commitable_paths)) {
6774 /* No-op change; commit will be elided. */
6775 err = got_ref_delete(commit_ref, repo);
6776 if (err)
6777 goto done;
6778 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6779 goto done;
6782 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6783 if (err)
6784 goto done;
6786 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6787 if (err)
6788 goto done;
6790 if (new_logmsg) {
6791 logmsg = strdup(new_logmsg);
6792 if (logmsg == NULL) {
6793 err = got_error_from_errno("strdup");
6794 goto done;
6796 } else {
6797 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6798 if (err)
6799 goto done;
6802 /* NB: commit_worktree will call free(logmsg) */
6803 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6804 NULL, worktree, got_object_commit_get_author(orig_commit),
6805 committer ? committer :
6806 got_object_commit_get_committer(orig_commit), NULL,
6807 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6808 if (err)
6809 goto done;
6811 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6812 if (err)
6813 goto done;
6815 err = got_ref_delete(commit_ref, repo);
6816 if (err)
6817 goto done;
6819 err = update_fileindex_after_commit(worktree, &commitable_paths,
6820 *new_commit_id, fileindex, 0);
6821 sync_err = sync_fileindex(fileindex, fileindex_path);
6822 if (sync_err && err == NULL)
6823 err = sync_err;
6824 done:
6825 free(fileindex_path);
6826 free(head_commit_id);
6827 if (head_ref)
6828 got_ref_close(head_ref);
6829 if (err) {
6830 free(*new_commit_id);
6831 *new_commit_id = NULL;
6833 return err;
6836 const struct got_error *
6837 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6838 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6839 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6840 const char *committer, struct got_commit_object *orig_commit,
6841 struct got_object_id *orig_commit_id, struct got_repository *repo)
6843 const struct got_error *err;
6844 char *commit_ref_name;
6845 struct got_reference *commit_ref = NULL;
6846 struct got_object_id *commit_id = NULL;
6848 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6849 if (err)
6850 return err;
6852 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6853 if (err)
6854 goto done;
6855 err = got_ref_resolve(&commit_id, repo, commit_ref);
6856 if (err)
6857 goto done;
6858 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6859 err = got_error(GOT_ERR_REBASE_COMMITID);
6860 goto done;
6863 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6864 worktree, fileindex, tmp_branch, committer, orig_commit,
6865 NULL, repo);
6866 done:
6867 if (commit_ref)
6868 got_ref_close(commit_ref);
6869 free(commit_ref_name);
6870 free(commit_id);
6871 return err;
6874 const struct got_error *
6875 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6876 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6877 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6878 const char *committer, struct got_commit_object *orig_commit,
6879 struct got_object_id *orig_commit_id, const char *new_logmsg,
6880 struct got_repository *repo)
6882 const struct got_error *err;
6883 char *commit_ref_name;
6884 struct got_reference *commit_ref = NULL;
6886 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6887 if (err)
6888 return err;
6890 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6891 if (err)
6892 goto done;
6894 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6895 worktree, fileindex, tmp_branch, committer, orig_commit,
6896 new_logmsg, repo);
6897 done:
6898 if (commit_ref)
6899 got_ref_close(commit_ref);
6900 free(commit_ref_name);
6901 return err;
6904 const struct got_error *
6905 got_worktree_rebase_postpone(struct got_worktree *worktree,
6906 struct got_fileindex *fileindex)
6908 if (fileindex)
6909 got_fileindex_free(fileindex);
6910 return lock_worktree(worktree, LOCK_SH);
6913 static const struct got_error *
6914 delete_ref(const char *name, struct got_repository *repo)
6916 const struct got_error *err;
6917 struct got_reference *ref;
6919 err = got_ref_open(&ref, repo, name, 0);
6920 if (err) {
6921 if (err->code == GOT_ERR_NOT_REF)
6922 return NULL;
6923 return err;
6926 err = got_ref_delete(ref, repo);
6927 got_ref_close(ref);
6928 return err;
6931 static const struct got_error *
6932 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6934 const struct got_error *err;
6935 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6936 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6938 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6939 if (err)
6940 goto done;
6941 err = delete_ref(tmp_branch_name, repo);
6942 if (err)
6943 goto done;
6945 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6946 if (err)
6947 goto done;
6948 err = delete_ref(new_base_branch_ref_name, repo);
6949 if (err)
6950 goto done;
6952 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6953 if (err)
6954 goto done;
6955 err = delete_ref(branch_ref_name, repo);
6956 if (err)
6957 goto done;
6959 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6960 if (err)
6961 goto done;
6962 err = delete_ref(commit_ref_name, repo);
6963 if (err)
6964 goto done;
6966 done:
6967 free(tmp_branch_name);
6968 free(new_base_branch_ref_name);
6969 free(branch_ref_name);
6970 free(commit_ref_name);
6971 return err;
6974 static const struct got_error *
6975 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6976 struct got_object_id *new_commit_id, struct got_repository *repo)
6978 const struct got_error *err;
6979 struct got_reference *ref = NULL;
6980 struct got_object_id *old_commit_id = NULL;
6981 const char *branch_name = NULL;
6982 char *new_id_str = NULL;
6983 char *refname = NULL;
6985 branch_name = got_ref_get_name(branch);
6986 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6987 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6988 branch_name += 11;
6990 err = got_object_id_str(&new_id_str, new_commit_id);
6991 if (err)
6992 return err;
6994 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6995 new_id_str) == -1) {
6996 err = got_error_from_errno("asprintf");
6997 goto done;
7000 err = got_ref_resolve(&old_commit_id, repo, branch);
7001 if (err)
7002 goto done;
7004 err = got_ref_alloc(&ref, refname, old_commit_id);
7005 if (err)
7006 goto done;
7008 err = got_ref_write(ref, repo);
7009 done:
7010 free(new_id_str);
7011 free(refname);
7012 free(old_commit_id);
7013 if (ref)
7014 got_ref_close(ref);
7015 return err;
7018 const struct got_error *
7019 got_worktree_rebase_complete(struct got_worktree *worktree,
7020 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7021 struct got_reference *rebased_branch, struct got_repository *repo,
7022 int create_backup)
7024 const struct got_error *err, *unlockerr, *sync_err;
7025 struct got_object_id *new_head_commit_id = NULL;
7026 char *fileindex_path = NULL;
7028 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7029 if (err)
7030 return err;
7032 if (create_backup) {
7033 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7034 rebased_branch, new_head_commit_id, repo);
7035 if (err)
7036 goto done;
7039 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7040 if (err)
7041 goto done;
7043 err = got_ref_write(rebased_branch, repo);
7044 if (err)
7045 goto done;
7047 err = got_worktree_set_head_ref(worktree, rebased_branch);
7048 if (err)
7049 goto done;
7051 err = delete_rebase_refs(worktree, repo);
7052 if (err)
7053 goto done;
7055 err = get_fileindex_path(&fileindex_path, worktree);
7056 if (err)
7057 goto done;
7058 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7059 sync_err = sync_fileindex(fileindex, fileindex_path);
7060 if (sync_err && err == NULL)
7061 err = sync_err;
7062 done:
7063 got_fileindex_free(fileindex);
7064 free(fileindex_path);
7065 free(new_head_commit_id);
7066 unlockerr = lock_worktree(worktree, LOCK_SH);
7067 if (unlockerr && err == NULL)
7068 err = unlockerr;
7069 return err;
7072 const struct got_error *
7073 got_worktree_rebase_abort(struct got_worktree *worktree,
7074 struct got_fileindex *fileindex, struct got_repository *repo,
7075 struct got_reference *new_base_branch,
7076 got_worktree_checkout_cb progress_cb, void *progress_arg)
7078 const struct got_error *err, *unlockerr, *sync_err;
7079 struct got_reference *resolved = NULL;
7080 struct got_object_id *commit_id = NULL;
7081 struct got_commit_object *commit = NULL;
7082 char *fileindex_path = NULL;
7083 struct revert_file_args rfa;
7084 struct got_object_id *tree_id = NULL;
7086 err = lock_worktree(worktree, LOCK_EX);
7087 if (err)
7088 return err;
7090 err = got_object_open_as_commit(&commit, repo,
7091 worktree->base_commit_id);
7092 if (err)
7093 goto done;
7095 err = got_ref_open(&resolved, repo,
7096 got_ref_get_symref_target(new_base_branch), 0);
7097 if (err)
7098 goto done;
7100 err = got_worktree_set_head_ref(worktree, resolved);
7101 if (err)
7102 goto done;
7105 * XXX commits to the base branch could have happened while
7106 * we were busy rebasing; should we store the original commit ID
7107 * when rebase begins and read it back here?
7109 err = got_ref_resolve(&commit_id, repo, resolved);
7110 if (err)
7111 goto done;
7113 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7114 if (err)
7115 goto done;
7117 err = got_object_id_by_path(&tree_id, repo, commit,
7118 worktree->path_prefix);
7119 if (err)
7120 goto done;
7122 err = delete_rebase_refs(worktree, repo);
7123 if (err)
7124 goto done;
7126 err = get_fileindex_path(&fileindex_path, worktree);
7127 if (err)
7128 goto done;
7130 rfa.worktree = worktree;
7131 rfa.fileindex = fileindex;
7132 rfa.progress_cb = progress_cb;
7133 rfa.progress_arg = progress_arg;
7134 rfa.patch_cb = NULL;
7135 rfa.patch_arg = NULL;
7136 rfa.repo = repo;
7137 rfa.unlink_added_files = 0;
7138 err = worktree_status(worktree, "", fileindex, repo,
7139 revert_file, &rfa, NULL, NULL, 1, 0);
7140 if (err)
7141 goto sync;
7143 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7144 repo, progress_cb, progress_arg, NULL, NULL);
7145 sync:
7146 sync_err = sync_fileindex(fileindex, fileindex_path);
7147 if (sync_err && err == NULL)
7148 err = sync_err;
7149 done:
7150 got_ref_close(resolved);
7151 free(tree_id);
7152 free(commit_id);
7153 if (commit)
7154 got_object_commit_close(commit);
7155 if (fileindex)
7156 got_fileindex_free(fileindex);
7157 free(fileindex_path);
7159 unlockerr = lock_worktree(worktree, LOCK_SH);
7160 if (unlockerr && err == NULL)
7161 err = unlockerr;
7162 return err;
7165 const struct got_error *
7166 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7167 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7168 struct got_fileindex **fileindex, struct got_worktree *worktree,
7169 struct got_repository *repo)
7171 const struct got_error *err = NULL;
7172 char *tmp_branch_name = NULL;
7173 char *branch_ref_name = NULL;
7174 char *base_commit_ref_name = NULL;
7175 char *fileindex_path = NULL;
7176 struct check_rebase_ok_arg ok_arg;
7177 struct got_reference *wt_branch = NULL;
7178 struct got_reference *base_commit_ref = NULL;
7180 *tmp_branch = NULL;
7181 *branch_ref = NULL;
7182 *base_commit_id = NULL;
7183 *fileindex = NULL;
7185 err = lock_worktree(worktree, LOCK_EX);
7186 if (err)
7187 return err;
7189 err = open_fileindex(fileindex, &fileindex_path, worktree);
7190 if (err)
7191 goto done;
7193 ok_arg.worktree = worktree;
7194 ok_arg.repo = repo;
7195 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7196 &ok_arg);
7197 if (err)
7198 goto done;
7200 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7201 if (err)
7202 goto done;
7204 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7205 if (err)
7206 goto done;
7208 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7209 worktree);
7210 if (err)
7211 goto done;
7213 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7214 0);
7215 if (err)
7216 goto done;
7218 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7219 if (err)
7220 goto done;
7222 err = got_ref_write(*branch_ref, repo);
7223 if (err)
7224 goto done;
7226 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7227 worktree->base_commit_id);
7228 if (err)
7229 goto done;
7230 err = got_ref_write(base_commit_ref, repo);
7231 if (err)
7232 goto done;
7233 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7234 if (*base_commit_id == NULL) {
7235 err = got_error_from_errno("got_object_id_dup");
7236 goto done;
7239 err = got_ref_alloc(tmp_branch, tmp_branch_name,
7240 worktree->base_commit_id);
7241 if (err)
7242 goto done;
7243 err = got_ref_write(*tmp_branch, repo);
7244 if (err)
7245 goto done;
7247 err = got_worktree_set_head_ref(worktree, *tmp_branch);
7248 if (err)
7249 goto done;
7250 done:
7251 free(fileindex_path);
7252 free(tmp_branch_name);
7253 free(branch_ref_name);
7254 free(base_commit_ref_name);
7255 if (wt_branch)
7256 got_ref_close(wt_branch);
7257 if (err) {
7258 if (*branch_ref) {
7259 got_ref_close(*branch_ref);
7260 *branch_ref = NULL;
7262 if (*tmp_branch) {
7263 got_ref_close(*tmp_branch);
7264 *tmp_branch = NULL;
7266 free(*base_commit_id);
7267 if (*fileindex) {
7268 got_fileindex_free(*fileindex);
7269 *fileindex = NULL;
7271 lock_worktree(worktree, LOCK_SH);
7273 return err;
7276 const struct got_error *
7277 got_worktree_histedit_postpone(struct got_worktree *worktree,
7278 struct got_fileindex *fileindex)
7280 if (fileindex)
7281 got_fileindex_free(fileindex);
7282 return lock_worktree(worktree, LOCK_SH);
7285 const struct got_error *
7286 got_worktree_histedit_in_progress(int *in_progress,
7287 struct got_worktree *worktree)
7289 const struct got_error *err;
7290 char *tmp_branch_name = NULL;
7292 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7293 if (err)
7294 return err;
7296 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7297 free(tmp_branch_name);
7298 return NULL;
7301 const struct got_error *
7302 got_worktree_histedit_continue(struct got_object_id **commit_id,
7303 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7304 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7305 struct got_worktree *worktree, struct got_repository *repo)
7307 const struct got_error *err;
7308 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7309 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7310 struct got_reference *commit_ref = NULL;
7311 struct got_reference *base_commit_ref = NULL;
7312 char *fileindex_path = NULL;
7313 int have_staged_files = 0;
7315 *commit_id = NULL;
7316 *tmp_branch = NULL;
7317 *base_commit_id = NULL;
7318 *fileindex = NULL;
7320 err = lock_worktree(worktree, LOCK_EX);
7321 if (err)
7322 return err;
7324 err = open_fileindex(fileindex, &fileindex_path, worktree);
7325 if (err)
7326 goto done;
7328 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7329 &have_staged_files);
7330 if (err && err->code != GOT_ERR_CANCELLED)
7331 goto done;
7332 if (have_staged_files) {
7333 err = got_error(GOT_ERR_STAGED_PATHS);
7334 goto done;
7337 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7338 if (err)
7339 goto done;
7341 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7342 if (err)
7343 goto done;
7345 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7346 if (err)
7347 goto done;
7349 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7350 worktree);
7351 if (err)
7352 goto done;
7354 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7355 if (err)
7356 goto done;
7358 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7359 if (err)
7360 goto done;
7361 err = got_ref_resolve(commit_id, repo, commit_ref);
7362 if (err)
7363 goto done;
7365 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7366 if (err)
7367 goto done;
7368 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7369 if (err)
7370 goto done;
7372 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7373 if (err)
7374 goto done;
7375 done:
7376 free(commit_ref_name);
7377 free(branch_ref_name);
7378 free(fileindex_path);
7379 if (commit_ref)
7380 got_ref_close(commit_ref);
7381 if (base_commit_ref)
7382 got_ref_close(base_commit_ref);
7383 if (err) {
7384 free(*commit_id);
7385 *commit_id = NULL;
7386 free(*base_commit_id);
7387 *base_commit_id = NULL;
7388 if (*tmp_branch) {
7389 got_ref_close(*tmp_branch);
7390 *tmp_branch = NULL;
7392 if (*fileindex) {
7393 got_fileindex_free(*fileindex);
7394 *fileindex = NULL;
7396 lock_worktree(worktree, LOCK_EX);
7398 return err;
7401 static const struct got_error *
7402 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7404 const struct got_error *err;
7405 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7406 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7408 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7409 if (err)
7410 goto done;
7411 err = delete_ref(tmp_branch_name, repo);
7412 if (err)
7413 goto done;
7415 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7416 worktree);
7417 if (err)
7418 goto done;
7419 err = delete_ref(base_commit_ref_name, repo);
7420 if (err)
7421 goto done;
7423 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7424 if (err)
7425 goto done;
7426 err = delete_ref(branch_ref_name, repo);
7427 if (err)
7428 goto done;
7430 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7431 if (err)
7432 goto done;
7433 err = delete_ref(commit_ref_name, repo);
7434 if (err)
7435 goto done;
7436 done:
7437 free(tmp_branch_name);
7438 free(base_commit_ref_name);
7439 free(branch_ref_name);
7440 free(commit_ref_name);
7441 return err;
7444 const struct got_error *
7445 got_worktree_histedit_abort(struct got_worktree *worktree,
7446 struct got_fileindex *fileindex, struct got_repository *repo,
7447 struct got_reference *branch, struct got_object_id *base_commit_id,
7448 got_worktree_checkout_cb progress_cb, void *progress_arg)
7450 const struct got_error *err, *unlockerr, *sync_err;
7451 struct got_reference *resolved = NULL;
7452 char *fileindex_path = NULL;
7453 struct got_commit_object *commit = NULL;
7454 struct got_object_id *tree_id = NULL;
7455 struct revert_file_args rfa;
7457 err = lock_worktree(worktree, LOCK_EX);
7458 if (err)
7459 return err;
7461 err = got_object_open_as_commit(&commit, repo,
7462 worktree->base_commit_id);
7463 if (err)
7464 goto done;
7466 err = got_ref_open(&resolved, repo,
7467 got_ref_get_symref_target(branch), 0);
7468 if (err)
7469 goto done;
7471 err = got_worktree_set_head_ref(worktree, resolved);
7472 if (err)
7473 goto done;
7475 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7476 if (err)
7477 goto done;
7479 err = got_object_id_by_path(&tree_id, repo, commit,
7480 worktree->path_prefix);
7481 if (err)
7482 goto done;
7484 err = delete_histedit_refs(worktree, repo);
7485 if (err)
7486 goto done;
7488 err = get_fileindex_path(&fileindex_path, worktree);
7489 if (err)
7490 goto done;
7492 rfa.worktree = worktree;
7493 rfa.fileindex = fileindex;
7494 rfa.progress_cb = progress_cb;
7495 rfa.progress_arg = progress_arg;
7496 rfa.patch_cb = NULL;
7497 rfa.patch_arg = NULL;
7498 rfa.repo = repo;
7499 rfa.unlink_added_files = 0;
7500 err = worktree_status(worktree, "", fileindex, repo,
7501 revert_file, &rfa, NULL, NULL, 1, 0);
7502 if (err)
7503 goto sync;
7505 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7506 repo, progress_cb, progress_arg, NULL, NULL);
7507 sync:
7508 sync_err = sync_fileindex(fileindex, fileindex_path);
7509 if (sync_err && err == NULL)
7510 err = sync_err;
7511 done:
7512 got_ref_close(resolved);
7513 free(tree_id);
7514 free(fileindex_path);
7516 unlockerr = lock_worktree(worktree, LOCK_SH);
7517 if (unlockerr && err == NULL)
7518 err = unlockerr;
7519 return err;
7522 const struct got_error *
7523 got_worktree_histedit_complete(struct got_worktree *worktree,
7524 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7525 struct got_reference *edited_branch, struct got_repository *repo)
7527 const struct got_error *err, *unlockerr, *sync_err;
7528 struct got_object_id *new_head_commit_id = NULL;
7529 struct got_reference *resolved = NULL;
7530 char *fileindex_path = NULL;
7532 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7533 if (err)
7534 return err;
7536 err = got_ref_open(&resolved, repo,
7537 got_ref_get_symref_target(edited_branch), 0);
7538 if (err)
7539 goto done;
7541 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7542 resolved, new_head_commit_id, repo);
7543 if (err)
7544 goto done;
7546 err = got_ref_change_ref(resolved, new_head_commit_id);
7547 if (err)
7548 goto done;
7550 err = got_ref_write(resolved, repo);
7551 if (err)
7552 goto done;
7554 err = got_worktree_set_head_ref(worktree, resolved);
7555 if (err)
7556 goto done;
7558 err = delete_histedit_refs(worktree, repo);
7559 if (err)
7560 goto done;
7562 err = get_fileindex_path(&fileindex_path, worktree);
7563 if (err)
7564 goto done;
7565 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7566 sync_err = sync_fileindex(fileindex, fileindex_path);
7567 if (sync_err && err == NULL)
7568 err = sync_err;
7569 done:
7570 got_fileindex_free(fileindex);
7571 free(fileindex_path);
7572 free(new_head_commit_id);
7573 unlockerr = lock_worktree(worktree, LOCK_SH);
7574 if (unlockerr && err == NULL)
7575 err = unlockerr;
7576 return err;
7579 const struct got_error *
7580 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7581 struct got_object_id *commit_id, struct got_repository *repo)
7583 const struct got_error *err;
7584 char *commit_ref_name;
7586 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7587 if (err)
7588 return err;
7590 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7591 if (err)
7592 goto done;
7594 err = delete_ref(commit_ref_name, repo);
7595 done:
7596 free(commit_ref_name);
7597 return err;
7600 const struct got_error *
7601 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7602 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7603 struct got_worktree *worktree, const char *refname,
7604 struct got_repository *repo)
7606 const struct got_error *err = NULL;
7607 char *fileindex_path = NULL;
7608 struct check_rebase_ok_arg ok_arg;
7610 *fileindex = NULL;
7611 *branch_ref = NULL;
7612 *base_branch_ref = NULL;
7614 err = lock_worktree(worktree, LOCK_EX);
7615 if (err)
7616 return err;
7618 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7619 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7620 "cannot integrate a branch into itself; "
7621 "update -b or different branch name required");
7622 goto done;
7625 err = open_fileindex(fileindex, &fileindex_path, worktree);
7626 if (err)
7627 goto done;
7629 /* Preconditions are the same as for rebase. */
7630 ok_arg.worktree = worktree;
7631 ok_arg.repo = repo;
7632 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7633 &ok_arg);
7634 if (err)
7635 goto done;
7637 err = got_ref_open(branch_ref, repo, refname, 1);
7638 if (err)
7639 goto done;
7641 err = got_ref_open(base_branch_ref, repo,
7642 got_worktree_get_head_ref_name(worktree), 1);
7643 done:
7644 if (err) {
7645 if (*branch_ref) {
7646 got_ref_close(*branch_ref);
7647 *branch_ref = NULL;
7649 if (*base_branch_ref) {
7650 got_ref_close(*base_branch_ref);
7651 *base_branch_ref = NULL;
7653 if (*fileindex) {
7654 got_fileindex_free(*fileindex);
7655 *fileindex = NULL;
7657 lock_worktree(worktree, LOCK_SH);
7659 return err;
7662 const struct got_error *
7663 got_worktree_integrate_continue(struct got_worktree *worktree,
7664 struct got_fileindex *fileindex, struct got_repository *repo,
7665 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7666 got_worktree_checkout_cb progress_cb, void *progress_arg,
7667 got_cancel_cb cancel_cb, void *cancel_arg)
7669 const struct got_error *err = NULL, *sync_err, *unlockerr;
7670 char *fileindex_path = NULL;
7671 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7672 struct got_commit_object *commit = NULL;
7674 err = get_fileindex_path(&fileindex_path, worktree);
7675 if (err)
7676 goto done;
7678 err = got_ref_resolve(&commit_id, repo, branch_ref);
7679 if (err)
7680 goto done;
7682 err = got_object_open_as_commit(&commit, repo, commit_id);
7683 if (err)
7684 goto done;
7686 err = got_object_id_by_path(&tree_id, repo, commit,
7687 worktree->path_prefix);
7688 if (err)
7689 goto done;
7691 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7692 if (err)
7693 goto done;
7695 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7696 progress_cb, progress_arg, cancel_cb, cancel_arg);
7697 if (err)
7698 goto sync;
7700 err = got_ref_change_ref(base_branch_ref, commit_id);
7701 if (err)
7702 goto sync;
7704 err = got_ref_write(base_branch_ref, repo);
7705 if (err)
7706 goto sync;
7708 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7709 sync:
7710 sync_err = sync_fileindex(fileindex, fileindex_path);
7711 if (sync_err && err == NULL)
7712 err = sync_err;
7714 done:
7715 unlockerr = got_ref_unlock(branch_ref);
7716 if (unlockerr && err == NULL)
7717 err = unlockerr;
7718 got_ref_close(branch_ref);
7720 unlockerr = got_ref_unlock(base_branch_ref);
7721 if (unlockerr && err == NULL)
7722 err = unlockerr;
7723 got_ref_close(base_branch_ref);
7725 got_fileindex_free(fileindex);
7726 free(fileindex_path);
7727 free(tree_id);
7728 if (commit)
7729 got_object_commit_close(commit);
7731 unlockerr = lock_worktree(worktree, LOCK_SH);
7732 if (unlockerr && err == NULL)
7733 err = unlockerr;
7734 return err;
7737 const struct got_error *
7738 got_worktree_integrate_abort(struct got_worktree *worktree,
7739 struct got_fileindex *fileindex, struct got_repository *repo,
7740 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7742 const struct got_error *err = NULL, *unlockerr = NULL;
7744 got_fileindex_free(fileindex);
7746 err = lock_worktree(worktree, LOCK_SH);
7748 unlockerr = got_ref_unlock(branch_ref);
7749 if (unlockerr && err == NULL)
7750 err = unlockerr;
7751 got_ref_close(branch_ref);
7753 unlockerr = got_ref_unlock(base_branch_ref);
7754 if (unlockerr && err == NULL)
7755 err = unlockerr;
7756 got_ref_close(base_branch_ref);
7758 return err;
7761 const struct got_error *
7762 got_worktree_merge_postpone(struct got_worktree *worktree,
7763 struct got_fileindex *fileindex)
7765 const struct got_error *err, *sync_err;
7766 char *fileindex_path = NULL;
7768 err = get_fileindex_path(&fileindex_path, worktree);
7769 if (err)
7770 goto done;
7772 sync_err = sync_fileindex(fileindex, fileindex_path);
7774 err = lock_worktree(worktree, LOCK_SH);
7775 if (sync_err && err == NULL)
7776 err = sync_err;
7777 done:
7778 got_fileindex_free(fileindex);
7779 free(fileindex_path);
7780 return err;
7783 static const struct got_error *
7784 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7786 const struct got_error *err;
7787 char *branch_refname = NULL, *commit_refname = NULL;
7789 err = get_merge_branch_ref_name(&branch_refname, worktree);
7790 if (err)
7791 goto done;
7792 err = delete_ref(branch_refname, repo);
7793 if (err)
7794 goto done;
7796 err = get_merge_commit_ref_name(&commit_refname, worktree);
7797 if (err)
7798 goto done;
7799 err = delete_ref(commit_refname, repo);
7800 if (err)
7801 goto done;
7803 done:
7804 free(branch_refname);
7805 free(commit_refname);
7806 return err;
7809 struct merge_commit_msg_arg {
7810 struct got_worktree *worktree;
7811 const char *branch_name;
7814 static const struct got_error *
7815 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
7816 const char *diff_path, char **logmsg, void *arg)
7818 struct merge_commit_msg_arg *a = arg;
7820 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7821 got_worktree_get_head_ref_name(a->worktree)) == -1)
7822 return got_error_from_errno("asprintf");
7824 return NULL;
7828 const struct got_error *
7829 got_worktree_merge_branch(struct got_worktree *worktree,
7830 struct got_fileindex *fileindex,
7831 struct got_object_id *yca_commit_id,
7832 struct got_object_id *branch_tip,
7833 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7834 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7836 const struct got_error *err;
7837 char *fileindex_path = NULL;
7839 err = get_fileindex_path(&fileindex_path, worktree);
7840 if (err)
7841 goto done;
7843 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7844 worktree);
7845 if (err)
7846 goto done;
7848 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7849 branch_tip, repo, progress_cb, progress_arg,
7850 cancel_cb, cancel_arg);
7851 done:
7852 free(fileindex_path);
7853 return err;
7856 const struct got_error *
7857 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7858 struct got_worktree *worktree, struct got_fileindex *fileindex,
7859 const char *author, const char *committer, int allow_bad_symlinks,
7860 struct got_object_id *branch_tip, const char *branch_name,
7861 struct got_repository *repo,
7862 got_worktree_status_cb status_cb, void *status_arg)
7865 const struct got_error *err = NULL, *sync_err;
7866 struct got_pathlist_head commitable_paths;
7867 struct collect_commitables_arg cc_arg;
7868 struct got_pathlist_entry *pe;
7869 struct got_reference *head_ref = NULL;
7870 struct got_object_id *head_commit_id = NULL;
7871 int have_staged_files = 0;
7872 struct merge_commit_msg_arg mcm_arg;
7873 char *fileindex_path = NULL;
7875 memset(&cc_arg, 0, sizeof(cc_arg));
7876 *new_commit_id = NULL;
7878 TAILQ_INIT(&commitable_paths);
7880 err = get_fileindex_path(&fileindex_path, worktree);
7881 if (err)
7882 goto done;
7884 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7885 if (err)
7886 goto done;
7888 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7889 if (err)
7890 goto done;
7892 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7893 &have_staged_files);
7894 if (err && err->code != GOT_ERR_CANCELLED)
7895 goto done;
7896 if (have_staged_files) {
7897 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7898 goto done;
7901 cc_arg.commitable_paths = &commitable_paths;
7902 cc_arg.worktree = worktree;
7903 cc_arg.fileindex = fileindex;
7904 cc_arg.repo = repo;
7905 cc_arg.have_staged_files = have_staged_files;
7906 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7907 err = worktree_status(worktree, "", fileindex, repo,
7908 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7909 if (err)
7910 goto done;
7912 if (TAILQ_EMPTY(&commitable_paths)) {
7913 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7914 "merge of %s cannot proceed", branch_name);
7915 goto done;
7918 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7919 struct got_commitable *ct = pe->data;
7920 const char *ct_path = ct->in_repo_path;
7922 while (ct_path[0] == '/')
7923 ct_path++;
7924 err = check_out_of_date(ct_path, ct->status,
7925 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7926 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7927 if (err)
7928 goto done;
7932 mcm_arg.worktree = worktree;
7933 mcm_arg.branch_name = branch_name;
7934 err = commit_worktree(new_commit_id, &commitable_paths,
7935 head_commit_id, branch_tip, worktree, author, committer, NULL,
7936 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7937 if (err)
7938 goto done;
7940 err = update_fileindex_after_commit(worktree, &commitable_paths,
7941 *new_commit_id, fileindex, have_staged_files);
7942 sync_err = sync_fileindex(fileindex, fileindex_path);
7943 if (sync_err && err == NULL)
7944 err = sync_err;
7945 done:
7946 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7947 struct got_commitable *ct = pe->data;
7949 free_commitable(ct);
7951 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
7952 free(fileindex_path);
7953 return err;
7956 const struct got_error *
7957 got_worktree_merge_complete(struct got_worktree *worktree,
7958 struct got_fileindex *fileindex, struct got_repository *repo)
7960 const struct got_error *err, *unlockerr, *sync_err;
7961 char *fileindex_path = NULL;
7963 err = delete_merge_refs(worktree, repo);
7964 if (err)
7965 goto done;
7967 err = get_fileindex_path(&fileindex_path, worktree);
7968 if (err)
7969 goto done;
7970 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7971 sync_err = sync_fileindex(fileindex, fileindex_path);
7972 if (sync_err && err == NULL)
7973 err = sync_err;
7974 done:
7975 got_fileindex_free(fileindex);
7976 free(fileindex_path);
7977 unlockerr = lock_worktree(worktree, LOCK_SH);
7978 if (unlockerr && err == NULL)
7979 err = unlockerr;
7980 return err;
7983 const struct got_error *
7984 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7985 struct got_repository *repo)
7987 const struct got_error *err;
7988 char *branch_refname = NULL;
7989 struct got_reference *branch_ref = NULL;
7991 *in_progress = 0;
7993 err = get_merge_branch_ref_name(&branch_refname, worktree);
7994 if (err)
7995 return err;
7996 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7997 free(branch_refname);
7998 if (err) {
7999 if (err->code != GOT_ERR_NOT_REF)
8000 return err;
8001 } else
8002 *in_progress = 1;
8004 return NULL;
8007 const struct got_error *got_worktree_merge_prepare(
8008 struct got_fileindex **fileindex, struct got_worktree *worktree,
8009 struct got_reference *branch, struct got_repository *repo)
8011 const struct got_error *err = NULL;
8012 char *fileindex_path = NULL;
8013 char *branch_refname = NULL, *commit_refname = NULL;
8014 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
8015 struct got_reference *commit_ref = NULL;
8016 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
8017 struct check_rebase_ok_arg ok_arg;
8019 *fileindex = NULL;
8021 err = lock_worktree(worktree, LOCK_EX);
8022 if (err)
8023 return err;
8025 err = open_fileindex(fileindex, &fileindex_path, worktree);
8026 if (err)
8027 goto done;
8029 /* Preconditions are the same as for rebase. */
8030 ok_arg.worktree = worktree;
8031 ok_arg.repo = repo;
8032 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8033 &ok_arg);
8034 if (err)
8035 goto done;
8037 err = get_merge_branch_ref_name(&branch_refname, worktree);
8038 if (err)
8039 return err;
8041 err = get_merge_commit_ref_name(&commit_refname, worktree);
8042 if (err)
8043 return err;
8045 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8046 0);
8047 if (err)
8048 goto done;
8050 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8051 if (err)
8052 goto done;
8054 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8055 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8056 goto done;
8059 err = got_ref_resolve(&branch_tip, repo, branch);
8060 if (err)
8061 goto done;
8063 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8064 if (err)
8065 goto done;
8066 err = got_ref_write(branch_ref, repo);
8067 if (err)
8068 goto done;
8070 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8071 if (err)
8072 goto done;
8073 err = got_ref_write(commit_ref, repo);
8074 if (err)
8075 goto done;
8077 done:
8078 free(branch_refname);
8079 free(commit_refname);
8080 free(fileindex_path);
8081 if (branch_ref)
8082 got_ref_close(branch_ref);
8083 if (commit_ref)
8084 got_ref_close(commit_ref);
8085 if (wt_branch)
8086 got_ref_close(wt_branch);
8087 free(wt_branch_tip);
8088 if (err) {
8089 if (*fileindex) {
8090 got_fileindex_free(*fileindex);
8091 *fileindex = NULL;
8093 lock_worktree(worktree, LOCK_SH);
8095 return err;
8098 const struct got_error *
8099 got_worktree_merge_continue(char **branch_name,
8100 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8101 struct got_worktree *worktree, struct got_repository *repo)
8103 const struct got_error *err;
8104 char *commit_refname = NULL, *branch_refname = NULL;
8105 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8106 char *fileindex_path = NULL;
8107 int have_staged_files = 0;
8109 *branch_name = NULL;
8110 *branch_tip = NULL;
8111 *fileindex = NULL;
8113 err = lock_worktree(worktree, LOCK_EX);
8114 if (err)
8115 return err;
8117 err = open_fileindex(fileindex, &fileindex_path, worktree);
8118 if (err)
8119 goto done;
8121 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8122 &have_staged_files);
8123 if (err && err->code != GOT_ERR_CANCELLED)
8124 goto done;
8125 if (have_staged_files) {
8126 err = got_error(GOT_ERR_STAGED_PATHS);
8127 goto done;
8130 err = get_merge_branch_ref_name(&branch_refname, worktree);
8131 if (err)
8132 goto done;
8134 err = get_merge_commit_ref_name(&commit_refname, worktree);
8135 if (err)
8136 goto done;
8138 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8139 if (err)
8140 goto done;
8142 if (!got_ref_is_symbolic(branch_ref)) {
8143 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8144 "%s is not a symbolic reference",
8145 got_ref_get_name(branch_ref));
8146 goto done;
8148 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8149 if (*branch_name == NULL) {
8150 err = got_error_from_errno("strdup");
8151 goto done;
8154 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8155 if (err)
8156 goto done;
8158 err = got_ref_resolve(branch_tip, repo, commit_ref);
8159 if (err)
8160 goto done;
8161 done:
8162 free(commit_refname);
8163 free(branch_refname);
8164 free(fileindex_path);
8165 if (commit_ref)
8166 got_ref_close(commit_ref);
8167 if (branch_ref)
8168 got_ref_close(branch_ref);
8169 if (err) {
8170 if (*branch_name) {
8171 free(*branch_name);
8172 *branch_name = NULL;
8174 free(*branch_tip);
8175 *branch_tip = NULL;
8176 if (*fileindex) {
8177 got_fileindex_free(*fileindex);
8178 *fileindex = NULL;
8180 lock_worktree(worktree, LOCK_SH);
8182 return err;
8185 const struct got_error *
8186 got_worktree_merge_abort(struct got_worktree *worktree,
8187 struct got_fileindex *fileindex, struct got_repository *repo,
8188 got_worktree_checkout_cb progress_cb, void *progress_arg)
8190 const struct got_error *err, *unlockerr, *sync_err;
8191 struct got_object_id *commit_id = NULL;
8192 struct got_commit_object *commit = NULL;
8193 char *fileindex_path = NULL;
8194 struct revert_file_args rfa;
8195 struct got_object_id *tree_id = NULL;
8197 err = got_object_open_as_commit(&commit, repo,
8198 worktree->base_commit_id);
8199 if (err)
8200 goto done;
8202 err = got_object_id_by_path(&tree_id, repo, commit,
8203 worktree->path_prefix);
8204 if (err)
8205 goto done;
8207 err = delete_merge_refs(worktree, repo);
8208 if (err)
8209 goto done;
8211 err = get_fileindex_path(&fileindex_path, worktree);
8212 if (err)
8213 goto done;
8215 rfa.worktree = worktree;
8216 rfa.fileindex = fileindex;
8217 rfa.progress_cb = progress_cb;
8218 rfa.progress_arg = progress_arg;
8219 rfa.patch_cb = NULL;
8220 rfa.patch_arg = NULL;
8221 rfa.repo = repo;
8222 rfa.unlink_added_files = 1;
8223 err = worktree_status(worktree, "", fileindex, repo,
8224 revert_file, &rfa, NULL, NULL, 1, 0);
8225 if (err)
8226 goto sync;
8228 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8229 repo, progress_cb, progress_arg, NULL, NULL);
8230 sync:
8231 sync_err = sync_fileindex(fileindex, fileindex_path);
8232 if (sync_err && err == NULL)
8233 err = sync_err;
8234 done:
8235 free(tree_id);
8236 free(commit_id);
8237 if (commit)
8238 got_object_commit_close(commit);
8239 if (fileindex)
8240 got_fileindex_free(fileindex);
8241 free(fileindex_path);
8243 unlockerr = lock_worktree(worktree, LOCK_SH);
8244 if (unlockerr && err == NULL)
8245 err = unlockerr;
8246 return err;
8249 struct check_stage_ok_arg {
8250 struct got_object_id *head_commit_id;
8251 struct got_worktree *worktree;
8252 struct got_fileindex *fileindex;
8253 struct got_repository *repo;
8254 int have_changes;
8257 static const struct got_error *
8258 check_stage_ok(void *arg, unsigned char status,
8259 unsigned char staged_status, const char *relpath,
8260 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8261 struct got_object_id *commit_id, int dirfd, const char *de_name)
8263 struct check_stage_ok_arg *a = arg;
8264 const struct got_error *err = NULL;
8265 struct got_fileindex_entry *ie;
8266 struct got_object_id base_commit_id;
8267 struct got_object_id *base_commit_idp = NULL;
8268 char *in_repo_path = NULL, *p;
8270 if (status == GOT_STATUS_UNVERSIONED ||
8271 status == GOT_STATUS_NO_CHANGE)
8272 return NULL;
8273 if (status == GOT_STATUS_NONEXISTENT)
8274 return got_error_set_errno(ENOENT, relpath);
8276 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8277 if (ie == NULL)
8278 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8280 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8281 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8282 relpath) == -1)
8283 return got_error_from_errno("asprintf");
8285 if (got_fileindex_entry_has_commit(ie)) {
8286 memcpy(base_commit_id.hash, ie->commit_hash,
8287 SHA1_DIGEST_LENGTH);
8288 base_commit_idp = &base_commit_id;
8291 if (status == GOT_STATUS_CONFLICT) {
8292 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8293 goto done;
8294 } else if (status != GOT_STATUS_ADD &&
8295 status != GOT_STATUS_MODIFY &&
8296 status != GOT_STATUS_DELETE) {
8297 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8298 goto done;
8301 a->have_changes = 1;
8303 p = in_repo_path;
8304 while (p[0] == '/')
8305 p++;
8306 err = check_out_of_date(p, status, staged_status,
8307 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8308 GOT_ERR_STAGE_OUT_OF_DATE);
8309 done:
8310 free(in_repo_path);
8311 return err;
8314 struct stage_path_arg {
8315 struct got_worktree *worktree;
8316 struct got_fileindex *fileindex;
8317 struct got_repository *repo;
8318 got_worktree_status_cb status_cb;
8319 void *status_arg;
8320 got_worktree_patch_cb patch_cb;
8321 void *patch_arg;
8322 int staged_something;
8323 int allow_bad_symlinks;
8326 static const struct got_error *
8327 stage_path(void *arg, unsigned char status,
8328 unsigned char staged_status, const char *relpath,
8329 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8330 struct got_object_id *commit_id, int dirfd, const char *de_name)
8332 struct stage_path_arg *a = arg;
8333 const struct got_error *err = NULL;
8334 struct got_fileindex_entry *ie;
8335 char *ondisk_path = NULL, *path_content = NULL;
8336 uint32_t stage;
8337 struct got_object_id *new_staged_blob_id = NULL;
8338 struct stat sb;
8340 if (status == GOT_STATUS_UNVERSIONED)
8341 return NULL;
8343 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8344 if (ie == NULL)
8345 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8347 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8348 relpath)== -1)
8349 return got_error_from_errno("asprintf");
8351 switch (status) {
8352 case GOT_STATUS_ADD:
8353 case GOT_STATUS_MODIFY:
8354 /* XXX could sb.st_mode be passed in by our caller? */
8355 if (lstat(ondisk_path, &sb) == -1) {
8356 err = got_error_from_errno2("lstat", ondisk_path);
8357 break;
8359 if (a->patch_cb) {
8360 if (status == GOT_STATUS_ADD) {
8361 int choice = GOT_PATCH_CHOICE_NONE;
8362 err = (*a->patch_cb)(&choice, a->patch_arg,
8363 status, ie->path, NULL, 1, 1);
8364 if (err)
8365 break;
8366 if (choice != GOT_PATCH_CHOICE_YES)
8367 break;
8368 } else {
8369 err = create_patched_content(&path_content, 0,
8370 staged_blob_id ? staged_blob_id : blob_id,
8371 ondisk_path, dirfd, de_name, ie->path,
8372 a->repo, a->patch_cb, a->patch_arg);
8373 if (err || path_content == NULL)
8374 break;
8377 err = got_object_blob_create(&new_staged_blob_id,
8378 path_content ? path_content : ondisk_path, a->repo);
8379 if (err)
8380 break;
8381 memcpy(ie->staged_blob_hash, new_staged_blob_id->hash,
8382 SHA1_DIGEST_LENGTH);
8383 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8384 stage = GOT_FILEIDX_STAGE_ADD;
8385 else
8386 stage = GOT_FILEIDX_STAGE_MODIFY;
8387 got_fileindex_entry_stage_set(ie, stage);
8388 if (S_ISLNK(sb.st_mode)) {
8389 int is_bad_symlink = 0;
8390 if (!a->allow_bad_symlinks) {
8391 char target_path[PATH_MAX];
8392 ssize_t target_len;
8393 target_len = readlink(ondisk_path, target_path,
8394 sizeof(target_path));
8395 if (target_len == -1) {
8396 err = got_error_from_errno2("readlink",
8397 ondisk_path);
8398 break;
8400 err = is_bad_symlink_target(&is_bad_symlink,
8401 target_path, target_len, ondisk_path,
8402 a->worktree->root_path);
8403 if (err)
8404 break;
8405 if (is_bad_symlink) {
8406 err = got_error_path(ondisk_path,
8407 GOT_ERR_BAD_SYMLINK);
8408 break;
8411 if (is_bad_symlink)
8412 got_fileindex_entry_staged_filetype_set(ie,
8413 GOT_FILEIDX_MODE_BAD_SYMLINK);
8414 else
8415 got_fileindex_entry_staged_filetype_set(ie,
8416 GOT_FILEIDX_MODE_SYMLINK);
8417 } else {
8418 got_fileindex_entry_staged_filetype_set(ie,
8419 GOT_FILEIDX_MODE_REGULAR_FILE);
8421 a->staged_something = 1;
8422 if (a->status_cb == NULL)
8423 break;
8424 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8425 get_staged_status(ie), relpath, blob_id,
8426 new_staged_blob_id, NULL, dirfd, de_name);
8427 if (err)
8428 break;
8430 * When staging the reverse of the staged diff,
8431 * implicitly unstage the file.
8433 if (memcmp(ie->staged_blob_hash, ie->blob_hash,
8434 sizeof(ie->blob_hash)) == 0) {
8435 got_fileindex_entry_stage_set(ie,
8436 GOT_FILEIDX_STAGE_NONE);
8438 break;
8439 case GOT_STATUS_DELETE:
8440 if (staged_status == GOT_STATUS_DELETE)
8441 break;
8442 if (a->patch_cb) {
8443 int choice = GOT_PATCH_CHOICE_NONE;
8444 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8445 ie->path, NULL, 1, 1);
8446 if (err)
8447 break;
8448 if (choice == GOT_PATCH_CHOICE_NO)
8449 break;
8450 if (choice != GOT_PATCH_CHOICE_YES) {
8451 err = got_error(GOT_ERR_PATCH_CHOICE);
8452 break;
8455 stage = GOT_FILEIDX_STAGE_DELETE;
8456 got_fileindex_entry_stage_set(ie, stage);
8457 a->staged_something = 1;
8458 if (a->status_cb == NULL)
8459 break;
8460 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8461 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8462 de_name);
8463 break;
8464 case GOT_STATUS_NO_CHANGE:
8465 break;
8466 case GOT_STATUS_CONFLICT:
8467 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8468 break;
8469 case GOT_STATUS_NONEXISTENT:
8470 err = got_error_set_errno(ENOENT, relpath);
8471 break;
8472 default:
8473 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8474 break;
8477 if (path_content && unlink(path_content) == -1 && err == NULL)
8478 err = got_error_from_errno2("unlink", path_content);
8479 free(path_content);
8480 free(ondisk_path);
8481 free(new_staged_blob_id);
8482 return err;
8485 const struct got_error *
8486 got_worktree_stage(struct got_worktree *worktree,
8487 struct got_pathlist_head *paths,
8488 got_worktree_status_cb status_cb, void *status_arg,
8489 got_worktree_patch_cb patch_cb, void *patch_arg,
8490 int allow_bad_symlinks, struct got_repository *repo)
8492 const struct got_error *err = NULL, *sync_err, *unlockerr;
8493 struct got_pathlist_entry *pe;
8494 struct got_fileindex *fileindex = NULL;
8495 char *fileindex_path = NULL;
8496 struct got_reference *head_ref = NULL;
8497 struct got_object_id *head_commit_id = NULL;
8498 struct check_stage_ok_arg oka;
8499 struct stage_path_arg spa;
8501 err = lock_worktree(worktree, LOCK_EX);
8502 if (err)
8503 return err;
8505 err = got_ref_open(&head_ref, repo,
8506 got_worktree_get_head_ref_name(worktree), 0);
8507 if (err)
8508 goto done;
8509 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8510 if (err)
8511 goto done;
8512 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8513 if (err)
8514 goto done;
8516 /* Check pre-conditions before staging anything. */
8517 oka.head_commit_id = head_commit_id;
8518 oka.worktree = worktree;
8519 oka.fileindex = fileindex;
8520 oka.repo = repo;
8521 oka.have_changes = 0;
8522 TAILQ_FOREACH(pe, paths, entry) {
8523 err = worktree_status(worktree, pe->path, fileindex, repo,
8524 check_stage_ok, &oka, NULL, NULL, 1, 0);
8525 if (err)
8526 goto done;
8528 if (!oka.have_changes) {
8529 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8530 goto done;
8533 spa.worktree = worktree;
8534 spa.fileindex = fileindex;
8535 spa.repo = repo;
8536 spa.patch_cb = patch_cb;
8537 spa.patch_arg = patch_arg;
8538 spa.status_cb = status_cb;
8539 spa.status_arg = status_arg;
8540 spa.staged_something = 0;
8541 spa.allow_bad_symlinks = allow_bad_symlinks;
8542 TAILQ_FOREACH(pe, paths, entry) {
8543 err = worktree_status(worktree, pe->path, fileindex, repo,
8544 stage_path, &spa, NULL, NULL, 1, 0);
8545 if (err)
8546 goto done;
8548 if (!spa.staged_something) {
8549 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8550 goto done;
8553 sync_err = sync_fileindex(fileindex, fileindex_path);
8554 if (sync_err && err == NULL)
8555 err = sync_err;
8556 done:
8557 if (head_ref)
8558 got_ref_close(head_ref);
8559 free(head_commit_id);
8560 free(fileindex_path);
8561 if (fileindex)
8562 got_fileindex_free(fileindex);
8563 unlockerr = lock_worktree(worktree, LOCK_SH);
8564 if (unlockerr && err == NULL)
8565 err = unlockerr;
8566 return err;
8569 struct unstage_path_arg {
8570 struct got_worktree *worktree;
8571 struct got_fileindex *fileindex;
8572 struct got_repository *repo;
8573 got_worktree_checkout_cb progress_cb;
8574 void *progress_arg;
8575 got_worktree_patch_cb patch_cb;
8576 void *patch_arg;
8579 static const struct got_error *
8580 create_unstaged_content(char **path_unstaged_content,
8581 char **path_new_staged_content, struct got_object_id *blob_id,
8582 struct got_object_id *staged_blob_id, const char *relpath,
8583 struct got_repository *repo,
8584 got_worktree_patch_cb patch_cb, void *patch_arg)
8586 const struct got_error *err, *free_err;
8587 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8588 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8589 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8590 struct got_diffreg_result *diffreg_result = NULL;
8591 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8592 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8593 int fd1 = -1, fd2 = -1;
8595 *path_unstaged_content = NULL;
8596 *path_new_staged_content = NULL;
8598 err = got_object_id_str(&label1, blob_id);
8599 if (err)
8600 return err;
8602 fd1 = got_opentempfd();
8603 if (fd1 == -1) {
8604 err = got_error_from_errno("got_opentempfd");
8605 goto done;
8607 fd2 = got_opentempfd();
8608 if (fd2 == -1) {
8609 err = got_error_from_errno("got_opentempfd");
8610 goto done;
8613 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8614 if (err)
8615 goto done;
8617 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
8618 if (err)
8619 goto done;
8621 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8622 if (err)
8623 goto done;
8625 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8626 fd2);
8627 if (err)
8628 goto done;
8630 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
8631 if (err)
8632 goto done;
8634 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8635 if (err)
8636 goto done;
8638 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8639 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8640 if (err)
8641 goto done;
8643 err = got_opentemp_named(path_unstaged_content, &outfile,
8644 "got-unstaged-content", "");
8645 if (err)
8646 goto done;
8647 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8648 "got-new-staged-content", "");
8649 if (err)
8650 goto done;
8652 if (fseek(f1, 0L, SEEK_SET) == -1) {
8653 err = got_ferror(f1, GOT_ERR_IO);
8654 goto done;
8656 if (fseek(f2, 0L, SEEK_SET) == -1) {
8657 err = got_ferror(f2, GOT_ERR_IO);
8658 goto done;
8660 /* Count the number of actual changes in the diff result. */
8661 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8662 struct diff_chunk_context cc = {};
8663 diff_chunk_context_load_change(&cc, &nchunks_used,
8664 diffreg_result->result, n, 0);
8665 nchanges++;
8667 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8668 int choice;
8669 err = apply_or_reject_change(&choice, &nchunks_used,
8670 diffreg_result->result, n, relpath, f1, f2,
8671 &line_cur1, &line_cur2,
8672 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8673 if (err)
8674 goto done;
8675 if (choice == GOT_PATCH_CHOICE_YES)
8676 have_content = 1;
8677 else
8678 have_rejected_content = 1;
8679 if (choice == GOT_PATCH_CHOICE_QUIT)
8680 break;
8682 if (have_content || have_rejected_content)
8683 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8684 outfile, rejectfile);
8685 done:
8686 free(label1);
8687 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8688 err = got_error_from_errno("close");
8689 if (blob)
8690 got_object_blob_close(blob);
8691 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8692 err = got_error_from_errno("close");
8693 if (staged_blob)
8694 got_object_blob_close(staged_blob);
8695 free_err = got_diffreg_result_free(diffreg_result);
8696 if (free_err && err == NULL)
8697 err = free_err;
8698 if (f1 && fclose(f1) == EOF && err == NULL)
8699 err = got_error_from_errno2("fclose", path1);
8700 if (f2 && fclose(f2) == EOF && err == NULL)
8701 err = got_error_from_errno2("fclose", path2);
8702 if (outfile && fclose(outfile) == EOF && err == NULL)
8703 err = got_error_from_errno2("fclose", *path_unstaged_content);
8704 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8705 err = got_error_from_errno2("fclose", *path_new_staged_content);
8706 if (path1 && unlink(path1) == -1 && err == NULL)
8707 err = got_error_from_errno2("unlink", path1);
8708 if (path2 && unlink(path2) == -1 && err == NULL)
8709 err = got_error_from_errno2("unlink", path2);
8710 if (err || !have_content) {
8711 if (*path_unstaged_content &&
8712 unlink(*path_unstaged_content) == -1 && err == NULL)
8713 err = got_error_from_errno2("unlink",
8714 *path_unstaged_content);
8715 free(*path_unstaged_content);
8716 *path_unstaged_content = NULL;
8718 if (err || !have_content || !have_rejected_content) {
8719 if (*path_new_staged_content &&
8720 unlink(*path_new_staged_content) == -1 && err == NULL)
8721 err = got_error_from_errno2("unlink",
8722 *path_new_staged_content);
8723 free(*path_new_staged_content);
8724 *path_new_staged_content = NULL;
8726 free(path1);
8727 free(path2);
8728 return err;
8731 static const struct got_error *
8732 unstage_hunks(struct got_object_id *staged_blob_id,
8733 struct got_blob_object *blob_base,
8734 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8735 const char *ondisk_path, const char *label_orig,
8736 struct got_worktree *worktree, struct got_repository *repo,
8737 got_worktree_patch_cb patch_cb, void *patch_arg,
8738 got_worktree_checkout_cb progress_cb, void *progress_arg)
8740 const struct got_error *err = NULL;
8741 char *path_unstaged_content = NULL;
8742 char *path_new_staged_content = NULL;
8743 char *parent = NULL, *base_path = NULL;
8744 char *blob_base_path = NULL;
8745 struct got_object_id *new_staged_blob_id = NULL;
8746 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8747 struct stat sb;
8749 err = create_unstaged_content(&path_unstaged_content,
8750 &path_new_staged_content, blob_id, staged_blob_id,
8751 ie->path, repo, patch_cb, patch_arg);
8752 if (err)
8753 return err;
8755 if (path_unstaged_content == NULL)
8756 return NULL;
8758 if (path_new_staged_content) {
8759 err = got_object_blob_create(&new_staged_blob_id,
8760 path_new_staged_content, repo);
8761 if (err)
8762 goto done;
8765 f = fopen(path_unstaged_content, "re");
8766 if (f == NULL) {
8767 err = got_error_from_errno2("fopen",
8768 path_unstaged_content);
8769 goto done;
8771 if (fstat(fileno(f), &sb) == -1) {
8772 err = got_error_from_errno2("fstat", path_unstaged_content);
8773 goto done;
8775 if (got_fileindex_entry_staged_filetype_get(ie) ==
8776 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8777 char link_target[PATH_MAX];
8778 size_t r;
8779 r = fread(link_target, 1, sizeof(link_target), f);
8780 if (r == 0 && ferror(f)) {
8781 err = got_error_from_errno("fread");
8782 goto done;
8784 if (r >= sizeof(link_target)) { /* should not happen */
8785 err = got_error(GOT_ERR_NO_SPACE);
8786 goto done;
8788 link_target[r] = '\0';
8789 err = merge_symlink(worktree, blob_base,
8790 ondisk_path, ie->path, label_orig, link_target,
8791 worktree->base_commit_id, repo, progress_cb,
8792 progress_arg);
8793 } else {
8794 int local_changes_subsumed;
8796 err = got_path_dirname(&parent, ondisk_path);
8797 if (err)
8798 return err;
8800 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8801 parent) == -1) {
8802 err = got_error_from_errno("asprintf");
8803 base_path = NULL;
8804 goto done;
8807 err = got_opentemp_named(&blob_base_path, &f_base,
8808 base_path, "");
8809 if (err)
8810 goto done;
8811 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8812 blob_base);
8813 if (err)
8814 goto done;
8817 * In order the run a 3-way merge with a symlink we copy the symlink's
8818 * target path into a temporary file and use that file with diff3.
8820 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8821 err = dump_symlink_target_path_to_file(&f_deriv2,
8822 ondisk_path);
8823 if (err)
8824 goto done;
8825 } else {
8826 int fd;
8827 fd = open(ondisk_path,
8828 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8829 if (fd == -1) {
8830 err = got_error_from_errno2("open", ondisk_path);
8831 goto done;
8833 f_deriv2 = fdopen(fd, "r");
8834 if (f_deriv2 == NULL) {
8835 err = got_error_from_errno2("fdopen", ondisk_path);
8836 close(fd);
8837 goto done;
8841 err = merge_file(&local_changes_subsumed, worktree,
8842 f_base, f, f_deriv2, ondisk_path, ie->path,
8843 got_fileindex_perms_to_st(ie),
8844 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8845 repo, progress_cb, progress_arg);
8847 if (err)
8848 goto done;
8850 if (new_staged_blob_id) {
8851 memcpy(ie->staged_blob_hash, new_staged_blob_id->hash,
8852 SHA1_DIGEST_LENGTH);
8853 } else {
8854 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8855 got_fileindex_entry_staged_filetype_set(ie, 0);
8857 done:
8858 free(new_staged_blob_id);
8859 if (path_unstaged_content &&
8860 unlink(path_unstaged_content) == -1 && err == NULL)
8861 err = got_error_from_errno2("unlink", path_unstaged_content);
8862 if (path_new_staged_content &&
8863 unlink(path_new_staged_content) == -1 && err == NULL)
8864 err = got_error_from_errno2("unlink", path_new_staged_content);
8865 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8866 err = got_error_from_errno2("unlink", blob_base_path);
8867 if (f_base && fclose(f_base) == EOF && err == NULL)
8868 err = got_error_from_errno2("fclose", path_unstaged_content);
8869 if (f && fclose(f) == EOF && err == NULL)
8870 err = got_error_from_errno2("fclose", path_unstaged_content);
8871 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8872 err = got_error_from_errno2("fclose", ondisk_path);
8873 free(path_unstaged_content);
8874 free(path_new_staged_content);
8875 free(blob_base_path);
8876 free(parent);
8877 free(base_path);
8878 return err;
8881 static const struct got_error *
8882 unstage_path(void *arg, unsigned char status,
8883 unsigned char staged_status, const char *relpath,
8884 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8885 struct got_object_id *commit_id, int dirfd, const char *de_name)
8887 const struct got_error *err = NULL;
8888 struct unstage_path_arg *a = arg;
8889 struct got_fileindex_entry *ie;
8890 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8891 char *ondisk_path = NULL;
8892 char *id_str = NULL, *label_orig = NULL;
8893 int local_changes_subsumed;
8894 struct stat sb;
8895 int fd1 = -1, fd2 = -1;
8897 if (staged_status != GOT_STATUS_ADD &&
8898 staged_status != GOT_STATUS_MODIFY &&
8899 staged_status != GOT_STATUS_DELETE)
8900 return NULL;
8902 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8903 if (ie == NULL)
8904 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8906 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8907 == -1)
8908 return got_error_from_errno("asprintf");
8910 err = got_object_id_str(&id_str,
8911 commit_id ? commit_id : a->worktree->base_commit_id);
8912 if (err)
8913 goto done;
8914 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8915 id_str) == -1) {
8916 err = got_error_from_errno("asprintf");
8917 goto done;
8920 fd1 = got_opentempfd();
8921 if (fd1 == -1) {
8922 err = got_error_from_errno("got_opentempfd");
8923 goto done;
8925 fd2 = got_opentempfd();
8926 if (fd2 == -1) {
8927 err = got_error_from_errno("got_opentempfd");
8928 goto done;
8931 switch (staged_status) {
8932 case GOT_STATUS_MODIFY:
8933 err = got_object_open_as_blob(&blob_base, a->repo,
8934 blob_id, 8192, fd1);
8935 if (err)
8936 break;
8937 /* fall through */
8938 case GOT_STATUS_ADD:
8939 if (a->patch_cb) {
8940 if (staged_status == GOT_STATUS_ADD) {
8941 int choice = GOT_PATCH_CHOICE_NONE;
8942 err = (*a->patch_cb)(&choice, a->patch_arg,
8943 staged_status, ie->path, NULL, 1, 1);
8944 if (err)
8945 break;
8946 if (choice != GOT_PATCH_CHOICE_YES)
8947 break;
8948 } else {
8949 err = unstage_hunks(staged_blob_id,
8950 blob_base, blob_id, ie, ondisk_path,
8951 label_orig, a->worktree, a->repo,
8952 a->patch_cb, a->patch_arg,
8953 a->progress_cb, a->progress_arg);
8954 break; /* Done with this file. */
8957 err = got_object_open_as_blob(&blob_staged, a->repo,
8958 staged_blob_id, 8192, fd2);
8959 if (err)
8960 break;
8961 switch (got_fileindex_entry_staged_filetype_get(ie)) {
8962 case GOT_FILEIDX_MODE_BAD_SYMLINK:
8963 case GOT_FILEIDX_MODE_REGULAR_FILE:
8964 err = merge_blob(&local_changes_subsumed, a->worktree,
8965 blob_base, ondisk_path, relpath,
8966 got_fileindex_perms_to_st(ie), label_orig,
8967 blob_staged, commit_id ? commit_id :
8968 a->worktree->base_commit_id, a->repo,
8969 a->progress_cb, a->progress_arg);
8970 break;
8971 case GOT_FILEIDX_MODE_SYMLINK:
8972 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8973 char *staged_target;
8974 err = got_object_blob_read_to_str(
8975 &staged_target, blob_staged);
8976 if (err)
8977 goto done;
8978 err = merge_symlink(a->worktree, blob_base,
8979 ondisk_path, relpath, label_orig,
8980 staged_target, commit_id ? commit_id :
8981 a->worktree->base_commit_id,
8982 a->repo, a->progress_cb, a->progress_arg);
8983 free(staged_target);
8984 } else {
8985 err = merge_blob(&local_changes_subsumed,
8986 a->worktree, blob_base, ondisk_path,
8987 relpath, got_fileindex_perms_to_st(ie),
8988 label_orig, blob_staged,
8989 commit_id ? commit_id :
8990 a->worktree->base_commit_id, a->repo,
8991 a->progress_cb, a->progress_arg);
8993 break;
8994 default:
8995 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8996 break;
8998 if (err == NULL) {
8999 got_fileindex_entry_stage_set(ie,
9000 GOT_FILEIDX_STAGE_NONE);
9001 got_fileindex_entry_staged_filetype_set(ie, 0);
9003 break;
9004 case GOT_STATUS_DELETE:
9005 if (a->patch_cb) {
9006 int choice = GOT_PATCH_CHOICE_NONE;
9007 err = (*a->patch_cb)(&choice, a->patch_arg,
9008 staged_status, ie->path, NULL, 1, 1);
9009 if (err)
9010 break;
9011 if (choice == GOT_PATCH_CHOICE_NO)
9012 break;
9013 if (choice != GOT_PATCH_CHOICE_YES) {
9014 err = got_error(GOT_ERR_PATCH_CHOICE);
9015 break;
9018 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9019 got_fileindex_entry_staged_filetype_set(ie, 0);
9020 err = get_file_status(&status, &sb, ie, ondisk_path,
9021 dirfd, de_name, a->repo);
9022 if (err)
9023 break;
9024 err = (*a->progress_cb)(a->progress_arg, status, relpath);
9025 break;
9027 done:
9028 free(ondisk_path);
9029 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9030 err = got_error_from_errno("close");
9031 if (blob_base)
9032 got_object_blob_close(blob_base);
9033 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9034 err = got_error_from_errno("close");
9035 if (blob_staged)
9036 got_object_blob_close(blob_staged);
9037 free(id_str);
9038 free(label_orig);
9039 return err;
9042 const struct got_error *
9043 got_worktree_unstage(struct got_worktree *worktree,
9044 struct got_pathlist_head *paths,
9045 got_worktree_checkout_cb progress_cb, void *progress_arg,
9046 got_worktree_patch_cb patch_cb, void *patch_arg,
9047 struct got_repository *repo)
9049 const struct got_error *err = NULL, *sync_err, *unlockerr;
9050 struct got_pathlist_entry *pe;
9051 struct got_fileindex *fileindex = NULL;
9052 char *fileindex_path = NULL;
9053 struct unstage_path_arg upa;
9055 err = lock_worktree(worktree, LOCK_EX);
9056 if (err)
9057 return err;
9059 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9060 if (err)
9061 goto done;
9063 upa.worktree = worktree;
9064 upa.fileindex = fileindex;
9065 upa.repo = repo;
9066 upa.progress_cb = progress_cb;
9067 upa.progress_arg = progress_arg;
9068 upa.patch_cb = patch_cb;
9069 upa.patch_arg = patch_arg;
9070 TAILQ_FOREACH(pe, paths, entry) {
9071 err = worktree_status(worktree, pe->path, fileindex, repo,
9072 unstage_path, &upa, NULL, NULL, 1, 0);
9073 if (err)
9074 goto done;
9077 sync_err = sync_fileindex(fileindex, fileindex_path);
9078 if (sync_err && err == NULL)
9079 err = sync_err;
9080 done:
9081 free(fileindex_path);
9082 if (fileindex)
9083 got_fileindex_free(fileindex);
9084 unlockerr = lock_worktree(worktree, LOCK_SH);
9085 if (unlockerr && err == NULL)
9086 err = unlockerr;
9087 return err;
9090 struct report_file_info_arg {
9091 struct got_worktree *worktree;
9092 got_worktree_path_info_cb info_cb;
9093 void *info_arg;
9094 struct got_pathlist_head *paths;
9095 got_cancel_cb cancel_cb;
9096 void *cancel_arg;
9099 static const struct got_error *
9100 report_file_info(void *arg, struct got_fileindex_entry *ie)
9102 struct report_file_info_arg *a = arg;
9103 struct got_pathlist_entry *pe;
9104 struct got_object_id blob_id, staged_blob_id, commit_id;
9105 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9106 struct got_object_id *commit_idp = NULL;
9107 int stage;
9109 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9110 return got_error(GOT_ERR_CANCELLED);
9112 TAILQ_FOREACH(pe, a->paths, entry) {
9113 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9114 got_path_is_child(ie->path, pe->path, pe->path_len))
9115 break;
9117 if (pe == NULL) /* not found */
9118 return NULL;
9120 if (got_fileindex_entry_has_blob(ie)) {
9121 memcpy(blob_id.hash, ie->blob_hash, SHA1_DIGEST_LENGTH);
9122 blob_idp = &blob_id;
9124 stage = got_fileindex_entry_stage_get(ie);
9125 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9126 stage == GOT_FILEIDX_STAGE_ADD) {
9127 memcpy(staged_blob_id.hash, ie->staged_blob_hash,
9128 SHA1_DIGEST_LENGTH);
9129 staged_blob_idp = &staged_blob_id;
9132 if (got_fileindex_entry_has_commit(ie)) {
9133 memcpy(commit_id.hash, ie->commit_hash, SHA1_DIGEST_LENGTH);
9134 commit_idp = &commit_id;
9137 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9138 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9141 const struct got_error *
9142 got_worktree_path_info(struct got_worktree *worktree,
9143 struct got_pathlist_head *paths,
9144 got_worktree_path_info_cb info_cb, void *info_arg,
9145 got_cancel_cb cancel_cb, void *cancel_arg)
9148 const struct got_error *err = NULL, *unlockerr;
9149 struct got_fileindex *fileindex = NULL;
9150 char *fileindex_path = NULL;
9151 struct report_file_info_arg arg;
9153 err = lock_worktree(worktree, LOCK_SH);
9154 if (err)
9155 return err;
9157 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9158 if (err)
9159 goto done;
9161 arg.worktree = worktree;
9162 arg.info_cb = info_cb;
9163 arg.info_arg = info_arg;
9164 arg.paths = paths;
9165 arg.cancel_cb = cancel_cb;
9166 arg.cancel_arg = cancel_arg;
9167 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9168 &arg);
9169 done:
9170 free(fileindex_path);
9171 if (fileindex)
9172 got_fileindex_free(fileindex);
9173 unlockerr = lock_worktree(worktree, LOCK_UN);
9174 if (unlockerr && err == NULL)
9175 err = unlockerr;
9176 return err;
9179 static const struct got_error *
9180 patch_check_path(const char *p, char **path, unsigned char *status,
9181 unsigned char *staged_status, struct got_fileindex *fileindex,
9182 struct got_worktree *worktree, struct got_repository *repo)
9184 const struct got_error *err;
9185 struct got_fileindex_entry *ie;
9186 struct stat sb;
9187 char *ondisk_path = NULL;
9189 err = got_worktree_resolve_path(path, worktree, p);
9190 if (err)
9191 return err;
9193 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9194 *path[0] ? "/" : "", *path) == -1)
9195 return got_error_from_errno("asprintf");
9197 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9198 if (ie) {
9199 *staged_status = get_staged_status(ie);
9200 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9201 repo);
9202 if (err)
9203 goto done;
9204 } else {
9205 *staged_status = GOT_STATUS_NO_CHANGE;
9206 *status = GOT_STATUS_UNVERSIONED;
9207 if (lstat(ondisk_path, &sb) == -1) {
9208 if (errno != ENOENT) {
9209 err = got_error_from_errno2("lstat",
9210 ondisk_path);
9211 goto done;
9213 *status = GOT_STATUS_NONEXISTENT;
9217 done:
9218 free(ondisk_path);
9219 return err;
9222 static const struct got_error *
9223 patch_can_rm(const char *path, unsigned char status,
9224 unsigned char staged_status)
9226 if (status == GOT_STATUS_NONEXISTENT)
9227 return got_error_set_errno(ENOENT, path);
9228 if (status != GOT_STATUS_NO_CHANGE &&
9229 status != GOT_STATUS_ADD &&
9230 status != GOT_STATUS_MODIFY &&
9231 status != GOT_STATUS_MODE_CHANGE)
9232 return got_error_path(path, GOT_ERR_FILE_STATUS);
9233 if (staged_status == GOT_STATUS_DELETE)
9234 return got_error_path(path, GOT_ERR_FILE_STATUS);
9235 return NULL;
9238 static const struct got_error *
9239 patch_can_add(const char *path, unsigned char status)
9241 if (status != GOT_STATUS_NONEXISTENT)
9242 return got_error_path(path, GOT_ERR_FILE_STATUS);
9243 return NULL;
9246 static const struct got_error *
9247 patch_can_edit(const char *path, unsigned char status,
9248 unsigned char staged_status)
9250 if (status == GOT_STATUS_NONEXISTENT)
9251 return got_error_set_errno(ENOENT, path);
9252 if (status != GOT_STATUS_NO_CHANGE &&
9253 status != GOT_STATUS_ADD &&
9254 status != GOT_STATUS_MODIFY)
9255 return got_error_path(path, GOT_ERR_FILE_STATUS);
9256 if (staged_status == GOT_STATUS_DELETE)
9257 return got_error_path(path, GOT_ERR_FILE_STATUS);
9258 return NULL;
9261 const struct got_error *
9262 got_worktree_patch_prepare(struct got_fileindex **fileindex,
9263 char **fileindex_path, struct got_worktree *worktree)
9265 return open_fileindex(fileindex, fileindex_path, worktree);
9268 const struct got_error *
9269 got_worktree_patch_check_path(const char *old, const char *new,
9270 char **oldpath, char **newpath, struct got_worktree *worktree,
9271 struct got_repository *repo, struct got_fileindex *fileindex)
9273 const struct got_error *err = NULL;
9274 int file_renamed = 0;
9275 unsigned char status_old, staged_status_old;
9276 unsigned char status_new, staged_status_new;
9278 *oldpath = NULL;
9279 *newpath = NULL;
9281 err = patch_check_path(old != NULL ? old : new, oldpath,
9282 &status_old, &staged_status_old, fileindex, worktree, repo);
9283 if (err)
9284 goto done;
9286 err = patch_check_path(new != NULL ? new : old, newpath,
9287 &status_new, &staged_status_new, fileindex, worktree, repo);
9288 if (err)
9289 goto done;
9291 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9292 file_renamed = 1;
9294 if (old != NULL && new == NULL)
9295 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9296 else if (file_renamed) {
9297 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9298 if (err == NULL)
9299 err = patch_can_add(*newpath, status_new);
9300 } else if (old == NULL)
9301 err = patch_can_add(*newpath, status_new);
9302 else
9303 err = patch_can_edit(*newpath, status_new, staged_status_new);
9305 done:
9306 if (err) {
9307 free(*oldpath);
9308 *oldpath = NULL;
9309 free(*newpath);
9310 *newpath = NULL;
9312 return err;
9315 const struct got_error *
9316 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9317 struct got_worktree *worktree, struct got_fileindex *fileindex,
9318 got_worktree_checkout_cb progress_cb, void *progress_arg)
9320 struct schedule_addition_args saa;
9322 memset(&saa, 0, sizeof(saa));
9323 saa.worktree = worktree;
9324 saa.fileindex = fileindex;
9325 saa.progress_cb = progress_cb;
9326 saa.progress_arg = progress_arg;
9327 saa.repo = repo;
9329 return worktree_status(worktree, path, fileindex, repo,
9330 schedule_addition, &saa, NULL, NULL, 1, 0);
9333 const struct got_error *
9334 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9335 struct got_worktree *worktree, struct got_fileindex *fileindex,
9336 got_worktree_delete_cb progress_cb, void *progress_arg)
9338 struct schedule_deletion_args sda;
9340 memset(&sda, 0, sizeof(sda));
9341 sda.worktree = worktree;
9342 sda.fileindex = fileindex;
9343 sda.progress_cb = progress_cb;
9344 sda.progress_arg = progress_arg;
9345 sda.repo = repo;
9346 sda.delete_local_mods = 0;
9347 sda.keep_on_disk = 0;
9348 sda.ignore_missing_paths = 0;
9349 sda.status_codes = NULL;
9351 return worktree_status(worktree, path, fileindex, repo,
9352 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9355 const struct got_error *
9356 got_worktree_patch_complete(struct got_fileindex *fileindex,
9357 const char *fileindex_path)
9359 const struct got_error *err = NULL;
9361 err = sync_fileindex(fileindex, fileindex_path);
9362 got_fileindex_free(fileindex);
9364 return err;