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 static const struct got_error *skip_one_line(FILE *);
1517 * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
1518 * conflict marker is found in newly added lines only.
1520 static const struct got_error *
1521 get_modified_file_content_status(unsigned char *status,
1522 struct got_blob_object *blob, const char *path, struct stat *sb,
1523 FILE *ondisk_file)
1525 const struct got_error *err, *free_err;
1526 const char *markers[3] = {
1527 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1528 GOT_DIFF_CONFLICT_MARKER_SEP,
1529 GOT_DIFF_CONFLICT_MARKER_END
1531 FILE *f1 = NULL;
1532 struct got_diffreg_result *diffreg_result = NULL;
1533 struct diff_result *r;
1534 int nchunks_parsed, n, i = 0, ln = 0;
1535 char *line = NULL;
1536 size_t linesize = 0;
1537 ssize_t linelen;
1539 if (*status != GOT_STATUS_MODIFY)
1540 return NULL;
1542 f1 = got_opentemp();
1543 if (f1 == NULL)
1544 return got_error_from_errno("got_opentemp");
1546 if (blob) {
1547 got_object_blob_rewind(blob);
1548 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
1549 if (err)
1550 goto done;
1553 err = got_diff_files(&diffreg_result, f1, 1, NULL, ondisk_file,
1554 1, NULL, 0, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
1555 if (err)
1556 goto done;
1558 if (fseek(ondisk_file, 0L, SEEK_SET) == -1) {
1559 err = got_ferror(ondisk_file, GOT_ERR_IO);
1560 goto done;
1563 r = diffreg_result->result;
1565 for (n = 0; n < r->chunks.len; n += nchunks_parsed) {
1566 struct diff_chunk *c;
1567 struct diff_chunk_context cc = {};
1568 int clc, crc;
1571 * We can optimise a little by advancing straight
1572 * to the next chunk if this one has no added lines.
1574 c = diff_chunk_get(r, n);
1575 clc = diff_chunk_get_left_count(c);
1576 crc = diff_chunk_get_right_count(c);
1578 if (!crc && clc) {
1579 nchunks_parsed = 1;
1580 continue; /* removed lines */
1583 diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
1585 while (ln < cc.right.start) {
1586 err = skip_one_line(ondisk_file);
1587 if (err)
1588 goto done;
1589 ++ln;
1592 while (ln < cc.right.end) {
1593 linelen = getline(&line, &linesize, ondisk_file);
1594 if (linelen == -1) {
1595 if (feof(ondisk_file))
1596 break;
1597 err = got_ferror(ondisk_file, GOT_ERR_IO);
1598 break;
1601 if (line && strncmp(line, markers[i],
1602 strlen(markers[i])) == 0) {
1603 if (strcmp(markers[i],
1604 GOT_DIFF_CONFLICT_MARKER_END) == 0) {
1605 *status = GOT_STATUS_CONFLICT;
1606 goto done;
1607 } else
1608 i++;
1610 ++ln;
1614 done:
1615 free(line);
1616 if (f1 != NULL && fclose(f1) == EOF && err == NULL)
1617 err = got_error_from_errno("fclose");
1618 free_err = got_diffreg_result_free(diffreg_result);
1619 if (err == NULL)
1620 err = free_err;
1622 return err;
1625 static int
1626 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1628 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1629 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1632 static int
1633 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1635 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1636 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1637 ie->mtime_sec == sb->st_mtim.tv_sec &&
1638 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1639 ie->size == (sb->st_size & 0xffffffff) &&
1640 !xbit_differs(ie, sb->st_mode));
1643 static unsigned char
1644 get_staged_status(struct got_fileindex_entry *ie)
1646 switch (got_fileindex_entry_stage_get(ie)) {
1647 case GOT_FILEIDX_STAGE_ADD:
1648 return GOT_STATUS_ADD;
1649 case GOT_FILEIDX_STAGE_DELETE:
1650 return GOT_STATUS_DELETE;
1651 case GOT_FILEIDX_STAGE_MODIFY:
1652 return GOT_STATUS_MODIFY;
1653 default:
1654 return GOT_STATUS_NO_CHANGE;
1658 static const struct got_error *
1659 get_symlink_modification_status(unsigned char *status,
1660 struct got_fileindex_entry *ie, const char *abspath,
1661 int dirfd, const char *de_name, struct got_blob_object *blob)
1663 const struct got_error *err = NULL;
1664 char target_path[PATH_MAX];
1665 char etarget[PATH_MAX];
1666 ssize_t elen;
1667 size_t len, target_len = 0;
1668 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1669 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1671 *status = GOT_STATUS_NO_CHANGE;
1673 /* Blob object content specifies the target path of the link. */
1674 do {
1675 err = got_object_blob_read_block(&len, blob);
1676 if (err)
1677 return err;
1678 if (len + target_len >= sizeof(target_path)) {
1680 * Should not happen. The blob contents were OK
1681 * when this symlink was installed.
1683 return got_error(GOT_ERR_NO_SPACE);
1685 if (len > 0) {
1686 /* Skip blob object header first time around. */
1687 memcpy(target_path + target_len, buf + hdrlen,
1688 len - hdrlen);
1689 target_len += len - hdrlen;
1690 hdrlen = 0;
1692 } while (len != 0);
1693 target_path[target_len] = '\0';
1695 if (dirfd != -1) {
1696 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1697 if (elen == -1)
1698 return got_error_from_errno2("readlinkat", abspath);
1699 } else {
1700 elen = readlink(abspath, etarget, sizeof(etarget));
1701 if (elen == -1)
1702 return got_error_from_errno2("readlink", abspath);
1705 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1706 *status = GOT_STATUS_MODIFY;
1708 return NULL;
1711 static const struct got_error *
1712 get_file_status(unsigned char *status, struct stat *sb,
1713 struct got_fileindex_entry *ie, const char *abspath,
1714 int dirfd, const char *de_name, struct got_repository *repo)
1716 const struct got_error *err = NULL;
1717 struct got_object_id id;
1718 size_t hdrlen;
1719 int fd = -1, fd1 = -1;
1720 FILE *f = NULL;
1721 uint8_t fbuf[8192];
1722 struct got_blob_object *blob = NULL;
1723 size_t flen, blen;
1724 unsigned char staged_status;
1726 staged_status = get_staged_status(ie);
1727 *status = GOT_STATUS_NO_CHANGE;
1728 memset(sb, 0, sizeof(*sb));
1731 * Whenever the caller provides a directory descriptor and a
1732 * directory entry name for the file, use them! This prevents
1733 * race conditions if filesystem paths change beneath our feet.
1735 if (dirfd != -1) {
1736 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1737 if (errno == ENOENT) {
1738 if (got_fileindex_entry_has_file_on_disk(ie))
1739 *status = GOT_STATUS_MISSING;
1740 else
1741 *status = GOT_STATUS_DELETE;
1742 goto done;
1744 err = got_error_from_errno2("fstatat", abspath);
1745 goto done;
1747 } else {
1748 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1749 if (fd == -1 && errno != ENOENT &&
1750 !got_err_open_nofollow_on_symlink())
1751 return got_error_from_errno2("open", abspath);
1752 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1753 if (lstat(abspath, sb) == -1)
1754 return got_error_from_errno2("lstat", abspath);
1755 } else if (fd == -1 || fstat(fd, sb) == -1) {
1756 if (errno == ENOENT) {
1757 if (got_fileindex_entry_has_file_on_disk(ie))
1758 *status = GOT_STATUS_MISSING;
1759 else
1760 *status = GOT_STATUS_DELETE;
1761 goto done;
1763 err = got_error_from_errno2("fstat", abspath);
1764 goto done;
1768 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1769 *status = GOT_STATUS_OBSTRUCTED;
1770 goto done;
1773 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1774 *status = GOT_STATUS_DELETE;
1775 goto done;
1776 } else if (!got_fileindex_entry_has_blob(ie) &&
1777 staged_status != GOT_STATUS_ADD) {
1778 *status = GOT_STATUS_ADD;
1779 goto done;
1782 if (!stat_info_differs(ie, sb))
1783 goto done;
1785 if (S_ISLNK(sb->st_mode) &&
1786 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1787 *status = GOT_STATUS_MODIFY;
1788 goto done;
1791 if (staged_status == GOT_STATUS_MODIFY ||
1792 staged_status == GOT_STATUS_ADD)
1793 got_fileindex_entry_get_staged_blob_id(&id, ie);
1794 else
1795 got_fileindex_entry_get_blob_id(&id, ie);
1797 fd1 = got_opentempfd();
1798 if (fd1 == -1) {
1799 err = got_error_from_errno("got_opentempfd");
1800 goto done;
1802 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1803 if (err)
1804 goto done;
1806 if (S_ISLNK(sb->st_mode)) {
1807 err = get_symlink_modification_status(status, ie,
1808 abspath, dirfd, de_name, blob);
1809 goto done;
1812 if (dirfd != -1) {
1813 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1814 if (fd == -1) {
1815 err = got_error_from_errno2("openat", abspath);
1816 goto done;
1820 f = fdopen(fd, "r");
1821 if (f == NULL) {
1822 err = got_error_from_errno2("fdopen", abspath);
1823 goto done;
1825 fd = -1;
1826 hdrlen = got_object_blob_get_hdrlen(blob);
1827 for (;;) {
1828 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1829 err = got_object_blob_read_block(&blen, blob);
1830 if (err)
1831 goto done;
1832 /* Skip length of blob object header first time around. */
1833 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1834 if (flen == 0 && ferror(f)) {
1835 err = got_error_from_errno("fread");
1836 goto done;
1838 if (blen - hdrlen == 0) {
1839 if (flen != 0)
1840 *status = GOT_STATUS_MODIFY;
1841 break;
1842 } else if (flen == 0) {
1843 if (blen - hdrlen != 0)
1844 *status = GOT_STATUS_MODIFY;
1845 break;
1846 } else if (blen - hdrlen == flen) {
1847 /* Skip blob object header first time around. */
1848 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1849 *status = GOT_STATUS_MODIFY;
1850 break;
1852 } else {
1853 *status = GOT_STATUS_MODIFY;
1854 break;
1856 hdrlen = 0;
1859 if (*status == GOT_STATUS_MODIFY) {
1860 rewind(f);
1861 err = get_modified_file_content_status(status, blob, ie->path,
1862 sb, f);
1863 } else if (xbit_differs(ie, sb->st_mode))
1864 *status = GOT_STATUS_MODE_CHANGE;
1865 done:
1866 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1867 err = got_error_from_errno("close");
1868 if (blob)
1869 got_object_blob_close(blob);
1870 if (f != NULL && fclose(f) == EOF && err == NULL)
1871 err = got_error_from_errno2("fclose", abspath);
1872 if (fd != -1 && close(fd) == -1 && err == NULL)
1873 err = got_error_from_errno2("close", abspath);
1874 return err;
1878 * Update timestamps in the file index if a file is unmodified and
1879 * we had to run a full content comparison to find out.
1881 static const struct got_error *
1882 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1883 struct got_fileindex_entry *ie, struct stat *sb)
1885 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1886 return got_fileindex_entry_update(ie, wt_fd, path,
1887 ie->blob_sha1, ie->commit_sha1, 1);
1889 return NULL;
1892 static const struct got_error *
1893 update_blob(struct got_worktree *worktree,
1894 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1895 struct got_tree_entry *te, const char *path,
1896 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1897 void *progress_arg)
1899 const struct got_error *err = NULL;
1900 struct got_blob_object *blob = NULL;
1901 char *ondisk_path = NULL;
1902 unsigned char status = GOT_STATUS_NO_CHANGE;
1903 struct stat sb;
1904 int fd1 = -1, fd2 = -1;
1906 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1907 return got_error_from_errno("asprintf");
1909 if (ie) {
1910 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1911 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1912 goto done;
1914 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1915 repo);
1916 if (err)
1917 goto done;
1918 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1919 sb.st_mode = got_fileindex_perms_to_st(ie);
1920 } else {
1921 if (stat(ondisk_path, &sb) == -1) {
1922 if (errno != ENOENT) {
1923 err = got_error_from_errno2("stat",
1924 ondisk_path);
1925 goto done;
1927 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1928 status = GOT_STATUS_UNVERSIONED;
1929 } else {
1930 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1931 status = GOT_STATUS_UNVERSIONED;
1932 else
1933 status = GOT_STATUS_OBSTRUCTED;
1937 if (status == GOT_STATUS_OBSTRUCTED) {
1938 if (ie)
1939 got_fileindex_entry_mark_skipped(ie);
1940 err = (*progress_cb)(progress_arg, status, path);
1941 goto done;
1943 if (status == GOT_STATUS_CONFLICT) {
1944 if (ie)
1945 got_fileindex_entry_mark_skipped(ie);
1946 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1947 path);
1948 goto done;
1951 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1952 (S_ISLNK(te->mode) ||
1953 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1955 * This is a regular file or an installed bad symlink.
1956 * If the file index indicates that this file is already
1957 * up-to-date with respect to the repository we can skip
1958 * updating contents of this file.
1960 if (got_fileindex_entry_has_commit(ie) &&
1961 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1962 SHA1_DIGEST_LENGTH) == 0) {
1963 /* Same commit. */
1964 err = sync_timestamps(worktree->root_fd,
1965 path, status, ie, &sb);
1966 if (err)
1967 goto done;
1968 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1969 path);
1970 goto done;
1972 if (got_fileindex_entry_has_blob(ie) &&
1973 memcmp(ie->blob_sha1, te->id.sha1,
1974 SHA1_DIGEST_LENGTH) == 0) {
1975 /* Different commit but the same blob. */
1976 err = sync_timestamps(worktree->root_fd,
1977 path, status, ie, &sb);
1978 if (err)
1979 goto done;
1980 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1981 path);
1982 goto done;
1986 fd1 = got_opentempfd();
1987 if (fd1 == -1) {
1988 err = got_error_from_errno("got_opentempfd");
1989 goto done;
1991 err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
1992 if (err)
1993 goto done;
1995 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1996 int update_timestamps;
1997 struct got_blob_object *blob2 = NULL;
1998 char *label_orig = NULL;
1999 if (got_fileindex_entry_has_blob(ie)) {
2000 fd2 = got_opentempfd();
2001 if (fd2 == -1) {
2002 err = got_error_from_errno("got_opentempfd");
2003 goto done;
2005 struct got_object_id id2;
2006 got_fileindex_entry_get_blob_id(&id2, ie);
2007 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
2008 fd2);
2009 if (err)
2010 goto done;
2012 if (got_fileindex_entry_has_commit(ie)) {
2013 char id_str[SHA1_DIGEST_STRING_LENGTH];
2014 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2015 sizeof(id_str)) == NULL) {
2016 err = got_error_path(id_str,
2017 GOT_ERR_BAD_OBJ_ID_STR);
2018 goto done;
2020 if (asprintf(&label_orig, "%s: commit %s",
2021 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2022 err = got_error_from_errno("asprintf");
2023 goto done;
2026 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2027 char *link_target;
2028 err = got_object_blob_read_to_str(&link_target, blob);
2029 if (err)
2030 goto done;
2031 err = merge_symlink(worktree, blob2, ondisk_path, path,
2032 label_orig, link_target, worktree->base_commit_id,
2033 repo, progress_cb, progress_arg);
2034 free(link_target);
2035 } else {
2036 err = merge_blob(&update_timestamps, worktree, blob2,
2037 ondisk_path, path, sb.st_mode, label_orig, blob,
2038 worktree->base_commit_id, repo,
2039 progress_cb, progress_arg);
2041 free(label_orig);
2042 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2043 err = got_error_from_errno("close");
2044 goto done;
2046 if (blob2)
2047 got_object_blob_close(blob2);
2048 if (err)
2049 goto done;
2051 * Do not update timestamps of files with local changes.
2052 * Otherwise, a future status walk would treat them as
2053 * unmodified files again.
2055 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2056 blob->id.sha1, worktree->base_commit_id->sha1,
2057 update_timestamps);
2058 } else if (status == GOT_STATUS_MODE_CHANGE) {
2059 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2060 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2061 } else if (status == GOT_STATUS_DELETE) {
2062 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2063 if (err)
2064 goto done;
2065 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2066 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2067 if (err)
2068 goto done;
2069 } else {
2070 int is_bad_symlink = 0;
2071 if (S_ISLNK(te->mode)) {
2072 err = install_symlink(&is_bad_symlink, worktree,
2073 ondisk_path, path, blob,
2074 status == GOT_STATUS_MISSING, 0,
2075 status == GOT_STATUS_UNVERSIONED, 0,
2076 repo, progress_cb, progress_arg);
2077 } else {
2078 err = install_blob(worktree, ondisk_path, path,
2079 te->mode, sb.st_mode, blob,
2080 status == GOT_STATUS_MISSING, 0, 0,
2081 status == GOT_STATUS_UNVERSIONED, repo,
2082 progress_cb, progress_arg);
2084 if (err)
2085 goto done;
2087 if (ie) {
2088 err = got_fileindex_entry_update(ie,
2089 worktree->root_fd, path, blob->id.sha1,
2090 worktree->base_commit_id->sha1, 1);
2091 } else {
2092 err = create_fileindex_entry(&ie, fileindex,
2093 worktree->base_commit_id, worktree->root_fd, path,
2094 &blob->id);
2096 if (err)
2097 goto done;
2099 if (is_bad_symlink) {
2100 got_fileindex_entry_filetype_set(ie,
2101 GOT_FILEIDX_MODE_BAD_SYMLINK);
2105 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2106 err = got_error_from_errno("close");
2107 goto done;
2109 got_object_blob_close(blob);
2110 done:
2111 free(ondisk_path);
2112 return err;
2115 static const struct got_error *
2116 remove_ondisk_file(const char *root_path, const char *path)
2118 const struct got_error *err = NULL;
2119 char *ondisk_path = NULL, *parent = NULL;
2121 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2122 return got_error_from_errno("asprintf");
2124 if (unlink(ondisk_path) == -1) {
2125 if (errno != ENOENT)
2126 err = got_error_from_errno2("unlink", ondisk_path);
2127 } else {
2128 size_t root_len = strlen(root_path);
2129 err = got_path_dirname(&parent, ondisk_path);
2130 if (err)
2131 goto done;
2132 while (got_path_cmp(parent, root_path,
2133 strlen(parent), root_len) != 0) {
2134 free(ondisk_path);
2135 ondisk_path = parent;
2136 parent = NULL;
2137 if (rmdir(ondisk_path) == -1) {
2138 if (errno != ENOTEMPTY)
2139 err = got_error_from_errno2("rmdir",
2140 ondisk_path);
2141 break;
2143 err = got_path_dirname(&parent, ondisk_path);
2144 if (err)
2145 break;
2148 done:
2149 free(ondisk_path);
2150 free(parent);
2151 return err;
2154 static const struct got_error *
2155 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2156 struct got_fileindex_entry *ie, struct got_repository *repo,
2157 got_worktree_checkout_cb progress_cb, void *progress_arg)
2159 const struct got_error *err = NULL;
2160 unsigned char status;
2161 struct stat sb;
2162 char *ondisk_path;
2164 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2165 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2167 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2168 == -1)
2169 return got_error_from_errno("asprintf");
2171 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2172 if (err)
2173 goto done;
2175 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2176 char ondisk_target[PATH_MAX];
2177 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2178 sizeof(ondisk_target));
2179 if (ondisk_len == -1) {
2180 err = got_error_from_errno2("readlink", ondisk_path);
2181 goto done;
2183 ondisk_target[ondisk_len] = '\0';
2184 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2185 NULL, NULL, /* XXX pass common ancestor info? */
2186 ondisk_target, ondisk_path);
2187 if (err)
2188 goto done;
2189 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2190 ie->path);
2191 goto done;
2194 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2195 status == GOT_STATUS_ADD) {
2196 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2197 if (err)
2198 goto done;
2200 * Preserve the working file and change the deleted blob's
2201 * entry into a schedule-add entry.
2203 err = got_fileindex_entry_update(ie, worktree->root_fd,
2204 ie->path, NULL, NULL, 0);
2205 } else {
2206 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2207 if (err)
2208 goto done;
2209 if (status == GOT_STATUS_NO_CHANGE) {
2210 err = remove_ondisk_file(worktree->root_path, ie->path);
2211 if (err)
2212 goto done;
2214 got_fileindex_entry_remove(fileindex, ie);
2216 done:
2217 free(ondisk_path);
2218 return err;
2221 struct diff_cb_arg {
2222 struct got_fileindex *fileindex;
2223 struct got_worktree *worktree;
2224 struct got_repository *repo;
2225 got_worktree_checkout_cb progress_cb;
2226 void *progress_arg;
2227 got_cancel_cb cancel_cb;
2228 void *cancel_arg;
2231 static const struct got_error *
2232 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2233 struct got_tree_entry *te, const char *parent_path)
2235 struct diff_cb_arg *a = arg;
2237 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2238 return got_error(GOT_ERR_CANCELLED);
2240 return update_blob(a->worktree, a->fileindex, ie, te,
2241 ie->path, a->repo, a->progress_cb, a->progress_arg);
2244 static const struct got_error *
2245 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2247 struct diff_cb_arg *a = arg;
2249 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2250 return got_error(GOT_ERR_CANCELLED);
2252 return delete_blob(a->worktree, a->fileindex, ie,
2253 a->repo, a->progress_cb, a->progress_arg);
2256 static const struct got_error *
2257 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2259 struct diff_cb_arg *a = arg;
2260 const struct got_error *err;
2261 char *path;
2263 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2264 return got_error(GOT_ERR_CANCELLED);
2266 if (got_object_tree_entry_is_submodule(te))
2267 return NULL;
2269 if (asprintf(&path, "%s%s%s", parent_path,
2270 parent_path[0] ? "/" : "", te->name)
2271 == -1)
2272 return got_error_from_errno("asprintf");
2274 if (S_ISDIR(te->mode))
2275 err = add_dir_on_disk(a->worktree, path);
2276 else
2277 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2278 a->repo, a->progress_cb, a->progress_arg);
2280 free(path);
2281 return err;
2284 const struct got_error *
2285 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2287 uint32_t uuid_status;
2289 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2290 if (uuid_status != uuid_s_ok) {
2291 *uuidstr = NULL;
2292 return got_error_uuid(uuid_status, "uuid_to_string");
2295 return NULL;
2298 static const struct got_error *
2299 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2301 const struct got_error *err = NULL;
2302 char *uuidstr = NULL;
2304 *refname = NULL;
2306 err = got_worktree_get_uuid(&uuidstr, worktree);
2307 if (err)
2308 return err;
2310 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2311 err = got_error_from_errno("asprintf");
2312 *refname = NULL;
2314 free(uuidstr);
2315 return err;
2318 const struct got_error *
2319 got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2320 const char *prefix)
2322 return get_ref_name(refname, worktree, prefix);
2325 const struct got_error *
2326 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2328 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2331 static const struct got_error *
2332 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2334 return get_ref_name(refname, worktree,
2335 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2338 static const struct got_error *
2339 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2341 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2344 static const struct got_error *
2345 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2347 return get_ref_name(refname, worktree,
2348 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2351 static const struct got_error *
2352 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2354 return get_ref_name(refname, worktree,
2355 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2358 static const struct got_error *
2359 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2361 return get_ref_name(refname, worktree,
2362 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2365 static const struct got_error *
2366 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2368 return get_ref_name(refname, worktree,
2369 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2372 static const struct got_error *
2373 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2375 return get_ref_name(refname, worktree,
2376 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2379 static const struct got_error *
2380 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2382 return get_ref_name(refname, worktree,
2383 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2386 const struct got_error *
2387 got_worktree_get_histedit_script_path(char **path,
2388 struct got_worktree *worktree)
2390 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2391 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2392 *path = NULL;
2393 return got_error_from_errno("asprintf");
2395 return NULL;
2398 static const struct got_error *
2399 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2401 return get_ref_name(refname, worktree,
2402 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2405 static const struct got_error *
2406 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2408 return get_ref_name(refname, worktree,
2409 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2413 * Prevent Git's garbage collector from deleting our base commit by
2414 * setting a reference to our base commit's ID.
2416 static const struct got_error *
2417 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2419 const struct got_error *err = NULL;
2420 struct got_reference *ref = NULL;
2421 char *refname;
2423 err = got_worktree_get_base_ref_name(&refname, worktree);
2424 if (err)
2425 return err;
2427 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2428 if (err)
2429 goto done;
2431 err = got_ref_write(ref, repo);
2432 done:
2433 free(refname);
2434 if (ref)
2435 got_ref_close(ref);
2436 return err;
2439 static const struct got_error *
2440 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2442 const struct got_error *err = NULL;
2444 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2445 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2446 err = got_error_from_errno("asprintf");
2447 *fileindex_path = NULL;
2449 return err;
2453 static const struct got_error *
2454 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2455 struct got_worktree *worktree)
2457 const struct got_error *err = NULL;
2458 FILE *index = NULL;
2460 *fileindex_path = NULL;
2461 *fileindex = got_fileindex_alloc();
2462 if (*fileindex == NULL)
2463 return got_error_from_errno("got_fileindex_alloc");
2465 err = get_fileindex_path(fileindex_path, worktree);
2466 if (err)
2467 goto done;
2469 index = fopen(*fileindex_path, "rbe");
2470 if (index == NULL) {
2471 if (errno != ENOENT)
2472 err = got_error_from_errno2("fopen", *fileindex_path);
2473 } else {
2474 err = got_fileindex_read(*fileindex, index);
2475 if (fclose(index) == EOF && err == NULL)
2476 err = got_error_from_errno("fclose");
2478 done:
2479 if (err) {
2480 free(*fileindex_path);
2481 *fileindex_path = NULL;
2482 got_fileindex_free(*fileindex);
2483 *fileindex = NULL;
2485 return err;
2488 struct bump_base_commit_id_arg {
2489 struct got_object_id *base_commit_id;
2490 const char *path;
2491 size_t path_len;
2492 const char *entry_name;
2493 got_worktree_checkout_cb progress_cb;
2494 void *progress_arg;
2497 static const struct got_error *
2498 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2500 const struct got_error *err;
2501 struct bump_base_commit_id_arg *a = arg;
2503 if (a->entry_name) {
2504 if (strcmp(ie->path, a->path) != 0)
2505 return NULL;
2506 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2507 return NULL;
2509 if (got_fileindex_entry_was_skipped(ie))
2510 return NULL;
2512 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2513 SHA1_DIGEST_LENGTH) == 0)
2514 return NULL;
2516 if (a->progress_cb) {
2517 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2518 ie->path);
2519 if (err)
2520 return err;
2522 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2523 return NULL;
2526 /* Bump base commit ID of all files within an updated part of the work tree. */
2527 static const struct got_error *
2528 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2529 struct got_fileindex *fileindex,
2530 got_worktree_checkout_cb progress_cb, void *progress_arg)
2532 struct bump_base_commit_id_arg bbc_arg;
2534 bbc_arg.base_commit_id = worktree->base_commit_id;
2535 bbc_arg.entry_name = NULL;
2536 bbc_arg.path = "";
2537 bbc_arg.path_len = 0;
2538 bbc_arg.progress_cb = progress_cb;
2539 bbc_arg.progress_arg = progress_arg;
2541 return got_fileindex_for_each_entry_safe(fileindex,
2542 bump_base_commit_id, &bbc_arg);
2545 static const struct got_error *
2546 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2548 const struct got_error *err = NULL;
2549 char *new_fileindex_path = NULL;
2550 FILE *new_index = NULL;
2551 struct timespec timeout;
2553 err = got_opentemp_named(&new_fileindex_path, &new_index,
2554 fileindex_path, "");
2555 if (err)
2556 goto done;
2558 err = got_fileindex_write(fileindex, new_index);
2559 if (err)
2560 goto done;
2562 if (rename(new_fileindex_path, fileindex_path) != 0) {
2563 err = got_error_from_errno3("rename", new_fileindex_path,
2564 fileindex_path);
2565 unlink(new_fileindex_path);
2569 * Sleep for a short amount of time to ensure that files modified after
2570 * this program exits have a different time stamp from the one which
2571 * was recorded in the file index.
2573 timeout.tv_sec = 0;
2574 timeout.tv_nsec = 1;
2575 nanosleep(&timeout, NULL);
2576 done:
2577 if (new_index)
2578 fclose(new_index);
2579 free(new_fileindex_path);
2580 return err;
2583 static const struct got_error *
2584 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2585 struct got_object_id **tree_id, const char *wt_relpath,
2586 struct got_commit_object *base_commit, struct got_worktree *worktree,
2587 struct got_repository *repo)
2589 const struct got_error *err = NULL;
2590 struct got_object_id *id = NULL;
2591 char *in_repo_path = NULL;
2592 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2594 *entry_type = GOT_OBJ_TYPE_ANY;
2595 *tree_relpath = NULL;
2596 *tree_id = NULL;
2598 if (wt_relpath[0] == '\0') {
2599 /* Check out all files within the work tree. */
2600 *entry_type = GOT_OBJ_TYPE_TREE;
2601 *tree_relpath = strdup("");
2602 if (*tree_relpath == NULL) {
2603 err = got_error_from_errno("strdup");
2604 goto done;
2606 err = got_object_id_by_path(tree_id, repo, base_commit,
2607 worktree->path_prefix);
2608 if (err)
2609 goto done;
2610 return NULL;
2613 /* Check out a subset of files in the work tree. */
2615 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2616 is_root_wt ? "" : "/", wt_relpath) == -1) {
2617 err = got_error_from_errno("asprintf");
2618 goto done;
2621 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2622 if (err)
2623 goto done;
2625 free(in_repo_path);
2626 in_repo_path = NULL;
2628 err = got_object_get_type(entry_type, repo, id);
2629 if (err)
2630 goto done;
2632 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2633 /* Check out a single file. */
2634 if (strchr(wt_relpath, '/') == NULL) {
2635 /* Check out a single file in work tree's root dir. */
2636 in_repo_path = strdup(worktree->path_prefix);
2637 if (in_repo_path == NULL) {
2638 err = got_error_from_errno("strdup");
2639 goto done;
2641 *tree_relpath = strdup("");
2642 if (*tree_relpath == NULL) {
2643 err = got_error_from_errno("strdup");
2644 goto done;
2646 } else {
2647 /* Check out a single file in a subdirectory. */
2648 err = got_path_dirname(tree_relpath, wt_relpath);
2649 if (err)
2650 return err;
2651 if (asprintf(&in_repo_path, "%s%s%s",
2652 worktree->path_prefix, is_root_wt ? "" : "/",
2653 *tree_relpath) == -1) {
2654 err = got_error_from_errno("asprintf");
2655 goto done;
2658 err = got_object_id_by_path(tree_id, repo,
2659 base_commit, in_repo_path);
2660 } else {
2661 /* Check out all files within a subdirectory. */
2662 *tree_id = got_object_id_dup(id);
2663 if (*tree_id == NULL) {
2664 err = got_error_from_errno("got_object_id_dup");
2665 goto done;
2667 *tree_relpath = strdup(wt_relpath);
2668 if (*tree_relpath == NULL) {
2669 err = got_error_from_errno("strdup");
2670 goto done;
2673 done:
2674 free(id);
2675 free(in_repo_path);
2676 if (err) {
2677 *entry_type = GOT_OBJ_TYPE_ANY;
2678 free(*tree_relpath);
2679 *tree_relpath = NULL;
2680 free(*tree_id);
2681 *tree_id = NULL;
2683 return err;
2686 static const struct got_error *
2687 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2688 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2689 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2690 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2692 const struct got_error *err = NULL;
2693 struct got_commit_object *commit = NULL;
2694 struct got_tree_object *tree = NULL;
2695 struct got_fileindex_diff_tree_cb diff_cb;
2696 struct diff_cb_arg arg;
2698 err = ref_base_commit(worktree, repo);
2699 if (err) {
2700 if (!(err->code == GOT_ERR_ERRNO &&
2701 (errno == EACCES || errno == EROFS)))
2702 goto done;
2703 err = (*progress_cb)(progress_arg,
2704 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2705 if (err)
2706 return err;
2709 err = got_object_open_as_commit(&commit, repo,
2710 worktree->base_commit_id);
2711 if (err)
2712 goto done;
2714 err = got_object_open_as_tree(&tree, repo, tree_id);
2715 if (err)
2716 goto done;
2718 if (entry_name &&
2719 got_object_tree_find_entry(tree, entry_name) == NULL) {
2720 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2721 goto done;
2724 diff_cb.diff_old_new = diff_old_new;
2725 diff_cb.diff_old = diff_old;
2726 diff_cb.diff_new = diff_new;
2727 arg.fileindex = fileindex;
2728 arg.worktree = worktree;
2729 arg.repo = repo;
2730 arg.progress_cb = progress_cb;
2731 arg.progress_arg = progress_arg;
2732 arg.cancel_cb = cancel_cb;
2733 arg.cancel_arg = cancel_arg;
2734 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2735 entry_name, repo, &diff_cb, &arg);
2736 done:
2737 if (tree)
2738 got_object_tree_close(tree);
2739 if (commit)
2740 got_object_commit_close(commit);
2741 return err;
2744 const struct got_error *
2745 got_worktree_checkout_files(struct got_worktree *worktree,
2746 struct got_pathlist_head *paths, struct got_repository *repo,
2747 got_worktree_checkout_cb progress_cb, void *progress_arg,
2748 got_cancel_cb cancel_cb, void *cancel_arg)
2750 const struct got_error *err = NULL, *sync_err, *unlockerr;
2751 struct got_commit_object *commit = NULL;
2752 struct got_tree_object *tree = NULL;
2753 struct got_fileindex *fileindex = NULL;
2754 char *fileindex_path = NULL;
2755 struct got_pathlist_entry *pe;
2756 struct tree_path_data {
2757 STAILQ_ENTRY(tree_path_data) entry;
2758 struct got_object_id *tree_id;
2759 int entry_type;
2760 char *relpath;
2761 char *entry_name;
2762 } *tpd = NULL;
2763 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2765 STAILQ_INIT(&tree_paths);
2767 err = lock_worktree(worktree, LOCK_EX);
2768 if (err)
2769 return err;
2771 err = got_object_open_as_commit(&commit, repo,
2772 worktree->base_commit_id);
2773 if (err)
2774 goto done;
2776 /* Map all specified paths to in-repository trees. */
2777 TAILQ_FOREACH(pe, paths, entry) {
2778 tpd = malloc(sizeof(*tpd));
2779 if (tpd == NULL) {
2780 err = got_error_from_errno("malloc");
2781 goto done;
2784 err = find_tree_entry_for_checkout(&tpd->entry_type,
2785 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2786 worktree, repo);
2787 if (err) {
2788 free(tpd);
2789 goto done;
2792 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2793 err = got_path_basename(&tpd->entry_name, pe->path);
2794 if (err) {
2795 free(tpd->relpath);
2796 free(tpd->tree_id);
2797 free(tpd);
2798 goto done;
2800 } else
2801 tpd->entry_name = NULL;
2803 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2807 * Read the file index.
2808 * Checking out files is supposed to be an idempotent operation.
2809 * If the on-disk file index is incomplete we will try to complete it.
2811 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2812 if (err)
2813 goto done;
2815 tpd = STAILQ_FIRST(&tree_paths);
2816 TAILQ_FOREACH(pe, paths, entry) {
2817 struct bump_base_commit_id_arg bbc_arg;
2819 err = checkout_files(worktree, fileindex, tpd->relpath,
2820 tpd->tree_id, tpd->entry_name, repo,
2821 progress_cb, progress_arg, cancel_cb, cancel_arg);
2822 if (err)
2823 break;
2825 bbc_arg.base_commit_id = worktree->base_commit_id;
2826 bbc_arg.entry_name = tpd->entry_name;
2827 bbc_arg.path = pe->path;
2828 bbc_arg.path_len = pe->path_len;
2829 bbc_arg.progress_cb = progress_cb;
2830 bbc_arg.progress_arg = progress_arg;
2831 err = got_fileindex_for_each_entry_safe(fileindex,
2832 bump_base_commit_id, &bbc_arg);
2833 if (err)
2834 break;
2836 tpd = STAILQ_NEXT(tpd, entry);
2838 sync_err = sync_fileindex(fileindex, fileindex_path);
2839 if (sync_err && err == NULL)
2840 err = sync_err;
2841 done:
2842 free(fileindex_path);
2843 if (tree)
2844 got_object_tree_close(tree);
2845 if (commit)
2846 got_object_commit_close(commit);
2847 if (fileindex)
2848 got_fileindex_free(fileindex);
2849 while (!STAILQ_EMPTY(&tree_paths)) {
2850 tpd = STAILQ_FIRST(&tree_paths);
2851 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2852 free(tpd->relpath);
2853 free(tpd->tree_id);
2854 free(tpd);
2856 unlockerr = lock_worktree(worktree, LOCK_SH);
2857 if (unlockerr && err == NULL)
2858 err = unlockerr;
2859 return err;
2862 struct merge_file_cb_arg {
2863 struct got_worktree *worktree;
2864 struct got_fileindex *fileindex;
2865 got_worktree_checkout_cb progress_cb;
2866 void *progress_arg;
2867 got_cancel_cb cancel_cb;
2868 void *cancel_arg;
2869 const char *label_orig;
2870 struct got_object_id *commit_id2;
2871 int allow_bad_symlinks;
2874 static const struct got_error *
2875 merge_file_cb(void *arg, struct got_blob_object *blob1,
2876 struct got_blob_object *blob2, FILE *f1, FILE *f2,
2877 struct got_object_id *id1, struct got_object_id *id2,
2878 const char *path1, const char *path2,
2879 mode_t mode1, mode_t mode2, struct got_repository *repo)
2881 static const struct got_error *err = NULL;
2882 struct merge_file_cb_arg *a = arg;
2883 struct got_fileindex_entry *ie;
2884 char *ondisk_path = NULL;
2885 struct stat sb;
2886 unsigned char status;
2887 int local_changes_subsumed;
2888 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2889 char *id_str = NULL, *label_deriv2 = NULL;
2891 if (blob1 && blob2) {
2892 ie = got_fileindex_entry_get(a->fileindex, path2,
2893 strlen(path2));
2894 if (ie == NULL)
2895 return (*a->progress_cb)(a->progress_arg,
2896 GOT_STATUS_MISSING, path2);
2898 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2899 path2) == -1)
2900 return got_error_from_errno("asprintf");
2902 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2903 repo);
2904 if (err)
2905 goto done;
2907 if (status == GOT_STATUS_DELETE) {
2908 err = (*a->progress_cb)(a->progress_arg,
2909 GOT_STATUS_MERGE, path2);
2910 goto done;
2912 if (status != GOT_STATUS_NO_CHANGE &&
2913 status != GOT_STATUS_MODIFY &&
2914 status != GOT_STATUS_CONFLICT &&
2915 status != GOT_STATUS_ADD) {
2916 err = (*a->progress_cb)(a->progress_arg, status, path2);
2917 goto done;
2920 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2921 char *link_target2;
2922 err = got_object_blob_read_to_str(&link_target2, blob2);
2923 if (err)
2924 goto done;
2925 err = merge_symlink(a->worktree, blob1, ondisk_path,
2926 path2, a->label_orig, link_target2, a->commit_id2,
2927 repo, a->progress_cb, a->progress_arg);
2928 free(link_target2);
2929 } else {
2930 int fd;
2932 f_orig = got_opentemp();
2933 if (f_orig == NULL) {
2934 err = got_error_from_errno("got_opentemp");
2935 goto done;
2937 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2938 f_orig, blob1);
2939 if (err)
2940 goto done;
2942 f_deriv2 = got_opentemp();
2943 if (f_deriv2 == NULL)
2944 goto done;
2945 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2946 f_deriv2, blob2);
2947 if (err)
2948 goto done;
2950 fd = open(ondisk_path,
2951 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2952 if (fd == -1) {
2953 err = got_error_from_errno2("open",
2954 ondisk_path);
2955 goto done;
2957 f_deriv = fdopen(fd, "r");
2958 if (f_deriv == NULL) {
2959 err = got_error_from_errno2("fdopen",
2960 ondisk_path);
2961 close(fd);
2962 goto done;
2964 err = got_object_id_str(&id_str, a->commit_id2);
2965 if (err)
2966 goto done;
2967 if (asprintf(&label_deriv2, "%s: commit %s",
2968 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2969 err = got_error_from_errno("asprintf");
2970 goto done;
2972 err = merge_file(&local_changes_subsumed, a->worktree,
2973 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2974 mode2, a->label_orig, NULL, label_deriv2,
2975 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2976 a->progress_cb, a->progress_arg);
2978 } else if (blob1) {
2979 ie = got_fileindex_entry_get(a->fileindex, path1,
2980 strlen(path1));
2981 if (ie == NULL)
2982 return (*a->progress_cb)(a->progress_arg,
2983 GOT_STATUS_MISSING, path1);
2985 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2986 path1) == -1)
2987 return got_error_from_errno("asprintf");
2989 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2990 repo);
2991 if (err)
2992 goto done;
2994 switch (status) {
2995 case GOT_STATUS_NO_CHANGE:
2996 err = (*a->progress_cb)(a->progress_arg,
2997 GOT_STATUS_DELETE, path1);
2998 if (err)
2999 goto done;
3000 err = remove_ondisk_file(a->worktree->root_path, path1);
3001 if (err)
3002 goto done;
3003 if (ie)
3004 got_fileindex_entry_mark_deleted_from_disk(ie);
3005 break;
3006 case GOT_STATUS_DELETE:
3007 case GOT_STATUS_MISSING:
3008 err = (*a->progress_cb)(a->progress_arg,
3009 GOT_STATUS_DELETE, path1);
3010 if (err)
3011 goto done;
3012 if (ie)
3013 got_fileindex_entry_mark_deleted_from_disk(ie);
3014 break;
3015 case GOT_STATUS_ADD: {
3016 struct got_object_id *id;
3017 FILE *blob1_f;
3018 off_t blob1_size;
3020 * Delete the added file only if its content already
3021 * exists in the repository.
3023 err = got_object_blob_file_create(&id, &blob1_f,
3024 &blob1_size, path1);
3025 if (err)
3026 goto done;
3027 if (got_object_id_cmp(id, id1) == 0) {
3028 err = (*a->progress_cb)(a->progress_arg,
3029 GOT_STATUS_DELETE, path1);
3030 if (err)
3031 goto done;
3032 err = remove_ondisk_file(a->worktree->root_path,
3033 path1);
3034 if (err)
3035 goto done;
3036 if (ie)
3037 got_fileindex_entry_remove(a->fileindex,
3038 ie);
3039 } else {
3040 err = (*a->progress_cb)(a->progress_arg,
3041 GOT_STATUS_CANNOT_DELETE, path1);
3043 if (fclose(blob1_f) == EOF && err == NULL)
3044 err = got_error_from_errno("fclose");
3045 free(id);
3046 if (err)
3047 goto done;
3048 break;
3050 case GOT_STATUS_MODIFY:
3051 case GOT_STATUS_CONFLICT:
3052 err = (*a->progress_cb)(a->progress_arg,
3053 GOT_STATUS_CANNOT_DELETE, path1);
3054 if (err)
3055 goto done;
3056 break;
3057 case GOT_STATUS_OBSTRUCTED:
3058 err = (*a->progress_cb)(a->progress_arg, status, path1);
3059 if (err)
3060 goto done;
3061 break;
3062 default:
3063 break;
3065 } else if (blob2) {
3066 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3067 path2) == -1)
3068 return got_error_from_errno("asprintf");
3069 ie = got_fileindex_entry_get(a->fileindex, path2,
3070 strlen(path2));
3071 if (ie) {
3072 err = get_file_status(&status, &sb, ie, ondisk_path,
3073 -1, NULL, repo);
3074 if (err)
3075 goto done;
3076 if (status != GOT_STATUS_NO_CHANGE &&
3077 status != GOT_STATUS_MODIFY &&
3078 status != GOT_STATUS_CONFLICT &&
3079 status != GOT_STATUS_ADD) {
3080 err = (*a->progress_cb)(a->progress_arg,
3081 status, path2);
3082 goto done;
3084 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3085 char *link_target2;
3086 err = got_object_blob_read_to_str(&link_target2,
3087 blob2);
3088 if (err)
3089 goto done;
3090 err = merge_symlink(a->worktree, NULL,
3091 ondisk_path, path2, a->label_orig,
3092 link_target2, a->commit_id2, repo,
3093 a->progress_cb, a->progress_arg);
3094 free(link_target2);
3095 } else if (S_ISREG(sb.st_mode)) {
3096 err = merge_blob(&local_changes_subsumed,
3097 a->worktree, NULL, ondisk_path, path2,
3098 sb.st_mode, a->label_orig, blob2,
3099 a->commit_id2, repo, a->progress_cb,
3100 a->progress_arg);
3101 } else {
3102 err = got_error_path(ondisk_path,
3103 GOT_ERR_FILE_OBSTRUCTED);
3105 if (err)
3106 goto done;
3107 if (status == GOT_STATUS_DELETE) {
3108 err = got_fileindex_entry_update(ie,
3109 a->worktree->root_fd, path2, blob2->id.sha1,
3110 a->worktree->base_commit_id->sha1, 0);
3111 if (err)
3112 goto done;
3114 } else {
3115 int is_bad_symlink = 0;
3116 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3117 if (S_ISLNK(mode2)) {
3118 err = install_symlink(&is_bad_symlink,
3119 a->worktree, ondisk_path, path2, blob2, 0,
3120 0, 1, a->allow_bad_symlinks, repo,
3121 a->progress_cb, a->progress_arg);
3122 } else {
3123 err = install_blob(a->worktree, ondisk_path, path2,
3124 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3125 a->progress_cb, a->progress_arg);
3127 if (err)
3128 goto done;
3129 err = got_fileindex_entry_alloc(&ie, path2);
3130 if (err)
3131 goto done;
3132 err = got_fileindex_entry_update(ie,
3133 a->worktree->root_fd, path2, NULL, NULL, 1);
3134 if (err) {
3135 got_fileindex_entry_free(ie);
3136 goto done;
3138 err = got_fileindex_entry_add(a->fileindex, ie);
3139 if (err) {
3140 got_fileindex_entry_free(ie);
3141 goto done;
3143 if (is_bad_symlink) {
3144 got_fileindex_entry_filetype_set(ie,
3145 GOT_FILEIDX_MODE_BAD_SYMLINK);
3149 done:
3150 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3151 err = got_error_from_errno("fclose");
3152 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3153 err = got_error_from_errno("fclose");
3154 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3155 err = got_error_from_errno("fclose");
3156 free(id_str);
3157 free(label_deriv2);
3158 free(ondisk_path);
3159 return err;
3162 static const struct got_error *
3163 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3165 struct got_worktree *worktree = arg;
3167 /* Reject merges into a work tree with mixed base commits. */
3168 if (got_fileindex_entry_has_commit(ie) &&
3169 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3170 SHA1_DIGEST_LENGTH) != 0)
3171 return got_error(GOT_ERR_MIXED_COMMITS);
3173 return NULL;
3176 struct check_merge_conflicts_arg {
3177 struct got_worktree *worktree;
3178 struct got_fileindex *fileindex;
3179 struct got_repository *repo;
3182 static const struct got_error *
3183 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3184 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3185 struct got_object_id *id1, struct got_object_id *id2,
3186 const char *path1, const char *path2,
3187 mode_t mode1, mode_t mode2, struct got_repository *repo)
3189 const struct got_error *err = NULL;
3190 struct check_merge_conflicts_arg *a = arg;
3191 unsigned char status;
3192 struct stat sb;
3193 struct got_fileindex_entry *ie;
3194 const char *path = path2 ? path2 : path1;
3195 struct got_object_id *id = id2 ? id2 : id1;
3196 char *ondisk_path;
3198 if (id == NULL)
3199 return NULL;
3201 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3202 if (ie == NULL)
3203 return NULL;
3205 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3206 == -1)
3207 return got_error_from_errno("asprintf");
3209 /* Reject merges into a work tree with conflicted files. */
3210 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3211 free(ondisk_path);
3212 if (err)
3213 return err;
3214 if (status == GOT_STATUS_CONFLICT)
3215 return got_error(GOT_ERR_CONFLICTS);
3217 return NULL;
3220 static const struct got_error *
3221 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3222 const char *fileindex_path, struct got_object_id *commit_id1,
3223 struct got_object_id *commit_id2, struct got_repository *repo,
3224 got_worktree_checkout_cb progress_cb, void *progress_arg,
3225 got_cancel_cb cancel_cb, void *cancel_arg)
3227 const struct got_error *err = NULL, *sync_err;
3228 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3229 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3230 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3231 struct check_merge_conflicts_arg cmc_arg;
3232 struct merge_file_cb_arg arg;
3233 char *label_orig = NULL;
3234 FILE *f1 = NULL, *f2 = NULL;
3235 int fd1 = -1, fd2 = -1;
3237 if (commit_id1) {
3238 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3239 if (err)
3240 goto done;
3241 err = got_object_id_by_path(&tree_id1, repo, commit1,
3242 worktree->path_prefix);
3243 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3244 goto done;
3246 if (tree_id1) {
3247 char *id_str;
3249 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3250 if (err)
3251 goto done;
3253 err = got_object_id_str(&id_str, commit_id1);
3254 if (err)
3255 goto done;
3257 if (asprintf(&label_orig, "%s: commit %s",
3258 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3259 err = got_error_from_errno("asprintf");
3260 free(id_str);
3261 goto done;
3263 free(id_str);
3265 f1 = got_opentemp();
3266 if (f1 == NULL) {
3267 err = got_error_from_errno("got_opentemp");
3268 goto done;
3272 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3273 if (err)
3274 goto done;
3276 err = got_object_id_by_path(&tree_id2, repo, commit2,
3277 worktree->path_prefix);
3278 if (err)
3279 goto done;
3281 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3282 if (err)
3283 goto done;
3285 f2 = got_opentemp();
3286 if (f2 == NULL) {
3287 err = got_error_from_errno("got_opentemp");
3288 goto done;
3291 fd1 = got_opentempfd();
3292 if (fd1 == -1) {
3293 err = got_error_from_errno("got_opentempfd");
3294 goto done;
3297 fd2 = got_opentempfd();
3298 if (fd2 == -1) {
3299 err = got_error_from_errno("got_opentempfd");
3300 goto done;
3303 cmc_arg.worktree = worktree;
3304 cmc_arg.fileindex = fileindex;
3305 cmc_arg.repo = repo;
3306 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3307 check_merge_conflicts, &cmc_arg, 0);
3308 if (err)
3309 goto done;
3311 arg.worktree = worktree;
3312 arg.fileindex = fileindex;
3313 arg.progress_cb = progress_cb;
3314 arg.progress_arg = progress_arg;
3315 arg.cancel_cb = cancel_cb;
3316 arg.cancel_arg = cancel_arg;
3317 arg.label_orig = label_orig;
3318 arg.commit_id2 = commit_id2;
3319 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3320 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3321 merge_file_cb, &arg, 1);
3322 sync_err = sync_fileindex(fileindex, fileindex_path);
3323 if (sync_err && err == NULL)
3324 err = sync_err;
3325 done:
3326 if (commit1)
3327 got_object_commit_close(commit1);
3328 if (commit2)
3329 got_object_commit_close(commit2);
3330 if (tree1)
3331 got_object_tree_close(tree1);
3332 if (tree2)
3333 got_object_tree_close(tree2);
3334 if (f1 && fclose(f1) == EOF && err == NULL)
3335 err = got_error_from_errno("fclose");
3336 if (f2 && fclose(f2) == EOF && err == NULL)
3337 err = got_error_from_errno("fclose");
3338 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3339 err = got_error_from_errno("close");
3340 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3341 err = got_error_from_errno("close");
3342 free(label_orig);
3343 return err;
3346 const struct got_error *
3347 got_worktree_merge_files(struct got_worktree *worktree,
3348 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3349 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3350 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3352 const struct got_error *err, *unlockerr;
3353 char *fileindex_path = NULL;
3354 struct got_fileindex *fileindex = NULL;
3356 err = lock_worktree(worktree, LOCK_EX);
3357 if (err)
3358 return err;
3360 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3361 if (err)
3362 goto done;
3364 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3365 worktree);
3366 if (err)
3367 goto done;
3369 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3370 commit_id2, repo, progress_cb, progress_arg,
3371 cancel_cb, cancel_arg);
3372 done:
3373 if (fileindex)
3374 got_fileindex_free(fileindex);
3375 free(fileindex_path);
3376 unlockerr = lock_worktree(worktree, LOCK_SH);
3377 if (unlockerr && err == NULL)
3378 err = unlockerr;
3379 return err;
3382 struct diff_dir_cb_arg {
3383 struct got_fileindex *fileindex;
3384 struct got_worktree *worktree;
3385 const char *status_path;
3386 size_t status_path_len;
3387 struct got_repository *repo;
3388 got_worktree_status_cb status_cb;
3389 void *status_arg;
3390 got_cancel_cb cancel_cb;
3391 void *cancel_arg;
3392 /* A pathlist containing per-directory pathlists of ignore patterns. */
3393 struct got_pathlist_head *ignores;
3394 int report_unchanged;
3395 int no_ignores;
3398 static const struct got_error *
3399 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3400 int dirfd, const char *de_name,
3401 got_worktree_status_cb status_cb, void *status_arg,
3402 struct got_repository *repo, int report_unchanged)
3404 const struct got_error *err = NULL;
3405 unsigned char status = GOT_STATUS_NO_CHANGE;
3406 unsigned char staged_status;
3407 struct stat sb;
3408 struct got_object_id blob_id, commit_id, staged_blob_id;
3409 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3410 struct got_object_id *staged_blob_idp = NULL;
3412 staged_status = get_staged_status(ie);
3413 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3414 if (err)
3415 return err;
3417 if (status == GOT_STATUS_NO_CHANGE &&
3418 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3419 return NULL;
3421 if (got_fileindex_entry_has_blob(ie))
3422 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3423 if (got_fileindex_entry_has_commit(ie))
3424 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3425 if (staged_status == GOT_STATUS_ADD ||
3426 staged_status == GOT_STATUS_MODIFY) {
3427 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3428 &staged_blob_id, ie);
3431 return (*status_cb)(status_arg, status, staged_status,
3432 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3435 static const struct got_error *
3436 status_old_new(void *arg, struct got_fileindex_entry *ie,
3437 struct dirent *de, const char *parent_path, int dirfd)
3439 const struct got_error *err = NULL;
3440 struct diff_dir_cb_arg *a = arg;
3441 char *abspath;
3443 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3444 return got_error(GOT_ERR_CANCELLED);
3446 if (got_path_cmp(parent_path, a->status_path,
3447 strlen(parent_path), a->status_path_len) != 0 &&
3448 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3449 return NULL;
3451 if (parent_path[0]) {
3452 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3453 parent_path, de->d_name) == -1)
3454 return got_error_from_errno("asprintf");
3455 } else {
3456 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3457 de->d_name) == -1)
3458 return got_error_from_errno("asprintf");
3461 err = report_file_status(ie, abspath, dirfd, de->d_name,
3462 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3463 free(abspath);
3464 return err;
3467 static const struct got_error *
3468 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3470 struct diff_dir_cb_arg *a = arg;
3471 struct got_object_id blob_id, commit_id;
3472 unsigned char status;
3474 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3475 return got_error(GOT_ERR_CANCELLED);
3477 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3478 return NULL;
3480 got_fileindex_entry_get_blob_id(&blob_id, ie);
3481 got_fileindex_entry_get_commit_id(&commit_id, ie);
3482 if (got_fileindex_entry_has_file_on_disk(ie))
3483 status = GOT_STATUS_MISSING;
3484 else
3485 status = GOT_STATUS_DELETE;
3486 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3487 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3490 static void
3491 free_ignores(struct got_pathlist_head *ignores)
3493 struct got_pathlist_entry *pe;
3495 TAILQ_FOREACH(pe, ignores, entry) {
3496 struct got_pathlist_head *ignorelist = pe->data;
3498 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3500 got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3503 static const struct got_error *
3504 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3506 const struct got_error *err = NULL;
3507 struct got_pathlist_entry *pe = NULL;
3508 struct got_pathlist_head *ignorelist;
3509 char *line = NULL, *pattern, *dirpath = NULL;
3510 size_t linesize = 0;
3511 ssize_t linelen;
3513 ignorelist = calloc(1, sizeof(*ignorelist));
3514 if (ignorelist == NULL)
3515 return got_error_from_errno("calloc");
3516 TAILQ_INIT(ignorelist);
3518 while ((linelen = getline(&line, &linesize, f)) != -1) {
3519 if (linelen > 0 && line[linelen - 1] == '\n')
3520 line[linelen - 1] = '\0';
3522 /* Git's ignores may contain comments. */
3523 if (line[0] == '#')
3524 continue;
3526 /* Git's negated patterns are not (yet?) supported. */
3527 if (line[0] == '!')
3528 continue;
3530 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3531 line) == -1) {
3532 err = got_error_from_errno("asprintf");
3533 goto done;
3535 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3536 if (err)
3537 goto done;
3539 if (ferror(f)) {
3540 err = got_error_from_errno("getline");
3541 goto done;
3544 dirpath = strdup(path);
3545 if (dirpath == NULL) {
3546 err = got_error_from_errno("strdup");
3547 goto done;
3549 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3550 done:
3551 free(line);
3552 if (err || pe == NULL) {
3553 free(dirpath);
3554 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3556 return err;
3559 static int
3560 match_ignores(struct got_pathlist_head *ignores, const char *path)
3562 struct got_pathlist_entry *pe;
3564 /* Handle patterns which match in all directories. */
3565 TAILQ_FOREACH(pe, ignores, entry) {
3566 struct got_pathlist_head *ignorelist = pe->data;
3567 struct got_pathlist_entry *pi;
3569 TAILQ_FOREACH(pi, ignorelist, entry) {
3570 const char *p, *pattern = pi->path;
3572 if (strncmp(pattern, "**/", 3) != 0)
3573 continue;
3574 pattern += 3;
3575 p = path;
3576 while (*p) {
3577 if (fnmatch(pattern, p,
3578 FNM_PATHNAME | FNM_LEADING_DIR)) {
3579 /* Retry in next directory. */
3580 while (*p && *p != '/')
3581 p++;
3582 while (*p == '/')
3583 p++;
3584 continue;
3586 return 1;
3592 * The ignores pathlist contains ignore lists from children before
3593 * parents, so we can find the most specific ignorelist by walking
3594 * ignores backwards.
3596 pe = TAILQ_LAST(ignores, got_pathlist_head);
3597 while (pe) {
3598 if (got_path_is_child(path, pe->path, pe->path_len)) {
3599 struct got_pathlist_head *ignorelist = pe->data;
3600 struct got_pathlist_entry *pi;
3601 TAILQ_FOREACH(pi, ignorelist, entry) {
3602 const char *pattern = pi->path;
3603 int flags = FNM_LEADING_DIR;
3604 if (strstr(pattern, "/**/") == NULL)
3605 flags |= FNM_PATHNAME;
3606 if (fnmatch(pattern, path, flags))
3607 continue;
3608 return 1;
3611 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3614 return 0;
3617 static const struct got_error *
3618 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3619 const char *path, int dirfd, const char *ignores_filename)
3621 const struct got_error *err = NULL;
3622 char *ignorespath;
3623 int fd = -1;
3624 FILE *ignoresfile = NULL;
3626 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3627 path[0] ? "/" : "", ignores_filename) == -1)
3628 return got_error_from_errno("asprintf");
3630 if (dirfd != -1) {
3631 fd = openat(dirfd, ignores_filename,
3632 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3633 if (fd == -1) {
3634 if (errno != ENOENT && errno != EACCES)
3635 err = got_error_from_errno2("openat",
3636 ignorespath);
3637 } else {
3638 ignoresfile = fdopen(fd, "r");
3639 if (ignoresfile == NULL)
3640 err = got_error_from_errno2("fdopen",
3641 ignorespath);
3642 else {
3643 fd = -1;
3644 err = read_ignores(ignores, path, ignoresfile);
3647 } else {
3648 ignoresfile = fopen(ignorespath, "re");
3649 if (ignoresfile == NULL) {
3650 if (errno != ENOENT && errno != EACCES)
3651 err = got_error_from_errno2("fopen",
3652 ignorespath);
3653 } else
3654 err = read_ignores(ignores, path, ignoresfile);
3657 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3658 err = got_error_from_errno2("fclose", path);
3659 if (fd != -1 && close(fd) == -1 && err == NULL)
3660 err = got_error_from_errno2("close", path);
3661 free(ignorespath);
3662 return err;
3665 static const struct got_error *
3666 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3667 int dirfd)
3669 const struct got_error *err = NULL;
3670 struct diff_dir_cb_arg *a = arg;
3671 char *path = NULL;
3673 if (ignore != NULL)
3674 *ignore = 0;
3676 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3677 return got_error(GOT_ERR_CANCELLED);
3679 if (parent_path[0]) {
3680 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3681 return got_error_from_errno("asprintf");
3682 } else {
3683 path = de->d_name;
3686 if (de->d_type == DT_DIR) {
3687 if (!a->no_ignores && ignore != NULL &&
3688 match_ignores(a->ignores, path))
3689 *ignore = 1;
3690 } else if (!match_ignores(a->ignores, path) &&
3691 got_path_is_child(path, a->status_path, a->status_path_len))
3692 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3693 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3694 if (parent_path[0])
3695 free(path);
3696 return err;
3699 static const struct got_error *
3700 status_traverse(void *arg, const char *path, int dirfd)
3702 const struct got_error *err = NULL;
3703 struct diff_dir_cb_arg *a = arg;
3705 if (a->no_ignores)
3706 return NULL;
3708 err = add_ignores(a->ignores, a->worktree->root_path,
3709 path, dirfd, ".cvsignore");
3710 if (err)
3711 return err;
3713 err = add_ignores(a->ignores, a->worktree->root_path, path,
3714 dirfd, ".gitignore");
3716 return err;
3719 static const struct got_error *
3720 report_single_file_status(const char *path, const char *ondisk_path,
3721 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3722 void *status_arg, struct got_repository *repo, int report_unchanged,
3723 struct got_pathlist_head *ignores, int no_ignores)
3725 struct got_fileindex_entry *ie;
3726 struct stat sb;
3728 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3729 if (ie)
3730 return report_file_status(ie, ondisk_path, -1, NULL,
3731 status_cb, status_arg, repo, report_unchanged);
3733 if (lstat(ondisk_path, &sb) == -1) {
3734 if (errno != ENOENT)
3735 return got_error_from_errno2("lstat", ondisk_path);
3736 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3737 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3740 if (!no_ignores && match_ignores(ignores, path))
3741 return NULL;
3743 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3744 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3745 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3747 return NULL;
3750 static const struct got_error *
3751 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3752 const char *root_path, const char *path)
3754 const struct got_error *err;
3755 char *parent_path, *next_parent_path = NULL;
3757 err = add_ignores(ignores, root_path, "", -1,
3758 ".cvsignore");
3759 if (err)
3760 return err;
3762 err = add_ignores(ignores, root_path, "", -1,
3763 ".gitignore");
3764 if (err)
3765 return err;
3767 err = got_path_dirname(&parent_path, path);
3768 if (err) {
3769 if (err->code == GOT_ERR_BAD_PATH)
3770 return NULL; /* cannot traverse parent */
3771 return err;
3773 for (;;) {
3774 err = add_ignores(ignores, root_path, parent_path, -1,
3775 ".cvsignore");
3776 if (err)
3777 break;
3778 err = add_ignores(ignores, root_path, parent_path, -1,
3779 ".gitignore");
3780 if (err)
3781 break;
3782 err = got_path_dirname(&next_parent_path, parent_path);
3783 if (err) {
3784 if (err->code == GOT_ERR_BAD_PATH)
3785 err = NULL; /* traversed everything */
3786 break;
3788 if (got_path_is_root_dir(parent_path))
3789 break;
3790 free(parent_path);
3791 parent_path = next_parent_path;
3792 next_parent_path = NULL;
3795 free(parent_path);
3796 free(next_parent_path);
3797 return err;
3800 static const struct got_error *
3801 worktree_status(struct got_worktree *worktree, const char *path,
3802 struct got_fileindex *fileindex, struct got_repository *repo,
3803 got_worktree_status_cb status_cb, void *status_arg,
3804 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3805 int report_unchanged)
3807 const struct got_error *err = NULL;
3808 int fd = -1;
3809 struct got_fileindex_diff_dir_cb fdiff_cb;
3810 struct diff_dir_cb_arg arg;
3811 char *ondisk_path = NULL;
3812 struct got_pathlist_head ignores;
3813 struct got_fileindex_entry *ie;
3815 TAILQ_INIT(&ignores);
3817 if (asprintf(&ondisk_path, "%s%s%s",
3818 worktree->root_path, path[0] ? "/" : "", path) == -1)
3819 return got_error_from_errno("asprintf");
3821 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3822 if (ie) {
3823 err = report_single_file_status(path, ondisk_path,
3824 fileindex, status_cb, status_arg, repo,
3825 report_unchanged, &ignores, no_ignores);
3826 goto done;
3829 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3830 if (fd == -1) {
3831 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3832 !got_err_open_nofollow_on_symlink())
3833 err = got_error_from_errno2("open", ondisk_path);
3834 else {
3835 if (!no_ignores) {
3836 err = add_ignores_from_parent_paths(&ignores,
3837 worktree->root_path, ondisk_path);
3838 if (err)
3839 goto done;
3841 err = report_single_file_status(path, ondisk_path,
3842 fileindex, status_cb, status_arg, repo,
3843 report_unchanged, &ignores, no_ignores);
3845 } else {
3846 fdiff_cb.diff_old_new = status_old_new;
3847 fdiff_cb.diff_old = status_old;
3848 fdiff_cb.diff_new = status_new;
3849 fdiff_cb.diff_traverse = status_traverse;
3850 arg.fileindex = fileindex;
3851 arg.worktree = worktree;
3852 arg.status_path = path;
3853 arg.status_path_len = strlen(path);
3854 arg.repo = repo;
3855 arg.status_cb = status_cb;
3856 arg.status_arg = status_arg;
3857 arg.cancel_cb = cancel_cb;
3858 arg.cancel_arg = cancel_arg;
3859 arg.report_unchanged = report_unchanged;
3860 arg.no_ignores = no_ignores;
3861 if (!no_ignores) {
3862 err = add_ignores_from_parent_paths(&ignores,
3863 worktree->root_path, path);
3864 if (err)
3865 goto done;
3867 arg.ignores = &ignores;
3868 err = got_fileindex_diff_dir(fileindex, fd,
3869 worktree->root_path, path, repo, &fdiff_cb, &arg);
3871 done:
3872 free_ignores(&ignores);
3873 if (fd != -1 && close(fd) == -1 && err == NULL)
3874 err = got_error_from_errno("close");
3875 free(ondisk_path);
3876 return err;
3879 const struct got_error *
3880 got_worktree_status(struct got_worktree *worktree,
3881 struct got_pathlist_head *paths, struct got_repository *repo,
3882 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3883 got_cancel_cb cancel_cb, void *cancel_arg)
3885 const struct got_error *err = NULL;
3886 char *fileindex_path = NULL;
3887 struct got_fileindex *fileindex = NULL;
3888 struct got_pathlist_entry *pe;
3890 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3891 if (err)
3892 return err;
3894 TAILQ_FOREACH(pe, paths, entry) {
3895 err = worktree_status(worktree, pe->path, fileindex, repo,
3896 status_cb, status_arg, cancel_cb, cancel_arg,
3897 no_ignores, 0);
3898 if (err)
3899 break;
3901 free(fileindex_path);
3902 got_fileindex_free(fileindex);
3903 return err;
3906 const struct got_error *
3907 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3908 const char *arg)
3910 const struct got_error *err = NULL;
3911 char *resolved = NULL, *cwd = NULL, *path = NULL;
3912 size_t len;
3913 struct stat sb;
3914 char *abspath = NULL;
3915 char canonpath[PATH_MAX];
3917 *wt_path = NULL;
3919 cwd = getcwd(NULL, 0);
3920 if (cwd == NULL)
3921 return got_error_from_errno("getcwd");
3923 if (lstat(arg, &sb) == -1) {
3924 if (errno != ENOENT) {
3925 err = got_error_from_errno2("lstat", arg);
3926 goto done;
3928 sb.st_mode = 0;
3930 if (S_ISLNK(sb.st_mode)) {
3932 * We cannot use realpath(3) with symlinks since we want to
3933 * operate on the symlink itself.
3934 * But we can make the path absolute, assuming it is relative
3935 * to the current working directory, and then canonicalize it.
3937 if (!got_path_is_absolute(arg)) {
3938 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3939 err = got_error_from_errno("asprintf");
3940 goto done;
3944 err = got_canonpath(abspath ? abspath : arg, canonpath,
3945 sizeof(canonpath));
3946 if (err)
3947 goto done;
3948 resolved = strdup(canonpath);
3949 if (resolved == NULL) {
3950 err = got_error_from_errno("strdup");
3951 goto done;
3953 } else {
3954 resolved = realpath(arg, NULL);
3955 if (resolved == NULL) {
3956 if (errno != ENOENT) {
3957 err = got_error_from_errno2("realpath", arg);
3958 goto done;
3960 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3961 err = got_error_from_errno("asprintf");
3962 goto done;
3964 err = got_canonpath(abspath, canonpath,
3965 sizeof(canonpath));
3966 if (err)
3967 goto done;
3968 resolved = strdup(canonpath);
3969 if (resolved == NULL) {
3970 err = got_error_from_errno("strdup");
3971 goto done;
3976 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3977 strlen(got_worktree_get_root_path(worktree)))) {
3978 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3979 goto done;
3982 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3983 err = got_path_skip_common_ancestor(&path,
3984 got_worktree_get_root_path(worktree), resolved);
3985 if (err)
3986 goto done;
3987 } else {
3988 path = strdup("");
3989 if (path == NULL) {
3990 err = got_error_from_errno("strdup");
3991 goto done;
3995 /* XXX status walk can't deal with trailing slash! */
3996 len = strlen(path);
3997 while (len > 0 && path[len - 1] == '/') {
3998 path[len - 1] = '\0';
3999 len--;
4001 done:
4002 free(abspath);
4003 free(resolved);
4004 free(cwd);
4005 if (err == NULL)
4006 *wt_path = path;
4007 else
4008 free(path);
4009 return err;
4012 struct schedule_addition_args {
4013 struct got_worktree *worktree;
4014 struct got_fileindex *fileindex;
4015 got_worktree_checkout_cb progress_cb;
4016 void *progress_arg;
4017 struct got_repository *repo;
4020 static const struct got_error *
4021 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4022 const char *relpath, struct got_object_id *blob_id,
4023 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4024 int dirfd, const char *de_name)
4026 struct schedule_addition_args *a = arg;
4027 const struct got_error *err = NULL;
4028 struct got_fileindex_entry *ie;
4029 struct stat sb;
4030 char *ondisk_path;
4032 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4033 relpath) == -1)
4034 return got_error_from_errno("asprintf");
4036 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4037 if (ie) {
4038 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4039 de_name, a->repo);
4040 if (err)
4041 goto done;
4042 /* Re-adding an existing entry is a no-op. */
4043 if (status == GOT_STATUS_ADD)
4044 goto done;
4045 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4046 if (err)
4047 goto done;
4050 if (status != GOT_STATUS_UNVERSIONED) {
4051 if (status == GOT_STATUS_NONEXISTENT)
4052 err = got_error_set_errno(ENOENT, ondisk_path);
4053 else
4054 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4055 goto done;
4058 err = got_fileindex_entry_alloc(&ie, relpath);
4059 if (err)
4060 goto done;
4061 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4062 relpath, NULL, NULL, 1);
4063 if (err) {
4064 got_fileindex_entry_free(ie);
4065 goto done;
4067 err = got_fileindex_entry_add(a->fileindex, ie);
4068 if (err) {
4069 got_fileindex_entry_free(ie);
4070 goto done;
4072 done:
4073 free(ondisk_path);
4074 if (err)
4075 return err;
4076 if (status == GOT_STATUS_ADD)
4077 return NULL;
4078 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4081 const struct got_error *
4082 got_worktree_schedule_add(struct got_worktree *worktree,
4083 struct got_pathlist_head *paths,
4084 got_worktree_checkout_cb progress_cb, void *progress_arg,
4085 struct got_repository *repo, int no_ignores)
4087 struct got_fileindex *fileindex = NULL;
4088 char *fileindex_path = NULL;
4089 const struct got_error *err = NULL, *sync_err, *unlockerr;
4090 struct got_pathlist_entry *pe;
4091 struct schedule_addition_args saa;
4093 err = lock_worktree(worktree, LOCK_EX);
4094 if (err)
4095 return err;
4097 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4098 if (err)
4099 goto done;
4101 saa.worktree = worktree;
4102 saa.fileindex = fileindex;
4103 saa.progress_cb = progress_cb;
4104 saa.progress_arg = progress_arg;
4105 saa.repo = repo;
4107 TAILQ_FOREACH(pe, paths, entry) {
4108 err = worktree_status(worktree, pe->path, fileindex, repo,
4109 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4110 if (err)
4111 break;
4113 sync_err = sync_fileindex(fileindex, fileindex_path);
4114 if (sync_err && err == NULL)
4115 err = sync_err;
4116 done:
4117 free(fileindex_path);
4118 if (fileindex)
4119 got_fileindex_free(fileindex);
4120 unlockerr = lock_worktree(worktree, LOCK_SH);
4121 if (unlockerr && err == NULL)
4122 err = unlockerr;
4123 return err;
4126 struct schedule_deletion_args {
4127 struct got_worktree *worktree;
4128 struct got_fileindex *fileindex;
4129 got_worktree_delete_cb progress_cb;
4130 void *progress_arg;
4131 struct got_repository *repo;
4132 int delete_local_mods;
4133 int keep_on_disk;
4134 int ignore_missing_paths;
4135 const char *status_codes;
4138 static const struct got_error *
4139 schedule_for_deletion(void *arg, unsigned char status,
4140 unsigned char staged_status, const char *relpath,
4141 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4142 struct got_object_id *commit_id, int dirfd, const char *de_name)
4144 struct schedule_deletion_args *a = arg;
4145 const struct got_error *err = NULL;
4146 struct got_fileindex_entry *ie = NULL;
4147 struct stat sb;
4148 char *ondisk_path;
4150 if (status == GOT_STATUS_NONEXISTENT) {
4151 if (a->ignore_missing_paths)
4152 return NULL;
4153 return got_error_set_errno(ENOENT, relpath);
4156 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4157 if (ie == NULL)
4158 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4160 staged_status = get_staged_status(ie);
4161 if (staged_status != GOT_STATUS_NO_CHANGE) {
4162 if (staged_status == GOT_STATUS_DELETE)
4163 return NULL;
4164 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4167 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4168 relpath) == -1)
4169 return got_error_from_errno("asprintf");
4171 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4172 a->repo);
4173 if (err)
4174 goto done;
4176 if (a->status_codes) {
4177 size_t ncodes = strlen(a->status_codes);
4178 int i;
4179 for (i = 0; i < ncodes ; i++) {
4180 if (status == a->status_codes[i])
4181 break;
4183 if (i == ncodes) {
4184 /* Do not delete files in non-matching status. */
4185 free(ondisk_path);
4186 return NULL;
4188 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4189 a->status_codes[i] != GOT_STATUS_MISSING) {
4190 static char msg[64];
4191 snprintf(msg, sizeof(msg),
4192 "invalid status code '%c'", a->status_codes[i]);
4193 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4194 goto done;
4198 if (status != GOT_STATUS_NO_CHANGE) {
4199 if (status == GOT_STATUS_DELETE)
4200 goto done;
4201 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4202 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4203 goto done;
4205 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4206 err = got_error_set_errno(ENOENT, relpath);
4207 goto done;
4209 if (status != GOT_STATUS_MODIFY &&
4210 status != GOT_STATUS_MISSING) {
4211 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4212 goto done;
4216 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4217 size_t root_len;
4219 if (dirfd != -1) {
4220 if (unlinkat(dirfd, de_name, 0) == -1) {
4221 err = got_error_from_errno2("unlinkat",
4222 ondisk_path);
4223 goto done;
4225 } else if (unlink(ondisk_path) == -1) {
4226 err = got_error_from_errno2("unlink", ondisk_path);
4227 goto done;
4230 root_len = strlen(a->worktree->root_path);
4231 do {
4232 char *parent;
4233 err = got_path_dirname(&parent, ondisk_path);
4234 if (err)
4235 goto done;
4236 free(ondisk_path);
4237 ondisk_path = parent;
4238 if (rmdir(ondisk_path) == -1) {
4239 if (errno != ENOTEMPTY)
4240 err = got_error_from_errno2("rmdir",
4241 ondisk_path);
4242 break;
4244 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4245 strlen(ondisk_path), root_len) != 0);
4248 got_fileindex_entry_mark_deleted_from_disk(ie);
4249 done:
4250 free(ondisk_path);
4251 if (err)
4252 return err;
4253 if (status == GOT_STATUS_DELETE)
4254 return NULL;
4255 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4256 staged_status, relpath);
4259 const struct got_error *
4260 got_worktree_schedule_delete(struct got_worktree *worktree,
4261 struct got_pathlist_head *paths, int delete_local_mods,
4262 const char *status_codes,
4263 got_worktree_delete_cb progress_cb, void *progress_arg,
4264 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4266 struct got_fileindex *fileindex = NULL;
4267 char *fileindex_path = NULL;
4268 const struct got_error *err = NULL, *sync_err, *unlockerr;
4269 struct got_pathlist_entry *pe;
4270 struct schedule_deletion_args sda;
4272 err = lock_worktree(worktree, LOCK_EX);
4273 if (err)
4274 return err;
4276 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4277 if (err)
4278 goto done;
4280 sda.worktree = worktree;
4281 sda.fileindex = fileindex;
4282 sda.progress_cb = progress_cb;
4283 sda.progress_arg = progress_arg;
4284 sda.repo = repo;
4285 sda.delete_local_mods = delete_local_mods;
4286 sda.keep_on_disk = keep_on_disk;
4287 sda.ignore_missing_paths = ignore_missing_paths;
4288 sda.status_codes = status_codes;
4290 TAILQ_FOREACH(pe, paths, entry) {
4291 err = worktree_status(worktree, pe->path, fileindex, repo,
4292 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4293 if (err)
4294 break;
4296 sync_err = sync_fileindex(fileindex, fileindex_path);
4297 if (sync_err && err == NULL)
4298 err = sync_err;
4299 done:
4300 free(fileindex_path);
4301 if (fileindex)
4302 got_fileindex_free(fileindex);
4303 unlockerr = lock_worktree(worktree, LOCK_SH);
4304 if (unlockerr && err == NULL)
4305 err = unlockerr;
4306 return err;
4309 static const struct got_error *
4310 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4312 const struct got_error *err = NULL;
4313 char *line = NULL;
4314 size_t linesize = 0, n;
4315 ssize_t linelen;
4317 linelen = getline(&line, &linesize, infile);
4318 if (linelen == -1) {
4319 if (ferror(infile)) {
4320 err = got_error_from_errno("getline");
4321 goto done;
4323 return NULL;
4325 if (outfile) {
4326 n = fwrite(line, 1, linelen, outfile);
4327 if (n != linelen) {
4328 err = got_ferror(outfile, GOT_ERR_IO);
4329 goto done;
4332 if (rejectfile) {
4333 n = fwrite(line, 1, linelen, rejectfile);
4334 if (n != linelen)
4335 err = got_ferror(rejectfile, GOT_ERR_IO);
4337 done:
4338 free(line);
4339 return err;
4342 static const struct got_error *
4343 skip_one_line(FILE *f)
4345 char *line = NULL;
4346 size_t linesize = 0;
4347 ssize_t linelen;
4349 linelen = getline(&line, &linesize, f);
4350 if (linelen == -1) {
4351 if (ferror(f))
4352 return got_error_from_errno("getline");
4353 return NULL;
4355 free(line);
4356 return NULL;
4359 static const struct got_error *
4360 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4361 int start_old, int end_old, int start_new, int end_new,
4362 FILE *outfile, FILE *rejectfile)
4364 const struct got_error *err;
4366 /* Copy old file's lines leading up to patch. */
4367 while (!feof(f1) && *line_cur1 < start_old) {
4368 err = copy_one_line(f1, outfile, NULL);
4369 if (err)
4370 return err;
4371 (*line_cur1)++;
4373 /* Skip new file's lines leading up to patch. */
4374 while (!feof(f2) && *line_cur2 < start_new) {
4375 if (rejectfile)
4376 err = copy_one_line(f2, NULL, rejectfile);
4377 else
4378 err = skip_one_line(f2);
4379 if (err)
4380 return err;
4381 (*line_cur2)++;
4383 /* Copy patched lines. */
4384 while (!feof(f2) && *line_cur2 <= end_new) {
4385 err = copy_one_line(f2, outfile, NULL);
4386 if (err)
4387 return err;
4388 (*line_cur2)++;
4390 /* Skip over old file's replaced lines. */
4391 while (!feof(f1) && *line_cur1 <= end_old) {
4392 if (rejectfile)
4393 err = copy_one_line(f1, NULL, rejectfile);
4394 else
4395 err = skip_one_line(f1);
4396 if (err)
4397 return err;
4398 (*line_cur1)++;
4401 return NULL;
4404 static const struct got_error *
4405 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4406 FILE *outfile, FILE *rejectfile)
4408 const struct got_error *err;
4410 if (outfile) {
4411 /* Copy old file's lines until EOF. */
4412 while (!feof(f1)) {
4413 err = copy_one_line(f1, outfile, NULL);
4414 if (err)
4415 return err;
4416 (*line_cur1)++;
4419 if (rejectfile) {
4420 /* Copy new file's lines until EOF. */
4421 while (!feof(f2)) {
4422 err = copy_one_line(f2, NULL, rejectfile);
4423 if (err)
4424 return err;
4425 (*line_cur2)++;
4429 return NULL;
4432 static const struct got_error *
4433 apply_or_reject_change(int *choice, int *nchunks_used,
4434 struct diff_result *diff_result, int n,
4435 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4436 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4437 got_worktree_patch_cb patch_cb, void *patch_arg)
4439 const struct got_error *err = NULL;
4440 struct diff_chunk_context cc = {};
4441 int start_old, end_old, start_new, end_new;
4442 FILE *hunkfile;
4443 struct diff_output_unidiff_state *diff_state;
4444 struct diff_input_info diff_info;
4445 int rc;
4447 *choice = GOT_PATCH_CHOICE_NONE;
4449 /* Get changed line numbers without context lines for copy_change(). */
4450 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4451 start_old = cc.left.start;
4452 end_old = cc.left.end;
4453 start_new = cc.right.start;
4454 end_new = cc.right.end;
4456 /* Get the same change with context lines for display. */
4457 memset(&cc, 0, sizeof(cc));
4458 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4460 memset(&diff_info, 0, sizeof(diff_info));
4461 diff_info.left_path = relpath;
4462 diff_info.right_path = relpath;
4464 diff_state = diff_output_unidiff_state_alloc();
4465 if (diff_state == NULL)
4466 return got_error_set_errno(ENOMEM,
4467 "diff_output_unidiff_state_alloc");
4469 hunkfile = got_opentemp();
4470 if (hunkfile == NULL) {
4471 err = got_error_from_errno("got_opentemp");
4472 goto done;
4475 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4476 diff_result, &cc);
4477 if (rc != DIFF_RC_OK) {
4478 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4479 goto done;
4482 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4483 err = got_ferror(hunkfile, GOT_ERR_IO);
4484 goto done;
4487 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4488 hunkfile, changeno, nchanges);
4489 if (err)
4490 goto done;
4492 switch (*choice) {
4493 case GOT_PATCH_CHOICE_YES:
4494 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4495 end_old, start_new, end_new, outfile, rejectfile);
4496 break;
4497 case GOT_PATCH_CHOICE_NO:
4498 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4499 end_old, start_new, end_new, rejectfile, outfile);
4500 break;
4501 case GOT_PATCH_CHOICE_QUIT:
4502 break;
4503 default:
4504 err = got_error(GOT_ERR_PATCH_CHOICE);
4505 break;
4507 done:
4508 diff_output_unidiff_state_free(diff_state);
4509 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4510 err = got_error_from_errno("fclose");
4511 return err;
4514 struct revert_file_args {
4515 struct got_worktree *worktree;
4516 struct got_fileindex *fileindex;
4517 got_worktree_checkout_cb progress_cb;
4518 void *progress_arg;
4519 got_worktree_patch_cb patch_cb;
4520 void *patch_arg;
4521 struct got_repository *repo;
4522 int unlink_added_files;
4525 static const struct got_error *
4526 create_patched_content(char **path_outfile, int reverse_patch,
4527 struct got_object_id *blob_id, const char *path2,
4528 int dirfd2, const char *de_name2,
4529 const char *relpath, struct got_repository *repo,
4530 got_worktree_patch_cb patch_cb, void *patch_arg)
4532 const struct got_error *err, *free_err;
4533 struct got_blob_object *blob = NULL;
4534 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4535 int fd = -1, fd2 = -1;
4536 char link_target[PATH_MAX];
4537 ssize_t link_len = 0;
4538 char *path1 = NULL, *id_str = NULL;
4539 struct stat sb2;
4540 struct got_diffreg_result *diffreg_result = NULL;
4541 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4542 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4544 *path_outfile = NULL;
4546 err = got_object_id_str(&id_str, blob_id);
4547 if (err)
4548 return err;
4550 if (dirfd2 != -1) {
4551 fd2 = openat(dirfd2, de_name2,
4552 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4553 if (fd2 == -1) {
4554 if (!got_err_open_nofollow_on_symlink()) {
4555 err = got_error_from_errno2("openat", path2);
4556 goto done;
4558 link_len = readlinkat(dirfd2, de_name2,
4559 link_target, sizeof(link_target));
4560 if (link_len == -1) {
4561 return got_error_from_errno2("readlinkat",
4562 path2);
4564 sb2.st_mode = S_IFLNK;
4565 sb2.st_size = link_len;
4567 } else {
4568 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4569 if (fd2 == -1) {
4570 if (!got_err_open_nofollow_on_symlink()) {
4571 err = got_error_from_errno2("open", path2);
4572 goto done;
4574 link_len = readlink(path2, link_target,
4575 sizeof(link_target));
4576 if (link_len == -1)
4577 return got_error_from_errno2("readlink", path2);
4578 sb2.st_mode = S_IFLNK;
4579 sb2.st_size = link_len;
4582 if (fd2 != -1) {
4583 if (fstat(fd2, &sb2) == -1) {
4584 err = got_error_from_errno2("fstat", path2);
4585 goto done;
4588 f2 = fdopen(fd2, "r");
4589 if (f2 == NULL) {
4590 err = got_error_from_errno2("fdopen", path2);
4591 goto done;
4593 fd2 = -1;
4594 } else {
4595 size_t n;
4596 f2 = got_opentemp();
4597 if (f2 == NULL) {
4598 err = got_error_from_errno2("got_opentemp", path2);
4599 goto done;
4601 n = fwrite(link_target, 1, link_len, f2);
4602 if (n != link_len) {
4603 err = got_ferror(f2, GOT_ERR_IO);
4604 goto done;
4606 if (fflush(f2) == EOF) {
4607 err = got_error_from_errno("fflush");
4608 goto done;
4610 rewind(f2);
4613 fd = got_opentempfd();
4614 if (fd == -1) {
4615 err = got_error_from_errno("got_opentempfd");
4616 goto done;
4619 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4620 if (err)
4621 goto done;
4623 err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4624 if (err)
4625 goto done;
4627 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4628 if (err)
4629 goto done;
4631 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4632 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4633 if (err)
4634 goto done;
4636 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4637 "");
4638 if (err)
4639 goto done;
4641 if (fseek(f1, 0L, SEEK_SET) == -1)
4642 return got_ferror(f1, GOT_ERR_IO);
4643 if (fseek(f2, 0L, SEEK_SET) == -1)
4644 return got_ferror(f2, GOT_ERR_IO);
4646 /* Count the number of actual changes in the diff result. */
4647 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4648 struct diff_chunk_context cc = {};
4649 diff_chunk_context_load_change(&cc, &nchunks_used,
4650 diffreg_result->result, n, 0);
4651 nchanges++;
4653 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4654 int choice;
4655 err = apply_or_reject_change(&choice, &nchunks_used,
4656 diffreg_result->result, n, relpath, f1, f2,
4657 &line_cur1, &line_cur2,
4658 reverse_patch ? NULL : outfile,
4659 reverse_patch ? outfile : NULL,
4660 ++i, nchanges, patch_cb, patch_arg);
4661 if (err)
4662 goto done;
4663 if (choice == GOT_PATCH_CHOICE_YES)
4664 have_content = 1;
4665 else if (choice == GOT_PATCH_CHOICE_QUIT)
4666 break;
4668 if (have_content) {
4669 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4670 reverse_patch ? NULL : outfile,
4671 reverse_patch ? outfile : NULL);
4672 if (err)
4673 goto done;
4675 if (!S_ISLNK(sb2.st_mode)) {
4676 mode_t mode;
4678 mode = apply_umask(sb2.st_mode);
4679 if (fchmod(fileno(outfile), mode) == -1) {
4680 err = got_error_from_errno2("fchmod", path2);
4681 goto done;
4685 done:
4686 free(id_str);
4687 if (fd != -1 && close(fd) == -1 && err == NULL)
4688 err = got_error_from_errno("close");
4689 if (blob)
4690 got_object_blob_close(blob);
4691 free_err = got_diffreg_result_free(diffreg_result);
4692 if (err == NULL)
4693 err = free_err;
4694 if (f1 && fclose(f1) == EOF && err == NULL)
4695 err = got_error_from_errno2("fclose", path1);
4696 if (f2 && fclose(f2) == EOF && err == NULL)
4697 err = got_error_from_errno2("fclose", path2);
4698 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4699 err = got_error_from_errno2("close", path2);
4700 if (outfile && fclose(outfile) == EOF && err == NULL)
4701 err = got_error_from_errno2("fclose", *path_outfile);
4702 if (path1 && unlink(path1) == -1 && err == NULL)
4703 err = got_error_from_errno2("unlink", path1);
4704 if (err || !have_content) {
4705 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4706 err = got_error_from_errno2("unlink", *path_outfile);
4707 free(*path_outfile);
4708 *path_outfile = NULL;
4710 free(path1);
4711 return err;
4714 static const struct got_error *
4715 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4716 const char *relpath, struct got_object_id *blob_id,
4717 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4718 int dirfd, const char *de_name)
4720 struct revert_file_args *a = arg;
4721 const struct got_error *err = NULL;
4722 char *parent_path = NULL;
4723 struct got_fileindex_entry *ie;
4724 struct got_commit_object *base_commit = NULL;
4725 struct got_tree_object *tree = NULL;
4726 struct got_object_id *tree_id = NULL;
4727 const struct got_tree_entry *te = NULL;
4728 char *tree_path = NULL, *te_name;
4729 char *ondisk_path = NULL, *path_content = NULL;
4730 struct got_blob_object *blob = NULL;
4731 int fd = -1;
4733 /* Reverting a staged deletion is a no-op. */
4734 if (status == GOT_STATUS_DELETE &&
4735 staged_status != GOT_STATUS_NO_CHANGE)
4736 return NULL;
4738 if (status == GOT_STATUS_UNVERSIONED)
4739 return (*a->progress_cb)(a->progress_arg,
4740 GOT_STATUS_UNVERSIONED, relpath);
4742 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4743 if (ie == NULL)
4744 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4746 /* Construct in-repository path of tree which contains this blob. */
4747 err = got_path_dirname(&parent_path, ie->path);
4748 if (err) {
4749 if (err->code != GOT_ERR_BAD_PATH)
4750 goto done;
4751 parent_path = strdup("/");
4752 if (parent_path == NULL) {
4753 err = got_error_from_errno("strdup");
4754 goto done;
4757 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4758 tree_path = strdup(parent_path);
4759 if (tree_path == NULL) {
4760 err = got_error_from_errno("strdup");
4761 goto done;
4763 } else {
4764 if (got_path_is_root_dir(parent_path)) {
4765 tree_path = strdup(a->worktree->path_prefix);
4766 if (tree_path == NULL) {
4767 err = got_error_from_errno("strdup");
4768 goto done;
4770 } else {
4771 if (asprintf(&tree_path, "%s/%s",
4772 a->worktree->path_prefix, parent_path) == -1) {
4773 err = got_error_from_errno("asprintf");
4774 goto done;
4779 err = got_object_open_as_commit(&base_commit, a->repo,
4780 a->worktree->base_commit_id);
4781 if (err)
4782 goto done;
4784 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4785 if (err) {
4786 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4787 (status == GOT_STATUS_ADD ||
4788 staged_status == GOT_STATUS_ADD)))
4789 goto done;
4790 } else {
4791 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4792 if (err)
4793 goto done;
4795 err = got_path_basename(&te_name, ie->path);
4796 if (err)
4797 goto done;
4799 te = got_object_tree_find_entry(tree, te_name);
4800 free(te_name);
4801 if (te == NULL && status != GOT_STATUS_ADD &&
4802 staged_status != GOT_STATUS_ADD) {
4803 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4804 goto done;
4808 switch (status) {
4809 case GOT_STATUS_ADD:
4810 if (a->patch_cb) {
4811 int choice = GOT_PATCH_CHOICE_NONE;
4812 err = (*a->patch_cb)(&choice, a->patch_arg,
4813 status, ie->path, NULL, 1, 1);
4814 if (err)
4815 goto done;
4816 if (choice != GOT_PATCH_CHOICE_YES)
4817 break;
4819 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4820 ie->path);
4821 if (err)
4822 goto done;
4823 got_fileindex_entry_remove(a->fileindex, ie);
4824 if (a->unlink_added_files) {
4825 if (asprintf(&ondisk_path, "%s/%s",
4826 got_worktree_get_root_path(a->worktree),
4827 relpath) == -1) {
4828 err = got_error_from_errno("asprintf");
4829 goto done;
4831 if (unlink(ondisk_path) == -1) {
4832 err = got_error_from_errno2("unlink",
4833 ondisk_path);
4834 break;
4837 break;
4838 case GOT_STATUS_DELETE:
4839 if (a->patch_cb) {
4840 int choice = GOT_PATCH_CHOICE_NONE;
4841 err = (*a->patch_cb)(&choice, a->patch_arg,
4842 status, ie->path, NULL, 1, 1);
4843 if (err)
4844 goto done;
4845 if (choice != GOT_PATCH_CHOICE_YES)
4846 break;
4848 /* fall through */
4849 case GOT_STATUS_MODIFY:
4850 case GOT_STATUS_MODE_CHANGE:
4851 case GOT_STATUS_CONFLICT:
4852 case GOT_STATUS_MISSING: {
4853 struct got_object_id id;
4854 if (staged_status == GOT_STATUS_ADD ||
4855 staged_status == GOT_STATUS_MODIFY)
4856 got_fileindex_entry_get_staged_blob_id(&id, ie);
4857 else
4858 got_fileindex_entry_get_blob_id(&id, ie);
4859 fd = got_opentempfd();
4860 if (fd == -1) {
4861 err = got_error_from_errno("got_opentempfd");
4862 goto done;
4865 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4866 if (err)
4867 goto done;
4869 if (asprintf(&ondisk_path, "%s/%s",
4870 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4871 err = got_error_from_errno("asprintf");
4872 goto done;
4875 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4876 status == GOT_STATUS_CONFLICT)) {
4877 int is_bad_symlink = 0;
4878 err = create_patched_content(&path_content, 1, &id,
4879 ondisk_path, dirfd, de_name, ie->path, a->repo,
4880 a->patch_cb, a->patch_arg);
4881 if (err || path_content == NULL)
4882 break;
4883 if (te && S_ISLNK(te->mode)) {
4884 if (unlink(path_content) == -1) {
4885 err = got_error_from_errno2("unlink",
4886 path_content);
4887 break;
4889 err = install_symlink(&is_bad_symlink,
4890 a->worktree, ondisk_path, ie->path,
4891 blob, 0, 1, 0, 0, a->repo,
4892 a->progress_cb, a->progress_arg);
4893 } else {
4894 if (rename(path_content, ondisk_path) == -1) {
4895 err = got_error_from_errno3("rename",
4896 path_content, ondisk_path);
4897 goto done;
4900 } else {
4901 int is_bad_symlink = 0;
4902 if (te && S_ISLNK(te->mode)) {
4903 err = install_symlink(&is_bad_symlink,
4904 a->worktree, ondisk_path, ie->path,
4905 blob, 0, 1, 0, 0, a->repo,
4906 a->progress_cb, a->progress_arg);
4907 } else {
4908 err = install_blob(a->worktree, ondisk_path,
4909 ie->path,
4910 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4911 got_fileindex_perms_to_st(ie), blob,
4912 0, 1, 0, 0, a->repo,
4913 a->progress_cb, a->progress_arg);
4915 if (err)
4916 goto done;
4917 if (status == GOT_STATUS_DELETE ||
4918 status == GOT_STATUS_MODE_CHANGE) {
4919 err = got_fileindex_entry_update(ie,
4920 a->worktree->root_fd, relpath,
4921 blob->id.sha1,
4922 a->worktree->base_commit_id->sha1, 1);
4923 if (err)
4924 goto done;
4926 if (is_bad_symlink) {
4927 got_fileindex_entry_filetype_set(ie,
4928 GOT_FILEIDX_MODE_BAD_SYMLINK);
4931 break;
4933 default:
4934 break;
4936 done:
4937 free(ondisk_path);
4938 free(path_content);
4939 free(parent_path);
4940 free(tree_path);
4941 if (fd != -1 && close(fd) == -1 && err == NULL)
4942 err = got_error_from_errno("close");
4943 if (blob)
4944 got_object_blob_close(blob);
4945 if (tree)
4946 got_object_tree_close(tree);
4947 free(tree_id);
4948 if (base_commit)
4949 got_object_commit_close(base_commit);
4950 return err;
4953 const struct got_error *
4954 got_worktree_revert(struct got_worktree *worktree,
4955 struct got_pathlist_head *paths,
4956 got_worktree_checkout_cb progress_cb, void *progress_arg,
4957 got_worktree_patch_cb patch_cb, void *patch_arg,
4958 struct got_repository *repo)
4960 struct got_fileindex *fileindex = NULL;
4961 char *fileindex_path = NULL;
4962 const struct got_error *err = NULL, *unlockerr = NULL;
4963 const struct got_error *sync_err = NULL;
4964 struct got_pathlist_entry *pe;
4965 struct revert_file_args rfa;
4967 err = lock_worktree(worktree, LOCK_EX);
4968 if (err)
4969 return err;
4971 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4972 if (err)
4973 goto done;
4975 rfa.worktree = worktree;
4976 rfa.fileindex = fileindex;
4977 rfa.progress_cb = progress_cb;
4978 rfa.progress_arg = progress_arg;
4979 rfa.patch_cb = patch_cb;
4980 rfa.patch_arg = patch_arg;
4981 rfa.repo = repo;
4982 rfa.unlink_added_files = 0;
4983 TAILQ_FOREACH(pe, paths, entry) {
4984 err = worktree_status(worktree, pe->path, fileindex, repo,
4985 revert_file, &rfa, NULL, NULL, 1, 0);
4986 if (err)
4987 break;
4989 sync_err = sync_fileindex(fileindex, fileindex_path);
4990 if (sync_err && err == NULL)
4991 err = sync_err;
4992 done:
4993 free(fileindex_path);
4994 if (fileindex)
4995 got_fileindex_free(fileindex);
4996 unlockerr = lock_worktree(worktree, LOCK_SH);
4997 if (unlockerr && err == NULL)
4998 err = unlockerr;
4999 return err;
5002 static void
5003 free_commitable(struct got_commitable *ct)
5005 free(ct->path);
5006 free(ct->in_repo_path);
5007 free(ct->ondisk_path);
5008 free(ct->blob_id);
5009 free(ct->base_blob_id);
5010 free(ct->staged_blob_id);
5011 free(ct->base_commit_id);
5012 free(ct);
5015 struct collect_commitables_arg {
5016 struct got_pathlist_head *commitable_paths;
5017 struct got_repository *repo;
5018 struct got_worktree *worktree;
5019 struct got_fileindex *fileindex;
5020 int have_staged_files;
5021 int allow_bad_symlinks;
5022 int diff_header_shown;
5023 int commit_conflicts;
5024 FILE *diff_outfile;
5025 FILE *f1;
5026 FILE *f2;
5030 * Create a file which contains the target path of a symlink so we can feed
5031 * it as content to the diff engine.
5033 static const struct got_error *
5034 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5035 const char *abspath)
5037 const struct got_error *err = NULL;
5038 char target_path[PATH_MAX];
5039 ssize_t target_len, outlen;
5041 *fd = -1;
5043 if (dirfd != -1) {
5044 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5045 if (target_len == -1)
5046 return got_error_from_errno2("readlinkat", abspath);
5047 } else {
5048 target_len = readlink(abspath, target_path, PATH_MAX);
5049 if (target_len == -1)
5050 return got_error_from_errno2("readlink", abspath);
5053 *fd = got_opentempfd();
5054 if (*fd == -1)
5055 return got_error_from_errno("got_opentempfd");
5057 outlen = write(*fd, target_path, target_len);
5058 if (outlen == -1) {
5059 err = got_error_from_errno("got_opentempfd");
5060 goto done;
5063 if (lseek(*fd, 0, SEEK_SET) == -1) {
5064 err = got_error_from_errno2("lseek", abspath);
5065 goto done;
5067 done:
5068 if (err) {
5069 close(*fd);
5070 *fd = -1;
5072 return err;
5075 static const struct got_error *
5076 append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5077 FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5078 int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5080 const struct got_error *err = NULL;
5081 struct got_blob_object *blob1 = NULL;
5082 int fd = -1, fd1 = -1, fd2 = -1;
5083 FILE *ondisk_file = NULL;
5084 char *label1 = NULL;
5085 struct stat sb;
5086 off_t size1 = 0;
5087 int f2_exists = 0;
5088 char *id_str = NULL;
5090 memset(&sb, 0, sizeof(sb));
5092 if (diff_staged) {
5093 if (ct->staged_status != GOT_STATUS_MODIFY &&
5094 ct->staged_status != GOT_STATUS_ADD &&
5095 ct->staged_status != GOT_STATUS_DELETE)
5096 return NULL;
5097 } else {
5098 if (ct->status != GOT_STATUS_MODIFY &&
5099 ct->status != GOT_STATUS_ADD &&
5100 ct->status != GOT_STATUS_DELETE &&
5101 ct->status != GOT_STATUS_CONFLICT)
5102 return NULL;
5105 err = got_opentemp_truncate(f1);
5106 if (err)
5107 return got_error_from_errno("got_opentemp_truncate");
5108 err = got_opentemp_truncate(f2);
5109 if (err)
5110 return got_error_from_errno("got_opentemp_truncate");
5112 if (!*diff_header_shown) {
5113 err = got_object_id_str(&id_str, worktree->base_commit_id);
5114 if (err)
5115 return err;
5116 fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5117 got_worktree_get_root_path(worktree));
5118 fprintf(diff_outfile, "commit - %s\n", id_str);
5119 fprintf(diff_outfile, "path + %s%s\n",
5120 got_worktree_get_root_path(worktree),
5121 diff_staged ? " (staged changes)" : "");
5122 *diff_header_shown = 1;
5125 if (diff_staged) {
5126 const char *label1 = NULL, *label2 = NULL;
5127 switch (ct->staged_status) {
5128 case GOT_STATUS_MODIFY:
5129 label1 = ct->path;
5130 label2 = ct->path;
5131 break;
5132 case GOT_STATUS_ADD:
5133 label2 = ct->path;
5134 break;
5135 case GOT_STATUS_DELETE:
5136 label1 = ct->path;
5137 break;
5138 default:
5139 return got_error(GOT_ERR_FILE_STATUS);
5141 fd1 = got_opentempfd();
5142 if (fd1 == -1) {
5143 err = got_error_from_errno("got_opentempfd");
5144 goto done;
5146 fd2 = got_opentempfd();
5147 if (fd2 == -1) {
5148 err = got_error_from_errno("got_opentempfd");
5149 goto done;
5151 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5152 fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5153 label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5154 NULL, repo, diff_outfile);
5155 goto done;
5158 fd1 = got_opentempfd();
5159 if (fd1 == -1) {
5160 err = got_error_from_errno("got_opentempfd");
5161 goto done;
5164 if (ct->status != GOT_STATUS_ADD) {
5165 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5166 8192, fd1);
5167 if (err)
5168 goto done;
5171 if (ct->status != GOT_STATUS_DELETE) {
5172 if (dirfd != -1) {
5173 fd = openat(dirfd, de_name,
5174 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5175 if (fd == -1) {
5176 if (!got_err_open_nofollow_on_symlink()) {
5177 err = got_error_from_errno2("openat",
5178 ct->ondisk_path);
5179 goto done;
5181 err = get_symlink_target_file(&fd, dirfd,
5182 de_name, ct->ondisk_path);
5183 if (err)
5184 goto done;
5186 } else {
5187 fd = open(ct->ondisk_path,
5188 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5189 if (fd == -1) {
5190 if (!got_err_open_nofollow_on_symlink()) {
5191 err = got_error_from_errno2("open",
5192 ct->ondisk_path);
5193 goto done;
5195 err = get_symlink_target_file(&fd, dirfd,
5196 de_name, ct->ondisk_path);
5197 if (err)
5198 goto done;
5201 if (fstatat(fd, ct->ondisk_path, &sb,
5202 AT_SYMLINK_NOFOLLOW) == -1) {
5203 err = got_error_from_errno2("fstatat", ct->ondisk_path);
5204 goto done;
5206 ondisk_file = fdopen(fd, "r");
5207 if (ondisk_file == NULL) {
5208 err = got_error_from_errno2("fdopen", ct->ondisk_path);
5209 goto done;
5211 fd = -1;
5212 f2_exists = 1;
5215 if (blob1) {
5216 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5217 f1, blob1);
5218 if (err)
5219 goto done;
5222 err = got_diff_blob_file(blob1, f1, size1, label1,
5223 ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5224 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5225 done:
5226 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5227 err = got_error_from_errno("close");
5228 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5229 err = got_error_from_errno("close");
5230 if (blob1)
5231 got_object_blob_close(blob1);
5232 if (fd != -1 && close(fd) == -1 && err == NULL)
5233 err = got_error_from_errno("close");
5234 if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5235 err = got_error_from_errno("fclose");
5236 return err;
5239 static const struct got_error *
5240 collect_commitables(void *arg, unsigned char status,
5241 unsigned char staged_status, const char *relpath,
5242 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5243 struct got_object_id *commit_id, int dirfd, const char *de_name)
5245 struct collect_commitables_arg *a = arg;
5246 const struct got_error *err = NULL;
5247 struct got_commitable *ct = NULL;
5248 struct got_pathlist_entry *new = NULL;
5249 char *parent_path = NULL, *path = NULL;
5250 struct stat sb;
5252 if (a->have_staged_files) {
5253 if (staged_status != GOT_STATUS_MODIFY &&
5254 staged_status != GOT_STATUS_ADD &&
5255 staged_status != GOT_STATUS_DELETE)
5256 return NULL;
5257 } else {
5258 if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5259 printf("C %s\n", relpath);
5260 return got_error(GOT_ERR_COMMIT_CONFLICT);
5263 if (status != GOT_STATUS_MODIFY &&
5264 status != GOT_STATUS_MODE_CHANGE &&
5265 status != GOT_STATUS_ADD &&
5266 status != GOT_STATUS_DELETE &&
5267 status != GOT_STATUS_CONFLICT)
5268 return NULL;
5271 if (asprintf(&path, "/%s", relpath) == -1) {
5272 err = got_error_from_errno("asprintf");
5273 goto done;
5275 if (strcmp(path, "/") == 0) {
5276 parent_path = strdup("");
5277 if (parent_path == NULL)
5278 return got_error_from_errno("strdup");
5279 } else {
5280 err = got_path_dirname(&parent_path, path);
5281 if (err)
5282 return err;
5285 ct = calloc(1, sizeof(*ct));
5286 if (ct == NULL) {
5287 err = got_error_from_errno("calloc");
5288 goto done;
5291 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5292 relpath) == -1) {
5293 err = got_error_from_errno("asprintf");
5294 goto done;
5297 if (staged_status == GOT_STATUS_ADD ||
5298 staged_status == GOT_STATUS_MODIFY) {
5299 struct got_fileindex_entry *ie;
5300 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5301 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5302 case GOT_FILEIDX_MODE_REGULAR_FILE:
5303 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5304 ct->mode = S_IFREG;
5305 break;
5306 case GOT_FILEIDX_MODE_SYMLINK:
5307 ct->mode = S_IFLNK;
5308 break;
5309 default:
5310 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5311 goto done;
5313 ct->mode |= got_fileindex_entry_perms_get(ie);
5314 } else if (status != GOT_STATUS_DELETE &&
5315 staged_status != GOT_STATUS_DELETE) {
5316 if (dirfd != -1) {
5317 if (fstatat(dirfd, de_name, &sb,
5318 AT_SYMLINK_NOFOLLOW) == -1) {
5319 err = got_error_from_errno2("fstatat",
5320 ct->ondisk_path);
5321 goto done;
5323 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5324 err = got_error_from_errno2("lstat", ct->ondisk_path);
5325 goto done;
5327 ct->mode = sb.st_mode;
5330 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5331 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5332 relpath) == -1) {
5333 err = got_error_from_errno("asprintf");
5334 goto done;
5337 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5338 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5339 int is_bad_symlink;
5340 char target_path[PATH_MAX];
5341 ssize_t target_len;
5342 target_len = readlink(ct->ondisk_path, target_path,
5343 sizeof(target_path));
5344 if (target_len == -1) {
5345 err = got_error_from_errno2("readlink",
5346 ct->ondisk_path);
5347 goto done;
5349 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5350 target_len, ct->ondisk_path, a->worktree->root_path);
5351 if (err)
5352 goto done;
5353 if (is_bad_symlink) {
5354 err = got_error_path(ct->ondisk_path,
5355 GOT_ERR_BAD_SYMLINK);
5356 goto done;
5361 ct->status = status;
5362 ct->staged_status = staged_status;
5363 ct->blob_id = NULL; /* will be filled in when blob gets created */
5364 if (ct->status != GOT_STATUS_ADD &&
5365 ct->staged_status != GOT_STATUS_ADD) {
5366 ct->base_blob_id = got_object_id_dup(blob_id);
5367 if (ct->base_blob_id == NULL) {
5368 err = got_error_from_errno("got_object_id_dup");
5369 goto done;
5371 ct->base_commit_id = got_object_id_dup(commit_id);
5372 if (ct->base_commit_id == NULL) {
5373 err = got_error_from_errno("got_object_id_dup");
5374 goto done;
5377 if (ct->staged_status == GOT_STATUS_ADD ||
5378 ct->staged_status == GOT_STATUS_MODIFY) {
5379 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5380 if (ct->staged_blob_id == NULL) {
5381 err = got_error_from_errno("got_object_id_dup");
5382 goto done;
5385 ct->path = strdup(path);
5386 if (ct->path == NULL) {
5387 err = got_error_from_errno("strdup");
5388 goto done;
5390 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5391 if (err)
5392 goto done;
5394 if (a->diff_outfile && ct && new != NULL) {
5395 err = append_ct_diff(ct, &a->diff_header_shown,
5396 a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5397 a->have_staged_files, a->repo, a->worktree);
5398 if (err)
5399 goto done;
5401 done:
5402 if (ct && (err || new == NULL))
5403 free_commitable(ct);
5404 free(parent_path);
5405 free(path);
5406 return err;
5409 static const struct got_error *write_tree(struct got_object_id **, int *,
5410 struct got_tree_object *, const char *, struct got_pathlist_head *,
5411 got_worktree_status_cb status_cb, void *status_arg,
5412 struct got_repository *);
5414 static const struct got_error *
5415 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5416 struct got_tree_entry *te, const char *parent_path,
5417 struct got_pathlist_head *commitable_paths,
5418 got_worktree_status_cb status_cb, void *status_arg,
5419 struct got_repository *repo)
5421 const struct got_error *err = NULL;
5422 struct got_tree_object *subtree;
5423 char *subpath;
5425 if (asprintf(&subpath, "%s%s%s", parent_path,
5426 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5427 return got_error_from_errno("asprintf");
5429 err = got_object_open_as_tree(&subtree, repo, &te->id);
5430 if (err)
5431 return err;
5433 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5434 commitable_paths, status_cb, status_arg, repo);
5435 got_object_tree_close(subtree);
5436 free(subpath);
5437 return err;
5440 static const struct got_error *
5441 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5443 const struct got_error *err = NULL;
5444 char *ct_parent_path = NULL;
5446 *match = 0;
5448 if (strchr(ct->in_repo_path, '/') == NULL) {
5449 *match = got_path_is_root_dir(path);
5450 return NULL;
5453 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5454 if (err)
5455 return err;
5456 *match = (strcmp(path, ct_parent_path) == 0);
5457 free(ct_parent_path);
5458 return err;
5461 static mode_t
5462 get_ct_file_mode(struct got_commitable *ct)
5464 if (S_ISLNK(ct->mode))
5465 return S_IFLNK;
5467 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5470 static const struct got_error *
5471 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5472 struct got_tree_entry *te, struct got_commitable *ct)
5474 const struct got_error *err = NULL;
5476 *new_te = NULL;
5478 err = got_object_tree_entry_dup(new_te, te);
5479 if (err)
5480 goto done;
5482 (*new_te)->mode = get_ct_file_mode(ct);
5484 if (ct->staged_status == GOT_STATUS_MODIFY)
5485 memcpy(&(*new_te)->id, ct->staged_blob_id,
5486 sizeof((*new_te)->id));
5487 else
5488 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5489 done:
5490 if (err && *new_te) {
5491 free(*new_te);
5492 *new_te = NULL;
5494 return err;
5497 static const struct got_error *
5498 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5499 struct got_commitable *ct)
5501 const struct got_error *err = NULL;
5502 char *ct_name = NULL;
5504 *new_te = NULL;
5506 *new_te = calloc(1, sizeof(**new_te));
5507 if (*new_te == NULL)
5508 return got_error_from_errno("calloc");
5510 err = got_path_basename(&ct_name, ct->path);
5511 if (err)
5512 goto done;
5513 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5514 sizeof((*new_te)->name)) {
5515 err = got_error(GOT_ERR_NO_SPACE);
5516 goto done;
5519 (*new_te)->mode = get_ct_file_mode(ct);
5521 if (ct->staged_status == GOT_STATUS_ADD)
5522 memcpy(&(*new_te)->id, ct->staged_blob_id,
5523 sizeof((*new_te)->id));
5524 else
5525 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5526 done:
5527 free(ct_name);
5528 if (err && *new_te) {
5529 free(*new_te);
5530 *new_te = NULL;
5532 return err;
5535 static const struct got_error *
5536 insert_tree_entry(struct got_tree_entry *new_te,
5537 struct got_pathlist_head *paths)
5539 const struct got_error *err = NULL;
5540 struct got_pathlist_entry *new_pe;
5542 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5543 if (err)
5544 return err;
5545 if (new_pe == NULL)
5546 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5547 return NULL;
5550 static const struct got_error *
5551 report_ct_status(struct got_commitable *ct,
5552 got_worktree_status_cb status_cb, void *status_arg)
5554 const char *ct_path = ct->path;
5555 unsigned char status;
5557 if (status_cb == NULL) /* no commit progress output desired */
5558 return NULL;
5560 while (ct_path[0] == '/')
5561 ct_path++;
5563 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5564 status = ct->staged_status;
5565 else
5566 status = ct->status;
5568 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5569 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5572 static const struct got_error *
5573 match_modified_subtree(int *modified, struct got_tree_entry *te,
5574 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5576 const struct got_error *err = NULL;
5577 struct got_pathlist_entry *pe;
5578 char *te_path;
5580 *modified = 0;
5582 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5583 got_path_is_root_dir(base_tree_path) ? "" : "/",
5584 te->name) == -1)
5585 return got_error_from_errno("asprintf");
5587 TAILQ_FOREACH(pe, commitable_paths, entry) {
5588 struct got_commitable *ct = pe->data;
5589 *modified = got_path_is_child(ct->in_repo_path, te_path,
5590 strlen(te_path));
5591 if (*modified)
5592 break;
5595 free(te_path);
5596 return err;
5599 static const struct got_error *
5600 match_deleted_or_modified_ct(struct got_commitable **ctp,
5601 struct got_tree_entry *te, const char *base_tree_path,
5602 struct got_pathlist_head *commitable_paths)
5604 const struct got_error *err = NULL;
5605 struct got_pathlist_entry *pe;
5607 *ctp = NULL;
5609 TAILQ_FOREACH(pe, commitable_paths, entry) {
5610 struct got_commitable *ct = pe->data;
5611 char *ct_name = NULL;
5612 int path_matches;
5614 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5615 if (ct->status != GOT_STATUS_MODIFY &&
5616 ct->status != GOT_STATUS_MODE_CHANGE &&
5617 ct->status != GOT_STATUS_DELETE &&
5618 ct->status != GOT_STATUS_CONFLICT)
5619 continue;
5620 } else {
5621 if (ct->staged_status != GOT_STATUS_MODIFY &&
5622 ct->staged_status != GOT_STATUS_DELETE)
5623 continue;
5626 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5627 continue;
5629 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5630 if (err)
5631 return err;
5632 if (!path_matches)
5633 continue;
5635 err = got_path_basename(&ct_name, pe->path);
5636 if (err)
5637 return err;
5639 if (strcmp(te->name, ct_name) != 0) {
5640 free(ct_name);
5641 continue;
5643 free(ct_name);
5645 *ctp = ct;
5646 break;
5649 return err;
5652 static const struct got_error *
5653 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5654 const char *child_path, const char *path_base_tree,
5655 struct got_pathlist_head *commitable_paths,
5656 got_worktree_status_cb status_cb, void *status_arg,
5657 struct got_repository *repo)
5659 const struct got_error *err = NULL;
5660 struct got_tree_entry *new_te;
5661 char *subtree_path;
5662 struct got_object_id *id = NULL;
5663 int nentries;
5665 *new_tep = NULL;
5667 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5668 got_path_is_root_dir(path_base_tree) ? "" : "/",
5669 child_path) == -1)
5670 return got_error_from_errno("asprintf");
5672 new_te = calloc(1, sizeof(*new_te));
5673 if (new_te == NULL)
5674 return got_error_from_errno("calloc");
5675 new_te->mode = S_IFDIR;
5677 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5678 sizeof(new_te->name)) {
5679 err = got_error(GOT_ERR_NO_SPACE);
5680 goto done;
5682 err = write_tree(&id, &nentries, NULL, subtree_path,
5683 commitable_paths, status_cb, status_arg, repo);
5684 if (err) {
5685 free(new_te);
5686 goto done;
5688 memcpy(&new_te->id, id, sizeof(new_te->id));
5689 done:
5690 free(id);
5691 free(subtree_path);
5692 if (err == NULL)
5693 *new_tep = new_te;
5694 return err;
5697 static const struct got_error *
5698 write_tree(struct got_object_id **new_tree_id, int *nentries,
5699 struct got_tree_object *base_tree, const char *path_base_tree,
5700 struct got_pathlist_head *commitable_paths,
5701 got_worktree_status_cb status_cb, void *status_arg,
5702 struct got_repository *repo)
5704 const struct got_error *err = NULL;
5705 struct got_pathlist_head paths;
5706 struct got_tree_entry *te, *new_te = NULL;
5707 struct got_pathlist_entry *pe;
5709 TAILQ_INIT(&paths);
5710 *nentries = 0;
5712 /* Insert, and recurse into, newly added entries first. */
5713 TAILQ_FOREACH(pe, commitable_paths, entry) {
5714 struct got_commitable *ct = pe->data;
5715 char *child_path = NULL, *slash;
5717 if ((ct->status != GOT_STATUS_ADD &&
5718 ct->staged_status != GOT_STATUS_ADD) ||
5719 (ct->flags & GOT_COMMITABLE_ADDED))
5720 continue;
5722 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5723 strlen(path_base_tree)))
5724 continue;
5726 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5727 ct->in_repo_path);
5728 if (err)
5729 goto done;
5731 slash = strchr(child_path, '/');
5732 if (slash == NULL) {
5733 err = alloc_added_blob_tree_entry(&new_te, ct);
5734 if (err)
5735 goto done;
5736 err = report_ct_status(ct, status_cb, status_arg);
5737 if (err)
5738 goto done;
5739 ct->flags |= GOT_COMMITABLE_ADDED;
5740 err = insert_tree_entry(new_te, &paths);
5741 if (err)
5742 goto done;
5743 (*nentries)++;
5744 } else {
5745 *slash = '\0'; /* trim trailing path components */
5746 if (base_tree == NULL ||
5747 got_object_tree_find_entry(base_tree, child_path)
5748 == NULL) {
5749 err = make_subtree_for_added_blob(&new_te,
5750 child_path, path_base_tree,
5751 commitable_paths, status_cb, status_arg,
5752 repo);
5753 if (err)
5754 goto done;
5755 err = insert_tree_entry(new_te, &paths);
5756 if (err)
5757 goto done;
5758 (*nentries)++;
5763 if (base_tree) {
5764 int i, nbase_entries;
5765 /* Handle modified and deleted entries. */
5766 nbase_entries = got_object_tree_get_nentries(base_tree);
5767 for (i = 0; i < nbase_entries; i++) {
5768 struct got_commitable *ct = NULL;
5770 te = got_object_tree_get_entry(base_tree, i);
5771 if (got_object_tree_entry_is_submodule(te)) {
5772 /* Entry is a submodule; just copy it. */
5773 err = got_object_tree_entry_dup(&new_te, te);
5774 if (err)
5775 goto done;
5776 err = insert_tree_entry(new_te, &paths);
5777 if (err)
5778 goto done;
5779 (*nentries)++;
5780 continue;
5783 if (S_ISDIR(te->mode)) {
5784 int modified;
5785 err = got_object_tree_entry_dup(&new_te, te);
5786 if (err)
5787 goto done;
5788 err = match_modified_subtree(&modified, te,
5789 path_base_tree, commitable_paths);
5790 if (err)
5791 goto done;
5792 /* Avoid recursion into unmodified subtrees. */
5793 if (modified) {
5794 struct got_object_id *new_id;
5795 int nsubentries;
5796 err = write_subtree(&new_id,
5797 &nsubentries, te,
5798 path_base_tree, commitable_paths,
5799 status_cb, status_arg, repo);
5800 if (err)
5801 goto done;
5802 if (nsubentries == 0) {
5803 /* All entries were deleted. */
5804 free(new_id);
5805 continue;
5807 memcpy(&new_te->id, new_id,
5808 sizeof(new_te->id));
5809 free(new_id);
5811 err = insert_tree_entry(new_te, &paths);
5812 if (err)
5813 goto done;
5814 (*nentries)++;
5815 continue;
5818 err = match_deleted_or_modified_ct(&ct, te,
5819 path_base_tree, commitable_paths);
5820 if (err)
5821 goto done;
5822 if (ct) {
5823 /* NB: Deleted entries get dropped here. */
5824 if (ct->status == GOT_STATUS_MODIFY ||
5825 ct->status == GOT_STATUS_MODE_CHANGE ||
5826 ct->status == GOT_STATUS_CONFLICT ||
5827 ct->staged_status == GOT_STATUS_MODIFY) {
5828 err = alloc_modified_blob_tree_entry(
5829 &new_te, te, ct);
5830 if (err)
5831 goto done;
5832 err = insert_tree_entry(new_te, &paths);
5833 if (err)
5834 goto done;
5835 (*nentries)++;
5837 err = report_ct_status(ct, status_cb,
5838 status_arg);
5839 if (err)
5840 goto done;
5841 } else {
5842 /* Entry is unchanged; just copy it. */
5843 err = got_object_tree_entry_dup(&new_te, te);
5844 if (err)
5845 goto done;
5846 err = insert_tree_entry(new_te, &paths);
5847 if (err)
5848 goto done;
5849 (*nentries)++;
5854 /* Write new list of entries; deleted entries have been dropped. */
5855 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5856 done:
5857 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
5858 return err;
5861 static const struct got_error *
5862 update_fileindex_after_commit(struct got_worktree *worktree,
5863 struct got_pathlist_head *commitable_paths,
5864 struct got_object_id *new_base_commit_id,
5865 struct got_fileindex *fileindex, int have_staged_files)
5867 const struct got_error *err = NULL;
5868 struct got_pathlist_entry *pe;
5869 char *relpath = NULL;
5871 TAILQ_FOREACH(pe, commitable_paths, entry) {
5872 struct got_fileindex_entry *ie;
5873 struct got_commitable *ct = pe->data;
5875 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5877 err = got_path_skip_common_ancestor(&relpath,
5878 worktree->root_path, ct->ondisk_path);
5879 if (err)
5880 goto done;
5882 if (ie) {
5883 if (ct->status == GOT_STATUS_DELETE ||
5884 ct->staged_status == GOT_STATUS_DELETE) {
5885 got_fileindex_entry_remove(fileindex, ie);
5886 } else if (ct->staged_status == GOT_STATUS_ADD ||
5887 ct->staged_status == GOT_STATUS_MODIFY) {
5888 got_fileindex_entry_stage_set(ie,
5889 GOT_FILEIDX_STAGE_NONE);
5890 got_fileindex_entry_staged_filetype_set(ie, 0);
5892 err = got_fileindex_entry_update(ie,
5893 worktree->root_fd, relpath,
5894 ct->staged_blob_id->sha1,
5895 new_base_commit_id->sha1,
5896 !have_staged_files);
5897 } else
5898 err = got_fileindex_entry_update(ie,
5899 worktree->root_fd, relpath,
5900 ct->blob_id->sha1,
5901 new_base_commit_id->sha1,
5902 !have_staged_files);
5903 } else {
5904 err = got_fileindex_entry_alloc(&ie, pe->path);
5905 if (err)
5906 goto done;
5907 err = got_fileindex_entry_update(ie,
5908 worktree->root_fd, relpath, ct->blob_id->sha1,
5909 new_base_commit_id->sha1, 1);
5910 if (err) {
5911 got_fileindex_entry_free(ie);
5912 goto done;
5914 err = got_fileindex_entry_add(fileindex, ie);
5915 if (err) {
5916 got_fileindex_entry_free(ie);
5917 goto done;
5920 free(relpath);
5921 relpath = NULL;
5923 done:
5924 free(relpath);
5925 return err;
5929 static const struct got_error *
5930 check_out_of_date(const char *in_repo_path, unsigned char status,
5931 unsigned char staged_status, struct got_object_id *base_blob_id,
5932 struct got_object_id *base_commit_id,
5933 struct got_object_id *head_commit_id, struct got_repository *repo,
5934 int ood_errcode)
5936 const struct got_error *err = NULL;
5937 struct got_commit_object *commit = NULL;
5938 struct got_object_id *id = NULL;
5940 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5941 /* Trivial case: base commit == head commit */
5942 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5943 return NULL;
5945 * Ensure file content which local changes were based
5946 * on matches file content in the branch head.
5948 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5949 if (err)
5950 goto done;
5951 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5952 if (err) {
5953 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5954 err = got_error(ood_errcode);
5955 goto done;
5956 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5957 err = got_error(ood_errcode);
5958 } else {
5959 /* Require that added files don't exist in the branch head. */
5960 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5961 if (err)
5962 goto done;
5963 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5964 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5965 goto done;
5966 err = id ? got_error(ood_errcode) : NULL;
5968 done:
5969 free(id);
5970 if (commit)
5971 got_object_commit_close(commit);
5972 return err;
5975 static const struct got_error *
5976 commit_worktree(struct got_object_id **new_commit_id,
5977 struct got_pathlist_head *commitable_paths,
5978 struct got_object_id *head_commit_id,
5979 struct got_object_id *parent_id2,
5980 struct got_worktree *worktree,
5981 const char *author, const char *committer, char *diff_path,
5982 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5983 got_worktree_status_cb status_cb, void *status_arg,
5984 struct got_repository *repo)
5986 const struct got_error *err = NULL, *unlockerr = NULL;
5987 struct got_pathlist_entry *pe;
5988 const char *head_ref_name = NULL;
5989 struct got_commit_object *head_commit = NULL;
5990 struct got_reference *head_ref2 = NULL;
5991 struct got_object_id *head_commit_id2 = NULL;
5992 struct got_tree_object *head_tree = NULL;
5993 struct got_object_id *new_tree_id = NULL;
5994 int nentries, nparents = 0;
5995 struct got_object_id_queue parent_ids;
5996 struct got_object_qid *pid = NULL;
5997 char *logmsg = NULL;
5998 time_t timestamp;
6000 *new_commit_id = NULL;
6002 STAILQ_INIT(&parent_ids);
6004 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6005 if (err)
6006 goto done;
6008 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6009 if (err)
6010 goto done;
6012 if (commit_msg_cb != NULL) {
6013 err = commit_msg_cb(commitable_paths, diff_path,
6014 &logmsg, commit_arg);
6015 if (err)
6016 goto done;
6019 if (logmsg == NULL || strlen(logmsg) == 0) {
6020 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6021 goto done;
6024 /* Create blobs from added and modified files and record their IDs. */
6025 TAILQ_FOREACH(pe, commitable_paths, entry) {
6026 struct got_commitable *ct = pe->data;
6027 char *ondisk_path;
6029 /* Blobs for staged files already exist. */
6030 if (ct->staged_status == GOT_STATUS_ADD ||
6031 ct->staged_status == GOT_STATUS_MODIFY)
6032 continue;
6034 if (ct->status != GOT_STATUS_ADD &&
6035 ct->status != GOT_STATUS_MODIFY &&
6036 ct->status != GOT_STATUS_MODE_CHANGE &&
6037 ct->status != GOT_STATUS_CONFLICT)
6038 continue;
6040 if (asprintf(&ondisk_path, "%s/%s",
6041 worktree->root_path, pe->path) == -1) {
6042 err = got_error_from_errno("asprintf");
6043 goto done;
6045 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6046 free(ondisk_path);
6047 if (err)
6048 goto done;
6051 /* Recursively write new tree objects. */
6052 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6053 commitable_paths, status_cb, status_arg, repo);
6054 if (err)
6055 goto done;
6057 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
6058 if (err)
6059 goto done;
6060 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6061 nparents++;
6062 if (parent_id2) {
6063 err = got_object_qid_alloc(&pid, parent_id2);
6064 if (err)
6065 goto done;
6066 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6067 nparents++;
6069 timestamp = time(NULL);
6070 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6071 nparents, author, timestamp, committer, timestamp, logmsg, repo);
6072 if (logmsg != NULL)
6073 free(logmsg);
6074 if (err)
6075 goto done;
6077 /* Check if a concurrent commit to our branch has occurred. */
6078 head_ref_name = got_worktree_get_head_ref_name(worktree);
6079 if (head_ref_name == NULL) {
6080 err = got_error_from_errno("got_worktree_get_head_ref_name");
6081 goto done;
6083 /* Lock the reference here to prevent concurrent modification. */
6084 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6085 if (err)
6086 goto done;
6087 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6088 if (err)
6089 goto done;
6090 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6091 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6092 goto done;
6094 /* Update branch head in repository. */
6095 err = got_ref_change_ref(head_ref2, *new_commit_id);
6096 if (err)
6097 goto done;
6098 err = got_ref_write(head_ref2, repo);
6099 if (err)
6100 goto done;
6102 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6103 if (err)
6104 goto done;
6106 err = ref_base_commit(worktree, repo);
6107 if (err)
6108 goto done;
6109 done:
6110 got_object_id_queue_free(&parent_ids);
6111 if (head_tree)
6112 got_object_tree_close(head_tree);
6113 if (head_commit)
6114 got_object_commit_close(head_commit);
6115 free(head_commit_id2);
6116 if (head_ref2) {
6117 unlockerr = got_ref_unlock(head_ref2);
6118 if (unlockerr && err == NULL)
6119 err = unlockerr;
6120 got_ref_close(head_ref2);
6122 return err;
6125 static const struct got_error *
6126 check_path_is_commitable(const char *path,
6127 struct got_pathlist_head *commitable_paths)
6129 struct got_pathlist_entry *cpe = NULL;
6130 size_t path_len = strlen(path);
6132 TAILQ_FOREACH(cpe, commitable_paths, entry) {
6133 struct got_commitable *ct = cpe->data;
6134 const char *ct_path = ct->path;
6136 while (ct_path[0] == '/')
6137 ct_path++;
6139 if (strcmp(path, ct_path) == 0 ||
6140 got_path_is_child(ct_path, path, path_len))
6141 break;
6144 if (cpe == NULL)
6145 return got_error_path(path, GOT_ERR_BAD_PATH);
6147 return NULL;
6150 static const struct got_error *
6151 check_staged_file(void *arg, struct got_fileindex_entry *ie)
6153 int *have_staged_files = arg;
6155 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6156 *have_staged_files = 1;
6157 return got_error(GOT_ERR_CANCELLED);
6160 return NULL;
6163 static const struct got_error *
6164 check_non_staged_files(struct got_fileindex *fileindex,
6165 struct got_pathlist_head *paths)
6167 struct got_pathlist_entry *pe;
6168 struct got_fileindex_entry *ie;
6170 TAILQ_FOREACH(pe, paths, entry) {
6171 if (pe->path[0] == '\0')
6172 continue;
6173 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6174 if (ie == NULL)
6175 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6176 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6177 return got_error_path(pe->path,
6178 GOT_ERR_FILE_NOT_STAGED);
6181 return NULL;
6184 const struct got_error *
6185 got_worktree_commit(struct got_object_id **new_commit_id,
6186 struct got_worktree *worktree, struct got_pathlist_head *paths,
6187 const char *author, const char *committer, int allow_bad_symlinks,
6188 int show_diff, int commit_conflicts,
6189 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6190 got_worktree_status_cb status_cb, void *status_arg,
6191 struct got_repository *repo)
6193 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6194 struct got_fileindex *fileindex = NULL;
6195 char *fileindex_path = NULL;
6196 struct got_pathlist_head commitable_paths;
6197 struct collect_commitables_arg cc_arg;
6198 struct got_pathlist_entry *pe;
6199 struct got_reference *head_ref = NULL;
6200 struct got_object_id *head_commit_id = NULL;
6201 char *diff_path = NULL;
6202 int have_staged_files = 0;
6204 *new_commit_id = NULL;
6206 memset(&cc_arg, 0, sizeof(cc_arg));
6207 TAILQ_INIT(&commitable_paths);
6209 err = lock_worktree(worktree, LOCK_EX);
6210 if (err)
6211 goto done;
6213 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6214 if (err)
6215 goto done;
6217 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6218 if (err)
6219 goto done;
6221 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6222 if (err)
6223 goto done;
6225 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6226 &have_staged_files);
6227 if (err && err->code != GOT_ERR_CANCELLED)
6228 goto done;
6229 if (have_staged_files) {
6230 err = check_non_staged_files(fileindex, paths);
6231 if (err)
6232 goto done;
6235 cc_arg.commitable_paths = &commitable_paths;
6236 cc_arg.worktree = worktree;
6237 cc_arg.fileindex = fileindex;
6238 cc_arg.repo = repo;
6239 cc_arg.have_staged_files = have_staged_files;
6240 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6241 cc_arg.diff_header_shown = 0;
6242 cc_arg.commit_conflicts = commit_conflicts;
6243 if (show_diff) {
6244 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6245 GOT_TMPDIR_STR "/got", ".diff");
6246 if (err)
6247 goto done;
6248 cc_arg.f1 = got_opentemp();
6249 if (cc_arg.f1 == NULL) {
6250 err = got_error_from_errno("got_opentemp");
6251 goto done;
6253 cc_arg.f2 = got_opentemp();
6254 if (cc_arg.f2 == NULL) {
6255 err = got_error_from_errno("got_opentemp");
6256 goto done;
6260 TAILQ_FOREACH(pe, paths, entry) {
6261 err = worktree_status(worktree, pe->path, fileindex, repo,
6262 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6263 if (err)
6264 goto done;
6267 if (show_diff) {
6268 if (fflush(cc_arg.diff_outfile) == EOF) {
6269 err = got_error_from_errno("fflush");
6270 goto done;
6274 if (TAILQ_EMPTY(&commitable_paths)) {
6275 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6276 goto done;
6279 TAILQ_FOREACH(pe, paths, entry) {
6280 err = check_path_is_commitable(pe->path, &commitable_paths);
6281 if (err)
6282 goto done;
6285 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6286 struct got_commitable *ct = pe->data;
6287 const char *ct_path = ct->in_repo_path;
6289 while (ct_path[0] == '/')
6290 ct_path++;
6291 err = check_out_of_date(ct_path, ct->status,
6292 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6293 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6294 if (err)
6295 goto done;
6299 err = commit_worktree(new_commit_id, &commitable_paths,
6300 head_commit_id, NULL, worktree, author, committer,
6301 (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6302 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6303 if (err)
6304 goto done;
6306 err = update_fileindex_after_commit(worktree, &commitable_paths,
6307 *new_commit_id, fileindex, have_staged_files);
6308 sync_err = sync_fileindex(fileindex, fileindex_path);
6309 if (sync_err && err == NULL)
6310 err = sync_err;
6311 done:
6312 if (fileindex)
6313 got_fileindex_free(fileindex);
6314 free(fileindex_path);
6315 unlockerr = lock_worktree(worktree, LOCK_SH);
6316 if (unlockerr && err == NULL)
6317 err = unlockerr;
6318 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6319 struct got_commitable *ct = pe->data;
6321 free_commitable(ct);
6323 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6324 if (diff_path && unlink(diff_path) == -1 && err == NULL)
6325 err = got_error_from_errno2("unlink", diff_path);
6326 free(diff_path);
6327 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6328 err == NULL)
6329 err = got_error_from_errno("fclose");
6330 return err;
6333 const char *
6334 got_commitable_get_path(struct got_commitable *ct)
6336 return ct->path;
6339 unsigned int
6340 got_commitable_get_status(struct got_commitable *ct)
6342 return ct->status;
6345 struct check_rebase_ok_arg {
6346 struct got_worktree *worktree;
6347 struct got_repository *repo;
6350 static const struct got_error *
6351 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6353 const struct got_error *err = NULL;
6354 struct check_rebase_ok_arg *a = arg;
6355 unsigned char status;
6356 struct stat sb;
6357 char *ondisk_path;
6359 /* Reject rebase of a work tree with mixed base commits. */
6360 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6361 SHA1_DIGEST_LENGTH))
6362 return got_error(GOT_ERR_MIXED_COMMITS);
6364 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6365 == -1)
6366 return got_error_from_errno("asprintf");
6368 /* Reject rebase of a work tree with modified or staged files. */
6369 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6370 free(ondisk_path);
6371 if (err)
6372 return err;
6374 if (status != GOT_STATUS_NO_CHANGE)
6375 return got_error(GOT_ERR_MODIFIED);
6376 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6377 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6379 return NULL;
6382 const struct got_error *
6383 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6384 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6385 struct got_worktree *worktree, struct got_reference *branch,
6386 struct got_repository *repo)
6388 const struct got_error *err = NULL;
6389 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6390 char *branch_ref_name = NULL;
6391 char *fileindex_path = NULL;
6392 struct check_rebase_ok_arg ok_arg;
6393 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6394 struct got_object_id *wt_branch_tip = NULL;
6396 *new_base_branch_ref = NULL;
6397 *tmp_branch = NULL;
6398 *fileindex = NULL;
6400 err = lock_worktree(worktree, LOCK_EX);
6401 if (err)
6402 return err;
6404 err = open_fileindex(fileindex, &fileindex_path, worktree);
6405 if (err)
6406 goto done;
6408 ok_arg.worktree = worktree;
6409 ok_arg.repo = repo;
6410 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6411 &ok_arg);
6412 if (err)
6413 goto done;
6415 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6416 if (err)
6417 goto done;
6419 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6420 if (err)
6421 goto done;
6423 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6424 if (err)
6425 goto done;
6427 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6428 0);
6429 if (err)
6430 goto done;
6432 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6433 if (err)
6434 goto done;
6435 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6436 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6437 goto done;
6440 err = got_ref_alloc_symref(new_base_branch_ref,
6441 new_base_branch_ref_name, wt_branch);
6442 if (err)
6443 goto done;
6444 err = got_ref_write(*new_base_branch_ref, repo);
6445 if (err)
6446 goto done;
6448 /* TODO Lock original branch's ref while rebasing? */
6450 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6451 if (err)
6452 goto done;
6454 err = got_ref_write(branch_ref, repo);
6455 if (err)
6456 goto done;
6458 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6459 worktree->base_commit_id);
6460 if (err)
6461 goto done;
6462 err = got_ref_write(*tmp_branch, repo);
6463 if (err)
6464 goto done;
6466 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6467 if (err)
6468 goto done;
6469 done:
6470 free(fileindex_path);
6471 free(tmp_branch_name);
6472 free(new_base_branch_ref_name);
6473 free(branch_ref_name);
6474 if (branch_ref)
6475 got_ref_close(branch_ref);
6476 if (wt_branch)
6477 got_ref_close(wt_branch);
6478 free(wt_branch_tip);
6479 if (err) {
6480 if (*new_base_branch_ref) {
6481 got_ref_close(*new_base_branch_ref);
6482 *new_base_branch_ref = NULL;
6484 if (*tmp_branch) {
6485 got_ref_close(*tmp_branch);
6486 *tmp_branch = NULL;
6488 if (*fileindex) {
6489 got_fileindex_free(*fileindex);
6490 *fileindex = NULL;
6492 lock_worktree(worktree, LOCK_SH);
6494 return err;
6497 const struct got_error *
6498 got_worktree_rebase_continue(struct got_object_id **commit_id,
6499 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6500 struct got_reference **branch, struct got_fileindex **fileindex,
6501 struct got_worktree *worktree, struct got_repository *repo)
6503 const struct got_error *err;
6504 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6505 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6506 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6507 char *fileindex_path = NULL;
6508 int have_staged_files = 0;
6510 *commit_id = NULL;
6511 *new_base_branch = NULL;
6512 *tmp_branch = NULL;
6513 *branch = NULL;
6514 *fileindex = NULL;
6516 err = lock_worktree(worktree, LOCK_EX);
6517 if (err)
6518 return err;
6520 err = open_fileindex(fileindex, &fileindex_path, worktree);
6521 if (err)
6522 goto done;
6524 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6525 &have_staged_files);
6526 if (err && err->code != GOT_ERR_CANCELLED)
6527 goto done;
6528 if (have_staged_files) {
6529 err = got_error(GOT_ERR_STAGED_PATHS);
6530 goto done;
6533 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6534 if (err)
6535 goto done;
6537 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6538 if (err)
6539 goto done;
6541 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6542 if (err)
6543 goto done;
6545 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6546 if (err)
6547 goto done;
6549 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6550 if (err)
6551 goto done;
6553 err = got_ref_open(branch, repo,
6554 got_ref_get_symref_target(branch_ref), 0);
6555 if (err)
6556 goto done;
6558 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6559 if (err)
6560 goto done;
6562 err = got_ref_resolve(commit_id, repo, commit_ref);
6563 if (err)
6564 goto done;
6566 err = got_ref_open(new_base_branch, repo,
6567 new_base_branch_ref_name, 0);
6568 if (err)
6569 goto done;
6571 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6572 if (err)
6573 goto done;
6574 done:
6575 free(commit_ref_name);
6576 free(branch_ref_name);
6577 free(fileindex_path);
6578 if (commit_ref)
6579 got_ref_close(commit_ref);
6580 if (branch_ref)
6581 got_ref_close(branch_ref);
6582 if (err) {
6583 free(*commit_id);
6584 *commit_id = NULL;
6585 if (*tmp_branch) {
6586 got_ref_close(*tmp_branch);
6587 *tmp_branch = NULL;
6589 if (*new_base_branch) {
6590 got_ref_close(*new_base_branch);
6591 *new_base_branch = NULL;
6593 if (*branch) {
6594 got_ref_close(*branch);
6595 *branch = NULL;
6597 if (*fileindex) {
6598 got_fileindex_free(*fileindex);
6599 *fileindex = NULL;
6601 lock_worktree(worktree, LOCK_SH);
6603 return err;
6606 const struct got_error *
6607 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6609 const struct got_error *err;
6610 char *tmp_branch_name = NULL;
6612 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6613 if (err)
6614 return err;
6616 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6617 free(tmp_branch_name);
6618 return NULL;
6621 static const struct got_error *
6622 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6623 const char *diff_path, char **logmsg, void *arg)
6625 *logmsg = arg;
6626 return NULL;
6629 static const struct got_error *
6630 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6631 const char *path, struct got_object_id *blob_id,
6632 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6633 int dirfd, const char *de_name)
6635 return NULL;
6638 struct collect_merged_paths_arg {
6639 got_worktree_checkout_cb progress_cb;
6640 void *progress_arg;
6641 struct got_pathlist_head *merged_paths;
6644 static const struct got_error *
6645 collect_merged_paths(void *arg, unsigned char status, const char *path)
6647 const struct got_error *err;
6648 struct collect_merged_paths_arg *a = arg;
6649 char *p;
6650 struct got_pathlist_entry *new;
6652 err = (*a->progress_cb)(a->progress_arg, status, path);
6653 if (err)
6654 return err;
6656 if (status != GOT_STATUS_MERGE &&
6657 status != GOT_STATUS_ADD &&
6658 status != GOT_STATUS_DELETE &&
6659 status != GOT_STATUS_CONFLICT)
6660 return NULL;
6662 p = strdup(path);
6663 if (p == NULL)
6664 return got_error_from_errno("strdup");
6666 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6667 if (err || new == NULL)
6668 free(p);
6669 return err;
6672 static const struct got_error *
6673 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6674 int is_rebase, struct got_repository *repo)
6676 const struct got_error *err;
6677 struct got_reference *commit_ref = NULL;
6679 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6680 if (err) {
6681 if (err->code != GOT_ERR_NOT_REF)
6682 goto done;
6683 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6684 if (err)
6685 goto done;
6686 err = got_ref_write(commit_ref, repo);
6687 if (err)
6688 goto done;
6689 } else if (is_rebase) {
6690 struct got_object_id *stored_id;
6691 int cmp;
6693 err = got_ref_resolve(&stored_id, repo, commit_ref);
6694 if (err)
6695 goto done;
6696 cmp = got_object_id_cmp(commit_id, stored_id);
6697 free(stored_id);
6698 if (cmp != 0) {
6699 err = got_error(GOT_ERR_REBASE_COMMITID);
6700 goto done;
6703 done:
6704 if (commit_ref)
6705 got_ref_close(commit_ref);
6706 return err;
6709 static const struct got_error *
6710 rebase_merge_files(struct got_pathlist_head *merged_paths,
6711 const char *commit_ref_name, struct got_worktree *worktree,
6712 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6713 struct got_object_id *commit_id, struct got_repository *repo,
6714 got_worktree_checkout_cb progress_cb, void *progress_arg,
6715 got_cancel_cb cancel_cb, void *cancel_arg)
6717 const struct got_error *err;
6718 struct got_reference *commit_ref = NULL;
6719 struct collect_merged_paths_arg cmp_arg;
6720 char *fileindex_path;
6722 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6724 err = get_fileindex_path(&fileindex_path, worktree);
6725 if (err)
6726 return err;
6728 cmp_arg.progress_cb = progress_cb;
6729 cmp_arg.progress_arg = progress_arg;
6730 cmp_arg.merged_paths = merged_paths;
6731 err = merge_files(worktree, fileindex, fileindex_path,
6732 parent_commit_id, commit_id, repo, collect_merged_paths,
6733 &cmp_arg, cancel_cb, cancel_arg);
6734 if (commit_ref)
6735 got_ref_close(commit_ref);
6736 return err;
6739 const struct got_error *
6740 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6741 struct got_worktree *worktree, struct got_fileindex *fileindex,
6742 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6743 struct got_repository *repo,
6744 got_worktree_checkout_cb progress_cb, void *progress_arg,
6745 got_cancel_cb cancel_cb, void *cancel_arg)
6747 const struct got_error *err;
6748 char *commit_ref_name;
6750 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6751 if (err)
6752 return err;
6754 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6755 if (err)
6756 goto done;
6758 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6759 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6760 progress_arg, cancel_cb, cancel_arg);
6761 done:
6762 free(commit_ref_name);
6763 return err;
6766 const struct got_error *
6767 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6768 struct got_worktree *worktree, struct got_fileindex *fileindex,
6769 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6770 struct got_repository *repo,
6771 got_worktree_checkout_cb progress_cb, void *progress_arg,
6772 got_cancel_cb cancel_cb, void *cancel_arg)
6774 const struct got_error *err;
6775 char *commit_ref_name;
6777 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6778 if (err)
6779 return err;
6781 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6782 if (err)
6783 goto done;
6785 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6786 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6787 progress_arg, cancel_cb, cancel_arg);
6788 done:
6789 free(commit_ref_name);
6790 return err;
6793 static const struct got_error *
6794 rebase_commit(struct got_object_id **new_commit_id,
6795 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6796 struct got_worktree *worktree, struct got_fileindex *fileindex,
6797 struct got_reference *tmp_branch, const char *committer,
6798 struct got_commit_object *orig_commit, const char *new_logmsg,
6799 int allow_conflict, struct got_repository *repo)
6801 const struct got_error *err, *sync_err;
6802 struct got_pathlist_head commitable_paths;
6803 struct collect_commitables_arg cc_arg;
6804 char *fileindex_path = NULL;
6805 struct got_reference *head_ref = NULL;
6806 struct got_object_id *head_commit_id = NULL;
6807 char *logmsg = NULL;
6809 memset(&cc_arg, 0, sizeof(cc_arg));
6810 TAILQ_INIT(&commitable_paths);
6811 *new_commit_id = NULL;
6813 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6815 err = get_fileindex_path(&fileindex_path, worktree);
6816 if (err)
6817 return err;
6819 cc_arg.commitable_paths = &commitable_paths;
6820 cc_arg.worktree = worktree;
6821 cc_arg.repo = repo;
6822 cc_arg.have_staged_files = 0;
6823 cc_arg.commit_conflicts = allow_conflict;
6825 * If possible get the status of individual files directly to
6826 * avoid crawling the entire work tree once per rebased commit.
6828 * Ideally, merged_paths would contain a list of commitables
6829 * we could use so we could skip worktree_status() entirely.
6830 * However, we would then need carefully keep track of cumulative
6831 * effects of operations such as file additions and deletions
6832 * in 'got histedit -f' (folding multiple commits into one),
6833 * and this extra complexity is not really worth it.
6835 if (merged_paths) {
6836 struct got_pathlist_entry *pe;
6837 TAILQ_FOREACH(pe, merged_paths, entry) {
6838 err = worktree_status(worktree, pe->path, fileindex,
6839 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6840 0);
6841 if (err)
6842 goto done;
6844 } else {
6845 err = worktree_status(worktree, "", fileindex, repo,
6846 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6847 if (err)
6848 goto done;
6851 if (TAILQ_EMPTY(&commitable_paths)) {
6852 /* No-op change; commit will be elided. */
6853 err = got_ref_delete(commit_ref, repo);
6854 if (err)
6855 goto done;
6856 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6857 goto done;
6860 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6861 if (err)
6862 goto done;
6864 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6865 if (err)
6866 goto done;
6868 if (new_logmsg) {
6869 logmsg = strdup(new_logmsg);
6870 if (logmsg == NULL) {
6871 err = got_error_from_errno("strdup");
6872 goto done;
6874 } else {
6875 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6876 if (err)
6877 goto done;
6880 /* NB: commit_worktree will call free(logmsg) */
6881 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6882 NULL, worktree, got_object_commit_get_author(orig_commit),
6883 committer ? committer :
6884 got_object_commit_get_committer(orig_commit), NULL,
6885 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6886 if (err)
6887 goto done;
6889 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6890 if (err)
6891 goto done;
6893 err = got_ref_delete(commit_ref, repo);
6894 if (err)
6895 goto done;
6897 err = update_fileindex_after_commit(worktree, &commitable_paths,
6898 *new_commit_id, fileindex, 0);
6899 sync_err = sync_fileindex(fileindex, fileindex_path);
6900 if (sync_err && err == NULL)
6901 err = sync_err;
6902 done:
6903 free(fileindex_path);
6904 free(head_commit_id);
6905 if (head_ref)
6906 got_ref_close(head_ref);
6907 if (err) {
6908 free(*new_commit_id);
6909 *new_commit_id = NULL;
6911 return err;
6914 const struct got_error *
6915 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6916 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6917 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6918 const char *committer, struct got_commit_object *orig_commit,
6919 struct got_object_id *orig_commit_id, int allow_conflict,
6920 struct got_repository *repo)
6922 const struct got_error *err;
6923 char *commit_ref_name;
6924 struct got_reference *commit_ref = NULL;
6925 struct got_object_id *commit_id = NULL;
6927 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6928 if (err)
6929 return err;
6931 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6932 if (err)
6933 goto done;
6934 err = got_ref_resolve(&commit_id, repo, commit_ref);
6935 if (err)
6936 goto done;
6937 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6938 err = got_error(GOT_ERR_REBASE_COMMITID);
6939 goto done;
6942 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6943 worktree, fileindex, tmp_branch, committer, orig_commit,
6944 NULL, allow_conflict, repo);
6945 done:
6946 if (commit_ref)
6947 got_ref_close(commit_ref);
6948 free(commit_ref_name);
6949 free(commit_id);
6950 return err;
6953 const struct got_error *
6954 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6955 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6956 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6957 const char *committer, struct got_commit_object *orig_commit,
6958 struct got_object_id *orig_commit_id, const char *new_logmsg,
6959 int allow_conflict, struct got_repository *repo)
6961 const struct got_error *err;
6962 char *commit_ref_name;
6963 struct got_reference *commit_ref = NULL;
6965 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6966 if (err)
6967 return err;
6969 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6970 if (err)
6971 goto done;
6973 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6974 worktree, fileindex, tmp_branch, committer, orig_commit,
6975 new_logmsg, allow_conflict, repo);
6976 done:
6977 if (commit_ref)
6978 got_ref_close(commit_ref);
6979 free(commit_ref_name);
6980 return err;
6983 const struct got_error *
6984 got_worktree_rebase_postpone(struct got_worktree *worktree,
6985 struct got_fileindex *fileindex)
6987 if (fileindex)
6988 got_fileindex_free(fileindex);
6989 return lock_worktree(worktree, LOCK_SH);
6992 static const struct got_error *
6993 delete_ref(const char *name, struct got_repository *repo)
6995 const struct got_error *err;
6996 struct got_reference *ref;
6998 err = got_ref_open(&ref, repo, name, 0);
6999 if (err) {
7000 if (err->code == GOT_ERR_NOT_REF)
7001 return NULL;
7002 return err;
7005 err = got_ref_delete(ref, repo);
7006 got_ref_close(ref);
7007 return err;
7010 static const struct got_error *
7011 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7013 const struct got_error *err;
7014 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7015 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7017 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7018 if (err)
7019 goto done;
7020 err = delete_ref(tmp_branch_name, repo);
7021 if (err)
7022 goto done;
7024 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7025 if (err)
7026 goto done;
7027 err = delete_ref(new_base_branch_ref_name, repo);
7028 if (err)
7029 goto done;
7031 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7032 if (err)
7033 goto done;
7034 err = delete_ref(branch_ref_name, repo);
7035 if (err)
7036 goto done;
7038 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7039 if (err)
7040 goto done;
7041 err = delete_ref(commit_ref_name, repo);
7042 if (err)
7043 goto done;
7045 done:
7046 free(tmp_branch_name);
7047 free(new_base_branch_ref_name);
7048 free(branch_ref_name);
7049 free(commit_ref_name);
7050 return err;
7053 static const struct got_error *
7054 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7055 struct got_object_id *new_commit_id, struct got_repository *repo)
7057 const struct got_error *err;
7058 struct got_reference *ref = NULL;
7059 struct got_object_id *old_commit_id = NULL;
7060 const char *branch_name = NULL;
7061 char *new_id_str = NULL;
7062 char *refname = NULL;
7064 branch_name = got_ref_get_name(branch);
7065 if (strncmp(branch_name, "refs/heads/", 11) != 0)
7066 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7067 branch_name += 11;
7069 err = got_object_id_str(&new_id_str, new_commit_id);
7070 if (err)
7071 return err;
7073 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7074 new_id_str) == -1) {
7075 err = got_error_from_errno("asprintf");
7076 goto done;
7079 err = got_ref_resolve(&old_commit_id, repo, branch);
7080 if (err)
7081 goto done;
7083 err = got_ref_alloc(&ref, refname, old_commit_id);
7084 if (err)
7085 goto done;
7087 err = got_ref_write(ref, repo);
7088 done:
7089 free(new_id_str);
7090 free(refname);
7091 free(old_commit_id);
7092 if (ref)
7093 got_ref_close(ref);
7094 return err;
7097 const struct got_error *
7098 got_worktree_rebase_complete(struct got_worktree *worktree,
7099 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7100 struct got_reference *rebased_branch, struct got_repository *repo,
7101 int create_backup)
7103 const struct got_error *err, *unlockerr, *sync_err;
7104 struct got_object_id *new_head_commit_id = NULL;
7105 char *fileindex_path = NULL;
7107 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7108 if (err)
7109 return err;
7111 if (create_backup) {
7112 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7113 rebased_branch, new_head_commit_id, repo);
7114 if (err)
7115 goto done;
7118 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7119 if (err)
7120 goto done;
7122 err = got_ref_write(rebased_branch, repo);
7123 if (err)
7124 goto done;
7126 err = got_worktree_set_head_ref(worktree, rebased_branch);
7127 if (err)
7128 goto done;
7130 err = delete_rebase_refs(worktree, repo);
7131 if (err)
7132 goto done;
7134 err = get_fileindex_path(&fileindex_path, worktree);
7135 if (err)
7136 goto done;
7137 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7138 sync_err = sync_fileindex(fileindex, fileindex_path);
7139 if (sync_err && err == NULL)
7140 err = sync_err;
7141 done:
7142 got_fileindex_free(fileindex);
7143 free(fileindex_path);
7144 free(new_head_commit_id);
7145 unlockerr = lock_worktree(worktree, LOCK_SH);
7146 if (unlockerr && err == NULL)
7147 err = unlockerr;
7148 return err;
7151 const struct got_error *
7152 got_worktree_rebase_abort(struct got_worktree *worktree,
7153 struct got_fileindex *fileindex, struct got_repository *repo,
7154 struct got_reference *new_base_branch,
7155 got_worktree_checkout_cb progress_cb, void *progress_arg)
7157 const struct got_error *err, *unlockerr, *sync_err;
7158 struct got_reference *resolved = NULL;
7159 struct got_object_id *commit_id = NULL;
7160 struct got_commit_object *commit = NULL;
7161 char *fileindex_path = NULL;
7162 struct revert_file_args rfa;
7163 struct got_object_id *tree_id = NULL;
7165 err = lock_worktree(worktree, LOCK_EX);
7166 if (err)
7167 return err;
7169 err = got_object_open_as_commit(&commit, repo,
7170 worktree->base_commit_id);
7171 if (err)
7172 goto done;
7174 err = got_ref_open(&resolved, repo,
7175 got_ref_get_symref_target(new_base_branch), 0);
7176 if (err)
7177 goto done;
7179 err = got_worktree_set_head_ref(worktree, resolved);
7180 if (err)
7181 goto done;
7184 * XXX commits to the base branch could have happened while
7185 * we were busy rebasing; should we store the original commit ID
7186 * when rebase begins and read it back here?
7188 err = got_ref_resolve(&commit_id, repo, resolved);
7189 if (err)
7190 goto done;
7192 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7193 if (err)
7194 goto done;
7196 err = got_object_id_by_path(&tree_id, repo, commit,
7197 worktree->path_prefix);
7198 if (err)
7199 goto done;
7201 err = delete_rebase_refs(worktree, repo);
7202 if (err)
7203 goto done;
7205 err = get_fileindex_path(&fileindex_path, worktree);
7206 if (err)
7207 goto done;
7209 rfa.worktree = worktree;
7210 rfa.fileindex = fileindex;
7211 rfa.progress_cb = progress_cb;
7212 rfa.progress_arg = progress_arg;
7213 rfa.patch_cb = NULL;
7214 rfa.patch_arg = NULL;
7215 rfa.repo = repo;
7216 rfa.unlink_added_files = 0;
7217 err = worktree_status(worktree, "", fileindex, repo,
7218 revert_file, &rfa, NULL, NULL, 1, 0);
7219 if (err)
7220 goto sync;
7222 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7223 repo, progress_cb, progress_arg, NULL, NULL);
7224 sync:
7225 sync_err = sync_fileindex(fileindex, fileindex_path);
7226 if (sync_err && err == NULL)
7227 err = sync_err;
7228 done:
7229 got_ref_close(resolved);
7230 free(tree_id);
7231 free(commit_id);
7232 if (commit)
7233 got_object_commit_close(commit);
7234 if (fileindex)
7235 got_fileindex_free(fileindex);
7236 free(fileindex_path);
7238 unlockerr = lock_worktree(worktree, LOCK_SH);
7239 if (unlockerr && err == NULL)
7240 err = unlockerr;
7241 return err;
7244 const struct got_error *
7245 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7246 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7247 struct got_fileindex **fileindex, struct got_worktree *worktree,
7248 struct got_repository *repo)
7250 const struct got_error *err = NULL;
7251 char *tmp_branch_name = NULL;
7252 char *branch_ref_name = NULL;
7253 char *base_commit_ref_name = NULL;
7254 char *fileindex_path = NULL;
7255 struct check_rebase_ok_arg ok_arg;
7256 struct got_reference *wt_branch = NULL;
7257 struct got_reference *base_commit_ref = NULL;
7259 *tmp_branch = NULL;
7260 *branch_ref = NULL;
7261 *base_commit_id = NULL;
7262 *fileindex = NULL;
7264 err = lock_worktree(worktree, LOCK_EX);
7265 if (err)
7266 return err;
7268 err = open_fileindex(fileindex, &fileindex_path, worktree);
7269 if (err)
7270 goto done;
7272 ok_arg.worktree = worktree;
7273 ok_arg.repo = repo;
7274 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7275 &ok_arg);
7276 if (err)
7277 goto done;
7279 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7280 if (err)
7281 goto done;
7283 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7284 if (err)
7285 goto done;
7287 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7288 worktree);
7289 if (err)
7290 goto done;
7292 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7293 0);
7294 if (err)
7295 goto done;
7297 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7298 if (err)
7299 goto done;
7301 err = got_ref_write(*branch_ref, repo);
7302 if (err)
7303 goto done;
7305 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7306 worktree->base_commit_id);
7307 if (err)
7308 goto done;
7309 err = got_ref_write(base_commit_ref, repo);
7310 if (err)
7311 goto done;
7312 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7313 if (*base_commit_id == NULL) {
7314 err = got_error_from_errno("got_object_id_dup");
7315 goto done;
7318 err = got_ref_alloc(tmp_branch, tmp_branch_name,
7319 worktree->base_commit_id);
7320 if (err)
7321 goto done;
7322 err = got_ref_write(*tmp_branch, repo);
7323 if (err)
7324 goto done;
7326 err = got_worktree_set_head_ref(worktree, *tmp_branch);
7327 if (err)
7328 goto done;
7329 done:
7330 free(fileindex_path);
7331 free(tmp_branch_name);
7332 free(branch_ref_name);
7333 free(base_commit_ref_name);
7334 if (wt_branch)
7335 got_ref_close(wt_branch);
7336 if (err) {
7337 if (*branch_ref) {
7338 got_ref_close(*branch_ref);
7339 *branch_ref = NULL;
7341 if (*tmp_branch) {
7342 got_ref_close(*tmp_branch);
7343 *tmp_branch = NULL;
7345 free(*base_commit_id);
7346 if (*fileindex) {
7347 got_fileindex_free(*fileindex);
7348 *fileindex = NULL;
7350 lock_worktree(worktree, LOCK_SH);
7352 return err;
7355 const struct got_error *
7356 got_worktree_histedit_postpone(struct got_worktree *worktree,
7357 struct got_fileindex *fileindex)
7359 if (fileindex)
7360 got_fileindex_free(fileindex);
7361 return lock_worktree(worktree, LOCK_SH);
7364 const struct got_error *
7365 got_worktree_histedit_in_progress(int *in_progress,
7366 struct got_worktree *worktree)
7368 const struct got_error *err;
7369 char *tmp_branch_name = NULL;
7371 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7372 if (err)
7373 return err;
7375 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7376 free(tmp_branch_name);
7377 return NULL;
7380 const struct got_error *
7381 got_worktree_histedit_continue(struct got_object_id **commit_id,
7382 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7383 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7384 struct got_worktree *worktree, struct got_repository *repo)
7386 const struct got_error *err;
7387 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7388 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7389 struct got_reference *commit_ref = NULL;
7390 struct got_reference *base_commit_ref = NULL;
7391 char *fileindex_path = NULL;
7392 int have_staged_files = 0;
7394 *commit_id = NULL;
7395 *tmp_branch = NULL;
7396 *base_commit_id = NULL;
7397 *fileindex = NULL;
7399 err = lock_worktree(worktree, LOCK_EX);
7400 if (err)
7401 return err;
7403 err = open_fileindex(fileindex, &fileindex_path, worktree);
7404 if (err)
7405 goto done;
7407 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7408 &have_staged_files);
7409 if (err && err->code != GOT_ERR_CANCELLED)
7410 goto done;
7411 if (have_staged_files) {
7412 err = got_error(GOT_ERR_STAGED_PATHS);
7413 goto done;
7416 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7417 if (err)
7418 goto done;
7420 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7421 if (err)
7422 goto done;
7424 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7425 if (err)
7426 goto done;
7428 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7429 worktree);
7430 if (err)
7431 goto done;
7433 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7434 if (err)
7435 goto done;
7437 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7438 if (err)
7439 goto done;
7440 err = got_ref_resolve(commit_id, repo, commit_ref);
7441 if (err)
7442 goto done;
7444 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7445 if (err)
7446 goto done;
7447 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7448 if (err)
7449 goto done;
7451 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7452 if (err)
7453 goto done;
7454 done:
7455 free(commit_ref_name);
7456 free(branch_ref_name);
7457 free(fileindex_path);
7458 if (commit_ref)
7459 got_ref_close(commit_ref);
7460 if (base_commit_ref)
7461 got_ref_close(base_commit_ref);
7462 if (err) {
7463 free(*commit_id);
7464 *commit_id = NULL;
7465 free(*base_commit_id);
7466 *base_commit_id = NULL;
7467 if (*tmp_branch) {
7468 got_ref_close(*tmp_branch);
7469 *tmp_branch = NULL;
7471 if (*fileindex) {
7472 got_fileindex_free(*fileindex);
7473 *fileindex = NULL;
7475 lock_worktree(worktree, LOCK_EX);
7477 return err;
7480 static const struct got_error *
7481 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7483 const struct got_error *err;
7484 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7485 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7487 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7488 if (err)
7489 goto done;
7490 err = delete_ref(tmp_branch_name, repo);
7491 if (err)
7492 goto done;
7494 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7495 worktree);
7496 if (err)
7497 goto done;
7498 err = delete_ref(base_commit_ref_name, repo);
7499 if (err)
7500 goto done;
7502 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7503 if (err)
7504 goto done;
7505 err = delete_ref(branch_ref_name, repo);
7506 if (err)
7507 goto done;
7509 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7510 if (err)
7511 goto done;
7512 err = delete_ref(commit_ref_name, repo);
7513 if (err)
7514 goto done;
7515 done:
7516 free(tmp_branch_name);
7517 free(base_commit_ref_name);
7518 free(branch_ref_name);
7519 free(commit_ref_name);
7520 return err;
7523 const struct got_error *
7524 got_worktree_histedit_abort(struct got_worktree *worktree,
7525 struct got_fileindex *fileindex, struct got_repository *repo,
7526 struct got_reference *branch, struct got_object_id *base_commit_id,
7527 got_worktree_checkout_cb progress_cb, void *progress_arg)
7529 const struct got_error *err, *unlockerr, *sync_err;
7530 struct got_reference *resolved = NULL;
7531 char *fileindex_path = NULL;
7532 struct got_commit_object *commit = NULL;
7533 struct got_object_id *tree_id = NULL;
7534 struct revert_file_args rfa;
7536 err = lock_worktree(worktree, LOCK_EX);
7537 if (err)
7538 return err;
7540 err = got_object_open_as_commit(&commit, repo,
7541 worktree->base_commit_id);
7542 if (err)
7543 goto done;
7545 err = got_ref_open(&resolved, repo,
7546 got_ref_get_symref_target(branch), 0);
7547 if (err)
7548 goto done;
7550 err = got_worktree_set_head_ref(worktree, resolved);
7551 if (err)
7552 goto done;
7554 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7555 if (err)
7556 goto done;
7558 err = got_object_id_by_path(&tree_id, repo, commit,
7559 worktree->path_prefix);
7560 if (err)
7561 goto done;
7563 err = delete_histedit_refs(worktree, repo);
7564 if (err)
7565 goto done;
7567 err = get_fileindex_path(&fileindex_path, worktree);
7568 if (err)
7569 goto done;
7571 rfa.worktree = worktree;
7572 rfa.fileindex = fileindex;
7573 rfa.progress_cb = progress_cb;
7574 rfa.progress_arg = progress_arg;
7575 rfa.patch_cb = NULL;
7576 rfa.patch_arg = NULL;
7577 rfa.repo = repo;
7578 rfa.unlink_added_files = 0;
7579 err = worktree_status(worktree, "", fileindex, repo,
7580 revert_file, &rfa, NULL, NULL, 1, 0);
7581 if (err)
7582 goto sync;
7584 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7585 repo, progress_cb, progress_arg, NULL, NULL);
7586 sync:
7587 sync_err = sync_fileindex(fileindex, fileindex_path);
7588 if (sync_err && err == NULL)
7589 err = sync_err;
7590 done:
7591 got_ref_close(resolved);
7592 free(tree_id);
7593 free(fileindex_path);
7595 unlockerr = lock_worktree(worktree, LOCK_SH);
7596 if (unlockerr && err == NULL)
7597 err = unlockerr;
7598 return err;
7601 const struct got_error *
7602 got_worktree_histedit_complete(struct got_worktree *worktree,
7603 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7604 struct got_reference *edited_branch, struct got_repository *repo)
7606 const struct got_error *err, *unlockerr, *sync_err;
7607 struct got_object_id *new_head_commit_id = NULL;
7608 struct got_reference *resolved = NULL;
7609 char *fileindex_path = NULL;
7611 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7612 if (err)
7613 return err;
7615 err = got_ref_open(&resolved, repo,
7616 got_ref_get_symref_target(edited_branch), 0);
7617 if (err)
7618 goto done;
7620 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7621 resolved, new_head_commit_id, repo);
7622 if (err)
7623 goto done;
7625 err = got_ref_change_ref(resolved, new_head_commit_id);
7626 if (err)
7627 goto done;
7629 err = got_ref_write(resolved, repo);
7630 if (err)
7631 goto done;
7633 err = got_worktree_set_head_ref(worktree, resolved);
7634 if (err)
7635 goto done;
7637 err = delete_histedit_refs(worktree, repo);
7638 if (err)
7639 goto done;
7641 err = get_fileindex_path(&fileindex_path, worktree);
7642 if (err)
7643 goto done;
7644 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7645 sync_err = sync_fileindex(fileindex, fileindex_path);
7646 if (sync_err && err == NULL)
7647 err = sync_err;
7648 done:
7649 got_fileindex_free(fileindex);
7650 free(fileindex_path);
7651 free(new_head_commit_id);
7652 unlockerr = lock_worktree(worktree, LOCK_SH);
7653 if (unlockerr && err == NULL)
7654 err = unlockerr;
7655 return err;
7658 const struct got_error *
7659 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7660 struct got_object_id *commit_id, struct got_repository *repo)
7662 const struct got_error *err;
7663 char *commit_ref_name;
7665 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7666 if (err)
7667 return err;
7669 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7670 if (err)
7671 goto done;
7673 err = delete_ref(commit_ref_name, repo);
7674 done:
7675 free(commit_ref_name);
7676 return err;
7679 const struct got_error *
7680 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7681 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7682 struct got_worktree *worktree, const char *refname,
7683 struct got_repository *repo)
7685 const struct got_error *err = NULL;
7686 char *fileindex_path = NULL;
7687 struct check_rebase_ok_arg ok_arg;
7689 *fileindex = NULL;
7690 *branch_ref = NULL;
7691 *base_branch_ref = NULL;
7693 err = lock_worktree(worktree, LOCK_EX);
7694 if (err)
7695 return err;
7697 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7698 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7699 "cannot integrate a branch into itself; "
7700 "update -b or different branch name required");
7701 goto done;
7704 err = open_fileindex(fileindex, &fileindex_path, worktree);
7705 if (err)
7706 goto done;
7708 /* Preconditions are the same as for rebase. */
7709 ok_arg.worktree = worktree;
7710 ok_arg.repo = repo;
7711 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7712 &ok_arg);
7713 if (err)
7714 goto done;
7716 err = got_ref_open(branch_ref, repo, refname, 1);
7717 if (err)
7718 goto done;
7720 err = got_ref_open(base_branch_ref, repo,
7721 got_worktree_get_head_ref_name(worktree), 1);
7722 done:
7723 if (err) {
7724 if (*branch_ref) {
7725 got_ref_close(*branch_ref);
7726 *branch_ref = NULL;
7728 if (*base_branch_ref) {
7729 got_ref_close(*base_branch_ref);
7730 *base_branch_ref = NULL;
7732 if (*fileindex) {
7733 got_fileindex_free(*fileindex);
7734 *fileindex = NULL;
7736 lock_worktree(worktree, LOCK_SH);
7738 return err;
7741 const struct got_error *
7742 got_worktree_integrate_continue(struct got_worktree *worktree,
7743 struct got_fileindex *fileindex, struct got_repository *repo,
7744 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7745 got_worktree_checkout_cb progress_cb, void *progress_arg,
7746 got_cancel_cb cancel_cb, void *cancel_arg)
7748 const struct got_error *err = NULL, *sync_err, *unlockerr;
7749 char *fileindex_path = NULL;
7750 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7751 struct got_commit_object *commit = NULL;
7753 err = get_fileindex_path(&fileindex_path, worktree);
7754 if (err)
7755 goto done;
7757 err = got_ref_resolve(&commit_id, repo, branch_ref);
7758 if (err)
7759 goto done;
7761 err = got_object_open_as_commit(&commit, repo, commit_id);
7762 if (err)
7763 goto done;
7765 err = got_object_id_by_path(&tree_id, repo, commit,
7766 worktree->path_prefix);
7767 if (err)
7768 goto done;
7770 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7771 if (err)
7772 goto done;
7774 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7775 progress_cb, progress_arg, cancel_cb, cancel_arg);
7776 if (err)
7777 goto sync;
7779 err = got_ref_change_ref(base_branch_ref, commit_id);
7780 if (err)
7781 goto sync;
7783 err = got_ref_write(base_branch_ref, repo);
7784 if (err)
7785 goto sync;
7787 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7788 sync:
7789 sync_err = sync_fileindex(fileindex, fileindex_path);
7790 if (sync_err && err == NULL)
7791 err = sync_err;
7793 done:
7794 unlockerr = got_ref_unlock(branch_ref);
7795 if (unlockerr && err == NULL)
7796 err = unlockerr;
7797 got_ref_close(branch_ref);
7799 unlockerr = got_ref_unlock(base_branch_ref);
7800 if (unlockerr && err == NULL)
7801 err = unlockerr;
7802 got_ref_close(base_branch_ref);
7804 got_fileindex_free(fileindex);
7805 free(fileindex_path);
7806 free(tree_id);
7807 if (commit)
7808 got_object_commit_close(commit);
7810 unlockerr = lock_worktree(worktree, LOCK_SH);
7811 if (unlockerr && err == NULL)
7812 err = unlockerr;
7813 return err;
7816 const struct got_error *
7817 got_worktree_integrate_abort(struct got_worktree *worktree,
7818 struct got_fileindex *fileindex, struct got_repository *repo,
7819 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7821 const struct got_error *err = NULL, *unlockerr = NULL;
7823 got_fileindex_free(fileindex);
7825 err = lock_worktree(worktree, LOCK_SH);
7827 unlockerr = got_ref_unlock(branch_ref);
7828 if (unlockerr && err == NULL)
7829 err = unlockerr;
7830 got_ref_close(branch_ref);
7832 unlockerr = got_ref_unlock(base_branch_ref);
7833 if (unlockerr && err == NULL)
7834 err = unlockerr;
7835 got_ref_close(base_branch_ref);
7837 return err;
7840 const struct got_error *
7841 got_worktree_merge_postpone(struct got_worktree *worktree,
7842 struct got_fileindex *fileindex)
7844 const struct got_error *err, *sync_err;
7845 char *fileindex_path = NULL;
7847 err = get_fileindex_path(&fileindex_path, worktree);
7848 if (err)
7849 goto done;
7851 sync_err = sync_fileindex(fileindex, fileindex_path);
7853 err = lock_worktree(worktree, LOCK_SH);
7854 if (sync_err && err == NULL)
7855 err = sync_err;
7856 done:
7857 got_fileindex_free(fileindex);
7858 free(fileindex_path);
7859 return err;
7862 static const struct got_error *
7863 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7865 const struct got_error *err;
7866 char *branch_refname = NULL, *commit_refname = NULL;
7868 err = get_merge_branch_ref_name(&branch_refname, worktree);
7869 if (err)
7870 goto done;
7871 err = delete_ref(branch_refname, repo);
7872 if (err)
7873 goto done;
7875 err = get_merge_commit_ref_name(&commit_refname, worktree);
7876 if (err)
7877 goto done;
7878 err = delete_ref(commit_refname, repo);
7879 if (err)
7880 goto done;
7882 done:
7883 free(branch_refname);
7884 free(commit_refname);
7885 return err;
7888 struct merge_commit_msg_arg {
7889 struct got_worktree *worktree;
7890 const char *branch_name;
7893 static const struct got_error *
7894 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
7895 const char *diff_path, char **logmsg, void *arg)
7897 struct merge_commit_msg_arg *a = arg;
7899 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7900 got_worktree_get_head_ref_name(a->worktree)) == -1)
7901 return got_error_from_errno("asprintf");
7903 return NULL;
7907 const struct got_error *
7908 got_worktree_merge_branch(struct got_worktree *worktree,
7909 struct got_fileindex *fileindex,
7910 struct got_object_id *yca_commit_id,
7911 struct got_object_id *branch_tip,
7912 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7913 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7915 const struct got_error *err;
7916 char *fileindex_path = NULL;
7918 err = get_fileindex_path(&fileindex_path, worktree);
7919 if (err)
7920 goto done;
7922 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7923 worktree);
7924 if (err)
7925 goto done;
7927 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7928 branch_tip, repo, progress_cb, progress_arg,
7929 cancel_cb, cancel_arg);
7930 done:
7931 free(fileindex_path);
7932 return err;
7935 const struct got_error *
7936 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7937 struct got_worktree *worktree, struct got_fileindex *fileindex,
7938 const char *author, const char *committer, int allow_bad_symlinks,
7939 struct got_object_id *branch_tip, const char *branch_name,
7940 int allow_conflict, struct got_repository *repo,
7941 got_worktree_status_cb status_cb, void *status_arg)
7944 const struct got_error *err = NULL, *sync_err;
7945 struct got_pathlist_head commitable_paths;
7946 struct collect_commitables_arg cc_arg;
7947 struct got_pathlist_entry *pe;
7948 struct got_reference *head_ref = NULL;
7949 struct got_object_id *head_commit_id = NULL;
7950 int have_staged_files = 0;
7951 struct merge_commit_msg_arg mcm_arg;
7952 char *fileindex_path = NULL;
7954 memset(&cc_arg, 0, sizeof(cc_arg));
7955 *new_commit_id = NULL;
7957 TAILQ_INIT(&commitable_paths);
7959 err = get_fileindex_path(&fileindex_path, worktree);
7960 if (err)
7961 goto done;
7963 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7964 if (err)
7965 goto done;
7967 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7968 if (err)
7969 goto done;
7971 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7972 &have_staged_files);
7973 if (err && err->code != GOT_ERR_CANCELLED)
7974 goto done;
7975 if (have_staged_files) {
7976 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7977 goto done;
7980 cc_arg.commitable_paths = &commitable_paths;
7981 cc_arg.worktree = worktree;
7982 cc_arg.fileindex = fileindex;
7983 cc_arg.repo = repo;
7984 cc_arg.have_staged_files = have_staged_files;
7985 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7986 cc_arg.commit_conflicts = allow_conflict;
7987 err = worktree_status(worktree, "", fileindex, repo,
7988 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7989 if (err)
7990 goto done;
7992 if (TAILQ_EMPTY(&commitable_paths)) {
7993 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7994 "merge of %s cannot proceed", branch_name);
7995 goto done;
7998 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7999 struct got_commitable *ct = pe->data;
8000 const char *ct_path = ct->in_repo_path;
8002 while (ct_path[0] == '/')
8003 ct_path++;
8004 err = check_out_of_date(ct_path, ct->status,
8005 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
8006 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
8007 if (err)
8008 goto done;
8012 mcm_arg.worktree = worktree;
8013 mcm_arg.branch_name = branch_name;
8014 err = commit_worktree(new_commit_id, &commitable_paths,
8015 head_commit_id, branch_tip, worktree, author, committer, NULL,
8016 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8017 if (err)
8018 goto done;
8020 err = update_fileindex_after_commit(worktree, &commitable_paths,
8021 *new_commit_id, fileindex, have_staged_files);
8022 sync_err = sync_fileindex(fileindex, fileindex_path);
8023 if (sync_err && err == NULL)
8024 err = sync_err;
8025 done:
8026 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8027 struct got_commitable *ct = pe->data;
8029 free_commitable(ct);
8031 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8032 free(fileindex_path);
8033 return err;
8036 const struct got_error *
8037 got_worktree_merge_complete(struct got_worktree *worktree,
8038 struct got_fileindex *fileindex, struct got_repository *repo)
8040 const struct got_error *err, *unlockerr, *sync_err;
8041 char *fileindex_path = NULL;
8043 err = delete_merge_refs(worktree, repo);
8044 if (err)
8045 goto done;
8047 err = get_fileindex_path(&fileindex_path, worktree);
8048 if (err)
8049 goto done;
8050 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8051 sync_err = sync_fileindex(fileindex, fileindex_path);
8052 if (sync_err && err == NULL)
8053 err = sync_err;
8054 done:
8055 got_fileindex_free(fileindex);
8056 free(fileindex_path);
8057 unlockerr = lock_worktree(worktree, LOCK_SH);
8058 if (unlockerr && err == NULL)
8059 err = unlockerr;
8060 return err;
8063 const struct got_error *
8064 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8065 struct got_repository *repo)
8067 const struct got_error *err;
8068 char *branch_refname = NULL;
8069 struct got_reference *branch_ref = NULL;
8071 *in_progress = 0;
8073 err = get_merge_branch_ref_name(&branch_refname, worktree);
8074 if (err)
8075 return err;
8076 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8077 free(branch_refname);
8078 if (err) {
8079 if (err->code != GOT_ERR_NOT_REF)
8080 return err;
8081 } else
8082 *in_progress = 1;
8084 return NULL;
8087 const struct got_error *got_worktree_merge_prepare(
8088 struct got_fileindex **fileindex, struct got_worktree *worktree,
8089 struct got_reference *branch, struct got_repository *repo)
8091 const struct got_error *err = NULL;
8092 char *fileindex_path = NULL;
8093 char *branch_refname = NULL, *commit_refname = NULL;
8094 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
8095 struct got_reference *commit_ref = NULL;
8096 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
8097 struct check_rebase_ok_arg ok_arg;
8099 *fileindex = NULL;
8101 err = lock_worktree(worktree, LOCK_EX);
8102 if (err)
8103 return err;
8105 err = open_fileindex(fileindex, &fileindex_path, worktree);
8106 if (err)
8107 goto done;
8109 /* Preconditions are the same as for rebase. */
8110 ok_arg.worktree = worktree;
8111 ok_arg.repo = repo;
8112 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8113 &ok_arg);
8114 if (err)
8115 goto done;
8117 err = get_merge_branch_ref_name(&branch_refname, worktree);
8118 if (err)
8119 return err;
8121 err = get_merge_commit_ref_name(&commit_refname, worktree);
8122 if (err)
8123 return err;
8125 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8126 0);
8127 if (err)
8128 goto done;
8130 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8131 if (err)
8132 goto done;
8134 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8135 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8136 goto done;
8139 err = got_ref_resolve(&branch_tip, repo, branch);
8140 if (err)
8141 goto done;
8143 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8144 if (err)
8145 goto done;
8146 err = got_ref_write(branch_ref, repo);
8147 if (err)
8148 goto done;
8150 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8151 if (err)
8152 goto done;
8153 err = got_ref_write(commit_ref, repo);
8154 if (err)
8155 goto done;
8157 done:
8158 free(branch_refname);
8159 free(commit_refname);
8160 free(fileindex_path);
8161 if (branch_ref)
8162 got_ref_close(branch_ref);
8163 if (commit_ref)
8164 got_ref_close(commit_ref);
8165 if (wt_branch)
8166 got_ref_close(wt_branch);
8167 free(wt_branch_tip);
8168 if (err) {
8169 if (*fileindex) {
8170 got_fileindex_free(*fileindex);
8171 *fileindex = NULL;
8173 lock_worktree(worktree, LOCK_SH);
8175 return err;
8178 const struct got_error *
8179 got_worktree_merge_continue(char **branch_name,
8180 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8181 struct got_worktree *worktree, struct got_repository *repo)
8183 const struct got_error *err;
8184 char *commit_refname = NULL, *branch_refname = NULL;
8185 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8186 char *fileindex_path = NULL;
8187 int have_staged_files = 0;
8189 *branch_name = NULL;
8190 *branch_tip = NULL;
8191 *fileindex = NULL;
8193 err = lock_worktree(worktree, LOCK_EX);
8194 if (err)
8195 return err;
8197 err = open_fileindex(fileindex, &fileindex_path, worktree);
8198 if (err)
8199 goto done;
8201 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8202 &have_staged_files);
8203 if (err && err->code != GOT_ERR_CANCELLED)
8204 goto done;
8205 if (have_staged_files) {
8206 err = got_error(GOT_ERR_STAGED_PATHS);
8207 goto done;
8210 err = get_merge_branch_ref_name(&branch_refname, worktree);
8211 if (err)
8212 goto done;
8214 err = get_merge_commit_ref_name(&commit_refname, worktree);
8215 if (err)
8216 goto done;
8218 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8219 if (err)
8220 goto done;
8222 if (!got_ref_is_symbolic(branch_ref)) {
8223 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8224 "%s is not a symbolic reference",
8225 got_ref_get_name(branch_ref));
8226 goto done;
8228 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8229 if (*branch_name == NULL) {
8230 err = got_error_from_errno("strdup");
8231 goto done;
8234 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8235 if (err)
8236 goto done;
8238 err = got_ref_resolve(branch_tip, repo, commit_ref);
8239 if (err)
8240 goto done;
8241 done:
8242 free(commit_refname);
8243 free(branch_refname);
8244 free(fileindex_path);
8245 if (commit_ref)
8246 got_ref_close(commit_ref);
8247 if (branch_ref)
8248 got_ref_close(branch_ref);
8249 if (err) {
8250 if (*branch_name) {
8251 free(*branch_name);
8252 *branch_name = NULL;
8254 free(*branch_tip);
8255 *branch_tip = NULL;
8256 if (*fileindex) {
8257 got_fileindex_free(*fileindex);
8258 *fileindex = NULL;
8260 lock_worktree(worktree, LOCK_SH);
8262 return err;
8265 const struct got_error *
8266 got_worktree_merge_abort(struct got_worktree *worktree,
8267 struct got_fileindex *fileindex, struct got_repository *repo,
8268 got_worktree_checkout_cb progress_cb, void *progress_arg)
8270 const struct got_error *err, *unlockerr, *sync_err;
8271 struct got_object_id *commit_id = NULL;
8272 struct got_commit_object *commit = NULL;
8273 char *fileindex_path = NULL;
8274 struct revert_file_args rfa;
8275 struct got_object_id *tree_id = NULL;
8277 err = got_object_open_as_commit(&commit, repo,
8278 worktree->base_commit_id);
8279 if (err)
8280 goto done;
8282 err = got_object_id_by_path(&tree_id, repo, commit,
8283 worktree->path_prefix);
8284 if (err)
8285 goto done;
8287 err = delete_merge_refs(worktree, repo);
8288 if (err)
8289 goto done;
8291 err = get_fileindex_path(&fileindex_path, worktree);
8292 if (err)
8293 goto done;
8295 rfa.worktree = worktree;
8296 rfa.fileindex = fileindex;
8297 rfa.progress_cb = progress_cb;
8298 rfa.progress_arg = progress_arg;
8299 rfa.patch_cb = NULL;
8300 rfa.patch_arg = NULL;
8301 rfa.repo = repo;
8302 rfa.unlink_added_files = 1;
8303 err = worktree_status(worktree, "", fileindex, repo,
8304 revert_file, &rfa, NULL, NULL, 1, 0);
8305 if (err)
8306 goto sync;
8308 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8309 repo, progress_cb, progress_arg, NULL, NULL);
8310 sync:
8311 sync_err = sync_fileindex(fileindex, fileindex_path);
8312 if (sync_err && err == NULL)
8313 err = sync_err;
8314 done:
8315 free(tree_id);
8316 free(commit_id);
8317 if (commit)
8318 got_object_commit_close(commit);
8319 if (fileindex)
8320 got_fileindex_free(fileindex);
8321 free(fileindex_path);
8323 unlockerr = lock_worktree(worktree, LOCK_SH);
8324 if (unlockerr && err == NULL)
8325 err = unlockerr;
8326 return err;
8329 struct check_stage_ok_arg {
8330 struct got_object_id *head_commit_id;
8331 struct got_worktree *worktree;
8332 struct got_fileindex *fileindex;
8333 struct got_repository *repo;
8334 int have_changes;
8337 static const struct got_error *
8338 check_stage_ok(void *arg, unsigned char status,
8339 unsigned char staged_status, const char *relpath,
8340 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8341 struct got_object_id *commit_id, int dirfd, const char *de_name)
8343 struct check_stage_ok_arg *a = arg;
8344 const struct got_error *err = NULL;
8345 struct got_fileindex_entry *ie;
8346 struct got_object_id base_commit_id;
8347 struct got_object_id *base_commit_idp = NULL;
8348 char *in_repo_path = NULL, *p;
8350 if (status == GOT_STATUS_UNVERSIONED ||
8351 status == GOT_STATUS_NO_CHANGE)
8352 return NULL;
8353 if (status == GOT_STATUS_NONEXISTENT)
8354 return got_error_set_errno(ENOENT, relpath);
8356 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8357 if (ie == NULL)
8358 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8360 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8361 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8362 relpath) == -1)
8363 return got_error_from_errno("asprintf");
8365 if (got_fileindex_entry_has_commit(ie)) {
8366 base_commit_idp = got_fileindex_entry_get_commit_id(
8367 &base_commit_id, ie);
8370 if (status == GOT_STATUS_CONFLICT) {
8371 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8372 goto done;
8373 } else if (status != GOT_STATUS_ADD &&
8374 status != GOT_STATUS_MODIFY &&
8375 status != GOT_STATUS_DELETE) {
8376 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8377 goto done;
8380 a->have_changes = 1;
8382 p = in_repo_path;
8383 while (p[0] == '/')
8384 p++;
8385 err = check_out_of_date(p, status, staged_status,
8386 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8387 GOT_ERR_STAGE_OUT_OF_DATE);
8388 done:
8389 free(in_repo_path);
8390 return err;
8393 struct stage_path_arg {
8394 struct got_worktree *worktree;
8395 struct got_fileindex *fileindex;
8396 struct got_repository *repo;
8397 got_worktree_status_cb status_cb;
8398 void *status_arg;
8399 got_worktree_patch_cb patch_cb;
8400 void *patch_arg;
8401 int staged_something;
8402 int allow_bad_symlinks;
8405 static const struct got_error *
8406 stage_path(void *arg, unsigned char status,
8407 unsigned char staged_status, const char *relpath,
8408 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8409 struct got_object_id *commit_id, int dirfd, const char *de_name)
8411 struct stage_path_arg *a = arg;
8412 const struct got_error *err = NULL;
8413 struct got_fileindex_entry *ie;
8414 char *ondisk_path = NULL, *path_content = NULL;
8415 uint32_t stage;
8416 struct got_object_id *new_staged_blob_id = NULL;
8417 struct stat sb;
8419 if (status == GOT_STATUS_UNVERSIONED)
8420 return NULL;
8422 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8423 if (ie == NULL)
8424 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8426 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8427 relpath)== -1)
8428 return got_error_from_errno("asprintf");
8430 switch (status) {
8431 case GOT_STATUS_ADD:
8432 case GOT_STATUS_MODIFY:
8433 /* XXX could sb.st_mode be passed in by our caller? */
8434 if (lstat(ondisk_path, &sb) == -1) {
8435 err = got_error_from_errno2("lstat", ondisk_path);
8436 break;
8438 if (a->patch_cb) {
8439 if (status == GOT_STATUS_ADD) {
8440 int choice = GOT_PATCH_CHOICE_NONE;
8441 err = (*a->patch_cb)(&choice, a->patch_arg,
8442 status, ie->path, NULL, 1, 1);
8443 if (err)
8444 break;
8445 if (choice != GOT_PATCH_CHOICE_YES)
8446 break;
8447 } else {
8448 err = create_patched_content(&path_content, 0,
8449 staged_blob_id ? staged_blob_id : blob_id,
8450 ondisk_path, dirfd, de_name, ie->path,
8451 a->repo, a->patch_cb, a->patch_arg);
8452 if (err || path_content == NULL)
8453 break;
8456 err = got_object_blob_create(&new_staged_blob_id,
8457 path_content ? path_content : ondisk_path, a->repo);
8458 if (err)
8459 break;
8460 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8461 SHA1_DIGEST_LENGTH);
8462 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8463 stage = GOT_FILEIDX_STAGE_ADD;
8464 else
8465 stage = GOT_FILEIDX_STAGE_MODIFY;
8466 got_fileindex_entry_stage_set(ie, stage);
8467 if (S_ISLNK(sb.st_mode)) {
8468 int is_bad_symlink = 0;
8469 if (!a->allow_bad_symlinks) {
8470 char target_path[PATH_MAX];
8471 ssize_t target_len;
8472 target_len = readlink(ondisk_path, target_path,
8473 sizeof(target_path));
8474 if (target_len == -1) {
8475 err = got_error_from_errno2("readlink",
8476 ondisk_path);
8477 break;
8479 err = is_bad_symlink_target(&is_bad_symlink,
8480 target_path, target_len, ondisk_path,
8481 a->worktree->root_path);
8482 if (err)
8483 break;
8484 if (is_bad_symlink) {
8485 err = got_error_path(ondisk_path,
8486 GOT_ERR_BAD_SYMLINK);
8487 break;
8490 if (is_bad_symlink)
8491 got_fileindex_entry_staged_filetype_set(ie,
8492 GOT_FILEIDX_MODE_BAD_SYMLINK);
8493 else
8494 got_fileindex_entry_staged_filetype_set(ie,
8495 GOT_FILEIDX_MODE_SYMLINK);
8496 } else {
8497 got_fileindex_entry_staged_filetype_set(ie,
8498 GOT_FILEIDX_MODE_REGULAR_FILE);
8500 a->staged_something = 1;
8501 if (a->status_cb == NULL)
8502 break;
8503 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8504 get_staged_status(ie), relpath, blob_id,
8505 new_staged_blob_id, NULL, dirfd, de_name);
8506 if (err)
8507 break;
8509 * When staging the reverse of the staged diff,
8510 * implicitly unstage the file.
8512 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8513 sizeof(ie->blob_sha1)) == 0) {
8514 got_fileindex_entry_stage_set(ie,
8515 GOT_FILEIDX_STAGE_NONE);
8517 break;
8518 case GOT_STATUS_DELETE:
8519 if (staged_status == GOT_STATUS_DELETE)
8520 break;
8521 if (a->patch_cb) {
8522 int choice = GOT_PATCH_CHOICE_NONE;
8523 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8524 ie->path, NULL, 1, 1);
8525 if (err)
8526 break;
8527 if (choice == GOT_PATCH_CHOICE_NO)
8528 break;
8529 if (choice != GOT_PATCH_CHOICE_YES) {
8530 err = got_error(GOT_ERR_PATCH_CHOICE);
8531 break;
8534 stage = GOT_FILEIDX_STAGE_DELETE;
8535 got_fileindex_entry_stage_set(ie, stage);
8536 a->staged_something = 1;
8537 if (a->status_cb == NULL)
8538 break;
8539 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8540 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8541 de_name);
8542 break;
8543 case GOT_STATUS_NO_CHANGE:
8544 break;
8545 case GOT_STATUS_CONFLICT:
8546 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8547 break;
8548 case GOT_STATUS_NONEXISTENT:
8549 err = got_error_set_errno(ENOENT, relpath);
8550 break;
8551 default:
8552 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8553 break;
8556 if (path_content && unlink(path_content) == -1 && err == NULL)
8557 err = got_error_from_errno2("unlink", path_content);
8558 free(path_content);
8559 free(ondisk_path);
8560 free(new_staged_blob_id);
8561 return err;
8564 const struct got_error *
8565 got_worktree_stage(struct got_worktree *worktree,
8566 struct got_pathlist_head *paths,
8567 got_worktree_status_cb status_cb, void *status_arg,
8568 got_worktree_patch_cb patch_cb, void *patch_arg,
8569 int allow_bad_symlinks, struct got_repository *repo)
8571 const struct got_error *err = NULL, *sync_err, *unlockerr;
8572 struct got_pathlist_entry *pe;
8573 struct got_fileindex *fileindex = NULL;
8574 char *fileindex_path = NULL;
8575 struct got_reference *head_ref = NULL;
8576 struct got_object_id *head_commit_id = NULL;
8577 struct check_stage_ok_arg oka;
8578 struct stage_path_arg spa;
8580 err = lock_worktree(worktree, LOCK_EX);
8581 if (err)
8582 return err;
8584 err = got_ref_open(&head_ref, repo,
8585 got_worktree_get_head_ref_name(worktree), 0);
8586 if (err)
8587 goto done;
8588 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8589 if (err)
8590 goto done;
8591 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8592 if (err)
8593 goto done;
8595 /* Check pre-conditions before staging anything. */
8596 oka.head_commit_id = head_commit_id;
8597 oka.worktree = worktree;
8598 oka.fileindex = fileindex;
8599 oka.repo = repo;
8600 oka.have_changes = 0;
8601 TAILQ_FOREACH(pe, paths, entry) {
8602 err = worktree_status(worktree, pe->path, fileindex, repo,
8603 check_stage_ok, &oka, NULL, NULL, 1, 0);
8604 if (err)
8605 goto done;
8607 if (!oka.have_changes) {
8608 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8609 goto done;
8612 spa.worktree = worktree;
8613 spa.fileindex = fileindex;
8614 spa.repo = repo;
8615 spa.patch_cb = patch_cb;
8616 spa.patch_arg = patch_arg;
8617 spa.status_cb = status_cb;
8618 spa.status_arg = status_arg;
8619 spa.staged_something = 0;
8620 spa.allow_bad_symlinks = allow_bad_symlinks;
8621 TAILQ_FOREACH(pe, paths, entry) {
8622 err = worktree_status(worktree, pe->path, fileindex, repo,
8623 stage_path, &spa, NULL, NULL, 1, 0);
8624 if (err)
8625 goto done;
8627 if (!spa.staged_something) {
8628 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8629 goto done;
8632 sync_err = sync_fileindex(fileindex, fileindex_path);
8633 if (sync_err && err == NULL)
8634 err = sync_err;
8635 done:
8636 if (head_ref)
8637 got_ref_close(head_ref);
8638 free(head_commit_id);
8639 free(fileindex_path);
8640 if (fileindex)
8641 got_fileindex_free(fileindex);
8642 unlockerr = lock_worktree(worktree, LOCK_SH);
8643 if (unlockerr && err == NULL)
8644 err = unlockerr;
8645 return err;
8648 struct unstage_path_arg {
8649 struct got_worktree *worktree;
8650 struct got_fileindex *fileindex;
8651 struct got_repository *repo;
8652 got_worktree_checkout_cb progress_cb;
8653 void *progress_arg;
8654 got_worktree_patch_cb patch_cb;
8655 void *patch_arg;
8658 static const struct got_error *
8659 create_unstaged_content(char **path_unstaged_content,
8660 char **path_new_staged_content, struct got_object_id *blob_id,
8661 struct got_object_id *staged_blob_id, const char *relpath,
8662 struct got_repository *repo,
8663 got_worktree_patch_cb patch_cb, void *patch_arg)
8665 const struct got_error *err, *free_err;
8666 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8667 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8668 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8669 struct got_diffreg_result *diffreg_result = NULL;
8670 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8671 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8672 int fd1 = -1, fd2 = -1;
8674 *path_unstaged_content = NULL;
8675 *path_new_staged_content = NULL;
8677 err = got_object_id_str(&label1, blob_id);
8678 if (err)
8679 return err;
8681 fd1 = got_opentempfd();
8682 if (fd1 == -1) {
8683 err = got_error_from_errno("got_opentempfd");
8684 goto done;
8686 fd2 = got_opentempfd();
8687 if (fd2 == -1) {
8688 err = got_error_from_errno("got_opentempfd");
8689 goto done;
8692 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8693 if (err)
8694 goto done;
8696 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
8697 if (err)
8698 goto done;
8700 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8701 if (err)
8702 goto done;
8704 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8705 fd2);
8706 if (err)
8707 goto done;
8709 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
8710 if (err)
8711 goto done;
8713 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8714 if (err)
8715 goto done;
8717 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8718 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8719 if (err)
8720 goto done;
8722 err = got_opentemp_named(path_unstaged_content, &outfile,
8723 "got-unstaged-content", "");
8724 if (err)
8725 goto done;
8726 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8727 "got-new-staged-content", "");
8728 if (err)
8729 goto done;
8731 if (fseek(f1, 0L, SEEK_SET) == -1) {
8732 err = got_ferror(f1, GOT_ERR_IO);
8733 goto done;
8735 if (fseek(f2, 0L, SEEK_SET) == -1) {
8736 err = got_ferror(f2, GOT_ERR_IO);
8737 goto done;
8739 /* Count the number of actual changes in the diff result. */
8740 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8741 struct diff_chunk_context cc = {};
8742 diff_chunk_context_load_change(&cc, &nchunks_used,
8743 diffreg_result->result, n, 0);
8744 nchanges++;
8746 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8747 int choice;
8748 err = apply_or_reject_change(&choice, &nchunks_used,
8749 diffreg_result->result, n, relpath, f1, f2,
8750 &line_cur1, &line_cur2,
8751 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8752 if (err)
8753 goto done;
8754 if (choice == GOT_PATCH_CHOICE_YES)
8755 have_content = 1;
8756 else
8757 have_rejected_content = 1;
8758 if (choice == GOT_PATCH_CHOICE_QUIT)
8759 break;
8761 if (have_content || have_rejected_content)
8762 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8763 outfile, rejectfile);
8764 done:
8765 free(label1);
8766 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8767 err = got_error_from_errno("close");
8768 if (blob)
8769 got_object_blob_close(blob);
8770 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8771 err = got_error_from_errno("close");
8772 if (staged_blob)
8773 got_object_blob_close(staged_blob);
8774 free_err = got_diffreg_result_free(diffreg_result);
8775 if (free_err && err == NULL)
8776 err = free_err;
8777 if (f1 && fclose(f1) == EOF && err == NULL)
8778 err = got_error_from_errno2("fclose", path1);
8779 if (f2 && fclose(f2) == EOF && err == NULL)
8780 err = got_error_from_errno2("fclose", path2);
8781 if (outfile && fclose(outfile) == EOF && err == NULL)
8782 err = got_error_from_errno2("fclose", *path_unstaged_content);
8783 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8784 err = got_error_from_errno2("fclose", *path_new_staged_content);
8785 if (path1 && unlink(path1) == -1 && err == NULL)
8786 err = got_error_from_errno2("unlink", path1);
8787 if (path2 && unlink(path2) == -1 && err == NULL)
8788 err = got_error_from_errno2("unlink", path2);
8789 if (err || !have_content) {
8790 if (*path_unstaged_content &&
8791 unlink(*path_unstaged_content) == -1 && err == NULL)
8792 err = got_error_from_errno2("unlink",
8793 *path_unstaged_content);
8794 free(*path_unstaged_content);
8795 *path_unstaged_content = NULL;
8797 if (err || !have_content || !have_rejected_content) {
8798 if (*path_new_staged_content &&
8799 unlink(*path_new_staged_content) == -1 && err == NULL)
8800 err = got_error_from_errno2("unlink",
8801 *path_new_staged_content);
8802 free(*path_new_staged_content);
8803 *path_new_staged_content = NULL;
8805 free(path1);
8806 free(path2);
8807 return err;
8810 static const struct got_error *
8811 unstage_hunks(struct got_object_id *staged_blob_id,
8812 struct got_blob_object *blob_base,
8813 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8814 const char *ondisk_path, const char *label_orig,
8815 struct got_worktree *worktree, struct got_repository *repo,
8816 got_worktree_patch_cb patch_cb, void *patch_arg,
8817 got_worktree_checkout_cb progress_cb, void *progress_arg)
8819 const struct got_error *err = NULL;
8820 char *path_unstaged_content = NULL;
8821 char *path_new_staged_content = NULL;
8822 char *parent = NULL, *base_path = NULL;
8823 char *blob_base_path = NULL;
8824 struct got_object_id *new_staged_blob_id = NULL;
8825 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8826 struct stat sb;
8828 err = create_unstaged_content(&path_unstaged_content,
8829 &path_new_staged_content, blob_id, staged_blob_id,
8830 ie->path, repo, patch_cb, patch_arg);
8831 if (err)
8832 return err;
8834 if (path_unstaged_content == NULL)
8835 return NULL;
8837 if (path_new_staged_content) {
8838 err = got_object_blob_create(&new_staged_blob_id,
8839 path_new_staged_content, repo);
8840 if (err)
8841 goto done;
8844 f = fopen(path_unstaged_content, "re");
8845 if (f == NULL) {
8846 err = got_error_from_errno2("fopen",
8847 path_unstaged_content);
8848 goto done;
8850 if (fstat(fileno(f), &sb) == -1) {
8851 err = got_error_from_errno2("fstat", path_unstaged_content);
8852 goto done;
8854 if (got_fileindex_entry_staged_filetype_get(ie) ==
8855 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8856 char link_target[PATH_MAX];
8857 size_t r;
8858 r = fread(link_target, 1, sizeof(link_target), f);
8859 if (r == 0 && ferror(f)) {
8860 err = got_error_from_errno("fread");
8861 goto done;
8863 if (r >= sizeof(link_target)) { /* should not happen */
8864 err = got_error(GOT_ERR_NO_SPACE);
8865 goto done;
8867 link_target[r] = '\0';
8868 err = merge_symlink(worktree, blob_base,
8869 ondisk_path, ie->path, label_orig, link_target,
8870 worktree->base_commit_id, repo, progress_cb,
8871 progress_arg);
8872 } else {
8873 int local_changes_subsumed;
8875 err = got_path_dirname(&parent, ondisk_path);
8876 if (err)
8877 return err;
8879 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8880 parent) == -1) {
8881 err = got_error_from_errno("asprintf");
8882 base_path = NULL;
8883 goto done;
8886 err = got_opentemp_named(&blob_base_path, &f_base,
8887 base_path, "");
8888 if (err)
8889 goto done;
8890 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8891 blob_base);
8892 if (err)
8893 goto done;
8896 * In order the run a 3-way merge with a symlink we copy the symlink's
8897 * target path into a temporary file and use that file with diff3.
8899 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8900 err = dump_symlink_target_path_to_file(&f_deriv2,
8901 ondisk_path);
8902 if (err)
8903 goto done;
8904 } else {
8905 int fd;
8906 fd = open(ondisk_path,
8907 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8908 if (fd == -1) {
8909 err = got_error_from_errno2("open", ondisk_path);
8910 goto done;
8912 f_deriv2 = fdopen(fd, "r");
8913 if (f_deriv2 == NULL) {
8914 err = got_error_from_errno2("fdopen", ondisk_path);
8915 close(fd);
8916 goto done;
8920 err = merge_file(&local_changes_subsumed, worktree,
8921 f_base, f, f_deriv2, ondisk_path, ie->path,
8922 got_fileindex_perms_to_st(ie),
8923 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8924 repo, progress_cb, progress_arg);
8926 if (err)
8927 goto done;
8929 if (new_staged_blob_id) {
8930 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8931 SHA1_DIGEST_LENGTH);
8932 } else {
8933 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8934 got_fileindex_entry_staged_filetype_set(ie, 0);
8936 done:
8937 free(new_staged_blob_id);
8938 if (path_unstaged_content &&
8939 unlink(path_unstaged_content) == -1 && err == NULL)
8940 err = got_error_from_errno2("unlink", path_unstaged_content);
8941 if (path_new_staged_content &&
8942 unlink(path_new_staged_content) == -1 && err == NULL)
8943 err = got_error_from_errno2("unlink", path_new_staged_content);
8944 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8945 err = got_error_from_errno2("unlink", blob_base_path);
8946 if (f_base && fclose(f_base) == EOF && err == NULL)
8947 err = got_error_from_errno2("fclose", path_unstaged_content);
8948 if (f && fclose(f) == EOF && err == NULL)
8949 err = got_error_from_errno2("fclose", path_unstaged_content);
8950 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8951 err = got_error_from_errno2("fclose", ondisk_path);
8952 free(path_unstaged_content);
8953 free(path_new_staged_content);
8954 free(blob_base_path);
8955 free(parent);
8956 free(base_path);
8957 return err;
8960 static const struct got_error *
8961 unstage_path(void *arg, unsigned char status,
8962 unsigned char staged_status, const char *relpath,
8963 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8964 struct got_object_id *commit_id, int dirfd, const char *de_name)
8966 const struct got_error *err = NULL;
8967 struct unstage_path_arg *a = arg;
8968 struct got_fileindex_entry *ie;
8969 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8970 char *ondisk_path = NULL;
8971 char *id_str = NULL, *label_orig = NULL;
8972 int local_changes_subsumed;
8973 struct stat sb;
8974 int fd1 = -1, fd2 = -1;
8976 if (staged_status != GOT_STATUS_ADD &&
8977 staged_status != GOT_STATUS_MODIFY &&
8978 staged_status != GOT_STATUS_DELETE)
8979 return NULL;
8981 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8982 if (ie == NULL)
8983 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8985 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8986 == -1)
8987 return got_error_from_errno("asprintf");
8989 err = got_object_id_str(&id_str,
8990 commit_id ? commit_id : a->worktree->base_commit_id);
8991 if (err)
8992 goto done;
8993 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8994 id_str) == -1) {
8995 err = got_error_from_errno("asprintf");
8996 goto done;
8999 fd1 = got_opentempfd();
9000 if (fd1 == -1) {
9001 err = got_error_from_errno("got_opentempfd");
9002 goto done;
9004 fd2 = got_opentempfd();
9005 if (fd2 == -1) {
9006 err = got_error_from_errno("got_opentempfd");
9007 goto done;
9010 switch (staged_status) {
9011 case GOT_STATUS_MODIFY:
9012 err = got_object_open_as_blob(&blob_base, a->repo,
9013 blob_id, 8192, fd1);
9014 if (err)
9015 break;
9016 /* fall through */
9017 case GOT_STATUS_ADD:
9018 if (a->patch_cb) {
9019 if (staged_status == GOT_STATUS_ADD) {
9020 int choice = GOT_PATCH_CHOICE_NONE;
9021 err = (*a->patch_cb)(&choice, a->patch_arg,
9022 staged_status, ie->path, NULL, 1, 1);
9023 if (err)
9024 break;
9025 if (choice != GOT_PATCH_CHOICE_YES)
9026 break;
9027 } else {
9028 err = unstage_hunks(staged_blob_id,
9029 blob_base, blob_id, ie, ondisk_path,
9030 label_orig, a->worktree, a->repo,
9031 a->patch_cb, a->patch_arg,
9032 a->progress_cb, a->progress_arg);
9033 break; /* Done with this file. */
9036 err = got_object_open_as_blob(&blob_staged, a->repo,
9037 staged_blob_id, 8192, fd2);
9038 if (err)
9039 break;
9040 switch (got_fileindex_entry_staged_filetype_get(ie)) {
9041 case GOT_FILEIDX_MODE_BAD_SYMLINK:
9042 case GOT_FILEIDX_MODE_REGULAR_FILE:
9043 err = merge_blob(&local_changes_subsumed, a->worktree,
9044 blob_base, ondisk_path, relpath,
9045 got_fileindex_perms_to_st(ie), label_orig,
9046 blob_staged, commit_id ? commit_id :
9047 a->worktree->base_commit_id, a->repo,
9048 a->progress_cb, a->progress_arg);
9049 break;
9050 case GOT_FILEIDX_MODE_SYMLINK:
9051 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9052 char *staged_target;
9053 err = got_object_blob_read_to_str(
9054 &staged_target, blob_staged);
9055 if (err)
9056 goto done;
9057 err = merge_symlink(a->worktree, blob_base,
9058 ondisk_path, relpath, label_orig,
9059 staged_target, commit_id ? commit_id :
9060 a->worktree->base_commit_id,
9061 a->repo, a->progress_cb, a->progress_arg);
9062 free(staged_target);
9063 } else {
9064 err = merge_blob(&local_changes_subsumed,
9065 a->worktree, blob_base, ondisk_path,
9066 relpath, got_fileindex_perms_to_st(ie),
9067 label_orig, blob_staged,
9068 commit_id ? commit_id :
9069 a->worktree->base_commit_id, a->repo,
9070 a->progress_cb, a->progress_arg);
9072 break;
9073 default:
9074 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9075 break;
9077 if (err == NULL) {
9078 got_fileindex_entry_stage_set(ie,
9079 GOT_FILEIDX_STAGE_NONE);
9080 got_fileindex_entry_staged_filetype_set(ie, 0);
9082 break;
9083 case GOT_STATUS_DELETE:
9084 if (a->patch_cb) {
9085 int choice = GOT_PATCH_CHOICE_NONE;
9086 err = (*a->patch_cb)(&choice, a->patch_arg,
9087 staged_status, ie->path, NULL, 1, 1);
9088 if (err)
9089 break;
9090 if (choice == GOT_PATCH_CHOICE_NO)
9091 break;
9092 if (choice != GOT_PATCH_CHOICE_YES) {
9093 err = got_error(GOT_ERR_PATCH_CHOICE);
9094 break;
9097 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9098 got_fileindex_entry_staged_filetype_set(ie, 0);
9099 err = get_file_status(&status, &sb, ie, ondisk_path,
9100 dirfd, de_name, a->repo);
9101 if (err)
9102 break;
9103 err = (*a->progress_cb)(a->progress_arg, status, relpath);
9104 break;
9106 done:
9107 free(ondisk_path);
9108 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9109 err = got_error_from_errno("close");
9110 if (blob_base)
9111 got_object_blob_close(blob_base);
9112 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9113 err = got_error_from_errno("close");
9114 if (blob_staged)
9115 got_object_blob_close(blob_staged);
9116 free(id_str);
9117 free(label_orig);
9118 return err;
9121 const struct got_error *
9122 got_worktree_unstage(struct got_worktree *worktree,
9123 struct got_pathlist_head *paths,
9124 got_worktree_checkout_cb progress_cb, void *progress_arg,
9125 got_worktree_patch_cb patch_cb, void *patch_arg,
9126 struct got_repository *repo)
9128 const struct got_error *err = NULL, *sync_err, *unlockerr;
9129 struct got_pathlist_entry *pe;
9130 struct got_fileindex *fileindex = NULL;
9131 char *fileindex_path = NULL;
9132 struct unstage_path_arg upa;
9134 err = lock_worktree(worktree, LOCK_EX);
9135 if (err)
9136 return err;
9138 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9139 if (err)
9140 goto done;
9142 upa.worktree = worktree;
9143 upa.fileindex = fileindex;
9144 upa.repo = repo;
9145 upa.progress_cb = progress_cb;
9146 upa.progress_arg = progress_arg;
9147 upa.patch_cb = patch_cb;
9148 upa.patch_arg = patch_arg;
9149 TAILQ_FOREACH(pe, paths, entry) {
9150 err = worktree_status(worktree, pe->path, fileindex, repo,
9151 unstage_path, &upa, NULL, NULL, 1, 0);
9152 if (err)
9153 goto done;
9156 sync_err = sync_fileindex(fileindex, fileindex_path);
9157 if (sync_err && err == NULL)
9158 err = sync_err;
9159 done:
9160 free(fileindex_path);
9161 if (fileindex)
9162 got_fileindex_free(fileindex);
9163 unlockerr = lock_worktree(worktree, LOCK_SH);
9164 if (unlockerr && err == NULL)
9165 err = unlockerr;
9166 return err;
9169 struct report_file_info_arg {
9170 struct got_worktree *worktree;
9171 got_worktree_path_info_cb info_cb;
9172 void *info_arg;
9173 struct got_pathlist_head *paths;
9174 got_cancel_cb cancel_cb;
9175 void *cancel_arg;
9178 static const struct got_error *
9179 report_file_info(void *arg, struct got_fileindex_entry *ie)
9181 struct report_file_info_arg *a = arg;
9182 struct got_pathlist_entry *pe;
9183 struct got_object_id blob_id, staged_blob_id, commit_id;
9184 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9185 struct got_object_id *commit_idp = NULL;
9186 int stage;
9188 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9189 return got_error(GOT_ERR_CANCELLED);
9191 TAILQ_FOREACH(pe, a->paths, entry) {
9192 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9193 got_path_is_child(ie->path, pe->path, pe->path_len))
9194 break;
9196 if (pe == NULL) /* not found */
9197 return NULL;
9199 if (got_fileindex_entry_has_blob(ie))
9200 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9201 stage = got_fileindex_entry_stage_get(ie);
9202 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9203 stage == GOT_FILEIDX_STAGE_ADD) {
9204 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9205 &staged_blob_id, ie);
9208 if (got_fileindex_entry_has_commit(ie))
9209 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9211 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9212 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9215 const struct got_error *
9216 got_worktree_path_info(struct got_worktree *worktree,
9217 struct got_pathlist_head *paths,
9218 got_worktree_path_info_cb info_cb, void *info_arg,
9219 got_cancel_cb cancel_cb, void *cancel_arg)
9222 const struct got_error *err = NULL, *unlockerr;
9223 struct got_fileindex *fileindex = NULL;
9224 char *fileindex_path = NULL;
9225 struct report_file_info_arg arg;
9227 err = lock_worktree(worktree, LOCK_SH);
9228 if (err)
9229 return err;
9231 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9232 if (err)
9233 goto done;
9235 arg.worktree = worktree;
9236 arg.info_cb = info_cb;
9237 arg.info_arg = info_arg;
9238 arg.paths = paths;
9239 arg.cancel_cb = cancel_cb;
9240 arg.cancel_arg = cancel_arg;
9241 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9242 &arg);
9243 done:
9244 free(fileindex_path);
9245 if (fileindex)
9246 got_fileindex_free(fileindex);
9247 unlockerr = lock_worktree(worktree, LOCK_UN);
9248 if (unlockerr && err == NULL)
9249 err = unlockerr;
9250 return err;
9253 static const struct got_error *
9254 patch_check_path(const char *p, char **path, unsigned char *status,
9255 unsigned char *staged_status, struct got_fileindex *fileindex,
9256 struct got_worktree *worktree, struct got_repository *repo)
9258 const struct got_error *err;
9259 struct got_fileindex_entry *ie;
9260 struct stat sb;
9261 char *ondisk_path = NULL;
9263 err = got_worktree_resolve_path(path, worktree, p);
9264 if (err)
9265 return err;
9267 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9268 *path[0] ? "/" : "", *path) == -1)
9269 return got_error_from_errno("asprintf");
9271 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9272 if (ie) {
9273 *staged_status = get_staged_status(ie);
9274 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9275 repo);
9276 if (err)
9277 goto done;
9278 } else {
9279 *staged_status = GOT_STATUS_NO_CHANGE;
9280 *status = GOT_STATUS_UNVERSIONED;
9281 if (lstat(ondisk_path, &sb) == -1) {
9282 if (errno != ENOENT) {
9283 err = got_error_from_errno2("lstat",
9284 ondisk_path);
9285 goto done;
9287 *status = GOT_STATUS_NONEXISTENT;
9291 done:
9292 free(ondisk_path);
9293 return err;
9296 static const struct got_error *
9297 patch_can_rm(const char *path, unsigned char status,
9298 unsigned char staged_status)
9300 if (status == GOT_STATUS_NONEXISTENT)
9301 return got_error_set_errno(ENOENT, path);
9302 if (status != GOT_STATUS_NO_CHANGE &&
9303 status != GOT_STATUS_ADD &&
9304 status != GOT_STATUS_MODIFY &&
9305 status != GOT_STATUS_MODE_CHANGE)
9306 return got_error_path(path, GOT_ERR_FILE_STATUS);
9307 if (staged_status == GOT_STATUS_DELETE)
9308 return got_error_path(path, GOT_ERR_FILE_STATUS);
9309 return NULL;
9312 static const struct got_error *
9313 patch_can_add(const char *path, unsigned char status)
9315 if (status != GOT_STATUS_NONEXISTENT)
9316 return got_error_path(path, GOT_ERR_FILE_STATUS);
9317 return NULL;
9320 static const struct got_error *
9321 patch_can_edit(const char *path, unsigned char status,
9322 unsigned char staged_status)
9324 if (status == GOT_STATUS_NONEXISTENT)
9325 return got_error_set_errno(ENOENT, path);
9326 if (status != GOT_STATUS_NO_CHANGE &&
9327 status != GOT_STATUS_ADD &&
9328 status != GOT_STATUS_MODIFY)
9329 return got_error_path(path, GOT_ERR_FILE_STATUS);
9330 if (staged_status == GOT_STATUS_DELETE)
9331 return got_error_path(path, GOT_ERR_FILE_STATUS);
9332 return NULL;
9335 const struct got_error *
9336 got_worktree_patch_prepare(struct got_fileindex **fileindex,
9337 char **fileindex_path, struct got_worktree *worktree)
9339 return open_fileindex(fileindex, fileindex_path, worktree);
9342 const struct got_error *
9343 got_worktree_patch_check_path(const char *old, const char *new,
9344 char **oldpath, char **newpath, struct got_worktree *worktree,
9345 struct got_repository *repo, struct got_fileindex *fileindex)
9347 const struct got_error *err = NULL;
9348 int file_renamed = 0;
9349 unsigned char status_old, staged_status_old;
9350 unsigned char status_new, staged_status_new;
9352 *oldpath = NULL;
9353 *newpath = NULL;
9355 err = patch_check_path(old != NULL ? old : new, oldpath,
9356 &status_old, &staged_status_old, fileindex, worktree, repo);
9357 if (err)
9358 goto done;
9360 err = patch_check_path(new != NULL ? new : old, newpath,
9361 &status_new, &staged_status_new, fileindex, worktree, repo);
9362 if (err)
9363 goto done;
9365 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9366 file_renamed = 1;
9368 if (old != NULL && new == NULL)
9369 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9370 else if (file_renamed) {
9371 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9372 if (err == NULL)
9373 err = patch_can_add(*newpath, status_new);
9374 } else if (old == NULL)
9375 err = patch_can_add(*newpath, status_new);
9376 else
9377 err = patch_can_edit(*newpath, status_new, staged_status_new);
9379 done:
9380 if (err) {
9381 free(*oldpath);
9382 *oldpath = NULL;
9383 free(*newpath);
9384 *newpath = NULL;
9386 return err;
9389 const struct got_error *
9390 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9391 struct got_worktree *worktree, struct got_fileindex *fileindex,
9392 got_worktree_checkout_cb progress_cb, void *progress_arg)
9394 struct schedule_addition_args saa;
9396 memset(&saa, 0, sizeof(saa));
9397 saa.worktree = worktree;
9398 saa.fileindex = fileindex;
9399 saa.progress_cb = progress_cb;
9400 saa.progress_arg = progress_arg;
9401 saa.repo = repo;
9403 return worktree_status(worktree, path, fileindex, repo,
9404 schedule_addition, &saa, NULL, NULL, 1, 0);
9407 const struct got_error *
9408 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9409 struct got_worktree *worktree, struct got_fileindex *fileindex,
9410 got_worktree_delete_cb progress_cb, void *progress_arg)
9412 struct schedule_deletion_args sda;
9414 memset(&sda, 0, sizeof(sda));
9415 sda.worktree = worktree;
9416 sda.fileindex = fileindex;
9417 sda.progress_cb = progress_cb;
9418 sda.progress_arg = progress_arg;
9419 sda.repo = repo;
9420 sda.delete_local_mods = 0;
9421 sda.keep_on_disk = 0;
9422 sda.ignore_missing_paths = 0;
9423 sda.status_codes = NULL;
9425 return worktree_status(worktree, path, fileindex, repo,
9426 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9429 const struct got_error *
9430 got_worktree_patch_complete(struct got_fileindex *fileindex,
9431 const char *fileindex_path)
9433 const struct got_error *err = NULL;
9435 err = sync_fileindex(fileindex, fileindex_path);
9436 got_fileindex_free(fileindex);
9438 return err;