Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/stat.h>
18 #include <sys/queue.h>
19 #include <sys/tree.h>
21 #include <dirent.h>
22 #include <limits.h>
23 #include <stddef.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <time.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <sha1.h>
32 #include <zlib.h>
33 #include <fnmatch.h>
34 #include <libgen.h>
35 #include <uuid.h>
36 #include <util.h>
38 #include "got_error.h"
39 #include "got_repository.h"
40 #include "got_reference.h"
41 #include "got_object.h"
42 #include "got_path.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_opentemp.h"
46 #include "got_diff.h"
48 #include "got_lib_worktree.h"
49 #include "got_lib_sha1.h"
50 #include "got_lib_fileindex.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_object_idset.h"
57 #include "got_lib_diff.h"
58 #include "got_lib_gotconfig.h"
60 #ifndef MIN
61 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
62 #endif
64 #define GOT_MERGE_LABEL_MERGED "merged change"
65 #define GOT_MERGE_LABEL_BASE "3-way merge base"
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), 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), 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, base_path);
994 if (err)
995 goto done;
996 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
997 blob_orig);
998 if (err)
999 goto done;
1000 free(base_path);
1001 } else {
1003 * No common ancestor exists. This is an "add vs add" conflict
1004 * and we simply use an empty ancestor file to make both files
1005 * appear in the merged result in their entirety.
1007 f_orig = got_opentemp();
1008 if (f_orig == NULL) {
1009 err = got_error_from_errno("got_opentemp");
1010 goto done;
1014 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1015 err = got_error_from_errno("asprintf");
1016 base_path = NULL;
1017 goto done;
1020 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1021 if (err)
1022 goto done;
1023 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1024 blob_deriv);
1025 if (err)
1026 goto done;
1028 err = got_object_id_str(&id_str, deriv_base_commit_id);
1029 if (err)
1030 goto done;
1031 if (asprintf(&label_deriv, "%s: commit %s",
1032 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1033 err = got_error_from_errno("asprintf");
1034 goto done;
1038 * In order the run a 3-way merge with a symlink we copy the symlink's
1039 * target path into a temporary file and use that file with diff3.
1041 if (S_ISLNK(st_mode)) {
1042 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1043 if (err)
1044 goto done;
1045 } else {
1046 int fd;
1047 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1048 if (fd == -1) {
1049 err = got_error_from_errno2("open", ondisk_path);
1050 goto done;
1052 f_deriv2 = fdopen(fd, "r");
1053 if (f_deriv2 == NULL) {
1054 err = got_error_from_errno2("fdopen", ondisk_path);
1055 close(fd);
1056 goto done;
1060 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1061 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1062 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1063 done:
1064 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1065 err = got_error_from_errno("fclose");
1066 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1067 err = got_error_from_errno("fclose");
1068 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1069 err = got_error_from_errno("fclose");
1070 free(base_path);
1071 if (blob_orig_path) {
1072 unlink(blob_orig_path);
1073 free(blob_orig_path);
1075 if (blob_deriv_path) {
1076 unlink(blob_deriv_path);
1077 free(blob_deriv_path);
1079 free(id_str);
1080 free(label_deriv);
1081 free(parent);
1082 return err;
1085 static const struct got_error *
1086 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1087 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1088 int wt_fd, const char *path, struct got_object_id *blob_id)
1090 const struct got_error *err = NULL;
1091 struct got_fileindex_entry *new_ie;
1093 *new_iep = NULL;
1095 err = got_fileindex_entry_alloc(&new_ie, path);
1096 if (err)
1097 return err;
1099 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1100 blob_id->sha1, base_commit_id->sha1, 1);
1101 if (err)
1102 goto done;
1104 err = got_fileindex_entry_add(fileindex, new_ie);
1105 done:
1106 if (err)
1107 got_fileindex_entry_free(new_ie);
1108 else
1109 *new_iep = new_ie;
1110 return err;
1113 static mode_t
1114 get_ondisk_perms(int executable, mode_t st_mode)
1116 mode_t xbits = S_IXUSR;
1118 if (executable) {
1119 /* Map read bits to execute bits. */
1120 if (st_mode & S_IRGRP)
1121 xbits |= S_IXGRP;
1122 if (st_mode & S_IROTH)
1123 xbits |= S_IXOTH;
1124 return st_mode | xbits;
1127 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1130 /* forward declaration */
1131 static const struct got_error *
1132 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1133 const char *path, mode_t te_mode, mode_t st_mode,
1134 struct got_blob_object *blob, int restoring_missing_file,
1135 int reverting_versioned_file, int installing_bad_symlink,
1136 int path_is_unversioned, struct got_repository *repo,
1137 got_worktree_checkout_cb progress_cb, void *progress_arg);
1140 * This function assumes that the provided symlink target points at a
1141 * safe location in the work tree!
1143 static const struct got_error *
1144 replace_existing_symlink(int *did_something, const char *ondisk_path,
1145 const char *target_path, size_t target_len)
1147 const struct got_error *err = NULL;
1148 ssize_t elen;
1149 char etarget[PATH_MAX];
1150 int fd;
1152 *did_something = 0;
1155 * "Bad" symlinks (those pointing outside the work tree or into the
1156 * .got directory) are installed in the work tree as a regular file
1157 * which contains the bad symlink target path.
1158 * The new symlink target has already been checked for safety by our
1159 * caller. If we can successfully open a regular file then we simply
1160 * replace this file with a symlink below.
1162 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1163 if (fd == -1) {
1164 if (!got_err_open_nofollow_on_symlink())
1165 return got_error_from_errno2("open", ondisk_path);
1167 /* We are updating an existing on-disk symlink. */
1168 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1169 if (elen == -1)
1170 return got_error_from_errno2("readlink", ondisk_path);
1172 if (elen == target_len &&
1173 memcmp(etarget, target_path, target_len) == 0)
1174 return NULL; /* nothing to do */
1177 *did_something = 1;
1178 err = update_symlink(ondisk_path, target_path, target_len);
1179 if (fd != -1 && close(fd) == -1 && err == NULL)
1180 err = got_error_from_errno2("close", ondisk_path);
1181 return err;
1184 static const struct got_error *
1185 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1186 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1188 const struct got_error *err = NULL;
1189 char canonpath[PATH_MAX];
1190 char *path_got = NULL;
1192 *is_bad_symlink = 0;
1194 if (target_len >= sizeof(canonpath)) {
1195 *is_bad_symlink = 1;
1196 return NULL;
1200 * We do not use realpath(3) to resolve the symlink's target
1201 * path because we don't want to resolve symlinks recursively.
1202 * Instead we make the path absolute and then canonicalize it.
1203 * Relative symlink target lookup should begin at the directory
1204 * in which the blob object is being installed.
1206 if (!got_path_is_absolute(target_path)) {
1207 char *abspath, *parent;
1208 err = got_path_dirname(&parent, ondisk_path);
1209 if (err)
1210 return err;
1211 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1212 free(parent);
1213 return got_error_from_errno("asprintf");
1215 free(parent);
1216 if (strlen(abspath) >= sizeof(canonpath)) {
1217 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1218 free(abspath);
1219 return err;
1221 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1222 free(abspath);
1223 if (err)
1224 return err;
1225 } else {
1226 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1227 if (err)
1228 return err;
1231 /* Only allow symlinks pointing at paths within the work tree. */
1232 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1233 *is_bad_symlink = 1;
1234 return NULL;
1237 /* Do not allow symlinks pointing into the .got directory. */
1238 if (asprintf(&path_got, "%s/%s", wtroot_path,
1239 GOT_WORKTREE_GOT_DIR) == -1)
1240 return got_error_from_errno("asprintf");
1241 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1242 *is_bad_symlink = 1;
1244 free(path_got);
1245 return NULL;
1248 static const struct got_error *
1249 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1250 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1251 int restoring_missing_file, int reverting_versioned_file,
1252 int path_is_unversioned, int allow_bad_symlinks,
1253 struct got_repository *repo,
1254 got_worktree_checkout_cb progress_cb, void *progress_arg)
1256 const struct got_error *err = NULL;
1257 char target_path[PATH_MAX];
1258 size_t len, target_len = 0;
1259 char *path_got = NULL;
1260 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1261 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1263 *is_bad_symlink = 0;
1266 * Blob object content specifies the target path of the link.
1267 * If a symbolic link cannot be installed we instead create
1268 * a regular file which contains the link target path stored
1269 * in the blob object.
1271 do {
1272 err = got_object_blob_read_block(&len, blob);
1273 if (len + target_len >= sizeof(target_path)) {
1274 /* Path too long; install as a regular file. */
1275 *is_bad_symlink = 1;
1276 got_object_blob_rewind(blob);
1277 return install_blob(worktree, ondisk_path, path,
1278 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1279 restoring_missing_file, reverting_versioned_file,
1280 1, path_is_unversioned, repo, progress_cb,
1281 progress_arg);
1283 if (len > 0) {
1284 /* Skip blob object header first time around. */
1285 memcpy(target_path + target_len, buf + hdrlen,
1286 len - hdrlen);
1287 target_len += len - hdrlen;
1288 hdrlen = 0;
1290 } while (len != 0);
1291 target_path[target_len] = '\0';
1293 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1294 ondisk_path, worktree->root_path);
1295 if (err)
1296 return err;
1298 if (*is_bad_symlink && !allow_bad_symlinks) {
1299 /* install as a regular file */
1300 got_object_blob_rewind(blob);
1301 err = install_blob(worktree, ondisk_path, path,
1302 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1303 restoring_missing_file, reverting_versioned_file, 1,
1304 path_is_unversioned, repo, progress_cb, progress_arg);
1305 goto done;
1308 if (symlink(target_path, ondisk_path) == -1) {
1309 if (errno == EEXIST) {
1310 int symlink_replaced;
1311 if (path_is_unversioned) {
1312 err = (*progress_cb)(progress_arg,
1313 GOT_STATUS_UNVERSIONED, path);
1314 goto done;
1316 err = replace_existing_symlink(&symlink_replaced,
1317 ondisk_path, target_path, target_len);
1318 if (err)
1319 goto done;
1320 if (progress_cb) {
1321 if (symlink_replaced) {
1322 err = (*progress_cb)(progress_arg,
1323 reverting_versioned_file ?
1324 GOT_STATUS_REVERT :
1325 GOT_STATUS_UPDATE, path);
1326 } else {
1327 err = (*progress_cb)(progress_arg,
1328 GOT_STATUS_EXISTS, path);
1331 goto done; /* Nothing else to do. */
1334 if (errno == ENOENT) {
1335 char *parent;
1336 err = got_path_dirname(&parent, ondisk_path);
1337 if (err)
1338 goto done;
1339 err = add_dir_on_disk(worktree, parent);
1340 free(parent);
1341 if (err)
1342 goto done;
1344 * Retry, and fall through to error handling
1345 * below if this second attempt fails.
1347 if (symlink(target_path, ondisk_path) != -1) {
1348 err = NULL; /* success */
1349 goto done;
1353 /* Handle errors from first or second creation attempt. */
1354 if (errno == ENAMETOOLONG) {
1355 /* bad target path; install as a regular file */
1356 *is_bad_symlink = 1;
1357 got_object_blob_rewind(blob);
1358 err = install_blob(worktree, ondisk_path, path,
1359 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1360 restoring_missing_file, reverting_versioned_file, 1,
1361 path_is_unversioned, repo,
1362 progress_cb, progress_arg);
1363 } else if (errno == ENOTDIR) {
1364 err = got_error_path(ondisk_path,
1365 GOT_ERR_FILE_OBSTRUCTED);
1366 } else {
1367 err = got_error_from_errno3("symlink",
1368 target_path, ondisk_path);
1370 } else if (progress_cb)
1371 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1372 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1373 done:
1374 free(path_got);
1375 return err;
1378 static const struct got_error *
1379 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1380 const char *path, mode_t te_mode, mode_t st_mode,
1381 struct got_blob_object *blob, int restoring_missing_file,
1382 int reverting_versioned_file, int installing_bad_symlink,
1383 int path_is_unversioned, struct got_repository *repo,
1384 got_worktree_checkout_cb progress_cb, void *progress_arg)
1386 const struct got_error *err = NULL;
1387 int fd = -1;
1388 size_t len, hdrlen;
1389 int update = 0;
1390 char *tmppath = NULL;
1392 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1393 O_CLOEXEC, GOT_DEFAULT_FILE_MODE);
1394 if (fd == -1) {
1395 if (errno == ENOENT) {
1396 char *parent;
1397 err = got_path_dirname(&parent, path);
1398 if (err)
1399 return err;
1400 err = add_dir_on_disk(worktree, parent);
1401 free(parent);
1402 if (err)
1403 return err;
1404 fd = open(ondisk_path,
1405 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1406 GOT_DEFAULT_FILE_MODE);
1407 if (fd == -1)
1408 return got_error_from_errno2("open",
1409 ondisk_path);
1410 } else if (errno == EEXIST) {
1411 if (path_is_unversioned) {
1412 err = (*progress_cb)(progress_arg,
1413 GOT_STATUS_UNVERSIONED, path);
1414 goto done;
1416 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1417 !S_ISREG(st_mode) && !installing_bad_symlink) {
1418 /* TODO file is obstructed; do something */
1419 err = got_error_path(ondisk_path,
1420 GOT_ERR_FILE_OBSTRUCTED);
1421 goto done;
1422 } else {
1423 err = got_opentemp_named_fd(&tmppath, &fd,
1424 ondisk_path);
1425 if (err)
1426 goto done;
1427 update = 1;
1429 } else
1430 return got_error_from_errno2("open", ondisk_path);
1433 if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1434 err = got_error_from_errno2("fchmod",
1435 update ? tmppath : ondisk_path);
1436 goto done;
1439 if (progress_cb) {
1440 if (restoring_missing_file)
1441 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1442 path);
1443 else if (reverting_versioned_file)
1444 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1445 path);
1446 else
1447 err = (*progress_cb)(progress_arg,
1448 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1449 if (err)
1450 goto done;
1453 hdrlen = got_object_blob_get_hdrlen(blob);
1454 do {
1455 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1456 err = got_object_blob_read_block(&len, blob);
1457 if (err)
1458 break;
1459 if (len > 0) {
1460 /* Skip blob object header first time around. */
1461 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1462 if (outlen == -1) {
1463 err = got_error_from_errno("write");
1464 goto done;
1465 } else if (outlen != len - hdrlen) {
1466 err = got_error(GOT_ERR_IO);
1467 goto done;
1469 hdrlen = 0;
1471 } while (len != 0);
1473 if (fsync(fd) != 0) {
1474 err = got_error_from_errno("fsync");
1475 goto done;
1478 if (update) {
1479 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1480 err = got_error_from_errno2("unlink", ondisk_path);
1481 goto done;
1483 if (rename(tmppath, ondisk_path) != 0) {
1484 err = got_error_from_errno3("rename", tmppath,
1485 ondisk_path);
1486 goto done;
1488 free(tmppath);
1489 tmppath = NULL;
1492 done:
1493 if (fd != -1 && close(fd) == -1 && err == NULL)
1494 err = got_error_from_errno("close");
1495 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1496 err = got_error_from_errno2("unlink", tmppath);
1497 free(tmppath);
1498 return err;
1501 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1502 static const struct got_error *
1503 get_modified_file_content_status(unsigned char *status, FILE *f)
1505 const struct got_error *err = NULL;
1506 const char *markers[3] = {
1507 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1508 GOT_DIFF_CONFLICT_MARKER_SEP,
1509 GOT_DIFF_CONFLICT_MARKER_END
1511 int i = 0;
1512 char *line = NULL;
1513 size_t linesize = 0;
1514 ssize_t linelen;
1516 while (*status == GOT_STATUS_MODIFY) {
1517 linelen = getline(&line, &linesize, f);
1518 if (linelen == -1) {
1519 if (feof(f))
1520 break;
1521 err = got_ferror(f, GOT_ERR_IO);
1522 break;
1525 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1526 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1527 == 0)
1528 *status = GOT_STATUS_CONFLICT;
1529 else
1530 i++;
1533 free(line);
1535 return err;
1538 static int
1539 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1541 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1542 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1545 static int
1546 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1548 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1549 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1550 ie->mtime_sec == sb->st_mtim.tv_sec &&
1551 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1552 ie->size == (sb->st_size & 0xffffffff) &&
1553 !xbit_differs(ie, sb->st_mode));
1556 static unsigned char
1557 get_staged_status(struct got_fileindex_entry *ie)
1559 switch (got_fileindex_entry_stage_get(ie)) {
1560 case GOT_FILEIDX_STAGE_ADD:
1561 return GOT_STATUS_ADD;
1562 case GOT_FILEIDX_STAGE_DELETE:
1563 return GOT_STATUS_DELETE;
1564 case GOT_FILEIDX_STAGE_MODIFY:
1565 return GOT_STATUS_MODIFY;
1566 default:
1567 return GOT_STATUS_NO_CHANGE;
1571 static const struct got_error *
1572 get_symlink_modification_status(unsigned char *status,
1573 struct got_fileindex_entry *ie, const char *abspath,
1574 int dirfd, const char *de_name, struct got_blob_object *blob)
1576 const struct got_error *err = NULL;
1577 char target_path[PATH_MAX];
1578 char etarget[PATH_MAX];
1579 ssize_t elen;
1580 size_t len, target_len = 0;
1581 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1582 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1584 *status = GOT_STATUS_NO_CHANGE;
1586 /* Blob object content specifies the target path of the link. */
1587 do {
1588 err = got_object_blob_read_block(&len, blob);
1589 if (err)
1590 return err;
1591 if (len + target_len >= sizeof(target_path)) {
1593 * Should not happen. The blob contents were OK
1594 * when this symlink was installed.
1596 return got_error(GOT_ERR_NO_SPACE);
1598 if (len > 0) {
1599 /* Skip blob object header first time around. */
1600 memcpy(target_path + target_len, buf + hdrlen,
1601 len - hdrlen);
1602 target_len += len - hdrlen;
1603 hdrlen = 0;
1605 } while (len != 0);
1606 target_path[target_len] = '\0';
1608 if (dirfd != -1) {
1609 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1610 if (elen == -1)
1611 return got_error_from_errno2("readlinkat", abspath);
1612 } else {
1613 elen = readlink(abspath, etarget, sizeof(etarget));
1614 if (elen == -1)
1615 return got_error_from_errno2("readlink", abspath);
1618 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1619 *status = GOT_STATUS_MODIFY;
1621 return NULL;
1624 static const struct got_error *
1625 get_file_status(unsigned char *status, struct stat *sb,
1626 struct got_fileindex_entry *ie, const char *abspath,
1627 int dirfd, const char *de_name, struct got_repository *repo)
1629 const struct got_error *err = NULL;
1630 struct got_object_id id;
1631 size_t hdrlen;
1632 int fd = -1;
1633 FILE *f = NULL;
1634 uint8_t fbuf[8192];
1635 struct got_blob_object *blob = NULL;
1636 size_t flen, blen;
1637 unsigned char staged_status = get_staged_status(ie);
1639 *status = GOT_STATUS_NO_CHANGE;
1640 memset(sb, 0, sizeof(*sb));
1643 * Whenever the caller provides a directory descriptor and a
1644 * directory entry name for the file, use them! This prevents
1645 * race conditions if filesystem paths change beneath our feet.
1647 if (dirfd != -1) {
1648 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1649 if (errno == ENOENT) {
1650 if (got_fileindex_entry_has_file_on_disk(ie))
1651 *status = GOT_STATUS_MISSING;
1652 else
1653 *status = GOT_STATUS_DELETE;
1654 goto done;
1656 err = got_error_from_errno2("fstatat", abspath);
1657 goto done;
1659 } else {
1660 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1661 if (fd == -1 && errno != ENOENT &&
1662 !got_err_open_nofollow_on_symlink())
1663 return got_error_from_errno2("open", abspath);
1664 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1665 if (lstat(abspath, sb) == -1)
1666 return got_error_from_errno2("lstat", abspath);
1667 } else if (fd == -1 || fstat(fd, sb) == -1) {
1668 if (errno == ENOENT) {
1669 if (got_fileindex_entry_has_file_on_disk(ie))
1670 *status = GOT_STATUS_MISSING;
1671 else
1672 *status = GOT_STATUS_DELETE;
1673 goto done;
1675 err = got_error_from_errno2("fstat", abspath);
1676 goto done;
1680 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1681 *status = GOT_STATUS_OBSTRUCTED;
1682 goto done;
1685 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1686 *status = GOT_STATUS_DELETE;
1687 goto done;
1688 } else if (!got_fileindex_entry_has_blob(ie) &&
1689 staged_status != GOT_STATUS_ADD) {
1690 *status = GOT_STATUS_ADD;
1691 goto done;
1694 if (!stat_info_differs(ie, sb))
1695 goto done;
1697 if (S_ISLNK(sb->st_mode) &&
1698 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1699 *status = GOT_STATUS_MODIFY;
1700 goto done;
1703 if (staged_status == GOT_STATUS_MODIFY ||
1704 staged_status == GOT_STATUS_ADD)
1705 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1706 else
1707 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1709 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1710 if (err)
1711 goto done;
1713 if (S_ISLNK(sb->st_mode)) {
1714 err = get_symlink_modification_status(status, ie,
1715 abspath, dirfd, de_name, blob);
1716 goto done;
1719 if (dirfd != -1) {
1720 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1721 if (fd == -1) {
1722 err = got_error_from_errno2("openat", abspath);
1723 goto done;
1727 f = fdopen(fd, "r");
1728 if (f == NULL) {
1729 err = got_error_from_errno2("fdopen", abspath);
1730 goto done;
1732 fd = -1;
1733 hdrlen = got_object_blob_get_hdrlen(blob);
1734 for (;;) {
1735 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1736 err = got_object_blob_read_block(&blen, blob);
1737 if (err)
1738 goto done;
1739 /* Skip length of blob object header first time around. */
1740 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1741 if (flen == 0 && ferror(f)) {
1742 err = got_error_from_errno("fread");
1743 goto done;
1745 if (blen - hdrlen == 0) {
1746 if (flen != 0)
1747 *status = GOT_STATUS_MODIFY;
1748 break;
1749 } else if (flen == 0) {
1750 if (blen - hdrlen != 0)
1751 *status = GOT_STATUS_MODIFY;
1752 break;
1753 } else if (blen - hdrlen == flen) {
1754 /* Skip blob object header first time around. */
1755 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1756 *status = GOT_STATUS_MODIFY;
1757 break;
1759 } else {
1760 *status = GOT_STATUS_MODIFY;
1761 break;
1763 hdrlen = 0;
1766 if (*status == GOT_STATUS_MODIFY) {
1767 rewind(f);
1768 err = get_modified_file_content_status(status, f);
1769 } else if (xbit_differs(ie, sb->st_mode))
1770 *status = GOT_STATUS_MODE_CHANGE;
1771 done:
1772 if (blob)
1773 got_object_blob_close(blob);
1774 if (f != NULL && fclose(f) == EOF && err == NULL)
1775 err = got_error_from_errno2("fclose", abspath);
1776 if (fd != -1 && close(fd) == -1 && err == NULL)
1777 err = got_error_from_errno2("close", abspath);
1778 return err;
1782 * Update timestamps in the file index if a file is unmodified and
1783 * we had to run a full content comparison to find out.
1785 static const struct got_error *
1786 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1787 struct got_fileindex_entry *ie, struct stat *sb)
1789 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1790 return got_fileindex_entry_update(ie, wt_fd, path,
1791 ie->blob_sha1, ie->commit_sha1, 1);
1793 return NULL;
1796 static const struct got_error *
1797 update_blob(struct got_worktree *worktree,
1798 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1799 struct got_tree_entry *te, const char *path,
1800 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1801 void *progress_arg)
1803 const struct got_error *err = NULL;
1804 struct got_blob_object *blob = NULL;
1805 char *ondisk_path;
1806 unsigned char status = GOT_STATUS_NO_CHANGE;
1807 struct stat sb;
1809 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1810 return got_error_from_errno("asprintf");
1812 if (ie) {
1813 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1814 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1815 goto done;
1817 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1818 repo);
1819 if (err)
1820 goto done;
1821 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1822 sb.st_mode = got_fileindex_perms_to_st(ie);
1823 } else {
1824 if (stat(ondisk_path, &sb) == -1) {
1825 if (errno != ENOENT) {
1826 err = got_error_from_errno2("stat",
1827 ondisk_path);
1828 goto done;
1830 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1831 status = GOT_STATUS_UNVERSIONED;
1832 } else {
1833 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1834 status = GOT_STATUS_UNVERSIONED;
1835 else
1836 status = GOT_STATUS_OBSTRUCTED;
1840 if (status == GOT_STATUS_OBSTRUCTED) {
1841 if (ie)
1842 got_fileindex_entry_mark_skipped(ie);
1843 err = (*progress_cb)(progress_arg, status, path);
1844 goto done;
1846 if (status == GOT_STATUS_CONFLICT) {
1847 if (ie)
1848 got_fileindex_entry_mark_skipped(ie);
1849 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1850 path);
1851 goto done;
1854 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1855 (S_ISLNK(te->mode) ||
1856 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1858 * This is a regular file or an installed bad symlink.
1859 * If the file index indicates that this file is already
1860 * up-to-date with respect to the repository we can skip
1861 * updating contents of this file.
1863 if (got_fileindex_entry_has_commit(ie) &&
1864 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1865 SHA1_DIGEST_LENGTH) == 0) {
1866 /* Same commit. */
1867 err = sync_timestamps(worktree->root_fd,
1868 path, status, ie, &sb);
1869 if (err)
1870 goto done;
1871 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1872 path);
1873 goto done;
1875 if (got_fileindex_entry_has_blob(ie) &&
1876 memcmp(ie->blob_sha1, te->id.sha1,
1877 SHA1_DIGEST_LENGTH) == 0) {
1878 /* Different commit but the same blob. */
1879 err = sync_timestamps(worktree->root_fd,
1880 path, status, ie, &sb);
1881 if (err)
1882 goto done;
1883 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1884 path);
1885 goto done;
1889 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1890 if (err)
1891 goto done;
1893 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1894 int update_timestamps;
1895 struct got_blob_object *blob2 = NULL;
1896 char *label_orig = NULL;
1897 if (got_fileindex_entry_has_blob(ie)) {
1898 struct got_object_id id2;
1899 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1900 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1901 if (err)
1902 goto done;
1904 if (got_fileindex_entry_has_commit(ie)) {
1905 char id_str[SHA1_DIGEST_STRING_LENGTH];
1906 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1907 sizeof(id_str)) == NULL) {
1908 err = got_error_path(id_str,
1909 GOT_ERR_BAD_OBJ_ID_STR);
1910 goto done;
1912 if (asprintf(&label_orig, "%s: commit %s",
1913 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1914 err = got_error_from_errno("asprintf");
1915 goto done;
1918 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1919 char *link_target;
1920 err = got_object_blob_read_to_str(&link_target, blob);
1921 if (err)
1922 goto done;
1923 err = merge_symlink(worktree, blob2, ondisk_path, path,
1924 label_orig, link_target, worktree->base_commit_id,
1925 repo, progress_cb, progress_arg);
1926 free(link_target);
1927 } else {
1928 err = merge_blob(&update_timestamps, worktree, blob2,
1929 ondisk_path, path, sb.st_mode, label_orig, blob,
1930 worktree->base_commit_id, repo,
1931 progress_cb, progress_arg);
1933 free(label_orig);
1934 if (blob2)
1935 got_object_blob_close(blob2);
1936 if (err)
1937 goto done;
1939 * Do not update timestamps of files with local changes.
1940 * Otherwise, a future status walk would treat them as
1941 * unmodified files again.
1943 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1944 blob->id.sha1, worktree->base_commit_id->sha1,
1945 update_timestamps);
1946 } else if (status == GOT_STATUS_MODE_CHANGE) {
1947 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1948 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1949 } else if (status == GOT_STATUS_DELETE) {
1950 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1951 if (err)
1952 goto done;
1953 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1954 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1955 if (err)
1956 goto done;
1957 } else {
1958 int is_bad_symlink = 0;
1959 if (S_ISLNK(te->mode)) {
1960 err = install_symlink(&is_bad_symlink, worktree,
1961 ondisk_path, path, blob,
1962 status == GOT_STATUS_MISSING, 0,
1963 status == GOT_STATUS_UNVERSIONED, 0,
1964 repo, progress_cb, progress_arg);
1965 } else {
1966 err = install_blob(worktree, ondisk_path, path,
1967 te->mode, sb.st_mode, blob,
1968 status == GOT_STATUS_MISSING, 0, 0,
1969 status == GOT_STATUS_UNVERSIONED, repo,
1970 progress_cb, progress_arg);
1972 if (err)
1973 goto done;
1975 if (ie) {
1976 err = got_fileindex_entry_update(ie,
1977 worktree->root_fd, path, blob->id.sha1,
1978 worktree->base_commit_id->sha1, 1);
1979 } else {
1980 err = create_fileindex_entry(&ie, fileindex,
1981 worktree->base_commit_id, worktree->root_fd, path,
1982 &blob->id);
1984 if (err)
1985 goto done;
1987 if (is_bad_symlink) {
1988 got_fileindex_entry_filetype_set(ie,
1989 GOT_FILEIDX_MODE_BAD_SYMLINK);
1992 got_object_blob_close(blob);
1993 done:
1994 free(ondisk_path);
1995 return err;
1998 static const struct got_error *
1999 remove_ondisk_file(const char *root_path, const char *path)
2001 const struct got_error *err = NULL;
2002 char *ondisk_path = NULL, *parent = NULL;
2004 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2005 return got_error_from_errno("asprintf");
2007 if (unlink(ondisk_path) == -1) {
2008 if (errno != ENOENT)
2009 err = got_error_from_errno2("unlink", ondisk_path);
2010 } else {
2011 size_t root_len = strlen(root_path);
2012 err = got_path_dirname(&parent, ondisk_path);
2013 if (err)
2014 goto done;
2015 while (got_path_cmp(parent, root_path,
2016 strlen(parent), root_len) != 0) {
2017 free(ondisk_path);
2018 ondisk_path = parent;
2019 parent = NULL;
2020 if (rmdir(ondisk_path) == -1) {
2021 if (errno != ENOTEMPTY)
2022 err = got_error_from_errno2("rmdir",
2023 ondisk_path);
2024 break;
2026 err = got_path_dirname(&parent, ondisk_path);
2027 if (err)
2028 break;
2031 done:
2032 free(ondisk_path);
2033 free(parent);
2034 return err;
2037 static const struct got_error *
2038 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2039 struct got_fileindex_entry *ie, struct got_repository *repo,
2040 got_worktree_checkout_cb progress_cb, void *progress_arg)
2042 const struct got_error *err = NULL;
2043 unsigned char status;
2044 struct stat sb;
2045 char *ondisk_path;
2047 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2048 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2050 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2051 == -1)
2052 return got_error_from_errno("asprintf");
2054 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2055 if (err)
2056 goto done;
2058 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2059 char ondisk_target[PATH_MAX];
2060 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2061 sizeof(ondisk_target));
2062 if (ondisk_len == -1) {
2063 err = got_error_from_errno2("readlink", ondisk_path);
2064 goto done;
2066 ondisk_target[ondisk_len] = '\0';
2067 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2068 NULL, NULL, /* XXX pass common ancestor info? */
2069 ondisk_target, ondisk_path);
2070 if (err)
2071 goto done;
2072 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2073 ie->path);
2074 goto done;
2077 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2078 status == GOT_STATUS_ADD) {
2079 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2080 if (err)
2081 goto done;
2083 * Preserve the working file and change the deleted blob's
2084 * entry into a schedule-add entry.
2086 err = got_fileindex_entry_update(ie, worktree->root_fd,
2087 ie->path, NULL, NULL, 0);
2088 } else {
2089 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2090 if (err)
2091 goto done;
2092 if (status == GOT_STATUS_NO_CHANGE) {
2093 err = remove_ondisk_file(worktree->root_path, ie->path);
2094 if (err)
2095 goto done;
2097 got_fileindex_entry_remove(fileindex, ie);
2099 done:
2100 free(ondisk_path);
2101 return err;
2104 struct diff_cb_arg {
2105 struct got_fileindex *fileindex;
2106 struct got_worktree *worktree;
2107 struct got_repository *repo;
2108 got_worktree_checkout_cb progress_cb;
2109 void *progress_arg;
2110 got_cancel_cb cancel_cb;
2111 void *cancel_arg;
2114 static const struct got_error *
2115 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2116 struct got_tree_entry *te, const char *parent_path)
2118 struct diff_cb_arg *a = arg;
2120 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2121 return got_error(GOT_ERR_CANCELLED);
2123 return update_blob(a->worktree, a->fileindex, ie, te,
2124 ie->path, a->repo, a->progress_cb, a->progress_arg);
2127 static const struct got_error *
2128 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2130 struct diff_cb_arg *a = arg;
2132 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2133 return got_error(GOT_ERR_CANCELLED);
2135 return delete_blob(a->worktree, a->fileindex, ie,
2136 a->repo, a->progress_cb, a->progress_arg);
2139 static const struct got_error *
2140 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2142 struct diff_cb_arg *a = arg;
2143 const struct got_error *err;
2144 char *path;
2146 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2147 return got_error(GOT_ERR_CANCELLED);
2149 if (got_object_tree_entry_is_submodule(te))
2150 return NULL;
2152 if (asprintf(&path, "%s%s%s", parent_path,
2153 parent_path[0] ? "/" : "", te->name)
2154 == -1)
2155 return got_error_from_errno("asprintf");
2157 if (S_ISDIR(te->mode))
2158 err = add_dir_on_disk(a->worktree, path);
2159 else
2160 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2161 a->repo, a->progress_cb, a->progress_arg);
2163 free(path);
2164 return err;
2167 const struct got_error *
2168 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2170 uint32_t uuid_status;
2172 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2173 if (uuid_status != uuid_s_ok) {
2174 *uuidstr = NULL;
2175 return got_error_uuid(uuid_status, "uuid_to_string");
2178 return NULL;
2181 static const struct got_error *
2182 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2184 const struct got_error *err = NULL;
2185 char *uuidstr = NULL;
2187 *refname = NULL;
2189 err = got_worktree_get_uuid(&uuidstr, worktree);
2190 if (err)
2191 return err;
2193 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2194 err = got_error_from_errno("asprintf");
2195 *refname = NULL;
2197 free(uuidstr);
2198 return err;
2201 const struct got_error *
2202 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2204 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2207 static const struct got_error *
2208 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2210 return get_ref_name(refname, worktree,
2211 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2214 static const struct got_error *
2215 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2217 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2220 static const struct got_error *
2221 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2223 return get_ref_name(refname, worktree,
2224 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2227 static const struct got_error *
2228 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2230 return get_ref_name(refname, worktree,
2231 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2234 static const struct got_error *
2235 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2237 return get_ref_name(refname, worktree,
2238 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2241 static const struct got_error *
2242 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2244 return get_ref_name(refname, worktree,
2245 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2248 static const struct got_error *
2249 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2251 return get_ref_name(refname, worktree,
2252 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2255 static const struct got_error *
2256 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2258 return get_ref_name(refname, worktree,
2259 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2262 const struct got_error *
2263 got_worktree_get_histedit_script_path(char **path,
2264 struct got_worktree *worktree)
2266 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2267 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2268 *path = NULL;
2269 return got_error_from_errno("asprintf");
2271 return NULL;
2274 static const struct got_error *
2275 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2277 return get_ref_name(refname, worktree,
2278 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2281 static const struct got_error *
2282 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2284 return get_ref_name(refname, worktree,
2285 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2289 * Prevent Git's garbage collector from deleting our base commit by
2290 * setting a reference to our base commit's ID.
2292 static const struct got_error *
2293 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2295 const struct got_error *err = NULL;
2296 struct got_reference *ref = NULL;
2297 char *refname;
2299 err = got_worktree_get_base_ref_name(&refname, worktree);
2300 if (err)
2301 return err;
2303 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2304 if (err)
2305 goto done;
2307 err = got_ref_write(ref, repo);
2308 done:
2309 free(refname);
2310 if (ref)
2311 got_ref_close(ref);
2312 return err;
2315 static const struct got_error *
2316 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2318 const struct got_error *err = NULL;
2320 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2321 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2322 err = got_error_from_errno("asprintf");
2323 *fileindex_path = NULL;
2325 return err;
2329 static const struct got_error *
2330 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2331 struct got_worktree *worktree)
2333 const struct got_error *err = NULL;
2334 FILE *index = NULL;
2336 *fileindex_path = NULL;
2337 *fileindex = got_fileindex_alloc();
2338 if (*fileindex == NULL)
2339 return got_error_from_errno("got_fileindex_alloc");
2341 err = get_fileindex_path(fileindex_path, worktree);
2342 if (err)
2343 goto done;
2345 index = fopen(*fileindex_path, "rbe");
2346 if (index == NULL) {
2347 if (errno != ENOENT)
2348 err = got_error_from_errno2("fopen", *fileindex_path);
2349 } else {
2350 err = got_fileindex_read(*fileindex, index);
2351 if (fclose(index) == EOF && err == NULL)
2352 err = got_error_from_errno("fclose");
2354 done:
2355 if (err) {
2356 free(*fileindex_path);
2357 *fileindex_path = NULL;
2358 got_fileindex_free(*fileindex);
2359 *fileindex = NULL;
2361 return err;
2364 struct bump_base_commit_id_arg {
2365 struct got_object_id *base_commit_id;
2366 const char *path;
2367 size_t path_len;
2368 const char *entry_name;
2369 got_worktree_checkout_cb progress_cb;
2370 void *progress_arg;
2373 /* Bump base commit ID of all files within an updated part of the work tree. */
2374 static const struct got_error *
2375 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2377 const struct got_error *err;
2378 struct bump_base_commit_id_arg *a = arg;
2380 if (a->entry_name) {
2381 if (strcmp(ie->path, a->path) != 0)
2382 return NULL;
2383 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2384 return NULL;
2386 if (got_fileindex_entry_was_skipped(ie))
2387 return NULL;
2389 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2390 SHA1_DIGEST_LENGTH) == 0)
2391 return NULL;
2393 if (a->progress_cb) {
2394 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2395 ie->path);
2396 if (err)
2397 return err;
2399 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2400 return NULL;
2403 static const struct got_error *
2404 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2405 struct got_fileindex *fileindex,
2406 got_worktree_checkout_cb progress_cb, void *progress_arg)
2408 struct bump_base_commit_id_arg bbc_arg;
2410 bbc_arg.base_commit_id = worktree->base_commit_id;
2411 bbc_arg.entry_name = NULL;
2412 bbc_arg.path = "";
2413 bbc_arg.path_len = 0;
2414 bbc_arg.progress_cb = progress_cb;
2415 bbc_arg.progress_arg = progress_arg;
2417 return got_fileindex_for_each_entry_safe(fileindex,
2418 bump_base_commit_id, &bbc_arg);
2421 static const struct got_error *
2422 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2424 const struct got_error *err = NULL;
2425 char *new_fileindex_path = NULL;
2426 FILE *new_index = NULL;
2427 struct timespec timeout;
2429 err = got_opentemp_named(&new_fileindex_path, &new_index,
2430 fileindex_path);
2431 if (err)
2432 goto done;
2434 err = got_fileindex_write(fileindex, new_index);
2435 if (err)
2436 goto done;
2438 if (rename(new_fileindex_path, fileindex_path) != 0) {
2439 err = got_error_from_errno3("rename", new_fileindex_path,
2440 fileindex_path);
2441 unlink(new_fileindex_path);
2445 * Sleep for a short amount of time to ensure that files modified after
2446 * this program exits have a different time stamp from the one which
2447 * was recorded in the file index.
2449 timeout.tv_sec = 0;
2450 timeout.tv_nsec = 1;
2451 nanosleep(&timeout, NULL);
2452 done:
2453 if (new_index)
2454 fclose(new_index);
2455 free(new_fileindex_path);
2456 return err;
2459 static const struct got_error *
2460 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2461 struct got_object_id **tree_id, const char *wt_relpath,
2462 struct got_commit_object *base_commit, struct got_worktree *worktree,
2463 struct got_repository *repo)
2465 const struct got_error *err = NULL;
2466 struct got_object_id *id = NULL;
2467 char *in_repo_path = NULL;
2468 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2470 *entry_type = GOT_OBJ_TYPE_ANY;
2471 *tree_relpath = NULL;
2472 *tree_id = NULL;
2474 if (wt_relpath[0] == '\0') {
2475 /* Check out all files within the work tree. */
2476 *entry_type = GOT_OBJ_TYPE_TREE;
2477 *tree_relpath = strdup("");
2478 if (*tree_relpath == NULL) {
2479 err = got_error_from_errno("strdup");
2480 goto done;
2482 err = got_object_id_by_path(tree_id, repo, base_commit,
2483 worktree->path_prefix);
2484 if (err)
2485 goto done;
2486 return NULL;
2489 /* Check out a subset of files in the work tree. */
2491 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2492 is_root_wt ? "" : "/", wt_relpath) == -1) {
2493 err = got_error_from_errno("asprintf");
2494 goto done;
2497 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2498 if (err)
2499 goto done;
2501 free(in_repo_path);
2502 in_repo_path = NULL;
2504 err = got_object_get_type(entry_type, repo, id);
2505 if (err)
2506 goto done;
2508 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2509 /* Check out a single file. */
2510 if (strchr(wt_relpath, '/') == NULL) {
2511 /* Check out a single file in work tree's root dir. */
2512 in_repo_path = strdup(worktree->path_prefix);
2513 if (in_repo_path == NULL) {
2514 err = got_error_from_errno("strdup");
2515 goto done;
2517 *tree_relpath = strdup("");
2518 if (*tree_relpath == NULL) {
2519 err = got_error_from_errno("strdup");
2520 goto done;
2522 } else {
2523 /* Check out a single file in a subdirectory. */
2524 err = got_path_dirname(tree_relpath, wt_relpath);
2525 if (err)
2526 return err;
2527 if (asprintf(&in_repo_path, "%s%s%s",
2528 worktree->path_prefix, is_root_wt ? "" : "/",
2529 *tree_relpath) == -1) {
2530 err = got_error_from_errno("asprintf");
2531 goto done;
2534 err = got_object_id_by_path(tree_id, repo,
2535 base_commit, in_repo_path);
2536 } else {
2537 /* Check out all files within a subdirectory. */
2538 *tree_id = got_object_id_dup(id);
2539 if (*tree_id == NULL) {
2540 err = got_error_from_errno("got_object_id_dup");
2541 goto done;
2543 *tree_relpath = strdup(wt_relpath);
2544 if (*tree_relpath == NULL) {
2545 err = got_error_from_errno("strdup");
2546 goto done;
2549 done:
2550 free(id);
2551 free(in_repo_path);
2552 if (err) {
2553 *entry_type = GOT_OBJ_TYPE_ANY;
2554 free(*tree_relpath);
2555 *tree_relpath = NULL;
2556 free(*tree_id);
2557 *tree_id = NULL;
2559 return err;
2562 static const struct got_error *
2563 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2564 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2565 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2566 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2568 const struct got_error *err = NULL;
2569 struct got_commit_object *commit = NULL;
2570 struct got_tree_object *tree = NULL;
2571 struct got_fileindex_diff_tree_cb diff_cb;
2572 struct diff_cb_arg arg;
2574 err = ref_base_commit(worktree, repo);
2575 if (err) {
2576 if (!(err->code == GOT_ERR_ERRNO &&
2577 (errno == EACCES || errno == EROFS)))
2578 goto done;
2579 err = (*progress_cb)(progress_arg,
2580 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2581 if (err)
2582 return err;
2585 err = got_object_open_as_commit(&commit, repo,
2586 worktree->base_commit_id);
2587 if (err)
2588 goto done;
2590 err = got_object_open_as_tree(&tree, repo, tree_id);
2591 if (err)
2592 goto done;
2594 if (entry_name &&
2595 got_object_tree_find_entry(tree, entry_name) == NULL) {
2596 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2597 goto done;
2600 diff_cb.diff_old_new = diff_old_new;
2601 diff_cb.diff_old = diff_old;
2602 diff_cb.diff_new = diff_new;
2603 arg.fileindex = fileindex;
2604 arg.worktree = worktree;
2605 arg.repo = repo;
2606 arg.progress_cb = progress_cb;
2607 arg.progress_arg = progress_arg;
2608 arg.cancel_cb = cancel_cb;
2609 arg.cancel_arg = cancel_arg;
2610 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2611 entry_name, repo, &diff_cb, &arg);
2612 done:
2613 if (tree)
2614 got_object_tree_close(tree);
2615 if (commit)
2616 got_object_commit_close(commit);
2617 return err;
2620 const struct got_error *
2621 got_worktree_checkout_files(struct got_worktree *worktree,
2622 struct got_pathlist_head *paths, struct got_repository *repo,
2623 got_worktree_checkout_cb progress_cb, void *progress_arg,
2624 got_cancel_cb cancel_cb, void *cancel_arg)
2626 const struct got_error *err = NULL, *sync_err, *unlockerr;
2627 struct got_commit_object *commit = NULL;
2628 struct got_tree_object *tree = NULL;
2629 struct got_fileindex *fileindex = NULL;
2630 char *fileindex_path = NULL;
2631 struct got_pathlist_entry *pe;
2632 struct tree_path_data {
2633 STAILQ_ENTRY(tree_path_data) entry;
2634 struct got_object_id *tree_id;
2635 int entry_type;
2636 char *relpath;
2637 char *entry_name;
2638 } *tpd = NULL;
2639 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2641 STAILQ_INIT(&tree_paths);
2643 err = lock_worktree(worktree, LOCK_EX);
2644 if (err)
2645 return err;
2647 err = got_object_open_as_commit(&commit, repo,
2648 worktree->base_commit_id);
2649 if (err)
2650 goto done;
2652 /* Map all specified paths to in-repository trees. */
2653 TAILQ_FOREACH(pe, paths, entry) {
2654 tpd = malloc(sizeof(*tpd));
2655 if (tpd == NULL) {
2656 err = got_error_from_errno("malloc");
2657 goto done;
2660 err = find_tree_entry_for_checkout(&tpd->entry_type,
2661 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2662 worktree, repo);
2663 if (err) {
2664 free(tpd);
2665 goto done;
2668 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2669 err = got_path_basename(&tpd->entry_name, pe->path);
2670 if (err) {
2671 free(tpd->relpath);
2672 free(tpd->tree_id);
2673 free(tpd);
2674 goto done;
2676 } else
2677 tpd->entry_name = NULL;
2679 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2683 * Read the file index.
2684 * Checking out files is supposed to be an idempotent operation.
2685 * If the on-disk file index is incomplete we will try to complete it.
2687 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2688 if (err)
2689 goto done;
2691 tpd = STAILQ_FIRST(&tree_paths);
2692 TAILQ_FOREACH(pe, paths, entry) {
2693 struct bump_base_commit_id_arg bbc_arg;
2695 err = checkout_files(worktree, fileindex, tpd->relpath,
2696 tpd->tree_id, tpd->entry_name, repo,
2697 progress_cb, progress_arg, cancel_cb, cancel_arg);
2698 if (err)
2699 break;
2701 bbc_arg.base_commit_id = worktree->base_commit_id;
2702 bbc_arg.entry_name = tpd->entry_name;
2703 bbc_arg.path = pe->path;
2704 bbc_arg.path_len = pe->path_len;
2705 bbc_arg.progress_cb = progress_cb;
2706 bbc_arg.progress_arg = progress_arg;
2707 err = got_fileindex_for_each_entry_safe(fileindex,
2708 bump_base_commit_id, &bbc_arg);
2709 if (err)
2710 break;
2712 tpd = STAILQ_NEXT(tpd, entry);
2714 sync_err = sync_fileindex(fileindex, fileindex_path);
2715 if (sync_err && err == NULL)
2716 err = sync_err;
2717 done:
2718 free(fileindex_path);
2719 if (tree)
2720 got_object_tree_close(tree);
2721 if (commit)
2722 got_object_commit_close(commit);
2723 if (fileindex)
2724 got_fileindex_free(fileindex);
2725 while (!STAILQ_EMPTY(&tree_paths)) {
2726 tpd = STAILQ_FIRST(&tree_paths);
2727 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2728 free(tpd->relpath);
2729 free(tpd->tree_id);
2730 free(tpd);
2732 unlockerr = lock_worktree(worktree, LOCK_SH);
2733 if (unlockerr && err == NULL)
2734 err = unlockerr;
2735 return err;
2738 struct merge_file_cb_arg {
2739 struct got_worktree *worktree;
2740 struct got_fileindex *fileindex;
2741 got_worktree_checkout_cb progress_cb;
2742 void *progress_arg;
2743 got_cancel_cb cancel_cb;
2744 void *cancel_arg;
2745 const char *label_orig;
2746 struct got_object_id *commit_id2;
2747 int allow_bad_symlinks;
2750 static const struct got_error *
2751 merge_file_cb(void *arg, struct got_blob_object *blob1,
2752 struct got_blob_object *blob2, FILE *f1, FILE *f2,
2753 struct got_object_id *id1, struct got_object_id *id2,
2754 const char *path1, const char *path2,
2755 mode_t mode1, mode_t mode2, struct got_repository *repo)
2757 static const struct got_error *err = NULL;
2758 struct merge_file_cb_arg *a = arg;
2759 struct got_fileindex_entry *ie;
2760 char *ondisk_path = NULL;
2761 struct stat sb;
2762 unsigned char status;
2763 int local_changes_subsumed;
2764 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2765 char *id_str = NULL, *label_deriv2 = NULL;
2767 if (blob1 && blob2) {
2768 ie = got_fileindex_entry_get(a->fileindex, path2,
2769 strlen(path2));
2770 if (ie == NULL)
2771 return (*a->progress_cb)(a->progress_arg,
2772 GOT_STATUS_MISSING, path2);
2774 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2775 path2) == -1)
2776 return got_error_from_errno("asprintf");
2778 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2779 repo);
2780 if (err)
2781 goto done;
2783 if (status == GOT_STATUS_DELETE) {
2784 err = (*a->progress_cb)(a->progress_arg,
2785 GOT_STATUS_MERGE, path2);
2786 goto done;
2788 if (status != GOT_STATUS_NO_CHANGE &&
2789 status != GOT_STATUS_MODIFY &&
2790 status != GOT_STATUS_CONFLICT &&
2791 status != GOT_STATUS_ADD) {
2792 err = (*a->progress_cb)(a->progress_arg, status, path2);
2793 goto done;
2796 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2797 char *link_target2;
2798 err = got_object_blob_read_to_str(&link_target2, blob2);
2799 if (err)
2800 goto done;
2801 err = merge_symlink(a->worktree, blob1, ondisk_path,
2802 path2, a->label_orig, link_target2, a->commit_id2,
2803 repo, a->progress_cb, a->progress_arg);
2804 free(link_target2);
2805 } else {
2806 int fd;
2808 f_orig = got_opentemp();
2809 if (f_orig == NULL) {
2810 err = got_error_from_errno("got_opentemp");
2811 goto done;
2813 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2814 f_orig, blob1);
2815 if (err)
2816 goto done;
2818 f_deriv2 = got_opentemp();
2819 if (f_deriv2 == NULL)
2820 goto done;
2821 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2822 f_deriv2, blob2);
2823 if (err)
2824 goto done;
2826 fd = open(ondisk_path,
2827 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2828 if (fd == -1) {
2829 err = got_error_from_errno2("open",
2830 ondisk_path);
2831 goto done;
2833 f_deriv = fdopen(fd, "r");
2834 if (f_deriv == NULL) {
2835 err = got_error_from_errno2("fdopen",
2836 ondisk_path);
2837 close(fd);
2838 goto done;
2840 err = got_object_id_str(&id_str, a->commit_id2);
2841 if (err)
2842 goto done;
2843 if (asprintf(&label_deriv2, "%s: commit %s",
2844 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2845 err = got_error_from_errno("asprintf");
2846 goto done;
2848 err = merge_file(&local_changes_subsumed, a->worktree,
2849 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2850 sb.st_mode, a->label_orig, NULL, label_deriv2,
2851 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2852 a->progress_cb, a->progress_arg);
2854 } else if (blob1) {
2855 ie = got_fileindex_entry_get(a->fileindex, path1,
2856 strlen(path1));
2857 if (ie == NULL)
2858 return (*a->progress_cb)(a->progress_arg,
2859 GOT_STATUS_MISSING, path1);
2861 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2862 path1) == -1)
2863 return got_error_from_errno("asprintf");
2865 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2866 repo);
2867 if (err)
2868 goto done;
2870 switch (status) {
2871 case GOT_STATUS_NO_CHANGE:
2872 err = (*a->progress_cb)(a->progress_arg,
2873 GOT_STATUS_DELETE, path1);
2874 if (err)
2875 goto done;
2876 err = remove_ondisk_file(a->worktree->root_path, path1);
2877 if (err)
2878 goto done;
2879 if (ie)
2880 got_fileindex_entry_mark_deleted_from_disk(ie);
2881 break;
2882 case GOT_STATUS_DELETE:
2883 case GOT_STATUS_MISSING:
2884 err = (*a->progress_cb)(a->progress_arg,
2885 GOT_STATUS_DELETE, path1);
2886 if (err)
2887 goto done;
2888 if (ie)
2889 got_fileindex_entry_mark_deleted_from_disk(ie);
2890 break;
2891 case GOT_STATUS_ADD: {
2892 struct got_object_id *id;
2893 FILE *blob1_f;
2894 off_t blob1_size;
2896 * Delete the added file only if its content already
2897 * exists in the repository.
2899 err = got_object_blob_file_create(&id, &blob1_f,
2900 &blob1_size, path1);
2901 if (err)
2902 goto done;
2903 if (got_object_id_cmp(id, id1) == 0) {
2904 err = (*a->progress_cb)(a->progress_arg,
2905 GOT_STATUS_DELETE, path1);
2906 if (err)
2907 goto done;
2908 err = remove_ondisk_file(a->worktree->root_path,
2909 path1);
2910 if (err)
2911 goto done;
2912 if (ie)
2913 got_fileindex_entry_remove(a->fileindex,
2914 ie);
2915 } else {
2916 err = (*a->progress_cb)(a->progress_arg,
2917 GOT_STATUS_CANNOT_DELETE, path1);
2919 if (fclose(blob1_f) == EOF && err == NULL)
2920 err = got_error_from_errno("fclose");
2921 free(id);
2922 if (err)
2923 goto done;
2924 break;
2926 case GOT_STATUS_MODIFY:
2927 case GOT_STATUS_CONFLICT:
2928 err = (*a->progress_cb)(a->progress_arg,
2929 GOT_STATUS_CANNOT_DELETE, path1);
2930 if (err)
2931 goto done;
2932 break;
2933 case GOT_STATUS_OBSTRUCTED:
2934 err = (*a->progress_cb)(a->progress_arg, status, path1);
2935 if (err)
2936 goto done;
2937 break;
2938 default:
2939 break;
2941 } else if (blob2) {
2942 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2943 path2) == -1)
2944 return got_error_from_errno("asprintf");
2945 ie = got_fileindex_entry_get(a->fileindex, path2,
2946 strlen(path2));
2947 if (ie) {
2948 err = get_file_status(&status, &sb, ie, ondisk_path,
2949 -1, NULL, repo);
2950 if (err)
2951 goto done;
2952 if (status != GOT_STATUS_NO_CHANGE &&
2953 status != GOT_STATUS_MODIFY &&
2954 status != GOT_STATUS_CONFLICT &&
2955 status != GOT_STATUS_ADD) {
2956 err = (*a->progress_cb)(a->progress_arg,
2957 status, path2);
2958 goto done;
2960 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2961 char *link_target2;
2962 err = got_object_blob_read_to_str(&link_target2,
2963 blob2);
2964 if (err)
2965 goto done;
2966 err = merge_symlink(a->worktree, NULL,
2967 ondisk_path, path2, a->label_orig,
2968 link_target2, a->commit_id2, repo,
2969 a->progress_cb, a->progress_arg);
2970 free(link_target2);
2971 } else if (S_ISREG(sb.st_mode)) {
2972 err = merge_blob(&local_changes_subsumed,
2973 a->worktree, NULL, ondisk_path, path2,
2974 sb.st_mode, a->label_orig, blob2,
2975 a->commit_id2, repo, a->progress_cb,
2976 a->progress_arg);
2977 } else {
2978 err = got_error_path(ondisk_path,
2979 GOT_ERR_FILE_OBSTRUCTED);
2981 if (err)
2982 goto done;
2983 if (status == GOT_STATUS_DELETE) {
2984 err = got_fileindex_entry_update(ie,
2985 a->worktree->root_fd, path2, blob2->id.sha1,
2986 a->worktree->base_commit_id->sha1, 0);
2987 if (err)
2988 goto done;
2990 } else {
2991 int is_bad_symlink = 0;
2992 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2993 if (S_ISLNK(mode2)) {
2994 err = install_symlink(&is_bad_symlink,
2995 a->worktree, ondisk_path, path2, blob2, 0,
2996 0, 1, a->allow_bad_symlinks, repo,
2997 a->progress_cb, a->progress_arg);
2998 } else {
2999 err = install_blob(a->worktree, ondisk_path, path2,
3000 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3001 a->progress_cb, a->progress_arg);
3003 if (err)
3004 goto done;
3005 err = got_fileindex_entry_alloc(&ie, path2);
3006 if (err)
3007 goto done;
3008 err = got_fileindex_entry_update(ie,
3009 a->worktree->root_fd, path2, NULL, NULL, 1);
3010 if (err) {
3011 got_fileindex_entry_free(ie);
3012 goto done;
3014 err = got_fileindex_entry_add(a->fileindex, ie);
3015 if (err) {
3016 got_fileindex_entry_free(ie);
3017 goto done;
3019 if (is_bad_symlink) {
3020 got_fileindex_entry_filetype_set(ie,
3021 GOT_FILEIDX_MODE_BAD_SYMLINK);
3025 done:
3026 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3027 err = got_error_from_errno("fclose");
3028 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3029 err = got_error_from_errno("fclose");
3030 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3031 err = got_error_from_errno("fclose");
3032 free(id_str);
3033 free(label_deriv2);
3034 free(ondisk_path);
3035 return err;
3038 static const struct got_error *
3039 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3041 struct got_worktree *worktree = arg;
3043 /* Reject merges into a work tree with mixed base commits. */
3044 if (got_fileindex_entry_has_commit(ie) &&
3045 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3046 SHA1_DIGEST_LENGTH) != 0)
3047 return got_error(GOT_ERR_MIXED_COMMITS);
3049 return NULL;
3052 struct check_merge_conflicts_arg {
3053 struct got_worktree *worktree;
3054 struct got_fileindex *fileindex;
3055 struct got_repository *repo;
3058 static const struct got_error *
3059 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3060 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3061 struct got_object_id *id1, struct got_object_id *id2,
3062 const char *path1, const char *path2,
3063 mode_t mode1, mode_t mode2, struct got_repository *repo)
3065 const struct got_error *err = NULL;
3066 struct check_merge_conflicts_arg *a = arg;
3067 unsigned char status;
3068 struct stat sb;
3069 struct got_fileindex_entry *ie;
3070 const char *path = path2 ? path2 : path1;
3071 struct got_object_id *id = id2 ? id2 : id1;
3072 char *ondisk_path;
3074 if (id == NULL)
3075 return NULL;
3077 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3078 if (ie == NULL)
3079 return NULL;
3081 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3082 == -1)
3083 return got_error_from_errno("asprintf");
3085 /* Reject merges into a work tree with conflicted files. */
3086 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3087 free(ondisk_path);
3088 if (err)
3089 return err;
3090 if (status == GOT_STATUS_CONFLICT)
3091 return got_error(GOT_ERR_CONFLICTS);
3093 return NULL;
3096 static const struct got_error *
3097 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3098 const char *fileindex_path, struct got_object_id *commit_id1,
3099 struct got_object_id *commit_id2, struct got_repository *repo,
3100 got_worktree_checkout_cb progress_cb, void *progress_arg,
3101 got_cancel_cb cancel_cb, void *cancel_arg)
3103 const struct got_error *err = NULL, *sync_err;
3104 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3105 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3106 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3107 struct check_merge_conflicts_arg cmc_arg;
3108 struct merge_file_cb_arg arg;
3109 char *label_orig = NULL;
3110 FILE *f1 = NULL, *f2 = NULL;
3112 if (commit_id1) {
3113 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3114 if (err)
3115 goto done;
3116 err = got_object_id_by_path(&tree_id1, repo, commit1,
3117 worktree->path_prefix);
3118 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3119 goto done;
3121 if (tree_id1) {
3122 char *id_str;
3124 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3125 if (err)
3126 goto done;
3128 err = got_object_id_str(&id_str, commit_id1);
3129 if (err)
3130 goto done;
3132 if (asprintf(&label_orig, "%s: commit %s",
3133 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3134 err = got_error_from_errno("asprintf");
3135 free(id_str);
3136 goto done;
3138 free(id_str);
3140 f1 = got_opentemp();
3141 if (f1 == NULL) {
3142 err = got_error_from_errno("got_opentemp");
3143 goto done;
3147 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3148 if (err)
3149 goto done;
3151 err = got_object_id_by_path(&tree_id2, repo, commit2,
3152 worktree->path_prefix);
3153 if (err)
3154 goto done;
3156 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3157 if (err)
3158 goto done;
3160 f2 = got_opentemp();
3161 if (f2 == NULL) {
3162 err = got_error_from_errno("got_opentemp");
3163 goto done;
3166 cmc_arg.worktree = worktree;
3167 cmc_arg.fileindex = fileindex;
3168 cmc_arg.repo = repo;
3169 err = got_diff_tree(tree1, tree2, f1, f2, "", "", repo,
3170 check_merge_conflicts, &cmc_arg, 0);
3171 if (err)
3172 goto done;
3174 arg.worktree = worktree;
3175 arg.fileindex = fileindex;
3176 arg.progress_cb = progress_cb;
3177 arg.progress_arg = progress_arg;
3178 arg.cancel_cb = cancel_cb;
3179 arg.cancel_arg = cancel_arg;
3180 arg.label_orig = label_orig;
3181 arg.commit_id2 = commit_id2;
3182 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3183 err = got_diff_tree(tree1, tree2, f1, f2, "", "", repo,
3184 merge_file_cb, &arg, 1);
3185 sync_err = sync_fileindex(fileindex, fileindex_path);
3186 if (sync_err && err == NULL)
3187 err = sync_err;
3188 done:
3189 if (commit1)
3190 got_object_commit_close(commit1);
3191 if (commit2)
3192 got_object_commit_close(commit2);
3193 if (tree1)
3194 got_object_tree_close(tree1);
3195 if (tree2)
3196 got_object_tree_close(tree2);
3197 if (f1 && fclose(f1) == EOF && err == NULL)
3198 err = got_error_from_errno("fclose");
3199 if (f2 && fclose(f2) == EOF && err == NULL)
3200 err = got_error_from_errno("fclose");
3201 free(label_orig);
3202 return err;
3205 const struct got_error *
3206 got_worktree_merge_files(struct got_worktree *worktree,
3207 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3208 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3209 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3211 const struct got_error *err, *unlockerr;
3212 char *fileindex_path = NULL;
3213 struct got_fileindex *fileindex = NULL;
3215 err = lock_worktree(worktree, LOCK_EX);
3216 if (err)
3217 return err;
3219 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3220 if (err)
3221 goto done;
3223 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3224 worktree);
3225 if (err)
3226 goto done;
3228 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3229 commit_id2, repo, progress_cb, progress_arg,
3230 cancel_cb, cancel_arg);
3231 done:
3232 if (fileindex)
3233 got_fileindex_free(fileindex);
3234 free(fileindex_path);
3235 unlockerr = lock_worktree(worktree, LOCK_SH);
3236 if (unlockerr && err == NULL)
3237 err = unlockerr;
3238 return err;
3241 struct diff_dir_cb_arg {
3242 struct got_fileindex *fileindex;
3243 struct got_worktree *worktree;
3244 const char *status_path;
3245 size_t status_path_len;
3246 struct got_repository *repo;
3247 got_worktree_status_cb status_cb;
3248 void *status_arg;
3249 got_cancel_cb cancel_cb;
3250 void *cancel_arg;
3251 /* A pathlist containing per-directory pathlists of ignore patterns. */
3252 struct got_pathlist_head *ignores;
3253 int report_unchanged;
3254 int no_ignores;
3257 static const struct got_error *
3258 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3259 int dirfd, const char *de_name,
3260 got_worktree_status_cb status_cb, void *status_arg,
3261 struct got_repository *repo, int report_unchanged)
3263 const struct got_error *err = NULL;
3264 unsigned char status = GOT_STATUS_NO_CHANGE;
3265 unsigned char staged_status = get_staged_status(ie);
3266 struct stat sb;
3267 struct got_object_id blob_id, commit_id, staged_blob_id;
3268 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3269 struct got_object_id *staged_blob_idp = NULL;
3271 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3272 if (err)
3273 return err;
3275 if (status == GOT_STATUS_NO_CHANGE &&
3276 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3277 return NULL;
3279 if (got_fileindex_entry_has_blob(ie)) {
3280 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3281 blob_idp = &blob_id;
3283 if (got_fileindex_entry_has_commit(ie)) {
3284 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3285 commit_idp = &commit_id;
3287 if (staged_status == GOT_STATUS_ADD ||
3288 staged_status == GOT_STATUS_MODIFY) {
3289 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3290 SHA1_DIGEST_LENGTH);
3291 staged_blob_idp = &staged_blob_id;
3294 return (*status_cb)(status_arg, status, staged_status,
3295 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3298 static const struct got_error *
3299 status_old_new(void *arg, struct got_fileindex_entry *ie,
3300 struct dirent *de, const char *parent_path, int dirfd)
3302 const struct got_error *err = NULL;
3303 struct diff_dir_cb_arg *a = arg;
3304 char *abspath;
3306 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3307 return got_error(GOT_ERR_CANCELLED);
3309 if (got_path_cmp(parent_path, a->status_path,
3310 strlen(parent_path), a->status_path_len) != 0 &&
3311 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3312 return NULL;
3314 if (parent_path[0]) {
3315 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3316 parent_path, de->d_name) == -1)
3317 return got_error_from_errno("asprintf");
3318 } else {
3319 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3320 de->d_name) == -1)
3321 return got_error_from_errno("asprintf");
3324 err = report_file_status(ie, abspath, dirfd, de->d_name,
3325 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3326 free(abspath);
3327 return err;
3330 static const struct got_error *
3331 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3333 struct diff_dir_cb_arg *a = arg;
3334 struct got_object_id blob_id, commit_id;
3335 unsigned char status;
3337 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3338 return got_error(GOT_ERR_CANCELLED);
3340 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3341 return NULL;
3343 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3344 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3345 if (got_fileindex_entry_has_file_on_disk(ie))
3346 status = GOT_STATUS_MISSING;
3347 else
3348 status = GOT_STATUS_DELETE;
3349 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3350 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3353 void
3354 free_ignorelist(struct got_pathlist_head *ignorelist)
3356 struct got_pathlist_entry *pe;
3358 TAILQ_FOREACH(pe, ignorelist, entry)
3359 free((char *)pe->path);
3360 got_pathlist_free(ignorelist);
3363 void
3364 free_ignores(struct got_pathlist_head *ignores)
3366 struct got_pathlist_entry *pe;
3368 TAILQ_FOREACH(pe, ignores, entry) {
3369 struct got_pathlist_head *ignorelist = pe->data;
3370 free_ignorelist(ignorelist);
3371 free((char *)pe->path);
3373 got_pathlist_free(ignores);
3376 static const struct got_error *
3377 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3379 const struct got_error *err = NULL;
3380 struct got_pathlist_entry *pe = NULL;
3381 struct got_pathlist_head *ignorelist;
3382 char *line = NULL, *pattern, *dirpath = NULL;
3383 size_t linesize = 0;
3384 ssize_t linelen;
3386 ignorelist = calloc(1, sizeof(*ignorelist));
3387 if (ignorelist == NULL)
3388 return got_error_from_errno("calloc");
3389 TAILQ_INIT(ignorelist);
3391 while ((linelen = getline(&line, &linesize, f)) != -1) {
3392 if (linelen > 0 && line[linelen - 1] == '\n')
3393 line[linelen - 1] = '\0';
3395 /* Git's ignores may contain comments. */
3396 if (line[0] == '#')
3397 continue;
3399 /* Git's negated patterns are not (yet?) supported. */
3400 if (line[0] == '!')
3401 continue;
3403 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3404 line) == -1) {
3405 err = got_error_from_errno("asprintf");
3406 goto done;
3408 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3409 if (err)
3410 goto done;
3412 if (ferror(f)) {
3413 err = got_error_from_errno("getline");
3414 goto done;
3417 dirpath = strdup(path);
3418 if (dirpath == NULL) {
3419 err = got_error_from_errno("strdup");
3420 goto done;
3422 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3423 done:
3424 free(line);
3425 if (err || pe == NULL) {
3426 free(dirpath);
3427 free_ignorelist(ignorelist);
3429 return err;
3432 int
3433 match_ignores(struct got_pathlist_head *ignores, const char *path)
3435 struct got_pathlist_entry *pe;
3437 /* Handle patterns which match in all directories. */
3438 TAILQ_FOREACH(pe, ignores, entry) {
3439 struct got_pathlist_head *ignorelist = pe->data;
3440 struct got_pathlist_entry *pi;
3442 TAILQ_FOREACH(pi, ignorelist, entry) {
3443 const char *p, *pattern = pi->path;
3445 if (strncmp(pattern, "**/", 3) != 0)
3446 continue;
3447 pattern += 3;
3448 p = path;
3449 while (*p) {
3450 if (fnmatch(pattern, p,
3451 FNM_PATHNAME | FNM_LEADING_DIR)) {
3452 /* Retry in next directory. */
3453 while (*p && *p != '/')
3454 p++;
3455 while (*p == '/')
3456 p++;
3457 continue;
3459 return 1;
3465 * The ignores pathlist contains ignore lists from children before
3466 * parents, so we can find the most specific ignorelist by walking
3467 * ignores backwards.
3469 pe = TAILQ_LAST(ignores, got_pathlist_head);
3470 while (pe) {
3471 if (got_path_is_child(path, pe->path, pe->path_len)) {
3472 struct got_pathlist_head *ignorelist = pe->data;
3473 struct got_pathlist_entry *pi;
3474 TAILQ_FOREACH(pi, ignorelist, entry) {
3475 const char *pattern = pi->path;
3476 int flags = FNM_LEADING_DIR;
3477 if (strstr(pattern, "/**/") == NULL)
3478 flags |= FNM_PATHNAME;
3479 if (fnmatch(pattern, path, flags))
3480 continue;
3481 return 1;
3484 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3487 return 0;
3490 static const struct got_error *
3491 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3492 const char *path, int dirfd, const char *ignores_filename)
3494 const struct got_error *err = NULL;
3495 char *ignorespath;
3496 int fd = -1;
3497 FILE *ignoresfile = NULL;
3499 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3500 path[0] ? "/" : "", ignores_filename) == -1)
3501 return got_error_from_errno("asprintf");
3503 if (dirfd != -1) {
3504 fd = openat(dirfd, ignores_filename,
3505 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3506 if (fd == -1) {
3507 if (errno != ENOENT && errno != EACCES)
3508 err = got_error_from_errno2("openat",
3509 ignorespath);
3510 } else {
3511 ignoresfile = fdopen(fd, "r");
3512 if (ignoresfile == NULL)
3513 err = got_error_from_errno2("fdopen",
3514 ignorespath);
3515 else {
3516 fd = -1;
3517 err = read_ignores(ignores, path, ignoresfile);
3520 } else {
3521 ignoresfile = fopen(ignorespath, "re");
3522 if (ignoresfile == NULL) {
3523 if (errno != ENOENT && errno != EACCES)
3524 err = got_error_from_errno2("fopen",
3525 ignorespath);
3526 } else
3527 err = read_ignores(ignores, path, ignoresfile);
3530 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3531 err = got_error_from_errno2("fclose", path);
3532 if (fd != -1 && close(fd) == -1 && err == NULL)
3533 err = got_error_from_errno2("close", path);
3534 free(ignorespath);
3535 return err;
3538 static const struct got_error *
3539 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3540 int dirfd)
3542 const struct got_error *err = NULL;
3543 struct diff_dir_cb_arg *a = arg;
3544 char *path = NULL;
3546 if (ignore != NULL)
3547 *ignore = 0;
3549 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3550 return got_error(GOT_ERR_CANCELLED);
3552 if (parent_path[0]) {
3553 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3554 return got_error_from_errno("asprintf");
3555 } else {
3556 path = de->d_name;
3559 if (de->d_type == DT_DIR) {
3560 if (!a->no_ignores && ignore != NULL &&
3561 match_ignores(a->ignores, path))
3562 *ignore = 1;
3563 } else if (!match_ignores(a->ignores, path) &&
3564 got_path_is_child(path, a->status_path, a->status_path_len))
3565 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3566 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3567 if (parent_path[0])
3568 free(path);
3569 return err;
3572 static const struct got_error *
3573 status_traverse(void *arg, const char *path, int dirfd)
3575 const struct got_error *err = NULL;
3576 struct diff_dir_cb_arg *a = arg;
3578 if (a->no_ignores)
3579 return NULL;
3581 err = add_ignores(a->ignores, a->worktree->root_path,
3582 path, dirfd, ".cvsignore");
3583 if (err)
3584 return err;
3586 err = add_ignores(a->ignores, a->worktree->root_path, path,
3587 dirfd, ".gitignore");
3589 return err;
3592 static const struct got_error *
3593 report_single_file_status(const char *path, const char *ondisk_path,
3594 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3595 void *status_arg, struct got_repository *repo, int report_unchanged,
3596 struct got_pathlist_head *ignores, int no_ignores)
3598 struct got_fileindex_entry *ie;
3599 struct stat sb;
3601 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3602 if (ie)
3603 return report_file_status(ie, ondisk_path, -1, NULL,
3604 status_cb, status_arg, repo, report_unchanged);
3606 if (lstat(ondisk_path, &sb) == -1) {
3607 if (errno != ENOENT)
3608 return got_error_from_errno2("lstat", ondisk_path);
3609 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3610 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3613 if (!no_ignores && match_ignores(ignores, path))
3614 return NULL;
3616 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3617 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3618 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3620 return NULL;
3623 static const struct got_error *
3624 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3625 const char *root_path, const char *path)
3627 const struct got_error *err;
3628 char *parent_path, *next_parent_path = NULL;
3630 err = add_ignores(ignores, root_path, "", -1,
3631 ".cvsignore");
3632 if (err)
3633 return err;
3635 err = add_ignores(ignores, root_path, "", -1,
3636 ".gitignore");
3637 if (err)
3638 return err;
3640 err = got_path_dirname(&parent_path, path);
3641 if (err) {
3642 if (err->code == GOT_ERR_BAD_PATH)
3643 return NULL; /* cannot traverse parent */
3644 return err;
3646 for (;;) {
3647 err = add_ignores(ignores, root_path, parent_path, -1,
3648 ".cvsignore");
3649 if (err)
3650 break;
3651 err = add_ignores(ignores, root_path, parent_path, -1,
3652 ".gitignore");
3653 if (err)
3654 break;
3655 err = got_path_dirname(&next_parent_path, parent_path);
3656 if (err) {
3657 if (err->code == GOT_ERR_BAD_PATH)
3658 err = NULL; /* traversed everything */
3659 break;
3661 if (got_path_is_root_dir(parent_path))
3662 break;
3663 free(parent_path);
3664 parent_path = next_parent_path;
3665 next_parent_path = NULL;
3668 free(parent_path);
3669 free(next_parent_path);
3670 return err;
3673 static const struct got_error *
3674 worktree_status(struct got_worktree *worktree, const char *path,
3675 struct got_fileindex *fileindex, struct got_repository *repo,
3676 got_worktree_status_cb status_cb, void *status_arg,
3677 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3678 int report_unchanged)
3680 const struct got_error *err = NULL;
3681 int fd = -1;
3682 struct got_fileindex_diff_dir_cb fdiff_cb;
3683 struct diff_dir_cb_arg arg;
3684 char *ondisk_path = NULL;
3685 struct got_pathlist_head ignores;
3686 struct got_fileindex_entry *ie;
3688 TAILQ_INIT(&ignores);
3690 if (asprintf(&ondisk_path, "%s%s%s",
3691 worktree->root_path, path[0] ? "/" : "", path) == -1)
3692 return got_error_from_errno("asprintf");
3694 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3695 if (ie) {
3696 err = report_single_file_status(path, ondisk_path,
3697 fileindex, status_cb, status_arg, repo,
3698 report_unchanged, &ignores, no_ignores);
3699 goto done;
3702 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3703 if (fd == -1) {
3704 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3705 !got_err_open_nofollow_on_symlink())
3706 err = got_error_from_errno2("open", ondisk_path);
3707 else {
3708 if (!no_ignores) {
3709 err = add_ignores_from_parent_paths(&ignores,
3710 worktree->root_path, ondisk_path);
3711 if (err)
3712 goto done;
3714 err = report_single_file_status(path, ondisk_path,
3715 fileindex, status_cb, status_arg, repo,
3716 report_unchanged, &ignores, no_ignores);
3718 } else {
3719 fdiff_cb.diff_old_new = status_old_new;
3720 fdiff_cb.diff_old = status_old;
3721 fdiff_cb.diff_new = status_new;
3722 fdiff_cb.diff_traverse = status_traverse;
3723 arg.fileindex = fileindex;
3724 arg.worktree = worktree;
3725 arg.status_path = path;
3726 arg.status_path_len = strlen(path);
3727 arg.repo = repo;
3728 arg.status_cb = status_cb;
3729 arg.status_arg = status_arg;
3730 arg.cancel_cb = cancel_cb;
3731 arg.cancel_arg = cancel_arg;
3732 arg.report_unchanged = report_unchanged;
3733 arg.no_ignores = no_ignores;
3734 if (!no_ignores) {
3735 err = add_ignores_from_parent_paths(&ignores,
3736 worktree->root_path, path);
3737 if (err)
3738 goto done;
3740 arg.ignores = &ignores;
3741 err = got_fileindex_diff_dir(fileindex, fd,
3742 worktree->root_path, path, repo, &fdiff_cb, &arg);
3744 done:
3745 free_ignores(&ignores);
3746 if (fd != -1 && close(fd) == -1 && err == NULL)
3747 err = got_error_from_errno("close");
3748 free(ondisk_path);
3749 return err;
3752 const struct got_error *
3753 got_worktree_status(struct got_worktree *worktree,
3754 struct got_pathlist_head *paths, struct got_repository *repo,
3755 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3756 got_cancel_cb cancel_cb, void *cancel_arg)
3758 const struct got_error *err = NULL;
3759 char *fileindex_path = NULL;
3760 struct got_fileindex *fileindex = NULL;
3761 struct got_pathlist_entry *pe;
3763 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3764 if (err)
3765 return err;
3767 TAILQ_FOREACH(pe, paths, entry) {
3768 err = worktree_status(worktree, pe->path, fileindex, repo,
3769 status_cb, status_arg, cancel_cb, cancel_arg,
3770 no_ignores, 0);
3771 if (err)
3772 break;
3774 free(fileindex_path);
3775 got_fileindex_free(fileindex);
3776 return err;
3779 const struct got_error *
3780 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3781 const char *arg)
3783 const struct got_error *err = NULL;
3784 char *resolved = NULL, *cwd = NULL, *path = NULL;
3785 size_t len;
3786 struct stat sb;
3787 char *abspath = NULL;
3788 char canonpath[PATH_MAX];
3790 *wt_path = NULL;
3792 cwd = getcwd(NULL, 0);
3793 if (cwd == NULL)
3794 return got_error_from_errno("getcwd");
3796 if (lstat(arg, &sb) == -1) {
3797 if (errno != ENOENT) {
3798 err = got_error_from_errno2("lstat", arg);
3799 goto done;
3801 sb.st_mode = 0;
3803 if (S_ISLNK(sb.st_mode)) {
3805 * We cannot use realpath(3) with symlinks since we want to
3806 * operate on the symlink itself.
3807 * But we can make the path absolute, assuming it is relative
3808 * to the current working directory, and then canonicalize it.
3810 if (!got_path_is_absolute(arg)) {
3811 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3812 err = got_error_from_errno("asprintf");
3813 goto done;
3817 err = got_canonpath(abspath ? abspath : arg, canonpath,
3818 sizeof(canonpath));
3819 if (err)
3820 goto done;
3821 resolved = strdup(canonpath);
3822 if (resolved == NULL) {
3823 err = got_error_from_errno("strdup");
3824 goto done;
3826 } else {
3827 resolved = realpath(arg, NULL);
3828 if (resolved == NULL) {
3829 if (errno != ENOENT) {
3830 err = got_error_from_errno2("realpath", arg);
3831 goto done;
3833 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3834 err = got_error_from_errno("asprintf");
3835 goto done;
3837 err = got_canonpath(abspath, canonpath,
3838 sizeof(canonpath));
3839 if (err)
3840 goto done;
3841 resolved = strdup(canonpath);
3842 if (resolved == NULL) {
3843 err = got_error_from_errno("strdup");
3844 goto done;
3849 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3850 strlen(got_worktree_get_root_path(worktree)))) {
3851 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3852 goto done;
3855 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3856 err = got_path_skip_common_ancestor(&path,
3857 got_worktree_get_root_path(worktree), resolved);
3858 if (err)
3859 goto done;
3860 } else {
3861 path = strdup("");
3862 if (path == NULL) {
3863 err = got_error_from_errno("strdup");
3864 goto done;
3868 /* XXX status walk can't deal with trailing slash! */
3869 len = strlen(path);
3870 while (len > 0 && path[len - 1] == '/') {
3871 path[len - 1] = '\0';
3872 len--;
3874 done:
3875 free(abspath);
3876 free(resolved);
3877 free(cwd);
3878 if (err == NULL)
3879 *wt_path = path;
3880 else
3881 free(path);
3882 return err;
3885 struct schedule_addition_args {
3886 struct got_worktree *worktree;
3887 struct got_fileindex *fileindex;
3888 got_worktree_checkout_cb progress_cb;
3889 void *progress_arg;
3890 struct got_repository *repo;
3893 static const struct got_error *
3894 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3895 const char *relpath, struct got_object_id *blob_id,
3896 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3897 int dirfd, const char *de_name)
3899 struct schedule_addition_args *a = arg;
3900 const struct got_error *err = NULL;
3901 struct got_fileindex_entry *ie;
3902 struct stat sb;
3903 char *ondisk_path;
3905 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3906 relpath) == -1)
3907 return got_error_from_errno("asprintf");
3909 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3910 if (ie) {
3911 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3912 de_name, a->repo);
3913 if (err)
3914 goto done;
3915 /* Re-adding an existing entry is a no-op. */
3916 if (status == GOT_STATUS_ADD)
3917 goto done;
3918 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3919 if (err)
3920 goto done;
3923 if (status != GOT_STATUS_UNVERSIONED) {
3924 if (status == GOT_STATUS_NONEXISTENT)
3925 err = got_error_set_errno(ENOENT, ondisk_path);
3926 else
3927 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3928 goto done;
3931 err = got_fileindex_entry_alloc(&ie, relpath);
3932 if (err)
3933 goto done;
3934 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3935 relpath, NULL, NULL, 1);
3936 if (err) {
3937 got_fileindex_entry_free(ie);
3938 goto done;
3940 err = got_fileindex_entry_add(a->fileindex, ie);
3941 if (err) {
3942 got_fileindex_entry_free(ie);
3943 goto done;
3945 done:
3946 free(ondisk_path);
3947 if (err)
3948 return err;
3949 if (status == GOT_STATUS_ADD)
3950 return NULL;
3951 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3954 const struct got_error *
3955 got_worktree_schedule_add(struct got_worktree *worktree,
3956 struct got_pathlist_head *paths,
3957 got_worktree_checkout_cb progress_cb, void *progress_arg,
3958 struct got_repository *repo, int no_ignores)
3960 struct got_fileindex *fileindex = NULL;
3961 char *fileindex_path = NULL;
3962 const struct got_error *err = NULL, *sync_err, *unlockerr;
3963 struct got_pathlist_entry *pe;
3964 struct schedule_addition_args saa;
3966 err = lock_worktree(worktree, LOCK_EX);
3967 if (err)
3968 return err;
3970 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3971 if (err)
3972 goto done;
3974 saa.worktree = worktree;
3975 saa.fileindex = fileindex;
3976 saa.progress_cb = progress_cb;
3977 saa.progress_arg = progress_arg;
3978 saa.repo = repo;
3980 TAILQ_FOREACH(pe, paths, entry) {
3981 err = worktree_status(worktree, pe->path, fileindex, repo,
3982 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3983 if (err)
3984 break;
3986 sync_err = sync_fileindex(fileindex, fileindex_path);
3987 if (sync_err && err == NULL)
3988 err = sync_err;
3989 done:
3990 free(fileindex_path);
3991 if (fileindex)
3992 got_fileindex_free(fileindex);
3993 unlockerr = lock_worktree(worktree, LOCK_SH);
3994 if (unlockerr && err == NULL)
3995 err = unlockerr;
3996 return err;
3999 struct schedule_deletion_args {
4000 struct got_worktree *worktree;
4001 struct got_fileindex *fileindex;
4002 got_worktree_delete_cb progress_cb;
4003 void *progress_arg;
4004 struct got_repository *repo;
4005 int delete_local_mods;
4006 int keep_on_disk;
4007 int ignore_missing_paths;
4008 const char *status_codes;
4011 static const struct got_error *
4012 schedule_for_deletion(void *arg, unsigned char status,
4013 unsigned char staged_status, const char *relpath,
4014 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4015 struct got_object_id *commit_id, int dirfd, const char *de_name)
4017 struct schedule_deletion_args *a = arg;
4018 const struct got_error *err = NULL;
4019 struct got_fileindex_entry *ie = NULL;
4020 struct stat sb;
4021 char *ondisk_path;
4023 if (status == GOT_STATUS_NONEXISTENT) {
4024 if (a->ignore_missing_paths)
4025 return NULL;
4026 return got_error_set_errno(ENOENT, relpath);
4029 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4030 if (ie == NULL)
4031 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4033 staged_status = get_staged_status(ie);
4034 if (staged_status != GOT_STATUS_NO_CHANGE) {
4035 if (staged_status == GOT_STATUS_DELETE)
4036 return NULL;
4037 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4040 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4041 relpath) == -1)
4042 return got_error_from_errno("asprintf");
4044 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4045 a->repo);
4046 if (err)
4047 goto done;
4049 if (a->status_codes) {
4050 size_t ncodes = strlen(a->status_codes);
4051 int i;
4052 for (i = 0; i < ncodes ; i++) {
4053 if (status == a->status_codes[i])
4054 break;
4056 if (i == ncodes) {
4057 /* Do not delete files in non-matching status. */
4058 free(ondisk_path);
4059 return NULL;
4061 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4062 a->status_codes[i] != GOT_STATUS_MISSING) {
4063 static char msg[64];
4064 snprintf(msg, sizeof(msg),
4065 "invalid status code '%c'", a->status_codes[i]);
4066 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4067 goto done;
4071 if (status != GOT_STATUS_NO_CHANGE) {
4072 if (status == GOT_STATUS_DELETE)
4073 goto done;
4074 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4075 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4076 goto done;
4078 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4079 err = got_error_set_errno(ENOENT, relpath);
4080 goto done;
4082 if (status != GOT_STATUS_MODIFY &&
4083 status != GOT_STATUS_MISSING) {
4084 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4085 goto done;
4089 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4090 size_t root_len;
4092 if (dirfd != -1) {
4093 if (unlinkat(dirfd, de_name, 0) != 0) {
4094 err = got_error_from_errno2("unlinkat",
4095 ondisk_path);
4096 goto done;
4098 } else if (unlink(ondisk_path) != 0) {
4099 err = got_error_from_errno2("unlink", ondisk_path);
4100 goto done;
4103 root_len = strlen(a->worktree->root_path);
4104 do {
4105 char *parent;
4106 err = got_path_dirname(&parent, ondisk_path);
4107 if (err)
4108 goto done;
4109 free(ondisk_path);
4110 ondisk_path = parent;
4111 if (rmdir(ondisk_path) == -1) {
4112 if (errno != ENOTEMPTY)
4113 err = got_error_from_errno2("rmdir",
4114 ondisk_path);
4115 break;
4117 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4118 strlen(ondisk_path), root_len) != 0);
4121 got_fileindex_entry_mark_deleted_from_disk(ie);
4122 done:
4123 free(ondisk_path);
4124 if (err)
4125 return err;
4126 if (status == GOT_STATUS_DELETE)
4127 return NULL;
4128 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4129 staged_status, relpath);
4132 const struct got_error *
4133 got_worktree_schedule_delete(struct got_worktree *worktree,
4134 struct got_pathlist_head *paths, int delete_local_mods,
4135 const char *status_codes,
4136 got_worktree_delete_cb progress_cb, void *progress_arg,
4137 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4139 struct got_fileindex *fileindex = NULL;
4140 char *fileindex_path = NULL;
4141 const struct got_error *err = NULL, *sync_err, *unlockerr;
4142 struct got_pathlist_entry *pe;
4143 struct schedule_deletion_args sda;
4145 err = lock_worktree(worktree, LOCK_EX);
4146 if (err)
4147 return err;
4149 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4150 if (err)
4151 goto done;
4153 sda.worktree = worktree;
4154 sda.fileindex = fileindex;
4155 sda.progress_cb = progress_cb;
4156 sda.progress_arg = progress_arg;
4157 sda.repo = repo;
4158 sda.delete_local_mods = delete_local_mods;
4159 sda.keep_on_disk = keep_on_disk;
4160 sda.ignore_missing_paths = ignore_missing_paths;
4161 sda.status_codes = status_codes;
4163 TAILQ_FOREACH(pe, paths, entry) {
4164 err = worktree_status(worktree, pe->path, fileindex, repo,
4165 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4166 if (err)
4167 break;
4169 sync_err = sync_fileindex(fileindex, fileindex_path);
4170 if (sync_err && err == NULL)
4171 err = sync_err;
4172 done:
4173 free(fileindex_path);
4174 if (fileindex)
4175 got_fileindex_free(fileindex);
4176 unlockerr = lock_worktree(worktree, LOCK_SH);
4177 if (unlockerr && err == NULL)
4178 err = unlockerr;
4179 return err;
4182 static const struct got_error *
4183 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4185 const struct got_error *err = NULL;
4186 char *line = NULL;
4187 size_t linesize = 0, n;
4188 ssize_t linelen;
4190 linelen = getline(&line, &linesize, infile);
4191 if (linelen == -1) {
4192 if (ferror(infile)) {
4193 err = got_error_from_errno("getline");
4194 goto done;
4196 return NULL;
4198 if (outfile) {
4199 n = fwrite(line, 1, linelen, outfile);
4200 if (n != linelen) {
4201 err = got_ferror(outfile, GOT_ERR_IO);
4202 goto done;
4205 if (rejectfile) {
4206 n = fwrite(line, 1, linelen, rejectfile);
4207 if (n != linelen)
4208 err = got_ferror(outfile, GOT_ERR_IO);
4210 done:
4211 free(line);
4212 return err;
4215 static const struct got_error *
4216 skip_one_line(FILE *f)
4218 char *line = NULL;
4219 size_t linesize = 0;
4220 ssize_t linelen;
4222 linelen = getline(&line, &linesize, f);
4223 if (linelen == -1) {
4224 if (ferror(f))
4225 return got_error_from_errno("getline");
4226 return NULL;
4228 free(line);
4229 return NULL;
4232 static const struct got_error *
4233 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4234 int start_old, int end_old, int start_new, int end_new,
4235 FILE *outfile, FILE *rejectfile)
4237 const struct got_error *err;
4239 /* Copy old file's lines leading up to patch. */
4240 while (!feof(f1) && *line_cur1 < start_old) {
4241 err = copy_one_line(f1, outfile, NULL);
4242 if (err)
4243 return err;
4244 (*line_cur1)++;
4246 /* Skip new file's lines leading up to patch. */
4247 while (!feof(f2) && *line_cur2 < start_new) {
4248 if (rejectfile)
4249 err = copy_one_line(f2, NULL, rejectfile);
4250 else
4251 err = skip_one_line(f2);
4252 if (err)
4253 return err;
4254 (*line_cur2)++;
4256 /* Copy patched lines. */
4257 while (!feof(f2) && *line_cur2 <= end_new) {
4258 err = copy_one_line(f2, outfile, NULL);
4259 if (err)
4260 return err;
4261 (*line_cur2)++;
4263 /* Skip over old file's replaced lines. */
4264 while (!feof(f1) && *line_cur1 <= end_old) {
4265 if (rejectfile)
4266 err = copy_one_line(f1, NULL, rejectfile);
4267 else
4268 err = skip_one_line(f1);
4269 if (err)
4270 return err;
4271 (*line_cur1)++;
4274 return NULL;
4277 static const struct got_error *
4278 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4279 FILE *outfile, FILE *rejectfile)
4281 const struct got_error *err;
4283 if (outfile) {
4284 /* Copy old file's lines until EOF. */
4285 while (!feof(f1)) {
4286 err = copy_one_line(f1, outfile, NULL);
4287 if (err)
4288 return err;
4289 (*line_cur1)++;
4292 if (rejectfile) {
4293 /* Copy new file's lines until EOF. */
4294 while (!feof(f2)) {
4295 err = copy_one_line(f2, NULL, rejectfile);
4296 if (err)
4297 return err;
4298 (*line_cur2)++;
4302 return NULL;
4305 static const struct got_error *
4306 apply_or_reject_change(int *choice, int *nchunks_used,
4307 struct diff_result *diff_result, int n,
4308 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4309 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4310 got_worktree_patch_cb patch_cb, void *patch_arg)
4312 const struct got_error *err = NULL;
4313 struct diff_chunk_context cc = {};
4314 int start_old, end_old, start_new, end_new;
4315 FILE *hunkfile;
4316 struct diff_output_unidiff_state *diff_state;
4317 struct diff_input_info diff_info;
4318 int rc;
4320 *choice = GOT_PATCH_CHOICE_NONE;
4322 /* Get changed line numbers without context lines for copy_change(). */
4323 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4324 start_old = cc.left.start;
4325 end_old = cc.left.end;
4326 start_new = cc.right.start;
4327 end_new = cc.right.end;
4329 /* Get the same change with context lines for display. */
4330 memset(&cc, 0, sizeof(cc));
4331 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4333 memset(&diff_info, 0, sizeof(diff_info));
4334 diff_info.left_path = relpath;
4335 diff_info.right_path = relpath;
4337 diff_state = diff_output_unidiff_state_alloc();
4338 if (diff_state == NULL)
4339 return got_error_set_errno(ENOMEM,
4340 "diff_output_unidiff_state_alloc");
4342 hunkfile = got_opentemp();
4343 if (hunkfile == NULL) {
4344 err = got_error_from_errno("got_opentemp");
4345 goto done;
4348 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4349 diff_result, &cc);
4350 if (rc != DIFF_RC_OK) {
4351 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4352 goto done;
4355 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4356 err = got_ferror(hunkfile, GOT_ERR_IO);
4357 goto done;
4360 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4361 hunkfile, changeno, nchanges);
4362 if (err)
4363 goto done;
4365 switch (*choice) {
4366 case GOT_PATCH_CHOICE_YES:
4367 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4368 end_old, start_new, end_new, outfile, rejectfile);
4369 break;
4370 case GOT_PATCH_CHOICE_NO:
4371 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4372 end_old, start_new, end_new, rejectfile, outfile);
4373 break;
4374 case GOT_PATCH_CHOICE_QUIT:
4375 break;
4376 default:
4377 err = got_error(GOT_ERR_PATCH_CHOICE);
4378 break;
4380 done:
4381 diff_output_unidiff_state_free(diff_state);
4382 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4383 err = got_error_from_errno("fclose");
4384 return err;
4387 struct revert_file_args {
4388 struct got_worktree *worktree;
4389 struct got_fileindex *fileindex;
4390 got_worktree_checkout_cb progress_cb;
4391 void *progress_arg;
4392 got_worktree_patch_cb patch_cb;
4393 void *patch_arg;
4394 struct got_repository *repo;
4395 int unlink_added_files;
4398 static const struct got_error *
4399 create_patched_content(char **path_outfile, int reverse_patch,
4400 struct got_object_id *blob_id, const char *path2,
4401 int dirfd2, const char *de_name2,
4402 const char *relpath, struct got_repository *repo,
4403 got_worktree_patch_cb patch_cb, void *patch_arg)
4405 const struct got_error *err, *free_err;
4406 struct got_blob_object *blob = NULL;
4407 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4408 int fd2 = -1;
4409 char link_target[PATH_MAX];
4410 ssize_t link_len = 0;
4411 char *path1 = NULL, *id_str = NULL;
4412 struct stat sb2;
4413 struct got_diffreg_result *diffreg_result = NULL;
4414 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4415 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4417 *path_outfile = NULL;
4419 err = got_object_id_str(&id_str, blob_id);
4420 if (err)
4421 return err;
4423 if (dirfd2 != -1) {
4424 fd2 = openat(dirfd2, de_name2,
4425 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4426 if (fd2 == -1) {
4427 if (!got_err_open_nofollow_on_symlink()) {
4428 err = got_error_from_errno2("openat", path2);
4429 goto done;
4431 link_len = readlinkat(dirfd2, de_name2,
4432 link_target, sizeof(link_target));
4433 if (link_len == -1) {
4434 return got_error_from_errno2("readlinkat",
4435 path2);
4437 sb2.st_mode = S_IFLNK;
4438 sb2.st_size = link_len;
4440 } else {
4441 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4442 if (fd2 == -1) {
4443 if (!got_err_open_nofollow_on_symlink()) {
4444 err = got_error_from_errno2("open", path2);
4445 goto done;
4447 link_len = readlink(path2, link_target,
4448 sizeof(link_target));
4449 if (link_len == -1)
4450 return got_error_from_errno2("readlink", path2);
4451 sb2.st_mode = S_IFLNK;
4452 sb2.st_size = link_len;
4455 if (fd2 != -1) {
4456 if (fstat(fd2, &sb2) == -1) {
4457 err = got_error_from_errno2("fstat", path2);
4458 goto done;
4461 f2 = fdopen(fd2, "r");
4462 if (f2 == NULL) {
4463 err = got_error_from_errno2("fdopen", path2);
4464 goto done;
4466 fd2 = -1;
4467 } else {
4468 size_t n;
4469 f2 = got_opentemp();
4470 if (f2 == NULL) {
4471 err = got_error_from_errno2("got_opentemp", path2);
4472 goto done;
4474 n = fwrite(link_target, 1, link_len, f2);
4475 if (n != link_len) {
4476 err = got_ferror(f2, GOT_ERR_IO);
4477 goto done;
4479 if (fflush(f2) == EOF) {
4480 err = got_error_from_errno("fflush");
4481 goto done;
4483 rewind(f2);
4486 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4487 if (err)
4488 goto done;
4490 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4491 if (err)
4492 goto done;
4494 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4495 if (err)
4496 goto done;
4498 err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4499 NULL);
4500 if (err)
4501 goto done;
4503 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4504 if (err)
4505 goto done;
4507 if (fseek(f1, 0L, SEEK_SET) == -1)
4508 return got_ferror(f1, GOT_ERR_IO);
4509 if (fseek(f2, 0L, SEEK_SET) == -1)
4510 return got_ferror(f2, GOT_ERR_IO);
4512 /* Count the number of actual changes in the diff result. */
4513 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4514 struct diff_chunk_context cc = {};
4515 diff_chunk_context_load_change(&cc, &nchunks_used,
4516 diffreg_result->result, n, 0);
4517 nchanges++;
4519 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4520 int choice;
4521 err = apply_or_reject_change(&choice, &nchunks_used,
4522 diffreg_result->result, n, relpath, f1, f2,
4523 &line_cur1, &line_cur2,
4524 reverse_patch ? NULL : outfile,
4525 reverse_patch ? outfile : NULL,
4526 ++i, nchanges, patch_cb, patch_arg);
4527 if (err)
4528 goto done;
4529 if (choice == GOT_PATCH_CHOICE_YES)
4530 have_content = 1;
4531 else if (choice == GOT_PATCH_CHOICE_QUIT)
4532 break;
4534 if (have_content) {
4535 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4536 reverse_patch ? NULL : outfile,
4537 reverse_patch ? outfile : NULL);
4538 if (err)
4539 goto done;
4541 if (!S_ISLNK(sb2.st_mode)) {
4542 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4543 err = got_error_from_errno2("fchmod", path2);
4544 goto done;
4548 done:
4549 free(id_str);
4550 if (blob)
4551 got_object_blob_close(blob);
4552 free_err = got_diffreg_result_free(diffreg_result);
4553 if (err == NULL)
4554 err = free_err;
4555 if (f1 && fclose(f1) == EOF && err == NULL)
4556 err = got_error_from_errno2("fclose", path1);
4557 if (f2 && fclose(f2) == EOF && err == NULL)
4558 err = got_error_from_errno2("fclose", path2);
4559 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4560 err = got_error_from_errno2("close", path2);
4561 if (outfile && fclose(outfile) == EOF && err == NULL)
4562 err = got_error_from_errno2("fclose", *path_outfile);
4563 if (path1 && unlink(path1) == -1 && err == NULL)
4564 err = got_error_from_errno2("unlink", path1);
4565 if (err || !have_content) {
4566 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4567 err = got_error_from_errno2("unlink", *path_outfile);
4568 free(*path_outfile);
4569 *path_outfile = NULL;
4571 free(path1);
4572 return err;
4575 static const struct got_error *
4576 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4577 const char *relpath, struct got_object_id *blob_id,
4578 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4579 int dirfd, const char *de_name)
4581 struct revert_file_args *a = arg;
4582 const struct got_error *err = NULL;
4583 char *parent_path = NULL;
4584 struct got_fileindex_entry *ie;
4585 struct got_commit_object *base_commit = NULL;
4586 struct got_tree_object *tree = NULL;
4587 struct got_object_id *tree_id = NULL;
4588 const struct got_tree_entry *te = NULL;
4589 char *tree_path = NULL, *te_name;
4590 char *ondisk_path = NULL, *path_content = NULL;
4591 struct got_blob_object *blob = NULL;
4593 /* Reverting a staged deletion is a no-op. */
4594 if (status == GOT_STATUS_DELETE &&
4595 staged_status != GOT_STATUS_NO_CHANGE)
4596 return NULL;
4598 if (status == GOT_STATUS_UNVERSIONED)
4599 return (*a->progress_cb)(a->progress_arg,
4600 GOT_STATUS_UNVERSIONED, relpath);
4602 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4603 if (ie == NULL)
4604 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4606 /* Construct in-repository path of tree which contains this blob. */
4607 err = got_path_dirname(&parent_path, ie->path);
4608 if (err) {
4609 if (err->code != GOT_ERR_BAD_PATH)
4610 goto done;
4611 parent_path = strdup("/");
4612 if (parent_path == NULL) {
4613 err = got_error_from_errno("strdup");
4614 goto done;
4617 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4618 tree_path = strdup(parent_path);
4619 if (tree_path == NULL) {
4620 err = got_error_from_errno("strdup");
4621 goto done;
4623 } else {
4624 if (got_path_is_root_dir(parent_path)) {
4625 tree_path = strdup(a->worktree->path_prefix);
4626 if (tree_path == NULL) {
4627 err = got_error_from_errno("strdup");
4628 goto done;
4630 } else {
4631 if (asprintf(&tree_path, "%s/%s",
4632 a->worktree->path_prefix, parent_path) == -1) {
4633 err = got_error_from_errno("asprintf");
4634 goto done;
4639 err = got_object_open_as_commit(&base_commit, a->repo,
4640 a->worktree->base_commit_id);
4641 if (err)
4642 goto done;
4644 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4645 if (err) {
4646 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4647 (status == GOT_STATUS_ADD ||
4648 staged_status == GOT_STATUS_ADD)))
4649 goto done;
4650 } else {
4651 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4652 if (err)
4653 goto done;
4655 err = got_path_basename(&te_name, ie->path);
4656 if (err)
4657 goto done;
4659 te = got_object_tree_find_entry(tree, te_name);
4660 free(te_name);
4661 if (te == NULL && status != GOT_STATUS_ADD &&
4662 staged_status != GOT_STATUS_ADD) {
4663 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4664 goto done;
4668 switch (status) {
4669 case GOT_STATUS_ADD:
4670 if (a->patch_cb) {
4671 int choice = GOT_PATCH_CHOICE_NONE;
4672 err = (*a->patch_cb)(&choice, a->patch_arg,
4673 status, ie->path, NULL, 1, 1);
4674 if (err)
4675 goto done;
4676 if (choice != GOT_PATCH_CHOICE_YES)
4677 break;
4679 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4680 ie->path);
4681 if (err)
4682 goto done;
4683 got_fileindex_entry_remove(a->fileindex, ie);
4684 if (a->unlink_added_files) {
4685 if (asprintf(&ondisk_path, "%s/%s",
4686 got_worktree_get_root_path(a->worktree),
4687 relpath) == -1) {
4688 err = got_error_from_errno("asprintf");
4689 goto done;
4691 if (unlink(ondisk_path) == -1) {
4692 err = got_error_from_errno2("unlink",
4693 ondisk_path);
4694 break;
4697 break;
4698 case GOT_STATUS_DELETE:
4699 if (a->patch_cb) {
4700 int choice = GOT_PATCH_CHOICE_NONE;
4701 err = (*a->patch_cb)(&choice, a->patch_arg,
4702 status, ie->path, NULL, 1, 1);
4703 if (err)
4704 goto done;
4705 if (choice != GOT_PATCH_CHOICE_YES)
4706 break;
4708 /* fall through */
4709 case GOT_STATUS_MODIFY:
4710 case GOT_STATUS_MODE_CHANGE:
4711 case GOT_STATUS_CONFLICT:
4712 case GOT_STATUS_MISSING: {
4713 struct got_object_id id;
4714 if (staged_status == GOT_STATUS_ADD ||
4715 staged_status == GOT_STATUS_MODIFY) {
4716 memcpy(id.sha1, ie->staged_blob_sha1,
4717 SHA1_DIGEST_LENGTH);
4718 } else
4719 memcpy(id.sha1, ie->blob_sha1,
4720 SHA1_DIGEST_LENGTH);
4721 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4722 if (err)
4723 goto done;
4725 if (asprintf(&ondisk_path, "%s/%s",
4726 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4727 err = got_error_from_errno("asprintf");
4728 goto done;
4731 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4732 status == GOT_STATUS_CONFLICT)) {
4733 int is_bad_symlink = 0;
4734 err = create_patched_content(&path_content, 1, &id,
4735 ondisk_path, dirfd, de_name, ie->path, a->repo,
4736 a->patch_cb, a->patch_arg);
4737 if (err || path_content == NULL)
4738 break;
4739 if (te && S_ISLNK(te->mode)) {
4740 if (unlink(path_content) == -1) {
4741 err = got_error_from_errno2("unlink",
4742 path_content);
4743 break;
4745 err = install_symlink(&is_bad_symlink,
4746 a->worktree, ondisk_path, ie->path,
4747 blob, 0, 1, 0, 0, a->repo,
4748 a->progress_cb, a->progress_arg);
4749 } else {
4750 if (rename(path_content, ondisk_path) == -1) {
4751 err = got_error_from_errno3("rename",
4752 path_content, ondisk_path);
4753 goto done;
4756 } else {
4757 int is_bad_symlink = 0;
4758 if (te && S_ISLNK(te->mode)) {
4759 err = install_symlink(&is_bad_symlink,
4760 a->worktree, ondisk_path, ie->path,
4761 blob, 0, 1, 0, 0, a->repo,
4762 a->progress_cb, a->progress_arg);
4763 } else {
4764 err = install_blob(a->worktree, ondisk_path,
4765 ie->path,
4766 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4767 got_fileindex_perms_to_st(ie), blob,
4768 0, 1, 0, 0, a->repo,
4769 a->progress_cb, a->progress_arg);
4771 if (err)
4772 goto done;
4773 if (status == GOT_STATUS_DELETE ||
4774 status == GOT_STATUS_MODE_CHANGE) {
4775 err = got_fileindex_entry_update(ie,
4776 a->worktree->root_fd, relpath,
4777 blob->id.sha1,
4778 a->worktree->base_commit_id->sha1, 1);
4779 if (err)
4780 goto done;
4782 if (is_bad_symlink) {
4783 got_fileindex_entry_filetype_set(ie,
4784 GOT_FILEIDX_MODE_BAD_SYMLINK);
4787 break;
4789 default:
4790 break;
4792 done:
4793 free(ondisk_path);
4794 free(path_content);
4795 free(parent_path);
4796 free(tree_path);
4797 if (blob)
4798 got_object_blob_close(blob);
4799 if (tree)
4800 got_object_tree_close(tree);
4801 free(tree_id);
4802 if (base_commit)
4803 got_object_commit_close(base_commit);
4804 return err;
4807 const struct got_error *
4808 got_worktree_revert(struct got_worktree *worktree,
4809 struct got_pathlist_head *paths,
4810 got_worktree_checkout_cb progress_cb, void *progress_arg,
4811 got_worktree_patch_cb patch_cb, void *patch_arg,
4812 struct got_repository *repo)
4814 struct got_fileindex *fileindex = NULL;
4815 char *fileindex_path = NULL;
4816 const struct got_error *err = NULL, *unlockerr = NULL;
4817 const struct got_error *sync_err = NULL;
4818 struct got_pathlist_entry *pe;
4819 struct revert_file_args rfa;
4821 err = lock_worktree(worktree, LOCK_EX);
4822 if (err)
4823 return err;
4825 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4826 if (err)
4827 goto done;
4829 rfa.worktree = worktree;
4830 rfa.fileindex = fileindex;
4831 rfa.progress_cb = progress_cb;
4832 rfa.progress_arg = progress_arg;
4833 rfa.patch_cb = patch_cb;
4834 rfa.patch_arg = patch_arg;
4835 rfa.repo = repo;
4836 rfa.unlink_added_files = 0;
4837 TAILQ_FOREACH(pe, paths, entry) {
4838 err = worktree_status(worktree, pe->path, fileindex, repo,
4839 revert_file, &rfa, NULL, NULL, 1, 0);
4840 if (err)
4841 break;
4843 sync_err = sync_fileindex(fileindex, fileindex_path);
4844 if (sync_err && err == NULL)
4845 err = sync_err;
4846 done:
4847 free(fileindex_path);
4848 if (fileindex)
4849 got_fileindex_free(fileindex);
4850 unlockerr = lock_worktree(worktree, LOCK_SH);
4851 if (unlockerr && err == NULL)
4852 err = unlockerr;
4853 return err;
4856 static void
4857 free_commitable(struct got_commitable *ct)
4859 free(ct->path);
4860 free(ct->in_repo_path);
4861 free(ct->ondisk_path);
4862 free(ct->blob_id);
4863 free(ct->base_blob_id);
4864 free(ct->staged_blob_id);
4865 free(ct->base_commit_id);
4866 free(ct);
4869 struct collect_commitables_arg {
4870 struct got_pathlist_head *commitable_paths;
4871 struct got_repository *repo;
4872 struct got_worktree *worktree;
4873 struct got_fileindex *fileindex;
4874 int have_staged_files;
4875 int allow_bad_symlinks;
4878 static const struct got_error *
4879 collect_commitables(void *arg, unsigned char status,
4880 unsigned char staged_status, const char *relpath,
4881 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4882 struct got_object_id *commit_id, int dirfd, const char *de_name)
4884 struct collect_commitables_arg *a = arg;
4885 const struct got_error *err = NULL;
4886 struct got_commitable *ct = NULL;
4887 struct got_pathlist_entry *new = NULL;
4888 char *parent_path = NULL, *path = NULL;
4889 struct stat sb;
4891 if (a->have_staged_files) {
4892 if (staged_status != GOT_STATUS_MODIFY &&
4893 staged_status != GOT_STATUS_ADD &&
4894 staged_status != GOT_STATUS_DELETE)
4895 return NULL;
4896 } else {
4897 if (status == GOT_STATUS_CONFLICT)
4898 return got_error(GOT_ERR_COMMIT_CONFLICT);
4900 if (status != GOT_STATUS_MODIFY &&
4901 status != GOT_STATUS_MODE_CHANGE &&
4902 status != GOT_STATUS_ADD &&
4903 status != GOT_STATUS_DELETE)
4904 return NULL;
4907 if (asprintf(&path, "/%s", relpath) == -1) {
4908 err = got_error_from_errno("asprintf");
4909 goto done;
4911 if (strcmp(path, "/") == 0) {
4912 parent_path = strdup("");
4913 if (parent_path == NULL)
4914 return got_error_from_errno("strdup");
4915 } else {
4916 err = got_path_dirname(&parent_path, path);
4917 if (err)
4918 return err;
4921 ct = calloc(1, sizeof(*ct));
4922 if (ct == NULL) {
4923 err = got_error_from_errno("calloc");
4924 goto done;
4927 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4928 relpath) == -1) {
4929 err = got_error_from_errno("asprintf");
4930 goto done;
4933 if (staged_status == GOT_STATUS_ADD ||
4934 staged_status == GOT_STATUS_MODIFY) {
4935 struct got_fileindex_entry *ie;
4936 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4937 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4938 case GOT_FILEIDX_MODE_REGULAR_FILE:
4939 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4940 ct->mode = S_IFREG;
4941 break;
4942 case GOT_FILEIDX_MODE_SYMLINK:
4943 ct->mode = S_IFLNK;
4944 break;
4945 default:
4946 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4947 goto done;
4949 ct->mode |= got_fileindex_entry_perms_get(ie);
4950 } else if (status != GOT_STATUS_DELETE &&
4951 staged_status != GOT_STATUS_DELETE) {
4952 if (dirfd != -1) {
4953 if (fstatat(dirfd, de_name, &sb,
4954 AT_SYMLINK_NOFOLLOW) == -1) {
4955 err = got_error_from_errno2("fstatat",
4956 ct->ondisk_path);
4957 goto done;
4959 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4960 err = got_error_from_errno2("lstat", ct->ondisk_path);
4961 goto done;
4963 ct->mode = sb.st_mode;
4966 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4967 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4968 relpath) == -1) {
4969 err = got_error_from_errno("asprintf");
4970 goto done;
4973 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4974 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4975 int is_bad_symlink;
4976 char target_path[PATH_MAX];
4977 ssize_t target_len;
4978 target_len = readlink(ct->ondisk_path, target_path,
4979 sizeof(target_path));
4980 if (target_len == -1) {
4981 err = got_error_from_errno2("readlink",
4982 ct->ondisk_path);
4983 goto done;
4985 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4986 target_len, ct->ondisk_path, a->worktree->root_path);
4987 if (err)
4988 goto done;
4989 if (is_bad_symlink) {
4990 err = got_error_path(ct->ondisk_path,
4991 GOT_ERR_BAD_SYMLINK);
4992 goto done;
4997 ct->status = status;
4998 ct->staged_status = staged_status;
4999 ct->blob_id = NULL; /* will be filled in when blob gets created */
5000 if (ct->status != GOT_STATUS_ADD &&
5001 ct->staged_status != GOT_STATUS_ADD) {
5002 ct->base_blob_id = got_object_id_dup(blob_id);
5003 if (ct->base_blob_id == NULL) {
5004 err = got_error_from_errno("got_object_id_dup");
5005 goto done;
5007 ct->base_commit_id = got_object_id_dup(commit_id);
5008 if (ct->base_commit_id == NULL) {
5009 err = got_error_from_errno("got_object_id_dup");
5010 goto done;
5013 if (ct->staged_status == GOT_STATUS_ADD ||
5014 ct->staged_status == GOT_STATUS_MODIFY) {
5015 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5016 if (ct->staged_blob_id == NULL) {
5017 err = got_error_from_errno("got_object_id_dup");
5018 goto done;
5021 ct->path = strdup(path);
5022 if (ct->path == NULL) {
5023 err = got_error_from_errno("strdup");
5024 goto done;
5026 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5027 done:
5028 if (ct && (err || new == NULL))
5029 free_commitable(ct);
5030 free(parent_path);
5031 free(path);
5032 return err;
5035 static const struct got_error *write_tree(struct got_object_id **, int *,
5036 struct got_tree_object *, const char *, struct got_pathlist_head *,
5037 got_worktree_status_cb status_cb, void *status_arg,
5038 struct got_repository *);
5040 static const struct got_error *
5041 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5042 struct got_tree_entry *te, const char *parent_path,
5043 struct got_pathlist_head *commitable_paths,
5044 got_worktree_status_cb status_cb, void *status_arg,
5045 struct got_repository *repo)
5047 const struct got_error *err = NULL;
5048 struct got_tree_object *subtree;
5049 char *subpath;
5051 if (asprintf(&subpath, "%s%s%s", parent_path,
5052 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5053 return got_error_from_errno("asprintf");
5055 err = got_object_open_as_tree(&subtree, repo, &te->id);
5056 if (err)
5057 return err;
5059 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5060 commitable_paths, status_cb, status_arg, repo);
5061 got_object_tree_close(subtree);
5062 free(subpath);
5063 return err;
5066 static const struct got_error *
5067 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5069 const struct got_error *err = NULL;
5070 char *ct_parent_path = NULL;
5072 *match = 0;
5074 if (strchr(ct->in_repo_path, '/') == NULL) {
5075 *match = got_path_is_root_dir(path);
5076 return NULL;
5079 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5080 if (err)
5081 return err;
5082 *match = (strcmp(path, ct_parent_path) == 0);
5083 free(ct_parent_path);
5084 return err;
5087 static mode_t
5088 get_ct_file_mode(struct got_commitable *ct)
5090 if (S_ISLNK(ct->mode))
5091 return S_IFLNK;
5093 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5096 static const struct got_error *
5097 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5098 struct got_tree_entry *te, struct got_commitable *ct)
5100 const struct got_error *err = NULL;
5102 *new_te = NULL;
5104 err = got_object_tree_entry_dup(new_te, te);
5105 if (err)
5106 goto done;
5108 (*new_te)->mode = get_ct_file_mode(ct);
5110 if (ct->staged_status == GOT_STATUS_MODIFY)
5111 memcpy(&(*new_te)->id, ct->staged_blob_id,
5112 sizeof((*new_te)->id));
5113 else
5114 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5115 done:
5116 if (err && *new_te) {
5117 free(*new_te);
5118 *new_te = NULL;
5120 return err;
5123 static const struct got_error *
5124 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5125 struct got_commitable *ct)
5127 const struct got_error *err = NULL;
5128 char *ct_name = NULL;
5130 *new_te = NULL;
5132 *new_te = calloc(1, sizeof(**new_te));
5133 if (*new_te == NULL)
5134 return got_error_from_errno("calloc");
5136 err = got_path_basename(&ct_name, ct->path);
5137 if (err)
5138 goto done;
5139 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5140 sizeof((*new_te)->name)) {
5141 err = got_error(GOT_ERR_NO_SPACE);
5142 goto done;
5145 (*new_te)->mode = get_ct_file_mode(ct);
5147 if (ct->staged_status == GOT_STATUS_ADD)
5148 memcpy(&(*new_te)->id, ct->staged_blob_id,
5149 sizeof((*new_te)->id));
5150 else
5151 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5152 done:
5153 free(ct_name);
5154 if (err && *new_te) {
5155 free(*new_te);
5156 *new_te = NULL;
5158 return err;
5161 static const struct got_error *
5162 insert_tree_entry(struct got_tree_entry *new_te,
5163 struct got_pathlist_head *paths)
5165 const struct got_error *err = NULL;
5166 struct got_pathlist_entry *new_pe;
5168 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5169 if (err)
5170 return err;
5171 if (new_pe == NULL)
5172 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5173 return NULL;
5176 static const struct got_error *
5177 report_ct_status(struct got_commitable *ct,
5178 got_worktree_status_cb status_cb, void *status_arg)
5180 const char *ct_path = ct->path;
5181 unsigned char status;
5183 if (status_cb == NULL) /* no commit progress output desired */
5184 return NULL;
5186 while (ct_path[0] == '/')
5187 ct_path++;
5189 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5190 status = ct->staged_status;
5191 else
5192 status = ct->status;
5194 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5195 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5198 static const struct got_error *
5199 match_modified_subtree(int *modified, struct got_tree_entry *te,
5200 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5202 const struct got_error *err = NULL;
5203 struct got_pathlist_entry *pe;
5204 char *te_path;
5206 *modified = 0;
5208 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5209 got_path_is_root_dir(base_tree_path) ? "" : "/",
5210 te->name) == -1)
5211 return got_error_from_errno("asprintf");
5213 TAILQ_FOREACH(pe, commitable_paths, entry) {
5214 struct got_commitable *ct = pe->data;
5215 *modified = got_path_is_child(ct->in_repo_path, te_path,
5216 strlen(te_path));
5217 if (*modified)
5218 break;
5221 free(te_path);
5222 return err;
5225 static const struct got_error *
5226 match_deleted_or_modified_ct(struct got_commitable **ctp,
5227 struct got_tree_entry *te, const char *base_tree_path,
5228 struct got_pathlist_head *commitable_paths)
5230 const struct got_error *err = NULL;
5231 struct got_pathlist_entry *pe;
5233 *ctp = NULL;
5235 TAILQ_FOREACH(pe, commitable_paths, entry) {
5236 struct got_commitable *ct = pe->data;
5237 char *ct_name = NULL;
5238 int path_matches;
5240 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5241 if (ct->status != GOT_STATUS_MODIFY &&
5242 ct->status != GOT_STATUS_MODE_CHANGE &&
5243 ct->status != GOT_STATUS_DELETE)
5244 continue;
5245 } else {
5246 if (ct->staged_status != GOT_STATUS_MODIFY &&
5247 ct->staged_status != GOT_STATUS_DELETE)
5248 continue;
5251 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5252 continue;
5254 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5255 if (err)
5256 return err;
5257 if (!path_matches)
5258 continue;
5260 err = got_path_basename(&ct_name, pe->path);
5261 if (err)
5262 return err;
5264 if (strcmp(te->name, ct_name) != 0) {
5265 free(ct_name);
5266 continue;
5268 free(ct_name);
5270 *ctp = ct;
5271 break;
5274 return err;
5277 static const struct got_error *
5278 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5279 const char *child_path, const char *path_base_tree,
5280 struct got_pathlist_head *commitable_paths,
5281 got_worktree_status_cb status_cb, void *status_arg,
5282 struct got_repository *repo)
5284 const struct got_error *err = NULL;
5285 struct got_tree_entry *new_te;
5286 char *subtree_path;
5287 struct got_object_id *id = NULL;
5288 int nentries;
5290 *new_tep = NULL;
5292 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5293 got_path_is_root_dir(path_base_tree) ? "" : "/",
5294 child_path) == -1)
5295 return got_error_from_errno("asprintf");
5297 new_te = calloc(1, sizeof(*new_te));
5298 if (new_te == NULL)
5299 return got_error_from_errno("calloc");
5300 new_te->mode = S_IFDIR;
5302 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5303 sizeof(new_te->name)) {
5304 err = got_error(GOT_ERR_NO_SPACE);
5305 goto done;
5307 err = write_tree(&id, &nentries, NULL, subtree_path,
5308 commitable_paths, status_cb, status_arg, repo);
5309 if (err) {
5310 free(new_te);
5311 goto done;
5313 memcpy(&new_te->id, id, sizeof(new_te->id));
5314 done:
5315 free(id);
5316 free(subtree_path);
5317 if (err == NULL)
5318 *new_tep = new_te;
5319 return err;
5322 static const struct got_error *
5323 write_tree(struct got_object_id **new_tree_id, int *nentries,
5324 struct got_tree_object *base_tree, const char *path_base_tree,
5325 struct got_pathlist_head *commitable_paths,
5326 got_worktree_status_cb status_cb, void *status_arg,
5327 struct got_repository *repo)
5329 const struct got_error *err = NULL;
5330 struct got_pathlist_head paths;
5331 struct got_tree_entry *te, *new_te = NULL;
5332 struct got_pathlist_entry *pe;
5334 TAILQ_INIT(&paths);
5335 *nentries = 0;
5337 /* Insert, and recurse into, newly added entries first. */
5338 TAILQ_FOREACH(pe, commitable_paths, entry) {
5339 struct got_commitable *ct = pe->data;
5340 char *child_path = NULL, *slash;
5342 if ((ct->status != GOT_STATUS_ADD &&
5343 ct->staged_status != GOT_STATUS_ADD) ||
5344 (ct->flags & GOT_COMMITABLE_ADDED))
5345 continue;
5347 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5348 strlen(path_base_tree)))
5349 continue;
5351 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5352 ct->in_repo_path);
5353 if (err)
5354 goto done;
5356 slash = strchr(child_path, '/');
5357 if (slash == NULL) {
5358 err = alloc_added_blob_tree_entry(&new_te, ct);
5359 if (err)
5360 goto done;
5361 err = report_ct_status(ct, status_cb, status_arg);
5362 if (err)
5363 goto done;
5364 ct->flags |= GOT_COMMITABLE_ADDED;
5365 err = insert_tree_entry(new_te, &paths);
5366 if (err)
5367 goto done;
5368 (*nentries)++;
5369 } else {
5370 *slash = '\0'; /* trim trailing path components */
5371 if (base_tree == NULL ||
5372 got_object_tree_find_entry(base_tree, child_path)
5373 == NULL) {
5374 err = make_subtree_for_added_blob(&new_te,
5375 child_path, path_base_tree,
5376 commitable_paths, status_cb, status_arg,
5377 repo);
5378 if (err)
5379 goto done;
5380 err = insert_tree_entry(new_te, &paths);
5381 if (err)
5382 goto done;
5383 (*nentries)++;
5388 if (base_tree) {
5389 int i, nbase_entries;
5390 /* Handle modified and deleted entries. */
5391 nbase_entries = got_object_tree_get_nentries(base_tree);
5392 for (i = 0; i < nbase_entries; i++) {
5393 struct got_commitable *ct = NULL;
5395 te = got_object_tree_get_entry(base_tree, i);
5396 if (got_object_tree_entry_is_submodule(te)) {
5397 /* Entry is a submodule; just copy it. */
5398 err = got_object_tree_entry_dup(&new_te, te);
5399 if (err)
5400 goto done;
5401 err = insert_tree_entry(new_te, &paths);
5402 if (err)
5403 goto done;
5404 (*nentries)++;
5405 continue;
5408 if (S_ISDIR(te->mode)) {
5409 int modified;
5410 err = got_object_tree_entry_dup(&new_te, te);
5411 if (err)
5412 goto done;
5413 err = match_modified_subtree(&modified, te,
5414 path_base_tree, commitable_paths);
5415 if (err)
5416 goto done;
5417 /* Avoid recursion into unmodified subtrees. */
5418 if (modified) {
5419 struct got_object_id *new_id;
5420 int nsubentries;
5421 err = write_subtree(&new_id,
5422 &nsubentries, te,
5423 path_base_tree, commitable_paths,
5424 status_cb, status_arg, repo);
5425 if (err)
5426 goto done;
5427 if (nsubentries == 0) {
5428 /* All entries were deleted. */
5429 free(new_id);
5430 continue;
5432 memcpy(&new_te->id, new_id,
5433 sizeof(new_te->id));
5434 free(new_id);
5436 err = insert_tree_entry(new_te, &paths);
5437 if (err)
5438 goto done;
5439 (*nentries)++;
5440 continue;
5443 err = match_deleted_or_modified_ct(&ct, te,
5444 path_base_tree, commitable_paths);
5445 if (err)
5446 goto done;
5447 if (ct) {
5448 /* NB: Deleted entries get dropped here. */
5449 if (ct->status == GOT_STATUS_MODIFY ||
5450 ct->status == GOT_STATUS_MODE_CHANGE ||
5451 ct->staged_status == GOT_STATUS_MODIFY) {
5452 err = alloc_modified_blob_tree_entry(
5453 &new_te, te, ct);
5454 if (err)
5455 goto done;
5456 err = insert_tree_entry(new_te, &paths);
5457 if (err)
5458 goto done;
5459 (*nentries)++;
5461 err = report_ct_status(ct, status_cb,
5462 status_arg);
5463 if (err)
5464 goto done;
5465 } else {
5466 /* Entry is unchanged; just copy it. */
5467 err = got_object_tree_entry_dup(&new_te, te);
5468 if (err)
5469 goto done;
5470 err = insert_tree_entry(new_te, &paths);
5471 if (err)
5472 goto done;
5473 (*nentries)++;
5478 /* Write new list of entries; deleted entries have been dropped. */
5479 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5480 done:
5481 got_pathlist_free(&paths);
5482 return err;
5485 static const struct got_error *
5486 update_fileindex_after_commit(struct got_worktree *worktree,
5487 struct got_pathlist_head *commitable_paths,
5488 struct got_object_id *new_base_commit_id,
5489 struct got_fileindex *fileindex, int have_staged_files)
5491 const struct got_error *err = NULL;
5492 struct got_pathlist_entry *pe;
5493 char *relpath = NULL;
5495 TAILQ_FOREACH(pe, commitable_paths, entry) {
5496 struct got_fileindex_entry *ie;
5497 struct got_commitable *ct = pe->data;
5499 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5501 err = got_path_skip_common_ancestor(&relpath,
5502 worktree->root_path, ct->ondisk_path);
5503 if (err)
5504 goto done;
5506 if (ie) {
5507 if (ct->status == GOT_STATUS_DELETE ||
5508 ct->staged_status == GOT_STATUS_DELETE) {
5509 got_fileindex_entry_remove(fileindex, ie);
5510 } else if (ct->staged_status == GOT_STATUS_ADD ||
5511 ct->staged_status == GOT_STATUS_MODIFY) {
5512 got_fileindex_entry_stage_set(ie,
5513 GOT_FILEIDX_STAGE_NONE);
5514 got_fileindex_entry_staged_filetype_set(ie, 0);
5516 err = got_fileindex_entry_update(ie,
5517 worktree->root_fd, relpath,
5518 ct->staged_blob_id->sha1,
5519 new_base_commit_id->sha1,
5520 !have_staged_files);
5521 } else
5522 err = got_fileindex_entry_update(ie,
5523 worktree->root_fd, relpath,
5524 ct->blob_id->sha1,
5525 new_base_commit_id->sha1,
5526 !have_staged_files);
5527 } else {
5528 err = got_fileindex_entry_alloc(&ie, pe->path);
5529 if (err)
5530 goto done;
5531 err = got_fileindex_entry_update(ie,
5532 worktree->root_fd, relpath, ct->blob_id->sha1,
5533 new_base_commit_id->sha1, 1);
5534 if (err) {
5535 got_fileindex_entry_free(ie);
5536 goto done;
5538 err = got_fileindex_entry_add(fileindex, ie);
5539 if (err) {
5540 got_fileindex_entry_free(ie);
5541 goto done;
5544 free(relpath);
5545 relpath = NULL;
5547 done:
5548 free(relpath);
5549 return err;
5553 static const struct got_error *
5554 check_out_of_date(const char *in_repo_path, unsigned char status,
5555 unsigned char staged_status, struct got_object_id *base_blob_id,
5556 struct got_object_id *base_commit_id,
5557 struct got_object_id *head_commit_id, struct got_repository *repo,
5558 int ood_errcode)
5560 const struct got_error *err = NULL;
5561 struct got_commit_object *commit = NULL;
5562 struct got_object_id *id = NULL;
5564 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5565 /* Trivial case: base commit == head commit */
5566 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5567 return NULL;
5569 * Ensure file content which local changes were based
5570 * on matches file content in the branch head.
5572 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5573 if (err)
5574 goto done;
5575 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5576 if (err) {
5577 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5578 err = got_error(ood_errcode);
5579 goto done;
5580 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5581 err = got_error(ood_errcode);
5582 } else {
5583 /* Require that added files don't exist in the branch head. */
5584 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5585 if (err)
5586 goto done;
5587 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5588 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5589 goto done;
5590 err = id ? got_error(ood_errcode) : NULL;
5592 done:
5593 free(id);
5594 if (commit)
5595 got_object_commit_close(commit);
5596 return err;
5599 const struct got_error *
5600 commit_worktree(struct got_object_id **new_commit_id,
5601 struct got_pathlist_head *commitable_paths,
5602 struct got_object_id *head_commit_id,
5603 struct got_object_id *parent_id2,
5604 struct got_worktree *worktree,
5605 const char *author, const char *committer,
5606 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5607 got_worktree_status_cb status_cb, void *status_arg,
5608 struct got_repository *repo)
5610 const struct got_error *err = NULL, *unlockerr = NULL;
5611 struct got_pathlist_entry *pe;
5612 const char *head_ref_name = NULL;
5613 struct got_commit_object *head_commit = NULL;
5614 struct got_reference *head_ref2 = NULL;
5615 struct got_object_id *head_commit_id2 = NULL;
5616 struct got_tree_object *head_tree = NULL;
5617 struct got_object_id *new_tree_id = NULL;
5618 int nentries, nparents = 0;
5619 struct got_object_id_queue parent_ids;
5620 struct got_object_qid *pid = NULL;
5621 char *logmsg = NULL;
5623 *new_commit_id = NULL;
5625 STAILQ_INIT(&parent_ids);
5627 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5628 if (err)
5629 goto done;
5631 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5632 if (err)
5633 goto done;
5635 if (commit_msg_cb != NULL) {
5636 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5637 if (err)
5638 goto done;
5641 if (logmsg == NULL || strlen(logmsg) == 0) {
5642 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5643 goto done;
5646 /* Create blobs from added and modified files and record their IDs. */
5647 TAILQ_FOREACH(pe, commitable_paths, entry) {
5648 struct got_commitable *ct = pe->data;
5649 char *ondisk_path;
5651 /* Blobs for staged files already exist. */
5652 if (ct->staged_status == GOT_STATUS_ADD ||
5653 ct->staged_status == GOT_STATUS_MODIFY)
5654 continue;
5656 if (ct->status != GOT_STATUS_ADD &&
5657 ct->status != GOT_STATUS_MODIFY &&
5658 ct->status != GOT_STATUS_MODE_CHANGE)
5659 continue;
5661 if (asprintf(&ondisk_path, "%s/%s",
5662 worktree->root_path, pe->path) == -1) {
5663 err = got_error_from_errno("asprintf");
5664 goto done;
5666 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5667 free(ondisk_path);
5668 if (err)
5669 goto done;
5672 /* Recursively write new tree objects. */
5673 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5674 commitable_paths, status_cb, status_arg, repo);
5675 if (err)
5676 goto done;
5678 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5679 if (err)
5680 goto done;
5681 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5682 nparents++;
5683 if (parent_id2) {
5684 err = got_object_qid_alloc(&pid, parent_id2);
5685 if (err)
5686 goto done;
5687 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5688 nparents++;
5690 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5691 nparents, author, time(NULL), committer, time(NULL), logmsg, repo);
5692 if (logmsg != NULL)
5693 free(logmsg);
5694 if (err)
5695 goto done;
5697 /* Check if a concurrent commit to our branch has occurred. */
5698 head_ref_name = got_worktree_get_head_ref_name(worktree);
5699 if (head_ref_name == NULL) {
5700 err = got_error_from_errno("got_worktree_get_head_ref_name");
5701 goto done;
5703 /* Lock the reference here to prevent concurrent modification. */
5704 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5705 if (err)
5706 goto done;
5707 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5708 if (err)
5709 goto done;
5710 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5711 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5712 goto done;
5714 /* Update branch head in repository. */
5715 err = got_ref_change_ref(head_ref2, *new_commit_id);
5716 if (err)
5717 goto done;
5718 err = got_ref_write(head_ref2, repo);
5719 if (err)
5720 goto done;
5722 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5723 if (err)
5724 goto done;
5726 err = ref_base_commit(worktree, repo);
5727 if (err)
5728 goto done;
5729 done:
5730 got_object_id_queue_free(&parent_ids);
5731 if (head_tree)
5732 got_object_tree_close(head_tree);
5733 if (head_commit)
5734 got_object_commit_close(head_commit);
5735 free(head_commit_id2);
5736 if (head_ref2) {
5737 unlockerr = got_ref_unlock(head_ref2);
5738 if (unlockerr && err == NULL)
5739 err = unlockerr;
5740 got_ref_close(head_ref2);
5742 return err;
5745 static const struct got_error *
5746 check_path_is_commitable(const char *path,
5747 struct got_pathlist_head *commitable_paths)
5749 struct got_pathlist_entry *cpe = NULL;
5750 size_t path_len = strlen(path);
5752 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5753 struct got_commitable *ct = cpe->data;
5754 const char *ct_path = ct->path;
5756 while (ct_path[0] == '/')
5757 ct_path++;
5759 if (strcmp(path, ct_path) == 0 ||
5760 got_path_is_child(ct_path, path, path_len))
5761 break;
5764 if (cpe == NULL)
5765 return got_error_path(path, GOT_ERR_BAD_PATH);
5767 return NULL;
5770 static const struct got_error *
5771 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5773 int *have_staged_files = arg;
5775 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5776 *have_staged_files = 1;
5777 return got_error(GOT_ERR_CANCELLED);
5780 return NULL;
5783 static const struct got_error *
5784 check_non_staged_files(struct got_fileindex *fileindex,
5785 struct got_pathlist_head *paths)
5787 struct got_pathlist_entry *pe;
5788 struct got_fileindex_entry *ie;
5790 TAILQ_FOREACH(pe, paths, entry) {
5791 if (pe->path[0] == '\0')
5792 continue;
5793 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5794 if (ie == NULL)
5795 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5796 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5797 return got_error_path(pe->path,
5798 GOT_ERR_FILE_NOT_STAGED);
5801 return NULL;
5804 const struct got_error *
5805 got_worktree_commit(struct got_object_id **new_commit_id,
5806 struct got_worktree *worktree, struct got_pathlist_head *paths,
5807 const char *author, const char *committer, int allow_bad_symlinks,
5808 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5809 got_worktree_status_cb status_cb, void *status_arg,
5810 struct got_repository *repo)
5812 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5813 struct got_fileindex *fileindex = NULL;
5814 char *fileindex_path = NULL;
5815 struct got_pathlist_head commitable_paths;
5816 struct collect_commitables_arg cc_arg;
5817 struct got_pathlist_entry *pe;
5818 struct got_reference *head_ref = NULL;
5819 struct got_object_id *head_commit_id = NULL;
5820 int have_staged_files = 0;
5822 *new_commit_id = NULL;
5824 TAILQ_INIT(&commitable_paths);
5826 err = lock_worktree(worktree, LOCK_EX);
5827 if (err)
5828 goto done;
5830 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5831 if (err)
5832 goto done;
5834 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5835 if (err)
5836 goto done;
5838 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5839 if (err)
5840 goto done;
5842 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5843 &have_staged_files);
5844 if (err && err->code != GOT_ERR_CANCELLED)
5845 goto done;
5846 if (have_staged_files) {
5847 err = check_non_staged_files(fileindex, paths);
5848 if (err)
5849 goto done;
5852 cc_arg.commitable_paths = &commitable_paths;
5853 cc_arg.worktree = worktree;
5854 cc_arg.fileindex = fileindex;
5855 cc_arg.repo = repo;
5856 cc_arg.have_staged_files = have_staged_files;
5857 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5858 TAILQ_FOREACH(pe, paths, entry) {
5859 err = worktree_status(worktree, pe->path, fileindex, repo,
5860 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
5861 if (err)
5862 goto done;
5865 if (TAILQ_EMPTY(&commitable_paths)) {
5866 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5867 goto done;
5870 TAILQ_FOREACH(pe, paths, entry) {
5871 err = check_path_is_commitable(pe->path, &commitable_paths);
5872 if (err)
5873 goto done;
5876 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5877 struct got_commitable *ct = pe->data;
5878 const char *ct_path = ct->in_repo_path;
5880 while (ct_path[0] == '/')
5881 ct_path++;
5882 err = check_out_of_date(ct_path, ct->status,
5883 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5884 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5885 if (err)
5886 goto done;
5890 err = commit_worktree(new_commit_id, &commitable_paths,
5891 head_commit_id, NULL, worktree, author, committer,
5892 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5893 if (err)
5894 goto done;
5896 err = update_fileindex_after_commit(worktree, &commitable_paths,
5897 *new_commit_id, fileindex, have_staged_files);
5898 sync_err = sync_fileindex(fileindex, fileindex_path);
5899 if (sync_err && err == NULL)
5900 err = sync_err;
5901 done:
5902 if (fileindex)
5903 got_fileindex_free(fileindex);
5904 free(fileindex_path);
5905 unlockerr = lock_worktree(worktree, LOCK_SH);
5906 if (unlockerr && err == NULL)
5907 err = unlockerr;
5908 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5909 struct got_commitable *ct = pe->data;
5910 free_commitable(ct);
5912 got_pathlist_free(&commitable_paths);
5913 return err;
5916 const char *
5917 got_commitable_get_path(struct got_commitable *ct)
5919 return ct->path;
5922 unsigned int
5923 got_commitable_get_status(struct got_commitable *ct)
5925 return ct->status;
5928 struct check_rebase_ok_arg {
5929 struct got_worktree *worktree;
5930 struct got_repository *repo;
5933 static const struct got_error *
5934 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5936 const struct got_error *err = NULL;
5937 struct check_rebase_ok_arg *a = arg;
5938 unsigned char status;
5939 struct stat sb;
5940 char *ondisk_path;
5942 /* Reject rebase of a work tree with mixed base commits. */
5943 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5944 SHA1_DIGEST_LENGTH))
5945 return got_error(GOT_ERR_MIXED_COMMITS);
5947 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5948 == -1)
5949 return got_error_from_errno("asprintf");
5951 /* Reject rebase of a work tree with modified or staged files. */
5952 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5953 free(ondisk_path);
5954 if (err)
5955 return err;
5957 if (status != GOT_STATUS_NO_CHANGE)
5958 return got_error(GOT_ERR_MODIFIED);
5959 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5960 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5962 return NULL;
5965 const struct got_error *
5966 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5967 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5968 struct got_worktree *worktree, struct got_reference *branch,
5969 struct got_repository *repo)
5971 const struct got_error *err = NULL;
5972 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5973 char *branch_ref_name = NULL;
5974 char *fileindex_path = NULL;
5975 struct check_rebase_ok_arg ok_arg;
5976 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5977 struct got_object_id *wt_branch_tip = NULL;
5979 *new_base_branch_ref = NULL;
5980 *tmp_branch = NULL;
5981 *fileindex = NULL;
5983 err = lock_worktree(worktree, LOCK_EX);
5984 if (err)
5985 return err;
5987 err = open_fileindex(fileindex, &fileindex_path, worktree);
5988 if (err)
5989 goto done;
5991 ok_arg.worktree = worktree;
5992 ok_arg.repo = repo;
5993 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5994 &ok_arg);
5995 if (err)
5996 goto done;
5998 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5999 if (err)
6000 goto done;
6002 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6003 if (err)
6004 goto done;
6006 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6007 if (err)
6008 goto done;
6010 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6011 0);
6012 if (err)
6013 goto done;
6015 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6016 if (err)
6017 goto done;
6018 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6019 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6020 goto done;
6023 err = got_ref_alloc_symref(new_base_branch_ref,
6024 new_base_branch_ref_name, wt_branch);
6025 if (err)
6026 goto done;
6027 err = got_ref_write(*new_base_branch_ref, repo);
6028 if (err)
6029 goto done;
6031 /* TODO Lock original branch's ref while rebasing? */
6033 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6034 if (err)
6035 goto done;
6037 err = got_ref_write(branch_ref, repo);
6038 if (err)
6039 goto done;
6041 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6042 worktree->base_commit_id);
6043 if (err)
6044 goto done;
6045 err = got_ref_write(*tmp_branch, repo);
6046 if (err)
6047 goto done;
6049 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6050 if (err)
6051 goto done;
6052 done:
6053 free(fileindex_path);
6054 free(tmp_branch_name);
6055 free(new_base_branch_ref_name);
6056 free(branch_ref_name);
6057 if (branch_ref)
6058 got_ref_close(branch_ref);
6059 if (wt_branch)
6060 got_ref_close(wt_branch);
6061 free(wt_branch_tip);
6062 if (err) {
6063 if (*new_base_branch_ref) {
6064 got_ref_close(*new_base_branch_ref);
6065 *new_base_branch_ref = NULL;
6067 if (*tmp_branch) {
6068 got_ref_close(*tmp_branch);
6069 *tmp_branch = NULL;
6071 if (*fileindex) {
6072 got_fileindex_free(*fileindex);
6073 *fileindex = NULL;
6075 lock_worktree(worktree, LOCK_SH);
6077 return err;
6080 const struct got_error *
6081 got_worktree_rebase_continue(struct got_object_id **commit_id,
6082 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6083 struct got_reference **branch, struct got_fileindex **fileindex,
6084 struct got_worktree *worktree, struct got_repository *repo)
6086 const struct got_error *err;
6087 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6088 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6089 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6090 char *fileindex_path = NULL;
6091 int have_staged_files = 0;
6093 *commit_id = NULL;
6094 *new_base_branch = NULL;
6095 *tmp_branch = NULL;
6096 *branch = NULL;
6097 *fileindex = NULL;
6099 err = lock_worktree(worktree, LOCK_EX);
6100 if (err)
6101 return err;
6103 err = open_fileindex(fileindex, &fileindex_path, worktree);
6104 if (err)
6105 goto done;
6107 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6108 &have_staged_files);
6109 if (err && err->code != GOT_ERR_CANCELLED)
6110 goto done;
6111 if (have_staged_files) {
6112 err = got_error(GOT_ERR_STAGED_PATHS);
6113 goto done;
6116 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6117 if (err)
6118 goto done;
6120 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6121 if (err)
6122 goto done;
6124 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6125 if (err)
6126 goto done;
6128 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6129 if (err)
6130 goto done;
6132 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6133 if (err)
6134 goto done;
6136 err = got_ref_open(branch, repo,
6137 got_ref_get_symref_target(branch_ref), 0);
6138 if (err)
6139 goto done;
6141 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6142 if (err)
6143 goto done;
6145 err = got_ref_resolve(commit_id, repo, commit_ref);
6146 if (err)
6147 goto done;
6149 err = got_ref_open(new_base_branch, repo,
6150 new_base_branch_ref_name, 0);
6151 if (err)
6152 goto done;
6154 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6155 if (err)
6156 goto done;
6157 done:
6158 free(commit_ref_name);
6159 free(branch_ref_name);
6160 free(fileindex_path);
6161 if (commit_ref)
6162 got_ref_close(commit_ref);
6163 if (branch_ref)
6164 got_ref_close(branch_ref);
6165 if (err) {
6166 free(*commit_id);
6167 *commit_id = NULL;
6168 if (*tmp_branch) {
6169 got_ref_close(*tmp_branch);
6170 *tmp_branch = NULL;
6172 if (*new_base_branch) {
6173 got_ref_close(*new_base_branch);
6174 *new_base_branch = NULL;
6176 if (*branch) {
6177 got_ref_close(*branch);
6178 *branch = NULL;
6180 if (*fileindex) {
6181 got_fileindex_free(*fileindex);
6182 *fileindex = NULL;
6184 lock_worktree(worktree, LOCK_SH);
6186 return err;
6189 const struct got_error *
6190 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6192 const struct got_error *err;
6193 char *tmp_branch_name = NULL;
6195 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6196 if (err)
6197 return err;
6199 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6200 free(tmp_branch_name);
6201 return NULL;
6204 static const struct got_error *
6205 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6206 char **logmsg, void *arg)
6208 *logmsg = arg;
6209 return NULL;
6212 static const struct got_error *
6213 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6214 const char *path, struct got_object_id *blob_id,
6215 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6216 int dirfd, const char *de_name)
6218 return NULL;
6221 struct collect_merged_paths_arg {
6222 got_worktree_checkout_cb progress_cb;
6223 void *progress_arg;
6224 struct got_pathlist_head *merged_paths;
6227 static const struct got_error *
6228 collect_merged_paths(void *arg, unsigned char status, const char *path)
6230 const struct got_error *err;
6231 struct collect_merged_paths_arg *a = arg;
6232 char *p;
6233 struct got_pathlist_entry *new;
6235 err = (*a->progress_cb)(a->progress_arg, status, path);
6236 if (err)
6237 return err;
6239 if (status != GOT_STATUS_MERGE &&
6240 status != GOT_STATUS_ADD &&
6241 status != GOT_STATUS_DELETE &&
6242 status != GOT_STATUS_CONFLICT)
6243 return NULL;
6245 p = strdup(path);
6246 if (p == NULL)
6247 return got_error_from_errno("strdup");
6249 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6250 if (err || new == NULL)
6251 free(p);
6252 return err;
6255 void
6256 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6258 struct got_pathlist_entry *pe;
6260 TAILQ_FOREACH(pe, merged_paths, entry)
6261 free((char *)pe->path);
6263 got_pathlist_free(merged_paths);
6266 static const struct got_error *
6267 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6268 int is_rebase, struct got_repository *repo)
6270 const struct got_error *err;
6271 struct got_reference *commit_ref = NULL;
6273 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6274 if (err) {
6275 if (err->code != GOT_ERR_NOT_REF)
6276 goto done;
6277 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6278 if (err)
6279 goto done;
6280 err = got_ref_write(commit_ref, repo);
6281 if (err)
6282 goto done;
6283 } else if (is_rebase) {
6284 struct got_object_id *stored_id;
6285 int cmp;
6287 err = got_ref_resolve(&stored_id, repo, commit_ref);
6288 if (err)
6289 goto done;
6290 cmp = got_object_id_cmp(commit_id, stored_id);
6291 free(stored_id);
6292 if (cmp != 0) {
6293 err = got_error(GOT_ERR_REBASE_COMMITID);
6294 goto done;
6297 done:
6298 if (commit_ref)
6299 got_ref_close(commit_ref);
6300 return err;
6303 static const struct got_error *
6304 rebase_merge_files(struct got_pathlist_head *merged_paths,
6305 const char *commit_ref_name, struct got_worktree *worktree,
6306 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6307 struct got_object_id *commit_id, struct got_repository *repo,
6308 got_worktree_checkout_cb progress_cb, void *progress_arg,
6309 got_cancel_cb cancel_cb, void *cancel_arg)
6311 const struct got_error *err;
6312 struct got_reference *commit_ref = NULL;
6313 struct collect_merged_paths_arg cmp_arg;
6314 char *fileindex_path;
6316 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6318 err = get_fileindex_path(&fileindex_path, worktree);
6319 if (err)
6320 return err;
6322 cmp_arg.progress_cb = progress_cb;
6323 cmp_arg.progress_arg = progress_arg;
6324 cmp_arg.merged_paths = merged_paths;
6325 err = merge_files(worktree, fileindex, fileindex_path,
6326 parent_commit_id, commit_id, repo, collect_merged_paths,
6327 &cmp_arg, cancel_cb, cancel_arg);
6328 if (commit_ref)
6329 got_ref_close(commit_ref);
6330 return err;
6333 const struct got_error *
6334 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6335 struct got_worktree *worktree, struct got_fileindex *fileindex,
6336 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6337 struct got_repository *repo,
6338 got_worktree_checkout_cb progress_cb, void *progress_arg,
6339 got_cancel_cb cancel_cb, void *cancel_arg)
6341 const struct got_error *err;
6342 char *commit_ref_name;
6344 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6345 if (err)
6346 return err;
6348 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6349 if (err)
6350 goto done;
6352 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6353 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6354 progress_arg, cancel_cb, cancel_arg);
6355 done:
6356 free(commit_ref_name);
6357 return err;
6360 const struct got_error *
6361 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6362 struct got_worktree *worktree, struct got_fileindex *fileindex,
6363 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6364 struct got_repository *repo,
6365 got_worktree_checkout_cb progress_cb, void *progress_arg,
6366 got_cancel_cb cancel_cb, void *cancel_arg)
6368 const struct got_error *err;
6369 char *commit_ref_name;
6371 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6372 if (err)
6373 return err;
6375 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6376 if (err)
6377 goto done;
6379 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6380 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6381 progress_arg, cancel_cb, cancel_arg);
6382 done:
6383 free(commit_ref_name);
6384 return err;
6387 static const struct got_error *
6388 rebase_commit(struct got_object_id **new_commit_id,
6389 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6390 struct got_worktree *worktree, struct got_fileindex *fileindex,
6391 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6392 const char *new_logmsg, struct got_repository *repo)
6394 const struct got_error *err, *sync_err;
6395 struct got_pathlist_head commitable_paths;
6396 struct collect_commitables_arg cc_arg;
6397 char *fileindex_path = NULL;
6398 struct got_reference *head_ref = NULL;
6399 struct got_object_id *head_commit_id = NULL;
6400 char *logmsg = NULL;
6402 TAILQ_INIT(&commitable_paths);
6403 *new_commit_id = NULL;
6405 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6407 err = get_fileindex_path(&fileindex_path, worktree);
6408 if (err)
6409 return err;
6411 cc_arg.commitable_paths = &commitable_paths;
6412 cc_arg.worktree = worktree;
6413 cc_arg.repo = repo;
6414 cc_arg.have_staged_files = 0;
6416 * If possible get the status of individual files directly to
6417 * avoid crawling the entire work tree once per rebased commit.
6419 * Ideally, merged_paths would contain a list of commitables
6420 * we could use so we could skip worktree_status() entirely.
6421 * However, we would then need carefully keep track of cumulative
6422 * effects of operations such as file additions and deletions
6423 * in 'got histedit -f' (folding multiple commits into one),
6424 * and this extra complexity is not really worth it.
6426 if (merged_paths) {
6427 struct got_pathlist_entry *pe;
6428 TAILQ_FOREACH(pe, merged_paths, entry) {
6429 err = worktree_status(worktree, pe->path, fileindex,
6430 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6431 0);
6432 if (err)
6433 goto done;
6435 } else {
6436 err = worktree_status(worktree, "", fileindex, repo,
6437 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6438 if (err)
6439 goto done;
6442 if (TAILQ_EMPTY(&commitable_paths)) {
6443 /* No-op change; commit will be elided. */
6444 err = got_ref_delete(commit_ref, repo);
6445 if (err)
6446 goto done;
6447 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6448 goto done;
6451 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6452 if (err)
6453 goto done;
6455 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6456 if (err)
6457 goto done;
6459 if (new_logmsg) {
6460 logmsg = strdup(new_logmsg);
6461 if (logmsg == NULL) {
6462 err = got_error_from_errno("strdup");
6463 goto done;
6465 } else {
6466 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6467 if (err)
6468 goto done;
6471 /* NB: commit_worktree will call free(logmsg) */
6472 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6473 NULL, worktree, got_object_commit_get_author(orig_commit),
6474 got_object_commit_get_committer(orig_commit),
6475 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6476 if (err)
6477 goto done;
6479 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6480 if (err)
6481 goto done;
6483 err = got_ref_delete(commit_ref, repo);
6484 if (err)
6485 goto done;
6487 err = update_fileindex_after_commit(worktree, &commitable_paths,
6488 *new_commit_id, fileindex, 0);
6489 sync_err = sync_fileindex(fileindex, fileindex_path);
6490 if (sync_err && err == NULL)
6491 err = sync_err;
6492 done:
6493 free(fileindex_path);
6494 free(head_commit_id);
6495 if (head_ref)
6496 got_ref_close(head_ref);
6497 if (err) {
6498 free(*new_commit_id);
6499 *new_commit_id = NULL;
6501 return err;
6504 const struct got_error *
6505 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6506 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6507 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6508 struct got_commit_object *orig_commit,
6509 struct got_object_id *orig_commit_id, struct got_repository *repo)
6511 const struct got_error *err;
6512 char *commit_ref_name;
6513 struct got_reference *commit_ref = NULL;
6514 struct got_object_id *commit_id = NULL;
6516 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6517 if (err)
6518 return err;
6520 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6521 if (err)
6522 goto done;
6523 err = got_ref_resolve(&commit_id, repo, commit_ref);
6524 if (err)
6525 goto done;
6526 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6527 err = got_error(GOT_ERR_REBASE_COMMITID);
6528 goto done;
6531 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6532 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6533 done:
6534 if (commit_ref)
6535 got_ref_close(commit_ref);
6536 free(commit_ref_name);
6537 free(commit_id);
6538 return err;
6541 const struct got_error *
6542 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6543 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6544 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6545 struct got_commit_object *orig_commit,
6546 struct got_object_id *orig_commit_id, const char *new_logmsg,
6547 struct got_repository *repo)
6549 const struct got_error *err;
6550 char *commit_ref_name;
6551 struct got_reference *commit_ref = NULL;
6553 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6554 if (err)
6555 return err;
6557 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6558 if (err)
6559 goto done;
6561 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6562 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6563 done:
6564 if (commit_ref)
6565 got_ref_close(commit_ref);
6566 free(commit_ref_name);
6567 return err;
6570 const struct got_error *
6571 got_worktree_rebase_postpone(struct got_worktree *worktree,
6572 struct got_fileindex *fileindex)
6574 if (fileindex)
6575 got_fileindex_free(fileindex);
6576 return lock_worktree(worktree, LOCK_SH);
6579 static const struct got_error *
6580 delete_ref(const char *name, struct got_repository *repo)
6582 const struct got_error *err;
6583 struct got_reference *ref;
6585 err = got_ref_open(&ref, repo, name, 0);
6586 if (err) {
6587 if (err->code == GOT_ERR_NOT_REF)
6588 return NULL;
6589 return err;
6592 err = got_ref_delete(ref, repo);
6593 got_ref_close(ref);
6594 return err;
6597 static const struct got_error *
6598 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6600 const struct got_error *err;
6601 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6602 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6604 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6605 if (err)
6606 goto done;
6607 err = delete_ref(tmp_branch_name, repo);
6608 if (err)
6609 goto done;
6611 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6612 if (err)
6613 goto done;
6614 err = delete_ref(new_base_branch_ref_name, repo);
6615 if (err)
6616 goto done;
6618 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6619 if (err)
6620 goto done;
6621 err = delete_ref(branch_ref_name, repo);
6622 if (err)
6623 goto done;
6625 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6626 if (err)
6627 goto done;
6628 err = delete_ref(commit_ref_name, repo);
6629 if (err)
6630 goto done;
6632 done:
6633 free(tmp_branch_name);
6634 free(new_base_branch_ref_name);
6635 free(branch_ref_name);
6636 free(commit_ref_name);
6637 return err;
6640 const struct got_error *
6641 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6642 struct got_object_id *new_commit_id, struct got_repository *repo)
6644 const struct got_error *err;
6645 struct got_reference *ref = NULL;
6646 struct got_object_id *old_commit_id = NULL;
6647 const char *branch_name = NULL;
6648 char *new_id_str = NULL;
6649 char *refname = NULL;
6651 branch_name = got_ref_get_name(branch);
6652 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6653 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6654 branch_name += 11;
6656 err = got_object_id_str(&new_id_str, new_commit_id);
6657 if (err)
6658 return err;
6660 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6661 new_id_str) == -1) {
6662 err = got_error_from_errno("asprintf");
6663 goto done;
6666 err = got_ref_resolve(&old_commit_id, repo, branch);
6667 if (err)
6668 goto done;
6670 err = got_ref_alloc(&ref, refname, old_commit_id);
6671 if (err)
6672 goto done;
6674 err = got_ref_write(ref, repo);
6675 done:
6676 free(new_id_str);
6677 free(refname);
6678 free(old_commit_id);
6679 if (ref)
6680 got_ref_close(ref);
6681 return err;
6684 const struct got_error *
6685 got_worktree_rebase_complete(struct got_worktree *worktree,
6686 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6687 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6688 struct got_repository *repo, int create_backup)
6690 const struct got_error *err, *unlockerr, *sync_err;
6691 struct got_object_id *new_head_commit_id = NULL;
6692 char *fileindex_path = NULL;
6694 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6695 if (err)
6696 return err;
6698 if (create_backup) {
6699 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6700 rebased_branch, new_head_commit_id, repo);
6701 if (err)
6702 goto done;
6705 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6706 if (err)
6707 goto done;
6709 err = got_ref_write(rebased_branch, repo);
6710 if (err)
6711 goto done;
6713 err = got_worktree_set_head_ref(worktree, rebased_branch);
6714 if (err)
6715 goto done;
6717 err = delete_rebase_refs(worktree, repo);
6718 if (err)
6719 goto done;
6721 err = get_fileindex_path(&fileindex_path, worktree);
6722 if (err)
6723 goto done;
6724 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6725 sync_err = sync_fileindex(fileindex, fileindex_path);
6726 if (sync_err && err == NULL)
6727 err = sync_err;
6728 done:
6729 got_fileindex_free(fileindex);
6730 free(fileindex_path);
6731 free(new_head_commit_id);
6732 unlockerr = lock_worktree(worktree, LOCK_SH);
6733 if (unlockerr && err == NULL)
6734 err = unlockerr;
6735 return err;
6738 const struct got_error *
6739 got_worktree_rebase_abort(struct got_worktree *worktree,
6740 struct got_fileindex *fileindex, struct got_repository *repo,
6741 struct got_reference *new_base_branch,
6742 got_worktree_checkout_cb progress_cb, void *progress_arg)
6744 const struct got_error *err, *unlockerr, *sync_err;
6745 struct got_reference *resolved = NULL;
6746 struct got_object_id *commit_id = NULL;
6747 struct got_commit_object *commit = NULL;
6748 char *fileindex_path = NULL;
6749 struct revert_file_args rfa;
6750 struct got_object_id *tree_id = NULL;
6752 err = lock_worktree(worktree, LOCK_EX);
6753 if (err)
6754 return err;
6756 err = got_object_open_as_commit(&commit, repo,
6757 worktree->base_commit_id);
6758 if (err)
6759 goto done;
6761 err = got_ref_open(&resolved, repo,
6762 got_ref_get_symref_target(new_base_branch), 0);
6763 if (err)
6764 goto done;
6766 err = got_worktree_set_head_ref(worktree, resolved);
6767 if (err)
6768 goto done;
6771 * XXX commits to the base branch could have happened while
6772 * we were busy rebasing; should we store the original commit ID
6773 * when rebase begins and read it back here?
6775 err = got_ref_resolve(&commit_id, repo, resolved);
6776 if (err)
6777 goto done;
6779 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6780 if (err)
6781 goto done;
6783 err = got_object_id_by_path(&tree_id, repo, commit,
6784 worktree->path_prefix);
6785 if (err)
6786 goto done;
6788 err = delete_rebase_refs(worktree, repo);
6789 if (err)
6790 goto done;
6792 err = get_fileindex_path(&fileindex_path, worktree);
6793 if (err)
6794 goto done;
6796 rfa.worktree = worktree;
6797 rfa.fileindex = fileindex;
6798 rfa.progress_cb = progress_cb;
6799 rfa.progress_arg = progress_arg;
6800 rfa.patch_cb = NULL;
6801 rfa.patch_arg = NULL;
6802 rfa.repo = repo;
6803 rfa.unlink_added_files = 0;
6804 err = worktree_status(worktree, "", fileindex, repo,
6805 revert_file, &rfa, NULL, NULL, 1, 0);
6806 if (err)
6807 goto sync;
6809 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6810 repo, progress_cb, progress_arg, NULL, NULL);
6811 sync:
6812 sync_err = sync_fileindex(fileindex, fileindex_path);
6813 if (sync_err && err == NULL)
6814 err = sync_err;
6815 done:
6816 got_ref_close(resolved);
6817 free(tree_id);
6818 free(commit_id);
6819 if (commit)
6820 got_object_commit_close(commit);
6821 if (fileindex)
6822 got_fileindex_free(fileindex);
6823 free(fileindex_path);
6825 unlockerr = lock_worktree(worktree, LOCK_SH);
6826 if (unlockerr && err == NULL)
6827 err = unlockerr;
6828 return err;
6831 const struct got_error *
6832 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6833 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6834 struct got_fileindex **fileindex, struct got_worktree *worktree,
6835 struct got_repository *repo)
6837 const struct got_error *err = NULL;
6838 char *tmp_branch_name = NULL;
6839 char *branch_ref_name = NULL;
6840 char *base_commit_ref_name = NULL;
6841 char *fileindex_path = NULL;
6842 struct check_rebase_ok_arg ok_arg;
6843 struct got_reference *wt_branch = NULL;
6844 struct got_reference *base_commit_ref = NULL;
6846 *tmp_branch = NULL;
6847 *branch_ref = NULL;
6848 *base_commit_id = NULL;
6849 *fileindex = NULL;
6851 err = lock_worktree(worktree, LOCK_EX);
6852 if (err)
6853 return err;
6855 err = open_fileindex(fileindex, &fileindex_path, worktree);
6856 if (err)
6857 goto done;
6859 ok_arg.worktree = worktree;
6860 ok_arg.repo = repo;
6861 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6862 &ok_arg);
6863 if (err)
6864 goto done;
6866 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6867 if (err)
6868 goto done;
6870 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6871 if (err)
6872 goto done;
6874 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6875 worktree);
6876 if (err)
6877 goto done;
6879 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6880 0);
6881 if (err)
6882 goto done;
6884 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6885 if (err)
6886 goto done;
6888 err = got_ref_write(*branch_ref, repo);
6889 if (err)
6890 goto done;
6892 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6893 worktree->base_commit_id);
6894 if (err)
6895 goto done;
6896 err = got_ref_write(base_commit_ref, repo);
6897 if (err)
6898 goto done;
6899 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6900 if (*base_commit_id == NULL) {
6901 err = got_error_from_errno("got_object_id_dup");
6902 goto done;
6905 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6906 worktree->base_commit_id);
6907 if (err)
6908 goto done;
6909 err = got_ref_write(*tmp_branch, repo);
6910 if (err)
6911 goto done;
6913 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6914 if (err)
6915 goto done;
6916 done:
6917 free(fileindex_path);
6918 free(tmp_branch_name);
6919 free(branch_ref_name);
6920 free(base_commit_ref_name);
6921 if (wt_branch)
6922 got_ref_close(wt_branch);
6923 if (err) {
6924 if (*branch_ref) {
6925 got_ref_close(*branch_ref);
6926 *branch_ref = NULL;
6928 if (*tmp_branch) {
6929 got_ref_close(*tmp_branch);
6930 *tmp_branch = NULL;
6932 free(*base_commit_id);
6933 if (*fileindex) {
6934 got_fileindex_free(*fileindex);
6935 *fileindex = NULL;
6937 lock_worktree(worktree, LOCK_SH);
6939 return err;
6942 const struct got_error *
6943 got_worktree_histedit_postpone(struct got_worktree *worktree,
6944 struct got_fileindex *fileindex)
6946 if (fileindex)
6947 got_fileindex_free(fileindex);
6948 return lock_worktree(worktree, LOCK_SH);
6951 const struct got_error *
6952 got_worktree_histedit_in_progress(int *in_progress,
6953 struct got_worktree *worktree)
6955 const struct got_error *err;
6956 char *tmp_branch_name = NULL;
6958 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6959 if (err)
6960 return err;
6962 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6963 free(tmp_branch_name);
6964 return NULL;
6967 const struct got_error *
6968 got_worktree_histedit_continue(struct got_object_id **commit_id,
6969 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6970 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6971 struct got_worktree *worktree, struct got_repository *repo)
6973 const struct got_error *err;
6974 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6975 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6976 struct got_reference *commit_ref = NULL;
6977 struct got_reference *base_commit_ref = NULL;
6978 char *fileindex_path = NULL;
6979 int have_staged_files = 0;
6981 *commit_id = NULL;
6982 *tmp_branch = NULL;
6983 *base_commit_id = NULL;
6984 *fileindex = NULL;
6986 err = lock_worktree(worktree, LOCK_EX);
6987 if (err)
6988 return err;
6990 err = open_fileindex(fileindex, &fileindex_path, worktree);
6991 if (err)
6992 goto done;
6994 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6995 &have_staged_files);
6996 if (err && err->code != GOT_ERR_CANCELLED)
6997 goto done;
6998 if (have_staged_files) {
6999 err = got_error(GOT_ERR_STAGED_PATHS);
7000 goto done;
7003 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7004 if (err)
7005 goto done;
7007 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7008 if (err)
7009 goto done;
7011 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7012 if (err)
7013 goto done;
7015 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7016 worktree);
7017 if (err)
7018 goto done;
7020 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7021 if (err)
7022 goto done;
7024 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7025 if (err)
7026 goto done;
7027 err = got_ref_resolve(commit_id, repo, commit_ref);
7028 if (err)
7029 goto done;
7031 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7032 if (err)
7033 goto done;
7034 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7035 if (err)
7036 goto done;
7038 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7039 if (err)
7040 goto done;
7041 done:
7042 free(commit_ref_name);
7043 free(branch_ref_name);
7044 free(fileindex_path);
7045 if (commit_ref)
7046 got_ref_close(commit_ref);
7047 if (base_commit_ref)
7048 got_ref_close(base_commit_ref);
7049 if (err) {
7050 free(*commit_id);
7051 *commit_id = NULL;
7052 free(*base_commit_id);
7053 *base_commit_id = NULL;
7054 if (*tmp_branch) {
7055 got_ref_close(*tmp_branch);
7056 *tmp_branch = NULL;
7058 if (*fileindex) {
7059 got_fileindex_free(*fileindex);
7060 *fileindex = NULL;
7062 lock_worktree(worktree, LOCK_EX);
7064 return err;
7067 static const struct got_error *
7068 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7070 const struct got_error *err;
7071 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7072 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7074 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7075 if (err)
7076 goto done;
7077 err = delete_ref(tmp_branch_name, repo);
7078 if (err)
7079 goto done;
7081 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7082 worktree);
7083 if (err)
7084 goto done;
7085 err = delete_ref(base_commit_ref_name, repo);
7086 if (err)
7087 goto done;
7089 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7090 if (err)
7091 goto done;
7092 err = delete_ref(branch_ref_name, repo);
7093 if (err)
7094 goto done;
7096 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7097 if (err)
7098 goto done;
7099 err = delete_ref(commit_ref_name, repo);
7100 if (err)
7101 goto done;
7102 done:
7103 free(tmp_branch_name);
7104 free(base_commit_ref_name);
7105 free(branch_ref_name);
7106 free(commit_ref_name);
7107 return err;
7110 const struct got_error *
7111 got_worktree_histedit_abort(struct got_worktree *worktree,
7112 struct got_fileindex *fileindex, struct got_repository *repo,
7113 struct got_reference *branch, struct got_object_id *base_commit_id,
7114 got_worktree_checkout_cb progress_cb, void *progress_arg)
7116 const struct got_error *err, *unlockerr, *sync_err;
7117 struct got_reference *resolved = NULL;
7118 char *fileindex_path = NULL;
7119 struct got_commit_object *commit = NULL;
7120 struct got_object_id *tree_id = NULL;
7121 struct revert_file_args rfa;
7123 err = lock_worktree(worktree, LOCK_EX);
7124 if (err)
7125 return err;
7127 err = got_object_open_as_commit(&commit, repo,
7128 worktree->base_commit_id);
7129 if (err)
7130 goto done;
7132 err = got_ref_open(&resolved, repo,
7133 got_ref_get_symref_target(branch), 0);
7134 if (err)
7135 goto done;
7137 err = got_worktree_set_head_ref(worktree, resolved);
7138 if (err)
7139 goto done;
7141 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7142 if (err)
7143 goto done;
7145 err = got_object_id_by_path(&tree_id, repo, commit,
7146 worktree->path_prefix);
7147 if (err)
7148 goto done;
7150 err = delete_histedit_refs(worktree, repo);
7151 if (err)
7152 goto done;
7154 err = get_fileindex_path(&fileindex_path, worktree);
7155 if (err)
7156 goto done;
7158 rfa.worktree = worktree;
7159 rfa.fileindex = fileindex;
7160 rfa.progress_cb = progress_cb;
7161 rfa.progress_arg = progress_arg;
7162 rfa.patch_cb = NULL;
7163 rfa.patch_arg = NULL;
7164 rfa.repo = repo;
7165 rfa.unlink_added_files = 0;
7166 err = worktree_status(worktree, "", fileindex, repo,
7167 revert_file, &rfa, NULL, NULL, 1, 0);
7168 if (err)
7169 goto sync;
7171 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7172 repo, progress_cb, progress_arg, NULL, NULL);
7173 sync:
7174 sync_err = sync_fileindex(fileindex, fileindex_path);
7175 if (sync_err && err == NULL)
7176 err = sync_err;
7177 done:
7178 got_ref_close(resolved);
7179 free(tree_id);
7180 free(fileindex_path);
7182 unlockerr = lock_worktree(worktree, LOCK_SH);
7183 if (unlockerr && err == NULL)
7184 err = unlockerr;
7185 return err;
7188 const struct got_error *
7189 got_worktree_histedit_complete(struct got_worktree *worktree,
7190 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7191 struct got_reference *edited_branch, struct got_repository *repo)
7193 const struct got_error *err, *unlockerr, *sync_err;
7194 struct got_object_id *new_head_commit_id = NULL;
7195 struct got_reference *resolved = NULL;
7196 char *fileindex_path = NULL;
7198 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7199 if (err)
7200 return err;
7202 err = got_ref_open(&resolved, repo,
7203 got_ref_get_symref_target(edited_branch), 0);
7204 if (err)
7205 goto done;
7207 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7208 resolved, new_head_commit_id, repo);
7209 if (err)
7210 goto done;
7212 err = got_ref_change_ref(resolved, new_head_commit_id);
7213 if (err)
7214 goto done;
7216 err = got_ref_write(resolved, repo);
7217 if (err)
7218 goto done;
7220 err = got_worktree_set_head_ref(worktree, resolved);
7221 if (err)
7222 goto done;
7224 err = delete_histedit_refs(worktree, repo);
7225 if (err)
7226 goto done;
7228 err = get_fileindex_path(&fileindex_path, worktree);
7229 if (err)
7230 goto done;
7231 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7232 sync_err = sync_fileindex(fileindex, fileindex_path);
7233 if (sync_err && err == NULL)
7234 err = sync_err;
7235 done:
7236 got_fileindex_free(fileindex);
7237 free(fileindex_path);
7238 free(new_head_commit_id);
7239 unlockerr = lock_worktree(worktree, LOCK_SH);
7240 if (unlockerr && err == NULL)
7241 err = unlockerr;
7242 return err;
7245 const struct got_error *
7246 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7247 struct got_object_id *commit_id, struct got_repository *repo)
7249 const struct got_error *err;
7250 char *commit_ref_name;
7252 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7253 if (err)
7254 return err;
7256 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7257 if (err)
7258 goto done;
7260 err = delete_ref(commit_ref_name, repo);
7261 done:
7262 free(commit_ref_name);
7263 return err;
7266 const struct got_error *
7267 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7268 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7269 struct got_worktree *worktree, const char *refname,
7270 struct got_repository *repo)
7272 const struct got_error *err = NULL;
7273 char *fileindex_path = NULL;
7274 struct check_rebase_ok_arg ok_arg;
7276 *fileindex = NULL;
7277 *branch_ref = NULL;
7278 *base_branch_ref = NULL;
7280 err = lock_worktree(worktree, LOCK_EX);
7281 if (err)
7282 return err;
7284 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7285 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7286 "cannot integrate a branch into itself; "
7287 "update -b or different branch name required");
7288 goto done;
7291 err = open_fileindex(fileindex, &fileindex_path, worktree);
7292 if (err)
7293 goto done;
7295 /* Preconditions are the same as for rebase. */
7296 ok_arg.worktree = worktree;
7297 ok_arg.repo = repo;
7298 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7299 &ok_arg);
7300 if (err)
7301 goto done;
7303 err = got_ref_open(branch_ref, repo, refname, 1);
7304 if (err)
7305 goto done;
7307 err = got_ref_open(base_branch_ref, repo,
7308 got_worktree_get_head_ref_name(worktree), 1);
7309 done:
7310 if (err) {
7311 if (*branch_ref) {
7312 got_ref_close(*branch_ref);
7313 *branch_ref = NULL;
7315 if (*base_branch_ref) {
7316 got_ref_close(*base_branch_ref);
7317 *base_branch_ref = NULL;
7319 if (*fileindex) {
7320 got_fileindex_free(*fileindex);
7321 *fileindex = NULL;
7323 lock_worktree(worktree, LOCK_SH);
7325 return err;
7328 const struct got_error *
7329 got_worktree_integrate_continue(struct got_worktree *worktree,
7330 struct got_fileindex *fileindex, struct got_repository *repo,
7331 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7332 got_worktree_checkout_cb progress_cb, void *progress_arg,
7333 got_cancel_cb cancel_cb, void *cancel_arg)
7335 const struct got_error *err = NULL, *sync_err, *unlockerr;
7336 char *fileindex_path = NULL;
7337 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7338 struct got_commit_object *commit = NULL;
7340 err = get_fileindex_path(&fileindex_path, worktree);
7341 if (err)
7342 goto done;
7344 err = got_ref_resolve(&commit_id, repo, branch_ref);
7345 if (err)
7346 goto done;
7348 err = got_object_open_as_commit(&commit, repo, commit_id);
7349 if (err)
7350 goto done;
7352 err = got_object_id_by_path(&tree_id, repo, commit,
7353 worktree->path_prefix);
7354 if (err)
7355 goto done;
7357 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7358 if (err)
7359 goto done;
7361 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7362 progress_cb, progress_arg, cancel_cb, cancel_arg);
7363 if (err)
7364 goto sync;
7366 err = got_ref_change_ref(base_branch_ref, commit_id);
7367 if (err)
7368 goto sync;
7370 err = got_ref_write(base_branch_ref, repo);
7371 if (err)
7372 goto sync;
7374 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7375 sync:
7376 sync_err = sync_fileindex(fileindex, fileindex_path);
7377 if (sync_err && err == NULL)
7378 err = sync_err;
7380 done:
7381 unlockerr = got_ref_unlock(branch_ref);
7382 if (unlockerr && err == NULL)
7383 err = unlockerr;
7384 got_ref_close(branch_ref);
7386 unlockerr = got_ref_unlock(base_branch_ref);
7387 if (unlockerr && err == NULL)
7388 err = unlockerr;
7389 got_ref_close(base_branch_ref);
7391 got_fileindex_free(fileindex);
7392 free(fileindex_path);
7393 free(tree_id);
7394 if (commit)
7395 got_object_commit_close(commit);
7397 unlockerr = lock_worktree(worktree, LOCK_SH);
7398 if (unlockerr && err == NULL)
7399 err = unlockerr;
7400 return err;
7403 const struct got_error *
7404 got_worktree_integrate_abort(struct got_worktree *worktree,
7405 struct got_fileindex *fileindex, struct got_repository *repo,
7406 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7408 const struct got_error *err = NULL, *unlockerr = NULL;
7410 got_fileindex_free(fileindex);
7412 err = lock_worktree(worktree, LOCK_SH);
7414 unlockerr = got_ref_unlock(branch_ref);
7415 if (unlockerr && err == NULL)
7416 err = unlockerr;
7417 got_ref_close(branch_ref);
7419 unlockerr = got_ref_unlock(base_branch_ref);
7420 if (unlockerr && err == NULL)
7421 err = unlockerr;
7422 got_ref_close(base_branch_ref);
7424 return err;
7427 const struct got_error *
7428 got_worktree_merge_postpone(struct got_worktree *worktree,
7429 struct got_fileindex *fileindex)
7431 const struct got_error *err, *sync_err;
7432 char *fileindex_path = NULL;
7434 err = get_fileindex_path(&fileindex_path, worktree);
7435 if (err)
7436 goto done;
7438 sync_err = sync_fileindex(fileindex, fileindex_path);
7440 err = lock_worktree(worktree, LOCK_SH);
7441 if (sync_err && err == NULL)
7442 err = sync_err;
7443 done:
7444 got_fileindex_free(fileindex);
7445 free(fileindex_path);
7446 return err;
7449 static const struct got_error *
7450 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7452 const struct got_error *err;
7453 char *branch_refname = NULL, *commit_refname = NULL;
7455 err = get_merge_branch_ref_name(&branch_refname, worktree);
7456 if (err)
7457 goto done;
7458 err = delete_ref(branch_refname, repo);
7459 if (err)
7460 goto done;
7462 err = get_merge_commit_ref_name(&commit_refname, worktree);
7463 if (err)
7464 goto done;
7465 err = delete_ref(commit_refname, repo);
7466 if (err)
7467 goto done;
7469 done:
7470 free(branch_refname);
7471 free(commit_refname);
7472 return err;
7475 struct merge_commit_msg_arg {
7476 struct got_worktree *worktree;
7477 const char *branch_name;
7480 static const struct got_error *
7481 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths, char **logmsg,
7482 void *arg)
7484 struct merge_commit_msg_arg *a = arg;
7486 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7487 got_worktree_get_head_ref_name(a->worktree)) == -1)
7488 return got_error_from_errno("asprintf");
7490 return NULL;
7494 const struct got_error *
7495 got_worktree_merge_branch(struct got_worktree *worktree,
7496 struct got_fileindex *fileindex,
7497 struct got_object_id *yca_commit_id,
7498 struct got_object_id *branch_tip,
7499 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7500 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7502 const struct got_error *err;
7503 char *fileindex_path = NULL;
7505 err = get_fileindex_path(&fileindex_path, worktree);
7506 if (err)
7507 goto done;
7509 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7510 worktree);
7511 if (err)
7512 goto done;
7514 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7515 branch_tip, repo, progress_cb, progress_arg,
7516 cancel_cb, cancel_arg);
7517 done:
7518 free(fileindex_path);
7519 return err;
7522 const struct got_error *
7523 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7524 struct got_worktree *worktree, struct got_fileindex *fileindex,
7525 const char *author, const char *committer, int allow_bad_symlinks,
7526 struct got_object_id *branch_tip, const char *branch_name,
7527 struct got_repository *repo,
7528 got_worktree_status_cb status_cb, void *status_arg)
7531 const struct got_error *err = NULL, *sync_err;
7532 struct got_pathlist_head commitable_paths;
7533 struct collect_commitables_arg cc_arg;
7534 struct got_pathlist_entry *pe;
7535 struct got_reference *head_ref = NULL;
7536 struct got_object_id *head_commit_id = NULL;
7537 int have_staged_files = 0;
7538 struct merge_commit_msg_arg mcm_arg;
7539 char *fileindex_path = NULL;
7541 *new_commit_id = NULL;
7543 TAILQ_INIT(&commitable_paths);
7545 err = get_fileindex_path(&fileindex_path, worktree);
7546 if (err)
7547 goto done;
7549 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7550 if (err)
7551 goto done;
7553 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7554 if (err)
7555 goto done;
7557 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7558 &have_staged_files);
7559 if (err && err->code != GOT_ERR_CANCELLED)
7560 goto done;
7561 if (have_staged_files) {
7562 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7563 goto done;
7566 cc_arg.commitable_paths = &commitable_paths;
7567 cc_arg.worktree = worktree;
7568 cc_arg.fileindex = fileindex;
7569 cc_arg.repo = repo;
7570 cc_arg.have_staged_files = have_staged_files;
7571 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7572 err = worktree_status(worktree, "", fileindex, repo,
7573 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7574 if (err)
7575 goto done;
7577 if (TAILQ_EMPTY(&commitable_paths)) {
7578 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7579 "merge of %s cannot proceed", branch_name);
7580 goto done;
7583 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7584 struct got_commitable *ct = pe->data;
7585 const char *ct_path = ct->in_repo_path;
7587 while (ct_path[0] == '/')
7588 ct_path++;
7589 err = check_out_of_date(ct_path, ct->status,
7590 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7591 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7592 if (err)
7593 goto done;
7597 mcm_arg.worktree = worktree;
7598 mcm_arg.branch_name = branch_name;
7599 err = commit_worktree(new_commit_id, &commitable_paths,
7600 head_commit_id, branch_tip, worktree, author, committer,
7601 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7602 if (err)
7603 goto done;
7605 err = update_fileindex_after_commit(worktree, &commitable_paths,
7606 *new_commit_id, fileindex, have_staged_files);
7607 sync_err = sync_fileindex(fileindex, fileindex_path);
7608 if (sync_err && err == NULL)
7609 err = sync_err;
7610 done:
7611 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7612 struct got_commitable *ct = pe->data;
7613 free_commitable(ct);
7615 got_pathlist_free(&commitable_paths);
7616 free(fileindex_path);
7617 return err;
7620 const struct got_error *
7621 got_worktree_merge_complete(struct got_worktree *worktree,
7622 struct got_fileindex *fileindex, struct got_repository *repo)
7624 const struct got_error *err, *unlockerr, *sync_err;
7625 char *fileindex_path = NULL;
7627 err = delete_merge_refs(worktree, repo);
7628 if (err)
7629 goto done;
7631 err = get_fileindex_path(&fileindex_path, worktree);
7632 if (err)
7633 goto done;
7634 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7635 sync_err = sync_fileindex(fileindex, fileindex_path);
7636 if (sync_err && err == NULL)
7637 err = sync_err;
7638 done:
7639 got_fileindex_free(fileindex);
7640 free(fileindex_path);
7641 unlockerr = lock_worktree(worktree, LOCK_SH);
7642 if (unlockerr && err == NULL)
7643 err = unlockerr;
7644 return err;
7647 const struct got_error *
7648 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7649 struct got_repository *repo)
7651 const struct got_error *err;
7652 char *branch_refname = NULL;
7653 struct got_reference *branch_ref = NULL;
7655 *in_progress = 0;
7657 err = get_merge_branch_ref_name(&branch_refname, worktree);
7658 if (err)
7659 return err;
7660 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7661 free(branch_refname);
7662 if (err) {
7663 if (err->code != GOT_ERR_NOT_REF)
7664 return err;
7665 } else
7666 *in_progress = 1;
7668 return NULL;
7671 const struct got_error *got_worktree_merge_prepare(
7672 struct got_fileindex **fileindex, struct got_worktree *worktree,
7673 struct got_reference *branch, struct got_repository *repo)
7675 const struct got_error *err = NULL;
7676 char *fileindex_path = NULL;
7677 char *branch_refname = NULL, *commit_refname = NULL;
7678 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
7679 struct got_reference *commit_ref = NULL;
7680 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
7681 struct check_rebase_ok_arg ok_arg;
7683 *fileindex = NULL;
7685 err = lock_worktree(worktree, LOCK_EX);
7686 if (err)
7687 return err;
7689 err = open_fileindex(fileindex, &fileindex_path, worktree);
7690 if (err)
7691 goto done;
7693 /* Preconditions are the same as for rebase. */
7694 ok_arg.worktree = worktree;
7695 ok_arg.repo = repo;
7696 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7697 &ok_arg);
7698 if (err)
7699 goto done;
7701 err = get_merge_branch_ref_name(&branch_refname, worktree);
7702 if (err)
7703 return err;
7705 err = get_merge_commit_ref_name(&commit_refname, worktree);
7706 if (err)
7707 return err;
7709 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7710 0);
7711 if (err)
7712 goto done;
7714 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
7715 if (err)
7716 goto done;
7718 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
7719 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
7720 goto done;
7723 err = got_ref_resolve(&branch_tip, repo, branch);
7724 if (err)
7725 goto done;
7727 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
7728 if (err)
7729 goto done;
7730 err = got_ref_write(branch_ref, repo);
7731 if (err)
7732 goto done;
7734 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
7735 if (err)
7736 goto done;
7737 err = got_ref_write(commit_ref, repo);
7738 if (err)
7739 goto done;
7741 done:
7742 free(branch_refname);
7743 free(commit_refname);
7744 free(fileindex_path);
7745 if (branch_ref)
7746 got_ref_close(branch_ref);
7747 if (commit_ref)
7748 got_ref_close(commit_ref);
7749 if (wt_branch)
7750 got_ref_close(wt_branch);
7751 free(wt_branch_tip);
7752 if (err) {
7753 if (*fileindex) {
7754 got_fileindex_free(*fileindex);
7755 *fileindex = NULL;
7757 lock_worktree(worktree, LOCK_SH);
7759 return err;
7762 const struct got_error *
7763 got_worktree_merge_continue(char **branch_name,
7764 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
7765 struct got_worktree *worktree, struct got_repository *repo)
7767 const struct got_error *err;
7768 char *commit_refname = NULL, *branch_refname = NULL;
7769 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
7770 char *fileindex_path = NULL;
7771 int have_staged_files = 0;
7773 *branch_name = NULL;
7774 *branch_tip = NULL;
7775 *fileindex = NULL;
7777 err = lock_worktree(worktree, LOCK_EX);
7778 if (err)
7779 return err;
7781 err = open_fileindex(fileindex, &fileindex_path, worktree);
7782 if (err)
7783 goto done;
7785 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7786 &have_staged_files);
7787 if (err && err->code != GOT_ERR_CANCELLED)
7788 goto done;
7789 if (have_staged_files) {
7790 err = got_error(GOT_ERR_STAGED_PATHS);
7791 goto done;
7794 err = get_merge_branch_ref_name(&branch_refname, worktree);
7795 if (err)
7796 goto done;
7798 err = get_merge_commit_ref_name(&commit_refname, worktree);
7799 if (err)
7800 goto done;
7802 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7803 if (err)
7804 goto done;
7806 if (!got_ref_is_symbolic(branch_ref)) {
7807 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7808 "%s is not a symbolic reference",
7809 got_ref_get_name(branch_ref));
7810 goto done;
7812 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
7813 if (*branch_name == NULL) {
7814 err = got_error_from_errno("strdup");
7815 goto done;
7818 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
7819 if (err)
7820 goto done;
7822 err = got_ref_resolve(branch_tip, repo, commit_ref);
7823 if (err)
7824 goto done;
7825 done:
7826 free(commit_refname);
7827 free(branch_refname);
7828 free(fileindex_path);
7829 if (commit_ref)
7830 got_ref_close(commit_ref);
7831 if (branch_ref)
7832 got_ref_close(branch_ref);
7833 if (err) {
7834 if (*branch_name) {
7835 free(*branch_name);
7836 *branch_name = NULL;
7838 free(*branch_tip);
7839 *branch_tip = NULL;
7840 if (*fileindex) {
7841 got_fileindex_free(*fileindex);
7842 *fileindex = NULL;
7844 lock_worktree(worktree, LOCK_SH);
7846 return err;
7849 const struct got_error *
7850 got_worktree_merge_abort(struct got_worktree *worktree,
7851 struct got_fileindex *fileindex, struct got_repository *repo,
7852 got_worktree_checkout_cb progress_cb, void *progress_arg)
7854 const struct got_error *err, *unlockerr, *sync_err;
7855 struct got_object_id *commit_id = NULL;
7856 struct got_commit_object *commit = NULL;
7857 char *fileindex_path = NULL;
7858 struct revert_file_args rfa;
7859 struct got_object_id *tree_id = NULL;
7861 err = got_object_open_as_commit(&commit, repo,
7862 worktree->base_commit_id);
7863 if (err)
7864 goto done;
7866 err = got_object_id_by_path(&tree_id, repo, commit,
7867 worktree->path_prefix);
7868 if (err)
7869 goto done;
7871 err = delete_merge_refs(worktree, repo);
7872 if (err)
7873 goto done;
7875 err = get_fileindex_path(&fileindex_path, worktree);
7876 if (err)
7877 goto done;
7879 rfa.worktree = worktree;
7880 rfa.fileindex = fileindex;
7881 rfa.progress_cb = progress_cb;
7882 rfa.progress_arg = progress_arg;
7883 rfa.patch_cb = NULL;
7884 rfa.patch_arg = NULL;
7885 rfa.repo = repo;
7886 rfa.unlink_added_files = 1;
7887 err = worktree_status(worktree, "", fileindex, repo,
7888 revert_file, &rfa, NULL, NULL, 1, 0);
7889 if (err)
7890 goto sync;
7892 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7893 repo, progress_cb, progress_arg, NULL, NULL);
7894 sync:
7895 sync_err = sync_fileindex(fileindex, fileindex_path);
7896 if (sync_err && err == NULL)
7897 err = sync_err;
7898 done:
7899 free(tree_id);
7900 free(commit_id);
7901 if (commit)
7902 got_object_commit_close(commit);
7903 if (fileindex)
7904 got_fileindex_free(fileindex);
7905 free(fileindex_path);
7907 unlockerr = lock_worktree(worktree, LOCK_SH);
7908 if (unlockerr && err == NULL)
7909 err = unlockerr;
7910 return err;
7913 struct check_stage_ok_arg {
7914 struct got_object_id *head_commit_id;
7915 struct got_worktree *worktree;
7916 struct got_fileindex *fileindex;
7917 struct got_repository *repo;
7918 int have_changes;
7921 const struct got_error *
7922 check_stage_ok(void *arg, unsigned char status,
7923 unsigned char staged_status, const char *relpath,
7924 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7925 struct got_object_id *commit_id, int dirfd, const char *de_name)
7927 struct check_stage_ok_arg *a = arg;
7928 const struct got_error *err = NULL;
7929 struct got_fileindex_entry *ie;
7930 struct got_object_id base_commit_id;
7931 struct got_object_id *base_commit_idp = NULL;
7932 char *in_repo_path = NULL, *p;
7934 if (status == GOT_STATUS_UNVERSIONED ||
7935 status == GOT_STATUS_NO_CHANGE)
7936 return NULL;
7937 if (status == GOT_STATUS_NONEXISTENT)
7938 return got_error_set_errno(ENOENT, relpath);
7940 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7941 if (ie == NULL)
7942 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7944 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7945 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7946 relpath) == -1)
7947 return got_error_from_errno("asprintf");
7949 if (got_fileindex_entry_has_commit(ie)) {
7950 memcpy(base_commit_id.sha1, ie->commit_sha1,
7951 SHA1_DIGEST_LENGTH);
7952 base_commit_idp = &base_commit_id;
7955 if (status == GOT_STATUS_CONFLICT) {
7956 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7957 goto done;
7958 } else if (status != GOT_STATUS_ADD &&
7959 status != GOT_STATUS_MODIFY &&
7960 status != GOT_STATUS_DELETE) {
7961 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7962 goto done;
7965 a->have_changes = 1;
7967 p = in_repo_path;
7968 while (p[0] == '/')
7969 p++;
7970 err = check_out_of_date(p, status, staged_status,
7971 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7972 GOT_ERR_STAGE_OUT_OF_DATE);
7973 done:
7974 free(in_repo_path);
7975 return err;
7978 struct stage_path_arg {
7979 struct got_worktree *worktree;
7980 struct got_fileindex *fileindex;
7981 struct got_repository *repo;
7982 got_worktree_status_cb status_cb;
7983 void *status_arg;
7984 got_worktree_patch_cb patch_cb;
7985 void *patch_arg;
7986 int staged_something;
7987 int allow_bad_symlinks;
7990 static const struct got_error *
7991 stage_path(void *arg, unsigned char status,
7992 unsigned char staged_status, const char *relpath,
7993 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7994 struct got_object_id *commit_id, int dirfd, const char *de_name)
7996 struct stage_path_arg *a = arg;
7997 const struct got_error *err = NULL;
7998 struct got_fileindex_entry *ie;
7999 char *ondisk_path = NULL, *path_content = NULL;
8000 uint32_t stage;
8001 struct got_object_id *new_staged_blob_id = NULL;
8002 struct stat sb;
8004 if (status == GOT_STATUS_UNVERSIONED)
8005 return NULL;
8007 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8008 if (ie == NULL)
8009 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8011 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8012 relpath)== -1)
8013 return got_error_from_errno("asprintf");
8015 switch (status) {
8016 case GOT_STATUS_ADD:
8017 case GOT_STATUS_MODIFY:
8018 /* XXX could sb.st_mode be passed in by our caller? */
8019 if (lstat(ondisk_path, &sb) == -1) {
8020 err = got_error_from_errno2("lstat", ondisk_path);
8021 break;
8023 if (a->patch_cb) {
8024 if (status == GOT_STATUS_ADD) {
8025 int choice = GOT_PATCH_CHOICE_NONE;
8026 err = (*a->patch_cb)(&choice, a->patch_arg,
8027 status, ie->path, NULL, 1, 1);
8028 if (err)
8029 break;
8030 if (choice != GOT_PATCH_CHOICE_YES)
8031 break;
8032 } else {
8033 err = create_patched_content(&path_content, 0,
8034 staged_blob_id ? staged_blob_id : blob_id,
8035 ondisk_path, dirfd, de_name, ie->path,
8036 a->repo, a->patch_cb, a->patch_arg);
8037 if (err || path_content == NULL)
8038 break;
8041 err = got_object_blob_create(&new_staged_blob_id,
8042 path_content ? path_content : ondisk_path, a->repo);
8043 if (err)
8044 break;
8045 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8046 SHA1_DIGEST_LENGTH);
8047 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8048 stage = GOT_FILEIDX_STAGE_ADD;
8049 else
8050 stage = GOT_FILEIDX_STAGE_MODIFY;
8051 got_fileindex_entry_stage_set(ie, stage);
8052 if (S_ISLNK(sb.st_mode)) {
8053 int is_bad_symlink = 0;
8054 if (!a->allow_bad_symlinks) {
8055 char target_path[PATH_MAX];
8056 ssize_t target_len;
8057 target_len = readlink(ondisk_path, target_path,
8058 sizeof(target_path));
8059 if (target_len == -1) {
8060 err = got_error_from_errno2("readlink",
8061 ondisk_path);
8062 break;
8064 err = is_bad_symlink_target(&is_bad_symlink,
8065 target_path, target_len, ondisk_path,
8066 a->worktree->root_path);
8067 if (err)
8068 break;
8069 if (is_bad_symlink) {
8070 err = got_error_path(ondisk_path,
8071 GOT_ERR_BAD_SYMLINK);
8072 break;
8075 if (is_bad_symlink)
8076 got_fileindex_entry_staged_filetype_set(ie,
8077 GOT_FILEIDX_MODE_BAD_SYMLINK);
8078 else
8079 got_fileindex_entry_staged_filetype_set(ie,
8080 GOT_FILEIDX_MODE_SYMLINK);
8081 } else {
8082 got_fileindex_entry_staged_filetype_set(ie,
8083 GOT_FILEIDX_MODE_REGULAR_FILE);
8085 a->staged_something = 1;
8086 if (a->status_cb == NULL)
8087 break;
8088 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8089 get_staged_status(ie), relpath, blob_id,
8090 new_staged_blob_id, NULL, dirfd, de_name);
8091 if (err)
8092 break;
8094 * When staging the reverse of the staged diff,
8095 * implicitly unstage the file.
8097 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8098 sizeof(ie->blob_sha1)) == 0) {
8099 got_fileindex_entry_stage_set(ie,
8100 GOT_FILEIDX_STAGE_NONE);
8102 break;
8103 case GOT_STATUS_DELETE:
8104 if (staged_status == GOT_STATUS_DELETE)
8105 break;
8106 if (a->patch_cb) {
8107 int choice = GOT_PATCH_CHOICE_NONE;
8108 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8109 ie->path, NULL, 1, 1);
8110 if (err)
8111 break;
8112 if (choice == GOT_PATCH_CHOICE_NO)
8113 break;
8114 if (choice != GOT_PATCH_CHOICE_YES) {
8115 err = got_error(GOT_ERR_PATCH_CHOICE);
8116 break;
8119 stage = GOT_FILEIDX_STAGE_DELETE;
8120 got_fileindex_entry_stage_set(ie, stage);
8121 a->staged_something = 1;
8122 if (a->status_cb == NULL)
8123 break;
8124 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8125 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8126 de_name);
8127 break;
8128 case GOT_STATUS_NO_CHANGE:
8129 break;
8130 case GOT_STATUS_CONFLICT:
8131 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8132 break;
8133 case GOT_STATUS_NONEXISTENT:
8134 err = got_error_set_errno(ENOENT, relpath);
8135 break;
8136 default:
8137 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8138 break;
8141 if (path_content && unlink(path_content) == -1 && err == NULL)
8142 err = got_error_from_errno2("unlink", path_content);
8143 free(path_content);
8144 free(ondisk_path);
8145 free(new_staged_blob_id);
8146 return err;
8149 const struct got_error *
8150 got_worktree_stage(struct got_worktree *worktree,
8151 struct got_pathlist_head *paths,
8152 got_worktree_status_cb status_cb, void *status_arg,
8153 got_worktree_patch_cb patch_cb, void *patch_arg,
8154 int allow_bad_symlinks, struct got_repository *repo)
8156 const struct got_error *err = NULL, *sync_err, *unlockerr;
8157 struct got_pathlist_entry *pe;
8158 struct got_fileindex *fileindex = NULL;
8159 char *fileindex_path = NULL;
8160 struct got_reference *head_ref = NULL;
8161 struct got_object_id *head_commit_id = NULL;
8162 struct check_stage_ok_arg oka;
8163 struct stage_path_arg spa;
8165 err = lock_worktree(worktree, LOCK_EX);
8166 if (err)
8167 return err;
8169 err = got_ref_open(&head_ref, repo,
8170 got_worktree_get_head_ref_name(worktree), 0);
8171 if (err)
8172 goto done;
8173 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8174 if (err)
8175 goto done;
8176 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8177 if (err)
8178 goto done;
8180 /* Check pre-conditions before staging anything. */
8181 oka.head_commit_id = head_commit_id;
8182 oka.worktree = worktree;
8183 oka.fileindex = fileindex;
8184 oka.repo = repo;
8185 oka.have_changes = 0;
8186 TAILQ_FOREACH(pe, paths, entry) {
8187 err = worktree_status(worktree, pe->path, fileindex, repo,
8188 check_stage_ok, &oka, NULL, NULL, 1, 0);
8189 if (err)
8190 goto done;
8192 if (!oka.have_changes) {
8193 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8194 goto done;
8197 spa.worktree = worktree;
8198 spa.fileindex = fileindex;
8199 spa.repo = repo;
8200 spa.patch_cb = patch_cb;
8201 spa.patch_arg = patch_arg;
8202 spa.status_cb = status_cb;
8203 spa.status_arg = status_arg;
8204 spa.staged_something = 0;
8205 spa.allow_bad_symlinks = allow_bad_symlinks;
8206 TAILQ_FOREACH(pe, paths, entry) {
8207 err = worktree_status(worktree, pe->path, fileindex, repo,
8208 stage_path, &spa, NULL, NULL, 1, 0);
8209 if (err)
8210 goto done;
8212 if (!spa.staged_something) {
8213 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8214 goto done;
8217 sync_err = sync_fileindex(fileindex, fileindex_path);
8218 if (sync_err && err == NULL)
8219 err = sync_err;
8220 done:
8221 if (head_ref)
8222 got_ref_close(head_ref);
8223 free(head_commit_id);
8224 free(fileindex_path);
8225 if (fileindex)
8226 got_fileindex_free(fileindex);
8227 unlockerr = lock_worktree(worktree, LOCK_SH);
8228 if (unlockerr && err == NULL)
8229 err = unlockerr;
8230 return err;
8233 struct unstage_path_arg {
8234 struct got_worktree *worktree;
8235 struct got_fileindex *fileindex;
8236 struct got_repository *repo;
8237 got_worktree_checkout_cb progress_cb;
8238 void *progress_arg;
8239 got_worktree_patch_cb patch_cb;
8240 void *patch_arg;
8243 static const struct got_error *
8244 create_unstaged_content(char **path_unstaged_content,
8245 char **path_new_staged_content, struct got_object_id *blob_id,
8246 struct got_object_id *staged_blob_id, const char *relpath,
8247 struct got_repository *repo,
8248 got_worktree_patch_cb patch_cb, void *patch_arg)
8250 const struct got_error *err, *free_err;
8251 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8252 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8253 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8254 struct got_diffreg_result *diffreg_result = NULL;
8255 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8256 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8258 *path_unstaged_content = NULL;
8259 *path_new_staged_content = NULL;
8261 err = got_object_id_str(&label1, blob_id);
8262 if (err)
8263 return err;
8264 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
8265 if (err)
8266 goto done;
8268 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
8269 if (err)
8270 goto done;
8272 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8273 if (err)
8274 goto done;
8276 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
8277 if (err)
8278 goto done;
8280 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
8281 if (err)
8282 goto done;
8284 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8285 if (err)
8286 goto done;
8288 err = got_diff_files(&diffreg_result, f1, label1, f2,
8289 path2, 3, 0, 1, NULL);
8290 if (err)
8291 goto done;
8293 err = got_opentemp_named(path_unstaged_content, &outfile,
8294 "got-unstaged-content");
8295 if (err)
8296 goto done;
8297 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8298 "got-new-staged-content");
8299 if (err)
8300 goto done;
8302 if (fseek(f1, 0L, SEEK_SET) == -1) {
8303 err = got_ferror(f1, GOT_ERR_IO);
8304 goto done;
8306 if (fseek(f2, 0L, SEEK_SET) == -1) {
8307 err = got_ferror(f2, GOT_ERR_IO);
8308 goto done;
8310 /* Count the number of actual changes in the diff result. */
8311 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8312 struct diff_chunk_context cc = {};
8313 diff_chunk_context_load_change(&cc, &nchunks_used,
8314 diffreg_result->result, n, 0);
8315 nchanges++;
8317 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8318 int choice;
8319 err = apply_or_reject_change(&choice, &nchunks_used,
8320 diffreg_result->result, n, relpath, f1, f2,
8321 &line_cur1, &line_cur2,
8322 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8323 if (err)
8324 goto done;
8325 if (choice == GOT_PATCH_CHOICE_YES)
8326 have_content = 1;
8327 else
8328 have_rejected_content = 1;
8329 if (choice == GOT_PATCH_CHOICE_QUIT)
8330 break;
8332 if (have_content || have_rejected_content)
8333 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8334 outfile, rejectfile);
8335 done:
8336 free(label1);
8337 if (blob)
8338 got_object_blob_close(blob);
8339 if (staged_blob)
8340 got_object_blob_close(staged_blob);
8341 free_err = got_diffreg_result_free(diffreg_result);
8342 if (free_err && err == NULL)
8343 err = free_err;
8344 if (f1 && fclose(f1) == EOF && err == NULL)
8345 err = got_error_from_errno2("fclose", path1);
8346 if (f2 && fclose(f2) == EOF && err == NULL)
8347 err = got_error_from_errno2("fclose", path2);
8348 if (outfile && fclose(outfile) == EOF && err == NULL)
8349 err = got_error_from_errno2("fclose", *path_unstaged_content);
8350 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8351 err = got_error_from_errno2("fclose", *path_new_staged_content);
8352 if (path1 && unlink(path1) == -1 && err == NULL)
8353 err = got_error_from_errno2("unlink", path1);
8354 if (path2 && unlink(path2) == -1 && err == NULL)
8355 err = got_error_from_errno2("unlink", path2);
8356 if (err || !have_content) {
8357 if (*path_unstaged_content &&
8358 unlink(*path_unstaged_content) == -1 && err == NULL)
8359 err = got_error_from_errno2("unlink",
8360 *path_unstaged_content);
8361 free(*path_unstaged_content);
8362 *path_unstaged_content = NULL;
8364 if (err || !have_content || !have_rejected_content) {
8365 if (*path_new_staged_content &&
8366 unlink(*path_new_staged_content) == -1 && err == NULL)
8367 err = got_error_from_errno2("unlink",
8368 *path_new_staged_content);
8369 free(*path_new_staged_content);
8370 *path_new_staged_content = NULL;
8372 free(path1);
8373 free(path2);
8374 return err;
8377 static const struct got_error *
8378 unstage_hunks(struct got_object_id *staged_blob_id,
8379 struct got_blob_object *blob_base,
8380 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8381 const char *ondisk_path, const char *label_orig,
8382 struct got_worktree *worktree, struct got_repository *repo,
8383 got_worktree_patch_cb patch_cb, void *patch_arg,
8384 got_worktree_checkout_cb progress_cb, void *progress_arg)
8386 const struct got_error *err = NULL;
8387 char *path_unstaged_content = NULL;
8388 char *path_new_staged_content = NULL;
8389 char *parent = NULL, *base_path = NULL;
8390 char *blob_base_path = NULL;
8391 struct got_object_id *new_staged_blob_id = NULL;
8392 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8393 struct stat sb;
8395 err = create_unstaged_content(&path_unstaged_content,
8396 &path_new_staged_content, blob_id, staged_blob_id,
8397 ie->path, repo, patch_cb, patch_arg);
8398 if (err)
8399 return err;
8401 if (path_unstaged_content == NULL)
8402 return NULL;
8404 if (path_new_staged_content) {
8405 err = got_object_blob_create(&new_staged_blob_id,
8406 path_new_staged_content, repo);
8407 if (err)
8408 goto done;
8411 f = fopen(path_unstaged_content, "re");
8412 if (f == NULL) {
8413 err = got_error_from_errno2("fopen",
8414 path_unstaged_content);
8415 goto done;
8417 if (fstat(fileno(f), &sb) == -1) {
8418 err = got_error_from_errno2("fstat", path_unstaged_content);
8419 goto done;
8421 if (got_fileindex_entry_staged_filetype_get(ie) ==
8422 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8423 char link_target[PATH_MAX];
8424 size_t r;
8425 r = fread(link_target, 1, sizeof(link_target), f);
8426 if (r == 0 && ferror(f)) {
8427 err = got_error_from_errno("fread");
8428 goto done;
8430 if (r >= sizeof(link_target)) { /* should not happen */
8431 err = got_error(GOT_ERR_NO_SPACE);
8432 goto done;
8434 link_target[r] = '\0';
8435 err = merge_symlink(worktree, blob_base,
8436 ondisk_path, ie->path, label_orig, link_target,
8437 worktree->base_commit_id, repo, progress_cb,
8438 progress_arg);
8439 } else {
8440 int local_changes_subsumed;
8442 err = got_path_dirname(&parent, ondisk_path);
8443 if (err)
8444 return err;
8446 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8447 parent) == -1) {
8448 err = got_error_from_errno("asprintf");
8449 base_path = NULL;
8450 goto done;
8453 err = got_opentemp_named(&blob_base_path, &f_base, base_path);
8454 if (err)
8455 goto done;
8456 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8457 blob_base);
8458 if (err)
8459 goto done;
8462 * In order the run a 3-way merge with a symlink we copy the symlink's
8463 * target path into a temporary file and use that file with diff3.
8465 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8466 err = dump_symlink_target_path_to_file(&f_deriv2,
8467 ondisk_path);
8468 if (err)
8469 goto done;
8470 } else {
8471 int fd;
8472 fd = open(ondisk_path,
8473 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8474 if (fd == -1) {
8475 err = got_error_from_errno2("open", ondisk_path);
8476 goto done;
8478 f_deriv2 = fdopen(fd, "r");
8479 if (f_deriv2 == NULL) {
8480 err = got_error_from_errno2("fdopen", ondisk_path);
8481 close(fd);
8482 goto done;
8486 err = merge_file(&local_changes_subsumed, worktree,
8487 f_base, f, f_deriv2, ondisk_path, ie->path,
8488 got_fileindex_perms_to_st(ie),
8489 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8490 repo, progress_cb, progress_arg);
8492 if (err)
8493 goto done;
8495 if (new_staged_blob_id) {
8496 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8497 SHA1_DIGEST_LENGTH);
8498 } else {
8499 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8500 got_fileindex_entry_staged_filetype_set(ie, 0);
8502 done:
8503 free(new_staged_blob_id);
8504 if (path_unstaged_content &&
8505 unlink(path_unstaged_content) == -1 && err == NULL)
8506 err = got_error_from_errno2("unlink", path_unstaged_content);
8507 if (path_new_staged_content &&
8508 unlink(path_new_staged_content) == -1 && err == NULL)
8509 err = got_error_from_errno2("unlink", path_new_staged_content);
8510 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8511 err = got_error_from_errno2("unlink", blob_base_path);
8512 if (f_base && fclose(f_base) == EOF && err == NULL)
8513 err = got_error_from_errno2("fclose", path_unstaged_content);
8514 if (f && fclose(f) == EOF && err == NULL)
8515 err = got_error_from_errno2("fclose", path_unstaged_content);
8516 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8517 err = got_error_from_errno2("fclose", ondisk_path);
8518 free(path_unstaged_content);
8519 free(path_new_staged_content);
8520 free(blob_base_path);
8521 free(parent);
8522 free(base_path);
8523 return err;
8526 static const struct got_error *
8527 unstage_path(void *arg, unsigned char status,
8528 unsigned char staged_status, const char *relpath,
8529 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8530 struct got_object_id *commit_id, int dirfd, const char *de_name)
8532 const struct got_error *err = NULL;
8533 struct unstage_path_arg *a = arg;
8534 struct got_fileindex_entry *ie;
8535 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8536 char *ondisk_path = NULL;
8537 char *id_str = NULL, *label_orig = NULL;
8538 int local_changes_subsumed;
8539 struct stat sb;
8541 if (staged_status != GOT_STATUS_ADD &&
8542 staged_status != GOT_STATUS_MODIFY &&
8543 staged_status != GOT_STATUS_DELETE)
8544 return NULL;
8546 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8547 if (ie == NULL)
8548 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8550 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8551 == -1)
8552 return got_error_from_errno("asprintf");
8554 err = got_object_id_str(&id_str,
8555 commit_id ? commit_id : a->worktree->base_commit_id);
8556 if (err)
8557 goto done;
8558 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8559 id_str) == -1) {
8560 err = got_error_from_errno("asprintf");
8561 goto done;
8564 switch (staged_status) {
8565 case GOT_STATUS_MODIFY:
8566 err = got_object_open_as_blob(&blob_base, a->repo,
8567 blob_id, 8192);
8568 if (err)
8569 break;
8570 /* fall through */
8571 case GOT_STATUS_ADD:
8572 if (a->patch_cb) {
8573 if (staged_status == GOT_STATUS_ADD) {
8574 int choice = GOT_PATCH_CHOICE_NONE;
8575 err = (*a->patch_cb)(&choice, a->patch_arg,
8576 staged_status, ie->path, NULL, 1, 1);
8577 if (err)
8578 break;
8579 if (choice != GOT_PATCH_CHOICE_YES)
8580 break;
8581 } else {
8582 err = unstage_hunks(staged_blob_id,
8583 blob_base, blob_id, ie, ondisk_path,
8584 label_orig, a->worktree, a->repo,
8585 a->patch_cb, a->patch_arg,
8586 a->progress_cb, a->progress_arg);
8587 break; /* Done with this file. */
8590 err = got_object_open_as_blob(&blob_staged, a->repo,
8591 staged_blob_id, 8192);
8592 if (err)
8593 break;
8594 switch (got_fileindex_entry_staged_filetype_get(ie)) {
8595 case GOT_FILEIDX_MODE_BAD_SYMLINK:
8596 case GOT_FILEIDX_MODE_REGULAR_FILE:
8597 err = merge_blob(&local_changes_subsumed, a->worktree,
8598 blob_base, ondisk_path, relpath,
8599 got_fileindex_perms_to_st(ie), label_orig,
8600 blob_staged, commit_id ? commit_id :
8601 a->worktree->base_commit_id, a->repo,
8602 a->progress_cb, a->progress_arg);
8603 break;
8604 case GOT_FILEIDX_MODE_SYMLINK:
8605 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8606 char *staged_target;
8607 err = got_object_blob_read_to_str(
8608 &staged_target, blob_staged);
8609 if (err)
8610 goto done;
8611 err = merge_symlink(a->worktree, blob_base,
8612 ondisk_path, relpath, label_orig,
8613 staged_target, commit_id ? commit_id :
8614 a->worktree->base_commit_id,
8615 a->repo, a->progress_cb, a->progress_arg);
8616 free(staged_target);
8617 } else {
8618 err = merge_blob(&local_changes_subsumed,
8619 a->worktree, blob_base, ondisk_path,
8620 relpath, got_fileindex_perms_to_st(ie),
8621 label_orig, blob_staged,
8622 commit_id ? commit_id :
8623 a->worktree->base_commit_id, a->repo,
8624 a->progress_cb, a->progress_arg);
8626 break;
8627 default:
8628 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8629 break;
8631 if (err == NULL) {
8632 got_fileindex_entry_stage_set(ie,
8633 GOT_FILEIDX_STAGE_NONE);
8634 got_fileindex_entry_staged_filetype_set(ie, 0);
8636 break;
8637 case GOT_STATUS_DELETE:
8638 if (a->patch_cb) {
8639 int choice = GOT_PATCH_CHOICE_NONE;
8640 err = (*a->patch_cb)(&choice, a->patch_arg,
8641 staged_status, ie->path, NULL, 1, 1);
8642 if (err)
8643 break;
8644 if (choice == GOT_PATCH_CHOICE_NO)
8645 break;
8646 if (choice != GOT_PATCH_CHOICE_YES) {
8647 err = got_error(GOT_ERR_PATCH_CHOICE);
8648 break;
8651 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8652 got_fileindex_entry_staged_filetype_set(ie, 0);
8653 err = get_file_status(&status, &sb, ie, ondisk_path,
8654 dirfd, de_name, a->repo);
8655 if (err)
8656 break;
8657 err = (*a->progress_cb)(a->progress_arg, status, relpath);
8658 break;
8660 done:
8661 free(ondisk_path);
8662 if (blob_base)
8663 got_object_blob_close(blob_base);
8664 if (blob_staged)
8665 got_object_blob_close(blob_staged);
8666 free(id_str);
8667 free(label_orig);
8668 return err;
8671 const struct got_error *
8672 got_worktree_unstage(struct got_worktree *worktree,
8673 struct got_pathlist_head *paths,
8674 got_worktree_checkout_cb progress_cb, void *progress_arg,
8675 got_worktree_patch_cb patch_cb, void *patch_arg,
8676 struct got_repository *repo)
8678 const struct got_error *err = NULL, *sync_err, *unlockerr;
8679 struct got_pathlist_entry *pe;
8680 struct got_fileindex *fileindex = NULL;
8681 char *fileindex_path = NULL;
8682 struct unstage_path_arg upa;
8684 err = lock_worktree(worktree, LOCK_EX);
8685 if (err)
8686 return err;
8688 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8689 if (err)
8690 goto done;
8692 upa.worktree = worktree;
8693 upa.fileindex = fileindex;
8694 upa.repo = repo;
8695 upa.progress_cb = progress_cb;
8696 upa.progress_arg = progress_arg;
8697 upa.patch_cb = patch_cb;
8698 upa.patch_arg = patch_arg;
8699 TAILQ_FOREACH(pe, paths, entry) {
8700 err = worktree_status(worktree, pe->path, fileindex, repo,
8701 unstage_path, &upa, NULL, NULL, 1, 0);
8702 if (err)
8703 goto done;
8706 sync_err = sync_fileindex(fileindex, fileindex_path);
8707 if (sync_err && err == NULL)
8708 err = sync_err;
8709 done:
8710 free(fileindex_path);
8711 if (fileindex)
8712 got_fileindex_free(fileindex);
8713 unlockerr = lock_worktree(worktree, LOCK_SH);
8714 if (unlockerr && err == NULL)
8715 err = unlockerr;
8716 return err;
8719 struct report_file_info_arg {
8720 struct got_worktree *worktree;
8721 got_worktree_path_info_cb info_cb;
8722 void *info_arg;
8723 struct got_pathlist_head *paths;
8724 got_cancel_cb cancel_cb;
8725 void *cancel_arg;
8728 static const struct got_error *
8729 report_file_info(void *arg, struct got_fileindex_entry *ie)
8731 struct report_file_info_arg *a = arg;
8732 struct got_pathlist_entry *pe;
8733 struct got_object_id blob_id, staged_blob_id, commit_id;
8734 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8735 struct got_object_id *commit_idp = NULL;
8736 int stage;
8738 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8739 return got_error(GOT_ERR_CANCELLED);
8741 TAILQ_FOREACH(pe, a->paths, entry) {
8742 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8743 got_path_is_child(ie->path, pe->path, pe->path_len))
8744 break;
8746 if (pe == NULL) /* not found */
8747 return NULL;
8749 if (got_fileindex_entry_has_blob(ie)) {
8750 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8751 blob_idp = &blob_id;
8753 stage = got_fileindex_entry_stage_get(ie);
8754 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8755 stage == GOT_FILEIDX_STAGE_ADD) {
8756 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8757 SHA1_DIGEST_LENGTH);
8758 staged_blob_idp = &staged_blob_id;
8761 if (got_fileindex_entry_has_commit(ie)) {
8762 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8763 commit_idp = &commit_id;
8766 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8767 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8770 const struct got_error *
8771 got_worktree_path_info(struct got_worktree *worktree,
8772 struct got_pathlist_head *paths,
8773 got_worktree_path_info_cb info_cb, void *info_arg,
8774 got_cancel_cb cancel_cb, void *cancel_arg)
8777 const struct got_error *err = NULL, *unlockerr;
8778 struct got_fileindex *fileindex = NULL;
8779 char *fileindex_path = NULL;
8780 struct report_file_info_arg arg;
8782 err = lock_worktree(worktree, LOCK_SH);
8783 if (err)
8784 return err;
8786 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8787 if (err)
8788 goto done;
8790 arg.worktree = worktree;
8791 arg.info_cb = info_cb;
8792 arg.info_arg = info_arg;
8793 arg.paths = paths;
8794 arg.cancel_cb = cancel_cb;
8795 arg.cancel_arg = cancel_arg;
8796 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8797 &arg);
8798 done:
8799 free(fileindex_path);
8800 if (fileindex)
8801 got_fileindex_free(fileindex);
8802 unlockerr = lock_worktree(worktree, LOCK_UN);
8803 if (unlockerr && err == NULL)
8804 err = unlockerr;
8805 return err;
8808 static const struct got_error *
8809 patch_check_path(const char *p, char **path, unsigned char *status,
8810 unsigned char *staged_status, struct got_fileindex *fileindex,
8811 struct got_worktree *worktree, struct got_repository *repo)
8813 const struct got_error *err;
8814 struct got_fileindex_entry *ie;
8815 struct stat sb;
8816 char *ondisk_path = NULL;
8818 err = got_worktree_resolve_path(path, worktree, p);
8819 if (err)
8820 return err;
8822 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
8823 *path[0] ? "/" : "", *path) == -1)
8824 return got_error_from_errno("asprintf");
8826 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
8827 if (ie) {
8828 *staged_status = get_staged_status(ie);
8829 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
8830 repo);
8831 if (err)
8832 goto done;
8833 } else {
8834 *staged_status = GOT_STATUS_NO_CHANGE;
8835 *status = GOT_STATUS_UNVERSIONED;
8836 if (lstat(ondisk_path, &sb) == -1) {
8837 if (errno != ENOENT) {
8838 err = got_error_from_errno2("lstat",
8839 ondisk_path);
8840 goto done;
8842 *status = GOT_STATUS_NONEXISTENT;
8846 done:
8847 free(ondisk_path);
8848 return err;
8851 static const struct got_error *
8852 patch_can_rm(const char *path, unsigned char status,
8853 unsigned char staged_status)
8855 if (status == GOT_STATUS_NONEXISTENT)
8856 return got_error_set_errno(ENOENT, path);
8857 if (status != GOT_STATUS_NO_CHANGE &&
8858 status != GOT_STATUS_ADD &&
8859 status != GOT_STATUS_MODIFY &&
8860 status != GOT_STATUS_MODE_CHANGE)
8861 return got_error_path(path, GOT_ERR_FILE_STATUS);
8862 if (staged_status == GOT_STATUS_DELETE)
8863 return got_error_path(path, GOT_ERR_FILE_STATUS);
8864 return NULL;
8867 static const struct got_error *
8868 patch_can_add(const char *path, unsigned char status)
8870 if (status != GOT_STATUS_NONEXISTENT)
8871 return got_error_path(path, GOT_ERR_FILE_STATUS);
8872 return NULL;
8875 static const struct got_error *
8876 patch_can_edit(const char *path, unsigned char status,
8877 unsigned char staged_status)
8879 if (status == GOT_STATUS_NONEXISTENT)
8880 return got_error_set_errno(ENOENT, path);
8881 if (status != GOT_STATUS_NO_CHANGE &&
8882 status != GOT_STATUS_ADD &&
8883 status != GOT_STATUS_MODIFY)
8884 return got_error_path(path, GOT_ERR_FILE_STATUS);
8885 if (staged_status == GOT_STATUS_DELETE)
8886 return got_error_path(path, GOT_ERR_FILE_STATUS);
8887 return NULL;
8890 const struct got_error *
8891 got_worktree_patch_prepare(struct got_fileindex **fileindex,
8892 char **fileindex_path, struct got_worktree *worktree)
8894 return open_fileindex(fileindex, fileindex_path, worktree);
8897 const struct got_error *
8898 got_worktree_patch_check_path(const char *old, const char *new,
8899 char **oldpath, char **newpath, struct got_worktree *worktree,
8900 struct got_repository *repo, struct got_fileindex *fileindex)
8902 const struct got_error *err = NULL;
8903 int file_renamed = 0;
8904 unsigned char status_old, staged_status_old;
8905 unsigned char status_new, staged_status_new;
8907 *oldpath = NULL;
8908 *newpath = NULL;
8910 err = patch_check_path(old != NULL ? old : new, oldpath,
8911 &status_old, &staged_status_old, fileindex, worktree, repo);
8912 if (err)
8913 goto done;
8915 err = patch_check_path(new != NULL ? new : old, newpath,
8916 &status_new, &staged_status_new, fileindex, worktree, repo);
8917 if (err)
8918 goto done;
8920 if (old != NULL && new != NULL && strcmp(old, new) != 0)
8921 file_renamed = 1;
8923 if (old != NULL && new == NULL)
8924 err = patch_can_rm(*oldpath, status_old, staged_status_old);
8925 else if (file_renamed) {
8926 err = patch_can_rm(*oldpath, status_old, staged_status_old);
8927 if (err == NULL)
8928 err = patch_can_add(*newpath, status_new);
8929 } else if (old == NULL)
8930 err = patch_can_add(*newpath, status_new);
8931 else
8932 err = patch_can_edit(*newpath, status_new, staged_status_new);
8934 done:
8935 if (err) {
8936 free(*oldpath);
8937 *oldpath = NULL;
8938 free(*newpath);
8939 *newpath = NULL;
8941 return err;
8944 const struct got_error *
8945 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
8946 struct got_worktree *worktree, struct got_fileindex *fileindex,
8947 got_worktree_checkout_cb progress_cb, void *progress_arg)
8949 struct schedule_addition_args saa;
8951 memset(&saa, 0, sizeof(saa));
8952 saa.worktree = worktree;
8953 saa.fileindex = fileindex;
8954 saa.progress_cb = progress_cb;
8955 saa.progress_arg = progress_arg;
8956 saa.repo = repo;
8958 return worktree_status(worktree, path, fileindex, repo,
8959 schedule_addition, &saa, NULL, NULL, 1, 0);
8962 const struct got_error *
8963 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
8964 struct got_worktree *worktree, struct got_fileindex *fileindex,
8965 got_worktree_delete_cb progress_cb, void *progress_arg)
8967 struct schedule_deletion_args sda;
8969 memset(&sda, 0, sizeof(sda));
8970 sda.worktree = worktree;
8971 sda.fileindex = fileindex;
8972 sda.progress_cb = progress_cb;
8973 sda.progress_arg = progress_arg;
8974 sda.repo = repo;
8975 sda.delete_local_mods = 0;
8976 sda.keep_on_disk = 0;
8977 sda.ignore_missing_paths = 0;
8978 sda.status_codes = NULL;
8980 return worktree_status(worktree, path, fileindex, repo,
8981 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
8984 const struct got_error *
8985 got_worktree_patch_complete(struct got_fileindex *fileindex,
8986 const char *fileindex_path)
8988 const struct got_error *err = NULL;
8990 err = sync_fileindex(fileindex, fileindex_path);
8991 got_fileindex_free(fileindex);
8993 return err;