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>
20 #include <dirent.h>
21 #include <limits.h>
22 #include <stddef.h>
23 #include <string.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <time.h>
27 #include <fcntl.h>
28 #include <errno.h>
29 #include <unistd.h>
30 #include <zlib.h>
31 #include <fnmatch.h>
32 #include <libgen.h>
34 #include "got_compat.h"
36 #include "got_error.h"
37 #include "got_repository.h"
38 #include "got_reference.h"
39 #include "got_object.h"
40 #include "got_path.h"
41 #include "got_cancel.h"
42 #include "got_worktree.h"
43 #include "got_opentemp.h"
44 #include "got_diff.h"
46 #include "got_lib_worktree.h"
47 #include "got_lib_sha1.h"
48 #include "got_lib_fileindex.h"
49 #include "got_lib_inflate.h"
50 #include "got_lib_delta.h"
51 #include "got_lib_object.h"
52 #include "got_lib_object_parse.h"
53 #include "got_lib_object_create.h"
54 #include "got_lib_object_idset.h"
55 #include "got_lib_diff.h"
56 #include "got_lib_gotconfig.h"
58 #ifndef MIN
59 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
60 #endif
62 #define GOT_MERGE_LABEL_MERGED "merged change"
63 #define GOT_MERGE_LABEL_BASE "3-way merge base"
65 static mode_t apply_umask(mode_t);
67 static const struct got_error *
68 create_meta_file(const char *path_got, const char *name, const char *content)
69 {
70 const struct got_error *err = NULL;
71 char *path;
73 if (asprintf(&path, "%s/%s", path_got, name) == -1)
74 return got_error_from_errno("asprintf");
76 err = got_path_create_file(path, content);
77 free(path);
78 return err;
79 }
81 static const struct got_error *
82 update_meta_file(const char *path_got, const char *name, const char *content)
83 {
84 const struct got_error *err = NULL;
85 FILE *tmpfile = NULL;
86 char *tmppath = NULL;
87 char *path = NULL;
89 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
90 err = got_error_from_errno("asprintf");
91 path = NULL;
92 goto done;
93 }
95 err = got_opentemp_named(&tmppath, &tmpfile, path, "");
96 if (err)
97 goto done;
99 if (content) {
100 int len = fprintf(tmpfile, "%s\n", content);
101 if (len != strlen(content) + 1) {
102 err = got_error_from_errno2("fprintf", tmppath);
103 goto done;
107 if (rename(tmppath, path) != 0) {
108 err = got_error_from_errno3("rename", tmppath, path);
109 unlink(tmppath);
110 goto done;
113 done:
114 if (fclose(tmpfile) == EOF && err == NULL)
115 err = got_error_from_errno2("fclose", tmppath);
116 free(tmppath);
117 return err;
120 static const struct got_error *
121 write_head_ref(const char *path_got, struct got_reference *head_ref)
123 const struct got_error *err = NULL;
124 char *refstr = NULL;
126 if (got_ref_is_symbolic(head_ref)) {
127 refstr = got_ref_to_str(head_ref);
128 if (refstr == NULL)
129 return got_error_from_errno("got_ref_to_str");
130 } else {
131 refstr = strdup(got_ref_get_name(head_ref));
132 if (refstr == NULL)
133 return got_error_from_errno("strdup");
135 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
136 free(refstr);
137 return err;
140 const struct got_error *
141 got_worktree_init(const char *path, struct got_reference *head_ref,
142 const char *prefix, struct got_repository *repo)
144 const struct got_error *err = NULL;
145 struct got_object_id *commit_id = NULL;
146 uuid_t uuid;
147 uint32_t uuid_status;
148 int obj_type;
149 char *path_got = NULL;
150 char *formatstr = NULL;
151 char *absprefix = NULL;
152 char *basestr = NULL;
153 char *uuidstr = NULL;
155 if (strcmp(path, got_repo_get_path(repo)) == 0) {
156 err = got_error(GOT_ERR_WORKTREE_REPO);
157 goto done;
160 err = got_ref_resolve(&commit_id, repo, head_ref);
161 if (err)
162 return err;
163 err = got_object_get_type(&obj_type, repo, commit_id);
164 if (err)
165 return err;
166 if (obj_type != GOT_OBJ_TYPE_COMMIT)
167 return got_error(GOT_ERR_OBJ_TYPE);
169 if (!got_path_is_absolute(prefix)) {
170 if (asprintf(&absprefix, "/%s", prefix) == -1)
171 return got_error_from_errno("asprintf");
174 /* Create top-level directory (may already exist). */
175 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
176 err = got_error_from_errno2("mkdir", path);
177 goto done;
180 /* Create .got directory (may already exist). */
181 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
182 err = got_error_from_errno("asprintf");
183 goto done;
185 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
186 err = got_error_from_errno2("mkdir", path_got);
187 goto done;
190 /* Create an empty lock file. */
191 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
192 if (err)
193 goto done;
195 /* Create an empty file index. */
196 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
197 if (err)
198 goto done;
200 /* Write the HEAD reference. */
201 err = write_head_ref(path_got, head_ref);
202 if (err)
203 goto done;
205 /* Record our base commit. */
206 err = got_object_id_str(&basestr, commit_id);
207 if (err)
208 goto done;
209 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
210 if (err)
211 goto done;
213 /* Store path to repository. */
214 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
215 got_repo_get_path(repo));
216 if (err)
217 goto done;
219 /* Store in-repository path prefix. */
220 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
221 absprefix ? absprefix : prefix);
222 if (err)
223 goto done;
225 /* Generate UUID. */
226 uuid_create(&uuid, &uuid_status);
227 if (uuid_status != uuid_s_ok) {
228 err = got_error_uuid(uuid_status, "uuid_create");
229 goto done;
231 uuid_to_string(&uuid, &uuidstr, &uuid_status);
232 if (uuid_status != uuid_s_ok) {
233 err = got_error_uuid(uuid_status, "uuid_to_string");
234 goto done;
236 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
237 if (err)
238 goto done;
240 /* Stamp work tree with format file. */
241 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
242 err = got_error_from_errno("asprintf");
243 goto done;
245 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
246 if (err)
247 goto done;
249 done:
250 free(commit_id);
251 free(path_got);
252 free(formatstr);
253 free(absprefix);
254 free(basestr);
255 free(uuidstr);
256 return err;
259 const struct got_error *
260 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
261 const char *path_prefix)
263 char *absprefix = NULL;
265 if (!got_path_is_absolute(path_prefix)) {
266 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
267 return got_error_from_errno("asprintf");
269 *match = (strcmp(absprefix ? absprefix : path_prefix,
270 worktree->path_prefix) == 0);
271 free(absprefix);
272 return NULL;
275 const char *
276 got_worktree_get_head_ref_name(struct got_worktree *worktree)
278 return worktree->head_ref_name;
281 const struct got_error *
282 got_worktree_set_head_ref(struct got_worktree *worktree,
283 struct got_reference *head_ref)
285 const struct got_error *err = NULL;
286 char *path_got = NULL, *head_ref_name = NULL;
288 if (asprintf(&path_got, "%s/%s", worktree->root_path,
289 GOT_WORKTREE_GOT_DIR) == -1) {
290 err = got_error_from_errno("asprintf");
291 path_got = NULL;
292 goto done;
295 head_ref_name = strdup(got_ref_get_name(head_ref));
296 if (head_ref_name == NULL) {
297 err = got_error_from_errno("strdup");
298 goto done;
301 err = write_head_ref(path_got, head_ref);
302 if (err)
303 goto done;
305 free(worktree->head_ref_name);
306 worktree->head_ref_name = head_ref_name;
307 done:
308 free(path_got);
309 if (err)
310 free(head_ref_name);
311 return err;
314 struct got_object_id *
315 got_worktree_get_base_commit_id(struct got_worktree *worktree)
317 return worktree->base_commit_id;
320 const struct got_error *
321 got_worktree_set_base_commit_id(struct got_worktree *worktree,
322 struct got_repository *repo, struct got_object_id *commit_id)
324 const struct got_error *err;
325 struct got_object *obj = NULL;
326 char *id_str = NULL;
327 char *path_got = NULL;
329 if (asprintf(&path_got, "%s/%s", worktree->root_path,
330 GOT_WORKTREE_GOT_DIR) == -1) {
331 err = got_error_from_errno("asprintf");
332 path_got = NULL;
333 goto done;
336 err = got_object_open(&obj, repo, commit_id);
337 if (err)
338 return err;
340 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
341 err = got_error(GOT_ERR_OBJ_TYPE);
342 goto done;
345 /* Record our base commit. */
346 err = got_object_id_str(&id_str, commit_id);
347 if (err)
348 goto done;
349 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
350 if (err)
351 goto done;
353 free(worktree->base_commit_id);
354 worktree->base_commit_id = got_object_id_dup(commit_id);
355 if (worktree->base_commit_id == NULL) {
356 err = got_error_from_errno("got_object_id_dup");
357 goto done;
359 done:
360 if (obj)
361 got_object_close(obj);
362 free(id_str);
363 free(path_got);
364 return err;
367 const struct got_gotconfig *
368 got_worktree_get_gotconfig(struct got_worktree *worktree)
370 return worktree->gotconfig;
373 static const struct got_error *
374 lock_worktree(struct got_worktree *worktree, int operation)
376 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
377 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
378 : got_error_from_errno2("flock",
379 got_worktree_get_root_path(worktree)));
380 return NULL;
383 static const struct got_error *
384 add_dir_on_disk(struct got_worktree *worktree, const char *path)
386 const struct got_error *err = NULL;
387 char *abspath;
389 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
390 return got_error_from_errno("asprintf");
392 err = got_path_mkdir(abspath);
393 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
394 struct stat sb;
395 err = NULL;
396 if (lstat(abspath, &sb) == -1) {
397 err = got_error_from_errno2("lstat", abspath);
398 } else if (!S_ISDIR(sb.st_mode)) {
399 /* TODO directory is obstructed; do something */
400 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
403 free(abspath);
404 return err;
407 static const struct got_error *
408 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
410 const struct got_error *err = NULL;
411 uint8_t fbuf1[8192];
412 uint8_t fbuf2[8192];
413 size_t flen1 = 0, flen2 = 0;
415 *same = 1;
417 for (;;) {
418 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
419 if (flen1 == 0 && ferror(f1)) {
420 err = got_error_from_errno("fread");
421 break;
423 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
424 if (flen2 == 0 && ferror(f2)) {
425 err = got_error_from_errno("fread");
426 break;
428 if (flen1 == 0) {
429 if (flen2 != 0)
430 *same = 0;
431 break;
432 } else if (flen2 == 0) {
433 if (flen1 != 0)
434 *same = 0;
435 break;
436 } else if (flen1 == flen2) {
437 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
438 *same = 0;
439 break;
441 } else {
442 *same = 0;
443 break;
447 return err;
450 static const struct got_error *
451 check_files_equal(int *same, FILE *f1, FILE *f2)
453 struct stat sb;
454 size_t size1, size2;
456 *same = 1;
458 if (fstat(fileno(f1), &sb) != 0)
459 return got_error_from_errno("fstat");
460 size1 = sb.st_size;
462 if (fstat(fileno(f2), &sb) != 0)
463 return got_error_from_errno("fstat");
464 size2 = sb.st_size;
466 if (size1 != size2) {
467 *same = 0;
468 return NULL;
471 if (fseek(f1, 0L, SEEK_SET) == -1)
472 return got_ferror(f1, GOT_ERR_IO);
473 if (fseek(f2, 0L, SEEK_SET) == -1)
474 return got_ferror(f2, GOT_ERR_IO);
476 return check_file_contents_equal(same, f1, f2);
479 static const struct got_error *
480 copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
482 uint8_t fbuf[65536];
483 size_t flen;
484 ssize_t outlen;
486 *outsize = 0;
488 if (fseek(f, 0L, SEEK_SET) == -1)
489 return got_ferror(f, GOT_ERR_IO);
491 for (;;) {
492 flen = fread(fbuf, 1, sizeof(fbuf), f);
493 if (flen == 0) {
494 if (ferror(f))
495 return got_error_from_errno("fread");
496 if (feof(f))
497 break;
499 outlen = write(outfd, fbuf, flen);
500 if (outlen == -1)
501 return got_error_from_errno("write");
502 if (outlen != flen)
503 return got_error(GOT_ERR_IO);
504 *outsize += outlen;
507 return NULL;
510 static const struct got_error *
511 merge_binary_file(int *overlapcnt, int merged_fd,
512 FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
513 const char *label_deriv, const char *label_orig, const char *label_deriv2,
514 const char *ondisk_path)
516 const struct got_error *err = NULL;
517 int same_content, changed_deriv, changed_deriv2;
518 int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
519 off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
520 char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
521 char *base_path_orig = NULL, *base_path_deriv = NULL;
522 char *base_path_deriv2 = NULL;
524 *overlapcnt = 0;
526 err = check_files_equal(&same_content, f_deriv, f_deriv2);
527 if (err)
528 return err;
530 if (same_content)
531 return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
533 err = check_files_equal(&same_content, f_deriv, f_orig);
534 if (err)
535 return err;
536 changed_deriv = !same_content;
537 err = check_files_equal(&same_content, f_deriv2, f_orig);
538 if (err)
539 return err;
540 changed_deriv2 = !same_content;
542 if (changed_deriv && changed_deriv2) {
543 *overlapcnt = 1;
544 if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
545 err = got_error_from_errno("asprintf");
546 goto done;
548 if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
549 err = got_error_from_errno("asprintf");
550 goto done;
552 if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
553 err = got_error_from_errno("asprintf");
554 goto done;
556 err = got_opentemp_named_fd(&path_orig, &fd_orig,
557 base_path_orig, "");
558 if (err)
559 goto done;
560 err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
561 base_path_deriv, "");
562 if (err)
563 goto done;
564 err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
565 base_path_deriv2, "");
566 if (err)
567 goto done;
568 err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
569 if (err)
570 goto done;
571 err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
572 if (err)
573 goto done;
574 err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
575 if (err)
576 goto done;
577 if (dprintf(merged_fd, "Binary files differ and cannot be "
578 "merged automatically:\n") < 0) {
579 err = got_error_from_errno("dprintf");
580 goto done;
582 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
583 GOT_DIFF_CONFLICT_MARKER_BEGIN,
584 label_deriv ? " " : "",
585 label_deriv ? label_deriv : "",
586 path_deriv) < 0) {
587 err = got_error_from_errno("dprintf");
588 goto done;
590 if (size_orig > 0) {
591 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
592 GOT_DIFF_CONFLICT_MARKER_ORIG,
593 label_orig ? " " : "",
594 label_orig ? label_orig : "",
595 path_orig) < 0) {
596 err = got_error_from_errno("dprintf");
597 goto done;
600 if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
601 GOT_DIFF_CONFLICT_MARKER_SEP,
602 path_deriv2,
603 GOT_DIFF_CONFLICT_MARKER_END,
604 label_deriv2 ? " " : "",
605 label_deriv2 ? label_deriv2 : "") < 0) {
606 err = got_error_from_errno("dprintf");
607 goto done;
609 } else if (changed_deriv)
610 err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
611 else if (changed_deriv2)
612 err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
613 done:
614 if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
615 err == NULL)
616 err = got_error_from_errno2("unlink", path_orig);
617 if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
618 err = got_error_from_errno2("close", path_orig);
619 if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
620 err = got_error_from_errno2("close", path_deriv);
621 if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
622 err = got_error_from_errno2("close", path_deriv2);
623 free(path_orig);
624 free(path_deriv);
625 free(path_deriv2);
626 free(base_path_orig);
627 free(base_path_deriv);
628 free(base_path_deriv2);
629 return err;
632 /*
633 * Perform a 3-way merge where the file f_orig acts as the common
634 * ancestor, the file f_deriv acts as the first derived version,
635 * and the file f_deriv2 acts as the second derived version.
636 * The merge result will be written to a new file at ondisk_path; any
637 * existing file at this path will be replaced.
638 */
639 static const struct got_error *
640 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
641 FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
642 const char *path, uint16_t st_mode,
643 const char *label_orig, const char *label_deriv, const char *label_deriv2,
644 enum got_diff_algorithm diff_algo, struct got_repository *repo,
645 got_worktree_checkout_cb progress_cb, void *progress_arg)
647 const struct got_error *err = NULL;
648 int merged_fd = -1;
649 FILE *f_merged = NULL;
650 char *merged_path = NULL, *base_path = NULL;
651 int overlapcnt = 0;
652 char *parent = NULL;
654 *local_changes_subsumed = 0;
656 err = got_path_dirname(&parent, ondisk_path);
657 if (err)
658 return err;
660 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
661 err = got_error_from_errno("asprintf");
662 goto done;
665 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path, "");
666 if (err)
667 goto done;
669 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
670 f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
671 if (err) {
672 if (err->code != GOT_ERR_FILE_BINARY)
673 goto done;
674 err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
675 f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
676 ondisk_path);
677 if (err)
678 goto done;
681 err = (*progress_cb)(progress_arg,
682 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
683 if (err)
684 goto done;
686 if (fsync(merged_fd) != 0) {
687 err = got_error_from_errno("fsync");
688 goto done;
691 f_merged = fdopen(merged_fd, "r");
692 if (f_merged == NULL) {
693 err = got_error_from_errno("fdopen");
694 goto done;
696 merged_fd = -1;
698 /* Check if a clean merge has subsumed all local changes. */
699 if (overlapcnt == 0) {
700 err = check_files_equal(local_changes_subsumed, f_deriv,
701 f_merged);
702 if (err)
703 goto done;
706 if (fchmod(fileno(f_merged), apply_umask(st_mode)) != 0) {
707 err = got_error_from_errno2("fchmod", merged_path);
708 goto done;
711 if (rename(merged_path, ondisk_path) != 0) {
712 err = got_error_from_errno3("rename", merged_path,
713 ondisk_path);
714 goto done;
716 done:
717 if (err) {
718 if (merged_path)
719 unlink(merged_path);
721 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
722 err = got_error_from_errno("close");
723 if (f_merged && fclose(f_merged) == EOF && err == NULL)
724 err = got_error_from_errno("fclose");
725 free(merged_path);
726 free(base_path);
727 free(parent);
728 return err;
731 static const struct got_error *
732 update_symlink(const char *ondisk_path, const char *target_path,
733 size_t target_len)
735 /* This is not atomic but matches what 'ln -sf' does. */
736 if (unlink(ondisk_path) == -1)
737 return got_error_from_errno2("unlink", ondisk_path);
738 if (symlink(target_path, ondisk_path) == -1)
739 return got_error_from_errno3("symlink", target_path,
740 ondisk_path);
741 return NULL;
744 /*
745 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
746 * in the work tree with a file that contains conflict markers and the
747 * conflicting target paths of the original version, a "derived version"
748 * of a symlink from an incoming change, and a local version of the symlink.
750 * The original versions's target path can be NULL if it is not available,
751 * such as if both derived versions added a new symlink at the same path.
753 * The incoming derived symlink target is NULL in case the incoming change
754 * has deleted this symlink.
755 */
756 static const struct got_error *
757 install_symlink_conflict(const char *deriv_target,
758 struct got_object_id *deriv_base_commit_id, const char *orig_target,
759 const char *label_orig, const char *local_target, const char *ondisk_path)
761 const struct got_error *err;
762 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
763 FILE *f = NULL;
765 err = got_object_id_str(&id_str, deriv_base_commit_id);
766 if (err)
767 return got_error_from_errno("asprintf");
769 if (asprintf(&label_deriv, "%s: commit %s",
770 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
771 err = got_error_from_errno("asprintf");
772 goto done;
775 err = got_opentemp_named(&path, &f, "got-symlink-conflict", "");
776 if (err)
777 goto done;
779 if (fchmod(fileno(f), apply_umask(GOT_DEFAULT_FILE_MODE)) == -1) {
780 err = got_error_from_errno2("fchmod", path);
781 goto done;
784 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
785 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
786 deriv_target ? deriv_target : "(symlink was deleted)",
787 orig_target ? label_orig : "",
788 orig_target ? "\n" : "",
789 orig_target ? orig_target : "",
790 orig_target ? "\n" : "",
791 GOT_DIFF_CONFLICT_MARKER_SEP,
792 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
793 err = got_error_from_errno2("fprintf", path);
794 goto done;
797 if (unlink(ondisk_path) == -1) {
798 err = got_error_from_errno2("unlink", ondisk_path);
799 goto done;
801 if (rename(path, ondisk_path) == -1) {
802 err = got_error_from_errno3("rename", path, ondisk_path);
803 goto done;
805 done:
806 if (f != NULL && fclose(f) == EOF && err == NULL)
807 err = got_error_from_errno2("fclose", path);
808 free(path);
809 free(id_str);
810 free(label_deriv);
811 return err;
814 /* forward declaration */
815 static const struct got_error *
816 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
817 const char *, const char *, uint16_t, const char *,
818 struct got_blob_object *, struct got_object_id *,
819 struct got_repository *, got_worktree_checkout_cb, void *);
821 /*
822 * Merge a symlink into the work tree, where blob_orig acts as the common
823 * ancestor, deriv_target is the link target of the first derived version,
824 * and the symlink on disk acts as the second derived version.
825 * Assume that contents of both blobs represent symlinks.
826 */
827 static const struct got_error *
828 merge_symlink(struct got_worktree *worktree,
829 struct got_blob_object *blob_orig, const char *ondisk_path,
830 const char *path, const char *label_orig, const char *deriv_target,
831 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
832 got_worktree_checkout_cb progress_cb, void *progress_arg)
834 const struct got_error *err = NULL;
835 char *ancestor_target = NULL;
836 struct stat sb;
837 ssize_t ondisk_len, deriv_len;
838 char ondisk_target[PATH_MAX];
839 int have_local_change = 0;
840 int have_incoming_change = 0;
842 if (lstat(ondisk_path, &sb) == -1)
843 return got_error_from_errno2("lstat", ondisk_path);
845 ondisk_len = readlink(ondisk_path, ondisk_target,
846 sizeof(ondisk_target));
847 if (ondisk_len == -1) {
848 err = got_error_from_errno2("readlink",
849 ondisk_path);
850 goto done;
852 ondisk_target[ondisk_len] = '\0';
854 if (blob_orig) {
855 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
856 if (err)
857 goto done;
860 if (ancestor_target == NULL ||
861 (ondisk_len != strlen(ancestor_target) ||
862 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
863 have_local_change = 1;
865 deriv_len = strlen(deriv_target);
866 if (ancestor_target == NULL ||
867 (deriv_len != strlen(ancestor_target) ||
868 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
869 have_incoming_change = 1;
871 if (!have_local_change && !have_incoming_change) {
872 if (ancestor_target) {
873 /* Both sides made the same change. */
874 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
875 path);
876 } else if (deriv_len == ondisk_len &&
877 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
878 /* Both sides added the same symlink. */
879 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
880 path);
881 } else {
882 /* Both sides added symlinks which don't match. */
883 err = install_symlink_conflict(deriv_target,
884 deriv_base_commit_id, ancestor_target,
885 label_orig, ondisk_target, ondisk_path);
886 if (err)
887 goto done;
888 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
889 path);
891 } else if (!have_local_change && have_incoming_change) {
892 /* Apply the incoming change. */
893 err = update_symlink(ondisk_path, deriv_target,
894 strlen(deriv_target));
895 if (err)
896 goto done;
897 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
898 } else if (have_local_change && have_incoming_change) {
899 if (deriv_len == ondisk_len &&
900 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
901 /* Both sides made the same change. */
902 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
903 path);
904 } else {
905 err = install_symlink_conflict(deriv_target,
906 deriv_base_commit_id, ancestor_target, label_orig,
907 ondisk_target, ondisk_path);
908 if (err)
909 goto done;
910 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
911 path);
915 done:
916 free(ancestor_target);
917 return err;
920 static const struct got_error *
921 dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
923 const struct got_error *err = NULL;
924 char target_path[PATH_MAX];
925 ssize_t target_len;
926 size_t n;
927 FILE *f;
929 *outfile = NULL;
931 f = got_opentemp();
932 if (f == NULL)
933 return got_error_from_errno("got_opentemp");
934 target_len = readlink(ondisk_path, target_path, sizeof(target_path));
935 if (target_len == -1) {
936 err = got_error_from_errno2("readlink", ondisk_path);
937 goto done;
939 n = fwrite(target_path, 1, target_len, f);
940 if (n != target_len) {
941 err = got_ferror(f, GOT_ERR_IO);
942 goto done;
944 if (fflush(f) == EOF) {
945 err = got_error_from_errno("fflush");
946 goto done;
948 if (fseek(f, 0L, SEEK_SET) == -1) {
949 err = got_ferror(f, GOT_ERR_IO);
950 goto done;
952 done:
953 if (err)
954 fclose(f);
955 else
956 *outfile = f;
957 return err;
960 /*
961 * Perform a 3-way merge where blob_orig acts as the common ancestor,
962 * blob_deriv acts as the first derived version, and the file on disk
963 * acts as the second derived version.
964 */
965 static const struct got_error *
966 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
967 struct got_blob_object *blob_orig, const char *ondisk_path,
968 const char *path, uint16_t st_mode, const char *label_orig,
969 struct got_blob_object *blob_deriv,
970 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
971 got_worktree_checkout_cb progress_cb, void *progress_arg)
973 const struct got_error *err = NULL;
974 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
975 char *blob_orig_path = NULL;
976 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
977 char *label_deriv = NULL, *parent = NULL;
979 *local_changes_subsumed = 0;
981 err = got_path_dirname(&parent, ondisk_path);
982 if (err)
983 return err;
985 if (blob_orig) {
986 if (asprintf(&base_path, "%s/got-merge-blob-orig",
987 parent) == -1) {
988 err = got_error_from_errno("asprintf");
989 base_path = NULL;
990 goto done;
993 err = got_opentemp_named(&blob_orig_path, &f_orig,
994 base_path, "");
995 if (err)
996 goto done;
997 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
998 blob_orig);
999 if (err)
1000 goto done;
1001 free(base_path);
1002 } else {
1004 * No common ancestor exists. This is an "add vs add" conflict
1005 * and we simply use an empty ancestor file to make both files
1006 * appear in the merged result in their entirety.
1008 f_orig = got_opentemp();
1009 if (f_orig == NULL) {
1010 err = got_error_from_errno("got_opentemp");
1011 goto done;
1015 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1016 err = got_error_from_errno("asprintf");
1017 base_path = NULL;
1018 goto done;
1021 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path, "");
1022 if (err)
1023 goto done;
1024 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1025 blob_deriv);
1026 if (err)
1027 goto done;
1029 err = got_object_id_str(&id_str, deriv_base_commit_id);
1030 if (err)
1031 goto done;
1032 if (asprintf(&label_deriv, "%s: commit %s",
1033 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1034 err = got_error_from_errno("asprintf");
1035 goto done;
1039 * In order the run a 3-way merge with a symlink we copy the symlink's
1040 * target path into a temporary file and use that file with diff3.
1042 if (S_ISLNK(st_mode)) {
1043 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1044 if (err)
1045 goto done;
1046 } else {
1047 int fd;
1048 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1049 if (fd == -1) {
1050 err = got_error_from_errno2("open", ondisk_path);
1051 goto done;
1053 f_deriv2 = fdopen(fd, "r");
1054 if (f_deriv2 == NULL) {
1055 err = got_error_from_errno2("fdopen", ondisk_path);
1056 close(fd);
1057 goto done;
1061 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1062 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1063 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1064 done:
1065 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1066 err = got_error_from_errno("fclose");
1067 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1068 err = got_error_from_errno("fclose");
1069 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1070 err = got_error_from_errno("fclose");
1071 free(base_path);
1072 if (blob_orig_path) {
1073 unlink(blob_orig_path);
1074 free(blob_orig_path);
1076 if (blob_deriv_path) {
1077 unlink(blob_deriv_path);
1078 free(blob_deriv_path);
1080 free(id_str);
1081 free(label_deriv);
1082 free(parent);
1083 return err;
1086 static const struct got_error *
1087 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1088 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1089 int wt_fd, const char *path, struct got_object_id *blob_id)
1091 const struct got_error *err = NULL;
1092 struct got_fileindex_entry *new_ie;
1094 *new_iep = NULL;
1096 err = got_fileindex_entry_alloc(&new_ie, path);
1097 if (err)
1098 return err;
1100 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1101 blob_id->sha1, base_commit_id->sha1, 1);
1102 if (err)
1103 goto done;
1105 err = got_fileindex_entry_add(fileindex, new_ie);
1106 done:
1107 if (err)
1108 got_fileindex_entry_free(new_ie);
1109 else
1110 *new_iep = new_ie;
1111 return err;
1114 static mode_t
1115 get_ondisk_perms(int executable, mode_t st_mode)
1117 mode_t xbits = S_IXUSR;
1119 if (executable) {
1120 /* Map read bits to execute bits. */
1121 if (st_mode & S_IRGRP)
1122 xbits |= S_IXGRP;
1123 if (st_mode & S_IROTH)
1124 xbits |= S_IXOTH;
1125 return st_mode | xbits;
1128 return st_mode;
1131 static mode_t
1132 apply_umask(mode_t mode)
1134 mode_t um;
1136 um = umask(000);
1137 umask(um);
1138 return mode & ~um;
1141 /* forward declaration */
1142 static const struct got_error *
1143 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1144 const char *path, mode_t te_mode, mode_t st_mode,
1145 struct got_blob_object *blob, int restoring_missing_file,
1146 int reverting_versioned_file, int installing_bad_symlink,
1147 int path_is_unversioned, struct got_repository *repo,
1148 got_worktree_checkout_cb progress_cb, void *progress_arg);
1151 * This function assumes that the provided symlink target points at a
1152 * safe location in the work tree!
1154 static const struct got_error *
1155 replace_existing_symlink(int *did_something, const char *ondisk_path,
1156 const char *target_path, size_t target_len)
1158 const struct got_error *err = NULL;
1159 ssize_t elen;
1160 char etarget[PATH_MAX];
1161 int fd;
1163 *did_something = 0;
1166 * "Bad" symlinks (those pointing outside the work tree or into the
1167 * .got directory) are installed in the work tree as a regular file
1168 * which contains the bad symlink target path.
1169 * The new symlink target has already been checked for safety by our
1170 * caller. If we can successfully open a regular file then we simply
1171 * replace this file with a symlink below.
1173 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1174 if (fd == -1) {
1175 if (!got_err_open_nofollow_on_symlink())
1176 return got_error_from_errno2("open", ondisk_path);
1178 /* We are updating an existing on-disk symlink. */
1179 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1180 if (elen == -1)
1181 return got_error_from_errno2("readlink", ondisk_path);
1183 if (elen == target_len &&
1184 memcmp(etarget, target_path, target_len) == 0)
1185 return NULL; /* nothing to do */
1188 *did_something = 1;
1189 err = update_symlink(ondisk_path, target_path, target_len);
1190 if (fd != -1 && close(fd) == -1 && err == NULL)
1191 err = got_error_from_errno2("close", ondisk_path);
1192 return err;
1195 static const struct got_error *
1196 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1197 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1199 const struct got_error *err = NULL;
1200 char canonpath[PATH_MAX];
1201 char *path_got = NULL;
1203 *is_bad_symlink = 0;
1205 if (target_len >= sizeof(canonpath)) {
1206 *is_bad_symlink = 1;
1207 return NULL;
1211 * We do not use realpath(3) to resolve the symlink's target
1212 * path because we don't want to resolve symlinks recursively.
1213 * Instead we make the path absolute and then canonicalize it.
1214 * Relative symlink target lookup should begin at the directory
1215 * in which the blob object is being installed.
1217 if (!got_path_is_absolute(target_path)) {
1218 char *abspath, *parent;
1219 err = got_path_dirname(&parent, ondisk_path);
1220 if (err)
1221 return err;
1222 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1223 free(parent);
1224 return got_error_from_errno("asprintf");
1226 free(parent);
1227 if (strlen(abspath) >= sizeof(canonpath)) {
1228 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1229 free(abspath);
1230 return err;
1232 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1233 free(abspath);
1234 if (err)
1235 return err;
1236 } else {
1237 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1238 if (err)
1239 return err;
1242 /* Only allow symlinks pointing at paths within the work tree. */
1243 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1244 *is_bad_symlink = 1;
1245 return NULL;
1248 /* Do not allow symlinks pointing into the .got directory. */
1249 if (asprintf(&path_got, "%s/%s", wtroot_path,
1250 GOT_WORKTREE_GOT_DIR) == -1)
1251 return got_error_from_errno("asprintf");
1252 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1253 *is_bad_symlink = 1;
1255 free(path_got);
1256 return NULL;
1259 static const struct got_error *
1260 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1261 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1262 int restoring_missing_file, int reverting_versioned_file,
1263 int path_is_unversioned, int allow_bad_symlinks,
1264 struct got_repository *repo,
1265 got_worktree_checkout_cb progress_cb, void *progress_arg)
1267 const struct got_error *err = NULL;
1268 char target_path[PATH_MAX];
1269 size_t len, target_len = 0;
1270 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1271 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1273 *is_bad_symlink = 0;
1276 * Blob object content specifies the target path of the link.
1277 * If a symbolic link cannot be installed we instead create
1278 * a regular file which contains the link target path stored
1279 * in the blob object.
1281 do {
1282 err = got_object_blob_read_block(&len, blob);
1283 if (err)
1284 return err;
1286 if (len + target_len >= sizeof(target_path)) {
1287 /* Path too long; install as a regular file. */
1288 *is_bad_symlink = 1;
1289 got_object_blob_rewind(blob);
1290 return install_blob(worktree, ondisk_path, path,
1291 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1292 restoring_missing_file, reverting_versioned_file,
1293 1, path_is_unversioned, repo, progress_cb,
1294 progress_arg);
1296 if (len > 0) {
1297 /* Skip blob object header first time around. */
1298 memcpy(target_path + target_len, buf + hdrlen,
1299 len - hdrlen);
1300 target_len += len - hdrlen;
1301 hdrlen = 0;
1303 } while (len != 0);
1304 target_path[target_len] = '\0';
1306 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1307 ondisk_path, worktree->root_path);
1308 if (err)
1309 return err;
1311 if (*is_bad_symlink && !allow_bad_symlinks) {
1312 /* install as a regular file */
1313 got_object_blob_rewind(blob);
1314 err = install_blob(worktree, ondisk_path, path,
1315 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1316 restoring_missing_file, reverting_versioned_file, 1,
1317 path_is_unversioned, repo, progress_cb, progress_arg);
1318 return err;
1321 if (symlink(target_path, ondisk_path) == -1) {
1322 if (errno == EEXIST) {
1323 int symlink_replaced;
1324 if (path_is_unversioned) {
1325 err = (*progress_cb)(progress_arg,
1326 GOT_STATUS_UNVERSIONED, path);
1327 return err;
1329 err = replace_existing_symlink(&symlink_replaced,
1330 ondisk_path, target_path, target_len);
1331 if (err)
1332 return err;
1333 if (progress_cb) {
1334 if (symlink_replaced) {
1335 err = (*progress_cb)(progress_arg,
1336 reverting_versioned_file ?
1337 GOT_STATUS_REVERT :
1338 GOT_STATUS_UPDATE, path);
1339 } else {
1340 err = (*progress_cb)(progress_arg,
1341 GOT_STATUS_EXISTS, path);
1344 return err; /* Nothing else to do. */
1347 if (errno == ENOENT) {
1348 char *parent;
1349 err = got_path_dirname(&parent, ondisk_path);
1350 if (err)
1351 return err;
1352 err = add_dir_on_disk(worktree, parent);
1353 free(parent);
1354 if (err)
1355 return err;
1357 * Retry, and fall through to error handling
1358 * below if this second attempt fails.
1360 if (symlink(target_path, ondisk_path) != -1) {
1361 err = NULL; /* success */
1362 return err;
1366 /* Handle errors from first or second creation attempt. */
1367 if (errno == ENAMETOOLONG) {
1368 /* bad target path; install as a regular file */
1369 *is_bad_symlink = 1;
1370 got_object_blob_rewind(blob);
1371 err = install_blob(worktree, ondisk_path, path,
1372 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1373 restoring_missing_file, reverting_versioned_file, 1,
1374 path_is_unversioned, repo,
1375 progress_cb, progress_arg);
1376 } else if (errno == ENOTDIR) {
1377 err = got_error_path(ondisk_path,
1378 GOT_ERR_FILE_OBSTRUCTED);
1379 } else {
1380 err = got_error_from_errno3("symlink",
1381 target_path, ondisk_path);
1383 } else if (progress_cb)
1384 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1385 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1386 return err;
1389 static const struct got_error *
1390 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1391 const char *path, mode_t te_mode, mode_t st_mode,
1392 struct got_blob_object *blob, int restoring_missing_file,
1393 int reverting_versioned_file, int installing_bad_symlink,
1394 int path_is_unversioned, struct got_repository *repo,
1395 got_worktree_checkout_cb progress_cb, void *progress_arg)
1397 const struct got_error *err = NULL;
1398 int fd = -1;
1399 size_t len, hdrlen;
1400 int update = 0;
1401 char *tmppath = NULL;
1402 mode_t mode;
1404 mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1405 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1406 O_CLOEXEC, mode);
1407 if (fd == -1) {
1408 if (errno == ENOENT) {
1409 char *parent;
1410 err = got_path_dirname(&parent, path);
1411 if (err)
1412 return err;
1413 err = add_dir_on_disk(worktree, parent);
1414 free(parent);
1415 if (err)
1416 return err;
1417 fd = open(ondisk_path,
1418 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1419 mode);
1420 if (fd == -1)
1421 return got_error_from_errno2("open",
1422 ondisk_path);
1423 } else if (errno == EEXIST) {
1424 if (path_is_unversioned) {
1425 err = (*progress_cb)(progress_arg,
1426 GOT_STATUS_UNVERSIONED, path);
1427 goto done;
1429 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1430 !S_ISREG(st_mode) && !installing_bad_symlink) {
1431 /* TODO file is obstructed; do something */
1432 err = got_error_path(ondisk_path,
1433 GOT_ERR_FILE_OBSTRUCTED);
1434 goto done;
1435 } else {
1436 err = got_opentemp_named_fd(&tmppath, &fd,
1437 ondisk_path, "");
1438 if (err)
1439 goto done;
1440 update = 1;
1442 if (fchmod(fd, apply_umask(mode)) == -1) {
1443 err = got_error_from_errno2("fchmod",
1444 tmppath);
1445 goto done;
1448 } else
1449 return got_error_from_errno2("open", ondisk_path);
1452 if (progress_cb) {
1453 if (restoring_missing_file)
1454 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1455 path);
1456 else if (reverting_versioned_file)
1457 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1458 path);
1459 else
1460 err = (*progress_cb)(progress_arg,
1461 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1462 if (err)
1463 goto done;
1466 hdrlen = got_object_blob_get_hdrlen(blob);
1467 do {
1468 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1469 err = got_object_blob_read_block(&len, blob);
1470 if (err)
1471 break;
1472 if (len > 0) {
1473 /* Skip blob object header first time around. */
1474 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1475 if (outlen == -1) {
1476 err = got_error_from_errno("write");
1477 goto done;
1478 } else if (outlen != len - hdrlen) {
1479 err = got_error(GOT_ERR_IO);
1480 goto done;
1482 hdrlen = 0;
1484 } while (len != 0);
1486 if (fsync(fd) != 0) {
1487 err = got_error_from_errno("fsync");
1488 goto done;
1491 if (update) {
1492 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1493 err = got_error_from_errno2("unlink", ondisk_path);
1494 goto done;
1496 if (rename(tmppath, ondisk_path) != 0) {
1497 err = got_error_from_errno3("rename", tmppath,
1498 ondisk_path);
1499 goto done;
1501 free(tmppath);
1502 tmppath = NULL;
1505 done:
1506 if (fd != -1 && close(fd) == -1 && err == NULL)
1507 err = got_error_from_errno("close");
1508 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1509 err = got_error_from_errno2("unlink", tmppath);
1510 free(tmppath);
1511 return err;
1514 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1515 static const struct got_error *
1516 get_modified_file_content_status(unsigned char *status, FILE *f)
1518 const struct got_error *err = NULL;
1519 const char *markers[3] = {
1520 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1521 GOT_DIFF_CONFLICT_MARKER_SEP,
1522 GOT_DIFF_CONFLICT_MARKER_END
1524 int i = 0;
1525 char *line = NULL;
1526 size_t linesize = 0;
1527 ssize_t linelen;
1529 while (*status == GOT_STATUS_MODIFY) {
1530 linelen = getline(&line, &linesize, f);
1531 if (linelen == -1) {
1532 if (feof(f))
1533 break;
1534 err = got_ferror(f, GOT_ERR_IO);
1535 break;
1538 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1539 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1540 == 0)
1541 *status = GOT_STATUS_CONFLICT;
1542 else
1543 i++;
1546 free(line);
1548 return err;
1551 static int
1552 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1554 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1555 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1558 static int
1559 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1561 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1562 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1563 ie->mtime_sec == sb->st_mtim.tv_sec &&
1564 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1565 ie->size == (sb->st_size & 0xffffffff) &&
1566 !xbit_differs(ie, sb->st_mode));
1569 static unsigned char
1570 get_staged_status(struct got_fileindex_entry *ie)
1572 switch (got_fileindex_entry_stage_get(ie)) {
1573 case GOT_FILEIDX_STAGE_ADD:
1574 return GOT_STATUS_ADD;
1575 case GOT_FILEIDX_STAGE_DELETE:
1576 return GOT_STATUS_DELETE;
1577 case GOT_FILEIDX_STAGE_MODIFY:
1578 return GOT_STATUS_MODIFY;
1579 default:
1580 return GOT_STATUS_NO_CHANGE;
1584 static const struct got_error *
1585 get_symlink_modification_status(unsigned char *status,
1586 struct got_fileindex_entry *ie, const char *abspath,
1587 int dirfd, const char *de_name, struct got_blob_object *blob)
1589 const struct got_error *err = NULL;
1590 char target_path[PATH_MAX];
1591 char etarget[PATH_MAX];
1592 ssize_t elen;
1593 size_t len, target_len = 0;
1594 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1595 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1597 *status = GOT_STATUS_NO_CHANGE;
1599 /* Blob object content specifies the target path of the link. */
1600 do {
1601 err = got_object_blob_read_block(&len, blob);
1602 if (err)
1603 return err;
1604 if (len + target_len >= sizeof(target_path)) {
1606 * Should not happen. The blob contents were OK
1607 * when this symlink was installed.
1609 return got_error(GOT_ERR_NO_SPACE);
1611 if (len > 0) {
1612 /* Skip blob object header first time around. */
1613 memcpy(target_path + target_len, buf + hdrlen,
1614 len - hdrlen);
1615 target_len += len - hdrlen;
1616 hdrlen = 0;
1618 } while (len != 0);
1619 target_path[target_len] = '\0';
1621 if (dirfd != -1) {
1622 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1623 if (elen == -1)
1624 return got_error_from_errno2("readlinkat", abspath);
1625 } else {
1626 elen = readlink(abspath, etarget, sizeof(etarget));
1627 if (elen == -1)
1628 return got_error_from_errno2("readlink", abspath);
1631 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1632 *status = GOT_STATUS_MODIFY;
1634 return NULL;
1637 static const struct got_error *
1638 get_file_status(unsigned char *status, struct stat *sb,
1639 struct got_fileindex_entry *ie, const char *abspath,
1640 int dirfd, const char *de_name, struct got_repository *repo)
1642 const struct got_error *err = NULL;
1643 struct got_object_id id;
1644 size_t hdrlen;
1645 int fd = -1, fd1 = -1;
1646 FILE *f = NULL;
1647 uint8_t fbuf[8192];
1648 struct got_blob_object *blob = NULL;
1649 size_t flen, blen;
1650 unsigned char staged_status = get_staged_status(ie);
1652 *status = GOT_STATUS_NO_CHANGE;
1653 memset(sb, 0, sizeof(*sb));
1656 * Whenever the caller provides a directory descriptor and a
1657 * directory entry name for the file, use them! This prevents
1658 * race conditions if filesystem paths change beneath our feet.
1660 if (dirfd != -1) {
1661 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1662 if (errno == ENOENT) {
1663 if (got_fileindex_entry_has_file_on_disk(ie))
1664 *status = GOT_STATUS_MISSING;
1665 else
1666 *status = GOT_STATUS_DELETE;
1667 goto done;
1669 err = got_error_from_errno2("fstatat", abspath);
1670 goto done;
1672 } else {
1673 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1674 if (fd == -1 && errno != ENOENT &&
1675 !got_err_open_nofollow_on_symlink())
1676 return got_error_from_errno2("open", abspath);
1677 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1678 if (lstat(abspath, sb) == -1)
1679 return got_error_from_errno2("lstat", abspath);
1680 } else if (fd == -1 || fstat(fd, sb) == -1) {
1681 if (errno == ENOENT) {
1682 if (got_fileindex_entry_has_file_on_disk(ie))
1683 *status = GOT_STATUS_MISSING;
1684 else
1685 *status = GOT_STATUS_DELETE;
1686 goto done;
1688 err = got_error_from_errno2("fstat", abspath);
1689 goto done;
1693 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1694 *status = GOT_STATUS_OBSTRUCTED;
1695 goto done;
1698 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1699 *status = GOT_STATUS_DELETE;
1700 goto done;
1701 } else if (!got_fileindex_entry_has_blob(ie) &&
1702 staged_status != GOT_STATUS_ADD) {
1703 *status = GOT_STATUS_ADD;
1704 goto done;
1707 if (!stat_info_differs(ie, sb))
1708 goto done;
1710 if (S_ISLNK(sb->st_mode) &&
1711 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1712 *status = GOT_STATUS_MODIFY;
1713 goto done;
1716 if (staged_status == GOT_STATUS_MODIFY ||
1717 staged_status == GOT_STATUS_ADD)
1718 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1719 else
1720 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1722 fd1 = got_opentempfd();
1723 if (fd1 == -1) {
1724 err = got_error_from_errno("got_opentempfd");
1725 goto done;
1727 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1728 if (err)
1729 goto done;
1731 if (S_ISLNK(sb->st_mode)) {
1732 err = get_symlink_modification_status(status, ie,
1733 abspath, dirfd, de_name, blob);
1734 goto done;
1737 if (dirfd != -1) {
1738 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1739 if (fd == -1) {
1740 err = got_error_from_errno2("openat", abspath);
1741 goto done;
1745 f = fdopen(fd, "r");
1746 if (f == NULL) {
1747 err = got_error_from_errno2("fdopen", abspath);
1748 goto done;
1750 fd = -1;
1751 hdrlen = got_object_blob_get_hdrlen(blob);
1752 for (;;) {
1753 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1754 err = got_object_blob_read_block(&blen, blob);
1755 if (err)
1756 goto done;
1757 /* Skip length of blob object header first time around. */
1758 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1759 if (flen == 0 && ferror(f)) {
1760 err = got_error_from_errno("fread");
1761 goto done;
1763 if (blen - hdrlen == 0) {
1764 if (flen != 0)
1765 *status = GOT_STATUS_MODIFY;
1766 break;
1767 } else if (flen == 0) {
1768 if (blen - hdrlen != 0)
1769 *status = GOT_STATUS_MODIFY;
1770 break;
1771 } else if (blen - hdrlen == flen) {
1772 /* Skip blob object header first time around. */
1773 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1774 *status = GOT_STATUS_MODIFY;
1775 break;
1777 } else {
1778 *status = GOT_STATUS_MODIFY;
1779 break;
1781 hdrlen = 0;
1784 if (*status == GOT_STATUS_MODIFY) {
1785 rewind(f);
1786 err = get_modified_file_content_status(status, f);
1787 } else if (xbit_differs(ie, sb->st_mode))
1788 *status = GOT_STATUS_MODE_CHANGE;
1789 done:
1790 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1791 err = got_error_from_errno("close");
1792 if (blob)
1793 got_object_blob_close(blob);
1794 if (f != NULL && fclose(f) == EOF && err == NULL)
1795 err = got_error_from_errno2("fclose", abspath);
1796 if (fd != -1 && close(fd) == -1 && err == NULL)
1797 err = got_error_from_errno2("close", abspath);
1798 return err;
1802 * Update timestamps in the file index if a file is unmodified and
1803 * we had to run a full content comparison to find out.
1805 static const struct got_error *
1806 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1807 struct got_fileindex_entry *ie, struct stat *sb)
1809 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1810 return got_fileindex_entry_update(ie, wt_fd, path,
1811 ie->blob_sha1, ie->commit_sha1, 1);
1813 return NULL;
1816 static const struct got_error *
1817 update_blob(struct got_worktree *worktree,
1818 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1819 struct got_tree_entry *te, const char *path,
1820 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1821 void *progress_arg)
1823 const struct got_error *err = NULL;
1824 struct got_blob_object *blob = NULL;
1825 char *ondisk_path = NULL;
1826 unsigned char status = GOT_STATUS_NO_CHANGE;
1827 struct stat sb;
1828 int fd1 = -1, fd2 = -1;
1830 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1831 return got_error_from_errno("asprintf");
1833 if (ie) {
1834 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1835 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1836 goto done;
1838 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1839 repo);
1840 if (err)
1841 goto done;
1842 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1843 sb.st_mode = got_fileindex_perms_to_st(ie);
1844 } else {
1845 if (stat(ondisk_path, &sb) == -1) {
1846 if (errno != ENOENT) {
1847 err = got_error_from_errno2("stat",
1848 ondisk_path);
1849 goto done;
1851 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1852 status = GOT_STATUS_UNVERSIONED;
1853 } else {
1854 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1855 status = GOT_STATUS_UNVERSIONED;
1856 else
1857 status = GOT_STATUS_OBSTRUCTED;
1861 if (status == GOT_STATUS_OBSTRUCTED) {
1862 if (ie)
1863 got_fileindex_entry_mark_skipped(ie);
1864 err = (*progress_cb)(progress_arg, status, path);
1865 goto done;
1867 if (status == GOT_STATUS_CONFLICT) {
1868 if (ie)
1869 got_fileindex_entry_mark_skipped(ie);
1870 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1871 path);
1872 goto done;
1875 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1876 (S_ISLNK(te->mode) ||
1877 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1879 * This is a regular file or an installed bad symlink.
1880 * If the file index indicates that this file is already
1881 * up-to-date with respect to the repository we can skip
1882 * updating contents of this file.
1884 if (got_fileindex_entry_has_commit(ie) &&
1885 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1886 SHA1_DIGEST_LENGTH) == 0) {
1887 /* Same commit. */
1888 err = sync_timestamps(worktree->root_fd,
1889 path, status, ie, &sb);
1890 if (err)
1891 goto done;
1892 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1893 path);
1894 goto done;
1896 if (got_fileindex_entry_has_blob(ie) &&
1897 memcmp(ie->blob_sha1, te->id.sha1,
1898 SHA1_DIGEST_LENGTH) == 0) {
1899 /* Different commit but the same blob. */
1900 err = sync_timestamps(worktree->root_fd,
1901 path, status, ie, &sb);
1902 if (err)
1903 goto done;
1904 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1905 path);
1906 goto done;
1910 fd1 = got_opentempfd();
1911 if (fd1 == -1) {
1912 err = got_error_from_errno("got_opentempfd");
1913 goto done;
1915 err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
1916 if (err)
1917 goto done;
1919 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1920 int update_timestamps;
1921 struct got_blob_object *blob2 = NULL;
1922 char *label_orig = NULL;
1923 if (got_fileindex_entry_has_blob(ie)) {
1924 fd2 = got_opentempfd();
1925 if (fd2 == -1) {
1926 err = got_error_from_errno("got_opentempfd");
1927 goto done;
1929 struct got_object_id id2;
1930 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1931 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
1932 fd2);
1933 if (err)
1934 goto done;
1936 if (got_fileindex_entry_has_commit(ie)) {
1937 char id_str[SHA1_DIGEST_STRING_LENGTH];
1938 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1939 sizeof(id_str)) == NULL) {
1940 err = got_error_path(id_str,
1941 GOT_ERR_BAD_OBJ_ID_STR);
1942 goto done;
1944 if (asprintf(&label_orig, "%s: commit %s",
1945 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1946 err = got_error_from_errno("asprintf");
1947 goto done;
1950 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1951 char *link_target;
1952 err = got_object_blob_read_to_str(&link_target, blob);
1953 if (err)
1954 goto done;
1955 err = merge_symlink(worktree, blob2, ondisk_path, path,
1956 label_orig, link_target, worktree->base_commit_id,
1957 repo, progress_cb, progress_arg);
1958 free(link_target);
1959 } else {
1960 err = merge_blob(&update_timestamps, worktree, blob2,
1961 ondisk_path, path, sb.st_mode, label_orig, blob,
1962 worktree->base_commit_id, repo,
1963 progress_cb, progress_arg);
1965 free(label_orig);
1966 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
1967 err = got_error_from_errno("close");
1968 goto done;
1970 if (blob2)
1971 got_object_blob_close(blob2);
1972 if (err)
1973 goto done;
1975 * Do not update timestamps of files with local changes.
1976 * Otherwise, a future status walk would treat them as
1977 * unmodified files again.
1979 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1980 blob->id.sha1, worktree->base_commit_id->sha1,
1981 update_timestamps);
1982 } else if (status == GOT_STATUS_MODE_CHANGE) {
1983 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1984 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1985 } else if (status == GOT_STATUS_DELETE) {
1986 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1987 if (err)
1988 goto done;
1989 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1990 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1991 if (err)
1992 goto done;
1993 } else {
1994 int is_bad_symlink = 0;
1995 if (S_ISLNK(te->mode)) {
1996 err = install_symlink(&is_bad_symlink, worktree,
1997 ondisk_path, path, blob,
1998 status == GOT_STATUS_MISSING, 0,
1999 status == GOT_STATUS_UNVERSIONED, 0,
2000 repo, progress_cb, progress_arg);
2001 } else {
2002 err = install_blob(worktree, ondisk_path, path,
2003 te->mode, sb.st_mode, blob,
2004 status == GOT_STATUS_MISSING, 0, 0,
2005 status == GOT_STATUS_UNVERSIONED, repo,
2006 progress_cb, progress_arg);
2008 if (err)
2009 goto done;
2011 if (ie) {
2012 err = got_fileindex_entry_update(ie,
2013 worktree->root_fd, path, blob->id.sha1,
2014 worktree->base_commit_id->sha1, 1);
2015 } else {
2016 err = create_fileindex_entry(&ie, fileindex,
2017 worktree->base_commit_id, worktree->root_fd, path,
2018 &blob->id);
2020 if (err)
2021 goto done;
2023 if (is_bad_symlink) {
2024 got_fileindex_entry_filetype_set(ie,
2025 GOT_FILEIDX_MODE_BAD_SYMLINK);
2029 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2030 err = got_error_from_errno("close");
2031 goto done;
2033 got_object_blob_close(blob);
2034 done:
2035 free(ondisk_path);
2036 return err;
2039 static const struct got_error *
2040 remove_ondisk_file(const char *root_path, const char *path)
2042 const struct got_error *err = NULL;
2043 char *ondisk_path = NULL, *parent = NULL;
2045 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2046 return got_error_from_errno("asprintf");
2048 if (unlink(ondisk_path) == -1) {
2049 if (errno != ENOENT)
2050 err = got_error_from_errno2("unlink", ondisk_path);
2051 } else {
2052 size_t root_len = strlen(root_path);
2053 err = got_path_dirname(&parent, ondisk_path);
2054 if (err)
2055 goto done;
2056 while (got_path_cmp(parent, root_path,
2057 strlen(parent), root_len) != 0) {
2058 free(ondisk_path);
2059 ondisk_path = parent;
2060 parent = NULL;
2061 if (rmdir(ondisk_path) == -1) {
2062 if (errno != ENOTEMPTY)
2063 err = got_error_from_errno2("rmdir",
2064 ondisk_path);
2065 break;
2067 err = got_path_dirname(&parent, ondisk_path);
2068 if (err)
2069 break;
2072 done:
2073 free(ondisk_path);
2074 free(parent);
2075 return err;
2078 static const struct got_error *
2079 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2080 struct got_fileindex_entry *ie, struct got_repository *repo,
2081 got_worktree_checkout_cb progress_cb, void *progress_arg)
2083 const struct got_error *err = NULL;
2084 unsigned char status;
2085 struct stat sb;
2086 char *ondisk_path;
2088 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2089 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2091 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2092 == -1)
2093 return got_error_from_errno("asprintf");
2095 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2096 if (err)
2097 goto done;
2099 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2100 char ondisk_target[PATH_MAX];
2101 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2102 sizeof(ondisk_target));
2103 if (ondisk_len == -1) {
2104 err = got_error_from_errno2("readlink", ondisk_path);
2105 goto done;
2107 ondisk_target[ondisk_len] = '\0';
2108 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2109 NULL, NULL, /* XXX pass common ancestor info? */
2110 ondisk_target, ondisk_path);
2111 if (err)
2112 goto done;
2113 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2114 ie->path);
2115 goto done;
2118 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2119 status == GOT_STATUS_ADD) {
2120 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2121 if (err)
2122 goto done;
2124 * Preserve the working file and change the deleted blob's
2125 * entry into a schedule-add entry.
2127 err = got_fileindex_entry_update(ie, worktree->root_fd,
2128 ie->path, NULL, NULL, 0);
2129 } else {
2130 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2131 if (err)
2132 goto done;
2133 if (status == GOT_STATUS_NO_CHANGE) {
2134 err = remove_ondisk_file(worktree->root_path, ie->path);
2135 if (err)
2136 goto done;
2138 got_fileindex_entry_remove(fileindex, ie);
2140 done:
2141 free(ondisk_path);
2142 return err;
2145 struct diff_cb_arg {
2146 struct got_fileindex *fileindex;
2147 struct got_worktree *worktree;
2148 struct got_repository *repo;
2149 got_worktree_checkout_cb progress_cb;
2150 void *progress_arg;
2151 got_cancel_cb cancel_cb;
2152 void *cancel_arg;
2155 static const struct got_error *
2156 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2157 struct got_tree_entry *te, const char *parent_path)
2159 struct diff_cb_arg *a = arg;
2161 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2162 return got_error(GOT_ERR_CANCELLED);
2164 return update_blob(a->worktree, a->fileindex, ie, te,
2165 ie->path, a->repo, a->progress_cb, a->progress_arg);
2168 static const struct got_error *
2169 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2171 struct diff_cb_arg *a = arg;
2173 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2174 return got_error(GOT_ERR_CANCELLED);
2176 return delete_blob(a->worktree, a->fileindex, ie,
2177 a->repo, a->progress_cb, a->progress_arg);
2180 static const struct got_error *
2181 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2183 struct diff_cb_arg *a = arg;
2184 const struct got_error *err;
2185 char *path;
2187 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2188 return got_error(GOT_ERR_CANCELLED);
2190 if (got_object_tree_entry_is_submodule(te))
2191 return NULL;
2193 if (asprintf(&path, "%s%s%s", parent_path,
2194 parent_path[0] ? "/" : "", te->name)
2195 == -1)
2196 return got_error_from_errno("asprintf");
2198 if (S_ISDIR(te->mode))
2199 err = add_dir_on_disk(a->worktree, path);
2200 else
2201 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2202 a->repo, a->progress_cb, a->progress_arg);
2204 free(path);
2205 return err;
2208 const struct got_error *
2209 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2211 uint32_t uuid_status;
2213 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2214 if (uuid_status != uuid_s_ok) {
2215 *uuidstr = NULL;
2216 return got_error_uuid(uuid_status, "uuid_to_string");
2219 return NULL;
2222 static const struct got_error *
2223 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2225 const struct got_error *err = NULL;
2226 char *uuidstr = NULL;
2228 *refname = NULL;
2230 err = got_worktree_get_uuid(&uuidstr, worktree);
2231 if (err)
2232 return err;
2234 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2235 err = got_error_from_errno("asprintf");
2236 *refname = NULL;
2238 free(uuidstr);
2239 return err;
2242 const struct got_error *
2243 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2245 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2248 static const struct got_error *
2249 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2251 return get_ref_name(refname, worktree,
2252 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2255 static const struct got_error *
2256 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2258 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2261 static const struct got_error *
2262 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2264 return get_ref_name(refname, worktree,
2265 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2268 static const struct got_error *
2269 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2271 return get_ref_name(refname, worktree,
2272 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2275 static const struct got_error *
2276 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2278 return get_ref_name(refname, worktree,
2279 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2282 static const struct got_error *
2283 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2285 return get_ref_name(refname, worktree,
2286 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2289 static const struct got_error *
2290 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2292 return get_ref_name(refname, worktree,
2293 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2296 static const struct got_error *
2297 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2299 return get_ref_name(refname, worktree,
2300 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2303 const struct got_error *
2304 got_worktree_get_histedit_script_path(char **path,
2305 struct got_worktree *worktree)
2307 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2308 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2309 *path = NULL;
2310 return got_error_from_errno("asprintf");
2312 return NULL;
2315 static const struct got_error *
2316 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2318 return get_ref_name(refname, worktree,
2319 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2322 static const struct got_error *
2323 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2325 return get_ref_name(refname, worktree,
2326 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2330 * Prevent Git's garbage collector from deleting our base commit by
2331 * setting a reference to our base commit's ID.
2333 static const struct got_error *
2334 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2336 const struct got_error *err = NULL;
2337 struct got_reference *ref = NULL;
2338 char *refname;
2340 err = got_worktree_get_base_ref_name(&refname, worktree);
2341 if (err)
2342 return err;
2344 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2345 if (err)
2346 goto done;
2348 err = got_ref_write(ref, repo);
2349 done:
2350 free(refname);
2351 if (ref)
2352 got_ref_close(ref);
2353 return err;
2356 static const struct got_error *
2357 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2359 const struct got_error *err = NULL;
2361 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2362 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2363 err = got_error_from_errno("asprintf");
2364 *fileindex_path = NULL;
2366 return err;
2370 static const struct got_error *
2371 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2372 struct got_worktree *worktree)
2374 const struct got_error *err = NULL;
2375 FILE *index = NULL;
2377 *fileindex_path = NULL;
2378 *fileindex = got_fileindex_alloc();
2379 if (*fileindex == NULL)
2380 return got_error_from_errno("got_fileindex_alloc");
2382 err = get_fileindex_path(fileindex_path, worktree);
2383 if (err)
2384 goto done;
2386 index = fopen(*fileindex_path, "rbe");
2387 if (index == NULL) {
2388 if (errno != ENOENT)
2389 err = got_error_from_errno2("fopen", *fileindex_path);
2390 } else {
2391 err = got_fileindex_read(*fileindex, index);
2392 if (fclose(index) == EOF && err == NULL)
2393 err = got_error_from_errno("fclose");
2395 done:
2396 if (err) {
2397 free(*fileindex_path);
2398 *fileindex_path = NULL;
2399 got_fileindex_free(*fileindex);
2400 *fileindex = NULL;
2402 return err;
2405 struct bump_base_commit_id_arg {
2406 struct got_object_id *base_commit_id;
2407 const char *path;
2408 size_t path_len;
2409 const char *entry_name;
2410 got_worktree_checkout_cb progress_cb;
2411 void *progress_arg;
2414 /* Bump base commit ID of all files within an updated part of the work tree. */
2415 static const struct got_error *
2416 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2418 const struct got_error *err;
2419 struct bump_base_commit_id_arg *a = arg;
2421 if (a->entry_name) {
2422 if (strcmp(ie->path, a->path) != 0)
2423 return NULL;
2424 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2425 return NULL;
2427 if (got_fileindex_entry_was_skipped(ie))
2428 return NULL;
2430 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2431 SHA1_DIGEST_LENGTH) == 0)
2432 return NULL;
2434 if (a->progress_cb) {
2435 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2436 ie->path);
2437 if (err)
2438 return err;
2440 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2441 return NULL;
2444 static const struct got_error *
2445 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2446 struct got_fileindex *fileindex,
2447 got_worktree_checkout_cb progress_cb, void *progress_arg)
2449 struct bump_base_commit_id_arg bbc_arg;
2451 bbc_arg.base_commit_id = worktree->base_commit_id;
2452 bbc_arg.entry_name = NULL;
2453 bbc_arg.path = "";
2454 bbc_arg.path_len = 0;
2455 bbc_arg.progress_cb = progress_cb;
2456 bbc_arg.progress_arg = progress_arg;
2458 return got_fileindex_for_each_entry_safe(fileindex,
2459 bump_base_commit_id, &bbc_arg);
2462 static const struct got_error *
2463 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2465 const struct got_error *err = NULL;
2466 char *new_fileindex_path = NULL;
2467 FILE *new_index = NULL;
2468 struct timespec timeout;
2470 err = got_opentemp_named(&new_fileindex_path, &new_index,
2471 fileindex_path, "");
2472 if (err)
2473 goto done;
2475 err = got_fileindex_write(fileindex, new_index);
2476 if (err)
2477 goto done;
2479 if (rename(new_fileindex_path, fileindex_path) != 0) {
2480 err = got_error_from_errno3("rename", new_fileindex_path,
2481 fileindex_path);
2482 unlink(new_fileindex_path);
2486 * Sleep for a short amount of time to ensure that files modified after
2487 * this program exits have a different time stamp from the one which
2488 * was recorded in the file index.
2490 timeout.tv_sec = 0;
2491 timeout.tv_nsec = 1;
2492 nanosleep(&timeout, NULL);
2493 done:
2494 if (new_index)
2495 fclose(new_index);
2496 free(new_fileindex_path);
2497 return err;
2500 static const struct got_error *
2501 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2502 struct got_object_id **tree_id, const char *wt_relpath,
2503 struct got_commit_object *base_commit, struct got_worktree *worktree,
2504 struct got_repository *repo)
2506 const struct got_error *err = NULL;
2507 struct got_object_id *id = NULL;
2508 char *in_repo_path = NULL;
2509 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2511 *entry_type = GOT_OBJ_TYPE_ANY;
2512 *tree_relpath = NULL;
2513 *tree_id = NULL;
2515 if (wt_relpath[0] == '\0') {
2516 /* Check out all files within the work tree. */
2517 *entry_type = GOT_OBJ_TYPE_TREE;
2518 *tree_relpath = strdup("");
2519 if (*tree_relpath == NULL) {
2520 err = got_error_from_errno("strdup");
2521 goto done;
2523 err = got_object_id_by_path(tree_id, repo, base_commit,
2524 worktree->path_prefix);
2525 if (err)
2526 goto done;
2527 return NULL;
2530 /* Check out a subset of files in the work tree. */
2532 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2533 is_root_wt ? "" : "/", wt_relpath) == -1) {
2534 err = got_error_from_errno("asprintf");
2535 goto done;
2538 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2539 if (err)
2540 goto done;
2542 free(in_repo_path);
2543 in_repo_path = NULL;
2545 err = got_object_get_type(entry_type, repo, id);
2546 if (err)
2547 goto done;
2549 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2550 /* Check out a single file. */
2551 if (strchr(wt_relpath, '/') == NULL) {
2552 /* Check out a single file in work tree's root dir. */
2553 in_repo_path = strdup(worktree->path_prefix);
2554 if (in_repo_path == NULL) {
2555 err = got_error_from_errno("strdup");
2556 goto done;
2558 *tree_relpath = strdup("");
2559 if (*tree_relpath == NULL) {
2560 err = got_error_from_errno("strdup");
2561 goto done;
2563 } else {
2564 /* Check out a single file in a subdirectory. */
2565 err = got_path_dirname(tree_relpath, wt_relpath);
2566 if (err)
2567 return err;
2568 if (asprintf(&in_repo_path, "%s%s%s",
2569 worktree->path_prefix, is_root_wt ? "" : "/",
2570 *tree_relpath) == -1) {
2571 err = got_error_from_errno("asprintf");
2572 goto done;
2575 err = got_object_id_by_path(tree_id, repo,
2576 base_commit, in_repo_path);
2577 } else {
2578 /* Check out all files within a subdirectory. */
2579 *tree_id = got_object_id_dup(id);
2580 if (*tree_id == NULL) {
2581 err = got_error_from_errno("got_object_id_dup");
2582 goto done;
2584 *tree_relpath = strdup(wt_relpath);
2585 if (*tree_relpath == NULL) {
2586 err = got_error_from_errno("strdup");
2587 goto done;
2590 done:
2591 free(id);
2592 free(in_repo_path);
2593 if (err) {
2594 *entry_type = GOT_OBJ_TYPE_ANY;
2595 free(*tree_relpath);
2596 *tree_relpath = NULL;
2597 free(*tree_id);
2598 *tree_id = NULL;
2600 return err;
2603 static const struct got_error *
2604 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2605 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2606 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2607 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2609 const struct got_error *err = NULL;
2610 struct got_commit_object *commit = NULL;
2611 struct got_tree_object *tree = NULL;
2612 struct got_fileindex_diff_tree_cb diff_cb;
2613 struct diff_cb_arg arg;
2615 err = ref_base_commit(worktree, repo);
2616 if (err) {
2617 if (!(err->code == GOT_ERR_ERRNO &&
2618 (errno == EACCES || errno == EROFS)))
2619 goto done;
2620 err = (*progress_cb)(progress_arg,
2621 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2622 if (err)
2623 return err;
2626 err = got_object_open_as_commit(&commit, repo,
2627 worktree->base_commit_id);
2628 if (err)
2629 goto done;
2631 err = got_object_open_as_tree(&tree, repo, tree_id);
2632 if (err)
2633 goto done;
2635 if (entry_name &&
2636 got_object_tree_find_entry(tree, entry_name) == NULL) {
2637 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2638 goto done;
2641 diff_cb.diff_old_new = diff_old_new;
2642 diff_cb.diff_old = diff_old;
2643 diff_cb.diff_new = diff_new;
2644 arg.fileindex = fileindex;
2645 arg.worktree = worktree;
2646 arg.repo = repo;
2647 arg.progress_cb = progress_cb;
2648 arg.progress_arg = progress_arg;
2649 arg.cancel_cb = cancel_cb;
2650 arg.cancel_arg = cancel_arg;
2651 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2652 entry_name, repo, &diff_cb, &arg);
2653 done:
2654 if (tree)
2655 got_object_tree_close(tree);
2656 if (commit)
2657 got_object_commit_close(commit);
2658 return err;
2661 const struct got_error *
2662 got_worktree_checkout_files(struct got_worktree *worktree,
2663 struct got_pathlist_head *paths, struct got_repository *repo,
2664 got_worktree_checkout_cb progress_cb, void *progress_arg,
2665 got_cancel_cb cancel_cb, void *cancel_arg)
2667 const struct got_error *err = NULL, *sync_err, *unlockerr;
2668 struct got_commit_object *commit = NULL;
2669 struct got_tree_object *tree = NULL;
2670 struct got_fileindex *fileindex = NULL;
2671 char *fileindex_path = NULL;
2672 struct got_pathlist_entry *pe;
2673 struct tree_path_data {
2674 STAILQ_ENTRY(tree_path_data) entry;
2675 struct got_object_id *tree_id;
2676 int entry_type;
2677 char *relpath;
2678 char *entry_name;
2679 } *tpd = NULL;
2680 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2682 STAILQ_INIT(&tree_paths);
2684 err = lock_worktree(worktree, LOCK_EX);
2685 if (err)
2686 return err;
2688 err = got_object_open_as_commit(&commit, repo,
2689 worktree->base_commit_id);
2690 if (err)
2691 goto done;
2693 /* Map all specified paths to in-repository trees. */
2694 TAILQ_FOREACH(pe, paths, entry) {
2695 tpd = malloc(sizeof(*tpd));
2696 if (tpd == NULL) {
2697 err = got_error_from_errno("malloc");
2698 goto done;
2701 err = find_tree_entry_for_checkout(&tpd->entry_type,
2702 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2703 worktree, repo);
2704 if (err) {
2705 free(tpd);
2706 goto done;
2709 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2710 err = got_path_basename(&tpd->entry_name, pe->path);
2711 if (err) {
2712 free(tpd->relpath);
2713 free(tpd->tree_id);
2714 free(tpd);
2715 goto done;
2717 } else
2718 tpd->entry_name = NULL;
2720 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2724 * Read the file index.
2725 * Checking out files is supposed to be an idempotent operation.
2726 * If the on-disk file index is incomplete we will try to complete it.
2728 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2729 if (err)
2730 goto done;
2732 tpd = STAILQ_FIRST(&tree_paths);
2733 TAILQ_FOREACH(pe, paths, entry) {
2734 struct bump_base_commit_id_arg bbc_arg;
2736 err = checkout_files(worktree, fileindex, tpd->relpath,
2737 tpd->tree_id, tpd->entry_name, repo,
2738 progress_cb, progress_arg, cancel_cb, cancel_arg);
2739 if (err)
2740 break;
2742 bbc_arg.base_commit_id = worktree->base_commit_id;
2743 bbc_arg.entry_name = tpd->entry_name;
2744 bbc_arg.path = pe->path;
2745 bbc_arg.path_len = pe->path_len;
2746 bbc_arg.progress_cb = progress_cb;
2747 bbc_arg.progress_arg = progress_arg;
2748 err = got_fileindex_for_each_entry_safe(fileindex,
2749 bump_base_commit_id, &bbc_arg);
2750 if (err)
2751 break;
2753 tpd = STAILQ_NEXT(tpd, entry);
2755 sync_err = sync_fileindex(fileindex, fileindex_path);
2756 if (sync_err && err == NULL)
2757 err = sync_err;
2758 done:
2759 free(fileindex_path);
2760 if (tree)
2761 got_object_tree_close(tree);
2762 if (commit)
2763 got_object_commit_close(commit);
2764 if (fileindex)
2765 got_fileindex_free(fileindex);
2766 while (!STAILQ_EMPTY(&tree_paths)) {
2767 tpd = STAILQ_FIRST(&tree_paths);
2768 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2769 free(tpd->relpath);
2770 free(tpd->tree_id);
2771 free(tpd);
2773 unlockerr = lock_worktree(worktree, LOCK_SH);
2774 if (unlockerr && err == NULL)
2775 err = unlockerr;
2776 return err;
2779 struct merge_file_cb_arg {
2780 struct got_worktree *worktree;
2781 struct got_fileindex *fileindex;
2782 got_worktree_checkout_cb progress_cb;
2783 void *progress_arg;
2784 got_cancel_cb cancel_cb;
2785 void *cancel_arg;
2786 const char *label_orig;
2787 struct got_object_id *commit_id2;
2788 int allow_bad_symlinks;
2791 static const struct got_error *
2792 merge_file_cb(void *arg, struct got_blob_object *blob1,
2793 struct got_blob_object *blob2, FILE *f1, FILE *f2,
2794 struct got_object_id *id1, struct got_object_id *id2,
2795 const char *path1, const char *path2,
2796 mode_t mode1, mode_t mode2, struct got_repository *repo)
2798 static const struct got_error *err = NULL;
2799 struct merge_file_cb_arg *a = arg;
2800 struct got_fileindex_entry *ie;
2801 char *ondisk_path = NULL;
2802 struct stat sb;
2803 unsigned char status;
2804 int local_changes_subsumed;
2805 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2806 char *id_str = NULL, *label_deriv2 = NULL;
2808 if (blob1 && blob2) {
2809 ie = got_fileindex_entry_get(a->fileindex, path2,
2810 strlen(path2));
2811 if (ie == NULL)
2812 return (*a->progress_cb)(a->progress_arg,
2813 GOT_STATUS_MISSING, path2);
2815 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2816 path2) == -1)
2817 return got_error_from_errno("asprintf");
2819 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2820 repo);
2821 if (err)
2822 goto done;
2824 if (status == GOT_STATUS_DELETE) {
2825 err = (*a->progress_cb)(a->progress_arg,
2826 GOT_STATUS_MERGE, path2);
2827 goto done;
2829 if (status != GOT_STATUS_NO_CHANGE &&
2830 status != GOT_STATUS_MODIFY &&
2831 status != GOT_STATUS_CONFLICT &&
2832 status != GOT_STATUS_ADD) {
2833 err = (*a->progress_cb)(a->progress_arg, status, path2);
2834 goto done;
2837 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2838 char *link_target2;
2839 err = got_object_blob_read_to_str(&link_target2, blob2);
2840 if (err)
2841 goto done;
2842 err = merge_symlink(a->worktree, blob1, ondisk_path,
2843 path2, a->label_orig, link_target2, a->commit_id2,
2844 repo, a->progress_cb, a->progress_arg);
2845 free(link_target2);
2846 } else {
2847 int fd;
2849 f_orig = got_opentemp();
2850 if (f_orig == NULL) {
2851 err = got_error_from_errno("got_opentemp");
2852 goto done;
2854 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2855 f_orig, blob1);
2856 if (err)
2857 goto done;
2859 f_deriv2 = got_opentemp();
2860 if (f_deriv2 == NULL)
2861 goto done;
2862 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2863 f_deriv2, blob2);
2864 if (err)
2865 goto done;
2867 fd = open(ondisk_path,
2868 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2869 if (fd == -1) {
2870 err = got_error_from_errno2("open",
2871 ondisk_path);
2872 goto done;
2874 f_deriv = fdopen(fd, "r");
2875 if (f_deriv == NULL) {
2876 err = got_error_from_errno2("fdopen",
2877 ondisk_path);
2878 close(fd);
2879 goto done;
2881 err = got_object_id_str(&id_str, a->commit_id2);
2882 if (err)
2883 goto done;
2884 if (asprintf(&label_deriv2, "%s: commit %s",
2885 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2886 err = got_error_from_errno("asprintf");
2887 goto done;
2889 err = merge_file(&local_changes_subsumed, a->worktree,
2890 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2891 sb.st_mode, a->label_orig, NULL, label_deriv2,
2892 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2893 a->progress_cb, a->progress_arg);
2895 } else if (blob1) {
2896 ie = got_fileindex_entry_get(a->fileindex, path1,
2897 strlen(path1));
2898 if (ie == NULL)
2899 return (*a->progress_cb)(a->progress_arg,
2900 GOT_STATUS_MISSING, path1);
2902 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2903 path1) == -1)
2904 return got_error_from_errno("asprintf");
2906 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2907 repo);
2908 if (err)
2909 goto done;
2911 switch (status) {
2912 case GOT_STATUS_NO_CHANGE:
2913 err = (*a->progress_cb)(a->progress_arg,
2914 GOT_STATUS_DELETE, path1);
2915 if (err)
2916 goto done;
2917 err = remove_ondisk_file(a->worktree->root_path, path1);
2918 if (err)
2919 goto done;
2920 if (ie)
2921 got_fileindex_entry_mark_deleted_from_disk(ie);
2922 break;
2923 case GOT_STATUS_DELETE:
2924 case GOT_STATUS_MISSING:
2925 err = (*a->progress_cb)(a->progress_arg,
2926 GOT_STATUS_DELETE, path1);
2927 if (err)
2928 goto done;
2929 if (ie)
2930 got_fileindex_entry_mark_deleted_from_disk(ie);
2931 break;
2932 case GOT_STATUS_ADD: {
2933 struct got_object_id *id;
2934 FILE *blob1_f;
2935 off_t blob1_size;
2937 * Delete the added file only if its content already
2938 * exists in the repository.
2940 err = got_object_blob_file_create(&id, &blob1_f,
2941 &blob1_size, path1);
2942 if (err)
2943 goto done;
2944 if (got_object_id_cmp(id, id1) == 0) {
2945 err = (*a->progress_cb)(a->progress_arg,
2946 GOT_STATUS_DELETE, path1);
2947 if (err)
2948 goto done;
2949 err = remove_ondisk_file(a->worktree->root_path,
2950 path1);
2951 if (err)
2952 goto done;
2953 if (ie)
2954 got_fileindex_entry_remove(a->fileindex,
2955 ie);
2956 } else {
2957 err = (*a->progress_cb)(a->progress_arg,
2958 GOT_STATUS_CANNOT_DELETE, path1);
2960 if (fclose(blob1_f) == EOF && err == NULL)
2961 err = got_error_from_errno("fclose");
2962 free(id);
2963 if (err)
2964 goto done;
2965 break;
2967 case GOT_STATUS_MODIFY:
2968 case GOT_STATUS_CONFLICT:
2969 err = (*a->progress_cb)(a->progress_arg,
2970 GOT_STATUS_CANNOT_DELETE, path1);
2971 if (err)
2972 goto done;
2973 break;
2974 case GOT_STATUS_OBSTRUCTED:
2975 err = (*a->progress_cb)(a->progress_arg, status, path1);
2976 if (err)
2977 goto done;
2978 break;
2979 default:
2980 break;
2982 } else if (blob2) {
2983 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2984 path2) == -1)
2985 return got_error_from_errno("asprintf");
2986 ie = got_fileindex_entry_get(a->fileindex, path2,
2987 strlen(path2));
2988 if (ie) {
2989 err = get_file_status(&status, &sb, ie, ondisk_path,
2990 -1, NULL, repo);
2991 if (err)
2992 goto done;
2993 if (status != GOT_STATUS_NO_CHANGE &&
2994 status != GOT_STATUS_MODIFY &&
2995 status != GOT_STATUS_CONFLICT &&
2996 status != GOT_STATUS_ADD) {
2997 err = (*a->progress_cb)(a->progress_arg,
2998 status, path2);
2999 goto done;
3001 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3002 char *link_target2;
3003 err = got_object_blob_read_to_str(&link_target2,
3004 blob2);
3005 if (err)
3006 goto done;
3007 err = merge_symlink(a->worktree, NULL,
3008 ondisk_path, path2, a->label_orig,
3009 link_target2, a->commit_id2, repo,
3010 a->progress_cb, a->progress_arg);
3011 free(link_target2);
3012 } else if (S_ISREG(sb.st_mode)) {
3013 err = merge_blob(&local_changes_subsumed,
3014 a->worktree, NULL, ondisk_path, path2,
3015 sb.st_mode, a->label_orig, blob2,
3016 a->commit_id2, repo, a->progress_cb,
3017 a->progress_arg);
3018 } else {
3019 err = got_error_path(ondisk_path,
3020 GOT_ERR_FILE_OBSTRUCTED);
3022 if (err)
3023 goto done;
3024 if (status == GOT_STATUS_DELETE) {
3025 err = got_fileindex_entry_update(ie,
3026 a->worktree->root_fd, path2, blob2->id.sha1,
3027 a->worktree->base_commit_id->sha1, 0);
3028 if (err)
3029 goto done;
3031 } else {
3032 int is_bad_symlink = 0;
3033 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3034 if (S_ISLNK(mode2)) {
3035 err = install_symlink(&is_bad_symlink,
3036 a->worktree, ondisk_path, path2, blob2, 0,
3037 0, 1, a->allow_bad_symlinks, repo,
3038 a->progress_cb, a->progress_arg);
3039 } else {
3040 err = install_blob(a->worktree, ondisk_path, path2,
3041 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3042 a->progress_cb, a->progress_arg);
3044 if (err)
3045 goto done;
3046 err = got_fileindex_entry_alloc(&ie, path2);
3047 if (err)
3048 goto done;
3049 err = got_fileindex_entry_update(ie,
3050 a->worktree->root_fd, path2, NULL, NULL, 1);
3051 if (err) {
3052 got_fileindex_entry_free(ie);
3053 goto done;
3055 err = got_fileindex_entry_add(a->fileindex, ie);
3056 if (err) {
3057 got_fileindex_entry_free(ie);
3058 goto done;
3060 if (is_bad_symlink) {
3061 got_fileindex_entry_filetype_set(ie,
3062 GOT_FILEIDX_MODE_BAD_SYMLINK);
3066 done:
3067 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3068 err = got_error_from_errno("fclose");
3069 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3070 err = got_error_from_errno("fclose");
3071 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3072 err = got_error_from_errno("fclose");
3073 free(id_str);
3074 free(label_deriv2);
3075 free(ondisk_path);
3076 return err;
3079 static const struct got_error *
3080 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3082 struct got_worktree *worktree = arg;
3084 /* Reject merges into a work tree with mixed base commits. */
3085 if (got_fileindex_entry_has_commit(ie) &&
3086 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3087 SHA1_DIGEST_LENGTH) != 0)
3088 return got_error(GOT_ERR_MIXED_COMMITS);
3090 return NULL;
3093 struct check_merge_conflicts_arg {
3094 struct got_worktree *worktree;
3095 struct got_fileindex *fileindex;
3096 struct got_repository *repo;
3099 static const struct got_error *
3100 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3101 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3102 struct got_object_id *id1, struct got_object_id *id2,
3103 const char *path1, const char *path2,
3104 mode_t mode1, mode_t mode2, struct got_repository *repo)
3106 const struct got_error *err = NULL;
3107 struct check_merge_conflicts_arg *a = arg;
3108 unsigned char status;
3109 struct stat sb;
3110 struct got_fileindex_entry *ie;
3111 const char *path = path2 ? path2 : path1;
3112 struct got_object_id *id = id2 ? id2 : id1;
3113 char *ondisk_path;
3115 if (id == NULL)
3116 return NULL;
3118 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3119 if (ie == NULL)
3120 return NULL;
3122 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3123 == -1)
3124 return got_error_from_errno("asprintf");
3126 /* Reject merges into a work tree with conflicted files. */
3127 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3128 free(ondisk_path);
3129 if (err)
3130 return err;
3131 if (status == GOT_STATUS_CONFLICT)
3132 return got_error(GOT_ERR_CONFLICTS);
3134 return NULL;
3137 static const struct got_error *
3138 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3139 const char *fileindex_path, struct got_object_id *commit_id1,
3140 struct got_object_id *commit_id2, struct got_repository *repo,
3141 got_worktree_checkout_cb progress_cb, void *progress_arg,
3142 got_cancel_cb cancel_cb, void *cancel_arg)
3144 const struct got_error *err = NULL, *sync_err;
3145 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3146 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3147 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3148 struct check_merge_conflicts_arg cmc_arg;
3149 struct merge_file_cb_arg arg;
3150 char *label_orig = NULL;
3151 FILE *f1 = NULL, *f2 = NULL;
3152 int fd1 = -1, fd2 = -1;
3154 if (commit_id1) {
3155 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3156 if (err)
3157 goto done;
3158 err = got_object_id_by_path(&tree_id1, repo, commit1,
3159 worktree->path_prefix);
3160 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3161 goto done;
3163 if (tree_id1) {
3164 char *id_str;
3166 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3167 if (err)
3168 goto done;
3170 err = got_object_id_str(&id_str, commit_id1);
3171 if (err)
3172 goto done;
3174 if (asprintf(&label_orig, "%s: commit %s",
3175 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3176 err = got_error_from_errno("asprintf");
3177 free(id_str);
3178 goto done;
3180 free(id_str);
3182 f1 = got_opentemp();
3183 if (f1 == NULL) {
3184 err = got_error_from_errno("got_opentemp");
3185 goto done;
3189 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3190 if (err)
3191 goto done;
3193 err = got_object_id_by_path(&tree_id2, repo, commit2,
3194 worktree->path_prefix);
3195 if (err)
3196 goto done;
3198 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3199 if (err)
3200 goto done;
3202 f2 = got_opentemp();
3203 if (f2 == NULL) {
3204 err = got_error_from_errno("got_opentemp");
3205 goto done;
3208 fd1 = got_opentempfd();
3209 if (fd1 == -1) {
3210 err = got_error_from_errno("got_opentempfd");
3211 goto done;
3214 fd2 = got_opentempfd();
3215 if (fd2 == -1) {
3216 err = got_error_from_errno("got_opentempfd");
3217 goto done;
3220 cmc_arg.worktree = worktree;
3221 cmc_arg.fileindex = fileindex;
3222 cmc_arg.repo = repo;
3223 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3224 check_merge_conflicts, &cmc_arg, 0);
3225 if (err)
3226 goto done;
3228 arg.worktree = worktree;
3229 arg.fileindex = fileindex;
3230 arg.progress_cb = progress_cb;
3231 arg.progress_arg = progress_arg;
3232 arg.cancel_cb = cancel_cb;
3233 arg.cancel_arg = cancel_arg;
3234 arg.label_orig = label_orig;
3235 arg.commit_id2 = commit_id2;
3236 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3237 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3238 merge_file_cb, &arg, 1);
3239 sync_err = sync_fileindex(fileindex, fileindex_path);
3240 if (sync_err && err == NULL)
3241 err = sync_err;
3242 done:
3243 if (commit1)
3244 got_object_commit_close(commit1);
3245 if (commit2)
3246 got_object_commit_close(commit2);
3247 if (tree1)
3248 got_object_tree_close(tree1);
3249 if (tree2)
3250 got_object_tree_close(tree2);
3251 if (f1 && fclose(f1) == EOF && err == NULL)
3252 err = got_error_from_errno("fclose");
3253 if (f2 && fclose(f2) == EOF && err == NULL)
3254 err = got_error_from_errno("fclose");
3255 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3256 err = got_error_from_errno("close");
3257 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3258 err = got_error_from_errno("close");
3259 free(label_orig);
3260 return err;
3263 const struct got_error *
3264 got_worktree_merge_files(struct got_worktree *worktree,
3265 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3266 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3267 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3269 const struct got_error *err, *unlockerr;
3270 char *fileindex_path = NULL;
3271 struct got_fileindex *fileindex = NULL;
3273 err = lock_worktree(worktree, LOCK_EX);
3274 if (err)
3275 return err;
3277 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3278 if (err)
3279 goto done;
3281 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3282 worktree);
3283 if (err)
3284 goto done;
3286 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3287 commit_id2, repo, progress_cb, progress_arg,
3288 cancel_cb, cancel_arg);
3289 done:
3290 if (fileindex)
3291 got_fileindex_free(fileindex);
3292 free(fileindex_path);
3293 unlockerr = lock_worktree(worktree, LOCK_SH);
3294 if (unlockerr && err == NULL)
3295 err = unlockerr;
3296 return err;
3299 struct diff_dir_cb_arg {
3300 struct got_fileindex *fileindex;
3301 struct got_worktree *worktree;
3302 const char *status_path;
3303 size_t status_path_len;
3304 struct got_repository *repo;
3305 got_worktree_status_cb status_cb;
3306 void *status_arg;
3307 got_cancel_cb cancel_cb;
3308 void *cancel_arg;
3309 /* A pathlist containing per-directory pathlists of ignore patterns. */
3310 struct got_pathlist_head *ignores;
3311 int report_unchanged;
3312 int no_ignores;
3315 static const struct got_error *
3316 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3317 int dirfd, const char *de_name,
3318 got_worktree_status_cb status_cb, void *status_arg,
3319 struct got_repository *repo, int report_unchanged)
3321 const struct got_error *err = NULL;
3322 unsigned char status = GOT_STATUS_NO_CHANGE;
3323 unsigned char staged_status = get_staged_status(ie);
3324 struct stat sb;
3325 struct got_object_id blob_id, commit_id, staged_blob_id;
3326 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3327 struct got_object_id *staged_blob_idp = NULL;
3329 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3330 if (err)
3331 return err;
3333 if (status == GOT_STATUS_NO_CHANGE &&
3334 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3335 return NULL;
3337 if (got_fileindex_entry_has_blob(ie)) {
3338 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3339 blob_idp = &blob_id;
3341 if (got_fileindex_entry_has_commit(ie)) {
3342 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3343 commit_idp = &commit_id;
3345 if (staged_status == GOT_STATUS_ADD ||
3346 staged_status == GOT_STATUS_MODIFY) {
3347 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3348 SHA1_DIGEST_LENGTH);
3349 staged_blob_idp = &staged_blob_id;
3352 return (*status_cb)(status_arg, status, staged_status,
3353 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3356 static const struct got_error *
3357 status_old_new(void *arg, struct got_fileindex_entry *ie,
3358 struct dirent *de, const char *parent_path, int dirfd)
3360 const struct got_error *err = NULL;
3361 struct diff_dir_cb_arg *a = arg;
3362 char *abspath;
3364 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3365 return got_error(GOT_ERR_CANCELLED);
3367 if (got_path_cmp(parent_path, a->status_path,
3368 strlen(parent_path), a->status_path_len) != 0 &&
3369 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3370 return NULL;
3372 if (parent_path[0]) {
3373 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3374 parent_path, de->d_name) == -1)
3375 return got_error_from_errno("asprintf");
3376 } else {
3377 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3378 de->d_name) == -1)
3379 return got_error_from_errno("asprintf");
3382 err = report_file_status(ie, abspath, dirfd, de->d_name,
3383 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3384 free(abspath);
3385 return err;
3388 static const struct got_error *
3389 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3391 struct diff_dir_cb_arg *a = arg;
3392 struct got_object_id blob_id, commit_id;
3393 unsigned char status;
3395 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3396 return got_error(GOT_ERR_CANCELLED);
3398 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3399 return NULL;
3401 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3402 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3403 if (got_fileindex_entry_has_file_on_disk(ie))
3404 status = GOT_STATUS_MISSING;
3405 else
3406 status = GOT_STATUS_DELETE;
3407 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3408 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3411 static void
3412 free_ignorelist(struct got_pathlist_head *ignorelist)
3414 struct got_pathlist_entry *pe;
3416 TAILQ_FOREACH(pe, ignorelist, entry)
3417 free((char *)pe->path);
3418 got_pathlist_free(ignorelist);
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;
3428 free_ignorelist(ignorelist);
3429 free((char *)pe->path);
3431 got_pathlist_free(ignores);
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 free_ignorelist(ignorelist);
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(outfile, 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.sha1, ie->staged_blob_sha1,
4788 SHA1_DIGEST_LENGTH);
4789 } else
4790 memcpy(id.sha1, ie->blob_sha1,
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.sha1,
4855 a->worktree->base_commit_id->sha1, 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 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, 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);
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->sha1,
5821 new_base_commit_id->sha1,
5822 !have_staged_files);
5823 } else
5824 err = got_fileindex_entry_update(ie,
5825 worktree->root_fd, relpath,
5826 ct->blob_id->sha1,
5827 new_base_commit_id->sha1,
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->sha1,
5835 new_base_commit_id->sha1, 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;
6243 free_commitable(ct);
6245 got_pathlist_free(&commitable_paths);
6246 if (diff_path && unlink(diff_path) == -1 && err == NULL)
6247 err = got_error_from_errno2("unlink", diff_path);
6248 free(diff_path);
6249 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6250 err == NULL)
6251 err = got_error_from_errno("fclose");
6252 return err;
6255 const char *
6256 got_commitable_get_path(struct got_commitable *ct)
6258 return ct->path;
6261 unsigned int
6262 got_commitable_get_status(struct got_commitable *ct)
6264 return ct->status;
6267 struct check_rebase_ok_arg {
6268 struct got_worktree *worktree;
6269 struct got_repository *repo;
6272 static const struct got_error *
6273 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6275 const struct got_error *err = NULL;
6276 struct check_rebase_ok_arg *a = arg;
6277 unsigned char status;
6278 struct stat sb;
6279 char *ondisk_path;
6281 /* Reject rebase of a work tree with mixed base commits. */
6282 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6283 SHA1_DIGEST_LENGTH))
6284 return got_error(GOT_ERR_MIXED_COMMITS);
6286 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6287 == -1)
6288 return got_error_from_errno("asprintf");
6290 /* Reject rebase of a work tree with modified or staged files. */
6291 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6292 free(ondisk_path);
6293 if (err)
6294 return err;
6296 if (status != GOT_STATUS_NO_CHANGE)
6297 return got_error(GOT_ERR_MODIFIED);
6298 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6299 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6301 return NULL;
6304 const struct got_error *
6305 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6306 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6307 struct got_worktree *worktree, struct got_reference *branch,
6308 struct got_repository *repo)
6310 const struct got_error *err = NULL;
6311 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6312 char *branch_ref_name = NULL;
6313 char *fileindex_path = NULL;
6314 struct check_rebase_ok_arg ok_arg;
6315 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6316 struct got_object_id *wt_branch_tip = NULL;
6318 *new_base_branch_ref = NULL;
6319 *tmp_branch = NULL;
6320 *fileindex = NULL;
6322 err = lock_worktree(worktree, LOCK_EX);
6323 if (err)
6324 return err;
6326 err = open_fileindex(fileindex, &fileindex_path, worktree);
6327 if (err)
6328 goto done;
6330 ok_arg.worktree = worktree;
6331 ok_arg.repo = repo;
6332 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6333 &ok_arg);
6334 if (err)
6335 goto done;
6337 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6338 if (err)
6339 goto done;
6341 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6342 if (err)
6343 goto done;
6345 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6346 if (err)
6347 goto done;
6349 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6350 0);
6351 if (err)
6352 goto done;
6354 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6355 if (err)
6356 goto done;
6357 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6358 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6359 goto done;
6362 err = got_ref_alloc_symref(new_base_branch_ref,
6363 new_base_branch_ref_name, wt_branch);
6364 if (err)
6365 goto done;
6366 err = got_ref_write(*new_base_branch_ref, repo);
6367 if (err)
6368 goto done;
6370 /* TODO Lock original branch's ref while rebasing? */
6372 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6373 if (err)
6374 goto done;
6376 err = got_ref_write(branch_ref, repo);
6377 if (err)
6378 goto done;
6380 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6381 worktree->base_commit_id);
6382 if (err)
6383 goto done;
6384 err = got_ref_write(*tmp_branch, repo);
6385 if (err)
6386 goto done;
6388 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6389 if (err)
6390 goto done;
6391 done:
6392 free(fileindex_path);
6393 free(tmp_branch_name);
6394 free(new_base_branch_ref_name);
6395 free(branch_ref_name);
6396 if (branch_ref)
6397 got_ref_close(branch_ref);
6398 if (wt_branch)
6399 got_ref_close(wt_branch);
6400 free(wt_branch_tip);
6401 if (err) {
6402 if (*new_base_branch_ref) {
6403 got_ref_close(*new_base_branch_ref);
6404 *new_base_branch_ref = NULL;
6406 if (*tmp_branch) {
6407 got_ref_close(*tmp_branch);
6408 *tmp_branch = NULL;
6410 if (*fileindex) {
6411 got_fileindex_free(*fileindex);
6412 *fileindex = NULL;
6414 lock_worktree(worktree, LOCK_SH);
6416 return err;
6419 const struct got_error *
6420 got_worktree_rebase_continue(struct got_object_id **commit_id,
6421 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6422 struct got_reference **branch, struct got_fileindex **fileindex,
6423 struct got_worktree *worktree, struct got_repository *repo)
6425 const struct got_error *err;
6426 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6427 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6428 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6429 char *fileindex_path = NULL;
6430 int have_staged_files = 0;
6432 *commit_id = NULL;
6433 *new_base_branch = NULL;
6434 *tmp_branch = NULL;
6435 *branch = NULL;
6436 *fileindex = NULL;
6438 err = lock_worktree(worktree, LOCK_EX);
6439 if (err)
6440 return err;
6442 err = open_fileindex(fileindex, &fileindex_path, worktree);
6443 if (err)
6444 goto done;
6446 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6447 &have_staged_files);
6448 if (err && err->code != GOT_ERR_CANCELLED)
6449 goto done;
6450 if (have_staged_files) {
6451 err = got_error(GOT_ERR_STAGED_PATHS);
6452 goto done;
6455 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6456 if (err)
6457 goto done;
6459 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6460 if (err)
6461 goto done;
6463 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6464 if (err)
6465 goto done;
6467 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6468 if (err)
6469 goto done;
6471 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6472 if (err)
6473 goto done;
6475 err = got_ref_open(branch, repo,
6476 got_ref_get_symref_target(branch_ref), 0);
6477 if (err)
6478 goto done;
6480 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6481 if (err)
6482 goto done;
6484 err = got_ref_resolve(commit_id, repo, commit_ref);
6485 if (err)
6486 goto done;
6488 err = got_ref_open(new_base_branch, repo,
6489 new_base_branch_ref_name, 0);
6490 if (err)
6491 goto done;
6493 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6494 if (err)
6495 goto done;
6496 done:
6497 free(commit_ref_name);
6498 free(branch_ref_name);
6499 free(fileindex_path);
6500 if (commit_ref)
6501 got_ref_close(commit_ref);
6502 if (branch_ref)
6503 got_ref_close(branch_ref);
6504 if (err) {
6505 free(*commit_id);
6506 *commit_id = NULL;
6507 if (*tmp_branch) {
6508 got_ref_close(*tmp_branch);
6509 *tmp_branch = NULL;
6511 if (*new_base_branch) {
6512 got_ref_close(*new_base_branch);
6513 *new_base_branch = NULL;
6515 if (*branch) {
6516 got_ref_close(*branch);
6517 *branch = NULL;
6519 if (*fileindex) {
6520 got_fileindex_free(*fileindex);
6521 *fileindex = NULL;
6523 lock_worktree(worktree, LOCK_SH);
6525 return err;
6528 const struct got_error *
6529 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6531 const struct got_error *err;
6532 char *tmp_branch_name = NULL;
6534 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6535 if (err)
6536 return err;
6538 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6539 free(tmp_branch_name);
6540 return NULL;
6543 static const struct got_error *
6544 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6545 const char *diff_path, char **logmsg, void *arg)
6547 *logmsg = arg;
6548 return NULL;
6551 static const struct got_error *
6552 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6553 const char *path, struct got_object_id *blob_id,
6554 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6555 int dirfd, const char *de_name)
6557 return NULL;
6560 struct collect_merged_paths_arg {
6561 got_worktree_checkout_cb progress_cb;
6562 void *progress_arg;
6563 struct got_pathlist_head *merged_paths;
6566 static const struct got_error *
6567 collect_merged_paths(void *arg, unsigned char status, const char *path)
6569 const struct got_error *err;
6570 struct collect_merged_paths_arg *a = arg;
6571 char *p;
6572 struct got_pathlist_entry *new;
6574 err = (*a->progress_cb)(a->progress_arg, status, path);
6575 if (err)
6576 return err;
6578 if (status != GOT_STATUS_MERGE &&
6579 status != GOT_STATUS_ADD &&
6580 status != GOT_STATUS_DELETE &&
6581 status != GOT_STATUS_CONFLICT)
6582 return NULL;
6584 p = strdup(path);
6585 if (p == NULL)
6586 return got_error_from_errno("strdup");
6588 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6589 if (err || new == NULL)
6590 free(p);
6591 return err;
6594 void
6595 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6597 struct got_pathlist_entry *pe;
6599 TAILQ_FOREACH(pe, merged_paths, entry)
6600 free((char *)pe->path);
6602 got_pathlist_free(merged_paths);
6605 static const struct got_error *
6606 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6607 int is_rebase, struct got_repository *repo)
6609 const struct got_error *err;
6610 struct got_reference *commit_ref = NULL;
6612 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6613 if (err) {
6614 if (err->code != GOT_ERR_NOT_REF)
6615 goto done;
6616 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6617 if (err)
6618 goto done;
6619 err = got_ref_write(commit_ref, repo);
6620 if (err)
6621 goto done;
6622 } else if (is_rebase) {
6623 struct got_object_id *stored_id;
6624 int cmp;
6626 err = got_ref_resolve(&stored_id, repo, commit_ref);
6627 if (err)
6628 goto done;
6629 cmp = got_object_id_cmp(commit_id, stored_id);
6630 free(stored_id);
6631 if (cmp != 0) {
6632 err = got_error(GOT_ERR_REBASE_COMMITID);
6633 goto done;
6636 done:
6637 if (commit_ref)
6638 got_ref_close(commit_ref);
6639 return err;
6642 static const struct got_error *
6643 rebase_merge_files(struct got_pathlist_head *merged_paths,
6644 const char *commit_ref_name, struct got_worktree *worktree,
6645 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6646 struct got_object_id *commit_id, struct got_repository *repo,
6647 got_worktree_checkout_cb progress_cb, void *progress_arg,
6648 got_cancel_cb cancel_cb, void *cancel_arg)
6650 const struct got_error *err;
6651 struct got_reference *commit_ref = NULL;
6652 struct collect_merged_paths_arg cmp_arg;
6653 char *fileindex_path;
6655 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6657 err = get_fileindex_path(&fileindex_path, worktree);
6658 if (err)
6659 return err;
6661 cmp_arg.progress_cb = progress_cb;
6662 cmp_arg.progress_arg = progress_arg;
6663 cmp_arg.merged_paths = merged_paths;
6664 err = merge_files(worktree, fileindex, fileindex_path,
6665 parent_commit_id, commit_id, repo, collect_merged_paths,
6666 &cmp_arg, cancel_cb, cancel_arg);
6667 if (commit_ref)
6668 got_ref_close(commit_ref);
6669 return err;
6672 const struct got_error *
6673 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6674 struct got_worktree *worktree, struct got_fileindex *fileindex,
6675 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6676 struct got_repository *repo,
6677 got_worktree_checkout_cb progress_cb, void *progress_arg,
6678 got_cancel_cb cancel_cb, void *cancel_arg)
6680 const struct got_error *err;
6681 char *commit_ref_name;
6683 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6684 if (err)
6685 return err;
6687 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6688 if (err)
6689 goto done;
6691 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6692 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6693 progress_arg, cancel_cb, cancel_arg);
6694 done:
6695 free(commit_ref_name);
6696 return err;
6699 const struct got_error *
6700 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6701 struct got_worktree *worktree, struct got_fileindex *fileindex,
6702 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6703 struct got_repository *repo,
6704 got_worktree_checkout_cb progress_cb, void *progress_arg,
6705 got_cancel_cb cancel_cb, void *cancel_arg)
6707 const struct got_error *err;
6708 char *commit_ref_name;
6710 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6711 if (err)
6712 return err;
6714 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6715 if (err)
6716 goto done;
6718 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6719 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6720 progress_arg, cancel_cb, cancel_arg);
6721 done:
6722 free(commit_ref_name);
6723 return err;
6726 static const struct got_error *
6727 rebase_commit(struct got_object_id **new_commit_id,
6728 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6729 struct got_worktree *worktree, struct got_fileindex *fileindex,
6730 struct got_reference *tmp_branch, const char *committer,
6731 struct got_commit_object *orig_commit, const char *new_logmsg,
6732 struct got_repository *repo)
6734 const struct got_error *err, *sync_err;
6735 struct got_pathlist_head commitable_paths;
6736 struct collect_commitables_arg cc_arg;
6737 char *fileindex_path = NULL;
6738 struct got_reference *head_ref = NULL;
6739 struct got_object_id *head_commit_id = NULL;
6740 char *logmsg = NULL;
6742 memset(&cc_arg, 0, sizeof(cc_arg));
6743 TAILQ_INIT(&commitable_paths);
6744 *new_commit_id = NULL;
6746 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6748 err = get_fileindex_path(&fileindex_path, worktree);
6749 if (err)
6750 return err;
6752 cc_arg.commitable_paths = &commitable_paths;
6753 cc_arg.worktree = worktree;
6754 cc_arg.repo = repo;
6755 cc_arg.have_staged_files = 0;
6757 * If possible get the status of individual files directly to
6758 * avoid crawling the entire work tree once per rebased commit.
6760 * Ideally, merged_paths would contain a list of commitables
6761 * we could use so we could skip worktree_status() entirely.
6762 * However, we would then need carefully keep track of cumulative
6763 * effects of operations such as file additions and deletions
6764 * in 'got histedit -f' (folding multiple commits into one),
6765 * and this extra complexity is not really worth it.
6767 if (merged_paths) {
6768 struct got_pathlist_entry *pe;
6769 TAILQ_FOREACH(pe, merged_paths, entry) {
6770 err = worktree_status(worktree, pe->path, fileindex,
6771 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6772 0);
6773 if (err)
6774 goto done;
6776 } else {
6777 err = worktree_status(worktree, "", fileindex, repo,
6778 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6779 if (err)
6780 goto done;
6783 if (TAILQ_EMPTY(&commitable_paths)) {
6784 /* No-op change; commit will be elided. */
6785 err = got_ref_delete(commit_ref, repo);
6786 if (err)
6787 goto done;
6788 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6789 goto done;
6792 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6793 if (err)
6794 goto done;
6796 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6797 if (err)
6798 goto done;
6800 if (new_logmsg) {
6801 logmsg = strdup(new_logmsg);
6802 if (logmsg == NULL) {
6803 err = got_error_from_errno("strdup");
6804 goto done;
6806 } else {
6807 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6808 if (err)
6809 goto done;
6812 /* NB: commit_worktree will call free(logmsg) */
6813 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6814 NULL, worktree, got_object_commit_get_author(orig_commit),
6815 committer ? committer :
6816 got_object_commit_get_committer(orig_commit), NULL,
6817 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6818 if (err)
6819 goto done;
6821 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6822 if (err)
6823 goto done;
6825 err = got_ref_delete(commit_ref, repo);
6826 if (err)
6827 goto done;
6829 err = update_fileindex_after_commit(worktree, &commitable_paths,
6830 *new_commit_id, fileindex, 0);
6831 sync_err = sync_fileindex(fileindex, fileindex_path);
6832 if (sync_err && err == NULL)
6833 err = sync_err;
6834 done:
6835 free(fileindex_path);
6836 free(head_commit_id);
6837 if (head_ref)
6838 got_ref_close(head_ref);
6839 if (err) {
6840 free(*new_commit_id);
6841 *new_commit_id = NULL;
6843 return err;
6846 const struct got_error *
6847 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6848 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6849 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6850 const char *committer, struct got_commit_object *orig_commit,
6851 struct got_object_id *orig_commit_id, struct got_repository *repo)
6853 const struct got_error *err;
6854 char *commit_ref_name;
6855 struct got_reference *commit_ref = NULL;
6856 struct got_object_id *commit_id = NULL;
6858 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6859 if (err)
6860 return err;
6862 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6863 if (err)
6864 goto done;
6865 err = got_ref_resolve(&commit_id, repo, commit_ref);
6866 if (err)
6867 goto done;
6868 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6869 err = got_error(GOT_ERR_REBASE_COMMITID);
6870 goto done;
6873 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6874 worktree, fileindex, tmp_branch, committer, orig_commit,
6875 NULL, repo);
6876 done:
6877 if (commit_ref)
6878 got_ref_close(commit_ref);
6879 free(commit_ref_name);
6880 free(commit_id);
6881 return err;
6884 const struct got_error *
6885 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6886 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6887 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6888 const char *committer, struct got_commit_object *orig_commit,
6889 struct got_object_id *orig_commit_id, const char *new_logmsg,
6890 struct got_repository *repo)
6892 const struct got_error *err;
6893 char *commit_ref_name;
6894 struct got_reference *commit_ref = NULL;
6896 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6897 if (err)
6898 return err;
6900 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6901 if (err)
6902 goto done;
6904 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6905 worktree, fileindex, tmp_branch, committer, orig_commit,
6906 new_logmsg, repo);
6907 done:
6908 if (commit_ref)
6909 got_ref_close(commit_ref);
6910 free(commit_ref_name);
6911 return err;
6914 const struct got_error *
6915 got_worktree_rebase_postpone(struct got_worktree *worktree,
6916 struct got_fileindex *fileindex)
6918 if (fileindex)
6919 got_fileindex_free(fileindex);
6920 return lock_worktree(worktree, LOCK_SH);
6923 static const struct got_error *
6924 delete_ref(const char *name, struct got_repository *repo)
6926 const struct got_error *err;
6927 struct got_reference *ref;
6929 err = got_ref_open(&ref, repo, name, 0);
6930 if (err) {
6931 if (err->code == GOT_ERR_NOT_REF)
6932 return NULL;
6933 return err;
6936 err = got_ref_delete(ref, repo);
6937 got_ref_close(ref);
6938 return err;
6941 static const struct got_error *
6942 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6944 const struct got_error *err;
6945 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6946 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6948 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6949 if (err)
6950 goto done;
6951 err = delete_ref(tmp_branch_name, repo);
6952 if (err)
6953 goto done;
6955 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6956 if (err)
6957 goto done;
6958 err = delete_ref(new_base_branch_ref_name, repo);
6959 if (err)
6960 goto done;
6962 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6963 if (err)
6964 goto done;
6965 err = delete_ref(branch_ref_name, repo);
6966 if (err)
6967 goto done;
6969 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6970 if (err)
6971 goto done;
6972 err = delete_ref(commit_ref_name, repo);
6973 if (err)
6974 goto done;
6976 done:
6977 free(tmp_branch_name);
6978 free(new_base_branch_ref_name);
6979 free(branch_ref_name);
6980 free(commit_ref_name);
6981 return err;
6984 static const struct got_error *
6985 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6986 struct got_object_id *new_commit_id, struct got_repository *repo)
6988 const struct got_error *err;
6989 struct got_reference *ref = NULL;
6990 struct got_object_id *old_commit_id = NULL;
6991 const char *branch_name = NULL;
6992 char *new_id_str = NULL;
6993 char *refname = NULL;
6995 branch_name = got_ref_get_name(branch);
6996 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6997 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6998 branch_name += 11;
7000 err = got_object_id_str(&new_id_str, new_commit_id);
7001 if (err)
7002 return err;
7004 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7005 new_id_str) == -1) {
7006 err = got_error_from_errno("asprintf");
7007 goto done;
7010 err = got_ref_resolve(&old_commit_id, repo, branch);
7011 if (err)
7012 goto done;
7014 err = got_ref_alloc(&ref, refname, old_commit_id);
7015 if (err)
7016 goto done;
7018 err = got_ref_write(ref, repo);
7019 done:
7020 free(new_id_str);
7021 free(refname);
7022 free(old_commit_id);
7023 if (ref)
7024 got_ref_close(ref);
7025 return err;
7028 const struct got_error *
7029 got_worktree_rebase_complete(struct got_worktree *worktree,
7030 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
7031 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
7032 struct got_repository *repo, int create_backup)
7034 const struct got_error *err, *unlockerr, *sync_err;
7035 struct got_object_id *new_head_commit_id = NULL;
7036 char *fileindex_path = NULL;
7038 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7039 if (err)
7040 return err;
7042 if (create_backup) {
7043 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7044 rebased_branch, new_head_commit_id, repo);
7045 if (err)
7046 goto done;
7049 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7050 if (err)
7051 goto done;
7053 err = got_ref_write(rebased_branch, repo);
7054 if (err)
7055 goto done;
7057 err = got_worktree_set_head_ref(worktree, rebased_branch);
7058 if (err)
7059 goto done;
7061 err = delete_rebase_refs(worktree, repo);
7062 if (err)
7063 goto done;
7065 err = get_fileindex_path(&fileindex_path, worktree);
7066 if (err)
7067 goto done;
7068 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7069 sync_err = sync_fileindex(fileindex, fileindex_path);
7070 if (sync_err && err == NULL)
7071 err = sync_err;
7072 done:
7073 got_fileindex_free(fileindex);
7074 free(fileindex_path);
7075 free(new_head_commit_id);
7076 unlockerr = lock_worktree(worktree, LOCK_SH);
7077 if (unlockerr && err == NULL)
7078 err = unlockerr;
7079 return err;
7082 const struct got_error *
7083 got_worktree_rebase_abort(struct got_worktree *worktree,
7084 struct got_fileindex *fileindex, struct got_repository *repo,
7085 struct got_reference *new_base_branch,
7086 got_worktree_checkout_cb progress_cb, void *progress_arg)
7088 const struct got_error *err, *unlockerr, *sync_err;
7089 struct got_reference *resolved = NULL;
7090 struct got_object_id *commit_id = NULL;
7091 struct got_commit_object *commit = NULL;
7092 char *fileindex_path = NULL;
7093 struct revert_file_args rfa;
7094 struct got_object_id *tree_id = NULL;
7096 err = lock_worktree(worktree, LOCK_EX);
7097 if (err)
7098 return err;
7100 err = got_object_open_as_commit(&commit, repo,
7101 worktree->base_commit_id);
7102 if (err)
7103 goto done;
7105 err = got_ref_open(&resolved, repo,
7106 got_ref_get_symref_target(new_base_branch), 0);
7107 if (err)
7108 goto done;
7110 err = got_worktree_set_head_ref(worktree, resolved);
7111 if (err)
7112 goto done;
7115 * XXX commits to the base branch could have happened while
7116 * we were busy rebasing; should we store the original commit ID
7117 * when rebase begins and read it back here?
7119 err = got_ref_resolve(&commit_id, repo, resolved);
7120 if (err)
7121 goto done;
7123 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7124 if (err)
7125 goto done;
7127 err = got_object_id_by_path(&tree_id, repo, commit,
7128 worktree->path_prefix);
7129 if (err)
7130 goto done;
7132 err = delete_rebase_refs(worktree, repo);
7133 if (err)
7134 goto done;
7136 err = get_fileindex_path(&fileindex_path, worktree);
7137 if (err)
7138 goto done;
7140 rfa.worktree = worktree;
7141 rfa.fileindex = fileindex;
7142 rfa.progress_cb = progress_cb;
7143 rfa.progress_arg = progress_arg;
7144 rfa.patch_cb = NULL;
7145 rfa.patch_arg = NULL;
7146 rfa.repo = repo;
7147 rfa.unlink_added_files = 0;
7148 err = worktree_status(worktree, "", fileindex, repo,
7149 revert_file, &rfa, NULL, NULL, 1, 0);
7150 if (err)
7151 goto sync;
7153 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7154 repo, progress_cb, progress_arg, NULL, NULL);
7155 sync:
7156 sync_err = sync_fileindex(fileindex, fileindex_path);
7157 if (sync_err && err == NULL)
7158 err = sync_err;
7159 done:
7160 got_ref_close(resolved);
7161 free(tree_id);
7162 free(commit_id);
7163 if (commit)
7164 got_object_commit_close(commit);
7165 if (fileindex)
7166 got_fileindex_free(fileindex);
7167 free(fileindex_path);
7169 unlockerr = lock_worktree(worktree, LOCK_SH);
7170 if (unlockerr && err == NULL)
7171 err = unlockerr;
7172 return err;
7175 const struct got_error *
7176 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7177 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7178 struct got_fileindex **fileindex, struct got_worktree *worktree,
7179 struct got_repository *repo)
7181 const struct got_error *err = NULL;
7182 char *tmp_branch_name = NULL;
7183 char *branch_ref_name = NULL;
7184 char *base_commit_ref_name = NULL;
7185 char *fileindex_path = NULL;
7186 struct check_rebase_ok_arg ok_arg;
7187 struct got_reference *wt_branch = NULL;
7188 struct got_reference *base_commit_ref = NULL;
7190 *tmp_branch = NULL;
7191 *branch_ref = NULL;
7192 *base_commit_id = NULL;
7193 *fileindex = NULL;
7195 err = lock_worktree(worktree, LOCK_EX);
7196 if (err)
7197 return err;
7199 err = open_fileindex(fileindex, &fileindex_path, worktree);
7200 if (err)
7201 goto done;
7203 ok_arg.worktree = worktree;
7204 ok_arg.repo = repo;
7205 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7206 &ok_arg);
7207 if (err)
7208 goto done;
7210 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7211 if (err)
7212 goto done;
7214 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7215 if (err)
7216 goto done;
7218 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7219 worktree);
7220 if (err)
7221 goto done;
7223 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7224 0);
7225 if (err)
7226 goto done;
7228 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7229 if (err)
7230 goto done;
7232 err = got_ref_write(*branch_ref, repo);
7233 if (err)
7234 goto done;
7236 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7237 worktree->base_commit_id);
7238 if (err)
7239 goto done;
7240 err = got_ref_write(base_commit_ref, repo);
7241 if (err)
7242 goto done;
7243 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7244 if (*base_commit_id == NULL) {
7245 err = got_error_from_errno("got_object_id_dup");
7246 goto done;
7249 err = got_ref_alloc(tmp_branch, tmp_branch_name,
7250 worktree->base_commit_id);
7251 if (err)
7252 goto done;
7253 err = got_ref_write(*tmp_branch, repo);
7254 if (err)
7255 goto done;
7257 err = got_worktree_set_head_ref(worktree, *tmp_branch);
7258 if (err)
7259 goto done;
7260 done:
7261 free(fileindex_path);
7262 free(tmp_branch_name);
7263 free(branch_ref_name);
7264 free(base_commit_ref_name);
7265 if (wt_branch)
7266 got_ref_close(wt_branch);
7267 if (err) {
7268 if (*branch_ref) {
7269 got_ref_close(*branch_ref);
7270 *branch_ref = NULL;
7272 if (*tmp_branch) {
7273 got_ref_close(*tmp_branch);
7274 *tmp_branch = NULL;
7276 free(*base_commit_id);
7277 if (*fileindex) {
7278 got_fileindex_free(*fileindex);
7279 *fileindex = NULL;
7281 lock_worktree(worktree, LOCK_SH);
7283 return err;
7286 const struct got_error *
7287 got_worktree_histedit_postpone(struct got_worktree *worktree,
7288 struct got_fileindex *fileindex)
7290 if (fileindex)
7291 got_fileindex_free(fileindex);
7292 return lock_worktree(worktree, LOCK_SH);
7295 const struct got_error *
7296 got_worktree_histedit_in_progress(int *in_progress,
7297 struct got_worktree *worktree)
7299 const struct got_error *err;
7300 char *tmp_branch_name = NULL;
7302 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7303 if (err)
7304 return err;
7306 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7307 free(tmp_branch_name);
7308 return NULL;
7311 const struct got_error *
7312 got_worktree_histedit_continue(struct got_object_id **commit_id,
7313 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7314 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7315 struct got_worktree *worktree, struct got_repository *repo)
7317 const struct got_error *err;
7318 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7319 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7320 struct got_reference *commit_ref = NULL;
7321 struct got_reference *base_commit_ref = NULL;
7322 char *fileindex_path = NULL;
7323 int have_staged_files = 0;
7325 *commit_id = NULL;
7326 *tmp_branch = NULL;
7327 *base_commit_id = NULL;
7328 *fileindex = NULL;
7330 err = lock_worktree(worktree, LOCK_EX);
7331 if (err)
7332 return err;
7334 err = open_fileindex(fileindex, &fileindex_path, worktree);
7335 if (err)
7336 goto done;
7338 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7339 &have_staged_files);
7340 if (err && err->code != GOT_ERR_CANCELLED)
7341 goto done;
7342 if (have_staged_files) {
7343 err = got_error(GOT_ERR_STAGED_PATHS);
7344 goto done;
7347 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7348 if (err)
7349 goto done;
7351 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7352 if (err)
7353 goto done;
7355 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7356 if (err)
7357 goto done;
7359 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7360 worktree);
7361 if (err)
7362 goto done;
7364 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7365 if (err)
7366 goto done;
7368 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7369 if (err)
7370 goto done;
7371 err = got_ref_resolve(commit_id, repo, commit_ref);
7372 if (err)
7373 goto done;
7375 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7376 if (err)
7377 goto done;
7378 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7379 if (err)
7380 goto done;
7382 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7383 if (err)
7384 goto done;
7385 done:
7386 free(commit_ref_name);
7387 free(branch_ref_name);
7388 free(fileindex_path);
7389 if (commit_ref)
7390 got_ref_close(commit_ref);
7391 if (base_commit_ref)
7392 got_ref_close(base_commit_ref);
7393 if (err) {
7394 free(*commit_id);
7395 *commit_id = NULL;
7396 free(*base_commit_id);
7397 *base_commit_id = NULL;
7398 if (*tmp_branch) {
7399 got_ref_close(*tmp_branch);
7400 *tmp_branch = NULL;
7402 if (*fileindex) {
7403 got_fileindex_free(*fileindex);
7404 *fileindex = NULL;
7406 lock_worktree(worktree, LOCK_EX);
7408 return err;
7411 static const struct got_error *
7412 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7414 const struct got_error *err;
7415 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7416 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7418 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7419 if (err)
7420 goto done;
7421 err = delete_ref(tmp_branch_name, repo);
7422 if (err)
7423 goto done;
7425 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7426 worktree);
7427 if (err)
7428 goto done;
7429 err = delete_ref(base_commit_ref_name, repo);
7430 if (err)
7431 goto done;
7433 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7434 if (err)
7435 goto done;
7436 err = delete_ref(branch_ref_name, repo);
7437 if (err)
7438 goto done;
7440 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7441 if (err)
7442 goto done;
7443 err = delete_ref(commit_ref_name, repo);
7444 if (err)
7445 goto done;
7446 done:
7447 free(tmp_branch_name);
7448 free(base_commit_ref_name);
7449 free(branch_ref_name);
7450 free(commit_ref_name);
7451 return err;
7454 const struct got_error *
7455 got_worktree_histedit_abort(struct got_worktree *worktree,
7456 struct got_fileindex *fileindex, struct got_repository *repo,
7457 struct got_reference *branch, struct got_object_id *base_commit_id,
7458 got_worktree_checkout_cb progress_cb, void *progress_arg)
7460 const struct got_error *err, *unlockerr, *sync_err;
7461 struct got_reference *resolved = NULL;
7462 char *fileindex_path = NULL;
7463 struct got_commit_object *commit = NULL;
7464 struct got_object_id *tree_id = NULL;
7465 struct revert_file_args rfa;
7467 err = lock_worktree(worktree, LOCK_EX);
7468 if (err)
7469 return err;
7471 err = got_object_open_as_commit(&commit, repo,
7472 worktree->base_commit_id);
7473 if (err)
7474 goto done;
7476 err = got_ref_open(&resolved, repo,
7477 got_ref_get_symref_target(branch), 0);
7478 if (err)
7479 goto done;
7481 err = got_worktree_set_head_ref(worktree, resolved);
7482 if (err)
7483 goto done;
7485 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7486 if (err)
7487 goto done;
7489 err = got_object_id_by_path(&tree_id, repo, commit,
7490 worktree->path_prefix);
7491 if (err)
7492 goto done;
7494 err = delete_histedit_refs(worktree, repo);
7495 if (err)
7496 goto done;
7498 err = get_fileindex_path(&fileindex_path, worktree);
7499 if (err)
7500 goto done;
7502 rfa.worktree = worktree;
7503 rfa.fileindex = fileindex;
7504 rfa.progress_cb = progress_cb;
7505 rfa.progress_arg = progress_arg;
7506 rfa.patch_cb = NULL;
7507 rfa.patch_arg = NULL;
7508 rfa.repo = repo;
7509 rfa.unlink_added_files = 0;
7510 err = worktree_status(worktree, "", fileindex, repo,
7511 revert_file, &rfa, NULL, NULL, 1, 0);
7512 if (err)
7513 goto sync;
7515 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7516 repo, progress_cb, progress_arg, NULL, NULL);
7517 sync:
7518 sync_err = sync_fileindex(fileindex, fileindex_path);
7519 if (sync_err && err == NULL)
7520 err = sync_err;
7521 done:
7522 got_ref_close(resolved);
7523 free(tree_id);
7524 free(fileindex_path);
7526 unlockerr = lock_worktree(worktree, LOCK_SH);
7527 if (unlockerr && err == NULL)
7528 err = unlockerr;
7529 return err;
7532 const struct got_error *
7533 got_worktree_histedit_complete(struct got_worktree *worktree,
7534 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7535 struct got_reference *edited_branch, struct got_repository *repo)
7537 const struct got_error *err, *unlockerr, *sync_err;
7538 struct got_object_id *new_head_commit_id = NULL;
7539 struct got_reference *resolved = NULL;
7540 char *fileindex_path = NULL;
7542 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7543 if (err)
7544 return err;
7546 err = got_ref_open(&resolved, repo,
7547 got_ref_get_symref_target(edited_branch), 0);
7548 if (err)
7549 goto done;
7551 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7552 resolved, new_head_commit_id, repo);
7553 if (err)
7554 goto done;
7556 err = got_ref_change_ref(resolved, new_head_commit_id);
7557 if (err)
7558 goto done;
7560 err = got_ref_write(resolved, repo);
7561 if (err)
7562 goto done;
7564 err = got_worktree_set_head_ref(worktree, resolved);
7565 if (err)
7566 goto done;
7568 err = delete_histedit_refs(worktree, repo);
7569 if (err)
7570 goto done;
7572 err = get_fileindex_path(&fileindex_path, worktree);
7573 if (err)
7574 goto done;
7575 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7576 sync_err = sync_fileindex(fileindex, fileindex_path);
7577 if (sync_err && err == NULL)
7578 err = sync_err;
7579 done:
7580 got_fileindex_free(fileindex);
7581 free(fileindex_path);
7582 free(new_head_commit_id);
7583 unlockerr = lock_worktree(worktree, LOCK_SH);
7584 if (unlockerr && err == NULL)
7585 err = unlockerr;
7586 return err;
7589 const struct got_error *
7590 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7591 struct got_object_id *commit_id, struct got_repository *repo)
7593 const struct got_error *err;
7594 char *commit_ref_name;
7596 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7597 if (err)
7598 return err;
7600 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7601 if (err)
7602 goto done;
7604 err = delete_ref(commit_ref_name, repo);
7605 done:
7606 free(commit_ref_name);
7607 return err;
7610 const struct got_error *
7611 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7612 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7613 struct got_worktree *worktree, const char *refname,
7614 struct got_repository *repo)
7616 const struct got_error *err = NULL;
7617 char *fileindex_path = NULL;
7618 struct check_rebase_ok_arg ok_arg;
7620 *fileindex = NULL;
7621 *branch_ref = NULL;
7622 *base_branch_ref = NULL;
7624 err = lock_worktree(worktree, LOCK_EX);
7625 if (err)
7626 return err;
7628 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7629 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7630 "cannot integrate a branch into itself; "
7631 "update -b or different branch name required");
7632 goto done;
7635 err = open_fileindex(fileindex, &fileindex_path, worktree);
7636 if (err)
7637 goto done;
7639 /* Preconditions are the same as for rebase. */
7640 ok_arg.worktree = worktree;
7641 ok_arg.repo = repo;
7642 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7643 &ok_arg);
7644 if (err)
7645 goto done;
7647 err = got_ref_open(branch_ref, repo, refname, 1);
7648 if (err)
7649 goto done;
7651 err = got_ref_open(base_branch_ref, repo,
7652 got_worktree_get_head_ref_name(worktree), 1);
7653 done:
7654 if (err) {
7655 if (*branch_ref) {
7656 got_ref_close(*branch_ref);
7657 *branch_ref = NULL;
7659 if (*base_branch_ref) {
7660 got_ref_close(*base_branch_ref);
7661 *base_branch_ref = NULL;
7663 if (*fileindex) {
7664 got_fileindex_free(*fileindex);
7665 *fileindex = NULL;
7667 lock_worktree(worktree, LOCK_SH);
7669 return err;
7672 const struct got_error *
7673 got_worktree_integrate_continue(struct got_worktree *worktree,
7674 struct got_fileindex *fileindex, struct got_repository *repo,
7675 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7676 got_worktree_checkout_cb progress_cb, void *progress_arg,
7677 got_cancel_cb cancel_cb, void *cancel_arg)
7679 const struct got_error *err = NULL, *sync_err, *unlockerr;
7680 char *fileindex_path = NULL;
7681 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7682 struct got_commit_object *commit = NULL;
7684 err = get_fileindex_path(&fileindex_path, worktree);
7685 if (err)
7686 goto done;
7688 err = got_ref_resolve(&commit_id, repo, branch_ref);
7689 if (err)
7690 goto done;
7692 err = got_object_open_as_commit(&commit, repo, commit_id);
7693 if (err)
7694 goto done;
7696 err = got_object_id_by_path(&tree_id, repo, commit,
7697 worktree->path_prefix);
7698 if (err)
7699 goto done;
7701 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7702 if (err)
7703 goto done;
7705 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7706 progress_cb, progress_arg, cancel_cb, cancel_arg);
7707 if (err)
7708 goto sync;
7710 err = got_ref_change_ref(base_branch_ref, commit_id);
7711 if (err)
7712 goto sync;
7714 err = got_ref_write(base_branch_ref, repo);
7715 if (err)
7716 goto sync;
7718 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7719 sync:
7720 sync_err = sync_fileindex(fileindex, fileindex_path);
7721 if (sync_err && err == NULL)
7722 err = sync_err;
7724 done:
7725 unlockerr = got_ref_unlock(branch_ref);
7726 if (unlockerr && err == NULL)
7727 err = unlockerr;
7728 got_ref_close(branch_ref);
7730 unlockerr = got_ref_unlock(base_branch_ref);
7731 if (unlockerr && err == NULL)
7732 err = unlockerr;
7733 got_ref_close(base_branch_ref);
7735 got_fileindex_free(fileindex);
7736 free(fileindex_path);
7737 free(tree_id);
7738 if (commit)
7739 got_object_commit_close(commit);
7741 unlockerr = lock_worktree(worktree, LOCK_SH);
7742 if (unlockerr && err == NULL)
7743 err = unlockerr;
7744 return err;
7747 const struct got_error *
7748 got_worktree_integrate_abort(struct got_worktree *worktree,
7749 struct got_fileindex *fileindex, struct got_repository *repo,
7750 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7752 const struct got_error *err = NULL, *unlockerr = NULL;
7754 got_fileindex_free(fileindex);
7756 err = lock_worktree(worktree, LOCK_SH);
7758 unlockerr = got_ref_unlock(branch_ref);
7759 if (unlockerr && err == NULL)
7760 err = unlockerr;
7761 got_ref_close(branch_ref);
7763 unlockerr = got_ref_unlock(base_branch_ref);
7764 if (unlockerr && err == NULL)
7765 err = unlockerr;
7766 got_ref_close(base_branch_ref);
7768 return err;
7771 const struct got_error *
7772 got_worktree_merge_postpone(struct got_worktree *worktree,
7773 struct got_fileindex *fileindex)
7775 const struct got_error *err, *sync_err;
7776 char *fileindex_path = NULL;
7778 err = get_fileindex_path(&fileindex_path, worktree);
7779 if (err)
7780 goto done;
7782 sync_err = sync_fileindex(fileindex, fileindex_path);
7784 err = lock_worktree(worktree, LOCK_SH);
7785 if (sync_err && err == NULL)
7786 err = sync_err;
7787 done:
7788 got_fileindex_free(fileindex);
7789 free(fileindex_path);
7790 return err;
7793 static const struct got_error *
7794 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7796 const struct got_error *err;
7797 char *branch_refname = NULL, *commit_refname = NULL;
7799 err = get_merge_branch_ref_name(&branch_refname, worktree);
7800 if (err)
7801 goto done;
7802 err = delete_ref(branch_refname, repo);
7803 if (err)
7804 goto done;
7806 err = get_merge_commit_ref_name(&commit_refname, worktree);
7807 if (err)
7808 goto done;
7809 err = delete_ref(commit_refname, repo);
7810 if (err)
7811 goto done;
7813 done:
7814 free(branch_refname);
7815 free(commit_refname);
7816 return err;
7819 struct merge_commit_msg_arg {
7820 struct got_worktree *worktree;
7821 const char *branch_name;
7824 static const struct got_error *
7825 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
7826 const char *diff_path, char **logmsg, void *arg)
7828 struct merge_commit_msg_arg *a = arg;
7830 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7831 got_worktree_get_head_ref_name(a->worktree)) == -1)
7832 return got_error_from_errno("asprintf");
7834 return NULL;
7838 const struct got_error *
7839 got_worktree_merge_branch(struct got_worktree *worktree,
7840 struct got_fileindex *fileindex,
7841 struct got_object_id *yca_commit_id,
7842 struct got_object_id *branch_tip,
7843 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7844 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7846 const struct got_error *err;
7847 char *fileindex_path = NULL;
7849 err = get_fileindex_path(&fileindex_path, worktree);
7850 if (err)
7851 goto done;
7853 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7854 worktree);
7855 if (err)
7856 goto done;
7858 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7859 branch_tip, repo, progress_cb, progress_arg,
7860 cancel_cb, cancel_arg);
7861 done:
7862 free(fileindex_path);
7863 return err;
7866 const struct got_error *
7867 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7868 struct got_worktree *worktree, struct got_fileindex *fileindex,
7869 const char *author, const char *committer, int allow_bad_symlinks,
7870 struct got_object_id *branch_tip, const char *branch_name,
7871 struct got_repository *repo,
7872 got_worktree_status_cb status_cb, void *status_arg)
7875 const struct got_error *err = NULL, *sync_err;
7876 struct got_pathlist_head commitable_paths;
7877 struct collect_commitables_arg cc_arg;
7878 struct got_pathlist_entry *pe;
7879 struct got_reference *head_ref = NULL;
7880 struct got_object_id *head_commit_id = NULL;
7881 int have_staged_files = 0;
7882 struct merge_commit_msg_arg mcm_arg;
7883 char *fileindex_path = NULL;
7885 memset(&cc_arg, 0, sizeof(cc_arg));
7886 *new_commit_id = NULL;
7888 TAILQ_INIT(&commitable_paths);
7890 err = get_fileindex_path(&fileindex_path, worktree);
7891 if (err)
7892 goto done;
7894 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7895 if (err)
7896 goto done;
7898 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7899 if (err)
7900 goto done;
7902 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7903 &have_staged_files);
7904 if (err && err->code != GOT_ERR_CANCELLED)
7905 goto done;
7906 if (have_staged_files) {
7907 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7908 goto done;
7911 cc_arg.commitable_paths = &commitable_paths;
7912 cc_arg.worktree = worktree;
7913 cc_arg.fileindex = fileindex;
7914 cc_arg.repo = repo;
7915 cc_arg.have_staged_files = have_staged_files;
7916 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7917 err = worktree_status(worktree, "", fileindex, repo,
7918 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7919 if (err)
7920 goto done;
7922 if (TAILQ_EMPTY(&commitable_paths)) {
7923 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7924 "merge of %s cannot proceed", branch_name);
7925 goto done;
7928 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7929 struct got_commitable *ct = pe->data;
7930 const char *ct_path = ct->in_repo_path;
7932 while (ct_path[0] == '/')
7933 ct_path++;
7934 err = check_out_of_date(ct_path, ct->status,
7935 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7936 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7937 if (err)
7938 goto done;
7942 mcm_arg.worktree = worktree;
7943 mcm_arg.branch_name = branch_name;
7944 err = commit_worktree(new_commit_id, &commitable_paths,
7945 head_commit_id, branch_tip, worktree, author, committer, NULL,
7946 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7947 if (err)
7948 goto done;
7950 err = update_fileindex_after_commit(worktree, &commitable_paths,
7951 *new_commit_id, fileindex, have_staged_files);
7952 sync_err = sync_fileindex(fileindex, fileindex_path);
7953 if (sync_err && err == NULL)
7954 err = sync_err;
7955 done:
7956 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7957 struct got_commitable *ct = pe->data;
7958 free_commitable(ct);
7960 got_pathlist_free(&commitable_paths);
7961 free(fileindex_path);
7962 return err;
7965 const struct got_error *
7966 got_worktree_merge_complete(struct got_worktree *worktree,
7967 struct got_fileindex *fileindex, struct got_repository *repo)
7969 const struct got_error *err, *unlockerr, *sync_err;
7970 char *fileindex_path = NULL;
7972 err = delete_merge_refs(worktree, repo);
7973 if (err)
7974 goto done;
7976 err = get_fileindex_path(&fileindex_path, worktree);
7977 if (err)
7978 goto done;
7979 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7980 sync_err = sync_fileindex(fileindex, fileindex_path);
7981 if (sync_err && err == NULL)
7982 err = sync_err;
7983 done:
7984 got_fileindex_free(fileindex);
7985 free(fileindex_path);
7986 unlockerr = lock_worktree(worktree, LOCK_SH);
7987 if (unlockerr && err == NULL)
7988 err = unlockerr;
7989 return err;
7992 const struct got_error *
7993 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7994 struct got_repository *repo)
7996 const struct got_error *err;
7997 char *branch_refname = NULL;
7998 struct got_reference *branch_ref = NULL;
8000 *in_progress = 0;
8002 err = get_merge_branch_ref_name(&branch_refname, worktree);
8003 if (err)
8004 return err;
8005 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8006 free(branch_refname);
8007 if (err) {
8008 if (err->code != GOT_ERR_NOT_REF)
8009 return err;
8010 } else
8011 *in_progress = 1;
8013 return NULL;
8016 const struct got_error *got_worktree_merge_prepare(
8017 struct got_fileindex **fileindex, struct got_worktree *worktree,
8018 struct got_reference *branch, struct got_repository *repo)
8020 const struct got_error *err = NULL;
8021 char *fileindex_path = NULL;
8022 char *branch_refname = NULL, *commit_refname = NULL;
8023 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
8024 struct got_reference *commit_ref = NULL;
8025 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
8026 struct check_rebase_ok_arg ok_arg;
8028 *fileindex = NULL;
8030 err = lock_worktree(worktree, LOCK_EX);
8031 if (err)
8032 return err;
8034 err = open_fileindex(fileindex, &fileindex_path, worktree);
8035 if (err)
8036 goto done;
8038 /* Preconditions are the same as for rebase. */
8039 ok_arg.worktree = worktree;
8040 ok_arg.repo = repo;
8041 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8042 &ok_arg);
8043 if (err)
8044 goto done;
8046 err = get_merge_branch_ref_name(&branch_refname, worktree);
8047 if (err)
8048 return err;
8050 err = get_merge_commit_ref_name(&commit_refname, worktree);
8051 if (err)
8052 return err;
8054 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8055 0);
8056 if (err)
8057 goto done;
8059 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8060 if (err)
8061 goto done;
8063 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8064 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8065 goto done;
8068 err = got_ref_resolve(&branch_tip, repo, branch);
8069 if (err)
8070 goto done;
8072 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8073 if (err)
8074 goto done;
8075 err = got_ref_write(branch_ref, repo);
8076 if (err)
8077 goto done;
8079 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8080 if (err)
8081 goto done;
8082 err = got_ref_write(commit_ref, repo);
8083 if (err)
8084 goto done;
8086 done:
8087 free(branch_refname);
8088 free(commit_refname);
8089 free(fileindex_path);
8090 if (branch_ref)
8091 got_ref_close(branch_ref);
8092 if (commit_ref)
8093 got_ref_close(commit_ref);
8094 if (wt_branch)
8095 got_ref_close(wt_branch);
8096 free(wt_branch_tip);
8097 if (err) {
8098 if (*fileindex) {
8099 got_fileindex_free(*fileindex);
8100 *fileindex = NULL;
8102 lock_worktree(worktree, LOCK_SH);
8104 return err;
8107 const struct got_error *
8108 got_worktree_merge_continue(char **branch_name,
8109 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8110 struct got_worktree *worktree, struct got_repository *repo)
8112 const struct got_error *err;
8113 char *commit_refname = NULL, *branch_refname = NULL;
8114 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8115 char *fileindex_path = NULL;
8116 int have_staged_files = 0;
8118 *branch_name = NULL;
8119 *branch_tip = NULL;
8120 *fileindex = NULL;
8122 err = lock_worktree(worktree, LOCK_EX);
8123 if (err)
8124 return err;
8126 err = open_fileindex(fileindex, &fileindex_path, worktree);
8127 if (err)
8128 goto done;
8130 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8131 &have_staged_files);
8132 if (err && err->code != GOT_ERR_CANCELLED)
8133 goto done;
8134 if (have_staged_files) {
8135 err = got_error(GOT_ERR_STAGED_PATHS);
8136 goto done;
8139 err = get_merge_branch_ref_name(&branch_refname, worktree);
8140 if (err)
8141 goto done;
8143 err = get_merge_commit_ref_name(&commit_refname, worktree);
8144 if (err)
8145 goto done;
8147 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8148 if (err)
8149 goto done;
8151 if (!got_ref_is_symbolic(branch_ref)) {
8152 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8153 "%s is not a symbolic reference",
8154 got_ref_get_name(branch_ref));
8155 goto done;
8157 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8158 if (*branch_name == NULL) {
8159 err = got_error_from_errno("strdup");
8160 goto done;
8163 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8164 if (err)
8165 goto done;
8167 err = got_ref_resolve(branch_tip, repo, commit_ref);
8168 if (err)
8169 goto done;
8170 done:
8171 free(commit_refname);
8172 free(branch_refname);
8173 free(fileindex_path);
8174 if (commit_ref)
8175 got_ref_close(commit_ref);
8176 if (branch_ref)
8177 got_ref_close(branch_ref);
8178 if (err) {
8179 if (*branch_name) {
8180 free(*branch_name);
8181 *branch_name = NULL;
8183 free(*branch_tip);
8184 *branch_tip = NULL;
8185 if (*fileindex) {
8186 got_fileindex_free(*fileindex);
8187 *fileindex = NULL;
8189 lock_worktree(worktree, LOCK_SH);
8191 return err;
8194 const struct got_error *
8195 got_worktree_merge_abort(struct got_worktree *worktree,
8196 struct got_fileindex *fileindex, struct got_repository *repo,
8197 got_worktree_checkout_cb progress_cb, void *progress_arg)
8199 const struct got_error *err, *unlockerr, *sync_err;
8200 struct got_object_id *commit_id = NULL;
8201 struct got_commit_object *commit = NULL;
8202 char *fileindex_path = NULL;
8203 struct revert_file_args rfa;
8204 struct got_object_id *tree_id = NULL;
8206 err = got_object_open_as_commit(&commit, repo,
8207 worktree->base_commit_id);
8208 if (err)
8209 goto done;
8211 err = got_object_id_by_path(&tree_id, repo, commit,
8212 worktree->path_prefix);
8213 if (err)
8214 goto done;
8216 err = delete_merge_refs(worktree, repo);
8217 if (err)
8218 goto done;
8220 err = get_fileindex_path(&fileindex_path, worktree);
8221 if (err)
8222 goto done;
8224 rfa.worktree = worktree;
8225 rfa.fileindex = fileindex;
8226 rfa.progress_cb = progress_cb;
8227 rfa.progress_arg = progress_arg;
8228 rfa.patch_cb = NULL;
8229 rfa.patch_arg = NULL;
8230 rfa.repo = repo;
8231 rfa.unlink_added_files = 1;
8232 err = worktree_status(worktree, "", fileindex, repo,
8233 revert_file, &rfa, NULL, NULL, 1, 0);
8234 if (err)
8235 goto sync;
8237 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8238 repo, progress_cb, progress_arg, NULL, NULL);
8239 sync:
8240 sync_err = sync_fileindex(fileindex, fileindex_path);
8241 if (sync_err && err == NULL)
8242 err = sync_err;
8243 done:
8244 free(tree_id);
8245 free(commit_id);
8246 if (commit)
8247 got_object_commit_close(commit);
8248 if (fileindex)
8249 got_fileindex_free(fileindex);
8250 free(fileindex_path);
8252 unlockerr = lock_worktree(worktree, LOCK_SH);
8253 if (unlockerr && err == NULL)
8254 err = unlockerr;
8255 return err;
8258 struct check_stage_ok_arg {
8259 struct got_object_id *head_commit_id;
8260 struct got_worktree *worktree;
8261 struct got_fileindex *fileindex;
8262 struct got_repository *repo;
8263 int have_changes;
8266 static const struct got_error *
8267 check_stage_ok(void *arg, unsigned char status,
8268 unsigned char staged_status, const char *relpath,
8269 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8270 struct got_object_id *commit_id, int dirfd, const char *de_name)
8272 struct check_stage_ok_arg *a = arg;
8273 const struct got_error *err = NULL;
8274 struct got_fileindex_entry *ie;
8275 struct got_object_id base_commit_id;
8276 struct got_object_id *base_commit_idp = NULL;
8277 char *in_repo_path = NULL, *p;
8279 if (status == GOT_STATUS_UNVERSIONED ||
8280 status == GOT_STATUS_NO_CHANGE)
8281 return NULL;
8282 if (status == GOT_STATUS_NONEXISTENT)
8283 return got_error_set_errno(ENOENT, relpath);
8285 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8286 if (ie == NULL)
8287 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8289 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8290 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8291 relpath) == -1)
8292 return got_error_from_errno("asprintf");
8294 if (got_fileindex_entry_has_commit(ie)) {
8295 memcpy(base_commit_id.sha1, ie->commit_sha1,
8296 SHA1_DIGEST_LENGTH);
8297 base_commit_idp = &base_commit_id;
8300 if (status == GOT_STATUS_CONFLICT) {
8301 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8302 goto done;
8303 } else if (status != GOT_STATUS_ADD &&
8304 status != GOT_STATUS_MODIFY &&
8305 status != GOT_STATUS_DELETE) {
8306 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8307 goto done;
8310 a->have_changes = 1;
8312 p = in_repo_path;
8313 while (p[0] == '/')
8314 p++;
8315 err = check_out_of_date(p, status, staged_status,
8316 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8317 GOT_ERR_STAGE_OUT_OF_DATE);
8318 done:
8319 free(in_repo_path);
8320 return err;
8323 struct stage_path_arg {
8324 struct got_worktree *worktree;
8325 struct got_fileindex *fileindex;
8326 struct got_repository *repo;
8327 got_worktree_status_cb status_cb;
8328 void *status_arg;
8329 got_worktree_patch_cb patch_cb;
8330 void *patch_arg;
8331 int staged_something;
8332 int allow_bad_symlinks;
8335 static const struct got_error *
8336 stage_path(void *arg, unsigned char status,
8337 unsigned char staged_status, const char *relpath,
8338 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8339 struct got_object_id *commit_id, int dirfd, const char *de_name)
8341 struct stage_path_arg *a = arg;
8342 const struct got_error *err = NULL;
8343 struct got_fileindex_entry *ie;
8344 char *ondisk_path = NULL, *path_content = NULL;
8345 uint32_t stage;
8346 struct got_object_id *new_staged_blob_id = NULL;
8347 struct stat sb;
8349 if (status == GOT_STATUS_UNVERSIONED)
8350 return NULL;
8352 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8353 if (ie == NULL)
8354 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8356 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8357 relpath)== -1)
8358 return got_error_from_errno("asprintf");
8360 switch (status) {
8361 case GOT_STATUS_ADD:
8362 case GOT_STATUS_MODIFY:
8363 /* XXX could sb.st_mode be passed in by our caller? */
8364 if (lstat(ondisk_path, &sb) == -1) {
8365 err = got_error_from_errno2("lstat", ondisk_path);
8366 break;
8368 if (a->patch_cb) {
8369 if (status == GOT_STATUS_ADD) {
8370 int choice = GOT_PATCH_CHOICE_NONE;
8371 err = (*a->patch_cb)(&choice, a->patch_arg,
8372 status, ie->path, NULL, 1, 1);
8373 if (err)
8374 break;
8375 if (choice != GOT_PATCH_CHOICE_YES)
8376 break;
8377 } else {
8378 err = create_patched_content(&path_content, 0,
8379 staged_blob_id ? staged_blob_id : blob_id,
8380 ondisk_path, dirfd, de_name, ie->path,
8381 a->repo, a->patch_cb, a->patch_arg);
8382 if (err || path_content == NULL)
8383 break;
8386 err = got_object_blob_create(&new_staged_blob_id,
8387 path_content ? path_content : ondisk_path, a->repo);
8388 if (err)
8389 break;
8390 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8391 SHA1_DIGEST_LENGTH);
8392 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8393 stage = GOT_FILEIDX_STAGE_ADD;
8394 else
8395 stage = GOT_FILEIDX_STAGE_MODIFY;
8396 got_fileindex_entry_stage_set(ie, stage);
8397 if (S_ISLNK(sb.st_mode)) {
8398 int is_bad_symlink = 0;
8399 if (!a->allow_bad_symlinks) {
8400 char target_path[PATH_MAX];
8401 ssize_t target_len;
8402 target_len = readlink(ondisk_path, target_path,
8403 sizeof(target_path));
8404 if (target_len == -1) {
8405 err = got_error_from_errno2("readlink",
8406 ondisk_path);
8407 break;
8409 err = is_bad_symlink_target(&is_bad_symlink,
8410 target_path, target_len, ondisk_path,
8411 a->worktree->root_path);
8412 if (err)
8413 break;
8414 if (is_bad_symlink) {
8415 err = got_error_path(ondisk_path,
8416 GOT_ERR_BAD_SYMLINK);
8417 break;
8420 if (is_bad_symlink)
8421 got_fileindex_entry_staged_filetype_set(ie,
8422 GOT_FILEIDX_MODE_BAD_SYMLINK);
8423 else
8424 got_fileindex_entry_staged_filetype_set(ie,
8425 GOT_FILEIDX_MODE_SYMLINK);
8426 } else {
8427 got_fileindex_entry_staged_filetype_set(ie,
8428 GOT_FILEIDX_MODE_REGULAR_FILE);
8430 a->staged_something = 1;
8431 if (a->status_cb == NULL)
8432 break;
8433 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8434 get_staged_status(ie), relpath, blob_id,
8435 new_staged_blob_id, NULL, dirfd, de_name);
8436 if (err)
8437 break;
8439 * When staging the reverse of the staged diff,
8440 * implicitly unstage the file.
8442 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8443 sizeof(ie->blob_sha1)) == 0) {
8444 got_fileindex_entry_stage_set(ie,
8445 GOT_FILEIDX_STAGE_NONE);
8447 break;
8448 case GOT_STATUS_DELETE:
8449 if (staged_status == GOT_STATUS_DELETE)
8450 break;
8451 if (a->patch_cb) {
8452 int choice = GOT_PATCH_CHOICE_NONE;
8453 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8454 ie->path, NULL, 1, 1);
8455 if (err)
8456 break;
8457 if (choice == GOT_PATCH_CHOICE_NO)
8458 break;
8459 if (choice != GOT_PATCH_CHOICE_YES) {
8460 err = got_error(GOT_ERR_PATCH_CHOICE);
8461 break;
8464 stage = GOT_FILEIDX_STAGE_DELETE;
8465 got_fileindex_entry_stage_set(ie, stage);
8466 a->staged_something = 1;
8467 if (a->status_cb == NULL)
8468 break;
8469 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8470 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8471 de_name);
8472 break;
8473 case GOT_STATUS_NO_CHANGE:
8474 break;
8475 case GOT_STATUS_CONFLICT:
8476 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8477 break;
8478 case GOT_STATUS_NONEXISTENT:
8479 err = got_error_set_errno(ENOENT, relpath);
8480 break;
8481 default:
8482 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8483 break;
8486 if (path_content && unlink(path_content) == -1 && err == NULL)
8487 err = got_error_from_errno2("unlink", path_content);
8488 free(path_content);
8489 free(ondisk_path);
8490 free(new_staged_blob_id);
8491 return err;
8494 const struct got_error *
8495 got_worktree_stage(struct got_worktree *worktree,
8496 struct got_pathlist_head *paths,
8497 got_worktree_status_cb status_cb, void *status_arg,
8498 got_worktree_patch_cb patch_cb, void *patch_arg,
8499 int allow_bad_symlinks, struct got_repository *repo)
8501 const struct got_error *err = NULL, *sync_err, *unlockerr;
8502 struct got_pathlist_entry *pe;
8503 struct got_fileindex *fileindex = NULL;
8504 char *fileindex_path = NULL;
8505 struct got_reference *head_ref = NULL;
8506 struct got_object_id *head_commit_id = NULL;
8507 struct check_stage_ok_arg oka;
8508 struct stage_path_arg spa;
8510 err = lock_worktree(worktree, LOCK_EX);
8511 if (err)
8512 return err;
8514 err = got_ref_open(&head_ref, repo,
8515 got_worktree_get_head_ref_name(worktree), 0);
8516 if (err)
8517 goto done;
8518 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8519 if (err)
8520 goto done;
8521 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8522 if (err)
8523 goto done;
8525 /* Check pre-conditions before staging anything. */
8526 oka.head_commit_id = head_commit_id;
8527 oka.worktree = worktree;
8528 oka.fileindex = fileindex;
8529 oka.repo = repo;
8530 oka.have_changes = 0;
8531 TAILQ_FOREACH(pe, paths, entry) {
8532 err = worktree_status(worktree, pe->path, fileindex, repo,
8533 check_stage_ok, &oka, NULL, NULL, 1, 0);
8534 if (err)
8535 goto done;
8537 if (!oka.have_changes) {
8538 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8539 goto done;
8542 spa.worktree = worktree;
8543 spa.fileindex = fileindex;
8544 spa.repo = repo;
8545 spa.patch_cb = patch_cb;
8546 spa.patch_arg = patch_arg;
8547 spa.status_cb = status_cb;
8548 spa.status_arg = status_arg;
8549 spa.staged_something = 0;
8550 spa.allow_bad_symlinks = allow_bad_symlinks;
8551 TAILQ_FOREACH(pe, paths, entry) {
8552 err = worktree_status(worktree, pe->path, fileindex, repo,
8553 stage_path, &spa, NULL, NULL, 1, 0);
8554 if (err)
8555 goto done;
8557 if (!spa.staged_something) {
8558 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8559 goto done;
8562 sync_err = sync_fileindex(fileindex, fileindex_path);
8563 if (sync_err && err == NULL)
8564 err = sync_err;
8565 done:
8566 if (head_ref)
8567 got_ref_close(head_ref);
8568 free(head_commit_id);
8569 free(fileindex_path);
8570 if (fileindex)
8571 got_fileindex_free(fileindex);
8572 unlockerr = lock_worktree(worktree, LOCK_SH);
8573 if (unlockerr && err == NULL)
8574 err = unlockerr;
8575 return err;
8578 struct unstage_path_arg {
8579 struct got_worktree *worktree;
8580 struct got_fileindex *fileindex;
8581 struct got_repository *repo;
8582 got_worktree_checkout_cb progress_cb;
8583 void *progress_arg;
8584 got_worktree_patch_cb patch_cb;
8585 void *patch_arg;
8588 static const struct got_error *
8589 create_unstaged_content(char **path_unstaged_content,
8590 char **path_new_staged_content, struct got_object_id *blob_id,
8591 struct got_object_id *staged_blob_id, const char *relpath,
8592 struct got_repository *repo,
8593 got_worktree_patch_cb patch_cb, void *patch_arg)
8595 const struct got_error *err, *free_err;
8596 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8597 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8598 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8599 struct got_diffreg_result *diffreg_result = NULL;
8600 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8601 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8602 int fd1 = -1, fd2 = -1;
8604 *path_unstaged_content = NULL;
8605 *path_new_staged_content = NULL;
8607 err = got_object_id_str(&label1, blob_id);
8608 if (err)
8609 return err;
8611 fd1 = got_opentempfd();
8612 if (fd1 == -1) {
8613 err = got_error_from_errno("got_opentempfd");
8614 goto done;
8616 fd2 = got_opentempfd();
8617 if (fd2 == -1) {
8618 err = got_error_from_errno("got_opentempfd");
8619 goto done;
8622 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8623 if (err)
8624 goto done;
8626 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
8627 if (err)
8628 goto done;
8630 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8631 if (err)
8632 goto done;
8634 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8635 fd2);
8636 if (err)
8637 goto done;
8639 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
8640 if (err)
8641 goto done;
8643 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8644 if (err)
8645 goto done;
8647 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8648 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8649 if (err)
8650 goto done;
8652 err = got_opentemp_named(path_unstaged_content, &outfile,
8653 "got-unstaged-content", "");
8654 if (err)
8655 goto done;
8656 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8657 "got-new-staged-content", "");
8658 if (err)
8659 goto done;
8661 if (fseek(f1, 0L, SEEK_SET) == -1) {
8662 err = got_ferror(f1, GOT_ERR_IO);
8663 goto done;
8665 if (fseek(f2, 0L, SEEK_SET) == -1) {
8666 err = got_ferror(f2, GOT_ERR_IO);
8667 goto done;
8669 /* Count the number of actual changes in the diff result. */
8670 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8671 struct diff_chunk_context cc = {};
8672 diff_chunk_context_load_change(&cc, &nchunks_used,
8673 diffreg_result->result, n, 0);
8674 nchanges++;
8676 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8677 int choice;
8678 err = apply_or_reject_change(&choice, &nchunks_used,
8679 diffreg_result->result, n, relpath, f1, f2,
8680 &line_cur1, &line_cur2,
8681 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8682 if (err)
8683 goto done;
8684 if (choice == GOT_PATCH_CHOICE_YES)
8685 have_content = 1;
8686 else
8687 have_rejected_content = 1;
8688 if (choice == GOT_PATCH_CHOICE_QUIT)
8689 break;
8691 if (have_content || have_rejected_content)
8692 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8693 outfile, rejectfile);
8694 done:
8695 free(label1);
8696 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8697 err = got_error_from_errno("close");
8698 if (blob)
8699 got_object_blob_close(blob);
8700 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8701 err = got_error_from_errno("close");
8702 if (staged_blob)
8703 got_object_blob_close(staged_blob);
8704 free_err = got_diffreg_result_free(diffreg_result);
8705 if (free_err && err == NULL)
8706 err = free_err;
8707 if (f1 && fclose(f1) == EOF && err == NULL)
8708 err = got_error_from_errno2("fclose", path1);
8709 if (f2 && fclose(f2) == EOF && err == NULL)
8710 err = got_error_from_errno2("fclose", path2);
8711 if (outfile && fclose(outfile) == EOF && err == NULL)
8712 err = got_error_from_errno2("fclose", *path_unstaged_content);
8713 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8714 err = got_error_from_errno2("fclose", *path_new_staged_content);
8715 if (path1 && unlink(path1) == -1 && err == NULL)
8716 err = got_error_from_errno2("unlink", path1);
8717 if (path2 && unlink(path2) == -1 && err == NULL)
8718 err = got_error_from_errno2("unlink", path2);
8719 if (err || !have_content) {
8720 if (*path_unstaged_content &&
8721 unlink(*path_unstaged_content) == -1 && err == NULL)
8722 err = got_error_from_errno2("unlink",
8723 *path_unstaged_content);
8724 free(*path_unstaged_content);
8725 *path_unstaged_content = NULL;
8727 if (err || !have_content || !have_rejected_content) {
8728 if (*path_new_staged_content &&
8729 unlink(*path_new_staged_content) == -1 && err == NULL)
8730 err = got_error_from_errno2("unlink",
8731 *path_new_staged_content);
8732 free(*path_new_staged_content);
8733 *path_new_staged_content = NULL;
8735 free(path1);
8736 free(path2);
8737 return err;
8740 static const struct got_error *
8741 unstage_hunks(struct got_object_id *staged_blob_id,
8742 struct got_blob_object *blob_base,
8743 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8744 const char *ondisk_path, const char *label_orig,
8745 struct got_worktree *worktree, struct got_repository *repo,
8746 got_worktree_patch_cb patch_cb, void *patch_arg,
8747 got_worktree_checkout_cb progress_cb, void *progress_arg)
8749 const struct got_error *err = NULL;
8750 char *path_unstaged_content = NULL;
8751 char *path_new_staged_content = NULL;
8752 char *parent = NULL, *base_path = NULL;
8753 char *blob_base_path = NULL;
8754 struct got_object_id *new_staged_blob_id = NULL;
8755 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8756 struct stat sb;
8758 err = create_unstaged_content(&path_unstaged_content,
8759 &path_new_staged_content, blob_id, staged_blob_id,
8760 ie->path, repo, patch_cb, patch_arg);
8761 if (err)
8762 return err;
8764 if (path_unstaged_content == NULL)
8765 return NULL;
8767 if (path_new_staged_content) {
8768 err = got_object_blob_create(&new_staged_blob_id,
8769 path_new_staged_content, repo);
8770 if (err)
8771 goto done;
8774 f = fopen(path_unstaged_content, "re");
8775 if (f == NULL) {
8776 err = got_error_from_errno2("fopen",
8777 path_unstaged_content);
8778 goto done;
8780 if (fstat(fileno(f), &sb) == -1) {
8781 err = got_error_from_errno2("fstat", path_unstaged_content);
8782 goto done;
8784 if (got_fileindex_entry_staged_filetype_get(ie) ==
8785 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8786 char link_target[PATH_MAX];
8787 size_t r;
8788 r = fread(link_target, 1, sizeof(link_target), f);
8789 if (r == 0 && ferror(f)) {
8790 err = got_error_from_errno("fread");
8791 goto done;
8793 if (r >= sizeof(link_target)) { /* should not happen */
8794 err = got_error(GOT_ERR_NO_SPACE);
8795 goto done;
8797 link_target[r] = '\0';
8798 err = merge_symlink(worktree, blob_base,
8799 ondisk_path, ie->path, label_orig, link_target,
8800 worktree->base_commit_id, repo, progress_cb,
8801 progress_arg);
8802 } else {
8803 int local_changes_subsumed;
8805 err = got_path_dirname(&parent, ondisk_path);
8806 if (err)
8807 return err;
8809 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8810 parent) == -1) {
8811 err = got_error_from_errno("asprintf");
8812 base_path = NULL;
8813 goto done;
8816 err = got_opentemp_named(&blob_base_path, &f_base,
8817 base_path, "");
8818 if (err)
8819 goto done;
8820 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8821 blob_base);
8822 if (err)
8823 goto done;
8826 * In order the run a 3-way merge with a symlink we copy the symlink's
8827 * target path into a temporary file and use that file with diff3.
8829 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8830 err = dump_symlink_target_path_to_file(&f_deriv2,
8831 ondisk_path);
8832 if (err)
8833 goto done;
8834 } else {
8835 int fd;
8836 fd = open(ondisk_path,
8837 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8838 if (fd == -1) {
8839 err = got_error_from_errno2("open", ondisk_path);
8840 goto done;
8842 f_deriv2 = fdopen(fd, "r");
8843 if (f_deriv2 == NULL) {
8844 err = got_error_from_errno2("fdopen", ondisk_path);
8845 close(fd);
8846 goto done;
8850 err = merge_file(&local_changes_subsumed, worktree,
8851 f_base, f, f_deriv2, ondisk_path, ie->path,
8852 got_fileindex_perms_to_st(ie),
8853 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8854 repo, progress_cb, progress_arg);
8856 if (err)
8857 goto done;
8859 if (new_staged_blob_id) {
8860 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8861 SHA1_DIGEST_LENGTH);
8862 } else {
8863 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8864 got_fileindex_entry_staged_filetype_set(ie, 0);
8866 done:
8867 free(new_staged_blob_id);
8868 if (path_unstaged_content &&
8869 unlink(path_unstaged_content) == -1 && err == NULL)
8870 err = got_error_from_errno2("unlink", path_unstaged_content);
8871 if (path_new_staged_content &&
8872 unlink(path_new_staged_content) == -1 && err == NULL)
8873 err = got_error_from_errno2("unlink", path_new_staged_content);
8874 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8875 err = got_error_from_errno2("unlink", blob_base_path);
8876 if (f_base && fclose(f_base) == EOF && err == NULL)
8877 err = got_error_from_errno2("fclose", path_unstaged_content);
8878 if (f && fclose(f) == EOF && err == NULL)
8879 err = got_error_from_errno2("fclose", path_unstaged_content);
8880 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8881 err = got_error_from_errno2("fclose", ondisk_path);
8882 free(path_unstaged_content);
8883 free(path_new_staged_content);
8884 free(blob_base_path);
8885 free(parent);
8886 free(base_path);
8887 return err;
8890 static const struct got_error *
8891 unstage_path(void *arg, unsigned char status,
8892 unsigned char staged_status, const char *relpath,
8893 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8894 struct got_object_id *commit_id, int dirfd, const char *de_name)
8896 const struct got_error *err = NULL;
8897 struct unstage_path_arg *a = arg;
8898 struct got_fileindex_entry *ie;
8899 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8900 char *ondisk_path = NULL;
8901 char *id_str = NULL, *label_orig = NULL;
8902 int local_changes_subsumed;
8903 struct stat sb;
8904 int fd1 = -1, fd2 = -1;
8906 if (staged_status != GOT_STATUS_ADD &&
8907 staged_status != GOT_STATUS_MODIFY &&
8908 staged_status != GOT_STATUS_DELETE)
8909 return NULL;
8911 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8912 if (ie == NULL)
8913 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8915 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8916 == -1)
8917 return got_error_from_errno("asprintf");
8919 err = got_object_id_str(&id_str,
8920 commit_id ? commit_id : a->worktree->base_commit_id);
8921 if (err)
8922 goto done;
8923 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8924 id_str) == -1) {
8925 err = got_error_from_errno("asprintf");
8926 goto done;
8929 fd1 = got_opentempfd();
8930 if (fd1 == -1) {
8931 err = got_error_from_errno("got_opentempfd");
8932 goto done;
8934 fd2 = got_opentempfd();
8935 if (fd2 == -1) {
8936 err = got_error_from_errno("got_opentempfd");
8937 goto done;
8940 switch (staged_status) {
8941 case GOT_STATUS_MODIFY:
8942 err = got_object_open_as_blob(&blob_base, a->repo,
8943 blob_id, 8192, fd1);
8944 if (err)
8945 break;
8946 /* fall through */
8947 case GOT_STATUS_ADD:
8948 if (a->patch_cb) {
8949 if (staged_status == GOT_STATUS_ADD) {
8950 int choice = GOT_PATCH_CHOICE_NONE;
8951 err = (*a->patch_cb)(&choice, a->patch_arg,
8952 staged_status, ie->path, NULL, 1, 1);
8953 if (err)
8954 break;
8955 if (choice != GOT_PATCH_CHOICE_YES)
8956 break;
8957 } else {
8958 err = unstage_hunks(staged_blob_id,
8959 blob_base, blob_id, ie, ondisk_path,
8960 label_orig, a->worktree, a->repo,
8961 a->patch_cb, a->patch_arg,
8962 a->progress_cb, a->progress_arg);
8963 break; /* Done with this file. */
8966 err = got_object_open_as_blob(&blob_staged, a->repo,
8967 staged_blob_id, 8192, fd2);
8968 if (err)
8969 break;
8970 switch (got_fileindex_entry_staged_filetype_get(ie)) {
8971 case GOT_FILEIDX_MODE_BAD_SYMLINK:
8972 case GOT_FILEIDX_MODE_REGULAR_FILE:
8973 err = merge_blob(&local_changes_subsumed, a->worktree,
8974 blob_base, ondisk_path, relpath,
8975 got_fileindex_perms_to_st(ie), label_orig,
8976 blob_staged, commit_id ? commit_id :
8977 a->worktree->base_commit_id, a->repo,
8978 a->progress_cb, a->progress_arg);
8979 break;
8980 case GOT_FILEIDX_MODE_SYMLINK:
8981 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8982 char *staged_target;
8983 err = got_object_blob_read_to_str(
8984 &staged_target, blob_staged);
8985 if (err)
8986 goto done;
8987 err = merge_symlink(a->worktree, blob_base,
8988 ondisk_path, relpath, label_orig,
8989 staged_target, commit_id ? commit_id :
8990 a->worktree->base_commit_id,
8991 a->repo, a->progress_cb, a->progress_arg);
8992 free(staged_target);
8993 } else {
8994 err = merge_blob(&local_changes_subsumed,
8995 a->worktree, blob_base, ondisk_path,
8996 relpath, got_fileindex_perms_to_st(ie),
8997 label_orig, blob_staged,
8998 commit_id ? commit_id :
8999 a->worktree->base_commit_id, a->repo,
9000 a->progress_cb, a->progress_arg);
9002 break;
9003 default:
9004 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9005 break;
9007 if (err == NULL) {
9008 got_fileindex_entry_stage_set(ie,
9009 GOT_FILEIDX_STAGE_NONE);
9010 got_fileindex_entry_staged_filetype_set(ie, 0);
9012 break;
9013 case GOT_STATUS_DELETE:
9014 if (a->patch_cb) {
9015 int choice = GOT_PATCH_CHOICE_NONE;
9016 err = (*a->patch_cb)(&choice, a->patch_arg,
9017 staged_status, ie->path, NULL, 1, 1);
9018 if (err)
9019 break;
9020 if (choice == GOT_PATCH_CHOICE_NO)
9021 break;
9022 if (choice != GOT_PATCH_CHOICE_YES) {
9023 err = got_error(GOT_ERR_PATCH_CHOICE);
9024 break;
9027 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9028 got_fileindex_entry_staged_filetype_set(ie, 0);
9029 err = get_file_status(&status, &sb, ie, ondisk_path,
9030 dirfd, de_name, a->repo);
9031 if (err)
9032 break;
9033 err = (*a->progress_cb)(a->progress_arg, status, relpath);
9034 break;
9036 done:
9037 free(ondisk_path);
9038 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9039 err = got_error_from_errno("close");
9040 if (blob_base)
9041 got_object_blob_close(blob_base);
9042 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9043 err = got_error_from_errno("close");
9044 if (blob_staged)
9045 got_object_blob_close(blob_staged);
9046 free(id_str);
9047 free(label_orig);
9048 return err;
9051 const struct got_error *
9052 got_worktree_unstage(struct got_worktree *worktree,
9053 struct got_pathlist_head *paths,
9054 got_worktree_checkout_cb progress_cb, void *progress_arg,
9055 got_worktree_patch_cb patch_cb, void *patch_arg,
9056 struct got_repository *repo)
9058 const struct got_error *err = NULL, *sync_err, *unlockerr;
9059 struct got_pathlist_entry *pe;
9060 struct got_fileindex *fileindex = NULL;
9061 char *fileindex_path = NULL;
9062 struct unstage_path_arg upa;
9064 err = lock_worktree(worktree, LOCK_EX);
9065 if (err)
9066 return err;
9068 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9069 if (err)
9070 goto done;
9072 upa.worktree = worktree;
9073 upa.fileindex = fileindex;
9074 upa.repo = repo;
9075 upa.progress_cb = progress_cb;
9076 upa.progress_arg = progress_arg;
9077 upa.patch_cb = patch_cb;
9078 upa.patch_arg = patch_arg;
9079 TAILQ_FOREACH(pe, paths, entry) {
9080 err = worktree_status(worktree, pe->path, fileindex, repo,
9081 unstage_path, &upa, NULL, NULL, 1, 0);
9082 if (err)
9083 goto done;
9086 sync_err = sync_fileindex(fileindex, fileindex_path);
9087 if (sync_err && err == NULL)
9088 err = sync_err;
9089 done:
9090 free(fileindex_path);
9091 if (fileindex)
9092 got_fileindex_free(fileindex);
9093 unlockerr = lock_worktree(worktree, LOCK_SH);
9094 if (unlockerr && err == NULL)
9095 err = unlockerr;
9096 return err;
9099 struct report_file_info_arg {
9100 struct got_worktree *worktree;
9101 got_worktree_path_info_cb info_cb;
9102 void *info_arg;
9103 struct got_pathlist_head *paths;
9104 got_cancel_cb cancel_cb;
9105 void *cancel_arg;
9108 static const struct got_error *
9109 report_file_info(void *arg, struct got_fileindex_entry *ie)
9111 struct report_file_info_arg *a = arg;
9112 struct got_pathlist_entry *pe;
9113 struct got_object_id blob_id, staged_blob_id, commit_id;
9114 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9115 struct got_object_id *commit_idp = NULL;
9116 int stage;
9118 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9119 return got_error(GOT_ERR_CANCELLED);
9121 TAILQ_FOREACH(pe, a->paths, entry) {
9122 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9123 got_path_is_child(ie->path, pe->path, pe->path_len))
9124 break;
9126 if (pe == NULL) /* not found */
9127 return NULL;
9129 if (got_fileindex_entry_has_blob(ie)) {
9130 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
9131 blob_idp = &blob_id;
9133 stage = got_fileindex_entry_stage_get(ie);
9134 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9135 stage == GOT_FILEIDX_STAGE_ADD) {
9136 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
9137 SHA1_DIGEST_LENGTH);
9138 staged_blob_idp = &staged_blob_id;
9141 if (got_fileindex_entry_has_commit(ie)) {
9142 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
9143 commit_idp = &commit_id;
9146 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9147 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9150 const struct got_error *
9151 got_worktree_path_info(struct got_worktree *worktree,
9152 struct got_pathlist_head *paths,
9153 got_worktree_path_info_cb info_cb, void *info_arg,
9154 got_cancel_cb cancel_cb, void *cancel_arg)
9157 const struct got_error *err = NULL, *unlockerr;
9158 struct got_fileindex *fileindex = NULL;
9159 char *fileindex_path = NULL;
9160 struct report_file_info_arg arg;
9162 err = lock_worktree(worktree, LOCK_SH);
9163 if (err)
9164 return err;
9166 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9167 if (err)
9168 goto done;
9170 arg.worktree = worktree;
9171 arg.info_cb = info_cb;
9172 arg.info_arg = info_arg;
9173 arg.paths = paths;
9174 arg.cancel_cb = cancel_cb;
9175 arg.cancel_arg = cancel_arg;
9176 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9177 &arg);
9178 done:
9179 free(fileindex_path);
9180 if (fileindex)
9181 got_fileindex_free(fileindex);
9182 unlockerr = lock_worktree(worktree, LOCK_UN);
9183 if (unlockerr && err == NULL)
9184 err = unlockerr;
9185 return err;
9188 static const struct got_error *
9189 patch_check_path(const char *p, char **path, unsigned char *status,
9190 unsigned char *staged_status, struct got_fileindex *fileindex,
9191 struct got_worktree *worktree, struct got_repository *repo)
9193 const struct got_error *err;
9194 struct got_fileindex_entry *ie;
9195 struct stat sb;
9196 char *ondisk_path = NULL;
9198 err = got_worktree_resolve_path(path, worktree, p);
9199 if (err)
9200 return err;
9202 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9203 *path[0] ? "/" : "", *path) == -1)
9204 return got_error_from_errno("asprintf");
9206 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9207 if (ie) {
9208 *staged_status = get_staged_status(ie);
9209 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9210 repo);
9211 if (err)
9212 goto done;
9213 } else {
9214 *staged_status = GOT_STATUS_NO_CHANGE;
9215 *status = GOT_STATUS_UNVERSIONED;
9216 if (lstat(ondisk_path, &sb) == -1) {
9217 if (errno != ENOENT) {
9218 err = got_error_from_errno2("lstat",
9219 ondisk_path);
9220 goto done;
9222 *status = GOT_STATUS_NONEXISTENT;
9226 done:
9227 free(ondisk_path);
9228 return err;
9231 static const struct got_error *
9232 patch_can_rm(const char *path, unsigned char status,
9233 unsigned char staged_status)
9235 if (status == GOT_STATUS_NONEXISTENT)
9236 return got_error_set_errno(ENOENT, path);
9237 if (status != GOT_STATUS_NO_CHANGE &&
9238 status != GOT_STATUS_ADD &&
9239 status != GOT_STATUS_MODIFY &&
9240 status != GOT_STATUS_MODE_CHANGE)
9241 return got_error_path(path, GOT_ERR_FILE_STATUS);
9242 if (staged_status == GOT_STATUS_DELETE)
9243 return got_error_path(path, GOT_ERR_FILE_STATUS);
9244 return NULL;
9247 static const struct got_error *
9248 patch_can_add(const char *path, unsigned char status)
9250 if (status != GOT_STATUS_NONEXISTENT)
9251 return got_error_path(path, GOT_ERR_FILE_STATUS);
9252 return NULL;
9255 static const struct got_error *
9256 patch_can_edit(const char *path, unsigned char status,
9257 unsigned char staged_status)
9259 if (status == GOT_STATUS_NONEXISTENT)
9260 return got_error_set_errno(ENOENT, path);
9261 if (status != GOT_STATUS_NO_CHANGE &&
9262 status != GOT_STATUS_ADD &&
9263 status != GOT_STATUS_MODIFY)
9264 return got_error_path(path, GOT_ERR_FILE_STATUS);
9265 if (staged_status == GOT_STATUS_DELETE)
9266 return got_error_path(path, GOT_ERR_FILE_STATUS);
9267 return NULL;
9270 const struct got_error *
9271 got_worktree_patch_prepare(struct got_fileindex **fileindex,
9272 char **fileindex_path, struct got_worktree *worktree)
9274 return open_fileindex(fileindex, fileindex_path, worktree);
9277 const struct got_error *
9278 got_worktree_patch_check_path(const char *old, const char *new,
9279 char **oldpath, char **newpath, struct got_worktree *worktree,
9280 struct got_repository *repo, struct got_fileindex *fileindex)
9282 const struct got_error *err = NULL;
9283 int file_renamed = 0;
9284 unsigned char status_old, staged_status_old;
9285 unsigned char status_new, staged_status_new;
9287 *oldpath = NULL;
9288 *newpath = NULL;
9290 err = patch_check_path(old != NULL ? old : new, oldpath,
9291 &status_old, &staged_status_old, fileindex, worktree, repo);
9292 if (err)
9293 goto done;
9295 err = patch_check_path(new != NULL ? new : old, newpath,
9296 &status_new, &staged_status_new, fileindex, worktree, repo);
9297 if (err)
9298 goto done;
9300 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9301 file_renamed = 1;
9303 if (old != NULL && new == NULL)
9304 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9305 else if (file_renamed) {
9306 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9307 if (err == NULL)
9308 err = patch_can_add(*newpath, status_new);
9309 } else if (old == NULL)
9310 err = patch_can_add(*newpath, status_new);
9311 else
9312 err = patch_can_edit(*newpath, status_new, staged_status_new);
9314 done:
9315 if (err) {
9316 free(*oldpath);
9317 *oldpath = NULL;
9318 free(*newpath);
9319 *newpath = NULL;
9321 return err;
9324 const struct got_error *
9325 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9326 struct got_worktree *worktree, struct got_fileindex *fileindex,
9327 got_worktree_checkout_cb progress_cb, void *progress_arg)
9329 struct schedule_addition_args saa;
9331 memset(&saa, 0, sizeof(saa));
9332 saa.worktree = worktree;
9333 saa.fileindex = fileindex;
9334 saa.progress_cb = progress_cb;
9335 saa.progress_arg = progress_arg;
9336 saa.repo = repo;
9338 return worktree_status(worktree, path, fileindex, repo,
9339 schedule_addition, &saa, NULL, NULL, 1, 0);
9342 const struct got_error *
9343 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9344 struct got_worktree *worktree, struct got_fileindex *fileindex,
9345 got_worktree_delete_cb progress_cb, void *progress_arg)
9347 struct schedule_deletion_args sda;
9349 memset(&sda, 0, sizeof(sda));
9350 sda.worktree = worktree;
9351 sda.fileindex = fileindex;
9352 sda.progress_cb = progress_cb;
9353 sda.progress_arg = progress_arg;
9354 sda.repo = repo;
9355 sda.delete_local_mods = 0;
9356 sda.keep_on_disk = 0;
9357 sda.ignore_missing_paths = 0;
9358 sda.status_codes = NULL;
9360 return worktree_status(worktree, path, fileindex, repo,
9361 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9364 const struct got_error *
9365 got_worktree_patch_complete(struct got_fileindex *fileindex,
9366 const char *fileindex_path)
9368 const struct got_error *err = NULL;
9370 err = sync_fileindex(fileindex, fileindex_path);
9371 got_fileindex_free(fileindex);
9373 return err;