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;
1515 * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
1516 * conflict marker is found in newly added lines only.
1518 static const struct got_error *
1519 get_modified_file_content_status(unsigned char *status,
1520 struct got_blob_object *blob, const char *path, struct stat *sb,
1521 FILE *ondisk_file)
1523 const struct got_error *err = NULL;
1524 const char *markers[3] = {
1525 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1526 GOT_DIFF_CONFLICT_MARKER_SEP,
1527 GOT_DIFF_CONFLICT_MARKER_END
1529 FILE *f = NULL, *f1 = NULL;
1530 int i = 0;
1531 char *line = NULL;
1532 size_t linesize = 0;
1533 ssize_t linelen;
1534 off_t sz1 = 0;
1536 if (*status == GOT_STATUS_MODIFY) {
1537 f = got_opentemp();
1538 if (f == NULL)
1539 return got_error_from_errno("got_opentemp");
1541 f1 = got_opentemp();
1542 if (f1 == NULL) {
1543 err = got_error_from_errno("got_opentemp");
1544 goto done;
1547 if (blob) {
1548 err = got_object_blob_dump_to_file(&sz1, NULL, NULL,
1549 f1, blob);
1550 if (err)
1551 goto done;
1554 err = got_diff_blob_file(blob, f1, sz1, NULL, ondisk_file, 1,
1555 sb, path, GOT_DIFF_ALGORITHM_MYERS, 0, 0, 0, NULL, f);
1556 if (err)
1557 goto done;
1559 if (fflush(f) == EOF) {
1560 err = got_error_from_errno("fflush");
1561 goto done;
1563 if (fseeko(f, 0L, SEEK_SET) == -1) {
1564 err = got_error_from_errno("fseek");
1565 goto done;
1569 while (*status == GOT_STATUS_MODIFY) {
1570 linelen = getline(&line, &linesize, f);
1571 if (linelen == -1) {
1572 if (feof(f))
1573 break;
1574 err = got_ferror(f, GOT_ERR_IO);
1575 break;
1578 if (*line == '+' &&
1579 strncmp(line + 1, markers[i], strlen(markers[i])) == 0) {
1580 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1581 == 0)
1582 *status = GOT_STATUS_CONFLICT;
1583 else
1584 i++;
1588 done:
1589 free(line);
1590 if (f != NULL && fclose(f) == EOF && err == NULL)
1591 err = got_error_from_errno("fclose");
1592 if (f1 != NULL && fclose(f1) == EOF && err == NULL)
1593 err = got_error_from_errno("fclose");
1595 return err;
1598 static int
1599 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1601 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1602 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1605 static int
1606 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1608 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1609 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1610 ie->mtime_sec == sb->st_mtim.tv_sec &&
1611 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1612 ie->size == (sb->st_size & 0xffffffff) &&
1613 !xbit_differs(ie, sb->st_mode));
1616 static unsigned char
1617 get_staged_status(struct got_fileindex_entry *ie)
1619 switch (got_fileindex_entry_stage_get(ie)) {
1620 case GOT_FILEIDX_STAGE_ADD:
1621 return GOT_STATUS_ADD;
1622 case GOT_FILEIDX_STAGE_DELETE:
1623 return GOT_STATUS_DELETE;
1624 case GOT_FILEIDX_STAGE_MODIFY:
1625 return GOT_STATUS_MODIFY;
1626 default:
1627 return GOT_STATUS_NO_CHANGE;
1631 static const struct got_error *
1632 get_symlink_modification_status(unsigned char *status,
1633 struct got_fileindex_entry *ie, const char *abspath,
1634 int dirfd, const char *de_name, struct got_blob_object *blob)
1636 const struct got_error *err = NULL;
1637 char target_path[PATH_MAX];
1638 char etarget[PATH_MAX];
1639 ssize_t elen;
1640 size_t len, target_len = 0;
1641 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1642 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1644 *status = GOT_STATUS_NO_CHANGE;
1646 /* Blob object content specifies the target path of the link. */
1647 do {
1648 err = got_object_blob_read_block(&len, blob);
1649 if (err)
1650 return err;
1651 if (len + target_len >= sizeof(target_path)) {
1653 * Should not happen. The blob contents were OK
1654 * when this symlink was installed.
1656 return got_error(GOT_ERR_NO_SPACE);
1658 if (len > 0) {
1659 /* Skip blob object header first time around. */
1660 memcpy(target_path + target_len, buf + hdrlen,
1661 len - hdrlen);
1662 target_len += len - hdrlen;
1663 hdrlen = 0;
1665 } while (len != 0);
1666 target_path[target_len] = '\0';
1668 if (dirfd != -1) {
1669 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1670 if (elen == -1)
1671 return got_error_from_errno2("readlinkat", abspath);
1672 } else {
1673 elen = readlink(abspath, etarget, sizeof(etarget));
1674 if (elen == -1)
1675 return got_error_from_errno2("readlink", abspath);
1678 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1679 *status = GOT_STATUS_MODIFY;
1681 return NULL;
1684 static const struct got_error *
1685 get_file_status(unsigned char *status, struct stat *sb,
1686 struct got_fileindex_entry *ie, const char *abspath,
1687 int dirfd, const char *de_name, struct got_repository *repo)
1689 const struct got_error *err = NULL;
1690 struct got_object_id id;
1691 size_t hdrlen;
1692 int fd = -1, fd1 = -1;
1693 FILE *f = NULL;
1694 uint8_t fbuf[8192];
1695 struct got_blob_object *blob = NULL;
1696 size_t flen, blen;
1697 unsigned char staged_status;
1699 staged_status = get_staged_status(ie);
1700 *status = GOT_STATUS_NO_CHANGE;
1701 memset(sb, 0, sizeof(*sb));
1704 * Whenever the caller provides a directory descriptor and a
1705 * directory entry name for the file, use them! This prevents
1706 * race conditions if filesystem paths change beneath our feet.
1708 if (dirfd != -1) {
1709 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1710 if (errno == ENOENT) {
1711 if (got_fileindex_entry_has_file_on_disk(ie))
1712 *status = GOT_STATUS_MISSING;
1713 else
1714 *status = GOT_STATUS_DELETE;
1715 goto done;
1717 err = got_error_from_errno2("fstatat", abspath);
1718 goto done;
1720 } else {
1721 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1722 if (fd == -1 && errno != ENOENT &&
1723 !got_err_open_nofollow_on_symlink())
1724 return got_error_from_errno2("open", abspath);
1725 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1726 if (lstat(abspath, sb) == -1)
1727 return got_error_from_errno2("lstat", abspath);
1728 } else if (fd == -1 || fstat(fd, sb) == -1) {
1729 if (errno == ENOENT) {
1730 if (got_fileindex_entry_has_file_on_disk(ie))
1731 *status = GOT_STATUS_MISSING;
1732 else
1733 *status = GOT_STATUS_DELETE;
1734 goto done;
1736 err = got_error_from_errno2("fstat", abspath);
1737 goto done;
1741 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1742 *status = GOT_STATUS_OBSTRUCTED;
1743 goto done;
1746 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1747 *status = GOT_STATUS_DELETE;
1748 goto done;
1749 } else if (!got_fileindex_entry_has_blob(ie) &&
1750 staged_status != GOT_STATUS_ADD) {
1751 *status = GOT_STATUS_ADD;
1752 goto done;
1755 if (!stat_info_differs(ie, sb))
1756 goto done;
1758 if (S_ISLNK(sb->st_mode) &&
1759 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1760 *status = GOT_STATUS_MODIFY;
1761 goto done;
1764 if (staged_status == GOT_STATUS_MODIFY ||
1765 staged_status == GOT_STATUS_ADD)
1766 got_fileindex_entry_get_staged_blob_id(&id, ie);
1767 else
1768 got_fileindex_entry_get_blob_id(&id, ie);
1770 fd1 = got_opentempfd();
1771 if (fd1 == -1) {
1772 err = got_error_from_errno("got_opentempfd");
1773 goto done;
1775 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1776 if (err)
1777 goto done;
1779 if (S_ISLNK(sb->st_mode)) {
1780 err = get_symlink_modification_status(status, ie,
1781 abspath, dirfd, de_name, blob);
1782 goto done;
1785 if (dirfd != -1) {
1786 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1787 if (fd == -1) {
1788 err = got_error_from_errno2("openat", abspath);
1789 goto done;
1793 f = fdopen(fd, "r");
1794 if (f == NULL) {
1795 err = got_error_from_errno2("fdopen", abspath);
1796 goto done;
1798 fd = -1;
1799 hdrlen = got_object_blob_get_hdrlen(blob);
1800 for (;;) {
1801 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1802 err = got_object_blob_read_block(&blen, blob);
1803 if (err)
1804 goto done;
1805 /* Skip length of blob object header first time around. */
1806 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1807 if (flen == 0 && ferror(f)) {
1808 err = got_error_from_errno("fread");
1809 goto done;
1811 if (blen - hdrlen == 0) {
1812 if (flen != 0)
1813 *status = GOT_STATUS_MODIFY;
1814 break;
1815 } else if (flen == 0) {
1816 if (blen - hdrlen != 0)
1817 *status = GOT_STATUS_MODIFY;
1818 break;
1819 } else if (blen - hdrlen == flen) {
1820 /* Skip blob object header first time around. */
1821 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1822 *status = GOT_STATUS_MODIFY;
1823 break;
1825 } else {
1826 *status = GOT_STATUS_MODIFY;
1827 break;
1829 hdrlen = 0;
1832 if (*status == GOT_STATUS_MODIFY) {
1833 rewind(f);
1834 err = get_modified_file_content_status(status, blob, ie->path,
1835 sb, f);
1836 } else if (xbit_differs(ie, sb->st_mode))
1837 *status = GOT_STATUS_MODE_CHANGE;
1838 done:
1839 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1840 err = got_error_from_errno("close");
1841 if (blob)
1842 got_object_blob_close(blob);
1843 if (f != NULL && fclose(f) == EOF && err == NULL)
1844 err = got_error_from_errno2("fclose", abspath);
1845 if (fd != -1 && close(fd) == -1 && err == NULL)
1846 err = got_error_from_errno2("close", abspath);
1847 return err;
1851 * Update timestamps in the file index if a file is unmodified and
1852 * we had to run a full content comparison to find out.
1854 static const struct got_error *
1855 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1856 struct got_fileindex_entry *ie, struct stat *sb)
1858 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1859 return got_fileindex_entry_update(ie, wt_fd, path,
1860 ie->blob_sha1, ie->commit_sha1, 1);
1862 return NULL;
1865 static const struct got_error *
1866 update_blob(struct got_worktree *worktree,
1867 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1868 struct got_tree_entry *te, const char *path,
1869 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1870 void *progress_arg)
1872 const struct got_error *err = NULL;
1873 struct got_blob_object *blob = NULL;
1874 char *ondisk_path = NULL;
1875 unsigned char status = GOT_STATUS_NO_CHANGE;
1876 struct stat sb;
1877 int fd1 = -1, fd2 = -1;
1879 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1880 return got_error_from_errno("asprintf");
1882 if (ie) {
1883 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1884 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1885 goto done;
1887 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1888 repo);
1889 if (err)
1890 goto done;
1891 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1892 sb.st_mode = got_fileindex_perms_to_st(ie);
1893 } else {
1894 if (stat(ondisk_path, &sb) == -1) {
1895 if (errno != ENOENT) {
1896 err = got_error_from_errno2("stat",
1897 ondisk_path);
1898 goto done;
1900 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1901 status = GOT_STATUS_UNVERSIONED;
1902 } else {
1903 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1904 status = GOT_STATUS_UNVERSIONED;
1905 else
1906 status = GOT_STATUS_OBSTRUCTED;
1910 if (status == GOT_STATUS_OBSTRUCTED) {
1911 if (ie)
1912 got_fileindex_entry_mark_skipped(ie);
1913 err = (*progress_cb)(progress_arg, status, path);
1914 goto done;
1916 if (status == GOT_STATUS_CONFLICT) {
1917 if (ie)
1918 got_fileindex_entry_mark_skipped(ie);
1919 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1920 path);
1921 goto done;
1924 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1925 (S_ISLNK(te->mode) ||
1926 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1928 * This is a regular file or an installed bad symlink.
1929 * If the file index indicates that this file is already
1930 * up-to-date with respect to the repository we can skip
1931 * updating contents of this file.
1933 if (got_fileindex_entry_has_commit(ie) &&
1934 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1935 SHA1_DIGEST_LENGTH) == 0) {
1936 /* Same commit. */
1937 err = sync_timestamps(worktree->root_fd,
1938 path, status, ie, &sb);
1939 if (err)
1940 goto done;
1941 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1942 path);
1943 goto done;
1945 if (got_fileindex_entry_has_blob(ie) &&
1946 memcmp(ie->blob_sha1, te->id.sha1,
1947 SHA1_DIGEST_LENGTH) == 0) {
1948 /* Different commit but the same blob. */
1949 err = sync_timestamps(worktree->root_fd,
1950 path, status, ie, &sb);
1951 if (err)
1952 goto done;
1953 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1954 path);
1955 goto done;
1959 fd1 = got_opentempfd();
1960 if (fd1 == -1) {
1961 err = got_error_from_errno("got_opentempfd");
1962 goto done;
1964 err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
1965 if (err)
1966 goto done;
1968 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1969 int update_timestamps;
1970 struct got_blob_object *blob2 = NULL;
1971 char *label_orig = NULL;
1972 if (got_fileindex_entry_has_blob(ie)) {
1973 fd2 = got_opentempfd();
1974 if (fd2 == -1) {
1975 err = got_error_from_errno("got_opentempfd");
1976 goto done;
1978 struct got_object_id id2;
1979 got_fileindex_entry_get_blob_id(&id2, ie);
1980 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
1981 fd2);
1982 if (err)
1983 goto done;
1985 if (got_fileindex_entry_has_commit(ie)) {
1986 char id_str[SHA1_DIGEST_STRING_LENGTH];
1987 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1988 sizeof(id_str)) == NULL) {
1989 err = got_error_path(id_str,
1990 GOT_ERR_BAD_OBJ_ID_STR);
1991 goto done;
1993 if (asprintf(&label_orig, "%s: commit %s",
1994 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1995 err = got_error_from_errno("asprintf");
1996 goto done;
1999 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2000 char *link_target;
2001 err = got_object_blob_read_to_str(&link_target, blob);
2002 if (err)
2003 goto done;
2004 err = merge_symlink(worktree, blob2, ondisk_path, path,
2005 label_orig, link_target, worktree->base_commit_id,
2006 repo, progress_cb, progress_arg);
2007 free(link_target);
2008 } else {
2009 err = merge_blob(&update_timestamps, worktree, blob2,
2010 ondisk_path, path, sb.st_mode, label_orig, blob,
2011 worktree->base_commit_id, repo,
2012 progress_cb, progress_arg);
2014 free(label_orig);
2015 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2016 err = got_error_from_errno("close");
2017 goto done;
2019 if (blob2)
2020 got_object_blob_close(blob2);
2021 if (err)
2022 goto done;
2024 * Do not update timestamps of files with local changes.
2025 * Otherwise, a future status walk would treat them as
2026 * unmodified files again.
2028 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2029 blob->id.sha1, worktree->base_commit_id->sha1,
2030 update_timestamps);
2031 } else if (status == GOT_STATUS_MODE_CHANGE) {
2032 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2033 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2034 } else if (status == GOT_STATUS_DELETE) {
2035 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2036 if (err)
2037 goto done;
2038 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2039 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2040 if (err)
2041 goto done;
2042 } else {
2043 int is_bad_symlink = 0;
2044 if (S_ISLNK(te->mode)) {
2045 err = install_symlink(&is_bad_symlink, worktree,
2046 ondisk_path, path, blob,
2047 status == GOT_STATUS_MISSING, 0,
2048 status == GOT_STATUS_UNVERSIONED, 0,
2049 repo, progress_cb, progress_arg);
2050 } else {
2051 err = install_blob(worktree, ondisk_path, path,
2052 te->mode, sb.st_mode, blob,
2053 status == GOT_STATUS_MISSING, 0, 0,
2054 status == GOT_STATUS_UNVERSIONED, repo,
2055 progress_cb, progress_arg);
2057 if (err)
2058 goto done;
2060 if (ie) {
2061 err = got_fileindex_entry_update(ie,
2062 worktree->root_fd, path, blob->id.sha1,
2063 worktree->base_commit_id->sha1, 1);
2064 } else {
2065 err = create_fileindex_entry(&ie, fileindex,
2066 worktree->base_commit_id, worktree->root_fd, path,
2067 &blob->id);
2069 if (err)
2070 goto done;
2072 if (is_bad_symlink) {
2073 got_fileindex_entry_filetype_set(ie,
2074 GOT_FILEIDX_MODE_BAD_SYMLINK);
2078 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2079 err = got_error_from_errno("close");
2080 goto done;
2082 got_object_blob_close(blob);
2083 done:
2084 free(ondisk_path);
2085 return err;
2088 static const struct got_error *
2089 remove_ondisk_file(const char *root_path, const char *path)
2091 const struct got_error *err = NULL;
2092 char *ondisk_path = NULL, *parent = NULL;
2094 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2095 return got_error_from_errno("asprintf");
2097 if (unlink(ondisk_path) == -1) {
2098 if (errno != ENOENT)
2099 err = got_error_from_errno2("unlink", ondisk_path);
2100 } else {
2101 size_t root_len = strlen(root_path);
2102 err = got_path_dirname(&parent, ondisk_path);
2103 if (err)
2104 goto done;
2105 while (got_path_cmp(parent, root_path,
2106 strlen(parent), root_len) != 0) {
2107 free(ondisk_path);
2108 ondisk_path = parent;
2109 parent = NULL;
2110 if (rmdir(ondisk_path) == -1) {
2111 if (errno != ENOTEMPTY)
2112 err = got_error_from_errno2("rmdir",
2113 ondisk_path);
2114 break;
2116 err = got_path_dirname(&parent, ondisk_path);
2117 if (err)
2118 break;
2121 done:
2122 free(ondisk_path);
2123 free(parent);
2124 return err;
2127 static const struct got_error *
2128 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2129 struct got_fileindex_entry *ie, struct got_repository *repo,
2130 got_worktree_checkout_cb progress_cb, void *progress_arg)
2132 const struct got_error *err = NULL;
2133 unsigned char status;
2134 struct stat sb;
2135 char *ondisk_path;
2137 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2138 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2140 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2141 == -1)
2142 return got_error_from_errno("asprintf");
2144 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2145 if (err)
2146 goto done;
2148 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2149 char ondisk_target[PATH_MAX];
2150 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2151 sizeof(ondisk_target));
2152 if (ondisk_len == -1) {
2153 err = got_error_from_errno2("readlink", ondisk_path);
2154 goto done;
2156 ondisk_target[ondisk_len] = '\0';
2157 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2158 NULL, NULL, /* XXX pass common ancestor info? */
2159 ondisk_target, ondisk_path);
2160 if (err)
2161 goto done;
2162 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2163 ie->path);
2164 goto done;
2167 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2168 status == GOT_STATUS_ADD) {
2169 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2170 if (err)
2171 goto done;
2173 * Preserve the working file and change the deleted blob's
2174 * entry into a schedule-add entry.
2176 err = got_fileindex_entry_update(ie, worktree->root_fd,
2177 ie->path, NULL, NULL, 0);
2178 } else {
2179 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2180 if (err)
2181 goto done;
2182 if (status == GOT_STATUS_NO_CHANGE) {
2183 err = remove_ondisk_file(worktree->root_path, ie->path);
2184 if (err)
2185 goto done;
2187 got_fileindex_entry_remove(fileindex, ie);
2189 done:
2190 free(ondisk_path);
2191 return err;
2194 struct diff_cb_arg {
2195 struct got_fileindex *fileindex;
2196 struct got_worktree *worktree;
2197 struct got_repository *repo;
2198 got_worktree_checkout_cb progress_cb;
2199 void *progress_arg;
2200 got_cancel_cb cancel_cb;
2201 void *cancel_arg;
2204 static const struct got_error *
2205 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2206 struct got_tree_entry *te, const char *parent_path)
2208 struct diff_cb_arg *a = arg;
2210 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2211 return got_error(GOT_ERR_CANCELLED);
2213 return update_blob(a->worktree, a->fileindex, ie, te,
2214 ie->path, a->repo, a->progress_cb, a->progress_arg);
2217 static const struct got_error *
2218 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2220 struct diff_cb_arg *a = arg;
2222 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2223 return got_error(GOT_ERR_CANCELLED);
2225 return delete_blob(a->worktree, a->fileindex, ie,
2226 a->repo, a->progress_cb, a->progress_arg);
2229 static const struct got_error *
2230 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2232 struct diff_cb_arg *a = arg;
2233 const struct got_error *err;
2234 char *path;
2236 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2237 return got_error(GOT_ERR_CANCELLED);
2239 if (got_object_tree_entry_is_submodule(te))
2240 return NULL;
2242 if (asprintf(&path, "%s%s%s", parent_path,
2243 parent_path[0] ? "/" : "", te->name)
2244 == -1)
2245 return got_error_from_errno("asprintf");
2247 if (S_ISDIR(te->mode))
2248 err = add_dir_on_disk(a->worktree, path);
2249 else
2250 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2251 a->repo, a->progress_cb, a->progress_arg);
2253 free(path);
2254 return err;
2257 const struct got_error *
2258 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2260 uint32_t uuid_status;
2262 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2263 if (uuid_status != uuid_s_ok) {
2264 *uuidstr = NULL;
2265 return got_error_uuid(uuid_status, "uuid_to_string");
2268 return NULL;
2271 static const struct got_error *
2272 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2274 const struct got_error *err = NULL;
2275 char *uuidstr = NULL;
2277 *refname = NULL;
2279 err = got_worktree_get_uuid(&uuidstr, worktree);
2280 if (err)
2281 return err;
2283 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2284 err = got_error_from_errno("asprintf");
2285 *refname = NULL;
2287 free(uuidstr);
2288 return err;
2291 const struct got_error *
2292 got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2293 const char *prefix)
2295 return get_ref_name(refname, worktree, prefix);
2298 const struct got_error *
2299 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2301 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2304 static const struct got_error *
2305 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2307 return get_ref_name(refname, worktree,
2308 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2311 static const struct got_error *
2312 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2314 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2317 static const struct got_error *
2318 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2320 return get_ref_name(refname, worktree,
2321 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2324 static const struct got_error *
2325 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2327 return get_ref_name(refname, worktree,
2328 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2331 static const struct got_error *
2332 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2334 return get_ref_name(refname, worktree,
2335 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2338 static const struct got_error *
2339 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2341 return get_ref_name(refname, worktree,
2342 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2345 static const struct got_error *
2346 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2348 return get_ref_name(refname, worktree,
2349 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2352 static const struct got_error *
2353 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2355 return get_ref_name(refname, worktree,
2356 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2359 const struct got_error *
2360 got_worktree_get_histedit_script_path(char **path,
2361 struct got_worktree *worktree)
2363 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2364 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2365 *path = NULL;
2366 return got_error_from_errno("asprintf");
2368 return NULL;
2371 static const struct got_error *
2372 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2374 return get_ref_name(refname, worktree,
2375 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2378 static const struct got_error *
2379 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2381 return get_ref_name(refname, worktree,
2382 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2386 * Prevent Git's garbage collector from deleting our base commit by
2387 * setting a reference to our base commit's ID.
2389 static const struct got_error *
2390 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2392 const struct got_error *err = NULL;
2393 struct got_reference *ref = NULL;
2394 char *refname;
2396 err = got_worktree_get_base_ref_name(&refname, worktree);
2397 if (err)
2398 return err;
2400 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2401 if (err)
2402 goto done;
2404 err = got_ref_write(ref, repo);
2405 done:
2406 free(refname);
2407 if (ref)
2408 got_ref_close(ref);
2409 return err;
2412 static const struct got_error *
2413 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2415 const struct got_error *err = NULL;
2417 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2418 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2419 err = got_error_from_errno("asprintf");
2420 *fileindex_path = NULL;
2422 return err;
2426 static const struct got_error *
2427 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2428 struct got_worktree *worktree)
2430 const struct got_error *err = NULL;
2431 FILE *index = NULL;
2433 *fileindex_path = NULL;
2434 *fileindex = got_fileindex_alloc();
2435 if (*fileindex == NULL)
2436 return got_error_from_errno("got_fileindex_alloc");
2438 err = get_fileindex_path(fileindex_path, worktree);
2439 if (err)
2440 goto done;
2442 index = fopen(*fileindex_path, "rbe");
2443 if (index == NULL) {
2444 if (errno != ENOENT)
2445 err = got_error_from_errno2("fopen", *fileindex_path);
2446 } else {
2447 err = got_fileindex_read(*fileindex, index);
2448 if (fclose(index) == EOF && err == NULL)
2449 err = got_error_from_errno("fclose");
2451 done:
2452 if (err) {
2453 free(*fileindex_path);
2454 *fileindex_path = NULL;
2455 got_fileindex_free(*fileindex);
2456 *fileindex = NULL;
2458 return err;
2461 struct bump_base_commit_id_arg {
2462 struct got_object_id *base_commit_id;
2463 const char *path;
2464 size_t path_len;
2465 const char *entry_name;
2466 got_worktree_checkout_cb progress_cb;
2467 void *progress_arg;
2470 static const struct got_error *
2471 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2473 const struct got_error *err;
2474 struct bump_base_commit_id_arg *a = arg;
2476 if (a->entry_name) {
2477 if (strcmp(ie->path, a->path) != 0)
2478 return NULL;
2479 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2480 return NULL;
2482 if (got_fileindex_entry_was_skipped(ie))
2483 return NULL;
2485 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2486 SHA1_DIGEST_LENGTH) == 0)
2487 return NULL;
2489 if (a->progress_cb) {
2490 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2491 ie->path);
2492 if (err)
2493 return err;
2495 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2496 return NULL;
2499 /* Bump base commit ID of all files within an updated part of the work tree. */
2500 static const struct got_error *
2501 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2502 struct got_fileindex *fileindex,
2503 got_worktree_checkout_cb progress_cb, void *progress_arg)
2505 struct bump_base_commit_id_arg bbc_arg;
2507 bbc_arg.base_commit_id = worktree->base_commit_id;
2508 bbc_arg.entry_name = NULL;
2509 bbc_arg.path = "";
2510 bbc_arg.path_len = 0;
2511 bbc_arg.progress_cb = progress_cb;
2512 bbc_arg.progress_arg = progress_arg;
2514 return got_fileindex_for_each_entry_safe(fileindex,
2515 bump_base_commit_id, &bbc_arg);
2518 static const struct got_error *
2519 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2521 const struct got_error *err = NULL;
2522 char *new_fileindex_path = NULL;
2523 FILE *new_index = NULL;
2524 struct timespec timeout;
2526 err = got_opentemp_named(&new_fileindex_path, &new_index,
2527 fileindex_path, "");
2528 if (err)
2529 goto done;
2531 err = got_fileindex_write(fileindex, new_index);
2532 if (err)
2533 goto done;
2535 if (rename(new_fileindex_path, fileindex_path) != 0) {
2536 err = got_error_from_errno3("rename", new_fileindex_path,
2537 fileindex_path);
2538 unlink(new_fileindex_path);
2542 * Sleep for a short amount of time to ensure that files modified after
2543 * this program exits have a different time stamp from the one which
2544 * was recorded in the file index.
2546 timeout.tv_sec = 0;
2547 timeout.tv_nsec = 1;
2548 nanosleep(&timeout, NULL);
2549 done:
2550 if (new_index)
2551 fclose(new_index);
2552 free(new_fileindex_path);
2553 return err;
2556 static const struct got_error *
2557 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2558 struct got_object_id **tree_id, const char *wt_relpath,
2559 struct got_commit_object *base_commit, struct got_worktree *worktree,
2560 struct got_repository *repo)
2562 const struct got_error *err = NULL;
2563 struct got_object_id *id = NULL;
2564 char *in_repo_path = NULL;
2565 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2567 *entry_type = GOT_OBJ_TYPE_ANY;
2568 *tree_relpath = NULL;
2569 *tree_id = NULL;
2571 if (wt_relpath[0] == '\0') {
2572 /* Check out all files within the work tree. */
2573 *entry_type = GOT_OBJ_TYPE_TREE;
2574 *tree_relpath = strdup("");
2575 if (*tree_relpath == NULL) {
2576 err = got_error_from_errno("strdup");
2577 goto done;
2579 err = got_object_id_by_path(tree_id, repo, base_commit,
2580 worktree->path_prefix);
2581 if (err)
2582 goto done;
2583 return NULL;
2586 /* Check out a subset of files in the work tree. */
2588 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2589 is_root_wt ? "" : "/", wt_relpath) == -1) {
2590 err = got_error_from_errno("asprintf");
2591 goto done;
2594 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2595 if (err)
2596 goto done;
2598 free(in_repo_path);
2599 in_repo_path = NULL;
2601 err = got_object_get_type(entry_type, repo, id);
2602 if (err)
2603 goto done;
2605 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2606 /* Check out a single file. */
2607 if (strchr(wt_relpath, '/') == NULL) {
2608 /* Check out a single file in work tree's root dir. */
2609 in_repo_path = strdup(worktree->path_prefix);
2610 if (in_repo_path == NULL) {
2611 err = got_error_from_errno("strdup");
2612 goto done;
2614 *tree_relpath = strdup("");
2615 if (*tree_relpath == NULL) {
2616 err = got_error_from_errno("strdup");
2617 goto done;
2619 } else {
2620 /* Check out a single file in a subdirectory. */
2621 err = got_path_dirname(tree_relpath, wt_relpath);
2622 if (err)
2623 return err;
2624 if (asprintf(&in_repo_path, "%s%s%s",
2625 worktree->path_prefix, is_root_wt ? "" : "/",
2626 *tree_relpath) == -1) {
2627 err = got_error_from_errno("asprintf");
2628 goto done;
2631 err = got_object_id_by_path(tree_id, repo,
2632 base_commit, in_repo_path);
2633 } else {
2634 /* Check out all files within a subdirectory. */
2635 *tree_id = got_object_id_dup(id);
2636 if (*tree_id == NULL) {
2637 err = got_error_from_errno("got_object_id_dup");
2638 goto done;
2640 *tree_relpath = strdup(wt_relpath);
2641 if (*tree_relpath == NULL) {
2642 err = got_error_from_errno("strdup");
2643 goto done;
2646 done:
2647 free(id);
2648 free(in_repo_path);
2649 if (err) {
2650 *entry_type = GOT_OBJ_TYPE_ANY;
2651 free(*tree_relpath);
2652 *tree_relpath = NULL;
2653 free(*tree_id);
2654 *tree_id = NULL;
2656 return err;
2659 static const struct got_error *
2660 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2661 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2662 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2663 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2665 const struct got_error *err = NULL;
2666 struct got_commit_object *commit = NULL;
2667 struct got_tree_object *tree = NULL;
2668 struct got_fileindex_diff_tree_cb diff_cb;
2669 struct diff_cb_arg arg;
2671 err = ref_base_commit(worktree, repo);
2672 if (err) {
2673 if (!(err->code == GOT_ERR_ERRNO &&
2674 (errno == EACCES || errno == EROFS)))
2675 goto done;
2676 err = (*progress_cb)(progress_arg,
2677 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2678 if (err)
2679 return err;
2682 err = got_object_open_as_commit(&commit, repo,
2683 worktree->base_commit_id);
2684 if (err)
2685 goto done;
2687 err = got_object_open_as_tree(&tree, repo, tree_id);
2688 if (err)
2689 goto done;
2691 if (entry_name &&
2692 got_object_tree_find_entry(tree, entry_name) == NULL) {
2693 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2694 goto done;
2697 diff_cb.diff_old_new = diff_old_new;
2698 diff_cb.diff_old = diff_old;
2699 diff_cb.diff_new = diff_new;
2700 arg.fileindex = fileindex;
2701 arg.worktree = worktree;
2702 arg.repo = repo;
2703 arg.progress_cb = progress_cb;
2704 arg.progress_arg = progress_arg;
2705 arg.cancel_cb = cancel_cb;
2706 arg.cancel_arg = cancel_arg;
2707 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2708 entry_name, repo, &diff_cb, &arg);
2709 done:
2710 if (tree)
2711 got_object_tree_close(tree);
2712 if (commit)
2713 got_object_commit_close(commit);
2714 return err;
2717 const struct got_error *
2718 got_worktree_checkout_files(struct got_worktree *worktree,
2719 struct got_pathlist_head *paths, struct got_repository *repo,
2720 got_worktree_checkout_cb progress_cb, void *progress_arg,
2721 got_cancel_cb cancel_cb, void *cancel_arg)
2723 const struct got_error *err = NULL, *sync_err, *unlockerr;
2724 struct got_commit_object *commit = NULL;
2725 struct got_tree_object *tree = NULL;
2726 struct got_fileindex *fileindex = NULL;
2727 char *fileindex_path = NULL;
2728 struct got_pathlist_entry *pe;
2729 struct tree_path_data {
2730 STAILQ_ENTRY(tree_path_data) entry;
2731 struct got_object_id *tree_id;
2732 int entry_type;
2733 char *relpath;
2734 char *entry_name;
2735 } *tpd = NULL;
2736 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2738 STAILQ_INIT(&tree_paths);
2740 err = lock_worktree(worktree, LOCK_EX);
2741 if (err)
2742 return err;
2744 err = got_object_open_as_commit(&commit, repo,
2745 worktree->base_commit_id);
2746 if (err)
2747 goto done;
2749 /* Map all specified paths to in-repository trees. */
2750 TAILQ_FOREACH(pe, paths, entry) {
2751 tpd = malloc(sizeof(*tpd));
2752 if (tpd == NULL) {
2753 err = got_error_from_errno("malloc");
2754 goto done;
2757 err = find_tree_entry_for_checkout(&tpd->entry_type,
2758 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2759 worktree, repo);
2760 if (err) {
2761 free(tpd);
2762 goto done;
2765 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2766 err = got_path_basename(&tpd->entry_name, pe->path);
2767 if (err) {
2768 free(tpd->relpath);
2769 free(tpd->tree_id);
2770 free(tpd);
2771 goto done;
2773 } else
2774 tpd->entry_name = NULL;
2776 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2780 * Read the file index.
2781 * Checking out files is supposed to be an idempotent operation.
2782 * If the on-disk file index is incomplete we will try to complete it.
2784 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2785 if (err)
2786 goto done;
2788 tpd = STAILQ_FIRST(&tree_paths);
2789 TAILQ_FOREACH(pe, paths, entry) {
2790 struct bump_base_commit_id_arg bbc_arg;
2792 err = checkout_files(worktree, fileindex, tpd->relpath,
2793 tpd->tree_id, tpd->entry_name, repo,
2794 progress_cb, progress_arg, cancel_cb, cancel_arg);
2795 if (err)
2796 break;
2798 bbc_arg.base_commit_id = worktree->base_commit_id;
2799 bbc_arg.entry_name = tpd->entry_name;
2800 bbc_arg.path = pe->path;
2801 bbc_arg.path_len = pe->path_len;
2802 bbc_arg.progress_cb = progress_cb;
2803 bbc_arg.progress_arg = progress_arg;
2804 err = got_fileindex_for_each_entry_safe(fileindex,
2805 bump_base_commit_id, &bbc_arg);
2806 if (err)
2807 break;
2809 tpd = STAILQ_NEXT(tpd, entry);
2811 sync_err = sync_fileindex(fileindex, fileindex_path);
2812 if (sync_err && err == NULL)
2813 err = sync_err;
2814 done:
2815 free(fileindex_path);
2816 if (tree)
2817 got_object_tree_close(tree);
2818 if (commit)
2819 got_object_commit_close(commit);
2820 if (fileindex)
2821 got_fileindex_free(fileindex);
2822 while (!STAILQ_EMPTY(&tree_paths)) {
2823 tpd = STAILQ_FIRST(&tree_paths);
2824 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2825 free(tpd->relpath);
2826 free(tpd->tree_id);
2827 free(tpd);
2829 unlockerr = lock_worktree(worktree, LOCK_SH);
2830 if (unlockerr && err == NULL)
2831 err = unlockerr;
2832 return err;
2835 struct merge_file_cb_arg {
2836 struct got_worktree *worktree;
2837 struct got_fileindex *fileindex;
2838 got_worktree_checkout_cb progress_cb;
2839 void *progress_arg;
2840 got_cancel_cb cancel_cb;
2841 void *cancel_arg;
2842 const char *label_orig;
2843 struct got_object_id *commit_id2;
2844 int allow_bad_symlinks;
2847 static const struct got_error *
2848 merge_file_cb(void *arg, struct got_blob_object *blob1,
2849 struct got_blob_object *blob2, FILE *f1, FILE *f2,
2850 struct got_object_id *id1, struct got_object_id *id2,
2851 const char *path1, const char *path2,
2852 mode_t mode1, mode_t mode2, struct got_repository *repo)
2854 static const struct got_error *err = NULL;
2855 struct merge_file_cb_arg *a = arg;
2856 struct got_fileindex_entry *ie;
2857 char *ondisk_path = NULL;
2858 struct stat sb;
2859 unsigned char status;
2860 int local_changes_subsumed;
2861 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2862 char *id_str = NULL, *label_deriv2 = NULL;
2864 if (blob1 && blob2) {
2865 ie = got_fileindex_entry_get(a->fileindex, path2,
2866 strlen(path2));
2867 if (ie == NULL)
2868 return (*a->progress_cb)(a->progress_arg,
2869 GOT_STATUS_MISSING, path2);
2871 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2872 path2) == -1)
2873 return got_error_from_errno("asprintf");
2875 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2876 repo);
2877 if (err)
2878 goto done;
2880 if (status == GOT_STATUS_DELETE) {
2881 err = (*a->progress_cb)(a->progress_arg,
2882 GOT_STATUS_MERGE, path2);
2883 goto done;
2885 if (status != GOT_STATUS_NO_CHANGE &&
2886 status != GOT_STATUS_MODIFY &&
2887 status != GOT_STATUS_CONFLICT &&
2888 status != GOT_STATUS_ADD) {
2889 err = (*a->progress_cb)(a->progress_arg, status, path2);
2890 goto done;
2893 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2894 char *link_target2;
2895 err = got_object_blob_read_to_str(&link_target2, blob2);
2896 if (err)
2897 goto done;
2898 err = merge_symlink(a->worktree, blob1, ondisk_path,
2899 path2, a->label_orig, link_target2, a->commit_id2,
2900 repo, a->progress_cb, a->progress_arg);
2901 free(link_target2);
2902 } else {
2903 int fd;
2905 f_orig = got_opentemp();
2906 if (f_orig == NULL) {
2907 err = got_error_from_errno("got_opentemp");
2908 goto done;
2910 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2911 f_orig, blob1);
2912 if (err)
2913 goto done;
2915 f_deriv2 = got_opentemp();
2916 if (f_deriv2 == NULL)
2917 goto done;
2918 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2919 f_deriv2, blob2);
2920 if (err)
2921 goto done;
2923 fd = open(ondisk_path,
2924 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2925 if (fd == -1) {
2926 err = got_error_from_errno2("open",
2927 ondisk_path);
2928 goto done;
2930 f_deriv = fdopen(fd, "r");
2931 if (f_deriv == NULL) {
2932 err = got_error_from_errno2("fdopen",
2933 ondisk_path);
2934 close(fd);
2935 goto done;
2937 err = got_object_id_str(&id_str, a->commit_id2);
2938 if (err)
2939 goto done;
2940 if (asprintf(&label_deriv2, "%s: commit %s",
2941 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2942 err = got_error_from_errno("asprintf");
2943 goto done;
2945 err = merge_file(&local_changes_subsumed, a->worktree,
2946 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2947 mode2, a->label_orig, NULL, label_deriv2,
2948 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2949 a->progress_cb, a->progress_arg);
2951 } else if (blob1) {
2952 ie = got_fileindex_entry_get(a->fileindex, path1,
2953 strlen(path1));
2954 if (ie == NULL)
2955 return (*a->progress_cb)(a->progress_arg,
2956 GOT_STATUS_MISSING, path1);
2958 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2959 path1) == -1)
2960 return got_error_from_errno("asprintf");
2962 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2963 repo);
2964 if (err)
2965 goto done;
2967 switch (status) {
2968 case GOT_STATUS_NO_CHANGE:
2969 err = (*a->progress_cb)(a->progress_arg,
2970 GOT_STATUS_DELETE, path1);
2971 if (err)
2972 goto done;
2973 err = remove_ondisk_file(a->worktree->root_path, path1);
2974 if (err)
2975 goto done;
2976 if (ie)
2977 got_fileindex_entry_mark_deleted_from_disk(ie);
2978 break;
2979 case GOT_STATUS_DELETE:
2980 case GOT_STATUS_MISSING:
2981 err = (*a->progress_cb)(a->progress_arg,
2982 GOT_STATUS_DELETE, path1);
2983 if (err)
2984 goto done;
2985 if (ie)
2986 got_fileindex_entry_mark_deleted_from_disk(ie);
2987 break;
2988 case GOT_STATUS_ADD: {
2989 struct got_object_id *id;
2990 FILE *blob1_f;
2991 off_t blob1_size;
2993 * Delete the added file only if its content already
2994 * exists in the repository.
2996 err = got_object_blob_file_create(&id, &blob1_f,
2997 &blob1_size, path1);
2998 if (err)
2999 goto done;
3000 if (got_object_id_cmp(id, id1) == 0) {
3001 err = (*a->progress_cb)(a->progress_arg,
3002 GOT_STATUS_DELETE, path1);
3003 if (err)
3004 goto done;
3005 err = remove_ondisk_file(a->worktree->root_path,
3006 path1);
3007 if (err)
3008 goto done;
3009 if (ie)
3010 got_fileindex_entry_remove(a->fileindex,
3011 ie);
3012 } else {
3013 err = (*a->progress_cb)(a->progress_arg,
3014 GOT_STATUS_CANNOT_DELETE, path1);
3016 if (fclose(blob1_f) == EOF && err == NULL)
3017 err = got_error_from_errno("fclose");
3018 free(id);
3019 if (err)
3020 goto done;
3021 break;
3023 case GOT_STATUS_MODIFY:
3024 case GOT_STATUS_CONFLICT:
3025 err = (*a->progress_cb)(a->progress_arg,
3026 GOT_STATUS_CANNOT_DELETE, path1);
3027 if (err)
3028 goto done;
3029 break;
3030 case GOT_STATUS_OBSTRUCTED:
3031 err = (*a->progress_cb)(a->progress_arg, status, path1);
3032 if (err)
3033 goto done;
3034 break;
3035 default:
3036 break;
3038 } else if (blob2) {
3039 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3040 path2) == -1)
3041 return got_error_from_errno("asprintf");
3042 ie = got_fileindex_entry_get(a->fileindex, path2,
3043 strlen(path2));
3044 if (ie) {
3045 err = get_file_status(&status, &sb, ie, ondisk_path,
3046 -1, NULL, repo);
3047 if (err)
3048 goto done;
3049 if (status != GOT_STATUS_NO_CHANGE &&
3050 status != GOT_STATUS_MODIFY &&
3051 status != GOT_STATUS_CONFLICT &&
3052 status != GOT_STATUS_ADD) {
3053 err = (*a->progress_cb)(a->progress_arg,
3054 status, path2);
3055 goto done;
3057 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3058 char *link_target2;
3059 err = got_object_blob_read_to_str(&link_target2,
3060 blob2);
3061 if (err)
3062 goto done;
3063 err = merge_symlink(a->worktree, NULL,
3064 ondisk_path, path2, a->label_orig,
3065 link_target2, a->commit_id2, repo,
3066 a->progress_cb, a->progress_arg);
3067 free(link_target2);
3068 } else if (S_ISREG(sb.st_mode)) {
3069 err = merge_blob(&local_changes_subsumed,
3070 a->worktree, NULL, ondisk_path, path2,
3071 sb.st_mode, a->label_orig, blob2,
3072 a->commit_id2, repo, a->progress_cb,
3073 a->progress_arg);
3074 } else {
3075 err = got_error_path(ondisk_path,
3076 GOT_ERR_FILE_OBSTRUCTED);
3078 if (err)
3079 goto done;
3080 if (status == GOT_STATUS_DELETE) {
3081 err = got_fileindex_entry_update(ie,
3082 a->worktree->root_fd, path2, blob2->id.sha1,
3083 a->worktree->base_commit_id->sha1, 0);
3084 if (err)
3085 goto done;
3087 } else {
3088 int is_bad_symlink = 0;
3089 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3090 if (S_ISLNK(mode2)) {
3091 err = install_symlink(&is_bad_symlink,
3092 a->worktree, ondisk_path, path2, blob2, 0,
3093 0, 1, a->allow_bad_symlinks, repo,
3094 a->progress_cb, a->progress_arg);
3095 } else {
3096 err = install_blob(a->worktree, ondisk_path, path2,
3097 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3098 a->progress_cb, a->progress_arg);
3100 if (err)
3101 goto done;
3102 err = got_fileindex_entry_alloc(&ie, path2);
3103 if (err)
3104 goto done;
3105 err = got_fileindex_entry_update(ie,
3106 a->worktree->root_fd, path2, NULL, NULL, 1);
3107 if (err) {
3108 got_fileindex_entry_free(ie);
3109 goto done;
3111 err = got_fileindex_entry_add(a->fileindex, ie);
3112 if (err) {
3113 got_fileindex_entry_free(ie);
3114 goto done;
3116 if (is_bad_symlink) {
3117 got_fileindex_entry_filetype_set(ie,
3118 GOT_FILEIDX_MODE_BAD_SYMLINK);
3122 done:
3123 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3124 err = got_error_from_errno("fclose");
3125 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3126 err = got_error_from_errno("fclose");
3127 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3128 err = got_error_from_errno("fclose");
3129 free(id_str);
3130 free(label_deriv2);
3131 free(ondisk_path);
3132 return err;
3135 static const struct got_error *
3136 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3138 struct got_worktree *worktree = arg;
3140 /* Reject merges into a work tree with mixed base commits. */
3141 if (got_fileindex_entry_has_commit(ie) &&
3142 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3143 SHA1_DIGEST_LENGTH) != 0)
3144 return got_error(GOT_ERR_MIXED_COMMITS);
3146 return NULL;
3149 struct check_merge_conflicts_arg {
3150 struct got_worktree *worktree;
3151 struct got_fileindex *fileindex;
3152 struct got_repository *repo;
3155 static const struct got_error *
3156 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3157 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3158 struct got_object_id *id1, struct got_object_id *id2,
3159 const char *path1, const char *path2,
3160 mode_t mode1, mode_t mode2, struct got_repository *repo)
3162 const struct got_error *err = NULL;
3163 struct check_merge_conflicts_arg *a = arg;
3164 unsigned char status;
3165 struct stat sb;
3166 struct got_fileindex_entry *ie;
3167 const char *path = path2 ? path2 : path1;
3168 struct got_object_id *id = id2 ? id2 : id1;
3169 char *ondisk_path;
3171 if (id == NULL)
3172 return NULL;
3174 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3175 if (ie == NULL)
3176 return NULL;
3178 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3179 == -1)
3180 return got_error_from_errno("asprintf");
3182 /* Reject merges into a work tree with conflicted files. */
3183 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3184 free(ondisk_path);
3185 if (err)
3186 return err;
3187 if (status == GOT_STATUS_CONFLICT)
3188 return got_error(GOT_ERR_CONFLICTS);
3190 return NULL;
3193 static const struct got_error *
3194 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3195 const char *fileindex_path, struct got_object_id *commit_id1,
3196 struct got_object_id *commit_id2, struct got_repository *repo,
3197 got_worktree_checkout_cb progress_cb, void *progress_arg,
3198 got_cancel_cb cancel_cb, void *cancel_arg)
3200 const struct got_error *err = NULL, *sync_err;
3201 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3202 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3203 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3204 struct check_merge_conflicts_arg cmc_arg;
3205 struct merge_file_cb_arg arg;
3206 char *label_orig = NULL;
3207 FILE *f1 = NULL, *f2 = NULL;
3208 int fd1 = -1, fd2 = -1;
3210 if (commit_id1) {
3211 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3212 if (err)
3213 goto done;
3214 err = got_object_id_by_path(&tree_id1, repo, commit1,
3215 worktree->path_prefix);
3216 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3217 goto done;
3219 if (tree_id1) {
3220 char *id_str;
3222 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3223 if (err)
3224 goto done;
3226 err = got_object_id_str(&id_str, commit_id1);
3227 if (err)
3228 goto done;
3230 if (asprintf(&label_orig, "%s: commit %s",
3231 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3232 err = got_error_from_errno("asprintf");
3233 free(id_str);
3234 goto done;
3236 free(id_str);
3238 f1 = got_opentemp();
3239 if (f1 == NULL) {
3240 err = got_error_from_errno("got_opentemp");
3241 goto done;
3245 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3246 if (err)
3247 goto done;
3249 err = got_object_id_by_path(&tree_id2, repo, commit2,
3250 worktree->path_prefix);
3251 if (err)
3252 goto done;
3254 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3255 if (err)
3256 goto done;
3258 f2 = got_opentemp();
3259 if (f2 == NULL) {
3260 err = got_error_from_errno("got_opentemp");
3261 goto done;
3264 fd1 = got_opentempfd();
3265 if (fd1 == -1) {
3266 err = got_error_from_errno("got_opentempfd");
3267 goto done;
3270 fd2 = got_opentempfd();
3271 if (fd2 == -1) {
3272 err = got_error_from_errno("got_opentempfd");
3273 goto done;
3276 cmc_arg.worktree = worktree;
3277 cmc_arg.fileindex = fileindex;
3278 cmc_arg.repo = repo;
3279 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3280 check_merge_conflicts, &cmc_arg, 0);
3281 if (err)
3282 goto done;
3284 arg.worktree = worktree;
3285 arg.fileindex = fileindex;
3286 arg.progress_cb = progress_cb;
3287 arg.progress_arg = progress_arg;
3288 arg.cancel_cb = cancel_cb;
3289 arg.cancel_arg = cancel_arg;
3290 arg.label_orig = label_orig;
3291 arg.commit_id2 = commit_id2;
3292 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3293 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3294 merge_file_cb, &arg, 1);
3295 sync_err = sync_fileindex(fileindex, fileindex_path);
3296 if (sync_err && err == NULL)
3297 err = sync_err;
3298 done:
3299 if (commit1)
3300 got_object_commit_close(commit1);
3301 if (commit2)
3302 got_object_commit_close(commit2);
3303 if (tree1)
3304 got_object_tree_close(tree1);
3305 if (tree2)
3306 got_object_tree_close(tree2);
3307 if (f1 && fclose(f1) == EOF && err == NULL)
3308 err = got_error_from_errno("fclose");
3309 if (f2 && fclose(f2) == EOF && err == NULL)
3310 err = got_error_from_errno("fclose");
3311 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3312 err = got_error_from_errno("close");
3313 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3314 err = got_error_from_errno("close");
3315 free(label_orig);
3316 return err;
3319 const struct got_error *
3320 got_worktree_merge_files(struct got_worktree *worktree,
3321 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3322 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3323 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3325 const struct got_error *err, *unlockerr;
3326 char *fileindex_path = NULL;
3327 struct got_fileindex *fileindex = NULL;
3329 err = lock_worktree(worktree, LOCK_EX);
3330 if (err)
3331 return err;
3333 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3334 if (err)
3335 goto done;
3337 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3338 worktree);
3339 if (err)
3340 goto done;
3342 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3343 commit_id2, repo, progress_cb, progress_arg,
3344 cancel_cb, cancel_arg);
3345 done:
3346 if (fileindex)
3347 got_fileindex_free(fileindex);
3348 free(fileindex_path);
3349 unlockerr = lock_worktree(worktree, LOCK_SH);
3350 if (unlockerr && err == NULL)
3351 err = unlockerr;
3352 return err;
3355 struct diff_dir_cb_arg {
3356 struct got_fileindex *fileindex;
3357 struct got_worktree *worktree;
3358 const char *status_path;
3359 size_t status_path_len;
3360 struct got_repository *repo;
3361 got_worktree_status_cb status_cb;
3362 void *status_arg;
3363 got_cancel_cb cancel_cb;
3364 void *cancel_arg;
3365 /* A pathlist containing per-directory pathlists of ignore patterns. */
3366 struct got_pathlist_head *ignores;
3367 int report_unchanged;
3368 int no_ignores;
3371 static const struct got_error *
3372 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3373 int dirfd, const char *de_name,
3374 got_worktree_status_cb status_cb, void *status_arg,
3375 struct got_repository *repo, int report_unchanged)
3377 const struct got_error *err = NULL;
3378 unsigned char status = GOT_STATUS_NO_CHANGE;
3379 unsigned char staged_status;
3380 struct stat sb;
3381 struct got_object_id blob_id, commit_id, staged_blob_id;
3382 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3383 struct got_object_id *staged_blob_idp = NULL;
3385 staged_status = get_staged_status(ie);
3386 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3387 if (err)
3388 return err;
3390 if (status == GOT_STATUS_NO_CHANGE &&
3391 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3392 return NULL;
3394 if (got_fileindex_entry_has_blob(ie))
3395 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3396 if (got_fileindex_entry_has_commit(ie))
3397 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3398 if (staged_status == GOT_STATUS_ADD ||
3399 staged_status == GOT_STATUS_MODIFY) {
3400 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3401 &staged_blob_id, ie);
3404 return (*status_cb)(status_arg, status, staged_status,
3405 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3408 static const struct got_error *
3409 status_old_new(void *arg, struct got_fileindex_entry *ie,
3410 struct dirent *de, const char *parent_path, int dirfd)
3412 const struct got_error *err = NULL;
3413 struct diff_dir_cb_arg *a = arg;
3414 char *abspath;
3416 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3417 return got_error(GOT_ERR_CANCELLED);
3419 if (got_path_cmp(parent_path, a->status_path,
3420 strlen(parent_path), a->status_path_len) != 0 &&
3421 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3422 return NULL;
3424 if (parent_path[0]) {
3425 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3426 parent_path, de->d_name) == -1)
3427 return got_error_from_errno("asprintf");
3428 } else {
3429 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3430 de->d_name) == -1)
3431 return got_error_from_errno("asprintf");
3434 err = report_file_status(ie, abspath, dirfd, de->d_name,
3435 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3436 free(abspath);
3437 return err;
3440 static const struct got_error *
3441 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3443 struct diff_dir_cb_arg *a = arg;
3444 struct got_object_id blob_id, commit_id;
3445 unsigned char status;
3447 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3448 return got_error(GOT_ERR_CANCELLED);
3450 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3451 return NULL;
3453 got_fileindex_entry_get_blob_id(&blob_id, ie);
3454 got_fileindex_entry_get_commit_id(&commit_id, ie);
3455 if (got_fileindex_entry_has_file_on_disk(ie))
3456 status = GOT_STATUS_MISSING;
3457 else
3458 status = GOT_STATUS_DELETE;
3459 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3460 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3463 static void
3464 free_ignores(struct got_pathlist_head *ignores)
3466 struct got_pathlist_entry *pe;
3468 TAILQ_FOREACH(pe, ignores, entry) {
3469 struct got_pathlist_head *ignorelist = pe->data;
3471 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3473 got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3476 static const struct got_error *
3477 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3479 const struct got_error *err = NULL;
3480 struct got_pathlist_entry *pe = NULL;
3481 struct got_pathlist_head *ignorelist;
3482 char *line = NULL, *pattern, *dirpath = NULL;
3483 size_t linesize = 0;
3484 ssize_t linelen;
3486 ignorelist = calloc(1, sizeof(*ignorelist));
3487 if (ignorelist == NULL)
3488 return got_error_from_errno("calloc");
3489 TAILQ_INIT(ignorelist);
3491 while ((linelen = getline(&line, &linesize, f)) != -1) {
3492 if (linelen > 0 && line[linelen - 1] == '\n')
3493 line[linelen - 1] = '\0';
3495 /* Git's ignores may contain comments. */
3496 if (line[0] == '#')
3497 continue;
3499 /* Git's negated patterns are not (yet?) supported. */
3500 if (line[0] == '!')
3501 continue;
3503 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3504 line) == -1) {
3505 err = got_error_from_errno("asprintf");
3506 goto done;
3508 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3509 if (err)
3510 goto done;
3512 if (ferror(f)) {
3513 err = got_error_from_errno("getline");
3514 goto done;
3517 dirpath = strdup(path);
3518 if (dirpath == NULL) {
3519 err = got_error_from_errno("strdup");
3520 goto done;
3522 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3523 done:
3524 free(line);
3525 if (err || pe == NULL) {
3526 free(dirpath);
3527 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3529 return err;
3532 static int
3533 match_ignores(struct got_pathlist_head *ignores, const char *path)
3535 struct got_pathlist_entry *pe;
3537 /* Handle patterns which match in all directories. */
3538 TAILQ_FOREACH(pe, ignores, entry) {
3539 struct got_pathlist_head *ignorelist = pe->data;
3540 struct got_pathlist_entry *pi;
3542 TAILQ_FOREACH(pi, ignorelist, entry) {
3543 const char *p, *pattern = pi->path;
3545 if (strncmp(pattern, "**/", 3) != 0)
3546 continue;
3547 pattern += 3;
3548 p = path;
3549 while (*p) {
3550 if (fnmatch(pattern, p,
3551 FNM_PATHNAME | FNM_LEADING_DIR)) {
3552 /* Retry in next directory. */
3553 while (*p && *p != '/')
3554 p++;
3555 while (*p == '/')
3556 p++;
3557 continue;
3559 return 1;
3565 * The ignores pathlist contains ignore lists from children before
3566 * parents, so we can find the most specific ignorelist by walking
3567 * ignores backwards.
3569 pe = TAILQ_LAST(ignores, got_pathlist_head);
3570 while (pe) {
3571 if (got_path_is_child(path, pe->path, pe->path_len)) {
3572 struct got_pathlist_head *ignorelist = pe->data;
3573 struct got_pathlist_entry *pi;
3574 TAILQ_FOREACH(pi, ignorelist, entry) {
3575 const char *pattern = pi->path;
3576 int flags = FNM_LEADING_DIR;
3577 if (strstr(pattern, "/**/") == NULL)
3578 flags |= FNM_PATHNAME;
3579 if (fnmatch(pattern, path, flags))
3580 continue;
3581 return 1;
3584 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3587 return 0;
3590 static const struct got_error *
3591 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3592 const char *path, int dirfd, const char *ignores_filename)
3594 const struct got_error *err = NULL;
3595 char *ignorespath;
3596 int fd = -1;
3597 FILE *ignoresfile = NULL;
3599 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3600 path[0] ? "/" : "", ignores_filename) == -1)
3601 return got_error_from_errno("asprintf");
3603 if (dirfd != -1) {
3604 fd = openat(dirfd, ignores_filename,
3605 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3606 if (fd == -1) {
3607 if (errno != ENOENT && errno != EACCES)
3608 err = got_error_from_errno2("openat",
3609 ignorespath);
3610 } else {
3611 ignoresfile = fdopen(fd, "r");
3612 if (ignoresfile == NULL)
3613 err = got_error_from_errno2("fdopen",
3614 ignorespath);
3615 else {
3616 fd = -1;
3617 err = read_ignores(ignores, path, ignoresfile);
3620 } else {
3621 ignoresfile = fopen(ignorespath, "re");
3622 if (ignoresfile == NULL) {
3623 if (errno != ENOENT && errno != EACCES)
3624 err = got_error_from_errno2("fopen",
3625 ignorespath);
3626 } else
3627 err = read_ignores(ignores, path, ignoresfile);
3630 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3631 err = got_error_from_errno2("fclose", path);
3632 if (fd != -1 && close(fd) == -1 && err == NULL)
3633 err = got_error_from_errno2("close", path);
3634 free(ignorespath);
3635 return err;
3638 static const struct got_error *
3639 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3640 int dirfd)
3642 const struct got_error *err = NULL;
3643 struct diff_dir_cb_arg *a = arg;
3644 char *path = NULL;
3646 if (ignore != NULL)
3647 *ignore = 0;
3649 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3650 return got_error(GOT_ERR_CANCELLED);
3652 if (parent_path[0]) {
3653 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3654 return got_error_from_errno("asprintf");
3655 } else {
3656 path = de->d_name;
3659 if (de->d_type == DT_DIR) {
3660 if (!a->no_ignores && ignore != NULL &&
3661 match_ignores(a->ignores, path))
3662 *ignore = 1;
3663 } else if (!match_ignores(a->ignores, path) &&
3664 got_path_is_child(path, a->status_path, a->status_path_len))
3665 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3666 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3667 if (parent_path[0])
3668 free(path);
3669 return err;
3672 static const struct got_error *
3673 status_traverse(void *arg, const char *path, int dirfd)
3675 const struct got_error *err = NULL;
3676 struct diff_dir_cb_arg *a = arg;
3678 if (a->no_ignores)
3679 return NULL;
3681 err = add_ignores(a->ignores, a->worktree->root_path,
3682 path, dirfd, ".cvsignore");
3683 if (err)
3684 return err;
3686 err = add_ignores(a->ignores, a->worktree->root_path, path,
3687 dirfd, ".gitignore");
3689 return err;
3692 static const struct got_error *
3693 report_single_file_status(const char *path, const char *ondisk_path,
3694 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3695 void *status_arg, struct got_repository *repo, int report_unchanged,
3696 struct got_pathlist_head *ignores, int no_ignores)
3698 struct got_fileindex_entry *ie;
3699 struct stat sb;
3701 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3702 if (ie)
3703 return report_file_status(ie, ondisk_path, -1, NULL,
3704 status_cb, status_arg, repo, report_unchanged);
3706 if (lstat(ondisk_path, &sb) == -1) {
3707 if (errno != ENOENT)
3708 return got_error_from_errno2("lstat", ondisk_path);
3709 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3710 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3713 if (!no_ignores && match_ignores(ignores, path))
3714 return NULL;
3716 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3717 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3718 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3720 return NULL;
3723 static const struct got_error *
3724 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3725 const char *root_path, const char *path)
3727 const struct got_error *err;
3728 char *parent_path, *next_parent_path = NULL;
3730 err = add_ignores(ignores, root_path, "", -1,
3731 ".cvsignore");
3732 if (err)
3733 return err;
3735 err = add_ignores(ignores, root_path, "", -1,
3736 ".gitignore");
3737 if (err)
3738 return err;
3740 err = got_path_dirname(&parent_path, path);
3741 if (err) {
3742 if (err->code == GOT_ERR_BAD_PATH)
3743 return NULL; /* cannot traverse parent */
3744 return err;
3746 for (;;) {
3747 err = add_ignores(ignores, root_path, parent_path, -1,
3748 ".cvsignore");
3749 if (err)
3750 break;
3751 err = add_ignores(ignores, root_path, parent_path, -1,
3752 ".gitignore");
3753 if (err)
3754 break;
3755 err = got_path_dirname(&next_parent_path, parent_path);
3756 if (err) {
3757 if (err->code == GOT_ERR_BAD_PATH)
3758 err = NULL; /* traversed everything */
3759 break;
3761 if (got_path_is_root_dir(parent_path))
3762 break;
3763 free(parent_path);
3764 parent_path = next_parent_path;
3765 next_parent_path = NULL;
3768 free(parent_path);
3769 free(next_parent_path);
3770 return err;
3773 static const struct got_error *
3774 worktree_status(struct got_worktree *worktree, const char *path,
3775 struct got_fileindex *fileindex, struct got_repository *repo,
3776 got_worktree_status_cb status_cb, void *status_arg,
3777 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3778 int report_unchanged)
3780 const struct got_error *err = NULL;
3781 int fd = -1;
3782 struct got_fileindex_diff_dir_cb fdiff_cb;
3783 struct diff_dir_cb_arg arg;
3784 char *ondisk_path = NULL;
3785 struct got_pathlist_head ignores;
3786 struct got_fileindex_entry *ie;
3788 TAILQ_INIT(&ignores);
3790 if (asprintf(&ondisk_path, "%s%s%s",
3791 worktree->root_path, path[0] ? "/" : "", path) == -1)
3792 return got_error_from_errno("asprintf");
3794 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3795 if (ie) {
3796 err = report_single_file_status(path, ondisk_path,
3797 fileindex, status_cb, status_arg, repo,
3798 report_unchanged, &ignores, no_ignores);
3799 goto done;
3802 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3803 if (fd == -1) {
3804 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3805 !got_err_open_nofollow_on_symlink())
3806 err = got_error_from_errno2("open", ondisk_path);
3807 else {
3808 if (!no_ignores) {
3809 err = add_ignores_from_parent_paths(&ignores,
3810 worktree->root_path, ondisk_path);
3811 if (err)
3812 goto done;
3814 err = report_single_file_status(path, ondisk_path,
3815 fileindex, status_cb, status_arg, repo,
3816 report_unchanged, &ignores, no_ignores);
3818 } else {
3819 fdiff_cb.diff_old_new = status_old_new;
3820 fdiff_cb.diff_old = status_old;
3821 fdiff_cb.diff_new = status_new;
3822 fdiff_cb.diff_traverse = status_traverse;
3823 arg.fileindex = fileindex;
3824 arg.worktree = worktree;
3825 arg.status_path = path;
3826 arg.status_path_len = strlen(path);
3827 arg.repo = repo;
3828 arg.status_cb = status_cb;
3829 arg.status_arg = status_arg;
3830 arg.cancel_cb = cancel_cb;
3831 arg.cancel_arg = cancel_arg;
3832 arg.report_unchanged = report_unchanged;
3833 arg.no_ignores = no_ignores;
3834 if (!no_ignores) {
3835 err = add_ignores_from_parent_paths(&ignores,
3836 worktree->root_path, path);
3837 if (err)
3838 goto done;
3840 arg.ignores = &ignores;
3841 err = got_fileindex_diff_dir(fileindex, fd,
3842 worktree->root_path, path, repo, &fdiff_cb, &arg);
3844 done:
3845 free_ignores(&ignores);
3846 if (fd != -1 && close(fd) == -1 && err == NULL)
3847 err = got_error_from_errno("close");
3848 free(ondisk_path);
3849 return err;
3852 const struct got_error *
3853 got_worktree_status(struct got_worktree *worktree,
3854 struct got_pathlist_head *paths, struct got_repository *repo,
3855 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3856 got_cancel_cb cancel_cb, void *cancel_arg)
3858 const struct got_error *err = NULL;
3859 char *fileindex_path = NULL;
3860 struct got_fileindex *fileindex = NULL;
3861 struct got_pathlist_entry *pe;
3863 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3864 if (err)
3865 return err;
3867 TAILQ_FOREACH(pe, paths, entry) {
3868 err = worktree_status(worktree, pe->path, fileindex, repo,
3869 status_cb, status_arg, cancel_cb, cancel_arg,
3870 no_ignores, 0);
3871 if (err)
3872 break;
3874 free(fileindex_path);
3875 got_fileindex_free(fileindex);
3876 return err;
3879 const struct got_error *
3880 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3881 const char *arg)
3883 const struct got_error *err = NULL;
3884 char *resolved = NULL, *cwd = NULL, *path = NULL;
3885 size_t len;
3886 struct stat sb;
3887 char *abspath = NULL;
3888 char canonpath[PATH_MAX];
3890 *wt_path = NULL;
3892 cwd = getcwd(NULL, 0);
3893 if (cwd == NULL)
3894 return got_error_from_errno("getcwd");
3896 if (lstat(arg, &sb) == -1) {
3897 if (errno != ENOENT) {
3898 err = got_error_from_errno2("lstat", arg);
3899 goto done;
3901 sb.st_mode = 0;
3903 if (S_ISLNK(sb.st_mode)) {
3905 * We cannot use realpath(3) with symlinks since we want to
3906 * operate on the symlink itself.
3907 * But we can make the path absolute, assuming it is relative
3908 * to the current working directory, and then canonicalize it.
3910 if (!got_path_is_absolute(arg)) {
3911 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3912 err = got_error_from_errno("asprintf");
3913 goto done;
3917 err = got_canonpath(abspath ? abspath : arg, canonpath,
3918 sizeof(canonpath));
3919 if (err)
3920 goto done;
3921 resolved = strdup(canonpath);
3922 if (resolved == NULL) {
3923 err = got_error_from_errno("strdup");
3924 goto done;
3926 } else {
3927 resolved = realpath(arg, NULL);
3928 if (resolved == NULL) {
3929 if (errno != ENOENT) {
3930 err = got_error_from_errno2("realpath", arg);
3931 goto done;
3933 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3934 err = got_error_from_errno("asprintf");
3935 goto done;
3937 err = got_canonpath(abspath, canonpath,
3938 sizeof(canonpath));
3939 if (err)
3940 goto done;
3941 resolved = strdup(canonpath);
3942 if (resolved == NULL) {
3943 err = got_error_from_errno("strdup");
3944 goto done;
3949 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3950 strlen(got_worktree_get_root_path(worktree)))) {
3951 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3952 goto done;
3955 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3956 err = got_path_skip_common_ancestor(&path,
3957 got_worktree_get_root_path(worktree), resolved);
3958 if (err)
3959 goto done;
3960 } else {
3961 path = strdup("");
3962 if (path == NULL) {
3963 err = got_error_from_errno("strdup");
3964 goto done;
3968 /* XXX status walk can't deal with trailing slash! */
3969 len = strlen(path);
3970 while (len > 0 && path[len - 1] == '/') {
3971 path[len - 1] = '\0';
3972 len--;
3974 done:
3975 free(abspath);
3976 free(resolved);
3977 free(cwd);
3978 if (err == NULL)
3979 *wt_path = path;
3980 else
3981 free(path);
3982 return err;
3985 struct schedule_addition_args {
3986 struct got_worktree *worktree;
3987 struct got_fileindex *fileindex;
3988 got_worktree_checkout_cb progress_cb;
3989 void *progress_arg;
3990 struct got_repository *repo;
3993 static const struct got_error *
3994 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3995 const char *relpath, struct got_object_id *blob_id,
3996 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3997 int dirfd, const char *de_name)
3999 struct schedule_addition_args *a = arg;
4000 const struct got_error *err = NULL;
4001 struct got_fileindex_entry *ie;
4002 struct stat sb;
4003 char *ondisk_path;
4005 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4006 relpath) == -1)
4007 return got_error_from_errno("asprintf");
4009 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4010 if (ie) {
4011 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4012 de_name, a->repo);
4013 if (err)
4014 goto done;
4015 /* Re-adding an existing entry is a no-op. */
4016 if (status == GOT_STATUS_ADD)
4017 goto done;
4018 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4019 if (err)
4020 goto done;
4023 if (status != GOT_STATUS_UNVERSIONED) {
4024 if (status == GOT_STATUS_NONEXISTENT)
4025 err = got_error_set_errno(ENOENT, ondisk_path);
4026 else
4027 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4028 goto done;
4031 err = got_fileindex_entry_alloc(&ie, relpath);
4032 if (err)
4033 goto done;
4034 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4035 relpath, NULL, NULL, 1);
4036 if (err) {
4037 got_fileindex_entry_free(ie);
4038 goto done;
4040 err = got_fileindex_entry_add(a->fileindex, ie);
4041 if (err) {
4042 got_fileindex_entry_free(ie);
4043 goto done;
4045 done:
4046 free(ondisk_path);
4047 if (err)
4048 return err;
4049 if (status == GOT_STATUS_ADD)
4050 return NULL;
4051 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4054 const struct got_error *
4055 got_worktree_schedule_add(struct got_worktree *worktree,
4056 struct got_pathlist_head *paths,
4057 got_worktree_checkout_cb progress_cb, void *progress_arg,
4058 struct got_repository *repo, int no_ignores)
4060 struct got_fileindex *fileindex = NULL;
4061 char *fileindex_path = NULL;
4062 const struct got_error *err = NULL, *sync_err, *unlockerr;
4063 struct got_pathlist_entry *pe;
4064 struct schedule_addition_args saa;
4066 err = lock_worktree(worktree, LOCK_EX);
4067 if (err)
4068 return err;
4070 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4071 if (err)
4072 goto done;
4074 saa.worktree = worktree;
4075 saa.fileindex = fileindex;
4076 saa.progress_cb = progress_cb;
4077 saa.progress_arg = progress_arg;
4078 saa.repo = repo;
4080 TAILQ_FOREACH(pe, paths, entry) {
4081 err = worktree_status(worktree, pe->path, fileindex, repo,
4082 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4083 if (err)
4084 break;
4086 sync_err = sync_fileindex(fileindex, fileindex_path);
4087 if (sync_err && err == NULL)
4088 err = sync_err;
4089 done:
4090 free(fileindex_path);
4091 if (fileindex)
4092 got_fileindex_free(fileindex);
4093 unlockerr = lock_worktree(worktree, LOCK_SH);
4094 if (unlockerr && err == NULL)
4095 err = unlockerr;
4096 return err;
4099 struct schedule_deletion_args {
4100 struct got_worktree *worktree;
4101 struct got_fileindex *fileindex;
4102 got_worktree_delete_cb progress_cb;
4103 void *progress_arg;
4104 struct got_repository *repo;
4105 int delete_local_mods;
4106 int keep_on_disk;
4107 int ignore_missing_paths;
4108 const char *status_codes;
4111 static const struct got_error *
4112 schedule_for_deletion(void *arg, unsigned char status,
4113 unsigned char staged_status, const char *relpath,
4114 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4115 struct got_object_id *commit_id, int dirfd, const char *de_name)
4117 struct schedule_deletion_args *a = arg;
4118 const struct got_error *err = NULL;
4119 struct got_fileindex_entry *ie = NULL;
4120 struct stat sb;
4121 char *ondisk_path;
4123 if (status == GOT_STATUS_NONEXISTENT) {
4124 if (a->ignore_missing_paths)
4125 return NULL;
4126 return got_error_set_errno(ENOENT, relpath);
4129 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4130 if (ie == NULL)
4131 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4133 staged_status = get_staged_status(ie);
4134 if (staged_status != GOT_STATUS_NO_CHANGE) {
4135 if (staged_status == GOT_STATUS_DELETE)
4136 return NULL;
4137 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4140 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4141 relpath) == -1)
4142 return got_error_from_errno("asprintf");
4144 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4145 a->repo);
4146 if (err)
4147 goto done;
4149 if (a->status_codes) {
4150 size_t ncodes = strlen(a->status_codes);
4151 int i;
4152 for (i = 0; i < ncodes ; i++) {
4153 if (status == a->status_codes[i])
4154 break;
4156 if (i == ncodes) {
4157 /* Do not delete files in non-matching status. */
4158 free(ondisk_path);
4159 return NULL;
4161 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4162 a->status_codes[i] != GOT_STATUS_MISSING) {
4163 static char msg[64];
4164 snprintf(msg, sizeof(msg),
4165 "invalid status code '%c'", a->status_codes[i]);
4166 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4167 goto done;
4171 if (status != GOT_STATUS_NO_CHANGE) {
4172 if (status == GOT_STATUS_DELETE)
4173 goto done;
4174 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4175 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4176 goto done;
4178 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4179 err = got_error_set_errno(ENOENT, relpath);
4180 goto done;
4182 if (status != GOT_STATUS_MODIFY &&
4183 status != GOT_STATUS_MISSING) {
4184 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4185 goto done;
4189 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4190 size_t root_len;
4192 if (dirfd != -1) {
4193 if (unlinkat(dirfd, de_name, 0) == -1) {
4194 err = got_error_from_errno2("unlinkat",
4195 ondisk_path);
4196 goto done;
4198 } else if (unlink(ondisk_path) == -1) {
4199 err = got_error_from_errno2("unlink", ondisk_path);
4200 goto done;
4203 root_len = strlen(a->worktree->root_path);
4204 do {
4205 char *parent;
4206 err = got_path_dirname(&parent, ondisk_path);
4207 if (err)
4208 goto done;
4209 free(ondisk_path);
4210 ondisk_path = parent;
4211 if (rmdir(ondisk_path) == -1) {
4212 if (errno != ENOTEMPTY)
4213 err = got_error_from_errno2("rmdir",
4214 ondisk_path);
4215 break;
4217 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4218 strlen(ondisk_path), root_len) != 0);
4221 got_fileindex_entry_mark_deleted_from_disk(ie);
4222 done:
4223 free(ondisk_path);
4224 if (err)
4225 return err;
4226 if (status == GOT_STATUS_DELETE)
4227 return NULL;
4228 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4229 staged_status, relpath);
4232 const struct got_error *
4233 got_worktree_schedule_delete(struct got_worktree *worktree,
4234 struct got_pathlist_head *paths, int delete_local_mods,
4235 const char *status_codes,
4236 got_worktree_delete_cb progress_cb, void *progress_arg,
4237 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4239 struct got_fileindex *fileindex = NULL;
4240 char *fileindex_path = NULL;
4241 const struct got_error *err = NULL, *sync_err, *unlockerr;
4242 struct got_pathlist_entry *pe;
4243 struct schedule_deletion_args sda;
4245 err = lock_worktree(worktree, LOCK_EX);
4246 if (err)
4247 return err;
4249 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4250 if (err)
4251 goto done;
4253 sda.worktree = worktree;
4254 sda.fileindex = fileindex;
4255 sda.progress_cb = progress_cb;
4256 sda.progress_arg = progress_arg;
4257 sda.repo = repo;
4258 sda.delete_local_mods = delete_local_mods;
4259 sda.keep_on_disk = keep_on_disk;
4260 sda.ignore_missing_paths = ignore_missing_paths;
4261 sda.status_codes = status_codes;
4263 TAILQ_FOREACH(pe, paths, entry) {
4264 err = worktree_status(worktree, pe->path, fileindex, repo,
4265 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4266 if (err)
4267 break;
4269 sync_err = sync_fileindex(fileindex, fileindex_path);
4270 if (sync_err && err == NULL)
4271 err = sync_err;
4272 done:
4273 free(fileindex_path);
4274 if (fileindex)
4275 got_fileindex_free(fileindex);
4276 unlockerr = lock_worktree(worktree, LOCK_SH);
4277 if (unlockerr && err == NULL)
4278 err = unlockerr;
4279 return err;
4282 static const struct got_error *
4283 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4285 const struct got_error *err = NULL;
4286 char *line = NULL;
4287 size_t linesize = 0, n;
4288 ssize_t linelen;
4290 linelen = getline(&line, &linesize, infile);
4291 if (linelen == -1) {
4292 if (ferror(infile)) {
4293 err = got_error_from_errno("getline");
4294 goto done;
4296 return NULL;
4298 if (outfile) {
4299 n = fwrite(line, 1, linelen, outfile);
4300 if (n != linelen) {
4301 err = got_ferror(outfile, GOT_ERR_IO);
4302 goto done;
4305 if (rejectfile) {
4306 n = fwrite(line, 1, linelen, rejectfile);
4307 if (n != linelen)
4308 err = got_ferror(rejectfile, GOT_ERR_IO);
4310 done:
4311 free(line);
4312 return err;
4315 static const struct got_error *
4316 skip_one_line(FILE *f)
4318 char *line = NULL;
4319 size_t linesize = 0;
4320 ssize_t linelen;
4322 linelen = getline(&line, &linesize, f);
4323 if (linelen == -1) {
4324 if (ferror(f))
4325 return got_error_from_errno("getline");
4326 return NULL;
4328 free(line);
4329 return NULL;
4332 static const struct got_error *
4333 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4334 int start_old, int end_old, int start_new, int end_new,
4335 FILE *outfile, FILE *rejectfile)
4337 const struct got_error *err;
4339 /* Copy old file's lines leading up to patch. */
4340 while (!feof(f1) && *line_cur1 < start_old) {
4341 err = copy_one_line(f1, outfile, NULL);
4342 if (err)
4343 return err;
4344 (*line_cur1)++;
4346 /* Skip new file's lines leading up to patch. */
4347 while (!feof(f2) && *line_cur2 < start_new) {
4348 if (rejectfile)
4349 err = copy_one_line(f2, NULL, rejectfile);
4350 else
4351 err = skip_one_line(f2);
4352 if (err)
4353 return err;
4354 (*line_cur2)++;
4356 /* Copy patched lines. */
4357 while (!feof(f2) && *line_cur2 <= end_new) {
4358 err = copy_one_line(f2, outfile, NULL);
4359 if (err)
4360 return err;
4361 (*line_cur2)++;
4363 /* Skip over old file's replaced lines. */
4364 while (!feof(f1) && *line_cur1 <= end_old) {
4365 if (rejectfile)
4366 err = copy_one_line(f1, NULL, rejectfile);
4367 else
4368 err = skip_one_line(f1);
4369 if (err)
4370 return err;
4371 (*line_cur1)++;
4374 return NULL;
4377 static const struct got_error *
4378 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4379 FILE *outfile, FILE *rejectfile)
4381 const struct got_error *err;
4383 if (outfile) {
4384 /* Copy old file's lines until EOF. */
4385 while (!feof(f1)) {
4386 err = copy_one_line(f1, outfile, NULL);
4387 if (err)
4388 return err;
4389 (*line_cur1)++;
4392 if (rejectfile) {
4393 /* Copy new file's lines until EOF. */
4394 while (!feof(f2)) {
4395 err = copy_one_line(f2, NULL, rejectfile);
4396 if (err)
4397 return err;
4398 (*line_cur2)++;
4402 return NULL;
4405 static const struct got_error *
4406 apply_or_reject_change(int *choice, int *nchunks_used,
4407 struct diff_result *diff_result, int n,
4408 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4409 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4410 got_worktree_patch_cb patch_cb, void *patch_arg)
4412 const struct got_error *err = NULL;
4413 struct diff_chunk_context cc = {};
4414 int start_old, end_old, start_new, end_new;
4415 FILE *hunkfile;
4416 struct diff_output_unidiff_state *diff_state;
4417 struct diff_input_info diff_info;
4418 int rc;
4420 *choice = GOT_PATCH_CHOICE_NONE;
4422 /* Get changed line numbers without context lines for copy_change(). */
4423 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4424 start_old = cc.left.start;
4425 end_old = cc.left.end;
4426 start_new = cc.right.start;
4427 end_new = cc.right.end;
4429 /* Get the same change with context lines for display. */
4430 memset(&cc, 0, sizeof(cc));
4431 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4433 memset(&diff_info, 0, sizeof(diff_info));
4434 diff_info.left_path = relpath;
4435 diff_info.right_path = relpath;
4437 diff_state = diff_output_unidiff_state_alloc();
4438 if (diff_state == NULL)
4439 return got_error_set_errno(ENOMEM,
4440 "diff_output_unidiff_state_alloc");
4442 hunkfile = got_opentemp();
4443 if (hunkfile == NULL) {
4444 err = got_error_from_errno("got_opentemp");
4445 goto done;
4448 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4449 diff_result, &cc);
4450 if (rc != DIFF_RC_OK) {
4451 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4452 goto done;
4455 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4456 err = got_ferror(hunkfile, GOT_ERR_IO);
4457 goto done;
4460 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4461 hunkfile, changeno, nchanges);
4462 if (err)
4463 goto done;
4465 switch (*choice) {
4466 case GOT_PATCH_CHOICE_YES:
4467 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4468 end_old, start_new, end_new, outfile, rejectfile);
4469 break;
4470 case GOT_PATCH_CHOICE_NO:
4471 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4472 end_old, start_new, end_new, rejectfile, outfile);
4473 break;
4474 case GOT_PATCH_CHOICE_QUIT:
4475 break;
4476 default:
4477 err = got_error(GOT_ERR_PATCH_CHOICE);
4478 break;
4480 done:
4481 diff_output_unidiff_state_free(diff_state);
4482 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4483 err = got_error_from_errno("fclose");
4484 return err;
4487 struct revert_file_args {
4488 struct got_worktree *worktree;
4489 struct got_fileindex *fileindex;
4490 got_worktree_checkout_cb progress_cb;
4491 void *progress_arg;
4492 got_worktree_patch_cb patch_cb;
4493 void *patch_arg;
4494 struct got_repository *repo;
4495 int unlink_added_files;
4498 static const struct got_error *
4499 create_patched_content(char **path_outfile, int reverse_patch,
4500 struct got_object_id *blob_id, const char *path2,
4501 int dirfd2, const char *de_name2,
4502 const char *relpath, struct got_repository *repo,
4503 got_worktree_patch_cb patch_cb, void *patch_arg)
4505 const struct got_error *err, *free_err;
4506 struct got_blob_object *blob = NULL;
4507 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4508 int fd = -1, fd2 = -1;
4509 char link_target[PATH_MAX];
4510 ssize_t link_len = 0;
4511 char *path1 = NULL, *id_str = NULL;
4512 struct stat sb2;
4513 struct got_diffreg_result *diffreg_result = NULL;
4514 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4515 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4517 *path_outfile = NULL;
4519 err = got_object_id_str(&id_str, blob_id);
4520 if (err)
4521 return err;
4523 if (dirfd2 != -1) {
4524 fd2 = openat(dirfd2, de_name2,
4525 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4526 if (fd2 == -1) {
4527 if (!got_err_open_nofollow_on_symlink()) {
4528 err = got_error_from_errno2("openat", path2);
4529 goto done;
4531 link_len = readlinkat(dirfd2, de_name2,
4532 link_target, sizeof(link_target));
4533 if (link_len == -1) {
4534 return got_error_from_errno2("readlinkat",
4535 path2);
4537 sb2.st_mode = S_IFLNK;
4538 sb2.st_size = link_len;
4540 } else {
4541 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4542 if (fd2 == -1) {
4543 if (!got_err_open_nofollow_on_symlink()) {
4544 err = got_error_from_errno2("open", path2);
4545 goto done;
4547 link_len = readlink(path2, link_target,
4548 sizeof(link_target));
4549 if (link_len == -1)
4550 return got_error_from_errno2("readlink", path2);
4551 sb2.st_mode = S_IFLNK;
4552 sb2.st_size = link_len;
4555 if (fd2 != -1) {
4556 if (fstat(fd2, &sb2) == -1) {
4557 err = got_error_from_errno2("fstat", path2);
4558 goto done;
4561 f2 = fdopen(fd2, "r");
4562 if (f2 == NULL) {
4563 err = got_error_from_errno2("fdopen", path2);
4564 goto done;
4566 fd2 = -1;
4567 } else {
4568 size_t n;
4569 f2 = got_opentemp();
4570 if (f2 == NULL) {
4571 err = got_error_from_errno2("got_opentemp", path2);
4572 goto done;
4574 n = fwrite(link_target, 1, link_len, f2);
4575 if (n != link_len) {
4576 err = got_ferror(f2, GOT_ERR_IO);
4577 goto done;
4579 if (fflush(f2) == EOF) {
4580 err = got_error_from_errno("fflush");
4581 goto done;
4583 rewind(f2);
4586 fd = got_opentempfd();
4587 if (fd == -1) {
4588 err = got_error_from_errno("got_opentempfd");
4589 goto done;
4592 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4593 if (err)
4594 goto done;
4596 err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4597 if (err)
4598 goto done;
4600 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4601 if (err)
4602 goto done;
4604 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4605 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4606 if (err)
4607 goto done;
4609 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4610 "");
4611 if (err)
4612 goto done;
4614 if (fseek(f1, 0L, SEEK_SET) == -1)
4615 return got_ferror(f1, GOT_ERR_IO);
4616 if (fseek(f2, 0L, SEEK_SET) == -1)
4617 return got_ferror(f2, GOT_ERR_IO);
4619 /* Count the number of actual changes in the diff result. */
4620 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4621 struct diff_chunk_context cc = {};
4622 diff_chunk_context_load_change(&cc, &nchunks_used,
4623 diffreg_result->result, n, 0);
4624 nchanges++;
4626 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4627 int choice;
4628 err = apply_or_reject_change(&choice, &nchunks_used,
4629 diffreg_result->result, n, relpath, f1, f2,
4630 &line_cur1, &line_cur2,
4631 reverse_patch ? NULL : outfile,
4632 reverse_patch ? outfile : NULL,
4633 ++i, nchanges, patch_cb, patch_arg);
4634 if (err)
4635 goto done;
4636 if (choice == GOT_PATCH_CHOICE_YES)
4637 have_content = 1;
4638 else if (choice == GOT_PATCH_CHOICE_QUIT)
4639 break;
4641 if (have_content) {
4642 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4643 reverse_patch ? NULL : outfile,
4644 reverse_patch ? outfile : NULL);
4645 if (err)
4646 goto done;
4648 if (!S_ISLNK(sb2.st_mode)) {
4649 mode_t mode;
4651 mode = apply_umask(sb2.st_mode);
4652 if (fchmod(fileno(outfile), mode) == -1) {
4653 err = got_error_from_errno2("fchmod", path2);
4654 goto done;
4658 done:
4659 free(id_str);
4660 if (fd != -1 && close(fd) == -1 && err == NULL)
4661 err = got_error_from_errno("close");
4662 if (blob)
4663 got_object_blob_close(blob);
4664 free_err = got_diffreg_result_free(diffreg_result);
4665 if (err == NULL)
4666 err = free_err;
4667 if (f1 && fclose(f1) == EOF && err == NULL)
4668 err = got_error_from_errno2("fclose", path1);
4669 if (f2 && fclose(f2) == EOF && err == NULL)
4670 err = got_error_from_errno2("fclose", path2);
4671 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4672 err = got_error_from_errno2("close", path2);
4673 if (outfile && fclose(outfile) == EOF && err == NULL)
4674 err = got_error_from_errno2("fclose", *path_outfile);
4675 if (path1 && unlink(path1) == -1 && err == NULL)
4676 err = got_error_from_errno2("unlink", path1);
4677 if (err || !have_content) {
4678 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4679 err = got_error_from_errno2("unlink", *path_outfile);
4680 free(*path_outfile);
4681 *path_outfile = NULL;
4683 free(path1);
4684 return err;
4687 static const struct got_error *
4688 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4689 const char *relpath, struct got_object_id *blob_id,
4690 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4691 int dirfd, const char *de_name)
4693 struct revert_file_args *a = arg;
4694 const struct got_error *err = NULL;
4695 char *parent_path = NULL;
4696 struct got_fileindex_entry *ie;
4697 struct got_commit_object *base_commit = NULL;
4698 struct got_tree_object *tree = NULL;
4699 struct got_object_id *tree_id = NULL;
4700 const struct got_tree_entry *te = NULL;
4701 char *tree_path = NULL, *te_name;
4702 char *ondisk_path = NULL, *path_content = NULL;
4703 struct got_blob_object *blob = NULL;
4704 int fd = -1;
4706 /* Reverting a staged deletion is a no-op. */
4707 if (status == GOT_STATUS_DELETE &&
4708 staged_status != GOT_STATUS_NO_CHANGE)
4709 return NULL;
4711 if (status == GOT_STATUS_UNVERSIONED)
4712 return (*a->progress_cb)(a->progress_arg,
4713 GOT_STATUS_UNVERSIONED, relpath);
4715 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4716 if (ie == NULL)
4717 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4719 /* Construct in-repository path of tree which contains this blob. */
4720 err = got_path_dirname(&parent_path, ie->path);
4721 if (err) {
4722 if (err->code != GOT_ERR_BAD_PATH)
4723 goto done;
4724 parent_path = strdup("/");
4725 if (parent_path == NULL) {
4726 err = got_error_from_errno("strdup");
4727 goto done;
4730 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4731 tree_path = strdup(parent_path);
4732 if (tree_path == NULL) {
4733 err = got_error_from_errno("strdup");
4734 goto done;
4736 } else {
4737 if (got_path_is_root_dir(parent_path)) {
4738 tree_path = strdup(a->worktree->path_prefix);
4739 if (tree_path == NULL) {
4740 err = got_error_from_errno("strdup");
4741 goto done;
4743 } else {
4744 if (asprintf(&tree_path, "%s/%s",
4745 a->worktree->path_prefix, parent_path) == -1) {
4746 err = got_error_from_errno("asprintf");
4747 goto done;
4752 err = got_object_open_as_commit(&base_commit, a->repo,
4753 a->worktree->base_commit_id);
4754 if (err)
4755 goto done;
4757 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4758 if (err) {
4759 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4760 (status == GOT_STATUS_ADD ||
4761 staged_status == GOT_STATUS_ADD)))
4762 goto done;
4763 } else {
4764 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4765 if (err)
4766 goto done;
4768 err = got_path_basename(&te_name, ie->path);
4769 if (err)
4770 goto done;
4772 te = got_object_tree_find_entry(tree, te_name);
4773 free(te_name);
4774 if (te == NULL && status != GOT_STATUS_ADD &&
4775 staged_status != GOT_STATUS_ADD) {
4776 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4777 goto done;
4781 switch (status) {
4782 case GOT_STATUS_ADD:
4783 if (a->patch_cb) {
4784 int choice = GOT_PATCH_CHOICE_NONE;
4785 err = (*a->patch_cb)(&choice, a->patch_arg,
4786 status, ie->path, NULL, 1, 1);
4787 if (err)
4788 goto done;
4789 if (choice != GOT_PATCH_CHOICE_YES)
4790 break;
4792 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4793 ie->path);
4794 if (err)
4795 goto done;
4796 got_fileindex_entry_remove(a->fileindex, ie);
4797 if (a->unlink_added_files) {
4798 if (asprintf(&ondisk_path, "%s/%s",
4799 got_worktree_get_root_path(a->worktree),
4800 relpath) == -1) {
4801 err = got_error_from_errno("asprintf");
4802 goto done;
4804 if (unlink(ondisk_path) == -1) {
4805 err = got_error_from_errno2("unlink",
4806 ondisk_path);
4807 break;
4810 break;
4811 case GOT_STATUS_DELETE:
4812 if (a->patch_cb) {
4813 int choice = GOT_PATCH_CHOICE_NONE;
4814 err = (*a->patch_cb)(&choice, a->patch_arg,
4815 status, ie->path, NULL, 1, 1);
4816 if (err)
4817 goto done;
4818 if (choice != GOT_PATCH_CHOICE_YES)
4819 break;
4821 /* fall through */
4822 case GOT_STATUS_MODIFY:
4823 case GOT_STATUS_MODE_CHANGE:
4824 case GOT_STATUS_CONFLICT:
4825 case GOT_STATUS_MISSING: {
4826 struct got_object_id id;
4827 if (staged_status == GOT_STATUS_ADD ||
4828 staged_status == GOT_STATUS_MODIFY)
4829 got_fileindex_entry_get_staged_blob_id(&id, ie);
4830 else
4831 got_fileindex_entry_get_blob_id(&id, ie);
4832 fd = got_opentempfd();
4833 if (fd == -1) {
4834 err = got_error_from_errno("got_opentempfd");
4835 goto done;
4838 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4839 if (err)
4840 goto done;
4842 if (asprintf(&ondisk_path, "%s/%s",
4843 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4844 err = got_error_from_errno("asprintf");
4845 goto done;
4848 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4849 status == GOT_STATUS_CONFLICT)) {
4850 int is_bad_symlink = 0;
4851 err = create_patched_content(&path_content, 1, &id,
4852 ondisk_path, dirfd, de_name, ie->path, a->repo,
4853 a->patch_cb, a->patch_arg);
4854 if (err || path_content == NULL)
4855 break;
4856 if (te && S_ISLNK(te->mode)) {
4857 if (unlink(path_content) == -1) {
4858 err = got_error_from_errno2("unlink",
4859 path_content);
4860 break;
4862 err = install_symlink(&is_bad_symlink,
4863 a->worktree, ondisk_path, ie->path,
4864 blob, 0, 1, 0, 0, a->repo,
4865 a->progress_cb, a->progress_arg);
4866 } else {
4867 if (rename(path_content, ondisk_path) == -1) {
4868 err = got_error_from_errno3("rename",
4869 path_content, ondisk_path);
4870 goto done;
4873 } else {
4874 int is_bad_symlink = 0;
4875 if (te && S_ISLNK(te->mode)) {
4876 err = install_symlink(&is_bad_symlink,
4877 a->worktree, ondisk_path, ie->path,
4878 blob, 0, 1, 0, 0, a->repo,
4879 a->progress_cb, a->progress_arg);
4880 } else {
4881 err = install_blob(a->worktree, ondisk_path,
4882 ie->path,
4883 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4884 got_fileindex_perms_to_st(ie), blob,
4885 0, 1, 0, 0, a->repo,
4886 a->progress_cb, a->progress_arg);
4888 if (err)
4889 goto done;
4890 if (status == GOT_STATUS_DELETE ||
4891 status == GOT_STATUS_MODE_CHANGE) {
4892 err = got_fileindex_entry_update(ie,
4893 a->worktree->root_fd, relpath,
4894 blob->id.sha1,
4895 a->worktree->base_commit_id->sha1, 1);
4896 if (err)
4897 goto done;
4899 if (is_bad_symlink) {
4900 got_fileindex_entry_filetype_set(ie,
4901 GOT_FILEIDX_MODE_BAD_SYMLINK);
4904 break;
4906 default:
4907 break;
4909 done:
4910 free(ondisk_path);
4911 free(path_content);
4912 free(parent_path);
4913 free(tree_path);
4914 if (fd != -1 && close(fd) == -1 && err == NULL)
4915 err = got_error_from_errno("close");
4916 if (blob)
4917 got_object_blob_close(blob);
4918 if (tree)
4919 got_object_tree_close(tree);
4920 free(tree_id);
4921 if (base_commit)
4922 got_object_commit_close(base_commit);
4923 return err;
4926 const struct got_error *
4927 got_worktree_revert(struct got_worktree *worktree,
4928 struct got_pathlist_head *paths,
4929 got_worktree_checkout_cb progress_cb, void *progress_arg,
4930 got_worktree_patch_cb patch_cb, void *patch_arg,
4931 struct got_repository *repo)
4933 struct got_fileindex *fileindex = NULL;
4934 char *fileindex_path = NULL;
4935 const struct got_error *err = NULL, *unlockerr = NULL;
4936 const struct got_error *sync_err = NULL;
4937 struct got_pathlist_entry *pe;
4938 struct revert_file_args rfa;
4940 err = lock_worktree(worktree, LOCK_EX);
4941 if (err)
4942 return err;
4944 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4945 if (err)
4946 goto done;
4948 rfa.worktree = worktree;
4949 rfa.fileindex = fileindex;
4950 rfa.progress_cb = progress_cb;
4951 rfa.progress_arg = progress_arg;
4952 rfa.patch_cb = patch_cb;
4953 rfa.patch_arg = patch_arg;
4954 rfa.repo = repo;
4955 rfa.unlink_added_files = 0;
4956 TAILQ_FOREACH(pe, paths, entry) {
4957 err = worktree_status(worktree, pe->path, fileindex, repo,
4958 revert_file, &rfa, NULL, NULL, 1, 0);
4959 if (err)
4960 break;
4962 sync_err = sync_fileindex(fileindex, fileindex_path);
4963 if (sync_err && err == NULL)
4964 err = sync_err;
4965 done:
4966 free(fileindex_path);
4967 if (fileindex)
4968 got_fileindex_free(fileindex);
4969 unlockerr = lock_worktree(worktree, LOCK_SH);
4970 if (unlockerr && err == NULL)
4971 err = unlockerr;
4972 return err;
4975 static void
4976 free_commitable(struct got_commitable *ct)
4978 free(ct->path);
4979 free(ct->in_repo_path);
4980 free(ct->ondisk_path);
4981 free(ct->blob_id);
4982 free(ct->base_blob_id);
4983 free(ct->staged_blob_id);
4984 free(ct->base_commit_id);
4985 free(ct);
4988 struct collect_commitables_arg {
4989 struct got_pathlist_head *commitable_paths;
4990 struct got_repository *repo;
4991 struct got_worktree *worktree;
4992 struct got_fileindex *fileindex;
4993 int have_staged_files;
4994 int allow_bad_symlinks;
4995 int diff_header_shown;
4996 int commit_conflicts;
4997 FILE *diff_outfile;
4998 FILE *f1;
4999 FILE *f2;
5003 * Create a file which contains the target path of a symlink so we can feed
5004 * it as content to the diff engine.
5006 static const struct got_error *
5007 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5008 const char *abspath)
5010 const struct got_error *err = NULL;
5011 char target_path[PATH_MAX];
5012 ssize_t target_len, outlen;
5014 *fd = -1;
5016 if (dirfd != -1) {
5017 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5018 if (target_len == -1)
5019 return got_error_from_errno2("readlinkat", abspath);
5020 } else {
5021 target_len = readlink(abspath, target_path, PATH_MAX);
5022 if (target_len == -1)
5023 return got_error_from_errno2("readlink", abspath);
5026 *fd = got_opentempfd();
5027 if (*fd == -1)
5028 return got_error_from_errno("got_opentempfd");
5030 outlen = write(*fd, target_path, target_len);
5031 if (outlen == -1) {
5032 err = got_error_from_errno("got_opentempfd");
5033 goto done;
5036 if (lseek(*fd, 0, SEEK_SET) == -1) {
5037 err = got_error_from_errno2("lseek", abspath);
5038 goto done;
5040 done:
5041 if (err) {
5042 close(*fd);
5043 *fd = -1;
5045 return err;
5048 static const struct got_error *
5049 append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5050 FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5051 int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5053 const struct got_error *err = NULL;
5054 struct got_blob_object *blob1 = NULL;
5055 int fd = -1, fd1 = -1, fd2 = -1;
5056 FILE *ondisk_file = NULL;
5057 char *label1 = NULL;
5058 struct stat sb;
5059 off_t size1 = 0;
5060 int f2_exists = 0;
5061 char *id_str = NULL;
5063 memset(&sb, 0, sizeof(sb));
5065 if (diff_staged) {
5066 if (ct->staged_status != GOT_STATUS_MODIFY &&
5067 ct->staged_status != GOT_STATUS_ADD &&
5068 ct->staged_status != GOT_STATUS_DELETE)
5069 return NULL;
5070 } else {
5071 if (ct->status != GOT_STATUS_MODIFY &&
5072 ct->status != GOT_STATUS_ADD &&
5073 ct->status != GOT_STATUS_DELETE &&
5074 ct->status != GOT_STATUS_CONFLICT)
5075 return NULL;
5078 err = got_opentemp_truncate(f1);
5079 if (err)
5080 return got_error_from_errno("got_opentemp_truncate");
5081 err = got_opentemp_truncate(f2);
5082 if (err)
5083 return got_error_from_errno("got_opentemp_truncate");
5085 if (!*diff_header_shown) {
5086 err = got_object_id_str(&id_str, worktree->base_commit_id);
5087 if (err)
5088 return err;
5089 fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5090 got_worktree_get_root_path(worktree));
5091 fprintf(diff_outfile, "commit - %s\n", id_str);
5092 fprintf(diff_outfile, "path + %s%s\n",
5093 got_worktree_get_root_path(worktree),
5094 diff_staged ? " (staged changes)" : "");
5095 *diff_header_shown = 1;
5098 if (diff_staged) {
5099 const char *label1 = NULL, *label2 = NULL;
5100 switch (ct->staged_status) {
5101 case GOT_STATUS_MODIFY:
5102 label1 = ct->path;
5103 label2 = ct->path;
5104 break;
5105 case GOT_STATUS_ADD:
5106 label2 = ct->path;
5107 break;
5108 case GOT_STATUS_DELETE:
5109 label1 = ct->path;
5110 break;
5111 default:
5112 return got_error(GOT_ERR_FILE_STATUS);
5114 fd1 = got_opentempfd();
5115 if (fd1 == -1) {
5116 err = got_error_from_errno("got_opentempfd");
5117 goto done;
5119 fd2 = got_opentempfd();
5120 if (fd2 == -1) {
5121 err = got_error_from_errno("got_opentempfd");
5122 goto done;
5124 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5125 fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5126 label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5127 NULL, repo, diff_outfile);
5128 goto done;
5131 fd1 = got_opentempfd();
5132 if (fd1 == -1) {
5133 err = got_error_from_errno("got_opentempfd");
5134 goto done;
5137 if (ct->status != GOT_STATUS_ADD) {
5138 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5139 8192, fd1);
5140 if (err)
5141 goto done;
5144 if (ct->status != GOT_STATUS_DELETE) {
5145 if (dirfd != -1) {
5146 fd = openat(dirfd, de_name,
5147 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5148 if (fd == -1) {
5149 if (!got_err_open_nofollow_on_symlink()) {
5150 err = got_error_from_errno2("openat",
5151 ct->ondisk_path);
5152 goto done;
5154 err = get_symlink_target_file(&fd, dirfd,
5155 de_name, ct->ondisk_path);
5156 if (err)
5157 goto done;
5159 } else {
5160 fd = open(ct->ondisk_path,
5161 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5162 if (fd == -1) {
5163 if (!got_err_open_nofollow_on_symlink()) {
5164 err = got_error_from_errno2("open",
5165 ct->ondisk_path);
5166 goto done;
5168 err = get_symlink_target_file(&fd, dirfd,
5169 de_name, ct->ondisk_path);
5170 if (err)
5171 goto done;
5174 if (fstatat(fd, ct->ondisk_path, &sb,
5175 AT_SYMLINK_NOFOLLOW) == -1) {
5176 err = got_error_from_errno2("fstatat", ct->ondisk_path);
5177 goto done;
5179 ondisk_file = fdopen(fd, "r");
5180 if (ondisk_file == NULL) {
5181 err = got_error_from_errno2("fdopen", ct->ondisk_path);
5182 goto done;
5184 fd = -1;
5185 f2_exists = 1;
5188 if (blob1) {
5189 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5190 f1, blob1);
5191 if (err)
5192 goto done;
5195 err = got_diff_blob_file(blob1, f1, size1, label1,
5196 ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5197 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5198 done:
5199 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5200 err = got_error_from_errno("close");
5201 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5202 err = got_error_from_errno("close");
5203 if (blob1)
5204 got_object_blob_close(blob1);
5205 if (fd != -1 && close(fd) == -1 && err == NULL)
5206 err = got_error_from_errno("close");
5207 if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5208 err = got_error_from_errno("fclose");
5209 return err;
5212 static const struct got_error *
5213 collect_commitables(void *arg, unsigned char status,
5214 unsigned char staged_status, const char *relpath,
5215 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5216 struct got_object_id *commit_id, int dirfd, const char *de_name)
5218 struct collect_commitables_arg *a = arg;
5219 const struct got_error *err = NULL;
5220 struct got_commitable *ct = NULL;
5221 struct got_pathlist_entry *new = NULL;
5222 char *parent_path = NULL, *path = NULL;
5223 struct stat sb;
5225 if (a->have_staged_files) {
5226 if (staged_status != GOT_STATUS_MODIFY &&
5227 staged_status != GOT_STATUS_ADD &&
5228 staged_status != GOT_STATUS_DELETE)
5229 return NULL;
5230 } else {
5231 if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5232 printf("C %s\n", relpath);
5233 return got_error(GOT_ERR_COMMIT_CONFLICT);
5236 if (status != GOT_STATUS_MODIFY &&
5237 status != GOT_STATUS_MODE_CHANGE &&
5238 status != GOT_STATUS_ADD &&
5239 status != GOT_STATUS_DELETE &&
5240 status != GOT_STATUS_CONFLICT)
5241 return NULL;
5244 if (asprintf(&path, "/%s", relpath) == -1) {
5245 err = got_error_from_errno("asprintf");
5246 goto done;
5248 if (strcmp(path, "/") == 0) {
5249 parent_path = strdup("");
5250 if (parent_path == NULL)
5251 return got_error_from_errno("strdup");
5252 } else {
5253 err = got_path_dirname(&parent_path, path);
5254 if (err)
5255 return err;
5258 ct = calloc(1, sizeof(*ct));
5259 if (ct == NULL) {
5260 err = got_error_from_errno("calloc");
5261 goto done;
5264 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5265 relpath) == -1) {
5266 err = got_error_from_errno("asprintf");
5267 goto done;
5270 if (staged_status == GOT_STATUS_ADD ||
5271 staged_status == GOT_STATUS_MODIFY) {
5272 struct got_fileindex_entry *ie;
5273 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5274 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5275 case GOT_FILEIDX_MODE_REGULAR_FILE:
5276 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5277 ct->mode = S_IFREG;
5278 break;
5279 case GOT_FILEIDX_MODE_SYMLINK:
5280 ct->mode = S_IFLNK;
5281 break;
5282 default:
5283 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5284 goto done;
5286 ct->mode |= got_fileindex_entry_perms_get(ie);
5287 } else if (status != GOT_STATUS_DELETE &&
5288 staged_status != GOT_STATUS_DELETE) {
5289 if (dirfd != -1) {
5290 if (fstatat(dirfd, de_name, &sb,
5291 AT_SYMLINK_NOFOLLOW) == -1) {
5292 err = got_error_from_errno2("fstatat",
5293 ct->ondisk_path);
5294 goto done;
5296 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5297 err = got_error_from_errno2("lstat", ct->ondisk_path);
5298 goto done;
5300 ct->mode = sb.st_mode;
5303 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5304 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5305 relpath) == -1) {
5306 err = got_error_from_errno("asprintf");
5307 goto done;
5310 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5311 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5312 int is_bad_symlink;
5313 char target_path[PATH_MAX];
5314 ssize_t target_len;
5315 target_len = readlink(ct->ondisk_path, target_path,
5316 sizeof(target_path));
5317 if (target_len == -1) {
5318 err = got_error_from_errno2("readlink",
5319 ct->ondisk_path);
5320 goto done;
5322 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5323 target_len, ct->ondisk_path, a->worktree->root_path);
5324 if (err)
5325 goto done;
5326 if (is_bad_symlink) {
5327 err = got_error_path(ct->ondisk_path,
5328 GOT_ERR_BAD_SYMLINK);
5329 goto done;
5334 ct->status = status;
5335 ct->staged_status = staged_status;
5336 ct->blob_id = NULL; /* will be filled in when blob gets created */
5337 if (ct->status != GOT_STATUS_ADD &&
5338 ct->staged_status != GOT_STATUS_ADD) {
5339 ct->base_blob_id = got_object_id_dup(blob_id);
5340 if (ct->base_blob_id == NULL) {
5341 err = got_error_from_errno("got_object_id_dup");
5342 goto done;
5344 ct->base_commit_id = got_object_id_dup(commit_id);
5345 if (ct->base_commit_id == NULL) {
5346 err = got_error_from_errno("got_object_id_dup");
5347 goto done;
5350 if (ct->staged_status == GOT_STATUS_ADD ||
5351 ct->staged_status == GOT_STATUS_MODIFY) {
5352 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5353 if (ct->staged_blob_id == NULL) {
5354 err = got_error_from_errno("got_object_id_dup");
5355 goto done;
5358 ct->path = strdup(path);
5359 if (ct->path == NULL) {
5360 err = got_error_from_errno("strdup");
5361 goto done;
5363 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5364 if (err)
5365 goto done;
5367 if (a->diff_outfile && ct && new != NULL) {
5368 err = append_ct_diff(ct, &a->diff_header_shown,
5369 a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5370 a->have_staged_files, a->repo, a->worktree);
5371 if (err)
5372 goto done;
5374 done:
5375 if (ct && (err || new == NULL))
5376 free_commitable(ct);
5377 free(parent_path);
5378 free(path);
5379 return err;
5382 static const struct got_error *write_tree(struct got_object_id **, int *,
5383 struct got_tree_object *, const char *, struct got_pathlist_head *,
5384 got_worktree_status_cb status_cb, void *status_arg,
5385 struct got_repository *);
5387 static const struct got_error *
5388 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5389 struct got_tree_entry *te, const char *parent_path,
5390 struct got_pathlist_head *commitable_paths,
5391 got_worktree_status_cb status_cb, void *status_arg,
5392 struct got_repository *repo)
5394 const struct got_error *err = NULL;
5395 struct got_tree_object *subtree;
5396 char *subpath;
5398 if (asprintf(&subpath, "%s%s%s", parent_path,
5399 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5400 return got_error_from_errno("asprintf");
5402 err = got_object_open_as_tree(&subtree, repo, &te->id);
5403 if (err)
5404 return err;
5406 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5407 commitable_paths, status_cb, status_arg, repo);
5408 got_object_tree_close(subtree);
5409 free(subpath);
5410 return err;
5413 static const struct got_error *
5414 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5416 const struct got_error *err = NULL;
5417 char *ct_parent_path = NULL;
5419 *match = 0;
5421 if (strchr(ct->in_repo_path, '/') == NULL) {
5422 *match = got_path_is_root_dir(path);
5423 return NULL;
5426 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5427 if (err)
5428 return err;
5429 *match = (strcmp(path, ct_parent_path) == 0);
5430 free(ct_parent_path);
5431 return err;
5434 static mode_t
5435 get_ct_file_mode(struct got_commitable *ct)
5437 if (S_ISLNK(ct->mode))
5438 return S_IFLNK;
5440 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5443 static const struct got_error *
5444 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5445 struct got_tree_entry *te, struct got_commitable *ct)
5447 const struct got_error *err = NULL;
5449 *new_te = NULL;
5451 err = got_object_tree_entry_dup(new_te, te);
5452 if (err)
5453 goto done;
5455 (*new_te)->mode = get_ct_file_mode(ct);
5457 if (ct->staged_status == GOT_STATUS_MODIFY)
5458 memcpy(&(*new_te)->id, ct->staged_blob_id,
5459 sizeof((*new_te)->id));
5460 else
5461 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5462 done:
5463 if (err && *new_te) {
5464 free(*new_te);
5465 *new_te = NULL;
5467 return err;
5470 static const struct got_error *
5471 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5472 struct got_commitable *ct)
5474 const struct got_error *err = NULL;
5475 char *ct_name = NULL;
5477 *new_te = NULL;
5479 *new_te = calloc(1, sizeof(**new_te));
5480 if (*new_te == NULL)
5481 return got_error_from_errno("calloc");
5483 err = got_path_basename(&ct_name, ct->path);
5484 if (err)
5485 goto done;
5486 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5487 sizeof((*new_te)->name)) {
5488 err = got_error(GOT_ERR_NO_SPACE);
5489 goto done;
5492 (*new_te)->mode = get_ct_file_mode(ct);
5494 if (ct->staged_status == GOT_STATUS_ADD)
5495 memcpy(&(*new_te)->id, ct->staged_blob_id,
5496 sizeof((*new_te)->id));
5497 else
5498 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5499 done:
5500 free(ct_name);
5501 if (err && *new_te) {
5502 free(*new_te);
5503 *new_te = NULL;
5505 return err;
5508 static const struct got_error *
5509 insert_tree_entry(struct got_tree_entry *new_te,
5510 struct got_pathlist_head *paths)
5512 const struct got_error *err = NULL;
5513 struct got_pathlist_entry *new_pe;
5515 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5516 if (err)
5517 return err;
5518 if (new_pe == NULL)
5519 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5520 return NULL;
5523 static const struct got_error *
5524 report_ct_status(struct got_commitable *ct,
5525 got_worktree_status_cb status_cb, void *status_arg)
5527 const char *ct_path = ct->path;
5528 unsigned char status;
5530 if (status_cb == NULL) /* no commit progress output desired */
5531 return NULL;
5533 while (ct_path[0] == '/')
5534 ct_path++;
5536 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5537 status = ct->staged_status;
5538 else
5539 status = ct->status;
5541 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5542 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5545 static const struct got_error *
5546 match_modified_subtree(int *modified, struct got_tree_entry *te,
5547 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5549 const struct got_error *err = NULL;
5550 struct got_pathlist_entry *pe;
5551 char *te_path;
5553 *modified = 0;
5555 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5556 got_path_is_root_dir(base_tree_path) ? "" : "/",
5557 te->name) == -1)
5558 return got_error_from_errno("asprintf");
5560 TAILQ_FOREACH(pe, commitable_paths, entry) {
5561 struct got_commitable *ct = pe->data;
5562 *modified = got_path_is_child(ct->in_repo_path, te_path,
5563 strlen(te_path));
5564 if (*modified)
5565 break;
5568 free(te_path);
5569 return err;
5572 static const struct got_error *
5573 match_deleted_or_modified_ct(struct got_commitable **ctp,
5574 struct got_tree_entry *te, const char *base_tree_path,
5575 struct got_pathlist_head *commitable_paths)
5577 const struct got_error *err = NULL;
5578 struct got_pathlist_entry *pe;
5580 *ctp = NULL;
5582 TAILQ_FOREACH(pe, commitable_paths, entry) {
5583 struct got_commitable *ct = pe->data;
5584 char *ct_name = NULL;
5585 int path_matches;
5587 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5588 if (ct->status != GOT_STATUS_MODIFY &&
5589 ct->status != GOT_STATUS_MODE_CHANGE &&
5590 ct->status != GOT_STATUS_DELETE &&
5591 ct->status != GOT_STATUS_CONFLICT)
5592 continue;
5593 } else {
5594 if (ct->staged_status != GOT_STATUS_MODIFY &&
5595 ct->staged_status != GOT_STATUS_DELETE)
5596 continue;
5599 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5600 continue;
5602 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5603 if (err)
5604 return err;
5605 if (!path_matches)
5606 continue;
5608 err = got_path_basename(&ct_name, pe->path);
5609 if (err)
5610 return err;
5612 if (strcmp(te->name, ct_name) != 0) {
5613 free(ct_name);
5614 continue;
5616 free(ct_name);
5618 *ctp = ct;
5619 break;
5622 return err;
5625 static const struct got_error *
5626 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5627 const char *child_path, const char *path_base_tree,
5628 struct got_pathlist_head *commitable_paths,
5629 got_worktree_status_cb status_cb, void *status_arg,
5630 struct got_repository *repo)
5632 const struct got_error *err = NULL;
5633 struct got_tree_entry *new_te;
5634 char *subtree_path;
5635 struct got_object_id *id = NULL;
5636 int nentries;
5638 *new_tep = NULL;
5640 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5641 got_path_is_root_dir(path_base_tree) ? "" : "/",
5642 child_path) == -1)
5643 return got_error_from_errno("asprintf");
5645 new_te = calloc(1, sizeof(*new_te));
5646 if (new_te == NULL)
5647 return got_error_from_errno("calloc");
5648 new_te->mode = S_IFDIR;
5650 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5651 sizeof(new_te->name)) {
5652 err = got_error(GOT_ERR_NO_SPACE);
5653 goto done;
5655 err = write_tree(&id, &nentries, NULL, subtree_path,
5656 commitable_paths, status_cb, status_arg, repo);
5657 if (err) {
5658 free(new_te);
5659 goto done;
5661 memcpy(&new_te->id, id, sizeof(new_te->id));
5662 done:
5663 free(id);
5664 free(subtree_path);
5665 if (err == NULL)
5666 *new_tep = new_te;
5667 return err;
5670 static const struct got_error *
5671 write_tree(struct got_object_id **new_tree_id, int *nentries,
5672 struct got_tree_object *base_tree, const char *path_base_tree,
5673 struct got_pathlist_head *commitable_paths,
5674 got_worktree_status_cb status_cb, void *status_arg,
5675 struct got_repository *repo)
5677 const struct got_error *err = NULL;
5678 struct got_pathlist_head paths;
5679 struct got_tree_entry *te, *new_te = NULL;
5680 struct got_pathlist_entry *pe;
5682 TAILQ_INIT(&paths);
5683 *nentries = 0;
5685 /* Insert, and recurse into, newly added entries first. */
5686 TAILQ_FOREACH(pe, commitable_paths, entry) {
5687 struct got_commitable *ct = pe->data;
5688 char *child_path = NULL, *slash;
5690 if ((ct->status != GOT_STATUS_ADD &&
5691 ct->staged_status != GOT_STATUS_ADD) ||
5692 (ct->flags & GOT_COMMITABLE_ADDED))
5693 continue;
5695 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5696 strlen(path_base_tree)))
5697 continue;
5699 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5700 ct->in_repo_path);
5701 if (err)
5702 goto done;
5704 slash = strchr(child_path, '/');
5705 if (slash == NULL) {
5706 err = alloc_added_blob_tree_entry(&new_te, ct);
5707 if (err)
5708 goto done;
5709 err = report_ct_status(ct, status_cb, status_arg);
5710 if (err)
5711 goto done;
5712 ct->flags |= GOT_COMMITABLE_ADDED;
5713 err = insert_tree_entry(new_te, &paths);
5714 if (err)
5715 goto done;
5716 (*nentries)++;
5717 } else {
5718 *slash = '\0'; /* trim trailing path components */
5719 if (base_tree == NULL ||
5720 got_object_tree_find_entry(base_tree, child_path)
5721 == NULL) {
5722 err = make_subtree_for_added_blob(&new_te,
5723 child_path, path_base_tree,
5724 commitable_paths, status_cb, status_arg,
5725 repo);
5726 if (err)
5727 goto done;
5728 err = insert_tree_entry(new_te, &paths);
5729 if (err)
5730 goto done;
5731 (*nentries)++;
5736 if (base_tree) {
5737 int i, nbase_entries;
5738 /* Handle modified and deleted entries. */
5739 nbase_entries = got_object_tree_get_nentries(base_tree);
5740 for (i = 0; i < nbase_entries; i++) {
5741 struct got_commitable *ct = NULL;
5743 te = got_object_tree_get_entry(base_tree, i);
5744 if (got_object_tree_entry_is_submodule(te)) {
5745 /* Entry is a submodule; just copy it. */
5746 err = got_object_tree_entry_dup(&new_te, te);
5747 if (err)
5748 goto done;
5749 err = insert_tree_entry(new_te, &paths);
5750 if (err)
5751 goto done;
5752 (*nentries)++;
5753 continue;
5756 if (S_ISDIR(te->mode)) {
5757 int modified;
5758 err = got_object_tree_entry_dup(&new_te, te);
5759 if (err)
5760 goto done;
5761 err = match_modified_subtree(&modified, te,
5762 path_base_tree, commitable_paths);
5763 if (err)
5764 goto done;
5765 /* Avoid recursion into unmodified subtrees. */
5766 if (modified) {
5767 struct got_object_id *new_id;
5768 int nsubentries;
5769 err = write_subtree(&new_id,
5770 &nsubentries, te,
5771 path_base_tree, commitable_paths,
5772 status_cb, status_arg, repo);
5773 if (err)
5774 goto done;
5775 if (nsubentries == 0) {
5776 /* All entries were deleted. */
5777 free(new_id);
5778 continue;
5780 memcpy(&new_te->id, new_id,
5781 sizeof(new_te->id));
5782 free(new_id);
5784 err = insert_tree_entry(new_te, &paths);
5785 if (err)
5786 goto done;
5787 (*nentries)++;
5788 continue;
5791 err = match_deleted_or_modified_ct(&ct, te,
5792 path_base_tree, commitable_paths);
5793 if (err)
5794 goto done;
5795 if (ct) {
5796 /* NB: Deleted entries get dropped here. */
5797 if (ct->status == GOT_STATUS_MODIFY ||
5798 ct->status == GOT_STATUS_MODE_CHANGE ||
5799 ct->status == GOT_STATUS_CONFLICT ||
5800 ct->staged_status == GOT_STATUS_MODIFY) {
5801 err = alloc_modified_blob_tree_entry(
5802 &new_te, te, ct);
5803 if (err)
5804 goto done;
5805 err = insert_tree_entry(new_te, &paths);
5806 if (err)
5807 goto done;
5808 (*nentries)++;
5810 err = report_ct_status(ct, status_cb,
5811 status_arg);
5812 if (err)
5813 goto done;
5814 } else {
5815 /* Entry is unchanged; just copy it. */
5816 err = got_object_tree_entry_dup(&new_te, te);
5817 if (err)
5818 goto done;
5819 err = insert_tree_entry(new_te, &paths);
5820 if (err)
5821 goto done;
5822 (*nentries)++;
5827 /* Write new list of entries; deleted entries have been dropped. */
5828 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5829 done:
5830 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
5831 return err;
5834 static const struct got_error *
5835 update_fileindex_after_commit(struct got_worktree *worktree,
5836 struct got_pathlist_head *commitable_paths,
5837 struct got_object_id *new_base_commit_id,
5838 struct got_fileindex *fileindex, int have_staged_files)
5840 const struct got_error *err = NULL;
5841 struct got_pathlist_entry *pe;
5842 char *relpath = NULL;
5844 TAILQ_FOREACH(pe, commitable_paths, entry) {
5845 struct got_fileindex_entry *ie;
5846 struct got_commitable *ct = pe->data;
5848 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5850 err = got_path_skip_common_ancestor(&relpath,
5851 worktree->root_path, ct->ondisk_path);
5852 if (err)
5853 goto done;
5855 if (ie) {
5856 if (ct->status == GOT_STATUS_DELETE ||
5857 ct->staged_status == GOT_STATUS_DELETE) {
5858 got_fileindex_entry_remove(fileindex, ie);
5859 } else if (ct->staged_status == GOT_STATUS_ADD ||
5860 ct->staged_status == GOT_STATUS_MODIFY) {
5861 got_fileindex_entry_stage_set(ie,
5862 GOT_FILEIDX_STAGE_NONE);
5863 got_fileindex_entry_staged_filetype_set(ie, 0);
5865 err = got_fileindex_entry_update(ie,
5866 worktree->root_fd, relpath,
5867 ct->staged_blob_id->sha1,
5868 new_base_commit_id->sha1,
5869 !have_staged_files);
5870 } else
5871 err = got_fileindex_entry_update(ie,
5872 worktree->root_fd, relpath,
5873 ct->blob_id->sha1,
5874 new_base_commit_id->sha1,
5875 !have_staged_files);
5876 } else {
5877 err = got_fileindex_entry_alloc(&ie, pe->path);
5878 if (err)
5879 goto done;
5880 err = got_fileindex_entry_update(ie,
5881 worktree->root_fd, relpath, ct->blob_id->sha1,
5882 new_base_commit_id->sha1, 1);
5883 if (err) {
5884 got_fileindex_entry_free(ie);
5885 goto done;
5887 err = got_fileindex_entry_add(fileindex, ie);
5888 if (err) {
5889 got_fileindex_entry_free(ie);
5890 goto done;
5893 free(relpath);
5894 relpath = NULL;
5896 done:
5897 free(relpath);
5898 return err;
5902 static const struct got_error *
5903 check_out_of_date(const char *in_repo_path, unsigned char status,
5904 unsigned char staged_status, struct got_object_id *base_blob_id,
5905 struct got_object_id *base_commit_id,
5906 struct got_object_id *head_commit_id, struct got_repository *repo,
5907 int ood_errcode)
5909 const struct got_error *err = NULL;
5910 struct got_commit_object *commit = NULL;
5911 struct got_object_id *id = NULL;
5913 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5914 /* Trivial case: base commit == head commit */
5915 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5916 return NULL;
5918 * Ensure file content which local changes were based
5919 * on matches file content in the branch head.
5921 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5922 if (err)
5923 goto done;
5924 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5925 if (err) {
5926 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5927 err = got_error(ood_errcode);
5928 goto done;
5929 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5930 err = got_error(ood_errcode);
5931 } else {
5932 /* Require that added files don't exist in the branch head. */
5933 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5934 if (err)
5935 goto done;
5936 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5937 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5938 goto done;
5939 err = id ? got_error(ood_errcode) : NULL;
5941 done:
5942 free(id);
5943 if (commit)
5944 got_object_commit_close(commit);
5945 return err;
5948 static const struct got_error *
5949 commit_worktree(struct got_object_id **new_commit_id,
5950 struct got_pathlist_head *commitable_paths,
5951 struct got_object_id *head_commit_id,
5952 struct got_object_id *parent_id2,
5953 struct got_worktree *worktree,
5954 const char *author, const char *committer, char *diff_path,
5955 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5956 got_worktree_status_cb status_cb, void *status_arg,
5957 struct got_repository *repo)
5959 const struct got_error *err = NULL, *unlockerr = NULL;
5960 struct got_pathlist_entry *pe;
5961 const char *head_ref_name = NULL;
5962 struct got_commit_object *head_commit = NULL;
5963 struct got_reference *head_ref2 = NULL;
5964 struct got_object_id *head_commit_id2 = NULL;
5965 struct got_tree_object *head_tree = NULL;
5966 struct got_object_id *new_tree_id = NULL;
5967 int nentries, nparents = 0;
5968 struct got_object_id_queue parent_ids;
5969 struct got_object_qid *pid = NULL;
5970 char *logmsg = NULL;
5971 time_t timestamp;
5973 *new_commit_id = NULL;
5975 STAILQ_INIT(&parent_ids);
5977 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5978 if (err)
5979 goto done;
5981 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5982 if (err)
5983 goto done;
5985 if (commit_msg_cb != NULL) {
5986 err = commit_msg_cb(commitable_paths, diff_path,
5987 &logmsg, commit_arg);
5988 if (err)
5989 goto done;
5992 if (logmsg == NULL || strlen(logmsg) == 0) {
5993 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5994 goto done;
5997 /* Create blobs from added and modified files and record their IDs. */
5998 TAILQ_FOREACH(pe, commitable_paths, entry) {
5999 struct got_commitable *ct = pe->data;
6000 char *ondisk_path;
6002 /* Blobs for staged files already exist. */
6003 if (ct->staged_status == GOT_STATUS_ADD ||
6004 ct->staged_status == GOT_STATUS_MODIFY)
6005 continue;
6007 if (ct->status != GOT_STATUS_ADD &&
6008 ct->status != GOT_STATUS_MODIFY &&
6009 ct->status != GOT_STATUS_MODE_CHANGE &&
6010 ct->status != GOT_STATUS_CONFLICT)
6011 continue;
6013 if (asprintf(&ondisk_path, "%s/%s",
6014 worktree->root_path, pe->path) == -1) {
6015 err = got_error_from_errno("asprintf");
6016 goto done;
6018 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6019 free(ondisk_path);
6020 if (err)
6021 goto done;
6024 /* Recursively write new tree objects. */
6025 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6026 commitable_paths, status_cb, status_arg, repo);
6027 if (err)
6028 goto done;
6030 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
6031 if (err)
6032 goto done;
6033 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6034 nparents++;
6035 if (parent_id2) {
6036 err = got_object_qid_alloc(&pid, parent_id2);
6037 if (err)
6038 goto done;
6039 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6040 nparents++;
6042 timestamp = time(NULL);
6043 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6044 nparents, author, timestamp, committer, timestamp, logmsg, repo);
6045 if (logmsg != NULL)
6046 free(logmsg);
6047 if (err)
6048 goto done;
6050 /* Check if a concurrent commit to our branch has occurred. */
6051 head_ref_name = got_worktree_get_head_ref_name(worktree);
6052 if (head_ref_name == NULL) {
6053 err = got_error_from_errno("got_worktree_get_head_ref_name");
6054 goto done;
6056 /* Lock the reference here to prevent concurrent modification. */
6057 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6058 if (err)
6059 goto done;
6060 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6061 if (err)
6062 goto done;
6063 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6064 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6065 goto done;
6067 /* Update branch head in repository. */
6068 err = got_ref_change_ref(head_ref2, *new_commit_id);
6069 if (err)
6070 goto done;
6071 err = got_ref_write(head_ref2, repo);
6072 if (err)
6073 goto done;
6075 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6076 if (err)
6077 goto done;
6079 err = ref_base_commit(worktree, repo);
6080 if (err)
6081 goto done;
6082 done:
6083 got_object_id_queue_free(&parent_ids);
6084 if (head_tree)
6085 got_object_tree_close(head_tree);
6086 if (head_commit)
6087 got_object_commit_close(head_commit);
6088 free(head_commit_id2);
6089 if (head_ref2) {
6090 unlockerr = got_ref_unlock(head_ref2);
6091 if (unlockerr && err == NULL)
6092 err = unlockerr;
6093 got_ref_close(head_ref2);
6095 return err;
6098 static const struct got_error *
6099 check_path_is_commitable(const char *path,
6100 struct got_pathlist_head *commitable_paths)
6102 struct got_pathlist_entry *cpe = NULL;
6103 size_t path_len = strlen(path);
6105 TAILQ_FOREACH(cpe, commitable_paths, entry) {
6106 struct got_commitable *ct = cpe->data;
6107 const char *ct_path = ct->path;
6109 while (ct_path[0] == '/')
6110 ct_path++;
6112 if (strcmp(path, ct_path) == 0 ||
6113 got_path_is_child(ct_path, path, path_len))
6114 break;
6117 if (cpe == NULL)
6118 return got_error_path(path, GOT_ERR_BAD_PATH);
6120 return NULL;
6123 static const struct got_error *
6124 check_staged_file(void *arg, struct got_fileindex_entry *ie)
6126 int *have_staged_files = arg;
6128 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6129 *have_staged_files = 1;
6130 return got_error(GOT_ERR_CANCELLED);
6133 return NULL;
6136 static const struct got_error *
6137 check_non_staged_files(struct got_fileindex *fileindex,
6138 struct got_pathlist_head *paths)
6140 struct got_pathlist_entry *pe;
6141 struct got_fileindex_entry *ie;
6143 TAILQ_FOREACH(pe, paths, entry) {
6144 if (pe->path[0] == '\0')
6145 continue;
6146 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6147 if (ie == NULL)
6148 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6149 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6150 return got_error_path(pe->path,
6151 GOT_ERR_FILE_NOT_STAGED);
6154 return NULL;
6157 const struct got_error *
6158 got_worktree_commit(struct got_object_id **new_commit_id,
6159 struct got_worktree *worktree, struct got_pathlist_head *paths,
6160 const char *author, const char *committer, int allow_bad_symlinks,
6161 int show_diff, int commit_conflicts,
6162 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6163 got_worktree_status_cb status_cb, void *status_arg,
6164 struct got_repository *repo)
6166 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6167 struct got_fileindex *fileindex = NULL;
6168 char *fileindex_path = NULL;
6169 struct got_pathlist_head commitable_paths;
6170 struct collect_commitables_arg cc_arg;
6171 struct got_pathlist_entry *pe;
6172 struct got_reference *head_ref = NULL;
6173 struct got_object_id *head_commit_id = NULL;
6174 char *diff_path = NULL;
6175 int have_staged_files = 0;
6177 *new_commit_id = NULL;
6179 memset(&cc_arg, 0, sizeof(cc_arg));
6180 TAILQ_INIT(&commitable_paths);
6182 err = lock_worktree(worktree, LOCK_EX);
6183 if (err)
6184 goto done;
6186 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6187 if (err)
6188 goto done;
6190 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6191 if (err)
6192 goto done;
6194 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6195 if (err)
6196 goto done;
6198 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6199 &have_staged_files);
6200 if (err && err->code != GOT_ERR_CANCELLED)
6201 goto done;
6202 if (have_staged_files) {
6203 err = check_non_staged_files(fileindex, paths);
6204 if (err)
6205 goto done;
6208 cc_arg.commitable_paths = &commitable_paths;
6209 cc_arg.worktree = worktree;
6210 cc_arg.fileindex = fileindex;
6211 cc_arg.repo = repo;
6212 cc_arg.have_staged_files = have_staged_files;
6213 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6214 cc_arg.diff_header_shown = 0;
6215 cc_arg.commit_conflicts = commit_conflicts;
6216 if (show_diff) {
6217 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6218 GOT_TMPDIR_STR "/got", ".diff");
6219 if (err)
6220 goto done;
6221 cc_arg.f1 = got_opentemp();
6222 if (cc_arg.f1 == NULL) {
6223 err = got_error_from_errno("got_opentemp");
6224 goto done;
6226 cc_arg.f2 = got_opentemp();
6227 if (cc_arg.f2 == NULL) {
6228 err = got_error_from_errno("got_opentemp");
6229 goto done;
6233 TAILQ_FOREACH(pe, paths, entry) {
6234 err = worktree_status(worktree, pe->path, fileindex, repo,
6235 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6236 if (err)
6237 goto done;
6240 if (show_diff) {
6241 if (fflush(cc_arg.diff_outfile) == EOF) {
6242 err = got_error_from_errno("fflush");
6243 goto done;
6247 if (TAILQ_EMPTY(&commitable_paths)) {
6248 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6249 goto done;
6252 TAILQ_FOREACH(pe, paths, entry) {
6253 err = check_path_is_commitable(pe->path, &commitable_paths);
6254 if (err)
6255 goto done;
6258 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6259 struct got_commitable *ct = pe->data;
6260 const char *ct_path = ct->in_repo_path;
6262 while (ct_path[0] == '/')
6263 ct_path++;
6264 err = check_out_of_date(ct_path, ct->status,
6265 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6266 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6267 if (err)
6268 goto done;
6272 err = commit_worktree(new_commit_id, &commitable_paths,
6273 head_commit_id, NULL, worktree, author, committer,
6274 (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6275 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6276 if (err)
6277 goto done;
6279 err = update_fileindex_after_commit(worktree, &commitable_paths,
6280 *new_commit_id, fileindex, have_staged_files);
6281 sync_err = sync_fileindex(fileindex, fileindex_path);
6282 if (sync_err && err == NULL)
6283 err = sync_err;
6284 done:
6285 if (fileindex)
6286 got_fileindex_free(fileindex);
6287 free(fileindex_path);
6288 unlockerr = lock_worktree(worktree, LOCK_SH);
6289 if (unlockerr && err == NULL)
6290 err = unlockerr;
6291 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6292 struct got_commitable *ct = pe->data;
6294 free_commitable(ct);
6296 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6297 if (diff_path && unlink(diff_path) == -1 && err == NULL)
6298 err = got_error_from_errno2("unlink", diff_path);
6299 free(diff_path);
6300 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6301 err == NULL)
6302 err = got_error_from_errno("fclose");
6303 return err;
6306 const char *
6307 got_commitable_get_path(struct got_commitable *ct)
6309 return ct->path;
6312 unsigned int
6313 got_commitable_get_status(struct got_commitable *ct)
6315 return ct->status;
6318 struct check_rebase_ok_arg {
6319 struct got_worktree *worktree;
6320 struct got_repository *repo;
6323 static const struct got_error *
6324 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6326 const struct got_error *err = NULL;
6327 struct check_rebase_ok_arg *a = arg;
6328 unsigned char status;
6329 struct stat sb;
6330 char *ondisk_path;
6332 /* Reject rebase of a work tree with mixed base commits. */
6333 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6334 SHA1_DIGEST_LENGTH))
6335 return got_error(GOT_ERR_MIXED_COMMITS);
6337 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6338 == -1)
6339 return got_error_from_errno("asprintf");
6341 /* Reject rebase of a work tree with modified or staged files. */
6342 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6343 free(ondisk_path);
6344 if (err)
6345 return err;
6347 if (status != GOT_STATUS_NO_CHANGE)
6348 return got_error(GOT_ERR_MODIFIED);
6349 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6350 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6352 return NULL;
6355 const struct got_error *
6356 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6357 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6358 struct got_worktree *worktree, struct got_reference *branch,
6359 struct got_repository *repo)
6361 const struct got_error *err = NULL;
6362 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6363 char *branch_ref_name = NULL;
6364 char *fileindex_path = NULL;
6365 struct check_rebase_ok_arg ok_arg;
6366 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6367 struct got_object_id *wt_branch_tip = NULL;
6369 *new_base_branch_ref = NULL;
6370 *tmp_branch = NULL;
6371 *fileindex = NULL;
6373 err = lock_worktree(worktree, LOCK_EX);
6374 if (err)
6375 return err;
6377 err = open_fileindex(fileindex, &fileindex_path, worktree);
6378 if (err)
6379 goto done;
6381 ok_arg.worktree = worktree;
6382 ok_arg.repo = repo;
6383 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6384 &ok_arg);
6385 if (err)
6386 goto done;
6388 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6389 if (err)
6390 goto done;
6392 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6393 if (err)
6394 goto done;
6396 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6397 if (err)
6398 goto done;
6400 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6401 0);
6402 if (err)
6403 goto done;
6405 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6406 if (err)
6407 goto done;
6408 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6409 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6410 goto done;
6413 err = got_ref_alloc_symref(new_base_branch_ref,
6414 new_base_branch_ref_name, wt_branch);
6415 if (err)
6416 goto done;
6417 err = got_ref_write(*new_base_branch_ref, repo);
6418 if (err)
6419 goto done;
6421 /* TODO Lock original branch's ref while rebasing? */
6423 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6424 if (err)
6425 goto done;
6427 err = got_ref_write(branch_ref, repo);
6428 if (err)
6429 goto done;
6431 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6432 worktree->base_commit_id);
6433 if (err)
6434 goto done;
6435 err = got_ref_write(*tmp_branch, repo);
6436 if (err)
6437 goto done;
6439 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6440 if (err)
6441 goto done;
6442 done:
6443 free(fileindex_path);
6444 free(tmp_branch_name);
6445 free(new_base_branch_ref_name);
6446 free(branch_ref_name);
6447 if (branch_ref)
6448 got_ref_close(branch_ref);
6449 if (wt_branch)
6450 got_ref_close(wt_branch);
6451 free(wt_branch_tip);
6452 if (err) {
6453 if (*new_base_branch_ref) {
6454 got_ref_close(*new_base_branch_ref);
6455 *new_base_branch_ref = NULL;
6457 if (*tmp_branch) {
6458 got_ref_close(*tmp_branch);
6459 *tmp_branch = NULL;
6461 if (*fileindex) {
6462 got_fileindex_free(*fileindex);
6463 *fileindex = NULL;
6465 lock_worktree(worktree, LOCK_SH);
6467 return err;
6470 const struct got_error *
6471 got_worktree_rebase_continue(struct got_object_id **commit_id,
6472 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6473 struct got_reference **branch, struct got_fileindex **fileindex,
6474 struct got_worktree *worktree, struct got_repository *repo)
6476 const struct got_error *err;
6477 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6478 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6479 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6480 char *fileindex_path = NULL;
6481 int have_staged_files = 0;
6483 *commit_id = NULL;
6484 *new_base_branch = NULL;
6485 *tmp_branch = NULL;
6486 *branch = NULL;
6487 *fileindex = NULL;
6489 err = lock_worktree(worktree, LOCK_EX);
6490 if (err)
6491 return err;
6493 err = open_fileindex(fileindex, &fileindex_path, worktree);
6494 if (err)
6495 goto done;
6497 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6498 &have_staged_files);
6499 if (err && err->code != GOT_ERR_CANCELLED)
6500 goto done;
6501 if (have_staged_files) {
6502 err = got_error(GOT_ERR_STAGED_PATHS);
6503 goto done;
6506 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6507 if (err)
6508 goto done;
6510 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6511 if (err)
6512 goto done;
6514 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6515 if (err)
6516 goto done;
6518 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6519 if (err)
6520 goto done;
6522 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6523 if (err)
6524 goto done;
6526 err = got_ref_open(branch, repo,
6527 got_ref_get_symref_target(branch_ref), 0);
6528 if (err)
6529 goto done;
6531 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6532 if (err)
6533 goto done;
6535 err = got_ref_resolve(commit_id, repo, commit_ref);
6536 if (err)
6537 goto done;
6539 err = got_ref_open(new_base_branch, repo,
6540 new_base_branch_ref_name, 0);
6541 if (err)
6542 goto done;
6544 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6545 if (err)
6546 goto done;
6547 done:
6548 free(commit_ref_name);
6549 free(branch_ref_name);
6550 free(fileindex_path);
6551 if (commit_ref)
6552 got_ref_close(commit_ref);
6553 if (branch_ref)
6554 got_ref_close(branch_ref);
6555 if (err) {
6556 free(*commit_id);
6557 *commit_id = NULL;
6558 if (*tmp_branch) {
6559 got_ref_close(*tmp_branch);
6560 *tmp_branch = NULL;
6562 if (*new_base_branch) {
6563 got_ref_close(*new_base_branch);
6564 *new_base_branch = NULL;
6566 if (*branch) {
6567 got_ref_close(*branch);
6568 *branch = NULL;
6570 if (*fileindex) {
6571 got_fileindex_free(*fileindex);
6572 *fileindex = NULL;
6574 lock_worktree(worktree, LOCK_SH);
6576 return err;
6579 const struct got_error *
6580 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6582 const struct got_error *err;
6583 char *tmp_branch_name = NULL;
6585 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6586 if (err)
6587 return err;
6589 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6590 free(tmp_branch_name);
6591 return NULL;
6594 static const struct got_error *
6595 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6596 const char *diff_path, char **logmsg, void *arg)
6598 *logmsg = arg;
6599 return NULL;
6602 static const struct got_error *
6603 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6604 const char *path, struct got_object_id *blob_id,
6605 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6606 int dirfd, const char *de_name)
6608 return NULL;
6611 struct collect_merged_paths_arg {
6612 got_worktree_checkout_cb progress_cb;
6613 void *progress_arg;
6614 struct got_pathlist_head *merged_paths;
6617 static const struct got_error *
6618 collect_merged_paths(void *arg, unsigned char status, const char *path)
6620 const struct got_error *err;
6621 struct collect_merged_paths_arg *a = arg;
6622 char *p;
6623 struct got_pathlist_entry *new;
6625 err = (*a->progress_cb)(a->progress_arg, status, path);
6626 if (err)
6627 return err;
6629 if (status != GOT_STATUS_MERGE &&
6630 status != GOT_STATUS_ADD &&
6631 status != GOT_STATUS_DELETE &&
6632 status != GOT_STATUS_CONFLICT)
6633 return NULL;
6635 p = strdup(path);
6636 if (p == NULL)
6637 return got_error_from_errno("strdup");
6639 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6640 if (err || new == NULL)
6641 free(p);
6642 return err;
6645 static const struct got_error *
6646 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6647 int is_rebase, struct got_repository *repo)
6649 const struct got_error *err;
6650 struct got_reference *commit_ref = NULL;
6652 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6653 if (err) {
6654 if (err->code != GOT_ERR_NOT_REF)
6655 goto done;
6656 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6657 if (err)
6658 goto done;
6659 err = got_ref_write(commit_ref, repo);
6660 if (err)
6661 goto done;
6662 } else if (is_rebase) {
6663 struct got_object_id *stored_id;
6664 int cmp;
6666 err = got_ref_resolve(&stored_id, repo, commit_ref);
6667 if (err)
6668 goto done;
6669 cmp = got_object_id_cmp(commit_id, stored_id);
6670 free(stored_id);
6671 if (cmp != 0) {
6672 err = got_error(GOT_ERR_REBASE_COMMITID);
6673 goto done;
6676 done:
6677 if (commit_ref)
6678 got_ref_close(commit_ref);
6679 return err;
6682 static const struct got_error *
6683 rebase_merge_files(struct got_pathlist_head *merged_paths,
6684 const char *commit_ref_name, struct got_worktree *worktree,
6685 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6686 struct got_object_id *commit_id, struct got_repository *repo,
6687 got_worktree_checkout_cb progress_cb, void *progress_arg,
6688 got_cancel_cb cancel_cb, void *cancel_arg)
6690 const struct got_error *err;
6691 struct got_reference *commit_ref = NULL;
6692 struct collect_merged_paths_arg cmp_arg;
6693 char *fileindex_path;
6695 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6697 err = get_fileindex_path(&fileindex_path, worktree);
6698 if (err)
6699 return err;
6701 cmp_arg.progress_cb = progress_cb;
6702 cmp_arg.progress_arg = progress_arg;
6703 cmp_arg.merged_paths = merged_paths;
6704 err = merge_files(worktree, fileindex, fileindex_path,
6705 parent_commit_id, commit_id, repo, collect_merged_paths,
6706 &cmp_arg, cancel_cb, cancel_arg);
6707 if (commit_ref)
6708 got_ref_close(commit_ref);
6709 return err;
6712 const struct got_error *
6713 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6714 struct got_worktree *worktree, struct got_fileindex *fileindex,
6715 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6716 struct got_repository *repo,
6717 got_worktree_checkout_cb progress_cb, void *progress_arg,
6718 got_cancel_cb cancel_cb, void *cancel_arg)
6720 const struct got_error *err;
6721 char *commit_ref_name;
6723 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6724 if (err)
6725 return err;
6727 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6728 if (err)
6729 goto done;
6731 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6732 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6733 progress_arg, cancel_cb, cancel_arg);
6734 done:
6735 free(commit_ref_name);
6736 return err;
6739 const struct got_error *
6740 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6741 struct got_worktree *worktree, struct got_fileindex *fileindex,
6742 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6743 struct got_repository *repo,
6744 got_worktree_checkout_cb progress_cb, void *progress_arg,
6745 got_cancel_cb cancel_cb, void *cancel_arg)
6747 const struct got_error *err;
6748 char *commit_ref_name;
6750 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6751 if (err)
6752 return err;
6754 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6755 if (err)
6756 goto done;
6758 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6759 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6760 progress_arg, cancel_cb, cancel_arg);
6761 done:
6762 free(commit_ref_name);
6763 return err;
6766 static const struct got_error *
6767 rebase_commit(struct got_object_id **new_commit_id,
6768 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6769 struct got_worktree *worktree, struct got_fileindex *fileindex,
6770 struct got_reference *tmp_branch, const char *committer,
6771 struct got_commit_object *orig_commit, const char *new_logmsg,
6772 int allow_conflict, struct got_repository *repo)
6774 const struct got_error *err, *sync_err;
6775 struct got_pathlist_head commitable_paths;
6776 struct collect_commitables_arg cc_arg;
6777 char *fileindex_path = NULL;
6778 struct got_reference *head_ref = NULL;
6779 struct got_object_id *head_commit_id = NULL;
6780 char *logmsg = NULL;
6782 memset(&cc_arg, 0, sizeof(cc_arg));
6783 TAILQ_INIT(&commitable_paths);
6784 *new_commit_id = NULL;
6786 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6788 err = get_fileindex_path(&fileindex_path, worktree);
6789 if (err)
6790 return err;
6792 cc_arg.commitable_paths = &commitable_paths;
6793 cc_arg.worktree = worktree;
6794 cc_arg.repo = repo;
6795 cc_arg.have_staged_files = 0;
6796 cc_arg.commit_conflicts = allow_conflict;
6798 * If possible get the status of individual files directly to
6799 * avoid crawling the entire work tree once per rebased commit.
6801 * Ideally, merged_paths would contain a list of commitables
6802 * we could use so we could skip worktree_status() entirely.
6803 * However, we would then need carefully keep track of cumulative
6804 * effects of operations such as file additions and deletions
6805 * in 'got histedit -f' (folding multiple commits into one),
6806 * and this extra complexity is not really worth it.
6808 if (merged_paths) {
6809 struct got_pathlist_entry *pe;
6810 TAILQ_FOREACH(pe, merged_paths, entry) {
6811 err = worktree_status(worktree, pe->path, fileindex,
6812 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6813 0);
6814 if (err)
6815 goto done;
6817 } else {
6818 err = worktree_status(worktree, "", fileindex, repo,
6819 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6820 if (err)
6821 goto done;
6824 if (TAILQ_EMPTY(&commitable_paths)) {
6825 /* No-op change; commit will be elided. */
6826 err = got_ref_delete(commit_ref, repo);
6827 if (err)
6828 goto done;
6829 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6830 goto done;
6833 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6834 if (err)
6835 goto done;
6837 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6838 if (err)
6839 goto done;
6841 if (new_logmsg) {
6842 logmsg = strdup(new_logmsg);
6843 if (logmsg == NULL) {
6844 err = got_error_from_errno("strdup");
6845 goto done;
6847 } else {
6848 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6849 if (err)
6850 goto done;
6853 /* NB: commit_worktree will call free(logmsg) */
6854 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6855 NULL, worktree, got_object_commit_get_author(orig_commit),
6856 committer ? committer :
6857 got_object_commit_get_committer(orig_commit), NULL,
6858 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6859 if (err)
6860 goto done;
6862 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6863 if (err)
6864 goto done;
6866 err = got_ref_delete(commit_ref, repo);
6867 if (err)
6868 goto done;
6870 err = update_fileindex_after_commit(worktree, &commitable_paths,
6871 *new_commit_id, fileindex, 0);
6872 sync_err = sync_fileindex(fileindex, fileindex_path);
6873 if (sync_err && err == NULL)
6874 err = sync_err;
6875 done:
6876 free(fileindex_path);
6877 free(head_commit_id);
6878 if (head_ref)
6879 got_ref_close(head_ref);
6880 if (err) {
6881 free(*new_commit_id);
6882 *new_commit_id = NULL;
6884 return err;
6887 const struct got_error *
6888 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6889 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6890 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6891 const char *committer, struct got_commit_object *orig_commit,
6892 struct got_object_id *orig_commit_id, int allow_conflict,
6893 struct got_repository *repo)
6895 const struct got_error *err;
6896 char *commit_ref_name;
6897 struct got_reference *commit_ref = NULL;
6898 struct got_object_id *commit_id = NULL;
6900 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6901 if (err)
6902 return err;
6904 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6905 if (err)
6906 goto done;
6907 err = got_ref_resolve(&commit_id, repo, commit_ref);
6908 if (err)
6909 goto done;
6910 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6911 err = got_error(GOT_ERR_REBASE_COMMITID);
6912 goto done;
6915 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6916 worktree, fileindex, tmp_branch, committer, orig_commit,
6917 NULL, allow_conflict, repo);
6918 done:
6919 if (commit_ref)
6920 got_ref_close(commit_ref);
6921 free(commit_ref_name);
6922 free(commit_id);
6923 return err;
6926 const struct got_error *
6927 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6928 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6929 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6930 const char *committer, struct got_commit_object *orig_commit,
6931 struct got_object_id *orig_commit_id, const char *new_logmsg,
6932 int allow_conflict, struct got_repository *repo)
6934 const struct got_error *err;
6935 char *commit_ref_name;
6936 struct got_reference *commit_ref = NULL;
6938 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6939 if (err)
6940 return err;
6942 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6943 if (err)
6944 goto done;
6946 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6947 worktree, fileindex, tmp_branch, committer, orig_commit,
6948 new_logmsg, allow_conflict, repo);
6949 done:
6950 if (commit_ref)
6951 got_ref_close(commit_ref);
6952 free(commit_ref_name);
6953 return err;
6956 const struct got_error *
6957 got_worktree_rebase_postpone(struct got_worktree *worktree,
6958 struct got_fileindex *fileindex)
6960 if (fileindex)
6961 got_fileindex_free(fileindex);
6962 return lock_worktree(worktree, LOCK_SH);
6965 static const struct got_error *
6966 delete_ref(const char *name, struct got_repository *repo)
6968 const struct got_error *err;
6969 struct got_reference *ref;
6971 err = got_ref_open(&ref, repo, name, 0);
6972 if (err) {
6973 if (err->code == GOT_ERR_NOT_REF)
6974 return NULL;
6975 return err;
6978 err = got_ref_delete(ref, repo);
6979 got_ref_close(ref);
6980 return err;
6983 static const struct got_error *
6984 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6986 const struct got_error *err;
6987 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6988 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6990 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6991 if (err)
6992 goto done;
6993 err = delete_ref(tmp_branch_name, repo);
6994 if (err)
6995 goto done;
6997 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6998 if (err)
6999 goto done;
7000 err = delete_ref(new_base_branch_ref_name, repo);
7001 if (err)
7002 goto done;
7004 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7005 if (err)
7006 goto done;
7007 err = delete_ref(branch_ref_name, repo);
7008 if (err)
7009 goto done;
7011 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7012 if (err)
7013 goto done;
7014 err = delete_ref(commit_ref_name, repo);
7015 if (err)
7016 goto done;
7018 done:
7019 free(tmp_branch_name);
7020 free(new_base_branch_ref_name);
7021 free(branch_ref_name);
7022 free(commit_ref_name);
7023 return err;
7026 static const struct got_error *
7027 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7028 struct got_object_id *new_commit_id, struct got_repository *repo)
7030 const struct got_error *err;
7031 struct got_reference *ref = NULL;
7032 struct got_object_id *old_commit_id = NULL;
7033 const char *branch_name = NULL;
7034 char *new_id_str = NULL;
7035 char *refname = NULL;
7037 branch_name = got_ref_get_name(branch);
7038 if (strncmp(branch_name, "refs/heads/", 11) != 0)
7039 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7040 branch_name += 11;
7042 err = got_object_id_str(&new_id_str, new_commit_id);
7043 if (err)
7044 return err;
7046 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7047 new_id_str) == -1) {
7048 err = got_error_from_errno("asprintf");
7049 goto done;
7052 err = got_ref_resolve(&old_commit_id, repo, branch);
7053 if (err)
7054 goto done;
7056 err = got_ref_alloc(&ref, refname, old_commit_id);
7057 if (err)
7058 goto done;
7060 err = got_ref_write(ref, repo);
7061 done:
7062 free(new_id_str);
7063 free(refname);
7064 free(old_commit_id);
7065 if (ref)
7066 got_ref_close(ref);
7067 return err;
7070 const struct got_error *
7071 got_worktree_rebase_complete(struct got_worktree *worktree,
7072 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7073 struct got_reference *rebased_branch, struct got_repository *repo,
7074 int create_backup)
7076 const struct got_error *err, *unlockerr, *sync_err;
7077 struct got_object_id *new_head_commit_id = NULL;
7078 char *fileindex_path = NULL;
7080 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7081 if (err)
7082 return err;
7084 if (create_backup) {
7085 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7086 rebased_branch, new_head_commit_id, repo);
7087 if (err)
7088 goto done;
7091 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7092 if (err)
7093 goto done;
7095 err = got_ref_write(rebased_branch, repo);
7096 if (err)
7097 goto done;
7099 err = got_worktree_set_head_ref(worktree, rebased_branch);
7100 if (err)
7101 goto done;
7103 err = delete_rebase_refs(worktree, repo);
7104 if (err)
7105 goto done;
7107 err = get_fileindex_path(&fileindex_path, worktree);
7108 if (err)
7109 goto done;
7110 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7111 sync_err = sync_fileindex(fileindex, fileindex_path);
7112 if (sync_err && err == NULL)
7113 err = sync_err;
7114 done:
7115 got_fileindex_free(fileindex);
7116 free(fileindex_path);
7117 free(new_head_commit_id);
7118 unlockerr = lock_worktree(worktree, LOCK_SH);
7119 if (unlockerr && err == NULL)
7120 err = unlockerr;
7121 return err;
7124 const struct got_error *
7125 got_worktree_rebase_abort(struct got_worktree *worktree,
7126 struct got_fileindex *fileindex, struct got_repository *repo,
7127 struct got_reference *new_base_branch,
7128 got_worktree_checkout_cb progress_cb, void *progress_arg)
7130 const struct got_error *err, *unlockerr, *sync_err;
7131 struct got_reference *resolved = NULL;
7132 struct got_object_id *commit_id = NULL;
7133 struct got_commit_object *commit = NULL;
7134 char *fileindex_path = NULL;
7135 struct revert_file_args rfa;
7136 struct got_object_id *tree_id = NULL;
7138 err = lock_worktree(worktree, LOCK_EX);
7139 if (err)
7140 return err;
7142 err = got_object_open_as_commit(&commit, repo,
7143 worktree->base_commit_id);
7144 if (err)
7145 goto done;
7147 err = got_ref_open(&resolved, repo,
7148 got_ref_get_symref_target(new_base_branch), 0);
7149 if (err)
7150 goto done;
7152 err = got_worktree_set_head_ref(worktree, resolved);
7153 if (err)
7154 goto done;
7157 * XXX commits to the base branch could have happened while
7158 * we were busy rebasing; should we store the original commit ID
7159 * when rebase begins and read it back here?
7161 err = got_ref_resolve(&commit_id, repo, resolved);
7162 if (err)
7163 goto done;
7165 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7166 if (err)
7167 goto done;
7169 err = got_object_id_by_path(&tree_id, repo, commit,
7170 worktree->path_prefix);
7171 if (err)
7172 goto done;
7174 err = delete_rebase_refs(worktree, repo);
7175 if (err)
7176 goto done;
7178 err = get_fileindex_path(&fileindex_path, worktree);
7179 if (err)
7180 goto done;
7182 rfa.worktree = worktree;
7183 rfa.fileindex = fileindex;
7184 rfa.progress_cb = progress_cb;
7185 rfa.progress_arg = progress_arg;
7186 rfa.patch_cb = NULL;
7187 rfa.patch_arg = NULL;
7188 rfa.repo = repo;
7189 rfa.unlink_added_files = 0;
7190 err = worktree_status(worktree, "", fileindex, repo,
7191 revert_file, &rfa, NULL, NULL, 1, 0);
7192 if (err)
7193 goto sync;
7195 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7196 repo, progress_cb, progress_arg, NULL, NULL);
7197 sync:
7198 sync_err = sync_fileindex(fileindex, fileindex_path);
7199 if (sync_err && err == NULL)
7200 err = sync_err;
7201 done:
7202 got_ref_close(resolved);
7203 free(tree_id);
7204 free(commit_id);
7205 if (commit)
7206 got_object_commit_close(commit);
7207 if (fileindex)
7208 got_fileindex_free(fileindex);
7209 free(fileindex_path);
7211 unlockerr = lock_worktree(worktree, LOCK_SH);
7212 if (unlockerr && err == NULL)
7213 err = unlockerr;
7214 return err;
7217 const struct got_error *
7218 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7219 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7220 struct got_fileindex **fileindex, struct got_worktree *worktree,
7221 struct got_repository *repo)
7223 const struct got_error *err = NULL;
7224 char *tmp_branch_name = NULL;
7225 char *branch_ref_name = NULL;
7226 char *base_commit_ref_name = NULL;
7227 char *fileindex_path = NULL;
7228 struct check_rebase_ok_arg ok_arg;
7229 struct got_reference *wt_branch = NULL;
7230 struct got_reference *base_commit_ref = NULL;
7232 *tmp_branch = NULL;
7233 *branch_ref = NULL;
7234 *base_commit_id = NULL;
7235 *fileindex = NULL;
7237 err = lock_worktree(worktree, LOCK_EX);
7238 if (err)
7239 return err;
7241 err = open_fileindex(fileindex, &fileindex_path, worktree);
7242 if (err)
7243 goto done;
7245 ok_arg.worktree = worktree;
7246 ok_arg.repo = repo;
7247 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7248 &ok_arg);
7249 if (err)
7250 goto done;
7252 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7253 if (err)
7254 goto done;
7256 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7257 if (err)
7258 goto done;
7260 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7261 worktree);
7262 if (err)
7263 goto done;
7265 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7266 0);
7267 if (err)
7268 goto done;
7270 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7271 if (err)
7272 goto done;
7274 err = got_ref_write(*branch_ref, repo);
7275 if (err)
7276 goto done;
7278 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7279 worktree->base_commit_id);
7280 if (err)
7281 goto done;
7282 err = got_ref_write(base_commit_ref, repo);
7283 if (err)
7284 goto done;
7285 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7286 if (*base_commit_id == NULL) {
7287 err = got_error_from_errno("got_object_id_dup");
7288 goto done;
7291 err = got_ref_alloc(tmp_branch, tmp_branch_name,
7292 worktree->base_commit_id);
7293 if (err)
7294 goto done;
7295 err = got_ref_write(*tmp_branch, repo);
7296 if (err)
7297 goto done;
7299 err = got_worktree_set_head_ref(worktree, *tmp_branch);
7300 if (err)
7301 goto done;
7302 done:
7303 free(fileindex_path);
7304 free(tmp_branch_name);
7305 free(branch_ref_name);
7306 free(base_commit_ref_name);
7307 if (wt_branch)
7308 got_ref_close(wt_branch);
7309 if (err) {
7310 if (*branch_ref) {
7311 got_ref_close(*branch_ref);
7312 *branch_ref = NULL;
7314 if (*tmp_branch) {
7315 got_ref_close(*tmp_branch);
7316 *tmp_branch = NULL;
7318 free(*base_commit_id);
7319 if (*fileindex) {
7320 got_fileindex_free(*fileindex);
7321 *fileindex = NULL;
7323 lock_worktree(worktree, LOCK_SH);
7325 return err;
7328 const struct got_error *
7329 got_worktree_histedit_postpone(struct got_worktree *worktree,
7330 struct got_fileindex *fileindex)
7332 if (fileindex)
7333 got_fileindex_free(fileindex);
7334 return lock_worktree(worktree, LOCK_SH);
7337 const struct got_error *
7338 got_worktree_histedit_in_progress(int *in_progress,
7339 struct got_worktree *worktree)
7341 const struct got_error *err;
7342 char *tmp_branch_name = NULL;
7344 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7345 if (err)
7346 return err;
7348 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7349 free(tmp_branch_name);
7350 return NULL;
7353 const struct got_error *
7354 got_worktree_histedit_continue(struct got_object_id **commit_id,
7355 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7356 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7357 struct got_worktree *worktree, struct got_repository *repo)
7359 const struct got_error *err;
7360 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7361 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7362 struct got_reference *commit_ref = NULL;
7363 struct got_reference *base_commit_ref = NULL;
7364 char *fileindex_path = NULL;
7365 int have_staged_files = 0;
7367 *commit_id = NULL;
7368 *tmp_branch = NULL;
7369 *base_commit_id = NULL;
7370 *fileindex = NULL;
7372 err = lock_worktree(worktree, LOCK_EX);
7373 if (err)
7374 return err;
7376 err = open_fileindex(fileindex, &fileindex_path, worktree);
7377 if (err)
7378 goto done;
7380 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7381 &have_staged_files);
7382 if (err && err->code != GOT_ERR_CANCELLED)
7383 goto done;
7384 if (have_staged_files) {
7385 err = got_error(GOT_ERR_STAGED_PATHS);
7386 goto done;
7389 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7390 if (err)
7391 goto done;
7393 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7394 if (err)
7395 goto done;
7397 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7398 if (err)
7399 goto done;
7401 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7402 worktree);
7403 if (err)
7404 goto done;
7406 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7407 if (err)
7408 goto done;
7410 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7411 if (err)
7412 goto done;
7413 err = got_ref_resolve(commit_id, repo, commit_ref);
7414 if (err)
7415 goto done;
7417 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7418 if (err)
7419 goto done;
7420 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7421 if (err)
7422 goto done;
7424 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7425 if (err)
7426 goto done;
7427 done:
7428 free(commit_ref_name);
7429 free(branch_ref_name);
7430 free(fileindex_path);
7431 if (commit_ref)
7432 got_ref_close(commit_ref);
7433 if (base_commit_ref)
7434 got_ref_close(base_commit_ref);
7435 if (err) {
7436 free(*commit_id);
7437 *commit_id = NULL;
7438 free(*base_commit_id);
7439 *base_commit_id = NULL;
7440 if (*tmp_branch) {
7441 got_ref_close(*tmp_branch);
7442 *tmp_branch = NULL;
7444 if (*fileindex) {
7445 got_fileindex_free(*fileindex);
7446 *fileindex = NULL;
7448 lock_worktree(worktree, LOCK_EX);
7450 return err;
7453 static const struct got_error *
7454 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7456 const struct got_error *err;
7457 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7458 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7460 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7461 if (err)
7462 goto done;
7463 err = delete_ref(tmp_branch_name, repo);
7464 if (err)
7465 goto done;
7467 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7468 worktree);
7469 if (err)
7470 goto done;
7471 err = delete_ref(base_commit_ref_name, repo);
7472 if (err)
7473 goto done;
7475 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7476 if (err)
7477 goto done;
7478 err = delete_ref(branch_ref_name, repo);
7479 if (err)
7480 goto done;
7482 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7483 if (err)
7484 goto done;
7485 err = delete_ref(commit_ref_name, repo);
7486 if (err)
7487 goto done;
7488 done:
7489 free(tmp_branch_name);
7490 free(base_commit_ref_name);
7491 free(branch_ref_name);
7492 free(commit_ref_name);
7493 return err;
7496 const struct got_error *
7497 got_worktree_histedit_abort(struct got_worktree *worktree,
7498 struct got_fileindex *fileindex, struct got_repository *repo,
7499 struct got_reference *branch, struct got_object_id *base_commit_id,
7500 got_worktree_checkout_cb progress_cb, void *progress_arg)
7502 const struct got_error *err, *unlockerr, *sync_err;
7503 struct got_reference *resolved = NULL;
7504 char *fileindex_path = NULL;
7505 struct got_commit_object *commit = NULL;
7506 struct got_object_id *tree_id = NULL;
7507 struct revert_file_args rfa;
7509 err = lock_worktree(worktree, LOCK_EX);
7510 if (err)
7511 return err;
7513 err = got_object_open_as_commit(&commit, repo,
7514 worktree->base_commit_id);
7515 if (err)
7516 goto done;
7518 err = got_ref_open(&resolved, repo,
7519 got_ref_get_symref_target(branch), 0);
7520 if (err)
7521 goto done;
7523 err = got_worktree_set_head_ref(worktree, resolved);
7524 if (err)
7525 goto done;
7527 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7528 if (err)
7529 goto done;
7531 err = got_object_id_by_path(&tree_id, repo, commit,
7532 worktree->path_prefix);
7533 if (err)
7534 goto done;
7536 err = delete_histedit_refs(worktree, repo);
7537 if (err)
7538 goto done;
7540 err = get_fileindex_path(&fileindex_path, worktree);
7541 if (err)
7542 goto done;
7544 rfa.worktree = worktree;
7545 rfa.fileindex = fileindex;
7546 rfa.progress_cb = progress_cb;
7547 rfa.progress_arg = progress_arg;
7548 rfa.patch_cb = NULL;
7549 rfa.patch_arg = NULL;
7550 rfa.repo = repo;
7551 rfa.unlink_added_files = 0;
7552 err = worktree_status(worktree, "", fileindex, repo,
7553 revert_file, &rfa, NULL, NULL, 1, 0);
7554 if (err)
7555 goto sync;
7557 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7558 repo, progress_cb, progress_arg, NULL, NULL);
7559 sync:
7560 sync_err = sync_fileindex(fileindex, fileindex_path);
7561 if (sync_err && err == NULL)
7562 err = sync_err;
7563 done:
7564 got_ref_close(resolved);
7565 free(tree_id);
7566 free(fileindex_path);
7568 unlockerr = lock_worktree(worktree, LOCK_SH);
7569 if (unlockerr && err == NULL)
7570 err = unlockerr;
7571 return err;
7574 const struct got_error *
7575 got_worktree_histedit_complete(struct got_worktree *worktree,
7576 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7577 struct got_reference *edited_branch, struct got_repository *repo)
7579 const struct got_error *err, *unlockerr, *sync_err;
7580 struct got_object_id *new_head_commit_id = NULL;
7581 struct got_reference *resolved = NULL;
7582 char *fileindex_path = NULL;
7584 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7585 if (err)
7586 return err;
7588 err = got_ref_open(&resolved, repo,
7589 got_ref_get_symref_target(edited_branch), 0);
7590 if (err)
7591 goto done;
7593 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7594 resolved, new_head_commit_id, repo);
7595 if (err)
7596 goto done;
7598 err = got_ref_change_ref(resolved, new_head_commit_id);
7599 if (err)
7600 goto done;
7602 err = got_ref_write(resolved, repo);
7603 if (err)
7604 goto done;
7606 err = got_worktree_set_head_ref(worktree, resolved);
7607 if (err)
7608 goto done;
7610 err = delete_histedit_refs(worktree, repo);
7611 if (err)
7612 goto done;
7614 err = get_fileindex_path(&fileindex_path, worktree);
7615 if (err)
7616 goto done;
7617 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7618 sync_err = sync_fileindex(fileindex, fileindex_path);
7619 if (sync_err && err == NULL)
7620 err = sync_err;
7621 done:
7622 got_fileindex_free(fileindex);
7623 free(fileindex_path);
7624 free(new_head_commit_id);
7625 unlockerr = lock_worktree(worktree, LOCK_SH);
7626 if (unlockerr && err == NULL)
7627 err = unlockerr;
7628 return err;
7631 const struct got_error *
7632 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7633 struct got_object_id *commit_id, struct got_repository *repo)
7635 const struct got_error *err;
7636 char *commit_ref_name;
7638 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7639 if (err)
7640 return err;
7642 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7643 if (err)
7644 goto done;
7646 err = delete_ref(commit_ref_name, repo);
7647 done:
7648 free(commit_ref_name);
7649 return err;
7652 const struct got_error *
7653 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7654 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7655 struct got_worktree *worktree, const char *refname,
7656 struct got_repository *repo)
7658 const struct got_error *err = NULL;
7659 char *fileindex_path = NULL;
7660 struct check_rebase_ok_arg ok_arg;
7662 *fileindex = NULL;
7663 *branch_ref = NULL;
7664 *base_branch_ref = NULL;
7666 err = lock_worktree(worktree, LOCK_EX);
7667 if (err)
7668 return err;
7670 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7671 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7672 "cannot integrate a branch into itself; "
7673 "update -b or different branch name required");
7674 goto done;
7677 err = open_fileindex(fileindex, &fileindex_path, worktree);
7678 if (err)
7679 goto done;
7681 /* Preconditions are the same as for rebase. */
7682 ok_arg.worktree = worktree;
7683 ok_arg.repo = repo;
7684 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7685 &ok_arg);
7686 if (err)
7687 goto done;
7689 err = got_ref_open(branch_ref, repo, refname, 1);
7690 if (err)
7691 goto done;
7693 err = got_ref_open(base_branch_ref, repo,
7694 got_worktree_get_head_ref_name(worktree), 1);
7695 done:
7696 if (err) {
7697 if (*branch_ref) {
7698 got_ref_close(*branch_ref);
7699 *branch_ref = NULL;
7701 if (*base_branch_ref) {
7702 got_ref_close(*base_branch_ref);
7703 *base_branch_ref = NULL;
7705 if (*fileindex) {
7706 got_fileindex_free(*fileindex);
7707 *fileindex = NULL;
7709 lock_worktree(worktree, LOCK_SH);
7711 return err;
7714 const struct got_error *
7715 got_worktree_integrate_continue(struct got_worktree *worktree,
7716 struct got_fileindex *fileindex, struct got_repository *repo,
7717 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7718 got_worktree_checkout_cb progress_cb, void *progress_arg,
7719 got_cancel_cb cancel_cb, void *cancel_arg)
7721 const struct got_error *err = NULL, *sync_err, *unlockerr;
7722 char *fileindex_path = NULL;
7723 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7724 struct got_commit_object *commit = NULL;
7726 err = get_fileindex_path(&fileindex_path, worktree);
7727 if (err)
7728 goto done;
7730 err = got_ref_resolve(&commit_id, repo, branch_ref);
7731 if (err)
7732 goto done;
7734 err = got_object_open_as_commit(&commit, repo, commit_id);
7735 if (err)
7736 goto done;
7738 err = got_object_id_by_path(&tree_id, repo, commit,
7739 worktree->path_prefix);
7740 if (err)
7741 goto done;
7743 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7744 if (err)
7745 goto done;
7747 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7748 progress_cb, progress_arg, cancel_cb, cancel_arg);
7749 if (err)
7750 goto sync;
7752 err = got_ref_change_ref(base_branch_ref, commit_id);
7753 if (err)
7754 goto sync;
7756 err = got_ref_write(base_branch_ref, repo);
7757 if (err)
7758 goto sync;
7760 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7761 sync:
7762 sync_err = sync_fileindex(fileindex, fileindex_path);
7763 if (sync_err && err == NULL)
7764 err = sync_err;
7766 done:
7767 unlockerr = got_ref_unlock(branch_ref);
7768 if (unlockerr && err == NULL)
7769 err = unlockerr;
7770 got_ref_close(branch_ref);
7772 unlockerr = got_ref_unlock(base_branch_ref);
7773 if (unlockerr && err == NULL)
7774 err = unlockerr;
7775 got_ref_close(base_branch_ref);
7777 got_fileindex_free(fileindex);
7778 free(fileindex_path);
7779 free(tree_id);
7780 if (commit)
7781 got_object_commit_close(commit);
7783 unlockerr = lock_worktree(worktree, LOCK_SH);
7784 if (unlockerr && err == NULL)
7785 err = unlockerr;
7786 return err;
7789 const struct got_error *
7790 got_worktree_integrate_abort(struct got_worktree *worktree,
7791 struct got_fileindex *fileindex, struct got_repository *repo,
7792 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7794 const struct got_error *err = NULL, *unlockerr = NULL;
7796 got_fileindex_free(fileindex);
7798 err = lock_worktree(worktree, LOCK_SH);
7800 unlockerr = got_ref_unlock(branch_ref);
7801 if (unlockerr && err == NULL)
7802 err = unlockerr;
7803 got_ref_close(branch_ref);
7805 unlockerr = got_ref_unlock(base_branch_ref);
7806 if (unlockerr && err == NULL)
7807 err = unlockerr;
7808 got_ref_close(base_branch_ref);
7810 return err;
7813 const struct got_error *
7814 got_worktree_merge_postpone(struct got_worktree *worktree,
7815 struct got_fileindex *fileindex)
7817 const struct got_error *err, *sync_err;
7818 char *fileindex_path = NULL;
7820 err = get_fileindex_path(&fileindex_path, worktree);
7821 if (err)
7822 goto done;
7824 sync_err = sync_fileindex(fileindex, fileindex_path);
7826 err = lock_worktree(worktree, LOCK_SH);
7827 if (sync_err && err == NULL)
7828 err = sync_err;
7829 done:
7830 got_fileindex_free(fileindex);
7831 free(fileindex_path);
7832 return err;
7835 static const struct got_error *
7836 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7838 const struct got_error *err;
7839 char *branch_refname = NULL, *commit_refname = NULL;
7841 err = get_merge_branch_ref_name(&branch_refname, worktree);
7842 if (err)
7843 goto done;
7844 err = delete_ref(branch_refname, repo);
7845 if (err)
7846 goto done;
7848 err = get_merge_commit_ref_name(&commit_refname, worktree);
7849 if (err)
7850 goto done;
7851 err = delete_ref(commit_refname, repo);
7852 if (err)
7853 goto done;
7855 done:
7856 free(branch_refname);
7857 free(commit_refname);
7858 return err;
7861 struct merge_commit_msg_arg {
7862 struct got_worktree *worktree;
7863 const char *branch_name;
7866 static const struct got_error *
7867 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
7868 const char *diff_path, char **logmsg, void *arg)
7870 struct merge_commit_msg_arg *a = arg;
7872 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7873 got_worktree_get_head_ref_name(a->worktree)) == -1)
7874 return got_error_from_errno("asprintf");
7876 return NULL;
7880 const struct got_error *
7881 got_worktree_merge_branch(struct got_worktree *worktree,
7882 struct got_fileindex *fileindex,
7883 struct got_object_id *yca_commit_id,
7884 struct got_object_id *branch_tip,
7885 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7886 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7888 const struct got_error *err;
7889 char *fileindex_path = NULL;
7891 err = get_fileindex_path(&fileindex_path, worktree);
7892 if (err)
7893 goto done;
7895 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7896 worktree);
7897 if (err)
7898 goto done;
7900 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7901 branch_tip, repo, progress_cb, progress_arg,
7902 cancel_cb, cancel_arg);
7903 done:
7904 free(fileindex_path);
7905 return err;
7908 const struct got_error *
7909 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7910 struct got_worktree *worktree, struct got_fileindex *fileindex,
7911 const char *author, const char *committer, int allow_bad_symlinks,
7912 struct got_object_id *branch_tip, const char *branch_name,
7913 int allow_conflict, struct got_repository *repo,
7914 got_worktree_status_cb status_cb, void *status_arg)
7917 const struct got_error *err = NULL, *sync_err;
7918 struct got_pathlist_head commitable_paths;
7919 struct collect_commitables_arg cc_arg;
7920 struct got_pathlist_entry *pe;
7921 struct got_reference *head_ref = NULL;
7922 struct got_object_id *head_commit_id = NULL;
7923 int have_staged_files = 0;
7924 struct merge_commit_msg_arg mcm_arg;
7925 char *fileindex_path = NULL;
7927 memset(&cc_arg, 0, sizeof(cc_arg));
7928 *new_commit_id = NULL;
7930 TAILQ_INIT(&commitable_paths);
7932 err = get_fileindex_path(&fileindex_path, worktree);
7933 if (err)
7934 goto done;
7936 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7937 if (err)
7938 goto done;
7940 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7941 if (err)
7942 goto done;
7944 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7945 &have_staged_files);
7946 if (err && err->code != GOT_ERR_CANCELLED)
7947 goto done;
7948 if (have_staged_files) {
7949 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7950 goto done;
7953 cc_arg.commitable_paths = &commitable_paths;
7954 cc_arg.worktree = worktree;
7955 cc_arg.fileindex = fileindex;
7956 cc_arg.repo = repo;
7957 cc_arg.have_staged_files = have_staged_files;
7958 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7959 cc_arg.commit_conflicts = allow_conflict;
7960 err = worktree_status(worktree, "", fileindex, repo,
7961 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7962 if (err)
7963 goto done;
7965 if (TAILQ_EMPTY(&commitable_paths)) {
7966 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7967 "merge of %s cannot proceed", branch_name);
7968 goto done;
7971 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7972 struct got_commitable *ct = pe->data;
7973 const char *ct_path = ct->in_repo_path;
7975 while (ct_path[0] == '/')
7976 ct_path++;
7977 err = check_out_of_date(ct_path, ct->status,
7978 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7979 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7980 if (err)
7981 goto done;
7985 mcm_arg.worktree = worktree;
7986 mcm_arg.branch_name = branch_name;
7987 err = commit_worktree(new_commit_id, &commitable_paths,
7988 head_commit_id, branch_tip, worktree, author, committer, NULL,
7989 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7990 if (err)
7991 goto done;
7993 err = update_fileindex_after_commit(worktree, &commitable_paths,
7994 *new_commit_id, fileindex, have_staged_files);
7995 sync_err = sync_fileindex(fileindex, fileindex_path);
7996 if (sync_err && err == NULL)
7997 err = sync_err;
7998 done:
7999 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8000 struct got_commitable *ct = pe->data;
8002 free_commitable(ct);
8004 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8005 free(fileindex_path);
8006 return err;
8009 const struct got_error *
8010 got_worktree_merge_complete(struct got_worktree *worktree,
8011 struct got_fileindex *fileindex, struct got_repository *repo)
8013 const struct got_error *err, *unlockerr, *sync_err;
8014 char *fileindex_path = NULL;
8016 err = delete_merge_refs(worktree, repo);
8017 if (err)
8018 goto done;
8020 err = get_fileindex_path(&fileindex_path, worktree);
8021 if (err)
8022 goto done;
8023 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8024 sync_err = sync_fileindex(fileindex, fileindex_path);
8025 if (sync_err && err == NULL)
8026 err = sync_err;
8027 done:
8028 got_fileindex_free(fileindex);
8029 free(fileindex_path);
8030 unlockerr = lock_worktree(worktree, LOCK_SH);
8031 if (unlockerr && err == NULL)
8032 err = unlockerr;
8033 return err;
8036 const struct got_error *
8037 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8038 struct got_repository *repo)
8040 const struct got_error *err;
8041 char *branch_refname = NULL;
8042 struct got_reference *branch_ref = NULL;
8044 *in_progress = 0;
8046 err = get_merge_branch_ref_name(&branch_refname, worktree);
8047 if (err)
8048 return err;
8049 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8050 free(branch_refname);
8051 if (err) {
8052 if (err->code != GOT_ERR_NOT_REF)
8053 return err;
8054 } else
8055 *in_progress = 1;
8057 return NULL;
8060 const struct got_error *got_worktree_merge_prepare(
8061 struct got_fileindex **fileindex, struct got_worktree *worktree,
8062 struct got_reference *branch, struct got_repository *repo)
8064 const struct got_error *err = NULL;
8065 char *fileindex_path = NULL;
8066 char *branch_refname = NULL, *commit_refname = NULL;
8067 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
8068 struct got_reference *commit_ref = NULL;
8069 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
8070 struct check_rebase_ok_arg ok_arg;
8072 *fileindex = NULL;
8074 err = lock_worktree(worktree, LOCK_EX);
8075 if (err)
8076 return err;
8078 err = open_fileindex(fileindex, &fileindex_path, worktree);
8079 if (err)
8080 goto done;
8082 /* Preconditions are the same as for rebase. */
8083 ok_arg.worktree = worktree;
8084 ok_arg.repo = repo;
8085 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8086 &ok_arg);
8087 if (err)
8088 goto done;
8090 err = get_merge_branch_ref_name(&branch_refname, worktree);
8091 if (err)
8092 return err;
8094 err = get_merge_commit_ref_name(&commit_refname, worktree);
8095 if (err)
8096 return err;
8098 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8099 0);
8100 if (err)
8101 goto done;
8103 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8104 if (err)
8105 goto done;
8107 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8108 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8109 goto done;
8112 err = got_ref_resolve(&branch_tip, repo, branch);
8113 if (err)
8114 goto done;
8116 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8117 if (err)
8118 goto done;
8119 err = got_ref_write(branch_ref, repo);
8120 if (err)
8121 goto done;
8123 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8124 if (err)
8125 goto done;
8126 err = got_ref_write(commit_ref, repo);
8127 if (err)
8128 goto done;
8130 done:
8131 free(branch_refname);
8132 free(commit_refname);
8133 free(fileindex_path);
8134 if (branch_ref)
8135 got_ref_close(branch_ref);
8136 if (commit_ref)
8137 got_ref_close(commit_ref);
8138 if (wt_branch)
8139 got_ref_close(wt_branch);
8140 free(wt_branch_tip);
8141 if (err) {
8142 if (*fileindex) {
8143 got_fileindex_free(*fileindex);
8144 *fileindex = NULL;
8146 lock_worktree(worktree, LOCK_SH);
8148 return err;
8151 const struct got_error *
8152 got_worktree_merge_continue(char **branch_name,
8153 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8154 struct got_worktree *worktree, struct got_repository *repo)
8156 const struct got_error *err;
8157 char *commit_refname = NULL, *branch_refname = NULL;
8158 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8159 char *fileindex_path = NULL;
8160 int have_staged_files = 0;
8162 *branch_name = NULL;
8163 *branch_tip = NULL;
8164 *fileindex = NULL;
8166 err = lock_worktree(worktree, LOCK_EX);
8167 if (err)
8168 return err;
8170 err = open_fileindex(fileindex, &fileindex_path, worktree);
8171 if (err)
8172 goto done;
8174 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8175 &have_staged_files);
8176 if (err && err->code != GOT_ERR_CANCELLED)
8177 goto done;
8178 if (have_staged_files) {
8179 err = got_error(GOT_ERR_STAGED_PATHS);
8180 goto done;
8183 err = get_merge_branch_ref_name(&branch_refname, worktree);
8184 if (err)
8185 goto done;
8187 err = get_merge_commit_ref_name(&commit_refname, worktree);
8188 if (err)
8189 goto done;
8191 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8192 if (err)
8193 goto done;
8195 if (!got_ref_is_symbolic(branch_ref)) {
8196 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8197 "%s is not a symbolic reference",
8198 got_ref_get_name(branch_ref));
8199 goto done;
8201 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8202 if (*branch_name == NULL) {
8203 err = got_error_from_errno("strdup");
8204 goto done;
8207 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8208 if (err)
8209 goto done;
8211 err = got_ref_resolve(branch_tip, repo, commit_ref);
8212 if (err)
8213 goto done;
8214 done:
8215 free(commit_refname);
8216 free(branch_refname);
8217 free(fileindex_path);
8218 if (commit_ref)
8219 got_ref_close(commit_ref);
8220 if (branch_ref)
8221 got_ref_close(branch_ref);
8222 if (err) {
8223 if (*branch_name) {
8224 free(*branch_name);
8225 *branch_name = NULL;
8227 free(*branch_tip);
8228 *branch_tip = NULL;
8229 if (*fileindex) {
8230 got_fileindex_free(*fileindex);
8231 *fileindex = NULL;
8233 lock_worktree(worktree, LOCK_SH);
8235 return err;
8238 const struct got_error *
8239 got_worktree_merge_abort(struct got_worktree *worktree,
8240 struct got_fileindex *fileindex, struct got_repository *repo,
8241 got_worktree_checkout_cb progress_cb, void *progress_arg)
8243 const struct got_error *err, *unlockerr, *sync_err;
8244 struct got_object_id *commit_id = NULL;
8245 struct got_commit_object *commit = NULL;
8246 char *fileindex_path = NULL;
8247 struct revert_file_args rfa;
8248 struct got_object_id *tree_id = NULL;
8250 err = got_object_open_as_commit(&commit, repo,
8251 worktree->base_commit_id);
8252 if (err)
8253 goto done;
8255 err = got_object_id_by_path(&tree_id, repo, commit,
8256 worktree->path_prefix);
8257 if (err)
8258 goto done;
8260 err = delete_merge_refs(worktree, repo);
8261 if (err)
8262 goto done;
8264 err = get_fileindex_path(&fileindex_path, worktree);
8265 if (err)
8266 goto done;
8268 rfa.worktree = worktree;
8269 rfa.fileindex = fileindex;
8270 rfa.progress_cb = progress_cb;
8271 rfa.progress_arg = progress_arg;
8272 rfa.patch_cb = NULL;
8273 rfa.patch_arg = NULL;
8274 rfa.repo = repo;
8275 rfa.unlink_added_files = 1;
8276 err = worktree_status(worktree, "", fileindex, repo,
8277 revert_file, &rfa, NULL, NULL, 1, 0);
8278 if (err)
8279 goto sync;
8281 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8282 repo, progress_cb, progress_arg, NULL, NULL);
8283 sync:
8284 sync_err = sync_fileindex(fileindex, fileindex_path);
8285 if (sync_err && err == NULL)
8286 err = sync_err;
8287 done:
8288 free(tree_id);
8289 free(commit_id);
8290 if (commit)
8291 got_object_commit_close(commit);
8292 if (fileindex)
8293 got_fileindex_free(fileindex);
8294 free(fileindex_path);
8296 unlockerr = lock_worktree(worktree, LOCK_SH);
8297 if (unlockerr && err == NULL)
8298 err = unlockerr;
8299 return err;
8302 struct check_stage_ok_arg {
8303 struct got_object_id *head_commit_id;
8304 struct got_worktree *worktree;
8305 struct got_fileindex *fileindex;
8306 struct got_repository *repo;
8307 int have_changes;
8310 static const struct got_error *
8311 check_stage_ok(void *arg, unsigned char status,
8312 unsigned char staged_status, const char *relpath,
8313 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8314 struct got_object_id *commit_id, int dirfd, const char *de_name)
8316 struct check_stage_ok_arg *a = arg;
8317 const struct got_error *err = NULL;
8318 struct got_fileindex_entry *ie;
8319 struct got_object_id base_commit_id;
8320 struct got_object_id *base_commit_idp = NULL;
8321 char *in_repo_path = NULL, *p;
8323 if (status == GOT_STATUS_UNVERSIONED ||
8324 status == GOT_STATUS_NO_CHANGE)
8325 return NULL;
8326 if (status == GOT_STATUS_NONEXISTENT)
8327 return got_error_set_errno(ENOENT, relpath);
8329 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8330 if (ie == NULL)
8331 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8333 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8334 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8335 relpath) == -1)
8336 return got_error_from_errno("asprintf");
8338 if (got_fileindex_entry_has_commit(ie)) {
8339 base_commit_idp = got_fileindex_entry_get_commit_id(
8340 &base_commit_id, ie);
8343 if (status == GOT_STATUS_CONFLICT) {
8344 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8345 goto done;
8346 } else if (status != GOT_STATUS_ADD &&
8347 status != GOT_STATUS_MODIFY &&
8348 status != GOT_STATUS_DELETE) {
8349 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8350 goto done;
8353 a->have_changes = 1;
8355 p = in_repo_path;
8356 while (p[0] == '/')
8357 p++;
8358 err = check_out_of_date(p, status, staged_status,
8359 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8360 GOT_ERR_STAGE_OUT_OF_DATE);
8361 done:
8362 free(in_repo_path);
8363 return err;
8366 struct stage_path_arg {
8367 struct got_worktree *worktree;
8368 struct got_fileindex *fileindex;
8369 struct got_repository *repo;
8370 got_worktree_status_cb status_cb;
8371 void *status_arg;
8372 got_worktree_patch_cb patch_cb;
8373 void *patch_arg;
8374 int staged_something;
8375 int allow_bad_symlinks;
8378 static const struct got_error *
8379 stage_path(void *arg, unsigned char status,
8380 unsigned char staged_status, const char *relpath,
8381 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8382 struct got_object_id *commit_id, int dirfd, const char *de_name)
8384 struct stage_path_arg *a = arg;
8385 const struct got_error *err = NULL;
8386 struct got_fileindex_entry *ie;
8387 char *ondisk_path = NULL, *path_content = NULL;
8388 uint32_t stage;
8389 struct got_object_id *new_staged_blob_id = NULL;
8390 struct stat sb;
8392 if (status == GOT_STATUS_UNVERSIONED)
8393 return NULL;
8395 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8396 if (ie == NULL)
8397 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8399 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8400 relpath)== -1)
8401 return got_error_from_errno("asprintf");
8403 switch (status) {
8404 case GOT_STATUS_ADD:
8405 case GOT_STATUS_MODIFY:
8406 /* XXX could sb.st_mode be passed in by our caller? */
8407 if (lstat(ondisk_path, &sb) == -1) {
8408 err = got_error_from_errno2("lstat", ondisk_path);
8409 break;
8411 if (a->patch_cb) {
8412 if (status == GOT_STATUS_ADD) {
8413 int choice = GOT_PATCH_CHOICE_NONE;
8414 err = (*a->patch_cb)(&choice, a->patch_arg,
8415 status, ie->path, NULL, 1, 1);
8416 if (err)
8417 break;
8418 if (choice != GOT_PATCH_CHOICE_YES)
8419 break;
8420 } else {
8421 err = create_patched_content(&path_content, 0,
8422 staged_blob_id ? staged_blob_id : blob_id,
8423 ondisk_path, dirfd, de_name, ie->path,
8424 a->repo, a->patch_cb, a->patch_arg);
8425 if (err || path_content == NULL)
8426 break;
8429 err = got_object_blob_create(&new_staged_blob_id,
8430 path_content ? path_content : ondisk_path, a->repo);
8431 if (err)
8432 break;
8433 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8434 SHA1_DIGEST_LENGTH);
8435 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8436 stage = GOT_FILEIDX_STAGE_ADD;
8437 else
8438 stage = GOT_FILEIDX_STAGE_MODIFY;
8439 got_fileindex_entry_stage_set(ie, stage);
8440 if (S_ISLNK(sb.st_mode)) {
8441 int is_bad_symlink = 0;
8442 if (!a->allow_bad_symlinks) {
8443 char target_path[PATH_MAX];
8444 ssize_t target_len;
8445 target_len = readlink(ondisk_path, target_path,
8446 sizeof(target_path));
8447 if (target_len == -1) {
8448 err = got_error_from_errno2("readlink",
8449 ondisk_path);
8450 break;
8452 err = is_bad_symlink_target(&is_bad_symlink,
8453 target_path, target_len, ondisk_path,
8454 a->worktree->root_path);
8455 if (err)
8456 break;
8457 if (is_bad_symlink) {
8458 err = got_error_path(ondisk_path,
8459 GOT_ERR_BAD_SYMLINK);
8460 break;
8463 if (is_bad_symlink)
8464 got_fileindex_entry_staged_filetype_set(ie,
8465 GOT_FILEIDX_MODE_BAD_SYMLINK);
8466 else
8467 got_fileindex_entry_staged_filetype_set(ie,
8468 GOT_FILEIDX_MODE_SYMLINK);
8469 } else {
8470 got_fileindex_entry_staged_filetype_set(ie,
8471 GOT_FILEIDX_MODE_REGULAR_FILE);
8473 a->staged_something = 1;
8474 if (a->status_cb == NULL)
8475 break;
8476 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8477 get_staged_status(ie), relpath, blob_id,
8478 new_staged_blob_id, NULL, dirfd, de_name);
8479 if (err)
8480 break;
8482 * When staging the reverse of the staged diff,
8483 * implicitly unstage the file.
8485 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8486 sizeof(ie->blob_sha1)) == 0) {
8487 got_fileindex_entry_stage_set(ie,
8488 GOT_FILEIDX_STAGE_NONE);
8490 break;
8491 case GOT_STATUS_DELETE:
8492 if (staged_status == GOT_STATUS_DELETE)
8493 break;
8494 if (a->patch_cb) {
8495 int choice = GOT_PATCH_CHOICE_NONE;
8496 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8497 ie->path, NULL, 1, 1);
8498 if (err)
8499 break;
8500 if (choice == GOT_PATCH_CHOICE_NO)
8501 break;
8502 if (choice != GOT_PATCH_CHOICE_YES) {
8503 err = got_error(GOT_ERR_PATCH_CHOICE);
8504 break;
8507 stage = GOT_FILEIDX_STAGE_DELETE;
8508 got_fileindex_entry_stage_set(ie, stage);
8509 a->staged_something = 1;
8510 if (a->status_cb == NULL)
8511 break;
8512 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8513 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8514 de_name);
8515 break;
8516 case GOT_STATUS_NO_CHANGE:
8517 break;
8518 case GOT_STATUS_CONFLICT:
8519 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8520 break;
8521 case GOT_STATUS_NONEXISTENT:
8522 err = got_error_set_errno(ENOENT, relpath);
8523 break;
8524 default:
8525 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8526 break;
8529 if (path_content && unlink(path_content) == -1 && err == NULL)
8530 err = got_error_from_errno2("unlink", path_content);
8531 free(path_content);
8532 free(ondisk_path);
8533 free(new_staged_blob_id);
8534 return err;
8537 const struct got_error *
8538 got_worktree_stage(struct got_worktree *worktree,
8539 struct got_pathlist_head *paths,
8540 got_worktree_status_cb status_cb, void *status_arg,
8541 got_worktree_patch_cb patch_cb, void *patch_arg,
8542 int allow_bad_symlinks, struct got_repository *repo)
8544 const struct got_error *err = NULL, *sync_err, *unlockerr;
8545 struct got_pathlist_entry *pe;
8546 struct got_fileindex *fileindex = NULL;
8547 char *fileindex_path = NULL;
8548 struct got_reference *head_ref = NULL;
8549 struct got_object_id *head_commit_id = NULL;
8550 struct check_stage_ok_arg oka;
8551 struct stage_path_arg spa;
8553 err = lock_worktree(worktree, LOCK_EX);
8554 if (err)
8555 return err;
8557 err = got_ref_open(&head_ref, repo,
8558 got_worktree_get_head_ref_name(worktree), 0);
8559 if (err)
8560 goto done;
8561 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8562 if (err)
8563 goto done;
8564 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8565 if (err)
8566 goto done;
8568 /* Check pre-conditions before staging anything. */
8569 oka.head_commit_id = head_commit_id;
8570 oka.worktree = worktree;
8571 oka.fileindex = fileindex;
8572 oka.repo = repo;
8573 oka.have_changes = 0;
8574 TAILQ_FOREACH(pe, paths, entry) {
8575 err = worktree_status(worktree, pe->path, fileindex, repo,
8576 check_stage_ok, &oka, NULL, NULL, 1, 0);
8577 if (err)
8578 goto done;
8580 if (!oka.have_changes) {
8581 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8582 goto done;
8585 spa.worktree = worktree;
8586 spa.fileindex = fileindex;
8587 spa.repo = repo;
8588 spa.patch_cb = patch_cb;
8589 spa.patch_arg = patch_arg;
8590 spa.status_cb = status_cb;
8591 spa.status_arg = status_arg;
8592 spa.staged_something = 0;
8593 spa.allow_bad_symlinks = allow_bad_symlinks;
8594 TAILQ_FOREACH(pe, paths, entry) {
8595 err = worktree_status(worktree, pe->path, fileindex, repo,
8596 stage_path, &spa, NULL, NULL, 1, 0);
8597 if (err)
8598 goto done;
8600 if (!spa.staged_something) {
8601 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8602 goto done;
8605 sync_err = sync_fileindex(fileindex, fileindex_path);
8606 if (sync_err && err == NULL)
8607 err = sync_err;
8608 done:
8609 if (head_ref)
8610 got_ref_close(head_ref);
8611 free(head_commit_id);
8612 free(fileindex_path);
8613 if (fileindex)
8614 got_fileindex_free(fileindex);
8615 unlockerr = lock_worktree(worktree, LOCK_SH);
8616 if (unlockerr && err == NULL)
8617 err = unlockerr;
8618 return err;
8621 struct unstage_path_arg {
8622 struct got_worktree *worktree;
8623 struct got_fileindex *fileindex;
8624 struct got_repository *repo;
8625 got_worktree_checkout_cb progress_cb;
8626 void *progress_arg;
8627 got_worktree_patch_cb patch_cb;
8628 void *patch_arg;
8631 static const struct got_error *
8632 create_unstaged_content(char **path_unstaged_content,
8633 char **path_new_staged_content, struct got_object_id *blob_id,
8634 struct got_object_id *staged_blob_id, const char *relpath,
8635 struct got_repository *repo,
8636 got_worktree_patch_cb patch_cb, void *patch_arg)
8638 const struct got_error *err, *free_err;
8639 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8640 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8641 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8642 struct got_diffreg_result *diffreg_result = NULL;
8643 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8644 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8645 int fd1 = -1, fd2 = -1;
8647 *path_unstaged_content = NULL;
8648 *path_new_staged_content = NULL;
8650 err = got_object_id_str(&label1, blob_id);
8651 if (err)
8652 return err;
8654 fd1 = got_opentempfd();
8655 if (fd1 == -1) {
8656 err = got_error_from_errno("got_opentempfd");
8657 goto done;
8659 fd2 = got_opentempfd();
8660 if (fd2 == -1) {
8661 err = got_error_from_errno("got_opentempfd");
8662 goto done;
8665 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8666 if (err)
8667 goto done;
8669 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
8670 if (err)
8671 goto done;
8673 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8674 if (err)
8675 goto done;
8677 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8678 fd2);
8679 if (err)
8680 goto done;
8682 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
8683 if (err)
8684 goto done;
8686 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8687 if (err)
8688 goto done;
8690 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8691 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8692 if (err)
8693 goto done;
8695 err = got_opentemp_named(path_unstaged_content, &outfile,
8696 "got-unstaged-content", "");
8697 if (err)
8698 goto done;
8699 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8700 "got-new-staged-content", "");
8701 if (err)
8702 goto done;
8704 if (fseek(f1, 0L, SEEK_SET) == -1) {
8705 err = got_ferror(f1, GOT_ERR_IO);
8706 goto done;
8708 if (fseek(f2, 0L, SEEK_SET) == -1) {
8709 err = got_ferror(f2, GOT_ERR_IO);
8710 goto done;
8712 /* Count the number of actual changes in the diff result. */
8713 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8714 struct diff_chunk_context cc = {};
8715 diff_chunk_context_load_change(&cc, &nchunks_used,
8716 diffreg_result->result, n, 0);
8717 nchanges++;
8719 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8720 int choice;
8721 err = apply_or_reject_change(&choice, &nchunks_used,
8722 diffreg_result->result, n, relpath, f1, f2,
8723 &line_cur1, &line_cur2,
8724 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8725 if (err)
8726 goto done;
8727 if (choice == GOT_PATCH_CHOICE_YES)
8728 have_content = 1;
8729 else
8730 have_rejected_content = 1;
8731 if (choice == GOT_PATCH_CHOICE_QUIT)
8732 break;
8734 if (have_content || have_rejected_content)
8735 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8736 outfile, rejectfile);
8737 done:
8738 free(label1);
8739 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8740 err = got_error_from_errno("close");
8741 if (blob)
8742 got_object_blob_close(blob);
8743 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8744 err = got_error_from_errno("close");
8745 if (staged_blob)
8746 got_object_blob_close(staged_blob);
8747 free_err = got_diffreg_result_free(diffreg_result);
8748 if (free_err && err == NULL)
8749 err = free_err;
8750 if (f1 && fclose(f1) == EOF && err == NULL)
8751 err = got_error_from_errno2("fclose", path1);
8752 if (f2 && fclose(f2) == EOF && err == NULL)
8753 err = got_error_from_errno2("fclose", path2);
8754 if (outfile && fclose(outfile) == EOF && err == NULL)
8755 err = got_error_from_errno2("fclose", *path_unstaged_content);
8756 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8757 err = got_error_from_errno2("fclose", *path_new_staged_content);
8758 if (path1 && unlink(path1) == -1 && err == NULL)
8759 err = got_error_from_errno2("unlink", path1);
8760 if (path2 && unlink(path2) == -1 && err == NULL)
8761 err = got_error_from_errno2("unlink", path2);
8762 if (err || !have_content) {
8763 if (*path_unstaged_content &&
8764 unlink(*path_unstaged_content) == -1 && err == NULL)
8765 err = got_error_from_errno2("unlink",
8766 *path_unstaged_content);
8767 free(*path_unstaged_content);
8768 *path_unstaged_content = NULL;
8770 if (err || !have_content || !have_rejected_content) {
8771 if (*path_new_staged_content &&
8772 unlink(*path_new_staged_content) == -1 && err == NULL)
8773 err = got_error_from_errno2("unlink",
8774 *path_new_staged_content);
8775 free(*path_new_staged_content);
8776 *path_new_staged_content = NULL;
8778 free(path1);
8779 free(path2);
8780 return err;
8783 static const struct got_error *
8784 unstage_hunks(struct got_object_id *staged_blob_id,
8785 struct got_blob_object *blob_base,
8786 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8787 const char *ondisk_path, const char *label_orig,
8788 struct got_worktree *worktree, struct got_repository *repo,
8789 got_worktree_patch_cb patch_cb, void *patch_arg,
8790 got_worktree_checkout_cb progress_cb, void *progress_arg)
8792 const struct got_error *err = NULL;
8793 char *path_unstaged_content = NULL;
8794 char *path_new_staged_content = NULL;
8795 char *parent = NULL, *base_path = NULL;
8796 char *blob_base_path = NULL;
8797 struct got_object_id *new_staged_blob_id = NULL;
8798 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8799 struct stat sb;
8801 err = create_unstaged_content(&path_unstaged_content,
8802 &path_new_staged_content, blob_id, staged_blob_id,
8803 ie->path, repo, patch_cb, patch_arg);
8804 if (err)
8805 return err;
8807 if (path_unstaged_content == NULL)
8808 return NULL;
8810 if (path_new_staged_content) {
8811 err = got_object_blob_create(&new_staged_blob_id,
8812 path_new_staged_content, repo);
8813 if (err)
8814 goto done;
8817 f = fopen(path_unstaged_content, "re");
8818 if (f == NULL) {
8819 err = got_error_from_errno2("fopen",
8820 path_unstaged_content);
8821 goto done;
8823 if (fstat(fileno(f), &sb) == -1) {
8824 err = got_error_from_errno2("fstat", path_unstaged_content);
8825 goto done;
8827 if (got_fileindex_entry_staged_filetype_get(ie) ==
8828 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8829 char link_target[PATH_MAX];
8830 size_t r;
8831 r = fread(link_target, 1, sizeof(link_target), f);
8832 if (r == 0 && ferror(f)) {
8833 err = got_error_from_errno("fread");
8834 goto done;
8836 if (r >= sizeof(link_target)) { /* should not happen */
8837 err = got_error(GOT_ERR_NO_SPACE);
8838 goto done;
8840 link_target[r] = '\0';
8841 err = merge_symlink(worktree, blob_base,
8842 ondisk_path, ie->path, label_orig, link_target,
8843 worktree->base_commit_id, repo, progress_cb,
8844 progress_arg);
8845 } else {
8846 int local_changes_subsumed;
8848 err = got_path_dirname(&parent, ondisk_path);
8849 if (err)
8850 return err;
8852 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8853 parent) == -1) {
8854 err = got_error_from_errno("asprintf");
8855 base_path = NULL;
8856 goto done;
8859 err = got_opentemp_named(&blob_base_path, &f_base,
8860 base_path, "");
8861 if (err)
8862 goto done;
8863 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8864 blob_base);
8865 if (err)
8866 goto done;
8869 * In order the run a 3-way merge with a symlink we copy the symlink's
8870 * target path into a temporary file and use that file with diff3.
8872 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8873 err = dump_symlink_target_path_to_file(&f_deriv2,
8874 ondisk_path);
8875 if (err)
8876 goto done;
8877 } else {
8878 int fd;
8879 fd = open(ondisk_path,
8880 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8881 if (fd == -1) {
8882 err = got_error_from_errno2("open", ondisk_path);
8883 goto done;
8885 f_deriv2 = fdopen(fd, "r");
8886 if (f_deriv2 == NULL) {
8887 err = got_error_from_errno2("fdopen", ondisk_path);
8888 close(fd);
8889 goto done;
8893 err = merge_file(&local_changes_subsumed, worktree,
8894 f_base, f, f_deriv2, ondisk_path, ie->path,
8895 got_fileindex_perms_to_st(ie),
8896 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8897 repo, progress_cb, progress_arg);
8899 if (err)
8900 goto done;
8902 if (new_staged_blob_id) {
8903 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8904 SHA1_DIGEST_LENGTH);
8905 } else {
8906 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8907 got_fileindex_entry_staged_filetype_set(ie, 0);
8909 done:
8910 free(new_staged_blob_id);
8911 if (path_unstaged_content &&
8912 unlink(path_unstaged_content) == -1 && err == NULL)
8913 err = got_error_from_errno2("unlink", path_unstaged_content);
8914 if (path_new_staged_content &&
8915 unlink(path_new_staged_content) == -1 && err == NULL)
8916 err = got_error_from_errno2("unlink", path_new_staged_content);
8917 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8918 err = got_error_from_errno2("unlink", blob_base_path);
8919 if (f_base && fclose(f_base) == EOF && err == NULL)
8920 err = got_error_from_errno2("fclose", path_unstaged_content);
8921 if (f && fclose(f) == EOF && err == NULL)
8922 err = got_error_from_errno2("fclose", path_unstaged_content);
8923 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8924 err = got_error_from_errno2("fclose", ondisk_path);
8925 free(path_unstaged_content);
8926 free(path_new_staged_content);
8927 free(blob_base_path);
8928 free(parent);
8929 free(base_path);
8930 return err;
8933 static const struct got_error *
8934 unstage_path(void *arg, unsigned char status,
8935 unsigned char staged_status, const char *relpath,
8936 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8937 struct got_object_id *commit_id, int dirfd, const char *de_name)
8939 const struct got_error *err = NULL;
8940 struct unstage_path_arg *a = arg;
8941 struct got_fileindex_entry *ie;
8942 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8943 char *ondisk_path = NULL;
8944 char *id_str = NULL, *label_orig = NULL;
8945 int local_changes_subsumed;
8946 struct stat sb;
8947 int fd1 = -1, fd2 = -1;
8949 if (staged_status != GOT_STATUS_ADD &&
8950 staged_status != GOT_STATUS_MODIFY &&
8951 staged_status != GOT_STATUS_DELETE)
8952 return NULL;
8954 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8955 if (ie == NULL)
8956 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8958 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8959 == -1)
8960 return got_error_from_errno("asprintf");
8962 err = got_object_id_str(&id_str,
8963 commit_id ? commit_id : a->worktree->base_commit_id);
8964 if (err)
8965 goto done;
8966 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8967 id_str) == -1) {
8968 err = got_error_from_errno("asprintf");
8969 goto done;
8972 fd1 = got_opentempfd();
8973 if (fd1 == -1) {
8974 err = got_error_from_errno("got_opentempfd");
8975 goto done;
8977 fd2 = got_opentempfd();
8978 if (fd2 == -1) {
8979 err = got_error_from_errno("got_opentempfd");
8980 goto done;
8983 switch (staged_status) {
8984 case GOT_STATUS_MODIFY:
8985 err = got_object_open_as_blob(&blob_base, a->repo,
8986 blob_id, 8192, fd1);
8987 if (err)
8988 break;
8989 /* fall through */
8990 case GOT_STATUS_ADD:
8991 if (a->patch_cb) {
8992 if (staged_status == GOT_STATUS_ADD) {
8993 int choice = GOT_PATCH_CHOICE_NONE;
8994 err = (*a->patch_cb)(&choice, a->patch_arg,
8995 staged_status, ie->path, NULL, 1, 1);
8996 if (err)
8997 break;
8998 if (choice != GOT_PATCH_CHOICE_YES)
8999 break;
9000 } else {
9001 err = unstage_hunks(staged_blob_id,
9002 blob_base, blob_id, ie, ondisk_path,
9003 label_orig, a->worktree, a->repo,
9004 a->patch_cb, a->patch_arg,
9005 a->progress_cb, a->progress_arg);
9006 break; /* Done with this file. */
9009 err = got_object_open_as_blob(&blob_staged, a->repo,
9010 staged_blob_id, 8192, fd2);
9011 if (err)
9012 break;
9013 switch (got_fileindex_entry_staged_filetype_get(ie)) {
9014 case GOT_FILEIDX_MODE_BAD_SYMLINK:
9015 case GOT_FILEIDX_MODE_REGULAR_FILE:
9016 err = merge_blob(&local_changes_subsumed, a->worktree,
9017 blob_base, ondisk_path, relpath,
9018 got_fileindex_perms_to_st(ie), label_orig,
9019 blob_staged, commit_id ? commit_id :
9020 a->worktree->base_commit_id, a->repo,
9021 a->progress_cb, a->progress_arg);
9022 break;
9023 case GOT_FILEIDX_MODE_SYMLINK:
9024 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9025 char *staged_target;
9026 err = got_object_blob_read_to_str(
9027 &staged_target, blob_staged);
9028 if (err)
9029 goto done;
9030 err = merge_symlink(a->worktree, blob_base,
9031 ondisk_path, relpath, label_orig,
9032 staged_target, commit_id ? commit_id :
9033 a->worktree->base_commit_id,
9034 a->repo, a->progress_cb, a->progress_arg);
9035 free(staged_target);
9036 } else {
9037 err = merge_blob(&local_changes_subsumed,
9038 a->worktree, blob_base, ondisk_path,
9039 relpath, got_fileindex_perms_to_st(ie),
9040 label_orig, blob_staged,
9041 commit_id ? commit_id :
9042 a->worktree->base_commit_id, a->repo,
9043 a->progress_cb, a->progress_arg);
9045 break;
9046 default:
9047 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9048 break;
9050 if (err == NULL) {
9051 got_fileindex_entry_stage_set(ie,
9052 GOT_FILEIDX_STAGE_NONE);
9053 got_fileindex_entry_staged_filetype_set(ie, 0);
9055 break;
9056 case GOT_STATUS_DELETE:
9057 if (a->patch_cb) {
9058 int choice = GOT_PATCH_CHOICE_NONE;
9059 err = (*a->patch_cb)(&choice, a->patch_arg,
9060 staged_status, ie->path, NULL, 1, 1);
9061 if (err)
9062 break;
9063 if (choice == GOT_PATCH_CHOICE_NO)
9064 break;
9065 if (choice != GOT_PATCH_CHOICE_YES) {
9066 err = got_error(GOT_ERR_PATCH_CHOICE);
9067 break;
9070 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9071 got_fileindex_entry_staged_filetype_set(ie, 0);
9072 err = get_file_status(&status, &sb, ie, ondisk_path,
9073 dirfd, de_name, a->repo);
9074 if (err)
9075 break;
9076 err = (*a->progress_cb)(a->progress_arg, status, relpath);
9077 break;
9079 done:
9080 free(ondisk_path);
9081 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9082 err = got_error_from_errno("close");
9083 if (blob_base)
9084 got_object_blob_close(blob_base);
9085 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9086 err = got_error_from_errno("close");
9087 if (blob_staged)
9088 got_object_blob_close(blob_staged);
9089 free(id_str);
9090 free(label_orig);
9091 return err;
9094 const struct got_error *
9095 got_worktree_unstage(struct got_worktree *worktree,
9096 struct got_pathlist_head *paths,
9097 got_worktree_checkout_cb progress_cb, void *progress_arg,
9098 got_worktree_patch_cb patch_cb, void *patch_arg,
9099 struct got_repository *repo)
9101 const struct got_error *err = NULL, *sync_err, *unlockerr;
9102 struct got_pathlist_entry *pe;
9103 struct got_fileindex *fileindex = NULL;
9104 char *fileindex_path = NULL;
9105 struct unstage_path_arg upa;
9107 err = lock_worktree(worktree, LOCK_EX);
9108 if (err)
9109 return err;
9111 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9112 if (err)
9113 goto done;
9115 upa.worktree = worktree;
9116 upa.fileindex = fileindex;
9117 upa.repo = repo;
9118 upa.progress_cb = progress_cb;
9119 upa.progress_arg = progress_arg;
9120 upa.patch_cb = patch_cb;
9121 upa.patch_arg = patch_arg;
9122 TAILQ_FOREACH(pe, paths, entry) {
9123 err = worktree_status(worktree, pe->path, fileindex, repo,
9124 unstage_path, &upa, NULL, NULL, 1, 0);
9125 if (err)
9126 goto done;
9129 sync_err = sync_fileindex(fileindex, fileindex_path);
9130 if (sync_err && err == NULL)
9131 err = sync_err;
9132 done:
9133 free(fileindex_path);
9134 if (fileindex)
9135 got_fileindex_free(fileindex);
9136 unlockerr = lock_worktree(worktree, LOCK_SH);
9137 if (unlockerr && err == NULL)
9138 err = unlockerr;
9139 return err;
9142 struct report_file_info_arg {
9143 struct got_worktree *worktree;
9144 got_worktree_path_info_cb info_cb;
9145 void *info_arg;
9146 struct got_pathlist_head *paths;
9147 got_cancel_cb cancel_cb;
9148 void *cancel_arg;
9151 static const struct got_error *
9152 report_file_info(void *arg, struct got_fileindex_entry *ie)
9154 struct report_file_info_arg *a = arg;
9155 struct got_pathlist_entry *pe;
9156 struct got_object_id blob_id, staged_blob_id, commit_id;
9157 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9158 struct got_object_id *commit_idp = NULL;
9159 int stage;
9161 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9162 return got_error(GOT_ERR_CANCELLED);
9164 TAILQ_FOREACH(pe, a->paths, entry) {
9165 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9166 got_path_is_child(ie->path, pe->path, pe->path_len))
9167 break;
9169 if (pe == NULL) /* not found */
9170 return NULL;
9172 if (got_fileindex_entry_has_blob(ie))
9173 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9174 stage = got_fileindex_entry_stage_get(ie);
9175 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9176 stage == GOT_FILEIDX_STAGE_ADD) {
9177 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9178 &staged_blob_id, ie);
9181 if (got_fileindex_entry_has_commit(ie))
9182 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9184 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9185 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9188 const struct got_error *
9189 got_worktree_path_info(struct got_worktree *worktree,
9190 struct got_pathlist_head *paths,
9191 got_worktree_path_info_cb info_cb, void *info_arg,
9192 got_cancel_cb cancel_cb, void *cancel_arg)
9195 const struct got_error *err = NULL, *unlockerr;
9196 struct got_fileindex *fileindex = NULL;
9197 char *fileindex_path = NULL;
9198 struct report_file_info_arg arg;
9200 err = lock_worktree(worktree, LOCK_SH);
9201 if (err)
9202 return err;
9204 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9205 if (err)
9206 goto done;
9208 arg.worktree = worktree;
9209 arg.info_cb = info_cb;
9210 arg.info_arg = info_arg;
9211 arg.paths = paths;
9212 arg.cancel_cb = cancel_cb;
9213 arg.cancel_arg = cancel_arg;
9214 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9215 &arg);
9216 done:
9217 free(fileindex_path);
9218 if (fileindex)
9219 got_fileindex_free(fileindex);
9220 unlockerr = lock_worktree(worktree, LOCK_UN);
9221 if (unlockerr && err == NULL)
9222 err = unlockerr;
9223 return err;
9226 static const struct got_error *
9227 patch_check_path(const char *p, char **path, unsigned char *status,
9228 unsigned char *staged_status, struct got_fileindex *fileindex,
9229 struct got_worktree *worktree, struct got_repository *repo)
9231 const struct got_error *err;
9232 struct got_fileindex_entry *ie;
9233 struct stat sb;
9234 char *ondisk_path = NULL;
9236 err = got_worktree_resolve_path(path, worktree, p);
9237 if (err)
9238 return err;
9240 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9241 *path[0] ? "/" : "", *path) == -1)
9242 return got_error_from_errno("asprintf");
9244 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9245 if (ie) {
9246 *staged_status = get_staged_status(ie);
9247 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9248 repo);
9249 if (err)
9250 goto done;
9251 } else {
9252 *staged_status = GOT_STATUS_NO_CHANGE;
9253 *status = GOT_STATUS_UNVERSIONED;
9254 if (lstat(ondisk_path, &sb) == -1) {
9255 if (errno != ENOENT) {
9256 err = got_error_from_errno2("lstat",
9257 ondisk_path);
9258 goto done;
9260 *status = GOT_STATUS_NONEXISTENT;
9264 done:
9265 free(ondisk_path);
9266 return err;
9269 static const struct got_error *
9270 patch_can_rm(const char *path, unsigned char status,
9271 unsigned char staged_status)
9273 if (status == GOT_STATUS_NONEXISTENT)
9274 return got_error_set_errno(ENOENT, path);
9275 if (status != GOT_STATUS_NO_CHANGE &&
9276 status != GOT_STATUS_ADD &&
9277 status != GOT_STATUS_MODIFY &&
9278 status != GOT_STATUS_MODE_CHANGE)
9279 return got_error_path(path, GOT_ERR_FILE_STATUS);
9280 if (staged_status == GOT_STATUS_DELETE)
9281 return got_error_path(path, GOT_ERR_FILE_STATUS);
9282 return NULL;
9285 static const struct got_error *
9286 patch_can_add(const char *path, unsigned char status)
9288 if (status != GOT_STATUS_NONEXISTENT)
9289 return got_error_path(path, GOT_ERR_FILE_STATUS);
9290 return NULL;
9293 static const struct got_error *
9294 patch_can_edit(const char *path, unsigned char status,
9295 unsigned char staged_status)
9297 if (status == GOT_STATUS_NONEXISTENT)
9298 return got_error_set_errno(ENOENT, path);
9299 if (status != GOT_STATUS_NO_CHANGE &&
9300 status != GOT_STATUS_ADD &&
9301 status != GOT_STATUS_MODIFY)
9302 return got_error_path(path, GOT_ERR_FILE_STATUS);
9303 if (staged_status == GOT_STATUS_DELETE)
9304 return got_error_path(path, GOT_ERR_FILE_STATUS);
9305 return NULL;
9308 const struct got_error *
9309 got_worktree_patch_prepare(struct got_fileindex **fileindex,
9310 char **fileindex_path, struct got_worktree *worktree)
9312 return open_fileindex(fileindex, fileindex_path, worktree);
9315 const struct got_error *
9316 got_worktree_patch_check_path(const char *old, const char *new,
9317 char **oldpath, char **newpath, struct got_worktree *worktree,
9318 struct got_repository *repo, struct got_fileindex *fileindex)
9320 const struct got_error *err = NULL;
9321 int file_renamed = 0;
9322 unsigned char status_old, staged_status_old;
9323 unsigned char status_new, staged_status_new;
9325 *oldpath = NULL;
9326 *newpath = NULL;
9328 err = patch_check_path(old != NULL ? old : new, oldpath,
9329 &status_old, &staged_status_old, fileindex, worktree, repo);
9330 if (err)
9331 goto done;
9333 err = patch_check_path(new != NULL ? new : old, newpath,
9334 &status_new, &staged_status_new, fileindex, worktree, repo);
9335 if (err)
9336 goto done;
9338 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9339 file_renamed = 1;
9341 if (old != NULL && new == NULL)
9342 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9343 else if (file_renamed) {
9344 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9345 if (err == NULL)
9346 err = patch_can_add(*newpath, status_new);
9347 } else if (old == NULL)
9348 err = patch_can_add(*newpath, status_new);
9349 else
9350 err = patch_can_edit(*newpath, status_new, staged_status_new);
9352 done:
9353 if (err) {
9354 free(*oldpath);
9355 *oldpath = NULL;
9356 free(*newpath);
9357 *newpath = NULL;
9359 return err;
9362 const struct got_error *
9363 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9364 struct got_worktree *worktree, struct got_fileindex *fileindex,
9365 got_worktree_checkout_cb progress_cb, void *progress_arg)
9367 struct schedule_addition_args saa;
9369 memset(&saa, 0, sizeof(saa));
9370 saa.worktree = worktree;
9371 saa.fileindex = fileindex;
9372 saa.progress_cb = progress_cb;
9373 saa.progress_arg = progress_arg;
9374 saa.repo = repo;
9376 return worktree_status(worktree, path, fileindex, repo,
9377 schedule_addition, &saa, NULL, NULL, 1, 0);
9380 const struct got_error *
9381 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9382 struct got_worktree *worktree, struct got_fileindex *fileindex,
9383 got_worktree_delete_cb progress_cb, void *progress_arg)
9385 struct schedule_deletion_args sda;
9387 memset(&sda, 0, sizeof(sda));
9388 sda.worktree = worktree;
9389 sda.fileindex = fileindex;
9390 sda.progress_cb = progress_cb;
9391 sda.progress_arg = progress_arg;
9392 sda.repo = repo;
9393 sda.delete_local_mods = 0;
9394 sda.keep_on_disk = 0;
9395 sda.ignore_missing_paths = 0;
9396 sda.status_codes = NULL;
9398 return worktree_status(worktree, path, fileindex, repo,
9399 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9402 const struct got_error *
9403 got_worktree_patch_complete(struct got_fileindex *fileindex,
9404 const char *fileindex_path)
9406 const struct got_error *err = NULL;
9408 err = sync_fileindex(fileindex, fileindex_path);
9409 got_fileindex_free(fileindex);
9411 return err;