Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/stat.h>
18 #include <sys/queue.h>
20 #include <dirent.h>
21 #include <limits.h>
22 #include <stddef.h>
23 #include <string.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <time.h>
27 #include <fcntl.h>
28 #include <errno.h>
29 #include <unistd.h>
30 #include <zlib.h>
31 #include <fnmatch.h>
32 #include <libgen.h>
34 #include "got_compat.h"
36 #include "got_error.h"
37 #include "got_repository.h"
38 #include "got_reference.h"
39 #include "got_object.h"
40 #include "got_path.h"
41 #include "got_cancel.h"
42 #include "got_worktree.h"
43 #include "got_opentemp.h"
44 #include "got_diff.h"
46 #include "got_lib_worktree.h"
47 #include "got_lib_sha1.h"
48 #include "got_lib_fileindex.h"
49 #include "got_lib_inflate.h"
50 #include "got_lib_delta.h"
51 #include "got_lib_object.h"
52 #include "got_lib_object_parse.h"
53 #include "got_lib_object_create.h"
54 #include "got_lib_object_idset.h"
55 #include "got_lib_diff.h"
56 #include "got_lib_gotconfig.h"
58 #ifndef MIN
59 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
60 #endif
62 #define GOT_MERGE_LABEL_MERGED "merged change"
63 #define GOT_MERGE_LABEL_BASE "3-way merge base"
65 static mode_t apply_umask(mode_t);
67 static const struct got_error *
68 create_meta_file(const char *path_got, const char *name, const char *content)
69 {
70 const struct got_error *err = NULL;
71 char *path;
73 if (asprintf(&path, "%s/%s", path_got, name) == -1)
74 return got_error_from_errno("asprintf");
76 err = got_path_create_file(path, content);
77 free(path);
78 return err;
79 }
81 static const struct got_error *
82 update_meta_file(const char *path_got, const char *name, const char *content)
83 {
84 const struct got_error *err = NULL;
85 FILE *tmpfile = NULL;
86 char *tmppath = NULL;
87 char *path = NULL;
89 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
90 err = got_error_from_errno("asprintf");
91 path = NULL;
92 goto done;
93 }
95 err = got_opentemp_named(&tmppath, &tmpfile, path, "");
96 if (err)
97 goto done;
99 if (content) {
100 int len = fprintf(tmpfile, "%s\n", content);
101 if (len != strlen(content) + 1) {
102 err = got_error_from_errno2("fprintf", tmppath);
103 goto done;
107 if (rename(tmppath, path) != 0) {
108 err = got_error_from_errno3("rename", tmppath, path);
109 unlink(tmppath);
110 goto done;
113 done:
114 if (fclose(tmpfile) == EOF && err == NULL)
115 err = got_error_from_errno2("fclose", tmppath);
116 free(tmppath);
117 return err;
120 static const struct got_error *
121 write_head_ref(const char *path_got, struct got_reference *head_ref)
123 const struct got_error *err = NULL;
124 char *refstr = NULL;
126 if (got_ref_is_symbolic(head_ref)) {
127 refstr = got_ref_to_str(head_ref);
128 if (refstr == NULL)
129 return got_error_from_errno("got_ref_to_str");
130 } else {
131 refstr = strdup(got_ref_get_name(head_ref));
132 if (refstr == NULL)
133 return got_error_from_errno("strdup");
135 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
136 free(refstr);
137 return err;
140 const struct got_error *
141 got_worktree_init(const char *path, struct got_reference *head_ref,
142 const char *prefix, struct got_repository *repo)
144 const struct got_error *err = NULL;
145 struct got_object_id *commit_id = NULL;
146 uuid_t uuid;
147 uint32_t uuid_status;
148 int obj_type;
149 char *path_got = NULL;
150 char *formatstr = NULL;
151 char *absprefix = NULL;
152 char *basestr = NULL;
153 char *uuidstr = NULL;
155 if (strcmp(path, got_repo_get_path(repo)) == 0) {
156 err = got_error(GOT_ERR_WORKTREE_REPO);
157 goto done;
160 err = got_ref_resolve(&commit_id, repo, head_ref);
161 if (err)
162 return err;
163 err = got_object_get_type(&obj_type, repo, commit_id);
164 if (err)
165 return err;
166 if (obj_type != GOT_OBJ_TYPE_COMMIT)
167 return got_error(GOT_ERR_OBJ_TYPE);
169 if (!got_path_is_absolute(prefix)) {
170 if (asprintf(&absprefix, "/%s", prefix) == -1)
171 return got_error_from_errno("asprintf");
174 /* Create top-level directory (may already exist). */
175 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
176 err = got_error_from_errno2("mkdir", path);
177 goto done;
180 /* Create .got directory (may already exist). */
181 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
182 err = got_error_from_errno("asprintf");
183 goto done;
185 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
186 err = got_error_from_errno2("mkdir", path_got);
187 goto done;
190 /* Create an empty lock file. */
191 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
192 if (err)
193 goto done;
195 /* Create an empty file index. */
196 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
197 if (err)
198 goto done;
200 /* Write the HEAD reference. */
201 err = write_head_ref(path_got, head_ref);
202 if (err)
203 goto done;
205 /* Record our base commit. */
206 err = got_object_id_str(&basestr, commit_id);
207 if (err)
208 goto done;
209 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
210 if (err)
211 goto done;
213 /* Store path to repository. */
214 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
215 got_repo_get_path(repo));
216 if (err)
217 goto done;
219 /* Store in-repository path prefix. */
220 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
221 absprefix ? absprefix : prefix);
222 if (err)
223 goto done;
225 /* Generate UUID. */
226 uuid_create(&uuid, &uuid_status);
227 if (uuid_status != uuid_s_ok) {
228 err = got_error_uuid(uuid_status, "uuid_create");
229 goto done;
231 uuid_to_string(&uuid, &uuidstr, &uuid_status);
232 if (uuid_status != uuid_s_ok) {
233 err = got_error_uuid(uuid_status, "uuid_to_string");
234 goto done;
236 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
237 if (err)
238 goto done;
240 /* Stamp work tree with format file. */
241 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
242 err = got_error_from_errno("asprintf");
243 goto done;
245 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
246 if (err)
247 goto done;
249 done:
250 free(commit_id);
251 free(path_got);
252 free(formatstr);
253 free(absprefix);
254 free(basestr);
255 free(uuidstr);
256 return err;
259 const struct got_error *
260 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
261 const char *path_prefix)
263 char *absprefix = NULL;
265 if (!got_path_is_absolute(path_prefix)) {
266 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
267 return got_error_from_errno("asprintf");
269 *match = (strcmp(absprefix ? absprefix : path_prefix,
270 worktree->path_prefix) == 0);
271 free(absprefix);
272 return NULL;
275 const char *
276 got_worktree_get_head_ref_name(struct got_worktree *worktree)
278 return worktree->head_ref_name;
281 const struct got_error *
282 got_worktree_set_head_ref(struct got_worktree *worktree,
283 struct got_reference *head_ref)
285 const struct got_error *err = NULL;
286 char *path_got = NULL, *head_ref_name = NULL;
288 if (asprintf(&path_got, "%s/%s", worktree->root_path,
289 GOT_WORKTREE_GOT_DIR) == -1) {
290 err = got_error_from_errno("asprintf");
291 path_got = NULL;
292 goto done;
295 head_ref_name = strdup(got_ref_get_name(head_ref));
296 if (head_ref_name == NULL) {
297 err = got_error_from_errno("strdup");
298 goto done;
301 err = write_head_ref(path_got, head_ref);
302 if (err)
303 goto done;
305 free(worktree->head_ref_name);
306 worktree->head_ref_name = head_ref_name;
307 done:
308 free(path_got);
309 if (err)
310 free(head_ref_name);
311 return err;
314 struct got_object_id *
315 got_worktree_get_base_commit_id(struct got_worktree *worktree)
317 return worktree->base_commit_id;
320 const struct got_error *
321 got_worktree_set_base_commit_id(struct got_worktree *worktree,
322 struct got_repository *repo, struct got_object_id *commit_id)
324 const struct got_error *err;
325 struct got_object *obj = NULL;
326 char *id_str = NULL;
327 char *path_got = NULL;
329 if (asprintf(&path_got, "%s/%s", worktree->root_path,
330 GOT_WORKTREE_GOT_DIR) == -1) {
331 err = got_error_from_errno("asprintf");
332 path_got = NULL;
333 goto done;
336 err = got_object_open(&obj, repo, commit_id);
337 if (err)
338 return err;
340 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
341 err = got_error(GOT_ERR_OBJ_TYPE);
342 goto done;
345 /* Record our base commit. */
346 err = got_object_id_str(&id_str, commit_id);
347 if (err)
348 goto done;
349 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
350 if (err)
351 goto done;
353 free(worktree->base_commit_id);
354 worktree->base_commit_id = got_object_id_dup(commit_id);
355 if (worktree->base_commit_id == NULL) {
356 err = got_error_from_errno("got_object_id_dup");
357 goto done;
359 done:
360 if (obj)
361 got_object_close(obj);
362 free(id_str);
363 free(path_got);
364 return err;
367 const struct got_gotconfig *
368 got_worktree_get_gotconfig(struct got_worktree *worktree)
370 return worktree->gotconfig;
373 static const struct got_error *
374 lock_worktree(struct got_worktree *worktree, int operation)
376 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
377 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
378 : got_error_from_errno2("flock",
379 got_worktree_get_root_path(worktree)));
380 return NULL;
383 static const struct got_error *
384 add_dir_on_disk(struct got_worktree *worktree, const char *path)
386 const struct got_error *err = NULL;
387 char *abspath;
389 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
390 return got_error_from_errno("asprintf");
392 err = got_path_mkdir(abspath);
393 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
394 struct stat sb;
395 err = NULL;
396 if (lstat(abspath, &sb) == -1) {
397 err = got_error_from_errno2("lstat", abspath);
398 } else if (!S_ISDIR(sb.st_mode)) {
399 /* TODO directory is obstructed; do something */
400 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
403 free(abspath);
404 return err;
407 static const struct got_error *
408 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
410 const struct got_error *err = NULL;
411 uint8_t fbuf1[8192];
412 uint8_t fbuf2[8192];
413 size_t flen1 = 0, flen2 = 0;
415 *same = 1;
417 for (;;) {
418 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
419 if (flen1 == 0 && ferror(f1)) {
420 err = got_error_from_errno("fread");
421 break;
423 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
424 if (flen2 == 0 && ferror(f2)) {
425 err = got_error_from_errno("fread");
426 break;
428 if (flen1 == 0) {
429 if (flen2 != 0)
430 *same = 0;
431 break;
432 } else if (flen2 == 0) {
433 if (flen1 != 0)
434 *same = 0;
435 break;
436 } else if (flen1 == flen2) {
437 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
438 *same = 0;
439 break;
441 } else {
442 *same = 0;
443 break;
447 return err;
450 static const struct got_error *
451 check_files_equal(int *same, FILE *f1, FILE *f2)
453 struct stat sb;
454 size_t size1, size2;
456 *same = 1;
458 if (fstat(fileno(f1), &sb) != 0)
459 return got_error_from_errno("fstat");
460 size1 = sb.st_size;
462 if (fstat(fileno(f2), &sb) != 0)
463 return got_error_from_errno("fstat");
464 size2 = sb.st_size;
466 if (size1 != size2) {
467 *same = 0;
468 return NULL;
471 if (fseek(f1, 0L, SEEK_SET) == -1)
472 return got_ferror(f1, GOT_ERR_IO);
473 if (fseek(f2, 0L, SEEK_SET) == -1)
474 return got_ferror(f2, GOT_ERR_IO);
476 return check_file_contents_equal(same, f1, f2);
479 static const struct got_error *
480 copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
482 uint8_t fbuf[65536];
483 size_t flen;
484 ssize_t outlen;
486 *outsize = 0;
488 if (fseek(f, 0L, SEEK_SET) == -1)
489 return got_ferror(f, GOT_ERR_IO);
491 for (;;) {
492 flen = fread(fbuf, 1, sizeof(fbuf), f);
493 if (flen == 0) {
494 if (ferror(f))
495 return got_error_from_errno("fread");
496 if (feof(f))
497 break;
499 outlen = write(outfd, fbuf, flen);
500 if (outlen == -1)
501 return got_error_from_errno("write");
502 if (outlen != flen)
503 return got_error(GOT_ERR_IO);
504 *outsize += outlen;
507 return NULL;
510 static const struct got_error *
511 merge_binary_file(int *overlapcnt, int merged_fd,
512 FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
513 const char *label_deriv, const char *label_orig, const char *label_deriv2,
514 const char *ondisk_path)
516 const struct got_error *err = NULL;
517 int same_content, changed_deriv, changed_deriv2;
518 int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
519 off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
520 char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
521 char *base_path_orig = NULL, *base_path_deriv = NULL;
522 char *base_path_deriv2 = NULL;
524 *overlapcnt = 0;
526 err = check_files_equal(&same_content, f_deriv, f_deriv2);
527 if (err)
528 return err;
530 if (same_content)
531 return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
533 err = check_files_equal(&same_content, f_deriv, f_orig);
534 if (err)
535 return err;
536 changed_deriv = !same_content;
537 err = check_files_equal(&same_content, f_deriv2, f_orig);
538 if (err)
539 return err;
540 changed_deriv2 = !same_content;
542 if (changed_deriv && changed_deriv2) {
543 *overlapcnt = 1;
544 if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
545 err = got_error_from_errno("asprintf");
546 goto done;
548 if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
549 err = got_error_from_errno("asprintf");
550 goto done;
552 if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
553 err = got_error_from_errno("asprintf");
554 goto done;
556 err = got_opentemp_named_fd(&path_orig, &fd_orig,
557 base_path_orig, "");
558 if (err)
559 goto done;
560 err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
561 base_path_deriv, "");
562 if (err)
563 goto done;
564 err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
565 base_path_deriv2, "");
566 if (err)
567 goto done;
568 err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
569 if (err)
570 goto done;
571 err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
572 if (err)
573 goto done;
574 err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
575 if (err)
576 goto done;
577 if (dprintf(merged_fd, "Binary files differ and cannot be "
578 "merged automatically:\n") < 0) {
579 err = got_error_from_errno("dprintf");
580 goto done;
582 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
583 GOT_DIFF_CONFLICT_MARKER_BEGIN,
584 label_deriv ? " " : "",
585 label_deriv ? label_deriv : "",
586 path_deriv) < 0) {
587 err = got_error_from_errno("dprintf");
588 goto done;
590 if (size_orig > 0) {
591 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
592 GOT_DIFF_CONFLICT_MARKER_ORIG,
593 label_orig ? " " : "",
594 label_orig ? label_orig : "",
595 path_orig) < 0) {
596 err = got_error_from_errno("dprintf");
597 goto done;
600 if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
601 GOT_DIFF_CONFLICT_MARKER_SEP,
602 path_deriv2,
603 GOT_DIFF_CONFLICT_MARKER_END,
604 label_deriv2 ? " " : "",
605 label_deriv2 ? label_deriv2 : "") < 0) {
606 err = got_error_from_errno("dprintf");
607 goto done;
609 } else if (changed_deriv)
610 err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
611 else if (changed_deriv2)
612 err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
613 done:
614 if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
615 err == NULL)
616 err = got_error_from_errno2("unlink", path_orig);
617 if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
618 err = got_error_from_errno2("close", path_orig);
619 if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
620 err = got_error_from_errno2("close", path_deriv);
621 if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
622 err = got_error_from_errno2("close", path_deriv2);
623 free(path_orig);
624 free(path_deriv);
625 free(path_deriv2);
626 free(base_path_orig);
627 free(base_path_deriv);
628 free(base_path_deriv2);
629 return err;
632 /*
633 * Perform a 3-way merge where the file f_orig acts as the common
634 * ancestor, the file f_deriv acts as the first derived version,
635 * and the file f_deriv2 acts as the second derived version.
636 * The merge result will be written to a new file at ondisk_path; any
637 * existing file at this path will be replaced.
638 */
639 static const struct got_error *
640 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
641 FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
642 const char *path, uint16_t st_mode,
643 const char *label_orig, const char *label_deriv, const char *label_deriv2,
644 enum got_diff_algorithm diff_algo, struct got_repository *repo,
645 got_worktree_checkout_cb progress_cb, void *progress_arg)
647 const struct got_error *err = NULL;
648 int merged_fd = -1;
649 FILE *f_merged = NULL;
650 char *merged_path = NULL, *base_path = NULL;
651 int overlapcnt = 0;
652 char *parent = NULL;
654 *local_changes_subsumed = 0;
656 err = got_path_dirname(&parent, ondisk_path);
657 if (err)
658 return err;
660 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
661 err = got_error_from_errno("asprintf");
662 goto done;
665 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path, "");
666 if (err)
667 goto done;
669 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
670 f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
671 if (err) {
672 if (err->code != GOT_ERR_FILE_BINARY)
673 goto done;
674 err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
675 f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
676 ondisk_path);
677 if (err)
678 goto done;
681 err = (*progress_cb)(progress_arg,
682 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
683 if (err)
684 goto done;
686 if (fsync(merged_fd) != 0) {
687 err = got_error_from_errno("fsync");
688 goto done;
691 f_merged = fdopen(merged_fd, "r");
692 if (f_merged == NULL) {
693 err = got_error_from_errno("fdopen");
694 goto done;
696 merged_fd = -1;
698 /* Check if a clean merge has subsumed all local changes. */
699 if (overlapcnt == 0) {
700 err = check_files_equal(local_changes_subsumed, f_deriv,
701 f_merged);
702 if (err)
703 goto done;
706 if (fchmod(fileno(f_merged), apply_umask(st_mode)) != 0) {
707 err = got_error_from_errno2("fchmod", merged_path);
708 goto done;
711 if (rename(merged_path, ondisk_path) != 0) {
712 err = got_error_from_errno3("rename", merged_path,
713 ondisk_path);
714 goto done;
716 done:
717 if (err) {
718 if (merged_path)
719 unlink(merged_path);
721 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
722 err = got_error_from_errno("close");
723 if (f_merged && fclose(f_merged) == EOF && err == NULL)
724 err = got_error_from_errno("fclose");
725 free(merged_path);
726 free(base_path);
727 free(parent);
728 return err;
731 static const struct got_error *
732 update_symlink(const char *ondisk_path, const char *target_path,
733 size_t target_len)
735 /* This is not atomic but matches what 'ln -sf' does. */
736 if (unlink(ondisk_path) == -1)
737 return got_error_from_errno2("unlink", ondisk_path);
738 if (symlink(target_path, ondisk_path) == -1)
739 return got_error_from_errno3("symlink", target_path,
740 ondisk_path);
741 return NULL;
744 /*
745 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
746 * in the work tree with a file that contains conflict markers and the
747 * conflicting target paths of the original version, a "derived version"
748 * of a symlink from an incoming change, and a local version of the symlink.
750 * The original versions's target path can be NULL if it is not available,
751 * such as if both derived versions added a new symlink at the same path.
753 * The incoming derived symlink target is NULL in case the incoming change
754 * has deleted this symlink.
755 */
756 static const struct got_error *
757 install_symlink_conflict(const char *deriv_target,
758 struct got_object_id *deriv_base_commit_id, const char *orig_target,
759 const char *label_orig, const char *local_target, const char *ondisk_path)
761 const struct got_error *err;
762 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
763 FILE *f = NULL;
765 err = got_object_id_str(&id_str, deriv_base_commit_id);
766 if (err)
767 return got_error_from_errno("asprintf");
769 if (asprintf(&label_deriv, "%s: commit %s",
770 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
771 err = got_error_from_errno("asprintf");
772 goto done;
775 err = got_opentemp_named(&path, &f, "got-symlink-conflict", "");
776 if (err)
777 goto done;
779 if (fchmod(fileno(f), apply_umask(GOT_DEFAULT_FILE_MODE)) == -1) {
780 err = got_error_from_errno2("fchmod", path);
781 goto done;
784 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
785 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
786 deriv_target ? deriv_target : "(symlink was deleted)",
787 orig_target ? label_orig : "",
788 orig_target ? "\n" : "",
789 orig_target ? orig_target : "",
790 orig_target ? "\n" : "",
791 GOT_DIFF_CONFLICT_MARKER_SEP,
792 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
793 err = got_error_from_errno2("fprintf", path);
794 goto done;
797 if (unlink(ondisk_path) == -1) {
798 err = got_error_from_errno2("unlink", ondisk_path);
799 goto done;
801 if (rename(path, ondisk_path) == -1) {
802 err = got_error_from_errno3("rename", path, ondisk_path);
803 goto done;
805 done:
806 if (f != NULL && fclose(f) == EOF && err == NULL)
807 err = got_error_from_errno2("fclose", path);
808 free(path);
809 free(id_str);
810 free(label_deriv);
811 return err;
814 /* forward declaration */
815 static const struct got_error *
816 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
817 const char *, const char *, uint16_t, const char *,
818 struct got_blob_object *, struct got_object_id *,
819 struct got_repository *, got_worktree_checkout_cb, void *);
821 /*
822 * Merge a symlink into the work tree, where blob_orig acts as the common
823 * ancestor, deriv_target is the link target of the first derived version,
824 * and the symlink on disk acts as the second derived version.
825 * Assume that contents of both blobs represent symlinks.
826 */
827 static const struct got_error *
828 merge_symlink(struct got_worktree *worktree,
829 struct got_blob_object *blob_orig, const char *ondisk_path,
830 const char *path, const char *label_orig, const char *deriv_target,
831 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
832 got_worktree_checkout_cb progress_cb, void *progress_arg)
834 const struct got_error *err = NULL;
835 char *ancestor_target = NULL;
836 struct stat sb;
837 ssize_t ondisk_len, deriv_len;
838 char ondisk_target[PATH_MAX];
839 int have_local_change = 0;
840 int have_incoming_change = 0;
842 if (lstat(ondisk_path, &sb) == -1)
843 return got_error_from_errno2("lstat", ondisk_path);
845 ondisk_len = readlink(ondisk_path, ondisk_target,
846 sizeof(ondisk_target));
847 if (ondisk_len == -1) {
848 err = got_error_from_errno2("readlink",
849 ondisk_path);
850 goto done;
852 ondisk_target[ondisk_len] = '\0';
854 if (blob_orig) {
855 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
856 if (err)
857 goto done;
860 if (ancestor_target == NULL ||
861 (ondisk_len != strlen(ancestor_target) ||
862 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
863 have_local_change = 1;
865 deriv_len = strlen(deriv_target);
866 if (ancestor_target == NULL ||
867 (deriv_len != strlen(ancestor_target) ||
868 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
869 have_incoming_change = 1;
871 if (!have_local_change && !have_incoming_change) {
872 if (ancestor_target) {
873 /* Both sides made the same change. */
874 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
875 path);
876 } else if (deriv_len == ondisk_len &&
877 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
878 /* Both sides added the same symlink. */
879 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
880 path);
881 } else {
882 /* Both sides added symlinks which don't match. */
883 err = install_symlink_conflict(deriv_target,
884 deriv_base_commit_id, ancestor_target,
885 label_orig, ondisk_target, ondisk_path);
886 if (err)
887 goto done;
888 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
889 path);
891 } else if (!have_local_change && have_incoming_change) {
892 /* Apply the incoming change. */
893 err = update_symlink(ondisk_path, deriv_target,
894 strlen(deriv_target));
895 if (err)
896 goto done;
897 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
898 } else if (have_local_change && have_incoming_change) {
899 if (deriv_len == ondisk_len &&
900 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
901 /* Both sides made the same change. */
902 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
903 path);
904 } else {
905 err = install_symlink_conflict(deriv_target,
906 deriv_base_commit_id, ancestor_target, label_orig,
907 ondisk_target, ondisk_path);
908 if (err)
909 goto done;
910 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
911 path);
915 done:
916 free(ancestor_target);
917 return err;
920 static const struct got_error *
921 dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
923 const struct got_error *err = NULL;
924 char target_path[PATH_MAX];
925 ssize_t target_len;
926 size_t n;
927 FILE *f;
929 *outfile = NULL;
931 f = got_opentemp();
932 if (f == NULL)
933 return got_error_from_errno("got_opentemp");
934 target_len = readlink(ondisk_path, target_path, sizeof(target_path));
935 if (target_len == -1) {
936 err = got_error_from_errno2("readlink", ondisk_path);
937 goto done;
939 n = fwrite(target_path, 1, target_len, f);
940 if (n != target_len) {
941 err = got_ferror(f, GOT_ERR_IO);
942 goto done;
944 if (fflush(f) == EOF) {
945 err = got_error_from_errno("fflush");
946 goto done;
948 if (fseek(f, 0L, SEEK_SET) == -1) {
949 err = got_ferror(f, GOT_ERR_IO);
950 goto done;
952 done:
953 if (err)
954 fclose(f);
955 else
956 *outfile = f;
957 return err;
960 /*
961 * Perform a 3-way merge where blob_orig acts as the common ancestor,
962 * blob_deriv acts as the first derived version, and the file on disk
963 * acts as the second derived version.
964 */
965 static const struct got_error *
966 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
967 struct got_blob_object *blob_orig, const char *ondisk_path,
968 const char *path, uint16_t st_mode, const char *label_orig,
969 struct got_blob_object *blob_deriv,
970 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
971 got_worktree_checkout_cb progress_cb, void *progress_arg)
973 const struct got_error *err = NULL;
974 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
975 char *blob_orig_path = NULL;
976 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
977 char *label_deriv = NULL, *parent = NULL;
979 *local_changes_subsumed = 0;
981 err = got_path_dirname(&parent, ondisk_path);
982 if (err)
983 return err;
985 if (blob_orig) {
986 if (asprintf(&base_path, "%s/got-merge-blob-orig",
987 parent) == -1) {
988 err = got_error_from_errno("asprintf");
989 base_path = NULL;
990 goto done;
993 err = got_opentemp_named(&blob_orig_path, &f_orig,
994 base_path, "");
995 if (err)
996 goto done;
997 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
998 blob_orig);
999 if (err)
1000 goto done;
1001 free(base_path);
1002 } else {
1004 * No common ancestor exists. This is an "add vs add" conflict
1005 * and we simply use an empty ancestor file to make both files
1006 * appear in the merged result in their entirety.
1008 f_orig = got_opentemp();
1009 if (f_orig == NULL) {
1010 err = got_error_from_errno("got_opentemp");
1011 goto done;
1015 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1016 err = got_error_from_errno("asprintf");
1017 base_path = NULL;
1018 goto done;
1021 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path, "");
1022 if (err)
1023 goto done;
1024 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1025 blob_deriv);
1026 if (err)
1027 goto done;
1029 err = got_object_id_str(&id_str, deriv_base_commit_id);
1030 if (err)
1031 goto done;
1032 if (asprintf(&label_deriv, "%s: commit %s",
1033 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1034 err = got_error_from_errno("asprintf");
1035 goto done;
1039 * In order the run a 3-way merge with a symlink we copy the symlink's
1040 * target path into a temporary file and use that file with diff3.
1042 if (S_ISLNK(st_mode)) {
1043 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1044 if (err)
1045 goto done;
1046 } else {
1047 int fd;
1048 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1049 if (fd == -1) {
1050 err = got_error_from_errno2("open", ondisk_path);
1051 goto done;
1053 f_deriv2 = fdopen(fd, "r");
1054 if (f_deriv2 == NULL) {
1055 err = got_error_from_errno2("fdopen", ondisk_path);
1056 close(fd);
1057 goto done;
1061 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1062 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1063 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1064 done:
1065 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1066 err = got_error_from_errno("fclose");
1067 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1068 err = got_error_from_errno("fclose");
1069 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1070 err = got_error_from_errno("fclose");
1071 free(base_path);
1072 if (blob_orig_path) {
1073 unlink(blob_orig_path);
1074 free(blob_orig_path);
1076 if (blob_deriv_path) {
1077 unlink(blob_deriv_path);
1078 free(blob_deriv_path);
1080 free(id_str);
1081 free(label_deriv);
1082 free(parent);
1083 return err;
1086 static const struct got_error *
1087 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1088 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1089 int wt_fd, const char *path, struct got_object_id *blob_id)
1091 const struct got_error *err = NULL;
1092 struct got_fileindex_entry *new_ie;
1094 *new_iep = NULL;
1096 err = got_fileindex_entry_alloc(&new_ie, path);
1097 if (err)
1098 return err;
1100 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1101 blob_id->sha1, base_commit_id->sha1, 1);
1102 if (err)
1103 goto done;
1105 err = got_fileindex_entry_add(fileindex, new_ie);
1106 done:
1107 if (err)
1108 got_fileindex_entry_free(new_ie);
1109 else
1110 *new_iep = new_ie;
1111 return err;
1114 static mode_t
1115 get_ondisk_perms(int executable, mode_t st_mode)
1117 mode_t xbits = S_IXUSR;
1119 if (executable) {
1120 /* Map read bits to execute bits. */
1121 if (st_mode & S_IRGRP)
1122 xbits |= S_IXGRP;
1123 if (st_mode & S_IROTH)
1124 xbits |= S_IXOTH;
1125 return st_mode | xbits;
1128 return st_mode;
1131 static mode_t
1132 apply_umask(mode_t mode)
1134 mode_t um;
1136 um = umask(000);
1137 umask(um);
1138 return mode & ~um;
1141 /* forward declaration */
1142 static const struct got_error *
1143 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1144 const char *path, mode_t te_mode, mode_t st_mode,
1145 struct got_blob_object *blob, int restoring_missing_file,
1146 int reverting_versioned_file, int installing_bad_symlink,
1147 int path_is_unversioned, struct got_repository *repo,
1148 got_worktree_checkout_cb progress_cb, void *progress_arg);
1151 * This function assumes that the provided symlink target points at a
1152 * safe location in the work tree!
1154 static const struct got_error *
1155 replace_existing_symlink(int *did_something, const char *ondisk_path,
1156 const char *target_path, size_t target_len)
1158 const struct got_error *err = NULL;
1159 ssize_t elen;
1160 char etarget[PATH_MAX];
1161 int fd;
1163 *did_something = 0;
1166 * "Bad" symlinks (those pointing outside the work tree or into the
1167 * .got directory) are installed in the work tree as a regular file
1168 * which contains the bad symlink target path.
1169 * The new symlink target has already been checked for safety by our
1170 * caller. If we can successfully open a regular file then we simply
1171 * replace this file with a symlink below.
1173 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1174 if (fd == -1) {
1175 if (!got_err_open_nofollow_on_symlink())
1176 return got_error_from_errno2("open", ondisk_path);
1178 /* We are updating an existing on-disk symlink. */
1179 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1180 if (elen == -1)
1181 return got_error_from_errno2("readlink", ondisk_path);
1183 if (elen == target_len &&
1184 memcmp(etarget, target_path, target_len) == 0)
1185 return NULL; /* nothing to do */
1188 *did_something = 1;
1189 err = update_symlink(ondisk_path, target_path, target_len);
1190 if (fd != -1 && close(fd) == -1 && err == NULL)
1191 err = got_error_from_errno2("close", ondisk_path);
1192 return err;
1195 static const struct got_error *
1196 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1197 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1199 const struct got_error *err = NULL;
1200 char canonpath[PATH_MAX];
1201 char *path_got = NULL;
1203 *is_bad_symlink = 0;
1205 if (target_len >= sizeof(canonpath)) {
1206 *is_bad_symlink = 1;
1207 return NULL;
1211 * We do not use realpath(3) to resolve the symlink's target
1212 * path because we don't want to resolve symlinks recursively.
1213 * Instead we make the path absolute and then canonicalize it.
1214 * Relative symlink target lookup should begin at the directory
1215 * in which the blob object is being installed.
1217 if (!got_path_is_absolute(target_path)) {
1218 char *abspath, *parent;
1219 err = got_path_dirname(&parent, ondisk_path);
1220 if (err)
1221 return err;
1222 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1223 free(parent);
1224 return got_error_from_errno("asprintf");
1226 free(parent);
1227 if (strlen(abspath) >= sizeof(canonpath)) {
1228 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1229 free(abspath);
1230 return err;
1232 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1233 free(abspath);
1234 if (err)
1235 return err;
1236 } else {
1237 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1238 if (err)
1239 return err;
1242 /* Only allow symlinks pointing at paths within the work tree. */
1243 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1244 *is_bad_symlink = 1;
1245 return NULL;
1248 /* Do not allow symlinks pointing into the .got directory. */
1249 if (asprintf(&path_got, "%s/%s", wtroot_path,
1250 GOT_WORKTREE_GOT_DIR) == -1)
1251 return got_error_from_errno("asprintf");
1252 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1253 *is_bad_symlink = 1;
1255 free(path_got);
1256 return NULL;
1259 static const struct got_error *
1260 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1261 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1262 int restoring_missing_file, int reverting_versioned_file,
1263 int path_is_unversioned, int allow_bad_symlinks,
1264 struct got_repository *repo,
1265 got_worktree_checkout_cb progress_cb, void *progress_arg)
1267 const struct got_error *err = NULL;
1268 char target_path[PATH_MAX];
1269 size_t len, target_len = 0;
1270 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1271 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1273 *is_bad_symlink = 0;
1276 * Blob object content specifies the target path of the link.
1277 * If a symbolic link cannot be installed we instead create
1278 * a regular file which contains the link target path stored
1279 * in the blob object.
1281 do {
1282 err = got_object_blob_read_block(&len, blob);
1283 if (err)
1284 return err;
1286 if (len + target_len >= sizeof(target_path)) {
1287 /* Path too long; install as a regular file. */
1288 *is_bad_symlink = 1;
1289 got_object_blob_rewind(blob);
1290 return install_blob(worktree, ondisk_path, path,
1291 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1292 restoring_missing_file, reverting_versioned_file,
1293 1, path_is_unversioned, repo, progress_cb,
1294 progress_arg);
1296 if (len > 0) {
1297 /* Skip blob object header first time around. */
1298 memcpy(target_path + target_len, buf + hdrlen,
1299 len - hdrlen);
1300 target_len += len - hdrlen;
1301 hdrlen = 0;
1303 } while (len != 0);
1304 target_path[target_len] = '\0';
1306 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1307 ondisk_path, worktree->root_path);
1308 if (err)
1309 return err;
1311 if (*is_bad_symlink && !allow_bad_symlinks) {
1312 /* install as a regular file */
1313 got_object_blob_rewind(blob);
1314 err = install_blob(worktree, ondisk_path, path,
1315 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1316 restoring_missing_file, reverting_versioned_file, 1,
1317 path_is_unversioned, repo, progress_cb, progress_arg);
1318 return err;
1321 if (symlink(target_path, ondisk_path) == -1) {
1322 if (errno == EEXIST) {
1323 int symlink_replaced;
1324 if (path_is_unversioned) {
1325 err = (*progress_cb)(progress_arg,
1326 GOT_STATUS_UNVERSIONED, path);
1327 return err;
1329 err = replace_existing_symlink(&symlink_replaced,
1330 ondisk_path, target_path, target_len);
1331 if (err)
1332 return err;
1333 if (progress_cb) {
1334 if (symlink_replaced) {
1335 err = (*progress_cb)(progress_arg,
1336 reverting_versioned_file ?
1337 GOT_STATUS_REVERT :
1338 GOT_STATUS_UPDATE, path);
1339 } else {
1340 err = (*progress_cb)(progress_arg,
1341 GOT_STATUS_EXISTS, path);
1344 return err; /* Nothing else to do. */
1347 if (errno == ENOENT) {
1348 char *parent;
1349 err = got_path_dirname(&parent, ondisk_path);
1350 if (err)
1351 return err;
1352 err = add_dir_on_disk(worktree, parent);
1353 free(parent);
1354 if (err)
1355 return err;
1357 * Retry, and fall through to error handling
1358 * below if this second attempt fails.
1360 if (symlink(target_path, ondisk_path) != -1) {
1361 err = NULL; /* success */
1362 return err;
1366 /* Handle errors from first or second creation attempt. */
1367 if (errno == ENAMETOOLONG) {
1368 /* bad target path; install as a regular file */
1369 *is_bad_symlink = 1;
1370 got_object_blob_rewind(blob);
1371 err = install_blob(worktree, ondisk_path, path,
1372 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1373 restoring_missing_file, reverting_versioned_file, 1,
1374 path_is_unversioned, repo,
1375 progress_cb, progress_arg);
1376 } else if (errno == ENOTDIR) {
1377 err = got_error_path(ondisk_path,
1378 GOT_ERR_FILE_OBSTRUCTED);
1379 } else {
1380 err = got_error_from_errno3("symlink",
1381 target_path, ondisk_path);
1383 } else if (progress_cb)
1384 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1385 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1386 return err;
1389 static const struct got_error *
1390 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1391 const char *path, mode_t te_mode, mode_t st_mode,
1392 struct got_blob_object *blob, int restoring_missing_file,
1393 int reverting_versioned_file, int installing_bad_symlink,
1394 int path_is_unversioned, struct got_repository *repo,
1395 got_worktree_checkout_cb progress_cb, void *progress_arg)
1397 const struct got_error *err = NULL;
1398 int fd = -1;
1399 size_t len, hdrlen;
1400 int update = 0;
1401 char *tmppath = NULL;
1402 mode_t mode;
1404 mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1405 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1406 O_CLOEXEC, mode);
1407 if (fd == -1) {
1408 if (errno == ENOENT) {
1409 char *parent;
1410 err = got_path_dirname(&parent, path);
1411 if (err)
1412 return err;
1413 err = add_dir_on_disk(worktree, parent);
1414 free(parent);
1415 if (err)
1416 return err;
1417 fd = open(ondisk_path,
1418 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1419 mode);
1420 if (fd == -1)
1421 return got_error_from_errno2("open",
1422 ondisk_path);
1423 } else if (errno == EEXIST) {
1424 if (path_is_unversioned) {
1425 err = (*progress_cb)(progress_arg,
1426 GOT_STATUS_UNVERSIONED, path);
1427 goto done;
1429 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1430 !S_ISREG(st_mode) && !installing_bad_symlink) {
1431 /* TODO file is obstructed; do something */
1432 err = got_error_path(ondisk_path,
1433 GOT_ERR_FILE_OBSTRUCTED);
1434 goto done;
1435 } else {
1436 err = got_opentemp_named_fd(&tmppath, &fd,
1437 ondisk_path, "");
1438 if (err)
1439 goto done;
1440 update = 1;
1442 if (fchmod(fd, apply_umask(mode)) == -1) {
1443 err = got_error_from_errno2("fchmod",
1444 tmppath);
1445 goto done;
1448 } else
1449 return got_error_from_errno2("open", ondisk_path);
1452 if (progress_cb) {
1453 if (restoring_missing_file)
1454 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1455 path);
1456 else if (reverting_versioned_file)
1457 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1458 path);
1459 else
1460 err = (*progress_cb)(progress_arg,
1461 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1462 if (err)
1463 goto done;
1466 hdrlen = got_object_blob_get_hdrlen(blob);
1467 do {
1468 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1469 err = got_object_blob_read_block(&len, blob);
1470 if (err)
1471 break;
1472 if (len > 0) {
1473 /* Skip blob object header first time around. */
1474 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1475 if (outlen == -1) {
1476 err = got_error_from_errno("write");
1477 goto done;
1478 } else if (outlen != len - hdrlen) {
1479 err = got_error(GOT_ERR_IO);
1480 goto done;
1482 hdrlen = 0;
1484 } while (len != 0);
1486 if (fsync(fd) != 0) {
1487 err = got_error_from_errno("fsync");
1488 goto done;
1491 if (update) {
1492 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1493 err = got_error_from_errno2("unlink", ondisk_path);
1494 goto done;
1496 if (rename(tmppath, ondisk_path) != 0) {
1497 err = got_error_from_errno3("rename", tmppath,
1498 ondisk_path);
1499 goto done;
1501 free(tmppath);
1502 tmppath = NULL;
1505 done:
1506 if (fd != -1 && close(fd) == -1 && err == NULL)
1507 err = got_error_from_errno("close");
1508 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1509 err = got_error_from_errno2("unlink", tmppath);
1510 free(tmppath);
1511 return err;
1514 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1515 static const struct got_error *
1516 get_modified_file_content_status(unsigned char *status, FILE *f)
1518 const struct got_error *err = NULL;
1519 const char *markers[3] = {
1520 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1521 GOT_DIFF_CONFLICT_MARKER_SEP,
1522 GOT_DIFF_CONFLICT_MARKER_END
1524 int i = 0;
1525 char *line = NULL;
1526 size_t linesize = 0;
1527 ssize_t linelen;
1529 while (*status == GOT_STATUS_MODIFY) {
1530 linelen = getline(&line, &linesize, f);
1531 if (linelen == -1) {
1532 if (feof(f))
1533 break;
1534 err = got_ferror(f, GOT_ERR_IO);
1535 break;
1538 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1539 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1540 == 0)
1541 *status = GOT_STATUS_CONFLICT;
1542 else
1543 i++;
1546 free(line);
1548 return err;
1551 static int
1552 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1554 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1555 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1558 static int
1559 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1561 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1562 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1563 ie->mtime_sec == sb->st_mtim.tv_sec &&
1564 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1565 ie->size == (sb->st_size & 0xffffffff) &&
1566 !xbit_differs(ie, sb->st_mode));
1569 static unsigned char
1570 get_staged_status(struct got_fileindex_entry *ie)
1572 switch (got_fileindex_entry_stage_get(ie)) {
1573 case GOT_FILEIDX_STAGE_ADD:
1574 return GOT_STATUS_ADD;
1575 case GOT_FILEIDX_STAGE_DELETE:
1576 return GOT_STATUS_DELETE;
1577 case GOT_FILEIDX_STAGE_MODIFY:
1578 return GOT_STATUS_MODIFY;
1579 default:
1580 return GOT_STATUS_NO_CHANGE;
1584 static const struct got_error *
1585 get_symlink_modification_status(unsigned char *status,
1586 struct got_fileindex_entry *ie, const char *abspath,
1587 int dirfd, const char *de_name, struct got_blob_object *blob)
1589 const struct got_error *err = NULL;
1590 char target_path[PATH_MAX];
1591 char etarget[PATH_MAX];
1592 ssize_t elen;
1593 size_t len, target_len = 0;
1594 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1595 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1597 *status = GOT_STATUS_NO_CHANGE;
1599 /* Blob object content specifies the target path of the link. */
1600 do {
1601 err = got_object_blob_read_block(&len, blob);
1602 if (err)
1603 return err;
1604 if (len + target_len >= sizeof(target_path)) {
1606 * Should not happen. The blob contents were OK
1607 * when this symlink was installed.
1609 return got_error(GOT_ERR_NO_SPACE);
1611 if (len > 0) {
1612 /* Skip blob object header first time around. */
1613 memcpy(target_path + target_len, buf + hdrlen,
1614 len - hdrlen);
1615 target_len += len - hdrlen;
1616 hdrlen = 0;
1618 } while (len != 0);
1619 target_path[target_len] = '\0';
1621 if (dirfd != -1) {
1622 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1623 if (elen == -1)
1624 return got_error_from_errno2("readlinkat", abspath);
1625 } else {
1626 elen = readlink(abspath, etarget, sizeof(etarget));
1627 if (elen == -1)
1628 return got_error_from_errno2("readlink", abspath);
1631 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1632 *status = GOT_STATUS_MODIFY;
1634 return NULL;
1637 static const struct got_error *
1638 get_file_status(unsigned char *status, struct stat *sb,
1639 struct got_fileindex_entry *ie, const char *abspath,
1640 int dirfd, const char *de_name, struct got_repository *repo)
1642 const struct got_error *err = NULL;
1643 struct got_object_id id;
1644 size_t hdrlen;
1645 int fd = -1, fd1 = -1;
1646 FILE *f = NULL;
1647 uint8_t fbuf[8192];
1648 struct got_blob_object *blob = NULL;
1649 size_t flen, blen;
1650 unsigned char staged_status = get_staged_status(ie);
1652 *status = GOT_STATUS_NO_CHANGE;
1653 memset(sb, 0, sizeof(*sb));
1656 * Whenever the caller provides a directory descriptor and a
1657 * directory entry name for the file, use them! This prevents
1658 * race conditions if filesystem paths change beneath our feet.
1660 if (dirfd != -1) {
1661 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1662 if (errno == ENOENT) {
1663 if (got_fileindex_entry_has_file_on_disk(ie))
1664 *status = GOT_STATUS_MISSING;
1665 else
1666 *status = GOT_STATUS_DELETE;
1667 goto done;
1669 err = got_error_from_errno2("fstatat", abspath);
1670 goto done;
1672 } else {
1673 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1674 if (fd == -1 && errno != ENOENT &&
1675 !got_err_open_nofollow_on_symlink())
1676 return got_error_from_errno2("open", abspath);
1677 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1678 if (lstat(abspath, sb) == -1)
1679 return got_error_from_errno2("lstat", abspath);
1680 } else if (fd == -1 || fstat(fd, sb) == -1) {
1681 if (errno == ENOENT) {
1682 if (got_fileindex_entry_has_file_on_disk(ie))
1683 *status = GOT_STATUS_MISSING;
1684 else
1685 *status = GOT_STATUS_DELETE;
1686 goto done;
1688 err = got_error_from_errno2("fstat", abspath);
1689 goto done;
1693 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1694 *status = GOT_STATUS_OBSTRUCTED;
1695 goto done;
1698 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1699 *status = GOT_STATUS_DELETE;
1700 goto done;
1701 } else if (!got_fileindex_entry_has_blob(ie) &&
1702 staged_status != GOT_STATUS_ADD) {
1703 *status = GOT_STATUS_ADD;
1704 goto done;
1707 if (!stat_info_differs(ie, sb))
1708 goto done;
1710 if (S_ISLNK(sb->st_mode) &&
1711 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1712 *status = GOT_STATUS_MODIFY;
1713 goto done;
1716 if (staged_status == GOT_STATUS_MODIFY ||
1717 staged_status == GOT_STATUS_ADD)
1718 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1719 else
1720 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1722 fd1 = got_opentempfd();
1723 if (fd1 == -1) {
1724 err = got_error_from_errno("got_opentempfd");
1725 goto done;
1727 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1728 if (err)
1729 goto done;
1731 if (S_ISLNK(sb->st_mode)) {
1732 err = get_symlink_modification_status(status, ie,
1733 abspath, dirfd, de_name, blob);
1734 goto done;
1737 if (dirfd != -1) {
1738 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1739 if (fd == -1) {
1740 err = got_error_from_errno2("openat", abspath);
1741 goto done;
1745 f = fdopen(fd, "r");
1746 if (f == NULL) {
1747 err = got_error_from_errno2("fdopen", abspath);
1748 goto done;
1750 fd = -1;
1751 hdrlen = got_object_blob_get_hdrlen(blob);
1752 for (;;) {
1753 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1754 err = got_object_blob_read_block(&blen, blob);
1755 if (err)
1756 goto done;
1757 /* Skip length of blob object header first time around. */
1758 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1759 if (flen == 0 && ferror(f)) {
1760 err = got_error_from_errno("fread");
1761 goto done;
1763 if (blen - hdrlen == 0) {
1764 if (flen != 0)
1765 *status = GOT_STATUS_MODIFY;
1766 break;
1767 } else if (flen == 0) {
1768 if (blen - hdrlen != 0)
1769 *status = GOT_STATUS_MODIFY;
1770 break;
1771 } else if (blen - hdrlen == flen) {
1772 /* Skip blob object header first time around. */
1773 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1774 *status = GOT_STATUS_MODIFY;
1775 break;
1777 } else {
1778 *status = GOT_STATUS_MODIFY;
1779 break;
1781 hdrlen = 0;
1784 if (*status == GOT_STATUS_MODIFY) {
1785 rewind(f);
1786 err = get_modified_file_content_status(status, f);
1787 } else if (xbit_differs(ie, sb->st_mode))
1788 *status = GOT_STATUS_MODE_CHANGE;
1789 done:
1790 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1791 err = got_error_from_errno("close");
1792 if (blob)
1793 got_object_blob_close(blob);
1794 if (f != NULL && fclose(f) == EOF && err == NULL)
1795 err = got_error_from_errno2("fclose", abspath);
1796 if (fd != -1 && close(fd) == -1 && err == NULL)
1797 err = got_error_from_errno2("close", abspath);
1798 return err;
1802 * Update timestamps in the file index if a file is unmodified and
1803 * we had to run a full content comparison to find out.
1805 static const struct got_error *
1806 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1807 struct got_fileindex_entry *ie, struct stat *sb)
1809 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1810 return got_fileindex_entry_update(ie, wt_fd, path,
1811 ie->blob_sha1, ie->commit_sha1, 1);
1813 return NULL;
1816 static const struct got_error *
1817 update_blob(struct got_worktree *worktree,
1818 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1819 struct got_tree_entry *te, const char *path,
1820 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1821 void *progress_arg)
1823 const struct got_error *err = NULL;
1824 struct got_blob_object *blob = NULL;
1825 char *ondisk_path = NULL;
1826 unsigned char status = GOT_STATUS_NO_CHANGE;
1827 struct stat sb;
1828 int fd1 = -1, fd2 = -1;
1830 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1831 return got_error_from_errno("asprintf");
1833 if (ie) {
1834 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1835 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1836 goto done;
1838 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1839 repo);
1840 if (err)
1841 goto done;
1842 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1843 sb.st_mode = got_fileindex_perms_to_st(ie);
1844 } else {
1845 if (stat(ondisk_path, &sb) == -1) {
1846 if (errno != ENOENT) {
1847 err = got_error_from_errno2("stat",
1848 ondisk_path);
1849 goto done;
1851 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1852 status = GOT_STATUS_UNVERSIONED;
1853 } else {
1854 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1855 status = GOT_STATUS_UNVERSIONED;
1856 else
1857 status = GOT_STATUS_OBSTRUCTED;
1861 if (status == GOT_STATUS_OBSTRUCTED) {
1862 if (ie)
1863 got_fileindex_entry_mark_skipped(ie);
1864 err = (*progress_cb)(progress_arg, status, path);
1865 goto done;
1867 if (status == GOT_STATUS_CONFLICT) {
1868 if (ie)
1869 got_fileindex_entry_mark_skipped(ie);
1870 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1871 path);
1872 goto done;
1875 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1876 (S_ISLNK(te->mode) ||
1877 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1879 * This is a regular file or an installed bad symlink.
1880 * If the file index indicates that this file is already
1881 * up-to-date with respect to the repository we can skip
1882 * updating contents of this file.
1884 if (got_fileindex_entry_has_commit(ie) &&
1885 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1886 SHA1_DIGEST_LENGTH) == 0) {
1887 /* Same commit. */
1888 err = sync_timestamps(worktree->root_fd,
1889 path, status, ie, &sb);
1890 if (err)
1891 goto done;
1892 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1893 path);
1894 goto done;
1896 if (got_fileindex_entry_has_blob(ie) &&
1897 memcmp(ie->blob_sha1, te->id.sha1,
1898 SHA1_DIGEST_LENGTH) == 0) {
1899 /* Different commit but the same blob. */
1900 err = sync_timestamps(worktree->root_fd,
1901 path, status, ie, &sb);
1902 if (err)
1903 goto done;
1904 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1905 path);
1906 goto done;
1910 fd1 = got_opentempfd();
1911 if (fd1 == -1) {
1912 err = got_error_from_errno("got_opentempfd");
1913 goto done;
1915 err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
1916 if (err)
1917 goto done;
1919 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1920 int update_timestamps;
1921 struct got_blob_object *blob2 = NULL;
1922 char *label_orig = NULL;
1923 if (got_fileindex_entry_has_blob(ie)) {
1924 fd2 = got_opentempfd();
1925 if (fd2 == -1) {
1926 err = got_error_from_errno("got_opentempfd");
1927 goto done;
1929 struct got_object_id id2;
1930 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1931 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
1932 fd2);
1933 if (err)
1934 goto done;
1936 if (got_fileindex_entry_has_commit(ie)) {
1937 char id_str[SHA1_DIGEST_STRING_LENGTH];
1938 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1939 sizeof(id_str)) == NULL) {
1940 err = got_error_path(id_str,
1941 GOT_ERR_BAD_OBJ_ID_STR);
1942 goto done;
1944 if (asprintf(&label_orig, "%s: commit %s",
1945 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1946 err = got_error_from_errno("asprintf");
1947 goto done;
1950 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1951 char *link_target;
1952 err = got_object_blob_read_to_str(&link_target, blob);
1953 if (err)
1954 goto done;
1955 err = merge_symlink(worktree, blob2, ondisk_path, path,
1956 label_orig, link_target, worktree->base_commit_id,
1957 repo, progress_cb, progress_arg);
1958 free(link_target);
1959 } else {
1960 err = merge_blob(&update_timestamps, worktree, blob2,
1961 ondisk_path, path, sb.st_mode, label_orig, blob,
1962 worktree->base_commit_id, repo,
1963 progress_cb, progress_arg);
1965 free(label_orig);
1966 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
1967 err = got_error_from_errno("close");
1968 goto done;
1970 if (blob2)
1971 got_object_blob_close(blob2);
1972 if (err)
1973 goto done;
1975 * Do not update timestamps of files with local changes.
1976 * Otherwise, a future status walk would treat them as
1977 * unmodified files again.
1979 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1980 blob->id.sha1, worktree->base_commit_id->sha1,
1981 update_timestamps);
1982 } else if (status == GOT_STATUS_MODE_CHANGE) {
1983 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1984 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1985 } else if (status == GOT_STATUS_DELETE) {
1986 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1987 if (err)
1988 goto done;
1989 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1990 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1991 if (err)
1992 goto done;
1993 } else {
1994 int is_bad_symlink = 0;
1995 if (S_ISLNK(te->mode)) {
1996 err = install_symlink(&is_bad_symlink, worktree,
1997 ondisk_path, path, blob,
1998 status == GOT_STATUS_MISSING, 0,
1999 status == GOT_STATUS_UNVERSIONED, 0,
2000 repo, progress_cb, progress_arg);
2001 } else {
2002 err = install_blob(worktree, ondisk_path, path,
2003 te->mode, sb.st_mode, blob,
2004 status == GOT_STATUS_MISSING, 0, 0,
2005 status == GOT_STATUS_UNVERSIONED, repo,
2006 progress_cb, progress_arg);
2008 if (err)
2009 goto done;
2011 if (ie) {
2012 err = got_fileindex_entry_update(ie,
2013 worktree->root_fd, path, blob->id.sha1,
2014 worktree->base_commit_id->sha1, 1);
2015 } else {
2016 err = create_fileindex_entry(&ie, fileindex,
2017 worktree->base_commit_id, worktree->root_fd, path,
2018 &blob->id);
2020 if (err)
2021 goto done;
2023 if (is_bad_symlink) {
2024 got_fileindex_entry_filetype_set(ie,
2025 GOT_FILEIDX_MODE_BAD_SYMLINK);
2029 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2030 err = got_error_from_errno("close");
2031 goto done;
2033 got_object_blob_close(blob);
2034 done:
2035 free(ondisk_path);
2036 return err;
2039 static const struct got_error *
2040 remove_ondisk_file(const char *root_path, const char *path)
2042 const struct got_error *err = NULL;
2043 char *ondisk_path = NULL, *parent = NULL;
2045 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2046 return got_error_from_errno("asprintf");
2048 if (unlink(ondisk_path) == -1) {
2049 if (errno != ENOENT)
2050 err = got_error_from_errno2("unlink", ondisk_path);
2051 } else {
2052 size_t root_len = strlen(root_path);
2053 err = got_path_dirname(&parent, ondisk_path);
2054 if (err)
2055 goto done;
2056 while (got_path_cmp(parent, root_path,
2057 strlen(parent), root_len) != 0) {
2058 free(ondisk_path);
2059 ondisk_path = parent;
2060 parent = NULL;
2061 if (rmdir(ondisk_path) == -1) {
2062 if (errno != ENOTEMPTY)
2063 err = got_error_from_errno2("rmdir",
2064 ondisk_path);
2065 break;
2067 err = got_path_dirname(&parent, ondisk_path);
2068 if (err)
2069 break;
2072 done:
2073 free(ondisk_path);
2074 free(parent);
2075 return err;
2078 static const struct got_error *
2079 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2080 struct got_fileindex_entry *ie, struct got_repository *repo,
2081 got_worktree_checkout_cb progress_cb, void *progress_arg)
2083 const struct got_error *err = NULL;
2084 unsigned char status;
2085 struct stat sb;
2086 char *ondisk_path;
2088 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2089 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2091 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2092 == -1)
2093 return got_error_from_errno("asprintf");
2095 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2096 if (err)
2097 goto done;
2099 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2100 char ondisk_target[PATH_MAX];
2101 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2102 sizeof(ondisk_target));
2103 if (ondisk_len == -1) {
2104 err = got_error_from_errno2("readlink", ondisk_path);
2105 goto done;
2107 ondisk_target[ondisk_len] = '\0';
2108 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2109 NULL, NULL, /* XXX pass common ancestor info? */
2110 ondisk_target, ondisk_path);
2111 if (err)
2112 goto done;
2113 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2114 ie->path);
2115 goto done;
2118 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2119 status == GOT_STATUS_ADD) {
2120 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2121 if (err)
2122 goto done;
2124 * Preserve the working file and change the deleted blob's
2125 * entry into a schedule-add entry.
2127 err = got_fileindex_entry_update(ie, worktree->root_fd,
2128 ie->path, NULL, NULL, 0);
2129 } else {
2130 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2131 if (err)
2132 goto done;
2133 if (status == GOT_STATUS_NO_CHANGE) {
2134 err = remove_ondisk_file(worktree->root_path, ie->path);
2135 if (err)
2136 goto done;
2138 got_fileindex_entry_remove(fileindex, ie);
2140 done:
2141 free(ondisk_path);
2142 return err;
2145 struct diff_cb_arg {
2146 struct got_fileindex *fileindex;
2147 struct got_worktree *worktree;
2148 struct got_repository *repo;
2149 got_worktree_checkout_cb progress_cb;
2150 void *progress_arg;
2151 got_cancel_cb cancel_cb;
2152 void *cancel_arg;
2155 static const struct got_error *
2156 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2157 struct got_tree_entry *te, const char *parent_path)
2159 struct diff_cb_arg *a = arg;
2161 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2162 return got_error(GOT_ERR_CANCELLED);
2164 return update_blob(a->worktree, a->fileindex, ie, te,
2165 ie->path, a->repo, a->progress_cb, a->progress_arg);
2168 static const struct got_error *
2169 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2171 struct diff_cb_arg *a = arg;
2173 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2174 return got_error(GOT_ERR_CANCELLED);
2176 return delete_blob(a->worktree, a->fileindex, ie,
2177 a->repo, a->progress_cb, a->progress_arg);
2180 static const struct got_error *
2181 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2183 struct diff_cb_arg *a = arg;
2184 const struct got_error *err;
2185 char *path;
2187 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2188 return got_error(GOT_ERR_CANCELLED);
2190 if (got_object_tree_entry_is_submodule(te))
2191 return NULL;
2193 if (asprintf(&path, "%s%s%s", parent_path,
2194 parent_path[0] ? "/" : "", te->name)
2195 == -1)
2196 return got_error_from_errno("asprintf");
2198 if (S_ISDIR(te->mode))
2199 err = add_dir_on_disk(a->worktree, path);
2200 else
2201 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2202 a->repo, a->progress_cb, a->progress_arg);
2204 free(path);
2205 return err;
2208 const struct got_error *
2209 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2211 uint32_t uuid_status;
2213 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2214 if (uuid_status != uuid_s_ok) {
2215 *uuidstr = NULL;
2216 return got_error_uuid(uuid_status, "uuid_to_string");
2219 return NULL;
2222 static const struct got_error *
2223 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2225 const struct got_error *err = NULL;
2226 char *uuidstr = NULL;
2228 *refname = NULL;
2230 err = got_worktree_get_uuid(&uuidstr, worktree);
2231 if (err)
2232 return err;
2234 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2235 err = got_error_from_errno("asprintf");
2236 *refname = NULL;
2238 free(uuidstr);
2239 return err;
2242 const struct got_error *
2243 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2245 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2248 static const struct got_error *
2249 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2251 return get_ref_name(refname, worktree,
2252 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2255 static const struct got_error *
2256 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2258 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2261 static const struct got_error *
2262 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2264 return get_ref_name(refname, worktree,
2265 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2268 static const struct got_error *
2269 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2271 return get_ref_name(refname, worktree,
2272 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2275 static const struct got_error *
2276 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2278 return get_ref_name(refname, worktree,
2279 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2282 static const struct got_error *
2283 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2285 return get_ref_name(refname, worktree,
2286 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2289 static const struct got_error *
2290 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2292 return get_ref_name(refname, worktree,
2293 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2296 static const struct got_error *
2297 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2299 return get_ref_name(refname, worktree,
2300 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2303 const struct got_error *
2304 got_worktree_get_histedit_script_path(char **path,
2305 struct got_worktree *worktree)
2307 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2308 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2309 *path = NULL;
2310 return got_error_from_errno("asprintf");
2312 return NULL;
2315 static const struct got_error *
2316 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2318 return get_ref_name(refname, worktree,
2319 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2322 static const struct got_error *
2323 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2325 return get_ref_name(refname, worktree,
2326 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2330 * Prevent Git's garbage collector from deleting our base commit by
2331 * setting a reference to our base commit's ID.
2333 static const struct got_error *
2334 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2336 const struct got_error *err = NULL;
2337 struct got_reference *ref = NULL;
2338 char *refname;
2340 err = got_worktree_get_base_ref_name(&refname, worktree);
2341 if (err)
2342 return err;
2344 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2345 if (err)
2346 goto done;
2348 err = got_ref_write(ref, repo);
2349 done:
2350 free(refname);
2351 if (ref)
2352 got_ref_close(ref);
2353 return err;
2356 static const struct got_error *
2357 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2359 const struct got_error *err = NULL;
2361 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2362 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2363 err = got_error_from_errno("asprintf");
2364 *fileindex_path = NULL;
2366 return err;
2370 static const struct got_error *
2371 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2372 struct got_worktree *worktree)
2374 const struct got_error *err = NULL;
2375 FILE *index = NULL;
2377 *fileindex_path = NULL;
2378 *fileindex = got_fileindex_alloc();
2379 if (*fileindex == NULL)
2380 return got_error_from_errno("got_fileindex_alloc");
2382 err = get_fileindex_path(fileindex_path, worktree);
2383 if (err)
2384 goto done;
2386 index = fopen(*fileindex_path, "rbe");
2387 if (index == NULL) {
2388 if (errno != ENOENT)
2389 err = got_error_from_errno2("fopen", *fileindex_path);
2390 } else {
2391 err = got_fileindex_read(*fileindex, index);
2392 if (fclose(index) == EOF && err == NULL)
2393 err = got_error_from_errno("fclose");
2395 done:
2396 if (err) {
2397 free(*fileindex_path);
2398 *fileindex_path = NULL;
2399 got_fileindex_free(*fileindex);
2400 *fileindex = NULL;
2402 return err;
2405 struct bump_base_commit_id_arg {
2406 struct got_object_id *base_commit_id;
2407 const char *path;
2408 size_t path_len;
2409 const char *entry_name;
2410 got_worktree_checkout_cb progress_cb;
2411 void *progress_arg;
2414 /* Bump base commit ID of all files within an updated part of the work tree. */
2415 static const struct got_error *
2416 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2418 const struct got_error *err;
2419 struct bump_base_commit_id_arg *a = arg;
2421 if (a->entry_name) {
2422 if (strcmp(ie->path, a->path) != 0)
2423 return NULL;
2424 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2425 return NULL;
2427 if (got_fileindex_entry_was_skipped(ie))
2428 return NULL;
2430 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2431 SHA1_DIGEST_LENGTH) == 0)
2432 return NULL;
2434 if (a->progress_cb) {
2435 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2436 ie->path);
2437 if (err)
2438 return err;
2440 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2441 return NULL;
2444 static const struct got_error *
2445 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2446 struct got_fileindex *fileindex,
2447 got_worktree_checkout_cb progress_cb, void *progress_arg)
2449 struct bump_base_commit_id_arg bbc_arg;
2451 bbc_arg.base_commit_id = worktree->base_commit_id;
2452 bbc_arg.entry_name = NULL;
2453 bbc_arg.path = "";
2454 bbc_arg.path_len = 0;
2455 bbc_arg.progress_cb = progress_cb;
2456 bbc_arg.progress_arg = progress_arg;
2458 return got_fileindex_for_each_entry_safe(fileindex,
2459 bump_base_commit_id, &bbc_arg);
2462 static const struct got_error *
2463 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2465 const struct got_error *err = NULL;
2466 char *new_fileindex_path = NULL;
2467 FILE *new_index = NULL;
2468 struct timespec timeout;
2470 err = got_opentemp_named(&new_fileindex_path, &new_index,
2471 fileindex_path, "");
2472 if (err)
2473 goto done;
2475 err = got_fileindex_write(fileindex, new_index);
2476 if (err)
2477 goto done;
2479 if (rename(new_fileindex_path, fileindex_path) != 0) {
2480 err = got_error_from_errno3("rename", new_fileindex_path,
2481 fileindex_path);
2482 unlink(new_fileindex_path);
2486 * Sleep for a short amount of time to ensure that files modified after
2487 * this program exits have a different time stamp from the one which
2488 * was recorded in the file index.
2490 timeout.tv_sec = 0;
2491 timeout.tv_nsec = 1;
2492 nanosleep(&timeout, NULL);
2493 done:
2494 if (new_index)
2495 fclose(new_index);
2496 free(new_fileindex_path);
2497 return err;
2500 static const struct got_error *
2501 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2502 struct got_object_id **tree_id, const char *wt_relpath,
2503 struct got_commit_object *base_commit, struct got_worktree *worktree,
2504 struct got_repository *repo)
2506 const struct got_error *err = NULL;
2507 struct got_object_id *id = NULL;
2508 char *in_repo_path = NULL;
2509 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2511 *entry_type = GOT_OBJ_TYPE_ANY;
2512 *tree_relpath = NULL;
2513 *tree_id = NULL;
2515 if (wt_relpath[0] == '\0') {
2516 /* Check out all files within the work tree. */
2517 *entry_type = GOT_OBJ_TYPE_TREE;
2518 *tree_relpath = strdup("");
2519 if (*tree_relpath == NULL) {
2520 err = got_error_from_errno("strdup");
2521 goto done;
2523 err = got_object_id_by_path(tree_id, repo, base_commit,
2524 worktree->path_prefix);
2525 if (err)
2526 goto done;
2527 return NULL;
2530 /* Check out a subset of files in the work tree. */
2532 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2533 is_root_wt ? "" : "/", wt_relpath) == -1) {
2534 err = got_error_from_errno("asprintf");
2535 goto done;
2538 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2539 if (err)
2540 goto done;
2542 free(in_repo_path);
2543 in_repo_path = NULL;
2545 err = got_object_get_type(entry_type, repo, id);
2546 if (err)
2547 goto done;
2549 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2550 /* Check out a single file. */
2551 if (strchr(wt_relpath, '/') == NULL) {
2552 /* Check out a single file in work tree's root dir. */
2553 in_repo_path = strdup(worktree->path_prefix);
2554 if (in_repo_path == NULL) {
2555 err = got_error_from_errno("strdup");
2556 goto done;
2558 *tree_relpath = strdup("");
2559 if (*tree_relpath == NULL) {
2560 err = got_error_from_errno("strdup");
2561 goto done;
2563 } else {
2564 /* Check out a single file in a subdirectory. */
2565 err = got_path_dirname(tree_relpath, wt_relpath);
2566 if (err)
2567 return err;
2568 if (asprintf(&in_repo_path, "%s%s%s",
2569 worktree->path_prefix, is_root_wt ? "" : "/",
2570 *tree_relpath) == -1) {
2571 err = got_error_from_errno("asprintf");
2572 goto done;
2575 err = got_object_id_by_path(tree_id, repo,
2576 base_commit, in_repo_path);
2577 } else {
2578 /* Check out all files within a subdirectory. */
2579 *tree_id = got_object_id_dup(id);
2580 if (*tree_id == NULL) {
2581 err = got_error_from_errno("got_object_id_dup");
2582 goto done;
2584 *tree_relpath = strdup(wt_relpath);
2585 if (*tree_relpath == NULL) {
2586 err = got_error_from_errno("strdup");
2587 goto done;
2590 done:
2591 free(id);
2592 free(in_repo_path);
2593 if (err) {
2594 *entry_type = GOT_OBJ_TYPE_ANY;
2595 free(*tree_relpath);
2596 *tree_relpath = NULL;
2597 free(*tree_id);
2598 *tree_id = NULL;
2600 return err;
2603 static const struct got_error *
2604 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2605 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2606 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2607 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2609 const struct got_error *err = NULL;
2610 struct got_commit_object *commit = NULL;
2611 struct got_tree_object *tree = NULL;
2612 struct got_fileindex_diff_tree_cb diff_cb;
2613 struct diff_cb_arg arg;
2615 err = ref_base_commit(worktree, repo);
2616 if (err) {
2617 if (!(err->code == GOT_ERR_ERRNO &&
2618 (errno == EACCES || errno == EROFS)))
2619 goto done;
2620 err = (*progress_cb)(progress_arg,
2621 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2622 if (err)
2623 return err;
2626 err = got_object_open_as_commit(&commit, repo,
2627 worktree->base_commit_id);
2628 if (err)
2629 goto done;
2631 err = got_object_open_as_tree(&tree, repo, tree_id);
2632 if (err)
2633 goto done;
2635 if (entry_name &&
2636 got_object_tree_find_entry(tree, entry_name) == NULL) {
2637 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2638 goto done;
2641 diff_cb.diff_old_new = diff_old_new;
2642 diff_cb.diff_old = diff_old;
2643 diff_cb.diff_new = diff_new;
2644 arg.fileindex = fileindex;
2645 arg.worktree = worktree;
2646 arg.repo = repo;
2647 arg.progress_cb = progress_cb;
2648 arg.progress_arg = progress_arg;
2649 arg.cancel_cb = cancel_cb;
2650 arg.cancel_arg = cancel_arg;
2651 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2652 entry_name, repo, &diff_cb, &arg);
2653 done:
2654 if (tree)
2655 got_object_tree_close(tree);
2656 if (commit)
2657 got_object_commit_close(commit);
2658 return err;
2661 const struct got_error *
2662 got_worktree_checkout_files(struct got_worktree *worktree,
2663 struct got_pathlist_head *paths, struct got_repository *repo,
2664 got_worktree_checkout_cb progress_cb, void *progress_arg,
2665 got_cancel_cb cancel_cb, void *cancel_arg)
2667 const struct got_error *err = NULL, *sync_err, *unlockerr;
2668 struct got_commit_object *commit = NULL;
2669 struct got_tree_object *tree = NULL;
2670 struct got_fileindex *fileindex = NULL;
2671 char *fileindex_path = NULL;
2672 struct got_pathlist_entry *pe;
2673 struct tree_path_data {
2674 STAILQ_ENTRY(tree_path_data) entry;
2675 struct got_object_id *tree_id;
2676 int entry_type;
2677 char *relpath;
2678 char *entry_name;
2679 } *tpd = NULL;
2680 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2682 STAILQ_INIT(&tree_paths);
2684 err = lock_worktree(worktree, LOCK_EX);
2685 if (err)
2686 return err;
2688 err = got_object_open_as_commit(&commit, repo,
2689 worktree->base_commit_id);
2690 if (err)
2691 goto done;
2693 /* Map all specified paths to in-repository trees. */
2694 TAILQ_FOREACH(pe, paths, entry) {
2695 tpd = malloc(sizeof(*tpd));
2696 if (tpd == NULL) {
2697 err = got_error_from_errno("malloc");
2698 goto done;
2701 err = find_tree_entry_for_checkout(&tpd->entry_type,
2702 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2703 worktree, repo);
2704 if (err) {
2705 free(tpd);
2706 goto done;
2709 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2710 err = got_path_basename(&tpd->entry_name, pe->path);
2711 if (err) {
2712 free(tpd->relpath);
2713 free(tpd->tree_id);
2714 free(tpd);
2715 goto done;
2717 } else
2718 tpd->entry_name = NULL;
2720 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2724 * Read the file index.
2725 * Checking out files is supposed to be an idempotent operation.
2726 * If the on-disk file index is incomplete we will try to complete it.
2728 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2729 if (err)
2730 goto done;
2732 tpd = STAILQ_FIRST(&tree_paths);
2733 TAILQ_FOREACH(pe, paths, entry) {
2734 struct bump_base_commit_id_arg bbc_arg;
2736 err = checkout_files(worktree, fileindex, tpd->relpath,
2737 tpd->tree_id, tpd->entry_name, repo,
2738 progress_cb, progress_arg, cancel_cb, cancel_arg);
2739 if (err)
2740 break;
2742 bbc_arg.base_commit_id = worktree->base_commit_id;
2743 bbc_arg.entry_name = tpd->entry_name;
2744 bbc_arg.path = pe->path;
2745 bbc_arg.path_len = pe->path_len;
2746 bbc_arg.progress_cb = progress_cb;
2747 bbc_arg.progress_arg = progress_arg;
2748 err = got_fileindex_for_each_entry_safe(fileindex,
2749 bump_base_commit_id, &bbc_arg);
2750 if (err)
2751 break;
2753 tpd = STAILQ_NEXT(tpd, entry);
2755 sync_err = sync_fileindex(fileindex, fileindex_path);
2756 if (sync_err && err == NULL)
2757 err = sync_err;
2758 done:
2759 free(fileindex_path);
2760 if (tree)
2761 got_object_tree_close(tree);
2762 if (commit)
2763 got_object_commit_close(commit);
2764 if (fileindex)
2765 got_fileindex_free(fileindex);
2766 while (!STAILQ_EMPTY(&tree_paths)) {
2767 tpd = STAILQ_FIRST(&tree_paths);
2768 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2769 free(tpd->relpath);
2770 free(tpd->tree_id);
2771 free(tpd);
2773 unlockerr = lock_worktree(worktree, LOCK_SH);
2774 if (unlockerr && err == NULL)
2775 err = unlockerr;
2776 return err;
2779 struct merge_file_cb_arg {
2780 struct got_worktree *worktree;
2781 struct got_fileindex *fileindex;
2782 got_worktree_checkout_cb progress_cb;
2783 void *progress_arg;
2784 got_cancel_cb cancel_cb;
2785 void *cancel_arg;
2786 const char *label_orig;
2787 struct got_object_id *commit_id2;
2788 int allow_bad_symlinks;
2791 static const struct got_error *
2792 merge_file_cb(void *arg, struct got_blob_object *blob1,
2793 struct got_blob_object *blob2, FILE *f1, FILE *f2,
2794 struct got_object_id *id1, struct got_object_id *id2,
2795 const char *path1, const char *path2,
2796 mode_t mode1, mode_t mode2, struct got_repository *repo)
2798 static const struct got_error *err = NULL;
2799 struct merge_file_cb_arg *a = arg;
2800 struct got_fileindex_entry *ie;
2801 char *ondisk_path = NULL;
2802 struct stat sb;
2803 unsigned char status;
2804 int local_changes_subsumed;
2805 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2806 char *id_str = NULL, *label_deriv2 = NULL;
2808 if (blob1 && blob2) {
2809 ie = got_fileindex_entry_get(a->fileindex, path2,
2810 strlen(path2));
2811 if (ie == NULL)
2812 return (*a->progress_cb)(a->progress_arg,
2813 GOT_STATUS_MISSING, path2);
2815 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2816 path2) == -1)
2817 return got_error_from_errno("asprintf");
2819 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2820 repo);
2821 if (err)
2822 goto done;
2824 if (status == GOT_STATUS_DELETE) {
2825 err = (*a->progress_cb)(a->progress_arg,
2826 GOT_STATUS_MERGE, path2);
2827 goto done;
2829 if (status != GOT_STATUS_NO_CHANGE &&
2830 status != GOT_STATUS_MODIFY &&
2831 status != GOT_STATUS_CONFLICT &&
2832 status != GOT_STATUS_ADD) {
2833 err = (*a->progress_cb)(a->progress_arg, status, path2);
2834 goto done;
2837 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2838 char *link_target2;
2839 err = got_object_blob_read_to_str(&link_target2, blob2);
2840 if (err)
2841 goto done;
2842 err = merge_symlink(a->worktree, blob1, ondisk_path,
2843 path2, a->label_orig, link_target2, a->commit_id2,
2844 repo, a->progress_cb, a->progress_arg);
2845 free(link_target2);
2846 } else {
2847 int fd;
2849 f_orig = got_opentemp();
2850 if (f_orig == NULL) {
2851 err = got_error_from_errno("got_opentemp");
2852 goto done;
2854 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2855 f_orig, blob1);
2856 if (err)
2857 goto done;
2859 f_deriv2 = got_opentemp();
2860 if (f_deriv2 == NULL)
2861 goto done;
2862 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2863 f_deriv2, blob2);
2864 if (err)
2865 goto done;
2867 fd = open(ondisk_path,
2868 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2869 if (fd == -1) {
2870 err = got_error_from_errno2("open",
2871 ondisk_path);
2872 goto done;
2874 f_deriv = fdopen(fd, "r");
2875 if (f_deriv == NULL) {
2876 err = got_error_from_errno2("fdopen",
2877 ondisk_path);
2878 close(fd);
2879 goto done;
2881 err = got_object_id_str(&id_str, a->commit_id2);
2882 if (err)
2883 goto done;
2884 if (asprintf(&label_deriv2, "%s: commit %s",
2885 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2886 err = got_error_from_errno("asprintf");
2887 goto done;
2889 err = merge_file(&local_changes_subsumed, a->worktree,
2890 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2891 sb.st_mode, a->label_orig, NULL, label_deriv2,
2892 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2893 a->progress_cb, a->progress_arg);
2895 } else if (blob1) {
2896 ie = got_fileindex_entry_get(a->fileindex, path1,
2897 strlen(path1));
2898 if (ie == NULL)
2899 return (*a->progress_cb)(a->progress_arg,
2900 GOT_STATUS_MISSING, path1);
2902 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2903 path1) == -1)
2904 return got_error_from_errno("asprintf");
2906 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2907 repo);
2908 if (err)
2909 goto done;
2911 switch (status) {
2912 case GOT_STATUS_NO_CHANGE:
2913 err = (*a->progress_cb)(a->progress_arg,
2914 GOT_STATUS_DELETE, path1);
2915 if (err)
2916 goto done;
2917 err = remove_ondisk_file(a->worktree->root_path, path1);
2918 if (err)
2919 goto done;
2920 if (ie)
2921 got_fileindex_entry_mark_deleted_from_disk(ie);
2922 break;
2923 case GOT_STATUS_DELETE:
2924 case GOT_STATUS_MISSING:
2925 err = (*a->progress_cb)(a->progress_arg,
2926 GOT_STATUS_DELETE, path1);
2927 if (err)
2928 goto done;
2929 if (ie)
2930 got_fileindex_entry_mark_deleted_from_disk(ie);
2931 break;
2932 case GOT_STATUS_ADD: {
2933 struct got_object_id *id;
2934 FILE *blob1_f;
2935 off_t blob1_size;
2937 * Delete the added file only if its content already
2938 * exists in the repository.
2940 err = got_object_blob_file_create(&id, &blob1_f,
2941 &blob1_size, path1);
2942 if (err)
2943 goto done;
2944 if (got_object_id_cmp(id, id1) == 0) {
2945 err = (*a->progress_cb)(a->progress_arg,
2946 GOT_STATUS_DELETE, path1);
2947 if (err)
2948 goto done;
2949 err = remove_ondisk_file(a->worktree->root_path,
2950 path1);
2951 if (err)
2952 goto done;
2953 if (ie)
2954 got_fileindex_entry_remove(a->fileindex,
2955 ie);
2956 } else {
2957 err = (*a->progress_cb)(a->progress_arg,
2958 GOT_STATUS_CANNOT_DELETE, path1);
2960 if (fclose(blob1_f) == EOF && err == NULL)
2961 err = got_error_from_errno("fclose");
2962 free(id);
2963 if (err)
2964 goto done;
2965 break;
2967 case GOT_STATUS_MODIFY:
2968 case GOT_STATUS_CONFLICT:
2969 err = (*a->progress_cb)(a->progress_arg,
2970 GOT_STATUS_CANNOT_DELETE, path1);
2971 if (err)
2972 goto done;
2973 break;
2974 case GOT_STATUS_OBSTRUCTED:
2975 err = (*a->progress_cb)(a->progress_arg, status, path1);
2976 if (err)
2977 goto done;
2978 break;
2979 default:
2980 break;
2982 } else if (blob2) {
2983 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2984 path2) == -1)
2985 return got_error_from_errno("asprintf");
2986 ie = got_fileindex_entry_get(a->fileindex, path2,
2987 strlen(path2));
2988 if (ie) {
2989 err = get_file_status(&status, &sb, ie, ondisk_path,
2990 -1, NULL, repo);
2991 if (err)
2992 goto done;
2993 if (status != GOT_STATUS_NO_CHANGE &&
2994 status != GOT_STATUS_MODIFY &&
2995 status != GOT_STATUS_CONFLICT &&
2996 status != GOT_STATUS_ADD) {
2997 err = (*a->progress_cb)(a->progress_arg,
2998 status, path2);
2999 goto done;
3001 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3002 char *link_target2;
3003 err = got_object_blob_read_to_str(&link_target2,
3004 blob2);
3005 if (err)
3006 goto done;
3007 err = merge_symlink(a->worktree, NULL,
3008 ondisk_path, path2, a->label_orig,
3009 link_target2, a->commit_id2, repo,
3010 a->progress_cb, a->progress_arg);
3011 free(link_target2);
3012 } else if (S_ISREG(sb.st_mode)) {
3013 err = merge_blob(&local_changes_subsumed,
3014 a->worktree, NULL, ondisk_path, path2,
3015 sb.st_mode, a->label_orig, blob2,
3016 a->commit_id2, repo, a->progress_cb,
3017 a->progress_arg);
3018 } else {
3019 err = got_error_path(ondisk_path,
3020 GOT_ERR_FILE_OBSTRUCTED);
3022 if (err)
3023 goto done;
3024 if (status == GOT_STATUS_DELETE) {
3025 err = got_fileindex_entry_update(ie,
3026 a->worktree->root_fd, path2, blob2->id.sha1,
3027 a->worktree->base_commit_id->sha1, 0);
3028 if (err)
3029 goto done;
3031 } else {
3032 int is_bad_symlink = 0;
3033 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3034 if (S_ISLNK(mode2)) {
3035 err = install_symlink(&is_bad_symlink,
3036 a->worktree, ondisk_path, path2, blob2, 0,
3037 0, 1, a->allow_bad_symlinks, repo,
3038 a->progress_cb, a->progress_arg);
3039 } else {
3040 err = install_blob(a->worktree, ondisk_path, path2,
3041 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3042 a->progress_cb, a->progress_arg);
3044 if (err)
3045 goto done;
3046 err = got_fileindex_entry_alloc(&ie, path2);
3047 if (err)
3048 goto done;
3049 err = got_fileindex_entry_update(ie,
3050 a->worktree->root_fd, path2, NULL, NULL, 1);
3051 if (err) {
3052 got_fileindex_entry_free(ie);
3053 goto done;
3055 err = got_fileindex_entry_add(a->fileindex, ie);
3056 if (err) {
3057 got_fileindex_entry_free(ie);
3058 goto done;
3060 if (is_bad_symlink) {
3061 got_fileindex_entry_filetype_set(ie,
3062 GOT_FILEIDX_MODE_BAD_SYMLINK);
3066 done:
3067 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3068 err = got_error_from_errno("fclose");
3069 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3070 err = got_error_from_errno("fclose");
3071 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3072 err = got_error_from_errno("fclose");
3073 free(id_str);
3074 free(label_deriv2);
3075 free(ondisk_path);
3076 return err;
3079 static const struct got_error *
3080 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3082 struct got_worktree *worktree = arg;
3084 /* Reject merges into a work tree with mixed base commits. */
3085 if (got_fileindex_entry_has_commit(ie) &&
3086 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3087 SHA1_DIGEST_LENGTH) != 0)
3088 return got_error(GOT_ERR_MIXED_COMMITS);
3090 return NULL;
3093 struct check_merge_conflicts_arg {
3094 struct got_worktree *worktree;
3095 struct got_fileindex *fileindex;
3096 struct got_repository *repo;
3099 static const struct got_error *
3100 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3101 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3102 struct got_object_id *id1, struct got_object_id *id2,
3103 const char *path1, const char *path2,
3104 mode_t mode1, mode_t mode2, struct got_repository *repo)
3106 const struct got_error *err = NULL;
3107 struct check_merge_conflicts_arg *a = arg;
3108 unsigned char status;
3109 struct stat sb;
3110 struct got_fileindex_entry *ie;
3111 const char *path = path2 ? path2 : path1;
3112 struct got_object_id *id = id2 ? id2 : id1;
3113 char *ondisk_path;
3115 if (id == NULL)
3116 return NULL;
3118 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3119 if (ie == NULL)
3120 return NULL;
3122 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3123 == -1)
3124 return got_error_from_errno("asprintf");
3126 /* Reject merges into a work tree with conflicted files. */
3127 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3128 free(ondisk_path);
3129 if (err)
3130 return err;
3131 if (status == GOT_STATUS_CONFLICT)
3132 return got_error(GOT_ERR_CONFLICTS);
3134 return NULL;
3137 static const struct got_error *
3138 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3139 const char *fileindex_path, struct got_object_id *commit_id1,
3140 struct got_object_id *commit_id2, struct got_repository *repo,
3141 got_worktree_checkout_cb progress_cb, void *progress_arg,
3142 got_cancel_cb cancel_cb, void *cancel_arg)
3144 const struct got_error *err = NULL, *sync_err;
3145 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3146 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3147 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3148 struct check_merge_conflicts_arg cmc_arg;
3149 struct merge_file_cb_arg arg;
3150 char *label_orig = NULL;
3151 FILE *f1 = NULL, *f2 = NULL;
3152 int fd1 = -1, fd2 = -1;
3154 if (commit_id1) {
3155 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3156 if (err)
3157 goto done;
3158 err = got_object_id_by_path(&tree_id1, repo, commit1,
3159 worktree->path_prefix);
3160 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3161 goto done;
3163 if (tree_id1) {
3164 char *id_str;
3166 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3167 if (err)
3168 goto done;
3170 err = got_object_id_str(&id_str, commit_id1);
3171 if (err)
3172 goto done;
3174 if (asprintf(&label_orig, "%s: commit %s",
3175 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3176 err = got_error_from_errno("asprintf");
3177 free(id_str);
3178 goto done;
3180 free(id_str);
3182 f1 = got_opentemp();
3183 if (f1 == NULL) {
3184 err = got_error_from_errno("got_opentemp");
3185 goto done;
3189 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3190 if (err)
3191 goto done;
3193 err = got_object_id_by_path(&tree_id2, repo, commit2,
3194 worktree->path_prefix);
3195 if (err)
3196 goto done;
3198 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3199 if (err)
3200 goto done;
3202 f2 = got_opentemp();
3203 if (f2 == NULL) {
3204 err = got_error_from_errno("got_opentemp");
3205 goto done;
3208 fd1 = got_opentempfd();
3209 if (fd1 == -1) {
3210 err = got_error_from_errno("got_opentempfd");
3211 goto done;
3214 fd2 = got_opentempfd();
3215 if (fd2 == -1) {
3216 err = got_error_from_errno("got_opentempfd");
3217 goto done;
3220 cmc_arg.worktree = worktree;
3221 cmc_arg.fileindex = fileindex;
3222 cmc_arg.repo = repo;
3223 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3224 check_merge_conflicts, &cmc_arg, 0);
3225 if (err)
3226 goto done;
3228 arg.worktree = worktree;
3229 arg.fileindex = fileindex;
3230 arg.progress_cb = progress_cb;
3231 arg.progress_arg = progress_arg;
3232 arg.cancel_cb = cancel_cb;
3233 arg.cancel_arg = cancel_arg;
3234 arg.label_orig = label_orig;
3235 arg.commit_id2 = commit_id2;
3236 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3237 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3238 merge_file_cb, &arg, 1);
3239 sync_err = sync_fileindex(fileindex, fileindex_path);
3240 if (sync_err && err == NULL)
3241 err = sync_err;
3242 done:
3243 if (commit1)
3244 got_object_commit_close(commit1);
3245 if (commit2)
3246 got_object_commit_close(commit2);
3247 if (tree1)
3248 got_object_tree_close(tree1);
3249 if (tree2)
3250 got_object_tree_close(tree2);
3251 if (f1 && fclose(f1) == EOF && err == NULL)
3252 err = got_error_from_errno("fclose");
3253 if (f2 && fclose(f2) == EOF && err == NULL)
3254 err = got_error_from_errno("fclose");
3255 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3256 err = got_error_from_errno("close");
3257 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3258 err = got_error_from_errno("close");
3259 free(label_orig);
3260 return err;
3263 const struct got_error *
3264 got_worktree_merge_files(struct got_worktree *worktree,
3265 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3266 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3267 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3269 const struct got_error *err, *unlockerr;
3270 char *fileindex_path = NULL;
3271 struct got_fileindex *fileindex = NULL;
3273 err = lock_worktree(worktree, LOCK_EX);
3274 if (err)
3275 return err;
3277 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3278 if (err)
3279 goto done;
3281 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3282 worktree);
3283 if (err)
3284 goto done;
3286 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3287 commit_id2, repo, progress_cb, progress_arg,
3288 cancel_cb, cancel_arg);
3289 done:
3290 if (fileindex)
3291 got_fileindex_free(fileindex);
3292 free(fileindex_path);
3293 unlockerr = lock_worktree(worktree, LOCK_SH);
3294 if (unlockerr && err == NULL)
3295 err = unlockerr;
3296 return err;
3299 struct diff_dir_cb_arg {
3300 struct got_fileindex *fileindex;
3301 struct got_worktree *worktree;
3302 const char *status_path;
3303 size_t status_path_len;
3304 struct got_repository *repo;
3305 got_worktree_status_cb status_cb;
3306 void *status_arg;
3307 got_cancel_cb cancel_cb;
3308 void *cancel_arg;
3309 /* A pathlist containing per-directory pathlists of ignore patterns. */
3310 struct got_pathlist_head *ignores;
3311 int report_unchanged;
3312 int no_ignores;
3315 static const struct got_error *
3316 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3317 int dirfd, const char *de_name,
3318 got_worktree_status_cb status_cb, void *status_arg,
3319 struct got_repository *repo, int report_unchanged)
3321 const struct got_error *err = NULL;
3322 unsigned char status = GOT_STATUS_NO_CHANGE;
3323 unsigned char staged_status = get_staged_status(ie);
3324 struct stat sb;
3325 struct got_object_id blob_id, commit_id, staged_blob_id;
3326 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3327 struct got_object_id *staged_blob_idp = NULL;
3329 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3330 if (err)
3331 return err;
3333 if (status == GOT_STATUS_NO_CHANGE &&
3334 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3335 return NULL;
3337 if (got_fileindex_entry_has_blob(ie)) {
3338 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3339 blob_idp = &blob_id;
3341 if (got_fileindex_entry_has_commit(ie)) {
3342 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3343 commit_idp = &commit_id;
3345 if (staged_status == GOT_STATUS_ADD ||
3346 staged_status == GOT_STATUS_MODIFY) {
3347 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3348 SHA1_DIGEST_LENGTH);
3349 staged_blob_idp = &staged_blob_id;
3352 return (*status_cb)(status_arg, status, staged_status,
3353 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3356 static const struct got_error *
3357 status_old_new(void *arg, struct got_fileindex_entry *ie,
3358 struct dirent *de, const char *parent_path, int dirfd)
3360 const struct got_error *err = NULL;
3361 struct diff_dir_cb_arg *a = arg;
3362 char *abspath;
3364 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3365 return got_error(GOT_ERR_CANCELLED);
3367 if (got_path_cmp(parent_path, a->status_path,
3368 strlen(parent_path), a->status_path_len) != 0 &&
3369 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3370 return NULL;
3372 if (parent_path[0]) {
3373 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3374 parent_path, de->d_name) == -1)
3375 return got_error_from_errno("asprintf");
3376 } else {
3377 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3378 de->d_name) == -1)
3379 return got_error_from_errno("asprintf");
3382 err = report_file_status(ie, abspath, dirfd, de->d_name,
3383 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3384 free(abspath);
3385 return err;
3388 static const struct got_error *
3389 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3391 struct diff_dir_cb_arg *a = arg;
3392 struct got_object_id blob_id, commit_id;
3393 unsigned char status;
3395 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3396 return got_error(GOT_ERR_CANCELLED);
3398 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3399 return NULL;
3401 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3402 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3403 if (got_fileindex_entry_has_file_on_disk(ie))
3404 status = GOT_STATUS_MISSING;
3405 else
3406 status = GOT_STATUS_DELETE;
3407 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3408 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3411 static void
3412 free_ignores(struct got_pathlist_head *ignores)
3414 struct got_pathlist_entry *pe;
3416 TAILQ_FOREACH(pe, ignores, entry) {
3417 struct got_pathlist_head *ignorelist = pe->data;
3419 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3421 got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3424 static const struct got_error *
3425 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3427 const struct got_error *err = NULL;
3428 struct got_pathlist_entry *pe = NULL;
3429 struct got_pathlist_head *ignorelist;
3430 char *line = NULL, *pattern, *dirpath = NULL;
3431 size_t linesize = 0;
3432 ssize_t linelen;
3434 ignorelist = calloc(1, sizeof(*ignorelist));
3435 if (ignorelist == NULL)
3436 return got_error_from_errno("calloc");
3437 TAILQ_INIT(ignorelist);
3439 while ((linelen = getline(&line, &linesize, f)) != -1) {
3440 if (linelen > 0 && line[linelen - 1] == '\n')
3441 line[linelen - 1] = '\0';
3443 /* Git's ignores may contain comments. */
3444 if (line[0] == '#')
3445 continue;
3447 /* Git's negated patterns are not (yet?) supported. */
3448 if (line[0] == '!')
3449 continue;
3451 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3452 line) == -1) {
3453 err = got_error_from_errno("asprintf");
3454 goto done;
3456 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3457 if (err)
3458 goto done;
3460 if (ferror(f)) {
3461 err = got_error_from_errno("getline");
3462 goto done;
3465 dirpath = strdup(path);
3466 if (dirpath == NULL) {
3467 err = got_error_from_errno("strdup");
3468 goto done;
3470 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3471 done:
3472 free(line);
3473 if (err || pe == NULL) {
3474 free(dirpath);
3475 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3477 return err;
3480 static int
3481 match_ignores(struct got_pathlist_head *ignores, const char *path)
3483 struct got_pathlist_entry *pe;
3485 /* Handle patterns which match in all directories. */
3486 TAILQ_FOREACH(pe, ignores, entry) {
3487 struct got_pathlist_head *ignorelist = pe->data;
3488 struct got_pathlist_entry *pi;
3490 TAILQ_FOREACH(pi, ignorelist, entry) {
3491 const char *p, *pattern = pi->path;
3493 if (strncmp(pattern, "**/", 3) != 0)
3494 continue;
3495 pattern += 3;
3496 p = path;
3497 while (*p) {
3498 if (fnmatch(pattern, p,
3499 FNM_PATHNAME | FNM_LEADING_DIR)) {
3500 /* Retry in next directory. */
3501 while (*p && *p != '/')
3502 p++;
3503 while (*p == '/')
3504 p++;
3505 continue;
3507 return 1;
3513 * The ignores pathlist contains ignore lists from children before
3514 * parents, so we can find the most specific ignorelist by walking
3515 * ignores backwards.
3517 pe = TAILQ_LAST(ignores, got_pathlist_head);
3518 while (pe) {
3519 if (got_path_is_child(path, pe->path, pe->path_len)) {
3520 struct got_pathlist_head *ignorelist = pe->data;
3521 struct got_pathlist_entry *pi;
3522 TAILQ_FOREACH(pi, ignorelist, entry) {
3523 const char *pattern = pi->path;
3524 int flags = FNM_LEADING_DIR;
3525 if (strstr(pattern, "/**/") == NULL)
3526 flags |= FNM_PATHNAME;
3527 if (fnmatch(pattern, path, flags))
3528 continue;
3529 return 1;
3532 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3535 return 0;
3538 static const struct got_error *
3539 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3540 const char *path, int dirfd, const char *ignores_filename)
3542 const struct got_error *err = NULL;
3543 char *ignorespath;
3544 int fd = -1;
3545 FILE *ignoresfile = NULL;
3547 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3548 path[0] ? "/" : "", ignores_filename) == -1)
3549 return got_error_from_errno("asprintf");
3551 if (dirfd != -1) {
3552 fd = openat(dirfd, ignores_filename,
3553 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3554 if (fd == -1) {
3555 if (errno != ENOENT && errno != EACCES)
3556 err = got_error_from_errno2("openat",
3557 ignorespath);
3558 } else {
3559 ignoresfile = fdopen(fd, "r");
3560 if (ignoresfile == NULL)
3561 err = got_error_from_errno2("fdopen",
3562 ignorespath);
3563 else {
3564 fd = -1;
3565 err = read_ignores(ignores, path, ignoresfile);
3568 } else {
3569 ignoresfile = fopen(ignorespath, "re");
3570 if (ignoresfile == NULL) {
3571 if (errno != ENOENT && errno != EACCES)
3572 err = got_error_from_errno2("fopen",
3573 ignorespath);
3574 } else
3575 err = read_ignores(ignores, path, ignoresfile);
3578 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3579 err = got_error_from_errno2("fclose", path);
3580 if (fd != -1 && close(fd) == -1 && err == NULL)
3581 err = got_error_from_errno2("close", path);
3582 free(ignorespath);
3583 return err;
3586 static const struct got_error *
3587 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3588 int dirfd)
3590 const struct got_error *err = NULL;
3591 struct diff_dir_cb_arg *a = arg;
3592 char *path = NULL;
3594 if (ignore != NULL)
3595 *ignore = 0;
3597 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3598 return got_error(GOT_ERR_CANCELLED);
3600 if (parent_path[0]) {
3601 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3602 return got_error_from_errno("asprintf");
3603 } else {
3604 path = de->d_name;
3607 if (de->d_type == DT_DIR) {
3608 if (!a->no_ignores && ignore != NULL &&
3609 match_ignores(a->ignores, path))
3610 *ignore = 1;
3611 } else if (!match_ignores(a->ignores, path) &&
3612 got_path_is_child(path, a->status_path, a->status_path_len))
3613 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3614 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3615 if (parent_path[0])
3616 free(path);
3617 return err;
3620 static const struct got_error *
3621 status_traverse(void *arg, const char *path, int dirfd)
3623 const struct got_error *err = NULL;
3624 struct diff_dir_cb_arg *a = arg;
3626 if (a->no_ignores)
3627 return NULL;
3629 err = add_ignores(a->ignores, a->worktree->root_path,
3630 path, dirfd, ".cvsignore");
3631 if (err)
3632 return err;
3634 err = add_ignores(a->ignores, a->worktree->root_path, path,
3635 dirfd, ".gitignore");
3637 return err;
3640 static const struct got_error *
3641 report_single_file_status(const char *path, const char *ondisk_path,
3642 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3643 void *status_arg, struct got_repository *repo, int report_unchanged,
3644 struct got_pathlist_head *ignores, int no_ignores)
3646 struct got_fileindex_entry *ie;
3647 struct stat sb;
3649 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3650 if (ie)
3651 return report_file_status(ie, ondisk_path, -1, NULL,
3652 status_cb, status_arg, repo, report_unchanged);
3654 if (lstat(ondisk_path, &sb) == -1) {
3655 if (errno != ENOENT)
3656 return got_error_from_errno2("lstat", ondisk_path);
3657 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3658 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3661 if (!no_ignores && match_ignores(ignores, path))
3662 return NULL;
3664 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3665 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3666 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3668 return NULL;
3671 static const struct got_error *
3672 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3673 const char *root_path, const char *path)
3675 const struct got_error *err;
3676 char *parent_path, *next_parent_path = NULL;
3678 err = add_ignores(ignores, root_path, "", -1,
3679 ".cvsignore");
3680 if (err)
3681 return err;
3683 err = add_ignores(ignores, root_path, "", -1,
3684 ".gitignore");
3685 if (err)
3686 return err;
3688 err = got_path_dirname(&parent_path, path);
3689 if (err) {
3690 if (err->code == GOT_ERR_BAD_PATH)
3691 return NULL; /* cannot traverse parent */
3692 return err;
3694 for (;;) {
3695 err = add_ignores(ignores, root_path, parent_path, -1,
3696 ".cvsignore");
3697 if (err)
3698 break;
3699 err = add_ignores(ignores, root_path, parent_path, -1,
3700 ".gitignore");
3701 if (err)
3702 break;
3703 err = got_path_dirname(&next_parent_path, parent_path);
3704 if (err) {
3705 if (err->code == GOT_ERR_BAD_PATH)
3706 err = NULL; /* traversed everything */
3707 break;
3709 if (got_path_is_root_dir(parent_path))
3710 break;
3711 free(parent_path);
3712 parent_path = next_parent_path;
3713 next_parent_path = NULL;
3716 free(parent_path);
3717 free(next_parent_path);
3718 return err;
3721 static const struct got_error *
3722 worktree_status(struct got_worktree *worktree, const char *path,
3723 struct got_fileindex *fileindex, struct got_repository *repo,
3724 got_worktree_status_cb status_cb, void *status_arg,
3725 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3726 int report_unchanged)
3728 const struct got_error *err = NULL;
3729 int fd = -1;
3730 struct got_fileindex_diff_dir_cb fdiff_cb;
3731 struct diff_dir_cb_arg arg;
3732 char *ondisk_path = NULL;
3733 struct got_pathlist_head ignores;
3734 struct got_fileindex_entry *ie;
3736 TAILQ_INIT(&ignores);
3738 if (asprintf(&ondisk_path, "%s%s%s",
3739 worktree->root_path, path[0] ? "/" : "", path) == -1)
3740 return got_error_from_errno("asprintf");
3742 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3743 if (ie) {
3744 err = report_single_file_status(path, ondisk_path,
3745 fileindex, status_cb, status_arg, repo,
3746 report_unchanged, &ignores, no_ignores);
3747 goto done;
3750 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3751 if (fd == -1) {
3752 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3753 !got_err_open_nofollow_on_symlink())
3754 err = got_error_from_errno2("open", ondisk_path);
3755 else {
3756 if (!no_ignores) {
3757 err = add_ignores_from_parent_paths(&ignores,
3758 worktree->root_path, ondisk_path);
3759 if (err)
3760 goto done;
3762 err = report_single_file_status(path, ondisk_path,
3763 fileindex, status_cb, status_arg, repo,
3764 report_unchanged, &ignores, no_ignores);
3766 } else {
3767 fdiff_cb.diff_old_new = status_old_new;
3768 fdiff_cb.diff_old = status_old;
3769 fdiff_cb.diff_new = status_new;
3770 fdiff_cb.diff_traverse = status_traverse;
3771 arg.fileindex = fileindex;
3772 arg.worktree = worktree;
3773 arg.status_path = path;
3774 arg.status_path_len = strlen(path);
3775 arg.repo = repo;
3776 arg.status_cb = status_cb;
3777 arg.status_arg = status_arg;
3778 arg.cancel_cb = cancel_cb;
3779 arg.cancel_arg = cancel_arg;
3780 arg.report_unchanged = report_unchanged;
3781 arg.no_ignores = no_ignores;
3782 if (!no_ignores) {
3783 err = add_ignores_from_parent_paths(&ignores,
3784 worktree->root_path, path);
3785 if (err)
3786 goto done;
3788 arg.ignores = &ignores;
3789 err = got_fileindex_diff_dir(fileindex, fd,
3790 worktree->root_path, path, repo, &fdiff_cb, &arg);
3792 done:
3793 free_ignores(&ignores);
3794 if (fd != -1 && close(fd) == -1 && err == NULL)
3795 err = got_error_from_errno("close");
3796 free(ondisk_path);
3797 return err;
3800 const struct got_error *
3801 got_worktree_status(struct got_worktree *worktree,
3802 struct got_pathlist_head *paths, struct got_repository *repo,
3803 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3804 got_cancel_cb cancel_cb, void *cancel_arg)
3806 const struct got_error *err = NULL;
3807 char *fileindex_path = NULL;
3808 struct got_fileindex *fileindex = NULL;
3809 struct got_pathlist_entry *pe;
3811 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3812 if (err)
3813 return err;
3815 TAILQ_FOREACH(pe, paths, entry) {
3816 err = worktree_status(worktree, pe->path, fileindex, repo,
3817 status_cb, status_arg, cancel_cb, cancel_arg,
3818 no_ignores, 0);
3819 if (err)
3820 break;
3822 free(fileindex_path);
3823 got_fileindex_free(fileindex);
3824 return err;
3827 const struct got_error *
3828 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3829 const char *arg)
3831 const struct got_error *err = NULL;
3832 char *resolved = NULL, *cwd = NULL, *path = NULL;
3833 size_t len;
3834 struct stat sb;
3835 char *abspath = NULL;
3836 char canonpath[PATH_MAX];
3838 *wt_path = NULL;
3840 cwd = getcwd(NULL, 0);
3841 if (cwd == NULL)
3842 return got_error_from_errno("getcwd");
3844 if (lstat(arg, &sb) == -1) {
3845 if (errno != ENOENT) {
3846 err = got_error_from_errno2("lstat", arg);
3847 goto done;
3849 sb.st_mode = 0;
3851 if (S_ISLNK(sb.st_mode)) {
3853 * We cannot use realpath(3) with symlinks since we want to
3854 * operate on the symlink itself.
3855 * But we can make the path absolute, assuming it is relative
3856 * to the current working directory, and then canonicalize it.
3858 if (!got_path_is_absolute(arg)) {
3859 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3860 err = got_error_from_errno("asprintf");
3861 goto done;
3865 err = got_canonpath(abspath ? abspath : arg, canonpath,
3866 sizeof(canonpath));
3867 if (err)
3868 goto done;
3869 resolved = strdup(canonpath);
3870 if (resolved == NULL) {
3871 err = got_error_from_errno("strdup");
3872 goto done;
3874 } else {
3875 resolved = realpath(arg, NULL);
3876 if (resolved == NULL) {
3877 if (errno != ENOENT) {
3878 err = got_error_from_errno2("realpath", arg);
3879 goto done;
3881 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3882 err = got_error_from_errno("asprintf");
3883 goto done;
3885 err = got_canonpath(abspath, canonpath,
3886 sizeof(canonpath));
3887 if (err)
3888 goto done;
3889 resolved = strdup(canonpath);
3890 if (resolved == NULL) {
3891 err = got_error_from_errno("strdup");
3892 goto done;
3897 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3898 strlen(got_worktree_get_root_path(worktree)))) {
3899 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3900 goto done;
3903 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3904 err = got_path_skip_common_ancestor(&path,
3905 got_worktree_get_root_path(worktree), resolved);
3906 if (err)
3907 goto done;
3908 } else {
3909 path = strdup("");
3910 if (path == NULL) {
3911 err = got_error_from_errno("strdup");
3912 goto done;
3916 /* XXX status walk can't deal with trailing slash! */
3917 len = strlen(path);
3918 while (len > 0 && path[len - 1] == '/') {
3919 path[len - 1] = '\0';
3920 len--;
3922 done:
3923 free(abspath);
3924 free(resolved);
3925 free(cwd);
3926 if (err == NULL)
3927 *wt_path = path;
3928 else
3929 free(path);
3930 return err;
3933 struct schedule_addition_args {
3934 struct got_worktree *worktree;
3935 struct got_fileindex *fileindex;
3936 got_worktree_checkout_cb progress_cb;
3937 void *progress_arg;
3938 struct got_repository *repo;
3941 static const struct got_error *
3942 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3943 const char *relpath, struct got_object_id *blob_id,
3944 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3945 int dirfd, const char *de_name)
3947 struct schedule_addition_args *a = arg;
3948 const struct got_error *err = NULL;
3949 struct got_fileindex_entry *ie;
3950 struct stat sb;
3951 char *ondisk_path;
3953 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3954 relpath) == -1)
3955 return got_error_from_errno("asprintf");
3957 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3958 if (ie) {
3959 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3960 de_name, a->repo);
3961 if (err)
3962 goto done;
3963 /* Re-adding an existing entry is a no-op. */
3964 if (status == GOT_STATUS_ADD)
3965 goto done;
3966 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3967 if (err)
3968 goto done;
3971 if (status != GOT_STATUS_UNVERSIONED) {
3972 if (status == GOT_STATUS_NONEXISTENT)
3973 err = got_error_set_errno(ENOENT, ondisk_path);
3974 else
3975 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3976 goto done;
3979 err = got_fileindex_entry_alloc(&ie, relpath);
3980 if (err)
3981 goto done;
3982 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3983 relpath, NULL, NULL, 1);
3984 if (err) {
3985 got_fileindex_entry_free(ie);
3986 goto done;
3988 err = got_fileindex_entry_add(a->fileindex, ie);
3989 if (err) {
3990 got_fileindex_entry_free(ie);
3991 goto done;
3993 done:
3994 free(ondisk_path);
3995 if (err)
3996 return err;
3997 if (status == GOT_STATUS_ADD)
3998 return NULL;
3999 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4002 const struct got_error *
4003 got_worktree_schedule_add(struct got_worktree *worktree,
4004 struct got_pathlist_head *paths,
4005 got_worktree_checkout_cb progress_cb, void *progress_arg,
4006 struct got_repository *repo, int no_ignores)
4008 struct got_fileindex *fileindex = NULL;
4009 char *fileindex_path = NULL;
4010 const struct got_error *err = NULL, *sync_err, *unlockerr;
4011 struct got_pathlist_entry *pe;
4012 struct schedule_addition_args saa;
4014 err = lock_worktree(worktree, LOCK_EX);
4015 if (err)
4016 return err;
4018 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4019 if (err)
4020 goto done;
4022 saa.worktree = worktree;
4023 saa.fileindex = fileindex;
4024 saa.progress_cb = progress_cb;
4025 saa.progress_arg = progress_arg;
4026 saa.repo = repo;
4028 TAILQ_FOREACH(pe, paths, entry) {
4029 err = worktree_status(worktree, pe->path, fileindex, repo,
4030 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4031 if (err)
4032 break;
4034 sync_err = sync_fileindex(fileindex, fileindex_path);
4035 if (sync_err && err == NULL)
4036 err = sync_err;
4037 done:
4038 free(fileindex_path);
4039 if (fileindex)
4040 got_fileindex_free(fileindex);
4041 unlockerr = lock_worktree(worktree, LOCK_SH);
4042 if (unlockerr && err == NULL)
4043 err = unlockerr;
4044 return err;
4047 struct schedule_deletion_args {
4048 struct got_worktree *worktree;
4049 struct got_fileindex *fileindex;
4050 got_worktree_delete_cb progress_cb;
4051 void *progress_arg;
4052 struct got_repository *repo;
4053 int delete_local_mods;
4054 int keep_on_disk;
4055 int ignore_missing_paths;
4056 const char *status_codes;
4059 static const struct got_error *
4060 schedule_for_deletion(void *arg, unsigned char status,
4061 unsigned char staged_status, const char *relpath,
4062 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4063 struct got_object_id *commit_id, int dirfd, const char *de_name)
4065 struct schedule_deletion_args *a = arg;
4066 const struct got_error *err = NULL;
4067 struct got_fileindex_entry *ie = NULL;
4068 struct stat sb;
4069 char *ondisk_path;
4071 if (status == GOT_STATUS_NONEXISTENT) {
4072 if (a->ignore_missing_paths)
4073 return NULL;
4074 return got_error_set_errno(ENOENT, relpath);
4077 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4078 if (ie == NULL)
4079 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4081 staged_status = get_staged_status(ie);
4082 if (staged_status != GOT_STATUS_NO_CHANGE) {
4083 if (staged_status == GOT_STATUS_DELETE)
4084 return NULL;
4085 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4088 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4089 relpath) == -1)
4090 return got_error_from_errno("asprintf");
4092 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4093 a->repo);
4094 if (err)
4095 goto done;
4097 if (a->status_codes) {
4098 size_t ncodes = strlen(a->status_codes);
4099 int i;
4100 for (i = 0; i < ncodes ; i++) {
4101 if (status == a->status_codes[i])
4102 break;
4104 if (i == ncodes) {
4105 /* Do not delete files in non-matching status. */
4106 free(ondisk_path);
4107 return NULL;
4109 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4110 a->status_codes[i] != GOT_STATUS_MISSING) {
4111 static char msg[64];
4112 snprintf(msg, sizeof(msg),
4113 "invalid status code '%c'", a->status_codes[i]);
4114 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4115 goto done;
4119 if (status != GOT_STATUS_NO_CHANGE) {
4120 if (status == GOT_STATUS_DELETE)
4121 goto done;
4122 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4123 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4124 goto done;
4126 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4127 err = got_error_set_errno(ENOENT, relpath);
4128 goto done;
4130 if (status != GOT_STATUS_MODIFY &&
4131 status != GOT_STATUS_MISSING) {
4132 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4133 goto done;
4137 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4138 size_t root_len;
4140 if (dirfd != -1) {
4141 if (unlinkat(dirfd, de_name, 0) == -1) {
4142 err = got_error_from_errno2("unlinkat",
4143 ondisk_path);
4144 goto done;
4146 } else if (unlink(ondisk_path) == -1) {
4147 err = got_error_from_errno2("unlink", ondisk_path);
4148 goto done;
4151 root_len = strlen(a->worktree->root_path);
4152 do {
4153 char *parent;
4154 err = got_path_dirname(&parent, ondisk_path);
4155 if (err)
4156 goto done;
4157 free(ondisk_path);
4158 ondisk_path = parent;
4159 if (rmdir(ondisk_path) == -1) {
4160 if (errno != ENOTEMPTY)
4161 err = got_error_from_errno2("rmdir",
4162 ondisk_path);
4163 break;
4165 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4166 strlen(ondisk_path), root_len) != 0);
4169 got_fileindex_entry_mark_deleted_from_disk(ie);
4170 done:
4171 free(ondisk_path);
4172 if (err)
4173 return err;
4174 if (status == GOT_STATUS_DELETE)
4175 return NULL;
4176 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4177 staged_status, relpath);
4180 const struct got_error *
4181 got_worktree_schedule_delete(struct got_worktree *worktree,
4182 struct got_pathlist_head *paths, int delete_local_mods,
4183 const char *status_codes,
4184 got_worktree_delete_cb progress_cb, void *progress_arg,
4185 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4187 struct got_fileindex *fileindex = NULL;
4188 char *fileindex_path = NULL;
4189 const struct got_error *err = NULL, *sync_err, *unlockerr;
4190 struct got_pathlist_entry *pe;
4191 struct schedule_deletion_args sda;
4193 err = lock_worktree(worktree, LOCK_EX);
4194 if (err)
4195 return err;
4197 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4198 if (err)
4199 goto done;
4201 sda.worktree = worktree;
4202 sda.fileindex = fileindex;
4203 sda.progress_cb = progress_cb;
4204 sda.progress_arg = progress_arg;
4205 sda.repo = repo;
4206 sda.delete_local_mods = delete_local_mods;
4207 sda.keep_on_disk = keep_on_disk;
4208 sda.ignore_missing_paths = ignore_missing_paths;
4209 sda.status_codes = status_codes;
4211 TAILQ_FOREACH(pe, paths, entry) {
4212 err = worktree_status(worktree, pe->path, fileindex, repo,
4213 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4214 if (err)
4215 break;
4217 sync_err = sync_fileindex(fileindex, fileindex_path);
4218 if (sync_err && err == NULL)
4219 err = sync_err;
4220 done:
4221 free(fileindex_path);
4222 if (fileindex)
4223 got_fileindex_free(fileindex);
4224 unlockerr = lock_worktree(worktree, LOCK_SH);
4225 if (unlockerr && err == NULL)
4226 err = unlockerr;
4227 return err;
4230 static const struct got_error *
4231 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4233 const struct got_error *err = NULL;
4234 char *line = NULL;
4235 size_t linesize = 0, n;
4236 ssize_t linelen;
4238 linelen = getline(&line, &linesize, infile);
4239 if (linelen == -1) {
4240 if (ferror(infile)) {
4241 err = got_error_from_errno("getline");
4242 goto done;
4244 return NULL;
4246 if (outfile) {
4247 n = fwrite(line, 1, linelen, outfile);
4248 if (n != linelen) {
4249 err = got_ferror(outfile, GOT_ERR_IO);
4250 goto done;
4253 if (rejectfile) {
4254 n = fwrite(line, 1, linelen, rejectfile);
4255 if (n != linelen)
4256 err = got_ferror(rejectfile, GOT_ERR_IO);
4258 done:
4259 free(line);
4260 return err;
4263 static const struct got_error *
4264 skip_one_line(FILE *f)
4266 char *line = NULL;
4267 size_t linesize = 0;
4268 ssize_t linelen;
4270 linelen = getline(&line, &linesize, f);
4271 if (linelen == -1) {
4272 if (ferror(f))
4273 return got_error_from_errno("getline");
4274 return NULL;
4276 free(line);
4277 return NULL;
4280 static const struct got_error *
4281 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4282 int start_old, int end_old, int start_new, int end_new,
4283 FILE *outfile, FILE *rejectfile)
4285 const struct got_error *err;
4287 /* Copy old file's lines leading up to patch. */
4288 while (!feof(f1) && *line_cur1 < start_old) {
4289 err = copy_one_line(f1, outfile, NULL);
4290 if (err)
4291 return err;
4292 (*line_cur1)++;
4294 /* Skip new file's lines leading up to patch. */
4295 while (!feof(f2) && *line_cur2 < start_new) {
4296 if (rejectfile)
4297 err = copy_one_line(f2, NULL, rejectfile);
4298 else
4299 err = skip_one_line(f2);
4300 if (err)
4301 return err;
4302 (*line_cur2)++;
4304 /* Copy patched lines. */
4305 while (!feof(f2) && *line_cur2 <= end_new) {
4306 err = copy_one_line(f2, outfile, NULL);
4307 if (err)
4308 return err;
4309 (*line_cur2)++;
4311 /* Skip over old file's replaced lines. */
4312 while (!feof(f1) && *line_cur1 <= end_old) {
4313 if (rejectfile)
4314 err = copy_one_line(f1, NULL, rejectfile);
4315 else
4316 err = skip_one_line(f1);
4317 if (err)
4318 return err;
4319 (*line_cur1)++;
4322 return NULL;
4325 static const struct got_error *
4326 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4327 FILE *outfile, FILE *rejectfile)
4329 const struct got_error *err;
4331 if (outfile) {
4332 /* Copy old file's lines until EOF. */
4333 while (!feof(f1)) {
4334 err = copy_one_line(f1, outfile, NULL);
4335 if (err)
4336 return err;
4337 (*line_cur1)++;
4340 if (rejectfile) {
4341 /* Copy new file's lines until EOF. */
4342 while (!feof(f2)) {
4343 err = copy_one_line(f2, NULL, rejectfile);
4344 if (err)
4345 return err;
4346 (*line_cur2)++;
4350 return NULL;
4353 static const struct got_error *
4354 apply_or_reject_change(int *choice, int *nchunks_used,
4355 struct diff_result *diff_result, int n,
4356 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4357 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4358 got_worktree_patch_cb patch_cb, void *patch_arg)
4360 const struct got_error *err = NULL;
4361 struct diff_chunk_context cc = {};
4362 int start_old, end_old, start_new, end_new;
4363 FILE *hunkfile;
4364 struct diff_output_unidiff_state *diff_state;
4365 struct diff_input_info diff_info;
4366 int rc;
4368 *choice = GOT_PATCH_CHOICE_NONE;
4370 /* Get changed line numbers without context lines for copy_change(). */
4371 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4372 start_old = cc.left.start;
4373 end_old = cc.left.end;
4374 start_new = cc.right.start;
4375 end_new = cc.right.end;
4377 /* Get the same change with context lines for display. */
4378 memset(&cc, 0, sizeof(cc));
4379 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4381 memset(&diff_info, 0, sizeof(diff_info));
4382 diff_info.left_path = relpath;
4383 diff_info.right_path = relpath;
4385 diff_state = diff_output_unidiff_state_alloc();
4386 if (diff_state == NULL)
4387 return got_error_set_errno(ENOMEM,
4388 "diff_output_unidiff_state_alloc");
4390 hunkfile = got_opentemp();
4391 if (hunkfile == NULL) {
4392 err = got_error_from_errno("got_opentemp");
4393 goto done;
4396 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4397 diff_result, &cc);
4398 if (rc != DIFF_RC_OK) {
4399 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4400 goto done;
4403 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4404 err = got_ferror(hunkfile, GOT_ERR_IO);
4405 goto done;
4408 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4409 hunkfile, changeno, nchanges);
4410 if (err)
4411 goto done;
4413 switch (*choice) {
4414 case GOT_PATCH_CHOICE_YES:
4415 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4416 end_old, start_new, end_new, outfile, rejectfile);
4417 break;
4418 case GOT_PATCH_CHOICE_NO:
4419 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4420 end_old, start_new, end_new, rejectfile, outfile);
4421 break;
4422 case GOT_PATCH_CHOICE_QUIT:
4423 break;
4424 default:
4425 err = got_error(GOT_ERR_PATCH_CHOICE);
4426 break;
4428 done:
4429 diff_output_unidiff_state_free(diff_state);
4430 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4431 err = got_error_from_errno("fclose");
4432 return err;
4435 struct revert_file_args {
4436 struct got_worktree *worktree;
4437 struct got_fileindex *fileindex;
4438 got_worktree_checkout_cb progress_cb;
4439 void *progress_arg;
4440 got_worktree_patch_cb patch_cb;
4441 void *patch_arg;
4442 struct got_repository *repo;
4443 int unlink_added_files;
4446 static const struct got_error *
4447 create_patched_content(char **path_outfile, int reverse_patch,
4448 struct got_object_id *blob_id, const char *path2,
4449 int dirfd2, const char *de_name2,
4450 const char *relpath, struct got_repository *repo,
4451 got_worktree_patch_cb patch_cb, void *patch_arg)
4453 const struct got_error *err, *free_err;
4454 struct got_blob_object *blob = NULL;
4455 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4456 int fd = -1, fd2 = -1;
4457 char link_target[PATH_MAX];
4458 ssize_t link_len = 0;
4459 char *path1 = NULL, *id_str = NULL;
4460 struct stat sb2;
4461 struct got_diffreg_result *diffreg_result = NULL;
4462 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4463 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4465 *path_outfile = NULL;
4467 err = got_object_id_str(&id_str, blob_id);
4468 if (err)
4469 return err;
4471 if (dirfd2 != -1) {
4472 fd2 = openat(dirfd2, de_name2,
4473 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4474 if (fd2 == -1) {
4475 if (!got_err_open_nofollow_on_symlink()) {
4476 err = got_error_from_errno2("openat", path2);
4477 goto done;
4479 link_len = readlinkat(dirfd2, de_name2,
4480 link_target, sizeof(link_target));
4481 if (link_len == -1) {
4482 return got_error_from_errno2("readlinkat",
4483 path2);
4485 sb2.st_mode = S_IFLNK;
4486 sb2.st_size = link_len;
4488 } else {
4489 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4490 if (fd2 == -1) {
4491 if (!got_err_open_nofollow_on_symlink()) {
4492 err = got_error_from_errno2("open", path2);
4493 goto done;
4495 link_len = readlink(path2, link_target,
4496 sizeof(link_target));
4497 if (link_len == -1)
4498 return got_error_from_errno2("readlink", path2);
4499 sb2.st_mode = S_IFLNK;
4500 sb2.st_size = link_len;
4503 if (fd2 != -1) {
4504 if (fstat(fd2, &sb2) == -1) {
4505 err = got_error_from_errno2("fstat", path2);
4506 goto done;
4509 f2 = fdopen(fd2, "r");
4510 if (f2 == NULL) {
4511 err = got_error_from_errno2("fdopen", path2);
4512 goto done;
4514 fd2 = -1;
4515 } else {
4516 size_t n;
4517 f2 = got_opentemp();
4518 if (f2 == NULL) {
4519 err = got_error_from_errno2("got_opentemp", path2);
4520 goto done;
4522 n = fwrite(link_target, 1, link_len, f2);
4523 if (n != link_len) {
4524 err = got_ferror(f2, GOT_ERR_IO);
4525 goto done;
4527 if (fflush(f2) == EOF) {
4528 err = got_error_from_errno("fflush");
4529 goto done;
4531 rewind(f2);
4534 fd = got_opentempfd();
4535 if (fd == -1) {
4536 err = got_error_from_errno("got_opentempfd");
4537 goto done;
4540 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4541 if (err)
4542 goto done;
4544 err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4545 if (err)
4546 goto done;
4548 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4549 if (err)
4550 goto done;
4552 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4553 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4554 if (err)
4555 goto done;
4557 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4558 "");
4559 if (err)
4560 goto done;
4562 if (fseek(f1, 0L, SEEK_SET) == -1)
4563 return got_ferror(f1, GOT_ERR_IO);
4564 if (fseek(f2, 0L, SEEK_SET) == -1)
4565 return got_ferror(f2, GOT_ERR_IO);
4567 /* Count the number of actual changes in the diff result. */
4568 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4569 struct diff_chunk_context cc = {};
4570 diff_chunk_context_load_change(&cc, &nchunks_used,
4571 diffreg_result->result, n, 0);
4572 nchanges++;
4574 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4575 int choice;
4576 err = apply_or_reject_change(&choice, &nchunks_used,
4577 diffreg_result->result, n, relpath, f1, f2,
4578 &line_cur1, &line_cur2,
4579 reverse_patch ? NULL : outfile,
4580 reverse_patch ? outfile : NULL,
4581 ++i, nchanges, patch_cb, patch_arg);
4582 if (err)
4583 goto done;
4584 if (choice == GOT_PATCH_CHOICE_YES)
4585 have_content = 1;
4586 else if (choice == GOT_PATCH_CHOICE_QUIT)
4587 break;
4589 if (have_content) {
4590 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4591 reverse_patch ? NULL : outfile,
4592 reverse_patch ? outfile : NULL);
4593 if (err)
4594 goto done;
4596 if (!S_ISLNK(sb2.st_mode)) {
4597 mode_t mode;
4599 mode = apply_umask(sb2.st_mode);
4600 if (fchmod(fileno(outfile), mode) == -1) {
4601 err = got_error_from_errno2("fchmod", path2);
4602 goto done;
4606 done:
4607 free(id_str);
4608 if (fd != -1 && close(fd) == -1 && err == NULL)
4609 err = got_error_from_errno("close");
4610 if (blob)
4611 got_object_blob_close(blob);
4612 free_err = got_diffreg_result_free(diffreg_result);
4613 if (err == NULL)
4614 err = free_err;
4615 if (f1 && fclose(f1) == EOF && err == NULL)
4616 err = got_error_from_errno2("fclose", path1);
4617 if (f2 && fclose(f2) == EOF && err == NULL)
4618 err = got_error_from_errno2("fclose", path2);
4619 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4620 err = got_error_from_errno2("close", path2);
4621 if (outfile && fclose(outfile) == EOF && err == NULL)
4622 err = got_error_from_errno2("fclose", *path_outfile);
4623 if (path1 && unlink(path1) == -1 && err == NULL)
4624 err = got_error_from_errno2("unlink", path1);
4625 if (err || !have_content) {
4626 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4627 err = got_error_from_errno2("unlink", *path_outfile);
4628 free(*path_outfile);
4629 *path_outfile = NULL;
4631 free(path1);
4632 return err;
4635 static const struct got_error *
4636 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4637 const char *relpath, struct got_object_id *blob_id,
4638 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4639 int dirfd, const char *de_name)
4641 struct revert_file_args *a = arg;
4642 const struct got_error *err = NULL;
4643 char *parent_path = NULL;
4644 struct got_fileindex_entry *ie;
4645 struct got_commit_object *base_commit = NULL;
4646 struct got_tree_object *tree = NULL;
4647 struct got_object_id *tree_id = NULL;
4648 const struct got_tree_entry *te = NULL;
4649 char *tree_path = NULL, *te_name;
4650 char *ondisk_path = NULL, *path_content = NULL;
4651 struct got_blob_object *blob = NULL;
4652 int fd = -1;
4654 /* Reverting a staged deletion is a no-op. */
4655 if (status == GOT_STATUS_DELETE &&
4656 staged_status != GOT_STATUS_NO_CHANGE)
4657 return NULL;
4659 if (status == GOT_STATUS_UNVERSIONED)
4660 return (*a->progress_cb)(a->progress_arg,
4661 GOT_STATUS_UNVERSIONED, relpath);
4663 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4664 if (ie == NULL)
4665 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4667 /* Construct in-repository path of tree which contains this blob. */
4668 err = got_path_dirname(&parent_path, ie->path);
4669 if (err) {
4670 if (err->code != GOT_ERR_BAD_PATH)
4671 goto done;
4672 parent_path = strdup("/");
4673 if (parent_path == NULL) {
4674 err = got_error_from_errno("strdup");
4675 goto done;
4678 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4679 tree_path = strdup(parent_path);
4680 if (tree_path == NULL) {
4681 err = got_error_from_errno("strdup");
4682 goto done;
4684 } else {
4685 if (got_path_is_root_dir(parent_path)) {
4686 tree_path = strdup(a->worktree->path_prefix);
4687 if (tree_path == NULL) {
4688 err = got_error_from_errno("strdup");
4689 goto done;
4691 } else {
4692 if (asprintf(&tree_path, "%s/%s",
4693 a->worktree->path_prefix, parent_path) == -1) {
4694 err = got_error_from_errno("asprintf");
4695 goto done;
4700 err = got_object_open_as_commit(&base_commit, a->repo,
4701 a->worktree->base_commit_id);
4702 if (err)
4703 goto done;
4705 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4706 if (err) {
4707 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4708 (status == GOT_STATUS_ADD ||
4709 staged_status == GOT_STATUS_ADD)))
4710 goto done;
4711 } else {
4712 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4713 if (err)
4714 goto done;
4716 err = got_path_basename(&te_name, ie->path);
4717 if (err)
4718 goto done;
4720 te = got_object_tree_find_entry(tree, te_name);
4721 free(te_name);
4722 if (te == NULL && status != GOT_STATUS_ADD &&
4723 staged_status != GOT_STATUS_ADD) {
4724 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4725 goto done;
4729 switch (status) {
4730 case GOT_STATUS_ADD:
4731 if (a->patch_cb) {
4732 int choice = GOT_PATCH_CHOICE_NONE;
4733 err = (*a->patch_cb)(&choice, a->patch_arg,
4734 status, ie->path, NULL, 1, 1);
4735 if (err)
4736 goto done;
4737 if (choice != GOT_PATCH_CHOICE_YES)
4738 break;
4740 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4741 ie->path);
4742 if (err)
4743 goto done;
4744 got_fileindex_entry_remove(a->fileindex, ie);
4745 if (a->unlink_added_files) {
4746 if (asprintf(&ondisk_path, "%s/%s",
4747 got_worktree_get_root_path(a->worktree),
4748 relpath) == -1) {
4749 err = got_error_from_errno("asprintf");
4750 goto done;
4752 if (unlink(ondisk_path) == -1) {
4753 err = got_error_from_errno2("unlink",
4754 ondisk_path);
4755 break;
4758 break;
4759 case GOT_STATUS_DELETE:
4760 if (a->patch_cb) {
4761 int choice = GOT_PATCH_CHOICE_NONE;
4762 err = (*a->patch_cb)(&choice, a->patch_arg,
4763 status, ie->path, NULL, 1, 1);
4764 if (err)
4765 goto done;
4766 if (choice != GOT_PATCH_CHOICE_YES)
4767 break;
4769 /* fall through */
4770 case GOT_STATUS_MODIFY:
4771 case GOT_STATUS_MODE_CHANGE:
4772 case GOT_STATUS_CONFLICT:
4773 case GOT_STATUS_MISSING: {
4774 struct got_object_id id;
4775 if (staged_status == GOT_STATUS_ADD ||
4776 staged_status == GOT_STATUS_MODIFY) {
4777 memcpy(id.sha1, ie->staged_blob_sha1,
4778 SHA1_DIGEST_LENGTH);
4779 } else
4780 memcpy(id.sha1, ie->blob_sha1,
4781 SHA1_DIGEST_LENGTH);
4782 fd = got_opentempfd();
4783 if (fd == -1) {
4784 err = got_error_from_errno("got_opentempfd");
4785 goto done;
4788 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4789 if (err)
4790 goto done;
4792 if (asprintf(&ondisk_path, "%s/%s",
4793 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4794 err = got_error_from_errno("asprintf");
4795 goto done;
4798 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4799 status == GOT_STATUS_CONFLICT)) {
4800 int is_bad_symlink = 0;
4801 err = create_patched_content(&path_content, 1, &id,
4802 ondisk_path, dirfd, de_name, ie->path, a->repo,
4803 a->patch_cb, a->patch_arg);
4804 if (err || path_content == NULL)
4805 break;
4806 if (te && S_ISLNK(te->mode)) {
4807 if (unlink(path_content) == -1) {
4808 err = got_error_from_errno2("unlink",
4809 path_content);
4810 break;
4812 err = install_symlink(&is_bad_symlink,
4813 a->worktree, ondisk_path, ie->path,
4814 blob, 0, 1, 0, 0, a->repo,
4815 a->progress_cb, a->progress_arg);
4816 } else {
4817 if (rename(path_content, ondisk_path) == -1) {
4818 err = got_error_from_errno3("rename",
4819 path_content, ondisk_path);
4820 goto done;
4823 } else {
4824 int is_bad_symlink = 0;
4825 if (te && S_ISLNK(te->mode)) {
4826 err = install_symlink(&is_bad_symlink,
4827 a->worktree, ondisk_path, ie->path,
4828 blob, 0, 1, 0, 0, a->repo,
4829 a->progress_cb, a->progress_arg);
4830 } else {
4831 err = install_blob(a->worktree, ondisk_path,
4832 ie->path,
4833 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4834 got_fileindex_perms_to_st(ie), blob,
4835 0, 1, 0, 0, a->repo,
4836 a->progress_cb, a->progress_arg);
4838 if (err)
4839 goto done;
4840 if (status == GOT_STATUS_DELETE ||
4841 status == GOT_STATUS_MODE_CHANGE) {
4842 err = got_fileindex_entry_update(ie,
4843 a->worktree->root_fd, relpath,
4844 blob->id.sha1,
4845 a->worktree->base_commit_id->sha1, 1);
4846 if (err)
4847 goto done;
4849 if (is_bad_symlink) {
4850 got_fileindex_entry_filetype_set(ie,
4851 GOT_FILEIDX_MODE_BAD_SYMLINK);
4854 break;
4856 default:
4857 break;
4859 done:
4860 free(ondisk_path);
4861 free(path_content);
4862 free(parent_path);
4863 free(tree_path);
4864 if (fd != -1 && close(fd) == -1 && err == NULL)
4865 err = got_error_from_errno("close");
4866 if (blob)
4867 got_object_blob_close(blob);
4868 if (tree)
4869 got_object_tree_close(tree);
4870 free(tree_id);
4871 if (base_commit)
4872 got_object_commit_close(base_commit);
4873 return err;
4876 const struct got_error *
4877 got_worktree_revert(struct got_worktree *worktree,
4878 struct got_pathlist_head *paths,
4879 got_worktree_checkout_cb progress_cb, void *progress_arg,
4880 got_worktree_patch_cb patch_cb, void *patch_arg,
4881 struct got_repository *repo)
4883 struct got_fileindex *fileindex = NULL;
4884 char *fileindex_path = NULL;
4885 const struct got_error *err = NULL, *unlockerr = NULL;
4886 const struct got_error *sync_err = NULL;
4887 struct got_pathlist_entry *pe;
4888 struct revert_file_args rfa;
4890 err = lock_worktree(worktree, LOCK_EX);
4891 if (err)
4892 return err;
4894 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4895 if (err)
4896 goto done;
4898 rfa.worktree = worktree;
4899 rfa.fileindex = fileindex;
4900 rfa.progress_cb = progress_cb;
4901 rfa.progress_arg = progress_arg;
4902 rfa.patch_cb = patch_cb;
4903 rfa.patch_arg = patch_arg;
4904 rfa.repo = repo;
4905 rfa.unlink_added_files = 0;
4906 TAILQ_FOREACH(pe, paths, entry) {
4907 err = worktree_status(worktree, pe->path, fileindex, repo,
4908 revert_file, &rfa, NULL, NULL, 1, 0);
4909 if (err)
4910 break;
4912 sync_err = sync_fileindex(fileindex, fileindex_path);
4913 if (sync_err && err == NULL)
4914 err = sync_err;
4915 done:
4916 free(fileindex_path);
4917 if (fileindex)
4918 got_fileindex_free(fileindex);
4919 unlockerr = lock_worktree(worktree, LOCK_SH);
4920 if (unlockerr && err == NULL)
4921 err = unlockerr;
4922 return err;
4925 static void
4926 free_commitable(struct got_commitable *ct)
4928 free(ct->path);
4929 free(ct->in_repo_path);
4930 free(ct->ondisk_path);
4931 free(ct->blob_id);
4932 free(ct->base_blob_id);
4933 free(ct->staged_blob_id);
4934 free(ct->base_commit_id);
4935 free(ct);
4938 struct collect_commitables_arg {
4939 struct got_pathlist_head *commitable_paths;
4940 struct got_repository *repo;
4941 struct got_worktree *worktree;
4942 struct got_fileindex *fileindex;
4943 int have_staged_files;
4944 int allow_bad_symlinks;
4945 int diff_header_shown;
4946 FILE *diff_outfile;
4947 FILE *f1;
4948 FILE *f2;
4952 * Create a file which contains the target path of a symlink so we can feed
4953 * it as content to the diff engine.
4955 static const struct got_error *
4956 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4957 const char *abspath)
4959 const struct got_error *err = NULL;
4960 char target_path[PATH_MAX];
4961 ssize_t target_len, outlen;
4963 *fd = -1;
4965 if (dirfd != -1) {
4966 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4967 if (target_len == -1)
4968 return got_error_from_errno2("readlinkat", abspath);
4969 } else {
4970 target_len = readlink(abspath, target_path, PATH_MAX);
4971 if (target_len == -1)
4972 return got_error_from_errno2("readlink", abspath);
4975 *fd = got_opentempfd();
4976 if (*fd == -1)
4977 return got_error_from_errno("got_opentempfd");
4979 outlen = write(*fd, target_path, target_len);
4980 if (outlen == -1) {
4981 err = got_error_from_errno("got_opentempfd");
4982 goto done;
4985 if (lseek(*fd, 0, SEEK_SET) == -1) {
4986 err = got_error_from_errno2("lseek", abspath);
4987 goto done;
4989 done:
4990 if (err) {
4991 close(*fd);
4992 *fd = -1;
4994 return err;
4997 static const struct got_error *
4998 append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
4999 FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5000 int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5002 const struct got_error *err = NULL;
5003 struct got_blob_object *blob1 = NULL;
5004 int fd = -1, fd1 = -1, fd2 = -1;
5005 FILE *ondisk_file = NULL;
5006 char *label1 = NULL;
5007 struct stat sb;
5008 off_t size1 = 0;
5009 int f2_exists = 0;
5010 char *id_str = NULL;
5012 memset(&sb, 0, sizeof(sb));
5014 if (diff_staged) {
5015 if (ct->staged_status != GOT_STATUS_MODIFY &&
5016 ct->staged_status != GOT_STATUS_ADD &&
5017 ct->staged_status != GOT_STATUS_DELETE)
5018 return NULL;
5019 } else {
5020 if (ct->status != GOT_STATUS_MODIFY &&
5021 ct->status != GOT_STATUS_ADD &&
5022 ct->status != GOT_STATUS_DELETE)
5023 return NULL;
5026 err = got_opentemp_truncate(f1);
5027 if (err)
5028 return got_error_from_errno("got_opentemp_truncate");
5029 err = got_opentemp_truncate(f2);
5030 if (err)
5031 return got_error_from_errno("got_opentemp_truncate");
5033 if (!*diff_header_shown) {
5034 err = got_object_id_str(&id_str, worktree->base_commit_id);
5035 if (err)
5036 return err;
5037 fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5038 got_worktree_get_root_path(worktree));
5039 fprintf(diff_outfile, "commit - %s\n", id_str);
5040 fprintf(diff_outfile, "path + %s%s\n",
5041 got_worktree_get_root_path(worktree),
5042 diff_staged ? " (staged changes)" : "");
5043 *diff_header_shown = 1;
5046 if (diff_staged) {
5047 const char *label1 = NULL, *label2 = NULL;
5048 switch (ct->staged_status) {
5049 case GOT_STATUS_MODIFY:
5050 label1 = ct->path;
5051 label2 = ct->path;
5052 break;
5053 case GOT_STATUS_ADD:
5054 label2 = ct->path;
5055 break;
5056 case GOT_STATUS_DELETE:
5057 label1 = ct->path;
5058 break;
5059 default:
5060 return got_error(GOT_ERR_FILE_STATUS);
5062 fd1 = got_opentempfd();
5063 if (fd1 == -1) {
5064 err = got_error_from_errno("got_opentempfd");
5065 goto done;
5067 fd2 = got_opentempfd();
5068 if (fd2 == -1) {
5069 err = got_error_from_errno("got_opentempfd");
5070 goto done;
5072 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5073 fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5074 label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5075 NULL, repo, diff_outfile);
5076 goto done;
5079 fd1 = got_opentempfd();
5080 if (fd1 == -1) {
5081 err = got_error_from_errno("got_opentempfd");
5082 goto done;
5085 if (ct->status != GOT_STATUS_ADD) {
5086 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5087 8192, fd1);
5088 if (err)
5089 goto done;
5092 if (ct->status != GOT_STATUS_DELETE) {
5093 if (dirfd != -1) {
5094 fd = openat(dirfd, de_name,
5095 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5096 if (fd == -1) {
5097 if (!got_err_open_nofollow_on_symlink()) {
5098 err = got_error_from_errno2("openat",
5099 ct->ondisk_path);
5100 goto done;
5102 err = get_symlink_target_file(&fd, dirfd,
5103 de_name, ct->ondisk_path);
5104 if (err)
5105 goto done;
5107 } else {
5108 fd = open(ct->ondisk_path,
5109 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5110 if (fd == -1) {
5111 if (!got_err_open_nofollow_on_symlink()) {
5112 err = got_error_from_errno2("open",
5113 ct->ondisk_path);
5114 goto done;
5116 err = get_symlink_target_file(&fd, dirfd,
5117 de_name, ct->ondisk_path);
5118 if (err)
5119 goto done;
5122 if (fstatat(fd, ct->ondisk_path, &sb,
5123 AT_SYMLINK_NOFOLLOW) == -1) {
5124 err = got_error_from_errno2("fstatat", ct->ondisk_path);
5125 goto done;
5127 ondisk_file = fdopen(fd, "r");
5128 if (ondisk_file == NULL) {
5129 err = got_error_from_errno2("fdopen", ct->ondisk_path);
5130 goto done;
5132 fd = -1;
5133 f2_exists = 1;
5136 if (blob1) {
5137 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5138 f1, blob1);
5139 if (err)
5140 goto done;
5143 err = got_diff_blob_file(blob1, f1, size1, label1,
5144 ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5145 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5146 done:
5147 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5148 err = got_error_from_errno("close");
5149 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5150 err = got_error_from_errno("close");
5151 if (blob1)
5152 got_object_blob_close(blob1);
5153 if (fd != -1 && close(fd) == -1 && err == NULL)
5154 err = got_error_from_errno("close");
5155 if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5156 err = got_error_from_errno("fclose");
5157 return err;
5160 static const struct got_error *
5161 collect_commitables(void *arg, unsigned char status,
5162 unsigned char staged_status, const char *relpath,
5163 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5164 struct got_object_id *commit_id, int dirfd, const char *de_name)
5166 struct collect_commitables_arg *a = arg;
5167 const struct got_error *err = NULL;
5168 struct got_commitable *ct = NULL;
5169 struct got_pathlist_entry *new = NULL;
5170 char *parent_path = NULL, *path = NULL;
5171 struct stat sb;
5173 if (a->have_staged_files) {
5174 if (staged_status != GOT_STATUS_MODIFY &&
5175 staged_status != GOT_STATUS_ADD &&
5176 staged_status != GOT_STATUS_DELETE)
5177 return NULL;
5178 } else {
5179 if (status == GOT_STATUS_CONFLICT)
5180 return got_error(GOT_ERR_COMMIT_CONFLICT);
5182 if (status != GOT_STATUS_MODIFY &&
5183 status != GOT_STATUS_MODE_CHANGE &&
5184 status != GOT_STATUS_ADD &&
5185 status != GOT_STATUS_DELETE)
5186 return NULL;
5189 if (asprintf(&path, "/%s", relpath) == -1) {
5190 err = got_error_from_errno("asprintf");
5191 goto done;
5193 if (strcmp(path, "/") == 0) {
5194 parent_path = strdup("");
5195 if (parent_path == NULL)
5196 return got_error_from_errno("strdup");
5197 } else {
5198 err = got_path_dirname(&parent_path, path);
5199 if (err)
5200 return err;
5203 ct = calloc(1, sizeof(*ct));
5204 if (ct == NULL) {
5205 err = got_error_from_errno("calloc");
5206 goto done;
5209 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5210 relpath) == -1) {
5211 err = got_error_from_errno("asprintf");
5212 goto done;
5215 if (staged_status == GOT_STATUS_ADD ||
5216 staged_status == GOT_STATUS_MODIFY) {
5217 struct got_fileindex_entry *ie;
5218 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5219 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5220 case GOT_FILEIDX_MODE_REGULAR_FILE:
5221 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5222 ct->mode = S_IFREG;
5223 break;
5224 case GOT_FILEIDX_MODE_SYMLINK:
5225 ct->mode = S_IFLNK;
5226 break;
5227 default:
5228 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5229 goto done;
5231 ct->mode |= got_fileindex_entry_perms_get(ie);
5232 } else if (status != GOT_STATUS_DELETE &&
5233 staged_status != GOT_STATUS_DELETE) {
5234 if (dirfd != -1) {
5235 if (fstatat(dirfd, de_name, &sb,
5236 AT_SYMLINK_NOFOLLOW) == -1) {
5237 err = got_error_from_errno2("fstatat",
5238 ct->ondisk_path);
5239 goto done;
5241 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5242 err = got_error_from_errno2("lstat", ct->ondisk_path);
5243 goto done;
5245 ct->mode = sb.st_mode;
5248 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5249 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5250 relpath) == -1) {
5251 err = got_error_from_errno("asprintf");
5252 goto done;
5255 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5256 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5257 int is_bad_symlink;
5258 char target_path[PATH_MAX];
5259 ssize_t target_len;
5260 target_len = readlink(ct->ondisk_path, target_path,
5261 sizeof(target_path));
5262 if (target_len == -1) {
5263 err = got_error_from_errno2("readlink",
5264 ct->ondisk_path);
5265 goto done;
5267 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5268 target_len, ct->ondisk_path, a->worktree->root_path);
5269 if (err)
5270 goto done;
5271 if (is_bad_symlink) {
5272 err = got_error_path(ct->ondisk_path,
5273 GOT_ERR_BAD_SYMLINK);
5274 goto done;
5279 ct->status = status;
5280 ct->staged_status = staged_status;
5281 ct->blob_id = NULL; /* will be filled in when blob gets created */
5282 if (ct->status != GOT_STATUS_ADD &&
5283 ct->staged_status != GOT_STATUS_ADD) {
5284 ct->base_blob_id = got_object_id_dup(blob_id);
5285 if (ct->base_blob_id == NULL) {
5286 err = got_error_from_errno("got_object_id_dup");
5287 goto done;
5289 ct->base_commit_id = got_object_id_dup(commit_id);
5290 if (ct->base_commit_id == NULL) {
5291 err = got_error_from_errno("got_object_id_dup");
5292 goto done;
5295 if (ct->staged_status == GOT_STATUS_ADD ||
5296 ct->staged_status == GOT_STATUS_MODIFY) {
5297 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5298 if (ct->staged_blob_id == NULL) {
5299 err = got_error_from_errno("got_object_id_dup");
5300 goto done;
5303 ct->path = strdup(path);
5304 if (ct->path == NULL) {
5305 err = got_error_from_errno("strdup");
5306 goto done;
5308 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5309 if (err)
5310 goto done;
5312 if (a->diff_outfile && ct && new != NULL) {
5313 err = append_ct_diff(ct, &a->diff_header_shown,
5314 a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5315 a->have_staged_files, a->repo, a->worktree);
5316 if (err)
5317 goto done;
5319 done:
5320 if (ct && (err || new == NULL))
5321 free_commitable(ct);
5322 free(parent_path);
5323 free(path);
5324 return err;
5327 static const struct got_error *write_tree(struct got_object_id **, int *,
5328 struct got_tree_object *, const char *, struct got_pathlist_head *,
5329 got_worktree_status_cb status_cb, void *status_arg,
5330 struct got_repository *);
5332 static const struct got_error *
5333 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5334 struct got_tree_entry *te, const char *parent_path,
5335 struct got_pathlist_head *commitable_paths,
5336 got_worktree_status_cb status_cb, void *status_arg,
5337 struct got_repository *repo)
5339 const struct got_error *err = NULL;
5340 struct got_tree_object *subtree;
5341 char *subpath;
5343 if (asprintf(&subpath, "%s%s%s", parent_path,
5344 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5345 return got_error_from_errno("asprintf");
5347 err = got_object_open_as_tree(&subtree, repo, &te->id);
5348 if (err)
5349 return err;
5351 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5352 commitable_paths, status_cb, status_arg, repo);
5353 got_object_tree_close(subtree);
5354 free(subpath);
5355 return err;
5358 static const struct got_error *
5359 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5361 const struct got_error *err = NULL;
5362 char *ct_parent_path = NULL;
5364 *match = 0;
5366 if (strchr(ct->in_repo_path, '/') == NULL) {
5367 *match = got_path_is_root_dir(path);
5368 return NULL;
5371 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5372 if (err)
5373 return err;
5374 *match = (strcmp(path, ct_parent_path) == 0);
5375 free(ct_parent_path);
5376 return err;
5379 static mode_t
5380 get_ct_file_mode(struct got_commitable *ct)
5382 if (S_ISLNK(ct->mode))
5383 return S_IFLNK;
5385 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5388 static const struct got_error *
5389 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5390 struct got_tree_entry *te, struct got_commitable *ct)
5392 const struct got_error *err = NULL;
5394 *new_te = NULL;
5396 err = got_object_tree_entry_dup(new_te, te);
5397 if (err)
5398 goto done;
5400 (*new_te)->mode = get_ct_file_mode(ct);
5402 if (ct->staged_status == GOT_STATUS_MODIFY)
5403 memcpy(&(*new_te)->id, ct->staged_blob_id,
5404 sizeof((*new_te)->id));
5405 else
5406 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5407 done:
5408 if (err && *new_te) {
5409 free(*new_te);
5410 *new_te = NULL;
5412 return err;
5415 static const struct got_error *
5416 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5417 struct got_commitable *ct)
5419 const struct got_error *err = NULL;
5420 char *ct_name = NULL;
5422 *new_te = NULL;
5424 *new_te = calloc(1, sizeof(**new_te));
5425 if (*new_te == NULL)
5426 return got_error_from_errno("calloc");
5428 err = got_path_basename(&ct_name, ct->path);
5429 if (err)
5430 goto done;
5431 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5432 sizeof((*new_te)->name)) {
5433 err = got_error(GOT_ERR_NO_SPACE);
5434 goto done;
5437 (*new_te)->mode = get_ct_file_mode(ct);
5439 if (ct->staged_status == GOT_STATUS_ADD)
5440 memcpy(&(*new_te)->id, ct->staged_blob_id,
5441 sizeof((*new_te)->id));
5442 else
5443 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5444 done:
5445 free(ct_name);
5446 if (err && *new_te) {
5447 free(*new_te);
5448 *new_te = NULL;
5450 return err;
5453 static const struct got_error *
5454 insert_tree_entry(struct got_tree_entry *new_te,
5455 struct got_pathlist_head *paths)
5457 const struct got_error *err = NULL;
5458 struct got_pathlist_entry *new_pe;
5460 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5461 if (err)
5462 return err;
5463 if (new_pe == NULL)
5464 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5465 return NULL;
5468 static const struct got_error *
5469 report_ct_status(struct got_commitable *ct,
5470 got_worktree_status_cb status_cb, void *status_arg)
5472 const char *ct_path = ct->path;
5473 unsigned char status;
5475 if (status_cb == NULL) /* no commit progress output desired */
5476 return NULL;
5478 while (ct_path[0] == '/')
5479 ct_path++;
5481 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5482 status = ct->staged_status;
5483 else
5484 status = ct->status;
5486 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5487 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5490 static const struct got_error *
5491 match_modified_subtree(int *modified, struct got_tree_entry *te,
5492 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5494 const struct got_error *err = NULL;
5495 struct got_pathlist_entry *pe;
5496 char *te_path;
5498 *modified = 0;
5500 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5501 got_path_is_root_dir(base_tree_path) ? "" : "/",
5502 te->name) == -1)
5503 return got_error_from_errno("asprintf");
5505 TAILQ_FOREACH(pe, commitable_paths, entry) {
5506 struct got_commitable *ct = pe->data;
5507 *modified = got_path_is_child(ct->in_repo_path, te_path,
5508 strlen(te_path));
5509 if (*modified)
5510 break;
5513 free(te_path);
5514 return err;
5517 static const struct got_error *
5518 match_deleted_or_modified_ct(struct got_commitable **ctp,
5519 struct got_tree_entry *te, const char *base_tree_path,
5520 struct got_pathlist_head *commitable_paths)
5522 const struct got_error *err = NULL;
5523 struct got_pathlist_entry *pe;
5525 *ctp = NULL;
5527 TAILQ_FOREACH(pe, commitable_paths, entry) {
5528 struct got_commitable *ct = pe->data;
5529 char *ct_name = NULL;
5530 int path_matches;
5532 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5533 if (ct->status != GOT_STATUS_MODIFY &&
5534 ct->status != GOT_STATUS_MODE_CHANGE &&
5535 ct->status != GOT_STATUS_DELETE)
5536 continue;
5537 } else {
5538 if (ct->staged_status != GOT_STATUS_MODIFY &&
5539 ct->staged_status != GOT_STATUS_DELETE)
5540 continue;
5543 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5544 continue;
5546 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5547 if (err)
5548 return err;
5549 if (!path_matches)
5550 continue;
5552 err = got_path_basename(&ct_name, pe->path);
5553 if (err)
5554 return err;
5556 if (strcmp(te->name, ct_name) != 0) {
5557 free(ct_name);
5558 continue;
5560 free(ct_name);
5562 *ctp = ct;
5563 break;
5566 return err;
5569 static const struct got_error *
5570 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5571 const char *child_path, const char *path_base_tree,
5572 struct got_pathlist_head *commitable_paths,
5573 got_worktree_status_cb status_cb, void *status_arg,
5574 struct got_repository *repo)
5576 const struct got_error *err = NULL;
5577 struct got_tree_entry *new_te;
5578 char *subtree_path;
5579 struct got_object_id *id = NULL;
5580 int nentries;
5582 *new_tep = NULL;
5584 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5585 got_path_is_root_dir(path_base_tree) ? "" : "/",
5586 child_path) == -1)
5587 return got_error_from_errno("asprintf");
5589 new_te = calloc(1, sizeof(*new_te));
5590 if (new_te == NULL)
5591 return got_error_from_errno("calloc");
5592 new_te->mode = S_IFDIR;
5594 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5595 sizeof(new_te->name)) {
5596 err = got_error(GOT_ERR_NO_SPACE);
5597 goto done;
5599 err = write_tree(&id, &nentries, NULL, subtree_path,
5600 commitable_paths, status_cb, status_arg, repo);
5601 if (err) {
5602 free(new_te);
5603 goto done;
5605 memcpy(&new_te->id, id, sizeof(new_te->id));
5606 done:
5607 free(id);
5608 free(subtree_path);
5609 if (err == NULL)
5610 *new_tep = new_te;
5611 return err;
5614 static const struct got_error *
5615 write_tree(struct got_object_id **new_tree_id, int *nentries,
5616 struct got_tree_object *base_tree, const char *path_base_tree,
5617 struct got_pathlist_head *commitable_paths,
5618 got_worktree_status_cb status_cb, void *status_arg,
5619 struct got_repository *repo)
5621 const struct got_error *err = NULL;
5622 struct got_pathlist_head paths;
5623 struct got_tree_entry *te, *new_te = NULL;
5624 struct got_pathlist_entry *pe;
5626 TAILQ_INIT(&paths);
5627 *nentries = 0;
5629 /* Insert, and recurse into, newly added entries first. */
5630 TAILQ_FOREACH(pe, commitable_paths, entry) {
5631 struct got_commitable *ct = pe->data;
5632 char *child_path = NULL, *slash;
5634 if ((ct->status != GOT_STATUS_ADD &&
5635 ct->staged_status != GOT_STATUS_ADD) ||
5636 (ct->flags & GOT_COMMITABLE_ADDED))
5637 continue;
5639 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5640 strlen(path_base_tree)))
5641 continue;
5643 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5644 ct->in_repo_path);
5645 if (err)
5646 goto done;
5648 slash = strchr(child_path, '/');
5649 if (slash == NULL) {
5650 err = alloc_added_blob_tree_entry(&new_te, ct);
5651 if (err)
5652 goto done;
5653 err = report_ct_status(ct, status_cb, status_arg);
5654 if (err)
5655 goto done;
5656 ct->flags |= GOT_COMMITABLE_ADDED;
5657 err = insert_tree_entry(new_te, &paths);
5658 if (err)
5659 goto done;
5660 (*nentries)++;
5661 } else {
5662 *slash = '\0'; /* trim trailing path components */
5663 if (base_tree == NULL ||
5664 got_object_tree_find_entry(base_tree, child_path)
5665 == NULL) {
5666 err = make_subtree_for_added_blob(&new_te,
5667 child_path, path_base_tree,
5668 commitable_paths, status_cb, status_arg,
5669 repo);
5670 if (err)
5671 goto done;
5672 err = insert_tree_entry(new_te, &paths);
5673 if (err)
5674 goto done;
5675 (*nentries)++;
5680 if (base_tree) {
5681 int i, nbase_entries;
5682 /* Handle modified and deleted entries. */
5683 nbase_entries = got_object_tree_get_nentries(base_tree);
5684 for (i = 0; i < nbase_entries; i++) {
5685 struct got_commitable *ct = NULL;
5687 te = got_object_tree_get_entry(base_tree, i);
5688 if (got_object_tree_entry_is_submodule(te)) {
5689 /* Entry is a submodule; just copy it. */
5690 err = got_object_tree_entry_dup(&new_te, te);
5691 if (err)
5692 goto done;
5693 err = insert_tree_entry(new_te, &paths);
5694 if (err)
5695 goto done;
5696 (*nentries)++;
5697 continue;
5700 if (S_ISDIR(te->mode)) {
5701 int modified;
5702 err = got_object_tree_entry_dup(&new_te, te);
5703 if (err)
5704 goto done;
5705 err = match_modified_subtree(&modified, te,
5706 path_base_tree, commitable_paths);
5707 if (err)
5708 goto done;
5709 /* Avoid recursion into unmodified subtrees. */
5710 if (modified) {
5711 struct got_object_id *new_id;
5712 int nsubentries;
5713 err = write_subtree(&new_id,
5714 &nsubentries, te,
5715 path_base_tree, commitable_paths,
5716 status_cb, status_arg, repo);
5717 if (err)
5718 goto done;
5719 if (nsubentries == 0) {
5720 /* All entries were deleted. */
5721 free(new_id);
5722 continue;
5724 memcpy(&new_te->id, new_id,
5725 sizeof(new_te->id));
5726 free(new_id);
5728 err = insert_tree_entry(new_te, &paths);
5729 if (err)
5730 goto done;
5731 (*nentries)++;
5732 continue;
5735 err = match_deleted_or_modified_ct(&ct, te,
5736 path_base_tree, commitable_paths);
5737 if (err)
5738 goto done;
5739 if (ct) {
5740 /* NB: Deleted entries get dropped here. */
5741 if (ct->status == GOT_STATUS_MODIFY ||
5742 ct->status == GOT_STATUS_MODE_CHANGE ||
5743 ct->staged_status == GOT_STATUS_MODIFY) {
5744 err = alloc_modified_blob_tree_entry(
5745 &new_te, te, ct);
5746 if (err)
5747 goto done;
5748 err = insert_tree_entry(new_te, &paths);
5749 if (err)
5750 goto done;
5751 (*nentries)++;
5753 err = report_ct_status(ct, status_cb,
5754 status_arg);
5755 if (err)
5756 goto done;
5757 } else {
5758 /* Entry is unchanged; just copy it. */
5759 err = got_object_tree_entry_dup(&new_te, te);
5760 if (err)
5761 goto done;
5762 err = insert_tree_entry(new_te, &paths);
5763 if (err)
5764 goto done;
5765 (*nentries)++;
5770 /* Write new list of entries; deleted entries have been dropped. */
5771 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5772 done:
5773 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
5774 return err;
5777 static const struct got_error *
5778 update_fileindex_after_commit(struct got_worktree *worktree,
5779 struct got_pathlist_head *commitable_paths,
5780 struct got_object_id *new_base_commit_id,
5781 struct got_fileindex *fileindex, int have_staged_files)
5783 const struct got_error *err = NULL;
5784 struct got_pathlist_entry *pe;
5785 char *relpath = NULL;
5787 TAILQ_FOREACH(pe, commitable_paths, entry) {
5788 struct got_fileindex_entry *ie;
5789 struct got_commitable *ct = pe->data;
5791 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5793 err = got_path_skip_common_ancestor(&relpath,
5794 worktree->root_path, ct->ondisk_path);
5795 if (err)
5796 goto done;
5798 if (ie) {
5799 if (ct->status == GOT_STATUS_DELETE ||
5800 ct->staged_status == GOT_STATUS_DELETE) {
5801 got_fileindex_entry_remove(fileindex, ie);
5802 } else if (ct->staged_status == GOT_STATUS_ADD ||
5803 ct->staged_status == GOT_STATUS_MODIFY) {
5804 got_fileindex_entry_stage_set(ie,
5805 GOT_FILEIDX_STAGE_NONE);
5806 got_fileindex_entry_staged_filetype_set(ie, 0);
5808 err = got_fileindex_entry_update(ie,
5809 worktree->root_fd, relpath,
5810 ct->staged_blob_id->sha1,
5811 new_base_commit_id->sha1,
5812 !have_staged_files);
5813 } else
5814 err = got_fileindex_entry_update(ie,
5815 worktree->root_fd, relpath,
5816 ct->blob_id->sha1,
5817 new_base_commit_id->sha1,
5818 !have_staged_files);
5819 } else {
5820 err = got_fileindex_entry_alloc(&ie, pe->path);
5821 if (err)
5822 goto done;
5823 err = got_fileindex_entry_update(ie,
5824 worktree->root_fd, relpath, ct->blob_id->sha1,
5825 new_base_commit_id->sha1, 1);
5826 if (err) {
5827 got_fileindex_entry_free(ie);
5828 goto done;
5830 err = got_fileindex_entry_add(fileindex, ie);
5831 if (err) {
5832 got_fileindex_entry_free(ie);
5833 goto done;
5836 free(relpath);
5837 relpath = NULL;
5839 done:
5840 free(relpath);
5841 return err;
5845 static const struct got_error *
5846 check_out_of_date(const char *in_repo_path, unsigned char status,
5847 unsigned char staged_status, struct got_object_id *base_blob_id,
5848 struct got_object_id *base_commit_id,
5849 struct got_object_id *head_commit_id, struct got_repository *repo,
5850 int ood_errcode)
5852 const struct got_error *err = NULL;
5853 struct got_commit_object *commit = NULL;
5854 struct got_object_id *id = NULL;
5856 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5857 /* Trivial case: base commit == head commit */
5858 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5859 return NULL;
5861 * Ensure file content which local changes were based
5862 * on matches file content in the branch head.
5864 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5865 if (err)
5866 goto done;
5867 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5868 if (err) {
5869 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5870 err = got_error(ood_errcode);
5871 goto done;
5872 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5873 err = got_error(ood_errcode);
5874 } else {
5875 /* Require that added files don't exist in the branch head. */
5876 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5877 if (err)
5878 goto done;
5879 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5880 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5881 goto done;
5882 err = id ? got_error(ood_errcode) : NULL;
5884 done:
5885 free(id);
5886 if (commit)
5887 got_object_commit_close(commit);
5888 return err;
5891 static const struct got_error *
5892 commit_worktree(struct got_object_id **new_commit_id,
5893 struct got_pathlist_head *commitable_paths,
5894 struct got_object_id *head_commit_id,
5895 struct got_object_id *parent_id2,
5896 struct got_worktree *worktree,
5897 const char *author, const char *committer, char *diff_path,
5898 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5899 got_worktree_status_cb status_cb, void *status_arg,
5900 struct got_repository *repo)
5902 const struct got_error *err = NULL, *unlockerr = NULL;
5903 struct got_pathlist_entry *pe;
5904 const char *head_ref_name = NULL;
5905 struct got_commit_object *head_commit = NULL;
5906 struct got_reference *head_ref2 = NULL;
5907 struct got_object_id *head_commit_id2 = NULL;
5908 struct got_tree_object *head_tree = NULL;
5909 struct got_object_id *new_tree_id = NULL;
5910 int nentries, nparents = 0;
5911 struct got_object_id_queue parent_ids;
5912 struct got_object_qid *pid = NULL;
5913 char *logmsg = NULL;
5914 time_t timestamp;
5916 *new_commit_id = NULL;
5918 STAILQ_INIT(&parent_ids);
5920 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5921 if (err)
5922 goto done;
5924 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5925 if (err)
5926 goto done;
5928 if (commit_msg_cb != NULL) {
5929 err = commit_msg_cb(commitable_paths, diff_path,
5930 &logmsg, commit_arg);
5931 if (err)
5932 goto done;
5935 if (logmsg == NULL || strlen(logmsg) == 0) {
5936 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5937 goto done;
5940 /* Create blobs from added and modified files and record their IDs. */
5941 TAILQ_FOREACH(pe, commitable_paths, entry) {
5942 struct got_commitable *ct = pe->data;
5943 char *ondisk_path;
5945 /* Blobs for staged files already exist. */
5946 if (ct->staged_status == GOT_STATUS_ADD ||
5947 ct->staged_status == GOT_STATUS_MODIFY)
5948 continue;
5950 if (ct->status != GOT_STATUS_ADD &&
5951 ct->status != GOT_STATUS_MODIFY &&
5952 ct->status != GOT_STATUS_MODE_CHANGE)
5953 continue;
5955 if (asprintf(&ondisk_path, "%s/%s",
5956 worktree->root_path, pe->path) == -1) {
5957 err = got_error_from_errno("asprintf");
5958 goto done;
5960 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5961 free(ondisk_path);
5962 if (err)
5963 goto done;
5966 /* Recursively write new tree objects. */
5967 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5968 commitable_paths, status_cb, status_arg, repo);
5969 if (err)
5970 goto done;
5972 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5973 if (err)
5974 goto done;
5975 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5976 nparents++;
5977 if (parent_id2) {
5978 err = got_object_qid_alloc(&pid, parent_id2);
5979 if (err)
5980 goto done;
5981 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5982 nparents++;
5984 timestamp = time(NULL);
5985 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5986 nparents, author, timestamp, committer, timestamp, logmsg, repo);
5987 if (logmsg != NULL)
5988 free(logmsg);
5989 if (err)
5990 goto done;
5992 /* Check if a concurrent commit to our branch has occurred. */
5993 head_ref_name = got_worktree_get_head_ref_name(worktree);
5994 if (head_ref_name == NULL) {
5995 err = got_error_from_errno("got_worktree_get_head_ref_name");
5996 goto done;
5998 /* Lock the reference here to prevent concurrent modification. */
5999 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6000 if (err)
6001 goto done;
6002 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6003 if (err)
6004 goto done;
6005 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6006 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6007 goto done;
6009 /* Update branch head in repository. */
6010 err = got_ref_change_ref(head_ref2, *new_commit_id);
6011 if (err)
6012 goto done;
6013 err = got_ref_write(head_ref2, repo);
6014 if (err)
6015 goto done;
6017 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6018 if (err)
6019 goto done;
6021 err = ref_base_commit(worktree, repo);
6022 if (err)
6023 goto done;
6024 done:
6025 got_object_id_queue_free(&parent_ids);
6026 if (head_tree)
6027 got_object_tree_close(head_tree);
6028 if (head_commit)
6029 got_object_commit_close(head_commit);
6030 free(head_commit_id2);
6031 if (head_ref2) {
6032 unlockerr = got_ref_unlock(head_ref2);
6033 if (unlockerr && err == NULL)
6034 err = unlockerr;
6035 got_ref_close(head_ref2);
6037 return err;
6040 static const struct got_error *
6041 check_path_is_commitable(const char *path,
6042 struct got_pathlist_head *commitable_paths)
6044 struct got_pathlist_entry *cpe = NULL;
6045 size_t path_len = strlen(path);
6047 TAILQ_FOREACH(cpe, commitable_paths, entry) {
6048 struct got_commitable *ct = cpe->data;
6049 const char *ct_path = ct->path;
6051 while (ct_path[0] == '/')
6052 ct_path++;
6054 if (strcmp(path, ct_path) == 0 ||
6055 got_path_is_child(ct_path, path, path_len))
6056 break;
6059 if (cpe == NULL)
6060 return got_error_path(path, GOT_ERR_BAD_PATH);
6062 return NULL;
6065 static const struct got_error *
6066 check_staged_file(void *arg, struct got_fileindex_entry *ie)
6068 int *have_staged_files = arg;
6070 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6071 *have_staged_files = 1;
6072 return got_error(GOT_ERR_CANCELLED);
6075 return NULL;
6078 static const struct got_error *
6079 check_non_staged_files(struct got_fileindex *fileindex,
6080 struct got_pathlist_head *paths)
6082 struct got_pathlist_entry *pe;
6083 struct got_fileindex_entry *ie;
6085 TAILQ_FOREACH(pe, paths, entry) {
6086 if (pe->path[0] == '\0')
6087 continue;
6088 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6089 if (ie == NULL)
6090 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6091 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6092 return got_error_path(pe->path,
6093 GOT_ERR_FILE_NOT_STAGED);
6096 return NULL;
6099 const struct got_error *
6100 got_worktree_commit(struct got_object_id **new_commit_id,
6101 struct got_worktree *worktree, struct got_pathlist_head *paths,
6102 const char *author, const char *committer, int allow_bad_symlinks,
6103 int show_diff, got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6104 got_worktree_status_cb status_cb, void *status_arg,
6105 struct got_repository *repo)
6107 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6108 struct got_fileindex *fileindex = NULL;
6109 char *fileindex_path = NULL;
6110 struct got_pathlist_head commitable_paths;
6111 struct collect_commitables_arg cc_arg;
6112 struct got_pathlist_entry *pe;
6113 struct got_reference *head_ref = NULL;
6114 struct got_object_id *head_commit_id = NULL;
6115 char *diff_path = NULL;
6116 int have_staged_files = 0;
6118 *new_commit_id = NULL;
6120 memset(&cc_arg, 0, sizeof(cc_arg));
6121 TAILQ_INIT(&commitable_paths);
6123 err = lock_worktree(worktree, LOCK_EX);
6124 if (err)
6125 goto done;
6127 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6128 if (err)
6129 goto done;
6131 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6132 if (err)
6133 goto done;
6135 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6136 if (err)
6137 goto done;
6139 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6140 &have_staged_files);
6141 if (err && err->code != GOT_ERR_CANCELLED)
6142 goto done;
6143 if (have_staged_files) {
6144 err = check_non_staged_files(fileindex, paths);
6145 if (err)
6146 goto done;
6149 cc_arg.commitable_paths = &commitable_paths;
6150 cc_arg.worktree = worktree;
6151 cc_arg.fileindex = fileindex;
6152 cc_arg.repo = repo;
6153 cc_arg.have_staged_files = have_staged_files;
6154 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6155 cc_arg.diff_header_shown = 0;
6156 if (show_diff) {
6157 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6158 GOT_TMPDIR_STR "/got", ".diff");
6159 if (err)
6160 goto done;
6161 cc_arg.f1 = got_opentemp();
6162 if (cc_arg.f1 == NULL) {
6163 err = got_error_from_errno("got_opentemp");
6164 goto done;
6166 cc_arg.f2 = got_opentemp();
6167 if (cc_arg.f2 == NULL) {
6168 err = got_error_from_errno("got_opentemp");
6169 goto done;
6173 TAILQ_FOREACH(pe, paths, entry) {
6174 err = worktree_status(worktree, pe->path, fileindex, repo,
6175 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6176 if (err)
6177 goto done;
6180 if (show_diff) {
6181 if (fflush(cc_arg.diff_outfile) == EOF) {
6182 err = got_error_from_errno("fflush");
6183 goto done;
6187 if (TAILQ_EMPTY(&commitable_paths)) {
6188 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6189 goto done;
6192 TAILQ_FOREACH(pe, paths, entry) {
6193 err = check_path_is_commitable(pe->path, &commitable_paths);
6194 if (err)
6195 goto done;
6198 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6199 struct got_commitable *ct = pe->data;
6200 const char *ct_path = ct->in_repo_path;
6202 while (ct_path[0] == '/')
6203 ct_path++;
6204 err = check_out_of_date(ct_path, ct->status,
6205 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6206 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6207 if (err)
6208 goto done;
6212 err = commit_worktree(new_commit_id, &commitable_paths,
6213 head_commit_id, NULL, worktree, author, committer,
6214 (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6215 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6216 if (err)
6217 goto done;
6219 err = update_fileindex_after_commit(worktree, &commitable_paths,
6220 *new_commit_id, fileindex, have_staged_files);
6221 sync_err = sync_fileindex(fileindex, fileindex_path);
6222 if (sync_err && err == NULL)
6223 err = sync_err;
6224 done:
6225 if (fileindex)
6226 got_fileindex_free(fileindex);
6227 free(fileindex_path);
6228 unlockerr = lock_worktree(worktree, LOCK_SH);
6229 if (unlockerr && err == NULL)
6230 err = unlockerr;
6231 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6232 struct got_commitable *ct = pe->data;
6234 free_commitable(ct);
6236 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6237 if (diff_path && unlink(diff_path) == -1 && err == NULL)
6238 err = got_error_from_errno2("unlink", diff_path);
6239 free(diff_path);
6240 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6241 err == NULL)
6242 err = got_error_from_errno("fclose");
6243 return err;
6246 const char *
6247 got_commitable_get_path(struct got_commitable *ct)
6249 return ct->path;
6252 unsigned int
6253 got_commitable_get_status(struct got_commitable *ct)
6255 return ct->status;
6258 struct check_rebase_ok_arg {
6259 struct got_worktree *worktree;
6260 struct got_repository *repo;
6263 static const struct got_error *
6264 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6266 const struct got_error *err = NULL;
6267 struct check_rebase_ok_arg *a = arg;
6268 unsigned char status;
6269 struct stat sb;
6270 char *ondisk_path;
6272 /* Reject rebase of a work tree with mixed base commits. */
6273 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6274 SHA1_DIGEST_LENGTH))
6275 return got_error(GOT_ERR_MIXED_COMMITS);
6277 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6278 == -1)
6279 return got_error_from_errno("asprintf");
6281 /* Reject rebase of a work tree with modified or staged files. */
6282 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6283 free(ondisk_path);
6284 if (err)
6285 return err;
6287 if (status != GOT_STATUS_NO_CHANGE)
6288 return got_error(GOT_ERR_MODIFIED);
6289 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6290 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6292 return NULL;
6295 const struct got_error *
6296 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6297 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6298 struct got_worktree *worktree, struct got_reference *branch,
6299 struct got_repository *repo)
6301 const struct got_error *err = NULL;
6302 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6303 char *branch_ref_name = NULL;
6304 char *fileindex_path = NULL;
6305 struct check_rebase_ok_arg ok_arg;
6306 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6307 struct got_object_id *wt_branch_tip = NULL;
6309 *new_base_branch_ref = NULL;
6310 *tmp_branch = NULL;
6311 *fileindex = NULL;
6313 err = lock_worktree(worktree, LOCK_EX);
6314 if (err)
6315 return err;
6317 err = open_fileindex(fileindex, &fileindex_path, worktree);
6318 if (err)
6319 goto done;
6321 ok_arg.worktree = worktree;
6322 ok_arg.repo = repo;
6323 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6324 &ok_arg);
6325 if (err)
6326 goto done;
6328 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6329 if (err)
6330 goto done;
6332 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6333 if (err)
6334 goto done;
6336 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6337 if (err)
6338 goto done;
6340 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6341 0);
6342 if (err)
6343 goto done;
6345 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6346 if (err)
6347 goto done;
6348 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6349 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6350 goto done;
6353 err = got_ref_alloc_symref(new_base_branch_ref,
6354 new_base_branch_ref_name, wt_branch);
6355 if (err)
6356 goto done;
6357 err = got_ref_write(*new_base_branch_ref, repo);
6358 if (err)
6359 goto done;
6361 /* TODO Lock original branch's ref while rebasing? */
6363 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6364 if (err)
6365 goto done;
6367 err = got_ref_write(branch_ref, repo);
6368 if (err)
6369 goto done;
6371 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6372 worktree->base_commit_id);
6373 if (err)
6374 goto done;
6375 err = got_ref_write(*tmp_branch, repo);
6376 if (err)
6377 goto done;
6379 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6380 if (err)
6381 goto done;
6382 done:
6383 free(fileindex_path);
6384 free(tmp_branch_name);
6385 free(new_base_branch_ref_name);
6386 free(branch_ref_name);
6387 if (branch_ref)
6388 got_ref_close(branch_ref);
6389 if (wt_branch)
6390 got_ref_close(wt_branch);
6391 free(wt_branch_tip);
6392 if (err) {
6393 if (*new_base_branch_ref) {
6394 got_ref_close(*new_base_branch_ref);
6395 *new_base_branch_ref = NULL;
6397 if (*tmp_branch) {
6398 got_ref_close(*tmp_branch);
6399 *tmp_branch = NULL;
6401 if (*fileindex) {
6402 got_fileindex_free(*fileindex);
6403 *fileindex = NULL;
6405 lock_worktree(worktree, LOCK_SH);
6407 return err;
6410 const struct got_error *
6411 got_worktree_rebase_continue(struct got_object_id **commit_id,
6412 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6413 struct got_reference **branch, struct got_fileindex **fileindex,
6414 struct got_worktree *worktree, struct got_repository *repo)
6416 const struct got_error *err;
6417 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6418 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6419 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6420 char *fileindex_path = NULL;
6421 int have_staged_files = 0;
6423 *commit_id = NULL;
6424 *new_base_branch = NULL;
6425 *tmp_branch = NULL;
6426 *branch = NULL;
6427 *fileindex = NULL;
6429 err = lock_worktree(worktree, LOCK_EX);
6430 if (err)
6431 return err;
6433 err = open_fileindex(fileindex, &fileindex_path, worktree);
6434 if (err)
6435 goto done;
6437 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6438 &have_staged_files);
6439 if (err && err->code != GOT_ERR_CANCELLED)
6440 goto done;
6441 if (have_staged_files) {
6442 err = got_error(GOT_ERR_STAGED_PATHS);
6443 goto done;
6446 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6447 if (err)
6448 goto done;
6450 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6451 if (err)
6452 goto done;
6454 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6455 if (err)
6456 goto done;
6458 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6459 if (err)
6460 goto done;
6462 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6463 if (err)
6464 goto done;
6466 err = got_ref_open(branch, repo,
6467 got_ref_get_symref_target(branch_ref), 0);
6468 if (err)
6469 goto done;
6471 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6472 if (err)
6473 goto done;
6475 err = got_ref_resolve(commit_id, repo, commit_ref);
6476 if (err)
6477 goto done;
6479 err = got_ref_open(new_base_branch, repo,
6480 new_base_branch_ref_name, 0);
6481 if (err)
6482 goto done;
6484 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6485 if (err)
6486 goto done;
6487 done:
6488 free(commit_ref_name);
6489 free(branch_ref_name);
6490 free(fileindex_path);
6491 if (commit_ref)
6492 got_ref_close(commit_ref);
6493 if (branch_ref)
6494 got_ref_close(branch_ref);
6495 if (err) {
6496 free(*commit_id);
6497 *commit_id = NULL;
6498 if (*tmp_branch) {
6499 got_ref_close(*tmp_branch);
6500 *tmp_branch = NULL;
6502 if (*new_base_branch) {
6503 got_ref_close(*new_base_branch);
6504 *new_base_branch = NULL;
6506 if (*branch) {
6507 got_ref_close(*branch);
6508 *branch = NULL;
6510 if (*fileindex) {
6511 got_fileindex_free(*fileindex);
6512 *fileindex = NULL;
6514 lock_worktree(worktree, LOCK_SH);
6516 return err;
6519 const struct got_error *
6520 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6522 const struct got_error *err;
6523 char *tmp_branch_name = NULL;
6525 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6526 if (err)
6527 return err;
6529 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6530 free(tmp_branch_name);
6531 return NULL;
6534 static const struct got_error *
6535 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6536 const char *diff_path, char **logmsg, void *arg)
6538 *logmsg = arg;
6539 return NULL;
6542 static const struct got_error *
6543 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6544 const char *path, struct got_object_id *blob_id,
6545 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6546 int dirfd, const char *de_name)
6548 return NULL;
6551 struct collect_merged_paths_arg {
6552 got_worktree_checkout_cb progress_cb;
6553 void *progress_arg;
6554 struct got_pathlist_head *merged_paths;
6557 static const struct got_error *
6558 collect_merged_paths(void *arg, unsigned char status, const char *path)
6560 const struct got_error *err;
6561 struct collect_merged_paths_arg *a = arg;
6562 char *p;
6563 struct got_pathlist_entry *new;
6565 err = (*a->progress_cb)(a->progress_arg, status, path);
6566 if (err)
6567 return err;
6569 if (status != GOT_STATUS_MERGE &&
6570 status != GOT_STATUS_ADD &&
6571 status != GOT_STATUS_DELETE &&
6572 status != GOT_STATUS_CONFLICT)
6573 return NULL;
6575 p = strdup(path);
6576 if (p == NULL)
6577 return got_error_from_errno("strdup");
6579 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6580 if (err || new == NULL)
6581 free(p);
6582 return err;
6585 static const struct got_error *
6586 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6587 int is_rebase, struct got_repository *repo)
6589 const struct got_error *err;
6590 struct got_reference *commit_ref = NULL;
6592 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6593 if (err) {
6594 if (err->code != GOT_ERR_NOT_REF)
6595 goto done;
6596 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6597 if (err)
6598 goto done;
6599 err = got_ref_write(commit_ref, repo);
6600 if (err)
6601 goto done;
6602 } else if (is_rebase) {
6603 struct got_object_id *stored_id;
6604 int cmp;
6606 err = got_ref_resolve(&stored_id, repo, commit_ref);
6607 if (err)
6608 goto done;
6609 cmp = got_object_id_cmp(commit_id, stored_id);
6610 free(stored_id);
6611 if (cmp != 0) {
6612 err = got_error(GOT_ERR_REBASE_COMMITID);
6613 goto done;
6616 done:
6617 if (commit_ref)
6618 got_ref_close(commit_ref);
6619 return err;
6622 static const struct got_error *
6623 rebase_merge_files(struct got_pathlist_head *merged_paths,
6624 const char *commit_ref_name, struct got_worktree *worktree,
6625 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6626 struct got_object_id *commit_id, struct got_repository *repo,
6627 got_worktree_checkout_cb progress_cb, void *progress_arg,
6628 got_cancel_cb cancel_cb, void *cancel_arg)
6630 const struct got_error *err;
6631 struct got_reference *commit_ref = NULL;
6632 struct collect_merged_paths_arg cmp_arg;
6633 char *fileindex_path;
6635 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6637 err = get_fileindex_path(&fileindex_path, worktree);
6638 if (err)
6639 return err;
6641 cmp_arg.progress_cb = progress_cb;
6642 cmp_arg.progress_arg = progress_arg;
6643 cmp_arg.merged_paths = merged_paths;
6644 err = merge_files(worktree, fileindex, fileindex_path,
6645 parent_commit_id, commit_id, repo, collect_merged_paths,
6646 &cmp_arg, cancel_cb, cancel_arg);
6647 if (commit_ref)
6648 got_ref_close(commit_ref);
6649 return err;
6652 const struct got_error *
6653 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6654 struct got_worktree *worktree, struct got_fileindex *fileindex,
6655 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6656 struct got_repository *repo,
6657 got_worktree_checkout_cb progress_cb, void *progress_arg,
6658 got_cancel_cb cancel_cb, void *cancel_arg)
6660 const struct got_error *err;
6661 char *commit_ref_name;
6663 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6664 if (err)
6665 return err;
6667 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6668 if (err)
6669 goto done;
6671 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6672 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6673 progress_arg, cancel_cb, cancel_arg);
6674 done:
6675 free(commit_ref_name);
6676 return err;
6679 const struct got_error *
6680 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6681 struct got_worktree *worktree, struct got_fileindex *fileindex,
6682 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6683 struct got_repository *repo,
6684 got_worktree_checkout_cb progress_cb, void *progress_arg,
6685 got_cancel_cb cancel_cb, void *cancel_arg)
6687 const struct got_error *err;
6688 char *commit_ref_name;
6690 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6691 if (err)
6692 return err;
6694 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6695 if (err)
6696 goto done;
6698 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6699 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6700 progress_arg, cancel_cb, cancel_arg);
6701 done:
6702 free(commit_ref_name);
6703 return err;
6706 static const struct got_error *
6707 rebase_commit(struct got_object_id **new_commit_id,
6708 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6709 struct got_worktree *worktree, struct got_fileindex *fileindex,
6710 struct got_reference *tmp_branch, const char *committer,
6711 struct got_commit_object *orig_commit, const char *new_logmsg,
6712 struct got_repository *repo)
6714 const struct got_error *err, *sync_err;
6715 struct got_pathlist_head commitable_paths;
6716 struct collect_commitables_arg cc_arg;
6717 char *fileindex_path = NULL;
6718 struct got_reference *head_ref = NULL;
6719 struct got_object_id *head_commit_id = NULL;
6720 char *logmsg = NULL;
6722 memset(&cc_arg, 0, sizeof(cc_arg));
6723 TAILQ_INIT(&commitable_paths);
6724 *new_commit_id = NULL;
6726 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6728 err = get_fileindex_path(&fileindex_path, worktree);
6729 if (err)
6730 return err;
6732 cc_arg.commitable_paths = &commitable_paths;
6733 cc_arg.worktree = worktree;
6734 cc_arg.repo = repo;
6735 cc_arg.have_staged_files = 0;
6737 * If possible get the status of individual files directly to
6738 * avoid crawling the entire work tree once per rebased commit.
6740 * Ideally, merged_paths would contain a list of commitables
6741 * we could use so we could skip worktree_status() entirely.
6742 * However, we would then need carefully keep track of cumulative
6743 * effects of operations such as file additions and deletions
6744 * in 'got histedit -f' (folding multiple commits into one),
6745 * and this extra complexity is not really worth it.
6747 if (merged_paths) {
6748 struct got_pathlist_entry *pe;
6749 TAILQ_FOREACH(pe, merged_paths, entry) {
6750 err = worktree_status(worktree, pe->path, fileindex,
6751 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6752 0);
6753 if (err)
6754 goto done;
6756 } else {
6757 err = worktree_status(worktree, "", fileindex, repo,
6758 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6759 if (err)
6760 goto done;
6763 if (TAILQ_EMPTY(&commitable_paths)) {
6764 /* No-op change; commit will be elided. */
6765 err = got_ref_delete(commit_ref, repo);
6766 if (err)
6767 goto done;
6768 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6769 goto done;
6772 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6773 if (err)
6774 goto done;
6776 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6777 if (err)
6778 goto done;
6780 if (new_logmsg) {
6781 logmsg = strdup(new_logmsg);
6782 if (logmsg == NULL) {
6783 err = got_error_from_errno("strdup");
6784 goto done;
6786 } else {
6787 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6788 if (err)
6789 goto done;
6792 /* NB: commit_worktree will call free(logmsg) */
6793 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6794 NULL, worktree, got_object_commit_get_author(orig_commit),
6795 committer ? committer :
6796 got_object_commit_get_committer(orig_commit), NULL,
6797 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6798 if (err)
6799 goto done;
6801 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6802 if (err)
6803 goto done;
6805 err = got_ref_delete(commit_ref, repo);
6806 if (err)
6807 goto done;
6809 err = update_fileindex_after_commit(worktree, &commitable_paths,
6810 *new_commit_id, fileindex, 0);
6811 sync_err = sync_fileindex(fileindex, fileindex_path);
6812 if (sync_err && err == NULL)
6813 err = sync_err;
6814 done:
6815 free(fileindex_path);
6816 free(head_commit_id);
6817 if (head_ref)
6818 got_ref_close(head_ref);
6819 if (err) {
6820 free(*new_commit_id);
6821 *new_commit_id = NULL;
6823 return err;
6826 const struct got_error *
6827 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6828 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6829 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6830 const char *committer, struct got_commit_object *orig_commit,
6831 struct got_object_id *orig_commit_id, struct got_repository *repo)
6833 const struct got_error *err;
6834 char *commit_ref_name;
6835 struct got_reference *commit_ref = NULL;
6836 struct got_object_id *commit_id = NULL;
6838 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6839 if (err)
6840 return err;
6842 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6843 if (err)
6844 goto done;
6845 err = got_ref_resolve(&commit_id, repo, commit_ref);
6846 if (err)
6847 goto done;
6848 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6849 err = got_error(GOT_ERR_REBASE_COMMITID);
6850 goto done;
6853 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6854 worktree, fileindex, tmp_branch, committer, orig_commit,
6855 NULL, repo);
6856 done:
6857 if (commit_ref)
6858 got_ref_close(commit_ref);
6859 free(commit_ref_name);
6860 free(commit_id);
6861 return err;
6864 const struct got_error *
6865 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6866 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6867 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6868 const char *committer, struct got_commit_object *orig_commit,
6869 struct got_object_id *orig_commit_id, const char *new_logmsg,
6870 struct got_repository *repo)
6872 const struct got_error *err;
6873 char *commit_ref_name;
6874 struct got_reference *commit_ref = NULL;
6876 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6877 if (err)
6878 return err;
6880 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6881 if (err)
6882 goto done;
6884 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6885 worktree, fileindex, tmp_branch, committer, orig_commit,
6886 new_logmsg, repo);
6887 done:
6888 if (commit_ref)
6889 got_ref_close(commit_ref);
6890 free(commit_ref_name);
6891 return err;
6894 const struct got_error *
6895 got_worktree_rebase_postpone(struct got_worktree *worktree,
6896 struct got_fileindex *fileindex)
6898 if (fileindex)
6899 got_fileindex_free(fileindex);
6900 return lock_worktree(worktree, LOCK_SH);
6903 static const struct got_error *
6904 delete_ref(const char *name, struct got_repository *repo)
6906 const struct got_error *err;
6907 struct got_reference *ref;
6909 err = got_ref_open(&ref, repo, name, 0);
6910 if (err) {
6911 if (err->code == GOT_ERR_NOT_REF)
6912 return NULL;
6913 return err;
6916 err = got_ref_delete(ref, repo);
6917 got_ref_close(ref);
6918 return err;
6921 static const struct got_error *
6922 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6924 const struct got_error *err;
6925 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6926 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6928 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6929 if (err)
6930 goto done;
6931 err = delete_ref(tmp_branch_name, repo);
6932 if (err)
6933 goto done;
6935 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6936 if (err)
6937 goto done;
6938 err = delete_ref(new_base_branch_ref_name, repo);
6939 if (err)
6940 goto done;
6942 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6943 if (err)
6944 goto done;
6945 err = delete_ref(branch_ref_name, repo);
6946 if (err)
6947 goto done;
6949 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6950 if (err)
6951 goto done;
6952 err = delete_ref(commit_ref_name, repo);
6953 if (err)
6954 goto done;
6956 done:
6957 free(tmp_branch_name);
6958 free(new_base_branch_ref_name);
6959 free(branch_ref_name);
6960 free(commit_ref_name);
6961 return err;
6964 static const struct got_error *
6965 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6966 struct got_object_id *new_commit_id, struct got_repository *repo)
6968 const struct got_error *err;
6969 struct got_reference *ref = NULL;
6970 struct got_object_id *old_commit_id = NULL;
6971 const char *branch_name = NULL;
6972 char *new_id_str = NULL;
6973 char *refname = NULL;
6975 branch_name = got_ref_get_name(branch);
6976 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6977 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6978 branch_name += 11;
6980 err = got_object_id_str(&new_id_str, new_commit_id);
6981 if (err)
6982 return err;
6984 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6985 new_id_str) == -1) {
6986 err = got_error_from_errno("asprintf");
6987 goto done;
6990 err = got_ref_resolve(&old_commit_id, repo, branch);
6991 if (err)
6992 goto done;
6994 err = got_ref_alloc(&ref, refname, old_commit_id);
6995 if (err)
6996 goto done;
6998 err = got_ref_write(ref, repo);
6999 done:
7000 free(new_id_str);
7001 free(refname);
7002 free(old_commit_id);
7003 if (ref)
7004 got_ref_close(ref);
7005 return err;
7008 const struct got_error *
7009 got_worktree_rebase_complete(struct got_worktree *worktree,
7010 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
7011 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
7012 struct got_repository *repo, int create_backup)
7014 const struct got_error *err, *unlockerr, *sync_err;
7015 struct got_object_id *new_head_commit_id = NULL;
7016 char *fileindex_path = NULL;
7018 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7019 if (err)
7020 return err;
7022 if (create_backup) {
7023 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7024 rebased_branch, new_head_commit_id, repo);
7025 if (err)
7026 goto done;
7029 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7030 if (err)
7031 goto done;
7033 err = got_ref_write(rebased_branch, repo);
7034 if (err)
7035 goto done;
7037 err = got_worktree_set_head_ref(worktree, rebased_branch);
7038 if (err)
7039 goto done;
7041 err = delete_rebase_refs(worktree, repo);
7042 if (err)
7043 goto done;
7045 err = get_fileindex_path(&fileindex_path, worktree);
7046 if (err)
7047 goto done;
7048 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7049 sync_err = sync_fileindex(fileindex, fileindex_path);
7050 if (sync_err && err == NULL)
7051 err = sync_err;
7052 done:
7053 got_fileindex_free(fileindex);
7054 free(fileindex_path);
7055 free(new_head_commit_id);
7056 unlockerr = lock_worktree(worktree, LOCK_SH);
7057 if (unlockerr && err == NULL)
7058 err = unlockerr;
7059 return err;
7062 const struct got_error *
7063 got_worktree_rebase_abort(struct got_worktree *worktree,
7064 struct got_fileindex *fileindex, struct got_repository *repo,
7065 struct got_reference *new_base_branch,
7066 got_worktree_checkout_cb progress_cb, void *progress_arg)
7068 const struct got_error *err, *unlockerr, *sync_err;
7069 struct got_reference *resolved = NULL;
7070 struct got_object_id *commit_id = NULL;
7071 struct got_commit_object *commit = NULL;
7072 char *fileindex_path = NULL;
7073 struct revert_file_args rfa;
7074 struct got_object_id *tree_id = NULL;
7076 err = lock_worktree(worktree, LOCK_EX);
7077 if (err)
7078 return err;
7080 err = got_object_open_as_commit(&commit, repo,
7081 worktree->base_commit_id);
7082 if (err)
7083 goto done;
7085 err = got_ref_open(&resolved, repo,
7086 got_ref_get_symref_target(new_base_branch), 0);
7087 if (err)
7088 goto done;
7090 err = got_worktree_set_head_ref(worktree, resolved);
7091 if (err)
7092 goto done;
7095 * XXX commits to the base branch could have happened while
7096 * we were busy rebasing; should we store the original commit ID
7097 * when rebase begins and read it back here?
7099 err = got_ref_resolve(&commit_id, repo, resolved);
7100 if (err)
7101 goto done;
7103 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7104 if (err)
7105 goto done;
7107 err = got_object_id_by_path(&tree_id, repo, commit,
7108 worktree->path_prefix);
7109 if (err)
7110 goto done;
7112 err = delete_rebase_refs(worktree, repo);
7113 if (err)
7114 goto done;
7116 err = get_fileindex_path(&fileindex_path, worktree);
7117 if (err)
7118 goto done;
7120 rfa.worktree = worktree;
7121 rfa.fileindex = fileindex;
7122 rfa.progress_cb = progress_cb;
7123 rfa.progress_arg = progress_arg;
7124 rfa.patch_cb = NULL;
7125 rfa.patch_arg = NULL;
7126 rfa.repo = repo;
7127 rfa.unlink_added_files = 0;
7128 err = worktree_status(worktree, "", fileindex, repo,
7129 revert_file, &rfa, NULL, NULL, 1, 0);
7130 if (err)
7131 goto sync;
7133 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7134 repo, progress_cb, progress_arg, NULL, NULL);
7135 sync:
7136 sync_err = sync_fileindex(fileindex, fileindex_path);
7137 if (sync_err && err == NULL)
7138 err = sync_err;
7139 done:
7140 got_ref_close(resolved);
7141 free(tree_id);
7142 free(commit_id);
7143 if (commit)
7144 got_object_commit_close(commit);
7145 if (fileindex)
7146 got_fileindex_free(fileindex);
7147 free(fileindex_path);
7149 unlockerr = lock_worktree(worktree, LOCK_SH);
7150 if (unlockerr && err == NULL)
7151 err = unlockerr;
7152 return err;
7155 const struct got_error *
7156 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7157 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7158 struct got_fileindex **fileindex, struct got_worktree *worktree,
7159 struct got_repository *repo)
7161 const struct got_error *err = NULL;
7162 char *tmp_branch_name = NULL;
7163 char *branch_ref_name = NULL;
7164 char *base_commit_ref_name = NULL;
7165 char *fileindex_path = NULL;
7166 struct check_rebase_ok_arg ok_arg;
7167 struct got_reference *wt_branch = NULL;
7168 struct got_reference *base_commit_ref = NULL;
7170 *tmp_branch = NULL;
7171 *branch_ref = NULL;
7172 *base_commit_id = NULL;
7173 *fileindex = NULL;
7175 err = lock_worktree(worktree, LOCK_EX);
7176 if (err)
7177 return err;
7179 err = open_fileindex(fileindex, &fileindex_path, worktree);
7180 if (err)
7181 goto done;
7183 ok_arg.worktree = worktree;
7184 ok_arg.repo = repo;
7185 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7186 &ok_arg);
7187 if (err)
7188 goto done;
7190 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7191 if (err)
7192 goto done;
7194 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7195 if (err)
7196 goto done;
7198 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7199 worktree);
7200 if (err)
7201 goto done;
7203 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7204 0);
7205 if (err)
7206 goto done;
7208 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7209 if (err)
7210 goto done;
7212 err = got_ref_write(*branch_ref, repo);
7213 if (err)
7214 goto done;
7216 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7217 worktree->base_commit_id);
7218 if (err)
7219 goto done;
7220 err = got_ref_write(base_commit_ref, repo);
7221 if (err)
7222 goto done;
7223 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7224 if (*base_commit_id == NULL) {
7225 err = got_error_from_errno("got_object_id_dup");
7226 goto done;
7229 err = got_ref_alloc(tmp_branch, tmp_branch_name,
7230 worktree->base_commit_id);
7231 if (err)
7232 goto done;
7233 err = got_ref_write(*tmp_branch, repo);
7234 if (err)
7235 goto done;
7237 err = got_worktree_set_head_ref(worktree, *tmp_branch);
7238 if (err)
7239 goto done;
7240 done:
7241 free(fileindex_path);
7242 free(tmp_branch_name);
7243 free(branch_ref_name);
7244 free(base_commit_ref_name);
7245 if (wt_branch)
7246 got_ref_close(wt_branch);
7247 if (err) {
7248 if (*branch_ref) {
7249 got_ref_close(*branch_ref);
7250 *branch_ref = NULL;
7252 if (*tmp_branch) {
7253 got_ref_close(*tmp_branch);
7254 *tmp_branch = NULL;
7256 free(*base_commit_id);
7257 if (*fileindex) {
7258 got_fileindex_free(*fileindex);
7259 *fileindex = NULL;
7261 lock_worktree(worktree, LOCK_SH);
7263 return err;
7266 const struct got_error *
7267 got_worktree_histedit_postpone(struct got_worktree *worktree,
7268 struct got_fileindex *fileindex)
7270 if (fileindex)
7271 got_fileindex_free(fileindex);
7272 return lock_worktree(worktree, LOCK_SH);
7275 const struct got_error *
7276 got_worktree_histedit_in_progress(int *in_progress,
7277 struct got_worktree *worktree)
7279 const struct got_error *err;
7280 char *tmp_branch_name = NULL;
7282 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7283 if (err)
7284 return err;
7286 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7287 free(tmp_branch_name);
7288 return NULL;
7291 const struct got_error *
7292 got_worktree_histedit_continue(struct got_object_id **commit_id,
7293 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7294 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7295 struct got_worktree *worktree, struct got_repository *repo)
7297 const struct got_error *err;
7298 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7299 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7300 struct got_reference *commit_ref = NULL;
7301 struct got_reference *base_commit_ref = NULL;
7302 char *fileindex_path = NULL;
7303 int have_staged_files = 0;
7305 *commit_id = NULL;
7306 *tmp_branch = NULL;
7307 *base_commit_id = NULL;
7308 *fileindex = NULL;
7310 err = lock_worktree(worktree, LOCK_EX);
7311 if (err)
7312 return err;
7314 err = open_fileindex(fileindex, &fileindex_path, worktree);
7315 if (err)
7316 goto done;
7318 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7319 &have_staged_files);
7320 if (err && err->code != GOT_ERR_CANCELLED)
7321 goto done;
7322 if (have_staged_files) {
7323 err = got_error(GOT_ERR_STAGED_PATHS);
7324 goto done;
7327 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7328 if (err)
7329 goto done;
7331 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7332 if (err)
7333 goto done;
7335 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7336 if (err)
7337 goto done;
7339 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7340 worktree);
7341 if (err)
7342 goto done;
7344 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7345 if (err)
7346 goto done;
7348 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7349 if (err)
7350 goto done;
7351 err = got_ref_resolve(commit_id, repo, commit_ref);
7352 if (err)
7353 goto done;
7355 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7356 if (err)
7357 goto done;
7358 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7359 if (err)
7360 goto done;
7362 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7363 if (err)
7364 goto done;
7365 done:
7366 free(commit_ref_name);
7367 free(branch_ref_name);
7368 free(fileindex_path);
7369 if (commit_ref)
7370 got_ref_close(commit_ref);
7371 if (base_commit_ref)
7372 got_ref_close(base_commit_ref);
7373 if (err) {
7374 free(*commit_id);
7375 *commit_id = NULL;
7376 free(*base_commit_id);
7377 *base_commit_id = NULL;
7378 if (*tmp_branch) {
7379 got_ref_close(*tmp_branch);
7380 *tmp_branch = NULL;
7382 if (*fileindex) {
7383 got_fileindex_free(*fileindex);
7384 *fileindex = NULL;
7386 lock_worktree(worktree, LOCK_EX);
7388 return err;
7391 static const struct got_error *
7392 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7394 const struct got_error *err;
7395 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7396 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7398 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7399 if (err)
7400 goto done;
7401 err = delete_ref(tmp_branch_name, repo);
7402 if (err)
7403 goto done;
7405 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7406 worktree);
7407 if (err)
7408 goto done;
7409 err = delete_ref(base_commit_ref_name, repo);
7410 if (err)
7411 goto done;
7413 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7414 if (err)
7415 goto done;
7416 err = delete_ref(branch_ref_name, repo);
7417 if (err)
7418 goto done;
7420 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7421 if (err)
7422 goto done;
7423 err = delete_ref(commit_ref_name, repo);
7424 if (err)
7425 goto done;
7426 done:
7427 free(tmp_branch_name);
7428 free(base_commit_ref_name);
7429 free(branch_ref_name);
7430 free(commit_ref_name);
7431 return err;
7434 const struct got_error *
7435 got_worktree_histedit_abort(struct got_worktree *worktree,
7436 struct got_fileindex *fileindex, struct got_repository *repo,
7437 struct got_reference *branch, struct got_object_id *base_commit_id,
7438 got_worktree_checkout_cb progress_cb, void *progress_arg)
7440 const struct got_error *err, *unlockerr, *sync_err;
7441 struct got_reference *resolved = NULL;
7442 char *fileindex_path = NULL;
7443 struct got_commit_object *commit = NULL;
7444 struct got_object_id *tree_id = NULL;
7445 struct revert_file_args rfa;
7447 err = lock_worktree(worktree, LOCK_EX);
7448 if (err)
7449 return err;
7451 err = got_object_open_as_commit(&commit, repo,
7452 worktree->base_commit_id);
7453 if (err)
7454 goto done;
7456 err = got_ref_open(&resolved, repo,
7457 got_ref_get_symref_target(branch), 0);
7458 if (err)
7459 goto done;
7461 err = got_worktree_set_head_ref(worktree, resolved);
7462 if (err)
7463 goto done;
7465 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7466 if (err)
7467 goto done;
7469 err = got_object_id_by_path(&tree_id, repo, commit,
7470 worktree->path_prefix);
7471 if (err)
7472 goto done;
7474 err = delete_histedit_refs(worktree, repo);
7475 if (err)
7476 goto done;
7478 err = get_fileindex_path(&fileindex_path, worktree);
7479 if (err)
7480 goto done;
7482 rfa.worktree = worktree;
7483 rfa.fileindex = fileindex;
7484 rfa.progress_cb = progress_cb;
7485 rfa.progress_arg = progress_arg;
7486 rfa.patch_cb = NULL;
7487 rfa.patch_arg = NULL;
7488 rfa.repo = repo;
7489 rfa.unlink_added_files = 0;
7490 err = worktree_status(worktree, "", fileindex, repo,
7491 revert_file, &rfa, NULL, NULL, 1, 0);
7492 if (err)
7493 goto sync;
7495 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7496 repo, progress_cb, progress_arg, NULL, NULL);
7497 sync:
7498 sync_err = sync_fileindex(fileindex, fileindex_path);
7499 if (sync_err && err == NULL)
7500 err = sync_err;
7501 done:
7502 got_ref_close(resolved);
7503 free(tree_id);
7504 free(fileindex_path);
7506 unlockerr = lock_worktree(worktree, LOCK_SH);
7507 if (unlockerr && err == NULL)
7508 err = unlockerr;
7509 return err;
7512 const struct got_error *
7513 got_worktree_histedit_complete(struct got_worktree *worktree,
7514 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7515 struct got_reference *edited_branch, struct got_repository *repo)
7517 const struct got_error *err, *unlockerr, *sync_err;
7518 struct got_object_id *new_head_commit_id = NULL;
7519 struct got_reference *resolved = NULL;
7520 char *fileindex_path = NULL;
7522 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7523 if (err)
7524 return err;
7526 err = got_ref_open(&resolved, repo,
7527 got_ref_get_symref_target(edited_branch), 0);
7528 if (err)
7529 goto done;
7531 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7532 resolved, new_head_commit_id, repo);
7533 if (err)
7534 goto done;
7536 err = got_ref_change_ref(resolved, new_head_commit_id);
7537 if (err)
7538 goto done;
7540 err = got_ref_write(resolved, repo);
7541 if (err)
7542 goto done;
7544 err = got_worktree_set_head_ref(worktree, resolved);
7545 if (err)
7546 goto done;
7548 err = delete_histedit_refs(worktree, repo);
7549 if (err)
7550 goto done;
7552 err = get_fileindex_path(&fileindex_path, worktree);
7553 if (err)
7554 goto done;
7555 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7556 sync_err = sync_fileindex(fileindex, fileindex_path);
7557 if (sync_err && err == NULL)
7558 err = sync_err;
7559 done:
7560 got_fileindex_free(fileindex);
7561 free(fileindex_path);
7562 free(new_head_commit_id);
7563 unlockerr = lock_worktree(worktree, LOCK_SH);
7564 if (unlockerr && err == NULL)
7565 err = unlockerr;
7566 return err;
7569 const struct got_error *
7570 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7571 struct got_object_id *commit_id, struct got_repository *repo)
7573 const struct got_error *err;
7574 char *commit_ref_name;
7576 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7577 if (err)
7578 return err;
7580 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7581 if (err)
7582 goto done;
7584 err = delete_ref(commit_ref_name, repo);
7585 done:
7586 free(commit_ref_name);
7587 return err;
7590 const struct got_error *
7591 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7592 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7593 struct got_worktree *worktree, const char *refname,
7594 struct got_repository *repo)
7596 const struct got_error *err = NULL;
7597 char *fileindex_path = NULL;
7598 struct check_rebase_ok_arg ok_arg;
7600 *fileindex = NULL;
7601 *branch_ref = NULL;
7602 *base_branch_ref = NULL;
7604 err = lock_worktree(worktree, LOCK_EX);
7605 if (err)
7606 return err;
7608 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7609 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7610 "cannot integrate a branch into itself; "
7611 "update -b or different branch name required");
7612 goto done;
7615 err = open_fileindex(fileindex, &fileindex_path, worktree);
7616 if (err)
7617 goto done;
7619 /* Preconditions are the same as for rebase. */
7620 ok_arg.worktree = worktree;
7621 ok_arg.repo = repo;
7622 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7623 &ok_arg);
7624 if (err)
7625 goto done;
7627 err = got_ref_open(branch_ref, repo, refname, 1);
7628 if (err)
7629 goto done;
7631 err = got_ref_open(base_branch_ref, repo,
7632 got_worktree_get_head_ref_name(worktree), 1);
7633 done:
7634 if (err) {
7635 if (*branch_ref) {
7636 got_ref_close(*branch_ref);
7637 *branch_ref = NULL;
7639 if (*base_branch_ref) {
7640 got_ref_close(*base_branch_ref);
7641 *base_branch_ref = NULL;
7643 if (*fileindex) {
7644 got_fileindex_free(*fileindex);
7645 *fileindex = NULL;
7647 lock_worktree(worktree, LOCK_SH);
7649 return err;
7652 const struct got_error *
7653 got_worktree_integrate_continue(struct got_worktree *worktree,
7654 struct got_fileindex *fileindex, struct got_repository *repo,
7655 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7656 got_worktree_checkout_cb progress_cb, void *progress_arg,
7657 got_cancel_cb cancel_cb, void *cancel_arg)
7659 const struct got_error *err = NULL, *sync_err, *unlockerr;
7660 char *fileindex_path = NULL;
7661 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7662 struct got_commit_object *commit = NULL;
7664 err = get_fileindex_path(&fileindex_path, worktree);
7665 if (err)
7666 goto done;
7668 err = got_ref_resolve(&commit_id, repo, branch_ref);
7669 if (err)
7670 goto done;
7672 err = got_object_open_as_commit(&commit, repo, commit_id);
7673 if (err)
7674 goto done;
7676 err = got_object_id_by_path(&tree_id, repo, commit,
7677 worktree->path_prefix);
7678 if (err)
7679 goto done;
7681 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7682 if (err)
7683 goto done;
7685 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7686 progress_cb, progress_arg, cancel_cb, cancel_arg);
7687 if (err)
7688 goto sync;
7690 err = got_ref_change_ref(base_branch_ref, commit_id);
7691 if (err)
7692 goto sync;
7694 err = got_ref_write(base_branch_ref, repo);
7695 if (err)
7696 goto sync;
7698 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7699 sync:
7700 sync_err = sync_fileindex(fileindex, fileindex_path);
7701 if (sync_err && err == NULL)
7702 err = sync_err;
7704 done:
7705 unlockerr = got_ref_unlock(branch_ref);
7706 if (unlockerr && err == NULL)
7707 err = unlockerr;
7708 got_ref_close(branch_ref);
7710 unlockerr = got_ref_unlock(base_branch_ref);
7711 if (unlockerr && err == NULL)
7712 err = unlockerr;
7713 got_ref_close(base_branch_ref);
7715 got_fileindex_free(fileindex);
7716 free(fileindex_path);
7717 free(tree_id);
7718 if (commit)
7719 got_object_commit_close(commit);
7721 unlockerr = lock_worktree(worktree, LOCK_SH);
7722 if (unlockerr && err == NULL)
7723 err = unlockerr;
7724 return err;
7727 const struct got_error *
7728 got_worktree_integrate_abort(struct got_worktree *worktree,
7729 struct got_fileindex *fileindex, struct got_repository *repo,
7730 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7732 const struct got_error *err = NULL, *unlockerr = NULL;
7734 got_fileindex_free(fileindex);
7736 err = lock_worktree(worktree, LOCK_SH);
7738 unlockerr = got_ref_unlock(branch_ref);
7739 if (unlockerr && err == NULL)
7740 err = unlockerr;
7741 got_ref_close(branch_ref);
7743 unlockerr = got_ref_unlock(base_branch_ref);
7744 if (unlockerr && err == NULL)
7745 err = unlockerr;
7746 got_ref_close(base_branch_ref);
7748 return err;
7751 const struct got_error *
7752 got_worktree_merge_postpone(struct got_worktree *worktree,
7753 struct got_fileindex *fileindex)
7755 const struct got_error *err, *sync_err;
7756 char *fileindex_path = NULL;
7758 err = get_fileindex_path(&fileindex_path, worktree);
7759 if (err)
7760 goto done;
7762 sync_err = sync_fileindex(fileindex, fileindex_path);
7764 err = lock_worktree(worktree, LOCK_SH);
7765 if (sync_err && err == NULL)
7766 err = sync_err;
7767 done:
7768 got_fileindex_free(fileindex);
7769 free(fileindex_path);
7770 return err;
7773 static const struct got_error *
7774 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7776 const struct got_error *err;
7777 char *branch_refname = NULL, *commit_refname = NULL;
7779 err = get_merge_branch_ref_name(&branch_refname, worktree);
7780 if (err)
7781 goto done;
7782 err = delete_ref(branch_refname, repo);
7783 if (err)
7784 goto done;
7786 err = get_merge_commit_ref_name(&commit_refname, worktree);
7787 if (err)
7788 goto done;
7789 err = delete_ref(commit_refname, repo);
7790 if (err)
7791 goto done;
7793 done:
7794 free(branch_refname);
7795 free(commit_refname);
7796 return err;
7799 struct merge_commit_msg_arg {
7800 struct got_worktree *worktree;
7801 const char *branch_name;
7804 static const struct got_error *
7805 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
7806 const char *diff_path, char **logmsg, void *arg)
7808 struct merge_commit_msg_arg *a = arg;
7810 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7811 got_worktree_get_head_ref_name(a->worktree)) == -1)
7812 return got_error_from_errno("asprintf");
7814 return NULL;
7818 const struct got_error *
7819 got_worktree_merge_branch(struct got_worktree *worktree,
7820 struct got_fileindex *fileindex,
7821 struct got_object_id *yca_commit_id,
7822 struct got_object_id *branch_tip,
7823 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7824 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7826 const struct got_error *err;
7827 char *fileindex_path = NULL;
7829 err = get_fileindex_path(&fileindex_path, worktree);
7830 if (err)
7831 goto done;
7833 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7834 worktree);
7835 if (err)
7836 goto done;
7838 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7839 branch_tip, repo, progress_cb, progress_arg,
7840 cancel_cb, cancel_arg);
7841 done:
7842 free(fileindex_path);
7843 return err;
7846 const struct got_error *
7847 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7848 struct got_worktree *worktree, struct got_fileindex *fileindex,
7849 const char *author, const char *committer, int allow_bad_symlinks,
7850 struct got_object_id *branch_tip, const char *branch_name,
7851 struct got_repository *repo,
7852 got_worktree_status_cb status_cb, void *status_arg)
7855 const struct got_error *err = NULL, *sync_err;
7856 struct got_pathlist_head commitable_paths;
7857 struct collect_commitables_arg cc_arg;
7858 struct got_pathlist_entry *pe;
7859 struct got_reference *head_ref = NULL;
7860 struct got_object_id *head_commit_id = NULL;
7861 int have_staged_files = 0;
7862 struct merge_commit_msg_arg mcm_arg;
7863 char *fileindex_path = NULL;
7865 memset(&cc_arg, 0, sizeof(cc_arg));
7866 *new_commit_id = NULL;
7868 TAILQ_INIT(&commitable_paths);
7870 err = get_fileindex_path(&fileindex_path, worktree);
7871 if (err)
7872 goto done;
7874 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7875 if (err)
7876 goto done;
7878 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7879 if (err)
7880 goto done;
7882 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7883 &have_staged_files);
7884 if (err && err->code != GOT_ERR_CANCELLED)
7885 goto done;
7886 if (have_staged_files) {
7887 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7888 goto done;
7891 cc_arg.commitable_paths = &commitable_paths;
7892 cc_arg.worktree = worktree;
7893 cc_arg.fileindex = fileindex;
7894 cc_arg.repo = repo;
7895 cc_arg.have_staged_files = have_staged_files;
7896 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7897 err = worktree_status(worktree, "", fileindex, repo,
7898 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7899 if (err)
7900 goto done;
7902 if (TAILQ_EMPTY(&commitable_paths)) {
7903 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7904 "merge of %s cannot proceed", branch_name);
7905 goto done;
7908 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7909 struct got_commitable *ct = pe->data;
7910 const char *ct_path = ct->in_repo_path;
7912 while (ct_path[0] == '/')
7913 ct_path++;
7914 err = check_out_of_date(ct_path, ct->status,
7915 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7916 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7917 if (err)
7918 goto done;
7922 mcm_arg.worktree = worktree;
7923 mcm_arg.branch_name = branch_name;
7924 err = commit_worktree(new_commit_id, &commitable_paths,
7925 head_commit_id, branch_tip, worktree, author, committer, NULL,
7926 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7927 if (err)
7928 goto done;
7930 err = update_fileindex_after_commit(worktree, &commitable_paths,
7931 *new_commit_id, fileindex, have_staged_files);
7932 sync_err = sync_fileindex(fileindex, fileindex_path);
7933 if (sync_err && err == NULL)
7934 err = sync_err;
7935 done:
7936 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7937 struct got_commitable *ct = pe->data;
7939 free_commitable(ct);
7941 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
7942 free(fileindex_path);
7943 return err;
7946 const struct got_error *
7947 got_worktree_merge_complete(struct got_worktree *worktree,
7948 struct got_fileindex *fileindex, struct got_repository *repo)
7950 const struct got_error *err, *unlockerr, *sync_err;
7951 char *fileindex_path = NULL;
7953 err = delete_merge_refs(worktree, repo);
7954 if (err)
7955 goto done;
7957 err = get_fileindex_path(&fileindex_path, worktree);
7958 if (err)
7959 goto done;
7960 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7961 sync_err = sync_fileindex(fileindex, fileindex_path);
7962 if (sync_err && err == NULL)
7963 err = sync_err;
7964 done:
7965 got_fileindex_free(fileindex);
7966 free(fileindex_path);
7967 unlockerr = lock_worktree(worktree, LOCK_SH);
7968 if (unlockerr && err == NULL)
7969 err = unlockerr;
7970 return err;
7973 const struct got_error *
7974 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7975 struct got_repository *repo)
7977 const struct got_error *err;
7978 char *branch_refname = NULL;
7979 struct got_reference *branch_ref = NULL;
7981 *in_progress = 0;
7983 err = get_merge_branch_ref_name(&branch_refname, worktree);
7984 if (err)
7985 return err;
7986 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7987 free(branch_refname);
7988 if (err) {
7989 if (err->code != GOT_ERR_NOT_REF)
7990 return err;
7991 } else
7992 *in_progress = 1;
7994 return NULL;
7997 const struct got_error *got_worktree_merge_prepare(
7998 struct got_fileindex **fileindex, struct got_worktree *worktree,
7999 struct got_reference *branch, struct got_repository *repo)
8001 const struct got_error *err = NULL;
8002 char *fileindex_path = NULL;
8003 char *branch_refname = NULL, *commit_refname = NULL;
8004 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
8005 struct got_reference *commit_ref = NULL;
8006 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
8007 struct check_rebase_ok_arg ok_arg;
8009 *fileindex = NULL;
8011 err = lock_worktree(worktree, LOCK_EX);
8012 if (err)
8013 return err;
8015 err = open_fileindex(fileindex, &fileindex_path, worktree);
8016 if (err)
8017 goto done;
8019 /* Preconditions are the same as for rebase. */
8020 ok_arg.worktree = worktree;
8021 ok_arg.repo = repo;
8022 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8023 &ok_arg);
8024 if (err)
8025 goto done;
8027 err = get_merge_branch_ref_name(&branch_refname, worktree);
8028 if (err)
8029 return err;
8031 err = get_merge_commit_ref_name(&commit_refname, worktree);
8032 if (err)
8033 return err;
8035 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8036 0);
8037 if (err)
8038 goto done;
8040 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8041 if (err)
8042 goto done;
8044 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8045 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8046 goto done;
8049 err = got_ref_resolve(&branch_tip, repo, branch);
8050 if (err)
8051 goto done;
8053 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8054 if (err)
8055 goto done;
8056 err = got_ref_write(branch_ref, repo);
8057 if (err)
8058 goto done;
8060 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8061 if (err)
8062 goto done;
8063 err = got_ref_write(commit_ref, repo);
8064 if (err)
8065 goto done;
8067 done:
8068 free(branch_refname);
8069 free(commit_refname);
8070 free(fileindex_path);
8071 if (branch_ref)
8072 got_ref_close(branch_ref);
8073 if (commit_ref)
8074 got_ref_close(commit_ref);
8075 if (wt_branch)
8076 got_ref_close(wt_branch);
8077 free(wt_branch_tip);
8078 if (err) {
8079 if (*fileindex) {
8080 got_fileindex_free(*fileindex);
8081 *fileindex = NULL;
8083 lock_worktree(worktree, LOCK_SH);
8085 return err;
8088 const struct got_error *
8089 got_worktree_merge_continue(char **branch_name,
8090 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8091 struct got_worktree *worktree, struct got_repository *repo)
8093 const struct got_error *err;
8094 char *commit_refname = NULL, *branch_refname = NULL;
8095 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8096 char *fileindex_path = NULL;
8097 int have_staged_files = 0;
8099 *branch_name = NULL;
8100 *branch_tip = NULL;
8101 *fileindex = NULL;
8103 err = lock_worktree(worktree, LOCK_EX);
8104 if (err)
8105 return err;
8107 err = open_fileindex(fileindex, &fileindex_path, worktree);
8108 if (err)
8109 goto done;
8111 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8112 &have_staged_files);
8113 if (err && err->code != GOT_ERR_CANCELLED)
8114 goto done;
8115 if (have_staged_files) {
8116 err = got_error(GOT_ERR_STAGED_PATHS);
8117 goto done;
8120 err = get_merge_branch_ref_name(&branch_refname, worktree);
8121 if (err)
8122 goto done;
8124 err = get_merge_commit_ref_name(&commit_refname, worktree);
8125 if (err)
8126 goto done;
8128 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8129 if (err)
8130 goto done;
8132 if (!got_ref_is_symbolic(branch_ref)) {
8133 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8134 "%s is not a symbolic reference",
8135 got_ref_get_name(branch_ref));
8136 goto done;
8138 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8139 if (*branch_name == NULL) {
8140 err = got_error_from_errno("strdup");
8141 goto done;
8144 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8145 if (err)
8146 goto done;
8148 err = got_ref_resolve(branch_tip, repo, commit_ref);
8149 if (err)
8150 goto done;
8151 done:
8152 free(commit_refname);
8153 free(branch_refname);
8154 free(fileindex_path);
8155 if (commit_ref)
8156 got_ref_close(commit_ref);
8157 if (branch_ref)
8158 got_ref_close(branch_ref);
8159 if (err) {
8160 if (*branch_name) {
8161 free(*branch_name);
8162 *branch_name = NULL;
8164 free(*branch_tip);
8165 *branch_tip = NULL;
8166 if (*fileindex) {
8167 got_fileindex_free(*fileindex);
8168 *fileindex = NULL;
8170 lock_worktree(worktree, LOCK_SH);
8172 return err;
8175 const struct got_error *
8176 got_worktree_merge_abort(struct got_worktree *worktree,
8177 struct got_fileindex *fileindex, struct got_repository *repo,
8178 got_worktree_checkout_cb progress_cb, void *progress_arg)
8180 const struct got_error *err, *unlockerr, *sync_err;
8181 struct got_object_id *commit_id = NULL;
8182 struct got_commit_object *commit = NULL;
8183 char *fileindex_path = NULL;
8184 struct revert_file_args rfa;
8185 struct got_object_id *tree_id = NULL;
8187 err = got_object_open_as_commit(&commit, repo,
8188 worktree->base_commit_id);
8189 if (err)
8190 goto done;
8192 err = got_object_id_by_path(&tree_id, repo, commit,
8193 worktree->path_prefix);
8194 if (err)
8195 goto done;
8197 err = delete_merge_refs(worktree, repo);
8198 if (err)
8199 goto done;
8201 err = get_fileindex_path(&fileindex_path, worktree);
8202 if (err)
8203 goto done;
8205 rfa.worktree = worktree;
8206 rfa.fileindex = fileindex;
8207 rfa.progress_cb = progress_cb;
8208 rfa.progress_arg = progress_arg;
8209 rfa.patch_cb = NULL;
8210 rfa.patch_arg = NULL;
8211 rfa.repo = repo;
8212 rfa.unlink_added_files = 1;
8213 err = worktree_status(worktree, "", fileindex, repo,
8214 revert_file, &rfa, NULL, NULL, 1, 0);
8215 if (err)
8216 goto sync;
8218 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8219 repo, progress_cb, progress_arg, NULL, NULL);
8220 sync:
8221 sync_err = sync_fileindex(fileindex, fileindex_path);
8222 if (sync_err && err == NULL)
8223 err = sync_err;
8224 done:
8225 free(tree_id);
8226 free(commit_id);
8227 if (commit)
8228 got_object_commit_close(commit);
8229 if (fileindex)
8230 got_fileindex_free(fileindex);
8231 free(fileindex_path);
8233 unlockerr = lock_worktree(worktree, LOCK_SH);
8234 if (unlockerr && err == NULL)
8235 err = unlockerr;
8236 return err;
8239 struct check_stage_ok_arg {
8240 struct got_object_id *head_commit_id;
8241 struct got_worktree *worktree;
8242 struct got_fileindex *fileindex;
8243 struct got_repository *repo;
8244 int have_changes;
8247 static const struct got_error *
8248 check_stage_ok(void *arg, unsigned char status,
8249 unsigned char staged_status, const char *relpath,
8250 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8251 struct got_object_id *commit_id, int dirfd, const char *de_name)
8253 struct check_stage_ok_arg *a = arg;
8254 const struct got_error *err = NULL;
8255 struct got_fileindex_entry *ie;
8256 struct got_object_id base_commit_id;
8257 struct got_object_id *base_commit_idp = NULL;
8258 char *in_repo_path = NULL, *p;
8260 if (status == GOT_STATUS_UNVERSIONED ||
8261 status == GOT_STATUS_NO_CHANGE)
8262 return NULL;
8263 if (status == GOT_STATUS_NONEXISTENT)
8264 return got_error_set_errno(ENOENT, relpath);
8266 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8267 if (ie == NULL)
8268 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8270 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8271 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8272 relpath) == -1)
8273 return got_error_from_errno("asprintf");
8275 if (got_fileindex_entry_has_commit(ie)) {
8276 memcpy(base_commit_id.sha1, ie->commit_sha1,
8277 SHA1_DIGEST_LENGTH);
8278 base_commit_idp = &base_commit_id;
8281 if (status == GOT_STATUS_CONFLICT) {
8282 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8283 goto done;
8284 } else if (status != GOT_STATUS_ADD &&
8285 status != GOT_STATUS_MODIFY &&
8286 status != GOT_STATUS_DELETE) {
8287 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8288 goto done;
8291 a->have_changes = 1;
8293 p = in_repo_path;
8294 while (p[0] == '/')
8295 p++;
8296 err = check_out_of_date(p, status, staged_status,
8297 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8298 GOT_ERR_STAGE_OUT_OF_DATE);
8299 done:
8300 free(in_repo_path);
8301 return err;
8304 struct stage_path_arg {
8305 struct got_worktree *worktree;
8306 struct got_fileindex *fileindex;
8307 struct got_repository *repo;
8308 got_worktree_status_cb status_cb;
8309 void *status_arg;
8310 got_worktree_patch_cb patch_cb;
8311 void *patch_arg;
8312 int staged_something;
8313 int allow_bad_symlinks;
8316 static const struct got_error *
8317 stage_path(void *arg, unsigned char status,
8318 unsigned char staged_status, const char *relpath,
8319 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8320 struct got_object_id *commit_id, int dirfd, const char *de_name)
8322 struct stage_path_arg *a = arg;
8323 const struct got_error *err = NULL;
8324 struct got_fileindex_entry *ie;
8325 char *ondisk_path = NULL, *path_content = NULL;
8326 uint32_t stage;
8327 struct got_object_id *new_staged_blob_id = NULL;
8328 struct stat sb;
8330 if (status == GOT_STATUS_UNVERSIONED)
8331 return NULL;
8333 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8334 if (ie == NULL)
8335 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8337 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8338 relpath)== -1)
8339 return got_error_from_errno("asprintf");
8341 switch (status) {
8342 case GOT_STATUS_ADD:
8343 case GOT_STATUS_MODIFY:
8344 /* XXX could sb.st_mode be passed in by our caller? */
8345 if (lstat(ondisk_path, &sb) == -1) {
8346 err = got_error_from_errno2("lstat", ondisk_path);
8347 break;
8349 if (a->patch_cb) {
8350 if (status == GOT_STATUS_ADD) {
8351 int choice = GOT_PATCH_CHOICE_NONE;
8352 err = (*a->patch_cb)(&choice, a->patch_arg,
8353 status, ie->path, NULL, 1, 1);
8354 if (err)
8355 break;
8356 if (choice != GOT_PATCH_CHOICE_YES)
8357 break;
8358 } else {
8359 err = create_patched_content(&path_content, 0,
8360 staged_blob_id ? staged_blob_id : blob_id,
8361 ondisk_path, dirfd, de_name, ie->path,
8362 a->repo, a->patch_cb, a->patch_arg);
8363 if (err || path_content == NULL)
8364 break;
8367 err = got_object_blob_create(&new_staged_blob_id,
8368 path_content ? path_content : ondisk_path, a->repo);
8369 if (err)
8370 break;
8371 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8372 SHA1_DIGEST_LENGTH);
8373 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8374 stage = GOT_FILEIDX_STAGE_ADD;
8375 else
8376 stage = GOT_FILEIDX_STAGE_MODIFY;
8377 got_fileindex_entry_stage_set(ie, stage);
8378 if (S_ISLNK(sb.st_mode)) {
8379 int is_bad_symlink = 0;
8380 if (!a->allow_bad_symlinks) {
8381 char target_path[PATH_MAX];
8382 ssize_t target_len;
8383 target_len = readlink(ondisk_path, target_path,
8384 sizeof(target_path));
8385 if (target_len == -1) {
8386 err = got_error_from_errno2("readlink",
8387 ondisk_path);
8388 break;
8390 err = is_bad_symlink_target(&is_bad_symlink,
8391 target_path, target_len, ondisk_path,
8392 a->worktree->root_path);
8393 if (err)
8394 break;
8395 if (is_bad_symlink) {
8396 err = got_error_path(ondisk_path,
8397 GOT_ERR_BAD_SYMLINK);
8398 break;
8401 if (is_bad_symlink)
8402 got_fileindex_entry_staged_filetype_set(ie,
8403 GOT_FILEIDX_MODE_BAD_SYMLINK);
8404 else
8405 got_fileindex_entry_staged_filetype_set(ie,
8406 GOT_FILEIDX_MODE_SYMLINK);
8407 } else {
8408 got_fileindex_entry_staged_filetype_set(ie,
8409 GOT_FILEIDX_MODE_REGULAR_FILE);
8411 a->staged_something = 1;
8412 if (a->status_cb == NULL)
8413 break;
8414 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8415 get_staged_status(ie), relpath, blob_id,
8416 new_staged_blob_id, NULL, dirfd, de_name);
8417 if (err)
8418 break;
8420 * When staging the reverse of the staged diff,
8421 * implicitly unstage the file.
8423 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8424 sizeof(ie->blob_sha1)) == 0) {
8425 got_fileindex_entry_stage_set(ie,
8426 GOT_FILEIDX_STAGE_NONE);
8428 break;
8429 case GOT_STATUS_DELETE:
8430 if (staged_status == GOT_STATUS_DELETE)
8431 break;
8432 if (a->patch_cb) {
8433 int choice = GOT_PATCH_CHOICE_NONE;
8434 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8435 ie->path, NULL, 1, 1);
8436 if (err)
8437 break;
8438 if (choice == GOT_PATCH_CHOICE_NO)
8439 break;
8440 if (choice != GOT_PATCH_CHOICE_YES) {
8441 err = got_error(GOT_ERR_PATCH_CHOICE);
8442 break;
8445 stage = GOT_FILEIDX_STAGE_DELETE;
8446 got_fileindex_entry_stage_set(ie, stage);
8447 a->staged_something = 1;
8448 if (a->status_cb == NULL)
8449 break;
8450 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8451 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8452 de_name);
8453 break;
8454 case GOT_STATUS_NO_CHANGE:
8455 break;
8456 case GOT_STATUS_CONFLICT:
8457 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8458 break;
8459 case GOT_STATUS_NONEXISTENT:
8460 err = got_error_set_errno(ENOENT, relpath);
8461 break;
8462 default:
8463 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8464 break;
8467 if (path_content && unlink(path_content) == -1 && err == NULL)
8468 err = got_error_from_errno2("unlink", path_content);
8469 free(path_content);
8470 free(ondisk_path);
8471 free(new_staged_blob_id);
8472 return err;
8475 const struct got_error *
8476 got_worktree_stage(struct got_worktree *worktree,
8477 struct got_pathlist_head *paths,
8478 got_worktree_status_cb status_cb, void *status_arg,
8479 got_worktree_patch_cb patch_cb, void *patch_arg,
8480 int allow_bad_symlinks, struct got_repository *repo)
8482 const struct got_error *err = NULL, *sync_err, *unlockerr;
8483 struct got_pathlist_entry *pe;
8484 struct got_fileindex *fileindex = NULL;
8485 char *fileindex_path = NULL;
8486 struct got_reference *head_ref = NULL;
8487 struct got_object_id *head_commit_id = NULL;
8488 struct check_stage_ok_arg oka;
8489 struct stage_path_arg spa;
8491 err = lock_worktree(worktree, LOCK_EX);
8492 if (err)
8493 return err;
8495 err = got_ref_open(&head_ref, repo,
8496 got_worktree_get_head_ref_name(worktree), 0);
8497 if (err)
8498 goto done;
8499 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8500 if (err)
8501 goto done;
8502 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8503 if (err)
8504 goto done;
8506 /* Check pre-conditions before staging anything. */
8507 oka.head_commit_id = head_commit_id;
8508 oka.worktree = worktree;
8509 oka.fileindex = fileindex;
8510 oka.repo = repo;
8511 oka.have_changes = 0;
8512 TAILQ_FOREACH(pe, paths, entry) {
8513 err = worktree_status(worktree, pe->path, fileindex, repo,
8514 check_stage_ok, &oka, NULL, NULL, 1, 0);
8515 if (err)
8516 goto done;
8518 if (!oka.have_changes) {
8519 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8520 goto done;
8523 spa.worktree = worktree;
8524 spa.fileindex = fileindex;
8525 spa.repo = repo;
8526 spa.patch_cb = patch_cb;
8527 spa.patch_arg = patch_arg;
8528 spa.status_cb = status_cb;
8529 spa.status_arg = status_arg;
8530 spa.staged_something = 0;
8531 spa.allow_bad_symlinks = allow_bad_symlinks;
8532 TAILQ_FOREACH(pe, paths, entry) {
8533 err = worktree_status(worktree, pe->path, fileindex, repo,
8534 stage_path, &spa, NULL, NULL, 1, 0);
8535 if (err)
8536 goto done;
8538 if (!spa.staged_something) {
8539 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8540 goto done;
8543 sync_err = sync_fileindex(fileindex, fileindex_path);
8544 if (sync_err && err == NULL)
8545 err = sync_err;
8546 done:
8547 if (head_ref)
8548 got_ref_close(head_ref);
8549 free(head_commit_id);
8550 free(fileindex_path);
8551 if (fileindex)
8552 got_fileindex_free(fileindex);
8553 unlockerr = lock_worktree(worktree, LOCK_SH);
8554 if (unlockerr && err == NULL)
8555 err = unlockerr;
8556 return err;
8559 struct unstage_path_arg {
8560 struct got_worktree *worktree;
8561 struct got_fileindex *fileindex;
8562 struct got_repository *repo;
8563 got_worktree_checkout_cb progress_cb;
8564 void *progress_arg;
8565 got_worktree_patch_cb patch_cb;
8566 void *patch_arg;
8569 static const struct got_error *
8570 create_unstaged_content(char **path_unstaged_content,
8571 char **path_new_staged_content, struct got_object_id *blob_id,
8572 struct got_object_id *staged_blob_id, const char *relpath,
8573 struct got_repository *repo,
8574 got_worktree_patch_cb patch_cb, void *patch_arg)
8576 const struct got_error *err, *free_err;
8577 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8578 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8579 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8580 struct got_diffreg_result *diffreg_result = NULL;
8581 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8582 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8583 int fd1 = -1, fd2 = -1;
8585 *path_unstaged_content = NULL;
8586 *path_new_staged_content = NULL;
8588 err = got_object_id_str(&label1, blob_id);
8589 if (err)
8590 return err;
8592 fd1 = got_opentempfd();
8593 if (fd1 == -1) {
8594 err = got_error_from_errno("got_opentempfd");
8595 goto done;
8597 fd2 = got_opentempfd();
8598 if (fd2 == -1) {
8599 err = got_error_from_errno("got_opentempfd");
8600 goto done;
8603 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8604 if (err)
8605 goto done;
8607 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
8608 if (err)
8609 goto done;
8611 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8612 if (err)
8613 goto done;
8615 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8616 fd2);
8617 if (err)
8618 goto done;
8620 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
8621 if (err)
8622 goto done;
8624 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8625 if (err)
8626 goto done;
8628 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8629 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8630 if (err)
8631 goto done;
8633 err = got_opentemp_named(path_unstaged_content, &outfile,
8634 "got-unstaged-content", "");
8635 if (err)
8636 goto done;
8637 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8638 "got-new-staged-content", "");
8639 if (err)
8640 goto done;
8642 if (fseek(f1, 0L, SEEK_SET) == -1) {
8643 err = got_ferror(f1, GOT_ERR_IO);
8644 goto done;
8646 if (fseek(f2, 0L, SEEK_SET) == -1) {
8647 err = got_ferror(f2, GOT_ERR_IO);
8648 goto done;
8650 /* Count the number of actual changes in the diff result. */
8651 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8652 struct diff_chunk_context cc = {};
8653 diff_chunk_context_load_change(&cc, &nchunks_used,
8654 diffreg_result->result, n, 0);
8655 nchanges++;
8657 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8658 int choice;
8659 err = apply_or_reject_change(&choice, &nchunks_used,
8660 diffreg_result->result, n, relpath, f1, f2,
8661 &line_cur1, &line_cur2,
8662 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8663 if (err)
8664 goto done;
8665 if (choice == GOT_PATCH_CHOICE_YES)
8666 have_content = 1;
8667 else
8668 have_rejected_content = 1;
8669 if (choice == GOT_PATCH_CHOICE_QUIT)
8670 break;
8672 if (have_content || have_rejected_content)
8673 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8674 outfile, rejectfile);
8675 done:
8676 free(label1);
8677 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8678 err = got_error_from_errno("close");
8679 if (blob)
8680 got_object_blob_close(blob);
8681 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8682 err = got_error_from_errno("close");
8683 if (staged_blob)
8684 got_object_blob_close(staged_blob);
8685 free_err = got_diffreg_result_free(diffreg_result);
8686 if (free_err && err == NULL)
8687 err = free_err;
8688 if (f1 && fclose(f1) == EOF && err == NULL)
8689 err = got_error_from_errno2("fclose", path1);
8690 if (f2 && fclose(f2) == EOF && err == NULL)
8691 err = got_error_from_errno2("fclose", path2);
8692 if (outfile && fclose(outfile) == EOF && err == NULL)
8693 err = got_error_from_errno2("fclose", *path_unstaged_content);
8694 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8695 err = got_error_from_errno2("fclose", *path_new_staged_content);
8696 if (path1 && unlink(path1) == -1 && err == NULL)
8697 err = got_error_from_errno2("unlink", path1);
8698 if (path2 && unlink(path2) == -1 && err == NULL)
8699 err = got_error_from_errno2("unlink", path2);
8700 if (err || !have_content) {
8701 if (*path_unstaged_content &&
8702 unlink(*path_unstaged_content) == -1 && err == NULL)
8703 err = got_error_from_errno2("unlink",
8704 *path_unstaged_content);
8705 free(*path_unstaged_content);
8706 *path_unstaged_content = NULL;
8708 if (err || !have_content || !have_rejected_content) {
8709 if (*path_new_staged_content &&
8710 unlink(*path_new_staged_content) == -1 && err == NULL)
8711 err = got_error_from_errno2("unlink",
8712 *path_new_staged_content);
8713 free(*path_new_staged_content);
8714 *path_new_staged_content = NULL;
8716 free(path1);
8717 free(path2);
8718 return err;
8721 static const struct got_error *
8722 unstage_hunks(struct got_object_id *staged_blob_id,
8723 struct got_blob_object *blob_base,
8724 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8725 const char *ondisk_path, const char *label_orig,
8726 struct got_worktree *worktree, struct got_repository *repo,
8727 got_worktree_patch_cb patch_cb, void *patch_arg,
8728 got_worktree_checkout_cb progress_cb, void *progress_arg)
8730 const struct got_error *err = NULL;
8731 char *path_unstaged_content = NULL;
8732 char *path_new_staged_content = NULL;
8733 char *parent = NULL, *base_path = NULL;
8734 char *blob_base_path = NULL;
8735 struct got_object_id *new_staged_blob_id = NULL;
8736 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8737 struct stat sb;
8739 err = create_unstaged_content(&path_unstaged_content,
8740 &path_new_staged_content, blob_id, staged_blob_id,
8741 ie->path, repo, patch_cb, patch_arg);
8742 if (err)
8743 return err;
8745 if (path_unstaged_content == NULL)
8746 return NULL;
8748 if (path_new_staged_content) {
8749 err = got_object_blob_create(&new_staged_blob_id,
8750 path_new_staged_content, repo);
8751 if (err)
8752 goto done;
8755 f = fopen(path_unstaged_content, "re");
8756 if (f == NULL) {
8757 err = got_error_from_errno2("fopen",
8758 path_unstaged_content);
8759 goto done;
8761 if (fstat(fileno(f), &sb) == -1) {
8762 err = got_error_from_errno2("fstat", path_unstaged_content);
8763 goto done;
8765 if (got_fileindex_entry_staged_filetype_get(ie) ==
8766 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8767 char link_target[PATH_MAX];
8768 size_t r;
8769 r = fread(link_target, 1, sizeof(link_target), f);
8770 if (r == 0 && ferror(f)) {
8771 err = got_error_from_errno("fread");
8772 goto done;
8774 if (r >= sizeof(link_target)) { /* should not happen */
8775 err = got_error(GOT_ERR_NO_SPACE);
8776 goto done;
8778 link_target[r] = '\0';
8779 err = merge_symlink(worktree, blob_base,
8780 ondisk_path, ie->path, label_orig, link_target,
8781 worktree->base_commit_id, repo, progress_cb,
8782 progress_arg);
8783 } else {
8784 int local_changes_subsumed;
8786 err = got_path_dirname(&parent, ondisk_path);
8787 if (err)
8788 return err;
8790 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8791 parent) == -1) {
8792 err = got_error_from_errno("asprintf");
8793 base_path = NULL;
8794 goto done;
8797 err = got_opentemp_named(&blob_base_path, &f_base,
8798 base_path, "");
8799 if (err)
8800 goto done;
8801 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8802 blob_base);
8803 if (err)
8804 goto done;
8807 * In order the run a 3-way merge with a symlink we copy the symlink's
8808 * target path into a temporary file and use that file with diff3.
8810 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8811 err = dump_symlink_target_path_to_file(&f_deriv2,
8812 ondisk_path);
8813 if (err)
8814 goto done;
8815 } else {
8816 int fd;
8817 fd = open(ondisk_path,
8818 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8819 if (fd == -1) {
8820 err = got_error_from_errno2("open", ondisk_path);
8821 goto done;
8823 f_deriv2 = fdopen(fd, "r");
8824 if (f_deriv2 == NULL) {
8825 err = got_error_from_errno2("fdopen", ondisk_path);
8826 close(fd);
8827 goto done;
8831 err = merge_file(&local_changes_subsumed, worktree,
8832 f_base, f, f_deriv2, ondisk_path, ie->path,
8833 got_fileindex_perms_to_st(ie),
8834 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8835 repo, progress_cb, progress_arg);
8837 if (err)
8838 goto done;
8840 if (new_staged_blob_id) {
8841 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8842 SHA1_DIGEST_LENGTH);
8843 } else {
8844 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8845 got_fileindex_entry_staged_filetype_set(ie, 0);
8847 done:
8848 free(new_staged_blob_id);
8849 if (path_unstaged_content &&
8850 unlink(path_unstaged_content) == -1 && err == NULL)
8851 err = got_error_from_errno2("unlink", path_unstaged_content);
8852 if (path_new_staged_content &&
8853 unlink(path_new_staged_content) == -1 && err == NULL)
8854 err = got_error_from_errno2("unlink", path_new_staged_content);
8855 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8856 err = got_error_from_errno2("unlink", blob_base_path);
8857 if (f_base && fclose(f_base) == EOF && err == NULL)
8858 err = got_error_from_errno2("fclose", path_unstaged_content);
8859 if (f && fclose(f) == EOF && err == NULL)
8860 err = got_error_from_errno2("fclose", path_unstaged_content);
8861 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8862 err = got_error_from_errno2("fclose", ondisk_path);
8863 free(path_unstaged_content);
8864 free(path_new_staged_content);
8865 free(blob_base_path);
8866 free(parent);
8867 free(base_path);
8868 return err;
8871 static const struct got_error *
8872 unstage_path(void *arg, unsigned char status,
8873 unsigned char staged_status, const char *relpath,
8874 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8875 struct got_object_id *commit_id, int dirfd, const char *de_name)
8877 const struct got_error *err = NULL;
8878 struct unstage_path_arg *a = arg;
8879 struct got_fileindex_entry *ie;
8880 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8881 char *ondisk_path = NULL;
8882 char *id_str = NULL, *label_orig = NULL;
8883 int local_changes_subsumed;
8884 struct stat sb;
8885 int fd1 = -1, fd2 = -1;
8887 if (staged_status != GOT_STATUS_ADD &&
8888 staged_status != GOT_STATUS_MODIFY &&
8889 staged_status != GOT_STATUS_DELETE)
8890 return NULL;
8892 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8893 if (ie == NULL)
8894 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8896 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8897 == -1)
8898 return got_error_from_errno("asprintf");
8900 err = got_object_id_str(&id_str,
8901 commit_id ? commit_id : a->worktree->base_commit_id);
8902 if (err)
8903 goto done;
8904 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8905 id_str) == -1) {
8906 err = got_error_from_errno("asprintf");
8907 goto done;
8910 fd1 = got_opentempfd();
8911 if (fd1 == -1) {
8912 err = got_error_from_errno("got_opentempfd");
8913 goto done;
8915 fd2 = got_opentempfd();
8916 if (fd2 == -1) {
8917 err = got_error_from_errno("got_opentempfd");
8918 goto done;
8921 switch (staged_status) {
8922 case GOT_STATUS_MODIFY:
8923 err = got_object_open_as_blob(&blob_base, a->repo,
8924 blob_id, 8192, fd1);
8925 if (err)
8926 break;
8927 /* fall through */
8928 case GOT_STATUS_ADD:
8929 if (a->patch_cb) {
8930 if (staged_status == GOT_STATUS_ADD) {
8931 int choice = GOT_PATCH_CHOICE_NONE;
8932 err = (*a->patch_cb)(&choice, a->patch_arg,
8933 staged_status, ie->path, NULL, 1, 1);
8934 if (err)
8935 break;
8936 if (choice != GOT_PATCH_CHOICE_YES)
8937 break;
8938 } else {
8939 err = unstage_hunks(staged_blob_id,
8940 blob_base, blob_id, ie, ondisk_path,
8941 label_orig, a->worktree, a->repo,
8942 a->patch_cb, a->patch_arg,
8943 a->progress_cb, a->progress_arg);
8944 break; /* Done with this file. */
8947 err = got_object_open_as_blob(&blob_staged, a->repo,
8948 staged_blob_id, 8192, fd2);
8949 if (err)
8950 break;
8951 switch (got_fileindex_entry_staged_filetype_get(ie)) {
8952 case GOT_FILEIDX_MODE_BAD_SYMLINK:
8953 case GOT_FILEIDX_MODE_REGULAR_FILE:
8954 err = merge_blob(&local_changes_subsumed, a->worktree,
8955 blob_base, ondisk_path, relpath,
8956 got_fileindex_perms_to_st(ie), label_orig,
8957 blob_staged, commit_id ? commit_id :
8958 a->worktree->base_commit_id, a->repo,
8959 a->progress_cb, a->progress_arg);
8960 break;
8961 case GOT_FILEIDX_MODE_SYMLINK:
8962 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8963 char *staged_target;
8964 err = got_object_blob_read_to_str(
8965 &staged_target, blob_staged);
8966 if (err)
8967 goto done;
8968 err = merge_symlink(a->worktree, blob_base,
8969 ondisk_path, relpath, label_orig,
8970 staged_target, commit_id ? commit_id :
8971 a->worktree->base_commit_id,
8972 a->repo, a->progress_cb, a->progress_arg);
8973 free(staged_target);
8974 } else {
8975 err = merge_blob(&local_changes_subsumed,
8976 a->worktree, blob_base, ondisk_path,
8977 relpath, got_fileindex_perms_to_st(ie),
8978 label_orig, blob_staged,
8979 commit_id ? commit_id :
8980 a->worktree->base_commit_id, a->repo,
8981 a->progress_cb, a->progress_arg);
8983 break;
8984 default:
8985 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8986 break;
8988 if (err == NULL) {
8989 got_fileindex_entry_stage_set(ie,
8990 GOT_FILEIDX_STAGE_NONE);
8991 got_fileindex_entry_staged_filetype_set(ie, 0);
8993 break;
8994 case GOT_STATUS_DELETE:
8995 if (a->patch_cb) {
8996 int choice = GOT_PATCH_CHOICE_NONE;
8997 err = (*a->patch_cb)(&choice, a->patch_arg,
8998 staged_status, ie->path, NULL, 1, 1);
8999 if (err)
9000 break;
9001 if (choice == GOT_PATCH_CHOICE_NO)
9002 break;
9003 if (choice != GOT_PATCH_CHOICE_YES) {
9004 err = got_error(GOT_ERR_PATCH_CHOICE);
9005 break;
9008 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9009 got_fileindex_entry_staged_filetype_set(ie, 0);
9010 err = get_file_status(&status, &sb, ie, ondisk_path,
9011 dirfd, de_name, a->repo);
9012 if (err)
9013 break;
9014 err = (*a->progress_cb)(a->progress_arg, status, relpath);
9015 break;
9017 done:
9018 free(ondisk_path);
9019 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9020 err = got_error_from_errno("close");
9021 if (blob_base)
9022 got_object_blob_close(blob_base);
9023 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9024 err = got_error_from_errno("close");
9025 if (blob_staged)
9026 got_object_blob_close(blob_staged);
9027 free(id_str);
9028 free(label_orig);
9029 return err;
9032 const struct got_error *
9033 got_worktree_unstage(struct got_worktree *worktree,
9034 struct got_pathlist_head *paths,
9035 got_worktree_checkout_cb progress_cb, void *progress_arg,
9036 got_worktree_patch_cb patch_cb, void *patch_arg,
9037 struct got_repository *repo)
9039 const struct got_error *err = NULL, *sync_err, *unlockerr;
9040 struct got_pathlist_entry *pe;
9041 struct got_fileindex *fileindex = NULL;
9042 char *fileindex_path = NULL;
9043 struct unstage_path_arg upa;
9045 err = lock_worktree(worktree, LOCK_EX);
9046 if (err)
9047 return err;
9049 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9050 if (err)
9051 goto done;
9053 upa.worktree = worktree;
9054 upa.fileindex = fileindex;
9055 upa.repo = repo;
9056 upa.progress_cb = progress_cb;
9057 upa.progress_arg = progress_arg;
9058 upa.patch_cb = patch_cb;
9059 upa.patch_arg = patch_arg;
9060 TAILQ_FOREACH(pe, paths, entry) {
9061 err = worktree_status(worktree, pe->path, fileindex, repo,
9062 unstage_path, &upa, NULL, NULL, 1, 0);
9063 if (err)
9064 goto done;
9067 sync_err = sync_fileindex(fileindex, fileindex_path);
9068 if (sync_err && err == NULL)
9069 err = sync_err;
9070 done:
9071 free(fileindex_path);
9072 if (fileindex)
9073 got_fileindex_free(fileindex);
9074 unlockerr = lock_worktree(worktree, LOCK_SH);
9075 if (unlockerr && err == NULL)
9076 err = unlockerr;
9077 return err;
9080 struct report_file_info_arg {
9081 struct got_worktree *worktree;
9082 got_worktree_path_info_cb info_cb;
9083 void *info_arg;
9084 struct got_pathlist_head *paths;
9085 got_cancel_cb cancel_cb;
9086 void *cancel_arg;
9089 static const struct got_error *
9090 report_file_info(void *arg, struct got_fileindex_entry *ie)
9092 struct report_file_info_arg *a = arg;
9093 struct got_pathlist_entry *pe;
9094 struct got_object_id blob_id, staged_blob_id, commit_id;
9095 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9096 struct got_object_id *commit_idp = NULL;
9097 int stage;
9099 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9100 return got_error(GOT_ERR_CANCELLED);
9102 TAILQ_FOREACH(pe, a->paths, entry) {
9103 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9104 got_path_is_child(ie->path, pe->path, pe->path_len))
9105 break;
9107 if (pe == NULL) /* not found */
9108 return NULL;
9110 if (got_fileindex_entry_has_blob(ie)) {
9111 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
9112 blob_idp = &blob_id;
9114 stage = got_fileindex_entry_stage_get(ie);
9115 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9116 stage == GOT_FILEIDX_STAGE_ADD) {
9117 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
9118 SHA1_DIGEST_LENGTH);
9119 staged_blob_idp = &staged_blob_id;
9122 if (got_fileindex_entry_has_commit(ie)) {
9123 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
9124 commit_idp = &commit_id;
9127 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9128 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9131 const struct got_error *
9132 got_worktree_path_info(struct got_worktree *worktree,
9133 struct got_pathlist_head *paths,
9134 got_worktree_path_info_cb info_cb, void *info_arg,
9135 got_cancel_cb cancel_cb, void *cancel_arg)
9138 const struct got_error *err = NULL, *unlockerr;
9139 struct got_fileindex *fileindex = NULL;
9140 char *fileindex_path = NULL;
9141 struct report_file_info_arg arg;
9143 err = lock_worktree(worktree, LOCK_SH);
9144 if (err)
9145 return err;
9147 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9148 if (err)
9149 goto done;
9151 arg.worktree = worktree;
9152 arg.info_cb = info_cb;
9153 arg.info_arg = info_arg;
9154 arg.paths = paths;
9155 arg.cancel_cb = cancel_cb;
9156 arg.cancel_arg = cancel_arg;
9157 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9158 &arg);
9159 done:
9160 free(fileindex_path);
9161 if (fileindex)
9162 got_fileindex_free(fileindex);
9163 unlockerr = lock_worktree(worktree, LOCK_UN);
9164 if (unlockerr && err == NULL)
9165 err = unlockerr;
9166 return err;
9169 static const struct got_error *
9170 patch_check_path(const char *p, char **path, unsigned char *status,
9171 unsigned char *staged_status, struct got_fileindex *fileindex,
9172 struct got_worktree *worktree, struct got_repository *repo)
9174 const struct got_error *err;
9175 struct got_fileindex_entry *ie;
9176 struct stat sb;
9177 char *ondisk_path = NULL;
9179 err = got_worktree_resolve_path(path, worktree, p);
9180 if (err)
9181 return err;
9183 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9184 *path[0] ? "/" : "", *path) == -1)
9185 return got_error_from_errno("asprintf");
9187 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9188 if (ie) {
9189 *staged_status = get_staged_status(ie);
9190 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9191 repo);
9192 if (err)
9193 goto done;
9194 } else {
9195 *staged_status = GOT_STATUS_NO_CHANGE;
9196 *status = GOT_STATUS_UNVERSIONED;
9197 if (lstat(ondisk_path, &sb) == -1) {
9198 if (errno != ENOENT) {
9199 err = got_error_from_errno2("lstat",
9200 ondisk_path);
9201 goto done;
9203 *status = GOT_STATUS_NONEXISTENT;
9207 done:
9208 free(ondisk_path);
9209 return err;
9212 static const struct got_error *
9213 patch_can_rm(const char *path, unsigned char status,
9214 unsigned char staged_status)
9216 if (status == GOT_STATUS_NONEXISTENT)
9217 return got_error_set_errno(ENOENT, path);
9218 if (status != GOT_STATUS_NO_CHANGE &&
9219 status != GOT_STATUS_ADD &&
9220 status != GOT_STATUS_MODIFY &&
9221 status != GOT_STATUS_MODE_CHANGE)
9222 return got_error_path(path, GOT_ERR_FILE_STATUS);
9223 if (staged_status == GOT_STATUS_DELETE)
9224 return got_error_path(path, GOT_ERR_FILE_STATUS);
9225 return NULL;
9228 static const struct got_error *
9229 patch_can_add(const char *path, unsigned char status)
9231 if (status != GOT_STATUS_NONEXISTENT)
9232 return got_error_path(path, GOT_ERR_FILE_STATUS);
9233 return NULL;
9236 static const struct got_error *
9237 patch_can_edit(const char *path, unsigned char status,
9238 unsigned char staged_status)
9240 if (status == GOT_STATUS_NONEXISTENT)
9241 return got_error_set_errno(ENOENT, path);
9242 if (status != GOT_STATUS_NO_CHANGE &&
9243 status != GOT_STATUS_ADD &&
9244 status != GOT_STATUS_MODIFY)
9245 return got_error_path(path, GOT_ERR_FILE_STATUS);
9246 if (staged_status == GOT_STATUS_DELETE)
9247 return got_error_path(path, GOT_ERR_FILE_STATUS);
9248 return NULL;
9251 const struct got_error *
9252 got_worktree_patch_prepare(struct got_fileindex **fileindex,
9253 char **fileindex_path, struct got_worktree *worktree)
9255 return open_fileindex(fileindex, fileindex_path, worktree);
9258 const struct got_error *
9259 got_worktree_patch_check_path(const char *old, const char *new,
9260 char **oldpath, char **newpath, struct got_worktree *worktree,
9261 struct got_repository *repo, struct got_fileindex *fileindex)
9263 const struct got_error *err = NULL;
9264 int file_renamed = 0;
9265 unsigned char status_old, staged_status_old;
9266 unsigned char status_new, staged_status_new;
9268 *oldpath = NULL;
9269 *newpath = NULL;
9271 err = patch_check_path(old != NULL ? old : new, oldpath,
9272 &status_old, &staged_status_old, fileindex, worktree, repo);
9273 if (err)
9274 goto done;
9276 err = patch_check_path(new != NULL ? new : old, newpath,
9277 &status_new, &staged_status_new, fileindex, worktree, repo);
9278 if (err)
9279 goto done;
9281 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9282 file_renamed = 1;
9284 if (old != NULL && new == NULL)
9285 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9286 else if (file_renamed) {
9287 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9288 if (err == NULL)
9289 err = patch_can_add(*newpath, status_new);
9290 } else if (old == NULL)
9291 err = patch_can_add(*newpath, status_new);
9292 else
9293 err = patch_can_edit(*newpath, status_new, staged_status_new);
9295 done:
9296 if (err) {
9297 free(*oldpath);
9298 *oldpath = NULL;
9299 free(*newpath);
9300 *newpath = NULL;
9302 return err;
9305 const struct got_error *
9306 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9307 struct got_worktree *worktree, struct got_fileindex *fileindex,
9308 got_worktree_checkout_cb progress_cb, void *progress_arg)
9310 struct schedule_addition_args saa;
9312 memset(&saa, 0, sizeof(saa));
9313 saa.worktree = worktree;
9314 saa.fileindex = fileindex;
9315 saa.progress_cb = progress_cb;
9316 saa.progress_arg = progress_arg;
9317 saa.repo = repo;
9319 return worktree_status(worktree, path, fileindex, repo,
9320 schedule_addition, &saa, NULL, NULL, 1, 0);
9323 const struct got_error *
9324 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9325 struct got_worktree *worktree, struct got_fileindex *fileindex,
9326 got_worktree_delete_cb progress_cb, void *progress_arg)
9328 struct schedule_deletion_args sda;
9330 memset(&sda, 0, sizeof(sda));
9331 sda.worktree = worktree;
9332 sda.fileindex = fileindex;
9333 sda.progress_cb = progress_cb;
9334 sda.progress_arg = progress_arg;
9335 sda.repo = repo;
9336 sda.delete_local_mods = 0;
9337 sda.keep_on_disk = 0;
9338 sda.ignore_missing_paths = 0;
9339 sda.status_codes = NULL;
9341 return worktree_status(worktree, path, fileindex, repo,
9342 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9345 const struct got_error *
9346 got_worktree_patch_complete(struct got_fileindex *fileindex,
9347 const char *fileindex_path)
9349 const struct got_error *err = NULL;
9351 err = sync_fileindex(fileindex, fileindex_path);
9352 got_fileindex_free(fileindex);
9354 return err;