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) {
1409 char *parent;
1410 err = got_path_dirname(&parent, path);
1411 if (err)
1412 return err;
1413 err = add_dir_on_disk(worktree, parent);
1414 free(parent);
1415 if (err)
1416 return err;
1417 fd = open(ondisk_path,
1418 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1419 mode);
1420 if (fd == -1)
1421 return got_error_from_errno2("open",
1422 ondisk_path);
1423 } else if (errno == EEXIST) {
1424 if (path_is_unversioned) {
1425 err = (*progress_cb)(progress_arg,
1426 GOT_STATUS_UNVERSIONED, path);
1427 goto done;
1429 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1430 !S_ISREG(st_mode) && !installing_bad_symlink) {
1431 /* TODO file is obstructed; do something */
1432 err = got_error_path(ondisk_path,
1433 GOT_ERR_FILE_OBSTRUCTED);
1434 goto done;
1435 } else {
1436 err = got_opentemp_named_fd(&tmppath, &fd,
1437 ondisk_path, "");
1438 if (err)
1439 goto done;
1440 update = 1;
1442 if (fchmod(fd, apply_umask(mode)) == -1) {
1443 err = got_error_from_errno2("fchmod",
1444 tmppath);
1445 goto done;
1448 } else
1449 return got_error_from_errno2("open", ondisk_path);
1452 if (progress_cb) {
1453 if (restoring_missing_file)
1454 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1455 path);
1456 else if (reverting_versioned_file)
1457 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1458 path);
1459 else
1460 err = (*progress_cb)(progress_arg,
1461 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1462 if (err)
1463 goto done;
1466 hdrlen = got_object_blob_get_hdrlen(blob);
1467 do {
1468 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1469 err = got_object_blob_read_block(&len, blob);
1470 if (err)
1471 break;
1472 if (len > 0) {
1473 /* Skip blob object header first time around. */
1474 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1475 if (outlen == -1) {
1476 err = got_error_from_errno("write");
1477 goto done;
1478 } else if (outlen != len - hdrlen) {
1479 err = got_error(GOT_ERR_IO);
1480 goto done;
1482 hdrlen = 0;
1484 } while (len != 0);
1486 if (fsync(fd) != 0) {
1487 err = got_error_from_errno("fsync");
1488 goto done;
1491 if (update) {
1492 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1493 err = got_error_from_errno2("unlink", ondisk_path);
1494 goto done;
1496 if (rename(tmppath, ondisk_path) != 0) {
1497 err = got_error_from_errno3("rename", tmppath,
1498 ondisk_path);
1499 goto done;
1501 free(tmppath);
1502 tmppath = NULL;
1505 done:
1506 if (fd != -1 && close(fd) == -1 && err == NULL)
1507 err = got_error_from_errno("close");
1508 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1509 err = got_error_from_errno2("unlink", tmppath);
1510 free(tmppath);
1511 return err;
1515 * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
1516 * conflict marker is found in newly added lines only.
1518 static const struct got_error *
1519 get_modified_file_content_status(unsigned char *status,
1520 struct got_blob_object *blob, const char *path, struct stat *sb,
1521 FILE *ondisk_file)
1523 const struct got_error *err, *free_err;
1524 const char *markers[3] = {
1525 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1526 GOT_DIFF_CONFLICT_MARKER_SEP,
1527 GOT_DIFF_CONFLICT_MARKER_END
1529 FILE *f1 = NULL;
1530 struct got_diffreg_result *diffreg_result = NULL;
1531 struct diff_result *r;
1532 int nchunks_parsed, n, i = 0, ln = 0;
1533 char *line = NULL;
1534 size_t linesize = 0;
1535 ssize_t linelen;
1537 if (*status != GOT_STATUS_MODIFY)
1538 return NULL;
1540 f1 = got_opentemp();
1541 if (f1 == NULL)
1542 return got_error_from_errno("got_opentemp");
1544 if (blob) {
1545 got_object_blob_rewind(blob);
1546 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
1547 if (err)
1548 goto done;
1551 err = got_diff_files(&diffreg_result, f1, 1, NULL, ondisk_file,
1552 1, NULL, 0, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
1553 if (err)
1554 goto done;
1556 r = diffreg_result->result;
1558 for (n = 0; n < r->chunks.len; n += nchunks_parsed) {
1559 struct diff_chunk *c;
1560 struct diff_chunk_context cc = {};
1561 off_t pos;
1564 * We can optimise a little by advancing straight
1565 * to the next chunk if this one has no added lines.
1567 c = diff_chunk_get(r, n);
1569 if (diff_chunk_type(c) != CHUNK_PLUS) {
1570 nchunks_parsed = 1;
1571 continue; /* removed or unchanged lines */
1574 pos = diff_chunk_get_right_start_pos(c);
1575 if (fseek(ondisk_file, pos, SEEK_SET) == -1) {
1576 err = got_ferror(ondisk_file, GOT_ERR_IO);
1577 goto done;
1580 diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
1581 ln = cc.right.start;
1583 while (ln < cc.right.end) {
1584 linelen = getline(&line, &linesize, ondisk_file);
1585 if (linelen == -1) {
1586 if (feof(ondisk_file))
1587 break;
1588 err = got_ferror(ondisk_file, GOT_ERR_IO);
1589 break;
1592 if (line && strncmp(line, markers[i],
1593 strlen(markers[i])) == 0) {
1594 if (strcmp(markers[i],
1595 GOT_DIFF_CONFLICT_MARKER_END) == 0) {
1596 *status = GOT_STATUS_CONFLICT;
1597 goto done;
1598 } else
1599 i++;
1601 ++ln;
1605 done:
1606 free(line);
1607 if (f1 != NULL && fclose(f1) == EOF && err == NULL)
1608 err = got_error_from_errno("fclose");
1609 free_err = got_diffreg_result_free(diffreg_result);
1610 if (err == NULL)
1611 err = free_err;
1613 return err;
1616 static int
1617 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1619 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1620 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1623 static int
1624 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1626 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1627 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1628 ie->mtime_sec == sb->st_mtim.tv_sec &&
1629 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1630 ie->size == (sb->st_size & 0xffffffff) &&
1631 !xbit_differs(ie, sb->st_mode));
1634 static unsigned char
1635 get_staged_status(struct got_fileindex_entry *ie)
1637 switch (got_fileindex_entry_stage_get(ie)) {
1638 case GOT_FILEIDX_STAGE_ADD:
1639 return GOT_STATUS_ADD;
1640 case GOT_FILEIDX_STAGE_DELETE:
1641 return GOT_STATUS_DELETE;
1642 case GOT_FILEIDX_STAGE_MODIFY:
1643 return GOT_STATUS_MODIFY;
1644 default:
1645 return GOT_STATUS_NO_CHANGE;
1649 static const struct got_error *
1650 get_symlink_modification_status(unsigned char *status,
1651 struct got_fileindex_entry *ie, const char *abspath,
1652 int dirfd, const char *de_name, struct got_blob_object *blob)
1654 const struct got_error *err = NULL;
1655 char target_path[PATH_MAX];
1656 char etarget[PATH_MAX];
1657 ssize_t elen;
1658 size_t len, target_len = 0;
1659 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1660 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1662 *status = GOT_STATUS_NO_CHANGE;
1664 /* Blob object content specifies the target path of the link. */
1665 do {
1666 err = got_object_blob_read_block(&len, blob);
1667 if (err)
1668 return err;
1669 if (len + target_len >= sizeof(target_path)) {
1671 * Should not happen. The blob contents were OK
1672 * when this symlink was installed.
1674 return got_error(GOT_ERR_NO_SPACE);
1676 if (len > 0) {
1677 /* Skip blob object header first time around. */
1678 memcpy(target_path + target_len, buf + hdrlen,
1679 len - hdrlen);
1680 target_len += len - hdrlen;
1681 hdrlen = 0;
1683 } while (len != 0);
1684 target_path[target_len] = '\0';
1686 if (dirfd != -1) {
1687 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1688 if (elen == -1)
1689 return got_error_from_errno2("readlinkat", abspath);
1690 } else {
1691 elen = readlink(abspath, etarget, sizeof(etarget));
1692 if (elen == -1)
1693 return got_error_from_errno2("readlink", abspath);
1696 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1697 *status = GOT_STATUS_MODIFY;
1699 return NULL;
1702 static const struct got_error *
1703 get_file_status(unsigned char *status, struct stat *sb,
1704 struct got_fileindex_entry *ie, const char *abspath,
1705 int dirfd, const char *de_name, struct got_repository *repo)
1707 const struct got_error *err = NULL;
1708 struct got_object_id id;
1709 size_t hdrlen;
1710 int fd = -1, fd1 = -1;
1711 FILE *f = NULL;
1712 uint8_t fbuf[8192];
1713 struct got_blob_object *blob = NULL;
1714 size_t flen, blen;
1715 unsigned char staged_status;
1717 staged_status = get_staged_status(ie);
1718 *status = GOT_STATUS_NO_CHANGE;
1719 memset(sb, 0, sizeof(*sb));
1722 * Whenever the caller provides a directory descriptor and a
1723 * directory entry name for the file, use them! This prevents
1724 * race conditions if filesystem paths change beneath our feet.
1726 if (dirfd != -1) {
1727 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1728 if (errno == ENOENT) {
1729 if (got_fileindex_entry_has_file_on_disk(ie))
1730 *status = GOT_STATUS_MISSING;
1731 else
1732 *status = GOT_STATUS_DELETE;
1733 goto done;
1735 err = got_error_from_errno2("fstatat", abspath);
1736 goto done;
1738 } else {
1739 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1740 if (fd == -1 && errno != ENOENT &&
1741 !got_err_open_nofollow_on_symlink())
1742 return got_error_from_errno2("open", abspath);
1743 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1744 if (lstat(abspath, sb) == -1)
1745 return got_error_from_errno2("lstat", abspath);
1746 } else if (fd == -1 || fstat(fd, sb) == -1) {
1747 if (errno == ENOENT) {
1748 if (got_fileindex_entry_has_file_on_disk(ie))
1749 *status = GOT_STATUS_MISSING;
1750 else
1751 *status = GOT_STATUS_DELETE;
1752 goto done;
1754 err = got_error_from_errno2("fstat", abspath);
1755 goto done;
1759 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1760 *status = GOT_STATUS_OBSTRUCTED;
1761 goto done;
1764 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1765 *status = GOT_STATUS_DELETE;
1766 goto done;
1767 } else if (!got_fileindex_entry_has_blob(ie) &&
1768 staged_status != GOT_STATUS_ADD) {
1769 *status = GOT_STATUS_ADD;
1770 goto done;
1773 if (!stat_info_differs(ie, sb))
1774 goto done;
1776 if (S_ISLNK(sb->st_mode) &&
1777 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1778 *status = GOT_STATUS_MODIFY;
1779 goto done;
1782 if (staged_status == GOT_STATUS_MODIFY ||
1783 staged_status == GOT_STATUS_ADD)
1784 got_fileindex_entry_get_staged_blob_id(&id, ie);
1785 else
1786 got_fileindex_entry_get_blob_id(&id, ie);
1788 fd1 = got_opentempfd();
1789 if (fd1 == -1) {
1790 err = got_error_from_errno("got_opentempfd");
1791 goto done;
1793 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1794 if (err)
1795 goto done;
1797 if (S_ISLNK(sb->st_mode)) {
1798 err = get_symlink_modification_status(status, ie,
1799 abspath, dirfd, de_name, blob);
1800 goto done;
1803 if (dirfd != -1) {
1804 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1805 if (fd == -1) {
1806 err = got_error_from_errno2("openat", abspath);
1807 goto done;
1811 f = fdopen(fd, "r");
1812 if (f == NULL) {
1813 err = got_error_from_errno2("fdopen", abspath);
1814 goto done;
1816 fd = -1;
1817 hdrlen = got_object_blob_get_hdrlen(blob);
1818 for (;;) {
1819 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1820 err = got_object_blob_read_block(&blen, blob);
1821 if (err)
1822 goto done;
1823 /* Skip length of blob object header first time around. */
1824 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1825 if (flen == 0 && ferror(f)) {
1826 err = got_error_from_errno("fread");
1827 goto done;
1829 if (blen - hdrlen == 0) {
1830 if (flen != 0)
1831 *status = GOT_STATUS_MODIFY;
1832 break;
1833 } else if (flen == 0) {
1834 if (blen - hdrlen != 0)
1835 *status = GOT_STATUS_MODIFY;
1836 break;
1837 } else if (blen - hdrlen == flen) {
1838 /* Skip blob object header first time around. */
1839 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1840 *status = GOT_STATUS_MODIFY;
1841 break;
1843 } else {
1844 *status = GOT_STATUS_MODIFY;
1845 break;
1847 hdrlen = 0;
1850 if (*status == GOT_STATUS_MODIFY) {
1851 rewind(f);
1852 err = get_modified_file_content_status(status, blob, ie->path,
1853 sb, f);
1854 } else if (xbit_differs(ie, sb->st_mode))
1855 *status = GOT_STATUS_MODE_CHANGE;
1856 done:
1857 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1858 err = got_error_from_errno("close");
1859 if (blob)
1860 got_object_blob_close(blob);
1861 if (f != NULL && fclose(f) == EOF && err == NULL)
1862 err = got_error_from_errno2("fclose", abspath);
1863 if (fd != -1 && close(fd) == -1 && err == NULL)
1864 err = got_error_from_errno2("close", abspath);
1865 return err;
1869 * Update timestamps in the file index if a file is unmodified and
1870 * we had to run a full content comparison to find out.
1872 static const struct got_error *
1873 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1874 struct got_fileindex_entry *ie, struct stat *sb)
1876 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1877 return got_fileindex_entry_update(ie, wt_fd, path,
1878 ie->blob_sha1, ie->commit_sha1, 1);
1880 return NULL;
1883 static const struct got_error *
1884 update_blob(struct got_worktree *worktree,
1885 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1886 struct got_tree_entry *te, const char *path,
1887 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1888 void *progress_arg)
1890 const struct got_error *err = NULL;
1891 struct got_blob_object *blob = NULL;
1892 char *ondisk_path = NULL;
1893 unsigned char status = GOT_STATUS_NO_CHANGE;
1894 struct stat sb;
1895 int fd1 = -1, fd2 = -1;
1897 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1898 return got_error_from_errno("asprintf");
1900 if (ie) {
1901 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1902 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1903 goto done;
1905 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1906 repo);
1907 if (err)
1908 goto done;
1909 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1910 sb.st_mode = got_fileindex_perms_to_st(ie);
1911 } else {
1912 if (stat(ondisk_path, &sb) == -1) {
1913 if (errno != ENOENT) {
1914 err = got_error_from_errno2("stat",
1915 ondisk_path);
1916 goto done;
1918 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1919 status = GOT_STATUS_UNVERSIONED;
1920 } else {
1921 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1922 status = GOT_STATUS_UNVERSIONED;
1923 else
1924 status = GOT_STATUS_OBSTRUCTED;
1928 if (status == GOT_STATUS_OBSTRUCTED) {
1929 if (ie)
1930 got_fileindex_entry_mark_skipped(ie);
1931 err = (*progress_cb)(progress_arg, status, path);
1932 goto done;
1934 if (status == GOT_STATUS_CONFLICT) {
1935 if (ie)
1936 got_fileindex_entry_mark_skipped(ie);
1937 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1938 path);
1939 goto done;
1942 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1943 (S_ISLNK(te->mode) ||
1944 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1946 * This is a regular file or an installed bad symlink.
1947 * If the file index indicates that this file is already
1948 * up-to-date with respect to the repository we can skip
1949 * updating contents of this file.
1951 if (got_fileindex_entry_has_commit(ie) &&
1952 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1953 SHA1_DIGEST_LENGTH) == 0) {
1954 /* Same commit. */
1955 err = sync_timestamps(worktree->root_fd,
1956 path, status, ie, &sb);
1957 if (err)
1958 goto done;
1959 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1960 path);
1961 goto done;
1963 if (got_fileindex_entry_has_blob(ie) &&
1964 memcmp(ie->blob_sha1, te->id.sha1,
1965 SHA1_DIGEST_LENGTH) == 0) {
1966 /* Different commit but the same blob. */
1967 err = sync_timestamps(worktree->root_fd,
1968 path, status, ie, &sb);
1969 if (err)
1970 goto done;
1971 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1972 path);
1973 goto done;
1977 fd1 = got_opentempfd();
1978 if (fd1 == -1) {
1979 err = got_error_from_errno("got_opentempfd");
1980 goto done;
1982 err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
1983 if (err)
1984 goto done;
1986 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1987 int update_timestamps;
1988 struct got_blob_object *blob2 = NULL;
1989 char *label_orig = NULL;
1990 if (got_fileindex_entry_has_blob(ie)) {
1991 fd2 = got_opentempfd();
1992 if (fd2 == -1) {
1993 err = got_error_from_errno("got_opentempfd");
1994 goto done;
1996 struct got_object_id id2;
1997 got_fileindex_entry_get_blob_id(&id2, ie);
1998 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
1999 fd2);
2000 if (err)
2001 goto done;
2003 if (got_fileindex_entry_has_commit(ie)) {
2004 char id_str[SHA1_DIGEST_STRING_LENGTH];
2005 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2006 sizeof(id_str)) == NULL) {
2007 err = got_error_path(id_str,
2008 GOT_ERR_BAD_OBJ_ID_STR);
2009 goto done;
2011 if (asprintf(&label_orig, "%s: commit %s",
2012 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2013 err = got_error_from_errno("asprintf");
2014 goto done;
2017 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2018 char *link_target;
2019 err = got_object_blob_read_to_str(&link_target, blob);
2020 if (err)
2021 goto done;
2022 err = merge_symlink(worktree, blob2, ondisk_path, path,
2023 label_orig, link_target, worktree->base_commit_id,
2024 repo, progress_cb, progress_arg);
2025 free(link_target);
2026 } else {
2027 err = merge_blob(&update_timestamps, worktree, blob2,
2028 ondisk_path, path, sb.st_mode, label_orig, blob,
2029 worktree->base_commit_id, repo,
2030 progress_cb, progress_arg);
2032 free(label_orig);
2033 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2034 err = got_error_from_errno("close");
2035 goto done;
2037 if (blob2)
2038 got_object_blob_close(blob2);
2039 if (err)
2040 goto done;
2042 * Do not update timestamps of files with local changes.
2043 * Otherwise, a future status walk would treat them as
2044 * unmodified files again.
2046 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2047 blob->id.sha1, worktree->base_commit_id->sha1,
2048 update_timestamps);
2049 } else if (status == GOT_STATUS_MODE_CHANGE) {
2050 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2051 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2052 } else if (status == GOT_STATUS_DELETE) {
2053 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2054 if (err)
2055 goto done;
2056 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2057 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2058 if (err)
2059 goto done;
2060 } else {
2061 int is_bad_symlink = 0;
2062 if (S_ISLNK(te->mode)) {
2063 err = install_symlink(&is_bad_symlink, worktree,
2064 ondisk_path, path, blob,
2065 status == GOT_STATUS_MISSING, 0,
2066 status == GOT_STATUS_UNVERSIONED, 0,
2067 repo, progress_cb, progress_arg);
2068 } else {
2069 err = install_blob(worktree, ondisk_path, path,
2070 te->mode, sb.st_mode, blob,
2071 status == GOT_STATUS_MISSING, 0, 0,
2072 status == GOT_STATUS_UNVERSIONED, repo,
2073 progress_cb, progress_arg);
2075 if (err)
2076 goto done;
2078 if (ie) {
2079 err = got_fileindex_entry_update(ie,
2080 worktree->root_fd, path, blob->id.sha1,
2081 worktree->base_commit_id->sha1, 1);
2082 } else {
2083 err = create_fileindex_entry(&ie, fileindex,
2084 worktree->base_commit_id, worktree->root_fd, path,
2085 &blob->id);
2087 if (err)
2088 goto done;
2090 if (is_bad_symlink) {
2091 got_fileindex_entry_filetype_set(ie,
2092 GOT_FILEIDX_MODE_BAD_SYMLINK);
2096 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2097 err = got_error_from_errno("close");
2098 goto done;
2100 got_object_blob_close(blob);
2101 done:
2102 free(ondisk_path);
2103 return err;
2106 static const struct got_error *
2107 remove_ondisk_file(const char *root_path, const char *path)
2109 const struct got_error *err = NULL;
2110 char *ondisk_path = NULL, *parent = NULL;
2112 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2113 return got_error_from_errno("asprintf");
2115 if (unlink(ondisk_path) == -1) {
2116 if (errno != ENOENT)
2117 err = got_error_from_errno2("unlink", ondisk_path);
2118 } else {
2119 size_t root_len = strlen(root_path);
2120 err = got_path_dirname(&parent, ondisk_path);
2121 if (err)
2122 goto done;
2123 while (got_path_cmp(parent, root_path,
2124 strlen(parent), root_len) != 0) {
2125 free(ondisk_path);
2126 ondisk_path = parent;
2127 parent = NULL;
2128 if (rmdir(ondisk_path) == -1) {
2129 if (errno != ENOTEMPTY)
2130 err = got_error_from_errno2("rmdir",
2131 ondisk_path);
2132 break;
2134 err = got_path_dirname(&parent, ondisk_path);
2135 if (err)
2136 break;
2139 done:
2140 free(ondisk_path);
2141 free(parent);
2142 return err;
2145 static const struct got_error *
2146 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2147 struct got_fileindex_entry *ie, struct got_repository *repo,
2148 got_worktree_checkout_cb progress_cb, void *progress_arg)
2150 const struct got_error *err = NULL;
2151 unsigned char status;
2152 struct stat sb;
2153 char *ondisk_path;
2155 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2156 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2158 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2159 == -1)
2160 return got_error_from_errno("asprintf");
2162 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2163 if (err)
2164 goto done;
2166 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2167 char ondisk_target[PATH_MAX];
2168 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2169 sizeof(ondisk_target));
2170 if (ondisk_len == -1) {
2171 err = got_error_from_errno2("readlink", ondisk_path);
2172 goto done;
2174 ondisk_target[ondisk_len] = '\0';
2175 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2176 NULL, NULL, /* XXX pass common ancestor info? */
2177 ondisk_target, ondisk_path);
2178 if (err)
2179 goto done;
2180 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2181 ie->path);
2182 goto done;
2185 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2186 status == GOT_STATUS_ADD) {
2187 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2188 if (err)
2189 goto done;
2191 * Preserve the working file and change the deleted blob's
2192 * entry into a schedule-add entry.
2194 err = got_fileindex_entry_update(ie, worktree->root_fd,
2195 ie->path, NULL, NULL, 0);
2196 } else {
2197 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2198 if (err)
2199 goto done;
2200 if (status == GOT_STATUS_NO_CHANGE) {
2201 err = remove_ondisk_file(worktree->root_path, ie->path);
2202 if (err)
2203 goto done;
2205 got_fileindex_entry_remove(fileindex, ie);
2207 done:
2208 free(ondisk_path);
2209 return err;
2212 struct diff_cb_arg {
2213 struct got_fileindex *fileindex;
2214 struct got_worktree *worktree;
2215 struct got_repository *repo;
2216 got_worktree_checkout_cb progress_cb;
2217 void *progress_arg;
2218 got_cancel_cb cancel_cb;
2219 void *cancel_arg;
2222 static const struct got_error *
2223 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2224 struct got_tree_entry *te, const char *parent_path)
2226 struct diff_cb_arg *a = arg;
2228 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2229 return got_error(GOT_ERR_CANCELLED);
2231 return update_blob(a->worktree, a->fileindex, ie, te,
2232 ie->path, a->repo, a->progress_cb, a->progress_arg);
2235 static const struct got_error *
2236 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2238 struct diff_cb_arg *a = arg;
2240 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2241 return got_error(GOT_ERR_CANCELLED);
2243 return delete_blob(a->worktree, a->fileindex, ie,
2244 a->repo, a->progress_cb, a->progress_arg);
2247 static const struct got_error *
2248 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2250 struct diff_cb_arg *a = arg;
2251 const struct got_error *err;
2252 char *path;
2254 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2255 return got_error(GOT_ERR_CANCELLED);
2257 if (got_object_tree_entry_is_submodule(te))
2258 return NULL;
2260 if (asprintf(&path, "%s%s%s", parent_path,
2261 parent_path[0] ? "/" : "", te->name)
2262 == -1)
2263 return got_error_from_errno("asprintf");
2265 if (S_ISDIR(te->mode))
2266 err = add_dir_on_disk(a->worktree, path);
2267 else
2268 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2269 a->repo, a->progress_cb, a->progress_arg);
2271 free(path);
2272 return err;
2275 const struct got_error *
2276 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2278 uint32_t uuid_status;
2280 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2281 if (uuid_status != uuid_s_ok) {
2282 *uuidstr = NULL;
2283 return got_error_uuid(uuid_status, "uuid_to_string");
2286 return NULL;
2289 static const struct got_error *
2290 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2292 const struct got_error *err = NULL;
2293 char *uuidstr = NULL;
2295 *refname = NULL;
2297 err = got_worktree_get_uuid(&uuidstr, worktree);
2298 if (err)
2299 return err;
2301 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2302 err = got_error_from_errno("asprintf");
2303 *refname = NULL;
2305 free(uuidstr);
2306 return err;
2309 const struct got_error *
2310 got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2311 const char *prefix)
2313 return get_ref_name(refname, worktree, prefix);
2316 const struct got_error *
2317 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2319 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2322 static const struct got_error *
2323 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2325 return get_ref_name(refname, worktree,
2326 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2329 static const struct got_error *
2330 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2332 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2335 static const struct got_error *
2336 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2338 return get_ref_name(refname, worktree,
2339 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2342 static const struct got_error *
2343 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2345 return get_ref_name(refname, worktree,
2346 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2349 static const struct got_error *
2350 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2352 return get_ref_name(refname, worktree,
2353 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2356 static const struct got_error *
2357 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2359 return get_ref_name(refname, worktree,
2360 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2363 static const struct got_error *
2364 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2366 return get_ref_name(refname, worktree,
2367 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2370 static const struct got_error *
2371 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2373 return get_ref_name(refname, worktree,
2374 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2377 const struct got_error *
2378 got_worktree_get_histedit_script_path(char **path,
2379 struct got_worktree *worktree)
2381 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2382 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2383 *path = NULL;
2384 return got_error_from_errno("asprintf");
2386 return NULL;
2389 static const struct got_error *
2390 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2392 return get_ref_name(refname, worktree,
2393 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2396 static const struct got_error *
2397 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2399 return get_ref_name(refname, worktree,
2400 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2404 * Prevent Git's garbage collector from deleting our base commit by
2405 * setting a reference to our base commit's ID.
2407 static const struct got_error *
2408 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2410 const struct got_error *err = NULL;
2411 struct got_reference *ref = NULL;
2412 char *refname;
2414 err = got_worktree_get_base_ref_name(&refname, worktree);
2415 if (err)
2416 return err;
2418 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2419 if (err)
2420 goto done;
2422 err = got_ref_write(ref, repo);
2423 done:
2424 free(refname);
2425 if (ref)
2426 got_ref_close(ref);
2427 return err;
2430 static const struct got_error *
2431 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2433 const struct got_error *err = NULL;
2435 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2436 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2437 err = got_error_from_errno("asprintf");
2438 *fileindex_path = NULL;
2440 return err;
2444 static const struct got_error *
2445 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2446 struct got_worktree *worktree)
2448 const struct got_error *err = NULL;
2449 FILE *index = NULL;
2451 *fileindex_path = NULL;
2452 *fileindex = got_fileindex_alloc();
2453 if (*fileindex == NULL)
2454 return got_error_from_errno("got_fileindex_alloc");
2456 err = get_fileindex_path(fileindex_path, worktree);
2457 if (err)
2458 goto done;
2460 index = fopen(*fileindex_path, "rbe");
2461 if (index == NULL) {
2462 if (errno != ENOENT)
2463 err = got_error_from_errno2("fopen", *fileindex_path);
2464 } else {
2465 err = got_fileindex_read(*fileindex, index);
2466 if (fclose(index) == EOF && err == NULL)
2467 err = got_error_from_errno("fclose");
2469 done:
2470 if (err) {
2471 free(*fileindex_path);
2472 *fileindex_path = NULL;
2473 got_fileindex_free(*fileindex);
2474 *fileindex = NULL;
2476 return err;
2479 struct bump_base_commit_id_arg {
2480 struct got_object_id *base_commit_id;
2481 const char *path;
2482 size_t path_len;
2483 const char *entry_name;
2484 got_worktree_checkout_cb progress_cb;
2485 void *progress_arg;
2488 static const struct got_error *
2489 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2491 const struct got_error *err;
2492 struct bump_base_commit_id_arg *a = arg;
2494 if (a->entry_name) {
2495 if (strcmp(ie->path, a->path) != 0)
2496 return NULL;
2497 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2498 return NULL;
2500 if (got_fileindex_entry_was_skipped(ie))
2501 return NULL;
2503 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2504 SHA1_DIGEST_LENGTH) == 0)
2505 return NULL;
2507 if (a->progress_cb) {
2508 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2509 ie->path);
2510 if (err)
2511 return err;
2513 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2514 return NULL;
2517 /* Bump base commit ID of all files within an updated part of the work tree. */
2518 static const struct got_error *
2519 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2520 struct got_fileindex *fileindex,
2521 got_worktree_checkout_cb progress_cb, void *progress_arg)
2523 struct bump_base_commit_id_arg bbc_arg;
2525 bbc_arg.base_commit_id = worktree->base_commit_id;
2526 bbc_arg.entry_name = NULL;
2527 bbc_arg.path = "";
2528 bbc_arg.path_len = 0;
2529 bbc_arg.progress_cb = progress_cb;
2530 bbc_arg.progress_arg = progress_arg;
2532 return got_fileindex_for_each_entry_safe(fileindex,
2533 bump_base_commit_id, &bbc_arg);
2536 static const struct got_error *
2537 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2539 const struct got_error *err = NULL;
2540 char *new_fileindex_path = NULL;
2541 FILE *new_index = NULL;
2542 struct timespec timeout;
2544 err = got_opentemp_named(&new_fileindex_path, &new_index,
2545 fileindex_path, "");
2546 if (err)
2547 goto done;
2549 err = got_fileindex_write(fileindex, new_index);
2550 if (err)
2551 goto done;
2553 if (rename(new_fileindex_path, fileindex_path) != 0) {
2554 err = got_error_from_errno3("rename", new_fileindex_path,
2555 fileindex_path);
2556 unlink(new_fileindex_path);
2560 * Sleep for a short amount of time to ensure that files modified after
2561 * this program exits have a different time stamp from the one which
2562 * was recorded in the file index.
2564 timeout.tv_sec = 0;
2565 timeout.tv_nsec = 1;
2566 nanosleep(&timeout, NULL);
2567 done:
2568 if (new_index)
2569 fclose(new_index);
2570 free(new_fileindex_path);
2571 return err;
2574 static const struct got_error *
2575 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2576 struct got_object_id **tree_id, const char *wt_relpath,
2577 struct got_commit_object *base_commit, struct got_worktree *worktree,
2578 struct got_repository *repo)
2580 const struct got_error *err = NULL;
2581 struct got_object_id *id = NULL;
2582 char *in_repo_path = NULL;
2583 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2585 *entry_type = GOT_OBJ_TYPE_ANY;
2586 *tree_relpath = NULL;
2587 *tree_id = NULL;
2589 if (wt_relpath[0] == '\0') {
2590 /* Check out all files within the work tree. */
2591 *entry_type = GOT_OBJ_TYPE_TREE;
2592 *tree_relpath = strdup("");
2593 if (*tree_relpath == NULL) {
2594 err = got_error_from_errno("strdup");
2595 goto done;
2597 err = got_object_id_by_path(tree_id, repo, base_commit,
2598 worktree->path_prefix);
2599 if (err)
2600 goto done;
2601 return NULL;
2604 /* Check out a subset of files in the work tree. */
2606 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2607 is_root_wt ? "" : "/", wt_relpath) == -1) {
2608 err = got_error_from_errno("asprintf");
2609 goto done;
2612 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2613 if (err)
2614 goto done;
2616 free(in_repo_path);
2617 in_repo_path = NULL;
2619 err = got_object_get_type(entry_type, repo, id);
2620 if (err)
2621 goto done;
2623 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2624 /* Check out a single file. */
2625 if (strchr(wt_relpath, '/') == NULL) {
2626 /* Check out a single file in work tree's root dir. */
2627 in_repo_path = strdup(worktree->path_prefix);
2628 if (in_repo_path == NULL) {
2629 err = got_error_from_errno("strdup");
2630 goto done;
2632 *tree_relpath = strdup("");
2633 if (*tree_relpath == NULL) {
2634 err = got_error_from_errno("strdup");
2635 goto done;
2637 } else {
2638 /* Check out a single file in a subdirectory. */
2639 err = got_path_dirname(tree_relpath, wt_relpath);
2640 if (err)
2641 return err;
2642 if (asprintf(&in_repo_path, "%s%s%s",
2643 worktree->path_prefix, is_root_wt ? "" : "/",
2644 *tree_relpath) == -1) {
2645 err = got_error_from_errno("asprintf");
2646 goto done;
2649 err = got_object_id_by_path(tree_id, repo,
2650 base_commit, in_repo_path);
2651 } else {
2652 /* Check out all files within a subdirectory. */
2653 *tree_id = got_object_id_dup(id);
2654 if (*tree_id == NULL) {
2655 err = got_error_from_errno("got_object_id_dup");
2656 goto done;
2658 *tree_relpath = strdup(wt_relpath);
2659 if (*tree_relpath == NULL) {
2660 err = got_error_from_errno("strdup");
2661 goto done;
2664 done:
2665 free(id);
2666 free(in_repo_path);
2667 if (err) {
2668 *entry_type = GOT_OBJ_TYPE_ANY;
2669 free(*tree_relpath);
2670 *tree_relpath = NULL;
2671 free(*tree_id);
2672 *tree_id = NULL;
2674 return err;
2677 static const struct got_error *
2678 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2679 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2680 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2681 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2683 const struct got_error *err = NULL;
2684 struct got_commit_object *commit = NULL;
2685 struct got_tree_object *tree = NULL;
2686 struct got_fileindex_diff_tree_cb diff_cb;
2687 struct diff_cb_arg arg;
2689 err = ref_base_commit(worktree, repo);
2690 if (err) {
2691 if (!(err->code == GOT_ERR_ERRNO &&
2692 (errno == EACCES || errno == EROFS)))
2693 goto done;
2694 err = (*progress_cb)(progress_arg,
2695 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2696 if (err)
2697 return err;
2700 err = got_object_open_as_commit(&commit, repo,
2701 worktree->base_commit_id);
2702 if (err)
2703 goto done;
2705 err = got_object_open_as_tree(&tree, repo, tree_id);
2706 if (err)
2707 goto done;
2709 if (entry_name &&
2710 got_object_tree_find_entry(tree, entry_name) == NULL) {
2711 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2712 goto done;
2715 diff_cb.diff_old_new = diff_old_new;
2716 diff_cb.diff_old = diff_old;
2717 diff_cb.diff_new = diff_new;
2718 arg.fileindex = fileindex;
2719 arg.worktree = worktree;
2720 arg.repo = repo;
2721 arg.progress_cb = progress_cb;
2722 arg.progress_arg = progress_arg;
2723 arg.cancel_cb = cancel_cb;
2724 arg.cancel_arg = cancel_arg;
2725 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2726 entry_name, repo, &diff_cb, &arg);
2727 done:
2728 if (tree)
2729 got_object_tree_close(tree);
2730 if (commit)
2731 got_object_commit_close(commit);
2732 return err;
2735 const struct got_error *
2736 got_worktree_checkout_files(struct got_worktree *worktree,
2737 struct got_pathlist_head *paths, struct got_repository *repo,
2738 got_worktree_checkout_cb progress_cb, void *progress_arg,
2739 got_cancel_cb cancel_cb, void *cancel_arg)
2741 const struct got_error *err = NULL, *sync_err, *unlockerr;
2742 struct got_commit_object *commit = NULL;
2743 struct got_tree_object *tree = NULL;
2744 struct got_fileindex *fileindex = NULL;
2745 char *fileindex_path = NULL;
2746 struct got_pathlist_entry *pe;
2747 struct tree_path_data {
2748 STAILQ_ENTRY(tree_path_data) entry;
2749 struct got_object_id *tree_id;
2750 int entry_type;
2751 char *relpath;
2752 char *entry_name;
2753 } *tpd = NULL;
2754 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2756 STAILQ_INIT(&tree_paths);
2758 err = lock_worktree(worktree, LOCK_EX);
2759 if (err)
2760 return err;
2762 err = got_object_open_as_commit(&commit, repo,
2763 worktree->base_commit_id);
2764 if (err)
2765 goto done;
2767 /* Map all specified paths to in-repository trees. */
2768 TAILQ_FOREACH(pe, paths, entry) {
2769 tpd = malloc(sizeof(*tpd));
2770 if (tpd == NULL) {
2771 err = got_error_from_errno("malloc");
2772 goto done;
2775 err = find_tree_entry_for_checkout(&tpd->entry_type,
2776 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2777 worktree, repo);
2778 if (err) {
2779 free(tpd);
2780 goto done;
2783 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2784 err = got_path_basename(&tpd->entry_name, pe->path);
2785 if (err) {
2786 free(tpd->relpath);
2787 free(tpd->tree_id);
2788 free(tpd);
2789 goto done;
2791 } else
2792 tpd->entry_name = NULL;
2794 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2798 * Read the file index.
2799 * Checking out files is supposed to be an idempotent operation.
2800 * If the on-disk file index is incomplete we will try to complete it.
2802 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2803 if (err)
2804 goto done;
2806 tpd = STAILQ_FIRST(&tree_paths);
2807 TAILQ_FOREACH(pe, paths, entry) {
2808 struct bump_base_commit_id_arg bbc_arg;
2810 err = checkout_files(worktree, fileindex, tpd->relpath,
2811 tpd->tree_id, tpd->entry_name, repo,
2812 progress_cb, progress_arg, cancel_cb, cancel_arg);
2813 if (err)
2814 break;
2816 bbc_arg.base_commit_id = worktree->base_commit_id;
2817 bbc_arg.entry_name = tpd->entry_name;
2818 bbc_arg.path = pe->path;
2819 bbc_arg.path_len = pe->path_len;
2820 bbc_arg.progress_cb = progress_cb;
2821 bbc_arg.progress_arg = progress_arg;
2822 err = got_fileindex_for_each_entry_safe(fileindex,
2823 bump_base_commit_id, &bbc_arg);
2824 if (err)
2825 break;
2827 tpd = STAILQ_NEXT(tpd, entry);
2829 sync_err = sync_fileindex(fileindex, fileindex_path);
2830 if (sync_err && err == NULL)
2831 err = sync_err;
2832 done:
2833 free(fileindex_path);
2834 if (tree)
2835 got_object_tree_close(tree);
2836 if (commit)
2837 got_object_commit_close(commit);
2838 if (fileindex)
2839 got_fileindex_free(fileindex);
2840 while (!STAILQ_EMPTY(&tree_paths)) {
2841 tpd = STAILQ_FIRST(&tree_paths);
2842 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2843 free(tpd->relpath);
2844 free(tpd->tree_id);
2845 free(tpd);
2847 unlockerr = lock_worktree(worktree, LOCK_SH);
2848 if (unlockerr && err == NULL)
2849 err = unlockerr;
2850 return err;
2853 struct merge_file_cb_arg {
2854 struct got_worktree *worktree;
2855 struct got_fileindex *fileindex;
2856 got_worktree_checkout_cb progress_cb;
2857 void *progress_arg;
2858 got_cancel_cb cancel_cb;
2859 void *cancel_arg;
2860 const char *label_orig;
2861 struct got_object_id *commit_id2;
2862 int allow_bad_symlinks;
2865 static const struct got_error *
2866 merge_file_cb(void *arg, struct got_blob_object *blob1,
2867 struct got_blob_object *blob2, FILE *f1, FILE *f2,
2868 struct got_object_id *id1, struct got_object_id *id2,
2869 const char *path1, const char *path2,
2870 mode_t mode1, mode_t mode2, struct got_repository *repo)
2872 static const struct got_error *err = NULL;
2873 struct merge_file_cb_arg *a = arg;
2874 struct got_fileindex_entry *ie;
2875 char *ondisk_path = NULL;
2876 struct stat sb;
2877 unsigned char status;
2878 int local_changes_subsumed;
2879 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2880 char *id_str = NULL, *label_deriv2 = NULL;
2882 if (blob1 && blob2) {
2883 ie = got_fileindex_entry_get(a->fileindex, path2,
2884 strlen(path2));
2885 if (ie == NULL)
2886 return (*a->progress_cb)(a->progress_arg,
2887 GOT_STATUS_MISSING, path2);
2889 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2890 path2) == -1)
2891 return got_error_from_errno("asprintf");
2893 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2894 repo);
2895 if (err)
2896 goto done;
2898 if (status == GOT_STATUS_DELETE) {
2899 err = (*a->progress_cb)(a->progress_arg,
2900 GOT_STATUS_MERGE, path2);
2901 goto done;
2903 if (status != GOT_STATUS_NO_CHANGE &&
2904 status != GOT_STATUS_MODIFY &&
2905 status != GOT_STATUS_CONFLICT &&
2906 status != GOT_STATUS_ADD) {
2907 err = (*a->progress_cb)(a->progress_arg, status, path2);
2908 goto done;
2911 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2912 char *link_target2;
2913 err = got_object_blob_read_to_str(&link_target2, blob2);
2914 if (err)
2915 goto done;
2916 err = merge_symlink(a->worktree, blob1, ondisk_path,
2917 path2, a->label_orig, link_target2, a->commit_id2,
2918 repo, a->progress_cb, a->progress_arg);
2919 free(link_target2);
2920 } else {
2921 int fd;
2923 f_orig = got_opentemp();
2924 if (f_orig == NULL) {
2925 err = got_error_from_errno("got_opentemp");
2926 goto done;
2928 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2929 f_orig, blob1);
2930 if (err)
2931 goto done;
2933 f_deriv2 = got_opentemp();
2934 if (f_deriv2 == NULL)
2935 goto done;
2936 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2937 f_deriv2, blob2);
2938 if (err)
2939 goto done;
2941 fd = open(ondisk_path,
2942 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2943 if (fd == -1) {
2944 err = got_error_from_errno2("open",
2945 ondisk_path);
2946 goto done;
2948 f_deriv = fdopen(fd, "r");
2949 if (f_deriv == NULL) {
2950 err = got_error_from_errno2("fdopen",
2951 ondisk_path);
2952 close(fd);
2953 goto done;
2955 err = got_object_id_str(&id_str, a->commit_id2);
2956 if (err)
2957 goto done;
2958 if (asprintf(&label_deriv2, "%s: commit %s",
2959 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2960 err = got_error_from_errno("asprintf");
2961 goto done;
2963 err = merge_file(&local_changes_subsumed, a->worktree,
2964 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2965 mode2, a->label_orig, NULL, label_deriv2,
2966 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2967 a->progress_cb, a->progress_arg);
2969 } else if (blob1) {
2970 ie = got_fileindex_entry_get(a->fileindex, path1,
2971 strlen(path1));
2972 if (ie == NULL)
2973 return (*a->progress_cb)(a->progress_arg,
2974 GOT_STATUS_MISSING, path1);
2976 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2977 path1) == -1)
2978 return got_error_from_errno("asprintf");
2980 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2981 repo);
2982 if (err)
2983 goto done;
2985 switch (status) {
2986 case GOT_STATUS_NO_CHANGE:
2987 err = (*a->progress_cb)(a->progress_arg,
2988 GOT_STATUS_DELETE, path1);
2989 if (err)
2990 goto done;
2991 err = remove_ondisk_file(a->worktree->root_path, path1);
2992 if (err)
2993 goto done;
2994 if (ie)
2995 got_fileindex_entry_mark_deleted_from_disk(ie);
2996 break;
2997 case GOT_STATUS_DELETE:
2998 case GOT_STATUS_MISSING:
2999 err = (*a->progress_cb)(a->progress_arg,
3000 GOT_STATUS_DELETE, path1);
3001 if (err)
3002 goto done;
3003 if (ie)
3004 got_fileindex_entry_mark_deleted_from_disk(ie);
3005 break;
3006 case GOT_STATUS_ADD: {
3007 struct got_object_id *id;
3008 FILE *blob1_f;
3009 off_t blob1_size;
3011 * Delete the added file only if its content already
3012 * exists in the repository.
3014 err = got_object_blob_file_create(&id, &blob1_f,
3015 &blob1_size, path1);
3016 if (err)
3017 goto done;
3018 if (got_object_id_cmp(id, id1) == 0) {
3019 err = (*a->progress_cb)(a->progress_arg,
3020 GOT_STATUS_DELETE, path1);
3021 if (err)
3022 goto done;
3023 err = remove_ondisk_file(a->worktree->root_path,
3024 path1);
3025 if (err)
3026 goto done;
3027 if (ie)
3028 got_fileindex_entry_remove(a->fileindex,
3029 ie);
3030 } else {
3031 err = (*a->progress_cb)(a->progress_arg,
3032 GOT_STATUS_CANNOT_DELETE, path1);
3034 if (fclose(blob1_f) == EOF && err == NULL)
3035 err = got_error_from_errno("fclose");
3036 free(id);
3037 if (err)
3038 goto done;
3039 break;
3041 case GOT_STATUS_MODIFY:
3042 case GOT_STATUS_CONFLICT:
3043 err = (*a->progress_cb)(a->progress_arg,
3044 GOT_STATUS_CANNOT_DELETE, path1);
3045 if (err)
3046 goto done;
3047 break;
3048 case GOT_STATUS_OBSTRUCTED:
3049 err = (*a->progress_cb)(a->progress_arg, status, path1);
3050 if (err)
3051 goto done;
3052 break;
3053 default:
3054 break;
3056 } else if (blob2) {
3057 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3058 path2) == -1)
3059 return got_error_from_errno("asprintf");
3060 ie = got_fileindex_entry_get(a->fileindex, path2,
3061 strlen(path2));
3062 if (ie) {
3063 err = get_file_status(&status, &sb, ie, ondisk_path,
3064 -1, NULL, repo);
3065 if (err)
3066 goto done;
3067 if (status != GOT_STATUS_NO_CHANGE &&
3068 status != GOT_STATUS_MODIFY &&
3069 status != GOT_STATUS_CONFLICT &&
3070 status != GOT_STATUS_ADD) {
3071 err = (*a->progress_cb)(a->progress_arg,
3072 status, path2);
3073 goto done;
3075 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3076 char *link_target2;
3077 err = got_object_blob_read_to_str(&link_target2,
3078 blob2);
3079 if (err)
3080 goto done;
3081 err = merge_symlink(a->worktree, NULL,
3082 ondisk_path, path2, a->label_orig,
3083 link_target2, a->commit_id2, repo,
3084 a->progress_cb, a->progress_arg);
3085 free(link_target2);
3086 } else if (S_ISREG(sb.st_mode)) {
3087 err = merge_blob(&local_changes_subsumed,
3088 a->worktree, NULL, ondisk_path, path2,
3089 sb.st_mode, a->label_orig, blob2,
3090 a->commit_id2, repo, a->progress_cb,
3091 a->progress_arg);
3092 } else {
3093 err = got_error_path(ondisk_path,
3094 GOT_ERR_FILE_OBSTRUCTED);
3096 if (err)
3097 goto done;
3098 if (status == GOT_STATUS_DELETE) {
3099 err = got_fileindex_entry_update(ie,
3100 a->worktree->root_fd, path2, blob2->id.sha1,
3101 a->worktree->base_commit_id->sha1, 0);
3102 if (err)
3103 goto done;
3105 } else {
3106 int is_bad_symlink = 0;
3107 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3108 if (S_ISLNK(mode2)) {
3109 err = install_symlink(&is_bad_symlink,
3110 a->worktree, ondisk_path, path2, blob2, 0,
3111 0, 1, a->allow_bad_symlinks, repo,
3112 a->progress_cb, a->progress_arg);
3113 } else {
3114 err = install_blob(a->worktree, ondisk_path, path2,
3115 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3116 a->progress_cb, a->progress_arg);
3118 if (err)
3119 goto done;
3120 err = got_fileindex_entry_alloc(&ie, path2);
3121 if (err)
3122 goto done;
3123 err = got_fileindex_entry_update(ie,
3124 a->worktree->root_fd, path2, NULL, NULL, 1);
3125 if (err) {
3126 got_fileindex_entry_free(ie);
3127 goto done;
3129 err = got_fileindex_entry_add(a->fileindex, ie);
3130 if (err) {
3131 got_fileindex_entry_free(ie);
3132 goto done;
3134 if (is_bad_symlink) {
3135 got_fileindex_entry_filetype_set(ie,
3136 GOT_FILEIDX_MODE_BAD_SYMLINK);
3140 done:
3141 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3142 err = got_error_from_errno("fclose");
3143 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3144 err = got_error_from_errno("fclose");
3145 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3146 err = got_error_from_errno("fclose");
3147 free(id_str);
3148 free(label_deriv2);
3149 free(ondisk_path);
3150 return err;
3153 static const struct got_error *
3154 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3156 struct got_worktree *worktree = arg;
3158 /* Reject merges into a work tree with mixed base commits. */
3159 if (got_fileindex_entry_has_commit(ie) &&
3160 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3161 SHA1_DIGEST_LENGTH) != 0)
3162 return got_error(GOT_ERR_MIXED_COMMITS);
3164 return NULL;
3167 struct check_merge_conflicts_arg {
3168 struct got_worktree *worktree;
3169 struct got_fileindex *fileindex;
3170 struct got_repository *repo;
3173 static const struct got_error *
3174 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3175 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3176 struct got_object_id *id1, struct got_object_id *id2,
3177 const char *path1, const char *path2,
3178 mode_t mode1, mode_t mode2, struct got_repository *repo)
3180 const struct got_error *err = NULL;
3181 struct check_merge_conflicts_arg *a = arg;
3182 unsigned char status;
3183 struct stat sb;
3184 struct got_fileindex_entry *ie;
3185 const char *path = path2 ? path2 : path1;
3186 struct got_object_id *id = id2 ? id2 : id1;
3187 char *ondisk_path;
3189 if (id == NULL)
3190 return NULL;
3192 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3193 if (ie == NULL)
3194 return NULL;
3196 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3197 == -1)
3198 return got_error_from_errno("asprintf");
3200 /* Reject merges into a work tree with conflicted files. */
3201 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3202 free(ondisk_path);
3203 if (err)
3204 return err;
3205 if (status == GOT_STATUS_CONFLICT)
3206 return got_error(GOT_ERR_CONFLICTS);
3208 return NULL;
3211 static const struct got_error *
3212 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3213 const char *fileindex_path, struct got_object_id *commit_id1,
3214 struct got_object_id *commit_id2, struct got_repository *repo,
3215 got_worktree_checkout_cb progress_cb, void *progress_arg,
3216 got_cancel_cb cancel_cb, void *cancel_arg)
3218 const struct got_error *err = NULL, *sync_err;
3219 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3220 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3221 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3222 struct check_merge_conflicts_arg cmc_arg;
3223 struct merge_file_cb_arg arg;
3224 char *label_orig = NULL;
3225 FILE *f1 = NULL, *f2 = NULL;
3226 int fd1 = -1, fd2 = -1;
3228 if (commit_id1) {
3229 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3230 if (err)
3231 goto done;
3232 err = got_object_id_by_path(&tree_id1, repo, commit1,
3233 worktree->path_prefix);
3234 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3235 goto done;
3237 if (tree_id1) {
3238 char *id_str;
3240 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3241 if (err)
3242 goto done;
3244 err = got_object_id_str(&id_str, commit_id1);
3245 if (err)
3246 goto done;
3248 if (asprintf(&label_orig, "%s: commit %s",
3249 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3250 err = got_error_from_errno("asprintf");
3251 free(id_str);
3252 goto done;
3254 free(id_str);
3256 f1 = got_opentemp();
3257 if (f1 == NULL) {
3258 err = got_error_from_errno("got_opentemp");
3259 goto done;
3263 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3264 if (err)
3265 goto done;
3267 err = got_object_id_by_path(&tree_id2, repo, commit2,
3268 worktree->path_prefix);
3269 if (err)
3270 goto done;
3272 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3273 if (err)
3274 goto done;
3276 f2 = got_opentemp();
3277 if (f2 == NULL) {
3278 err = got_error_from_errno("got_opentemp");
3279 goto done;
3282 fd1 = got_opentempfd();
3283 if (fd1 == -1) {
3284 err = got_error_from_errno("got_opentempfd");
3285 goto done;
3288 fd2 = got_opentempfd();
3289 if (fd2 == -1) {
3290 err = got_error_from_errno("got_opentempfd");
3291 goto done;
3294 cmc_arg.worktree = worktree;
3295 cmc_arg.fileindex = fileindex;
3296 cmc_arg.repo = repo;
3297 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3298 check_merge_conflicts, &cmc_arg, 0);
3299 if (err)
3300 goto done;
3302 arg.worktree = worktree;
3303 arg.fileindex = fileindex;
3304 arg.progress_cb = progress_cb;
3305 arg.progress_arg = progress_arg;
3306 arg.cancel_cb = cancel_cb;
3307 arg.cancel_arg = cancel_arg;
3308 arg.label_orig = label_orig;
3309 arg.commit_id2 = commit_id2;
3310 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3311 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3312 merge_file_cb, &arg, 1);
3313 sync_err = sync_fileindex(fileindex, fileindex_path);
3314 if (sync_err && err == NULL)
3315 err = sync_err;
3316 done:
3317 if (commit1)
3318 got_object_commit_close(commit1);
3319 if (commit2)
3320 got_object_commit_close(commit2);
3321 if (tree1)
3322 got_object_tree_close(tree1);
3323 if (tree2)
3324 got_object_tree_close(tree2);
3325 if (f1 && fclose(f1) == EOF && err == NULL)
3326 err = got_error_from_errno("fclose");
3327 if (f2 && fclose(f2) == EOF && err == NULL)
3328 err = got_error_from_errno("fclose");
3329 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3330 err = got_error_from_errno("close");
3331 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3332 err = got_error_from_errno("close");
3333 free(label_orig);
3334 return err;
3337 const struct got_error *
3338 got_worktree_merge_files(struct got_worktree *worktree,
3339 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3340 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3341 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3343 const struct got_error *err, *unlockerr;
3344 char *fileindex_path = NULL;
3345 struct got_fileindex *fileindex = NULL;
3347 err = lock_worktree(worktree, LOCK_EX);
3348 if (err)
3349 return err;
3351 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3352 if (err)
3353 goto done;
3355 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3356 worktree);
3357 if (err)
3358 goto done;
3360 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3361 commit_id2, repo, progress_cb, progress_arg,
3362 cancel_cb, cancel_arg);
3363 done:
3364 if (fileindex)
3365 got_fileindex_free(fileindex);
3366 free(fileindex_path);
3367 unlockerr = lock_worktree(worktree, LOCK_SH);
3368 if (unlockerr && err == NULL)
3369 err = unlockerr;
3370 return err;
3373 struct diff_dir_cb_arg {
3374 struct got_fileindex *fileindex;
3375 struct got_worktree *worktree;
3376 const char *status_path;
3377 size_t status_path_len;
3378 struct got_repository *repo;
3379 got_worktree_status_cb status_cb;
3380 void *status_arg;
3381 got_cancel_cb cancel_cb;
3382 void *cancel_arg;
3383 /* A pathlist containing per-directory pathlists of ignore patterns. */
3384 struct got_pathlist_head *ignores;
3385 int report_unchanged;
3386 int no_ignores;
3389 static const struct got_error *
3390 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3391 int dirfd, const char *de_name,
3392 got_worktree_status_cb status_cb, void *status_arg,
3393 struct got_repository *repo, int report_unchanged)
3395 const struct got_error *err = NULL;
3396 unsigned char status = GOT_STATUS_NO_CHANGE;
3397 unsigned char staged_status;
3398 struct stat sb;
3399 struct got_object_id blob_id, commit_id, staged_blob_id;
3400 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3401 struct got_object_id *staged_blob_idp = NULL;
3403 staged_status = get_staged_status(ie);
3404 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3405 if (err)
3406 return err;
3408 if (status == GOT_STATUS_NO_CHANGE &&
3409 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3410 return NULL;
3412 if (got_fileindex_entry_has_blob(ie))
3413 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3414 if (got_fileindex_entry_has_commit(ie))
3415 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3416 if (staged_status == GOT_STATUS_ADD ||
3417 staged_status == GOT_STATUS_MODIFY) {
3418 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3419 &staged_blob_id, ie);
3422 return (*status_cb)(status_arg, status, staged_status,
3423 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3426 static const struct got_error *
3427 status_old_new(void *arg, struct got_fileindex_entry *ie,
3428 struct dirent *de, const char *parent_path, int dirfd)
3430 const struct got_error *err = NULL;
3431 struct diff_dir_cb_arg *a = arg;
3432 char *abspath;
3434 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3435 return got_error(GOT_ERR_CANCELLED);
3437 if (got_path_cmp(parent_path, a->status_path,
3438 strlen(parent_path), a->status_path_len) != 0 &&
3439 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3440 return NULL;
3442 if (parent_path[0]) {
3443 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3444 parent_path, de->d_name) == -1)
3445 return got_error_from_errno("asprintf");
3446 } else {
3447 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3448 de->d_name) == -1)
3449 return got_error_from_errno("asprintf");
3452 err = report_file_status(ie, abspath, dirfd, de->d_name,
3453 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3454 free(abspath);
3455 return err;
3458 static const struct got_error *
3459 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3461 struct diff_dir_cb_arg *a = arg;
3462 struct got_object_id blob_id, commit_id;
3463 unsigned char status;
3465 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3466 return got_error(GOT_ERR_CANCELLED);
3468 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3469 return NULL;
3471 got_fileindex_entry_get_blob_id(&blob_id, ie);
3472 got_fileindex_entry_get_commit_id(&commit_id, ie);
3473 if (got_fileindex_entry_has_file_on_disk(ie))
3474 status = GOT_STATUS_MISSING;
3475 else
3476 status = GOT_STATUS_DELETE;
3477 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3478 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3481 static void
3482 free_ignores(struct got_pathlist_head *ignores)
3484 struct got_pathlist_entry *pe;
3486 TAILQ_FOREACH(pe, ignores, entry) {
3487 struct got_pathlist_head *ignorelist = pe->data;
3489 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3491 got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3494 static const struct got_error *
3495 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3497 const struct got_error *err = NULL;
3498 struct got_pathlist_entry *pe = NULL;
3499 struct got_pathlist_head *ignorelist;
3500 char *line = NULL, *pattern, *dirpath = NULL;
3501 size_t linesize = 0;
3502 ssize_t linelen;
3504 ignorelist = calloc(1, sizeof(*ignorelist));
3505 if (ignorelist == NULL)
3506 return got_error_from_errno("calloc");
3507 TAILQ_INIT(ignorelist);
3509 while ((linelen = getline(&line, &linesize, f)) != -1) {
3510 if (linelen > 0 && line[linelen - 1] == '\n')
3511 line[linelen - 1] = '\0';
3513 /* Git's ignores may contain comments. */
3514 if (line[0] == '#')
3515 continue;
3517 /* Git's negated patterns are not (yet?) supported. */
3518 if (line[0] == '!')
3519 continue;
3521 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3522 line) == -1) {
3523 err = got_error_from_errno("asprintf");
3524 goto done;
3526 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3527 if (err)
3528 goto done;
3530 if (ferror(f)) {
3531 err = got_error_from_errno("getline");
3532 goto done;
3535 dirpath = strdup(path);
3536 if (dirpath == NULL) {
3537 err = got_error_from_errno("strdup");
3538 goto done;
3540 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3541 done:
3542 free(line);
3543 if (err || pe == NULL) {
3544 free(dirpath);
3545 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3547 return err;
3550 static int
3551 match_path(const char *pattern, size_t pattern_len, const char *path,
3552 int flags)
3554 char buf[PATH_MAX];
3557 * Trailing slashes signify directories.
3558 * Append a * to make such patterns conform to fnmatch rules.
3560 if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3561 if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3562 return FNM_NOMATCH; /* XXX */
3564 return fnmatch(buf, path, flags);
3567 return fnmatch(pattern, path, flags);
3570 static int
3571 match_ignores(struct got_pathlist_head *ignores, const char *path)
3573 struct got_pathlist_entry *pe;
3575 /* Handle patterns which match in all directories. */
3576 TAILQ_FOREACH(pe, ignores, entry) {
3577 struct got_pathlist_head *ignorelist = pe->data;
3578 struct got_pathlist_entry *pi;
3580 TAILQ_FOREACH(pi, ignorelist, entry) {
3581 const char *p;
3583 if (pi->path_len < 3 ||
3584 strncmp(pi->path, "**/", 3) != 0)
3585 continue;
3586 p = path;
3587 while (*p) {
3588 if (match_path(pi->path + 3,
3589 pi->path_len - 3, p,
3590 FNM_PATHNAME | FNM_LEADING_DIR)) {
3591 /* Retry in next directory. */
3592 while (*p && *p != '/')
3593 p++;
3594 while (*p == '/')
3595 p++;
3596 continue;
3598 return 1;
3604 * The ignores pathlist contains ignore lists from children before
3605 * parents, so we can find the most specific ignorelist by walking
3606 * ignores backwards.
3608 pe = TAILQ_LAST(ignores, got_pathlist_head);
3609 while (pe) {
3610 if (got_path_is_child(path, pe->path, pe->path_len)) {
3611 struct got_pathlist_head *ignorelist = pe->data;
3612 struct got_pathlist_entry *pi;
3613 TAILQ_FOREACH(pi, ignorelist, entry) {
3614 int flags = FNM_LEADING_DIR;
3615 if (strstr(pi->path, "/**/") == NULL)
3616 flags |= FNM_PATHNAME;
3617 if (match_path(pi->path, pi->path_len,
3618 path, flags))
3619 continue;
3620 return 1;
3623 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3626 return 0;
3629 static const struct got_error *
3630 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3631 const char *path, int dirfd, const char *ignores_filename)
3633 const struct got_error *err = NULL;
3634 char *ignorespath;
3635 int fd = -1;
3636 FILE *ignoresfile = NULL;
3638 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3639 path[0] ? "/" : "", ignores_filename) == -1)
3640 return got_error_from_errno("asprintf");
3642 if (dirfd != -1) {
3643 fd = openat(dirfd, ignores_filename,
3644 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3645 if (fd == -1) {
3646 if (errno != ENOENT && errno != EACCES)
3647 err = got_error_from_errno2("openat",
3648 ignorespath);
3649 } else {
3650 ignoresfile = fdopen(fd, "r");
3651 if (ignoresfile == NULL)
3652 err = got_error_from_errno2("fdopen",
3653 ignorespath);
3654 else {
3655 fd = -1;
3656 err = read_ignores(ignores, path, ignoresfile);
3659 } else {
3660 ignoresfile = fopen(ignorespath, "re");
3661 if (ignoresfile == NULL) {
3662 if (errno != ENOENT && errno != EACCES)
3663 err = got_error_from_errno2("fopen",
3664 ignorespath);
3665 } else
3666 err = read_ignores(ignores, path, ignoresfile);
3669 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3670 err = got_error_from_errno2("fclose", path);
3671 if (fd != -1 && close(fd) == -1 && err == NULL)
3672 err = got_error_from_errno2("close", path);
3673 free(ignorespath);
3674 return err;
3677 static const struct got_error *
3678 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3679 int dirfd)
3681 const struct got_error *err = NULL;
3682 struct diff_dir_cb_arg *a = arg;
3683 char *path = NULL;
3685 if (ignore != NULL)
3686 *ignore = 0;
3688 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3689 return got_error(GOT_ERR_CANCELLED);
3691 if (parent_path[0]) {
3692 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3693 return got_error_from_errno("asprintf");
3694 } else {
3695 path = de->d_name;
3698 if (de->d_type == DT_DIR) {
3699 if (!a->no_ignores && ignore != NULL &&
3700 match_ignores(a->ignores, path))
3701 *ignore = 1;
3702 } else if (!match_ignores(a->ignores, path) &&
3703 got_path_is_child(path, a->status_path, a->status_path_len))
3704 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3705 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3706 if (parent_path[0])
3707 free(path);
3708 return err;
3711 static const struct got_error *
3712 status_traverse(void *arg, const char *path, int dirfd)
3714 const struct got_error *err = NULL;
3715 struct diff_dir_cb_arg *a = arg;
3717 if (a->no_ignores)
3718 return NULL;
3720 err = add_ignores(a->ignores, a->worktree->root_path,
3721 path, dirfd, ".cvsignore");
3722 if (err)
3723 return err;
3725 err = add_ignores(a->ignores, a->worktree->root_path, path,
3726 dirfd, ".gitignore");
3728 return err;
3731 static const struct got_error *
3732 report_single_file_status(const char *path, const char *ondisk_path,
3733 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3734 void *status_arg, struct got_repository *repo, int report_unchanged,
3735 struct got_pathlist_head *ignores, int no_ignores)
3737 struct got_fileindex_entry *ie;
3738 struct stat sb;
3740 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3741 if (ie)
3742 return report_file_status(ie, ondisk_path, -1, NULL,
3743 status_cb, status_arg, repo, report_unchanged);
3745 if (lstat(ondisk_path, &sb) == -1) {
3746 if (errno != ENOENT)
3747 return got_error_from_errno2("lstat", ondisk_path);
3748 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3749 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3752 if (!no_ignores && match_ignores(ignores, path))
3753 return NULL;
3755 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3756 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3757 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3759 return NULL;
3762 static const struct got_error *
3763 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3764 const char *root_path, const char *path)
3766 const struct got_error *err;
3767 char *parent_path, *next_parent_path = NULL;
3769 err = add_ignores(ignores, root_path, "", -1,
3770 ".cvsignore");
3771 if (err)
3772 return err;
3774 err = add_ignores(ignores, root_path, "", -1,
3775 ".gitignore");
3776 if (err)
3777 return err;
3779 err = got_path_dirname(&parent_path, path);
3780 if (err) {
3781 if (err->code == GOT_ERR_BAD_PATH)
3782 return NULL; /* cannot traverse parent */
3783 return err;
3785 for (;;) {
3786 err = add_ignores(ignores, root_path, parent_path, -1,
3787 ".cvsignore");
3788 if (err)
3789 break;
3790 err = add_ignores(ignores, root_path, parent_path, -1,
3791 ".gitignore");
3792 if (err)
3793 break;
3794 err = got_path_dirname(&next_parent_path, parent_path);
3795 if (err) {
3796 if (err->code == GOT_ERR_BAD_PATH)
3797 err = NULL; /* traversed everything */
3798 break;
3800 if (got_path_is_root_dir(parent_path))
3801 break;
3802 free(parent_path);
3803 parent_path = next_parent_path;
3804 next_parent_path = NULL;
3807 free(parent_path);
3808 free(next_parent_path);
3809 return err;
3812 static const struct got_error *
3813 worktree_status(struct got_worktree *worktree, const char *path,
3814 struct got_fileindex *fileindex, struct got_repository *repo,
3815 got_worktree_status_cb status_cb, void *status_arg,
3816 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3817 int report_unchanged)
3819 const struct got_error *err = NULL;
3820 int fd = -1;
3821 struct got_fileindex_diff_dir_cb fdiff_cb;
3822 struct diff_dir_cb_arg arg;
3823 char *ondisk_path = NULL;
3824 struct got_pathlist_head ignores;
3825 struct got_fileindex_entry *ie;
3827 TAILQ_INIT(&ignores);
3829 if (asprintf(&ondisk_path, "%s%s%s",
3830 worktree->root_path, path[0] ? "/" : "", path) == -1)
3831 return got_error_from_errno("asprintf");
3833 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3834 if (ie) {
3835 err = report_single_file_status(path, ondisk_path,
3836 fileindex, status_cb, status_arg, repo,
3837 report_unchanged, &ignores, no_ignores);
3838 goto done;
3841 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3842 if (fd == -1) {
3843 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3844 !got_err_open_nofollow_on_symlink())
3845 err = got_error_from_errno2("open", ondisk_path);
3846 else {
3847 if (!no_ignores) {
3848 err = add_ignores_from_parent_paths(&ignores,
3849 worktree->root_path, ondisk_path);
3850 if (err)
3851 goto done;
3853 err = report_single_file_status(path, ondisk_path,
3854 fileindex, status_cb, status_arg, repo,
3855 report_unchanged, &ignores, no_ignores);
3857 } else {
3858 fdiff_cb.diff_old_new = status_old_new;
3859 fdiff_cb.diff_old = status_old;
3860 fdiff_cb.diff_new = status_new;
3861 fdiff_cb.diff_traverse = status_traverse;
3862 arg.fileindex = fileindex;
3863 arg.worktree = worktree;
3864 arg.status_path = path;
3865 arg.status_path_len = strlen(path);
3866 arg.repo = repo;
3867 arg.status_cb = status_cb;
3868 arg.status_arg = status_arg;
3869 arg.cancel_cb = cancel_cb;
3870 arg.cancel_arg = cancel_arg;
3871 arg.report_unchanged = report_unchanged;
3872 arg.no_ignores = no_ignores;
3873 if (!no_ignores) {
3874 err = add_ignores_from_parent_paths(&ignores,
3875 worktree->root_path, path);
3876 if (err)
3877 goto done;
3879 arg.ignores = &ignores;
3880 err = got_fileindex_diff_dir(fileindex, fd,
3881 worktree->root_path, path, repo, &fdiff_cb, &arg);
3883 done:
3884 free_ignores(&ignores);
3885 if (fd != -1 && close(fd) == -1 && err == NULL)
3886 err = got_error_from_errno("close");
3887 free(ondisk_path);
3888 return err;
3891 const struct got_error *
3892 got_worktree_status(struct got_worktree *worktree,
3893 struct got_pathlist_head *paths, struct got_repository *repo,
3894 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3895 got_cancel_cb cancel_cb, void *cancel_arg)
3897 const struct got_error *err = NULL;
3898 char *fileindex_path = NULL;
3899 struct got_fileindex *fileindex = NULL;
3900 struct got_pathlist_entry *pe;
3902 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3903 if (err)
3904 return err;
3906 TAILQ_FOREACH(pe, paths, entry) {
3907 err = worktree_status(worktree, pe->path, fileindex, repo,
3908 status_cb, status_arg, cancel_cb, cancel_arg,
3909 no_ignores, 0);
3910 if (err)
3911 break;
3913 free(fileindex_path);
3914 got_fileindex_free(fileindex);
3915 return err;
3918 const struct got_error *
3919 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3920 const char *arg)
3922 const struct got_error *err = NULL;
3923 char *resolved = NULL, *cwd = NULL, *path = NULL;
3924 size_t len;
3925 struct stat sb;
3926 char *abspath = NULL;
3927 char canonpath[PATH_MAX];
3929 *wt_path = NULL;
3931 cwd = getcwd(NULL, 0);
3932 if (cwd == NULL)
3933 return got_error_from_errno("getcwd");
3935 if (lstat(arg, &sb) == -1) {
3936 if (errno != ENOENT) {
3937 err = got_error_from_errno2("lstat", arg);
3938 goto done;
3940 sb.st_mode = 0;
3942 if (S_ISLNK(sb.st_mode)) {
3944 * We cannot use realpath(3) with symlinks since we want to
3945 * operate on the symlink itself.
3946 * But we can make the path absolute, assuming it is relative
3947 * to the current working directory, and then canonicalize it.
3949 if (!got_path_is_absolute(arg)) {
3950 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3951 err = got_error_from_errno("asprintf");
3952 goto done;
3956 err = got_canonpath(abspath ? abspath : arg, canonpath,
3957 sizeof(canonpath));
3958 if (err)
3959 goto done;
3960 resolved = strdup(canonpath);
3961 if (resolved == NULL) {
3962 err = got_error_from_errno("strdup");
3963 goto done;
3965 } else {
3966 resolved = realpath(arg, NULL);
3967 if (resolved == NULL) {
3968 if (errno != ENOENT) {
3969 err = got_error_from_errno2("realpath", arg);
3970 goto done;
3972 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3973 err = got_error_from_errno("asprintf");
3974 goto done;
3976 err = got_canonpath(abspath, canonpath,
3977 sizeof(canonpath));
3978 if (err)
3979 goto done;
3980 resolved = strdup(canonpath);
3981 if (resolved == NULL) {
3982 err = got_error_from_errno("strdup");
3983 goto done;
3988 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3989 strlen(got_worktree_get_root_path(worktree)))) {
3990 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3991 goto done;
3994 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3995 err = got_path_skip_common_ancestor(&path,
3996 got_worktree_get_root_path(worktree), resolved);
3997 if (err)
3998 goto done;
3999 } else {
4000 path = strdup("");
4001 if (path == NULL) {
4002 err = got_error_from_errno("strdup");
4003 goto done;
4007 /* XXX status walk can't deal with trailing slash! */
4008 len = strlen(path);
4009 while (len > 0 && path[len - 1] == '/') {
4010 path[len - 1] = '\0';
4011 len--;
4013 done:
4014 free(abspath);
4015 free(resolved);
4016 free(cwd);
4017 if (err == NULL)
4018 *wt_path = path;
4019 else
4020 free(path);
4021 return err;
4024 struct schedule_addition_args {
4025 struct got_worktree *worktree;
4026 struct got_fileindex *fileindex;
4027 got_worktree_checkout_cb progress_cb;
4028 void *progress_arg;
4029 struct got_repository *repo;
4032 static const struct got_error *
4033 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4034 const char *relpath, struct got_object_id *blob_id,
4035 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4036 int dirfd, const char *de_name)
4038 struct schedule_addition_args *a = arg;
4039 const struct got_error *err = NULL;
4040 struct got_fileindex_entry *ie;
4041 struct stat sb;
4042 char *ondisk_path;
4044 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4045 relpath) == -1)
4046 return got_error_from_errno("asprintf");
4048 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4049 if (ie) {
4050 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4051 de_name, a->repo);
4052 if (err)
4053 goto done;
4054 /* Re-adding an existing entry is a no-op. */
4055 if (status == GOT_STATUS_ADD)
4056 goto done;
4057 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4058 if (err)
4059 goto done;
4062 if (status != GOT_STATUS_UNVERSIONED) {
4063 if (status == GOT_STATUS_NONEXISTENT)
4064 err = got_error_set_errno(ENOENT, ondisk_path);
4065 else
4066 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4067 goto done;
4070 err = got_fileindex_entry_alloc(&ie, relpath);
4071 if (err)
4072 goto done;
4073 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4074 relpath, NULL, NULL, 1);
4075 if (err) {
4076 got_fileindex_entry_free(ie);
4077 goto done;
4079 err = got_fileindex_entry_add(a->fileindex, ie);
4080 if (err) {
4081 got_fileindex_entry_free(ie);
4082 goto done;
4084 done:
4085 free(ondisk_path);
4086 if (err)
4087 return err;
4088 if (status == GOT_STATUS_ADD)
4089 return NULL;
4090 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4093 const struct got_error *
4094 got_worktree_schedule_add(struct got_worktree *worktree,
4095 struct got_pathlist_head *paths,
4096 got_worktree_checkout_cb progress_cb, void *progress_arg,
4097 struct got_repository *repo, int no_ignores)
4099 struct got_fileindex *fileindex = NULL;
4100 char *fileindex_path = NULL;
4101 const struct got_error *err = NULL, *sync_err, *unlockerr;
4102 struct got_pathlist_entry *pe;
4103 struct schedule_addition_args saa;
4105 err = lock_worktree(worktree, LOCK_EX);
4106 if (err)
4107 return err;
4109 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4110 if (err)
4111 goto done;
4113 saa.worktree = worktree;
4114 saa.fileindex = fileindex;
4115 saa.progress_cb = progress_cb;
4116 saa.progress_arg = progress_arg;
4117 saa.repo = repo;
4119 TAILQ_FOREACH(pe, paths, entry) {
4120 err = worktree_status(worktree, pe->path, fileindex, repo,
4121 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4122 if (err)
4123 break;
4125 sync_err = sync_fileindex(fileindex, fileindex_path);
4126 if (sync_err && err == NULL)
4127 err = sync_err;
4128 done:
4129 free(fileindex_path);
4130 if (fileindex)
4131 got_fileindex_free(fileindex);
4132 unlockerr = lock_worktree(worktree, LOCK_SH);
4133 if (unlockerr && err == NULL)
4134 err = unlockerr;
4135 return err;
4138 struct schedule_deletion_args {
4139 struct got_worktree *worktree;
4140 struct got_fileindex *fileindex;
4141 got_worktree_delete_cb progress_cb;
4142 void *progress_arg;
4143 struct got_repository *repo;
4144 int delete_local_mods;
4145 int keep_on_disk;
4146 int ignore_missing_paths;
4147 const char *status_codes;
4150 static const struct got_error *
4151 schedule_for_deletion(void *arg, unsigned char status,
4152 unsigned char staged_status, const char *relpath,
4153 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4154 struct got_object_id *commit_id, int dirfd, const char *de_name)
4156 struct schedule_deletion_args *a = arg;
4157 const struct got_error *err = NULL;
4158 struct got_fileindex_entry *ie = NULL;
4159 struct stat sb;
4160 char *ondisk_path;
4162 if (status == GOT_STATUS_NONEXISTENT) {
4163 if (a->ignore_missing_paths)
4164 return NULL;
4165 return got_error_set_errno(ENOENT, relpath);
4168 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4169 if (ie == NULL)
4170 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4172 staged_status = get_staged_status(ie);
4173 if (staged_status != GOT_STATUS_NO_CHANGE) {
4174 if (staged_status == GOT_STATUS_DELETE)
4175 return NULL;
4176 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4179 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4180 relpath) == -1)
4181 return got_error_from_errno("asprintf");
4183 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4184 a->repo);
4185 if (err)
4186 goto done;
4188 if (a->status_codes) {
4189 size_t ncodes = strlen(a->status_codes);
4190 int i;
4191 for (i = 0; i < ncodes ; i++) {
4192 if (status == a->status_codes[i])
4193 break;
4195 if (i == ncodes) {
4196 /* Do not delete files in non-matching status. */
4197 free(ondisk_path);
4198 return NULL;
4200 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4201 a->status_codes[i] != GOT_STATUS_MISSING) {
4202 static char msg[64];
4203 snprintf(msg, sizeof(msg),
4204 "invalid status code '%c'", a->status_codes[i]);
4205 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4206 goto done;
4210 if (status != GOT_STATUS_NO_CHANGE) {
4211 if (status == GOT_STATUS_DELETE)
4212 goto done;
4213 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4214 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4215 goto done;
4217 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4218 err = got_error_set_errno(ENOENT, relpath);
4219 goto done;
4221 if (status != GOT_STATUS_MODIFY &&
4222 status != GOT_STATUS_MISSING) {
4223 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4224 goto done;
4228 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4229 size_t root_len;
4231 if (dirfd != -1) {
4232 if (unlinkat(dirfd, de_name, 0) == -1) {
4233 err = got_error_from_errno2("unlinkat",
4234 ondisk_path);
4235 goto done;
4237 } else if (unlink(ondisk_path) == -1) {
4238 err = got_error_from_errno2("unlink", ondisk_path);
4239 goto done;
4242 root_len = strlen(a->worktree->root_path);
4243 do {
4244 char *parent;
4245 err = got_path_dirname(&parent, ondisk_path);
4246 if (err)
4247 goto done;
4248 free(ondisk_path);
4249 ondisk_path = parent;
4250 if (rmdir(ondisk_path) == -1) {
4251 if (errno != ENOTEMPTY)
4252 err = got_error_from_errno2("rmdir",
4253 ondisk_path);
4254 break;
4256 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4257 strlen(ondisk_path), root_len) != 0);
4260 got_fileindex_entry_mark_deleted_from_disk(ie);
4261 done:
4262 free(ondisk_path);
4263 if (err)
4264 return err;
4265 if (status == GOT_STATUS_DELETE)
4266 return NULL;
4267 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4268 staged_status, relpath);
4271 const struct got_error *
4272 got_worktree_schedule_delete(struct got_worktree *worktree,
4273 struct got_pathlist_head *paths, int delete_local_mods,
4274 const char *status_codes,
4275 got_worktree_delete_cb progress_cb, void *progress_arg,
4276 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4278 struct got_fileindex *fileindex = NULL;
4279 char *fileindex_path = NULL;
4280 const struct got_error *err = NULL, *sync_err, *unlockerr;
4281 struct got_pathlist_entry *pe;
4282 struct schedule_deletion_args sda;
4284 err = lock_worktree(worktree, LOCK_EX);
4285 if (err)
4286 return err;
4288 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4289 if (err)
4290 goto done;
4292 sda.worktree = worktree;
4293 sda.fileindex = fileindex;
4294 sda.progress_cb = progress_cb;
4295 sda.progress_arg = progress_arg;
4296 sda.repo = repo;
4297 sda.delete_local_mods = delete_local_mods;
4298 sda.keep_on_disk = keep_on_disk;
4299 sda.ignore_missing_paths = ignore_missing_paths;
4300 sda.status_codes = status_codes;
4302 TAILQ_FOREACH(pe, paths, entry) {
4303 err = worktree_status(worktree, pe->path, fileindex, repo,
4304 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4305 if (err)
4306 break;
4308 sync_err = sync_fileindex(fileindex, fileindex_path);
4309 if (sync_err && err == NULL)
4310 err = sync_err;
4311 done:
4312 free(fileindex_path);
4313 if (fileindex)
4314 got_fileindex_free(fileindex);
4315 unlockerr = lock_worktree(worktree, LOCK_SH);
4316 if (unlockerr && err == NULL)
4317 err = unlockerr;
4318 return err;
4321 static const struct got_error *
4322 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4324 const struct got_error *err = NULL;
4325 char *line = NULL;
4326 size_t linesize = 0, n;
4327 ssize_t linelen;
4329 linelen = getline(&line, &linesize, infile);
4330 if (linelen == -1) {
4331 if (ferror(infile)) {
4332 err = got_error_from_errno("getline");
4333 goto done;
4335 return NULL;
4337 if (outfile) {
4338 n = fwrite(line, 1, linelen, outfile);
4339 if (n != linelen) {
4340 err = got_ferror(outfile, GOT_ERR_IO);
4341 goto done;
4344 if (rejectfile) {
4345 n = fwrite(line, 1, linelen, rejectfile);
4346 if (n != linelen)
4347 err = got_ferror(rejectfile, GOT_ERR_IO);
4349 done:
4350 free(line);
4351 return err;
4354 static const struct got_error *
4355 skip_one_line(FILE *f)
4357 char *line = NULL;
4358 size_t linesize = 0;
4359 ssize_t linelen;
4361 linelen = getline(&line, &linesize, f);
4362 if (linelen == -1) {
4363 if (ferror(f))
4364 return got_error_from_errno("getline");
4365 return NULL;
4367 free(line);
4368 return NULL;
4371 static const struct got_error *
4372 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4373 int start_old, int end_old, int start_new, int end_new,
4374 FILE *outfile, FILE *rejectfile)
4376 const struct got_error *err;
4378 /* Copy old file's lines leading up to patch. */
4379 while (!feof(f1) && *line_cur1 < start_old) {
4380 err = copy_one_line(f1, outfile, NULL);
4381 if (err)
4382 return err;
4383 (*line_cur1)++;
4385 /* Skip new file's lines leading up to patch. */
4386 while (!feof(f2) && *line_cur2 < start_new) {
4387 if (rejectfile)
4388 err = copy_one_line(f2, NULL, rejectfile);
4389 else
4390 err = skip_one_line(f2);
4391 if (err)
4392 return err;
4393 (*line_cur2)++;
4395 /* Copy patched lines. */
4396 while (!feof(f2) && *line_cur2 <= end_new) {
4397 err = copy_one_line(f2, outfile, NULL);
4398 if (err)
4399 return err;
4400 (*line_cur2)++;
4402 /* Skip over old file's replaced lines. */
4403 while (!feof(f1) && *line_cur1 <= end_old) {
4404 if (rejectfile)
4405 err = copy_one_line(f1, NULL, rejectfile);
4406 else
4407 err = skip_one_line(f1);
4408 if (err)
4409 return err;
4410 (*line_cur1)++;
4413 return NULL;
4416 static const struct got_error *
4417 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4418 FILE *outfile, FILE *rejectfile)
4420 const struct got_error *err;
4422 if (outfile) {
4423 /* Copy old file's lines until EOF. */
4424 while (!feof(f1)) {
4425 err = copy_one_line(f1, outfile, NULL);
4426 if (err)
4427 return err;
4428 (*line_cur1)++;
4431 if (rejectfile) {
4432 /* Copy new file's lines until EOF. */
4433 while (!feof(f2)) {
4434 err = copy_one_line(f2, NULL, rejectfile);
4435 if (err)
4436 return err;
4437 (*line_cur2)++;
4441 return NULL;
4444 static const struct got_error *
4445 apply_or_reject_change(int *choice, int *nchunks_used,
4446 struct diff_result *diff_result, int n,
4447 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4448 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4449 got_worktree_patch_cb patch_cb, void *patch_arg)
4451 const struct got_error *err = NULL;
4452 struct diff_chunk_context cc = {};
4453 int start_old, end_old, start_new, end_new;
4454 FILE *hunkfile;
4455 struct diff_output_unidiff_state *diff_state;
4456 struct diff_input_info diff_info;
4457 int rc;
4459 *choice = GOT_PATCH_CHOICE_NONE;
4461 /* Get changed line numbers without context lines for copy_change(). */
4462 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4463 start_old = cc.left.start;
4464 end_old = cc.left.end;
4465 start_new = cc.right.start;
4466 end_new = cc.right.end;
4468 /* Get the same change with context lines for display. */
4469 memset(&cc, 0, sizeof(cc));
4470 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4472 memset(&diff_info, 0, sizeof(diff_info));
4473 diff_info.left_path = relpath;
4474 diff_info.right_path = relpath;
4476 diff_state = diff_output_unidiff_state_alloc();
4477 if (diff_state == NULL)
4478 return got_error_set_errno(ENOMEM,
4479 "diff_output_unidiff_state_alloc");
4481 hunkfile = got_opentemp();
4482 if (hunkfile == NULL) {
4483 err = got_error_from_errno("got_opentemp");
4484 goto done;
4487 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4488 diff_result, &cc);
4489 if (rc != DIFF_RC_OK) {
4490 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4491 goto done;
4494 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4495 err = got_ferror(hunkfile, GOT_ERR_IO);
4496 goto done;
4499 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4500 hunkfile, changeno, nchanges);
4501 if (err)
4502 goto done;
4504 switch (*choice) {
4505 case GOT_PATCH_CHOICE_YES:
4506 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4507 end_old, start_new, end_new, outfile, rejectfile);
4508 break;
4509 case GOT_PATCH_CHOICE_NO:
4510 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4511 end_old, start_new, end_new, rejectfile, outfile);
4512 break;
4513 case GOT_PATCH_CHOICE_QUIT:
4514 break;
4515 default:
4516 err = got_error(GOT_ERR_PATCH_CHOICE);
4517 break;
4519 done:
4520 diff_output_unidiff_state_free(diff_state);
4521 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4522 err = got_error_from_errno("fclose");
4523 return err;
4526 struct revert_file_args {
4527 struct got_worktree *worktree;
4528 struct got_fileindex *fileindex;
4529 got_worktree_checkout_cb progress_cb;
4530 void *progress_arg;
4531 got_worktree_patch_cb patch_cb;
4532 void *patch_arg;
4533 struct got_repository *repo;
4534 int unlink_added_files;
4537 static const struct got_error *
4538 create_patched_content(char **path_outfile, int reverse_patch,
4539 struct got_object_id *blob_id, const char *path2,
4540 int dirfd2, const char *de_name2,
4541 const char *relpath, struct got_repository *repo,
4542 got_worktree_patch_cb patch_cb, void *patch_arg)
4544 const struct got_error *err, *free_err;
4545 struct got_blob_object *blob = NULL;
4546 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4547 int fd = -1, fd2 = -1;
4548 char link_target[PATH_MAX];
4549 ssize_t link_len = 0;
4550 char *path1 = NULL, *id_str = NULL;
4551 struct stat sb2;
4552 struct got_diffreg_result *diffreg_result = NULL;
4553 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4554 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4556 *path_outfile = NULL;
4558 err = got_object_id_str(&id_str, blob_id);
4559 if (err)
4560 return err;
4562 if (dirfd2 != -1) {
4563 fd2 = openat(dirfd2, de_name2,
4564 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4565 if (fd2 == -1) {
4566 if (!got_err_open_nofollow_on_symlink()) {
4567 err = got_error_from_errno2("openat", path2);
4568 goto done;
4570 link_len = readlinkat(dirfd2, de_name2,
4571 link_target, sizeof(link_target));
4572 if (link_len == -1) {
4573 return got_error_from_errno2("readlinkat",
4574 path2);
4576 sb2.st_mode = S_IFLNK;
4577 sb2.st_size = link_len;
4579 } else {
4580 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4581 if (fd2 == -1) {
4582 if (!got_err_open_nofollow_on_symlink()) {
4583 err = got_error_from_errno2("open", path2);
4584 goto done;
4586 link_len = readlink(path2, link_target,
4587 sizeof(link_target));
4588 if (link_len == -1)
4589 return got_error_from_errno2("readlink", path2);
4590 sb2.st_mode = S_IFLNK;
4591 sb2.st_size = link_len;
4594 if (fd2 != -1) {
4595 if (fstat(fd2, &sb2) == -1) {
4596 err = got_error_from_errno2("fstat", path2);
4597 goto done;
4600 f2 = fdopen(fd2, "r");
4601 if (f2 == NULL) {
4602 err = got_error_from_errno2("fdopen", path2);
4603 goto done;
4605 fd2 = -1;
4606 } else {
4607 size_t n;
4608 f2 = got_opentemp();
4609 if (f2 == NULL) {
4610 err = got_error_from_errno2("got_opentemp", path2);
4611 goto done;
4613 n = fwrite(link_target, 1, link_len, f2);
4614 if (n != link_len) {
4615 err = got_ferror(f2, GOT_ERR_IO);
4616 goto done;
4618 if (fflush(f2) == EOF) {
4619 err = got_error_from_errno("fflush");
4620 goto done;
4622 rewind(f2);
4625 fd = got_opentempfd();
4626 if (fd == -1) {
4627 err = got_error_from_errno("got_opentempfd");
4628 goto done;
4631 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4632 if (err)
4633 goto done;
4635 err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4636 if (err)
4637 goto done;
4639 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4640 if (err)
4641 goto done;
4643 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4644 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4645 if (err)
4646 goto done;
4648 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4649 "");
4650 if (err)
4651 goto done;
4653 if (fseek(f1, 0L, SEEK_SET) == -1)
4654 return got_ferror(f1, GOT_ERR_IO);
4655 if (fseek(f2, 0L, SEEK_SET) == -1)
4656 return got_ferror(f2, GOT_ERR_IO);
4658 /* Count the number of actual changes in the diff result. */
4659 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4660 struct diff_chunk_context cc = {};
4661 diff_chunk_context_load_change(&cc, &nchunks_used,
4662 diffreg_result->result, n, 0);
4663 nchanges++;
4665 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4666 int choice;
4667 err = apply_or_reject_change(&choice, &nchunks_used,
4668 diffreg_result->result, n, relpath, f1, f2,
4669 &line_cur1, &line_cur2,
4670 reverse_patch ? NULL : outfile,
4671 reverse_patch ? outfile : NULL,
4672 ++i, nchanges, patch_cb, patch_arg);
4673 if (err)
4674 goto done;
4675 if (choice == GOT_PATCH_CHOICE_YES)
4676 have_content = 1;
4677 else if (choice == GOT_PATCH_CHOICE_QUIT)
4678 break;
4680 if (have_content) {
4681 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4682 reverse_patch ? NULL : outfile,
4683 reverse_patch ? outfile : NULL);
4684 if (err)
4685 goto done;
4687 if (!S_ISLNK(sb2.st_mode)) {
4688 mode_t mode;
4690 mode = apply_umask(sb2.st_mode);
4691 if (fchmod(fileno(outfile), mode) == -1) {
4692 err = got_error_from_errno2("fchmod", path2);
4693 goto done;
4697 done:
4698 free(id_str);
4699 if (fd != -1 && close(fd) == -1 && err == NULL)
4700 err = got_error_from_errno("close");
4701 if (blob)
4702 got_object_blob_close(blob);
4703 free_err = got_diffreg_result_free(diffreg_result);
4704 if (err == NULL)
4705 err = free_err;
4706 if (f1 && fclose(f1) == EOF && err == NULL)
4707 err = got_error_from_errno2("fclose", path1);
4708 if (f2 && fclose(f2) == EOF && err == NULL)
4709 err = got_error_from_errno2("fclose", path2);
4710 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4711 err = got_error_from_errno2("close", path2);
4712 if (outfile && fclose(outfile) == EOF && err == NULL)
4713 err = got_error_from_errno2("fclose", *path_outfile);
4714 if (path1 && unlink(path1) == -1 && err == NULL)
4715 err = got_error_from_errno2("unlink", path1);
4716 if (err || !have_content) {
4717 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4718 err = got_error_from_errno2("unlink", *path_outfile);
4719 free(*path_outfile);
4720 *path_outfile = NULL;
4722 free(path1);
4723 return err;
4726 static const struct got_error *
4727 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4728 const char *relpath, struct got_object_id *blob_id,
4729 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4730 int dirfd, const char *de_name)
4732 struct revert_file_args *a = arg;
4733 const struct got_error *err = NULL;
4734 char *parent_path = NULL;
4735 struct got_fileindex_entry *ie;
4736 struct got_commit_object *base_commit = NULL;
4737 struct got_tree_object *tree = NULL;
4738 struct got_object_id *tree_id = NULL;
4739 const struct got_tree_entry *te = NULL;
4740 char *tree_path = NULL, *te_name;
4741 char *ondisk_path = NULL, *path_content = NULL;
4742 struct got_blob_object *blob = NULL;
4743 int fd = -1;
4745 /* Reverting a staged deletion is a no-op. */
4746 if (status == GOT_STATUS_DELETE &&
4747 staged_status != GOT_STATUS_NO_CHANGE)
4748 return NULL;
4750 if (status == GOT_STATUS_UNVERSIONED)
4751 return (*a->progress_cb)(a->progress_arg,
4752 GOT_STATUS_UNVERSIONED, relpath);
4754 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4755 if (ie == NULL)
4756 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4758 /* Construct in-repository path of tree which contains this blob. */
4759 err = got_path_dirname(&parent_path, ie->path);
4760 if (err) {
4761 if (err->code != GOT_ERR_BAD_PATH)
4762 goto done;
4763 parent_path = strdup("/");
4764 if (parent_path == NULL) {
4765 err = got_error_from_errno("strdup");
4766 goto done;
4769 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4770 tree_path = strdup(parent_path);
4771 if (tree_path == NULL) {
4772 err = got_error_from_errno("strdup");
4773 goto done;
4775 } else {
4776 if (got_path_is_root_dir(parent_path)) {
4777 tree_path = strdup(a->worktree->path_prefix);
4778 if (tree_path == NULL) {
4779 err = got_error_from_errno("strdup");
4780 goto done;
4782 } else {
4783 if (asprintf(&tree_path, "%s/%s",
4784 a->worktree->path_prefix, parent_path) == -1) {
4785 err = got_error_from_errno("asprintf");
4786 goto done;
4791 err = got_object_open_as_commit(&base_commit, a->repo,
4792 a->worktree->base_commit_id);
4793 if (err)
4794 goto done;
4796 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4797 if (err) {
4798 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4799 (status == GOT_STATUS_ADD ||
4800 staged_status == GOT_STATUS_ADD)))
4801 goto done;
4802 } else {
4803 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4804 if (err)
4805 goto done;
4807 err = got_path_basename(&te_name, ie->path);
4808 if (err)
4809 goto done;
4811 te = got_object_tree_find_entry(tree, te_name);
4812 free(te_name);
4813 if (te == NULL && status != GOT_STATUS_ADD &&
4814 staged_status != GOT_STATUS_ADD) {
4815 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4816 goto done;
4820 switch (status) {
4821 case GOT_STATUS_ADD:
4822 if (a->patch_cb) {
4823 int choice = GOT_PATCH_CHOICE_NONE;
4824 err = (*a->patch_cb)(&choice, a->patch_arg,
4825 status, ie->path, NULL, 1, 1);
4826 if (err)
4827 goto done;
4828 if (choice != GOT_PATCH_CHOICE_YES)
4829 break;
4831 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4832 ie->path);
4833 if (err)
4834 goto done;
4835 got_fileindex_entry_remove(a->fileindex, ie);
4836 if (a->unlink_added_files) {
4837 if (asprintf(&ondisk_path, "%s/%s",
4838 got_worktree_get_root_path(a->worktree),
4839 relpath) == -1) {
4840 err = got_error_from_errno("asprintf");
4841 goto done;
4843 if (unlink(ondisk_path) == -1) {
4844 err = got_error_from_errno2("unlink",
4845 ondisk_path);
4846 break;
4849 break;
4850 case GOT_STATUS_DELETE:
4851 if (a->patch_cb) {
4852 int choice = GOT_PATCH_CHOICE_NONE;
4853 err = (*a->patch_cb)(&choice, a->patch_arg,
4854 status, ie->path, NULL, 1, 1);
4855 if (err)
4856 goto done;
4857 if (choice != GOT_PATCH_CHOICE_YES)
4858 break;
4860 /* fall through */
4861 case GOT_STATUS_MODIFY:
4862 case GOT_STATUS_MODE_CHANGE:
4863 case GOT_STATUS_CONFLICT:
4864 case GOT_STATUS_MISSING: {
4865 struct got_object_id id;
4866 if (staged_status == GOT_STATUS_ADD ||
4867 staged_status == GOT_STATUS_MODIFY)
4868 got_fileindex_entry_get_staged_blob_id(&id, ie);
4869 else
4870 got_fileindex_entry_get_blob_id(&id, ie);
4871 fd = got_opentempfd();
4872 if (fd == -1) {
4873 err = got_error_from_errno("got_opentempfd");
4874 goto done;
4877 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4878 if (err)
4879 goto done;
4881 if (asprintf(&ondisk_path, "%s/%s",
4882 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4883 err = got_error_from_errno("asprintf");
4884 goto done;
4887 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4888 status == GOT_STATUS_CONFLICT)) {
4889 int is_bad_symlink = 0;
4890 err = create_patched_content(&path_content, 1, &id,
4891 ondisk_path, dirfd, de_name, ie->path, a->repo,
4892 a->patch_cb, a->patch_arg);
4893 if (err || path_content == NULL)
4894 break;
4895 if (te && S_ISLNK(te->mode)) {
4896 if (unlink(path_content) == -1) {
4897 err = got_error_from_errno2("unlink",
4898 path_content);
4899 break;
4901 err = install_symlink(&is_bad_symlink,
4902 a->worktree, ondisk_path, ie->path,
4903 blob, 0, 1, 0, 0, a->repo,
4904 a->progress_cb, a->progress_arg);
4905 } else {
4906 if (rename(path_content, ondisk_path) == -1) {
4907 err = got_error_from_errno3("rename",
4908 path_content, ondisk_path);
4909 goto done;
4912 } else {
4913 int is_bad_symlink = 0;
4914 if (te && S_ISLNK(te->mode)) {
4915 err = install_symlink(&is_bad_symlink,
4916 a->worktree, ondisk_path, ie->path,
4917 blob, 0, 1, 0, 0, a->repo,
4918 a->progress_cb, a->progress_arg);
4919 } else {
4920 err = install_blob(a->worktree, ondisk_path,
4921 ie->path,
4922 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4923 got_fileindex_perms_to_st(ie), blob,
4924 0, 1, 0, 0, a->repo,
4925 a->progress_cb, a->progress_arg);
4927 if (err)
4928 goto done;
4929 if (status == GOT_STATUS_DELETE ||
4930 status == GOT_STATUS_MODE_CHANGE) {
4931 err = got_fileindex_entry_update(ie,
4932 a->worktree->root_fd, relpath,
4933 blob->id.sha1,
4934 a->worktree->base_commit_id->sha1, 1);
4935 if (err)
4936 goto done;
4938 if (is_bad_symlink) {
4939 got_fileindex_entry_filetype_set(ie,
4940 GOT_FILEIDX_MODE_BAD_SYMLINK);
4943 break;
4945 default:
4946 break;
4948 done:
4949 free(ondisk_path);
4950 free(path_content);
4951 free(parent_path);
4952 free(tree_path);
4953 if (fd != -1 && close(fd) == -1 && err == NULL)
4954 err = got_error_from_errno("close");
4955 if (blob)
4956 got_object_blob_close(blob);
4957 if (tree)
4958 got_object_tree_close(tree);
4959 free(tree_id);
4960 if (base_commit)
4961 got_object_commit_close(base_commit);
4962 return err;
4965 const struct got_error *
4966 got_worktree_revert(struct got_worktree *worktree,
4967 struct got_pathlist_head *paths,
4968 got_worktree_checkout_cb progress_cb, void *progress_arg,
4969 got_worktree_patch_cb patch_cb, void *patch_arg,
4970 struct got_repository *repo)
4972 struct got_fileindex *fileindex = NULL;
4973 char *fileindex_path = NULL;
4974 const struct got_error *err = NULL, *unlockerr = NULL;
4975 const struct got_error *sync_err = NULL;
4976 struct got_pathlist_entry *pe;
4977 struct revert_file_args rfa;
4979 err = lock_worktree(worktree, LOCK_EX);
4980 if (err)
4981 return err;
4983 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4984 if (err)
4985 goto done;
4987 rfa.worktree = worktree;
4988 rfa.fileindex = fileindex;
4989 rfa.progress_cb = progress_cb;
4990 rfa.progress_arg = progress_arg;
4991 rfa.patch_cb = patch_cb;
4992 rfa.patch_arg = patch_arg;
4993 rfa.repo = repo;
4994 rfa.unlink_added_files = 0;
4995 TAILQ_FOREACH(pe, paths, entry) {
4996 err = worktree_status(worktree, pe->path, fileindex, repo,
4997 revert_file, &rfa, NULL, NULL, 1, 0);
4998 if (err)
4999 break;
5001 sync_err = sync_fileindex(fileindex, fileindex_path);
5002 if (sync_err && err == NULL)
5003 err = sync_err;
5004 done:
5005 free(fileindex_path);
5006 if (fileindex)
5007 got_fileindex_free(fileindex);
5008 unlockerr = lock_worktree(worktree, LOCK_SH);
5009 if (unlockerr && err == NULL)
5010 err = unlockerr;
5011 return err;
5014 static void
5015 free_commitable(struct got_commitable *ct)
5017 free(ct->path);
5018 free(ct->in_repo_path);
5019 free(ct->ondisk_path);
5020 free(ct->blob_id);
5021 free(ct->base_blob_id);
5022 free(ct->staged_blob_id);
5023 free(ct->base_commit_id);
5024 free(ct);
5027 struct collect_commitables_arg {
5028 struct got_pathlist_head *commitable_paths;
5029 struct got_repository *repo;
5030 struct got_worktree *worktree;
5031 struct got_fileindex *fileindex;
5032 int have_staged_files;
5033 int allow_bad_symlinks;
5034 int diff_header_shown;
5035 int commit_conflicts;
5036 FILE *diff_outfile;
5037 FILE *f1;
5038 FILE *f2;
5042 * Create a file which contains the target path of a symlink so we can feed
5043 * it as content to the diff engine.
5045 static const struct got_error *
5046 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5047 const char *abspath)
5049 const struct got_error *err = NULL;
5050 char target_path[PATH_MAX];
5051 ssize_t target_len, outlen;
5053 *fd = -1;
5055 if (dirfd != -1) {
5056 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5057 if (target_len == -1)
5058 return got_error_from_errno2("readlinkat", abspath);
5059 } else {
5060 target_len = readlink(abspath, target_path, PATH_MAX);
5061 if (target_len == -1)
5062 return got_error_from_errno2("readlink", abspath);
5065 *fd = got_opentempfd();
5066 if (*fd == -1)
5067 return got_error_from_errno("got_opentempfd");
5069 outlen = write(*fd, target_path, target_len);
5070 if (outlen == -1) {
5071 err = got_error_from_errno("got_opentempfd");
5072 goto done;
5075 if (lseek(*fd, 0, SEEK_SET) == -1) {
5076 err = got_error_from_errno2("lseek", abspath);
5077 goto done;
5079 done:
5080 if (err) {
5081 close(*fd);
5082 *fd = -1;
5084 return err;
5087 static const struct got_error *
5088 append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5089 FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5090 int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5092 const struct got_error *err = NULL;
5093 struct got_blob_object *blob1 = NULL;
5094 int fd = -1, fd1 = -1, fd2 = -1;
5095 FILE *ondisk_file = NULL;
5096 char *label1 = NULL;
5097 struct stat sb;
5098 off_t size1 = 0;
5099 int f2_exists = 0;
5100 char *id_str = NULL;
5102 memset(&sb, 0, sizeof(sb));
5104 if (diff_staged) {
5105 if (ct->staged_status != GOT_STATUS_MODIFY &&
5106 ct->staged_status != GOT_STATUS_ADD &&
5107 ct->staged_status != GOT_STATUS_DELETE)
5108 return NULL;
5109 } else {
5110 if (ct->status != GOT_STATUS_MODIFY &&
5111 ct->status != GOT_STATUS_ADD &&
5112 ct->status != GOT_STATUS_DELETE &&
5113 ct->status != GOT_STATUS_CONFLICT)
5114 return NULL;
5117 err = got_opentemp_truncate(f1);
5118 if (err)
5119 return got_error_from_errno("got_opentemp_truncate");
5120 err = got_opentemp_truncate(f2);
5121 if (err)
5122 return got_error_from_errno("got_opentemp_truncate");
5124 if (!*diff_header_shown) {
5125 err = got_object_id_str(&id_str, worktree->base_commit_id);
5126 if (err)
5127 return err;
5128 fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5129 got_worktree_get_root_path(worktree));
5130 fprintf(diff_outfile, "commit - %s\n", id_str);
5131 fprintf(diff_outfile, "path + %s%s\n",
5132 got_worktree_get_root_path(worktree),
5133 diff_staged ? " (staged changes)" : "");
5134 *diff_header_shown = 1;
5137 if (diff_staged) {
5138 const char *label1 = NULL, *label2 = NULL;
5139 switch (ct->staged_status) {
5140 case GOT_STATUS_MODIFY:
5141 label1 = ct->path;
5142 label2 = ct->path;
5143 break;
5144 case GOT_STATUS_ADD:
5145 label2 = ct->path;
5146 break;
5147 case GOT_STATUS_DELETE:
5148 label1 = ct->path;
5149 break;
5150 default:
5151 return got_error(GOT_ERR_FILE_STATUS);
5153 fd1 = got_opentempfd();
5154 if (fd1 == -1) {
5155 err = got_error_from_errno("got_opentempfd");
5156 goto done;
5158 fd2 = got_opentempfd();
5159 if (fd2 == -1) {
5160 err = got_error_from_errno("got_opentempfd");
5161 goto done;
5163 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5164 fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5165 label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5166 NULL, repo, diff_outfile);
5167 goto done;
5170 fd1 = got_opentempfd();
5171 if (fd1 == -1) {
5172 err = got_error_from_errno("got_opentempfd");
5173 goto done;
5176 if (ct->status != GOT_STATUS_ADD) {
5177 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5178 8192, fd1);
5179 if (err)
5180 goto done;
5183 if (ct->status != GOT_STATUS_DELETE) {
5184 if (dirfd != -1) {
5185 fd = openat(dirfd, de_name,
5186 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5187 if (fd == -1) {
5188 if (!got_err_open_nofollow_on_symlink()) {
5189 err = got_error_from_errno2("openat",
5190 ct->ondisk_path);
5191 goto done;
5193 err = get_symlink_target_file(&fd, dirfd,
5194 de_name, ct->ondisk_path);
5195 if (err)
5196 goto done;
5198 } else {
5199 fd = open(ct->ondisk_path,
5200 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5201 if (fd == -1) {
5202 if (!got_err_open_nofollow_on_symlink()) {
5203 err = got_error_from_errno2("open",
5204 ct->ondisk_path);
5205 goto done;
5207 err = get_symlink_target_file(&fd, dirfd,
5208 de_name, ct->ondisk_path);
5209 if (err)
5210 goto done;
5213 if (fstatat(fd, ct->ondisk_path, &sb,
5214 AT_SYMLINK_NOFOLLOW) == -1) {
5215 err = got_error_from_errno2("fstatat", ct->ondisk_path);
5216 goto done;
5218 ondisk_file = fdopen(fd, "r");
5219 if (ondisk_file == NULL) {
5220 err = got_error_from_errno2("fdopen", ct->ondisk_path);
5221 goto done;
5223 fd = -1;
5224 f2_exists = 1;
5227 if (blob1) {
5228 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5229 f1, blob1);
5230 if (err)
5231 goto done;
5234 err = got_diff_blob_file(blob1, f1, size1, label1,
5235 ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5236 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5237 done:
5238 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5239 err = got_error_from_errno("close");
5240 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5241 err = got_error_from_errno("close");
5242 if (blob1)
5243 got_object_blob_close(blob1);
5244 if (fd != -1 && close(fd) == -1 && err == NULL)
5245 err = got_error_from_errno("close");
5246 if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5247 err = got_error_from_errno("fclose");
5248 return err;
5251 static const struct got_error *
5252 collect_commitables(void *arg, unsigned char status,
5253 unsigned char staged_status, const char *relpath,
5254 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5255 struct got_object_id *commit_id, int dirfd, const char *de_name)
5257 struct collect_commitables_arg *a = arg;
5258 const struct got_error *err = NULL;
5259 struct got_commitable *ct = NULL;
5260 struct got_pathlist_entry *new = NULL;
5261 char *parent_path = NULL, *path = NULL;
5262 struct stat sb;
5264 if (a->have_staged_files) {
5265 if (staged_status != GOT_STATUS_MODIFY &&
5266 staged_status != GOT_STATUS_ADD &&
5267 staged_status != GOT_STATUS_DELETE)
5268 return NULL;
5269 } else {
5270 if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5271 printf("C %s\n", relpath);
5272 return got_error(GOT_ERR_COMMIT_CONFLICT);
5275 if (status != GOT_STATUS_MODIFY &&
5276 status != GOT_STATUS_MODE_CHANGE &&
5277 status != GOT_STATUS_ADD &&
5278 status != GOT_STATUS_DELETE &&
5279 status != GOT_STATUS_CONFLICT)
5280 return NULL;
5283 if (asprintf(&path, "/%s", relpath) == -1) {
5284 err = got_error_from_errno("asprintf");
5285 goto done;
5287 if (strcmp(path, "/") == 0) {
5288 parent_path = strdup("");
5289 if (parent_path == NULL)
5290 return got_error_from_errno("strdup");
5291 } else {
5292 err = got_path_dirname(&parent_path, path);
5293 if (err)
5294 return err;
5297 ct = calloc(1, sizeof(*ct));
5298 if (ct == NULL) {
5299 err = got_error_from_errno("calloc");
5300 goto done;
5303 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5304 relpath) == -1) {
5305 err = got_error_from_errno("asprintf");
5306 goto done;
5309 if (staged_status == GOT_STATUS_ADD ||
5310 staged_status == GOT_STATUS_MODIFY) {
5311 struct got_fileindex_entry *ie;
5312 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5313 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5314 case GOT_FILEIDX_MODE_REGULAR_FILE:
5315 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5316 ct->mode = S_IFREG;
5317 break;
5318 case GOT_FILEIDX_MODE_SYMLINK:
5319 ct->mode = S_IFLNK;
5320 break;
5321 default:
5322 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5323 goto done;
5325 ct->mode |= got_fileindex_entry_perms_get(ie);
5326 } else if (status != GOT_STATUS_DELETE &&
5327 staged_status != GOT_STATUS_DELETE) {
5328 if (dirfd != -1) {
5329 if (fstatat(dirfd, de_name, &sb,
5330 AT_SYMLINK_NOFOLLOW) == -1) {
5331 err = got_error_from_errno2("fstatat",
5332 ct->ondisk_path);
5333 goto done;
5335 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5336 err = got_error_from_errno2("lstat", ct->ondisk_path);
5337 goto done;
5339 ct->mode = sb.st_mode;
5342 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5343 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5344 relpath) == -1) {
5345 err = got_error_from_errno("asprintf");
5346 goto done;
5349 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5350 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5351 int is_bad_symlink;
5352 char target_path[PATH_MAX];
5353 ssize_t target_len;
5354 target_len = readlink(ct->ondisk_path, target_path,
5355 sizeof(target_path));
5356 if (target_len == -1) {
5357 err = got_error_from_errno2("readlink",
5358 ct->ondisk_path);
5359 goto done;
5361 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5362 target_len, ct->ondisk_path, a->worktree->root_path);
5363 if (err)
5364 goto done;
5365 if (is_bad_symlink) {
5366 err = got_error_path(ct->ondisk_path,
5367 GOT_ERR_BAD_SYMLINK);
5368 goto done;
5373 ct->status = status;
5374 ct->staged_status = staged_status;
5375 ct->blob_id = NULL; /* will be filled in when blob gets created */
5376 if (ct->status != GOT_STATUS_ADD &&
5377 ct->staged_status != GOT_STATUS_ADD) {
5378 ct->base_blob_id = got_object_id_dup(blob_id);
5379 if (ct->base_blob_id == NULL) {
5380 err = got_error_from_errno("got_object_id_dup");
5381 goto done;
5383 ct->base_commit_id = got_object_id_dup(commit_id);
5384 if (ct->base_commit_id == NULL) {
5385 err = got_error_from_errno("got_object_id_dup");
5386 goto done;
5389 if (ct->staged_status == GOT_STATUS_ADD ||
5390 ct->staged_status == GOT_STATUS_MODIFY) {
5391 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5392 if (ct->staged_blob_id == NULL) {
5393 err = got_error_from_errno("got_object_id_dup");
5394 goto done;
5397 ct->path = strdup(path);
5398 if (ct->path == NULL) {
5399 err = got_error_from_errno("strdup");
5400 goto done;
5402 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5403 if (err)
5404 goto done;
5406 if (a->diff_outfile && ct && new != NULL) {
5407 err = append_ct_diff(ct, &a->diff_header_shown,
5408 a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5409 a->have_staged_files, a->repo, a->worktree);
5410 if (err)
5411 goto done;
5413 done:
5414 if (ct && (err || new == NULL))
5415 free_commitable(ct);
5416 free(parent_path);
5417 free(path);
5418 return err;
5421 static const struct got_error *write_tree(struct got_object_id **, int *,
5422 struct got_tree_object *, const char *, struct got_pathlist_head *,
5423 got_worktree_status_cb status_cb, void *status_arg,
5424 struct got_repository *);
5426 static const struct got_error *
5427 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5428 struct got_tree_entry *te, const char *parent_path,
5429 struct got_pathlist_head *commitable_paths,
5430 got_worktree_status_cb status_cb, void *status_arg,
5431 struct got_repository *repo)
5433 const struct got_error *err = NULL;
5434 struct got_tree_object *subtree;
5435 char *subpath;
5437 if (asprintf(&subpath, "%s%s%s", parent_path,
5438 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5439 return got_error_from_errno("asprintf");
5441 err = got_object_open_as_tree(&subtree, repo, &te->id);
5442 if (err)
5443 return err;
5445 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5446 commitable_paths, status_cb, status_arg, repo);
5447 got_object_tree_close(subtree);
5448 free(subpath);
5449 return err;
5452 static const struct got_error *
5453 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5455 const struct got_error *err = NULL;
5456 char *ct_parent_path = NULL;
5458 *match = 0;
5460 if (strchr(ct->in_repo_path, '/') == NULL) {
5461 *match = got_path_is_root_dir(path);
5462 return NULL;
5465 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5466 if (err)
5467 return err;
5468 *match = (strcmp(path, ct_parent_path) == 0);
5469 free(ct_parent_path);
5470 return err;
5473 static mode_t
5474 get_ct_file_mode(struct got_commitable *ct)
5476 if (S_ISLNK(ct->mode))
5477 return S_IFLNK;
5479 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5482 static const struct got_error *
5483 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5484 struct got_tree_entry *te, struct got_commitable *ct)
5486 const struct got_error *err = NULL;
5488 *new_te = NULL;
5490 err = got_object_tree_entry_dup(new_te, te);
5491 if (err)
5492 goto done;
5494 (*new_te)->mode = get_ct_file_mode(ct);
5496 if (ct->staged_status == GOT_STATUS_MODIFY)
5497 memcpy(&(*new_te)->id, ct->staged_blob_id,
5498 sizeof((*new_te)->id));
5499 else
5500 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5501 done:
5502 if (err && *new_te) {
5503 free(*new_te);
5504 *new_te = NULL;
5506 return err;
5509 static const struct got_error *
5510 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5511 struct got_commitable *ct)
5513 const struct got_error *err = NULL;
5514 char *ct_name = NULL;
5516 *new_te = NULL;
5518 *new_te = calloc(1, sizeof(**new_te));
5519 if (*new_te == NULL)
5520 return got_error_from_errno("calloc");
5522 err = got_path_basename(&ct_name, ct->path);
5523 if (err)
5524 goto done;
5525 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5526 sizeof((*new_te)->name)) {
5527 err = got_error(GOT_ERR_NO_SPACE);
5528 goto done;
5531 (*new_te)->mode = get_ct_file_mode(ct);
5533 if (ct->staged_status == GOT_STATUS_ADD)
5534 memcpy(&(*new_te)->id, ct->staged_blob_id,
5535 sizeof((*new_te)->id));
5536 else
5537 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5538 done:
5539 free(ct_name);
5540 if (err && *new_te) {
5541 free(*new_te);
5542 *new_te = NULL;
5544 return err;
5547 static const struct got_error *
5548 insert_tree_entry(struct got_tree_entry *new_te,
5549 struct got_pathlist_head *paths)
5551 const struct got_error *err = NULL;
5552 struct got_pathlist_entry *new_pe;
5554 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5555 if (err)
5556 return err;
5557 if (new_pe == NULL)
5558 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5559 return NULL;
5562 static const struct got_error *
5563 report_ct_status(struct got_commitable *ct,
5564 got_worktree_status_cb status_cb, void *status_arg)
5566 const char *ct_path = ct->path;
5567 unsigned char status;
5569 if (status_cb == NULL) /* no commit progress output desired */
5570 return NULL;
5572 while (ct_path[0] == '/')
5573 ct_path++;
5575 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5576 status = ct->staged_status;
5577 else
5578 status = ct->status;
5580 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5581 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5584 static const struct got_error *
5585 match_modified_subtree(int *modified, struct got_tree_entry *te,
5586 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5588 const struct got_error *err = NULL;
5589 struct got_pathlist_entry *pe;
5590 char *te_path;
5592 *modified = 0;
5594 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5595 got_path_is_root_dir(base_tree_path) ? "" : "/",
5596 te->name) == -1)
5597 return got_error_from_errno("asprintf");
5599 TAILQ_FOREACH(pe, commitable_paths, entry) {
5600 struct got_commitable *ct = pe->data;
5601 *modified = got_path_is_child(ct->in_repo_path, te_path,
5602 strlen(te_path));
5603 if (*modified)
5604 break;
5607 free(te_path);
5608 return err;
5611 static const struct got_error *
5612 match_deleted_or_modified_ct(struct got_commitable **ctp,
5613 struct got_tree_entry *te, const char *base_tree_path,
5614 struct got_pathlist_head *commitable_paths)
5616 const struct got_error *err = NULL;
5617 struct got_pathlist_entry *pe;
5619 *ctp = NULL;
5621 TAILQ_FOREACH(pe, commitable_paths, entry) {
5622 struct got_commitable *ct = pe->data;
5623 char *ct_name = NULL;
5624 int path_matches;
5626 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5627 if (ct->status != GOT_STATUS_MODIFY &&
5628 ct->status != GOT_STATUS_MODE_CHANGE &&
5629 ct->status != GOT_STATUS_DELETE &&
5630 ct->status != GOT_STATUS_CONFLICT)
5631 continue;
5632 } else {
5633 if (ct->staged_status != GOT_STATUS_MODIFY &&
5634 ct->staged_status != GOT_STATUS_DELETE)
5635 continue;
5638 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5639 continue;
5641 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5642 if (err)
5643 return err;
5644 if (!path_matches)
5645 continue;
5647 err = got_path_basename(&ct_name, pe->path);
5648 if (err)
5649 return err;
5651 if (strcmp(te->name, ct_name) != 0) {
5652 free(ct_name);
5653 continue;
5655 free(ct_name);
5657 *ctp = ct;
5658 break;
5661 return err;
5664 static const struct got_error *
5665 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5666 const char *child_path, const char *path_base_tree,
5667 struct got_pathlist_head *commitable_paths,
5668 got_worktree_status_cb status_cb, void *status_arg,
5669 struct got_repository *repo)
5671 const struct got_error *err = NULL;
5672 struct got_tree_entry *new_te;
5673 char *subtree_path;
5674 struct got_object_id *id = NULL;
5675 int nentries;
5677 *new_tep = NULL;
5679 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5680 got_path_is_root_dir(path_base_tree) ? "" : "/",
5681 child_path) == -1)
5682 return got_error_from_errno("asprintf");
5684 new_te = calloc(1, sizeof(*new_te));
5685 if (new_te == NULL)
5686 return got_error_from_errno("calloc");
5687 new_te->mode = S_IFDIR;
5689 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5690 sizeof(new_te->name)) {
5691 err = got_error(GOT_ERR_NO_SPACE);
5692 goto done;
5694 err = write_tree(&id, &nentries, NULL, subtree_path,
5695 commitable_paths, status_cb, status_arg, repo);
5696 if (err) {
5697 free(new_te);
5698 goto done;
5700 memcpy(&new_te->id, id, sizeof(new_te->id));
5701 done:
5702 free(id);
5703 free(subtree_path);
5704 if (err == NULL)
5705 *new_tep = new_te;
5706 return err;
5709 static const struct got_error *
5710 write_tree(struct got_object_id **new_tree_id, int *nentries,
5711 struct got_tree_object *base_tree, const char *path_base_tree,
5712 struct got_pathlist_head *commitable_paths,
5713 got_worktree_status_cb status_cb, void *status_arg,
5714 struct got_repository *repo)
5716 const struct got_error *err = NULL;
5717 struct got_pathlist_head paths;
5718 struct got_tree_entry *te, *new_te = NULL;
5719 struct got_pathlist_entry *pe;
5721 TAILQ_INIT(&paths);
5722 *nentries = 0;
5724 /* Insert, and recurse into, newly added entries first. */
5725 TAILQ_FOREACH(pe, commitable_paths, entry) {
5726 struct got_commitable *ct = pe->data;
5727 char *child_path = NULL, *slash;
5729 if ((ct->status != GOT_STATUS_ADD &&
5730 ct->staged_status != GOT_STATUS_ADD) ||
5731 (ct->flags & GOT_COMMITABLE_ADDED))
5732 continue;
5734 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5735 strlen(path_base_tree)))
5736 continue;
5738 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5739 ct->in_repo_path);
5740 if (err)
5741 goto done;
5743 slash = strchr(child_path, '/');
5744 if (slash == NULL) {
5745 err = alloc_added_blob_tree_entry(&new_te, ct);
5746 if (err)
5747 goto done;
5748 err = report_ct_status(ct, status_cb, status_arg);
5749 if (err)
5750 goto done;
5751 ct->flags |= GOT_COMMITABLE_ADDED;
5752 err = insert_tree_entry(new_te, &paths);
5753 if (err)
5754 goto done;
5755 (*nentries)++;
5756 } else {
5757 *slash = '\0'; /* trim trailing path components */
5758 if (base_tree == NULL ||
5759 got_object_tree_find_entry(base_tree, child_path)
5760 == NULL) {
5761 err = make_subtree_for_added_blob(&new_te,
5762 child_path, path_base_tree,
5763 commitable_paths, status_cb, status_arg,
5764 repo);
5765 if (err)
5766 goto done;
5767 err = insert_tree_entry(new_te, &paths);
5768 if (err)
5769 goto done;
5770 (*nentries)++;
5775 if (base_tree) {
5776 int i, nbase_entries;
5777 /* Handle modified and deleted entries. */
5778 nbase_entries = got_object_tree_get_nentries(base_tree);
5779 for (i = 0; i < nbase_entries; i++) {
5780 struct got_commitable *ct = NULL;
5782 te = got_object_tree_get_entry(base_tree, i);
5783 if (got_object_tree_entry_is_submodule(te)) {
5784 /* Entry is a submodule; just copy it. */
5785 err = got_object_tree_entry_dup(&new_te, te);
5786 if (err)
5787 goto done;
5788 err = insert_tree_entry(new_te, &paths);
5789 if (err)
5790 goto done;
5791 (*nentries)++;
5792 continue;
5795 if (S_ISDIR(te->mode)) {
5796 int modified;
5797 err = got_object_tree_entry_dup(&new_te, te);
5798 if (err)
5799 goto done;
5800 err = match_modified_subtree(&modified, te,
5801 path_base_tree, commitable_paths);
5802 if (err)
5803 goto done;
5804 /* Avoid recursion into unmodified subtrees. */
5805 if (modified) {
5806 struct got_object_id *new_id;
5807 int nsubentries;
5808 err = write_subtree(&new_id,
5809 &nsubentries, te,
5810 path_base_tree, commitable_paths,
5811 status_cb, status_arg, repo);
5812 if (err)
5813 goto done;
5814 if (nsubentries == 0) {
5815 /* All entries were deleted. */
5816 free(new_id);
5817 continue;
5819 memcpy(&new_te->id, new_id,
5820 sizeof(new_te->id));
5821 free(new_id);
5823 err = insert_tree_entry(new_te, &paths);
5824 if (err)
5825 goto done;
5826 (*nentries)++;
5827 continue;
5830 err = match_deleted_or_modified_ct(&ct, te,
5831 path_base_tree, commitable_paths);
5832 if (err)
5833 goto done;
5834 if (ct) {
5835 /* NB: Deleted entries get dropped here. */
5836 if (ct->status == GOT_STATUS_MODIFY ||
5837 ct->status == GOT_STATUS_MODE_CHANGE ||
5838 ct->status == GOT_STATUS_CONFLICT ||
5839 ct->staged_status == GOT_STATUS_MODIFY) {
5840 err = alloc_modified_blob_tree_entry(
5841 &new_te, te, ct);
5842 if (err)
5843 goto done;
5844 err = insert_tree_entry(new_te, &paths);
5845 if (err)
5846 goto done;
5847 (*nentries)++;
5849 err = report_ct_status(ct, status_cb,
5850 status_arg);
5851 if (err)
5852 goto done;
5853 } else {
5854 /* Entry is unchanged; just copy it. */
5855 err = got_object_tree_entry_dup(&new_te, te);
5856 if (err)
5857 goto done;
5858 err = insert_tree_entry(new_te, &paths);
5859 if (err)
5860 goto done;
5861 (*nentries)++;
5866 /* Write new list of entries; deleted entries have been dropped. */
5867 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5868 done:
5869 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
5870 return err;
5873 static const struct got_error *
5874 update_fileindex_after_commit(struct got_worktree *worktree,
5875 struct got_pathlist_head *commitable_paths,
5876 struct got_object_id *new_base_commit_id,
5877 struct got_fileindex *fileindex, int have_staged_files)
5879 const struct got_error *err = NULL;
5880 struct got_pathlist_entry *pe;
5881 char *relpath = NULL;
5883 TAILQ_FOREACH(pe, commitable_paths, entry) {
5884 struct got_fileindex_entry *ie;
5885 struct got_commitable *ct = pe->data;
5887 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5889 err = got_path_skip_common_ancestor(&relpath,
5890 worktree->root_path, ct->ondisk_path);
5891 if (err)
5892 goto done;
5894 if (ie) {
5895 if (ct->status == GOT_STATUS_DELETE ||
5896 ct->staged_status == GOT_STATUS_DELETE) {
5897 got_fileindex_entry_remove(fileindex, ie);
5898 } else if (ct->staged_status == GOT_STATUS_ADD ||
5899 ct->staged_status == GOT_STATUS_MODIFY) {
5900 got_fileindex_entry_stage_set(ie,
5901 GOT_FILEIDX_STAGE_NONE);
5902 got_fileindex_entry_staged_filetype_set(ie, 0);
5904 err = got_fileindex_entry_update(ie,
5905 worktree->root_fd, relpath,
5906 ct->staged_blob_id->sha1,
5907 new_base_commit_id->sha1,
5908 !have_staged_files);
5909 } else
5910 err = got_fileindex_entry_update(ie,
5911 worktree->root_fd, relpath,
5912 ct->blob_id->sha1,
5913 new_base_commit_id->sha1,
5914 !have_staged_files);
5915 } else {
5916 err = got_fileindex_entry_alloc(&ie, pe->path);
5917 if (err)
5918 goto done;
5919 err = got_fileindex_entry_update(ie,
5920 worktree->root_fd, relpath, ct->blob_id->sha1,
5921 new_base_commit_id->sha1, 1);
5922 if (err) {
5923 got_fileindex_entry_free(ie);
5924 goto done;
5926 err = got_fileindex_entry_add(fileindex, ie);
5927 if (err) {
5928 got_fileindex_entry_free(ie);
5929 goto done;
5932 free(relpath);
5933 relpath = NULL;
5935 done:
5936 free(relpath);
5937 return err;
5941 static const struct got_error *
5942 check_out_of_date(const char *in_repo_path, unsigned char status,
5943 unsigned char staged_status, struct got_object_id *base_blob_id,
5944 struct got_object_id *base_commit_id,
5945 struct got_object_id *head_commit_id, struct got_repository *repo,
5946 int ood_errcode)
5948 const struct got_error *err = NULL;
5949 struct got_commit_object *commit = NULL;
5950 struct got_object_id *id = NULL;
5952 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5953 /* Trivial case: base commit == head commit */
5954 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5955 return NULL;
5957 * Ensure file content which local changes were based
5958 * on matches file content in the branch head.
5960 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5961 if (err)
5962 goto done;
5963 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5964 if (err) {
5965 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5966 err = got_error(ood_errcode);
5967 goto done;
5968 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5969 err = got_error(ood_errcode);
5970 } else {
5971 /* Require that added files don't exist in the branch head. */
5972 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5973 if (err)
5974 goto done;
5975 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5976 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5977 goto done;
5978 err = id ? got_error(ood_errcode) : NULL;
5980 done:
5981 free(id);
5982 if (commit)
5983 got_object_commit_close(commit);
5984 return err;
5987 static const struct got_error *
5988 commit_worktree(struct got_object_id **new_commit_id,
5989 struct got_pathlist_head *commitable_paths,
5990 struct got_object_id *head_commit_id,
5991 struct got_object_id *parent_id2,
5992 struct got_worktree *worktree,
5993 const char *author, const char *committer, char *diff_path,
5994 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5995 got_worktree_status_cb status_cb, void *status_arg,
5996 struct got_repository *repo)
5998 const struct got_error *err = NULL, *unlockerr = NULL;
5999 struct got_pathlist_entry *pe;
6000 const char *head_ref_name = NULL;
6001 struct got_commit_object *head_commit = NULL;
6002 struct got_reference *head_ref2 = NULL;
6003 struct got_object_id *head_commit_id2 = NULL;
6004 struct got_tree_object *head_tree = NULL;
6005 struct got_object_id *new_tree_id = NULL;
6006 int nentries, nparents = 0;
6007 struct got_object_id_queue parent_ids;
6008 struct got_object_qid *pid = NULL;
6009 char *logmsg = NULL;
6010 time_t timestamp;
6012 *new_commit_id = NULL;
6014 STAILQ_INIT(&parent_ids);
6016 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6017 if (err)
6018 goto done;
6020 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6021 if (err)
6022 goto done;
6024 if (commit_msg_cb != NULL) {
6025 err = commit_msg_cb(commitable_paths, diff_path,
6026 &logmsg, commit_arg);
6027 if (err)
6028 goto done;
6031 if (logmsg == NULL || strlen(logmsg) == 0) {
6032 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6033 goto done;
6036 /* Create blobs from added and modified files and record their IDs. */
6037 TAILQ_FOREACH(pe, commitable_paths, entry) {
6038 struct got_commitable *ct = pe->data;
6039 char *ondisk_path;
6041 /* Blobs for staged files already exist. */
6042 if (ct->staged_status == GOT_STATUS_ADD ||
6043 ct->staged_status == GOT_STATUS_MODIFY)
6044 continue;
6046 if (ct->status != GOT_STATUS_ADD &&
6047 ct->status != GOT_STATUS_MODIFY &&
6048 ct->status != GOT_STATUS_MODE_CHANGE &&
6049 ct->status != GOT_STATUS_CONFLICT)
6050 continue;
6052 if (asprintf(&ondisk_path, "%s/%s",
6053 worktree->root_path, pe->path) == -1) {
6054 err = got_error_from_errno("asprintf");
6055 goto done;
6057 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6058 free(ondisk_path);
6059 if (err)
6060 goto done;
6063 /* Recursively write new tree objects. */
6064 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6065 commitable_paths, status_cb, status_arg, repo);
6066 if (err)
6067 goto done;
6069 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
6070 if (err)
6071 goto done;
6072 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6073 nparents++;
6074 if (parent_id2) {
6075 err = got_object_qid_alloc(&pid, parent_id2);
6076 if (err)
6077 goto done;
6078 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6079 nparents++;
6081 timestamp = time(NULL);
6082 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6083 nparents, author, timestamp, committer, timestamp, logmsg, repo);
6084 if (logmsg != NULL)
6085 free(logmsg);
6086 if (err)
6087 goto done;
6089 /* Check if a concurrent commit to our branch has occurred. */
6090 head_ref_name = got_worktree_get_head_ref_name(worktree);
6091 if (head_ref_name == NULL) {
6092 err = got_error_from_errno("got_worktree_get_head_ref_name");
6093 goto done;
6095 /* Lock the reference here to prevent concurrent modification. */
6096 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6097 if (err)
6098 goto done;
6099 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6100 if (err)
6101 goto done;
6102 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6103 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6104 goto done;
6106 /* Update branch head in repository. */
6107 err = got_ref_change_ref(head_ref2, *new_commit_id);
6108 if (err)
6109 goto done;
6110 err = got_ref_write(head_ref2, repo);
6111 if (err)
6112 goto done;
6114 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6115 if (err)
6116 goto done;
6118 err = ref_base_commit(worktree, repo);
6119 if (err)
6120 goto done;
6121 done:
6122 got_object_id_queue_free(&parent_ids);
6123 if (head_tree)
6124 got_object_tree_close(head_tree);
6125 if (head_commit)
6126 got_object_commit_close(head_commit);
6127 free(head_commit_id2);
6128 if (head_ref2) {
6129 unlockerr = got_ref_unlock(head_ref2);
6130 if (unlockerr && err == NULL)
6131 err = unlockerr;
6132 got_ref_close(head_ref2);
6134 return err;
6137 static const struct got_error *
6138 check_path_is_commitable(const char *path,
6139 struct got_pathlist_head *commitable_paths)
6141 struct got_pathlist_entry *cpe = NULL;
6142 size_t path_len = strlen(path);
6144 TAILQ_FOREACH(cpe, commitable_paths, entry) {
6145 struct got_commitable *ct = cpe->data;
6146 const char *ct_path = ct->path;
6148 while (ct_path[0] == '/')
6149 ct_path++;
6151 if (strcmp(path, ct_path) == 0 ||
6152 got_path_is_child(ct_path, path, path_len))
6153 break;
6156 if (cpe == NULL)
6157 return got_error_path(path, GOT_ERR_BAD_PATH);
6159 return NULL;
6162 static const struct got_error *
6163 check_staged_file(void *arg, struct got_fileindex_entry *ie)
6165 int *have_staged_files = arg;
6167 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6168 *have_staged_files = 1;
6169 return got_error(GOT_ERR_CANCELLED);
6172 return NULL;
6175 static const struct got_error *
6176 check_non_staged_files(struct got_fileindex *fileindex,
6177 struct got_pathlist_head *paths)
6179 struct got_pathlist_entry *pe;
6180 struct got_fileindex_entry *ie;
6182 TAILQ_FOREACH(pe, paths, entry) {
6183 if (pe->path[0] == '\0')
6184 continue;
6185 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6186 if (ie == NULL)
6187 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6188 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6189 return got_error_path(pe->path,
6190 GOT_ERR_FILE_NOT_STAGED);
6193 return NULL;
6196 const struct got_error *
6197 got_worktree_commit(struct got_object_id **new_commit_id,
6198 struct got_worktree *worktree, struct got_pathlist_head *paths,
6199 const char *author, const char *committer, int allow_bad_symlinks,
6200 int show_diff, int commit_conflicts,
6201 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6202 got_worktree_status_cb status_cb, void *status_arg,
6203 struct got_repository *repo)
6205 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6206 struct got_fileindex *fileindex = NULL;
6207 char *fileindex_path = NULL;
6208 struct got_pathlist_head commitable_paths;
6209 struct collect_commitables_arg cc_arg;
6210 struct got_pathlist_entry *pe;
6211 struct got_reference *head_ref = NULL;
6212 struct got_object_id *head_commit_id = NULL;
6213 char *diff_path = NULL;
6214 int have_staged_files = 0;
6216 *new_commit_id = NULL;
6218 memset(&cc_arg, 0, sizeof(cc_arg));
6219 TAILQ_INIT(&commitable_paths);
6221 err = lock_worktree(worktree, LOCK_EX);
6222 if (err)
6223 goto done;
6225 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6226 if (err)
6227 goto done;
6229 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6230 if (err)
6231 goto done;
6233 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6234 if (err)
6235 goto done;
6237 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6238 &have_staged_files);
6239 if (err && err->code != GOT_ERR_CANCELLED)
6240 goto done;
6241 if (have_staged_files) {
6242 err = check_non_staged_files(fileindex, paths);
6243 if (err)
6244 goto done;
6247 cc_arg.commitable_paths = &commitable_paths;
6248 cc_arg.worktree = worktree;
6249 cc_arg.fileindex = fileindex;
6250 cc_arg.repo = repo;
6251 cc_arg.have_staged_files = have_staged_files;
6252 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6253 cc_arg.diff_header_shown = 0;
6254 cc_arg.commit_conflicts = commit_conflicts;
6255 if (show_diff) {
6256 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6257 GOT_TMPDIR_STR "/got", ".diff");
6258 if (err)
6259 goto done;
6260 cc_arg.f1 = got_opentemp();
6261 if (cc_arg.f1 == NULL) {
6262 err = got_error_from_errno("got_opentemp");
6263 goto done;
6265 cc_arg.f2 = got_opentemp();
6266 if (cc_arg.f2 == NULL) {
6267 err = got_error_from_errno("got_opentemp");
6268 goto done;
6272 TAILQ_FOREACH(pe, paths, entry) {
6273 err = worktree_status(worktree, pe->path, fileindex, repo,
6274 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6275 if (err)
6276 goto done;
6279 if (show_diff) {
6280 if (fflush(cc_arg.diff_outfile) == EOF) {
6281 err = got_error_from_errno("fflush");
6282 goto done;
6286 if (TAILQ_EMPTY(&commitable_paths)) {
6287 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6288 goto done;
6291 TAILQ_FOREACH(pe, paths, entry) {
6292 err = check_path_is_commitable(pe->path, &commitable_paths);
6293 if (err)
6294 goto done;
6297 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6298 struct got_commitable *ct = pe->data;
6299 const char *ct_path = ct->in_repo_path;
6301 while (ct_path[0] == '/')
6302 ct_path++;
6303 err = check_out_of_date(ct_path, ct->status,
6304 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6305 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6306 if (err)
6307 goto done;
6311 err = commit_worktree(new_commit_id, &commitable_paths,
6312 head_commit_id, NULL, worktree, author, committer,
6313 (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6314 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6315 if (err)
6316 goto done;
6318 err = update_fileindex_after_commit(worktree, &commitable_paths,
6319 *new_commit_id, fileindex, have_staged_files);
6320 sync_err = sync_fileindex(fileindex, fileindex_path);
6321 if (sync_err && err == NULL)
6322 err = sync_err;
6323 done:
6324 if (fileindex)
6325 got_fileindex_free(fileindex);
6326 free(fileindex_path);
6327 unlockerr = lock_worktree(worktree, LOCK_SH);
6328 if (unlockerr && err == NULL)
6329 err = unlockerr;
6330 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6331 struct got_commitable *ct = pe->data;
6333 free_commitable(ct);
6335 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6336 if (diff_path && unlink(diff_path) == -1 && err == NULL)
6337 err = got_error_from_errno2("unlink", diff_path);
6338 free(diff_path);
6339 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6340 err == NULL)
6341 err = got_error_from_errno("fclose");
6342 return err;
6345 const char *
6346 got_commitable_get_path(struct got_commitable *ct)
6348 return ct->path;
6351 unsigned int
6352 got_commitable_get_status(struct got_commitable *ct)
6354 return ct->status;
6357 struct check_rebase_ok_arg {
6358 struct got_worktree *worktree;
6359 struct got_repository *repo;
6362 static const struct got_error *
6363 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6365 const struct got_error *err = NULL;
6366 struct check_rebase_ok_arg *a = arg;
6367 unsigned char status;
6368 struct stat sb;
6369 char *ondisk_path;
6371 /* Reject rebase of a work tree with mixed base commits. */
6372 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6373 SHA1_DIGEST_LENGTH))
6374 return got_error(GOT_ERR_MIXED_COMMITS);
6376 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6377 == -1)
6378 return got_error_from_errno("asprintf");
6380 /* Reject rebase of a work tree with modified or staged files. */
6381 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6382 free(ondisk_path);
6383 if (err)
6384 return err;
6386 if (status != GOT_STATUS_NO_CHANGE)
6387 return got_error(GOT_ERR_MODIFIED);
6388 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6389 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6391 return NULL;
6394 const struct got_error *
6395 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6396 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6397 struct got_worktree *worktree, struct got_reference *branch,
6398 struct got_repository *repo)
6400 const struct got_error *err = NULL;
6401 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6402 char *branch_ref_name = NULL;
6403 char *fileindex_path = NULL;
6404 struct check_rebase_ok_arg ok_arg;
6405 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6406 struct got_object_id *wt_branch_tip = NULL;
6408 *new_base_branch_ref = NULL;
6409 *tmp_branch = NULL;
6410 *fileindex = NULL;
6412 err = lock_worktree(worktree, LOCK_EX);
6413 if (err)
6414 return err;
6416 err = open_fileindex(fileindex, &fileindex_path, worktree);
6417 if (err)
6418 goto done;
6420 ok_arg.worktree = worktree;
6421 ok_arg.repo = repo;
6422 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6423 &ok_arg);
6424 if (err)
6425 goto done;
6427 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6428 if (err)
6429 goto done;
6431 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6432 if (err)
6433 goto done;
6435 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6436 if (err)
6437 goto done;
6439 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6440 0);
6441 if (err)
6442 goto done;
6444 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6445 if (err)
6446 goto done;
6447 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6448 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6449 goto done;
6452 err = got_ref_alloc_symref(new_base_branch_ref,
6453 new_base_branch_ref_name, wt_branch);
6454 if (err)
6455 goto done;
6456 err = got_ref_write(*new_base_branch_ref, repo);
6457 if (err)
6458 goto done;
6460 /* TODO Lock original branch's ref while rebasing? */
6462 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6463 if (err)
6464 goto done;
6466 err = got_ref_write(branch_ref, repo);
6467 if (err)
6468 goto done;
6470 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6471 worktree->base_commit_id);
6472 if (err)
6473 goto done;
6474 err = got_ref_write(*tmp_branch, repo);
6475 if (err)
6476 goto done;
6478 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6479 if (err)
6480 goto done;
6481 done:
6482 free(fileindex_path);
6483 free(tmp_branch_name);
6484 free(new_base_branch_ref_name);
6485 free(branch_ref_name);
6486 if (branch_ref)
6487 got_ref_close(branch_ref);
6488 if (wt_branch)
6489 got_ref_close(wt_branch);
6490 free(wt_branch_tip);
6491 if (err) {
6492 if (*new_base_branch_ref) {
6493 got_ref_close(*new_base_branch_ref);
6494 *new_base_branch_ref = NULL;
6496 if (*tmp_branch) {
6497 got_ref_close(*tmp_branch);
6498 *tmp_branch = NULL;
6500 if (*fileindex) {
6501 got_fileindex_free(*fileindex);
6502 *fileindex = NULL;
6504 lock_worktree(worktree, LOCK_SH);
6506 return err;
6509 const struct got_error *
6510 got_worktree_rebase_continue(struct got_object_id **commit_id,
6511 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6512 struct got_reference **branch, struct got_fileindex **fileindex,
6513 struct got_worktree *worktree, struct got_repository *repo)
6515 const struct got_error *err;
6516 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6517 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6518 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6519 char *fileindex_path = NULL;
6520 int have_staged_files = 0;
6522 *commit_id = NULL;
6523 *new_base_branch = NULL;
6524 *tmp_branch = NULL;
6525 *branch = NULL;
6526 *fileindex = NULL;
6528 err = lock_worktree(worktree, LOCK_EX);
6529 if (err)
6530 return err;
6532 err = open_fileindex(fileindex, &fileindex_path, worktree);
6533 if (err)
6534 goto done;
6536 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6537 &have_staged_files);
6538 if (err && err->code != GOT_ERR_CANCELLED)
6539 goto done;
6540 if (have_staged_files) {
6541 err = got_error(GOT_ERR_STAGED_PATHS);
6542 goto done;
6545 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6546 if (err)
6547 goto done;
6549 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6550 if (err)
6551 goto done;
6553 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6554 if (err)
6555 goto done;
6557 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6558 if (err)
6559 goto done;
6561 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6562 if (err)
6563 goto done;
6565 err = got_ref_open(branch, repo,
6566 got_ref_get_symref_target(branch_ref), 0);
6567 if (err)
6568 goto done;
6570 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6571 if (err)
6572 goto done;
6574 err = got_ref_resolve(commit_id, repo, commit_ref);
6575 if (err)
6576 goto done;
6578 err = got_ref_open(new_base_branch, repo,
6579 new_base_branch_ref_name, 0);
6580 if (err)
6581 goto done;
6583 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6584 if (err)
6585 goto done;
6586 done:
6587 free(commit_ref_name);
6588 free(branch_ref_name);
6589 free(fileindex_path);
6590 if (commit_ref)
6591 got_ref_close(commit_ref);
6592 if (branch_ref)
6593 got_ref_close(branch_ref);
6594 if (err) {
6595 free(*commit_id);
6596 *commit_id = NULL;
6597 if (*tmp_branch) {
6598 got_ref_close(*tmp_branch);
6599 *tmp_branch = NULL;
6601 if (*new_base_branch) {
6602 got_ref_close(*new_base_branch);
6603 *new_base_branch = NULL;
6605 if (*branch) {
6606 got_ref_close(*branch);
6607 *branch = NULL;
6609 if (*fileindex) {
6610 got_fileindex_free(*fileindex);
6611 *fileindex = NULL;
6613 lock_worktree(worktree, LOCK_SH);
6615 return err;
6618 const struct got_error *
6619 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6621 const struct got_error *err;
6622 char *tmp_branch_name = NULL;
6624 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6625 if (err)
6626 return err;
6628 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6629 free(tmp_branch_name);
6630 return NULL;
6633 static const struct got_error *
6634 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6635 const char *diff_path, char **logmsg, void *arg)
6637 *logmsg = arg;
6638 return NULL;
6641 static const struct got_error *
6642 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6643 const char *path, struct got_object_id *blob_id,
6644 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6645 int dirfd, const char *de_name)
6647 return NULL;
6650 struct collect_merged_paths_arg {
6651 got_worktree_checkout_cb progress_cb;
6652 void *progress_arg;
6653 struct got_pathlist_head *merged_paths;
6656 static const struct got_error *
6657 collect_merged_paths(void *arg, unsigned char status, const char *path)
6659 const struct got_error *err;
6660 struct collect_merged_paths_arg *a = arg;
6661 char *p;
6662 struct got_pathlist_entry *new;
6664 err = (*a->progress_cb)(a->progress_arg, status, path);
6665 if (err)
6666 return err;
6668 if (status != GOT_STATUS_MERGE &&
6669 status != GOT_STATUS_ADD &&
6670 status != GOT_STATUS_DELETE &&
6671 status != GOT_STATUS_CONFLICT)
6672 return NULL;
6674 p = strdup(path);
6675 if (p == NULL)
6676 return got_error_from_errno("strdup");
6678 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6679 if (err || new == NULL)
6680 free(p);
6681 return err;
6684 static const struct got_error *
6685 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6686 int is_rebase, struct got_repository *repo)
6688 const struct got_error *err;
6689 struct got_reference *commit_ref = NULL;
6691 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6692 if (err) {
6693 if (err->code != GOT_ERR_NOT_REF)
6694 goto done;
6695 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6696 if (err)
6697 goto done;
6698 err = got_ref_write(commit_ref, repo);
6699 if (err)
6700 goto done;
6701 } else if (is_rebase) {
6702 struct got_object_id *stored_id;
6703 int cmp;
6705 err = got_ref_resolve(&stored_id, repo, commit_ref);
6706 if (err)
6707 goto done;
6708 cmp = got_object_id_cmp(commit_id, stored_id);
6709 free(stored_id);
6710 if (cmp != 0) {
6711 err = got_error(GOT_ERR_REBASE_COMMITID);
6712 goto done;
6715 done:
6716 if (commit_ref)
6717 got_ref_close(commit_ref);
6718 return err;
6721 static const struct got_error *
6722 rebase_merge_files(struct got_pathlist_head *merged_paths,
6723 const char *commit_ref_name, struct got_worktree *worktree,
6724 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6725 struct got_object_id *commit_id, struct got_repository *repo,
6726 got_worktree_checkout_cb progress_cb, void *progress_arg,
6727 got_cancel_cb cancel_cb, void *cancel_arg)
6729 const struct got_error *err;
6730 struct got_reference *commit_ref = NULL;
6731 struct collect_merged_paths_arg cmp_arg;
6732 char *fileindex_path;
6734 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6736 err = get_fileindex_path(&fileindex_path, worktree);
6737 if (err)
6738 return err;
6740 cmp_arg.progress_cb = progress_cb;
6741 cmp_arg.progress_arg = progress_arg;
6742 cmp_arg.merged_paths = merged_paths;
6743 err = merge_files(worktree, fileindex, fileindex_path,
6744 parent_commit_id, commit_id, repo, collect_merged_paths,
6745 &cmp_arg, cancel_cb, cancel_arg);
6746 if (commit_ref)
6747 got_ref_close(commit_ref);
6748 return err;
6751 const struct got_error *
6752 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6753 struct got_worktree *worktree, struct got_fileindex *fileindex,
6754 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6755 struct got_repository *repo,
6756 got_worktree_checkout_cb progress_cb, void *progress_arg,
6757 got_cancel_cb cancel_cb, void *cancel_arg)
6759 const struct got_error *err;
6760 char *commit_ref_name;
6762 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6763 if (err)
6764 return err;
6766 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6767 if (err)
6768 goto done;
6770 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6771 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6772 progress_arg, cancel_cb, cancel_arg);
6773 done:
6774 free(commit_ref_name);
6775 return err;
6778 const struct got_error *
6779 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6780 struct got_worktree *worktree, struct got_fileindex *fileindex,
6781 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6782 struct got_repository *repo,
6783 got_worktree_checkout_cb progress_cb, void *progress_arg,
6784 got_cancel_cb cancel_cb, void *cancel_arg)
6786 const struct got_error *err;
6787 char *commit_ref_name;
6789 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6790 if (err)
6791 return err;
6793 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6794 if (err)
6795 goto done;
6797 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6798 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6799 progress_arg, cancel_cb, cancel_arg);
6800 done:
6801 free(commit_ref_name);
6802 return err;
6805 static const struct got_error *
6806 rebase_commit(struct got_object_id **new_commit_id,
6807 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6808 struct got_worktree *worktree, struct got_fileindex *fileindex,
6809 struct got_reference *tmp_branch, const char *committer,
6810 struct got_commit_object *orig_commit, const char *new_logmsg,
6811 int allow_conflict, struct got_repository *repo)
6813 const struct got_error *err, *sync_err;
6814 struct got_pathlist_head commitable_paths;
6815 struct collect_commitables_arg cc_arg;
6816 char *fileindex_path = NULL;
6817 struct got_reference *head_ref = NULL;
6818 struct got_object_id *head_commit_id = NULL;
6819 char *logmsg = NULL;
6821 memset(&cc_arg, 0, sizeof(cc_arg));
6822 TAILQ_INIT(&commitable_paths);
6823 *new_commit_id = NULL;
6825 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6827 err = get_fileindex_path(&fileindex_path, worktree);
6828 if (err)
6829 return err;
6831 cc_arg.commitable_paths = &commitable_paths;
6832 cc_arg.worktree = worktree;
6833 cc_arg.repo = repo;
6834 cc_arg.have_staged_files = 0;
6835 cc_arg.commit_conflicts = allow_conflict;
6837 * If possible get the status of individual files directly to
6838 * avoid crawling the entire work tree once per rebased commit.
6840 * Ideally, merged_paths would contain a list of commitables
6841 * we could use so we could skip worktree_status() entirely.
6842 * However, we would then need carefully keep track of cumulative
6843 * effects of operations such as file additions and deletions
6844 * in 'got histedit -f' (folding multiple commits into one),
6845 * and this extra complexity is not really worth it.
6847 if (merged_paths) {
6848 struct got_pathlist_entry *pe;
6849 TAILQ_FOREACH(pe, merged_paths, entry) {
6850 err = worktree_status(worktree, pe->path, fileindex,
6851 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6852 0);
6853 if (err)
6854 goto done;
6856 } else {
6857 err = worktree_status(worktree, "", fileindex, repo,
6858 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6859 if (err)
6860 goto done;
6863 if (TAILQ_EMPTY(&commitable_paths)) {
6864 /* No-op change; commit will be elided. */
6865 err = got_ref_delete(commit_ref, repo);
6866 if (err)
6867 goto done;
6868 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6869 goto done;
6872 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6873 if (err)
6874 goto done;
6876 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6877 if (err)
6878 goto done;
6880 if (new_logmsg) {
6881 logmsg = strdup(new_logmsg);
6882 if (logmsg == NULL) {
6883 err = got_error_from_errno("strdup");
6884 goto done;
6886 } else {
6887 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6888 if (err)
6889 goto done;
6892 /* NB: commit_worktree will call free(logmsg) */
6893 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6894 NULL, worktree, got_object_commit_get_author(orig_commit),
6895 committer ? committer :
6896 got_object_commit_get_committer(orig_commit), NULL,
6897 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6898 if (err)
6899 goto done;
6901 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6902 if (err)
6903 goto done;
6905 err = got_ref_delete(commit_ref, repo);
6906 if (err)
6907 goto done;
6909 err = update_fileindex_after_commit(worktree, &commitable_paths,
6910 *new_commit_id, fileindex, 0);
6911 sync_err = sync_fileindex(fileindex, fileindex_path);
6912 if (sync_err && err == NULL)
6913 err = sync_err;
6914 done:
6915 free(fileindex_path);
6916 free(head_commit_id);
6917 if (head_ref)
6918 got_ref_close(head_ref);
6919 if (err) {
6920 free(*new_commit_id);
6921 *new_commit_id = NULL;
6923 return err;
6926 const struct got_error *
6927 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6928 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6929 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6930 const char *committer, struct got_commit_object *orig_commit,
6931 struct got_object_id *orig_commit_id, int allow_conflict,
6932 struct got_repository *repo)
6934 const struct got_error *err;
6935 char *commit_ref_name;
6936 struct got_reference *commit_ref = NULL;
6937 struct got_object_id *commit_id = NULL;
6939 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6940 if (err)
6941 return err;
6943 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6944 if (err)
6945 goto done;
6946 err = got_ref_resolve(&commit_id, repo, commit_ref);
6947 if (err)
6948 goto done;
6949 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6950 err = got_error(GOT_ERR_REBASE_COMMITID);
6951 goto done;
6954 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6955 worktree, fileindex, tmp_branch, committer, orig_commit,
6956 NULL, allow_conflict, repo);
6957 done:
6958 if (commit_ref)
6959 got_ref_close(commit_ref);
6960 free(commit_ref_name);
6961 free(commit_id);
6962 return err;
6965 const struct got_error *
6966 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6967 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6968 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6969 const char *committer, struct got_commit_object *orig_commit,
6970 struct got_object_id *orig_commit_id, const char *new_logmsg,
6971 int allow_conflict, struct got_repository *repo)
6973 const struct got_error *err;
6974 char *commit_ref_name;
6975 struct got_reference *commit_ref = NULL;
6977 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6978 if (err)
6979 return err;
6981 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6982 if (err)
6983 goto done;
6985 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6986 worktree, fileindex, tmp_branch, committer, orig_commit,
6987 new_logmsg, allow_conflict, repo);
6988 done:
6989 if (commit_ref)
6990 got_ref_close(commit_ref);
6991 free(commit_ref_name);
6992 return err;
6995 const struct got_error *
6996 got_worktree_rebase_postpone(struct got_worktree *worktree,
6997 struct got_fileindex *fileindex)
6999 if (fileindex)
7000 got_fileindex_free(fileindex);
7001 return lock_worktree(worktree, LOCK_SH);
7004 static const struct got_error *
7005 delete_ref(const char *name, struct got_repository *repo)
7007 const struct got_error *err;
7008 struct got_reference *ref;
7010 err = got_ref_open(&ref, repo, name, 0);
7011 if (err) {
7012 if (err->code == GOT_ERR_NOT_REF)
7013 return NULL;
7014 return err;
7017 err = got_ref_delete(ref, repo);
7018 got_ref_close(ref);
7019 return err;
7022 static const struct got_error *
7023 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7025 const struct got_error *err;
7026 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7027 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7029 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7030 if (err)
7031 goto done;
7032 err = delete_ref(tmp_branch_name, repo);
7033 if (err)
7034 goto done;
7036 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7037 if (err)
7038 goto done;
7039 err = delete_ref(new_base_branch_ref_name, repo);
7040 if (err)
7041 goto done;
7043 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7044 if (err)
7045 goto done;
7046 err = delete_ref(branch_ref_name, repo);
7047 if (err)
7048 goto done;
7050 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7051 if (err)
7052 goto done;
7053 err = delete_ref(commit_ref_name, repo);
7054 if (err)
7055 goto done;
7057 done:
7058 free(tmp_branch_name);
7059 free(new_base_branch_ref_name);
7060 free(branch_ref_name);
7061 free(commit_ref_name);
7062 return err;
7065 static const struct got_error *
7066 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7067 struct got_object_id *new_commit_id, struct got_repository *repo)
7069 const struct got_error *err;
7070 struct got_reference *ref = NULL;
7071 struct got_object_id *old_commit_id = NULL;
7072 const char *branch_name = NULL;
7073 char *new_id_str = NULL;
7074 char *refname = NULL;
7076 branch_name = got_ref_get_name(branch);
7077 if (strncmp(branch_name, "refs/heads/", 11) != 0)
7078 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7079 branch_name += 11;
7081 err = got_object_id_str(&new_id_str, new_commit_id);
7082 if (err)
7083 return err;
7085 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7086 new_id_str) == -1) {
7087 err = got_error_from_errno("asprintf");
7088 goto done;
7091 err = got_ref_resolve(&old_commit_id, repo, branch);
7092 if (err)
7093 goto done;
7095 err = got_ref_alloc(&ref, refname, old_commit_id);
7096 if (err)
7097 goto done;
7099 err = got_ref_write(ref, repo);
7100 done:
7101 free(new_id_str);
7102 free(refname);
7103 free(old_commit_id);
7104 if (ref)
7105 got_ref_close(ref);
7106 return err;
7109 const struct got_error *
7110 got_worktree_rebase_complete(struct got_worktree *worktree,
7111 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7112 struct got_reference *rebased_branch, struct got_repository *repo,
7113 int create_backup)
7115 const struct got_error *err, *unlockerr, *sync_err;
7116 struct got_object_id *new_head_commit_id = NULL;
7117 char *fileindex_path = NULL;
7119 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7120 if (err)
7121 return err;
7123 if (create_backup) {
7124 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7125 rebased_branch, new_head_commit_id, repo);
7126 if (err)
7127 goto done;
7130 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7131 if (err)
7132 goto done;
7134 err = got_ref_write(rebased_branch, repo);
7135 if (err)
7136 goto done;
7138 err = got_worktree_set_head_ref(worktree, rebased_branch);
7139 if (err)
7140 goto done;
7142 err = delete_rebase_refs(worktree, repo);
7143 if (err)
7144 goto done;
7146 err = get_fileindex_path(&fileindex_path, worktree);
7147 if (err)
7148 goto done;
7149 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7150 sync_err = sync_fileindex(fileindex, fileindex_path);
7151 if (sync_err && err == NULL)
7152 err = sync_err;
7153 done:
7154 got_fileindex_free(fileindex);
7155 free(fileindex_path);
7156 free(new_head_commit_id);
7157 unlockerr = lock_worktree(worktree, LOCK_SH);
7158 if (unlockerr && err == NULL)
7159 err = unlockerr;
7160 return err;
7163 const struct got_error *
7164 got_worktree_rebase_abort(struct got_worktree *worktree,
7165 struct got_fileindex *fileindex, struct got_repository *repo,
7166 struct got_reference *new_base_branch,
7167 got_worktree_checkout_cb progress_cb, void *progress_arg)
7169 const struct got_error *err, *unlockerr, *sync_err;
7170 struct got_reference *resolved = NULL;
7171 struct got_object_id *commit_id = NULL;
7172 struct got_commit_object *commit = NULL;
7173 char *fileindex_path = NULL;
7174 struct revert_file_args rfa;
7175 struct got_object_id *tree_id = NULL;
7177 err = lock_worktree(worktree, LOCK_EX);
7178 if (err)
7179 return err;
7181 err = got_object_open_as_commit(&commit, repo,
7182 worktree->base_commit_id);
7183 if (err)
7184 goto done;
7186 err = got_ref_open(&resolved, repo,
7187 got_ref_get_symref_target(new_base_branch), 0);
7188 if (err)
7189 goto done;
7191 err = got_worktree_set_head_ref(worktree, resolved);
7192 if (err)
7193 goto done;
7196 * XXX commits to the base branch could have happened while
7197 * we were busy rebasing; should we store the original commit ID
7198 * when rebase begins and read it back here?
7200 err = got_ref_resolve(&commit_id, repo, resolved);
7201 if (err)
7202 goto done;
7204 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7205 if (err)
7206 goto done;
7208 err = got_object_id_by_path(&tree_id, repo, commit,
7209 worktree->path_prefix);
7210 if (err)
7211 goto done;
7213 err = delete_rebase_refs(worktree, repo);
7214 if (err)
7215 goto done;
7217 err = get_fileindex_path(&fileindex_path, worktree);
7218 if (err)
7219 goto done;
7221 rfa.worktree = worktree;
7222 rfa.fileindex = fileindex;
7223 rfa.progress_cb = progress_cb;
7224 rfa.progress_arg = progress_arg;
7225 rfa.patch_cb = NULL;
7226 rfa.patch_arg = NULL;
7227 rfa.repo = repo;
7228 rfa.unlink_added_files = 0;
7229 err = worktree_status(worktree, "", fileindex, repo,
7230 revert_file, &rfa, NULL, NULL, 1, 0);
7231 if (err)
7232 goto sync;
7234 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7235 repo, progress_cb, progress_arg, NULL, NULL);
7236 sync:
7237 sync_err = sync_fileindex(fileindex, fileindex_path);
7238 if (sync_err && err == NULL)
7239 err = sync_err;
7240 done:
7241 got_ref_close(resolved);
7242 free(tree_id);
7243 free(commit_id);
7244 if (commit)
7245 got_object_commit_close(commit);
7246 if (fileindex)
7247 got_fileindex_free(fileindex);
7248 free(fileindex_path);
7250 unlockerr = lock_worktree(worktree, LOCK_SH);
7251 if (unlockerr && err == NULL)
7252 err = unlockerr;
7253 return err;
7256 const struct got_error *
7257 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7258 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7259 struct got_fileindex **fileindex, struct got_worktree *worktree,
7260 struct got_repository *repo)
7262 const struct got_error *err = NULL;
7263 char *tmp_branch_name = NULL;
7264 char *branch_ref_name = NULL;
7265 char *base_commit_ref_name = NULL;
7266 char *fileindex_path = NULL;
7267 struct check_rebase_ok_arg ok_arg;
7268 struct got_reference *wt_branch = NULL;
7269 struct got_reference *base_commit_ref = NULL;
7271 *tmp_branch = NULL;
7272 *branch_ref = NULL;
7273 *base_commit_id = NULL;
7274 *fileindex = NULL;
7276 err = lock_worktree(worktree, LOCK_EX);
7277 if (err)
7278 return err;
7280 err = open_fileindex(fileindex, &fileindex_path, worktree);
7281 if (err)
7282 goto done;
7284 ok_arg.worktree = worktree;
7285 ok_arg.repo = repo;
7286 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7287 &ok_arg);
7288 if (err)
7289 goto done;
7291 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7292 if (err)
7293 goto done;
7295 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7296 if (err)
7297 goto done;
7299 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7300 worktree);
7301 if (err)
7302 goto done;
7304 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7305 0);
7306 if (err)
7307 goto done;
7309 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7310 if (err)
7311 goto done;
7313 err = got_ref_write(*branch_ref, repo);
7314 if (err)
7315 goto done;
7317 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7318 worktree->base_commit_id);
7319 if (err)
7320 goto done;
7321 err = got_ref_write(base_commit_ref, repo);
7322 if (err)
7323 goto done;
7324 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7325 if (*base_commit_id == NULL) {
7326 err = got_error_from_errno("got_object_id_dup");
7327 goto done;
7330 err = got_ref_alloc(tmp_branch, tmp_branch_name,
7331 worktree->base_commit_id);
7332 if (err)
7333 goto done;
7334 err = got_ref_write(*tmp_branch, repo);
7335 if (err)
7336 goto done;
7338 err = got_worktree_set_head_ref(worktree, *tmp_branch);
7339 if (err)
7340 goto done;
7341 done:
7342 free(fileindex_path);
7343 free(tmp_branch_name);
7344 free(branch_ref_name);
7345 free(base_commit_ref_name);
7346 if (wt_branch)
7347 got_ref_close(wt_branch);
7348 if (err) {
7349 if (*branch_ref) {
7350 got_ref_close(*branch_ref);
7351 *branch_ref = NULL;
7353 if (*tmp_branch) {
7354 got_ref_close(*tmp_branch);
7355 *tmp_branch = NULL;
7357 free(*base_commit_id);
7358 if (*fileindex) {
7359 got_fileindex_free(*fileindex);
7360 *fileindex = NULL;
7362 lock_worktree(worktree, LOCK_SH);
7364 return err;
7367 const struct got_error *
7368 got_worktree_histedit_postpone(struct got_worktree *worktree,
7369 struct got_fileindex *fileindex)
7371 if (fileindex)
7372 got_fileindex_free(fileindex);
7373 return lock_worktree(worktree, LOCK_SH);
7376 const struct got_error *
7377 got_worktree_histedit_in_progress(int *in_progress,
7378 struct got_worktree *worktree)
7380 const struct got_error *err;
7381 char *tmp_branch_name = NULL;
7383 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7384 if (err)
7385 return err;
7387 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7388 free(tmp_branch_name);
7389 return NULL;
7392 const struct got_error *
7393 got_worktree_histedit_continue(struct got_object_id **commit_id,
7394 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7395 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7396 struct got_worktree *worktree, struct got_repository *repo)
7398 const struct got_error *err;
7399 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7400 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7401 struct got_reference *commit_ref = NULL;
7402 struct got_reference *base_commit_ref = NULL;
7403 char *fileindex_path = NULL;
7404 int have_staged_files = 0;
7406 *commit_id = NULL;
7407 *tmp_branch = NULL;
7408 *base_commit_id = NULL;
7409 *fileindex = NULL;
7411 err = lock_worktree(worktree, LOCK_EX);
7412 if (err)
7413 return err;
7415 err = open_fileindex(fileindex, &fileindex_path, worktree);
7416 if (err)
7417 goto done;
7419 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7420 &have_staged_files);
7421 if (err && err->code != GOT_ERR_CANCELLED)
7422 goto done;
7423 if (have_staged_files) {
7424 err = got_error(GOT_ERR_STAGED_PATHS);
7425 goto done;
7428 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7429 if (err)
7430 goto done;
7432 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7433 if (err)
7434 goto done;
7436 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7437 if (err)
7438 goto done;
7440 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7441 worktree);
7442 if (err)
7443 goto done;
7445 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7446 if (err)
7447 goto done;
7449 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7450 if (err)
7451 goto done;
7452 err = got_ref_resolve(commit_id, repo, commit_ref);
7453 if (err)
7454 goto done;
7456 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7457 if (err)
7458 goto done;
7459 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7460 if (err)
7461 goto done;
7463 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7464 if (err)
7465 goto done;
7466 done:
7467 free(commit_ref_name);
7468 free(branch_ref_name);
7469 free(fileindex_path);
7470 if (commit_ref)
7471 got_ref_close(commit_ref);
7472 if (base_commit_ref)
7473 got_ref_close(base_commit_ref);
7474 if (err) {
7475 free(*commit_id);
7476 *commit_id = NULL;
7477 free(*base_commit_id);
7478 *base_commit_id = NULL;
7479 if (*tmp_branch) {
7480 got_ref_close(*tmp_branch);
7481 *tmp_branch = NULL;
7483 if (*fileindex) {
7484 got_fileindex_free(*fileindex);
7485 *fileindex = NULL;
7487 lock_worktree(worktree, LOCK_EX);
7489 return err;
7492 static const struct got_error *
7493 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7495 const struct got_error *err;
7496 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7497 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7499 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7500 if (err)
7501 goto done;
7502 err = delete_ref(tmp_branch_name, repo);
7503 if (err)
7504 goto done;
7506 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7507 worktree);
7508 if (err)
7509 goto done;
7510 err = delete_ref(base_commit_ref_name, repo);
7511 if (err)
7512 goto done;
7514 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7515 if (err)
7516 goto done;
7517 err = delete_ref(branch_ref_name, repo);
7518 if (err)
7519 goto done;
7521 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7522 if (err)
7523 goto done;
7524 err = delete_ref(commit_ref_name, repo);
7525 if (err)
7526 goto done;
7527 done:
7528 free(tmp_branch_name);
7529 free(base_commit_ref_name);
7530 free(branch_ref_name);
7531 free(commit_ref_name);
7532 return err;
7535 const struct got_error *
7536 got_worktree_histedit_abort(struct got_worktree *worktree,
7537 struct got_fileindex *fileindex, struct got_repository *repo,
7538 struct got_reference *branch, struct got_object_id *base_commit_id,
7539 got_worktree_checkout_cb progress_cb, void *progress_arg)
7541 const struct got_error *err, *unlockerr, *sync_err;
7542 struct got_reference *resolved = NULL;
7543 char *fileindex_path = NULL;
7544 struct got_commit_object *commit = NULL;
7545 struct got_object_id *tree_id = NULL;
7546 struct revert_file_args rfa;
7548 err = lock_worktree(worktree, LOCK_EX);
7549 if (err)
7550 return err;
7552 err = got_object_open_as_commit(&commit, repo,
7553 worktree->base_commit_id);
7554 if (err)
7555 goto done;
7557 err = got_ref_open(&resolved, repo,
7558 got_ref_get_symref_target(branch), 0);
7559 if (err)
7560 goto done;
7562 err = got_worktree_set_head_ref(worktree, resolved);
7563 if (err)
7564 goto done;
7566 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7567 if (err)
7568 goto done;
7570 err = got_object_id_by_path(&tree_id, repo, commit,
7571 worktree->path_prefix);
7572 if (err)
7573 goto done;
7575 err = delete_histedit_refs(worktree, repo);
7576 if (err)
7577 goto done;
7579 err = get_fileindex_path(&fileindex_path, worktree);
7580 if (err)
7581 goto done;
7583 rfa.worktree = worktree;
7584 rfa.fileindex = fileindex;
7585 rfa.progress_cb = progress_cb;
7586 rfa.progress_arg = progress_arg;
7587 rfa.patch_cb = NULL;
7588 rfa.patch_arg = NULL;
7589 rfa.repo = repo;
7590 rfa.unlink_added_files = 0;
7591 err = worktree_status(worktree, "", fileindex, repo,
7592 revert_file, &rfa, NULL, NULL, 1, 0);
7593 if (err)
7594 goto sync;
7596 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7597 repo, progress_cb, progress_arg, NULL, NULL);
7598 sync:
7599 sync_err = sync_fileindex(fileindex, fileindex_path);
7600 if (sync_err && err == NULL)
7601 err = sync_err;
7602 done:
7603 got_ref_close(resolved);
7604 free(tree_id);
7605 free(fileindex_path);
7607 unlockerr = lock_worktree(worktree, LOCK_SH);
7608 if (unlockerr && err == NULL)
7609 err = unlockerr;
7610 return err;
7613 const struct got_error *
7614 got_worktree_histedit_complete(struct got_worktree *worktree,
7615 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7616 struct got_reference *edited_branch, struct got_repository *repo)
7618 const struct got_error *err, *unlockerr, *sync_err;
7619 struct got_object_id *new_head_commit_id = NULL;
7620 struct got_reference *resolved = NULL;
7621 char *fileindex_path = NULL;
7623 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7624 if (err)
7625 return err;
7627 err = got_ref_open(&resolved, repo,
7628 got_ref_get_symref_target(edited_branch), 0);
7629 if (err)
7630 goto done;
7632 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7633 resolved, new_head_commit_id, repo);
7634 if (err)
7635 goto done;
7637 err = got_ref_change_ref(resolved, new_head_commit_id);
7638 if (err)
7639 goto done;
7641 err = got_ref_write(resolved, repo);
7642 if (err)
7643 goto done;
7645 err = got_worktree_set_head_ref(worktree, resolved);
7646 if (err)
7647 goto done;
7649 err = delete_histedit_refs(worktree, repo);
7650 if (err)
7651 goto done;
7653 err = get_fileindex_path(&fileindex_path, worktree);
7654 if (err)
7655 goto done;
7656 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7657 sync_err = sync_fileindex(fileindex, fileindex_path);
7658 if (sync_err && err == NULL)
7659 err = sync_err;
7660 done:
7661 got_fileindex_free(fileindex);
7662 free(fileindex_path);
7663 free(new_head_commit_id);
7664 unlockerr = lock_worktree(worktree, LOCK_SH);
7665 if (unlockerr && err == NULL)
7666 err = unlockerr;
7667 return err;
7670 const struct got_error *
7671 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7672 struct got_object_id *commit_id, struct got_repository *repo)
7674 const struct got_error *err;
7675 char *commit_ref_name;
7677 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7678 if (err)
7679 return err;
7681 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7682 if (err)
7683 goto done;
7685 err = delete_ref(commit_ref_name, repo);
7686 done:
7687 free(commit_ref_name);
7688 return err;
7691 const struct got_error *
7692 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7693 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7694 struct got_worktree *worktree, const char *refname,
7695 struct got_repository *repo)
7697 const struct got_error *err = NULL;
7698 char *fileindex_path = NULL;
7699 struct check_rebase_ok_arg ok_arg;
7701 *fileindex = NULL;
7702 *branch_ref = NULL;
7703 *base_branch_ref = NULL;
7705 err = lock_worktree(worktree, LOCK_EX);
7706 if (err)
7707 return err;
7709 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7710 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7711 "cannot integrate a branch into itself; "
7712 "update -b or different branch name required");
7713 goto done;
7716 err = open_fileindex(fileindex, &fileindex_path, worktree);
7717 if (err)
7718 goto done;
7720 /* Preconditions are the same as for rebase. */
7721 ok_arg.worktree = worktree;
7722 ok_arg.repo = repo;
7723 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7724 &ok_arg);
7725 if (err)
7726 goto done;
7728 err = got_ref_open(branch_ref, repo, refname, 1);
7729 if (err)
7730 goto done;
7732 err = got_ref_open(base_branch_ref, repo,
7733 got_worktree_get_head_ref_name(worktree), 1);
7734 done:
7735 if (err) {
7736 if (*branch_ref) {
7737 got_ref_close(*branch_ref);
7738 *branch_ref = NULL;
7740 if (*base_branch_ref) {
7741 got_ref_close(*base_branch_ref);
7742 *base_branch_ref = NULL;
7744 if (*fileindex) {
7745 got_fileindex_free(*fileindex);
7746 *fileindex = NULL;
7748 lock_worktree(worktree, LOCK_SH);
7750 return err;
7753 const struct got_error *
7754 got_worktree_integrate_continue(struct got_worktree *worktree,
7755 struct got_fileindex *fileindex, struct got_repository *repo,
7756 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7757 got_worktree_checkout_cb progress_cb, void *progress_arg,
7758 got_cancel_cb cancel_cb, void *cancel_arg)
7760 const struct got_error *err = NULL, *sync_err, *unlockerr;
7761 char *fileindex_path = NULL;
7762 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7763 struct got_commit_object *commit = NULL;
7765 err = get_fileindex_path(&fileindex_path, worktree);
7766 if (err)
7767 goto done;
7769 err = got_ref_resolve(&commit_id, repo, branch_ref);
7770 if (err)
7771 goto done;
7773 err = got_object_open_as_commit(&commit, repo, commit_id);
7774 if (err)
7775 goto done;
7777 err = got_object_id_by_path(&tree_id, repo, commit,
7778 worktree->path_prefix);
7779 if (err)
7780 goto done;
7782 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7783 if (err)
7784 goto done;
7786 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7787 progress_cb, progress_arg, cancel_cb, cancel_arg);
7788 if (err)
7789 goto sync;
7791 err = got_ref_change_ref(base_branch_ref, commit_id);
7792 if (err)
7793 goto sync;
7795 err = got_ref_write(base_branch_ref, repo);
7796 if (err)
7797 goto sync;
7799 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7800 sync:
7801 sync_err = sync_fileindex(fileindex, fileindex_path);
7802 if (sync_err && err == NULL)
7803 err = sync_err;
7805 done:
7806 unlockerr = got_ref_unlock(branch_ref);
7807 if (unlockerr && err == NULL)
7808 err = unlockerr;
7809 got_ref_close(branch_ref);
7811 unlockerr = got_ref_unlock(base_branch_ref);
7812 if (unlockerr && err == NULL)
7813 err = unlockerr;
7814 got_ref_close(base_branch_ref);
7816 got_fileindex_free(fileindex);
7817 free(fileindex_path);
7818 free(tree_id);
7819 if (commit)
7820 got_object_commit_close(commit);
7822 unlockerr = lock_worktree(worktree, LOCK_SH);
7823 if (unlockerr && err == NULL)
7824 err = unlockerr;
7825 return err;
7828 const struct got_error *
7829 got_worktree_integrate_abort(struct got_worktree *worktree,
7830 struct got_fileindex *fileindex, struct got_repository *repo,
7831 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7833 const struct got_error *err = NULL, *unlockerr = NULL;
7835 got_fileindex_free(fileindex);
7837 err = lock_worktree(worktree, LOCK_SH);
7839 unlockerr = got_ref_unlock(branch_ref);
7840 if (unlockerr && err == NULL)
7841 err = unlockerr;
7842 got_ref_close(branch_ref);
7844 unlockerr = got_ref_unlock(base_branch_ref);
7845 if (unlockerr && err == NULL)
7846 err = unlockerr;
7847 got_ref_close(base_branch_ref);
7849 return err;
7852 const struct got_error *
7853 got_worktree_merge_postpone(struct got_worktree *worktree,
7854 struct got_fileindex *fileindex)
7856 const struct got_error *err, *sync_err;
7857 char *fileindex_path = NULL;
7859 err = get_fileindex_path(&fileindex_path, worktree);
7860 if (err)
7861 goto done;
7863 sync_err = sync_fileindex(fileindex, fileindex_path);
7865 err = lock_worktree(worktree, LOCK_SH);
7866 if (sync_err && err == NULL)
7867 err = sync_err;
7868 done:
7869 got_fileindex_free(fileindex);
7870 free(fileindex_path);
7871 return err;
7874 static const struct got_error *
7875 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7877 const struct got_error *err;
7878 char *branch_refname = NULL, *commit_refname = NULL;
7880 err = get_merge_branch_ref_name(&branch_refname, worktree);
7881 if (err)
7882 goto done;
7883 err = delete_ref(branch_refname, repo);
7884 if (err)
7885 goto done;
7887 err = get_merge_commit_ref_name(&commit_refname, worktree);
7888 if (err)
7889 goto done;
7890 err = delete_ref(commit_refname, repo);
7891 if (err)
7892 goto done;
7894 done:
7895 free(branch_refname);
7896 free(commit_refname);
7897 return err;
7900 struct merge_commit_msg_arg {
7901 struct got_worktree *worktree;
7902 const char *branch_name;
7905 static const struct got_error *
7906 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
7907 const char *diff_path, char **logmsg, void *arg)
7909 struct merge_commit_msg_arg *a = arg;
7911 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7912 got_worktree_get_head_ref_name(a->worktree)) == -1)
7913 return got_error_from_errno("asprintf");
7915 return NULL;
7919 const struct got_error *
7920 got_worktree_merge_branch(struct got_worktree *worktree,
7921 struct got_fileindex *fileindex,
7922 struct got_object_id *yca_commit_id,
7923 struct got_object_id *branch_tip,
7924 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7925 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7927 const struct got_error *err;
7928 char *fileindex_path = NULL;
7930 err = get_fileindex_path(&fileindex_path, worktree);
7931 if (err)
7932 goto done;
7934 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7935 worktree);
7936 if (err)
7937 goto done;
7939 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7940 branch_tip, repo, progress_cb, progress_arg,
7941 cancel_cb, cancel_arg);
7942 done:
7943 free(fileindex_path);
7944 return err;
7947 const struct got_error *
7948 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7949 struct got_worktree *worktree, struct got_fileindex *fileindex,
7950 const char *author, const char *committer, int allow_bad_symlinks,
7951 struct got_object_id *branch_tip, const char *branch_name,
7952 int allow_conflict, struct got_repository *repo,
7953 got_worktree_status_cb status_cb, void *status_arg)
7956 const struct got_error *err = NULL, *sync_err;
7957 struct got_pathlist_head commitable_paths;
7958 struct collect_commitables_arg cc_arg;
7959 struct got_pathlist_entry *pe;
7960 struct got_reference *head_ref = NULL;
7961 struct got_object_id *head_commit_id = NULL;
7962 int have_staged_files = 0;
7963 struct merge_commit_msg_arg mcm_arg;
7964 char *fileindex_path = NULL;
7966 memset(&cc_arg, 0, sizeof(cc_arg));
7967 *new_commit_id = NULL;
7969 TAILQ_INIT(&commitable_paths);
7971 err = get_fileindex_path(&fileindex_path, worktree);
7972 if (err)
7973 goto done;
7975 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7976 if (err)
7977 goto done;
7979 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7980 if (err)
7981 goto done;
7983 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7984 &have_staged_files);
7985 if (err && err->code != GOT_ERR_CANCELLED)
7986 goto done;
7987 if (have_staged_files) {
7988 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7989 goto done;
7992 cc_arg.commitable_paths = &commitable_paths;
7993 cc_arg.worktree = worktree;
7994 cc_arg.fileindex = fileindex;
7995 cc_arg.repo = repo;
7996 cc_arg.have_staged_files = have_staged_files;
7997 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7998 cc_arg.commit_conflicts = allow_conflict;
7999 err = worktree_status(worktree, "", fileindex, repo,
8000 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8001 if (err)
8002 goto done;
8004 if (TAILQ_EMPTY(&commitable_paths)) {
8005 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
8006 "merge of %s cannot proceed", branch_name);
8007 goto done;
8010 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8011 struct got_commitable *ct = pe->data;
8012 const char *ct_path = ct->in_repo_path;
8014 while (ct_path[0] == '/')
8015 ct_path++;
8016 err = check_out_of_date(ct_path, ct->status,
8017 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
8018 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
8019 if (err)
8020 goto done;
8024 mcm_arg.worktree = worktree;
8025 mcm_arg.branch_name = branch_name;
8026 err = commit_worktree(new_commit_id, &commitable_paths,
8027 head_commit_id, branch_tip, worktree, author, committer, NULL,
8028 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8029 if (err)
8030 goto done;
8032 err = update_fileindex_after_commit(worktree, &commitable_paths,
8033 *new_commit_id, fileindex, have_staged_files);
8034 sync_err = sync_fileindex(fileindex, fileindex_path);
8035 if (sync_err && err == NULL)
8036 err = sync_err;
8037 done:
8038 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8039 struct got_commitable *ct = pe->data;
8041 free_commitable(ct);
8043 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8044 free(fileindex_path);
8045 return err;
8048 const struct got_error *
8049 got_worktree_merge_complete(struct got_worktree *worktree,
8050 struct got_fileindex *fileindex, struct got_repository *repo)
8052 const struct got_error *err, *unlockerr, *sync_err;
8053 char *fileindex_path = NULL;
8055 err = delete_merge_refs(worktree, repo);
8056 if (err)
8057 goto done;
8059 err = get_fileindex_path(&fileindex_path, worktree);
8060 if (err)
8061 goto done;
8062 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8063 sync_err = sync_fileindex(fileindex, fileindex_path);
8064 if (sync_err && err == NULL)
8065 err = sync_err;
8066 done:
8067 got_fileindex_free(fileindex);
8068 free(fileindex_path);
8069 unlockerr = lock_worktree(worktree, LOCK_SH);
8070 if (unlockerr && err == NULL)
8071 err = unlockerr;
8072 return err;
8075 const struct got_error *
8076 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8077 struct got_repository *repo)
8079 const struct got_error *err;
8080 char *branch_refname = NULL;
8081 struct got_reference *branch_ref = NULL;
8083 *in_progress = 0;
8085 err = get_merge_branch_ref_name(&branch_refname, worktree);
8086 if (err)
8087 return err;
8088 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8089 free(branch_refname);
8090 if (err) {
8091 if (err->code != GOT_ERR_NOT_REF)
8092 return err;
8093 } else
8094 *in_progress = 1;
8096 return NULL;
8099 const struct got_error *got_worktree_merge_prepare(
8100 struct got_fileindex **fileindex, struct got_worktree *worktree,
8101 struct got_reference *branch, struct got_repository *repo)
8103 const struct got_error *err = NULL;
8104 char *fileindex_path = NULL;
8105 char *branch_refname = NULL, *commit_refname = NULL;
8106 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
8107 struct got_reference *commit_ref = NULL;
8108 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
8109 struct check_rebase_ok_arg ok_arg;
8111 *fileindex = NULL;
8113 err = lock_worktree(worktree, LOCK_EX);
8114 if (err)
8115 return err;
8117 err = open_fileindex(fileindex, &fileindex_path, worktree);
8118 if (err)
8119 goto done;
8121 /* Preconditions are the same as for rebase. */
8122 ok_arg.worktree = worktree;
8123 ok_arg.repo = repo;
8124 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8125 &ok_arg);
8126 if (err)
8127 goto done;
8129 err = get_merge_branch_ref_name(&branch_refname, worktree);
8130 if (err)
8131 return err;
8133 err = get_merge_commit_ref_name(&commit_refname, worktree);
8134 if (err)
8135 return err;
8137 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8138 0);
8139 if (err)
8140 goto done;
8142 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8143 if (err)
8144 goto done;
8146 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8147 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8148 goto done;
8151 err = got_ref_resolve(&branch_tip, repo, branch);
8152 if (err)
8153 goto done;
8155 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8156 if (err)
8157 goto done;
8158 err = got_ref_write(branch_ref, repo);
8159 if (err)
8160 goto done;
8162 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8163 if (err)
8164 goto done;
8165 err = got_ref_write(commit_ref, repo);
8166 if (err)
8167 goto done;
8169 done:
8170 free(branch_refname);
8171 free(commit_refname);
8172 free(fileindex_path);
8173 if (branch_ref)
8174 got_ref_close(branch_ref);
8175 if (commit_ref)
8176 got_ref_close(commit_ref);
8177 if (wt_branch)
8178 got_ref_close(wt_branch);
8179 free(wt_branch_tip);
8180 if (err) {
8181 if (*fileindex) {
8182 got_fileindex_free(*fileindex);
8183 *fileindex = NULL;
8185 lock_worktree(worktree, LOCK_SH);
8187 return err;
8190 const struct got_error *
8191 got_worktree_merge_continue(char **branch_name,
8192 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8193 struct got_worktree *worktree, struct got_repository *repo)
8195 const struct got_error *err;
8196 char *commit_refname = NULL, *branch_refname = NULL;
8197 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8198 char *fileindex_path = NULL;
8199 int have_staged_files = 0;
8201 *branch_name = NULL;
8202 *branch_tip = NULL;
8203 *fileindex = NULL;
8205 err = lock_worktree(worktree, LOCK_EX);
8206 if (err)
8207 return err;
8209 err = open_fileindex(fileindex, &fileindex_path, worktree);
8210 if (err)
8211 goto done;
8213 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8214 &have_staged_files);
8215 if (err && err->code != GOT_ERR_CANCELLED)
8216 goto done;
8217 if (have_staged_files) {
8218 err = got_error(GOT_ERR_STAGED_PATHS);
8219 goto done;
8222 err = get_merge_branch_ref_name(&branch_refname, worktree);
8223 if (err)
8224 goto done;
8226 err = get_merge_commit_ref_name(&commit_refname, worktree);
8227 if (err)
8228 goto done;
8230 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8231 if (err)
8232 goto done;
8234 if (!got_ref_is_symbolic(branch_ref)) {
8235 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8236 "%s is not a symbolic reference",
8237 got_ref_get_name(branch_ref));
8238 goto done;
8240 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8241 if (*branch_name == NULL) {
8242 err = got_error_from_errno("strdup");
8243 goto done;
8246 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8247 if (err)
8248 goto done;
8250 err = got_ref_resolve(branch_tip, repo, commit_ref);
8251 if (err)
8252 goto done;
8253 done:
8254 free(commit_refname);
8255 free(branch_refname);
8256 free(fileindex_path);
8257 if (commit_ref)
8258 got_ref_close(commit_ref);
8259 if (branch_ref)
8260 got_ref_close(branch_ref);
8261 if (err) {
8262 if (*branch_name) {
8263 free(*branch_name);
8264 *branch_name = NULL;
8266 free(*branch_tip);
8267 *branch_tip = NULL;
8268 if (*fileindex) {
8269 got_fileindex_free(*fileindex);
8270 *fileindex = NULL;
8272 lock_worktree(worktree, LOCK_SH);
8274 return err;
8277 const struct got_error *
8278 got_worktree_merge_abort(struct got_worktree *worktree,
8279 struct got_fileindex *fileindex, struct got_repository *repo,
8280 got_worktree_checkout_cb progress_cb, void *progress_arg)
8282 const struct got_error *err, *unlockerr, *sync_err;
8283 struct got_object_id *commit_id = NULL;
8284 struct got_commit_object *commit = NULL;
8285 char *fileindex_path = NULL;
8286 struct revert_file_args rfa;
8287 struct got_object_id *tree_id = NULL;
8289 err = got_object_open_as_commit(&commit, repo,
8290 worktree->base_commit_id);
8291 if (err)
8292 goto done;
8294 err = got_object_id_by_path(&tree_id, repo, commit,
8295 worktree->path_prefix);
8296 if (err)
8297 goto done;
8299 err = delete_merge_refs(worktree, repo);
8300 if (err)
8301 goto done;
8303 err = get_fileindex_path(&fileindex_path, worktree);
8304 if (err)
8305 goto done;
8307 rfa.worktree = worktree;
8308 rfa.fileindex = fileindex;
8309 rfa.progress_cb = progress_cb;
8310 rfa.progress_arg = progress_arg;
8311 rfa.patch_cb = NULL;
8312 rfa.patch_arg = NULL;
8313 rfa.repo = repo;
8314 rfa.unlink_added_files = 1;
8315 err = worktree_status(worktree, "", fileindex, repo,
8316 revert_file, &rfa, NULL, NULL, 1, 0);
8317 if (err)
8318 goto sync;
8320 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8321 repo, progress_cb, progress_arg, NULL, NULL);
8322 sync:
8323 sync_err = sync_fileindex(fileindex, fileindex_path);
8324 if (sync_err && err == NULL)
8325 err = sync_err;
8326 done:
8327 free(tree_id);
8328 free(commit_id);
8329 if (commit)
8330 got_object_commit_close(commit);
8331 if (fileindex)
8332 got_fileindex_free(fileindex);
8333 free(fileindex_path);
8335 unlockerr = lock_worktree(worktree, LOCK_SH);
8336 if (unlockerr && err == NULL)
8337 err = unlockerr;
8338 return err;
8341 struct check_stage_ok_arg {
8342 struct got_object_id *head_commit_id;
8343 struct got_worktree *worktree;
8344 struct got_fileindex *fileindex;
8345 struct got_repository *repo;
8346 int have_changes;
8349 static const struct got_error *
8350 check_stage_ok(void *arg, unsigned char status,
8351 unsigned char staged_status, const char *relpath,
8352 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8353 struct got_object_id *commit_id, int dirfd, const char *de_name)
8355 struct check_stage_ok_arg *a = arg;
8356 const struct got_error *err = NULL;
8357 struct got_fileindex_entry *ie;
8358 struct got_object_id base_commit_id;
8359 struct got_object_id *base_commit_idp = NULL;
8360 char *in_repo_path = NULL, *p;
8362 if (status == GOT_STATUS_UNVERSIONED ||
8363 status == GOT_STATUS_NO_CHANGE)
8364 return NULL;
8365 if (status == GOT_STATUS_NONEXISTENT)
8366 return got_error_set_errno(ENOENT, relpath);
8368 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8369 if (ie == NULL)
8370 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8372 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8373 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8374 relpath) == -1)
8375 return got_error_from_errno("asprintf");
8377 if (got_fileindex_entry_has_commit(ie)) {
8378 base_commit_idp = got_fileindex_entry_get_commit_id(
8379 &base_commit_id, ie);
8382 if (status == GOT_STATUS_CONFLICT) {
8383 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8384 goto done;
8385 } else if (status != GOT_STATUS_ADD &&
8386 status != GOT_STATUS_MODIFY &&
8387 status != GOT_STATUS_DELETE) {
8388 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8389 goto done;
8392 a->have_changes = 1;
8394 p = in_repo_path;
8395 while (p[0] == '/')
8396 p++;
8397 err = check_out_of_date(p, status, staged_status,
8398 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8399 GOT_ERR_STAGE_OUT_OF_DATE);
8400 done:
8401 free(in_repo_path);
8402 return err;
8405 struct stage_path_arg {
8406 struct got_worktree *worktree;
8407 struct got_fileindex *fileindex;
8408 struct got_repository *repo;
8409 got_worktree_status_cb status_cb;
8410 void *status_arg;
8411 got_worktree_patch_cb patch_cb;
8412 void *patch_arg;
8413 int staged_something;
8414 int allow_bad_symlinks;
8417 static const struct got_error *
8418 stage_path(void *arg, unsigned char status,
8419 unsigned char staged_status, const char *relpath,
8420 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8421 struct got_object_id *commit_id, int dirfd, const char *de_name)
8423 struct stage_path_arg *a = arg;
8424 const struct got_error *err = NULL;
8425 struct got_fileindex_entry *ie;
8426 char *ondisk_path = NULL, *path_content = NULL;
8427 uint32_t stage;
8428 struct got_object_id *new_staged_blob_id = NULL;
8429 struct stat sb;
8431 if (status == GOT_STATUS_UNVERSIONED)
8432 return NULL;
8434 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8435 if (ie == NULL)
8436 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8438 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8439 relpath)== -1)
8440 return got_error_from_errno("asprintf");
8442 switch (status) {
8443 case GOT_STATUS_ADD:
8444 case GOT_STATUS_MODIFY:
8445 /* XXX could sb.st_mode be passed in by our caller? */
8446 if (lstat(ondisk_path, &sb) == -1) {
8447 err = got_error_from_errno2("lstat", ondisk_path);
8448 break;
8450 if (a->patch_cb) {
8451 if (status == GOT_STATUS_ADD) {
8452 int choice = GOT_PATCH_CHOICE_NONE;
8453 err = (*a->patch_cb)(&choice, a->patch_arg,
8454 status, ie->path, NULL, 1, 1);
8455 if (err)
8456 break;
8457 if (choice != GOT_PATCH_CHOICE_YES)
8458 break;
8459 } else {
8460 err = create_patched_content(&path_content, 0,
8461 staged_blob_id ? staged_blob_id : blob_id,
8462 ondisk_path, dirfd, de_name, ie->path,
8463 a->repo, a->patch_cb, a->patch_arg);
8464 if (err || path_content == NULL)
8465 break;
8468 err = got_object_blob_create(&new_staged_blob_id,
8469 path_content ? path_content : ondisk_path, a->repo);
8470 if (err)
8471 break;
8472 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8473 SHA1_DIGEST_LENGTH);
8474 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8475 stage = GOT_FILEIDX_STAGE_ADD;
8476 else
8477 stage = GOT_FILEIDX_STAGE_MODIFY;
8478 got_fileindex_entry_stage_set(ie, stage);
8479 if (S_ISLNK(sb.st_mode)) {
8480 int is_bad_symlink = 0;
8481 if (!a->allow_bad_symlinks) {
8482 char target_path[PATH_MAX];
8483 ssize_t target_len;
8484 target_len = readlink(ondisk_path, target_path,
8485 sizeof(target_path));
8486 if (target_len == -1) {
8487 err = got_error_from_errno2("readlink",
8488 ondisk_path);
8489 break;
8491 err = is_bad_symlink_target(&is_bad_symlink,
8492 target_path, target_len, ondisk_path,
8493 a->worktree->root_path);
8494 if (err)
8495 break;
8496 if (is_bad_symlink) {
8497 err = got_error_path(ondisk_path,
8498 GOT_ERR_BAD_SYMLINK);
8499 break;
8502 if (is_bad_symlink)
8503 got_fileindex_entry_staged_filetype_set(ie,
8504 GOT_FILEIDX_MODE_BAD_SYMLINK);
8505 else
8506 got_fileindex_entry_staged_filetype_set(ie,
8507 GOT_FILEIDX_MODE_SYMLINK);
8508 } else {
8509 got_fileindex_entry_staged_filetype_set(ie,
8510 GOT_FILEIDX_MODE_REGULAR_FILE);
8512 a->staged_something = 1;
8513 if (a->status_cb == NULL)
8514 break;
8515 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8516 get_staged_status(ie), relpath, blob_id,
8517 new_staged_blob_id, NULL, dirfd, de_name);
8518 if (err)
8519 break;
8521 * When staging the reverse of the staged diff,
8522 * implicitly unstage the file.
8524 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8525 sizeof(ie->blob_sha1)) == 0) {
8526 got_fileindex_entry_stage_set(ie,
8527 GOT_FILEIDX_STAGE_NONE);
8529 break;
8530 case GOT_STATUS_DELETE:
8531 if (staged_status == GOT_STATUS_DELETE)
8532 break;
8533 if (a->patch_cb) {
8534 int choice = GOT_PATCH_CHOICE_NONE;
8535 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8536 ie->path, NULL, 1, 1);
8537 if (err)
8538 break;
8539 if (choice == GOT_PATCH_CHOICE_NO)
8540 break;
8541 if (choice != GOT_PATCH_CHOICE_YES) {
8542 err = got_error(GOT_ERR_PATCH_CHOICE);
8543 break;
8546 stage = GOT_FILEIDX_STAGE_DELETE;
8547 got_fileindex_entry_stage_set(ie, stage);
8548 a->staged_something = 1;
8549 if (a->status_cb == NULL)
8550 break;
8551 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8552 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8553 de_name);
8554 break;
8555 case GOT_STATUS_NO_CHANGE:
8556 break;
8557 case GOT_STATUS_CONFLICT:
8558 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8559 break;
8560 case GOT_STATUS_NONEXISTENT:
8561 err = got_error_set_errno(ENOENT, relpath);
8562 break;
8563 default:
8564 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8565 break;
8568 if (path_content && unlink(path_content) == -1 && err == NULL)
8569 err = got_error_from_errno2("unlink", path_content);
8570 free(path_content);
8571 free(ondisk_path);
8572 free(new_staged_blob_id);
8573 return err;
8576 const struct got_error *
8577 got_worktree_stage(struct got_worktree *worktree,
8578 struct got_pathlist_head *paths,
8579 got_worktree_status_cb status_cb, void *status_arg,
8580 got_worktree_patch_cb patch_cb, void *patch_arg,
8581 int allow_bad_symlinks, struct got_repository *repo)
8583 const struct got_error *err = NULL, *sync_err, *unlockerr;
8584 struct got_pathlist_entry *pe;
8585 struct got_fileindex *fileindex = NULL;
8586 char *fileindex_path = NULL;
8587 struct got_reference *head_ref = NULL;
8588 struct got_object_id *head_commit_id = NULL;
8589 struct check_stage_ok_arg oka;
8590 struct stage_path_arg spa;
8592 err = lock_worktree(worktree, LOCK_EX);
8593 if (err)
8594 return err;
8596 err = got_ref_open(&head_ref, repo,
8597 got_worktree_get_head_ref_name(worktree), 0);
8598 if (err)
8599 goto done;
8600 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8601 if (err)
8602 goto done;
8603 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8604 if (err)
8605 goto done;
8607 /* Check pre-conditions before staging anything. */
8608 oka.head_commit_id = head_commit_id;
8609 oka.worktree = worktree;
8610 oka.fileindex = fileindex;
8611 oka.repo = repo;
8612 oka.have_changes = 0;
8613 TAILQ_FOREACH(pe, paths, entry) {
8614 err = worktree_status(worktree, pe->path, fileindex, repo,
8615 check_stage_ok, &oka, NULL, NULL, 1, 0);
8616 if (err)
8617 goto done;
8619 if (!oka.have_changes) {
8620 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8621 goto done;
8624 spa.worktree = worktree;
8625 spa.fileindex = fileindex;
8626 spa.repo = repo;
8627 spa.patch_cb = patch_cb;
8628 spa.patch_arg = patch_arg;
8629 spa.status_cb = status_cb;
8630 spa.status_arg = status_arg;
8631 spa.staged_something = 0;
8632 spa.allow_bad_symlinks = allow_bad_symlinks;
8633 TAILQ_FOREACH(pe, paths, entry) {
8634 err = worktree_status(worktree, pe->path, fileindex, repo,
8635 stage_path, &spa, NULL, NULL, 1, 0);
8636 if (err)
8637 goto done;
8639 if (!spa.staged_something) {
8640 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8641 goto done;
8644 sync_err = sync_fileindex(fileindex, fileindex_path);
8645 if (sync_err && err == NULL)
8646 err = sync_err;
8647 done:
8648 if (head_ref)
8649 got_ref_close(head_ref);
8650 free(head_commit_id);
8651 free(fileindex_path);
8652 if (fileindex)
8653 got_fileindex_free(fileindex);
8654 unlockerr = lock_worktree(worktree, LOCK_SH);
8655 if (unlockerr && err == NULL)
8656 err = unlockerr;
8657 return err;
8660 struct unstage_path_arg {
8661 struct got_worktree *worktree;
8662 struct got_fileindex *fileindex;
8663 struct got_repository *repo;
8664 got_worktree_checkout_cb progress_cb;
8665 void *progress_arg;
8666 got_worktree_patch_cb patch_cb;
8667 void *patch_arg;
8670 static const struct got_error *
8671 create_unstaged_content(char **path_unstaged_content,
8672 char **path_new_staged_content, struct got_object_id *blob_id,
8673 struct got_object_id *staged_blob_id, const char *relpath,
8674 struct got_repository *repo,
8675 got_worktree_patch_cb patch_cb, void *patch_arg)
8677 const struct got_error *err, *free_err;
8678 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8679 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8680 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8681 struct got_diffreg_result *diffreg_result = NULL;
8682 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8683 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8684 int fd1 = -1, fd2 = -1;
8686 *path_unstaged_content = NULL;
8687 *path_new_staged_content = NULL;
8689 err = got_object_id_str(&label1, blob_id);
8690 if (err)
8691 return err;
8693 fd1 = got_opentempfd();
8694 if (fd1 == -1) {
8695 err = got_error_from_errno("got_opentempfd");
8696 goto done;
8698 fd2 = got_opentempfd();
8699 if (fd2 == -1) {
8700 err = got_error_from_errno("got_opentempfd");
8701 goto done;
8704 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8705 if (err)
8706 goto done;
8708 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
8709 if (err)
8710 goto done;
8712 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8713 if (err)
8714 goto done;
8716 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8717 fd2);
8718 if (err)
8719 goto done;
8721 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
8722 if (err)
8723 goto done;
8725 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8726 if (err)
8727 goto done;
8729 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8730 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8731 if (err)
8732 goto done;
8734 err = got_opentemp_named(path_unstaged_content, &outfile,
8735 "got-unstaged-content", "");
8736 if (err)
8737 goto done;
8738 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8739 "got-new-staged-content", "");
8740 if (err)
8741 goto done;
8743 if (fseek(f1, 0L, SEEK_SET) == -1) {
8744 err = got_ferror(f1, GOT_ERR_IO);
8745 goto done;
8747 if (fseek(f2, 0L, SEEK_SET) == -1) {
8748 err = got_ferror(f2, GOT_ERR_IO);
8749 goto done;
8751 /* Count the number of actual changes in the diff result. */
8752 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8753 struct diff_chunk_context cc = {};
8754 diff_chunk_context_load_change(&cc, &nchunks_used,
8755 diffreg_result->result, n, 0);
8756 nchanges++;
8758 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8759 int choice;
8760 err = apply_or_reject_change(&choice, &nchunks_used,
8761 diffreg_result->result, n, relpath, f1, f2,
8762 &line_cur1, &line_cur2,
8763 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8764 if (err)
8765 goto done;
8766 if (choice == GOT_PATCH_CHOICE_YES)
8767 have_content = 1;
8768 else
8769 have_rejected_content = 1;
8770 if (choice == GOT_PATCH_CHOICE_QUIT)
8771 break;
8773 if (have_content || have_rejected_content)
8774 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8775 outfile, rejectfile);
8776 done:
8777 free(label1);
8778 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8779 err = got_error_from_errno("close");
8780 if (blob)
8781 got_object_blob_close(blob);
8782 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8783 err = got_error_from_errno("close");
8784 if (staged_blob)
8785 got_object_blob_close(staged_blob);
8786 free_err = got_diffreg_result_free(diffreg_result);
8787 if (free_err && err == NULL)
8788 err = free_err;
8789 if (f1 && fclose(f1) == EOF && err == NULL)
8790 err = got_error_from_errno2("fclose", path1);
8791 if (f2 && fclose(f2) == EOF && err == NULL)
8792 err = got_error_from_errno2("fclose", path2);
8793 if (outfile && fclose(outfile) == EOF && err == NULL)
8794 err = got_error_from_errno2("fclose", *path_unstaged_content);
8795 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8796 err = got_error_from_errno2("fclose", *path_new_staged_content);
8797 if (path1 && unlink(path1) == -1 && err == NULL)
8798 err = got_error_from_errno2("unlink", path1);
8799 if (path2 && unlink(path2) == -1 && err == NULL)
8800 err = got_error_from_errno2("unlink", path2);
8801 if (err || !have_content) {
8802 if (*path_unstaged_content &&
8803 unlink(*path_unstaged_content) == -1 && err == NULL)
8804 err = got_error_from_errno2("unlink",
8805 *path_unstaged_content);
8806 free(*path_unstaged_content);
8807 *path_unstaged_content = NULL;
8809 if (err || !have_content || !have_rejected_content) {
8810 if (*path_new_staged_content &&
8811 unlink(*path_new_staged_content) == -1 && err == NULL)
8812 err = got_error_from_errno2("unlink",
8813 *path_new_staged_content);
8814 free(*path_new_staged_content);
8815 *path_new_staged_content = NULL;
8817 free(path1);
8818 free(path2);
8819 return err;
8822 static const struct got_error *
8823 unstage_hunks(struct got_object_id *staged_blob_id,
8824 struct got_blob_object *blob_base,
8825 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8826 const char *ondisk_path, const char *label_orig,
8827 struct got_worktree *worktree, struct got_repository *repo,
8828 got_worktree_patch_cb patch_cb, void *patch_arg,
8829 got_worktree_checkout_cb progress_cb, void *progress_arg)
8831 const struct got_error *err = NULL;
8832 char *path_unstaged_content = NULL;
8833 char *path_new_staged_content = NULL;
8834 char *parent = NULL, *base_path = NULL;
8835 char *blob_base_path = NULL;
8836 struct got_object_id *new_staged_blob_id = NULL;
8837 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8838 struct stat sb;
8840 err = create_unstaged_content(&path_unstaged_content,
8841 &path_new_staged_content, blob_id, staged_blob_id,
8842 ie->path, repo, patch_cb, patch_arg);
8843 if (err)
8844 return err;
8846 if (path_unstaged_content == NULL)
8847 return NULL;
8849 if (path_new_staged_content) {
8850 err = got_object_blob_create(&new_staged_blob_id,
8851 path_new_staged_content, repo);
8852 if (err)
8853 goto done;
8856 f = fopen(path_unstaged_content, "re");
8857 if (f == NULL) {
8858 err = got_error_from_errno2("fopen",
8859 path_unstaged_content);
8860 goto done;
8862 if (fstat(fileno(f), &sb) == -1) {
8863 err = got_error_from_errno2("fstat", path_unstaged_content);
8864 goto done;
8866 if (got_fileindex_entry_staged_filetype_get(ie) ==
8867 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8868 char link_target[PATH_MAX];
8869 size_t r;
8870 r = fread(link_target, 1, sizeof(link_target), f);
8871 if (r == 0 && ferror(f)) {
8872 err = got_error_from_errno("fread");
8873 goto done;
8875 if (r >= sizeof(link_target)) { /* should not happen */
8876 err = got_error(GOT_ERR_NO_SPACE);
8877 goto done;
8879 link_target[r] = '\0';
8880 err = merge_symlink(worktree, blob_base,
8881 ondisk_path, ie->path, label_orig, link_target,
8882 worktree->base_commit_id, repo, progress_cb,
8883 progress_arg);
8884 } else {
8885 int local_changes_subsumed;
8887 err = got_path_dirname(&parent, ondisk_path);
8888 if (err)
8889 return err;
8891 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8892 parent) == -1) {
8893 err = got_error_from_errno("asprintf");
8894 base_path = NULL;
8895 goto done;
8898 err = got_opentemp_named(&blob_base_path, &f_base,
8899 base_path, "");
8900 if (err)
8901 goto done;
8902 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8903 blob_base);
8904 if (err)
8905 goto done;
8908 * In order the run a 3-way merge with a symlink we copy the symlink's
8909 * target path into a temporary file and use that file with diff3.
8911 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8912 err = dump_symlink_target_path_to_file(&f_deriv2,
8913 ondisk_path);
8914 if (err)
8915 goto done;
8916 } else {
8917 int fd;
8918 fd = open(ondisk_path,
8919 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8920 if (fd == -1) {
8921 err = got_error_from_errno2("open", ondisk_path);
8922 goto done;
8924 f_deriv2 = fdopen(fd, "r");
8925 if (f_deriv2 == NULL) {
8926 err = got_error_from_errno2("fdopen", ondisk_path);
8927 close(fd);
8928 goto done;
8932 err = merge_file(&local_changes_subsumed, worktree,
8933 f_base, f, f_deriv2, ondisk_path, ie->path,
8934 got_fileindex_perms_to_st(ie),
8935 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8936 repo, progress_cb, progress_arg);
8938 if (err)
8939 goto done;
8941 if (new_staged_blob_id) {
8942 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8943 SHA1_DIGEST_LENGTH);
8944 } else {
8945 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8946 got_fileindex_entry_staged_filetype_set(ie, 0);
8948 done:
8949 free(new_staged_blob_id);
8950 if (path_unstaged_content &&
8951 unlink(path_unstaged_content) == -1 && err == NULL)
8952 err = got_error_from_errno2("unlink", path_unstaged_content);
8953 if (path_new_staged_content &&
8954 unlink(path_new_staged_content) == -1 && err == NULL)
8955 err = got_error_from_errno2("unlink", path_new_staged_content);
8956 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8957 err = got_error_from_errno2("unlink", blob_base_path);
8958 if (f_base && fclose(f_base) == EOF && err == NULL)
8959 err = got_error_from_errno2("fclose", path_unstaged_content);
8960 if (f && fclose(f) == EOF && err == NULL)
8961 err = got_error_from_errno2("fclose", path_unstaged_content);
8962 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8963 err = got_error_from_errno2("fclose", ondisk_path);
8964 free(path_unstaged_content);
8965 free(path_new_staged_content);
8966 free(blob_base_path);
8967 free(parent);
8968 free(base_path);
8969 return err;
8972 static const struct got_error *
8973 unstage_path(void *arg, unsigned char status,
8974 unsigned char staged_status, const char *relpath,
8975 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8976 struct got_object_id *commit_id, int dirfd, const char *de_name)
8978 const struct got_error *err = NULL;
8979 struct unstage_path_arg *a = arg;
8980 struct got_fileindex_entry *ie;
8981 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8982 char *ondisk_path = NULL;
8983 char *id_str = NULL, *label_orig = NULL;
8984 int local_changes_subsumed;
8985 struct stat sb;
8986 int fd1 = -1, fd2 = -1;
8988 if (staged_status != GOT_STATUS_ADD &&
8989 staged_status != GOT_STATUS_MODIFY &&
8990 staged_status != GOT_STATUS_DELETE)
8991 return NULL;
8993 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8994 if (ie == NULL)
8995 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8997 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8998 == -1)
8999 return got_error_from_errno("asprintf");
9001 err = got_object_id_str(&id_str,
9002 commit_id ? commit_id : a->worktree->base_commit_id);
9003 if (err)
9004 goto done;
9005 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9006 id_str) == -1) {
9007 err = got_error_from_errno("asprintf");
9008 goto done;
9011 fd1 = got_opentempfd();
9012 if (fd1 == -1) {
9013 err = got_error_from_errno("got_opentempfd");
9014 goto done;
9016 fd2 = got_opentempfd();
9017 if (fd2 == -1) {
9018 err = got_error_from_errno("got_opentempfd");
9019 goto done;
9022 switch (staged_status) {
9023 case GOT_STATUS_MODIFY:
9024 err = got_object_open_as_blob(&blob_base, a->repo,
9025 blob_id, 8192, fd1);
9026 if (err)
9027 break;
9028 /* fall through */
9029 case GOT_STATUS_ADD:
9030 if (a->patch_cb) {
9031 if (staged_status == GOT_STATUS_ADD) {
9032 int choice = GOT_PATCH_CHOICE_NONE;
9033 err = (*a->patch_cb)(&choice, a->patch_arg,
9034 staged_status, ie->path, NULL, 1, 1);
9035 if (err)
9036 break;
9037 if (choice != GOT_PATCH_CHOICE_YES)
9038 break;
9039 } else {
9040 err = unstage_hunks(staged_blob_id,
9041 blob_base, blob_id, ie, ondisk_path,
9042 label_orig, a->worktree, a->repo,
9043 a->patch_cb, a->patch_arg,
9044 a->progress_cb, a->progress_arg);
9045 break; /* Done with this file. */
9048 err = got_object_open_as_blob(&blob_staged, a->repo,
9049 staged_blob_id, 8192, fd2);
9050 if (err)
9051 break;
9052 switch (got_fileindex_entry_staged_filetype_get(ie)) {
9053 case GOT_FILEIDX_MODE_BAD_SYMLINK:
9054 case GOT_FILEIDX_MODE_REGULAR_FILE:
9055 err = merge_blob(&local_changes_subsumed, a->worktree,
9056 blob_base, ondisk_path, relpath,
9057 got_fileindex_perms_to_st(ie), label_orig,
9058 blob_staged, commit_id ? commit_id :
9059 a->worktree->base_commit_id, a->repo,
9060 a->progress_cb, a->progress_arg);
9061 break;
9062 case GOT_FILEIDX_MODE_SYMLINK:
9063 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9064 char *staged_target;
9065 err = got_object_blob_read_to_str(
9066 &staged_target, blob_staged);
9067 if (err)
9068 goto done;
9069 err = merge_symlink(a->worktree, blob_base,
9070 ondisk_path, relpath, label_orig,
9071 staged_target, commit_id ? commit_id :
9072 a->worktree->base_commit_id,
9073 a->repo, a->progress_cb, a->progress_arg);
9074 free(staged_target);
9075 } else {
9076 err = merge_blob(&local_changes_subsumed,
9077 a->worktree, blob_base, ondisk_path,
9078 relpath, got_fileindex_perms_to_st(ie),
9079 label_orig, blob_staged,
9080 commit_id ? commit_id :
9081 a->worktree->base_commit_id, a->repo,
9082 a->progress_cb, a->progress_arg);
9084 break;
9085 default:
9086 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9087 break;
9089 if (err == NULL) {
9090 got_fileindex_entry_stage_set(ie,
9091 GOT_FILEIDX_STAGE_NONE);
9092 got_fileindex_entry_staged_filetype_set(ie, 0);
9094 break;
9095 case GOT_STATUS_DELETE:
9096 if (a->patch_cb) {
9097 int choice = GOT_PATCH_CHOICE_NONE;
9098 err = (*a->patch_cb)(&choice, a->patch_arg,
9099 staged_status, ie->path, NULL, 1, 1);
9100 if (err)
9101 break;
9102 if (choice == GOT_PATCH_CHOICE_NO)
9103 break;
9104 if (choice != GOT_PATCH_CHOICE_YES) {
9105 err = got_error(GOT_ERR_PATCH_CHOICE);
9106 break;
9109 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9110 got_fileindex_entry_staged_filetype_set(ie, 0);
9111 err = get_file_status(&status, &sb, ie, ondisk_path,
9112 dirfd, de_name, a->repo);
9113 if (err)
9114 break;
9115 err = (*a->progress_cb)(a->progress_arg, status, relpath);
9116 break;
9118 done:
9119 free(ondisk_path);
9120 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9121 err = got_error_from_errno("close");
9122 if (blob_base)
9123 got_object_blob_close(blob_base);
9124 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9125 err = got_error_from_errno("close");
9126 if (blob_staged)
9127 got_object_blob_close(blob_staged);
9128 free(id_str);
9129 free(label_orig);
9130 return err;
9133 const struct got_error *
9134 got_worktree_unstage(struct got_worktree *worktree,
9135 struct got_pathlist_head *paths,
9136 got_worktree_checkout_cb progress_cb, void *progress_arg,
9137 got_worktree_patch_cb patch_cb, void *patch_arg,
9138 struct got_repository *repo)
9140 const struct got_error *err = NULL, *sync_err, *unlockerr;
9141 struct got_pathlist_entry *pe;
9142 struct got_fileindex *fileindex = NULL;
9143 char *fileindex_path = NULL;
9144 struct unstage_path_arg upa;
9146 err = lock_worktree(worktree, LOCK_EX);
9147 if (err)
9148 return err;
9150 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9151 if (err)
9152 goto done;
9154 upa.worktree = worktree;
9155 upa.fileindex = fileindex;
9156 upa.repo = repo;
9157 upa.progress_cb = progress_cb;
9158 upa.progress_arg = progress_arg;
9159 upa.patch_cb = patch_cb;
9160 upa.patch_arg = patch_arg;
9161 TAILQ_FOREACH(pe, paths, entry) {
9162 err = worktree_status(worktree, pe->path, fileindex, repo,
9163 unstage_path, &upa, NULL, NULL, 1, 0);
9164 if (err)
9165 goto done;
9168 sync_err = sync_fileindex(fileindex, fileindex_path);
9169 if (sync_err && err == NULL)
9170 err = sync_err;
9171 done:
9172 free(fileindex_path);
9173 if (fileindex)
9174 got_fileindex_free(fileindex);
9175 unlockerr = lock_worktree(worktree, LOCK_SH);
9176 if (unlockerr && err == NULL)
9177 err = unlockerr;
9178 return err;
9181 struct report_file_info_arg {
9182 struct got_worktree *worktree;
9183 got_worktree_path_info_cb info_cb;
9184 void *info_arg;
9185 struct got_pathlist_head *paths;
9186 got_cancel_cb cancel_cb;
9187 void *cancel_arg;
9190 static const struct got_error *
9191 report_file_info(void *arg, struct got_fileindex_entry *ie)
9193 struct report_file_info_arg *a = arg;
9194 struct got_pathlist_entry *pe;
9195 struct got_object_id blob_id, staged_blob_id, commit_id;
9196 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9197 struct got_object_id *commit_idp = NULL;
9198 int stage;
9200 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9201 return got_error(GOT_ERR_CANCELLED);
9203 TAILQ_FOREACH(pe, a->paths, entry) {
9204 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9205 got_path_is_child(ie->path, pe->path, pe->path_len))
9206 break;
9208 if (pe == NULL) /* not found */
9209 return NULL;
9211 if (got_fileindex_entry_has_blob(ie))
9212 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9213 stage = got_fileindex_entry_stage_get(ie);
9214 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9215 stage == GOT_FILEIDX_STAGE_ADD) {
9216 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9217 &staged_blob_id, ie);
9220 if (got_fileindex_entry_has_commit(ie))
9221 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9223 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9224 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9227 const struct got_error *
9228 got_worktree_path_info(struct got_worktree *worktree,
9229 struct got_pathlist_head *paths,
9230 got_worktree_path_info_cb info_cb, void *info_arg,
9231 got_cancel_cb cancel_cb, void *cancel_arg)
9234 const struct got_error *err = NULL, *unlockerr;
9235 struct got_fileindex *fileindex = NULL;
9236 char *fileindex_path = NULL;
9237 struct report_file_info_arg arg;
9239 err = lock_worktree(worktree, LOCK_SH);
9240 if (err)
9241 return err;
9243 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9244 if (err)
9245 goto done;
9247 arg.worktree = worktree;
9248 arg.info_cb = info_cb;
9249 arg.info_arg = info_arg;
9250 arg.paths = paths;
9251 arg.cancel_cb = cancel_cb;
9252 arg.cancel_arg = cancel_arg;
9253 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9254 &arg);
9255 done:
9256 free(fileindex_path);
9257 if (fileindex)
9258 got_fileindex_free(fileindex);
9259 unlockerr = lock_worktree(worktree, LOCK_UN);
9260 if (unlockerr && err == NULL)
9261 err = unlockerr;
9262 return err;
9265 static const struct got_error *
9266 patch_check_path(const char *p, char **path, unsigned char *status,
9267 unsigned char *staged_status, struct got_fileindex *fileindex,
9268 struct got_worktree *worktree, struct got_repository *repo)
9270 const struct got_error *err;
9271 struct got_fileindex_entry *ie;
9272 struct stat sb;
9273 char *ondisk_path = NULL;
9275 err = got_worktree_resolve_path(path, worktree, p);
9276 if (err)
9277 return err;
9279 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9280 *path[0] ? "/" : "", *path) == -1)
9281 return got_error_from_errno("asprintf");
9283 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9284 if (ie) {
9285 *staged_status = get_staged_status(ie);
9286 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9287 repo);
9288 if (err)
9289 goto done;
9290 } else {
9291 *staged_status = GOT_STATUS_NO_CHANGE;
9292 *status = GOT_STATUS_UNVERSIONED;
9293 if (lstat(ondisk_path, &sb) == -1) {
9294 if (errno != ENOENT) {
9295 err = got_error_from_errno2("lstat",
9296 ondisk_path);
9297 goto done;
9299 *status = GOT_STATUS_NONEXISTENT;
9303 done:
9304 free(ondisk_path);
9305 return err;
9308 static const struct got_error *
9309 patch_can_rm(const char *path, unsigned char status,
9310 unsigned char staged_status)
9312 if (status == GOT_STATUS_NONEXISTENT)
9313 return got_error_set_errno(ENOENT, path);
9314 if (status != GOT_STATUS_NO_CHANGE &&
9315 status != GOT_STATUS_ADD &&
9316 status != GOT_STATUS_MODIFY &&
9317 status != GOT_STATUS_MODE_CHANGE)
9318 return got_error_path(path, GOT_ERR_FILE_STATUS);
9319 if (staged_status == GOT_STATUS_DELETE)
9320 return got_error_path(path, GOT_ERR_FILE_STATUS);
9321 return NULL;
9324 static const struct got_error *
9325 patch_can_add(const char *path, unsigned char status)
9327 if (status != GOT_STATUS_NONEXISTENT)
9328 return got_error_path(path, GOT_ERR_FILE_STATUS);
9329 return NULL;
9332 static const struct got_error *
9333 patch_can_edit(const char *path, unsigned char status,
9334 unsigned char staged_status)
9336 if (status == GOT_STATUS_NONEXISTENT)
9337 return got_error_set_errno(ENOENT, path);
9338 if (status != GOT_STATUS_NO_CHANGE &&
9339 status != GOT_STATUS_ADD &&
9340 status != GOT_STATUS_MODIFY)
9341 return got_error_path(path, GOT_ERR_FILE_STATUS);
9342 if (staged_status == GOT_STATUS_DELETE)
9343 return got_error_path(path, GOT_ERR_FILE_STATUS);
9344 return NULL;
9347 const struct got_error *
9348 got_worktree_patch_prepare(struct got_fileindex **fileindex,
9349 char **fileindex_path, struct got_worktree *worktree)
9351 return open_fileindex(fileindex, fileindex_path, worktree);
9354 const struct got_error *
9355 got_worktree_patch_check_path(const char *old, const char *new,
9356 char **oldpath, char **newpath, struct got_worktree *worktree,
9357 struct got_repository *repo, struct got_fileindex *fileindex)
9359 const struct got_error *err = NULL;
9360 int file_renamed = 0;
9361 unsigned char status_old, staged_status_old;
9362 unsigned char status_new, staged_status_new;
9364 *oldpath = NULL;
9365 *newpath = NULL;
9367 err = patch_check_path(old != NULL ? old : new, oldpath,
9368 &status_old, &staged_status_old, fileindex, worktree, repo);
9369 if (err)
9370 goto done;
9372 err = patch_check_path(new != NULL ? new : old, newpath,
9373 &status_new, &staged_status_new, fileindex, worktree, repo);
9374 if (err)
9375 goto done;
9377 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9378 file_renamed = 1;
9380 if (old != NULL && new == NULL)
9381 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9382 else if (file_renamed) {
9383 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9384 if (err == NULL)
9385 err = patch_can_add(*newpath, status_new);
9386 } else if (old == NULL)
9387 err = patch_can_add(*newpath, status_new);
9388 else
9389 err = patch_can_edit(*newpath, status_new, staged_status_new);
9391 done:
9392 if (err) {
9393 free(*oldpath);
9394 *oldpath = NULL;
9395 free(*newpath);
9396 *newpath = NULL;
9398 return err;
9401 const struct got_error *
9402 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9403 struct got_worktree *worktree, struct got_fileindex *fileindex,
9404 got_worktree_checkout_cb progress_cb, void *progress_arg)
9406 struct schedule_addition_args saa;
9408 memset(&saa, 0, sizeof(saa));
9409 saa.worktree = worktree;
9410 saa.fileindex = fileindex;
9411 saa.progress_cb = progress_cb;
9412 saa.progress_arg = progress_arg;
9413 saa.repo = repo;
9415 return worktree_status(worktree, path, fileindex, repo,
9416 schedule_addition, &saa, NULL, NULL, 1, 0);
9419 const struct got_error *
9420 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9421 struct got_worktree *worktree, struct got_fileindex *fileindex,
9422 got_worktree_delete_cb progress_cb, void *progress_arg)
9424 struct schedule_deletion_args sda;
9426 memset(&sda, 0, sizeof(sda));
9427 sda.worktree = worktree;
9428 sda.fileindex = fileindex;
9429 sda.progress_cb = progress_cb;
9430 sda.progress_arg = progress_arg;
9431 sda.repo = repo;
9432 sda.delete_local_mods = 0;
9433 sda.keep_on_disk = 0;
9434 sda.ignore_missing_paths = 0;
9435 sda.status_codes = NULL;
9437 return worktree_status(worktree, path, fileindex, repo,
9438 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9441 const struct got_error *
9442 got_worktree_patch_complete(struct got_fileindex *fileindex,
9443 const char *fileindex_path)
9445 const struct got_error *err = NULL;
9447 err = sync_fileindex(fileindex, fileindex_path);
9448 got_fileindex_free(fileindex);
9450 return err;