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 "got_compat.h"
19 #include <sys/stat.h>
20 #include <sys/queue.h>
22 #include <dirent.h>
23 #include <limits.h>
24 #include <stddef.h>
25 #include <string.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <time.h>
29 #include <fcntl.h>
30 #include <errno.h>
31 #include <unistd.h>
32 #include <zlib.h>
33 #include <fnmatch.h>
34 #include <libgen.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_hash.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 || errno == ENOTDIR) {
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 if (err && err->code == GOT_ERR_FILE_OBSTRUCTED)
1415 err = got_error_path(path, err->code);
1416 free(parent);
1417 if (err)
1418 return err;
1419 fd = open(ondisk_path,
1420 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1421 mode);
1422 if (fd == -1)
1423 return got_error_from_errno2("open",
1424 ondisk_path);
1425 } else if (errno == EEXIST) {
1426 if (path_is_unversioned) {
1427 err = (*progress_cb)(progress_arg,
1428 GOT_STATUS_UNVERSIONED, path);
1429 goto done;
1431 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1432 !S_ISREG(st_mode) && !installing_bad_symlink) {
1433 /* TODO file is obstructed; do something */
1434 err = got_error_path(ondisk_path,
1435 GOT_ERR_FILE_OBSTRUCTED);
1436 goto done;
1437 } else {
1438 err = got_opentemp_named_fd(&tmppath, &fd,
1439 ondisk_path, "");
1440 if (err)
1441 goto done;
1442 update = 1;
1444 if (fchmod(fd, apply_umask(mode)) == -1) {
1445 err = got_error_from_errno2("fchmod",
1446 tmppath);
1447 goto done;
1450 } else
1451 return got_error_from_errno2("open", ondisk_path);
1454 if (progress_cb) {
1455 if (restoring_missing_file)
1456 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1457 path);
1458 else if (reverting_versioned_file)
1459 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1460 path);
1461 else
1462 err = (*progress_cb)(progress_arg,
1463 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1464 if (err)
1465 goto done;
1468 hdrlen = got_object_blob_get_hdrlen(blob);
1469 do {
1470 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1471 err = got_object_blob_read_block(&len, blob);
1472 if (err)
1473 break;
1474 if (len > 0) {
1475 /* Skip blob object header first time around. */
1476 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1477 if (outlen == -1) {
1478 err = got_error_from_errno("write");
1479 goto done;
1480 } else if (outlen != len - hdrlen) {
1481 err = got_error(GOT_ERR_IO);
1482 goto done;
1484 hdrlen = 0;
1486 } while (len != 0);
1488 if (fsync(fd) != 0) {
1489 err = got_error_from_errno("fsync");
1490 goto done;
1493 if (update) {
1494 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1495 err = got_error_from_errno2("unlink", ondisk_path);
1496 goto done;
1498 if (rename(tmppath, ondisk_path) != 0) {
1499 err = got_error_from_errno3("rename", tmppath,
1500 ondisk_path);
1501 goto done;
1503 free(tmppath);
1504 tmppath = NULL;
1507 done:
1508 if (fd != -1 && close(fd) == -1 && err == NULL)
1509 err = got_error_from_errno("close");
1510 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1511 err = got_error_from_errno2("unlink", tmppath);
1512 free(tmppath);
1513 return err;
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 r = diffreg_result->result;
1560 for (n = 0; n < r->chunks.len; n += nchunks_parsed) {
1561 struct diff_chunk *c;
1562 struct diff_chunk_context cc = {};
1563 off_t pos;
1566 * We can optimise a little by advancing straight
1567 * to the next chunk if this one has no added lines.
1569 c = diff_chunk_get(r, n);
1571 if (diff_chunk_type(c) != CHUNK_PLUS) {
1572 nchunks_parsed = 1;
1573 continue; /* removed or unchanged lines */
1576 pos = diff_chunk_get_right_start_pos(c);
1577 if (fseek(ondisk_file, pos, SEEK_SET) == -1) {
1578 err = got_ferror(ondisk_file, GOT_ERR_IO);
1579 goto done;
1582 diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
1583 ln = cc.right.start;
1585 while (ln < cc.right.end) {
1586 linelen = getline(&line, &linesize, ondisk_file);
1587 if (linelen == -1) {
1588 if (feof(ondisk_file))
1589 break;
1590 err = got_ferror(ondisk_file, GOT_ERR_IO);
1591 break;
1594 if (line && strncmp(line, markers[i],
1595 strlen(markers[i])) == 0) {
1596 if (strcmp(markers[i],
1597 GOT_DIFF_CONFLICT_MARKER_END) == 0) {
1598 *status = GOT_STATUS_CONFLICT;
1599 goto done;
1600 } else
1601 i++;
1603 ++ln;
1607 done:
1608 free(line);
1609 if (f1 != NULL && fclose(f1) == EOF && err == NULL)
1610 err = got_error_from_errno("fclose");
1611 free_err = got_diffreg_result_free(diffreg_result);
1612 if (err == NULL)
1613 err = free_err;
1615 return err;
1618 static int
1619 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1621 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1622 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1625 static int
1626 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1628 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1629 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1630 ie->mtime_sec == sb->st_mtim.tv_sec &&
1631 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1632 ie->size == (sb->st_size & 0xffffffff) &&
1633 !xbit_differs(ie, sb->st_mode));
1636 static unsigned char
1637 get_staged_status(struct got_fileindex_entry *ie)
1639 switch (got_fileindex_entry_stage_get(ie)) {
1640 case GOT_FILEIDX_STAGE_ADD:
1641 return GOT_STATUS_ADD;
1642 case GOT_FILEIDX_STAGE_DELETE:
1643 return GOT_STATUS_DELETE;
1644 case GOT_FILEIDX_STAGE_MODIFY:
1645 return GOT_STATUS_MODIFY;
1646 default:
1647 return GOT_STATUS_NO_CHANGE;
1651 static const struct got_error *
1652 get_symlink_modification_status(unsigned char *status,
1653 struct got_fileindex_entry *ie, const char *abspath,
1654 int dirfd, const char *de_name, struct got_blob_object *blob)
1656 const struct got_error *err = NULL;
1657 char target_path[PATH_MAX];
1658 char etarget[PATH_MAX];
1659 ssize_t elen;
1660 size_t len, target_len = 0;
1661 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1662 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1664 *status = GOT_STATUS_NO_CHANGE;
1666 /* Blob object content specifies the target path of the link. */
1667 do {
1668 err = got_object_blob_read_block(&len, blob);
1669 if (err)
1670 return err;
1671 if (len + target_len >= sizeof(target_path)) {
1673 * Should not happen. The blob contents were OK
1674 * when this symlink was installed.
1676 return got_error(GOT_ERR_NO_SPACE);
1678 if (len > 0) {
1679 /* Skip blob object header first time around. */
1680 memcpy(target_path + target_len, buf + hdrlen,
1681 len - hdrlen);
1682 target_len += len - hdrlen;
1683 hdrlen = 0;
1685 } while (len != 0);
1686 target_path[target_len] = '\0';
1688 if (dirfd != -1) {
1689 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1690 if (elen == -1)
1691 return got_error_from_errno2("readlinkat", abspath);
1692 } else {
1693 elen = readlink(abspath, etarget, sizeof(etarget));
1694 if (elen == -1)
1695 return got_error_from_errno2("readlink", abspath);
1698 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1699 *status = GOT_STATUS_MODIFY;
1701 return NULL;
1704 static const struct got_error *
1705 get_file_status(unsigned char *status, struct stat *sb,
1706 struct got_fileindex_entry *ie, const char *abspath,
1707 int dirfd, const char *de_name, struct got_repository *repo)
1709 const struct got_error *err = NULL;
1710 struct got_object_id id;
1711 size_t hdrlen;
1712 int fd = -1, fd1 = -1;
1713 FILE *f = NULL;
1714 uint8_t fbuf[8192];
1715 struct got_blob_object *blob = NULL;
1716 size_t flen, blen;
1717 unsigned char staged_status;
1719 staged_status = get_staged_status(ie);
1720 *status = GOT_STATUS_NO_CHANGE;
1721 memset(sb, 0, sizeof(*sb));
1724 * Whenever the caller provides a directory descriptor and a
1725 * directory entry name for the file, use them! This prevents
1726 * race conditions if filesystem paths change beneath our feet.
1728 if (dirfd != -1) {
1729 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1730 if (errno == ENOENT) {
1731 if (got_fileindex_entry_has_file_on_disk(ie))
1732 *status = GOT_STATUS_MISSING;
1733 else
1734 *status = GOT_STATUS_DELETE;
1735 goto done;
1737 err = got_error_from_errno2("fstatat", abspath);
1738 goto done;
1740 } else {
1741 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1742 if (fd == -1 && errno != ENOENT &&
1743 !got_err_open_nofollow_on_symlink())
1744 return got_error_from_errno2("open", abspath);
1745 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1746 if (lstat(abspath, sb) == -1)
1747 return got_error_from_errno2("lstat", abspath);
1748 } else if (fd == -1 || fstat(fd, sb) == -1) {
1749 if (errno == ENOENT) {
1750 if (got_fileindex_entry_has_file_on_disk(ie))
1751 *status = GOT_STATUS_MISSING;
1752 else
1753 *status = GOT_STATUS_DELETE;
1754 goto done;
1756 err = got_error_from_errno2("fstat", abspath);
1757 goto done;
1761 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1762 *status = GOT_STATUS_OBSTRUCTED;
1763 goto done;
1766 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1767 *status = GOT_STATUS_DELETE;
1768 goto done;
1769 } else if (!got_fileindex_entry_has_blob(ie) &&
1770 staged_status != GOT_STATUS_ADD) {
1771 *status = GOT_STATUS_ADD;
1772 goto done;
1775 if (!stat_info_differs(ie, sb))
1776 goto done;
1778 if (S_ISLNK(sb->st_mode) &&
1779 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1780 *status = GOT_STATUS_MODIFY;
1781 goto done;
1784 if (staged_status == GOT_STATUS_MODIFY ||
1785 staged_status == GOT_STATUS_ADD)
1786 got_fileindex_entry_get_staged_blob_id(&id, ie);
1787 else
1788 got_fileindex_entry_get_blob_id(&id, ie);
1790 fd1 = got_opentempfd();
1791 if (fd1 == -1) {
1792 err = got_error_from_errno("got_opentempfd");
1793 goto done;
1795 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1796 if (err)
1797 goto done;
1799 if (S_ISLNK(sb->st_mode)) {
1800 err = get_symlink_modification_status(status, ie,
1801 abspath, dirfd, de_name, blob);
1802 goto done;
1805 if (dirfd != -1) {
1806 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1807 if (fd == -1) {
1808 err = got_error_from_errno2("openat", abspath);
1809 goto done;
1813 f = fdopen(fd, "r");
1814 if (f == NULL) {
1815 err = got_error_from_errno2("fdopen", abspath);
1816 goto done;
1818 fd = -1;
1819 hdrlen = got_object_blob_get_hdrlen(blob);
1820 for (;;) {
1821 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1822 err = got_object_blob_read_block(&blen, blob);
1823 if (err)
1824 goto done;
1825 /* Skip length of blob object header first time around. */
1826 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1827 if (flen == 0 && ferror(f)) {
1828 err = got_error_from_errno("fread");
1829 goto done;
1831 if (blen - hdrlen == 0) {
1832 if (flen != 0)
1833 *status = GOT_STATUS_MODIFY;
1834 break;
1835 } else if (flen == 0) {
1836 if (blen - hdrlen != 0)
1837 *status = GOT_STATUS_MODIFY;
1838 break;
1839 } else if (blen - hdrlen == flen) {
1840 /* Skip blob object header first time around. */
1841 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1842 *status = GOT_STATUS_MODIFY;
1843 break;
1845 } else {
1846 *status = GOT_STATUS_MODIFY;
1847 break;
1849 hdrlen = 0;
1852 if (*status == GOT_STATUS_MODIFY) {
1853 rewind(f);
1854 err = get_modified_file_content_status(status, blob, ie->path,
1855 sb, f);
1856 } else if (xbit_differs(ie, sb->st_mode))
1857 *status = GOT_STATUS_MODE_CHANGE;
1858 done:
1859 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1860 err = got_error_from_errno("close");
1861 if (blob)
1862 got_object_blob_close(blob);
1863 if (f != NULL && fclose(f) == EOF && err == NULL)
1864 err = got_error_from_errno2("fclose", abspath);
1865 if (fd != -1 && close(fd) == -1 && err == NULL)
1866 err = got_error_from_errno2("close", abspath);
1867 return err;
1871 * Update timestamps in the file index if a file is unmodified and
1872 * we had to run a full content comparison to find out.
1874 static const struct got_error *
1875 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1876 struct got_fileindex_entry *ie, struct stat *sb)
1878 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1879 return got_fileindex_entry_update(ie, wt_fd, path,
1880 ie->blob_sha1, ie->commit_sha1, 1);
1882 return NULL;
1885 static const struct got_error *remove_ondisk_file(const char *, const char *);
1887 static const struct got_error *
1888 update_blob(struct got_worktree *worktree,
1889 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1890 struct got_tree_entry *te, const char *path,
1891 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1892 void *progress_arg)
1894 const struct got_error *err = NULL;
1895 struct got_blob_object *blob = NULL;
1896 char *ondisk_path = NULL;
1897 unsigned char status = GOT_STATUS_NO_CHANGE;
1898 struct stat sb;
1899 int fd1 = -1, fd2 = -1;
1901 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1902 return got_error_from_errno("asprintf");
1904 if (ie) {
1905 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1906 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1907 goto done;
1909 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1910 repo);
1911 if (err)
1912 goto done;
1913 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1914 sb.st_mode = got_fileindex_perms_to_st(ie);
1915 } else {
1916 if (stat(ondisk_path, &sb) == -1) {
1917 if (errno != ENOENT && errno != ENOTDIR) {
1918 err = got_error_from_errno2("stat",
1919 ondisk_path);
1920 goto done;
1922 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1923 status = GOT_STATUS_UNVERSIONED;
1924 } else {
1925 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1926 status = GOT_STATUS_UNVERSIONED;
1927 else
1928 status = GOT_STATUS_OBSTRUCTED;
1932 if (status == GOT_STATUS_OBSTRUCTED) {
1933 if (ie)
1934 got_fileindex_entry_mark_skipped(ie);
1935 err = (*progress_cb)(progress_arg, status, path);
1936 goto done;
1938 if (status == GOT_STATUS_CONFLICT) {
1939 if (ie)
1940 got_fileindex_entry_mark_skipped(ie);
1941 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1942 path);
1943 goto done;
1946 if (S_ISDIR(te->mode)) { /* file changing into a directory */
1947 if (status == GOT_STATUS_UNVERSIONED) {
1948 err = (*progress_cb)(progress_arg, status, path);
1949 } else if (status != GOT_STATUS_NO_CHANGE &&
1950 status != GOT_STATUS_DELETE &&
1951 status != GOT_STATUS_NONEXISTENT &&
1952 status != GOT_STATUS_MISSING) {
1953 err = (*progress_cb)(progress_arg,
1954 GOT_STATUS_CANNOT_DELETE, path);
1955 } else if (ie) {
1956 if (status != GOT_STATUS_DELETE &&
1957 status != GOT_STATUS_NONEXISTENT &&
1958 status != GOT_STATUS_MISSING) {
1959 err = remove_ondisk_file(worktree->root_path,
1960 ie->path);
1961 if (err && !(err->code == GOT_ERR_ERRNO &&
1962 errno == ENOENT))
1963 goto done;
1965 got_fileindex_entry_remove(fileindex, ie);
1966 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE,
1967 ie->path);
1969 goto done; /* nothing else to do */
1972 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1973 (S_ISLNK(te->mode) ||
1974 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1976 * This is a regular file or an installed bad symlink.
1977 * If the file index indicates that this file is already
1978 * up-to-date with respect to the repository we can skip
1979 * updating contents of this file.
1981 if (got_fileindex_entry_has_commit(ie) &&
1982 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1983 SHA1_DIGEST_LENGTH) == 0) {
1984 /* Same commit. */
1985 err = sync_timestamps(worktree->root_fd,
1986 path, status, ie, &sb);
1987 if (err)
1988 goto done;
1989 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1990 path);
1991 goto done;
1993 if (got_fileindex_entry_has_blob(ie) &&
1994 memcmp(ie->blob_sha1, te->id.sha1,
1995 SHA1_DIGEST_LENGTH) == 0) {
1996 /* Different commit but the same blob. */
1997 err = sync_timestamps(worktree->root_fd,
1998 path, status, ie, &sb);
1999 if (err)
2000 goto done;
2001 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2002 path);
2003 goto done;
2007 fd1 = got_opentempfd();
2008 if (fd1 == -1) {
2009 err = got_error_from_errno("got_opentempfd");
2010 goto done;
2012 err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
2013 if (err)
2014 goto done;
2016 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
2017 int update_timestamps;
2018 struct got_blob_object *blob2 = NULL;
2019 char *label_orig = NULL;
2020 if (got_fileindex_entry_has_blob(ie)) {
2021 fd2 = got_opentempfd();
2022 if (fd2 == -1) {
2023 err = got_error_from_errno("got_opentempfd");
2024 goto done;
2026 struct got_object_id id2;
2027 got_fileindex_entry_get_blob_id(&id2, ie);
2028 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
2029 fd2);
2030 if (err)
2031 goto done;
2033 if (got_fileindex_entry_has_commit(ie)) {
2034 char id_str[SHA1_DIGEST_STRING_LENGTH];
2035 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2036 sizeof(id_str)) == NULL) {
2037 err = got_error_path(id_str,
2038 GOT_ERR_BAD_OBJ_ID_STR);
2039 goto done;
2041 if (asprintf(&label_orig, "%s: commit %s",
2042 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2043 err = got_error_from_errno("asprintf");
2044 goto done;
2047 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2048 char *link_target;
2049 err = got_object_blob_read_to_str(&link_target, blob);
2050 if (err)
2051 goto done;
2052 err = merge_symlink(worktree, blob2, ondisk_path, path,
2053 label_orig, link_target, worktree->base_commit_id,
2054 repo, progress_cb, progress_arg);
2055 free(link_target);
2056 } else {
2057 err = merge_blob(&update_timestamps, worktree, blob2,
2058 ondisk_path, path, sb.st_mode, label_orig, blob,
2059 worktree->base_commit_id, repo,
2060 progress_cb, progress_arg);
2062 free(label_orig);
2063 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2064 err = got_error_from_errno("close");
2065 goto done;
2067 if (blob2)
2068 got_object_blob_close(blob2);
2069 if (err)
2070 goto done;
2072 * Do not update timestamps of files with local changes.
2073 * Otherwise, a future status walk would treat them as
2074 * unmodified files again.
2076 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2077 blob->id.sha1, worktree->base_commit_id->sha1,
2078 update_timestamps);
2079 } else if (status == GOT_STATUS_MODE_CHANGE) {
2080 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2081 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2082 } else if (status == GOT_STATUS_DELETE) {
2083 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2084 if (err)
2085 goto done;
2086 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2087 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2088 if (err)
2089 goto done;
2090 } else {
2091 int is_bad_symlink = 0;
2092 if (S_ISLNK(te->mode)) {
2093 err = install_symlink(&is_bad_symlink, worktree,
2094 ondisk_path, path, blob,
2095 status == GOT_STATUS_MISSING, 0,
2096 status == GOT_STATUS_UNVERSIONED, 0,
2097 repo, progress_cb, progress_arg);
2098 } else {
2099 err = install_blob(worktree, ondisk_path, path,
2100 te->mode, sb.st_mode, blob,
2101 status == GOT_STATUS_MISSING, 0, 0,
2102 status == GOT_STATUS_UNVERSIONED, repo,
2103 progress_cb, progress_arg);
2105 if (err)
2106 goto done;
2108 if (ie) {
2109 err = got_fileindex_entry_update(ie,
2110 worktree->root_fd, path, blob->id.sha1,
2111 worktree->base_commit_id->sha1, 1);
2112 } else {
2113 err = create_fileindex_entry(&ie, fileindex,
2114 worktree->base_commit_id, worktree->root_fd, path,
2115 &blob->id);
2117 if (err)
2118 goto done;
2120 if (is_bad_symlink) {
2121 got_fileindex_entry_filetype_set(ie,
2122 GOT_FILEIDX_MODE_BAD_SYMLINK);
2126 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2127 err = got_error_from_errno("close");
2128 goto done;
2130 got_object_blob_close(blob);
2131 done:
2132 free(ondisk_path);
2133 return err;
2136 static const struct got_error *
2137 remove_ondisk_file(const char *root_path, const char *path)
2139 const struct got_error *err = NULL;
2140 char *ondisk_path = NULL, *parent = NULL;
2142 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2143 return got_error_from_errno("asprintf");
2145 if (unlink(ondisk_path) == -1) {
2146 if (errno != ENOENT)
2147 err = got_error_from_errno2("unlink", ondisk_path);
2148 } else {
2149 size_t root_len = strlen(root_path);
2150 err = got_path_dirname(&parent, ondisk_path);
2151 if (err)
2152 goto done;
2153 while (got_path_cmp(parent, root_path,
2154 strlen(parent), root_len) != 0) {
2155 free(ondisk_path);
2156 ondisk_path = parent;
2157 parent = NULL;
2158 if (rmdir(ondisk_path) == -1) {
2159 if (errno != ENOTEMPTY)
2160 err = got_error_from_errno2("rmdir",
2161 ondisk_path);
2162 break;
2164 err = got_path_dirname(&parent, ondisk_path);
2165 if (err)
2166 break;
2169 done:
2170 free(ondisk_path);
2171 free(parent);
2172 return err;
2175 static const struct got_error *
2176 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2177 struct got_fileindex_entry *ie, struct got_repository *repo,
2178 got_worktree_checkout_cb progress_cb, void *progress_arg)
2180 const struct got_error *err = NULL;
2181 unsigned char status;
2182 struct stat sb;
2183 char *ondisk_path;
2185 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2186 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2188 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2189 == -1)
2190 return got_error_from_errno("asprintf");
2192 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2193 if (err)
2194 goto done;
2196 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2197 char ondisk_target[PATH_MAX];
2198 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2199 sizeof(ondisk_target));
2200 if (ondisk_len == -1) {
2201 err = got_error_from_errno2("readlink", ondisk_path);
2202 goto done;
2204 ondisk_target[ondisk_len] = '\0';
2205 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2206 NULL, NULL, /* XXX pass common ancestor info? */
2207 ondisk_target, ondisk_path);
2208 if (err)
2209 goto done;
2210 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2211 ie->path);
2212 goto done;
2215 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2216 status == GOT_STATUS_ADD) {
2217 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2218 if (err)
2219 goto done;
2221 * Preserve the working file and change the deleted blob's
2222 * entry into a schedule-add entry.
2224 err = got_fileindex_entry_update(ie, worktree->root_fd,
2225 ie->path, NULL, NULL, 0);
2226 } else {
2227 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2228 if (err)
2229 goto done;
2230 if (status == GOT_STATUS_NO_CHANGE) {
2231 err = remove_ondisk_file(worktree->root_path, ie->path);
2232 if (err)
2233 goto done;
2235 got_fileindex_entry_remove(fileindex, ie);
2237 done:
2238 free(ondisk_path);
2239 return err;
2242 struct diff_cb_arg {
2243 struct got_fileindex *fileindex;
2244 struct got_worktree *worktree;
2245 struct got_repository *repo;
2246 got_worktree_checkout_cb progress_cb;
2247 void *progress_arg;
2248 got_cancel_cb cancel_cb;
2249 void *cancel_arg;
2252 static const struct got_error *
2253 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2254 struct got_tree_entry *te, const char *parent_path)
2256 struct diff_cb_arg *a = arg;
2258 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2259 return got_error(GOT_ERR_CANCELLED);
2261 return update_blob(a->worktree, a->fileindex, ie, te,
2262 ie->path, a->repo, a->progress_cb, a->progress_arg);
2265 static const struct got_error *
2266 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2268 struct diff_cb_arg *a = arg;
2270 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2271 return got_error(GOT_ERR_CANCELLED);
2273 return delete_blob(a->worktree, a->fileindex, ie,
2274 a->repo, a->progress_cb, a->progress_arg);
2277 static const struct got_error *
2278 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2280 struct diff_cb_arg *a = arg;
2281 const struct got_error *err;
2282 char *path;
2284 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2285 return got_error(GOT_ERR_CANCELLED);
2287 if (got_object_tree_entry_is_submodule(te))
2288 return NULL;
2290 if (asprintf(&path, "%s%s%s", parent_path,
2291 parent_path[0] ? "/" : "", te->name)
2292 == -1)
2293 return got_error_from_errno("asprintf");
2295 if (S_ISDIR(te->mode))
2296 err = add_dir_on_disk(a->worktree, path);
2297 else
2298 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2299 a->repo, a->progress_cb, a->progress_arg);
2301 free(path);
2302 return err;
2305 const struct got_error *
2306 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2308 uint32_t uuid_status;
2310 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2311 if (uuid_status != uuid_s_ok) {
2312 *uuidstr = NULL;
2313 return got_error_uuid(uuid_status, "uuid_to_string");
2316 return NULL;
2319 static const struct got_error *
2320 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2322 const struct got_error *err = NULL;
2323 char *uuidstr = NULL;
2325 *refname = NULL;
2327 err = got_worktree_get_uuid(&uuidstr, worktree);
2328 if (err)
2329 return err;
2331 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2332 err = got_error_from_errno("asprintf");
2333 *refname = NULL;
2335 free(uuidstr);
2336 return err;
2339 const struct got_error *
2340 got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2341 const char *prefix)
2343 return get_ref_name(refname, worktree, prefix);
2346 const struct got_error *
2347 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2349 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2352 static const struct got_error *
2353 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2355 return get_ref_name(refname, worktree,
2356 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2359 static const struct got_error *
2360 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2362 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2365 static const struct got_error *
2366 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2368 return get_ref_name(refname, worktree,
2369 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2372 static const struct got_error *
2373 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2375 return get_ref_name(refname, worktree,
2376 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2379 static const struct got_error *
2380 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2382 return get_ref_name(refname, worktree,
2383 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2386 static const struct got_error *
2387 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2389 return get_ref_name(refname, worktree,
2390 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2393 static const struct got_error *
2394 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2396 return get_ref_name(refname, worktree,
2397 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2400 static const struct got_error *
2401 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2403 return get_ref_name(refname, worktree,
2404 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2407 const struct got_error *
2408 got_worktree_get_histedit_script_path(char **path,
2409 struct got_worktree *worktree)
2411 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2412 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2413 *path = NULL;
2414 return got_error_from_errno("asprintf");
2416 return NULL;
2419 static const struct got_error *
2420 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2422 return get_ref_name(refname, worktree,
2423 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2426 static const struct got_error *
2427 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2429 return get_ref_name(refname, worktree,
2430 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2434 * Prevent Git's garbage collector from deleting our base commit by
2435 * setting a reference to our base commit's ID.
2437 static const struct got_error *
2438 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2440 const struct got_error *err = NULL;
2441 struct got_reference *ref = NULL;
2442 char *refname;
2444 err = got_worktree_get_base_ref_name(&refname, worktree);
2445 if (err)
2446 return err;
2448 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2449 if (err)
2450 goto done;
2452 err = got_ref_write(ref, repo);
2453 done:
2454 free(refname);
2455 if (ref)
2456 got_ref_close(ref);
2457 return err;
2460 static const struct got_error *
2461 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2463 const struct got_error *err = NULL;
2465 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2466 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2467 err = got_error_from_errno("asprintf");
2468 *fileindex_path = NULL;
2470 return err;
2474 static const struct got_error *
2475 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2476 struct got_worktree *worktree)
2478 const struct got_error *err = NULL;
2479 FILE *index = NULL;
2481 *fileindex_path = NULL;
2482 *fileindex = got_fileindex_alloc();
2483 if (*fileindex == NULL)
2484 return got_error_from_errno("got_fileindex_alloc");
2486 err = get_fileindex_path(fileindex_path, worktree);
2487 if (err)
2488 goto done;
2490 index = fopen(*fileindex_path, "rbe");
2491 if (index == NULL) {
2492 if (errno != ENOENT)
2493 err = got_error_from_errno2("fopen", *fileindex_path);
2494 } else {
2495 err = got_fileindex_read(*fileindex, index);
2496 if (fclose(index) == EOF && err == NULL)
2497 err = got_error_from_errno("fclose");
2499 done:
2500 if (err) {
2501 free(*fileindex_path);
2502 *fileindex_path = NULL;
2503 got_fileindex_free(*fileindex);
2504 *fileindex = NULL;
2506 return err;
2509 struct bump_base_commit_id_arg {
2510 struct got_object_id *base_commit_id;
2511 const char *path;
2512 size_t path_len;
2513 const char *entry_name;
2514 got_worktree_checkout_cb progress_cb;
2515 void *progress_arg;
2518 static const struct got_error *
2519 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2521 const struct got_error *err;
2522 struct bump_base_commit_id_arg *a = arg;
2524 if (a->entry_name) {
2525 if (strcmp(ie->path, a->path) != 0)
2526 return NULL;
2527 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2528 return NULL;
2530 if (got_fileindex_entry_was_skipped(ie))
2531 return NULL;
2533 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2534 SHA1_DIGEST_LENGTH) == 0)
2535 return NULL;
2537 if (a->progress_cb) {
2538 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2539 ie->path);
2540 if (err)
2541 return err;
2543 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2544 return NULL;
2547 /* Bump base commit ID of all files within an updated part of the work tree. */
2548 static const struct got_error *
2549 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2550 struct got_fileindex *fileindex,
2551 got_worktree_checkout_cb progress_cb, void *progress_arg)
2553 struct bump_base_commit_id_arg bbc_arg;
2555 bbc_arg.base_commit_id = worktree->base_commit_id;
2556 bbc_arg.entry_name = NULL;
2557 bbc_arg.path = "";
2558 bbc_arg.path_len = 0;
2559 bbc_arg.progress_cb = progress_cb;
2560 bbc_arg.progress_arg = progress_arg;
2562 return got_fileindex_for_each_entry_safe(fileindex,
2563 bump_base_commit_id, &bbc_arg);
2566 static const struct got_error *
2567 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2569 const struct got_error *err = NULL;
2570 char *new_fileindex_path = NULL;
2571 FILE *new_index = NULL;
2572 struct timespec timeout;
2574 err = got_opentemp_named(&new_fileindex_path, &new_index,
2575 fileindex_path, "");
2576 if (err)
2577 goto done;
2579 err = got_fileindex_write(fileindex, new_index);
2580 if (err)
2581 goto done;
2583 if (rename(new_fileindex_path, fileindex_path) != 0) {
2584 err = got_error_from_errno3("rename", new_fileindex_path,
2585 fileindex_path);
2586 unlink(new_fileindex_path);
2590 * Sleep for a short amount of time to ensure that files modified after
2591 * this program exits have a different time stamp from the one which
2592 * was recorded in the file index.
2594 timeout.tv_sec = 0;
2595 timeout.tv_nsec = 1;
2596 nanosleep(&timeout, NULL);
2597 done:
2598 if (new_index)
2599 fclose(new_index);
2600 free(new_fileindex_path);
2601 return err;
2604 static const struct got_error *
2605 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2606 struct got_object_id **tree_id, const char *wt_relpath,
2607 struct got_commit_object *base_commit, struct got_worktree *worktree,
2608 struct got_repository *repo)
2610 const struct got_error *err = NULL;
2611 struct got_object_id *id = NULL;
2612 char *in_repo_path = NULL;
2613 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2615 *entry_type = GOT_OBJ_TYPE_ANY;
2616 *tree_relpath = NULL;
2617 *tree_id = NULL;
2619 if (wt_relpath[0] == '\0') {
2620 /* Check out all files within the work tree. */
2621 *entry_type = GOT_OBJ_TYPE_TREE;
2622 *tree_relpath = strdup("");
2623 if (*tree_relpath == NULL) {
2624 err = got_error_from_errno("strdup");
2625 goto done;
2627 err = got_object_id_by_path(tree_id, repo, base_commit,
2628 worktree->path_prefix);
2629 if (err)
2630 goto done;
2631 return NULL;
2634 /* Check out a subset of files in the work tree. */
2636 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2637 is_root_wt ? "" : "/", wt_relpath) == -1) {
2638 err = got_error_from_errno("asprintf");
2639 goto done;
2642 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2643 if (err)
2644 goto done;
2646 free(in_repo_path);
2647 in_repo_path = NULL;
2649 err = got_object_get_type(entry_type, repo, id);
2650 if (err)
2651 goto done;
2653 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2654 /* Check out a single file. */
2655 if (strchr(wt_relpath, '/') == NULL) {
2656 /* Check out a single file in work tree's root dir. */
2657 in_repo_path = strdup(worktree->path_prefix);
2658 if (in_repo_path == NULL) {
2659 err = got_error_from_errno("strdup");
2660 goto done;
2662 *tree_relpath = strdup("");
2663 if (*tree_relpath == NULL) {
2664 err = got_error_from_errno("strdup");
2665 goto done;
2667 } else {
2668 /* Check out a single file in a subdirectory. */
2669 err = got_path_dirname(tree_relpath, wt_relpath);
2670 if (err)
2671 return err;
2672 if (asprintf(&in_repo_path, "%s%s%s",
2673 worktree->path_prefix, is_root_wt ? "" : "/",
2674 *tree_relpath) == -1) {
2675 err = got_error_from_errno("asprintf");
2676 goto done;
2679 err = got_object_id_by_path(tree_id, repo,
2680 base_commit, in_repo_path);
2681 } else {
2682 /* Check out all files within a subdirectory. */
2683 *tree_id = got_object_id_dup(id);
2684 if (*tree_id == NULL) {
2685 err = got_error_from_errno("got_object_id_dup");
2686 goto done;
2688 *tree_relpath = strdup(wt_relpath);
2689 if (*tree_relpath == NULL) {
2690 err = got_error_from_errno("strdup");
2691 goto done;
2694 done:
2695 free(id);
2696 free(in_repo_path);
2697 if (err) {
2698 *entry_type = GOT_OBJ_TYPE_ANY;
2699 free(*tree_relpath);
2700 *tree_relpath = NULL;
2701 free(*tree_id);
2702 *tree_id = NULL;
2704 return err;
2707 static const struct got_error *
2708 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2709 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2710 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2711 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2713 const struct got_error *err = NULL;
2714 struct got_commit_object *commit = NULL;
2715 struct got_tree_object *tree = NULL;
2716 struct got_fileindex_diff_tree_cb diff_cb;
2717 struct diff_cb_arg arg;
2719 err = ref_base_commit(worktree, repo);
2720 if (err) {
2721 if (!(err->code == GOT_ERR_ERRNO &&
2722 (errno == EACCES || errno == EROFS)))
2723 goto done;
2724 err = (*progress_cb)(progress_arg,
2725 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2726 if (err)
2727 return err;
2730 err = got_object_open_as_commit(&commit, repo,
2731 worktree->base_commit_id);
2732 if (err)
2733 goto done;
2735 err = got_object_open_as_tree(&tree, repo, tree_id);
2736 if (err)
2737 goto done;
2739 if (entry_name &&
2740 got_object_tree_find_entry(tree, entry_name) == NULL) {
2741 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2742 goto done;
2745 diff_cb.diff_old_new = diff_old_new;
2746 diff_cb.diff_old = diff_old;
2747 diff_cb.diff_new = diff_new;
2748 arg.fileindex = fileindex;
2749 arg.worktree = worktree;
2750 arg.repo = repo;
2751 arg.progress_cb = progress_cb;
2752 arg.progress_arg = progress_arg;
2753 arg.cancel_cb = cancel_cb;
2754 arg.cancel_arg = cancel_arg;
2755 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2756 entry_name, repo, &diff_cb, &arg);
2757 done:
2758 if (tree)
2759 got_object_tree_close(tree);
2760 if (commit)
2761 got_object_commit_close(commit);
2762 return err;
2765 const struct got_error *
2766 got_worktree_checkout_files(struct got_worktree *worktree,
2767 struct got_pathlist_head *paths, struct got_repository *repo,
2768 got_worktree_checkout_cb progress_cb, void *progress_arg,
2769 got_cancel_cb cancel_cb, void *cancel_arg)
2771 const struct got_error *err = NULL, *sync_err, *unlockerr;
2772 struct got_commit_object *commit = NULL;
2773 struct got_tree_object *tree = NULL;
2774 struct got_fileindex *fileindex = NULL;
2775 char *fileindex_path = NULL;
2776 struct got_pathlist_entry *pe;
2777 struct tree_path_data {
2778 STAILQ_ENTRY(tree_path_data) entry;
2779 struct got_object_id *tree_id;
2780 int entry_type;
2781 char *relpath;
2782 char *entry_name;
2783 } *tpd = NULL;
2784 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2786 STAILQ_INIT(&tree_paths);
2788 err = lock_worktree(worktree, LOCK_EX);
2789 if (err)
2790 return err;
2792 err = got_object_open_as_commit(&commit, repo,
2793 worktree->base_commit_id);
2794 if (err)
2795 goto done;
2797 /* Map all specified paths to in-repository trees. */
2798 TAILQ_FOREACH(pe, paths, entry) {
2799 tpd = malloc(sizeof(*tpd));
2800 if (tpd == NULL) {
2801 err = got_error_from_errno("malloc");
2802 goto done;
2805 err = find_tree_entry_for_checkout(&tpd->entry_type,
2806 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2807 worktree, repo);
2808 if (err) {
2809 free(tpd);
2810 goto done;
2813 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2814 err = got_path_basename(&tpd->entry_name, pe->path);
2815 if (err) {
2816 free(tpd->relpath);
2817 free(tpd->tree_id);
2818 free(tpd);
2819 goto done;
2821 } else
2822 tpd->entry_name = NULL;
2824 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2828 * Read the file index.
2829 * Checking out files is supposed to be an idempotent operation.
2830 * If the on-disk file index is incomplete we will try to complete it.
2832 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2833 if (err)
2834 goto done;
2836 tpd = STAILQ_FIRST(&tree_paths);
2837 TAILQ_FOREACH(pe, paths, entry) {
2838 struct bump_base_commit_id_arg bbc_arg;
2840 err = checkout_files(worktree, fileindex, tpd->relpath,
2841 tpd->tree_id, tpd->entry_name, repo,
2842 progress_cb, progress_arg, cancel_cb, cancel_arg);
2843 if (err)
2844 break;
2846 bbc_arg.base_commit_id = worktree->base_commit_id;
2847 bbc_arg.entry_name = tpd->entry_name;
2848 bbc_arg.path = pe->path;
2849 bbc_arg.path_len = pe->path_len;
2850 bbc_arg.progress_cb = progress_cb;
2851 bbc_arg.progress_arg = progress_arg;
2852 err = got_fileindex_for_each_entry_safe(fileindex,
2853 bump_base_commit_id, &bbc_arg);
2854 if (err)
2855 break;
2857 tpd = STAILQ_NEXT(tpd, entry);
2859 sync_err = sync_fileindex(fileindex, fileindex_path);
2860 if (sync_err && err == NULL)
2861 err = sync_err;
2862 done:
2863 free(fileindex_path);
2864 if (tree)
2865 got_object_tree_close(tree);
2866 if (commit)
2867 got_object_commit_close(commit);
2868 if (fileindex)
2869 got_fileindex_free(fileindex);
2870 while (!STAILQ_EMPTY(&tree_paths)) {
2871 tpd = STAILQ_FIRST(&tree_paths);
2872 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2873 free(tpd->relpath);
2874 free(tpd->tree_id);
2875 free(tpd);
2877 unlockerr = lock_worktree(worktree, LOCK_SH);
2878 if (unlockerr && err == NULL)
2879 err = unlockerr;
2880 return err;
2883 static const struct got_error *
2884 add_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2885 struct got_fileindex_entry *ie, const char *ondisk_path,
2886 const char *path2, struct got_blob_object *blob2, mode_t mode2,
2887 int restoring_missing_file, int reverting_versioned_file,
2888 int path_is_unversioned, int allow_bad_symlinks,
2889 struct got_repository *repo,
2890 got_worktree_checkout_cb progress_cb, void *progress_arg)
2892 const struct got_error *err = NULL;
2893 int is_bad_symlink = 0;
2895 if (S_ISLNK(mode2)) {
2896 err = install_symlink(&is_bad_symlink,
2897 worktree, ondisk_path, path2, blob2,
2898 restoring_missing_file,
2899 reverting_versioned_file,
2900 path_is_unversioned, allow_bad_symlinks,
2901 repo, progress_cb, progress_arg);
2902 } else {
2903 err = install_blob(worktree, ondisk_path, path2,
2904 mode2, GOT_DEFAULT_FILE_MODE, blob2,
2905 restoring_missing_file, reverting_versioned_file, 0,
2906 path_is_unversioned, repo, progress_cb, progress_arg);
2908 if (err)
2909 return err;
2910 if (ie == NULL) {
2911 /* Adding an unversioned file. */
2912 err = got_fileindex_entry_alloc(&ie, path2);
2913 if (err)
2914 return err;
2915 err = got_fileindex_entry_update(ie,
2916 worktree->root_fd, path2, NULL, NULL, 1);
2917 if (err) {
2918 got_fileindex_entry_free(ie);
2919 return err;
2921 err = got_fileindex_entry_add(fileindex, ie);
2922 if (err) {
2923 got_fileindex_entry_free(ie);
2924 return err;
2926 } else {
2927 /* Re-adding a locally deleted file. */
2928 err = got_fileindex_entry_update(ie,
2929 worktree->root_fd, path2, ie->blob_sha1,
2930 worktree->base_commit_id->sha1, 0);
2931 if (err)
2932 return err;
2935 if (is_bad_symlink) {
2936 got_fileindex_entry_filetype_set(ie,
2937 GOT_FILEIDX_MODE_BAD_SYMLINK);
2940 return NULL;
2943 struct merge_file_cb_arg {
2944 struct got_worktree *worktree;
2945 struct got_fileindex *fileindex;
2946 got_worktree_checkout_cb progress_cb;
2947 void *progress_arg;
2948 got_cancel_cb cancel_cb;
2949 void *cancel_arg;
2950 const char *label_orig;
2951 struct got_object_id *commit_id2;
2952 int allow_bad_symlinks;
2955 static const struct got_error *
2956 merge_file_cb(void *arg, struct got_blob_object *blob1,
2957 struct got_blob_object *blob2, FILE *f1, FILE *f2,
2958 struct got_object_id *id1, struct got_object_id *id2,
2959 const char *path1, const char *path2,
2960 mode_t mode1, mode_t mode2, struct got_repository *repo)
2962 static const struct got_error *err = NULL;
2963 struct merge_file_cb_arg *a = arg;
2964 struct got_fileindex_entry *ie;
2965 char *ondisk_path = NULL;
2966 struct stat sb;
2967 unsigned char status;
2968 int local_changes_subsumed;
2969 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2970 char *id_str = NULL, *label_deriv2 = NULL;
2972 if (blob1 && blob2) {
2973 ie = got_fileindex_entry_get(a->fileindex, path2,
2974 strlen(path2));
2975 if (ie == NULL)
2976 return (*a->progress_cb)(a->progress_arg,
2977 GOT_STATUS_MISSING, path2);
2979 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2980 path2) == -1)
2981 return got_error_from_errno("asprintf");
2983 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2984 repo);
2985 if (err)
2986 goto done;
2988 if (status == GOT_STATUS_DELETE) {
2989 err = (*a->progress_cb)(a->progress_arg,
2990 GOT_STATUS_MERGE, path2);
2991 goto done;
2993 if (status != GOT_STATUS_NO_CHANGE &&
2994 status != GOT_STATUS_MODIFY &&
2995 status != GOT_STATUS_CONFLICT &&
2996 status != GOT_STATUS_ADD) {
2997 err = (*a->progress_cb)(a->progress_arg, status, path2);
2998 goto done;
3001 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
3002 char *link_target2;
3003 err = got_object_blob_read_to_str(&link_target2, blob2);
3004 if (err)
3005 goto done;
3006 err = merge_symlink(a->worktree, blob1, ondisk_path,
3007 path2, a->label_orig, link_target2, a->commit_id2,
3008 repo, a->progress_cb, a->progress_arg);
3009 free(link_target2);
3010 } else {
3011 int fd;
3013 f_orig = got_opentemp();
3014 if (f_orig == NULL) {
3015 err = got_error_from_errno("got_opentemp");
3016 goto done;
3018 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3019 f_orig, blob1);
3020 if (err)
3021 goto done;
3023 f_deriv2 = got_opentemp();
3024 if (f_deriv2 == NULL)
3025 goto done;
3026 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3027 f_deriv2, blob2);
3028 if (err)
3029 goto done;
3031 fd = open(ondisk_path,
3032 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3033 if (fd == -1) {
3034 err = got_error_from_errno2("open",
3035 ondisk_path);
3036 goto done;
3038 f_deriv = fdopen(fd, "r");
3039 if (f_deriv == NULL) {
3040 err = got_error_from_errno2("fdopen",
3041 ondisk_path);
3042 close(fd);
3043 goto done;
3045 err = got_object_id_str(&id_str, a->commit_id2);
3046 if (err)
3047 goto done;
3048 if (asprintf(&label_deriv2, "%s: commit %s",
3049 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
3050 err = got_error_from_errno("asprintf");
3051 goto done;
3053 err = merge_file(&local_changes_subsumed, a->worktree,
3054 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
3055 mode2, a->label_orig, NULL, label_deriv2,
3056 GOT_DIFF_ALGORITHM_PATIENCE, repo,
3057 a->progress_cb, a->progress_arg);
3059 } else if (blob1) {
3060 ie = got_fileindex_entry_get(a->fileindex, path1,
3061 strlen(path1));
3062 if (ie == NULL)
3063 return (*a->progress_cb)(a->progress_arg,
3064 GOT_STATUS_MISSING, path1);
3066 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3067 path1) == -1)
3068 return got_error_from_errno("asprintf");
3070 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3071 repo);
3072 if (err)
3073 goto done;
3075 switch (status) {
3076 case GOT_STATUS_NO_CHANGE:
3077 err = (*a->progress_cb)(a->progress_arg,
3078 GOT_STATUS_DELETE, path1);
3079 if (err)
3080 goto done;
3081 err = remove_ondisk_file(a->worktree->root_path, path1);
3082 if (err)
3083 goto done;
3084 if (ie)
3085 got_fileindex_entry_mark_deleted_from_disk(ie);
3086 break;
3087 case GOT_STATUS_DELETE:
3088 case GOT_STATUS_MISSING:
3089 err = (*a->progress_cb)(a->progress_arg,
3090 GOT_STATUS_DELETE, path1);
3091 if (err)
3092 goto done;
3093 if (ie)
3094 got_fileindex_entry_mark_deleted_from_disk(ie);
3095 break;
3096 case GOT_STATUS_ADD: {
3097 struct got_object_id *id;
3098 FILE *blob1_f;
3099 off_t blob1_size;
3101 * Delete the added file only if its content already
3102 * exists in the repository.
3104 err = got_object_blob_file_create(&id, &blob1_f,
3105 &blob1_size, path1);
3106 if (err)
3107 goto done;
3108 if (got_object_id_cmp(id, id1) == 0) {
3109 err = (*a->progress_cb)(a->progress_arg,
3110 GOT_STATUS_DELETE, path1);
3111 if (err)
3112 goto done;
3113 err = remove_ondisk_file(a->worktree->root_path,
3114 path1);
3115 if (err)
3116 goto done;
3117 if (ie)
3118 got_fileindex_entry_remove(a->fileindex,
3119 ie);
3120 } else {
3121 err = (*a->progress_cb)(a->progress_arg,
3122 GOT_STATUS_CANNOT_DELETE, path1);
3124 if (fclose(blob1_f) == EOF && err == NULL)
3125 err = got_error_from_errno("fclose");
3126 free(id);
3127 if (err)
3128 goto done;
3129 break;
3131 case GOT_STATUS_MODIFY:
3132 case GOT_STATUS_CONFLICT:
3133 err = (*a->progress_cb)(a->progress_arg,
3134 GOT_STATUS_CANNOT_DELETE, path1);
3135 if (err)
3136 goto done;
3137 break;
3138 case GOT_STATUS_OBSTRUCTED:
3139 err = (*a->progress_cb)(a->progress_arg, status, path1);
3140 if (err)
3141 goto done;
3142 break;
3143 default:
3144 break;
3146 } else if (blob2) {
3147 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3148 path2) == -1)
3149 return got_error_from_errno("asprintf");
3150 ie = got_fileindex_entry_get(a->fileindex, path2,
3151 strlen(path2));
3152 if (ie) {
3153 err = get_file_status(&status, &sb, ie, ondisk_path,
3154 -1, NULL, repo);
3155 if (err)
3156 goto done;
3157 if (status != GOT_STATUS_NO_CHANGE &&
3158 status != GOT_STATUS_MODIFY &&
3159 status != GOT_STATUS_CONFLICT &&
3160 status != GOT_STATUS_ADD &&
3161 status != GOT_STATUS_DELETE) {
3162 err = (*a->progress_cb)(a->progress_arg,
3163 status, path2);
3164 goto done;
3166 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3167 char *link_target2;
3168 err = got_object_blob_read_to_str(&link_target2,
3169 blob2);
3170 if (err)
3171 goto done;
3172 err = merge_symlink(a->worktree, NULL,
3173 ondisk_path, path2, a->label_orig,
3174 link_target2, a->commit_id2, repo,
3175 a->progress_cb, a->progress_arg);
3176 free(link_target2);
3177 } else if (S_ISREG(sb.st_mode)) {
3178 err = merge_blob(&local_changes_subsumed,
3179 a->worktree, NULL, ondisk_path, path2,
3180 sb.st_mode, a->label_orig, blob2,
3181 a->commit_id2, repo, a->progress_cb,
3182 a->progress_arg);
3183 } else if (status != GOT_STATUS_DELETE) {
3184 err = got_error_path(ondisk_path,
3185 GOT_ERR_FILE_OBSTRUCTED);
3187 if (err)
3188 goto done;
3189 if (status == GOT_STATUS_DELETE) {
3190 /* Re-add file with content from new blob. */
3191 err = add_file(a->worktree, a->fileindex, ie,
3192 ondisk_path, path2, blob2, mode2,
3193 0, 0, 0, a->allow_bad_symlinks,
3194 repo, a->progress_cb, a->progress_arg);
3195 if (err)
3196 goto done;
3198 } else {
3199 err = add_file(a->worktree, a->fileindex, NULL,
3200 ondisk_path, path2, blob2, mode2,
3201 0, 0, 1, a->allow_bad_symlinks,
3202 repo, a->progress_cb, a->progress_arg);
3203 if (err)
3204 goto done;
3207 done:
3208 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3209 err = got_error_from_errno("fclose");
3210 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3211 err = got_error_from_errno("fclose");
3212 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3213 err = got_error_from_errno("fclose");
3214 free(id_str);
3215 free(label_deriv2);
3216 free(ondisk_path);
3217 return err;
3220 static const struct got_error *
3221 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3223 struct got_worktree *worktree = arg;
3225 /* Reject merges into a work tree with mixed base commits. */
3226 if (got_fileindex_entry_has_commit(ie) &&
3227 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3228 SHA1_DIGEST_LENGTH) != 0)
3229 return got_error(GOT_ERR_MIXED_COMMITS);
3231 return NULL;
3234 struct check_merge_conflicts_arg {
3235 struct got_worktree *worktree;
3236 struct got_fileindex *fileindex;
3237 struct got_repository *repo;
3240 static const struct got_error *
3241 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3242 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3243 struct got_object_id *id1, struct got_object_id *id2,
3244 const char *path1, const char *path2,
3245 mode_t mode1, mode_t mode2, struct got_repository *repo)
3247 const struct got_error *err = NULL;
3248 struct check_merge_conflicts_arg *a = arg;
3249 unsigned char status;
3250 struct stat sb;
3251 struct got_fileindex_entry *ie;
3252 const char *path = path2 ? path2 : path1;
3253 struct got_object_id *id = id2 ? id2 : id1;
3254 char *ondisk_path;
3256 if (id == NULL)
3257 return NULL;
3259 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3260 if (ie == NULL)
3261 return NULL;
3263 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3264 == -1)
3265 return got_error_from_errno("asprintf");
3267 /* Reject merges into a work tree with conflicted files. */
3268 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3269 free(ondisk_path);
3270 if (err)
3271 return err;
3272 if (status == GOT_STATUS_CONFLICT)
3273 return got_error(GOT_ERR_CONFLICTS);
3275 return NULL;
3278 static const struct got_error *
3279 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3280 const char *fileindex_path, struct got_object_id *commit_id1,
3281 struct got_object_id *commit_id2, struct got_repository *repo,
3282 got_worktree_checkout_cb progress_cb, void *progress_arg,
3283 got_cancel_cb cancel_cb, void *cancel_arg)
3285 const struct got_error *err = NULL, *sync_err;
3286 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3287 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3288 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3289 struct check_merge_conflicts_arg cmc_arg;
3290 struct merge_file_cb_arg arg;
3291 char *label_orig = NULL;
3292 FILE *f1 = NULL, *f2 = NULL;
3293 int fd1 = -1, fd2 = -1;
3295 if (commit_id1) {
3296 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3297 if (err)
3298 goto done;
3299 err = got_object_id_by_path(&tree_id1, repo, commit1,
3300 worktree->path_prefix);
3301 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3302 goto done;
3304 if (tree_id1) {
3305 char *id_str;
3307 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3308 if (err)
3309 goto done;
3311 err = got_object_id_str(&id_str, commit_id1);
3312 if (err)
3313 goto done;
3315 if (asprintf(&label_orig, "%s: commit %s",
3316 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3317 err = got_error_from_errno("asprintf");
3318 free(id_str);
3319 goto done;
3321 free(id_str);
3323 f1 = got_opentemp();
3324 if (f1 == NULL) {
3325 err = got_error_from_errno("got_opentemp");
3326 goto done;
3330 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3331 if (err)
3332 goto done;
3334 err = got_object_id_by_path(&tree_id2, repo, commit2,
3335 worktree->path_prefix);
3336 if (err)
3337 goto done;
3339 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3340 if (err)
3341 goto done;
3343 f2 = got_opentemp();
3344 if (f2 == NULL) {
3345 err = got_error_from_errno("got_opentemp");
3346 goto done;
3349 fd1 = got_opentempfd();
3350 if (fd1 == -1) {
3351 err = got_error_from_errno("got_opentempfd");
3352 goto done;
3355 fd2 = got_opentempfd();
3356 if (fd2 == -1) {
3357 err = got_error_from_errno("got_opentempfd");
3358 goto done;
3361 cmc_arg.worktree = worktree;
3362 cmc_arg.fileindex = fileindex;
3363 cmc_arg.repo = repo;
3364 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3365 check_merge_conflicts, &cmc_arg, 0);
3366 if (err)
3367 goto done;
3369 arg.worktree = worktree;
3370 arg.fileindex = fileindex;
3371 arg.progress_cb = progress_cb;
3372 arg.progress_arg = progress_arg;
3373 arg.cancel_cb = cancel_cb;
3374 arg.cancel_arg = cancel_arg;
3375 arg.label_orig = label_orig;
3376 arg.commit_id2 = commit_id2;
3377 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3378 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3379 merge_file_cb, &arg, 1);
3380 sync_err = sync_fileindex(fileindex, fileindex_path);
3381 if (sync_err && err == NULL)
3382 err = sync_err;
3383 done:
3384 if (commit1)
3385 got_object_commit_close(commit1);
3386 if (commit2)
3387 got_object_commit_close(commit2);
3388 if (tree1)
3389 got_object_tree_close(tree1);
3390 if (tree2)
3391 got_object_tree_close(tree2);
3392 if (f1 && fclose(f1) == EOF && err == NULL)
3393 err = got_error_from_errno("fclose");
3394 if (f2 && fclose(f2) == EOF && err == NULL)
3395 err = got_error_from_errno("fclose");
3396 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3397 err = got_error_from_errno("close");
3398 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3399 err = got_error_from_errno("close");
3400 free(label_orig);
3401 return err;
3404 const struct got_error *
3405 got_worktree_merge_files(struct got_worktree *worktree,
3406 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3407 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3408 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3410 const struct got_error *err, *unlockerr;
3411 char *fileindex_path = NULL;
3412 struct got_fileindex *fileindex = NULL;
3414 err = lock_worktree(worktree, LOCK_EX);
3415 if (err)
3416 return err;
3418 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3419 if (err)
3420 goto done;
3422 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3423 worktree);
3424 if (err)
3425 goto done;
3427 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3428 commit_id2, repo, progress_cb, progress_arg,
3429 cancel_cb, cancel_arg);
3430 done:
3431 if (fileindex)
3432 got_fileindex_free(fileindex);
3433 free(fileindex_path);
3434 unlockerr = lock_worktree(worktree, LOCK_SH);
3435 if (unlockerr && err == NULL)
3436 err = unlockerr;
3437 return err;
3440 struct diff_dir_cb_arg {
3441 struct got_fileindex *fileindex;
3442 struct got_worktree *worktree;
3443 const char *status_path;
3444 size_t status_path_len;
3445 struct got_repository *repo;
3446 got_worktree_status_cb status_cb;
3447 void *status_arg;
3448 got_cancel_cb cancel_cb;
3449 void *cancel_arg;
3450 /* A pathlist containing per-directory pathlists of ignore patterns. */
3451 struct got_pathlist_head *ignores;
3452 int report_unchanged;
3453 int no_ignores;
3456 static const struct got_error *
3457 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3458 int dirfd, const char *de_name,
3459 got_worktree_status_cb status_cb, void *status_arg,
3460 struct got_repository *repo, int report_unchanged)
3462 const struct got_error *err = NULL;
3463 unsigned char status = GOT_STATUS_NO_CHANGE;
3464 unsigned char staged_status;
3465 struct stat sb;
3466 struct got_object_id blob_id, commit_id, staged_blob_id;
3467 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3468 struct got_object_id *staged_blob_idp = NULL;
3470 staged_status = get_staged_status(ie);
3471 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3472 if (err)
3473 return err;
3475 if (status == GOT_STATUS_NO_CHANGE &&
3476 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3477 return NULL;
3479 if (got_fileindex_entry_has_blob(ie))
3480 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3481 if (got_fileindex_entry_has_commit(ie))
3482 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3483 if (staged_status == GOT_STATUS_ADD ||
3484 staged_status == GOT_STATUS_MODIFY) {
3485 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3486 &staged_blob_id, ie);
3489 return (*status_cb)(status_arg, status, staged_status,
3490 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3493 static const struct got_error *
3494 status_old_new(void *arg, struct got_fileindex_entry *ie,
3495 struct dirent *de, const char *parent_path, int dirfd)
3497 const struct got_error *err = NULL;
3498 struct diff_dir_cb_arg *a = arg;
3499 char *abspath;
3501 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3502 return got_error(GOT_ERR_CANCELLED);
3504 if (got_path_cmp(parent_path, a->status_path,
3505 strlen(parent_path), a->status_path_len) != 0 &&
3506 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3507 return NULL;
3509 if (parent_path[0]) {
3510 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3511 parent_path, de->d_name) == -1)
3512 return got_error_from_errno("asprintf");
3513 } else {
3514 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3515 de->d_name) == -1)
3516 return got_error_from_errno("asprintf");
3519 err = report_file_status(ie, abspath, dirfd, de->d_name,
3520 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3521 free(abspath);
3522 return err;
3525 static const struct got_error *
3526 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3528 struct diff_dir_cb_arg *a = arg;
3529 struct got_object_id blob_id, commit_id;
3530 unsigned char status;
3532 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3533 return got_error(GOT_ERR_CANCELLED);
3535 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3536 return NULL;
3538 got_fileindex_entry_get_blob_id(&blob_id, ie);
3539 got_fileindex_entry_get_commit_id(&commit_id, ie);
3540 if (got_fileindex_entry_has_file_on_disk(ie))
3541 status = GOT_STATUS_MISSING;
3542 else
3543 status = GOT_STATUS_DELETE;
3544 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3545 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3548 static void
3549 free_ignores(struct got_pathlist_head *ignores)
3551 struct got_pathlist_entry *pe;
3553 TAILQ_FOREACH(pe, ignores, entry) {
3554 struct got_pathlist_head *ignorelist = pe->data;
3556 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3558 got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3561 static const struct got_error *
3562 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3564 const struct got_error *err = NULL;
3565 struct got_pathlist_entry *pe = NULL;
3566 struct got_pathlist_head *ignorelist;
3567 char *line = NULL, *pattern, *dirpath = NULL;
3568 size_t linesize = 0;
3569 ssize_t linelen;
3571 ignorelist = calloc(1, sizeof(*ignorelist));
3572 if (ignorelist == NULL)
3573 return got_error_from_errno("calloc");
3574 TAILQ_INIT(ignorelist);
3576 while ((linelen = getline(&line, &linesize, f)) != -1) {
3577 if (linelen > 0 && line[linelen - 1] == '\n')
3578 line[linelen - 1] = '\0';
3580 /* Git's ignores may contain comments. */
3581 if (line[0] == '#')
3582 continue;
3584 /* Git's negated patterns are not (yet?) supported. */
3585 if (line[0] == '!')
3586 continue;
3588 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3589 line) == -1) {
3590 err = got_error_from_errno("asprintf");
3591 goto done;
3593 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3594 if (err)
3595 goto done;
3597 if (ferror(f)) {
3598 err = got_error_from_errno("getline");
3599 goto done;
3602 dirpath = strdup(path);
3603 if (dirpath == NULL) {
3604 err = got_error_from_errno("strdup");
3605 goto done;
3607 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3608 done:
3609 free(line);
3610 if (err || pe == NULL) {
3611 free(dirpath);
3612 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3614 return err;
3617 static int
3618 match_path(const char *pattern, size_t pattern_len, const char *path,
3619 int flags)
3621 char buf[PATH_MAX];
3624 * Trailing slashes signify directories.
3625 * Append a * to make such patterns conform to fnmatch rules.
3627 if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3628 if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3629 return FNM_NOMATCH; /* XXX */
3631 return fnmatch(buf, path, flags);
3634 return fnmatch(pattern, path, flags);
3637 static int
3638 match_ignores(struct got_pathlist_head *ignores, const char *path)
3640 struct got_pathlist_entry *pe;
3642 /* Handle patterns which match in all directories. */
3643 TAILQ_FOREACH(pe, ignores, entry) {
3644 struct got_pathlist_head *ignorelist = pe->data;
3645 struct got_pathlist_entry *pi;
3647 TAILQ_FOREACH(pi, ignorelist, entry) {
3648 const char *p;
3650 if (pi->path_len < 3 ||
3651 strncmp(pi->path, "**/", 3) != 0)
3652 continue;
3653 p = path;
3654 while (*p) {
3655 if (match_path(pi->path + 3,
3656 pi->path_len - 3, p,
3657 FNM_PATHNAME | FNM_LEADING_DIR)) {
3658 /* Retry in next directory. */
3659 while (*p && *p != '/')
3660 p++;
3661 while (*p == '/')
3662 p++;
3663 continue;
3665 return 1;
3671 * The ignores pathlist contains ignore lists from children before
3672 * parents, so we can find the most specific ignorelist by walking
3673 * ignores backwards.
3675 pe = TAILQ_LAST(ignores, got_pathlist_head);
3676 while (pe) {
3677 if (got_path_is_child(path, pe->path, pe->path_len)) {
3678 struct got_pathlist_head *ignorelist = pe->data;
3679 struct got_pathlist_entry *pi;
3680 TAILQ_FOREACH(pi, ignorelist, entry) {
3681 int flags = FNM_LEADING_DIR;
3682 if (strstr(pi->path, "/**/") == NULL)
3683 flags |= FNM_PATHNAME;
3684 if (match_path(pi->path, pi->path_len,
3685 path, flags))
3686 continue;
3687 return 1;
3690 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3693 return 0;
3696 static const struct got_error *
3697 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3698 const char *path, int dirfd, const char *ignores_filename)
3700 const struct got_error *err = NULL;
3701 char *ignorespath;
3702 int fd = -1;
3703 FILE *ignoresfile = NULL;
3705 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3706 path[0] ? "/" : "", ignores_filename) == -1)
3707 return got_error_from_errno("asprintf");
3709 if (dirfd != -1) {
3710 fd = openat(dirfd, ignores_filename,
3711 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3712 if (fd == -1) {
3713 if (errno != ENOENT && errno != EACCES)
3714 err = got_error_from_errno2("openat",
3715 ignorespath);
3716 } else {
3717 ignoresfile = fdopen(fd, "r");
3718 if (ignoresfile == NULL)
3719 err = got_error_from_errno2("fdopen",
3720 ignorespath);
3721 else {
3722 fd = -1;
3723 err = read_ignores(ignores, path, ignoresfile);
3726 } else {
3727 ignoresfile = fopen(ignorespath, "re");
3728 if (ignoresfile == NULL) {
3729 if (errno != ENOENT && errno != EACCES)
3730 err = got_error_from_errno2("fopen",
3731 ignorespath);
3732 } else
3733 err = read_ignores(ignores, path, ignoresfile);
3736 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3737 err = got_error_from_errno2("fclose", path);
3738 if (fd != -1 && close(fd) == -1 && err == NULL)
3739 err = got_error_from_errno2("close", path);
3740 free(ignorespath);
3741 return err;
3744 static const struct got_error *
3745 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3746 int dirfd)
3748 const struct got_error *err = NULL;
3749 struct diff_dir_cb_arg *a = arg;
3750 char *path = NULL;
3752 if (ignore != NULL)
3753 *ignore = 0;
3755 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3756 return got_error(GOT_ERR_CANCELLED);
3758 if (parent_path[0]) {
3759 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3760 return got_error_from_errno("asprintf");
3761 } else {
3762 path = de->d_name;
3765 if (de->d_type == DT_DIR) {
3766 if (!a->no_ignores && ignore != NULL &&
3767 match_ignores(a->ignores, path))
3768 *ignore = 1;
3769 } else if (!match_ignores(a->ignores, path) &&
3770 got_path_is_child(path, a->status_path, a->status_path_len))
3771 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3772 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3773 if (parent_path[0])
3774 free(path);
3775 return err;
3778 static const struct got_error *
3779 status_traverse(void *arg, const char *path, int dirfd)
3781 const struct got_error *err = NULL;
3782 struct diff_dir_cb_arg *a = arg;
3784 if (a->no_ignores)
3785 return NULL;
3787 err = add_ignores(a->ignores, a->worktree->root_path,
3788 path, dirfd, ".cvsignore");
3789 if (err)
3790 return err;
3792 err = add_ignores(a->ignores, a->worktree->root_path, path,
3793 dirfd, ".gitignore");
3795 return err;
3798 static const struct got_error *
3799 report_single_file_status(const char *path, const char *ondisk_path,
3800 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3801 void *status_arg, struct got_repository *repo, int report_unchanged,
3802 struct got_pathlist_head *ignores, int no_ignores)
3804 struct got_fileindex_entry *ie;
3805 struct stat sb;
3807 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3808 if (ie)
3809 return report_file_status(ie, ondisk_path, -1, NULL,
3810 status_cb, status_arg, repo, report_unchanged);
3812 if (lstat(ondisk_path, &sb) == -1) {
3813 if (errno != ENOENT)
3814 return got_error_from_errno2("lstat", ondisk_path);
3815 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3816 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3819 if (!no_ignores && match_ignores(ignores, path))
3820 return NULL;
3822 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3823 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3824 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3826 return NULL;
3829 static const struct got_error *
3830 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3831 const char *root_path, const char *path)
3833 const struct got_error *err;
3834 char *parent_path, *next_parent_path = NULL;
3836 err = add_ignores(ignores, root_path, "", -1,
3837 ".cvsignore");
3838 if (err)
3839 return err;
3841 err = add_ignores(ignores, root_path, "", -1,
3842 ".gitignore");
3843 if (err)
3844 return err;
3846 err = got_path_dirname(&parent_path, path);
3847 if (err) {
3848 if (err->code == GOT_ERR_BAD_PATH)
3849 return NULL; /* cannot traverse parent */
3850 return err;
3852 for (;;) {
3853 err = add_ignores(ignores, root_path, parent_path, -1,
3854 ".cvsignore");
3855 if (err)
3856 break;
3857 err = add_ignores(ignores, root_path, parent_path, -1,
3858 ".gitignore");
3859 if (err)
3860 break;
3861 err = got_path_dirname(&next_parent_path, parent_path);
3862 if (err) {
3863 if (err->code == GOT_ERR_BAD_PATH)
3864 err = NULL; /* traversed everything */
3865 break;
3867 if (got_path_is_root_dir(parent_path))
3868 break;
3869 free(parent_path);
3870 parent_path = next_parent_path;
3871 next_parent_path = NULL;
3874 free(parent_path);
3875 free(next_parent_path);
3876 return err;
3879 static const struct got_error *
3880 worktree_status(struct got_worktree *worktree, const char *path,
3881 struct got_fileindex *fileindex, struct got_repository *repo,
3882 got_worktree_status_cb status_cb, void *status_arg,
3883 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3884 int report_unchanged)
3886 const struct got_error *err = NULL;
3887 int fd = -1;
3888 struct got_fileindex_diff_dir_cb fdiff_cb;
3889 struct diff_dir_cb_arg arg;
3890 char *ondisk_path = NULL;
3891 struct got_pathlist_head ignores;
3892 struct got_fileindex_entry *ie;
3894 TAILQ_INIT(&ignores);
3896 if (asprintf(&ondisk_path, "%s%s%s",
3897 worktree->root_path, path[0] ? "/" : "", path) == -1)
3898 return got_error_from_errno("asprintf");
3900 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3901 if (ie) {
3902 err = report_single_file_status(path, ondisk_path,
3903 fileindex, status_cb, status_arg, repo,
3904 report_unchanged, &ignores, no_ignores);
3905 goto done;
3908 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3909 if (fd == -1) {
3910 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3911 !got_err_open_nofollow_on_symlink())
3912 err = got_error_from_errno2("open", ondisk_path);
3913 else {
3914 if (!no_ignores) {
3915 err = add_ignores_from_parent_paths(&ignores,
3916 worktree->root_path, ondisk_path);
3917 if (err)
3918 goto done;
3920 err = report_single_file_status(path, ondisk_path,
3921 fileindex, status_cb, status_arg, repo,
3922 report_unchanged, &ignores, no_ignores);
3924 } else {
3925 fdiff_cb.diff_old_new = status_old_new;
3926 fdiff_cb.diff_old = status_old;
3927 fdiff_cb.diff_new = status_new;
3928 fdiff_cb.diff_traverse = status_traverse;
3929 arg.fileindex = fileindex;
3930 arg.worktree = worktree;
3931 arg.status_path = path;
3932 arg.status_path_len = strlen(path);
3933 arg.repo = repo;
3934 arg.status_cb = status_cb;
3935 arg.status_arg = status_arg;
3936 arg.cancel_cb = cancel_cb;
3937 arg.cancel_arg = cancel_arg;
3938 arg.report_unchanged = report_unchanged;
3939 arg.no_ignores = no_ignores;
3940 if (!no_ignores) {
3941 err = add_ignores_from_parent_paths(&ignores,
3942 worktree->root_path, path);
3943 if (err)
3944 goto done;
3946 arg.ignores = &ignores;
3947 err = got_fileindex_diff_dir(fileindex, fd,
3948 worktree->root_path, path, repo, &fdiff_cb, &arg);
3950 done:
3951 free_ignores(&ignores);
3952 if (fd != -1 && close(fd) == -1 && err == NULL)
3953 err = got_error_from_errno("close");
3954 free(ondisk_path);
3955 return err;
3958 const struct got_error *
3959 got_worktree_status(struct got_worktree *worktree,
3960 struct got_pathlist_head *paths, struct got_repository *repo,
3961 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3962 got_cancel_cb cancel_cb, void *cancel_arg)
3964 const struct got_error *err = NULL;
3965 char *fileindex_path = NULL;
3966 struct got_fileindex *fileindex = NULL;
3967 struct got_pathlist_entry *pe;
3969 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3970 if (err)
3971 return err;
3973 TAILQ_FOREACH(pe, paths, entry) {
3974 err = worktree_status(worktree, pe->path, fileindex, repo,
3975 status_cb, status_arg, cancel_cb, cancel_arg,
3976 no_ignores, 0);
3977 if (err)
3978 break;
3980 free(fileindex_path);
3981 got_fileindex_free(fileindex);
3982 return err;
3985 const struct got_error *
3986 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3987 const char *arg)
3989 const struct got_error *err = NULL;
3990 char *resolved = NULL, *cwd = NULL, *path = NULL;
3991 size_t len;
3992 struct stat sb;
3993 char *abspath = NULL;
3994 char canonpath[PATH_MAX];
3996 *wt_path = NULL;
3998 cwd = getcwd(NULL, 0);
3999 if (cwd == NULL)
4000 return got_error_from_errno("getcwd");
4002 if (lstat(arg, &sb) == -1) {
4003 if (errno != ENOENT) {
4004 err = got_error_from_errno2("lstat", arg);
4005 goto done;
4007 sb.st_mode = 0;
4009 if (S_ISLNK(sb.st_mode)) {
4011 * We cannot use realpath(3) with symlinks since we want to
4012 * operate on the symlink itself.
4013 * But we can make the path absolute, assuming it is relative
4014 * to the current working directory, and then canonicalize it.
4016 if (!got_path_is_absolute(arg)) {
4017 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4018 err = got_error_from_errno("asprintf");
4019 goto done;
4023 err = got_canonpath(abspath ? abspath : arg, canonpath,
4024 sizeof(canonpath));
4025 if (err)
4026 goto done;
4027 resolved = strdup(canonpath);
4028 if (resolved == NULL) {
4029 err = got_error_from_errno("strdup");
4030 goto done;
4032 } else {
4033 resolved = realpath(arg, NULL);
4034 if (resolved == NULL) {
4035 if (errno != ENOENT) {
4036 err = got_error_from_errno2("realpath", arg);
4037 goto done;
4039 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4040 err = got_error_from_errno("asprintf");
4041 goto done;
4043 err = got_canonpath(abspath, canonpath,
4044 sizeof(canonpath));
4045 if (err)
4046 goto done;
4047 resolved = strdup(canonpath);
4048 if (resolved == NULL) {
4049 err = got_error_from_errno("strdup");
4050 goto done;
4055 if (strncmp(got_worktree_get_root_path(worktree), resolved,
4056 strlen(got_worktree_get_root_path(worktree)))) {
4057 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
4058 goto done;
4061 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
4062 err = got_path_skip_common_ancestor(&path,
4063 got_worktree_get_root_path(worktree), resolved);
4064 if (err)
4065 goto done;
4066 } else {
4067 path = strdup("");
4068 if (path == NULL) {
4069 err = got_error_from_errno("strdup");
4070 goto done;
4074 /* XXX status walk can't deal with trailing slash! */
4075 len = strlen(path);
4076 while (len > 0 && path[len - 1] == '/') {
4077 path[len - 1] = '\0';
4078 len--;
4080 done:
4081 free(abspath);
4082 free(resolved);
4083 free(cwd);
4084 if (err == NULL)
4085 *wt_path = path;
4086 else
4087 free(path);
4088 return err;
4091 struct schedule_addition_args {
4092 struct got_worktree *worktree;
4093 struct got_fileindex *fileindex;
4094 got_worktree_checkout_cb progress_cb;
4095 void *progress_arg;
4096 struct got_repository *repo;
4099 static const struct got_error *
4100 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4101 const char *relpath, struct got_object_id *blob_id,
4102 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4103 int dirfd, const char *de_name)
4105 struct schedule_addition_args *a = arg;
4106 const struct got_error *err = NULL;
4107 struct got_fileindex_entry *ie;
4108 struct stat sb;
4109 char *ondisk_path;
4111 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4112 relpath) == -1)
4113 return got_error_from_errno("asprintf");
4115 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4116 if (ie) {
4117 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4118 de_name, a->repo);
4119 if (err)
4120 goto done;
4121 /* Re-adding an existing entry is a no-op. */
4122 if (status == GOT_STATUS_ADD)
4123 goto done;
4124 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4125 if (err)
4126 goto done;
4129 if (status != GOT_STATUS_UNVERSIONED) {
4130 if (status == GOT_STATUS_NONEXISTENT)
4131 err = got_error_set_errno(ENOENT, ondisk_path);
4132 else
4133 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4134 goto done;
4137 err = got_fileindex_entry_alloc(&ie, relpath);
4138 if (err)
4139 goto done;
4140 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4141 relpath, NULL, NULL, 1);
4142 if (err) {
4143 got_fileindex_entry_free(ie);
4144 goto done;
4146 err = got_fileindex_entry_add(a->fileindex, ie);
4147 if (err) {
4148 got_fileindex_entry_free(ie);
4149 goto done;
4151 done:
4152 free(ondisk_path);
4153 if (err)
4154 return err;
4155 if (status == GOT_STATUS_ADD)
4156 return NULL;
4157 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4160 const struct got_error *
4161 got_worktree_schedule_add(struct got_worktree *worktree,
4162 struct got_pathlist_head *paths,
4163 got_worktree_checkout_cb progress_cb, void *progress_arg,
4164 struct got_repository *repo, int no_ignores)
4166 struct got_fileindex *fileindex = NULL;
4167 char *fileindex_path = NULL;
4168 const struct got_error *err = NULL, *sync_err, *unlockerr;
4169 struct got_pathlist_entry *pe;
4170 struct schedule_addition_args saa;
4172 err = lock_worktree(worktree, LOCK_EX);
4173 if (err)
4174 return err;
4176 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4177 if (err)
4178 goto done;
4180 saa.worktree = worktree;
4181 saa.fileindex = fileindex;
4182 saa.progress_cb = progress_cb;
4183 saa.progress_arg = progress_arg;
4184 saa.repo = repo;
4186 TAILQ_FOREACH(pe, paths, entry) {
4187 err = worktree_status(worktree, pe->path, fileindex, repo,
4188 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4189 if (err)
4190 break;
4192 sync_err = sync_fileindex(fileindex, fileindex_path);
4193 if (sync_err && err == NULL)
4194 err = sync_err;
4195 done:
4196 free(fileindex_path);
4197 if (fileindex)
4198 got_fileindex_free(fileindex);
4199 unlockerr = lock_worktree(worktree, LOCK_SH);
4200 if (unlockerr && err == NULL)
4201 err = unlockerr;
4202 return err;
4205 struct schedule_deletion_args {
4206 struct got_worktree *worktree;
4207 struct got_fileindex *fileindex;
4208 got_worktree_delete_cb progress_cb;
4209 void *progress_arg;
4210 struct got_repository *repo;
4211 int delete_local_mods;
4212 int keep_on_disk;
4213 int ignore_missing_paths;
4214 const char *status_codes;
4217 static const struct got_error *
4218 schedule_for_deletion(void *arg, unsigned char status,
4219 unsigned char staged_status, const char *relpath,
4220 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4221 struct got_object_id *commit_id, int dirfd, const char *de_name)
4223 struct schedule_deletion_args *a = arg;
4224 const struct got_error *err = NULL;
4225 struct got_fileindex_entry *ie = NULL;
4226 struct stat sb;
4227 char *ondisk_path;
4229 if (status == GOT_STATUS_NONEXISTENT) {
4230 if (a->ignore_missing_paths)
4231 return NULL;
4232 return got_error_set_errno(ENOENT, relpath);
4235 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4236 if (ie == NULL)
4237 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4239 staged_status = get_staged_status(ie);
4240 if (staged_status != GOT_STATUS_NO_CHANGE) {
4241 if (staged_status == GOT_STATUS_DELETE)
4242 return NULL;
4243 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4246 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4247 relpath) == -1)
4248 return got_error_from_errno("asprintf");
4250 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4251 a->repo);
4252 if (err)
4253 goto done;
4255 if (a->status_codes) {
4256 size_t ncodes = strlen(a->status_codes);
4257 int i;
4258 for (i = 0; i < ncodes ; i++) {
4259 if (status == a->status_codes[i])
4260 break;
4262 if (i == ncodes) {
4263 /* Do not delete files in non-matching status. */
4264 free(ondisk_path);
4265 return NULL;
4267 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4268 a->status_codes[i] != GOT_STATUS_MISSING) {
4269 static char msg[64];
4270 snprintf(msg, sizeof(msg),
4271 "invalid status code '%c'", a->status_codes[i]);
4272 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4273 goto done;
4277 if (status != GOT_STATUS_NO_CHANGE) {
4278 if (status == GOT_STATUS_DELETE)
4279 goto done;
4280 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4281 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4282 goto done;
4284 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4285 err = got_error_set_errno(ENOENT, relpath);
4286 goto done;
4288 if (status != GOT_STATUS_MODIFY &&
4289 status != GOT_STATUS_MISSING) {
4290 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4291 goto done;
4295 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4296 size_t root_len;
4298 if (dirfd != -1) {
4299 if (unlinkat(dirfd, de_name, 0) == -1) {
4300 err = got_error_from_errno2("unlinkat",
4301 ondisk_path);
4302 goto done;
4304 } else if (unlink(ondisk_path) == -1) {
4305 err = got_error_from_errno2("unlink", ondisk_path);
4306 goto done;
4309 root_len = strlen(a->worktree->root_path);
4310 do {
4311 char *parent;
4312 err = got_path_dirname(&parent, ondisk_path);
4313 if (err)
4314 goto done;
4315 free(ondisk_path);
4316 ondisk_path = parent;
4317 if (rmdir(ondisk_path) == -1) {
4318 if (errno != ENOTEMPTY)
4319 err = got_error_from_errno2("rmdir",
4320 ondisk_path);
4321 break;
4323 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4324 strlen(ondisk_path), root_len) != 0);
4327 got_fileindex_entry_mark_deleted_from_disk(ie);
4328 done:
4329 free(ondisk_path);
4330 if (err)
4331 return err;
4332 if (status == GOT_STATUS_DELETE)
4333 return NULL;
4334 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4335 staged_status, relpath);
4338 const struct got_error *
4339 got_worktree_schedule_delete(struct got_worktree *worktree,
4340 struct got_pathlist_head *paths, int delete_local_mods,
4341 const char *status_codes,
4342 got_worktree_delete_cb progress_cb, void *progress_arg,
4343 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4345 struct got_fileindex *fileindex = NULL;
4346 char *fileindex_path = NULL;
4347 const struct got_error *err = NULL, *sync_err, *unlockerr;
4348 struct got_pathlist_entry *pe;
4349 struct schedule_deletion_args sda;
4351 err = lock_worktree(worktree, LOCK_EX);
4352 if (err)
4353 return err;
4355 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4356 if (err)
4357 goto done;
4359 sda.worktree = worktree;
4360 sda.fileindex = fileindex;
4361 sda.progress_cb = progress_cb;
4362 sda.progress_arg = progress_arg;
4363 sda.repo = repo;
4364 sda.delete_local_mods = delete_local_mods;
4365 sda.keep_on_disk = keep_on_disk;
4366 sda.ignore_missing_paths = ignore_missing_paths;
4367 sda.status_codes = status_codes;
4369 TAILQ_FOREACH(pe, paths, entry) {
4370 err = worktree_status(worktree, pe->path, fileindex, repo,
4371 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4372 if (err)
4373 break;
4375 sync_err = sync_fileindex(fileindex, fileindex_path);
4376 if (sync_err && err == NULL)
4377 err = sync_err;
4378 done:
4379 free(fileindex_path);
4380 if (fileindex)
4381 got_fileindex_free(fileindex);
4382 unlockerr = lock_worktree(worktree, LOCK_SH);
4383 if (unlockerr && err == NULL)
4384 err = unlockerr;
4385 return err;
4388 static const struct got_error *
4389 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4391 const struct got_error *err = NULL;
4392 char *line = NULL;
4393 size_t linesize = 0, n;
4394 ssize_t linelen;
4396 linelen = getline(&line, &linesize, infile);
4397 if (linelen == -1) {
4398 if (ferror(infile)) {
4399 err = got_error_from_errno("getline");
4400 goto done;
4402 return NULL;
4404 if (outfile) {
4405 n = fwrite(line, 1, linelen, outfile);
4406 if (n != linelen) {
4407 err = got_ferror(outfile, GOT_ERR_IO);
4408 goto done;
4411 if (rejectfile) {
4412 n = fwrite(line, 1, linelen, rejectfile);
4413 if (n != linelen)
4414 err = got_ferror(rejectfile, GOT_ERR_IO);
4416 done:
4417 free(line);
4418 return err;
4421 static const struct got_error *
4422 skip_one_line(FILE *f)
4424 char *line = NULL;
4425 size_t linesize = 0;
4426 ssize_t linelen;
4428 linelen = getline(&line, &linesize, f);
4429 if (linelen == -1) {
4430 if (ferror(f))
4431 return got_error_from_errno("getline");
4432 return NULL;
4434 free(line);
4435 return NULL;
4438 static const struct got_error *
4439 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4440 int start_old, int end_old, int start_new, int end_new,
4441 FILE *outfile, FILE *rejectfile)
4443 const struct got_error *err;
4445 /* Copy old file's lines leading up to patch. */
4446 while (!feof(f1) && *line_cur1 < start_old) {
4447 err = copy_one_line(f1, outfile, NULL);
4448 if (err)
4449 return err;
4450 (*line_cur1)++;
4452 /* Skip new file's lines leading up to patch. */
4453 while (!feof(f2) && *line_cur2 < start_new) {
4454 if (rejectfile)
4455 err = copy_one_line(f2, NULL, rejectfile);
4456 else
4457 err = skip_one_line(f2);
4458 if (err)
4459 return err;
4460 (*line_cur2)++;
4462 /* Copy patched lines. */
4463 while (!feof(f2) && *line_cur2 <= end_new) {
4464 err = copy_one_line(f2, outfile, NULL);
4465 if (err)
4466 return err;
4467 (*line_cur2)++;
4469 /* Skip over old file's replaced lines. */
4470 while (!feof(f1) && *line_cur1 <= end_old) {
4471 if (rejectfile)
4472 err = copy_one_line(f1, NULL, rejectfile);
4473 else
4474 err = skip_one_line(f1);
4475 if (err)
4476 return err;
4477 (*line_cur1)++;
4480 return NULL;
4483 static const struct got_error *
4484 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4485 FILE *outfile, FILE *rejectfile)
4487 const struct got_error *err;
4489 if (outfile) {
4490 /* Copy old file's lines until EOF. */
4491 while (!feof(f1)) {
4492 err = copy_one_line(f1, outfile, NULL);
4493 if (err)
4494 return err;
4495 (*line_cur1)++;
4498 if (rejectfile) {
4499 /* Copy new file's lines until EOF. */
4500 while (!feof(f2)) {
4501 err = copy_one_line(f2, NULL, rejectfile);
4502 if (err)
4503 return err;
4504 (*line_cur2)++;
4508 return NULL;
4511 static const struct got_error *
4512 apply_or_reject_change(int *choice, int *nchunks_used,
4513 struct diff_result *diff_result, int n,
4514 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4515 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4516 got_worktree_patch_cb patch_cb, void *patch_arg)
4518 const struct got_error *err = NULL;
4519 struct diff_chunk_context cc = {};
4520 int start_old, end_old, start_new, end_new;
4521 FILE *hunkfile;
4522 struct diff_output_unidiff_state *diff_state;
4523 struct diff_input_info diff_info;
4524 int rc;
4526 *choice = GOT_PATCH_CHOICE_NONE;
4528 /* Get changed line numbers without context lines for copy_change(). */
4529 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4530 start_old = cc.left.start;
4531 end_old = cc.left.end;
4532 start_new = cc.right.start;
4533 end_new = cc.right.end;
4535 /* Get the same change with context lines for display. */
4536 memset(&cc, 0, sizeof(cc));
4537 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4539 memset(&diff_info, 0, sizeof(diff_info));
4540 diff_info.left_path = relpath;
4541 diff_info.right_path = relpath;
4543 diff_state = diff_output_unidiff_state_alloc();
4544 if (diff_state == NULL)
4545 return got_error_set_errno(ENOMEM,
4546 "diff_output_unidiff_state_alloc");
4548 hunkfile = got_opentemp();
4549 if (hunkfile == NULL) {
4550 err = got_error_from_errno("got_opentemp");
4551 goto done;
4554 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4555 diff_result, &cc);
4556 if (rc != DIFF_RC_OK) {
4557 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4558 goto done;
4561 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4562 err = got_ferror(hunkfile, GOT_ERR_IO);
4563 goto done;
4566 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4567 hunkfile, changeno, nchanges);
4568 if (err)
4569 goto done;
4571 switch (*choice) {
4572 case GOT_PATCH_CHOICE_YES:
4573 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4574 end_old, start_new, end_new, outfile, rejectfile);
4575 break;
4576 case GOT_PATCH_CHOICE_NO:
4577 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4578 end_old, start_new, end_new, rejectfile, outfile);
4579 break;
4580 case GOT_PATCH_CHOICE_QUIT:
4581 break;
4582 default:
4583 err = got_error(GOT_ERR_PATCH_CHOICE);
4584 break;
4586 done:
4587 diff_output_unidiff_state_free(diff_state);
4588 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4589 err = got_error_from_errno("fclose");
4590 return err;
4593 struct revert_file_args {
4594 struct got_worktree *worktree;
4595 struct got_fileindex *fileindex;
4596 got_worktree_checkout_cb progress_cb;
4597 void *progress_arg;
4598 got_worktree_patch_cb patch_cb;
4599 void *patch_arg;
4600 struct got_repository *repo;
4601 int unlink_added_files;
4604 static const struct got_error *
4605 create_patched_content(char **path_outfile, int reverse_patch,
4606 struct got_object_id *blob_id, const char *path2,
4607 int dirfd2, const char *de_name2,
4608 const char *relpath, struct got_repository *repo,
4609 got_worktree_patch_cb patch_cb, void *patch_arg)
4611 const struct got_error *err, *free_err;
4612 struct got_blob_object *blob = NULL;
4613 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4614 int fd = -1, fd2 = -1;
4615 char link_target[PATH_MAX];
4616 ssize_t link_len = 0;
4617 char *path1 = NULL, *id_str = NULL;
4618 struct stat sb2;
4619 struct got_diffreg_result *diffreg_result = NULL;
4620 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4621 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4623 *path_outfile = NULL;
4625 err = got_object_id_str(&id_str, blob_id);
4626 if (err)
4627 return err;
4629 if (dirfd2 != -1) {
4630 fd2 = openat(dirfd2, de_name2,
4631 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4632 if (fd2 == -1) {
4633 if (!got_err_open_nofollow_on_symlink()) {
4634 err = got_error_from_errno2("openat", path2);
4635 goto done;
4637 link_len = readlinkat(dirfd2, de_name2,
4638 link_target, sizeof(link_target));
4639 if (link_len == -1) {
4640 return got_error_from_errno2("readlinkat",
4641 path2);
4643 sb2.st_mode = S_IFLNK;
4644 sb2.st_size = link_len;
4646 } else {
4647 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4648 if (fd2 == -1) {
4649 if (!got_err_open_nofollow_on_symlink()) {
4650 err = got_error_from_errno2("open", path2);
4651 goto done;
4653 link_len = readlink(path2, link_target,
4654 sizeof(link_target));
4655 if (link_len == -1)
4656 return got_error_from_errno2("readlink", path2);
4657 sb2.st_mode = S_IFLNK;
4658 sb2.st_size = link_len;
4661 if (fd2 != -1) {
4662 if (fstat(fd2, &sb2) == -1) {
4663 err = got_error_from_errno2("fstat", path2);
4664 goto done;
4667 f2 = fdopen(fd2, "r");
4668 if (f2 == NULL) {
4669 err = got_error_from_errno2("fdopen", path2);
4670 goto done;
4672 fd2 = -1;
4673 } else {
4674 size_t n;
4675 f2 = got_opentemp();
4676 if (f2 == NULL) {
4677 err = got_error_from_errno2("got_opentemp", path2);
4678 goto done;
4680 n = fwrite(link_target, 1, link_len, f2);
4681 if (n != link_len) {
4682 err = got_ferror(f2, GOT_ERR_IO);
4683 goto done;
4685 if (fflush(f2) == EOF) {
4686 err = got_error_from_errno("fflush");
4687 goto done;
4689 rewind(f2);
4692 fd = got_opentempfd();
4693 if (fd == -1) {
4694 err = got_error_from_errno("got_opentempfd");
4695 goto done;
4698 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4699 if (err)
4700 goto done;
4702 err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4703 if (err)
4704 goto done;
4706 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4707 if (err)
4708 goto done;
4710 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4711 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4712 if (err)
4713 goto done;
4715 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4716 "");
4717 if (err)
4718 goto done;
4720 if (fseek(f1, 0L, SEEK_SET) == -1)
4721 return got_ferror(f1, GOT_ERR_IO);
4722 if (fseek(f2, 0L, SEEK_SET) == -1)
4723 return got_ferror(f2, GOT_ERR_IO);
4725 /* Count the number of actual changes in the diff result. */
4726 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4727 struct diff_chunk_context cc = {};
4728 diff_chunk_context_load_change(&cc, &nchunks_used,
4729 diffreg_result->result, n, 0);
4730 nchanges++;
4732 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4733 int choice;
4734 err = apply_or_reject_change(&choice, &nchunks_used,
4735 diffreg_result->result, n, relpath, f1, f2,
4736 &line_cur1, &line_cur2,
4737 reverse_patch ? NULL : outfile,
4738 reverse_patch ? outfile : NULL,
4739 ++i, nchanges, patch_cb, patch_arg);
4740 if (err)
4741 goto done;
4742 if (choice == GOT_PATCH_CHOICE_YES)
4743 have_content = 1;
4744 else if (choice == GOT_PATCH_CHOICE_QUIT)
4745 break;
4747 if (have_content) {
4748 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4749 reverse_patch ? NULL : outfile,
4750 reverse_patch ? outfile : NULL);
4751 if (err)
4752 goto done;
4754 if (!S_ISLNK(sb2.st_mode)) {
4755 mode_t mode;
4757 mode = apply_umask(sb2.st_mode);
4758 if (fchmod(fileno(outfile), mode) == -1) {
4759 err = got_error_from_errno2("fchmod", path2);
4760 goto done;
4764 done:
4765 free(id_str);
4766 if (fd != -1 && close(fd) == -1 && err == NULL)
4767 err = got_error_from_errno("close");
4768 if (blob)
4769 got_object_blob_close(blob);
4770 free_err = got_diffreg_result_free(diffreg_result);
4771 if (err == NULL)
4772 err = free_err;
4773 if (f1 && fclose(f1) == EOF && err == NULL)
4774 err = got_error_from_errno2("fclose", path1);
4775 if (f2 && fclose(f2) == EOF && err == NULL)
4776 err = got_error_from_errno2("fclose", path2);
4777 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4778 err = got_error_from_errno2("close", path2);
4779 if (outfile && fclose(outfile) == EOF && err == NULL)
4780 err = got_error_from_errno2("fclose", *path_outfile);
4781 if (path1 && unlink(path1) == -1 && err == NULL)
4782 err = got_error_from_errno2("unlink", path1);
4783 if (err || !have_content) {
4784 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4785 err = got_error_from_errno2("unlink", *path_outfile);
4786 free(*path_outfile);
4787 *path_outfile = NULL;
4789 free(path1);
4790 return err;
4793 static const struct got_error *
4794 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4795 const char *relpath, struct got_object_id *blob_id,
4796 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4797 int dirfd, const char *de_name)
4799 struct revert_file_args *a = arg;
4800 const struct got_error *err = NULL;
4801 char *parent_path = NULL;
4802 struct got_fileindex_entry *ie;
4803 struct got_commit_object *base_commit = NULL;
4804 struct got_tree_object *tree = NULL;
4805 struct got_object_id *tree_id = NULL;
4806 const struct got_tree_entry *te = NULL;
4807 char *tree_path = NULL, *te_name;
4808 char *ondisk_path = NULL, *path_content = NULL;
4809 struct got_blob_object *blob = NULL;
4810 int fd = -1;
4812 /* Reverting a staged deletion is a no-op. */
4813 if (status == GOT_STATUS_DELETE &&
4814 staged_status != GOT_STATUS_NO_CHANGE)
4815 return NULL;
4817 if (status == GOT_STATUS_UNVERSIONED)
4818 return (*a->progress_cb)(a->progress_arg,
4819 GOT_STATUS_UNVERSIONED, relpath);
4821 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4822 if (ie == NULL)
4823 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4825 /* Construct in-repository path of tree which contains this blob. */
4826 err = got_path_dirname(&parent_path, ie->path);
4827 if (err) {
4828 if (err->code != GOT_ERR_BAD_PATH)
4829 goto done;
4830 parent_path = strdup("/");
4831 if (parent_path == NULL) {
4832 err = got_error_from_errno("strdup");
4833 goto done;
4836 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4837 tree_path = strdup(parent_path);
4838 if (tree_path == NULL) {
4839 err = got_error_from_errno("strdup");
4840 goto done;
4842 } else {
4843 if (got_path_is_root_dir(parent_path)) {
4844 tree_path = strdup(a->worktree->path_prefix);
4845 if (tree_path == NULL) {
4846 err = got_error_from_errno("strdup");
4847 goto done;
4849 } else {
4850 if (asprintf(&tree_path, "%s/%s",
4851 a->worktree->path_prefix, parent_path) == -1) {
4852 err = got_error_from_errno("asprintf");
4853 goto done;
4858 err = got_object_open_as_commit(&base_commit, a->repo,
4859 a->worktree->base_commit_id);
4860 if (err)
4861 goto done;
4863 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4864 if (err) {
4865 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4866 (status == GOT_STATUS_ADD ||
4867 staged_status == GOT_STATUS_ADD)))
4868 goto done;
4869 } else {
4870 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4871 if (err)
4872 goto done;
4874 err = got_path_basename(&te_name, ie->path);
4875 if (err)
4876 goto done;
4878 te = got_object_tree_find_entry(tree, te_name);
4879 free(te_name);
4880 if (te == NULL && status != GOT_STATUS_ADD &&
4881 staged_status != GOT_STATUS_ADD) {
4882 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4883 goto done;
4887 switch (status) {
4888 case GOT_STATUS_ADD:
4889 if (a->patch_cb) {
4890 int choice = GOT_PATCH_CHOICE_NONE;
4891 err = (*a->patch_cb)(&choice, a->patch_arg,
4892 status, ie->path, NULL, 1, 1);
4893 if (err)
4894 goto done;
4895 if (choice != GOT_PATCH_CHOICE_YES)
4896 break;
4898 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4899 ie->path);
4900 if (err)
4901 goto done;
4902 got_fileindex_entry_remove(a->fileindex, ie);
4903 if (a->unlink_added_files) {
4904 if (asprintf(&ondisk_path, "%s/%s",
4905 got_worktree_get_root_path(a->worktree),
4906 relpath) == -1) {
4907 err = got_error_from_errno("asprintf");
4908 goto done;
4910 if (unlink(ondisk_path) == -1) {
4911 err = got_error_from_errno2("unlink",
4912 ondisk_path);
4913 break;
4916 break;
4917 case GOT_STATUS_DELETE:
4918 if (a->patch_cb) {
4919 int choice = GOT_PATCH_CHOICE_NONE;
4920 err = (*a->patch_cb)(&choice, a->patch_arg,
4921 status, ie->path, NULL, 1, 1);
4922 if (err)
4923 goto done;
4924 if (choice != GOT_PATCH_CHOICE_YES)
4925 break;
4927 /* fall through */
4928 case GOT_STATUS_MODIFY:
4929 case GOT_STATUS_MODE_CHANGE:
4930 case GOT_STATUS_CONFLICT:
4931 case GOT_STATUS_MISSING: {
4932 struct got_object_id id;
4933 if (staged_status == GOT_STATUS_ADD ||
4934 staged_status == GOT_STATUS_MODIFY)
4935 got_fileindex_entry_get_staged_blob_id(&id, ie);
4936 else
4937 got_fileindex_entry_get_blob_id(&id, ie);
4938 fd = got_opentempfd();
4939 if (fd == -1) {
4940 err = got_error_from_errno("got_opentempfd");
4941 goto done;
4944 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4945 if (err)
4946 goto done;
4948 if (asprintf(&ondisk_path, "%s/%s",
4949 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4950 err = got_error_from_errno("asprintf");
4951 goto done;
4954 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4955 status == GOT_STATUS_CONFLICT)) {
4956 int is_bad_symlink = 0;
4957 err = create_patched_content(&path_content, 1, &id,
4958 ondisk_path, dirfd, de_name, ie->path, a->repo,
4959 a->patch_cb, a->patch_arg);
4960 if (err || path_content == NULL)
4961 break;
4962 if (te && S_ISLNK(te->mode)) {
4963 if (unlink(path_content) == -1) {
4964 err = got_error_from_errno2("unlink",
4965 path_content);
4966 break;
4968 err = install_symlink(&is_bad_symlink,
4969 a->worktree, ondisk_path, ie->path,
4970 blob, 0, 1, 0, 0, a->repo,
4971 a->progress_cb, a->progress_arg);
4972 } else {
4973 if (rename(path_content, ondisk_path) == -1) {
4974 err = got_error_from_errno3("rename",
4975 path_content, ondisk_path);
4976 goto done;
4979 } else {
4980 int is_bad_symlink = 0;
4981 if (te && S_ISLNK(te->mode)) {
4982 err = install_symlink(&is_bad_symlink,
4983 a->worktree, ondisk_path, ie->path,
4984 blob, 0, 1, 0, 0, a->repo,
4985 a->progress_cb, a->progress_arg);
4986 } else {
4987 err = install_blob(a->worktree, ondisk_path,
4988 ie->path,
4989 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4990 got_fileindex_perms_to_st(ie), blob,
4991 0, 1, 0, 0, a->repo,
4992 a->progress_cb, a->progress_arg);
4994 if (err)
4995 goto done;
4996 if (status == GOT_STATUS_DELETE ||
4997 status == GOT_STATUS_MODE_CHANGE) {
4998 err = got_fileindex_entry_update(ie,
4999 a->worktree->root_fd, relpath,
5000 blob->id.sha1,
5001 a->worktree->base_commit_id->sha1, 1);
5002 if (err)
5003 goto done;
5005 if (is_bad_symlink) {
5006 got_fileindex_entry_filetype_set(ie,
5007 GOT_FILEIDX_MODE_BAD_SYMLINK);
5010 break;
5012 default:
5013 break;
5015 done:
5016 free(ondisk_path);
5017 free(path_content);
5018 free(parent_path);
5019 free(tree_path);
5020 if (fd != -1 && close(fd) == -1 && err == NULL)
5021 err = got_error_from_errno("close");
5022 if (blob)
5023 got_object_blob_close(blob);
5024 if (tree)
5025 got_object_tree_close(tree);
5026 free(tree_id);
5027 if (base_commit)
5028 got_object_commit_close(base_commit);
5029 return err;
5032 const struct got_error *
5033 got_worktree_revert(struct got_worktree *worktree,
5034 struct got_pathlist_head *paths,
5035 got_worktree_checkout_cb progress_cb, void *progress_arg,
5036 got_worktree_patch_cb patch_cb, void *patch_arg,
5037 struct got_repository *repo)
5039 struct got_fileindex *fileindex = NULL;
5040 char *fileindex_path = NULL;
5041 const struct got_error *err = NULL, *unlockerr = NULL;
5042 const struct got_error *sync_err = NULL;
5043 struct got_pathlist_entry *pe;
5044 struct revert_file_args rfa;
5046 err = lock_worktree(worktree, LOCK_EX);
5047 if (err)
5048 return err;
5050 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5051 if (err)
5052 goto done;
5054 rfa.worktree = worktree;
5055 rfa.fileindex = fileindex;
5056 rfa.progress_cb = progress_cb;
5057 rfa.progress_arg = progress_arg;
5058 rfa.patch_cb = patch_cb;
5059 rfa.patch_arg = patch_arg;
5060 rfa.repo = repo;
5061 rfa.unlink_added_files = 0;
5062 TAILQ_FOREACH(pe, paths, entry) {
5063 err = worktree_status(worktree, pe->path, fileindex, repo,
5064 revert_file, &rfa, NULL, NULL, 1, 0);
5065 if (err)
5066 break;
5068 sync_err = sync_fileindex(fileindex, fileindex_path);
5069 if (sync_err && err == NULL)
5070 err = sync_err;
5071 done:
5072 free(fileindex_path);
5073 if (fileindex)
5074 got_fileindex_free(fileindex);
5075 unlockerr = lock_worktree(worktree, LOCK_SH);
5076 if (unlockerr && err == NULL)
5077 err = unlockerr;
5078 return err;
5081 static void
5082 free_commitable(struct got_commitable *ct)
5084 free(ct->path);
5085 free(ct->in_repo_path);
5086 free(ct->ondisk_path);
5087 free(ct->blob_id);
5088 free(ct->base_blob_id);
5089 free(ct->staged_blob_id);
5090 free(ct->base_commit_id);
5091 free(ct);
5094 struct collect_commitables_arg {
5095 struct got_pathlist_head *commitable_paths;
5096 struct got_repository *repo;
5097 struct got_worktree *worktree;
5098 struct got_fileindex *fileindex;
5099 int have_staged_files;
5100 int allow_bad_symlinks;
5101 int diff_header_shown;
5102 int commit_conflicts;
5103 FILE *diff_outfile;
5104 FILE *f1;
5105 FILE *f2;
5109 * Create a file which contains the target path of a symlink so we can feed
5110 * it as content to the diff engine.
5112 static const struct got_error *
5113 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5114 const char *abspath)
5116 const struct got_error *err = NULL;
5117 char target_path[PATH_MAX];
5118 ssize_t target_len, outlen;
5120 *fd = -1;
5122 if (dirfd != -1) {
5123 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5124 if (target_len == -1)
5125 return got_error_from_errno2("readlinkat", abspath);
5126 } else {
5127 target_len = readlink(abspath, target_path, PATH_MAX);
5128 if (target_len == -1)
5129 return got_error_from_errno2("readlink", abspath);
5132 *fd = got_opentempfd();
5133 if (*fd == -1)
5134 return got_error_from_errno("got_opentempfd");
5136 outlen = write(*fd, target_path, target_len);
5137 if (outlen == -1) {
5138 err = got_error_from_errno("got_opentempfd");
5139 goto done;
5142 if (lseek(*fd, 0, SEEK_SET) == -1) {
5143 err = got_error_from_errno2("lseek", abspath);
5144 goto done;
5146 done:
5147 if (err) {
5148 close(*fd);
5149 *fd = -1;
5151 return err;
5154 static const struct got_error *
5155 append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5156 FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5157 int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5159 const struct got_error *err = NULL;
5160 struct got_blob_object *blob1 = NULL;
5161 int fd = -1, fd1 = -1, fd2 = -1;
5162 FILE *ondisk_file = NULL;
5163 char *label1 = NULL;
5164 struct stat sb;
5165 off_t size1 = 0;
5166 int f2_exists = 0;
5167 char *id_str = NULL;
5169 memset(&sb, 0, sizeof(sb));
5171 if (diff_staged) {
5172 if (ct->staged_status != GOT_STATUS_MODIFY &&
5173 ct->staged_status != GOT_STATUS_ADD &&
5174 ct->staged_status != GOT_STATUS_DELETE)
5175 return NULL;
5176 } else {
5177 if (ct->status != GOT_STATUS_MODIFY &&
5178 ct->status != GOT_STATUS_ADD &&
5179 ct->status != GOT_STATUS_DELETE &&
5180 ct->status != GOT_STATUS_CONFLICT)
5181 return NULL;
5184 err = got_opentemp_truncate(f1);
5185 if (err)
5186 return got_error_from_errno("got_opentemp_truncate");
5187 err = got_opentemp_truncate(f2);
5188 if (err)
5189 return got_error_from_errno("got_opentemp_truncate");
5191 if (!*diff_header_shown) {
5192 err = got_object_id_str(&id_str, worktree->base_commit_id);
5193 if (err)
5194 return err;
5195 fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5196 got_worktree_get_root_path(worktree));
5197 fprintf(diff_outfile, "commit - %s\n", id_str);
5198 fprintf(diff_outfile, "path + %s%s\n",
5199 got_worktree_get_root_path(worktree),
5200 diff_staged ? " (staged changes)" : "");
5201 *diff_header_shown = 1;
5204 if (diff_staged) {
5205 const char *label1 = NULL, *label2 = NULL;
5206 switch (ct->staged_status) {
5207 case GOT_STATUS_MODIFY:
5208 label1 = ct->path;
5209 label2 = ct->path;
5210 break;
5211 case GOT_STATUS_ADD:
5212 label2 = ct->path;
5213 break;
5214 case GOT_STATUS_DELETE:
5215 label1 = ct->path;
5216 break;
5217 default:
5218 return got_error(GOT_ERR_FILE_STATUS);
5220 fd1 = got_opentempfd();
5221 if (fd1 == -1) {
5222 err = got_error_from_errno("got_opentempfd");
5223 goto done;
5225 fd2 = got_opentempfd();
5226 if (fd2 == -1) {
5227 err = got_error_from_errno("got_opentempfd");
5228 goto done;
5230 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5231 fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5232 label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5233 NULL, repo, diff_outfile);
5234 goto done;
5237 fd1 = got_opentempfd();
5238 if (fd1 == -1) {
5239 err = got_error_from_errno("got_opentempfd");
5240 goto done;
5243 if (ct->status != GOT_STATUS_ADD) {
5244 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5245 8192, fd1);
5246 if (err)
5247 goto done;
5250 if (ct->status != GOT_STATUS_DELETE) {
5251 if (dirfd != -1) {
5252 fd = openat(dirfd, de_name,
5253 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5254 if (fd == -1) {
5255 if (!got_err_open_nofollow_on_symlink()) {
5256 err = got_error_from_errno2("openat",
5257 ct->ondisk_path);
5258 goto done;
5260 err = get_symlink_target_file(&fd, dirfd,
5261 de_name, ct->ondisk_path);
5262 if (err)
5263 goto done;
5265 } else {
5266 fd = open(ct->ondisk_path,
5267 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5268 if (fd == -1) {
5269 if (!got_err_open_nofollow_on_symlink()) {
5270 err = got_error_from_errno2("open",
5271 ct->ondisk_path);
5272 goto done;
5274 err = get_symlink_target_file(&fd, dirfd,
5275 de_name, ct->ondisk_path);
5276 if (err)
5277 goto done;
5280 if (fstatat(fd, ct->ondisk_path, &sb,
5281 AT_SYMLINK_NOFOLLOW) == -1) {
5282 err = got_error_from_errno2("fstatat", ct->ondisk_path);
5283 goto done;
5285 ondisk_file = fdopen(fd, "r");
5286 if (ondisk_file == NULL) {
5287 err = got_error_from_errno2("fdopen", ct->ondisk_path);
5288 goto done;
5290 fd = -1;
5291 f2_exists = 1;
5294 if (blob1) {
5295 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5296 f1, blob1);
5297 if (err)
5298 goto done;
5301 err = got_diff_blob_file(blob1, f1, size1, label1,
5302 ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5303 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5304 done:
5305 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5306 err = got_error_from_errno("close");
5307 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5308 err = got_error_from_errno("close");
5309 if (blob1)
5310 got_object_blob_close(blob1);
5311 if (fd != -1 && close(fd) == -1 && err == NULL)
5312 err = got_error_from_errno("close");
5313 if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5314 err = got_error_from_errno("fclose");
5315 return err;
5318 static const struct got_error *
5319 collect_commitables(void *arg, unsigned char status,
5320 unsigned char staged_status, const char *relpath,
5321 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5322 struct got_object_id *commit_id, int dirfd, const char *de_name)
5324 struct collect_commitables_arg *a = arg;
5325 const struct got_error *err = NULL;
5326 struct got_commitable *ct = NULL;
5327 struct got_pathlist_entry *new = NULL;
5328 char *parent_path = NULL, *path = NULL;
5329 struct stat sb;
5331 if (a->have_staged_files) {
5332 if (staged_status != GOT_STATUS_MODIFY &&
5333 staged_status != GOT_STATUS_ADD &&
5334 staged_status != GOT_STATUS_DELETE)
5335 return NULL;
5336 } else {
5337 if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5338 printf("C %s\n", relpath);
5339 return got_error(GOT_ERR_COMMIT_CONFLICT);
5342 if (status != GOT_STATUS_MODIFY &&
5343 status != GOT_STATUS_MODE_CHANGE &&
5344 status != GOT_STATUS_ADD &&
5345 status != GOT_STATUS_DELETE &&
5346 status != GOT_STATUS_CONFLICT)
5347 return NULL;
5350 if (asprintf(&path, "/%s", relpath) == -1) {
5351 err = got_error_from_errno("asprintf");
5352 goto done;
5354 if (strcmp(path, "/") == 0) {
5355 parent_path = strdup("");
5356 if (parent_path == NULL)
5357 return got_error_from_errno("strdup");
5358 } else {
5359 err = got_path_dirname(&parent_path, path);
5360 if (err)
5361 return err;
5364 ct = calloc(1, sizeof(*ct));
5365 if (ct == NULL) {
5366 err = got_error_from_errno("calloc");
5367 goto done;
5370 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5371 relpath) == -1) {
5372 err = got_error_from_errno("asprintf");
5373 goto done;
5376 if (staged_status == GOT_STATUS_ADD ||
5377 staged_status == GOT_STATUS_MODIFY) {
5378 struct got_fileindex_entry *ie;
5379 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5380 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5381 case GOT_FILEIDX_MODE_REGULAR_FILE:
5382 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5383 ct->mode = S_IFREG;
5384 break;
5385 case GOT_FILEIDX_MODE_SYMLINK:
5386 ct->mode = S_IFLNK;
5387 break;
5388 default:
5389 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5390 goto done;
5392 ct->mode |= got_fileindex_entry_perms_get(ie);
5393 } else if (status != GOT_STATUS_DELETE &&
5394 staged_status != GOT_STATUS_DELETE) {
5395 if (dirfd != -1) {
5396 if (fstatat(dirfd, de_name, &sb,
5397 AT_SYMLINK_NOFOLLOW) == -1) {
5398 err = got_error_from_errno2("fstatat",
5399 ct->ondisk_path);
5400 goto done;
5402 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5403 err = got_error_from_errno2("lstat", ct->ondisk_path);
5404 goto done;
5406 ct->mode = sb.st_mode;
5409 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5410 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5411 relpath) == -1) {
5412 err = got_error_from_errno("asprintf");
5413 goto done;
5416 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5417 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5418 int is_bad_symlink;
5419 char target_path[PATH_MAX];
5420 ssize_t target_len;
5421 target_len = readlink(ct->ondisk_path, target_path,
5422 sizeof(target_path));
5423 if (target_len == -1) {
5424 err = got_error_from_errno2("readlink",
5425 ct->ondisk_path);
5426 goto done;
5428 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5429 target_len, ct->ondisk_path, a->worktree->root_path);
5430 if (err)
5431 goto done;
5432 if (is_bad_symlink) {
5433 err = got_error_path(ct->ondisk_path,
5434 GOT_ERR_BAD_SYMLINK);
5435 goto done;
5440 ct->status = status;
5441 ct->staged_status = staged_status;
5442 ct->blob_id = NULL; /* will be filled in when blob gets created */
5443 if (ct->status != GOT_STATUS_ADD &&
5444 ct->staged_status != GOT_STATUS_ADD) {
5445 ct->base_blob_id = got_object_id_dup(blob_id);
5446 if (ct->base_blob_id == NULL) {
5447 err = got_error_from_errno("got_object_id_dup");
5448 goto done;
5450 ct->base_commit_id = got_object_id_dup(commit_id);
5451 if (ct->base_commit_id == NULL) {
5452 err = got_error_from_errno("got_object_id_dup");
5453 goto done;
5456 if (ct->staged_status == GOT_STATUS_ADD ||
5457 ct->staged_status == GOT_STATUS_MODIFY) {
5458 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5459 if (ct->staged_blob_id == NULL) {
5460 err = got_error_from_errno("got_object_id_dup");
5461 goto done;
5464 ct->path = strdup(path);
5465 if (ct->path == NULL) {
5466 err = got_error_from_errno("strdup");
5467 goto done;
5469 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5470 if (err)
5471 goto done;
5473 if (a->diff_outfile && ct && new != NULL) {
5474 err = append_ct_diff(ct, &a->diff_header_shown,
5475 a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5476 a->have_staged_files, a->repo, a->worktree);
5477 if (err)
5478 goto done;
5480 done:
5481 if (ct && (err || new == NULL))
5482 free_commitable(ct);
5483 free(parent_path);
5484 free(path);
5485 return err;
5488 static const struct got_error *write_tree(struct got_object_id **, int *,
5489 struct got_tree_object *, const char *, struct got_pathlist_head *,
5490 got_worktree_status_cb status_cb, void *status_arg,
5491 struct got_repository *);
5493 static const struct got_error *
5494 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5495 struct got_tree_entry *te, const char *parent_path,
5496 struct got_pathlist_head *commitable_paths,
5497 got_worktree_status_cb status_cb, void *status_arg,
5498 struct got_repository *repo)
5500 const struct got_error *err = NULL;
5501 struct got_tree_object *subtree;
5502 char *subpath;
5504 if (asprintf(&subpath, "%s%s%s", parent_path,
5505 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5506 return got_error_from_errno("asprintf");
5508 err = got_object_open_as_tree(&subtree, repo, &te->id);
5509 if (err)
5510 return err;
5512 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5513 commitable_paths, status_cb, status_arg, repo);
5514 got_object_tree_close(subtree);
5515 free(subpath);
5516 return err;
5519 static const struct got_error *
5520 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5522 const struct got_error *err = NULL;
5523 char *ct_parent_path = NULL;
5525 *match = 0;
5527 if (strchr(ct->in_repo_path, '/') == NULL) {
5528 *match = got_path_is_root_dir(path);
5529 return NULL;
5532 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5533 if (err)
5534 return err;
5535 *match = (strcmp(path, ct_parent_path) == 0);
5536 free(ct_parent_path);
5537 return err;
5540 static mode_t
5541 get_ct_file_mode(struct got_commitable *ct)
5543 if (S_ISLNK(ct->mode))
5544 return S_IFLNK;
5546 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5549 static const struct got_error *
5550 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5551 struct got_tree_entry *te, struct got_commitable *ct)
5553 const struct got_error *err = NULL;
5555 *new_te = NULL;
5557 err = got_object_tree_entry_dup(new_te, te);
5558 if (err)
5559 goto done;
5561 (*new_te)->mode = get_ct_file_mode(ct);
5563 if (ct->staged_status == GOT_STATUS_MODIFY)
5564 memcpy(&(*new_te)->id, ct->staged_blob_id,
5565 sizeof((*new_te)->id));
5566 else
5567 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5568 done:
5569 if (err && *new_te) {
5570 free(*new_te);
5571 *new_te = NULL;
5573 return err;
5576 static const struct got_error *
5577 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5578 struct got_commitable *ct)
5580 const struct got_error *err = NULL;
5581 char *ct_name = NULL;
5583 *new_te = NULL;
5585 *new_te = calloc(1, sizeof(**new_te));
5586 if (*new_te == NULL)
5587 return got_error_from_errno("calloc");
5589 err = got_path_basename(&ct_name, ct->path);
5590 if (err)
5591 goto done;
5592 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5593 sizeof((*new_te)->name)) {
5594 err = got_error(GOT_ERR_NO_SPACE);
5595 goto done;
5598 (*new_te)->mode = get_ct_file_mode(ct);
5600 if (ct->staged_status == GOT_STATUS_ADD)
5601 memcpy(&(*new_te)->id, ct->staged_blob_id,
5602 sizeof((*new_te)->id));
5603 else
5604 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5605 done:
5606 free(ct_name);
5607 if (err && *new_te) {
5608 free(*new_te);
5609 *new_te = NULL;
5611 return err;
5614 static const struct got_error *
5615 insert_tree_entry(struct got_tree_entry *new_te,
5616 struct got_pathlist_head *paths)
5618 const struct got_error *err = NULL;
5619 struct got_pathlist_entry *new_pe;
5621 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5622 if (err)
5623 return err;
5624 if (new_pe == NULL)
5625 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5626 return NULL;
5629 static const struct got_error *
5630 report_ct_status(struct got_commitable *ct,
5631 got_worktree_status_cb status_cb, void *status_arg)
5633 const char *ct_path = ct->path;
5634 unsigned char status;
5636 if (status_cb == NULL) /* no commit progress output desired */
5637 return NULL;
5639 while (ct_path[0] == '/')
5640 ct_path++;
5642 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5643 status = ct->staged_status;
5644 else
5645 status = ct->status;
5647 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5648 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5651 static const struct got_error *
5652 match_modified_subtree(int *modified, struct got_tree_entry *te,
5653 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5655 const struct got_error *err = NULL;
5656 struct got_pathlist_entry *pe;
5657 char *te_path;
5659 *modified = 0;
5661 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5662 got_path_is_root_dir(base_tree_path) ? "" : "/",
5663 te->name) == -1)
5664 return got_error_from_errno("asprintf");
5666 TAILQ_FOREACH(pe, commitable_paths, entry) {
5667 struct got_commitable *ct = pe->data;
5668 *modified = got_path_is_child(ct->in_repo_path, te_path,
5669 strlen(te_path));
5670 if (*modified)
5671 break;
5674 free(te_path);
5675 return err;
5678 static const struct got_error *
5679 match_deleted_or_modified_ct(struct got_commitable **ctp,
5680 struct got_tree_entry *te, const char *base_tree_path,
5681 struct got_pathlist_head *commitable_paths)
5683 const struct got_error *err = NULL;
5684 struct got_pathlist_entry *pe;
5686 *ctp = NULL;
5688 TAILQ_FOREACH(pe, commitable_paths, entry) {
5689 struct got_commitable *ct = pe->data;
5690 char *ct_name = NULL;
5691 int path_matches;
5693 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5694 if (ct->status != GOT_STATUS_MODIFY &&
5695 ct->status != GOT_STATUS_MODE_CHANGE &&
5696 ct->status != GOT_STATUS_DELETE &&
5697 ct->status != GOT_STATUS_CONFLICT)
5698 continue;
5699 } else {
5700 if (ct->staged_status != GOT_STATUS_MODIFY &&
5701 ct->staged_status != GOT_STATUS_DELETE)
5702 continue;
5705 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5706 continue;
5708 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5709 if (err)
5710 return err;
5711 if (!path_matches)
5712 continue;
5714 err = got_path_basename(&ct_name, pe->path);
5715 if (err)
5716 return err;
5718 if (strcmp(te->name, ct_name) != 0) {
5719 free(ct_name);
5720 continue;
5722 free(ct_name);
5724 *ctp = ct;
5725 break;
5728 return err;
5731 static const struct got_error *
5732 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5733 const char *child_path, const char *path_base_tree,
5734 struct got_pathlist_head *commitable_paths,
5735 got_worktree_status_cb status_cb, void *status_arg,
5736 struct got_repository *repo)
5738 const struct got_error *err = NULL;
5739 struct got_tree_entry *new_te;
5740 char *subtree_path;
5741 struct got_object_id *id = NULL;
5742 int nentries;
5744 *new_tep = NULL;
5746 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5747 got_path_is_root_dir(path_base_tree) ? "" : "/",
5748 child_path) == -1)
5749 return got_error_from_errno("asprintf");
5751 new_te = calloc(1, sizeof(*new_te));
5752 if (new_te == NULL)
5753 return got_error_from_errno("calloc");
5754 new_te->mode = S_IFDIR;
5756 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5757 sizeof(new_te->name)) {
5758 err = got_error(GOT_ERR_NO_SPACE);
5759 goto done;
5761 err = write_tree(&id, &nentries, NULL, subtree_path,
5762 commitable_paths, status_cb, status_arg, repo);
5763 if (err) {
5764 free(new_te);
5765 goto done;
5767 memcpy(&new_te->id, id, sizeof(new_te->id));
5768 done:
5769 free(id);
5770 free(subtree_path);
5771 if (err == NULL)
5772 *new_tep = new_te;
5773 return err;
5776 static const struct got_error *
5777 write_tree(struct got_object_id **new_tree_id, int *nentries,
5778 struct got_tree_object *base_tree, const char *path_base_tree,
5779 struct got_pathlist_head *commitable_paths,
5780 got_worktree_status_cb status_cb, void *status_arg,
5781 struct got_repository *repo)
5783 const struct got_error *err = NULL;
5784 struct got_pathlist_head paths;
5785 struct got_tree_entry *te, *new_te = NULL;
5786 struct got_pathlist_entry *pe;
5788 TAILQ_INIT(&paths);
5789 *nentries = 0;
5791 /* Insert, and recurse into, newly added entries first. */
5792 TAILQ_FOREACH(pe, commitable_paths, entry) {
5793 struct got_commitable *ct = pe->data;
5794 char *child_path = NULL, *slash;
5796 if ((ct->status != GOT_STATUS_ADD &&
5797 ct->staged_status != GOT_STATUS_ADD) ||
5798 (ct->flags & GOT_COMMITABLE_ADDED))
5799 continue;
5801 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5802 strlen(path_base_tree)))
5803 continue;
5805 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5806 ct->in_repo_path);
5807 if (err)
5808 goto done;
5810 slash = strchr(child_path, '/');
5811 if (slash == NULL) {
5812 err = alloc_added_blob_tree_entry(&new_te, ct);
5813 if (err)
5814 goto done;
5815 err = report_ct_status(ct, status_cb, status_arg);
5816 if (err)
5817 goto done;
5818 ct->flags |= GOT_COMMITABLE_ADDED;
5819 err = insert_tree_entry(new_te, &paths);
5820 if (err)
5821 goto done;
5822 (*nentries)++;
5823 } else {
5824 *slash = '\0'; /* trim trailing path components */
5825 if (base_tree == NULL ||
5826 got_object_tree_find_entry(base_tree, child_path)
5827 == NULL) {
5828 err = make_subtree_for_added_blob(&new_te,
5829 child_path, path_base_tree,
5830 commitable_paths, status_cb, status_arg,
5831 repo);
5832 if (err)
5833 goto done;
5834 err = insert_tree_entry(new_te, &paths);
5835 if (err)
5836 goto done;
5837 (*nentries)++;
5842 if (base_tree) {
5843 int i, nbase_entries;
5844 /* Handle modified and deleted entries. */
5845 nbase_entries = got_object_tree_get_nentries(base_tree);
5846 for (i = 0; i < nbase_entries; i++) {
5847 struct got_commitable *ct = NULL;
5849 te = got_object_tree_get_entry(base_tree, i);
5850 if (got_object_tree_entry_is_submodule(te)) {
5851 /* Entry is a submodule; just copy it. */
5852 err = got_object_tree_entry_dup(&new_te, te);
5853 if (err)
5854 goto done;
5855 err = insert_tree_entry(new_te, &paths);
5856 if (err)
5857 goto done;
5858 (*nentries)++;
5859 continue;
5862 if (S_ISDIR(te->mode)) {
5863 int modified;
5864 err = got_object_tree_entry_dup(&new_te, te);
5865 if (err)
5866 goto done;
5867 err = match_modified_subtree(&modified, te,
5868 path_base_tree, commitable_paths);
5869 if (err)
5870 goto done;
5871 /* Avoid recursion into unmodified subtrees. */
5872 if (modified) {
5873 struct got_object_id *new_id;
5874 int nsubentries;
5875 err = write_subtree(&new_id,
5876 &nsubentries, te,
5877 path_base_tree, commitable_paths,
5878 status_cb, status_arg, repo);
5879 if (err)
5880 goto done;
5881 if (nsubentries == 0) {
5882 /* All entries were deleted. */
5883 free(new_id);
5884 continue;
5886 memcpy(&new_te->id, new_id,
5887 sizeof(new_te->id));
5888 free(new_id);
5890 err = insert_tree_entry(new_te, &paths);
5891 if (err)
5892 goto done;
5893 (*nentries)++;
5894 continue;
5897 err = match_deleted_or_modified_ct(&ct, te,
5898 path_base_tree, commitable_paths);
5899 if (err)
5900 goto done;
5901 if (ct) {
5902 /* NB: Deleted entries get dropped here. */
5903 if (ct->status == GOT_STATUS_MODIFY ||
5904 ct->status == GOT_STATUS_MODE_CHANGE ||
5905 ct->status == GOT_STATUS_CONFLICT ||
5906 ct->staged_status == GOT_STATUS_MODIFY) {
5907 err = alloc_modified_blob_tree_entry(
5908 &new_te, te, ct);
5909 if (err)
5910 goto done;
5911 err = insert_tree_entry(new_te, &paths);
5912 if (err)
5913 goto done;
5914 (*nentries)++;
5916 err = report_ct_status(ct, status_cb,
5917 status_arg);
5918 if (err)
5919 goto done;
5920 } else {
5921 /* Entry is unchanged; just copy it. */
5922 err = got_object_tree_entry_dup(&new_te, te);
5923 if (err)
5924 goto done;
5925 err = insert_tree_entry(new_te, &paths);
5926 if (err)
5927 goto done;
5928 (*nentries)++;
5933 /* Write new list of entries; deleted entries have been dropped. */
5934 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5935 done:
5936 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
5937 return err;
5940 static const struct got_error *
5941 update_fileindex_after_commit(struct got_worktree *worktree,
5942 struct got_pathlist_head *commitable_paths,
5943 struct got_object_id *new_base_commit_id,
5944 struct got_fileindex *fileindex, int have_staged_files)
5946 const struct got_error *err = NULL;
5947 struct got_pathlist_entry *pe;
5948 char *relpath = NULL;
5950 TAILQ_FOREACH(pe, commitable_paths, entry) {
5951 struct got_fileindex_entry *ie;
5952 struct got_commitable *ct = pe->data;
5954 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5956 err = got_path_skip_common_ancestor(&relpath,
5957 worktree->root_path, ct->ondisk_path);
5958 if (err)
5959 goto done;
5961 if (ie) {
5962 if (ct->status == GOT_STATUS_DELETE ||
5963 ct->staged_status == GOT_STATUS_DELETE) {
5964 got_fileindex_entry_remove(fileindex, ie);
5965 } else if (ct->staged_status == GOT_STATUS_ADD ||
5966 ct->staged_status == GOT_STATUS_MODIFY) {
5967 got_fileindex_entry_stage_set(ie,
5968 GOT_FILEIDX_STAGE_NONE);
5969 got_fileindex_entry_staged_filetype_set(ie, 0);
5971 err = got_fileindex_entry_update(ie,
5972 worktree->root_fd, relpath,
5973 ct->staged_blob_id->sha1,
5974 new_base_commit_id->sha1,
5975 !have_staged_files);
5976 } else
5977 err = got_fileindex_entry_update(ie,
5978 worktree->root_fd, relpath,
5979 ct->blob_id->sha1,
5980 new_base_commit_id->sha1,
5981 !have_staged_files);
5982 } else {
5983 err = got_fileindex_entry_alloc(&ie, pe->path);
5984 if (err)
5985 goto done;
5986 err = got_fileindex_entry_update(ie,
5987 worktree->root_fd, relpath, ct->blob_id->sha1,
5988 new_base_commit_id->sha1, 1);
5989 if (err) {
5990 got_fileindex_entry_free(ie);
5991 goto done;
5993 err = got_fileindex_entry_add(fileindex, ie);
5994 if (err) {
5995 got_fileindex_entry_free(ie);
5996 goto done;
5999 free(relpath);
6000 relpath = NULL;
6002 done:
6003 free(relpath);
6004 return err;
6008 static const struct got_error *
6009 check_out_of_date(const char *in_repo_path, unsigned char status,
6010 unsigned char staged_status, struct got_object_id *base_blob_id,
6011 struct got_object_id *base_commit_id,
6012 struct got_object_id *head_commit_id, struct got_repository *repo,
6013 int ood_errcode)
6015 const struct got_error *err = NULL;
6016 struct got_commit_object *commit = NULL;
6017 struct got_object_id *id = NULL;
6019 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
6020 /* Trivial case: base commit == head commit */
6021 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
6022 return NULL;
6024 * Ensure file content which local changes were based
6025 * on matches file content in the branch head.
6027 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6028 if (err)
6029 goto done;
6030 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6031 if (err) {
6032 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6033 err = got_error(ood_errcode);
6034 goto done;
6035 } else if (got_object_id_cmp(id, base_blob_id) != 0)
6036 err = got_error(ood_errcode);
6037 } else {
6038 /* Require that added files don't exist in the branch head. */
6039 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6040 if (err)
6041 goto done;
6042 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6043 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
6044 goto done;
6045 err = id ? got_error(ood_errcode) : NULL;
6047 done:
6048 free(id);
6049 if (commit)
6050 got_object_commit_close(commit);
6051 return err;
6054 static const struct got_error *
6055 commit_worktree(struct got_object_id **new_commit_id,
6056 struct got_pathlist_head *commitable_paths,
6057 struct got_object_id *head_commit_id,
6058 struct got_object_id *parent_id2,
6059 struct got_worktree *worktree,
6060 const char *author, const char *committer, char *diff_path,
6061 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6062 got_worktree_status_cb status_cb, void *status_arg,
6063 struct got_repository *repo)
6065 const struct got_error *err = NULL, *unlockerr = NULL;
6066 struct got_pathlist_entry *pe;
6067 const char *head_ref_name = NULL;
6068 struct got_commit_object *head_commit = NULL;
6069 struct got_reference *head_ref2 = NULL;
6070 struct got_object_id *head_commit_id2 = NULL;
6071 struct got_tree_object *head_tree = NULL;
6072 struct got_object_id *new_tree_id = NULL;
6073 int nentries, nparents = 0;
6074 struct got_object_id_queue parent_ids;
6075 struct got_object_qid *pid = NULL;
6076 char *logmsg = NULL;
6077 time_t timestamp;
6079 *new_commit_id = NULL;
6081 STAILQ_INIT(&parent_ids);
6083 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6084 if (err)
6085 goto done;
6087 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6088 if (err)
6089 goto done;
6091 if (commit_msg_cb != NULL) {
6092 err = commit_msg_cb(commitable_paths, diff_path,
6093 &logmsg, commit_arg);
6094 if (err)
6095 goto done;
6098 if (logmsg == NULL || strlen(logmsg) == 0) {
6099 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6100 goto done;
6103 /* Create blobs from added and modified files and record their IDs. */
6104 TAILQ_FOREACH(pe, commitable_paths, entry) {
6105 struct got_commitable *ct = pe->data;
6106 char *ondisk_path;
6108 /* Blobs for staged files already exist. */
6109 if (ct->staged_status == GOT_STATUS_ADD ||
6110 ct->staged_status == GOT_STATUS_MODIFY)
6111 continue;
6113 if (ct->status != GOT_STATUS_ADD &&
6114 ct->status != GOT_STATUS_MODIFY &&
6115 ct->status != GOT_STATUS_MODE_CHANGE &&
6116 ct->status != GOT_STATUS_CONFLICT)
6117 continue;
6119 if (asprintf(&ondisk_path, "%s/%s",
6120 worktree->root_path, pe->path) == -1) {
6121 err = got_error_from_errno("asprintf");
6122 goto done;
6124 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6125 free(ondisk_path);
6126 if (err)
6127 goto done;
6130 /* Recursively write new tree objects. */
6131 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6132 commitable_paths, status_cb, status_arg, repo);
6133 if (err)
6134 goto done;
6136 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
6137 if (err)
6138 goto done;
6139 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6140 nparents++;
6141 if (parent_id2) {
6142 err = got_object_qid_alloc(&pid, parent_id2);
6143 if (err)
6144 goto done;
6145 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6146 nparents++;
6148 timestamp = time(NULL);
6149 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6150 nparents, author, timestamp, committer, timestamp, logmsg, repo);
6151 if (logmsg != NULL)
6152 free(logmsg);
6153 if (err)
6154 goto done;
6156 /* Check if a concurrent commit to our branch has occurred. */
6157 head_ref_name = got_worktree_get_head_ref_name(worktree);
6158 if (head_ref_name == NULL) {
6159 err = got_error_from_errno("got_worktree_get_head_ref_name");
6160 goto done;
6162 /* Lock the reference here to prevent concurrent modification. */
6163 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6164 if (err)
6165 goto done;
6166 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6167 if (err)
6168 goto done;
6169 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6170 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6171 goto done;
6173 /* Update branch head in repository. */
6174 err = got_ref_change_ref(head_ref2, *new_commit_id);
6175 if (err)
6176 goto done;
6177 err = got_ref_write(head_ref2, repo);
6178 if (err)
6179 goto done;
6181 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6182 if (err)
6183 goto done;
6185 err = ref_base_commit(worktree, repo);
6186 if (err)
6187 goto done;
6188 done:
6189 got_object_id_queue_free(&parent_ids);
6190 if (head_tree)
6191 got_object_tree_close(head_tree);
6192 if (head_commit)
6193 got_object_commit_close(head_commit);
6194 free(head_commit_id2);
6195 if (head_ref2) {
6196 unlockerr = got_ref_unlock(head_ref2);
6197 if (unlockerr && err == NULL)
6198 err = unlockerr;
6199 got_ref_close(head_ref2);
6201 return err;
6204 static const struct got_error *
6205 check_path_is_commitable(const char *path,
6206 struct got_pathlist_head *commitable_paths)
6208 struct got_pathlist_entry *cpe = NULL;
6209 size_t path_len = strlen(path);
6211 TAILQ_FOREACH(cpe, commitable_paths, entry) {
6212 struct got_commitable *ct = cpe->data;
6213 const char *ct_path = ct->path;
6215 while (ct_path[0] == '/')
6216 ct_path++;
6218 if (strcmp(path, ct_path) == 0 ||
6219 got_path_is_child(ct_path, path, path_len))
6220 break;
6223 if (cpe == NULL)
6224 return got_error_path(path, GOT_ERR_BAD_PATH);
6226 return NULL;
6229 static const struct got_error *
6230 check_staged_file(void *arg, struct got_fileindex_entry *ie)
6232 int *have_staged_files = arg;
6234 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6235 *have_staged_files = 1;
6236 return got_error(GOT_ERR_CANCELLED);
6239 return NULL;
6242 static const struct got_error *
6243 check_non_staged_files(struct got_fileindex *fileindex,
6244 struct got_pathlist_head *paths)
6246 struct got_pathlist_entry *pe;
6247 struct got_fileindex_entry *ie;
6249 TAILQ_FOREACH(pe, paths, entry) {
6250 if (pe->path[0] == '\0')
6251 continue;
6252 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6253 if (ie == NULL)
6254 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6255 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6256 return got_error_path(pe->path,
6257 GOT_ERR_FILE_NOT_STAGED);
6260 return NULL;
6263 const struct got_error *
6264 got_worktree_commit(struct got_object_id **new_commit_id,
6265 struct got_worktree *worktree, struct got_pathlist_head *paths,
6266 const char *author, const char *committer, int allow_bad_symlinks,
6267 int show_diff, int commit_conflicts,
6268 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6269 got_worktree_status_cb status_cb, void *status_arg,
6270 struct got_repository *repo)
6272 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6273 struct got_fileindex *fileindex = NULL;
6274 char *fileindex_path = NULL;
6275 struct got_pathlist_head commitable_paths;
6276 struct collect_commitables_arg cc_arg;
6277 struct got_pathlist_entry *pe;
6278 struct got_reference *head_ref = NULL;
6279 struct got_object_id *head_commit_id = NULL;
6280 char *diff_path = NULL;
6281 int have_staged_files = 0;
6283 *new_commit_id = NULL;
6285 memset(&cc_arg, 0, sizeof(cc_arg));
6286 TAILQ_INIT(&commitable_paths);
6288 err = lock_worktree(worktree, LOCK_EX);
6289 if (err)
6290 goto done;
6292 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6293 if (err)
6294 goto done;
6296 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6297 if (err)
6298 goto done;
6300 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6301 if (err)
6302 goto done;
6304 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6305 &have_staged_files);
6306 if (err && err->code != GOT_ERR_CANCELLED)
6307 goto done;
6308 if (have_staged_files) {
6309 err = check_non_staged_files(fileindex, paths);
6310 if (err)
6311 goto done;
6314 cc_arg.commitable_paths = &commitable_paths;
6315 cc_arg.worktree = worktree;
6316 cc_arg.fileindex = fileindex;
6317 cc_arg.repo = repo;
6318 cc_arg.have_staged_files = have_staged_files;
6319 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6320 cc_arg.diff_header_shown = 0;
6321 cc_arg.commit_conflicts = commit_conflicts;
6322 if (show_diff) {
6323 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6324 GOT_TMPDIR_STR "/got", ".diff");
6325 if (err)
6326 goto done;
6327 cc_arg.f1 = got_opentemp();
6328 if (cc_arg.f1 == NULL) {
6329 err = got_error_from_errno("got_opentemp");
6330 goto done;
6332 cc_arg.f2 = got_opentemp();
6333 if (cc_arg.f2 == NULL) {
6334 err = got_error_from_errno("got_opentemp");
6335 goto done;
6339 TAILQ_FOREACH(pe, paths, entry) {
6340 err = worktree_status(worktree, pe->path, fileindex, repo,
6341 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6342 if (err)
6343 goto done;
6346 if (show_diff) {
6347 if (fflush(cc_arg.diff_outfile) == EOF) {
6348 err = got_error_from_errno("fflush");
6349 goto done;
6353 if (TAILQ_EMPTY(&commitable_paths)) {
6354 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6355 goto done;
6358 TAILQ_FOREACH(pe, paths, entry) {
6359 err = check_path_is_commitable(pe->path, &commitable_paths);
6360 if (err)
6361 goto done;
6364 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6365 struct got_commitable *ct = pe->data;
6366 const char *ct_path = ct->in_repo_path;
6368 while (ct_path[0] == '/')
6369 ct_path++;
6370 err = check_out_of_date(ct_path, ct->status,
6371 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6372 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6373 if (err)
6374 goto done;
6378 err = commit_worktree(new_commit_id, &commitable_paths,
6379 head_commit_id, NULL, worktree, author, committer,
6380 (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6381 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6382 if (err)
6383 goto done;
6385 err = update_fileindex_after_commit(worktree, &commitable_paths,
6386 *new_commit_id, fileindex, have_staged_files);
6387 sync_err = sync_fileindex(fileindex, fileindex_path);
6388 if (sync_err && err == NULL)
6389 err = sync_err;
6390 done:
6391 if (fileindex)
6392 got_fileindex_free(fileindex);
6393 free(fileindex_path);
6394 unlockerr = lock_worktree(worktree, LOCK_SH);
6395 if (unlockerr && err == NULL)
6396 err = unlockerr;
6397 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6398 struct got_commitable *ct = pe->data;
6400 free_commitable(ct);
6402 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6403 if (diff_path && unlink(diff_path) == -1 && err == NULL)
6404 err = got_error_from_errno2("unlink", diff_path);
6405 free(diff_path);
6406 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6407 err == NULL)
6408 err = got_error_from_errno("fclose");
6409 return err;
6412 const char *
6413 got_commitable_get_path(struct got_commitable *ct)
6415 return ct->path;
6418 unsigned int
6419 got_commitable_get_status(struct got_commitable *ct)
6421 return ct->status;
6424 struct check_rebase_ok_arg {
6425 struct got_worktree *worktree;
6426 struct got_repository *repo;
6429 static const struct got_error *
6430 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6432 const struct got_error *err = NULL;
6433 struct check_rebase_ok_arg *a = arg;
6434 unsigned char status;
6435 struct stat sb;
6436 char *ondisk_path;
6438 /* Reject rebase of a work tree with mixed base commits. */
6439 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6440 SHA1_DIGEST_LENGTH))
6441 return got_error(GOT_ERR_MIXED_COMMITS);
6443 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6444 == -1)
6445 return got_error_from_errno("asprintf");
6447 /* Reject rebase of a work tree with modified or staged files. */
6448 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6449 free(ondisk_path);
6450 if (err)
6451 return err;
6453 if (status != GOT_STATUS_NO_CHANGE)
6454 return got_error(GOT_ERR_MODIFIED);
6455 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6456 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6458 return NULL;
6461 const struct got_error *
6462 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6463 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6464 struct got_worktree *worktree, struct got_reference *branch,
6465 struct got_repository *repo)
6467 const struct got_error *err = NULL;
6468 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6469 char *branch_ref_name = NULL;
6470 char *fileindex_path = NULL;
6471 struct check_rebase_ok_arg ok_arg;
6472 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6473 struct got_object_id *wt_branch_tip = NULL;
6475 *new_base_branch_ref = NULL;
6476 *tmp_branch = NULL;
6477 *fileindex = NULL;
6479 err = lock_worktree(worktree, LOCK_EX);
6480 if (err)
6481 return err;
6483 err = open_fileindex(fileindex, &fileindex_path, worktree);
6484 if (err)
6485 goto done;
6487 ok_arg.worktree = worktree;
6488 ok_arg.repo = repo;
6489 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6490 &ok_arg);
6491 if (err)
6492 goto done;
6494 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6495 if (err)
6496 goto done;
6498 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6499 if (err)
6500 goto done;
6502 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6503 if (err)
6504 goto done;
6506 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6507 0);
6508 if (err)
6509 goto done;
6511 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6512 if (err)
6513 goto done;
6514 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6515 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6516 goto done;
6519 err = got_ref_alloc_symref(new_base_branch_ref,
6520 new_base_branch_ref_name, wt_branch);
6521 if (err)
6522 goto done;
6523 err = got_ref_write(*new_base_branch_ref, repo);
6524 if (err)
6525 goto done;
6527 /* TODO Lock original branch's ref while rebasing? */
6529 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6530 if (err)
6531 goto done;
6533 err = got_ref_write(branch_ref, repo);
6534 if (err)
6535 goto done;
6537 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6538 worktree->base_commit_id);
6539 if (err)
6540 goto done;
6541 err = got_ref_write(*tmp_branch, repo);
6542 if (err)
6543 goto done;
6545 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6546 if (err)
6547 goto done;
6548 done:
6549 free(fileindex_path);
6550 free(tmp_branch_name);
6551 free(new_base_branch_ref_name);
6552 free(branch_ref_name);
6553 if (branch_ref)
6554 got_ref_close(branch_ref);
6555 if (wt_branch)
6556 got_ref_close(wt_branch);
6557 free(wt_branch_tip);
6558 if (err) {
6559 if (*new_base_branch_ref) {
6560 got_ref_close(*new_base_branch_ref);
6561 *new_base_branch_ref = NULL;
6563 if (*tmp_branch) {
6564 got_ref_close(*tmp_branch);
6565 *tmp_branch = NULL;
6567 if (*fileindex) {
6568 got_fileindex_free(*fileindex);
6569 *fileindex = NULL;
6571 lock_worktree(worktree, LOCK_SH);
6573 return err;
6576 const struct got_error *
6577 got_worktree_rebase_continue(struct got_object_id **commit_id,
6578 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6579 struct got_reference **branch, struct got_fileindex **fileindex,
6580 struct got_worktree *worktree, struct got_repository *repo)
6582 const struct got_error *err;
6583 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6584 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6585 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6586 char *fileindex_path = NULL;
6587 int have_staged_files = 0;
6589 *commit_id = NULL;
6590 *new_base_branch = NULL;
6591 *tmp_branch = NULL;
6592 *branch = NULL;
6593 *fileindex = NULL;
6595 err = lock_worktree(worktree, LOCK_EX);
6596 if (err)
6597 return err;
6599 err = open_fileindex(fileindex, &fileindex_path, worktree);
6600 if (err)
6601 goto done;
6603 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6604 &have_staged_files);
6605 if (err && err->code != GOT_ERR_CANCELLED)
6606 goto done;
6607 if (have_staged_files) {
6608 err = got_error(GOT_ERR_STAGED_PATHS);
6609 goto done;
6612 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6613 if (err)
6614 goto done;
6616 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6617 if (err)
6618 goto done;
6620 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6621 if (err)
6622 goto done;
6624 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6625 if (err)
6626 goto done;
6628 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6629 if (err)
6630 goto done;
6632 err = got_ref_open(branch, repo,
6633 got_ref_get_symref_target(branch_ref), 0);
6634 if (err)
6635 goto done;
6637 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6638 if (err)
6639 goto done;
6641 err = got_ref_resolve(commit_id, repo, commit_ref);
6642 if (err)
6643 goto done;
6645 err = got_ref_open(new_base_branch, repo,
6646 new_base_branch_ref_name, 0);
6647 if (err)
6648 goto done;
6650 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6651 if (err)
6652 goto done;
6653 done:
6654 free(commit_ref_name);
6655 free(branch_ref_name);
6656 free(fileindex_path);
6657 if (commit_ref)
6658 got_ref_close(commit_ref);
6659 if (branch_ref)
6660 got_ref_close(branch_ref);
6661 if (err) {
6662 free(*commit_id);
6663 *commit_id = NULL;
6664 if (*tmp_branch) {
6665 got_ref_close(*tmp_branch);
6666 *tmp_branch = NULL;
6668 if (*new_base_branch) {
6669 got_ref_close(*new_base_branch);
6670 *new_base_branch = NULL;
6672 if (*branch) {
6673 got_ref_close(*branch);
6674 *branch = NULL;
6676 if (*fileindex) {
6677 got_fileindex_free(*fileindex);
6678 *fileindex = NULL;
6680 lock_worktree(worktree, LOCK_SH);
6682 return err;
6685 const struct got_error *
6686 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6688 const struct got_error *err;
6689 char *tmp_branch_name = NULL;
6691 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6692 if (err)
6693 return err;
6695 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6696 free(tmp_branch_name);
6697 return NULL;
6700 static const struct got_error *
6701 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6702 const char *diff_path, char **logmsg, void *arg)
6704 *logmsg = arg;
6705 return NULL;
6708 static const struct got_error *
6709 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6710 const char *path, struct got_object_id *blob_id,
6711 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6712 int dirfd, const char *de_name)
6714 return NULL;
6717 struct collect_merged_paths_arg {
6718 got_worktree_checkout_cb progress_cb;
6719 void *progress_arg;
6720 struct got_pathlist_head *merged_paths;
6723 static const struct got_error *
6724 collect_merged_paths(void *arg, unsigned char status, const char *path)
6726 const struct got_error *err;
6727 struct collect_merged_paths_arg *a = arg;
6728 char *p;
6729 struct got_pathlist_entry *new;
6731 err = (*a->progress_cb)(a->progress_arg, status, path);
6732 if (err)
6733 return err;
6735 if (status != GOT_STATUS_MERGE &&
6736 status != GOT_STATUS_ADD &&
6737 status != GOT_STATUS_DELETE &&
6738 status != GOT_STATUS_CONFLICT)
6739 return NULL;
6741 p = strdup(path);
6742 if (p == NULL)
6743 return got_error_from_errno("strdup");
6745 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6746 if (err || new == NULL)
6747 free(p);
6748 return err;
6751 static const struct got_error *
6752 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6753 int is_rebase, struct got_repository *repo)
6755 const struct got_error *err;
6756 struct got_reference *commit_ref = NULL;
6758 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6759 if (err) {
6760 if (err->code != GOT_ERR_NOT_REF)
6761 goto done;
6762 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6763 if (err)
6764 goto done;
6765 err = got_ref_write(commit_ref, repo);
6766 if (err)
6767 goto done;
6768 } else if (is_rebase) {
6769 struct got_object_id *stored_id;
6770 int cmp;
6772 err = got_ref_resolve(&stored_id, repo, commit_ref);
6773 if (err)
6774 goto done;
6775 cmp = got_object_id_cmp(commit_id, stored_id);
6776 free(stored_id);
6777 if (cmp != 0) {
6778 err = got_error(GOT_ERR_REBASE_COMMITID);
6779 goto done;
6782 done:
6783 if (commit_ref)
6784 got_ref_close(commit_ref);
6785 return err;
6788 static const struct got_error *
6789 rebase_merge_files(struct got_pathlist_head *merged_paths,
6790 const char *commit_ref_name, struct got_worktree *worktree,
6791 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6792 struct got_object_id *commit_id, struct got_repository *repo,
6793 got_worktree_checkout_cb progress_cb, void *progress_arg,
6794 got_cancel_cb cancel_cb, void *cancel_arg)
6796 const struct got_error *err;
6797 struct got_reference *commit_ref = NULL;
6798 struct collect_merged_paths_arg cmp_arg;
6799 char *fileindex_path;
6801 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6803 err = get_fileindex_path(&fileindex_path, worktree);
6804 if (err)
6805 return err;
6807 cmp_arg.progress_cb = progress_cb;
6808 cmp_arg.progress_arg = progress_arg;
6809 cmp_arg.merged_paths = merged_paths;
6810 err = merge_files(worktree, fileindex, fileindex_path,
6811 parent_commit_id, commit_id, repo, collect_merged_paths,
6812 &cmp_arg, cancel_cb, cancel_arg);
6813 if (commit_ref)
6814 got_ref_close(commit_ref);
6815 return err;
6818 const struct got_error *
6819 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6820 struct got_worktree *worktree, struct got_fileindex *fileindex,
6821 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6822 struct got_repository *repo,
6823 got_worktree_checkout_cb progress_cb, void *progress_arg,
6824 got_cancel_cb cancel_cb, void *cancel_arg)
6826 const struct got_error *err;
6827 char *commit_ref_name;
6829 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6830 if (err)
6831 return err;
6833 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6834 if (err)
6835 goto done;
6837 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6838 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6839 progress_arg, cancel_cb, cancel_arg);
6840 done:
6841 free(commit_ref_name);
6842 return err;
6845 const struct got_error *
6846 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6847 struct got_worktree *worktree, struct got_fileindex *fileindex,
6848 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6849 struct got_repository *repo,
6850 got_worktree_checkout_cb progress_cb, void *progress_arg,
6851 got_cancel_cb cancel_cb, void *cancel_arg)
6853 const struct got_error *err;
6854 char *commit_ref_name;
6856 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6857 if (err)
6858 return err;
6860 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6861 if (err)
6862 goto done;
6864 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6865 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6866 progress_arg, cancel_cb, cancel_arg);
6867 done:
6868 free(commit_ref_name);
6869 return err;
6872 static const struct got_error *
6873 rebase_commit(struct got_object_id **new_commit_id,
6874 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6875 struct got_worktree *worktree, struct got_fileindex *fileindex,
6876 struct got_reference *tmp_branch, const char *committer,
6877 struct got_commit_object *orig_commit, const char *new_logmsg,
6878 int allow_conflict, struct got_repository *repo)
6880 const struct got_error *err, *sync_err;
6881 struct got_pathlist_head commitable_paths;
6882 struct collect_commitables_arg cc_arg;
6883 char *fileindex_path = NULL;
6884 struct got_reference *head_ref = NULL;
6885 struct got_object_id *head_commit_id = NULL;
6886 char *logmsg = NULL;
6888 memset(&cc_arg, 0, sizeof(cc_arg));
6889 TAILQ_INIT(&commitable_paths);
6890 *new_commit_id = NULL;
6892 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6894 err = get_fileindex_path(&fileindex_path, worktree);
6895 if (err)
6896 return err;
6898 cc_arg.commitable_paths = &commitable_paths;
6899 cc_arg.worktree = worktree;
6900 cc_arg.repo = repo;
6901 cc_arg.have_staged_files = 0;
6902 cc_arg.commit_conflicts = allow_conflict;
6904 * If possible get the status of individual files directly to
6905 * avoid crawling the entire work tree once per rebased commit.
6907 * Ideally, merged_paths would contain a list of commitables
6908 * we could use so we could skip worktree_status() entirely.
6909 * However, we would then need carefully keep track of cumulative
6910 * effects of operations such as file additions and deletions
6911 * in 'got histedit -f' (folding multiple commits into one),
6912 * and this extra complexity is not really worth it.
6914 if (merged_paths) {
6915 struct got_pathlist_entry *pe;
6916 TAILQ_FOREACH(pe, merged_paths, entry) {
6917 err = worktree_status(worktree, pe->path, fileindex,
6918 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6919 0);
6920 if (err)
6921 goto done;
6923 } else {
6924 err = worktree_status(worktree, "", fileindex, repo,
6925 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6926 if (err)
6927 goto done;
6930 if (TAILQ_EMPTY(&commitable_paths)) {
6931 /* No-op change; commit will be elided. */
6932 err = got_ref_delete(commit_ref, repo);
6933 if (err)
6934 goto done;
6935 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6936 goto done;
6939 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6940 if (err)
6941 goto done;
6943 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6944 if (err)
6945 goto done;
6947 if (new_logmsg) {
6948 logmsg = strdup(new_logmsg);
6949 if (logmsg == NULL) {
6950 err = got_error_from_errno("strdup");
6951 goto done;
6953 } else {
6954 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6955 if (err)
6956 goto done;
6959 /* NB: commit_worktree will call free(logmsg) */
6960 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6961 NULL, worktree, got_object_commit_get_author(orig_commit),
6962 committer ? committer :
6963 got_object_commit_get_committer(orig_commit), NULL,
6964 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6965 if (err)
6966 goto done;
6968 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6969 if (err)
6970 goto done;
6972 err = got_ref_delete(commit_ref, repo);
6973 if (err)
6974 goto done;
6976 err = update_fileindex_after_commit(worktree, &commitable_paths,
6977 *new_commit_id, fileindex, 0);
6978 sync_err = sync_fileindex(fileindex, fileindex_path);
6979 if (sync_err && err == NULL)
6980 err = sync_err;
6981 done:
6982 free(fileindex_path);
6983 free(head_commit_id);
6984 if (head_ref)
6985 got_ref_close(head_ref);
6986 if (err) {
6987 free(*new_commit_id);
6988 *new_commit_id = NULL;
6990 return err;
6993 const struct got_error *
6994 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6995 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6996 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6997 const char *committer, struct got_commit_object *orig_commit,
6998 struct got_object_id *orig_commit_id, int allow_conflict,
6999 struct got_repository *repo)
7001 const struct got_error *err;
7002 char *commit_ref_name;
7003 struct got_reference *commit_ref = NULL;
7004 struct got_object_id *commit_id = NULL;
7006 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7007 if (err)
7008 return err;
7010 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7011 if (err)
7012 goto done;
7013 err = got_ref_resolve(&commit_id, repo, commit_ref);
7014 if (err)
7015 goto done;
7016 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
7017 err = got_error(GOT_ERR_REBASE_COMMITID);
7018 goto done;
7021 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7022 worktree, fileindex, tmp_branch, committer, orig_commit,
7023 NULL, allow_conflict, repo);
7024 done:
7025 if (commit_ref)
7026 got_ref_close(commit_ref);
7027 free(commit_ref_name);
7028 free(commit_id);
7029 return err;
7032 const struct got_error *
7033 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
7034 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7035 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7036 const char *committer, struct got_commit_object *orig_commit,
7037 struct got_object_id *orig_commit_id, const char *new_logmsg,
7038 int allow_conflict, struct got_repository *repo)
7040 const struct got_error *err;
7041 char *commit_ref_name;
7042 struct got_reference *commit_ref = NULL;
7044 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7045 if (err)
7046 return err;
7048 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7049 if (err)
7050 goto done;
7052 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7053 worktree, fileindex, tmp_branch, committer, orig_commit,
7054 new_logmsg, allow_conflict, repo);
7055 done:
7056 if (commit_ref)
7057 got_ref_close(commit_ref);
7058 free(commit_ref_name);
7059 return err;
7062 const struct got_error *
7063 got_worktree_rebase_postpone(struct got_worktree *worktree,
7064 struct got_fileindex *fileindex)
7066 if (fileindex)
7067 got_fileindex_free(fileindex);
7068 return lock_worktree(worktree, LOCK_SH);
7071 static const struct got_error *
7072 delete_ref(const char *name, struct got_repository *repo)
7074 const struct got_error *err;
7075 struct got_reference *ref;
7077 err = got_ref_open(&ref, repo, name, 0);
7078 if (err) {
7079 if (err->code == GOT_ERR_NOT_REF)
7080 return NULL;
7081 return err;
7084 err = got_ref_delete(ref, repo);
7085 got_ref_close(ref);
7086 return err;
7089 static const struct got_error *
7090 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7092 const struct got_error *err;
7093 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7094 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7096 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7097 if (err)
7098 goto done;
7099 err = delete_ref(tmp_branch_name, repo);
7100 if (err)
7101 goto done;
7103 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7104 if (err)
7105 goto done;
7106 err = delete_ref(new_base_branch_ref_name, repo);
7107 if (err)
7108 goto done;
7110 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7111 if (err)
7112 goto done;
7113 err = delete_ref(branch_ref_name, repo);
7114 if (err)
7115 goto done;
7117 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7118 if (err)
7119 goto done;
7120 err = delete_ref(commit_ref_name, repo);
7121 if (err)
7122 goto done;
7124 done:
7125 free(tmp_branch_name);
7126 free(new_base_branch_ref_name);
7127 free(branch_ref_name);
7128 free(commit_ref_name);
7129 return err;
7132 static const struct got_error *
7133 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7134 struct got_object_id *new_commit_id, struct got_repository *repo)
7136 const struct got_error *err;
7137 struct got_reference *ref = NULL;
7138 struct got_object_id *old_commit_id = NULL;
7139 const char *branch_name = NULL;
7140 char *new_id_str = NULL;
7141 char *refname = NULL;
7143 branch_name = got_ref_get_name(branch);
7144 if (strncmp(branch_name, "refs/heads/", 11) != 0)
7145 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7146 branch_name += 11;
7148 err = got_object_id_str(&new_id_str, new_commit_id);
7149 if (err)
7150 return err;
7152 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7153 new_id_str) == -1) {
7154 err = got_error_from_errno("asprintf");
7155 goto done;
7158 err = got_ref_resolve(&old_commit_id, repo, branch);
7159 if (err)
7160 goto done;
7162 err = got_ref_alloc(&ref, refname, old_commit_id);
7163 if (err)
7164 goto done;
7166 err = got_ref_write(ref, repo);
7167 done:
7168 free(new_id_str);
7169 free(refname);
7170 free(old_commit_id);
7171 if (ref)
7172 got_ref_close(ref);
7173 return err;
7176 const struct got_error *
7177 got_worktree_rebase_complete(struct got_worktree *worktree,
7178 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7179 struct got_reference *rebased_branch, struct got_repository *repo,
7180 int create_backup)
7182 const struct got_error *err, *unlockerr, *sync_err;
7183 struct got_object_id *new_head_commit_id = NULL;
7184 char *fileindex_path = NULL;
7186 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7187 if (err)
7188 return err;
7190 if (create_backup) {
7191 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7192 rebased_branch, new_head_commit_id, repo);
7193 if (err)
7194 goto done;
7197 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7198 if (err)
7199 goto done;
7201 err = got_ref_write(rebased_branch, repo);
7202 if (err)
7203 goto done;
7205 err = got_worktree_set_head_ref(worktree, rebased_branch);
7206 if (err)
7207 goto done;
7209 err = delete_rebase_refs(worktree, repo);
7210 if (err)
7211 goto done;
7213 err = get_fileindex_path(&fileindex_path, worktree);
7214 if (err)
7215 goto done;
7216 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7217 sync_err = sync_fileindex(fileindex, fileindex_path);
7218 if (sync_err && err == NULL)
7219 err = sync_err;
7220 done:
7221 got_fileindex_free(fileindex);
7222 free(fileindex_path);
7223 free(new_head_commit_id);
7224 unlockerr = lock_worktree(worktree, LOCK_SH);
7225 if (unlockerr && err == NULL)
7226 err = unlockerr;
7227 return err;
7230 const struct got_error *
7231 got_worktree_rebase_abort(struct got_worktree *worktree,
7232 struct got_fileindex *fileindex, struct got_repository *repo,
7233 struct got_reference *new_base_branch,
7234 got_worktree_checkout_cb progress_cb, void *progress_arg)
7236 const struct got_error *err, *unlockerr, *sync_err;
7237 struct got_reference *resolved = NULL;
7238 struct got_object_id *commit_id = NULL;
7239 struct got_commit_object *commit = NULL;
7240 char *fileindex_path = NULL;
7241 struct revert_file_args rfa;
7242 struct got_object_id *tree_id = NULL;
7244 err = lock_worktree(worktree, LOCK_EX);
7245 if (err)
7246 return err;
7248 err = got_object_open_as_commit(&commit, repo,
7249 worktree->base_commit_id);
7250 if (err)
7251 goto done;
7253 err = got_ref_open(&resolved, repo,
7254 got_ref_get_symref_target(new_base_branch), 0);
7255 if (err)
7256 goto done;
7258 err = got_worktree_set_head_ref(worktree, resolved);
7259 if (err)
7260 goto done;
7263 * XXX commits to the base branch could have happened while
7264 * we were busy rebasing; should we store the original commit ID
7265 * when rebase begins and read it back here?
7267 err = got_ref_resolve(&commit_id, repo, resolved);
7268 if (err)
7269 goto done;
7271 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7272 if (err)
7273 goto done;
7275 err = got_object_id_by_path(&tree_id, repo, commit,
7276 worktree->path_prefix);
7277 if (err)
7278 goto done;
7280 err = delete_rebase_refs(worktree, repo);
7281 if (err)
7282 goto done;
7284 err = get_fileindex_path(&fileindex_path, worktree);
7285 if (err)
7286 goto done;
7288 rfa.worktree = worktree;
7289 rfa.fileindex = fileindex;
7290 rfa.progress_cb = progress_cb;
7291 rfa.progress_arg = progress_arg;
7292 rfa.patch_cb = NULL;
7293 rfa.patch_arg = NULL;
7294 rfa.repo = repo;
7295 rfa.unlink_added_files = 0;
7296 err = worktree_status(worktree, "", fileindex, repo,
7297 revert_file, &rfa, NULL, NULL, 1, 0);
7298 if (err)
7299 goto sync;
7301 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7302 repo, progress_cb, progress_arg, NULL, NULL);
7303 sync:
7304 sync_err = sync_fileindex(fileindex, fileindex_path);
7305 if (sync_err && err == NULL)
7306 err = sync_err;
7307 done:
7308 got_ref_close(resolved);
7309 free(tree_id);
7310 free(commit_id);
7311 if (commit)
7312 got_object_commit_close(commit);
7313 if (fileindex)
7314 got_fileindex_free(fileindex);
7315 free(fileindex_path);
7317 unlockerr = lock_worktree(worktree, LOCK_SH);
7318 if (unlockerr && err == NULL)
7319 err = unlockerr;
7320 return err;
7323 const struct got_error *
7324 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7325 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7326 struct got_fileindex **fileindex, struct got_worktree *worktree,
7327 struct got_repository *repo)
7329 const struct got_error *err = NULL;
7330 char *tmp_branch_name = NULL;
7331 char *branch_ref_name = NULL;
7332 char *base_commit_ref_name = NULL;
7333 char *fileindex_path = NULL;
7334 struct check_rebase_ok_arg ok_arg;
7335 struct got_reference *wt_branch = NULL;
7336 struct got_reference *base_commit_ref = NULL;
7338 *tmp_branch = NULL;
7339 *branch_ref = NULL;
7340 *base_commit_id = NULL;
7341 *fileindex = NULL;
7343 err = lock_worktree(worktree, LOCK_EX);
7344 if (err)
7345 return err;
7347 err = open_fileindex(fileindex, &fileindex_path, worktree);
7348 if (err)
7349 goto done;
7351 ok_arg.worktree = worktree;
7352 ok_arg.repo = repo;
7353 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7354 &ok_arg);
7355 if (err)
7356 goto done;
7358 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7359 if (err)
7360 goto done;
7362 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7363 if (err)
7364 goto done;
7366 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7367 worktree);
7368 if (err)
7369 goto done;
7371 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7372 0);
7373 if (err)
7374 goto done;
7376 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7377 if (err)
7378 goto done;
7380 err = got_ref_write(*branch_ref, repo);
7381 if (err)
7382 goto done;
7384 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7385 worktree->base_commit_id);
7386 if (err)
7387 goto done;
7388 err = got_ref_write(base_commit_ref, repo);
7389 if (err)
7390 goto done;
7391 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7392 if (*base_commit_id == NULL) {
7393 err = got_error_from_errno("got_object_id_dup");
7394 goto done;
7397 err = got_ref_alloc(tmp_branch, tmp_branch_name,
7398 worktree->base_commit_id);
7399 if (err)
7400 goto done;
7401 err = got_ref_write(*tmp_branch, repo);
7402 if (err)
7403 goto done;
7405 err = got_worktree_set_head_ref(worktree, *tmp_branch);
7406 if (err)
7407 goto done;
7408 done:
7409 free(fileindex_path);
7410 free(tmp_branch_name);
7411 free(branch_ref_name);
7412 free(base_commit_ref_name);
7413 if (wt_branch)
7414 got_ref_close(wt_branch);
7415 if (err) {
7416 if (*branch_ref) {
7417 got_ref_close(*branch_ref);
7418 *branch_ref = NULL;
7420 if (*tmp_branch) {
7421 got_ref_close(*tmp_branch);
7422 *tmp_branch = NULL;
7424 free(*base_commit_id);
7425 if (*fileindex) {
7426 got_fileindex_free(*fileindex);
7427 *fileindex = NULL;
7429 lock_worktree(worktree, LOCK_SH);
7431 return err;
7434 const struct got_error *
7435 got_worktree_histedit_postpone(struct got_worktree *worktree,
7436 struct got_fileindex *fileindex)
7438 if (fileindex)
7439 got_fileindex_free(fileindex);
7440 return lock_worktree(worktree, LOCK_SH);
7443 const struct got_error *
7444 got_worktree_histedit_in_progress(int *in_progress,
7445 struct got_worktree *worktree)
7447 const struct got_error *err;
7448 char *tmp_branch_name = NULL;
7450 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7451 if (err)
7452 return err;
7454 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7455 free(tmp_branch_name);
7456 return NULL;
7459 const struct got_error *
7460 got_worktree_histedit_continue(struct got_object_id **commit_id,
7461 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7462 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7463 struct got_worktree *worktree, struct got_repository *repo)
7465 const struct got_error *err;
7466 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7467 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7468 struct got_reference *commit_ref = NULL;
7469 struct got_reference *base_commit_ref = NULL;
7470 char *fileindex_path = NULL;
7471 int have_staged_files = 0;
7473 *commit_id = NULL;
7474 *tmp_branch = NULL;
7475 *base_commit_id = NULL;
7476 *fileindex = NULL;
7478 err = lock_worktree(worktree, LOCK_EX);
7479 if (err)
7480 return err;
7482 err = open_fileindex(fileindex, &fileindex_path, worktree);
7483 if (err)
7484 goto done;
7486 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7487 &have_staged_files);
7488 if (err && err->code != GOT_ERR_CANCELLED)
7489 goto done;
7490 if (have_staged_files) {
7491 err = got_error(GOT_ERR_STAGED_PATHS);
7492 goto done;
7495 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7496 if (err)
7497 goto done;
7499 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7500 if (err)
7501 goto done;
7503 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7504 if (err)
7505 goto done;
7507 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7508 worktree);
7509 if (err)
7510 goto done;
7512 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7513 if (err)
7514 goto done;
7516 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7517 if (err)
7518 goto done;
7519 err = got_ref_resolve(commit_id, repo, commit_ref);
7520 if (err)
7521 goto done;
7523 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7524 if (err)
7525 goto done;
7526 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7527 if (err)
7528 goto done;
7530 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7531 if (err)
7532 goto done;
7533 done:
7534 free(commit_ref_name);
7535 free(branch_ref_name);
7536 free(fileindex_path);
7537 if (commit_ref)
7538 got_ref_close(commit_ref);
7539 if (base_commit_ref)
7540 got_ref_close(base_commit_ref);
7541 if (err) {
7542 free(*commit_id);
7543 *commit_id = NULL;
7544 free(*base_commit_id);
7545 *base_commit_id = NULL;
7546 if (*tmp_branch) {
7547 got_ref_close(*tmp_branch);
7548 *tmp_branch = NULL;
7550 if (*fileindex) {
7551 got_fileindex_free(*fileindex);
7552 *fileindex = NULL;
7554 lock_worktree(worktree, LOCK_EX);
7556 return err;
7559 static const struct got_error *
7560 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7562 const struct got_error *err;
7563 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7564 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7566 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7567 if (err)
7568 goto done;
7569 err = delete_ref(tmp_branch_name, repo);
7570 if (err)
7571 goto done;
7573 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7574 worktree);
7575 if (err)
7576 goto done;
7577 err = delete_ref(base_commit_ref_name, repo);
7578 if (err)
7579 goto done;
7581 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7582 if (err)
7583 goto done;
7584 err = delete_ref(branch_ref_name, repo);
7585 if (err)
7586 goto done;
7588 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7589 if (err)
7590 goto done;
7591 err = delete_ref(commit_ref_name, repo);
7592 if (err)
7593 goto done;
7594 done:
7595 free(tmp_branch_name);
7596 free(base_commit_ref_name);
7597 free(branch_ref_name);
7598 free(commit_ref_name);
7599 return err;
7602 const struct got_error *
7603 got_worktree_histedit_abort(struct got_worktree *worktree,
7604 struct got_fileindex *fileindex, struct got_repository *repo,
7605 struct got_reference *branch, struct got_object_id *base_commit_id,
7606 got_worktree_checkout_cb progress_cb, void *progress_arg)
7608 const struct got_error *err, *unlockerr, *sync_err;
7609 struct got_reference *resolved = NULL;
7610 char *fileindex_path = NULL;
7611 struct got_commit_object *commit = NULL;
7612 struct got_object_id *tree_id = NULL;
7613 struct revert_file_args rfa;
7615 err = lock_worktree(worktree, LOCK_EX);
7616 if (err)
7617 return err;
7619 err = got_object_open_as_commit(&commit, repo,
7620 worktree->base_commit_id);
7621 if (err)
7622 goto done;
7624 err = got_ref_open(&resolved, repo,
7625 got_ref_get_symref_target(branch), 0);
7626 if (err)
7627 goto done;
7629 err = got_worktree_set_head_ref(worktree, resolved);
7630 if (err)
7631 goto done;
7633 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7634 if (err)
7635 goto done;
7637 err = got_object_id_by_path(&tree_id, repo, commit,
7638 worktree->path_prefix);
7639 if (err)
7640 goto done;
7642 err = delete_histedit_refs(worktree, repo);
7643 if (err)
7644 goto done;
7646 err = get_fileindex_path(&fileindex_path, worktree);
7647 if (err)
7648 goto done;
7650 rfa.worktree = worktree;
7651 rfa.fileindex = fileindex;
7652 rfa.progress_cb = progress_cb;
7653 rfa.progress_arg = progress_arg;
7654 rfa.patch_cb = NULL;
7655 rfa.patch_arg = NULL;
7656 rfa.repo = repo;
7657 rfa.unlink_added_files = 0;
7658 err = worktree_status(worktree, "", fileindex, repo,
7659 revert_file, &rfa, NULL, NULL, 1, 0);
7660 if (err)
7661 goto sync;
7663 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7664 repo, progress_cb, progress_arg, NULL, NULL);
7665 sync:
7666 sync_err = sync_fileindex(fileindex, fileindex_path);
7667 if (sync_err && err == NULL)
7668 err = sync_err;
7669 done:
7670 got_ref_close(resolved);
7671 free(tree_id);
7672 free(fileindex_path);
7674 unlockerr = lock_worktree(worktree, LOCK_SH);
7675 if (unlockerr && err == NULL)
7676 err = unlockerr;
7677 return err;
7680 const struct got_error *
7681 got_worktree_histedit_complete(struct got_worktree *worktree,
7682 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7683 struct got_reference *edited_branch, struct got_repository *repo)
7685 const struct got_error *err, *unlockerr, *sync_err;
7686 struct got_object_id *new_head_commit_id = NULL;
7687 struct got_reference *resolved = NULL;
7688 char *fileindex_path = NULL;
7690 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7691 if (err)
7692 return err;
7694 err = got_ref_open(&resolved, repo,
7695 got_ref_get_symref_target(edited_branch), 0);
7696 if (err)
7697 goto done;
7699 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7700 resolved, new_head_commit_id, repo);
7701 if (err)
7702 goto done;
7704 err = got_ref_change_ref(resolved, new_head_commit_id);
7705 if (err)
7706 goto done;
7708 err = got_ref_write(resolved, repo);
7709 if (err)
7710 goto done;
7712 err = got_worktree_set_head_ref(worktree, resolved);
7713 if (err)
7714 goto done;
7716 err = delete_histedit_refs(worktree, repo);
7717 if (err)
7718 goto done;
7720 err = get_fileindex_path(&fileindex_path, worktree);
7721 if (err)
7722 goto done;
7723 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7724 sync_err = sync_fileindex(fileindex, fileindex_path);
7725 if (sync_err && err == NULL)
7726 err = sync_err;
7727 done:
7728 got_fileindex_free(fileindex);
7729 free(fileindex_path);
7730 free(new_head_commit_id);
7731 unlockerr = lock_worktree(worktree, LOCK_SH);
7732 if (unlockerr && err == NULL)
7733 err = unlockerr;
7734 return err;
7737 const struct got_error *
7738 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7739 struct got_object_id *commit_id, struct got_repository *repo)
7741 const struct got_error *err;
7742 char *commit_ref_name;
7744 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7745 if (err)
7746 return err;
7748 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7749 if (err)
7750 goto done;
7752 err = delete_ref(commit_ref_name, repo);
7753 done:
7754 free(commit_ref_name);
7755 return err;
7758 const struct got_error *
7759 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7760 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7761 struct got_worktree *worktree, const char *refname,
7762 struct got_repository *repo)
7764 const struct got_error *err = NULL;
7765 char *fileindex_path = NULL;
7766 struct check_rebase_ok_arg ok_arg;
7768 *fileindex = NULL;
7769 *branch_ref = NULL;
7770 *base_branch_ref = NULL;
7772 err = lock_worktree(worktree, LOCK_EX);
7773 if (err)
7774 return err;
7776 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7777 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7778 "cannot integrate a branch into itself; "
7779 "update -b or different branch name required");
7780 goto done;
7783 err = open_fileindex(fileindex, &fileindex_path, worktree);
7784 if (err)
7785 goto done;
7787 /* Preconditions are the same as for rebase. */
7788 ok_arg.worktree = worktree;
7789 ok_arg.repo = repo;
7790 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7791 &ok_arg);
7792 if (err)
7793 goto done;
7795 err = got_ref_open(branch_ref, repo, refname, 1);
7796 if (err)
7797 goto done;
7799 err = got_ref_open(base_branch_ref, repo,
7800 got_worktree_get_head_ref_name(worktree), 1);
7801 done:
7802 if (err) {
7803 if (*branch_ref) {
7804 got_ref_close(*branch_ref);
7805 *branch_ref = NULL;
7807 if (*base_branch_ref) {
7808 got_ref_close(*base_branch_ref);
7809 *base_branch_ref = NULL;
7811 if (*fileindex) {
7812 got_fileindex_free(*fileindex);
7813 *fileindex = NULL;
7815 lock_worktree(worktree, LOCK_SH);
7817 return err;
7820 const struct got_error *
7821 got_worktree_integrate_continue(struct got_worktree *worktree,
7822 struct got_fileindex *fileindex, struct got_repository *repo,
7823 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7824 got_worktree_checkout_cb progress_cb, void *progress_arg,
7825 got_cancel_cb cancel_cb, void *cancel_arg)
7827 const struct got_error *err = NULL, *sync_err, *unlockerr;
7828 char *fileindex_path = NULL;
7829 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7830 struct got_commit_object *commit = NULL;
7832 err = get_fileindex_path(&fileindex_path, worktree);
7833 if (err)
7834 goto done;
7836 err = got_ref_resolve(&commit_id, repo, branch_ref);
7837 if (err)
7838 goto done;
7840 err = got_object_open_as_commit(&commit, repo, commit_id);
7841 if (err)
7842 goto done;
7844 err = got_object_id_by_path(&tree_id, repo, commit,
7845 worktree->path_prefix);
7846 if (err)
7847 goto done;
7849 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7850 if (err)
7851 goto done;
7853 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7854 progress_cb, progress_arg, cancel_cb, cancel_arg);
7855 if (err)
7856 goto sync;
7858 err = got_ref_change_ref(base_branch_ref, commit_id);
7859 if (err)
7860 goto sync;
7862 err = got_ref_write(base_branch_ref, repo);
7863 if (err)
7864 goto sync;
7866 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7867 sync:
7868 sync_err = sync_fileindex(fileindex, fileindex_path);
7869 if (sync_err && err == NULL)
7870 err = sync_err;
7872 done:
7873 unlockerr = got_ref_unlock(branch_ref);
7874 if (unlockerr && err == NULL)
7875 err = unlockerr;
7876 got_ref_close(branch_ref);
7878 unlockerr = got_ref_unlock(base_branch_ref);
7879 if (unlockerr && err == NULL)
7880 err = unlockerr;
7881 got_ref_close(base_branch_ref);
7883 got_fileindex_free(fileindex);
7884 free(fileindex_path);
7885 free(tree_id);
7886 if (commit)
7887 got_object_commit_close(commit);
7889 unlockerr = lock_worktree(worktree, LOCK_SH);
7890 if (unlockerr && err == NULL)
7891 err = unlockerr;
7892 return err;
7895 const struct got_error *
7896 got_worktree_integrate_abort(struct got_worktree *worktree,
7897 struct got_fileindex *fileindex, struct got_repository *repo,
7898 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7900 const struct got_error *err = NULL, *unlockerr = NULL;
7902 got_fileindex_free(fileindex);
7904 err = lock_worktree(worktree, LOCK_SH);
7906 unlockerr = got_ref_unlock(branch_ref);
7907 if (unlockerr && err == NULL)
7908 err = unlockerr;
7909 got_ref_close(branch_ref);
7911 unlockerr = got_ref_unlock(base_branch_ref);
7912 if (unlockerr && err == NULL)
7913 err = unlockerr;
7914 got_ref_close(base_branch_ref);
7916 return err;
7919 const struct got_error *
7920 got_worktree_merge_postpone(struct got_worktree *worktree,
7921 struct got_fileindex *fileindex)
7923 const struct got_error *err, *sync_err;
7924 char *fileindex_path = NULL;
7926 err = get_fileindex_path(&fileindex_path, worktree);
7927 if (err)
7928 goto done;
7930 sync_err = sync_fileindex(fileindex, fileindex_path);
7932 err = lock_worktree(worktree, LOCK_SH);
7933 if (sync_err && err == NULL)
7934 err = sync_err;
7935 done:
7936 got_fileindex_free(fileindex);
7937 free(fileindex_path);
7938 return err;
7941 static const struct got_error *
7942 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7944 const struct got_error *err;
7945 char *branch_refname = NULL, *commit_refname = NULL;
7947 err = get_merge_branch_ref_name(&branch_refname, worktree);
7948 if (err)
7949 goto done;
7950 err = delete_ref(branch_refname, repo);
7951 if (err)
7952 goto done;
7954 err = get_merge_commit_ref_name(&commit_refname, worktree);
7955 if (err)
7956 goto done;
7957 err = delete_ref(commit_refname, repo);
7958 if (err)
7959 goto done;
7961 done:
7962 free(branch_refname);
7963 free(commit_refname);
7964 return err;
7967 struct merge_commit_msg_arg {
7968 struct got_worktree *worktree;
7969 const char *branch_name;
7972 static const struct got_error *
7973 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
7974 const char *diff_path, char **logmsg, void *arg)
7976 struct merge_commit_msg_arg *a = arg;
7978 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7979 got_worktree_get_head_ref_name(a->worktree)) == -1)
7980 return got_error_from_errno("asprintf");
7982 return NULL;
7986 const struct got_error *
7987 got_worktree_merge_branch(struct got_worktree *worktree,
7988 struct got_fileindex *fileindex,
7989 struct got_object_id *yca_commit_id,
7990 struct got_object_id *branch_tip,
7991 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7992 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7994 const struct got_error *err;
7995 char *fileindex_path = NULL;
7997 err = get_fileindex_path(&fileindex_path, worktree);
7998 if (err)
7999 goto done;
8001 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
8002 worktree);
8003 if (err)
8004 goto done;
8006 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
8007 branch_tip, repo, progress_cb, progress_arg,
8008 cancel_cb, cancel_arg);
8009 done:
8010 free(fileindex_path);
8011 return err;
8014 const struct got_error *
8015 got_worktree_merge_commit(struct got_object_id **new_commit_id,
8016 struct got_worktree *worktree, struct got_fileindex *fileindex,
8017 const char *author, const char *committer, int allow_bad_symlinks,
8018 struct got_object_id *branch_tip, const char *branch_name,
8019 int allow_conflict, struct got_repository *repo,
8020 got_worktree_status_cb status_cb, void *status_arg)
8023 const struct got_error *err = NULL, *sync_err;
8024 struct got_pathlist_head commitable_paths;
8025 struct collect_commitables_arg cc_arg;
8026 struct got_pathlist_entry *pe;
8027 struct got_reference *head_ref = NULL;
8028 struct got_object_id *head_commit_id = NULL;
8029 int have_staged_files = 0;
8030 struct merge_commit_msg_arg mcm_arg;
8031 char *fileindex_path = NULL;
8033 memset(&cc_arg, 0, sizeof(cc_arg));
8034 *new_commit_id = NULL;
8036 TAILQ_INIT(&commitable_paths);
8038 err = get_fileindex_path(&fileindex_path, worktree);
8039 if (err)
8040 goto done;
8042 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
8043 if (err)
8044 goto done;
8046 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8047 if (err)
8048 goto done;
8050 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
8051 &have_staged_files);
8052 if (err && err->code != GOT_ERR_CANCELLED)
8053 goto done;
8054 if (have_staged_files) {
8055 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
8056 goto done;
8059 cc_arg.commitable_paths = &commitable_paths;
8060 cc_arg.worktree = worktree;
8061 cc_arg.fileindex = fileindex;
8062 cc_arg.repo = repo;
8063 cc_arg.have_staged_files = have_staged_files;
8064 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
8065 cc_arg.commit_conflicts = allow_conflict;
8066 err = worktree_status(worktree, "", fileindex, repo,
8067 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8068 if (err)
8069 goto done;
8071 if (TAILQ_EMPTY(&commitable_paths)) {
8072 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
8073 "merge of %s cannot proceed", branch_name);
8074 goto done;
8077 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8078 struct got_commitable *ct = pe->data;
8079 const char *ct_path = ct->in_repo_path;
8081 while (ct_path[0] == '/')
8082 ct_path++;
8083 err = check_out_of_date(ct_path, ct->status,
8084 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
8085 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
8086 if (err)
8087 goto done;
8091 mcm_arg.worktree = worktree;
8092 mcm_arg.branch_name = branch_name;
8093 err = commit_worktree(new_commit_id, &commitable_paths,
8094 head_commit_id, branch_tip, worktree, author, committer, NULL,
8095 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8096 if (err)
8097 goto done;
8099 err = update_fileindex_after_commit(worktree, &commitable_paths,
8100 *new_commit_id, fileindex, have_staged_files);
8101 sync_err = sync_fileindex(fileindex, fileindex_path);
8102 if (sync_err && err == NULL)
8103 err = sync_err;
8104 done:
8105 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8106 struct got_commitable *ct = pe->data;
8108 free_commitable(ct);
8110 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8111 free(fileindex_path);
8112 return err;
8115 const struct got_error *
8116 got_worktree_merge_complete(struct got_worktree *worktree,
8117 struct got_fileindex *fileindex, struct got_repository *repo)
8119 const struct got_error *err, *unlockerr, *sync_err;
8120 char *fileindex_path = NULL;
8122 err = delete_merge_refs(worktree, repo);
8123 if (err)
8124 goto done;
8126 err = get_fileindex_path(&fileindex_path, worktree);
8127 if (err)
8128 goto done;
8129 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8130 sync_err = sync_fileindex(fileindex, fileindex_path);
8131 if (sync_err && err == NULL)
8132 err = sync_err;
8133 done:
8134 got_fileindex_free(fileindex);
8135 free(fileindex_path);
8136 unlockerr = lock_worktree(worktree, LOCK_SH);
8137 if (unlockerr && err == NULL)
8138 err = unlockerr;
8139 return err;
8142 const struct got_error *
8143 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8144 struct got_repository *repo)
8146 const struct got_error *err;
8147 char *branch_refname = NULL;
8148 struct got_reference *branch_ref = NULL;
8150 *in_progress = 0;
8152 err = get_merge_branch_ref_name(&branch_refname, worktree);
8153 if (err)
8154 return err;
8155 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8156 free(branch_refname);
8157 if (err) {
8158 if (err->code != GOT_ERR_NOT_REF)
8159 return err;
8160 } else
8161 *in_progress = 1;
8163 return NULL;
8166 const struct got_error *got_worktree_merge_prepare(
8167 struct got_fileindex **fileindex, struct got_worktree *worktree,
8168 struct got_reference *branch, struct got_repository *repo)
8170 const struct got_error *err = NULL;
8171 char *fileindex_path = NULL;
8172 char *branch_refname = NULL, *commit_refname = NULL;
8173 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
8174 struct got_reference *commit_ref = NULL;
8175 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
8176 struct check_rebase_ok_arg ok_arg;
8178 *fileindex = NULL;
8180 err = lock_worktree(worktree, LOCK_EX);
8181 if (err)
8182 return err;
8184 err = open_fileindex(fileindex, &fileindex_path, worktree);
8185 if (err)
8186 goto done;
8188 /* Preconditions are the same as for rebase. */
8189 ok_arg.worktree = worktree;
8190 ok_arg.repo = repo;
8191 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8192 &ok_arg);
8193 if (err)
8194 goto done;
8196 err = get_merge_branch_ref_name(&branch_refname, worktree);
8197 if (err)
8198 return err;
8200 err = get_merge_commit_ref_name(&commit_refname, worktree);
8201 if (err)
8202 return err;
8204 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8205 0);
8206 if (err)
8207 goto done;
8209 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8210 if (err)
8211 goto done;
8213 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8214 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8215 goto done;
8218 err = got_ref_resolve(&branch_tip, repo, branch);
8219 if (err)
8220 goto done;
8222 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8223 if (err)
8224 goto done;
8225 err = got_ref_write(branch_ref, repo);
8226 if (err)
8227 goto done;
8229 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8230 if (err)
8231 goto done;
8232 err = got_ref_write(commit_ref, repo);
8233 if (err)
8234 goto done;
8236 done:
8237 free(branch_refname);
8238 free(commit_refname);
8239 free(fileindex_path);
8240 if (branch_ref)
8241 got_ref_close(branch_ref);
8242 if (commit_ref)
8243 got_ref_close(commit_ref);
8244 if (wt_branch)
8245 got_ref_close(wt_branch);
8246 free(wt_branch_tip);
8247 if (err) {
8248 if (*fileindex) {
8249 got_fileindex_free(*fileindex);
8250 *fileindex = NULL;
8252 lock_worktree(worktree, LOCK_SH);
8254 return err;
8257 const struct got_error *
8258 got_worktree_merge_continue(char **branch_name,
8259 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8260 struct got_worktree *worktree, struct got_repository *repo)
8262 const struct got_error *err;
8263 char *commit_refname = NULL, *branch_refname = NULL;
8264 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8265 char *fileindex_path = NULL;
8266 int have_staged_files = 0;
8268 *branch_name = NULL;
8269 *branch_tip = NULL;
8270 *fileindex = NULL;
8272 err = lock_worktree(worktree, LOCK_EX);
8273 if (err)
8274 return err;
8276 err = open_fileindex(fileindex, &fileindex_path, worktree);
8277 if (err)
8278 goto done;
8280 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8281 &have_staged_files);
8282 if (err && err->code != GOT_ERR_CANCELLED)
8283 goto done;
8284 if (have_staged_files) {
8285 err = got_error(GOT_ERR_STAGED_PATHS);
8286 goto done;
8289 err = get_merge_branch_ref_name(&branch_refname, worktree);
8290 if (err)
8291 goto done;
8293 err = get_merge_commit_ref_name(&commit_refname, worktree);
8294 if (err)
8295 goto done;
8297 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8298 if (err)
8299 goto done;
8301 if (!got_ref_is_symbolic(branch_ref)) {
8302 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8303 "%s is not a symbolic reference",
8304 got_ref_get_name(branch_ref));
8305 goto done;
8307 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8308 if (*branch_name == NULL) {
8309 err = got_error_from_errno("strdup");
8310 goto done;
8313 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8314 if (err)
8315 goto done;
8317 err = got_ref_resolve(branch_tip, repo, commit_ref);
8318 if (err)
8319 goto done;
8320 done:
8321 free(commit_refname);
8322 free(branch_refname);
8323 free(fileindex_path);
8324 if (commit_ref)
8325 got_ref_close(commit_ref);
8326 if (branch_ref)
8327 got_ref_close(branch_ref);
8328 if (err) {
8329 if (*branch_name) {
8330 free(*branch_name);
8331 *branch_name = NULL;
8333 free(*branch_tip);
8334 *branch_tip = NULL;
8335 if (*fileindex) {
8336 got_fileindex_free(*fileindex);
8337 *fileindex = NULL;
8339 lock_worktree(worktree, LOCK_SH);
8341 return err;
8344 const struct got_error *
8345 got_worktree_merge_abort(struct got_worktree *worktree,
8346 struct got_fileindex *fileindex, struct got_repository *repo,
8347 got_worktree_checkout_cb progress_cb, void *progress_arg)
8349 const struct got_error *err, *unlockerr, *sync_err;
8350 struct got_object_id *commit_id = NULL;
8351 struct got_commit_object *commit = NULL;
8352 char *fileindex_path = NULL;
8353 struct revert_file_args rfa;
8354 struct got_object_id *tree_id = NULL;
8356 err = got_object_open_as_commit(&commit, repo,
8357 worktree->base_commit_id);
8358 if (err)
8359 goto done;
8361 err = got_object_id_by_path(&tree_id, repo, commit,
8362 worktree->path_prefix);
8363 if (err)
8364 goto done;
8366 err = delete_merge_refs(worktree, repo);
8367 if (err)
8368 goto done;
8370 err = get_fileindex_path(&fileindex_path, worktree);
8371 if (err)
8372 goto done;
8374 rfa.worktree = worktree;
8375 rfa.fileindex = fileindex;
8376 rfa.progress_cb = progress_cb;
8377 rfa.progress_arg = progress_arg;
8378 rfa.patch_cb = NULL;
8379 rfa.patch_arg = NULL;
8380 rfa.repo = repo;
8381 rfa.unlink_added_files = 1;
8382 err = worktree_status(worktree, "", fileindex, repo,
8383 revert_file, &rfa, NULL, NULL, 1, 0);
8384 if (err)
8385 goto sync;
8387 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8388 repo, progress_cb, progress_arg, NULL, NULL);
8389 sync:
8390 sync_err = sync_fileindex(fileindex, fileindex_path);
8391 if (sync_err && err == NULL)
8392 err = sync_err;
8393 done:
8394 free(tree_id);
8395 free(commit_id);
8396 if (commit)
8397 got_object_commit_close(commit);
8398 if (fileindex)
8399 got_fileindex_free(fileindex);
8400 free(fileindex_path);
8402 unlockerr = lock_worktree(worktree, LOCK_SH);
8403 if (unlockerr && err == NULL)
8404 err = unlockerr;
8405 return err;
8408 struct check_stage_ok_arg {
8409 struct got_object_id *head_commit_id;
8410 struct got_worktree *worktree;
8411 struct got_fileindex *fileindex;
8412 struct got_repository *repo;
8413 int have_changes;
8416 static const struct got_error *
8417 check_stage_ok(void *arg, unsigned char status,
8418 unsigned char staged_status, const char *relpath,
8419 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8420 struct got_object_id *commit_id, int dirfd, const char *de_name)
8422 struct check_stage_ok_arg *a = arg;
8423 const struct got_error *err = NULL;
8424 struct got_fileindex_entry *ie;
8425 struct got_object_id base_commit_id;
8426 struct got_object_id *base_commit_idp = NULL;
8427 char *in_repo_path = NULL, *p;
8429 if (status == GOT_STATUS_UNVERSIONED ||
8430 status == GOT_STATUS_NO_CHANGE)
8431 return NULL;
8432 if (status == GOT_STATUS_NONEXISTENT)
8433 return got_error_set_errno(ENOENT, relpath);
8435 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8436 if (ie == NULL)
8437 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8439 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8440 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8441 relpath) == -1)
8442 return got_error_from_errno("asprintf");
8444 if (got_fileindex_entry_has_commit(ie)) {
8445 base_commit_idp = got_fileindex_entry_get_commit_id(
8446 &base_commit_id, ie);
8449 if (status == GOT_STATUS_CONFLICT) {
8450 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8451 goto done;
8452 } else if (status != GOT_STATUS_ADD &&
8453 status != GOT_STATUS_MODIFY &&
8454 status != GOT_STATUS_DELETE) {
8455 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8456 goto done;
8459 a->have_changes = 1;
8461 p = in_repo_path;
8462 while (p[0] == '/')
8463 p++;
8464 err = check_out_of_date(p, status, staged_status,
8465 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8466 GOT_ERR_STAGE_OUT_OF_DATE);
8467 done:
8468 free(in_repo_path);
8469 return err;
8472 struct stage_path_arg {
8473 struct got_worktree *worktree;
8474 struct got_fileindex *fileindex;
8475 struct got_repository *repo;
8476 got_worktree_status_cb status_cb;
8477 void *status_arg;
8478 got_worktree_patch_cb patch_cb;
8479 void *patch_arg;
8480 int staged_something;
8481 int allow_bad_symlinks;
8484 static const struct got_error *
8485 stage_path(void *arg, unsigned char status,
8486 unsigned char staged_status, const char *relpath,
8487 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8488 struct got_object_id *commit_id, int dirfd, const char *de_name)
8490 struct stage_path_arg *a = arg;
8491 const struct got_error *err = NULL;
8492 struct got_fileindex_entry *ie;
8493 char *ondisk_path = NULL, *path_content = NULL;
8494 uint32_t stage;
8495 struct got_object_id *new_staged_blob_id = NULL;
8496 struct stat sb;
8498 if (status == GOT_STATUS_UNVERSIONED)
8499 return NULL;
8501 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8502 if (ie == NULL)
8503 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8505 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8506 relpath)== -1)
8507 return got_error_from_errno("asprintf");
8509 switch (status) {
8510 case GOT_STATUS_ADD:
8511 case GOT_STATUS_MODIFY:
8512 /* XXX could sb.st_mode be passed in by our caller? */
8513 if (lstat(ondisk_path, &sb) == -1) {
8514 err = got_error_from_errno2("lstat", ondisk_path);
8515 break;
8517 if (a->patch_cb) {
8518 if (status == GOT_STATUS_ADD) {
8519 int choice = GOT_PATCH_CHOICE_NONE;
8520 err = (*a->patch_cb)(&choice, a->patch_arg,
8521 status, ie->path, NULL, 1, 1);
8522 if (err)
8523 break;
8524 if (choice != GOT_PATCH_CHOICE_YES)
8525 break;
8526 } else {
8527 err = create_patched_content(&path_content, 0,
8528 staged_blob_id ? staged_blob_id : blob_id,
8529 ondisk_path, dirfd, de_name, ie->path,
8530 a->repo, a->patch_cb, a->patch_arg);
8531 if (err || path_content == NULL)
8532 break;
8535 err = got_object_blob_create(&new_staged_blob_id,
8536 path_content ? path_content : ondisk_path, a->repo);
8537 if (err)
8538 break;
8539 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8540 SHA1_DIGEST_LENGTH);
8541 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8542 stage = GOT_FILEIDX_STAGE_ADD;
8543 else
8544 stage = GOT_FILEIDX_STAGE_MODIFY;
8545 got_fileindex_entry_stage_set(ie, stage);
8546 if (S_ISLNK(sb.st_mode)) {
8547 int is_bad_symlink = 0;
8548 if (!a->allow_bad_symlinks) {
8549 char target_path[PATH_MAX];
8550 ssize_t target_len;
8551 target_len = readlink(ondisk_path, target_path,
8552 sizeof(target_path));
8553 if (target_len == -1) {
8554 err = got_error_from_errno2("readlink",
8555 ondisk_path);
8556 break;
8558 err = is_bad_symlink_target(&is_bad_symlink,
8559 target_path, target_len, ondisk_path,
8560 a->worktree->root_path);
8561 if (err)
8562 break;
8563 if (is_bad_symlink) {
8564 err = got_error_path(ondisk_path,
8565 GOT_ERR_BAD_SYMLINK);
8566 break;
8569 if (is_bad_symlink)
8570 got_fileindex_entry_staged_filetype_set(ie,
8571 GOT_FILEIDX_MODE_BAD_SYMLINK);
8572 else
8573 got_fileindex_entry_staged_filetype_set(ie,
8574 GOT_FILEIDX_MODE_SYMLINK);
8575 } else {
8576 got_fileindex_entry_staged_filetype_set(ie,
8577 GOT_FILEIDX_MODE_REGULAR_FILE);
8579 a->staged_something = 1;
8580 if (a->status_cb == NULL)
8581 break;
8582 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8583 get_staged_status(ie), relpath, blob_id,
8584 new_staged_blob_id, NULL, dirfd, de_name);
8585 if (err)
8586 break;
8588 * When staging the reverse of the staged diff,
8589 * implicitly unstage the file.
8591 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8592 sizeof(ie->blob_sha1)) == 0) {
8593 got_fileindex_entry_stage_set(ie,
8594 GOT_FILEIDX_STAGE_NONE);
8596 break;
8597 case GOT_STATUS_DELETE:
8598 if (staged_status == GOT_STATUS_DELETE)
8599 break;
8600 if (a->patch_cb) {
8601 int choice = GOT_PATCH_CHOICE_NONE;
8602 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8603 ie->path, NULL, 1, 1);
8604 if (err)
8605 break;
8606 if (choice == GOT_PATCH_CHOICE_NO)
8607 break;
8608 if (choice != GOT_PATCH_CHOICE_YES) {
8609 err = got_error(GOT_ERR_PATCH_CHOICE);
8610 break;
8613 stage = GOT_FILEIDX_STAGE_DELETE;
8614 got_fileindex_entry_stage_set(ie, stage);
8615 a->staged_something = 1;
8616 if (a->status_cb == NULL)
8617 break;
8618 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8619 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8620 de_name);
8621 break;
8622 case GOT_STATUS_NO_CHANGE:
8623 break;
8624 case GOT_STATUS_CONFLICT:
8625 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8626 break;
8627 case GOT_STATUS_NONEXISTENT:
8628 err = got_error_set_errno(ENOENT, relpath);
8629 break;
8630 default:
8631 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8632 break;
8635 if (path_content && unlink(path_content) == -1 && err == NULL)
8636 err = got_error_from_errno2("unlink", path_content);
8637 free(path_content);
8638 free(ondisk_path);
8639 free(new_staged_blob_id);
8640 return err;
8643 const struct got_error *
8644 got_worktree_stage(struct got_worktree *worktree,
8645 struct got_pathlist_head *paths,
8646 got_worktree_status_cb status_cb, void *status_arg,
8647 got_worktree_patch_cb patch_cb, void *patch_arg,
8648 int allow_bad_symlinks, struct got_repository *repo)
8650 const struct got_error *err = NULL, *sync_err, *unlockerr;
8651 struct got_pathlist_entry *pe;
8652 struct got_fileindex *fileindex = NULL;
8653 char *fileindex_path = NULL;
8654 struct got_reference *head_ref = NULL;
8655 struct got_object_id *head_commit_id = NULL;
8656 struct check_stage_ok_arg oka;
8657 struct stage_path_arg spa;
8659 err = lock_worktree(worktree, LOCK_EX);
8660 if (err)
8661 return err;
8663 err = got_ref_open(&head_ref, repo,
8664 got_worktree_get_head_ref_name(worktree), 0);
8665 if (err)
8666 goto done;
8667 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8668 if (err)
8669 goto done;
8670 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8671 if (err)
8672 goto done;
8674 /* Check pre-conditions before staging anything. */
8675 oka.head_commit_id = head_commit_id;
8676 oka.worktree = worktree;
8677 oka.fileindex = fileindex;
8678 oka.repo = repo;
8679 oka.have_changes = 0;
8680 TAILQ_FOREACH(pe, paths, entry) {
8681 err = worktree_status(worktree, pe->path, fileindex, repo,
8682 check_stage_ok, &oka, NULL, NULL, 1, 0);
8683 if (err)
8684 goto done;
8686 if (!oka.have_changes) {
8687 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8688 goto done;
8691 spa.worktree = worktree;
8692 spa.fileindex = fileindex;
8693 spa.repo = repo;
8694 spa.patch_cb = patch_cb;
8695 spa.patch_arg = patch_arg;
8696 spa.status_cb = status_cb;
8697 spa.status_arg = status_arg;
8698 spa.staged_something = 0;
8699 spa.allow_bad_symlinks = allow_bad_symlinks;
8700 TAILQ_FOREACH(pe, paths, entry) {
8701 err = worktree_status(worktree, pe->path, fileindex, repo,
8702 stage_path, &spa, NULL, NULL, 1, 0);
8703 if (err)
8704 goto done;
8706 if (!spa.staged_something) {
8707 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8708 goto done;
8711 sync_err = sync_fileindex(fileindex, fileindex_path);
8712 if (sync_err && err == NULL)
8713 err = sync_err;
8714 done:
8715 if (head_ref)
8716 got_ref_close(head_ref);
8717 free(head_commit_id);
8718 free(fileindex_path);
8719 if (fileindex)
8720 got_fileindex_free(fileindex);
8721 unlockerr = lock_worktree(worktree, LOCK_SH);
8722 if (unlockerr && err == NULL)
8723 err = unlockerr;
8724 return err;
8727 struct unstage_path_arg {
8728 struct got_worktree *worktree;
8729 struct got_fileindex *fileindex;
8730 struct got_repository *repo;
8731 got_worktree_checkout_cb progress_cb;
8732 void *progress_arg;
8733 got_worktree_patch_cb patch_cb;
8734 void *patch_arg;
8737 static const struct got_error *
8738 create_unstaged_content(char **path_unstaged_content,
8739 char **path_new_staged_content, struct got_object_id *blob_id,
8740 struct got_object_id *staged_blob_id, const char *relpath,
8741 struct got_repository *repo,
8742 got_worktree_patch_cb patch_cb, void *patch_arg)
8744 const struct got_error *err, *free_err;
8745 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8746 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8747 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8748 struct got_diffreg_result *diffreg_result = NULL;
8749 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8750 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8751 int fd1 = -1, fd2 = -1;
8753 *path_unstaged_content = NULL;
8754 *path_new_staged_content = NULL;
8756 err = got_object_id_str(&label1, blob_id);
8757 if (err)
8758 return err;
8760 fd1 = got_opentempfd();
8761 if (fd1 == -1) {
8762 err = got_error_from_errno("got_opentempfd");
8763 goto done;
8765 fd2 = got_opentempfd();
8766 if (fd2 == -1) {
8767 err = got_error_from_errno("got_opentempfd");
8768 goto done;
8771 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8772 if (err)
8773 goto done;
8775 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
8776 if (err)
8777 goto done;
8779 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8780 if (err)
8781 goto done;
8783 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8784 fd2);
8785 if (err)
8786 goto done;
8788 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
8789 if (err)
8790 goto done;
8792 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8793 if (err)
8794 goto done;
8796 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8797 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8798 if (err)
8799 goto done;
8801 err = got_opentemp_named(path_unstaged_content, &outfile,
8802 "got-unstaged-content", "");
8803 if (err)
8804 goto done;
8805 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8806 "got-new-staged-content", "");
8807 if (err)
8808 goto done;
8810 if (fseek(f1, 0L, SEEK_SET) == -1) {
8811 err = got_ferror(f1, GOT_ERR_IO);
8812 goto done;
8814 if (fseek(f2, 0L, SEEK_SET) == -1) {
8815 err = got_ferror(f2, GOT_ERR_IO);
8816 goto done;
8818 /* Count the number of actual changes in the diff result. */
8819 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8820 struct diff_chunk_context cc = {};
8821 diff_chunk_context_load_change(&cc, &nchunks_used,
8822 diffreg_result->result, n, 0);
8823 nchanges++;
8825 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8826 int choice;
8827 err = apply_or_reject_change(&choice, &nchunks_used,
8828 diffreg_result->result, n, relpath, f1, f2,
8829 &line_cur1, &line_cur2,
8830 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8831 if (err)
8832 goto done;
8833 if (choice == GOT_PATCH_CHOICE_YES)
8834 have_content = 1;
8835 else
8836 have_rejected_content = 1;
8837 if (choice == GOT_PATCH_CHOICE_QUIT)
8838 break;
8840 if (have_content || have_rejected_content)
8841 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8842 outfile, rejectfile);
8843 done:
8844 free(label1);
8845 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8846 err = got_error_from_errno("close");
8847 if (blob)
8848 got_object_blob_close(blob);
8849 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8850 err = got_error_from_errno("close");
8851 if (staged_blob)
8852 got_object_blob_close(staged_blob);
8853 free_err = got_diffreg_result_free(diffreg_result);
8854 if (free_err && err == NULL)
8855 err = free_err;
8856 if (f1 && fclose(f1) == EOF && err == NULL)
8857 err = got_error_from_errno2("fclose", path1);
8858 if (f2 && fclose(f2) == EOF && err == NULL)
8859 err = got_error_from_errno2("fclose", path2);
8860 if (outfile && fclose(outfile) == EOF && err == NULL)
8861 err = got_error_from_errno2("fclose", *path_unstaged_content);
8862 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8863 err = got_error_from_errno2("fclose", *path_new_staged_content);
8864 if (path1 && unlink(path1) == -1 && err == NULL)
8865 err = got_error_from_errno2("unlink", path1);
8866 if (path2 && unlink(path2) == -1 && err == NULL)
8867 err = got_error_from_errno2("unlink", path2);
8868 if (err || !have_content) {
8869 if (*path_unstaged_content &&
8870 unlink(*path_unstaged_content) == -1 && err == NULL)
8871 err = got_error_from_errno2("unlink",
8872 *path_unstaged_content);
8873 free(*path_unstaged_content);
8874 *path_unstaged_content = NULL;
8876 if (err || !have_content || !have_rejected_content) {
8877 if (*path_new_staged_content &&
8878 unlink(*path_new_staged_content) == -1 && err == NULL)
8879 err = got_error_from_errno2("unlink",
8880 *path_new_staged_content);
8881 free(*path_new_staged_content);
8882 *path_new_staged_content = NULL;
8884 free(path1);
8885 free(path2);
8886 return err;
8889 static const struct got_error *
8890 unstage_hunks(struct got_object_id *staged_blob_id,
8891 struct got_blob_object *blob_base,
8892 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8893 const char *ondisk_path, const char *label_orig,
8894 struct got_worktree *worktree, struct got_repository *repo,
8895 got_worktree_patch_cb patch_cb, void *patch_arg,
8896 got_worktree_checkout_cb progress_cb, void *progress_arg)
8898 const struct got_error *err = NULL;
8899 char *path_unstaged_content = NULL;
8900 char *path_new_staged_content = NULL;
8901 char *parent = NULL, *base_path = NULL;
8902 char *blob_base_path = NULL;
8903 struct got_object_id *new_staged_blob_id = NULL;
8904 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8905 struct stat sb;
8907 err = create_unstaged_content(&path_unstaged_content,
8908 &path_new_staged_content, blob_id, staged_blob_id,
8909 ie->path, repo, patch_cb, patch_arg);
8910 if (err)
8911 return err;
8913 if (path_unstaged_content == NULL)
8914 return NULL;
8916 if (path_new_staged_content) {
8917 err = got_object_blob_create(&new_staged_blob_id,
8918 path_new_staged_content, repo);
8919 if (err)
8920 goto done;
8923 f = fopen(path_unstaged_content, "re");
8924 if (f == NULL) {
8925 err = got_error_from_errno2("fopen",
8926 path_unstaged_content);
8927 goto done;
8929 if (fstat(fileno(f), &sb) == -1) {
8930 err = got_error_from_errno2("fstat", path_unstaged_content);
8931 goto done;
8933 if (got_fileindex_entry_staged_filetype_get(ie) ==
8934 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8935 char link_target[PATH_MAX];
8936 size_t r;
8937 r = fread(link_target, 1, sizeof(link_target), f);
8938 if (r == 0 && ferror(f)) {
8939 err = got_error_from_errno("fread");
8940 goto done;
8942 if (r >= sizeof(link_target)) { /* should not happen */
8943 err = got_error(GOT_ERR_NO_SPACE);
8944 goto done;
8946 link_target[r] = '\0';
8947 err = merge_symlink(worktree, blob_base,
8948 ondisk_path, ie->path, label_orig, link_target,
8949 worktree->base_commit_id, repo, progress_cb,
8950 progress_arg);
8951 } else {
8952 int local_changes_subsumed;
8954 err = got_path_dirname(&parent, ondisk_path);
8955 if (err)
8956 return err;
8958 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8959 parent) == -1) {
8960 err = got_error_from_errno("asprintf");
8961 base_path = NULL;
8962 goto done;
8965 err = got_opentemp_named(&blob_base_path, &f_base,
8966 base_path, "");
8967 if (err)
8968 goto done;
8969 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8970 blob_base);
8971 if (err)
8972 goto done;
8975 * In order the run a 3-way merge with a symlink we copy the symlink's
8976 * target path into a temporary file and use that file with diff3.
8978 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8979 err = dump_symlink_target_path_to_file(&f_deriv2,
8980 ondisk_path);
8981 if (err)
8982 goto done;
8983 } else {
8984 int fd;
8985 fd = open(ondisk_path,
8986 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8987 if (fd == -1) {
8988 err = got_error_from_errno2("open", ondisk_path);
8989 goto done;
8991 f_deriv2 = fdopen(fd, "r");
8992 if (f_deriv2 == NULL) {
8993 err = got_error_from_errno2("fdopen", ondisk_path);
8994 close(fd);
8995 goto done;
8999 err = merge_file(&local_changes_subsumed, worktree,
9000 f_base, f, f_deriv2, ondisk_path, ie->path,
9001 got_fileindex_perms_to_st(ie),
9002 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
9003 repo, progress_cb, progress_arg);
9005 if (err)
9006 goto done;
9008 if (new_staged_blob_id) {
9009 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9010 SHA1_DIGEST_LENGTH);
9011 } else {
9012 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9013 got_fileindex_entry_staged_filetype_set(ie, 0);
9015 done:
9016 free(new_staged_blob_id);
9017 if (path_unstaged_content &&
9018 unlink(path_unstaged_content) == -1 && err == NULL)
9019 err = got_error_from_errno2("unlink", path_unstaged_content);
9020 if (path_new_staged_content &&
9021 unlink(path_new_staged_content) == -1 && err == NULL)
9022 err = got_error_from_errno2("unlink", path_new_staged_content);
9023 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
9024 err = got_error_from_errno2("unlink", blob_base_path);
9025 if (f_base && fclose(f_base) == EOF && err == NULL)
9026 err = got_error_from_errno2("fclose", path_unstaged_content);
9027 if (f && fclose(f) == EOF && err == NULL)
9028 err = got_error_from_errno2("fclose", path_unstaged_content);
9029 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
9030 err = got_error_from_errno2("fclose", ondisk_path);
9031 free(path_unstaged_content);
9032 free(path_new_staged_content);
9033 free(blob_base_path);
9034 free(parent);
9035 free(base_path);
9036 return err;
9039 static const struct got_error *
9040 unstage_path(void *arg, unsigned char status,
9041 unsigned char staged_status, const char *relpath,
9042 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9043 struct got_object_id *commit_id, int dirfd, const char *de_name)
9045 const struct got_error *err = NULL;
9046 struct unstage_path_arg *a = arg;
9047 struct got_fileindex_entry *ie;
9048 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
9049 char *ondisk_path = NULL;
9050 char *id_str = NULL, *label_orig = NULL;
9051 int local_changes_subsumed;
9052 struct stat sb;
9053 int fd1 = -1, fd2 = -1;
9055 if (staged_status != GOT_STATUS_ADD &&
9056 staged_status != GOT_STATUS_MODIFY &&
9057 staged_status != GOT_STATUS_DELETE)
9058 return NULL;
9060 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9061 if (ie == NULL)
9062 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9064 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9065 == -1)
9066 return got_error_from_errno("asprintf");
9068 err = got_object_id_str(&id_str,
9069 commit_id ? commit_id : a->worktree->base_commit_id);
9070 if (err)
9071 goto done;
9072 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9073 id_str) == -1) {
9074 err = got_error_from_errno("asprintf");
9075 goto done;
9078 fd1 = got_opentempfd();
9079 if (fd1 == -1) {
9080 err = got_error_from_errno("got_opentempfd");
9081 goto done;
9083 fd2 = got_opentempfd();
9084 if (fd2 == -1) {
9085 err = got_error_from_errno("got_opentempfd");
9086 goto done;
9089 switch (staged_status) {
9090 case GOT_STATUS_MODIFY:
9091 err = got_object_open_as_blob(&blob_base, a->repo,
9092 blob_id, 8192, fd1);
9093 if (err)
9094 break;
9095 /* fall through */
9096 case GOT_STATUS_ADD:
9097 if (a->patch_cb) {
9098 if (staged_status == GOT_STATUS_ADD) {
9099 int choice = GOT_PATCH_CHOICE_NONE;
9100 err = (*a->patch_cb)(&choice, a->patch_arg,
9101 staged_status, ie->path, NULL, 1, 1);
9102 if (err)
9103 break;
9104 if (choice != GOT_PATCH_CHOICE_YES)
9105 break;
9106 } else {
9107 err = unstage_hunks(staged_blob_id,
9108 blob_base, blob_id, ie, ondisk_path,
9109 label_orig, a->worktree, a->repo,
9110 a->patch_cb, a->patch_arg,
9111 a->progress_cb, a->progress_arg);
9112 break; /* Done with this file. */
9115 err = got_object_open_as_blob(&blob_staged, a->repo,
9116 staged_blob_id, 8192, fd2);
9117 if (err)
9118 break;
9119 switch (got_fileindex_entry_staged_filetype_get(ie)) {
9120 case GOT_FILEIDX_MODE_BAD_SYMLINK:
9121 case GOT_FILEIDX_MODE_REGULAR_FILE:
9122 err = merge_blob(&local_changes_subsumed, a->worktree,
9123 blob_base, ondisk_path, relpath,
9124 got_fileindex_perms_to_st(ie), label_orig,
9125 blob_staged, commit_id ? commit_id :
9126 a->worktree->base_commit_id, a->repo,
9127 a->progress_cb, a->progress_arg);
9128 break;
9129 case GOT_FILEIDX_MODE_SYMLINK:
9130 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9131 char *staged_target;
9132 err = got_object_blob_read_to_str(
9133 &staged_target, blob_staged);
9134 if (err)
9135 goto done;
9136 err = merge_symlink(a->worktree, blob_base,
9137 ondisk_path, relpath, label_orig,
9138 staged_target, commit_id ? commit_id :
9139 a->worktree->base_commit_id,
9140 a->repo, a->progress_cb, a->progress_arg);
9141 free(staged_target);
9142 } else {
9143 err = merge_blob(&local_changes_subsumed,
9144 a->worktree, blob_base, ondisk_path,
9145 relpath, got_fileindex_perms_to_st(ie),
9146 label_orig, blob_staged,
9147 commit_id ? commit_id :
9148 a->worktree->base_commit_id, a->repo,
9149 a->progress_cb, a->progress_arg);
9151 break;
9152 default:
9153 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9154 break;
9156 if (err == NULL) {
9157 got_fileindex_entry_stage_set(ie,
9158 GOT_FILEIDX_STAGE_NONE);
9159 got_fileindex_entry_staged_filetype_set(ie, 0);
9161 break;
9162 case GOT_STATUS_DELETE:
9163 if (a->patch_cb) {
9164 int choice = GOT_PATCH_CHOICE_NONE;
9165 err = (*a->patch_cb)(&choice, a->patch_arg,
9166 staged_status, ie->path, NULL, 1, 1);
9167 if (err)
9168 break;
9169 if (choice == GOT_PATCH_CHOICE_NO)
9170 break;
9171 if (choice != GOT_PATCH_CHOICE_YES) {
9172 err = got_error(GOT_ERR_PATCH_CHOICE);
9173 break;
9176 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9177 got_fileindex_entry_staged_filetype_set(ie, 0);
9178 err = get_file_status(&status, &sb, ie, ondisk_path,
9179 dirfd, de_name, a->repo);
9180 if (err)
9181 break;
9182 err = (*a->progress_cb)(a->progress_arg, status, relpath);
9183 break;
9185 done:
9186 free(ondisk_path);
9187 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9188 err = got_error_from_errno("close");
9189 if (blob_base)
9190 got_object_blob_close(blob_base);
9191 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9192 err = got_error_from_errno("close");
9193 if (blob_staged)
9194 got_object_blob_close(blob_staged);
9195 free(id_str);
9196 free(label_orig);
9197 return err;
9200 const struct got_error *
9201 got_worktree_unstage(struct got_worktree *worktree,
9202 struct got_pathlist_head *paths,
9203 got_worktree_checkout_cb progress_cb, void *progress_arg,
9204 got_worktree_patch_cb patch_cb, void *patch_arg,
9205 struct got_repository *repo)
9207 const struct got_error *err = NULL, *sync_err, *unlockerr;
9208 struct got_pathlist_entry *pe;
9209 struct got_fileindex *fileindex = NULL;
9210 char *fileindex_path = NULL;
9211 struct unstage_path_arg upa;
9213 err = lock_worktree(worktree, LOCK_EX);
9214 if (err)
9215 return err;
9217 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9218 if (err)
9219 goto done;
9221 upa.worktree = worktree;
9222 upa.fileindex = fileindex;
9223 upa.repo = repo;
9224 upa.progress_cb = progress_cb;
9225 upa.progress_arg = progress_arg;
9226 upa.patch_cb = patch_cb;
9227 upa.patch_arg = patch_arg;
9228 TAILQ_FOREACH(pe, paths, entry) {
9229 err = worktree_status(worktree, pe->path, fileindex, repo,
9230 unstage_path, &upa, NULL, NULL, 1, 0);
9231 if (err)
9232 goto done;
9235 sync_err = sync_fileindex(fileindex, fileindex_path);
9236 if (sync_err && err == NULL)
9237 err = sync_err;
9238 done:
9239 free(fileindex_path);
9240 if (fileindex)
9241 got_fileindex_free(fileindex);
9242 unlockerr = lock_worktree(worktree, LOCK_SH);
9243 if (unlockerr && err == NULL)
9244 err = unlockerr;
9245 return err;
9248 struct report_file_info_arg {
9249 struct got_worktree *worktree;
9250 got_worktree_path_info_cb info_cb;
9251 void *info_arg;
9252 struct got_pathlist_head *paths;
9253 got_cancel_cb cancel_cb;
9254 void *cancel_arg;
9257 static const struct got_error *
9258 report_file_info(void *arg, struct got_fileindex_entry *ie)
9260 struct report_file_info_arg *a = arg;
9261 struct got_pathlist_entry *pe;
9262 struct got_object_id blob_id, staged_blob_id, commit_id;
9263 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9264 struct got_object_id *commit_idp = NULL;
9265 int stage;
9267 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9268 return got_error(GOT_ERR_CANCELLED);
9270 TAILQ_FOREACH(pe, a->paths, entry) {
9271 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9272 got_path_is_child(ie->path, pe->path, pe->path_len))
9273 break;
9275 if (pe == NULL) /* not found */
9276 return NULL;
9278 if (got_fileindex_entry_has_blob(ie))
9279 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9280 stage = got_fileindex_entry_stage_get(ie);
9281 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9282 stage == GOT_FILEIDX_STAGE_ADD) {
9283 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9284 &staged_blob_id, ie);
9287 if (got_fileindex_entry_has_commit(ie))
9288 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9290 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9291 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9294 const struct got_error *
9295 got_worktree_path_info(struct got_worktree *worktree,
9296 struct got_pathlist_head *paths,
9297 got_worktree_path_info_cb info_cb, void *info_arg,
9298 got_cancel_cb cancel_cb, void *cancel_arg)
9301 const struct got_error *err = NULL, *unlockerr;
9302 struct got_fileindex *fileindex = NULL;
9303 char *fileindex_path = NULL;
9304 struct report_file_info_arg arg;
9306 err = lock_worktree(worktree, LOCK_SH);
9307 if (err)
9308 return err;
9310 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9311 if (err)
9312 goto done;
9314 arg.worktree = worktree;
9315 arg.info_cb = info_cb;
9316 arg.info_arg = info_arg;
9317 arg.paths = paths;
9318 arg.cancel_cb = cancel_cb;
9319 arg.cancel_arg = cancel_arg;
9320 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9321 &arg);
9322 done:
9323 free(fileindex_path);
9324 if (fileindex)
9325 got_fileindex_free(fileindex);
9326 unlockerr = lock_worktree(worktree, LOCK_UN);
9327 if (unlockerr && err == NULL)
9328 err = unlockerr;
9329 return err;
9332 static const struct got_error *
9333 patch_check_path(const char *p, char **path, unsigned char *status,
9334 unsigned char *staged_status, struct got_fileindex *fileindex,
9335 struct got_worktree *worktree, struct got_repository *repo)
9337 const struct got_error *err;
9338 struct got_fileindex_entry *ie;
9339 struct stat sb;
9340 char *ondisk_path = NULL;
9342 err = got_worktree_resolve_path(path, worktree, p);
9343 if (err)
9344 return err;
9346 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9347 *path[0] ? "/" : "", *path) == -1)
9348 return got_error_from_errno("asprintf");
9350 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9351 if (ie) {
9352 *staged_status = get_staged_status(ie);
9353 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9354 repo);
9355 if (err)
9356 goto done;
9357 } else {
9358 *staged_status = GOT_STATUS_NO_CHANGE;
9359 *status = GOT_STATUS_UNVERSIONED;
9360 if (lstat(ondisk_path, &sb) == -1) {
9361 if (errno != ENOENT) {
9362 err = got_error_from_errno2("lstat",
9363 ondisk_path);
9364 goto done;
9366 *status = GOT_STATUS_NONEXISTENT;
9370 done:
9371 free(ondisk_path);
9372 return err;
9375 static const struct got_error *
9376 patch_can_rm(const char *path, unsigned char status,
9377 unsigned char staged_status)
9379 if (status == GOT_STATUS_NONEXISTENT)
9380 return got_error_set_errno(ENOENT, path);
9381 if (status != GOT_STATUS_NO_CHANGE &&
9382 status != GOT_STATUS_ADD &&
9383 status != GOT_STATUS_MODIFY &&
9384 status != GOT_STATUS_MODE_CHANGE)
9385 return got_error_path(path, GOT_ERR_FILE_STATUS);
9386 if (staged_status == GOT_STATUS_DELETE)
9387 return got_error_path(path, GOT_ERR_FILE_STATUS);
9388 return NULL;
9391 static const struct got_error *
9392 patch_can_add(const char *path, unsigned char status)
9394 if (status != GOT_STATUS_NONEXISTENT)
9395 return got_error_path(path, GOT_ERR_FILE_STATUS);
9396 return NULL;
9399 static const struct got_error *
9400 patch_can_edit(const char *path, unsigned char status,
9401 unsigned char staged_status)
9403 if (status == GOT_STATUS_NONEXISTENT)
9404 return got_error_set_errno(ENOENT, path);
9405 if (status != GOT_STATUS_NO_CHANGE &&
9406 status != GOT_STATUS_ADD &&
9407 status != GOT_STATUS_MODIFY)
9408 return got_error_path(path, GOT_ERR_FILE_STATUS);
9409 if (staged_status == GOT_STATUS_DELETE)
9410 return got_error_path(path, GOT_ERR_FILE_STATUS);
9411 return NULL;
9414 const struct got_error *
9415 got_worktree_patch_prepare(struct got_fileindex **fileindex,
9416 char **fileindex_path, struct got_worktree *worktree)
9418 return open_fileindex(fileindex, fileindex_path, worktree);
9421 const struct got_error *
9422 got_worktree_patch_check_path(const char *old, const char *new,
9423 char **oldpath, char **newpath, struct got_worktree *worktree,
9424 struct got_repository *repo, struct got_fileindex *fileindex)
9426 const struct got_error *err = NULL;
9427 int file_renamed = 0;
9428 unsigned char status_old, staged_status_old;
9429 unsigned char status_new, staged_status_new;
9431 *oldpath = NULL;
9432 *newpath = NULL;
9434 err = patch_check_path(old != NULL ? old : new, oldpath,
9435 &status_old, &staged_status_old, fileindex, worktree, repo);
9436 if (err)
9437 goto done;
9439 err = patch_check_path(new != NULL ? new : old, newpath,
9440 &status_new, &staged_status_new, fileindex, worktree, repo);
9441 if (err)
9442 goto done;
9444 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9445 file_renamed = 1;
9447 if (old != NULL && new == NULL)
9448 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9449 else if (file_renamed) {
9450 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9451 if (err == NULL)
9452 err = patch_can_add(*newpath, status_new);
9453 } else if (old == NULL)
9454 err = patch_can_add(*newpath, status_new);
9455 else
9456 err = patch_can_edit(*newpath, status_new, staged_status_new);
9458 done:
9459 if (err) {
9460 free(*oldpath);
9461 *oldpath = NULL;
9462 free(*newpath);
9463 *newpath = NULL;
9465 return err;
9468 const struct got_error *
9469 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9470 struct got_worktree *worktree, struct got_fileindex *fileindex,
9471 got_worktree_checkout_cb progress_cb, void *progress_arg)
9473 struct schedule_addition_args saa;
9475 memset(&saa, 0, sizeof(saa));
9476 saa.worktree = worktree;
9477 saa.fileindex = fileindex;
9478 saa.progress_cb = progress_cb;
9479 saa.progress_arg = progress_arg;
9480 saa.repo = repo;
9482 return worktree_status(worktree, path, fileindex, repo,
9483 schedule_addition, &saa, NULL, NULL, 1, 0);
9486 const struct got_error *
9487 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9488 struct got_worktree *worktree, struct got_fileindex *fileindex,
9489 got_worktree_delete_cb progress_cb, void *progress_arg)
9491 struct schedule_deletion_args sda;
9493 memset(&sda, 0, sizeof(sda));
9494 sda.worktree = worktree;
9495 sda.fileindex = fileindex;
9496 sda.progress_cb = progress_cb;
9497 sda.progress_arg = progress_arg;
9498 sda.repo = repo;
9499 sda.delete_local_mods = 0;
9500 sda.keep_on_disk = 0;
9501 sda.ignore_missing_paths = 0;
9502 sda.status_codes = NULL;
9504 return worktree_status(worktree, path, fileindex, repo,
9505 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9508 const struct got_error *
9509 got_worktree_patch_complete(struct got_fileindex *fileindex,
9510 const char *fileindex_path)
9512 const struct got_error *err = NULL;
9514 err = sync_fileindex(fileindex, fileindex_path);
9515 got_fileindex_free(fileindex);
9517 return err;