Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/stat.h>
18 #include <sys/queue.h>
20 #include <dirent.h>
21 #include <limits.h>
22 #include <stddef.h>
23 #include <string.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <time.h>
27 #include <fcntl.h>
28 #include <errno.h>
29 #include <unistd.h>
30 #include <zlib.h>
31 #include <fnmatch.h>
32 #include <libgen.h>
34 #include "got_compat.h"
36 #include "got_error.h"
37 #include "got_repository.h"
38 #include "got_reference.h"
39 #include "got_object.h"
40 #include "got_path.h"
41 #include "got_cancel.h"
42 #include "got_worktree.h"
43 #include "got_opentemp.h"
44 #include "got_diff.h"
46 #include "got_lib_worktree.h"
47 #include "got_lib_sha1.h"
48 #include "got_lib_fileindex.h"
49 #include "got_lib_inflate.h"
50 #include "got_lib_delta.h"
51 #include "got_lib_object.h"
52 #include "got_lib_object_parse.h"
53 #include "got_lib_object_create.h"
54 #include "got_lib_object_idset.h"
55 #include "got_lib_diff.h"
56 #include "got_lib_gotconfig.h"
58 #ifndef MIN
59 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
60 #endif
62 #define GOT_MERGE_LABEL_MERGED "merged change"
63 #define GOT_MERGE_LABEL_BASE "3-way merge base"
65 static mode_t apply_umask(mode_t);
67 static const struct got_error *
68 create_meta_file(const char *path_got, const char *name, const char *content)
69 {
70 const struct got_error *err = NULL;
71 char *path;
73 if (asprintf(&path, "%s/%s", path_got, name) == -1)
74 return got_error_from_errno("asprintf");
76 err = got_path_create_file(path, content);
77 free(path);
78 return err;
79 }
81 static const struct got_error *
82 update_meta_file(const char *path_got, const char *name, const char *content)
83 {
84 const struct got_error *err = NULL;
85 FILE *tmpfile = NULL;
86 char *tmppath = NULL;
87 char *path = NULL;
89 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
90 err = got_error_from_errno("asprintf");
91 path = NULL;
92 goto done;
93 }
95 err = got_opentemp_named(&tmppath, &tmpfile, path);
96 if (err)
97 goto done;
99 if (content) {
100 int len = fprintf(tmpfile, "%s\n", content);
101 if (len != strlen(content) + 1) {
102 err = got_error_from_errno2("fprintf", tmppath);
103 goto done;
107 if (rename(tmppath, path) != 0) {
108 err = got_error_from_errno3("rename", tmppath, path);
109 unlink(tmppath);
110 goto done;
113 done:
114 if (fclose(tmpfile) == EOF && err == NULL)
115 err = got_error_from_errno2("fclose", tmppath);
116 free(tmppath);
117 return err;
120 static const struct got_error *
121 write_head_ref(const char *path_got, struct got_reference *head_ref)
123 const struct got_error *err = NULL;
124 char *refstr = NULL;
126 if (got_ref_is_symbolic(head_ref)) {
127 refstr = got_ref_to_str(head_ref);
128 if (refstr == NULL)
129 return got_error_from_errno("got_ref_to_str");
130 } else {
131 refstr = strdup(got_ref_get_name(head_ref));
132 if (refstr == NULL)
133 return got_error_from_errno("strdup");
135 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
136 free(refstr);
137 return err;
140 const struct got_error *
141 got_worktree_init(const char *path, struct got_reference *head_ref,
142 const char *prefix, struct got_repository *repo)
144 const struct got_error *err = NULL;
145 struct got_object_id *commit_id = NULL;
146 uuid_t uuid;
147 uint32_t uuid_status;
148 int obj_type;
149 char *path_got = NULL;
150 char *formatstr = NULL;
151 char *absprefix = NULL;
152 char *basestr = NULL;
153 char *uuidstr = NULL;
155 if (strcmp(path, got_repo_get_path(repo)) == 0) {
156 err = got_error(GOT_ERR_WORKTREE_REPO);
157 goto done;
160 err = got_ref_resolve(&commit_id, repo, head_ref);
161 if (err)
162 return err;
163 err = got_object_get_type(&obj_type, repo, commit_id);
164 if (err)
165 return err;
166 if (obj_type != GOT_OBJ_TYPE_COMMIT)
167 return got_error(GOT_ERR_OBJ_TYPE);
169 if (!got_path_is_absolute(prefix)) {
170 if (asprintf(&absprefix, "/%s", prefix) == -1)
171 return got_error_from_errno("asprintf");
174 /* Create top-level directory (may already exist). */
175 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
176 err = got_error_from_errno2("mkdir", path);
177 goto done;
180 /* Create .got directory (may already exist). */
181 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
182 err = got_error_from_errno("asprintf");
183 goto done;
185 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
186 err = got_error_from_errno2("mkdir", path_got);
187 goto done;
190 /* Create an empty lock file. */
191 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
192 if (err)
193 goto done;
195 /* Create an empty file index. */
196 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
197 if (err)
198 goto done;
200 /* Write the HEAD reference. */
201 err = write_head_ref(path_got, head_ref);
202 if (err)
203 goto done;
205 /* Record our base commit. */
206 err = got_object_id_str(&basestr, commit_id);
207 if (err)
208 goto done;
209 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
210 if (err)
211 goto done;
213 /* Store path to repository. */
214 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
215 got_repo_get_path(repo));
216 if (err)
217 goto done;
219 /* Store in-repository path prefix. */
220 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
221 absprefix ? absprefix : prefix);
222 if (err)
223 goto done;
225 /* Generate UUID. */
226 uuid_create(&uuid, &uuid_status);
227 if (uuid_status != uuid_s_ok) {
228 err = got_error_uuid(uuid_status, "uuid_create");
229 goto done;
231 uuid_to_string(&uuid, &uuidstr, &uuid_status);
232 if (uuid_status != uuid_s_ok) {
233 err = got_error_uuid(uuid_status, "uuid_to_string");
234 goto done;
236 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
237 if (err)
238 goto done;
240 /* Stamp work tree with format file. */
241 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
242 err = got_error_from_errno("asprintf");
243 goto done;
245 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
246 if (err)
247 goto done;
249 done:
250 free(commit_id);
251 free(path_got);
252 free(formatstr);
253 free(absprefix);
254 free(basestr);
255 free(uuidstr);
256 return err;
259 const struct got_error *
260 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
261 const char *path_prefix)
263 char *absprefix = NULL;
265 if (!got_path_is_absolute(path_prefix)) {
266 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
267 return got_error_from_errno("asprintf");
269 *match = (strcmp(absprefix ? absprefix : path_prefix,
270 worktree->path_prefix) == 0);
271 free(absprefix);
272 return NULL;
275 const char *
276 got_worktree_get_head_ref_name(struct got_worktree *worktree)
278 return worktree->head_ref_name;
281 const struct got_error *
282 got_worktree_set_head_ref(struct got_worktree *worktree,
283 struct got_reference *head_ref)
285 const struct got_error *err = NULL;
286 char *path_got = NULL, *head_ref_name = NULL;
288 if (asprintf(&path_got, "%s/%s", worktree->root_path,
289 GOT_WORKTREE_GOT_DIR) == -1) {
290 err = got_error_from_errno("asprintf");
291 path_got = NULL;
292 goto done;
295 head_ref_name = strdup(got_ref_get_name(head_ref));
296 if (head_ref_name == NULL) {
297 err = got_error_from_errno("strdup");
298 goto done;
301 err = write_head_ref(path_got, head_ref);
302 if (err)
303 goto done;
305 free(worktree->head_ref_name);
306 worktree->head_ref_name = head_ref_name;
307 done:
308 free(path_got);
309 if (err)
310 free(head_ref_name);
311 return err;
314 struct got_object_id *
315 got_worktree_get_base_commit_id(struct got_worktree *worktree)
317 return worktree->base_commit_id;
320 const struct got_error *
321 got_worktree_set_base_commit_id(struct got_worktree *worktree,
322 struct got_repository *repo, struct got_object_id *commit_id)
324 const struct got_error *err;
325 struct got_object *obj = NULL;
326 char *id_str = NULL;
327 char *path_got = NULL;
329 if (asprintf(&path_got, "%s/%s", worktree->root_path,
330 GOT_WORKTREE_GOT_DIR) == -1) {
331 err = got_error_from_errno("asprintf");
332 path_got = NULL;
333 goto done;
336 err = got_object_open(&obj, repo, commit_id);
337 if (err)
338 return err;
340 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
341 err = got_error(GOT_ERR_OBJ_TYPE);
342 goto done;
345 /* Record our base commit. */
346 err = got_object_id_str(&id_str, commit_id);
347 if (err)
348 goto done;
349 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
350 if (err)
351 goto done;
353 free(worktree->base_commit_id);
354 worktree->base_commit_id = got_object_id_dup(commit_id);
355 if (worktree->base_commit_id == NULL) {
356 err = got_error_from_errno("got_object_id_dup");
357 goto done;
359 done:
360 if (obj)
361 got_object_close(obj);
362 free(id_str);
363 free(path_got);
364 return err;
367 const struct got_gotconfig *
368 got_worktree_get_gotconfig(struct got_worktree *worktree)
370 return worktree->gotconfig;
373 static const struct got_error *
374 lock_worktree(struct got_worktree *worktree, int operation)
376 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
377 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
378 : got_error_from_errno2("flock",
379 got_worktree_get_root_path(worktree)));
380 return NULL;
383 static const struct got_error *
384 add_dir_on_disk(struct got_worktree *worktree, const char *path)
386 const struct got_error *err = NULL;
387 char *abspath;
389 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
390 return got_error_from_errno("asprintf");
392 err = got_path_mkdir(abspath);
393 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
394 struct stat sb;
395 err = NULL;
396 if (lstat(abspath, &sb) == -1) {
397 err = got_error_from_errno2("lstat", abspath);
398 } else if (!S_ISDIR(sb.st_mode)) {
399 /* TODO directory is obstructed; do something */
400 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
403 free(abspath);
404 return err;
407 static const struct got_error *
408 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
410 const struct got_error *err = NULL;
411 uint8_t fbuf1[8192];
412 uint8_t fbuf2[8192];
413 size_t flen1 = 0, flen2 = 0;
415 *same = 1;
417 for (;;) {
418 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
419 if (flen1 == 0 && ferror(f1)) {
420 err = got_error_from_errno("fread");
421 break;
423 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
424 if (flen2 == 0 && ferror(f2)) {
425 err = got_error_from_errno("fread");
426 break;
428 if (flen1 == 0) {
429 if (flen2 != 0)
430 *same = 0;
431 break;
432 } else if (flen2 == 0) {
433 if (flen1 != 0)
434 *same = 0;
435 break;
436 } else if (flen1 == flen2) {
437 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
438 *same = 0;
439 break;
441 } else {
442 *same = 0;
443 break;
447 return err;
450 static const struct got_error *
451 check_files_equal(int *same, FILE *f1, FILE *f2)
453 struct stat sb;
454 size_t size1, size2;
456 *same = 1;
458 if (fstat(fileno(f1), &sb) != 0)
459 return got_error_from_errno("fstat");
460 size1 = sb.st_size;
462 if (fstat(fileno(f2), &sb) != 0)
463 return got_error_from_errno("fstat");
464 size2 = sb.st_size;
466 if (size1 != size2) {
467 *same = 0;
468 return NULL;
471 if (fseek(f1, 0L, SEEK_SET) == -1)
472 return got_ferror(f1, GOT_ERR_IO);
473 if (fseek(f2, 0L, SEEK_SET) == -1)
474 return got_ferror(f2, GOT_ERR_IO);
476 return check_file_contents_equal(same, f1, f2);
479 static const struct got_error *
480 copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
482 uint8_t fbuf[65536];
483 size_t flen;
484 ssize_t outlen;
486 *outsize = 0;
488 if (fseek(f, 0L, SEEK_SET) == -1)
489 return got_ferror(f, GOT_ERR_IO);
491 for (;;) {
492 flen = fread(fbuf, 1, sizeof(fbuf), f);
493 if (flen == 0) {
494 if (ferror(f))
495 return got_error_from_errno("fread");
496 if (feof(f))
497 break;
499 outlen = write(outfd, fbuf, flen);
500 if (outlen == -1)
501 return got_error_from_errno("write");
502 if (outlen != flen)
503 return got_error(GOT_ERR_IO);
504 *outsize += outlen;
507 return NULL;
510 static const struct got_error *
511 merge_binary_file(int *overlapcnt, int merged_fd,
512 FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
513 const char *label_deriv, const char *label_orig, const char *label_deriv2,
514 const char *ondisk_path)
516 const struct got_error *err = NULL;
517 int same_content, changed_deriv, changed_deriv2;
518 int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
519 off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
520 char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
521 char *base_path_orig = NULL, *base_path_deriv = NULL;
522 char *base_path_deriv2 = NULL;
524 *overlapcnt = 0;
526 err = check_files_equal(&same_content, f_deriv, f_deriv2);
527 if (err)
528 return err;
530 if (same_content)
531 return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
533 err = check_files_equal(&same_content, f_deriv, f_orig);
534 if (err)
535 return err;
536 changed_deriv = !same_content;
537 err = check_files_equal(&same_content, f_deriv2, f_orig);
538 if (err)
539 return err;
540 changed_deriv2 = !same_content;
542 if (changed_deriv && changed_deriv2) {
543 *overlapcnt = 1;
544 if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
545 err = got_error_from_errno("asprintf");
546 goto done;
548 if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
549 err = got_error_from_errno("asprintf");
550 goto done;
552 if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
553 err = got_error_from_errno("asprintf");
554 goto done;
556 err = got_opentemp_named_fd(&path_orig, &fd_orig,
557 base_path_orig);
558 if (err)
559 goto done;
560 err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
561 base_path_deriv);
562 if (err)
563 goto done;
564 err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
565 base_path_deriv2);
566 if (err)
567 goto done;
568 err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
569 if (err)
570 goto done;
571 err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
572 if (err)
573 goto done;
574 err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
575 if (err)
576 goto done;
577 if (dprintf(merged_fd, "Binary files differ and cannot be "
578 "merged automatically:\n") < 0) {
579 err = got_error_from_errno("dprintf");
580 goto done;
582 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
583 GOT_DIFF_CONFLICT_MARKER_BEGIN,
584 label_deriv ? " " : "",
585 label_deriv ? label_deriv : "",
586 path_deriv) < 0) {
587 err = got_error_from_errno("dprintf");
588 goto done;
590 if (size_orig > 0) {
591 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
592 GOT_DIFF_CONFLICT_MARKER_ORIG,
593 label_orig ? " " : "",
594 label_orig ? label_orig : "",
595 path_orig) < 0) {
596 err = got_error_from_errno("dprintf");
597 goto done;
600 if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
601 GOT_DIFF_CONFLICT_MARKER_SEP,
602 path_deriv2,
603 GOT_DIFF_CONFLICT_MARKER_END,
604 label_deriv2 ? " " : "",
605 label_deriv2 ? label_deriv2 : "") < 0) {
606 err = got_error_from_errno("dprintf");
607 goto done;
609 } else if (changed_deriv)
610 err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
611 else if (changed_deriv2)
612 err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
613 done:
614 if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
615 err == NULL)
616 err = got_error_from_errno2("unlink", path_orig);
617 if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
618 err = got_error_from_errno2("close", path_orig);
619 if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
620 err = got_error_from_errno2("close", path_deriv);
621 if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
622 err = got_error_from_errno2("close", path_deriv2);
623 free(path_orig);
624 free(path_deriv);
625 free(path_deriv2);
626 free(base_path_orig);
627 free(base_path_deriv);
628 free(base_path_deriv2);
629 return err;
632 /*
633 * Perform a 3-way merge where the file f_orig acts as the common
634 * ancestor, the file f_deriv acts as the first derived version,
635 * and the file f_deriv2 acts as the second derived version.
636 * The merge result will be written to a new file at ondisk_path; any
637 * existing file at this path will be replaced.
638 */
639 static const struct got_error *
640 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
641 FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
642 const char *path, uint16_t st_mode,
643 const char *label_orig, const char *label_deriv, const char *label_deriv2,
644 enum got_diff_algorithm diff_algo, struct got_repository *repo,
645 got_worktree_checkout_cb progress_cb, void *progress_arg)
647 const struct got_error *err = NULL;
648 int merged_fd = -1;
649 FILE *f_merged = NULL;
650 char *merged_path = NULL, *base_path = NULL;
651 int overlapcnt = 0;
652 char *parent = NULL;
654 *local_changes_subsumed = 0;
656 err = got_path_dirname(&parent, ondisk_path);
657 if (err)
658 return err;
660 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
661 err = got_error_from_errno("asprintf");
662 goto done;
665 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
666 if (err)
667 goto done;
669 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
670 f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
671 if (err) {
672 if (err->code != GOT_ERR_FILE_BINARY)
673 goto done;
674 err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
675 f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
676 ondisk_path);
677 if (err)
678 goto done;
681 err = (*progress_cb)(progress_arg,
682 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
683 if (err)
684 goto done;
686 if (fsync(merged_fd) != 0) {
687 err = got_error_from_errno("fsync");
688 goto done;
691 f_merged = fdopen(merged_fd, "r");
692 if (f_merged == NULL) {
693 err = got_error_from_errno("fdopen");
694 goto done;
696 merged_fd = -1;
698 /* Check if a clean merge has subsumed all local changes. */
699 if (overlapcnt == 0) {
700 err = check_files_equal(local_changes_subsumed, f_deriv,
701 f_merged);
702 if (err)
703 goto done;
706 if (fchmod(fileno(f_merged), apply_umask(st_mode)) != 0) {
707 err = got_error_from_errno2("fchmod", merged_path);
708 goto done;
711 if (rename(merged_path, ondisk_path) != 0) {
712 err = got_error_from_errno3("rename", merged_path,
713 ondisk_path);
714 goto done;
716 done:
717 if (err) {
718 if (merged_path)
719 unlink(merged_path);
721 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
722 err = got_error_from_errno("close");
723 if (f_merged && fclose(f_merged) == EOF && err == NULL)
724 err = got_error_from_errno("fclose");
725 free(merged_path);
726 free(base_path);
727 free(parent);
728 return err;
731 static const struct got_error *
732 update_symlink(const char *ondisk_path, const char *target_path,
733 size_t target_len)
735 /* This is not atomic but matches what 'ln -sf' does. */
736 if (unlink(ondisk_path) == -1)
737 return got_error_from_errno2("unlink", ondisk_path);
738 if (symlink(target_path, ondisk_path) == -1)
739 return got_error_from_errno3("symlink", target_path,
740 ondisk_path);
741 return NULL;
744 /*
745 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
746 * in the work tree with a file that contains conflict markers and the
747 * conflicting target paths of the original version, a "derived version"
748 * of a symlink from an incoming change, and a local version of the symlink.
750 * The original versions's target path can be NULL if it is not available,
751 * such as if both derived versions added a new symlink at the same path.
753 * The incoming derived symlink target is NULL in case the incoming change
754 * has deleted this symlink.
755 */
756 static const struct got_error *
757 install_symlink_conflict(const char *deriv_target,
758 struct got_object_id *deriv_base_commit_id, const char *orig_target,
759 const char *label_orig, const char *local_target, const char *ondisk_path)
761 const struct got_error *err;
762 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
763 FILE *f = NULL;
765 err = got_object_id_str(&id_str, deriv_base_commit_id);
766 if (err)
767 return got_error_from_errno("asprintf");
769 if (asprintf(&label_deriv, "%s: commit %s",
770 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
771 err = got_error_from_errno("asprintf");
772 goto done;
775 err = got_opentemp_named(&path, &f, "got-symlink-conflict");
776 if (err)
777 goto done;
779 if (fchmod(fileno(f), apply_umask(GOT_DEFAULT_FILE_MODE)) == -1) {
780 err = got_error_from_errno2("fchmod", path);
781 goto done;
784 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
785 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
786 deriv_target ? deriv_target : "(symlink was deleted)",
787 orig_target ? label_orig : "",
788 orig_target ? "\n" : "",
789 orig_target ? orig_target : "",
790 orig_target ? "\n" : "",
791 GOT_DIFF_CONFLICT_MARKER_SEP,
792 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
793 err = got_error_from_errno2("fprintf", path);
794 goto done;
797 if (unlink(ondisk_path) == -1) {
798 err = got_error_from_errno2("unlink", ondisk_path);
799 goto done;
801 if (rename(path, ondisk_path) == -1) {
802 err = got_error_from_errno3("rename", path, ondisk_path);
803 goto done;
805 done:
806 if (f != NULL && fclose(f) == EOF && err == NULL)
807 err = got_error_from_errno2("fclose", path);
808 free(path);
809 free(id_str);
810 free(label_deriv);
811 return err;
814 /* forward declaration */
815 static const struct got_error *
816 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
817 const char *, const char *, uint16_t, const char *,
818 struct got_blob_object *, struct got_object_id *,
819 struct got_repository *, got_worktree_checkout_cb, void *);
821 /*
822 * Merge a symlink into the work tree, where blob_orig acts as the common
823 * ancestor, deriv_target is the link target of the first derived version,
824 * and the symlink on disk acts as the second derived version.
825 * Assume that contents of both blobs represent symlinks.
826 */
827 static const struct got_error *
828 merge_symlink(struct got_worktree *worktree,
829 struct got_blob_object *blob_orig, const char *ondisk_path,
830 const char *path, const char *label_orig, const char *deriv_target,
831 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
832 got_worktree_checkout_cb progress_cb, void *progress_arg)
834 const struct got_error *err = NULL;
835 char *ancestor_target = NULL;
836 struct stat sb;
837 ssize_t ondisk_len, deriv_len;
838 char ondisk_target[PATH_MAX];
839 int have_local_change = 0;
840 int have_incoming_change = 0;
842 if (lstat(ondisk_path, &sb) == -1)
843 return got_error_from_errno2("lstat", ondisk_path);
845 ondisk_len = readlink(ondisk_path, ondisk_target,
846 sizeof(ondisk_target));
847 if (ondisk_len == -1) {
848 err = got_error_from_errno2("readlink",
849 ondisk_path);
850 goto done;
852 ondisk_target[ondisk_len] = '\0';
854 if (blob_orig) {
855 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
856 if (err)
857 goto done;
860 if (ancestor_target == NULL ||
861 (ondisk_len != strlen(ancestor_target) ||
862 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
863 have_local_change = 1;
865 deriv_len = strlen(deriv_target);
866 if (ancestor_target == NULL ||
867 (deriv_len != strlen(ancestor_target) ||
868 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
869 have_incoming_change = 1;
871 if (!have_local_change && !have_incoming_change) {
872 if (ancestor_target) {
873 /* Both sides made the same change. */
874 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
875 path);
876 } else if (deriv_len == ondisk_len &&
877 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
878 /* Both sides added the same symlink. */
879 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
880 path);
881 } else {
882 /* Both sides added symlinks which don't match. */
883 err = install_symlink_conflict(deriv_target,
884 deriv_base_commit_id, ancestor_target,
885 label_orig, ondisk_target, ondisk_path);
886 if (err)
887 goto done;
888 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
889 path);
891 } else if (!have_local_change && have_incoming_change) {
892 /* Apply the incoming change. */
893 err = update_symlink(ondisk_path, deriv_target,
894 strlen(deriv_target));
895 if (err)
896 goto done;
897 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
898 } else if (have_local_change && have_incoming_change) {
899 if (deriv_len == ondisk_len &&
900 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
901 /* Both sides made the same change. */
902 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
903 path);
904 } else {
905 err = install_symlink_conflict(deriv_target,
906 deriv_base_commit_id, ancestor_target, label_orig,
907 ondisk_target, ondisk_path);
908 if (err)
909 goto done;
910 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
911 path);
915 done:
916 free(ancestor_target);
917 return err;
920 static const struct got_error *
921 dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
923 const struct got_error *err = NULL;
924 char target_path[PATH_MAX];
925 ssize_t target_len;
926 size_t n;
927 FILE *f;
929 *outfile = NULL;
931 f = got_opentemp();
932 if (f == NULL)
933 return got_error_from_errno("got_opentemp");
934 target_len = readlink(ondisk_path, target_path, sizeof(target_path));
935 if (target_len == -1) {
936 err = got_error_from_errno2("readlink", ondisk_path);
937 goto done;
939 n = fwrite(target_path, 1, target_len, f);
940 if (n != target_len) {
941 err = got_ferror(f, GOT_ERR_IO);
942 goto done;
944 if (fflush(f) == EOF) {
945 err = got_error_from_errno("fflush");
946 goto done;
948 if (fseek(f, 0L, SEEK_SET) == -1) {
949 err = got_ferror(f, GOT_ERR_IO);
950 goto done;
952 done:
953 if (err)
954 fclose(f);
955 else
956 *outfile = f;
957 return err;
960 /*
961 * Perform a 3-way merge where blob_orig acts as the common ancestor,
962 * blob_deriv acts as the first derived version, and the file on disk
963 * acts as the second derived version.
964 */
965 static const struct got_error *
966 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
967 struct got_blob_object *blob_orig, const char *ondisk_path,
968 const char *path, uint16_t st_mode, const char *label_orig,
969 struct got_blob_object *blob_deriv,
970 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
971 got_worktree_checkout_cb progress_cb, void *progress_arg)
973 const struct got_error *err = NULL;
974 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
975 char *blob_orig_path = NULL;
976 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
977 char *label_deriv = NULL, *parent = NULL;
979 *local_changes_subsumed = 0;
981 err = got_path_dirname(&parent, ondisk_path);
982 if (err)
983 return err;
985 if (blob_orig) {
986 if (asprintf(&base_path, "%s/got-merge-blob-orig",
987 parent) == -1) {
988 err = got_error_from_errno("asprintf");
989 base_path = NULL;
990 goto done;
993 err = got_opentemp_named(&blob_orig_path, &f_orig, 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;
1130 static mode_t
1131 apply_umask(mode_t mode)
1133 mode_t um;
1135 um = umask(000);
1136 umask(um);
1137 return mode & ~um;
1140 /* forward declaration */
1141 static const struct got_error *
1142 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1143 const char *path, mode_t te_mode, mode_t st_mode,
1144 struct got_blob_object *blob, int restoring_missing_file,
1145 int reverting_versioned_file, int installing_bad_symlink,
1146 int path_is_unversioned, struct got_repository *repo,
1147 got_worktree_checkout_cb progress_cb, void *progress_arg);
1150 * This function assumes that the provided symlink target points at a
1151 * safe location in the work tree!
1153 static const struct got_error *
1154 replace_existing_symlink(int *did_something, const char *ondisk_path,
1155 const char *target_path, size_t target_len)
1157 const struct got_error *err = NULL;
1158 ssize_t elen;
1159 char etarget[PATH_MAX];
1160 int fd;
1162 *did_something = 0;
1165 * "Bad" symlinks (those pointing outside the work tree or into the
1166 * .got directory) are installed in the work tree as a regular file
1167 * which contains the bad symlink target path.
1168 * The new symlink target has already been checked for safety by our
1169 * caller. If we can successfully open a regular file then we simply
1170 * replace this file with a symlink below.
1172 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1173 if (fd == -1) {
1174 if (!got_err_open_nofollow_on_symlink())
1175 return got_error_from_errno2("open", ondisk_path);
1177 /* We are updating an existing on-disk symlink. */
1178 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1179 if (elen == -1)
1180 return got_error_from_errno2("readlink", ondisk_path);
1182 if (elen == target_len &&
1183 memcmp(etarget, target_path, target_len) == 0)
1184 return NULL; /* nothing to do */
1187 *did_something = 1;
1188 err = update_symlink(ondisk_path, target_path, target_len);
1189 if (fd != -1 && close(fd) == -1 && err == NULL)
1190 err = got_error_from_errno2("close", ondisk_path);
1191 return err;
1194 static const struct got_error *
1195 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1196 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1198 const struct got_error *err = NULL;
1199 char canonpath[PATH_MAX];
1200 char *path_got = NULL;
1202 *is_bad_symlink = 0;
1204 if (target_len >= sizeof(canonpath)) {
1205 *is_bad_symlink = 1;
1206 return NULL;
1210 * We do not use realpath(3) to resolve the symlink's target
1211 * path because we don't want to resolve symlinks recursively.
1212 * Instead we make the path absolute and then canonicalize it.
1213 * Relative symlink target lookup should begin at the directory
1214 * in which the blob object is being installed.
1216 if (!got_path_is_absolute(target_path)) {
1217 char *abspath, *parent;
1218 err = got_path_dirname(&parent, ondisk_path);
1219 if (err)
1220 return err;
1221 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1222 free(parent);
1223 return got_error_from_errno("asprintf");
1225 free(parent);
1226 if (strlen(abspath) >= sizeof(canonpath)) {
1227 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1228 free(abspath);
1229 return err;
1231 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1232 free(abspath);
1233 if (err)
1234 return err;
1235 } else {
1236 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1237 if (err)
1238 return err;
1241 /* Only allow symlinks pointing at paths within the work tree. */
1242 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1243 *is_bad_symlink = 1;
1244 return NULL;
1247 /* Do not allow symlinks pointing into the .got directory. */
1248 if (asprintf(&path_got, "%s/%s", wtroot_path,
1249 GOT_WORKTREE_GOT_DIR) == -1)
1250 return got_error_from_errno("asprintf");
1251 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1252 *is_bad_symlink = 1;
1254 free(path_got);
1255 return NULL;
1258 static const struct got_error *
1259 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1260 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1261 int restoring_missing_file, int reverting_versioned_file,
1262 int path_is_unversioned, int allow_bad_symlinks,
1263 struct got_repository *repo,
1264 got_worktree_checkout_cb progress_cb, void *progress_arg)
1266 const struct got_error *err = NULL;
1267 char target_path[PATH_MAX];
1268 size_t len, target_len = 0;
1269 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1270 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1272 *is_bad_symlink = 0;
1275 * Blob object content specifies the target path of the link.
1276 * If a symbolic link cannot be installed we instead create
1277 * a regular file which contains the link target path stored
1278 * in the blob object.
1280 do {
1281 err = got_object_blob_read_block(&len, blob);
1282 if (err)
1283 return err;
1285 if (len + target_len >= sizeof(target_path)) {
1286 /* Path too long; install as a regular file. */
1287 *is_bad_symlink = 1;
1288 got_object_blob_rewind(blob);
1289 return install_blob(worktree, ondisk_path, path,
1290 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1291 restoring_missing_file, reverting_versioned_file,
1292 1, path_is_unversioned, repo, progress_cb,
1293 progress_arg);
1295 if (len > 0) {
1296 /* Skip blob object header first time around. */
1297 memcpy(target_path + target_len, buf + hdrlen,
1298 len - hdrlen);
1299 target_len += len - hdrlen;
1300 hdrlen = 0;
1302 } while (len != 0);
1303 target_path[target_len] = '\0';
1305 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1306 ondisk_path, worktree->root_path);
1307 if (err)
1308 return err;
1310 if (*is_bad_symlink && !allow_bad_symlinks) {
1311 /* install as a regular file */
1312 got_object_blob_rewind(blob);
1313 err = install_blob(worktree, ondisk_path, path,
1314 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1315 restoring_missing_file, reverting_versioned_file, 1,
1316 path_is_unversioned, repo, progress_cb, progress_arg);
1317 return err;
1320 if (symlink(target_path, ondisk_path) == -1) {
1321 if (errno == EEXIST) {
1322 int symlink_replaced;
1323 if (path_is_unversioned) {
1324 err = (*progress_cb)(progress_arg,
1325 GOT_STATUS_UNVERSIONED, path);
1326 return err;
1328 err = replace_existing_symlink(&symlink_replaced,
1329 ondisk_path, target_path, target_len);
1330 if (err)
1331 return err;
1332 if (progress_cb) {
1333 if (symlink_replaced) {
1334 err = (*progress_cb)(progress_arg,
1335 reverting_versioned_file ?
1336 GOT_STATUS_REVERT :
1337 GOT_STATUS_UPDATE, path);
1338 } else {
1339 err = (*progress_cb)(progress_arg,
1340 GOT_STATUS_EXISTS, path);
1343 return err; /* Nothing else to do. */
1346 if (errno == ENOENT) {
1347 char *parent;
1348 err = got_path_dirname(&parent, ondisk_path);
1349 if (err)
1350 return err;
1351 err = add_dir_on_disk(worktree, parent);
1352 free(parent);
1353 if (err)
1354 return err;
1356 * Retry, and fall through to error handling
1357 * below if this second attempt fails.
1359 if (symlink(target_path, ondisk_path) != -1) {
1360 err = NULL; /* success */
1361 return err;
1365 /* Handle errors from first or second creation attempt. */
1366 if (errno == ENAMETOOLONG) {
1367 /* bad target path; install as a regular file */
1368 *is_bad_symlink = 1;
1369 got_object_blob_rewind(blob);
1370 err = install_blob(worktree, ondisk_path, path,
1371 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1372 restoring_missing_file, reverting_versioned_file, 1,
1373 path_is_unversioned, repo,
1374 progress_cb, progress_arg);
1375 } else if (errno == ENOTDIR) {
1376 err = got_error_path(ondisk_path,
1377 GOT_ERR_FILE_OBSTRUCTED);
1378 } else {
1379 err = got_error_from_errno3("symlink",
1380 target_path, ondisk_path);
1382 } else if (progress_cb)
1383 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1384 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1385 return err;
1388 static const struct got_error *
1389 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1390 const char *path, mode_t te_mode, mode_t st_mode,
1391 struct got_blob_object *blob, int restoring_missing_file,
1392 int reverting_versioned_file, int installing_bad_symlink,
1393 int path_is_unversioned, struct got_repository *repo,
1394 got_worktree_checkout_cb progress_cb, void *progress_arg)
1396 const struct got_error *err = NULL;
1397 int fd = -1;
1398 size_t len, hdrlen;
1399 int update = 0;
1400 char *tmppath = NULL;
1401 mode_t mode;
1403 mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1404 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1405 O_CLOEXEC, mode);
1406 if (fd == -1) {
1407 if (errno == ENOENT) {
1408 char *parent;
1409 err = got_path_dirname(&parent, path);
1410 if (err)
1411 return err;
1412 err = add_dir_on_disk(worktree, parent);
1413 free(parent);
1414 if (err)
1415 return err;
1416 fd = open(ondisk_path,
1417 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1418 mode);
1419 if (fd == -1)
1420 return got_error_from_errno2("open",
1421 ondisk_path);
1422 } else if (errno == EEXIST) {
1423 if (path_is_unversioned) {
1424 err = (*progress_cb)(progress_arg,
1425 GOT_STATUS_UNVERSIONED, path);
1426 goto done;
1428 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1429 !S_ISREG(st_mode) && !installing_bad_symlink) {
1430 /* TODO file is obstructed; do something */
1431 err = got_error_path(ondisk_path,
1432 GOT_ERR_FILE_OBSTRUCTED);
1433 goto done;
1434 } else {
1435 err = got_opentemp_named_fd(&tmppath, &fd,
1436 ondisk_path);
1437 if (err)
1438 goto done;
1439 update = 1;
1441 if (fchmod(fd, apply_umask(mode)) == -1) {
1442 err = got_error_from_errno2("fchmod",
1443 tmppath);
1444 goto done;
1447 } else
1448 return got_error_from_errno2("open", ondisk_path);
1451 if (progress_cb) {
1452 if (restoring_missing_file)
1453 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1454 path);
1455 else if (reverting_versioned_file)
1456 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1457 path);
1458 else
1459 err = (*progress_cb)(progress_arg,
1460 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1461 if (err)
1462 goto done;
1465 hdrlen = got_object_blob_get_hdrlen(blob);
1466 do {
1467 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1468 err = got_object_blob_read_block(&len, blob);
1469 if (err)
1470 break;
1471 if (len > 0) {
1472 /* Skip blob object header first time around. */
1473 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1474 if (outlen == -1) {
1475 err = got_error_from_errno("write");
1476 goto done;
1477 } else if (outlen != len - hdrlen) {
1478 err = got_error(GOT_ERR_IO);
1479 goto done;
1481 hdrlen = 0;
1483 } while (len != 0);
1485 if (fsync(fd) != 0) {
1486 err = got_error_from_errno("fsync");
1487 goto done;
1490 if (update) {
1491 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1492 err = got_error_from_errno2("unlink", ondisk_path);
1493 goto done;
1495 if (rename(tmppath, ondisk_path) != 0) {
1496 err = got_error_from_errno3("rename", tmppath,
1497 ondisk_path);
1498 goto done;
1500 free(tmppath);
1501 tmppath = NULL;
1504 done:
1505 if (fd != -1 && close(fd) == -1 && err == NULL)
1506 err = got_error_from_errno("close");
1507 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1508 err = got_error_from_errno2("unlink", tmppath);
1509 free(tmppath);
1510 return err;
1513 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1514 static const struct got_error *
1515 get_modified_file_content_status(unsigned char *status, FILE *f)
1517 const struct got_error *err = NULL;
1518 const char *markers[3] = {
1519 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1520 GOT_DIFF_CONFLICT_MARKER_SEP,
1521 GOT_DIFF_CONFLICT_MARKER_END
1523 int i = 0;
1524 char *line = NULL;
1525 size_t linesize = 0;
1526 ssize_t linelen;
1528 while (*status == GOT_STATUS_MODIFY) {
1529 linelen = getline(&line, &linesize, f);
1530 if (linelen == -1) {
1531 if (feof(f))
1532 break;
1533 err = got_ferror(f, GOT_ERR_IO);
1534 break;
1537 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1538 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1539 == 0)
1540 *status = GOT_STATUS_CONFLICT;
1541 else
1542 i++;
1545 free(line);
1547 return err;
1550 static int
1551 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1553 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1554 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1557 static int
1558 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1560 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1561 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1562 ie->mtime_sec == sb->st_mtim.tv_sec &&
1563 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1564 ie->size == (sb->st_size & 0xffffffff) &&
1565 !xbit_differs(ie, sb->st_mode));
1568 static unsigned char
1569 get_staged_status(struct got_fileindex_entry *ie)
1571 switch (got_fileindex_entry_stage_get(ie)) {
1572 case GOT_FILEIDX_STAGE_ADD:
1573 return GOT_STATUS_ADD;
1574 case GOT_FILEIDX_STAGE_DELETE:
1575 return GOT_STATUS_DELETE;
1576 case GOT_FILEIDX_STAGE_MODIFY:
1577 return GOT_STATUS_MODIFY;
1578 default:
1579 return GOT_STATUS_NO_CHANGE;
1583 static const struct got_error *
1584 get_symlink_modification_status(unsigned char *status,
1585 struct got_fileindex_entry *ie, const char *abspath,
1586 int dirfd, const char *de_name, struct got_blob_object *blob)
1588 const struct got_error *err = NULL;
1589 char target_path[PATH_MAX];
1590 char etarget[PATH_MAX];
1591 ssize_t elen;
1592 size_t len, target_len = 0;
1593 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1594 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1596 *status = GOT_STATUS_NO_CHANGE;
1598 /* Blob object content specifies the target path of the link. */
1599 do {
1600 err = got_object_blob_read_block(&len, blob);
1601 if (err)
1602 return err;
1603 if (len + target_len >= sizeof(target_path)) {
1605 * Should not happen. The blob contents were OK
1606 * when this symlink was installed.
1608 return got_error(GOT_ERR_NO_SPACE);
1610 if (len > 0) {
1611 /* Skip blob object header first time around. */
1612 memcpy(target_path + target_len, buf + hdrlen,
1613 len - hdrlen);
1614 target_len += len - hdrlen;
1615 hdrlen = 0;
1617 } while (len != 0);
1618 target_path[target_len] = '\0';
1620 if (dirfd != -1) {
1621 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1622 if (elen == -1)
1623 return got_error_from_errno2("readlinkat", abspath);
1624 } else {
1625 elen = readlink(abspath, etarget, sizeof(etarget));
1626 if (elen == -1)
1627 return got_error_from_errno2("readlink", abspath);
1630 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1631 *status = GOT_STATUS_MODIFY;
1633 return NULL;
1636 static const struct got_error *
1637 get_file_status(unsigned char *status, struct stat *sb,
1638 struct got_fileindex_entry *ie, const char *abspath,
1639 int dirfd, const char *de_name, struct got_repository *repo)
1641 const struct got_error *err = NULL;
1642 struct got_object_id id;
1643 size_t hdrlen;
1644 int fd = -1, fd1 = -1;
1645 FILE *f = NULL;
1646 uint8_t fbuf[8192];
1647 struct got_blob_object *blob = NULL;
1648 size_t flen, blen;
1649 unsigned char staged_status = get_staged_status(ie);
1651 *status = GOT_STATUS_NO_CHANGE;
1652 memset(sb, 0, sizeof(*sb));
1655 * Whenever the caller provides a directory descriptor and a
1656 * directory entry name for the file, use them! This prevents
1657 * race conditions if filesystem paths change beneath our feet.
1659 if (dirfd != -1) {
1660 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1661 if (errno == ENOENT) {
1662 if (got_fileindex_entry_has_file_on_disk(ie))
1663 *status = GOT_STATUS_MISSING;
1664 else
1665 *status = GOT_STATUS_DELETE;
1666 goto done;
1668 err = got_error_from_errno2("fstatat", abspath);
1669 goto done;
1671 } else {
1672 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1673 if (fd == -1 && errno != ENOENT &&
1674 !got_err_open_nofollow_on_symlink())
1675 return got_error_from_errno2("open", abspath);
1676 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1677 if (lstat(abspath, sb) == -1)
1678 return got_error_from_errno2("lstat", abspath);
1679 } else if (fd == -1 || fstat(fd, sb) == -1) {
1680 if (errno == ENOENT) {
1681 if (got_fileindex_entry_has_file_on_disk(ie))
1682 *status = GOT_STATUS_MISSING;
1683 else
1684 *status = GOT_STATUS_DELETE;
1685 goto done;
1687 err = got_error_from_errno2("fstat", abspath);
1688 goto done;
1692 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1693 *status = GOT_STATUS_OBSTRUCTED;
1694 goto done;
1697 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1698 *status = GOT_STATUS_DELETE;
1699 goto done;
1700 } else if (!got_fileindex_entry_has_blob(ie) &&
1701 staged_status != GOT_STATUS_ADD) {
1702 *status = GOT_STATUS_ADD;
1703 goto done;
1706 if (!stat_info_differs(ie, sb))
1707 goto done;
1709 if (S_ISLNK(sb->st_mode) &&
1710 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1711 *status = GOT_STATUS_MODIFY;
1712 goto done;
1715 if (staged_status == GOT_STATUS_MODIFY ||
1716 staged_status == GOT_STATUS_ADD)
1717 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1718 else
1719 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1721 fd1 = got_opentempfd();
1722 if (fd1 == -1) {
1723 err = got_error_from_errno("got_opentempfd");
1724 goto done;
1726 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1727 if (err)
1728 goto done;
1730 if (S_ISLNK(sb->st_mode)) {
1731 err = get_symlink_modification_status(status, ie,
1732 abspath, dirfd, de_name, blob);
1733 goto done;
1736 if (dirfd != -1) {
1737 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1738 if (fd == -1) {
1739 err = got_error_from_errno2("openat", abspath);
1740 goto done;
1744 f = fdopen(fd, "r");
1745 if (f == NULL) {
1746 err = got_error_from_errno2("fdopen", abspath);
1747 goto done;
1749 fd = -1;
1750 hdrlen = got_object_blob_get_hdrlen(blob);
1751 for (;;) {
1752 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1753 err = got_object_blob_read_block(&blen, blob);
1754 if (err)
1755 goto done;
1756 /* Skip length of blob object header first time around. */
1757 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1758 if (flen == 0 && ferror(f)) {
1759 err = got_error_from_errno("fread");
1760 goto done;
1762 if (blen - hdrlen == 0) {
1763 if (flen != 0)
1764 *status = GOT_STATUS_MODIFY;
1765 break;
1766 } else if (flen == 0) {
1767 if (blen - hdrlen != 0)
1768 *status = GOT_STATUS_MODIFY;
1769 break;
1770 } else if (blen - hdrlen == flen) {
1771 /* Skip blob object header first time around. */
1772 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1773 *status = GOT_STATUS_MODIFY;
1774 break;
1776 } else {
1777 *status = GOT_STATUS_MODIFY;
1778 break;
1780 hdrlen = 0;
1783 if (*status == GOT_STATUS_MODIFY) {
1784 rewind(f);
1785 err = get_modified_file_content_status(status, f);
1786 } else if (xbit_differs(ie, sb->st_mode))
1787 *status = GOT_STATUS_MODE_CHANGE;
1788 done:
1789 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1790 err = got_error_from_errno("close");
1791 if (blob)
1792 got_object_blob_close(blob);
1793 if (f != NULL && fclose(f) == EOF && err == NULL)
1794 err = got_error_from_errno2("fclose", abspath);
1795 if (fd != -1 && close(fd) == -1 && err == NULL)
1796 err = got_error_from_errno2("close", abspath);
1797 return err;
1801 * Update timestamps in the file index if a file is unmodified and
1802 * we had to run a full content comparison to find out.
1804 static const struct got_error *
1805 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1806 struct got_fileindex_entry *ie, struct stat *sb)
1808 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1809 return got_fileindex_entry_update(ie, wt_fd, path,
1810 ie->blob_sha1, ie->commit_sha1, 1);
1812 return NULL;
1815 static const struct got_error *
1816 update_blob(struct got_worktree *worktree,
1817 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1818 struct got_tree_entry *te, const char *path,
1819 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1820 void *progress_arg)
1822 const struct got_error *err = NULL;
1823 struct got_blob_object *blob = NULL;
1824 char *ondisk_path = NULL;
1825 unsigned char status = GOT_STATUS_NO_CHANGE;
1826 struct stat sb;
1827 int fd1 = -1, fd2 = -1;
1829 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1830 return got_error_from_errno("asprintf");
1832 if (ie) {
1833 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1834 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1835 goto done;
1837 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1838 repo);
1839 if (err)
1840 goto done;
1841 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1842 sb.st_mode = got_fileindex_perms_to_st(ie);
1843 } else {
1844 if (stat(ondisk_path, &sb) == -1) {
1845 if (errno != ENOENT) {
1846 err = got_error_from_errno2("stat",
1847 ondisk_path);
1848 goto done;
1850 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1851 status = GOT_STATUS_UNVERSIONED;
1852 } else {
1853 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1854 status = GOT_STATUS_UNVERSIONED;
1855 else
1856 status = GOT_STATUS_OBSTRUCTED;
1860 if (status == GOT_STATUS_OBSTRUCTED) {
1861 if (ie)
1862 got_fileindex_entry_mark_skipped(ie);
1863 err = (*progress_cb)(progress_arg, status, path);
1864 goto done;
1866 if (status == GOT_STATUS_CONFLICT) {
1867 if (ie)
1868 got_fileindex_entry_mark_skipped(ie);
1869 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1870 path);
1871 goto done;
1874 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1875 (S_ISLNK(te->mode) ||
1876 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1878 * This is a regular file or an installed bad symlink.
1879 * If the file index indicates that this file is already
1880 * up-to-date with respect to the repository we can skip
1881 * updating contents of this file.
1883 if (got_fileindex_entry_has_commit(ie) &&
1884 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1885 SHA1_DIGEST_LENGTH) == 0) {
1886 /* Same commit. */
1887 err = sync_timestamps(worktree->root_fd,
1888 path, status, ie, &sb);
1889 if (err)
1890 goto done;
1891 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1892 path);
1893 goto done;
1895 if (got_fileindex_entry_has_blob(ie) &&
1896 memcmp(ie->blob_sha1, te->id.sha1,
1897 SHA1_DIGEST_LENGTH) == 0) {
1898 /* Different commit but the same blob. */
1899 err = sync_timestamps(worktree->root_fd,
1900 path, status, ie, &sb);
1901 if (err)
1902 goto done;
1903 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1904 path);
1905 goto done;
1909 fd1 = got_opentempfd();
1910 if (fd1 == -1) {
1911 err = got_error_from_errno("got_opentempfd");
1912 goto done;
1914 err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
1915 if (err)
1916 goto done;
1918 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1919 int update_timestamps;
1920 struct got_blob_object *blob2 = NULL;
1921 char *label_orig = NULL;
1922 if (got_fileindex_entry_has_blob(ie)) {
1923 fd2 = got_opentempfd();
1924 if (fd2 == -1) {
1925 err = got_error_from_errno("got_opentempfd");
1926 goto done;
1928 struct got_object_id id2;
1929 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1930 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
1931 fd2);
1932 if (err)
1933 goto done;
1935 if (got_fileindex_entry_has_commit(ie)) {
1936 char id_str[SHA1_DIGEST_STRING_LENGTH];
1937 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1938 sizeof(id_str)) == NULL) {
1939 err = got_error_path(id_str,
1940 GOT_ERR_BAD_OBJ_ID_STR);
1941 goto done;
1943 if (asprintf(&label_orig, "%s: commit %s",
1944 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1945 err = got_error_from_errno("asprintf");
1946 goto done;
1949 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1950 char *link_target;
1951 err = got_object_blob_read_to_str(&link_target, blob);
1952 if (err)
1953 goto done;
1954 err = merge_symlink(worktree, blob2, ondisk_path, path,
1955 label_orig, link_target, worktree->base_commit_id,
1956 repo, progress_cb, progress_arg);
1957 free(link_target);
1958 } else {
1959 err = merge_blob(&update_timestamps, worktree, blob2,
1960 ondisk_path, path, sb.st_mode, label_orig, blob,
1961 worktree->base_commit_id, repo,
1962 progress_cb, progress_arg);
1964 free(label_orig);
1965 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
1966 err = got_error_from_errno("close");
1967 goto done;
1969 if (blob2)
1970 got_object_blob_close(blob2);
1971 if (err)
1972 goto done;
1974 * Do not update timestamps of files with local changes.
1975 * Otherwise, a future status walk would treat them as
1976 * unmodified files again.
1978 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1979 blob->id.sha1, worktree->base_commit_id->sha1,
1980 update_timestamps);
1981 } else if (status == GOT_STATUS_MODE_CHANGE) {
1982 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1983 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1984 } else if (status == GOT_STATUS_DELETE) {
1985 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1986 if (err)
1987 goto done;
1988 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1989 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1990 if (err)
1991 goto done;
1992 } else {
1993 int is_bad_symlink = 0;
1994 if (S_ISLNK(te->mode)) {
1995 err = install_symlink(&is_bad_symlink, worktree,
1996 ondisk_path, path, blob,
1997 status == GOT_STATUS_MISSING, 0,
1998 status == GOT_STATUS_UNVERSIONED, 0,
1999 repo, progress_cb, progress_arg);
2000 } else {
2001 err = install_blob(worktree, ondisk_path, path,
2002 te->mode, sb.st_mode, blob,
2003 status == GOT_STATUS_MISSING, 0, 0,
2004 status == GOT_STATUS_UNVERSIONED, repo,
2005 progress_cb, progress_arg);
2007 if (err)
2008 goto done;
2010 if (ie) {
2011 err = got_fileindex_entry_update(ie,
2012 worktree->root_fd, path, blob->id.sha1,
2013 worktree->base_commit_id->sha1, 1);
2014 } else {
2015 err = create_fileindex_entry(&ie, fileindex,
2016 worktree->base_commit_id, worktree->root_fd, path,
2017 &blob->id);
2019 if (err)
2020 goto done;
2022 if (is_bad_symlink) {
2023 got_fileindex_entry_filetype_set(ie,
2024 GOT_FILEIDX_MODE_BAD_SYMLINK);
2028 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2029 err = got_error_from_errno("close");
2030 goto done;
2032 got_object_blob_close(blob);
2033 done:
2034 free(ondisk_path);
2035 return err;
2038 static const struct got_error *
2039 remove_ondisk_file(const char *root_path, const char *path)
2041 const struct got_error *err = NULL;
2042 char *ondisk_path = NULL, *parent = NULL;
2044 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2045 return got_error_from_errno("asprintf");
2047 if (unlink(ondisk_path) == -1) {
2048 if (errno != ENOENT)
2049 err = got_error_from_errno2("unlink", ondisk_path);
2050 } else {
2051 size_t root_len = strlen(root_path);
2052 err = got_path_dirname(&parent, ondisk_path);
2053 if (err)
2054 goto done;
2055 while (got_path_cmp(parent, root_path,
2056 strlen(parent), root_len) != 0) {
2057 free(ondisk_path);
2058 ondisk_path = parent;
2059 parent = NULL;
2060 if (rmdir(ondisk_path) == -1) {
2061 if (errno != ENOTEMPTY)
2062 err = got_error_from_errno2("rmdir",
2063 ondisk_path);
2064 break;
2066 err = got_path_dirname(&parent, ondisk_path);
2067 if (err)
2068 break;
2071 done:
2072 free(ondisk_path);
2073 free(parent);
2074 return err;
2077 static const struct got_error *
2078 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2079 struct got_fileindex_entry *ie, struct got_repository *repo,
2080 got_worktree_checkout_cb progress_cb, void *progress_arg)
2082 const struct got_error *err = NULL;
2083 unsigned char status;
2084 struct stat sb;
2085 char *ondisk_path;
2087 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2088 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2090 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2091 == -1)
2092 return got_error_from_errno("asprintf");
2094 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2095 if (err)
2096 goto done;
2098 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2099 char ondisk_target[PATH_MAX];
2100 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2101 sizeof(ondisk_target));
2102 if (ondisk_len == -1) {
2103 err = got_error_from_errno2("readlink", ondisk_path);
2104 goto done;
2106 ondisk_target[ondisk_len] = '\0';
2107 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2108 NULL, NULL, /* XXX pass common ancestor info? */
2109 ondisk_target, ondisk_path);
2110 if (err)
2111 goto done;
2112 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2113 ie->path);
2114 goto done;
2117 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2118 status == GOT_STATUS_ADD) {
2119 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2120 if (err)
2121 goto done;
2123 * Preserve the working file and change the deleted blob's
2124 * entry into a schedule-add entry.
2126 err = got_fileindex_entry_update(ie, worktree->root_fd,
2127 ie->path, NULL, NULL, 0);
2128 } else {
2129 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2130 if (err)
2131 goto done;
2132 if (status == GOT_STATUS_NO_CHANGE) {
2133 err = remove_ondisk_file(worktree->root_path, ie->path);
2134 if (err)
2135 goto done;
2137 got_fileindex_entry_remove(fileindex, ie);
2139 done:
2140 free(ondisk_path);
2141 return err;
2144 struct diff_cb_arg {
2145 struct got_fileindex *fileindex;
2146 struct got_worktree *worktree;
2147 struct got_repository *repo;
2148 got_worktree_checkout_cb progress_cb;
2149 void *progress_arg;
2150 got_cancel_cb cancel_cb;
2151 void *cancel_arg;
2154 static const struct got_error *
2155 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2156 struct got_tree_entry *te, const char *parent_path)
2158 struct diff_cb_arg *a = arg;
2160 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2161 return got_error(GOT_ERR_CANCELLED);
2163 return update_blob(a->worktree, a->fileindex, ie, te,
2164 ie->path, a->repo, a->progress_cb, a->progress_arg);
2167 static const struct got_error *
2168 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2170 struct diff_cb_arg *a = arg;
2172 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2173 return got_error(GOT_ERR_CANCELLED);
2175 return delete_blob(a->worktree, a->fileindex, ie,
2176 a->repo, a->progress_cb, a->progress_arg);
2179 static const struct got_error *
2180 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2182 struct diff_cb_arg *a = arg;
2183 const struct got_error *err;
2184 char *path;
2186 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2187 return got_error(GOT_ERR_CANCELLED);
2189 if (got_object_tree_entry_is_submodule(te))
2190 return NULL;
2192 if (asprintf(&path, "%s%s%s", parent_path,
2193 parent_path[0] ? "/" : "", te->name)
2194 == -1)
2195 return got_error_from_errno("asprintf");
2197 if (S_ISDIR(te->mode))
2198 err = add_dir_on_disk(a->worktree, path);
2199 else
2200 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2201 a->repo, a->progress_cb, a->progress_arg);
2203 free(path);
2204 return err;
2207 const struct got_error *
2208 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2210 uint32_t uuid_status;
2212 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2213 if (uuid_status != uuid_s_ok) {
2214 *uuidstr = NULL;
2215 return got_error_uuid(uuid_status, "uuid_to_string");
2218 return NULL;
2221 static const struct got_error *
2222 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2224 const struct got_error *err = NULL;
2225 char *uuidstr = NULL;
2227 *refname = NULL;
2229 err = got_worktree_get_uuid(&uuidstr, worktree);
2230 if (err)
2231 return err;
2233 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2234 err = got_error_from_errno("asprintf");
2235 *refname = NULL;
2237 free(uuidstr);
2238 return err;
2241 const struct got_error *
2242 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2244 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2247 static const struct got_error *
2248 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2250 return get_ref_name(refname, worktree,
2251 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2254 static const struct got_error *
2255 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2257 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2260 static const struct got_error *
2261 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2263 return get_ref_name(refname, worktree,
2264 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2267 static const struct got_error *
2268 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2270 return get_ref_name(refname, worktree,
2271 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2274 static const struct got_error *
2275 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2277 return get_ref_name(refname, worktree,
2278 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2281 static const struct got_error *
2282 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2284 return get_ref_name(refname, worktree,
2285 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2288 static const struct got_error *
2289 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2291 return get_ref_name(refname, worktree,
2292 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2295 static const struct got_error *
2296 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2298 return get_ref_name(refname, worktree,
2299 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2302 const struct got_error *
2303 got_worktree_get_histedit_script_path(char **path,
2304 struct got_worktree *worktree)
2306 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2307 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2308 *path = NULL;
2309 return got_error_from_errno("asprintf");
2311 return NULL;
2314 static const struct got_error *
2315 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2317 return get_ref_name(refname, worktree,
2318 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2321 static const struct got_error *
2322 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2324 return get_ref_name(refname, worktree,
2325 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2329 * Prevent Git's garbage collector from deleting our base commit by
2330 * setting a reference to our base commit's ID.
2332 static const struct got_error *
2333 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2335 const struct got_error *err = NULL;
2336 struct got_reference *ref = NULL;
2337 char *refname;
2339 err = got_worktree_get_base_ref_name(&refname, worktree);
2340 if (err)
2341 return err;
2343 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2344 if (err)
2345 goto done;
2347 err = got_ref_write(ref, repo);
2348 done:
2349 free(refname);
2350 if (ref)
2351 got_ref_close(ref);
2352 return err;
2355 static const struct got_error *
2356 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2358 const struct got_error *err = NULL;
2360 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2361 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2362 err = got_error_from_errno("asprintf");
2363 *fileindex_path = NULL;
2365 return err;
2369 static const struct got_error *
2370 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2371 struct got_worktree *worktree)
2373 const struct got_error *err = NULL;
2374 FILE *index = NULL;
2376 *fileindex_path = NULL;
2377 *fileindex = got_fileindex_alloc();
2378 if (*fileindex == NULL)
2379 return got_error_from_errno("got_fileindex_alloc");
2381 err = get_fileindex_path(fileindex_path, worktree);
2382 if (err)
2383 goto done;
2385 index = fopen(*fileindex_path, "rbe");
2386 if (index == NULL) {
2387 if (errno != ENOENT)
2388 err = got_error_from_errno2("fopen", *fileindex_path);
2389 } else {
2390 err = got_fileindex_read(*fileindex, index);
2391 if (fclose(index) == EOF && err == NULL)
2392 err = got_error_from_errno("fclose");
2394 done:
2395 if (err) {
2396 free(*fileindex_path);
2397 *fileindex_path = NULL;
2398 got_fileindex_free(*fileindex);
2399 *fileindex = NULL;
2401 return err;
2404 struct bump_base_commit_id_arg {
2405 struct got_object_id *base_commit_id;
2406 const char *path;
2407 size_t path_len;
2408 const char *entry_name;
2409 got_worktree_checkout_cb progress_cb;
2410 void *progress_arg;
2413 /* Bump base commit ID of all files within an updated part of the work tree. */
2414 static const struct got_error *
2415 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2417 const struct got_error *err;
2418 struct bump_base_commit_id_arg *a = arg;
2420 if (a->entry_name) {
2421 if (strcmp(ie->path, a->path) != 0)
2422 return NULL;
2423 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2424 return NULL;
2426 if (got_fileindex_entry_was_skipped(ie))
2427 return NULL;
2429 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2430 SHA1_DIGEST_LENGTH) == 0)
2431 return NULL;
2433 if (a->progress_cb) {
2434 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2435 ie->path);
2436 if (err)
2437 return err;
2439 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2440 return NULL;
2443 static const struct got_error *
2444 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2445 struct got_fileindex *fileindex,
2446 got_worktree_checkout_cb progress_cb, void *progress_arg)
2448 struct bump_base_commit_id_arg bbc_arg;
2450 bbc_arg.base_commit_id = worktree->base_commit_id;
2451 bbc_arg.entry_name = NULL;
2452 bbc_arg.path = "";
2453 bbc_arg.path_len = 0;
2454 bbc_arg.progress_cb = progress_cb;
2455 bbc_arg.progress_arg = progress_arg;
2457 return got_fileindex_for_each_entry_safe(fileindex,
2458 bump_base_commit_id, &bbc_arg);
2461 static const struct got_error *
2462 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2464 const struct got_error *err = NULL;
2465 char *new_fileindex_path = NULL;
2466 FILE *new_index = NULL;
2467 struct timespec timeout;
2469 err = got_opentemp_named(&new_fileindex_path, &new_index,
2470 fileindex_path);
2471 if (err)
2472 goto done;
2474 err = got_fileindex_write(fileindex, new_index);
2475 if (err)
2476 goto done;
2478 if (rename(new_fileindex_path, fileindex_path) != 0) {
2479 err = got_error_from_errno3("rename", new_fileindex_path,
2480 fileindex_path);
2481 unlink(new_fileindex_path);
2485 * Sleep for a short amount of time to ensure that files modified after
2486 * this program exits have a different time stamp from the one which
2487 * was recorded in the file index.
2489 timeout.tv_sec = 0;
2490 timeout.tv_nsec = 1;
2491 nanosleep(&timeout, NULL);
2492 done:
2493 if (new_index)
2494 fclose(new_index);
2495 free(new_fileindex_path);
2496 return err;
2499 static const struct got_error *
2500 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2501 struct got_object_id **tree_id, const char *wt_relpath,
2502 struct got_commit_object *base_commit, struct got_worktree *worktree,
2503 struct got_repository *repo)
2505 const struct got_error *err = NULL;
2506 struct got_object_id *id = NULL;
2507 char *in_repo_path = NULL;
2508 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2510 *entry_type = GOT_OBJ_TYPE_ANY;
2511 *tree_relpath = NULL;
2512 *tree_id = NULL;
2514 if (wt_relpath[0] == '\0') {
2515 /* Check out all files within the work tree. */
2516 *entry_type = GOT_OBJ_TYPE_TREE;
2517 *tree_relpath = strdup("");
2518 if (*tree_relpath == NULL) {
2519 err = got_error_from_errno("strdup");
2520 goto done;
2522 err = got_object_id_by_path(tree_id, repo, base_commit,
2523 worktree->path_prefix);
2524 if (err)
2525 goto done;
2526 return NULL;
2529 /* Check out a subset of files in the work tree. */
2531 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2532 is_root_wt ? "" : "/", wt_relpath) == -1) {
2533 err = got_error_from_errno("asprintf");
2534 goto done;
2537 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2538 if (err)
2539 goto done;
2541 free(in_repo_path);
2542 in_repo_path = NULL;
2544 err = got_object_get_type(entry_type, repo, id);
2545 if (err)
2546 goto done;
2548 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2549 /* Check out a single file. */
2550 if (strchr(wt_relpath, '/') == NULL) {
2551 /* Check out a single file in work tree's root dir. */
2552 in_repo_path = strdup(worktree->path_prefix);
2553 if (in_repo_path == NULL) {
2554 err = got_error_from_errno("strdup");
2555 goto done;
2557 *tree_relpath = strdup("");
2558 if (*tree_relpath == NULL) {
2559 err = got_error_from_errno("strdup");
2560 goto done;
2562 } else {
2563 /* Check out a single file in a subdirectory. */
2564 err = got_path_dirname(tree_relpath, wt_relpath);
2565 if (err)
2566 return err;
2567 if (asprintf(&in_repo_path, "%s%s%s",
2568 worktree->path_prefix, is_root_wt ? "" : "/",
2569 *tree_relpath) == -1) {
2570 err = got_error_from_errno("asprintf");
2571 goto done;
2574 err = got_object_id_by_path(tree_id, repo,
2575 base_commit, in_repo_path);
2576 } else {
2577 /* Check out all files within a subdirectory. */
2578 *tree_id = got_object_id_dup(id);
2579 if (*tree_id == NULL) {
2580 err = got_error_from_errno("got_object_id_dup");
2581 goto done;
2583 *tree_relpath = strdup(wt_relpath);
2584 if (*tree_relpath == NULL) {
2585 err = got_error_from_errno("strdup");
2586 goto done;
2589 done:
2590 free(id);
2591 free(in_repo_path);
2592 if (err) {
2593 *entry_type = GOT_OBJ_TYPE_ANY;
2594 free(*tree_relpath);
2595 *tree_relpath = NULL;
2596 free(*tree_id);
2597 *tree_id = NULL;
2599 return err;
2602 static const struct got_error *
2603 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2604 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2605 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2606 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2608 const struct got_error *err = NULL;
2609 struct got_commit_object *commit = NULL;
2610 struct got_tree_object *tree = NULL;
2611 struct got_fileindex_diff_tree_cb diff_cb;
2612 struct diff_cb_arg arg;
2614 err = ref_base_commit(worktree, repo);
2615 if (err) {
2616 if (!(err->code == GOT_ERR_ERRNO &&
2617 (errno == EACCES || errno == EROFS)))
2618 goto done;
2619 err = (*progress_cb)(progress_arg,
2620 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2621 if (err)
2622 return err;
2625 err = got_object_open_as_commit(&commit, repo,
2626 worktree->base_commit_id);
2627 if (err)
2628 goto done;
2630 err = got_object_open_as_tree(&tree, repo, tree_id);
2631 if (err)
2632 goto done;
2634 if (entry_name &&
2635 got_object_tree_find_entry(tree, entry_name) == NULL) {
2636 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2637 goto done;
2640 diff_cb.diff_old_new = diff_old_new;
2641 diff_cb.diff_old = diff_old;
2642 diff_cb.diff_new = diff_new;
2643 arg.fileindex = fileindex;
2644 arg.worktree = worktree;
2645 arg.repo = repo;
2646 arg.progress_cb = progress_cb;
2647 arg.progress_arg = progress_arg;
2648 arg.cancel_cb = cancel_cb;
2649 arg.cancel_arg = cancel_arg;
2650 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2651 entry_name, repo, &diff_cb, &arg);
2652 done:
2653 if (tree)
2654 got_object_tree_close(tree);
2655 if (commit)
2656 got_object_commit_close(commit);
2657 return err;
2660 const struct got_error *
2661 got_worktree_checkout_files(struct got_worktree *worktree,
2662 struct got_pathlist_head *paths, struct got_repository *repo,
2663 got_worktree_checkout_cb progress_cb, void *progress_arg,
2664 got_cancel_cb cancel_cb, void *cancel_arg)
2666 const struct got_error *err = NULL, *sync_err, *unlockerr;
2667 struct got_commit_object *commit = NULL;
2668 struct got_tree_object *tree = NULL;
2669 struct got_fileindex *fileindex = NULL;
2670 char *fileindex_path = NULL;
2671 struct got_pathlist_entry *pe;
2672 struct tree_path_data {
2673 STAILQ_ENTRY(tree_path_data) entry;
2674 struct got_object_id *tree_id;
2675 int entry_type;
2676 char *relpath;
2677 char *entry_name;
2678 } *tpd = NULL;
2679 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2681 STAILQ_INIT(&tree_paths);
2683 err = lock_worktree(worktree, LOCK_EX);
2684 if (err)
2685 return err;
2687 err = got_object_open_as_commit(&commit, repo,
2688 worktree->base_commit_id);
2689 if (err)
2690 goto done;
2692 /* Map all specified paths to in-repository trees. */
2693 TAILQ_FOREACH(pe, paths, entry) {
2694 tpd = malloc(sizeof(*tpd));
2695 if (tpd == NULL) {
2696 err = got_error_from_errno("malloc");
2697 goto done;
2700 err = find_tree_entry_for_checkout(&tpd->entry_type,
2701 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2702 worktree, repo);
2703 if (err) {
2704 free(tpd);
2705 goto done;
2708 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2709 err = got_path_basename(&tpd->entry_name, pe->path);
2710 if (err) {
2711 free(tpd->relpath);
2712 free(tpd->tree_id);
2713 free(tpd);
2714 goto done;
2716 } else
2717 tpd->entry_name = NULL;
2719 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2723 * Read the file index.
2724 * Checking out files is supposed to be an idempotent operation.
2725 * If the on-disk file index is incomplete we will try to complete it.
2727 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2728 if (err)
2729 goto done;
2731 tpd = STAILQ_FIRST(&tree_paths);
2732 TAILQ_FOREACH(pe, paths, entry) {
2733 struct bump_base_commit_id_arg bbc_arg;
2735 err = checkout_files(worktree, fileindex, tpd->relpath,
2736 tpd->tree_id, tpd->entry_name, repo,
2737 progress_cb, progress_arg, cancel_cb, cancel_arg);
2738 if (err)
2739 break;
2741 bbc_arg.base_commit_id = worktree->base_commit_id;
2742 bbc_arg.entry_name = tpd->entry_name;
2743 bbc_arg.path = pe->path;
2744 bbc_arg.path_len = pe->path_len;
2745 bbc_arg.progress_cb = progress_cb;
2746 bbc_arg.progress_arg = progress_arg;
2747 err = got_fileindex_for_each_entry_safe(fileindex,
2748 bump_base_commit_id, &bbc_arg);
2749 if (err)
2750 break;
2752 tpd = STAILQ_NEXT(tpd, entry);
2754 sync_err = sync_fileindex(fileindex, fileindex_path);
2755 if (sync_err && err == NULL)
2756 err = sync_err;
2757 done:
2758 free(fileindex_path);
2759 if (tree)
2760 got_object_tree_close(tree);
2761 if (commit)
2762 got_object_commit_close(commit);
2763 if (fileindex)
2764 got_fileindex_free(fileindex);
2765 while (!STAILQ_EMPTY(&tree_paths)) {
2766 tpd = STAILQ_FIRST(&tree_paths);
2767 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2768 free(tpd->relpath);
2769 free(tpd->tree_id);
2770 free(tpd);
2772 unlockerr = lock_worktree(worktree, LOCK_SH);
2773 if (unlockerr && err == NULL)
2774 err = unlockerr;
2775 return err;
2778 struct merge_file_cb_arg {
2779 struct got_worktree *worktree;
2780 struct got_fileindex *fileindex;
2781 got_worktree_checkout_cb progress_cb;
2782 void *progress_arg;
2783 got_cancel_cb cancel_cb;
2784 void *cancel_arg;
2785 const char *label_orig;
2786 struct got_object_id *commit_id2;
2787 int allow_bad_symlinks;
2790 static const struct got_error *
2791 merge_file_cb(void *arg, struct got_blob_object *blob1,
2792 struct got_blob_object *blob2, FILE *f1, FILE *f2,
2793 struct got_object_id *id1, struct got_object_id *id2,
2794 const char *path1, const char *path2,
2795 mode_t mode1, mode_t mode2, struct got_repository *repo)
2797 static const struct got_error *err = NULL;
2798 struct merge_file_cb_arg *a = arg;
2799 struct got_fileindex_entry *ie;
2800 char *ondisk_path = NULL;
2801 struct stat sb;
2802 unsigned char status;
2803 int local_changes_subsumed;
2804 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2805 char *id_str = NULL, *label_deriv2 = NULL;
2807 if (blob1 && blob2) {
2808 ie = got_fileindex_entry_get(a->fileindex, path2,
2809 strlen(path2));
2810 if (ie == NULL)
2811 return (*a->progress_cb)(a->progress_arg,
2812 GOT_STATUS_MISSING, path2);
2814 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2815 path2) == -1)
2816 return got_error_from_errno("asprintf");
2818 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2819 repo);
2820 if (err)
2821 goto done;
2823 if (status == GOT_STATUS_DELETE) {
2824 err = (*a->progress_cb)(a->progress_arg,
2825 GOT_STATUS_MERGE, path2);
2826 goto done;
2828 if (status != GOT_STATUS_NO_CHANGE &&
2829 status != GOT_STATUS_MODIFY &&
2830 status != GOT_STATUS_CONFLICT &&
2831 status != GOT_STATUS_ADD) {
2832 err = (*a->progress_cb)(a->progress_arg, status, path2);
2833 goto done;
2836 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2837 char *link_target2;
2838 err = got_object_blob_read_to_str(&link_target2, blob2);
2839 if (err)
2840 goto done;
2841 err = merge_symlink(a->worktree, blob1, ondisk_path,
2842 path2, a->label_orig, link_target2, a->commit_id2,
2843 repo, a->progress_cb, a->progress_arg);
2844 free(link_target2);
2845 } else {
2846 int fd;
2848 f_orig = got_opentemp();
2849 if (f_orig == NULL) {
2850 err = got_error_from_errno("got_opentemp");
2851 goto done;
2853 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2854 f_orig, blob1);
2855 if (err)
2856 goto done;
2858 f_deriv2 = got_opentemp();
2859 if (f_deriv2 == NULL)
2860 goto done;
2861 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2862 f_deriv2, blob2);
2863 if (err)
2864 goto done;
2866 fd = open(ondisk_path,
2867 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2868 if (fd == -1) {
2869 err = got_error_from_errno2("open",
2870 ondisk_path);
2871 goto done;
2873 f_deriv = fdopen(fd, "r");
2874 if (f_deriv == NULL) {
2875 err = got_error_from_errno2("fdopen",
2876 ondisk_path);
2877 close(fd);
2878 goto done;
2880 err = got_object_id_str(&id_str, a->commit_id2);
2881 if (err)
2882 goto done;
2883 if (asprintf(&label_deriv2, "%s: commit %s",
2884 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2885 err = got_error_from_errno("asprintf");
2886 goto done;
2888 err = merge_file(&local_changes_subsumed, a->worktree,
2889 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2890 sb.st_mode, a->label_orig, NULL, label_deriv2,
2891 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2892 a->progress_cb, a->progress_arg);
2894 } else if (blob1) {
2895 ie = got_fileindex_entry_get(a->fileindex, path1,
2896 strlen(path1));
2897 if (ie == NULL)
2898 return (*a->progress_cb)(a->progress_arg,
2899 GOT_STATUS_MISSING, path1);
2901 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2902 path1) == -1)
2903 return got_error_from_errno("asprintf");
2905 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2906 repo);
2907 if (err)
2908 goto done;
2910 switch (status) {
2911 case GOT_STATUS_NO_CHANGE:
2912 err = (*a->progress_cb)(a->progress_arg,
2913 GOT_STATUS_DELETE, path1);
2914 if (err)
2915 goto done;
2916 err = remove_ondisk_file(a->worktree->root_path, path1);
2917 if (err)
2918 goto done;
2919 if (ie)
2920 got_fileindex_entry_mark_deleted_from_disk(ie);
2921 break;
2922 case GOT_STATUS_DELETE:
2923 case GOT_STATUS_MISSING:
2924 err = (*a->progress_cb)(a->progress_arg,
2925 GOT_STATUS_DELETE, path1);
2926 if (err)
2927 goto done;
2928 if (ie)
2929 got_fileindex_entry_mark_deleted_from_disk(ie);
2930 break;
2931 case GOT_STATUS_ADD: {
2932 struct got_object_id *id;
2933 FILE *blob1_f;
2934 off_t blob1_size;
2936 * Delete the added file only if its content already
2937 * exists in the repository.
2939 err = got_object_blob_file_create(&id, &blob1_f,
2940 &blob1_size, path1);
2941 if (err)
2942 goto done;
2943 if (got_object_id_cmp(id, id1) == 0) {
2944 err = (*a->progress_cb)(a->progress_arg,
2945 GOT_STATUS_DELETE, path1);
2946 if (err)
2947 goto done;
2948 err = remove_ondisk_file(a->worktree->root_path,
2949 path1);
2950 if (err)
2951 goto done;
2952 if (ie)
2953 got_fileindex_entry_remove(a->fileindex,
2954 ie);
2955 } else {
2956 err = (*a->progress_cb)(a->progress_arg,
2957 GOT_STATUS_CANNOT_DELETE, path1);
2959 if (fclose(blob1_f) == EOF && err == NULL)
2960 err = got_error_from_errno("fclose");
2961 free(id);
2962 if (err)
2963 goto done;
2964 break;
2966 case GOT_STATUS_MODIFY:
2967 case GOT_STATUS_CONFLICT:
2968 err = (*a->progress_cb)(a->progress_arg,
2969 GOT_STATUS_CANNOT_DELETE, path1);
2970 if (err)
2971 goto done;
2972 break;
2973 case GOT_STATUS_OBSTRUCTED:
2974 err = (*a->progress_cb)(a->progress_arg, status, path1);
2975 if (err)
2976 goto done;
2977 break;
2978 default:
2979 break;
2981 } else if (blob2) {
2982 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2983 path2) == -1)
2984 return got_error_from_errno("asprintf");
2985 ie = got_fileindex_entry_get(a->fileindex, path2,
2986 strlen(path2));
2987 if (ie) {
2988 err = get_file_status(&status, &sb, ie, ondisk_path,
2989 -1, NULL, repo);
2990 if (err)
2991 goto done;
2992 if (status != GOT_STATUS_NO_CHANGE &&
2993 status != GOT_STATUS_MODIFY &&
2994 status != GOT_STATUS_CONFLICT &&
2995 status != GOT_STATUS_ADD) {
2996 err = (*a->progress_cb)(a->progress_arg,
2997 status, path2);
2998 goto done;
3000 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3001 char *link_target2;
3002 err = got_object_blob_read_to_str(&link_target2,
3003 blob2);
3004 if (err)
3005 goto done;
3006 err = merge_symlink(a->worktree, NULL,
3007 ondisk_path, path2, a->label_orig,
3008 link_target2, a->commit_id2, repo,
3009 a->progress_cb, a->progress_arg);
3010 free(link_target2);
3011 } else if (S_ISREG(sb.st_mode)) {
3012 err = merge_blob(&local_changes_subsumed,
3013 a->worktree, NULL, ondisk_path, path2,
3014 sb.st_mode, a->label_orig, blob2,
3015 a->commit_id2, repo, a->progress_cb,
3016 a->progress_arg);
3017 } else {
3018 err = got_error_path(ondisk_path,
3019 GOT_ERR_FILE_OBSTRUCTED);
3021 if (err)
3022 goto done;
3023 if (status == GOT_STATUS_DELETE) {
3024 err = got_fileindex_entry_update(ie,
3025 a->worktree->root_fd, path2, blob2->id.sha1,
3026 a->worktree->base_commit_id->sha1, 0);
3027 if (err)
3028 goto done;
3030 } else {
3031 int is_bad_symlink = 0;
3032 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3033 if (S_ISLNK(mode2)) {
3034 err = install_symlink(&is_bad_symlink,
3035 a->worktree, ondisk_path, path2, blob2, 0,
3036 0, 1, a->allow_bad_symlinks, repo,
3037 a->progress_cb, a->progress_arg);
3038 } else {
3039 err = install_blob(a->worktree, ondisk_path, path2,
3040 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3041 a->progress_cb, a->progress_arg);
3043 if (err)
3044 goto done;
3045 err = got_fileindex_entry_alloc(&ie, path2);
3046 if (err)
3047 goto done;
3048 err = got_fileindex_entry_update(ie,
3049 a->worktree->root_fd, path2, NULL, NULL, 1);
3050 if (err) {
3051 got_fileindex_entry_free(ie);
3052 goto done;
3054 err = got_fileindex_entry_add(a->fileindex, ie);
3055 if (err) {
3056 got_fileindex_entry_free(ie);
3057 goto done;
3059 if (is_bad_symlink) {
3060 got_fileindex_entry_filetype_set(ie,
3061 GOT_FILEIDX_MODE_BAD_SYMLINK);
3065 done:
3066 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3067 err = got_error_from_errno("fclose");
3068 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3069 err = got_error_from_errno("fclose");
3070 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3071 err = got_error_from_errno("fclose");
3072 free(id_str);
3073 free(label_deriv2);
3074 free(ondisk_path);
3075 return err;
3078 static const struct got_error *
3079 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3081 struct got_worktree *worktree = arg;
3083 /* Reject merges into a work tree with mixed base commits. */
3084 if (got_fileindex_entry_has_commit(ie) &&
3085 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3086 SHA1_DIGEST_LENGTH) != 0)
3087 return got_error(GOT_ERR_MIXED_COMMITS);
3089 return NULL;
3092 struct check_merge_conflicts_arg {
3093 struct got_worktree *worktree;
3094 struct got_fileindex *fileindex;
3095 struct got_repository *repo;
3098 static const struct got_error *
3099 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3100 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3101 struct got_object_id *id1, struct got_object_id *id2,
3102 const char *path1, const char *path2,
3103 mode_t mode1, mode_t mode2, struct got_repository *repo)
3105 const struct got_error *err = NULL;
3106 struct check_merge_conflicts_arg *a = arg;
3107 unsigned char status;
3108 struct stat sb;
3109 struct got_fileindex_entry *ie;
3110 const char *path = path2 ? path2 : path1;
3111 struct got_object_id *id = id2 ? id2 : id1;
3112 char *ondisk_path;
3114 if (id == NULL)
3115 return NULL;
3117 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3118 if (ie == NULL)
3119 return NULL;
3121 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3122 == -1)
3123 return got_error_from_errno("asprintf");
3125 /* Reject merges into a work tree with conflicted files. */
3126 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3127 free(ondisk_path);
3128 if (err)
3129 return err;
3130 if (status == GOT_STATUS_CONFLICT)
3131 return got_error(GOT_ERR_CONFLICTS);
3133 return NULL;
3136 static const struct got_error *
3137 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3138 const char *fileindex_path, struct got_object_id *commit_id1,
3139 struct got_object_id *commit_id2, struct got_repository *repo,
3140 got_worktree_checkout_cb progress_cb, void *progress_arg,
3141 got_cancel_cb cancel_cb, void *cancel_arg)
3143 const struct got_error *err = NULL, *sync_err;
3144 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3145 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3146 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3147 struct check_merge_conflicts_arg cmc_arg;
3148 struct merge_file_cb_arg arg;
3149 char *label_orig = NULL;
3150 FILE *f1 = NULL, *f2 = NULL;
3151 int fd1 = -1, fd2 = -1;
3153 if (commit_id1) {
3154 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3155 if (err)
3156 goto done;
3157 err = got_object_id_by_path(&tree_id1, repo, commit1,
3158 worktree->path_prefix);
3159 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3160 goto done;
3162 if (tree_id1) {
3163 char *id_str;
3165 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3166 if (err)
3167 goto done;
3169 err = got_object_id_str(&id_str, commit_id1);
3170 if (err)
3171 goto done;
3173 if (asprintf(&label_orig, "%s: commit %s",
3174 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3175 err = got_error_from_errno("asprintf");
3176 free(id_str);
3177 goto done;
3179 free(id_str);
3181 f1 = got_opentemp();
3182 if (f1 == NULL) {
3183 err = got_error_from_errno("got_opentemp");
3184 goto done;
3188 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3189 if (err)
3190 goto done;
3192 err = got_object_id_by_path(&tree_id2, repo, commit2,
3193 worktree->path_prefix);
3194 if (err)
3195 goto done;
3197 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3198 if (err)
3199 goto done;
3201 f2 = got_opentemp();
3202 if (f2 == NULL) {
3203 err = got_error_from_errno("got_opentemp");
3204 goto done;
3207 fd1 = got_opentempfd();
3208 if (fd1 == -1) {
3209 err = got_error_from_errno("got_opentempfd");
3210 goto done;
3213 fd2 = got_opentempfd();
3214 if (fd2 == -1) {
3215 err = got_error_from_errno("got_opentempfd");
3216 goto done;
3219 cmc_arg.worktree = worktree;
3220 cmc_arg.fileindex = fileindex;
3221 cmc_arg.repo = repo;
3222 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3223 check_merge_conflicts, &cmc_arg, 0);
3224 if (err)
3225 goto done;
3227 arg.worktree = worktree;
3228 arg.fileindex = fileindex;
3229 arg.progress_cb = progress_cb;
3230 arg.progress_arg = progress_arg;
3231 arg.cancel_cb = cancel_cb;
3232 arg.cancel_arg = cancel_arg;
3233 arg.label_orig = label_orig;
3234 arg.commit_id2 = commit_id2;
3235 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3236 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3237 merge_file_cb, &arg, 1);
3238 sync_err = sync_fileindex(fileindex, fileindex_path);
3239 if (sync_err && err == NULL)
3240 err = sync_err;
3241 done:
3242 if (commit1)
3243 got_object_commit_close(commit1);
3244 if (commit2)
3245 got_object_commit_close(commit2);
3246 if (tree1)
3247 got_object_tree_close(tree1);
3248 if (tree2)
3249 got_object_tree_close(tree2);
3250 if (f1 && fclose(f1) == EOF && err == NULL)
3251 err = got_error_from_errno("fclose");
3252 if (f2 && fclose(f2) == EOF && err == NULL)
3253 err = got_error_from_errno("fclose");
3254 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3255 err = got_error_from_errno("close");
3256 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3257 err = got_error_from_errno("close");
3258 free(label_orig);
3259 return err;
3262 const struct got_error *
3263 got_worktree_merge_files(struct got_worktree *worktree,
3264 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3265 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3266 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3268 const struct got_error *err, *unlockerr;
3269 char *fileindex_path = NULL;
3270 struct got_fileindex *fileindex = NULL;
3272 err = lock_worktree(worktree, LOCK_EX);
3273 if (err)
3274 return err;
3276 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3277 if (err)
3278 goto done;
3280 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3281 worktree);
3282 if (err)
3283 goto done;
3285 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3286 commit_id2, repo, progress_cb, progress_arg,
3287 cancel_cb, cancel_arg);
3288 done:
3289 if (fileindex)
3290 got_fileindex_free(fileindex);
3291 free(fileindex_path);
3292 unlockerr = lock_worktree(worktree, LOCK_SH);
3293 if (unlockerr && err == NULL)
3294 err = unlockerr;
3295 return err;
3298 struct diff_dir_cb_arg {
3299 struct got_fileindex *fileindex;
3300 struct got_worktree *worktree;
3301 const char *status_path;
3302 size_t status_path_len;
3303 struct got_repository *repo;
3304 got_worktree_status_cb status_cb;
3305 void *status_arg;
3306 got_cancel_cb cancel_cb;
3307 void *cancel_arg;
3308 /* A pathlist containing per-directory pathlists of ignore patterns. */
3309 struct got_pathlist_head *ignores;
3310 int report_unchanged;
3311 int no_ignores;
3314 static const struct got_error *
3315 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3316 int dirfd, const char *de_name,
3317 got_worktree_status_cb status_cb, void *status_arg,
3318 struct got_repository *repo, int report_unchanged)
3320 const struct got_error *err = NULL;
3321 unsigned char status = GOT_STATUS_NO_CHANGE;
3322 unsigned char staged_status = get_staged_status(ie);
3323 struct stat sb;
3324 struct got_object_id blob_id, commit_id, staged_blob_id;
3325 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3326 struct got_object_id *staged_blob_idp = NULL;
3328 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3329 if (err)
3330 return err;
3332 if (status == GOT_STATUS_NO_CHANGE &&
3333 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3334 return NULL;
3336 if (got_fileindex_entry_has_blob(ie)) {
3337 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3338 blob_idp = &blob_id;
3340 if (got_fileindex_entry_has_commit(ie)) {
3341 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3342 commit_idp = &commit_id;
3344 if (staged_status == GOT_STATUS_ADD ||
3345 staged_status == GOT_STATUS_MODIFY) {
3346 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3347 SHA1_DIGEST_LENGTH);
3348 staged_blob_idp = &staged_blob_id;
3351 return (*status_cb)(status_arg, status, staged_status,
3352 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3355 static const struct got_error *
3356 status_old_new(void *arg, struct got_fileindex_entry *ie,
3357 struct dirent *de, const char *parent_path, int dirfd)
3359 const struct got_error *err = NULL;
3360 struct diff_dir_cb_arg *a = arg;
3361 char *abspath;
3363 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3364 return got_error(GOT_ERR_CANCELLED);
3366 if (got_path_cmp(parent_path, a->status_path,
3367 strlen(parent_path), a->status_path_len) != 0 &&
3368 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3369 return NULL;
3371 if (parent_path[0]) {
3372 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3373 parent_path, de->d_name) == -1)
3374 return got_error_from_errno("asprintf");
3375 } else {
3376 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3377 de->d_name) == -1)
3378 return got_error_from_errno("asprintf");
3381 err = report_file_status(ie, abspath, dirfd, de->d_name,
3382 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3383 free(abspath);
3384 return err;
3387 static const struct got_error *
3388 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3390 struct diff_dir_cb_arg *a = arg;
3391 struct got_object_id blob_id, commit_id;
3392 unsigned char status;
3394 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3395 return got_error(GOT_ERR_CANCELLED);
3397 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3398 return NULL;
3400 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3401 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3402 if (got_fileindex_entry_has_file_on_disk(ie))
3403 status = GOT_STATUS_MISSING;
3404 else
3405 status = GOT_STATUS_DELETE;
3406 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3407 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3410 static void
3411 free_ignorelist(struct got_pathlist_head *ignorelist)
3413 struct got_pathlist_entry *pe;
3415 TAILQ_FOREACH(pe, ignorelist, entry)
3416 free((char *)pe->path);
3417 got_pathlist_free(ignorelist);
3420 static void
3421 free_ignores(struct got_pathlist_head *ignores)
3423 struct got_pathlist_entry *pe;
3425 TAILQ_FOREACH(pe, ignores, entry) {
3426 struct got_pathlist_head *ignorelist = pe->data;
3427 free_ignorelist(ignorelist);
3428 free((char *)pe->path);
3430 got_pathlist_free(ignores);
3433 static const struct got_error *
3434 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3436 const struct got_error *err = NULL;
3437 struct got_pathlist_entry *pe = NULL;
3438 struct got_pathlist_head *ignorelist;
3439 char *line = NULL, *pattern, *dirpath = NULL;
3440 size_t linesize = 0;
3441 ssize_t linelen;
3443 ignorelist = calloc(1, sizeof(*ignorelist));
3444 if (ignorelist == NULL)
3445 return got_error_from_errno("calloc");
3446 TAILQ_INIT(ignorelist);
3448 while ((linelen = getline(&line, &linesize, f)) != -1) {
3449 if (linelen > 0 && line[linelen - 1] == '\n')
3450 line[linelen - 1] = '\0';
3452 /* Git's ignores may contain comments. */
3453 if (line[0] == '#')
3454 continue;
3456 /* Git's negated patterns are not (yet?) supported. */
3457 if (line[0] == '!')
3458 continue;
3460 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3461 line) == -1) {
3462 err = got_error_from_errno("asprintf");
3463 goto done;
3465 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3466 if (err)
3467 goto done;
3469 if (ferror(f)) {
3470 err = got_error_from_errno("getline");
3471 goto done;
3474 dirpath = strdup(path);
3475 if (dirpath == NULL) {
3476 err = got_error_from_errno("strdup");
3477 goto done;
3479 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3480 done:
3481 free(line);
3482 if (err || pe == NULL) {
3483 free(dirpath);
3484 free_ignorelist(ignorelist);
3486 return err;
3489 static int
3490 match_ignores(struct got_pathlist_head *ignores, const char *path)
3492 struct got_pathlist_entry *pe;
3494 /* Handle patterns which match in all directories. */
3495 TAILQ_FOREACH(pe, ignores, entry) {
3496 struct got_pathlist_head *ignorelist = pe->data;
3497 struct got_pathlist_entry *pi;
3499 TAILQ_FOREACH(pi, ignorelist, entry) {
3500 const char *p, *pattern = pi->path;
3502 if (strncmp(pattern, "**/", 3) != 0)
3503 continue;
3504 pattern += 3;
3505 p = path;
3506 while (*p) {
3507 if (fnmatch(pattern, p,
3508 FNM_PATHNAME | FNM_LEADING_DIR)) {
3509 /* Retry in next directory. */
3510 while (*p && *p != '/')
3511 p++;
3512 while (*p == '/')
3513 p++;
3514 continue;
3516 return 1;
3522 * The ignores pathlist contains ignore lists from children before
3523 * parents, so we can find the most specific ignorelist by walking
3524 * ignores backwards.
3526 pe = TAILQ_LAST(ignores, got_pathlist_head);
3527 while (pe) {
3528 if (got_path_is_child(path, pe->path, pe->path_len)) {
3529 struct got_pathlist_head *ignorelist = pe->data;
3530 struct got_pathlist_entry *pi;
3531 TAILQ_FOREACH(pi, ignorelist, entry) {
3532 const char *pattern = pi->path;
3533 int flags = FNM_LEADING_DIR;
3534 if (strstr(pattern, "/**/") == NULL)
3535 flags |= FNM_PATHNAME;
3536 if (fnmatch(pattern, path, flags))
3537 continue;
3538 return 1;
3541 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3544 return 0;
3547 static const struct got_error *
3548 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3549 const char *path, int dirfd, const char *ignores_filename)
3551 const struct got_error *err = NULL;
3552 char *ignorespath;
3553 int fd = -1;
3554 FILE *ignoresfile = NULL;
3556 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3557 path[0] ? "/" : "", ignores_filename) == -1)
3558 return got_error_from_errno("asprintf");
3560 if (dirfd != -1) {
3561 fd = openat(dirfd, ignores_filename,
3562 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3563 if (fd == -1) {
3564 if (errno != ENOENT && errno != EACCES)
3565 err = got_error_from_errno2("openat",
3566 ignorespath);
3567 } else {
3568 ignoresfile = fdopen(fd, "r");
3569 if (ignoresfile == NULL)
3570 err = got_error_from_errno2("fdopen",
3571 ignorespath);
3572 else {
3573 fd = -1;
3574 err = read_ignores(ignores, path, ignoresfile);
3577 } else {
3578 ignoresfile = fopen(ignorespath, "re");
3579 if (ignoresfile == NULL) {
3580 if (errno != ENOENT && errno != EACCES)
3581 err = got_error_from_errno2("fopen",
3582 ignorespath);
3583 } else
3584 err = read_ignores(ignores, path, ignoresfile);
3587 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3588 err = got_error_from_errno2("fclose", path);
3589 if (fd != -1 && close(fd) == -1 && err == NULL)
3590 err = got_error_from_errno2("close", path);
3591 free(ignorespath);
3592 return err;
3595 static const struct got_error *
3596 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3597 int dirfd)
3599 const struct got_error *err = NULL;
3600 struct diff_dir_cb_arg *a = arg;
3601 char *path = NULL;
3603 if (ignore != NULL)
3604 *ignore = 0;
3606 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3607 return got_error(GOT_ERR_CANCELLED);
3609 if (parent_path[0]) {
3610 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3611 return got_error_from_errno("asprintf");
3612 } else {
3613 path = de->d_name;
3616 if (de->d_type == DT_DIR) {
3617 if (!a->no_ignores && ignore != NULL &&
3618 match_ignores(a->ignores, path))
3619 *ignore = 1;
3620 } else if (!match_ignores(a->ignores, path) &&
3621 got_path_is_child(path, a->status_path, a->status_path_len))
3622 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3623 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3624 if (parent_path[0])
3625 free(path);
3626 return err;
3629 static const struct got_error *
3630 status_traverse(void *arg, const char *path, int dirfd)
3632 const struct got_error *err = NULL;
3633 struct diff_dir_cb_arg *a = arg;
3635 if (a->no_ignores)
3636 return NULL;
3638 err = add_ignores(a->ignores, a->worktree->root_path,
3639 path, dirfd, ".cvsignore");
3640 if (err)
3641 return err;
3643 err = add_ignores(a->ignores, a->worktree->root_path, path,
3644 dirfd, ".gitignore");
3646 return err;
3649 static const struct got_error *
3650 report_single_file_status(const char *path, const char *ondisk_path,
3651 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3652 void *status_arg, struct got_repository *repo, int report_unchanged,
3653 struct got_pathlist_head *ignores, int no_ignores)
3655 struct got_fileindex_entry *ie;
3656 struct stat sb;
3658 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3659 if (ie)
3660 return report_file_status(ie, ondisk_path, -1, NULL,
3661 status_cb, status_arg, repo, report_unchanged);
3663 if (lstat(ondisk_path, &sb) == -1) {
3664 if (errno != ENOENT)
3665 return got_error_from_errno2("lstat", ondisk_path);
3666 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3667 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3670 if (!no_ignores && match_ignores(ignores, path))
3671 return NULL;
3673 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3674 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3675 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3677 return NULL;
3680 static const struct got_error *
3681 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3682 const char *root_path, const char *path)
3684 const struct got_error *err;
3685 char *parent_path, *next_parent_path = NULL;
3687 err = add_ignores(ignores, root_path, "", -1,
3688 ".cvsignore");
3689 if (err)
3690 return err;
3692 err = add_ignores(ignores, root_path, "", -1,
3693 ".gitignore");
3694 if (err)
3695 return err;
3697 err = got_path_dirname(&parent_path, path);
3698 if (err) {
3699 if (err->code == GOT_ERR_BAD_PATH)
3700 return NULL; /* cannot traverse parent */
3701 return err;
3703 for (;;) {
3704 err = add_ignores(ignores, root_path, parent_path, -1,
3705 ".cvsignore");
3706 if (err)
3707 break;
3708 err = add_ignores(ignores, root_path, parent_path, -1,
3709 ".gitignore");
3710 if (err)
3711 break;
3712 err = got_path_dirname(&next_parent_path, parent_path);
3713 if (err) {
3714 if (err->code == GOT_ERR_BAD_PATH)
3715 err = NULL; /* traversed everything */
3716 break;
3718 if (got_path_is_root_dir(parent_path))
3719 break;
3720 free(parent_path);
3721 parent_path = next_parent_path;
3722 next_parent_path = NULL;
3725 free(parent_path);
3726 free(next_parent_path);
3727 return err;
3730 static const struct got_error *
3731 worktree_status(struct got_worktree *worktree, const char *path,
3732 struct got_fileindex *fileindex, struct got_repository *repo,
3733 got_worktree_status_cb status_cb, void *status_arg,
3734 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3735 int report_unchanged)
3737 const struct got_error *err = NULL;
3738 int fd = -1;
3739 struct got_fileindex_diff_dir_cb fdiff_cb;
3740 struct diff_dir_cb_arg arg;
3741 char *ondisk_path = NULL;
3742 struct got_pathlist_head ignores;
3743 struct got_fileindex_entry *ie;
3745 TAILQ_INIT(&ignores);
3747 if (asprintf(&ondisk_path, "%s%s%s",
3748 worktree->root_path, path[0] ? "/" : "", path) == -1)
3749 return got_error_from_errno("asprintf");
3751 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3752 if (ie) {
3753 err = report_single_file_status(path, ondisk_path,
3754 fileindex, status_cb, status_arg, repo,
3755 report_unchanged, &ignores, no_ignores);
3756 goto done;
3759 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3760 if (fd == -1) {
3761 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3762 !got_err_open_nofollow_on_symlink())
3763 err = got_error_from_errno2("open", ondisk_path);
3764 else {
3765 if (!no_ignores) {
3766 err = add_ignores_from_parent_paths(&ignores,
3767 worktree->root_path, ondisk_path);
3768 if (err)
3769 goto done;
3771 err = report_single_file_status(path, ondisk_path,
3772 fileindex, status_cb, status_arg, repo,
3773 report_unchanged, &ignores, no_ignores);
3775 } else {
3776 fdiff_cb.diff_old_new = status_old_new;
3777 fdiff_cb.diff_old = status_old;
3778 fdiff_cb.diff_new = status_new;
3779 fdiff_cb.diff_traverse = status_traverse;
3780 arg.fileindex = fileindex;
3781 arg.worktree = worktree;
3782 arg.status_path = path;
3783 arg.status_path_len = strlen(path);
3784 arg.repo = repo;
3785 arg.status_cb = status_cb;
3786 arg.status_arg = status_arg;
3787 arg.cancel_cb = cancel_cb;
3788 arg.cancel_arg = cancel_arg;
3789 arg.report_unchanged = report_unchanged;
3790 arg.no_ignores = no_ignores;
3791 if (!no_ignores) {
3792 err = add_ignores_from_parent_paths(&ignores,
3793 worktree->root_path, path);
3794 if (err)
3795 goto done;
3797 arg.ignores = &ignores;
3798 err = got_fileindex_diff_dir(fileindex, fd,
3799 worktree->root_path, path, repo, &fdiff_cb, &arg);
3801 done:
3802 free_ignores(&ignores);
3803 if (fd != -1 && close(fd) == -1 && err == NULL)
3804 err = got_error_from_errno("close");
3805 free(ondisk_path);
3806 return err;
3809 const struct got_error *
3810 got_worktree_status(struct got_worktree *worktree,
3811 struct got_pathlist_head *paths, struct got_repository *repo,
3812 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3813 got_cancel_cb cancel_cb, void *cancel_arg)
3815 const struct got_error *err = NULL;
3816 char *fileindex_path = NULL;
3817 struct got_fileindex *fileindex = NULL;
3818 struct got_pathlist_entry *pe;
3820 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3821 if (err)
3822 return err;
3824 TAILQ_FOREACH(pe, paths, entry) {
3825 err = worktree_status(worktree, pe->path, fileindex, repo,
3826 status_cb, status_arg, cancel_cb, cancel_arg,
3827 no_ignores, 0);
3828 if (err)
3829 break;
3831 free(fileindex_path);
3832 got_fileindex_free(fileindex);
3833 return err;
3836 const struct got_error *
3837 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3838 const char *arg)
3840 const struct got_error *err = NULL;
3841 char *resolved = NULL, *cwd = NULL, *path = NULL;
3842 size_t len;
3843 struct stat sb;
3844 char *abspath = NULL;
3845 char canonpath[PATH_MAX];
3847 *wt_path = NULL;
3849 cwd = getcwd(NULL, 0);
3850 if (cwd == NULL)
3851 return got_error_from_errno("getcwd");
3853 if (lstat(arg, &sb) == -1) {
3854 if (errno != ENOENT) {
3855 err = got_error_from_errno2("lstat", arg);
3856 goto done;
3858 sb.st_mode = 0;
3860 if (S_ISLNK(sb.st_mode)) {
3862 * We cannot use realpath(3) with symlinks since we want to
3863 * operate on the symlink itself.
3864 * But we can make the path absolute, assuming it is relative
3865 * to the current working directory, and then canonicalize it.
3867 if (!got_path_is_absolute(arg)) {
3868 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3869 err = got_error_from_errno("asprintf");
3870 goto done;
3874 err = got_canonpath(abspath ? abspath : arg, canonpath,
3875 sizeof(canonpath));
3876 if (err)
3877 goto done;
3878 resolved = strdup(canonpath);
3879 if (resolved == NULL) {
3880 err = got_error_from_errno("strdup");
3881 goto done;
3883 } else {
3884 resolved = realpath(arg, NULL);
3885 if (resolved == NULL) {
3886 if (errno != ENOENT) {
3887 err = got_error_from_errno2("realpath", arg);
3888 goto done;
3890 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3891 err = got_error_from_errno("asprintf");
3892 goto done;
3894 err = got_canonpath(abspath, canonpath,
3895 sizeof(canonpath));
3896 if (err)
3897 goto done;
3898 resolved = strdup(canonpath);
3899 if (resolved == NULL) {
3900 err = got_error_from_errno("strdup");
3901 goto done;
3906 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3907 strlen(got_worktree_get_root_path(worktree)))) {
3908 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3909 goto done;
3912 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3913 err = got_path_skip_common_ancestor(&path,
3914 got_worktree_get_root_path(worktree), resolved);
3915 if (err)
3916 goto done;
3917 } else {
3918 path = strdup("");
3919 if (path == NULL) {
3920 err = got_error_from_errno("strdup");
3921 goto done;
3925 /* XXX status walk can't deal with trailing slash! */
3926 len = strlen(path);
3927 while (len > 0 && path[len - 1] == '/') {
3928 path[len - 1] = '\0';
3929 len--;
3931 done:
3932 free(abspath);
3933 free(resolved);
3934 free(cwd);
3935 if (err == NULL)
3936 *wt_path = path;
3937 else
3938 free(path);
3939 return err;
3942 struct schedule_addition_args {
3943 struct got_worktree *worktree;
3944 struct got_fileindex *fileindex;
3945 got_worktree_checkout_cb progress_cb;
3946 void *progress_arg;
3947 struct got_repository *repo;
3950 static const struct got_error *
3951 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3952 const char *relpath, struct got_object_id *blob_id,
3953 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3954 int dirfd, const char *de_name)
3956 struct schedule_addition_args *a = arg;
3957 const struct got_error *err = NULL;
3958 struct got_fileindex_entry *ie;
3959 struct stat sb;
3960 char *ondisk_path;
3962 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3963 relpath) == -1)
3964 return got_error_from_errno("asprintf");
3966 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3967 if (ie) {
3968 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3969 de_name, a->repo);
3970 if (err)
3971 goto done;
3972 /* Re-adding an existing entry is a no-op. */
3973 if (status == GOT_STATUS_ADD)
3974 goto done;
3975 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3976 if (err)
3977 goto done;
3980 if (status != GOT_STATUS_UNVERSIONED) {
3981 if (status == GOT_STATUS_NONEXISTENT)
3982 err = got_error_set_errno(ENOENT, ondisk_path);
3983 else
3984 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3985 goto done;
3988 err = got_fileindex_entry_alloc(&ie, relpath);
3989 if (err)
3990 goto done;
3991 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3992 relpath, NULL, NULL, 1);
3993 if (err) {
3994 got_fileindex_entry_free(ie);
3995 goto done;
3997 err = got_fileindex_entry_add(a->fileindex, ie);
3998 if (err) {
3999 got_fileindex_entry_free(ie);
4000 goto done;
4002 done:
4003 free(ondisk_path);
4004 if (err)
4005 return err;
4006 if (status == GOT_STATUS_ADD)
4007 return NULL;
4008 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4011 const struct got_error *
4012 got_worktree_schedule_add(struct got_worktree *worktree,
4013 struct got_pathlist_head *paths,
4014 got_worktree_checkout_cb progress_cb, void *progress_arg,
4015 struct got_repository *repo, int no_ignores)
4017 struct got_fileindex *fileindex = NULL;
4018 char *fileindex_path = NULL;
4019 const struct got_error *err = NULL, *sync_err, *unlockerr;
4020 struct got_pathlist_entry *pe;
4021 struct schedule_addition_args saa;
4023 err = lock_worktree(worktree, LOCK_EX);
4024 if (err)
4025 return err;
4027 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4028 if (err)
4029 goto done;
4031 saa.worktree = worktree;
4032 saa.fileindex = fileindex;
4033 saa.progress_cb = progress_cb;
4034 saa.progress_arg = progress_arg;
4035 saa.repo = repo;
4037 TAILQ_FOREACH(pe, paths, entry) {
4038 err = worktree_status(worktree, pe->path, fileindex, repo,
4039 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4040 if (err)
4041 break;
4043 sync_err = sync_fileindex(fileindex, fileindex_path);
4044 if (sync_err && err == NULL)
4045 err = sync_err;
4046 done:
4047 free(fileindex_path);
4048 if (fileindex)
4049 got_fileindex_free(fileindex);
4050 unlockerr = lock_worktree(worktree, LOCK_SH);
4051 if (unlockerr && err == NULL)
4052 err = unlockerr;
4053 return err;
4056 struct schedule_deletion_args {
4057 struct got_worktree *worktree;
4058 struct got_fileindex *fileindex;
4059 got_worktree_delete_cb progress_cb;
4060 void *progress_arg;
4061 struct got_repository *repo;
4062 int delete_local_mods;
4063 int keep_on_disk;
4064 int ignore_missing_paths;
4065 const char *status_codes;
4068 static const struct got_error *
4069 schedule_for_deletion(void *arg, unsigned char status,
4070 unsigned char staged_status, const char *relpath,
4071 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4072 struct got_object_id *commit_id, int dirfd, const char *de_name)
4074 struct schedule_deletion_args *a = arg;
4075 const struct got_error *err = NULL;
4076 struct got_fileindex_entry *ie = NULL;
4077 struct stat sb;
4078 char *ondisk_path;
4080 if (status == GOT_STATUS_NONEXISTENT) {
4081 if (a->ignore_missing_paths)
4082 return NULL;
4083 return got_error_set_errno(ENOENT, relpath);
4086 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4087 if (ie == NULL)
4088 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4090 staged_status = get_staged_status(ie);
4091 if (staged_status != GOT_STATUS_NO_CHANGE) {
4092 if (staged_status == GOT_STATUS_DELETE)
4093 return NULL;
4094 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4097 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4098 relpath) == -1)
4099 return got_error_from_errno("asprintf");
4101 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4102 a->repo);
4103 if (err)
4104 goto done;
4106 if (a->status_codes) {
4107 size_t ncodes = strlen(a->status_codes);
4108 int i;
4109 for (i = 0; i < ncodes ; i++) {
4110 if (status == a->status_codes[i])
4111 break;
4113 if (i == ncodes) {
4114 /* Do not delete files in non-matching status. */
4115 free(ondisk_path);
4116 return NULL;
4118 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4119 a->status_codes[i] != GOT_STATUS_MISSING) {
4120 static char msg[64];
4121 snprintf(msg, sizeof(msg),
4122 "invalid status code '%c'", a->status_codes[i]);
4123 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4124 goto done;
4128 if (status != GOT_STATUS_NO_CHANGE) {
4129 if (status == GOT_STATUS_DELETE)
4130 goto done;
4131 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4132 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4133 goto done;
4135 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4136 err = got_error_set_errno(ENOENT, relpath);
4137 goto done;
4139 if (status != GOT_STATUS_MODIFY &&
4140 status != GOT_STATUS_MISSING) {
4141 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4142 goto done;
4146 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4147 size_t root_len;
4149 if (dirfd != -1) {
4150 if (unlinkat(dirfd, de_name, 0) == -1) {
4151 err = got_error_from_errno2("unlinkat",
4152 ondisk_path);
4153 goto done;
4155 } else if (unlink(ondisk_path) == -1) {
4156 err = got_error_from_errno2("unlink", ondisk_path);
4157 goto done;
4160 root_len = strlen(a->worktree->root_path);
4161 do {
4162 char *parent;
4163 err = got_path_dirname(&parent, ondisk_path);
4164 if (err)
4165 goto done;
4166 free(ondisk_path);
4167 ondisk_path = parent;
4168 if (rmdir(ondisk_path) == -1) {
4169 if (errno != ENOTEMPTY)
4170 err = got_error_from_errno2("rmdir",
4171 ondisk_path);
4172 break;
4174 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4175 strlen(ondisk_path), root_len) != 0);
4178 got_fileindex_entry_mark_deleted_from_disk(ie);
4179 done:
4180 free(ondisk_path);
4181 if (err)
4182 return err;
4183 if (status == GOT_STATUS_DELETE)
4184 return NULL;
4185 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4186 staged_status, relpath);
4189 const struct got_error *
4190 got_worktree_schedule_delete(struct got_worktree *worktree,
4191 struct got_pathlist_head *paths, int delete_local_mods,
4192 const char *status_codes,
4193 got_worktree_delete_cb progress_cb, void *progress_arg,
4194 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4196 struct got_fileindex *fileindex = NULL;
4197 char *fileindex_path = NULL;
4198 const struct got_error *err = NULL, *sync_err, *unlockerr;
4199 struct got_pathlist_entry *pe;
4200 struct schedule_deletion_args sda;
4202 err = lock_worktree(worktree, LOCK_EX);
4203 if (err)
4204 return err;
4206 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4207 if (err)
4208 goto done;
4210 sda.worktree = worktree;
4211 sda.fileindex = fileindex;
4212 sda.progress_cb = progress_cb;
4213 sda.progress_arg = progress_arg;
4214 sda.repo = repo;
4215 sda.delete_local_mods = delete_local_mods;
4216 sda.keep_on_disk = keep_on_disk;
4217 sda.ignore_missing_paths = ignore_missing_paths;
4218 sda.status_codes = status_codes;
4220 TAILQ_FOREACH(pe, paths, entry) {
4221 err = worktree_status(worktree, pe->path, fileindex, repo,
4222 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4223 if (err)
4224 break;
4226 sync_err = sync_fileindex(fileindex, fileindex_path);
4227 if (sync_err && err == NULL)
4228 err = sync_err;
4229 done:
4230 free(fileindex_path);
4231 if (fileindex)
4232 got_fileindex_free(fileindex);
4233 unlockerr = lock_worktree(worktree, LOCK_SH);
4234 if (unlockerr && err == NULL)
4235 err = unlockerr;
4236 return err;
4239 static const struct got_error *
4240 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4242 const struct got_error *err = NULL;
4243 char *line = NULL;
4244 size_t linesize = 0, n;
4245 ssize_t linelen;
4247 linelen = getline(&line, &linesize, infile);
4248 if (linelen == -1) {
4249 if (ferror(infile)) {
4250 err = got_error_from_errno("getline");
4251 goto done;
4253 return NULL;
4255 if (outfile) {
4256 n = fwrite(line, 1, linelen, outfile);
4257 if (n != linelen) {
4258 err = got_ferror(outfile, GOT_ERR_IO);
4259 goto done;
4262 if (rejectfile) {
4263 n = fwrite(line, 1, linelen, rejectfile);
4264 if (n != linelen)
4265 err = got_ferror(outfile, GOT_ERR_IO);
4267 done:
4268 free(line);
4269 return err;
4272 static const struct got_error *
4273 skip_one_line(FILE *f)
4275 char *line = NULL;
4276 size_t linesize = 0;
4277 ssize_t linelen;
4279 linelen = getline(&line, &linesize, f);
4280 if (linelen == -1) {
4281 if (ferror(f))
4282 return got_error_from_errno("getline");
4283 return NULL;
4285 free(line);
4286 return NULL;
4289 static const struct got_error *
4290 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4291 int start_old, int end_old, int start_new, int end_new,
4292 FILE *outfile, FILE *rejectfile)
4294 const struct got_error *err;
4296 /* Copy old file's lines leading up to patch. */
4297 while (!feof(f1) && *line_cur1 < start_old) {
4298 err = copy_one_line(f1, outfile, NULL);
4299 if (err)
4300 return err;
4301 (*line_cur1)++;
4303 /* Skip new file's lines leading up to patch. */
4304 while (!feof(f2) && *line_cur2 < start_new) {
4305 if (rejectfile)
4306 err = copy_one_line(f2, NULL, rejectfile);
4307 else
4308 err = skip_one_line(f2);
4309 if (err)
4310 return err;
4311 (*line_cur2)++;
4313 /* Copy patched lines. */
4314 while (!feof(f2) && *line_cur2 <= end_new) {
4315 err = copy_one_line(f2, outfile, NULL);
4316 if (err)
4317 return err;
4318 (*line_cur2)++;
4320 /* Skip over old file's replaced lines. */
4321 while (!feof(f1) && *line_cur1 <= end_old) {
4322 if (rejectfile)
4323 err = copy_one_line(f1, NULL, rejectfile);
4324 else
4325 err = skip_one_line(f1);
4326 if (err)
4327 return err;
4328 (*line_cur1)++;
4331 return NULL;
4334 static const struct got_error *
4335 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4336 FILE *outfile, FILE *rejectfile)
4338 const struct got_error *err;
4340 if (outfile) {
4341 /* Copy old file's lines until EOF. */
4342 while (!feof(f1)) {
4343 err = copy_one_line(f1, outfile, NULL);
4344 if (err)
4345 return err;
4346 (*line_cur1)++;
4349 if (rejectfile) {
4350 /* Copy new file's lines until EOF. */
4351 while (!feof(f2)) {
4352 err = copy_one_line(f2, NULL, rejectfile);
4353 if (err)
4354 return err;
4355 (*line_cur2)++;
4359 return NULL;
4362 static const struct got_error *
4363 apply_or_reject_change(int *choice, int *nchunks_used,
4364 struct diff_result *diff_result, int n,
4365 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4366 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4367 got_worktree_patch_cb patch_cb, void *patch_arg)
4369 const struct got_error *err = NULL;
4370 struct diff_chunk_context cc = {};
4371 int start_old, end_old, start_new, end_new;
4372 FILE *hunkfile;
4373 struct diff_output_unidiff_state *diff_state;
4374 struct diff_input_info diff_info;
4375 int rc;
4377 *choice = GOT_PATCH_CHOICE_NONE;
4379 /* Get changed line numbers without context lines for copy_change(). */
4380 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4381 start_old = cc.left.start;
4382 end_old = cc.left.end;
4383 start_new = cc.right.start;
4384 end_new = cc.right.end;
4386 /* Get the same change with context lines for display. */
4387 memset(&cc, 0, sizeof(cc));
4388 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4390 memset(&diff_info, 0, sizeof(diff_info));
4391 diff_info.left_path = relpath;
4392 diff_info.right_path = relpath;
4394 diff_state = diff_output_unidiff_state_alloc();
4395 if (diff_state == NULL)
4396 return got_error_set_errno(ENOMEM,
4397 "diff_output_unidiff_state_alloc");
4399 hunkfile = got_opentemp();
4400 if (hunkfile == NULL) {
4401 err = got_error_from_errno("got_opentemp");
4402 goto done;
4405 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4406 diff_result, &cc);
4407 if (rc != DIFF_RC_OK) {
4408 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4409 goto done;
4412 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4413 err = got_ferror(hunkfile, GOT_ERR_IO);
4414 goto done;
4417 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4418 hunkfile, changeno, nchanges);
4419 if (err)
4420 goto done;
4422 switch (*choice) {
4423 case GOT_PATCH_CHOICE_YES:
4424 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4425 end_old, start_new, end_new, outfile, rejectfile);
4426 break;
4427 case GOT_PATCH_CHOICE_NO:
4428 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4429 end_old, start_new, end_new, rejectfile, outfile);
4430 break;
4431 case GOT_PATCH_CHOICE_QUIT:
4432 break;
4433 default:
4434 err = got_error(GOT_ERR_PATCH_CHOICE);
4435 break;
4437 done:
4438 diff_output_unidiff_state_free(diff_state);
4439 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4440 err = got_error_from_errno("fclose");
4441 return err;
4444 struct revert_file_args {
4445 struct got_worktree *worktree;
4446 struct got_fileindex *fileindex;
4447 got_worktree_checkout_cb progress_cb;
4448 void *progress_arg;
4449 got_worktree_patch_cb patch_cb;
4450 void *patch_arg;
4451 struct got_repository *repo;
4452 int unlink_added_files;
4455 static const struct got_error *
4456 create_patched_content(char **path_outfile, int reverse_patch,
4457 struct got_object_id *blob_id, const char *path2,
4458 int dirfd2, const char *de_name2,
4459 const char *relpath, struct got_repository *repo,
4460 got_worktree_patch_cb patch_cb, void *patch_arg)
4462 const struct got_error *err, *free_err;
4463 struct got_blob_object *blob = NULL;
4464 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4465 int fd = -1, fd2 = -1;
4466 char link_target[PATH_MAX];
4467 ssize_t link_len = 0;
4468 char *path1 = NULL, *id_str = NULL;
4469 struct stat sb2;
4470 struct got_diffreg_result *diffreg_result = NULL;
4471 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4472 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4474 *path_outfile = NULL;
4476 err = got_object_id_str(&id_str, blob_id);
4477 if (err)
4478 return err;
4480 if (dirfd2 != -1) {
4481 fd2 = openat(dirfd2, de_name2,
4482 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4483 if (fd2 == -1) {
4484 if (!got_err_open_nofollow_on_symlink()) {
4485 err = got_error_from_errno2("openat", path2);
4486 goto done;
4488 link_len = readlinkat(dirfd2, de_name2,
4489 link_target, sizeof(link_target));
4490 if (link_len == -1) {
4491 return got_error_from_errno2("readlinkat",
4492 path2);
4494 sb2.st_mode = S_IFLNK;
4495 sb2.st_size = link_len;
4497 } else {
4498 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4499 if (fd2 == -1) {
4500 if (!got_err_open_nofollow_on_symlink()) {
4501 err = got_error_from_errno2("open", path2);
4502 goto done;
4504 link_len = readlink(path2, link_target,
4505 sizeof(link_target));
4506 if (link_len == -1)
4507 return got_error_from_errno2("readlink", path2);
4508 sb2.st_mode = S_IFLNK;
4509 sb2.st_size = link_len;
4512 if (fd2 != -1) {
4513 if (fstat(fd2, &sb2) == -1) {
4514 err = got_error_from_errno2("fstat", path2);
4515 goto done;
4518 f2 = fdopen(fd2, "r");
4519 if (f2 == NULL) {
4520 err = got_error_from_errno2("fdopen", path2);
4521 goto done;
4523 fd2 = -1;
4524 } else {
4525 size_t n;
4526 f2 = got_opentemp();
4527 if (f2 == NULL) {
4528 err = got_error_from_errno2("got_opentemp", path2);
4529 goto done;
4531 n = fwrite(link_target, 1, link_len, f2);
4532 if (n != link_len) {
4533 err = got_ferror(f2, GOT_ERR_IO);
4534 goto done;
4536 if (fflush(f2) == EOF) {
4537 err = got_error_from_errno("fflush");
4538 goto done;
4540 rewind(f2);
4543 fd = got_opentempfd();
4544 if (fd == -1) {
4545 err = got_error_from_errno("got_opentempfd");
4546 goto done;
4549 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4550 if (err)
4551 goto done;
4553 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4554 if (err)
4555 goto done;
4557 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4558 if (err)
4559 goto done;
4561 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4562 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4563 if (err)
4564 goto done;
4566 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4567 if (err)
4568 goto done;
4570 if (fseek(f1, 0L, SEEK_SET) == -1)
4571 return got_ferror(f1, GOT_ERR_IO);
4572 if (fseek(f2, 0L, SEEK_SET) == -1)
4573 return got_ferror(f2, GOT_ERR_IO);
4575 /* Count the number of actual changes in the diff result. */
4576 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4577 struct diff_chunk_context cc = {};
4578 diff_chunk_context_load_change(&cc, &nchunks_used,
4579 diffreg_result->result, n, 0);
4580 nchanges++;
4582 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4583 int choice;
4584 err = apply_or_reject_change(&choice, &nchunks_used,
4585 diffreg_result->result, n, relpath, f1, f2,
4586 &line_cur1, &line_cur2,
4587 reverse_patch ? NULL : outfile,
4588 reverse_patch ? outfile : NULL,
4589 ++i, nchanges, patch_cb, patch_arg);
4590 if (err)
4591 goto done;
4592 if (choice == GOT_PATCH_CHOICE_YES)
4593 have_content = 1;
4594 else if (choice == GOT_PATCH_CHOICE_QUIT)
4595 break;
4597 if (have_content) {
4598 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4599 reverse_patch ? NULL : outfile,
4600 reverse_patch ? outfile : NULL);
4601 if (err)
4602 goto done;
4604 if (!S_ISLNK(sb2.st_mode)) {
4605 mode_t mode;
4607 mode = apply_umask(sb2.st_mode);
4608 if (fchmod(fileno(outfile), mode) == -1) {
4609 err = got_error_from_errno2("fchmod", path2);
4610 goto done;
4614 done:
4615 free(id_str);
4616 if (fd != -1 && close(fd) == -1 && err == NULL)
4617 err = got_error_from_errno("close");
4618 if (blob)
4619 got_object_blob_close(blob);
4620 free_err = got_diffreg_result_free(diffreg_result);
4621 if (err == NULL)
4622 err = free_err;
4623 if (f1 && fclose(f1) == EOF && err == NULL)
4624 err = got_error_from_errno2("fclose", path1);
4625 if (f2 && fclose(f2) == EOF && err == NULL)
4626 err = got_error_from_errno2("fclose", path2);
4627 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4628 err = got_error_from_errno2("close", path2);
4629 if (outfile && fclose(outfile) == EOF && err == NULL)
4630 err = got_error_from_errno2("fclose", *path_outfile);
4631 if (path1 && unlink(path1) == -1 && err == NULL)
4632 err = got_error_from_errno2("unlink", path1);
4633 if (err || !have_content) {
4634 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4635 err = got_error_from_errno2("unlink", *path_outfile);
4636 free(*path_outfile);
4637 *path_outfile = NULL;
4639 free(path1);
4640 return err;
4643 static const struct got_error *
4644 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4645 const char *relpath, struct got_object_id *blob_id,
4646 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4647 int dirfd, const char *de_name)
4649 struct revert_file_args *a = arg;
4650 const struct got_error *err = NULL;
4651 char *parent_path = NULL;
4652 struct got_fileindex_entry *ie;
4653 struct got_commit_object *base_commit = NULL;
4654 struct got_tree_object *tree = NULL;
4655 struct got_object_id *tree_id = NULL;
4656 const struct got_tree_entry *te = NULL;
4657 char *tree_path = NULL, *te_name;
4658 char *ondisk_path = NULL, *path_content = NULL;
4659 struct got_blob_object *blob = NULL;
4660 int fd = -1;
4662 /* Reverting a staged deletion is a no-op. */
4663 if (status == GOT_STATUS_DELETE &&
4664 staged_status != GOT_STATUS_NO_CHANGE)
4665 return NULL;
4667 if (status == GOT_STATUS_UNVERSIONED)
4668 return (*a->progress_cb)(a->progress_arg,
4669 GOT_STATUS_UNVERSIONED, relpath);
4671 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4672 if (ie == NULL)
4673 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4675 /* Construct in-repository path of tree which contains this blob. */
4676 err = got_path_dirname(&parent_path, ie->path);
4677 if (err) {
4678 if (err->code != GOT_ERR_BAD_PATH)
4679 goto done;
4680 parent_path = strdup("/");
4681 if (parent_path == NULL) {
4682 err = got_error_from_errno("strdup");
4683 goto done;
4686 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4687 tree_path = strdup(parent_path);
4688 if (tree_path == NULL) {
4689 err = got_error_from_errno("strdup");
4690 goto done;
4692 } else {
4693 if (got_path_is_root_dir(parent_path)) {
4694 tree_path = strdup(a->worktree->path_prefix);
4695 if (tree_path == NULL) {
4696 err = got_error_from_errno("strdup");
4697 goto done;
4699 } else {
4700 if (asprintf(&tree_path, "%s/%s",
4701 a->worktree->path_prefix, parent_path) == -1) {
4702 err = got_error_from_errno("asprintf");
4703 goto done;
4708 err = got_object_open_as_commit(&base_commit, a->repo,
4709 a->worktree->base_commit_id);
4710 if (err)
4711 goto done;
4713 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4714 if (err) {
4715 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4716 (status == GOT_STATUS_ADD ||
4717 staged_status == GOT_STATUS_ADD)))
4718 goto done;
4719 } else {
4720 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4721 if (err)
4722 goto done;
4724 err = got_path_basename(&te_name, ie->path);
4725 if (err)
4726 goto done;
4728 te = got_object_tree_find_entry(tree, te_name);
4729 free(te_name);
4730 if (te == NULL && status != GOT_STATUS_ADD &&
4731 staged_status != GOT_STATUS_ADD) {
4732 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4733 goto done;
4737 switch (status) {
4738 case GOT_STATUS_ADD:
4739 if (a->patch_cb) {
4740 int choice = GOT_PATCH_CHOICE_NONE;
4741 err = (*a->patch_cb)(&choice, a->patch_arg,
4742 status, ie->path, NULL, 1, 1);
4743 if (err)
4744 goto done;
4745 if (choice != GOT_PATCH_CHOICE_YES)
4746 break;
4748 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4749 ie->path);
4750 if (err)
4751 goto done;
4752 got_fileindex_entry_remove(a->fileindex, ie);
4753 if (a->unlink_added_files) {
4754 if (asprintf(&ondisk_path, "%s/%s",
4755 got_worktree_get_root_path(a->worktree),
4756 relpath) == -1) {
4757 err = got_error_from_errno("asprintf");
4758 goto done;
4760 if (unlink(ondisk_path) == -1) {
4761 err = got_error_from_errno2("unlink",
4762 ondisk_path);
4763 break;
4766 break;
4767 case GOT_STATUS_DELETE:
4768 if (a->patch_cb) {
4769 int choice = GOT_PATCH_CHOICE_NONE;
4770 err = (*a->patch_cb)(&choice, a->patch_arg,
4771 status, ie->path, NULL, 1, 1);
4772 if (err)
4773 goto done;
4774 if (choice != GOT_PATCH_CHOICE_YES)
4775 break;
4777 /* fall through */
4778 case GOT_STATUS_MODIFY:
4779 case GOT_STATUS_MODE_CHANGE:
4780 case GOT_STATUS_CONFLICT:
4781 case GOT_STATUS_MISSING: {
4782 struct got_object_id id;
4783 if (staged_status == GOT_STATUS_ADD ||
4784 staged_status == GOT_STATUS_MODIFY) {
4785 memcpy(id.sha1, ie->staged_blob_sha1,
4786 SHA1_DIGEST_LENGTH);
4787 } else
4788 memcpy(id.sha1, ie->blob_sha1,
4789 SHA1_DIGEST_LENGTH);
4790 fd = got_opentempfd();
4791 if (fd == -1) {
4792 err = got_error_from_errno("got_opentempfd");
4793 goto done;
4796 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4797 if (err)
4798 goto done;
4800 if (asprintf(&ondisk_path, "%s/%s",
4801 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4802 err = got_error_from_errno("asprintf");
4803 goto done;
4806 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4807 status == GOT_STATUS_CONFLICT)) {
4808 int is_bad_symlink = 0;
4809 err = create_patched_content(&path_content, 1, &id,
4810 ondisk_path, dirfd, de_name, ie->path, a->repo,
4811 a->patch_cb, a->patch_arg);
4812 if (err || path_content == NULL)
4813 break;
4814 if (te && S_ISLNK(te->mode)) {
4815 if (unlink(path_content) == -1) {
4816 err = got_error_from_errno2("unlink",
4817 path_content);
4818 break;
4820 err = install_symlink(&is_bad_symlink,
4821 a->worktree, ondisk_path, ie->path,
4822 blob, 0, 1, 0, 0, a->repo,
4823 a->progress_cb, a->progress_arg);
4824 } else {
4825 if (rename(path_content, ondisk_path) == -1) {
4826 err = got_error_from_errno3("rename",
4827 path_content, ondisk_path);
4828 goto done;
4831 } else {
4832 int is_bad_symlink = 0;
4833 if (te && S_ISLNK(te->mode)) {
4834 err = install_symlink(&is_bad_symlink,
4835 a->worktree, ondisk_path, ie->path,
4836 blob, 0, 1, 0, 0, a->repo,
4837 a->progress_cb, a->progress_arg);
4838 } else {
4839 err = install_blob(a->worktree, ondisk_path,
4840 ie->path,
4841 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4842 got_fileindex_perms_to_st(ie), blob,
4843 0, 1, 0, 0, a->repo,
4844 a->progress_cb, a->progress_arg);
4846 if (err)
4847 goto done;
4848 if (status == GOT_STATUS_DELETE ||
4849 status == GOT_STATUS_MODE_CHANGE) {
4850 err = got_fileindex_entry_update(ie,
4851 a->worktree->root_fd, relpath,
4852 blob->id.sha1,
4853 a->worktree->base_commit_id->sha1, 1);
4854 if (err)
4855 goto done;
4857 if (is_bad_symlink) {
4858 got_fileindex_entry_filetype_set(ie,
4859 GOT_FILEIDX_MODE_BAD_SYMLINK);
4862 break;
4864 default:
4865 break;
4867 done:
4868 free(ondisk_path);
4869 free(path_content);
4870 free(parent_path);
4871 free(tree_path);
4872 if (fd != -1 && close(fd) == -1 && err == NULL)
4873 err = got_error_from_errno("close");
4874 if (blob)
4875 got_object_blob_close(blob);
4876 if (tree)
4877 got_object_tree_close(tree);
4878 free(tree_id);
4879 if (base_commit)
4880 got_object_commit_close(base_commit);
4881 return err;
4884 const struct got_error *
4885 got_worktree_revert(struct got_worktree *worktree,
4886 struct got_pathlist_head *paths,
4887 got_worktree_checkout_cb progress_cb, void *progress_arg,
4888 got_worktree_patch_cb patch_cb, void *patch_arg,
4889 struct got_repository *repo)
4891 struct got_fileindex *fileindex = NULL;
4892 char *fileindex_path = NULL;
4893 const struct got_error *err = NULL, *unlockerr = NULL;
4894 const struct got_error *sync_err = NULL;
4895 struct got_pathlist_entry *pe;
4896 struct revert_file_args rfa;
4898 err = lock_worktree(worktree, LOCK_EX);
4899 if (err)
4900 return err;
4902 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4903 if (err)
4904 goto done;
4906 rfa.worktree = worktree;
4907 rfa.fileindex = fileindex;
4908 rfa.progress_cb = progress_cb;
4909 rfa.progress_arg = progress_arg;
4910 rfa.patch_cb = patch_cb;
4911 rfa.patch_arg = patch_arg;
4912 rfa.repo = repo;
4913 rfa.unlink_added_files = 0;
4914 TAILQ_FOREACH(pe, paths, entry) {
4915 err = worktree_status(worktree, pe->path, fileindex, repo,
4916 revert_file, &rfa, NULL, NULL, 1, 0);
4917 if (err)
4918 break;
4920 sync_err = sync_fileindex(fileindex, fileindex_path);
4921 if (sync_err && err == NULL)
4922 err = sync_err;
4923 done:
4924 free(fileindex_path);
4925 if (fileindex)
4926 got_fileindex_free(fileindex);
4927 unlockerr = lock_worktree(worktree, LOCK_SH);
4928 if (unlockerr && err == NULL)
4929 err = unlockerr;
4930 return err;
4933 static void
4934 free_commitable(struct got_commitable *ct)
4936 free(ct->path);
4937 free(ct->in_repo_path);
4938 free(ct->ondisk_path);
4939 free(ct->blob_id);
4940 free(ct->base_blob_id);
4941 free(ct->staged_blob_id);
4942 free(ct->base_commit_id);
4943 free(ct);
4946 struct collect_commitables_arg {
4947 struct got_pathlist_head *commitable_paths;
4948 struct got_repository *repo;
4949 struct got_worktree *worktree;
4950 struct got_fileindex *fileindex;
4951 int have_staged_files;
4952 int allow_bad_symlinks;
4955 static const struct got_error *
4956 collect_commitables(void *arg, unsigned char status,
4957 unsigned char staged_status, const char *relpath,
4958 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4959 struct got_object_id *commit_id, int dirfd, const char *de_name)
4961 struct collect_commitables_arg *a = arg;
4962 const struct got_error *err = NULL;
4963 struct got_commitable *ct = NULL;
4964 struct got_pathlist_entry *new = NULL;
4965 char *parent_path = NULL, *path = NULL;
4966 struct stat sb;
4968 if (a->have_staged_files) {
4969 if (staged_status != GOT_STATUS_MODIFY &&
4970 staged_status != GOT_STATUS_ADD &&
4971 staged_status != GOT_STATUS_DELETE)
4972 return NULL;
4973 } else {
4974 if (status == GOT_STATUS_CONFLICT)
4975 return got_error(GOT_ERR_COMMIT_CONFLICT);
4977 if (status != GOT_STATUS_MODIFY &&
4978 status != GOT_STATUS_MODE_CHANGE &&
4979 status != GOT_STATUS_ADD &&
4980 status != GOT_STATUS_DELETE)
4981 return NULL;
4984 if (asprintf(&path, "/%s", relpath) == -1) {
4985 err = got_error_from_errno("asprintf");
4986 goto done;
4988 if (strcmp(path, "/") == 0) {
4989 parent_path = strdup("");
4990 if (parent_path == NULL)
4991 return got_error_from_errno("strdup");
4992 } else {
4993 err = got_path_dirname(&parent_path, path);
4994 if (err)
4995 return err;
4998 ct = calloc(1, sizeof(*ct));
4999 if (ct == NULL) {
5000 err = got_error_from_errno("calloc");
5001 goto done;
5004 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5005 relpath) == -1) {
5006 err = got_error_from_errno("asprintf");
5007 goto done;
5010 if (staged_status == GOT_STATUS_ADD ||
5011 staged_status == GOT_STATUS_MODIFY) {
5012 struct got_fileindex_entry *ie;
5013 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5014 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5015 case GOT_FILEIDX_MODE_REGULAR_FILE:
5016 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5017 ct->mode = S_IFREG;
5018 break;
5019 case GOT_FILEIDX_MODE_SYMLINK:
5020 ct->mode = S_IFLNK;
5021 break;
5022 default:
5023 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5024 goto done;
5026 ct->mode |= got_fileindex_entry_perms_get(ie);
5027 } else if (status != GOT_STATUS_DELETE &&
5028 staged_status != GOT_STATUS_DELETE) {
5029 if (dirfd != -1) {
5030 if (fstatat(dirfd, de_name, &sb,
5031 AT_SYMLINK_NOFOLLOW) == -1) {
5032 err = got_error_from_errno2("fstatat",
5033 ct->ondisk_path);
5034 goto done;
5036 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5037 err = got_error_from_errno2("lstat", ct->ondisk_path);
5038 goto done;
5040 ct->mode = sb.st_mode;
5043 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5044 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5045 relpath) == -1) {
5046 err = got_error_from_errno("asprintf");
5047 goto done;
5050 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5051 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5052 int is_bad_symlink;
5053 char target_path[PATH_MAX];
5054 ssize_t target_len;
5055 target_len = readlink(ct->ondisk_path, target_path,
5056 sizeof(target_path));
5057 if (target_len == -1) {
5058 err = got_error_from_errno2("readlink",
5059 ct->ondisk_path);
5060 goto done;
5062 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5063 target_len, ct->ondisk_path, a->worktree->root_path);
5064 if (err)
5065 goto done;
5066 if (is_bad_symlink) {
5067 err = got_error_path(ct->ondisk_path,
5068 GOT_ERR_BAD_SYMLINK);
5069 goto done;
5074 ct->status = status;
5075 ct->staged_status = staged_status;
5076 ct->blob_id = NULL; /* will be filled in when blob gets created */
5077 if (ct->status != GOT_STATUS_ADD &&
5078 ct->staged_status != GOT_STATUS_ADD) {
5079 ct->base_blob_id = got_object_id_dup(blob_id);
5080 if (ct->base_blob_id == NULL) {
5081 err = got_error_from_errno("got_object_id_dup");
5082 goto done;
5084 ct->base_commit_id = got_object_id_dup(commit_id);
5085 if (ct->base_commit_id == NULL) {
5086 err = got_error_from_errno("got_object_id_dup");
5087 goto done;
5090 if (ct->staged_status == GOT_STATUS_ADD ||
5091 ct->staged_status == GOT_STATUS_MODIFY) {
5092 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5093 if (ct->staged_blob_id == NULL) {
5094 err = got_error_from_errno("got_object_id_dup");
5095 goto done;
5098 ct->path = strdup(path);
5099 if (ct->path == NULL) {
5100 err = got_error_from_errno("strdup");
5101 goto done;
5103 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5104 done:
5105 if (ct && (err || new == NULL))
5106 free_commitable(ct);
5107 free(parent_path);
5108 free(path);
5109 return err;
5112 static const struct got_error *write_tree(struct got_object_id **, int *,
5113 struct got_tree_object *, const char *, struct got_pathlist_head *,
5114 got_worktree_status_cb status_cb, void *status_arg,
5115 struct got_repository *);
5117 static const struct got_error *
5118 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5119 struct got_tree_entry *te, const char *parent_path,
5120 struct got_pathlist_head *commitable_paths,
5121 got_worktree_status_cb status_cb, void *status_arg,
5122 struct got_repository *repo)
5124 const struct got_error *err = NULL;
5125 struct got_tree_object *subtree;
5126 char *subpath;
5128 if (asprintf(&subpath, "%s%s%s", parent_path,
5129 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5130 return got_error_from_errno("asprintf");
5132 err = got_object_open_as_tree(&subtree, repo, &te->id);
5133 if (err)
5134 return err;
5136 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5137 commitable_paths, status_cb, status_arg, repo);
5138 got_object_tree_close(subtree);
5139 free(subpath);
5140 return err;
5143 static const struct got_error *
5144 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5146 const struct got_error *err = NULL;
5147 char *ct_parent_path = NULL;
5149 *match = 0;
5151 if (strchr(ct->in_repo_path, '/') == NULL) {
5152 *match = got_path_is_root_dir(path);
5153 return NULL;
5156 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5157 if (err)
5158 return err;
5159 *match = (strcmp(path, ct_parent_path) == 0);
5160 free(ct_parent_path);
5161 return err;
5164 static mode_t
5165 get_ct_file_mode(struct got_commitable *ct)
5167 if (S_ISLNK(ct->mode))
5168 return S_IFLNK;
5170 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5173 static const struct got_error *
5174 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5175 struct got_tree_entry *te, struct got_commitable *ct)
5177 const struct got_error *err = NULL;
5179 *new_te = NULL;
5181 err = got_object_tree_entry_dup(new_te, te);
5182 if (err)
5183 goto done;
5185 (*new_te)->mode = get_ct_file_mode(ct);
5187 if (ct->staged_status == GOT_STATUS_MODIFY)
5188 memcpy(&(*new_te)->id, ct->staged_blob_id,
5189 sizeof((*new_te)->id));
5190 else
5191 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5192 done:
5193 if (err && *new_te) {
5194 free(*new_te);
5195 *new_te = NULL;
5197 return err;
5200 static const struct got_error *
5201 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5202 struct got_commitable *ct)
5204 const struct got_error *err = NULL;
5205 char *ct_name = NULL;
5207 *new_te = NULL;
5209 *new_te = calloc(1, sizeof(**new_te));
5210 if (*new_te == NULL)
5211 return got_error_from_errno("calloc");
5213 err = got_path_basename(&ct_name, ct->path);
5214 if (err)
5215 goto done;
5216 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5217 sizeof((*new_te)->name)) {
5218 err = got_error(GOT_ERR_NO_SPACE);
5219 goto done;
5222 (*new_te)->mode = get_ct_file_mode(ct);
5224 if (ct->staged_status == GOT_STATUS_ADD)
5225 memcpy(&(*new_te)->id, ct->staged_blob_id,
5226 sizeof((*new_te)->id));
5227 else
5228 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5229 done:
5230 free(ct_name);
5231 if (err && *new_te) {
5232 free(*new_te);
5233 *new_te = NULL;
5235 return err;
5238 static const struct got_error *
5239 insert_tree_entry(struct got_tree_entry *new_te,
5240 struct got_pathlist_head *paths)
5242 const struct got_error *err = NULL;
5243 struct got_pathlist_entry *new_pe;
5245 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5246 if (err)
5247 return err;
5248 if (new_pe == NULL)
5249 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5250 return NULL;
5253 static const struct got_error *
5254 report_ct_status(struct got_commitable *ct,
5255 got_worktree_status_cb status_cb, void *status_arg)
5257 const char *ct_path = ct->path;
5258 unsigned char status;
5260 if (status_cb == NULL) /* no commit progress output desired */
5261 return NULL;
5263 while (ct_path[0] == '/')
5264 ct_path++;
5266 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5267 status = ct->staged_status;
5268 else
5269 status = ct->status;
5271 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5272 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5275 static const struct got_error *
5276 match_modified_subtree(int *modified, struct got_tree_entry *te,
5277 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5279 const struct got_error *err = NULL;
5280 struct got_pathlist_entry *pe;
5281 char *te_path;
5283 *modified = 0;
5285 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5286 got_path_is_root_dir(base_tree_path) ? "" : "/",
5287 te->name) == -1)
5288 return got_error_from_errno("asprintf");
5290 TAILQ_FOREACH(pe, commitable_paths, entry) {
5291 struct got_commitable *ct = pe->data;
5292 *modified = got_path_is_child(ct->in_repo_path, te_path,
5293 strlen(te_path));
5294 if (*modified)
5295 break;
5298 free(te_path);
5299 return err;
5302 static const struct got_error *
5303 match_deleted_or_modified_ct(struct got_commitable **ctp,
5304 struct got_tree_entry *te, const char *base_tree_path,
5305 struct got_pathlist_head *commitable_paths)
5307 const struct got_error *err = NULL;
5308 struct got_pathlist_entry *pe;
5310 *ctp = NULL;
5312 TAILQ_FOREACH(pe, commitable_paths, entry) {
5313 struct got_commitable *ct = pe->data;
5314 char *ct_name = NULL;
5315 int path_matches;
5317 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5318 if (ct->status != GOT_STATUS_MODIFY &&
5319 ct->status != GOT_STATUS_MODE_CHANGE &&
5320 ct->status != GOT_STATUS_DELETE)
5321 continue;
5322 } else {
5323 if (ct->staged_status != GOT_STATUS_MODIFY &&
5324 ct->staged_status != GOT_STATUS_DELETE)
5325 continue;
5328 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5329 continue;
5331 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5332 if (err)
5333 return err;
5334 if (!path_matches)
5335 continue;
5337 err = got_path_basename(&ct_name, pe->path);
5338 if (err)
5339 return err;
5341 if (strcmp(te->name, ct_name) != 0) {
5342 free(ct_name);
5343 continue;
5345 free(ct_name);
5347 *ctp = ct;
5348 break;
5351 return err;
5354 static const struct got_error *
5355 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5356 const char *child_path, const char *path_base_tree,
5357 struct got_pathlist_head *commitable_paths,
5358 got_worktree_status_cb status_cb, void *status_arg,
5359 struct got_repository *repo)
5361 const struct got_error *err = NULL;
5362 struct got_tree_entry *new_te;
5363 char *subtree_path;
5364 struct got_object_id *id = NULL;
5365 int nentries;
5367 *new_tep = NULL;
5369 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5370 got_path_is_root_dir(path_base_tree) ? "" : "/",
5371 child_path) == -1)
5372 return got_error_from_errno("asprintf");
5374 new_te = calloc(1, sizeof(*new_te));
5375 if (new_te == NULL)
5376 return got_error_from_errno("calloc");
5377 new_te->mode = S_IFDIR;
5379 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5380 sizeof(new_te->name)) {
5381 err = got_error(GOT_ERR_NO_SPACE);
5382 goto done;
5384 err = write_tree(&id, &nentries, NULL, subtree_path,
5385 commitable_paths, status_cb, status_arg, repo);
5386 if (err) {
5387 free(new_te);
5388 goto done;
5390 memcpy(&new_te->id, id, sizeof(new_te->id));
5391 done:
5392 free(id);
5393 free(subtree_path);
5394 if (err == NULL)
5395 *new_tep = new_te;
5396 return err;
5399 static const struct got_error *
5400 write_tree(struct got_object_id **new_tree_id, int *nentries,
5401 struct got_tree_object *base_tree, const char *path_base_tree,
5402 struct got_pathlist_head *commitable_paths,
5403 got_worktree_status_cb status_cb, void *status_arg,
5404 struct got_repository *repo)
5406 const struct got_error *err = NULL;
5407 struct got_pathlist_head paths;
5408 struct got_tree_entry *te, *new_te = NULL;
5409 struct got_pathlist_entry *pe;
5411 TAILQ_INIT(&paths);
5412 *nentries = 0;
5414 /* Insert, and recurse into, newly added entries first. */
5415 TAILQ_FOREACH(pe, commitable_paths, entry) {
5416 struct got_commitable *ct = pe->data;
5417 char *child_path = NULL, *slash;
5419 if ((ct->status != GOT_STATUS_ADD &&
5420 ct->staged_status != GOT_STATUS_ADD) ||
5421 (ct->flags & GOT_COMMITABLE_ADDED))
5422 continue;
5424 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5425 strlen(path_base_tree)))
5426 continue;
5428 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5429 ct->in_repo_path);
5430 if (err)
5431 goto done;
5433 slash = strchr(child_path, '/');
5434 if (slash == NULL) {
5435 err = alloc_added_blob_tree_entry(&new_te, ct);
5436 if (err)
5437 goto done;
5438 err = report_ct_status(ct, status_cb, status_arg);
5439 if (err)
5440 goto done;
5441 ct->flags |= GOT_COMMITABLE_ADDED;
5442 err = insert_tree_entry(new_te, &paths);
5443 if (err)
5444 goto done;
5445 (*nentries)++;
5446 } else {
5447 *slash = '\0'; /* trim trailing path components */
5448 if (base_tree == NULL ||
5449 got_object_tree_find_entry(base_tree, child_path)
5450 == NULL) {
5451 err = make_subtree_for_added_blob(&new_te,
5452 child_path, path_base_tree,
5453 commitable_paths, status_cb, status_arg,
5454 repo);
5455 if (err)
5456 goto done;
5457 err = insert_tree_entry(new_te, &paths);
5458 if (err)
5459 goto done;
5460 (*nentries)++;
5465 if (base_tree) {
5466 int i, nbase_entries;
5467 /* Handle modified and deleted entries. */
5468 nbase_entries = got_object_tree_get_nentries(base_tree);
5469 for (i = 0; i < nbase_entries; i++) {
5470 struct got_commitable *ct = NULL;
5472 te = got_object_tree_get_entry(base_tree, i);
5473 if (got_object_tree_entry_is_submodule(te)) {
5474 /* Entry is a submodule; just copy it. */
5475 err = got_object_tree_entry_dup(&new_te, te);
5476 if (err)
5477 goto done;
5478 err = insert_tree_entry(new_te, &paths);
5479 if (err)
5480 goto done;
5481 (*nentries)++;
5482 continue;
5485 if (S_ISDIR(te->mode)) {
5486 int modified;
5487 err = got_object_tree_entry_dup(&new_te, te);
5488 if (err)
5489 goto done;
5490 err = match_modified_subtree(&modified, te,
5491 path_base_tree, commitable_paths);
5492 if (err)
5493 goto done;
5494 /* Avoid recursion into unmodified subtrees. */
5495 if (modified) {
5496 struct got_object_id *new_id;
5497 int nsubentries;
5498 err = write_subtree(&new_id,
5499 &nsubentries, te,
5500 path_base_tree, commitable_paths,
5501 status_cb, status_arg, repo);
5502 if (err)
5503 goto done;
5504 if (nsubentries == 0) {
5505 /* All entries were deleted. */
5506 free(new_id);
5507 continue;
5509 memcpy(&new_te->id, new_id,
5510 sizeof(new_te->id));
5511 free(new_id);
5513 err = insert_tree_entry(new_te, &paths);
5514 if (err)
5515 goto done;
5516 (*nentries)++;
5517 continue;
5520 err = match_deleted_or_modified_ct(&ct, te,
5521 path_base_tree, commitable_paths);
5522 if (err)
5523 goto done;
5524 if (ct) {
5525 /* NB: Deleted entries get dropped here. */
5526 if (ct->status == GOT_STATUS_MODIFY ||
5527 ct->status == GOT_STATUS_MODE_CHANGE ||
5528 ct->staged_status == GOT_STATUS_MODIFY) {
5529 err = alloc_modified_blob_tree_entry(
5530 &new_te, te, ct);
5531 if (err)
5532 goto done;
5533 err = insert_tree_entry(new_te, &paths);
5534 if (err)
5535 goto done;
5536 (*nentries)++;
5538 err = report_ct_status(ct, status_cb,
5539 status_arg);
5540 if (err)
5541 goto done;
5542 } else {
5543 /* Entry is unchanged; just copy it. */
5544 err = got_object_tree_entry_dup(&new_te, te);
5545 if (err)
5546 goto done;
5547 err = insert_tree_entry(new_te, &paths);
5548 if (err)
5549 goto done;
5550 (*nentries)++;
5555 /* Write new list of entries; deleted entries have been dropped. */
5556 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5557 done:
5558 got_pathlist_free(&paths);
5559 return err;
5562 static const struct got_error *
5563 update_fileindex_after_commit(struct got_worktree *worktree,
5564 struct got_pathlist_head *commitable_paths,
5565 struct got_object_id *new_base_commit_id,
5566 struct got_fileindex *fileindex, int have_staged_files)
5568 const struct got_error *err = NULL;
5569 struct got_pathlist_entry *pe;
5570 char *relpath = NULL;
5572 TAILQ_FOREACH(pe, commitable_paths, entry) {
5573 struct got_fileindex_entry *ie;
5574 struct got_commitable *ct = pe->data;
5576 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5578 err = got_path_skip_common_ancestor(&relpath,
5579 worktree->root_path, ct->ondisk_path);
5580 if (err)
5581 goto done;
5583 if (ie) {
5584 if (ct->status == GOT_STATUS_DELETE ||
5585 ct->staged_status == GOT_STATUS_DELETE) {
5586 got_fileindex_entry_remove(fileindex, ie);
5587 } else if (ct->staged_status == GOT_STATUS_ADD ||
5588 ct->staged_status == GOT_STATUS_MODIFY) {
5589 got_fileindex_entry_stage_set(ie,
5590 GOT_FILEIDX_STAGE_NONE);
5591 got_fileindex_entry_staged_filetype_set(ie, 0);
5593 err = got_fileindex_entry_update(ie,
5594 worktree->root_fd, relpath,
5595 ct->staged_blob_id->sha1,
5596 new_base_commit_id->sha1,
5597 !have_staged_files);
5598 } else
5599 err = got_fileindex_entry_update(ie,
5600 worktree->root_fd, relpath,
5601 ct->blob_id->sha1,
5602 new_base_commit_id->sha1,
5603 !have_staged_files);
5604 } else {
5605 err = got_fileindex_entry_alloc(&ie, pe->path);
5606 if (err)
5607 goto done;
5608 err = got_fileindex_entry_update(ie,
5609 worktree->root_fd, relpath, ct->blob_id->sha1,
5610 new_base_commit_id->sha1, 1);
5611 if (err) {
5612 got_fileindex_entry_free(ie);
5613 goto done;
5615 err = got_fileindex_entry_add(fileindex, ie);
5616 if (err) {
5617 got_fileindex_entry_free(ie);
5618 goto done;
5621 free(relpath);
5622 relpath = NULL;
5624 done:
5625 free(relpath);
5626 return err;
5630 static const struct got_error *
5631 check_out_of_date(const char *in_repo_path, unsigned char status,
5632 unsigned char staged_status, struct got_object_id *base_blob_id,
5633 struct got_object_id *base_commit_id,
5634 struct got_object_id *head_commit_id, struct got_repository *repo,
5635 int ood_errcode)
5637 const struct got_error *err = NULL;
5638 struct got_commit_object *commit = NULL;
5639 struct got_object_id *id = NULL;
5641 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5642 /* Trivial case: base commit == head commit */
5643 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5644 return NULL;
5646 * Ensure file content which local changes were based
5647 * on matches file content in the branch head.
5649 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5650 if (err)
5651 goto done;
5652 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5653 if (err) {
5654 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5655 err = got_error(ood_errcode);
5656 goto done;
5657 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5658 err = got_error(ood_errcode);
5659 } else {
5660 /* Require that added files don't exist in the branch head. */
5661 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5662 if (err)
5663 goto done;
5664 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5665 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5666 goto done;
5667 err = id ? got_error(ood_errcode) : NULL;
5669 done:
5670 free(id);
5671 if (commit)
5672 got_object_commit_close(commit);
5673 return err;
5676 static const struct got_error *
5677 commit_worktree(struct got_object_id **new_commit_id,
5678 struct got_pathlist_head *commitable_paths,
5679 struct got_object_id *head_commit_id,
5680 struct got_object_id *parent_id2,
5681 struct got_worktree *worktree,
5682 const char *author, const char *committer,
5683 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5684 got_worktree_status_cb status_cb, void *status_arg,
5685 struct got_repository *repo)
5687 const struct got_error *err = NULL, *unlockerr = NULL;
5688 struct got_pathlist_entry *pe;
5689 const char *head_ref_name = NULL;
5690 struct got_commit_object *head_commit = NULL;
5691 struct got_reference *head_ref2 = NULL;
5692 struct got_object_id *head_commit_id2 = NULL;
5693 struct got_tree_object *head_tree = NULL;
5694 struct got_object_id *new_tree_id = NULL;
5695 int nentries, nparents = 0;
5696 struct got_object_id_queue parent_ids;
5697 struct got_object_qid *pid = NULL;
5698 char *logmsg = NULL;
5699 time_t timestamp;
5701 *new_commit_id = NULL;
5703 STAILQ_INIT(&parent_ids);
5705 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5706 if (err)
5707 goto done;
5709 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5710 if (err)
5711 goto done;
5713 if (commit_msg_cb != NULL) {
5714 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5715 if (err)
5716 goto done;
5719 if (logmsg == NULL || strlen(logmsg) == 0) {
5720 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5721 goto done;
5724 /* Create blobs from added and modified files and record their IDs. */
5725 TAILQ_FOREACH(pe, commitable_paths, entry) {
5726 struct got_commitable *ct = pe->data;
5727 char *ondisk_path;
5729 /* Blobs for staged files already exist. */
5730 if (ct->staged_status == GOT_STATUS_ADD ||
5731 ct->staged_status == GOT_STATUS_MODIFY)
5732 continue;
5734 if (ct->status != GOT_STATUS_ADD &&
5735 ct->status != GOT_STATUS_MODIFY &&
5736 ct->status != GOT_STATUS_MODE_CHANGE)
5737 continue;
5739 if (asprintf(&ondisk_path, "%s/%s",
5740 worktree->root_path, pe->path) == -1) {
5741 err = got_error_from_errno("asprintf");
5742 goto done;
5744 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5745 free(ondisk_path);
5746 if (err)
5747 goto done;
5750 /* Recursively write new tree objects. */
5751 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5752 commitable_paths, status_cb, status_arg, repo);
5753 if (err)
5754 goto done;
5756 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5757 if (err)
5758 goto done;
5759 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5760 nparents++;
5761 if (parent_id2) {
5762 err = got_object_qid_alloc(&pid, parent_id2);
5763 if (err)
5764 goto done;
5765 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5766 nparents++;
5768 timestamp = time(NULL);
5769 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5770 nparents, author, timestamp, committer, timestamp, logmsg, repo);
5771 if (logmsg != NULL)
5772 free(logmsg);
5773 if (err)
5774 goto done;
5776 /* Check if a concurrent commit to our branch has occurred. */
5777 head_ref_name = got_worktree_get_head_ref_name(worktree);
5778 if (head_ref_name == NULL) {
5779 err = got_error_from_errno("got_worktree_get_head_ref_name");
5780 goto done;
5782 /* Lock the reference here to prevent concurrent modification. */
5783 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5784 if (err)
5785 goto done;
5786 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5787 if (err)
5788 goto done;
5789 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5790 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5791 goto done;
5793 /* Update branch head in repository. */
5794 err = got_ref_change_ref(head_ref2, *new_commit_id);
5795 if (err)
5796 goto done;
5797 err = got_ref_write(head_ref2, repo);
5798 if (err)
5799 goto done;
5801 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5802 if (err)
5803 goto done;
5805 err = ref_base_commit(worktree, repo);
5806 if (err)
5807 goto done;
5808 done:
5809 got_object_id_queue_free(&parent_ids);
5810 if (head_tree)
5811 got_object_tree_close(head_tree);
5812 if (head_commit)
5813 got_object_commit_close(head_commit);
5814 free(head_commit_id2);
5815 if (head_ref2) {
5816 unlockerr = got_ref_unlock(head_ref2);
5817 if (unlockerr && err == NULL)
5818 err = unlockerr;
5819 got_ref_close(head_ref2);
5821 return err;
5824 static const struct got_error *
5825 check_path_is_commitable(const char *path,
5826 struct got_pathlist_head *commitable_paths)
5828 struct got_pathlist_entry *cpe = NULL;
5829 size_t path_len = strlen(path);
5831 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5832 struct got_commitable *ct = cpe->data;
5833 const char *ct_path = ct->path;
5835 while (ct_path[0] == '/')
5836 ct_path++;
5838 if (strcmp(path, ct_path) == 0 ||
5839 got_path_is_child(ct_path, path, path_len))
5840 break;
5843 if (cpe == NULL)
5844 return got_error_path(path, GOT_ERR_BAD_PATH);
5846 return NULL;
5849 static const struct got_error *
5850 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5852 int *have_staged_files = arg;
5854 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5855 *have_staged_files = 1;
5856 return got_error(GOT_ERR_CANCELLED);
5859 return NULL;
5862 static const struct got_error *
5863 check_non_staged_files(struct got_fileindex *fileindex,
5864 struct got_pathlist_head *paths)
5866 struct got_pathlist_entry *pe;
5867 struct got_fileindex_entry *ie;
5869 TAILQ_FOREACH(pe, paths, entry) {
5870 if (pe->path[0] == '\0')
5871 continue;
5872 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5873 if (ie == NULL)
5874 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5875 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5876 return got_error_path(pe->path,
5877 GOT_ERR_FILE_NOT_STAGED);
5880 return NULL;
5883 const struct got_error *
5884 got_worktree_commit(struct got_object_id **new_commit_id,
5885 struct got_worktree *worktree, struct got_pathlist_head *paths,
5886 const char *author, const char *committer, int allow_bad_symlinks,
5887 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5888 got_worktree_status_cb status_cb, void *status_arg,
5889 struct got_repository *repo)
5891 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5892 struct got_fileindex *fileindex = NULL;
5893 char *fileindex_path = NULL;
5894 struct got_pathlist_head commitable_paths;
5895 struct collect_commitables_arg cc_arg;
5896 struct got_pathlist_entry *pe;
5897 struct got_reference *head_ref = NULL;
5898 struct got_object_id *head_commit_id = NULL;
5899 int have_staged_files = 0;
5901 *new_commit_id = NULL;
5903 TAILQ_INIT(&commitable_paths);
5905 err = lock_worktree(worktree, LOCK_EX);
5906 if (err)
5907 goto done;
5909 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5910 if (err)
5911 goto done;
5913 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5914 if (err)
5915 goto done;
5917 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5918 if (err)
5919 goto done;
5921 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5922 &have_staged_files);
5923 if (err && err->code != GOT_ERR_CANCELLED)
5924 goto done;
5925 if (have_staged_files) {
5926 err = check_non_staged_files(fileindex, paths);
5927 if (err)
5928 goto done;
5931 cc_arg.commitable_paths = &commitable_paths;
5932 cc_arg.worktree = worktree;
5933 cc_arg.fileindex = fileindex;
5934 cc_arg.repo = repo;
5935 cc_arg.have_staged_files = have_staged_files;
5936 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5937 TAILQ_FOREACH(pe, paths, entry) {
5938 err = worktree_status(worktree, pe->path, fileindex, repo,
5939 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5940 if (err)
5941 goto done;
5944 if (TAILQ_EMPTY(&commitable_paths)) {
5945 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5946 goto done;
5949 TAILQ_FOREACH(pe, paths, entry) {
5950 err = check_path_is_commitable(pe->path, &commitable_paths);
5951 if (err)
5952 goto done;
5955 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5956 struct got_commitable *ct = pe->data;
5957 const char *ct_path = ct->in_repo_path;
5959 while (ct_path[0] == '/')
5960 ct_path++;
5961 err = check_out_of_date(ct_path, ct->status,
5962 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5963 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5964 if (err)
5965 goto done;
5969 err = commit_worktree(new_commit_id, &commitable_paths,
5970 head_commit_id, NULL, worktree, author, committer,
5971 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5972 if (err)
5973 goto done;
5975 err = update_fileindex_after_commit(worktree, &commitable_paths,
5976 *new_commit_id, fileindex, have_staged_files);
5977 sync_err = sync_fileindex(fileindex, fileindex_path);
5978 if (sync_err && err == NULL)
5979 err = sync_err;
5980 done:
5981 if (fileindex)
5982 got_fileindex_free(fileindex);
5983 free(fileindex_path);
5984 unlockerr = lock_worktree(worktree, LOCK_SH);
5985 if (unlockerr && err == NULL)
5986 err = unlockerr;
5987 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5988 struct got_commitable *ct = pe->data;
5989 free_commitable(ct);
5991 got_pathlist_free(&commitable_paths);
5992 return err;
5995 const char *
5996 got_commitable_get_path(struct got_commitable *ct)
5998 return ct->path;
6001 unsigned int
6002 got_commitable_get_status(struct got_commitable *ct)
6004 return ct->status;
6007 struct check_rebase_ok_arg {
6008 struct got_worktree *worktree;
6009 struct got_repository *repo;
6012 static const struct got_error *
6013 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6015 const struct got_error *err = NULL;
6016 struct check_rebase_ok_arg *a = arg;
6017 unsigned char status;
6018 struct stat sb;
6019 char *ondisk_path;
6021 /* Reject rebase of a work tree with mixed base commits. */
6022 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6023 SHA1_DIGEST_LENGTH))
6024 return got_error(GOT_ERR_MIXED_COMMITS);
6026 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6027 == -1)
6028 return got_error_from_errno("asprintf");
6030 /* Reject rebase of a work tree with modified or staged files. */
6031 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6032 free(ondisk_path);
6033 if (err)
6034 return err;
6036 if (status != GOT_STATUS_NO_CHANGE)
6037 return got_error(GOT_ERR_MODIFIED);
6038 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6039 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6041 return NULL;
6044 const struct got_error *
6045 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6046 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6047 struct got_worktree *worktree, struct got_reference *branch,
6048 struct got_repository *repo)
6050 const struct got_error *err = NULL;
6051 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6052 char *branch_ref_name = NULL;
6053 char *fileindex_path = NULL;
6054 struct check_rebase_ok_arg ok_arg;
6055 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6056 struct got_object_id *wt_branch_tip = NULL;
6058 *new_base_branch_ref = NULL;
6059 *tmp_branch = NULL;
6060 *fileindex = NULL;
6062 err = lock_worktree(worktree, LOCK_EX);
6063 if (err)
6064 return err;
6066 err = open_fileindex(fileindex, &fileindex_path, worktree);
6067 if (err)
6068 goto done;
6070 ok_arg.worktree = worktree;
6071 ok_arg.repo = repo;
6072 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6073 &ok_arg);
6074 if (err)
6075 goto done;
6077 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6078 if (err)
6079 goto done;
6081 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6082 if (err)
6083 goto done;
6085 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6086 if (err)
6087 goto done;
6089 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6090 0);
6091 if (err)
6092 goto done;
6094 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6095 if (err)
6096 goto done;
6097 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6098 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6099 goto done;
6102 err = got_ref_alloc_symref(new_base_branch_ref,
6103 new_base_branch_ref_name, wt_branch);
6104 if (err)
6105 goto done;
6106 err = got_ref_write(*new_base_branch_ref, repo);
6107 if (err)
6108 goto done;
6110 /* TODO Lock original branch's ref while rebasing? */
6112 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6113 if (err)
6114 goto done;
6116 err = got_ref_write(branch_ref, repo);
6117 if (err)
6118 goto done;
6120 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6121 worktree->base_commit_id);
6122 if (err)
6123 goto done;
6124 err = got_ref_write(*tmp_branch, repo);
6125 if (err)
6126 goto done;
6128 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6129 if (err)
6130 goto done;
6131 done:
6132 free(fileindex_path);
6133 free(tmp_branch_name);
6134 free(new_base_branch_ref_name);
6135 free(branch_ref_name);
6136 if (branch_ref)
6137 got_ref_close(branch_ref);
6138 if (wt_branch)
6139 got_ref_close(wt_branch);
6140 free(wt_branch_tip);
6141 if (err) {
6142 if (*new_base_branch_ref) {
6143 got_ref_close(*new_base_branch_ref);
6144 *new_base_branch_ref = NULL;
6146 if (*tmp_branch) {
6147 got_ref_close(*tmp_branch);
6148 *tmp_branch = NULL;
6150 if (*fileindex) {
6151 got_fileindex_free(*fileindex);
6152 *fileindex = NULL;
6154 lock_worktree(worktree, LOCK_SH);
6156 return err;
6159 const struct got_error *
6160 got_worktree_rebase_continue(struct got_object_id **commit_id,
6161 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6162 struct got_reference **branch, struct got_fileindex **fileindex,
6163 struct got_worktree *worktree, struct got_repository *repo)
6165 const struct got_error *err;
6166 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6167 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6168 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6169 char *fileindex_path = NULL;
6170 int have_staged_files = 0;
6172 *commit_id = NULL;
6173 *new_base_branch = NULL;
6174 *tmp_branch = NULL;
6175 *branch = NULL;
6176 *fileindex = NULL;
6178 err = lock_worktree(worktree, LOCK_EX);
6179 if (err)
6180 return err;
6182 err = open_fileindex(fileindex, &fileindex_path, worktree);
6183 if (err)
6184 goto done;
6186 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6187 &have_staged_files);
6188 if (err && err->code != GOT_ERR_CANCELLED)
6189 goto done;
6190 if (have_staged_files) {
6191 err = got_error(GOT_ERR_STAGED_PATHS);
6192 goto done;
6195 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6196 if (err)
6197 goto done;
6199 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6200 if (err)
6201 goto done;
6203 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6204 if (err)
6205 goto done;
6207 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6208 if (err)
6209 goto done;
6211 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6212 if (err)
6213 goto done;
6215 err = got_ref_open(branch, repo,
6216 got_ref_get_symref_target(branch_ref), 0);
6217 if (err)
6218 goto done;
6220 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6221 if (err)
6222 goto done;
6224 err = got_ref_resolve(commit_id, repo, commit_ref);
6225 if (err)
6226 goto done;
6228 err = got_ref_open(new_base_branch, repo,
6229 new_base_branch_ref_name, 0);
6230 if (err)
6231 goto done;
6233 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6234 if (err)
6235 goto done;
6236 done:
6237 free(commit_ref_name);
6238 free(branch_ref_name);
6239 free(fileindex_path);
6240 if (commit_ref)
6241 got_ref_close(commit_ref);
6242 if (branch_ref)
6243 got_ref_close(branch_ref);
6244 if (err) {
6245 free(*commit_id);
6246 *commit_id = NULL;
6247 if (*tmp_branch) {
6248 got_ref_close(*tmp_branch);
6249 *tmp_branch = NULL;
6251 if (*new_base_branch) {
6252 got_ref_close(*new_base_branch);
6253 *new_base_branch = NULL;
6255 if (*branch) {
6256 got_ref_close(*branch);
6257 *branch = NULL;
6259 if (*fileindex) {
6260 got_fileindex_free(*fileindex);
6261 *fileindex = NULL;
6263 lock_worktree(worktree, LOCK_SH);
6265 return err;
6268 const struct got_error *
6269 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6271 const struct got_error *err;
6272 char *tmp_branch_name = NULL;
6274 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6275 if (err)
6276 return err;
6278 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6279 free(tmp_branch_name);
6280 return NULL;
6283 static const struct got_error *
6284 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6285 char **logmsg, void *arg)
6287 *logmsg = arg;
6288 return NULL;
6291 static const struct got_error *
6292 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6293 const char *path, struct got_object_id *blob_id,
6294 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6295 int dirfd, const char *de_name)
6297 return NULL;
6300 struct collect_merged_paths_arg {
6301 got_worktree_checkout_cb progress_cb;
6302 void *progress_arg;
6303 struct got_pathlist_head *merged_paths;
6306 static const struct got_error *
6307 collect_merged_paths(void *arg, unsigned char status, const char *path)
6309 const struct got_error *err;
6310 struct collect_merged_paths_arg *a = arg;
6311 char *p;
6312 struct got_pathlist_entry *new;
6314 err = (*a->progress_cb)(a->progress_arg, status, path);
6315 if (err)
6316 return err;
6318 if (status != GOT_STATUS_MERGE &&
6319 status != GOT_STATUS_ADD &&
6320 status != GOT_STATUS_DELETE &&
6321 status != GOT_STATUS_CONFLICT)
6322 return NULL;
6324 p = strdup(path);
6325 if (p == NULL)
6326 return got_error_from_errno("strdup");
6328 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6329 if (err || new == NULL)
6330 free(p);
6331 return err;
6334 void
6335 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6337 struct got_pathlist_entry *pe;
6339 TAILQ_FOREACH(pe, merged_paths, entry)
6340 free((char *)pe->path);
6342 got_pathlist_free(merged_paths);
6345 static const struct got_error *
6346 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6347 int is_rebase, struct got_repository *repo)
6349 const struct got_error *err;
6350 struct got_reference *commit_ref = NULL;
6352 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6353 if (err) {
6354 if (err->code != GOT_ERR_NOT_REF)
6355 goto done;
6356 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6357 if (err)
6358 goto done;
6359 err = got_ref_write(commit_ref, repo);
6360 if (err)
6361 goto done;
6362 } else if (is_rebase) {
6363 struct got_object_id *stored_id;
6364 int cmp;
6366 err = got_ref_resolve(&stored_id, repo, commit_ref);
6367 if (err)
6368 goto done;
6369 cmp = got_object_id_cmp(commit_id, stored_id);
6370 free(stored_id);
6371 if (cmp != 0) {
6372 err = got_error(GOT_ERR_REBASE_COMMITID);
6373 goto done;
6376 done:
6377 if (commit_ref)
6378 got_ref_close(commit_ref);
6379 return err;
6382 static const struct got_error *
6383 rebase_merge_files(struct got_pathlist_head *merged_paths,
6384 const char *commit_ref_name, struct got_worktree *worktree,
6385 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6386 struct got_object_id *commit_id, struct got_repository *repo,
6387 got_worktree_checkout_cb progress_cb, void *progress_arg,
6388 got_cancel_cb cancel_cb, void *cancel_arg)
6390 const struct got_error *err;
6391 struct got_reference *commit_ref = NULL;
6392 struct collect_merged_paths_arg cmp_arg;
6393 char *fileindex_path;
6395 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6397 err = get_fileindex_path(&fileindex_path, worktree);
6398 if (err)
6399 return err;
6401 cmp_arg.progress_cb = progress_cb;
6402 cmp_arg.progress_arg = progress_arg;
6403 cmp_arg.merged_paths = merged_paths;
6404 err = merge_files(worktree, fileindex, fileindex_path,
6405 parent_commit_id, commit_id, repo, collect_merged_paths,
6406 &cmp_arg, cancel_cb, cancel_arg);
6407 if (commit_ref)
6408 got_ref_close(commit_ref);
6409 return err;
6412 const struct got_error *
6413 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6414 struct got_worktree *worktree, struct got_fileindex *fileindex,
6415 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6416 struct got_repository *repo,
6417 got_worktree_checkout_cb progress_cb, void *progress_arg,
6418 got_cancel_cb cancel_cb, void *cancel_arg)
6420 const struct got_error *err;
6421 char *commit_ref_name;
6423 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6424 if (err)
6425 return err;
6427 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6428 if (err)
6429 goto done;
6431 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6432 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6433 progress_arg, cancel_cb, cancel_arg);
6434 done:
6435 free(commit_ref_name);
6436 return err;
6439 const struct got_error *
6440 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6441 struct got_worktree *worktree, struct got_fileindex *fileindex,
6442 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6443 struct got_repository *repo,
6444 got_worktree_checkout_cb progress_cb, void *progress_arg,
6445 got_cancel_cb cancel_cb, void *cancel_arg)
6447 const struct got_error *err;
6448 char *commit_ref_name;
6450 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6451 if (err)
6452 return err;
6454 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6455 if (err)
6456 goto done;
6458 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6459 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6460 progress_arg, cancel_cb, cancel_arg);
6461 done:
6462 free(commit_ref_name);
6463 return err;
6466 static const struct got_error *
6467 rebase_commit(struct got_object_id **new_commit_id,
6468 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6469 struct got_worktree *worktree, struct got_fileindex *fileindex,
6470 struct got_reference *tmp_branch, const char *committer,
6471 struct got_commit_object *orig_commit, const char *new_logmsg,
6472 struct got_repository *repo)
6474 const struct got_error *err, *sync_err;
6475 struct got_pathlist_head commitable_paths;
6476 struct collect_commitables_arg cc_arg;
6477 char *fileindex_path = NULL;
6478 struct got_reference *head_ref = NULL;
6479 struct got_object_id *head_commit_id = NULL;
6480 char *logmsg = NULL;
6482 TAILQ_INIT(&commitable_paths);
6483 *new_commit_id = NULL;
6485 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6487 err = get_fileindex_path(&fileindex_path, worktree);
6488 if (err)
6489 return err;
6491 cc_arg.commitable_paths = &commitable_paths;
6492 cc_arg.worktree = worktree;
6493 cc_arg.repo = repo;
6494 cc_arg.have_staged_files = 0;
6496 * If possible get the status of individual files directly to
6497 * avoid crawling the entire work tree once per rebased commit.
6499 * Ideally, merged_paths would contain a list of commitables
6500 * we could use so we could skip worktree_status() entirely.
6501 * However, we would then need carefully keep track of cumulative
6502 * effects of operations such as file additions and deletions
6503 * in 'got histedit -f' (folding multiple commits into one),
6504 * and this extra complexity is not really worth it.
6506 if (merged_paths) {
6507 struct got_pathlist_entry *pe;
6508 TAILQ_FOREACH(pe, merged_paths, entry) {
6509 err = worktree_status(worktree, pe->path, fileindex,
6510 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6511 0);
6512 if (err)
6513 goto done;
6515 } else {
6516 err = worktree_status(worktree, "", fileindex, repo,
6517 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6518 if (err)
6519 goto done;
6522 if (TAILQ_EMPTY(&commitable_paths)) {
6523 /* No-op change; commit will be elided. */
6524 err = got_ref_delete(commit_ref, repo);
6525 if (err)
6526 goto done;
6527 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6528 goto done;
6531 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6532 if (err)
6533 goto done;
6535 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6536 if (err)
6537 goto done;
6539 if (new_logmsg) {
6540 logmsg = strdup(new_logmsg);
6541 if (logmsg == NULL) {
6542 err = got_error_from_errno("strdup");
6543 goto done;
6545 } else {
6546 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6547 if (err)
6548 goto done;
6551 /* NB: commit_worktree will call free(logmsg) */
6552 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6553 NULL, worktree, got_object_commit_get_author(orig_commit),
6554 committer ? committer :
6555 got_object_commit_get_committer(orig_commit),
6556 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6557 if (err)
6558 goto done;
6560 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6561 if (err)
6562 goto done;
6564 err = got_ref_delete(commit_ref, repo);
6565 if (err)
6566 goto done;
6568 err = update_fileindex_after_commit(worktree, &commitable_paths,
6569 *new_commit_id, fileindex, 0);
6570 sync_err = sync_fileindex(fileindex, fileindex_path);
6571 if (sync_err && err == NULL)
6572 err = sync_err;
6573 done:
6574 free(fileindex_path);
6575 free(head_commit_id);
6576 if (head_ref)
6577 got_ref_close(head_ref);
6578 if (err) {
6579 free(*new_commit_id);
6580 *new_commit_id = NULL;
6582 return err;
6585 const struct got_error *
6586 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6587 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6588 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6589 const char *committer, struct got_commit_object *orig_commit,
6590 struct got_object_id *orig_commit_id, struct got_repository *repo)
6592 const struct got_error *err;
6593 char *commit_ref_name;
6594 struct got_reference *commit_ref = NULL;
6595 struct got_object_id *commit_id = NULL;
6597 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6598 if (err)
6599 return err;
6601 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6602 if (err)
6603 goto done;
6604 err = got_ref_resolve(&commit_id, repo, commit_ref);
6605 if (err)
6606 goto done;
6607 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6608 err = got_error(GOT_ERR_REBASE_COMMITID);
6609 goto done;
6612 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6613 worktree, fileindex, tmp_branch, committer, orig_commit,
6614 NULL, repo);
6615 done:
6616 if (commit_ref)
6617 got_ref_close(commit_ref);
6618 free(commit_ref_name);
6619 free(commit_id);
6620 return err;
6623 const struct got_error *
6624 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6625 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6626 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6627 const char *committer, struct got_commit_object *orig_commit,
6628 struct got_object_id *orig_commit_id, const char *new_logmsg,
6629 struct got_repository *repo)
6631 const struct got_error *err;
6632 char *commit_ref_name;
6633 struct got_reference *commit_ref = NULL;
6635 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6636 if (err)
6637 return err;
6639 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6640 if (err)
6641 goto done;
6643 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6644 worktree, fileindex, tmp_branch, committer, orig_commit,
6645 new_logmsg, repo);
6646 done:
6647 if (commit_ref)
6648 got_ref_close(commit_ref);
6649 free(commit_ref_name);
6650 return err;
6653 const struct got_error *
6654 got_worktree_rebase_postpone(struct got_worktree *worktree,
6655 struct got_fileindex *fileindex)
6657 if (fileindex)
6658 got_fileindex_free(fileindex);
6659 return lock_worktree(worktree, LOCK_SH);
6662 static const struct got_error *
6663 delete_ref(const char *name, struct got_repository *repo)
6665 const struct got_error *err;
6666 struct got_reference *ref;
6668 err = got_ref_open(&ref, repo, name, 0);
6669 if (err) {
6670 if (err->code == GOT_ERR_NOT_REF)
6671 return NULL;
6672 return err;
6675 err = got_ref_delete(ref, repo);
6676 got_ref_close(ref);
6677 return err;
6680 static const struct got_error *
6681 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6683 const struct got_error *err;
6684 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6685 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6687 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6688 if (err)
6689 goto done;
6690 err = delete_ref(tmp_branch_name, repo);
6691 if (err)
6692 goto done;
6694 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6695 if (err)
6696 goto done;
6697 err = delete_ref(new_base_branch_ref_name, repo);
6698 if (err)
6699 goto done;
6701 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6702 if (err)
6703 goto done;
6704 err = delete_ref(branch_ref_name, repo);
6705 if (err)
6706 goto done;
6708 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6709 if (err)
6710 goto done;
6711 err = delete_ref(commit_ref_name, repo);
6712 if (err)
6713 goto done;
6715 done:
6716 free(tmp_branch_name);
6717 free(new_base_branch_ref_name);
6718 free(branch_ref_name);
6719 free(commit_ref_name);
6720 return err;
6723 static const struct got_error *
6724 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6725 struct got_object_id *new_commit_id, struct got_repository *repo)
6727 const struct got_error *err;
6728 struct got_reference *ref = NULL;
6729 struct got_object_id *old_commit_id = NULL;
6730 const char *branch_name = NULL;
6731 char *new_id_str = NULL;
6732 char *refname = NULL;
6734 branch_name = got_ref_get_name(branch);
6735 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6736 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6737 branch_name += 11;
6739 err = got_object_id_str(&new_id_str, new_commit_id);
6740 if (err)
6741 return err;
6743 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6744 new_id_str) == -1) {
6745 err = got_error_from_errno("asprintf");
6746 goto done;
6749 err = got_ref_resolve(&old_commit_id, repo, branch);
6750 if (err)
6751 goto done;
6753 err = got_ref_alloc(&ref, refname, old_commit_id);
6754 if (err)
6755 goto done;
6757 err = got_ref_write(ref, repo);
6758 done:
6759 free(new_id_str);
6760 free(refname);
6761 free(old_commit_id);
6762 if (ref)
6763 got_ref_close(ref);
6764 return err;
6767 const struct got_error *
6768 got_worktree_rebase_complete(struct got_worktree *worktree,
6769 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6770 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6771 struct got_repository *repo, int create_backup)
6773 const struct got_error *err, *unlockerr, *sync_err;
6774 struct got_object_id *new_head_commit_id = NULL;
6775 char *fileindex_path = NULL;
6777 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6778 if (err)
6779 return err;
6781 if (create_backup) {
6782 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6783 rebased_branch, new_head_commit_id, repo);
6784 if (err)
6785 goto done;
6788 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6789 if (err)
6790 goto done;
6792 err = got_ref_write(rebased_branch, repo);
6793 if (err)
6794 goto done;
6796 err = got_worktree_set_head_ref(worktree, rebased_branch);
6797 if (err)
6798 goto done;
6800 err = delete_rebase_refs(worktree, repo);
6801 if (err)
6802 goto done;
6804 err = get_fileindex_path(&fileindex_path, worktree);
6805 if (err)
6806 goto done;
6807 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6808 sync_err = sync_fileindex(fileindex, fileindex_path);
6809 if (sync_err && err == NULL)
6810 err = sync_err;
6811 done:
6812 got_fileindex_free(fileindex);
6813 free(fileindex_path);
6814 free(new_head_commit_id);
6815 unlockerr = lock_worktree(worktree, LOCK_SH);
6816 if (unlockerr && err == NULL)
6817 err = unlockerr;
6818 return err;
6821 const struct got_error *
6822 got_worktree_rebase_abort(struct got_worktree *worktree,
6823 struct got_fileindex *fileindex, struct got_repository *repo,
6824 struct got_reference *new_base_branch,
6825 got_worktree_checkout_cb progress_cb, void *progress_arg)
6827 const struct got_error *err, *unlockerr, *sync_err;
6828 struct got_reference *resolved = NULL;
6829 struct got_object_id *commit_id = NULL;
6830 struct got_commit_object *commit = NULL;
6831 char *fileindex_path = NULL;
6832 struct revert_file_args rfa;
6833 struct got_object_id *tree_id = NULL;
6835 err = lock_worktree(worktree, LOCK_EX);
6836 if (err)
6837 return err;
6839 err = got_object_open_as_commit(&commit, repo,
6840 worktree->base_commit_id);
6841 if (err)
6842 goto done;
6844 err = got_ref_open(&resolved, repo,
6845 got_ref_get_symref_target(new_base_branch), 0);
6846 if (err)
6847 goto done;
6849 err = got_worktree_set_head_ref(worktree, resolved);
6850 if (err)
6851 goto done;
6854 * XXX commits to the base branch could have happened while
6855 * we were busy rebasing; should we store the original commit ID
6856 * when rebase begins and read it back here?
6858 err = got_ref_resolve(&commit_id, repo, resolved);
6859 if (err)
6860 goto done;
6862 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6863 if (err)
6864 goto done;
6866 err = got_object_id_by_path(&tree_id, repo, commit,
6867 worktree->path_prefix);
6868 if (err)
6869 goto done;
6871 err = delete_rebase_refs(worktree, repo);
6872 if (err)
6873 goto done;
6875 err = get_fileindex_path(&fileindex_path, worktree);
6876 if (err)
6877 goto done;
6879 rfa.worktree = worktree;
6880 rfa.fileindex = fileindex;
6881 rfa.progress_cb = progress_cb;
6882 rfa.progress_arg = progress_arg;
6883 rfa.patch_cb = NULL;
6884 rfa.patch_arg = NULL;
6885 rfa.repo = repo;
6886 rfa.unlink_added_files = 0;
6887 err = worktree_status(worktree, "", fileindex, repo,
6888 revert_file, &rfa, NULL, NULL, 1, 0);
6889 if (err)
6890 goto sync;
6892 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6893 repo, progress_cb, progress_arg, NULL, NULL);
6894 sync:
6895 sync_err = sync_fileindex(fileindex, fileindex_path);
6896 if (sync_err && err == NULL)
6897 err = sync_err;
6898 done:
6899 got_ref_close(resolved);
6900 free(tree_id);
6901 free(commit_id);
6902 if (commit)
6903 got_object_commit_close(commit);
6904 if (fileindex)
6905 got_fileindex_free(fileindex);
6906 free(fileindex_path);
6908 unlockerr = lock_worktree(worktree, LOCK_SH);
6909 if (unlockerr && err == NULL)
6910 err = unlockerr;
6911 return err;
6914 const struct got_error *
6915 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6916 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6917 struct got_fileindex **fileindex, struct got_worktree *worktree,
6918 struct got_repository *repo)
6920 const struct got_error *err = NULL;
6921 char *tmp_branch_name = NULL;
6922 char *branch_ref_name = NULL;
6923 char *base_commit_ref_name = NULL;
6924 char *fileindex_path = NULL;
6925 struct check_rebase_ok_arg ok_arg;
6926 struct got_reference *wt_branch = NULL;
6927 struct got_reference *base_commit_ref = NULL;
6929 *tmp_branch = NULL;
6930 *branch_ref = NULL;
6931 *base_commit_id = NULL;
6932 *fileindex = NULL;
6934 err = lock_worktree(worktree, LOCK_EX);
6935 if (err)
6936 return err;
6938 err = open_fileindex(fileindex, &fileindex_path, worktree);
6939 if (err)
6940 goto done;
6942 ok_arg.worktree = worktree;
6943 ok_arg.repo = repo;
6944 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6945 &ok_arg);
6946 if (err)
6947 goto done;
6949 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6950 if (err)
6951 goto done;
6953 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6954 if (err)
6955 goto done;
6957 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6958 worktree);
6959 if (err)
6960 goto done;
6962 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6963 0);
6964 if (err)
6965 goto done;
6967 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6968 if (err)
6969 goto done;
6971 err = got_ref_write(*branch_ref, repo);
6972 if (err)
6973 goto done;
6975 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6976 worktree->base_commit_id);
6977 if (err)
6978 goto done;
6979 err = got_ref_write(base_commit_ref, repo);
6980 if (err)
6981 goto done;
6982 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6983 if (*base_commit_id == NULL) {
6984 err = got_error_from_errno("got_object_id_dup");
6985 goto done;
6988 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6989 worktree->base_commit_id);
6990 if (err)
6991 goto done;
6992 err = got_ref_write(*tmp_branch, repo);
6993 if (err)
6994 goto done;
6996 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6997 if (err)
6998 goto done;
6999 done:
7000 free(fileindex_path);
7001 free(tmp_branch_name);
7002 free(branch_ref_name);
7003 free(base_commit_ref_name);
7004 if (wt_branch)
7005 got_ref_close(wt_branch);
7006 if (err) {
7007 if (*branch_ref) {
7008 got_ref_close(*branch_ref);
7009 *branch_ref = NULL;
7011 if (*tmp_branch) {
7012 got_ref_close(*tmp_branch);
7013 *tmp_branch = NULL;
7015 free(*base_commit_id);
7016 if (*fileindex) {
7017 got_fileindex_free(*fileindex);
7018 *fileindex = NULL;
7020 lock_worktree(worktree, LOCK_SH);
7022 return err;
7025 const struct got_error *
7026 got_worktree_histedit_postpone(struct got_worktree *worktree,
7027 struct got_fileindex *fileindex)
7029 if (fileindex)
7030 got_fileindex_free(fileindex);
7031 return lock_worktree(worktree, LOCK_SH);
7034 const struct got_error *
7035 got_worktree_histedit_in_progress(int *in_progress,
7036 struct got_worktree *worktree)
7038 const struct got_error *err;
7039 char *tmp_branch_name = NULL;
7041 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7042 if (err)
7043 return err;
7045 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7046 free(tmp_branch_name);
7047 return NULL;
7050 const struct got_error *
7051 got_worktree_histedit_continue(struct got_object_id **commit_id,
7052 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7053 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7054 struct got_worktree *worktree, struct got_repository *repo)
7056 const struct got_error *err;
7057 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7058 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7059 struct got_reference *commit_ref = NULL;
7060 struct got_reference *base_commit_ref = NULL;
7061 char *fileindex_path = NULL;
7062 int have_staged_files = 0;
7064 *commit_id = NULL;
7065 *tmp_branch = NULL;
7066 *base_commit_id = NULL;
7067 *fileindex = NULL;
7069 err = lock_worktree(worktree, LOCK_EX);
7070 if (err)
7071 return err;
7073 err = open_fileindex(fileindex, &fileindex_path, worktree);
7074 if (err)
7075 goto done;
7077 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7078 &have_staged_files);
7079 if (err && err->code != GOT_ERR_CANCELLED)
7080 goto done;
7081 if (have_staged_files) {
7082 err = got_error(GOT_ERR_STAGED_PATHS);
7083 goto done;
7086 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7087 if (err)
7088 goto done;
7090 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7091 if (err)
7092 goto done;
7094 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7095 if (err)
7096 goto done;
7098 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7099 worktree);
7100 if (err)
7101 goto done;
7103 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7104 if (err)
7105 goto done;
7107 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7108 if (err)
7109 goto done;
7110 err = got_ref_resolve(commit_id, repo, commit_ref);
7111 if (err)
7112 goto done;
7114 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7115 if (err)
7116 goto done;
7117 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7118 if (err)
7119 goto done;
7121 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7122 if (err)
7123 goto done;
7124 done:
7125 free(commit_ref_name);
7126 free(branch_ref_name);
7127 free(fileindex_path);
7128 if (commit_ref)
7129 got_ref_close(commit_ref);
7130 if (base_commit_ref)
7131 got_ref_close(base_commit_ref);
7132 if (err) {
7133 free(*commit_id);
7134 *commit_id = NULL;
7135 free(*base_commit_id);
7136 *base_commit_id = NULL;
7137 if (*tmp_branch) {
7138 got_ref_close(*tmp_branch);
7139 *tmp_branch = NULL;
7141 if (*fileindex) {
7142 got_fileindex_free(*fileindex);
7143 *fileindex = NULL;
7145 lock_worktree(worktree, LOCK_EX);
7147 return err;
7150 static const struct got_error *
7151 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7153 const struct got_error *err;
7154 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7155 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7157 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7158 if (err)
7159 goto done;
7160 err = delete_ref(tmp_branch_name, repo);
7161 if (err)
7162 goto done;
7164 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7165 worktree);
7166 if (err)
7167 goto done;
7168 err = delete_ref(base_commit_ref_name, repo);
7169 if (err)
7170 goto done;
7172 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7173 if (err)
7174 goto done;
7175 err = delete_ref(branch_ref_name, repo);
7176 if (err)
7177 goto done;
7179 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7180 if (err)
7181 goto done;
7182 err = delete_ref(commit_ref_name, repo);
7183 if (err)
7184 goto done;
7185 done:
7186 free(tmp_branch_name);
7187 free(base_commit_ref_name);
7188 free(branch_ref_name);
7189 free(commit_ref_name);
7190 return err;
7193 const struct got_error *
7194 got_worktree_histedit_abort(struct got_worktree *worktree,
7195 struct got_fileindex *fileindex, struct got_repository *repo,
7196 struct got_reference *branch, struct got_object_id *base_commit_id,
7197 got_worktree_checkout_cb progress_cb, void *progress_arg)
7199 const struct got_error *err, *unlockerr, *sync_err;
7200 struct got_reference *resolved = NULL;
7201 char *fileindex_path = NULL;
7202 struct got_commit_object *commit = NULL;
7203 struct got_object_id *tree_id = NULL;
7204 struct revert_file_args rfa;
7206 err = lock_worktree(worktree, LOCK_EX);
7207 if (err)
7208 return err;
7210 err = got_object_open_as_commit(&commit, repo,
7211 worktree->base_commit_id);
7212 if (err)
7213 goto done;
7215 err = got_ref_open(&resolved, repo,
7216 got_ref_get_symref_target(branch), 0);
7217 if (err)
7218 goto done;
7220 err = got_worktree_set_head_ref(worktree, resolved);
7221 if (err)
7222 goto done;
7224 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7225 if (err)
7226 goto done;
7228 err = got_object_id_by_path(&tree_id, repo, commit,
7229 worktree->path_prefix);
7230 if (err)
7231 goto done;
7233 err = delete_histedit_refs(worktree, repo);
7234 if (err)
7235 goto done;
7237 err = get_fileindex_path(&fileindex_path, worktree);
7238 if (err)
7239 goto done;
7241 rfa.worktree = worktree;
7242 rfa.fileindex = fileindex;
7243 rfa.progress_cb = progress_cb;
7244 rfa.progress_arg = progress_arg;
7245 rfa.patch_cb = NULL;
7246 rfa.patch_arg = NULL;
7247 rfa.repo = repo;
7248 rfa.unlink_added_files = 0;
7249 err = worktree_status(worktree, "", fileindex, repo,
7250 revert_file, &rfa, NULL, NULL, 1, 0);
7251 if (err)
7252 goto sync;
7254 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7255 repo, progress_cb, progress_arg, NULL, NULL);
7256 sync:
7257 sync_err = sync_fileindex(fileindex, fileindex_path);
7258 if (sync_err && err == NULL)
7259 err = sync_err;
7260 done:
7261 got_ref_close(resolved);
7262 free(tree_id);
7263 free(fileindex_path);
7265 unlockerr = lock_worktree(worktree, LOCK_SH);
7266 if (unlockerr && err == NULL)
7267 err = unlockerr;
7268 return err;
7271 const struct got_error *
7272 got_worktree_histedit_complete(struct got_worktree *worktree,
7273 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7274 struct got_reference *edited_branch, struct got_repository *repo)
7276 const struct got_error *err, *unlockerr, *sync_err;
7277 struct got_object_id *new_head_commit_id = NULL;
7278 struct got_reference *resolved = NULL;
7279 char *fileindex_path = NULL;
7281 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7282 if (err)
7283 return err;
7285 err = got_ref_open(&resolved, repo,
7286 got_ref_get_symref_target(edited_branch), 0);
7287 if (err)
7288 goto done;
7290 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7291 resolved, new_head_commit_id, repo);
7292 if (err)
7293 goto done;
7295 err = got_ref_change_ref(resolved, new_head_commit_id);
7296 if (err)
7297 goto done;
7299 err = got_ref_write(resolved, repo);
7300 if (err)
7301 goto done;
7303 err = got_worktree_set_head_ref(worktree, resolved);
7304 if (err)
7305 goto done;
7307 err = delete_histedit_refs(worktree, repo);
7308 if (err)
7309 goto done;
7311 err = get_fileindex_path(&fileindex_path, worktree);
7312 if (err)
7313 goto done;
7314 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7315 sync_err = sync_fileindex(fileindex, fileindex_path);
7316 if (sync_err && err == NULL)
7317 err = sync_err;
7318 done:
7319 got_fileindex_free(fileindex);
7320 free(fileindex_path);
7321 free(new_head_commit_id);
7322 unlockerr = lock_worktree(worktree, LOCK_SH);
7323 if (unlockerr && err == NULL)
7324 err = unlockerr;
7325 return err;
7328 const struct got_error *
7329 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7330 struct got_object_id *commit_id, struct got_repository *repo)
7332 const struct got_error *err;
7333 char *commit_ref_name;
7335 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7336 if (err)
7337 return err;
7339 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7340 if (err)
7341 goto done;
7343 err = delete_ref(commit_ref_name, repo);
7344 done:
7345 free(commit_ref_name);
7346 return err;
7349 const struct got_error *
7350 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7351 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7352 struct got_worktree *worktree, const char *refname,
7353 struct got_repository *repo)
7355 const struct got_error *err = NULL;
7356 char *fileindex_path = NULL;
7357 struct check_rebase_ok_arg ok_arg;
7359 *fileindex = NULL;
7360 *branch_ref = NULL;
7361 *base_branch_ref = NULL;
7363 err = lock_worktree(worktree, LOCK_EX);
7364 if (err)
7365 return err;
7367 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7368 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7369 "cannot integrate a branch into itself; "
7370 "update -b or different branch name required");
7371 goto done;
7374 err = open_fileindex(fileindex, &fileindex_path, worktree);
7375 if (err)
7376 goto done;
7378 /* Preconditions are the same as for rebase. */
7379 ok_arg.worktree = worktree;
7380 ok_arg.repo = repo;
7381 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7382 &ok_arg);
7383 if (err)
7384 goto done;
7386 err = got_ref_open(branch_ref, repo, refname, 1);
7387 if (err)
7388 goto done;
7390 err = got_ref_open(base_branch_ref, repo,
7391 got_worktree_get_head_ref_name(worktree), 1);
7392 done:
7393 if (err) {
7394 if (*branch_ref) {
7395 got_ref_close(*branch_ref);
7396 *branch_ref = NULL;
7398 if (*base_branch_ref) {
7399 got_ref_close(*base_branch_ref);
7400 *base_branch_ref = NULL;
7402 if (*fileindex) {
7403 got_fileindex_free(*fileindex);
7404 *fileindex = NULL;
7406 lock_worktree(worktree, LOCK_SH);
7408 return err;
7411 const struct got_error *
7412 got_worktree_integrate_continue(struct got_worktree *worktree,
7413 struct got_fileindex *fileindex, struct got_repository *repo,
7414 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7415 got_worktree_checkout_cb progress_cb, void *progress_arg,
7416 got_cancel_cb cancel_cb, void *cancel_arg)
7418 const struct got_error *err = NULL, *sync_err, *unlockerr;
7419 char *fileindex_path = NULL;
7420 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7421 struct got_commit_object *commit = NULL;
7423 err = get_fileindex_path(&fileindex_path, worktree);
7424 if (err)
7425 goto done;
7427 err = got_ref_resolve(&commit_id, repo, branch_ref);
7428 if (err)
7429 goto done;
7431 err = got_object_open_as_commit(&commit, repo, commit_id);
7432 if (err)
7433 goto done;
7435 err = got_object_id_by_path(&tree_id, repo, commit,
7436 worktree->path_prefix);
7437 if (err)
7438 goto done;
7440 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7441 if (err)
7442 goto done;
7444 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7445 progress_cb, progress_arg, cancel_cb, cancel_arg);
7446 if (err)
7447 goto sync;
7449 err = got_ref_change_ref(base_branch_ref, commit_id);
7450 if (err)
7451 goto sync;
7453 err = got_ref_write(base_branch_ref, repo);
7454 if (err)
7455 goto sync;
7457 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7458 sync:
7459 sync_err = sync_fileindex(fileindex, fileindex_path);
7460 if (sync_err && err == NULL)
7461 err = sync_err;
7463 done:
7464 unlockerr = got_ref_unlock(branch_ref);
7465 if (unlockerr && err == NULL)
7466 err = unlockerr;
7467 got_ref_close(branch_ref);
7469 unlockerr = got_ref_unlock(base_branch_ref);
7470 if (unlockerr && err == NULL)
7471 err = unlockerr;
7472 got_ref_close(base_branch_ref);
7474 got_fileindex_free(fileindex);
7475 free(fileindex_path);
7476 free(tree_id);
7477 if (commit)
7478 got_object_commit_close(commit);
7480 unlockerr = lock_worktree(worktree, LOCK_SH);
7481 if (unlockerr && err == NULL)
7482 err = unlockerr;
7483 return err;
7486 const struct got_error *
7487 got_worktree_integrate_abort(struct got_worktree *worktree,
7488 struct got_fileindex *fileindex, struct got_repository *repo,
7489 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7491 const struct got_error *err = NULL, *unlockerr = NULL;
7493 got_fileindex_free(fileindex);
7495 err = lock_worktree(worktree, LOCK_SH);
7497 unlockerr = got_ref_unlock(branch_ref);
7498 if (unlockerr && err == NULL)
7499 err = unlockerr;
7500 got_ref_close(branch_ref);
7502 unlockerr = got_ref_unlock(base_branch_ref);
7503 if (unlockerr && err == NULL)
7504 err = unlockerr;
7505 got_ref_close(base_branch_ref);
7507 return err;
7510 const struct got_error *
7511 got_worktree_merge_postpone(struct got_worktree *worktree,
7512 struct got_fileindex *fileindex)
7514 const struct got_error *err, *sync_err;
7515 char *fileindex_path = NULL;
7517 err = get_fileindex_path(&fileindex_path, worktree);
7518 if (err)
7519 goto done;
7521 sync_err = sync_fileindex(fileindex, fileindex_path);
7523 err = lock_worktree(worktree, LOCK_SH);
7524 if (sync_err && err == NULL)
7525 err = sync_err;
7526 done:
7527 got_fileindex_free(fileindex);
7528 free(fileindex_path);
7529 return err;
7532 static const struct got_error *
7533 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7535 const struct got_error *err;
7536 char *branch_refname = NULL, *commit_refname = NULL;
7538 err = get_merge_branch_ref_name(&branch_refname, worktree);
7539 if (err)
7540 goto done;
7541 err = delete_ref(branch_refname, repo);
7542 if (err)
7543 goto done;
7545 err = get_merge_commit_ref_name(&commit_refname, worktree);
7546 if (err)
7547 goto done;
7548 err = delete_ref(commit_refname, repo);
7549 if (err)
7550 goto done;
7552 done:
7553 free(branch_refname);
7554 free(commit_refname);
7555 return err;
7558 struct merge_commit_msg_arg {
7559 struct got_worktree *worktree;
7560 const char *branch_name;
7563 static const struct got_error *
7564 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths, char **logmsg,
7565 void *arg)
7567 struct merge_commit_msg_arg *a = arg;
7569 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7570 got_worktree_get_head_ref_name(a->worktree)) == -1)
7571 return got_error_from_errno("asprintf");
7573 return NULL;
7577 const struct got_error *
7578 got_worktree_merge_branch(struct got_worktree *worktree,
7579 struct got_fileindex *fileindex,
7580 struct got_object_id *yca_commit_id,
7581 struct got_object_id *branch_tip,
7582 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7583 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7585 const struct got_error *err;
7586 char *fileindex_path = NULL;
7588 err = get_fileindex_path(&fileindex_path, worktree);
7589 if (err)
7590 goto done;
7592 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7593 worktree);
7594 if (err)
7595 goto done;
7597 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7598 branch_tip, repo, progress_cb, progress_arg,
7599 cancel_cb, cancel_arg);
7600 done:
7601 free(fileindex_path);
7602 return err;
7605 const struct got_error *
7606 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7607 struct got_worktree *worktree, struct got_fileindex *fileindex,
7608 const char *author, const char *committer, int allow_bad_symlinks,
7609 struct got_object_id *branch_tip, const char *branch_name,
7610 struct got_repository *repo,
7611 got_worktree_status_cb status_cb, void *status_arg)
7614 const struct got_error *err = NULL, *sync_err;
7615 struct got_pathlist_head commitable_paths;
7616 struct collect_commitables_arg cc_arg;
7617 struct got_pathlist_entry *pe;
7618 struct got_reference *head_ref = NULL;
7619 struct got_object_id *head_commit_id = NULL;
7620 int have_staged_files = 0;
7621 struct merge_commit_msg_arg mcm_arg;
7622 char *fileindex_path = NULL;
7624 *new_commit_id = NULL;
7626 TAILQ_INIT(&commitable_paths);
7628 err = get_fileindex_path(&fileindex_path, worktree);
7629 if (err)
7630 goto done;
7632 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7633 if (err)
7634 goto done;
7636 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7637 if (err)
7638 goto done;
7640 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7641 &have_staged_files);
7642 if (err && err->code != GOT_ERR_CANCELLED)
7643 goto done;
7644 if (have_staged_files) {
7645 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7646 goto done;
7649 cc_arg.commitable_paths = &commitable_paths;
7650 cc_arg.worktree = worktree;
7651 cc_arg.fileindex = fileindex;
7652 cc_arg.repo = repo;
7653 cc_arg.have_staged_files = have_staged_files;
7654 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7655 err = worktree_status(worktree, "", fileindex, repo,
7656 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7657 if (err)
7658 goto done;
7660 if (TAILQ_EMPTY(&commitable_paths)) {
7661 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7662 "merge of %s cannot proceed", branch_name);
7663 goto done;
7666 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7667 struct got_commitable *ct = pe->data;
7668 const char *ct_path = ct->in_repo_path;
7670 while (ct_path[0] == '/')
7671 ct_path++;
7672 err = check_out_of_date(ct_path, ct->status,
7673 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7674 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7675 if (err)
7676 goto done;
7680 mcm_arg.worktree = worktree;
7681 mcm_arg.branch_name = branch_name;
7682 err = commit_worktree(new_commit_id, &commitable_paths,
7683 head_commit_id, branch_tip, worktree, author, committer,
7684 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7685 if (err)
7686 goto done;
7688 err = update_fileindex_after_commit(worktree, &commitable_paths,
7689 *new_commit_id, fileindex, have_staged_files);
7690 sync_err = sync_fileindex(fileindex, fileindex_path);
7691 if (sync_err && err == NULL)
7692 err = sync_err;
7693 done:
7694 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7695 struct got_commitable *ct = pe->data;
7696 free_commitable(ct);
7698 got_pathlist_free(&commitable_paths);
7699 free(fileindex_path);
7700 return err;
7703 const struct got_error *
7704 got_worktree_merge_complete(struct got_worktree *worktree,
7705 struct got_fileindex *fileindex, struct got_repository *repo)
7707 const struct got_error *err, *unlockerr, *sync_err;
7708 char *fileindex_path = NULL;
7710 err = delete_merge_refs(worktree, repo);
7711 if (err)
7712 goto done;
7714 err = get_fileindex_path(&fileindex_path, worktree);
7715 if (err)
7716 goto done;
7717 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7718 sync_err = sync_fileindex(fileindex, fileindex_path);
7719 if (sync_err && err == NULL)
7720 err = sync_err;
7721 done:
7722 got_fileindex_free(fileindex);
7723 free(fileindex_path);
7724 unlockerr = lock_worktree(worktree, LOCK_SH);
7725 if (unlockerr && err == NULL)
7726 err = unlockerr;
7727 return err;
7730 const struct got_error *
7731 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7732 struct got_repository *repo)
7734 const struct got_error *err;
7735 char *branch_refname = NULL;
7736 struct got_reference *branch_ref = NULL;
7738 *in_progress = 0;
7740 err = get_merge_branch_ref_name(&branch_refname, worktree);
7741 if (err)
7742 return err;
7743 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7744 free(branch_refname);
7745 if (err) {
7746 if (err->code != GOT_ERR_NOT_REF)
7747 return err;
7748 } else
7749 *in_progress = 1;
7751 return NULL;
7754 const struct got_error *got_worktree_merge_prepare(
7755 struct got_fileindex **fileindex, struct got_worktree *worktree,
7756 struct got_reference *branch, struct got_repository *repo)
7758 const struct got_error *err = NULL;
7759 char *fileindex_path = NULL;
7760 char *branch_refname = NULL, *commit_refname = NULL;
7761 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
7762 struct got_reference *commit_ref = NULL;
7763 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
7764 struct check_rebase_ok_arg ok_arg;
7766 *fileindex = NULL;
7768 err = lock_worktree(worktree, LOCK_EX);
7769 if (err)
7770 return err;
7772 err = open_fileindex(fileindex, &fileindex_path, worktree);
7773 if (err)
7774 goto done;
7776 /* Preconditions are the same as for rebase. */
7777 ok_arg.worktree = worktree;
7778 ok_arg.repo = repo;
7779 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7780 &ok_arg);
7781 if (err)
7782 goto done;
7784 err = get_merge_branch_ref_name(&branch_refname, worktree);
7785 if (err)
7786 return err;
7788 err = get_merge_commit_ref_name(&commit_refname, worktree);
7789 if (err)
7790 return err;
7792 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7793 0);
7794 if (err)
7795 goto done;
7797 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
7798 if (err)
7799 goto done;
7801 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
7802 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
7803 goto done;
7806 err = got_ref_resolve(&branch_tip, repo, branch);
7807 if (err)
7808 goto done;
7810 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
7811 if (err)
7812 goto done;
7813 err = got_ref_write(branch_ref, repo);
7814 if (err)
7815 goto done;
7817 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
7818 if (err)
7819 goto done;
7820 err = got_ref_write(commit_ref, repo);
7821 if (err)
7822 goto done;
7824 done:
7825 free(branch_refname);
7826 free(commit_refname);
7827 free(fileindex_path);
7828 if (branch_ref)
7829 got_ref_close(branch_ref);
7830 if (commit_ref)
7831 got_ref_close(commit_ref);
7832 if (wt_branch)
7833 got_ref_close(wt_branch);
7834 free(wt_branch_tip);
7835 if (err) {
7836 if (*fileindex) {
7837 got_fileindex_free(*fileindex);
7838 *fileindex = NULL;
7840 lock_worktree(worktree, LOCK_SH);
7842 return err;
7845 const struct got_error *
7846 got_worktree_merge_continue(char **branch_name,
7847 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
7848 struct got_worktree *worktree, struct got_repository *repo)
7850 const struct got_error *err;
7851 char *commit_refname = NULL, *branch_refname = NULL;
7852 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
7853 char *fileindex_path = NULL;
7854 int have_staged_files = 0;
7856 *branch_name = NULL;
7857 *branch_tip = NULL;
7858 *fileindex = NULL;
7860 err = lock_worktree(worktree, LOCK_EX);
7861 if (err)
7862 return err;
7864 err = open_fileindex(fileindex, &fileindex_path, worktree);
7865 if (err)
7866 goto done;
7868 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7869 &have_staged_files);
7870 if (err && err->code != GOT_ERR_CANCELLED)
7871 goto done;
7872 if (have_staged_files) {
7873 err = got_error(GOT_ERR_STAGED_PATHS);
7874 goto done;
7877 err = get_merge_branch_ref_name(&branch_refname, worktree);
7878 if (err)
7879 goto done;
7881 err = get_merge_commit_ref_name(&commit_refname, worktree);
7882 if (err)
7883 goto done;
7885 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7886 if (err)
7887 goto done;
7889 if (!got_ref_is_symbolic(branch_ref)) {
7890 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7891 "%s is not a symbolic reference",
7892 got_ref_get_name(branch_ref));
7893 goto done;
7895 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
7896 if (*branch_name == NULL) {
7897 err = got_error_from_errno("strdup");
7898 goto done;
7901 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
7902 if (err)
7903 goto done;
7905 err = got_ref_resolve(branch_tip, repo, commit_ref);
7906 if (err)
7907 goto done;
7908 done:
7909 free(commit_refname);
7910 free(branch_refname);
7911 free(fileindex_path);
7912 if (commit_ref)
7913 got_ref_close(commit_ref);
7914 if (branch_ref)
7915 got_ref_close(branch_ref);
7916 if (err) {
7917 if (*branch_name) {
7918 free(*branch_name);
7919 *branch_name = NULL;
7921 free(*branch_tip);
7922 *branch_tip = NULL;
7923 if (*fileindex) {
7924 got_fileindex_free(*fileindex);
7925 *fileindex = NULL;
7927 lock_worktree(worktree, LOCK_SH);
7929 return err;
7932 const struct got_error *
7933 got_worktree_merge_abort(struct got_worktree *worktree,
7934 struct got_fileindex *fileindex, struct got_repository *repo,
7935 got_worktree_checkout_cb progress_cb, void *progress_arg)
7937 const struct got_error *err, *unlockerr, *sync_err;
7938 struct got_object_id *commit_id = NULL;
7939 struct got_commit_object *commit = NULL;
7940 char *fileindex_path = NULL;
7941 struct revert_file_args rfa;
7942 struct got_object_id *tree_id = NULL;
7944 err = got_object_open_as_commit(&commit, repo,
7945 worktree->base_commit_id);
7946 if (err)
7947 goto done;
7949 err = got_object_id_by_path(&tree_id, repo, commit,
7950 worktree->path_prefix);
7951 if (err)
7952 goto done;
7954 err = delete_merge_refs(worktree, repo);
7955 if (err)
7956 goto done;
7958 err = get_fileindex_path(&fileindex_path, worktree);
7959 if (err)
7960 goto done;
7962 rfa.worktree = worktree;
7963 rfa.fileindex = fileindex;
7964 rfa.progress_cb = progress_cb;
7965 rfa.progress_arg = progress_arg;
7966 rfa.patch_cb = NULL;
7967 rfa.patch_arg = NULL;
7968 rfa.repo = repo;
7969 rfa.unlink_added_files = 1;
7970 err = worktree_status(worktree, "", fileindex, repo,
7971 revert_file, &rfa, NULL, NULL, 1, 0);
7972 if (err)
7973 goto sync;
7975 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7976 repo, progress_cb, progress_arg, NULL, NULL);
7977 sync:
7978 sync_err = sync_fileindex(fileindex, fileindex_path);
7979 if (sync_err && err == NULL)
7980 err = sync_err;
7981 done:
7982 free(tree_id);
7983 free(commit_id);
7984 if (commit)
7985 got_object_commit_close(commit);
7986 if (fileindex)
7987 got_fileindex_free(fileindex);
7988 free(fileindex_path);
7990 unlockerr = lock_worktree(worktree, LOCK_SH);
7991 if (unlockerr && err == NULL)
7992 err = unlockerr;
7993 return err;
7996 struct check_stage_ok_arg {
7997 struct got_object_id *head_commit_id;
7998 struct got_worktree *worktree;
7999 struct got_fileindex *fileindex;
8000 struct got_repository *repo;
8001 int have_changes;
8004 static const struct got_error *
8005 check_stage_ok(void *arg, unsigned char status,
8006 unsigned char staged_status, const char *relpath,
8007 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8008 struct got_object_id *commit_id, int dirfd, const char *de_name)
8010 struct check_stage_ok_arg *a = arg;
8011 const struct got_error *err = NULL;
8012 struct got_fileindex_entry *ie;
8013 struct got_object_id base_commit_id;
8014 struct got_object_id *base_commit_idp = NULL;
8015 char *in_repo_path = NULL, *p;
8017 if (status == GOT_STATUS_UNVERSIONED ||
8018 status == GOT_STATUS_NO_CHANGE)
8019 return NULL;
8020 if (status == GOT_STATUS_NONEXISTENT)
8021 return got_error_set_errno(ENOENT, relpath);
8023 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8024 if (ie == NULL)
8025 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8027 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8028 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8029 relpath) == -1)
8030 return got_error_from_errno("asprintf");
8032 if (got_fileindex_entry_has_commit(ie)) {
8033 memcpy(base_commit_id.sha1, ie->commit_sha1,
8034 SHA1_DIGEST_LENGTH);
8035 base_commit_idp = &base_commit_id;
8038 if (status == GOT_STATUS_CONFLICT) {
8039 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8040 goto done;
8041 } else if (status != GOT_STATUS_ADD &&
8042 status != GOT_STATUS_MODIFY &&
8043 status != GOT_STATUS_DELETE) {
8044 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8045 goto done;
8048 a->have_changes = 1;
8050 p = in_repo_path;
8051 while (p[0] == '/')
8052 p++;
8053 err = check_out_of_date(p, status, staged_status,
8054 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8055 GOT_ERR_STAGE_OUT_OF_DATE);
8056 done:
8057 free(in_repo_path);
8058 return err;
8061 struct stage_path_arg {
8062 struct got_worktree *worktree;
8063 struct got_fileindex *fileindex;
8064 struct got_repository *repo;
8065 got_worktree_status_cb status_cb;
8066 void *status_arg;
8067 got_worktree_patch_cb patch_cb;
8068 void *patch_arg;
8069 int staged_something;
8070 int allow_bad_symlinks;
8073 static const struct got_error *
8074 stage_path(void *arg, unsigned char status,
8075 unsigned char staged_status, const char *relpath,
8076 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8077 struct got_object_id *commit_id, int dirfd, const char *de_name)
8079 struct stage_path_arg *a = arg;
8080 const struct got_error *err = NULL;
8081 struct got_fileindex_entry *ie;
8082 char *ondisk_path = NULL, *path_content = NULL;
8083 uint32_t stage;
8084 struct got_object_id *new_staged_blob_id = NULL;
8085 struct stat sb;
8087 if (status == GOT_STATUS_UNVERSIONED)
8088 return NULL;
8090 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8091 if (ie == NULL)
8092 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8094 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8095 relpath)== -1)
8096 return got_error_from_errno("asprintf");
8098 switch (status) {
8099 case GOT_STATUS_ADD:
8100 case GOT_STATUS_MODIFY:
8101 /* XXX could sb.st_mode be passed in by our caller? */
8102 if (lstat(ondisk_path, &sb) == -1) {
8103 err = got_error_from_errno2("lstat", ondisk_path);
8104 break;
8106 if (a->patch_cb) {
8107 if (status == GOT_STATUS_ADD) {
8108 int choice = GOT_PATCH_CHOICE_NONE;
8109 err = (*a->patch_cb)(&choice, a->patch_arg,
8110 status, ie->path, NULL, 1, 1);
8111 if (err)
8112 break;
8113 if (choice != GOT_PATCH_CHOICE_YES)
8114 break;
8115 } else {
8116 err = create_patched_content(&path_content, 0,
8117 staged_blob_id ? staged_blob_id : blob_id,
8118 ondisk_path, dirfd, de_name, ie->path,
8119 a->repo, a->patch_cb, a->patch_arg);
8120 if (err || path_content == NULL)
8121 break;
8124 err = got_object_blob_create(&new_staged_blob_id,
8125 path_content ? path_content : ondisk_path, a->repo);
8126 if (err)
8127 break;
8128 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8129 SHA1_DIGEST_LENGTH);
8130 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8131 stage = GOT_FILEIDX_STAGE_ADD;
8132 else
8133 stage = GOT_FILEIDX_STAGE_MODIFY;
8134 got_fileindex_entry_stage_set(ie, stage);
8135 if (S_ISLNK(sb.st_mode)) {
8136 int is_bad_symlink = 0;
8137 if (!a->allow_bad_symlinks) {
8138 char target_path[PATH_MAX];
8139 ssize_t target_len;
8140 target_len = readlink(ondisk_path, target_path,
8141 sizeof(target_path));
8142 if (target_len == -1) {
8143 err = got_error_from_errno2("readlink",
8144 ondisk_path);
8145 break;
8147 err = is_bad_symlink_target(&is_bad_symlink,
8148 target_path, target_len, ondisk_path,
8149 a->worktree->root_path);
8150 if (err)
8151 break;
8152 if (is_bad_symlink) {
8153 err = got_error_path(ondisk_path,
8154 GOT_ERR_BAD_SYMLINK);
8155 break;
8158 if (is_bad_symlink)
8159 got_fileindex_entry_staged_filetype_set(ie,
8160 GOT_FILEIDX_MODE_BAD_SYMLINK);
8161 else
8162 got_fileindex_entry_staged_filetype_set(ie,
8163 GOT_FILEIDX_MODE_SYMLINK);
8164 } else {
8165 got_fileindex_entry_staged_filetype_set(ie,
8166 GOT_FILEIDX_MODE_REGULAR_FILE);
8168 a->staged_something = 1;
8169 if (a->status_cb == NULL)
8170 break;
8171 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8172 get_staged_status(ie), relpath, blob_id,
8173 new_staged_blob_id, NULL, dirfd, de_name);
8174 if (err)
8175 break;
8177 * When staging the reverse of the staged diff,
8178 * implicitly unstage the file.
8180 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8181 sizeof(ie->blob_sha1)) == 0) {
8182 got_fileindex_entry_stage_set(ie,
8183 GOT_FILEIDX_STAGE_NONE);
8185 break;
8186 case GOT_STATUS_DELETE:
8187 if (staged_status == GOT_STATUS_DELETE)
8188 break;
8189 if (a->patch_cb) {
8190 int choice = GOT_PATCH_CHOICE_NONE;
8191 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8192 ie->path, NULL, 1, 1);
8193 if (err)
8194 break;
8195 if (choice == GOT_PATCH_CHOICE_NO)
8196 break;
8197 if (choice != GOT_PATCH_CHOICE_YES) {
8198 err = got_error(GOT_ERR_PATCH_CHOICE);
8199 break;
8202 stage = GOT_FILEIDX_STAGE_DELETE;
8203 got_fileindex_entry_stage_set(ie, stage);
8204 a->staged_something = 1;
8205 if (a->status_cb == NULL)
8206 break;
8207 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8208 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8209 de_name);
8210 break;
8211 case GOT_STATUS_NO_CHANGE:
8212 break;
8213 case GOT_STATUS_CONFLICT:
8214 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8215 break;
8216 case GOT_STATUS_NONEXISTENT:
8217 err = got_error_set_errno(ENOENT, relpath);
8218 break;
8219 default:
8220 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8221 break;
8224 if (path_content && unlink(path_content) == -1 && err == NULL)
8225 err = got_error_from_errno2("unlink", path_content);
8226 free(path_content);
8227 free(ondisk_path);
8228 free(new_staged_blob_id);
8229 return err;
8232 const struct got_error *
8233 got_worktree_stage(struct got_worktree *worktree,
8234 struct got_pathlist_head *paths,
8235 got_worktree_status_cb status_cb, void *status_arg,
8236 got_worktree_patch_cb patch_cb, void *patch_arg,
8237 int allow_bad_symlinks, struct got_repository *repo)
8239 const struct got_error *err = NULL, *sync_err, *unlockerr;
8240 struct got_pathlist_entry *pe;
8241 struct got_fileindex *fileindex = NULL;
8242 char *fileindex_path = NULL;
8243 struct got_reference *head_ref = NULL;
8244 struct got_object_id *head_commit_id = NULL;
8245 struct check_stage_ok_arg oka;
8246 struct stage_path_arg spa;
8248 err = lock_worktree(worktree, LOCK_EX);
8249 if (err)
8250 return err;
8252 err = got_ref_open(&head_ref, repo,
8253 got_worktree_get_head_ref_name(worktree), 0);
8254 if (err)
8255 goto done;
8256 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8257 if (err)
8258 goto done;
8259 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8260 if (err)
8261 goto done;
8263 /* Check pre-conditions before staging anything. */
8264 oka.head_commit_id = head_commit_id;
8265 oka.worktree = worktree;
8266 oka.fileindex = fileindex;
8267 oka.repo = repo;
8268 oka.have_changes = 0;
8269 TAILQ_FOREACH(pe, paths, entry) {
8270 err = worktree_status(worktree, pe->path, fileindex, repo,
8271 check_stage_ok, &oka, NULL, NULL, 1, 0);
8272 if (err)
8273 goto done;
8275 if (!oka.have_changes) {
8276 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8277 goto done;
8280 spa.worktree = worktree;
8281 spa.fileindex = fileindex;
8282 spa.repo = repo;
8283 spa.patch_cb = patch_cb;
8284 spa.patch_arg = patch_arg;
8285 spa.status_cb = status_cb;
8286 spa.status_arg = status_arg;
8287 spa.staged_something = 0;
8288 spa.allow_bad_symlinks = allow_bad_symlinks;
8289 TAILQ_FOREACH(pe, paths, entry) {
8290 err = worktree_status(worktree, pe->path, fileindex, repo,
8291 stage_path, &spa, NULL, NULL, 1, 0);
8292 if (err)
8293 goto done;
8295 if (!spa.staged_something) {
8296 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8297 goto done;
8300 sync_err = sync_fileindex(fileindex, fileindex_path);
8301 if (sync_err && err == NULL)
8302 err = sync_err;
8303 done:
8304 if (head_ref)
8305 got_ref_close(head_ref);
8306 free(head_commit_id);
8307 free(fileindex_path);
8308 if (fileindex)
8309 got_fileindex_free(fileindex);
8310 unlockerr = lock_worktree(worktree, LOCK_SH);
8311 if (unlockerr && err == NULL)
8312 err = unlockerr;
8313 return err;
8316 struct unstage_path_arg {
8317 struct got_worktree *worktree;
8318 struct got_fileindex *fileindex;
8319 struct got_repository *repo;
8320 got_worktree_checkout_cb progress_cb;
8321 void *progress_arg;
8322 got_worktree_patch_cb patch_cb;
8323 void *patch_arg;
8326 static const struct got_error *
8327 create_unstaged_content(char **path_unstaged_content,
8328 char **path_new_staged_content, struct got_object_id *blob_id,
8329 struct got_object_id *staged_blob_id, const char *relpath,
8330 struct got_repository *repo,
8331 got_worktree_patch_cb patch_cb, void *patch_arg)
8333 const struct got_error *err, *free_err;
8334 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8335 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8336 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8337 struct got_diffreg_result *diffreg_result = NULL;
8338 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8339 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8340 int fd1 = -1, fd2 = -1;
8342 *path_unstaged_content = NULL;
8343 *path_new_staged_content = NULL;
8345 err = got_object_id_str(&label1, blob_id);
8346 if (err)
8347 return err;
8349 fd1 = got_opentempfd();
8350 if (fd1 == -1) {
8351 err = got_error_from_errno("got_opentempfd");
8352 goto done;
8354 fd2 = got_opentempfd();
8355 if (fd2 == -1) {
8356 err = got_error_from_errno("got_opentempfd");
8357 goto done;
8360 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8361 if (err)
8362 goto done;
8364 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
8365 if (err)
8366 goto done;
8368 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8369 if (err)
8370 goto done;
8372 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8373 fd2);
8374 if (err)
8375 goto done;
8377 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
8378 if (err)
8379 goto done;
8381 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8382 if (err)
8383 goto done;
8385 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8386 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8387 if (err)
8388 goto done;
8390 err = got_opentemp_named(path_unstaged_content, &outfile,
8391 "got-unstaged-content");
8392 if (err)
8393 goto done;
8394 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8395 "got-new-staged-content");
8396 if (err)
8397 goto done;
8399 if (fseek(f1, 0L, SEEK_SET) == -1) {
8400 err = got_ferror(f1, GOT_ERR_IO);
8401 goto done;
8403 if (fseek(f2, 0L, SEEK_SET) == -1) {
8404 err = got_ferror(f2, GOT_ERR_IO);
8405 goto done;
8407 /* Count the number of actual changes in the diff result. */
8408 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8409 struct diff_chunk_context cc = {};
8410 diff_chunk_context_load_change(&cc, &nchunks_used,
8411 diffreg_result->result, n, 0);
8412 nchanges++;
8414 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8415 int choice;
8416 err = apply_or_reject_change(&choice, &nchunks_used,
8417 diffreg_result->result, n, relpath, f1, f2,
8418 &line_cur1, &line_cur2,
8419 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8420 if (err)
8421 goto done;
8422 if (choice == GOT_PATCH_CHOICE_YES)
8423 have_content = 1;
8424 else
8425 have_rejected_content = 1;
8426 if (choice == GOT_PATCH_CHOICE_QUIT)
8427 break;
8429 if (have_content || have_rejected_content)
8430 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8431 outfile, rejectfile);
8432 done:
8433 free(label1);
8434 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8435 err = got_error_from_errno("close");
8436 if (blob)
8437 got_object_blob_close(blob);
8438 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8439 err = got_error_from_errno("close");
8440 if (staged_blob)
8441 got_object_blob_close(staged_blob);
8442 free_err = got_diffreg_result_free(diffreg_result);
8443 if (free_err && err == NULL)
8444 err = free_err;
8445 if (f1 && fclose(f1) == EOF && err == NULL)
8446 err = got_error_from_errno2("fclose", path1);
8447 if (f2 && fclose(f2) == EOF && err == NULL)
8448 err = got_error_from_errno2("fclose", path2);
8449 if (outfile && fclose(outfile) == EOF && err == NULL)
8450 err = got_error_from_errno2("fclose", *path_unstaged_content);
8451 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8452 err = got_error_from_errno2("fclose", *path_new_staged_content);
8453 if (path1 && unlink(path1) == -1 && err == NULL)
8454 err = got_error_from_errno2("unlink", path1);
8455 if (path2 && unlink(path2) == -1 && err == NULL)
8456 err = got_error_from_errno2("unlink", path2);
8457 if (err || !have_content) {
8458 if (*path_unstaged_content &&
8459 unlink(*path_unstaged_content) == -1 && err == NULL)
8460 err = got_error_from_errno2("unlink",
8461 *path_unstaged_content);
8462 free(*path_unstaged_content);
8463 *path_unstaged_content = NULL;
8465 if (err || !have_content || !have_rejected_content) {
8466 if (*path_new_staged_content &&
8467 unlink(*path_new_staged_content) == -1 && err == NULL)
8468 err = got_error_from_errno2("unlink",
8469 *path_new_staged_content);
8470 free(*path_new_staged_content);
8471 *path_new_staged_content = NULL;
8473 free(path1);
8474 free(path2);
8475 return err;
8478 static const struct got_error *
8479 unstage_hunks(struct got_object_id *staged_blob_id,
8480 struct got_blob_object *blob_base,
8481 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8482 const char *ondisk_path, const char *label_orig,
8483 struct got_worktree *worktree, struct got_repository *repo,
8484 got_worktree_patch_cb patch_cb, void *patch_arg,
8485 got_worktree_checkout_cb progress_cb, void *progress_arg)
8487 const struct got_error *err = NULL;
8488 char *path_unstaged_content = NULL;
8489 char *path_new_staged_content = NULL;
8490 char *parent = NULL, *base_path = NULL;
8491 char *blob_base_path = NULL;
8492 struct got_object_id *new_staged_blob_id = NULL;
8493 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8494 struct stat sb;
8496 err = create_unstaged_content(&path_unstaged_content,
8497 &path_new_staged_content, blob_id, staged_blob_id,
8498 ie->path, repo, patch_cb, patch_arg);
8499 if (err)
8500 return err;
8502 if (path_unstaged_content == NULL)
8503 return NULL;
8505 if (path_new_staged_content) {
8506 err = got_object_blob_create(&new_staged_blob_id,
8507 path_new_staged_content, repo);
8508 if (err)
8509 goto done;
8512 f = fopen(path_unstaged_content, "re");
8513 if (f == NULL) {
8514 err = got_error_from_errno2("fopen",
8515 path_unstaged_content);
8516 goto done;
8518 if (fstat(fileno(f), &sb) == -1) {
8519 err = got_error_from_errno2("fstat", path_unstaged_content);
8520 goto done;
8522 if (got_fileindex_entry_staged_filetype_get(ie) ==
8523 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8524 char link_target[PATH_MAX];
8525 size_t r;
8526 r = fread(link_target, 1, sizeof(link_target), f);
8527 if (r == 0 && ferror(f)) {
8528 err = got_error_from_errno("fread");
8529 goto done;
8531 if (r >= sizeof(link_target)) { /* should not happen */
8532 err = got_error(GOT_ERR_NO_SPACE);
8533 goto done;
8535 link_target[r] = '\0';
8536 err = merge_symlink(worktree, blob_base,
8537 ondisk_path, ie->path, label_orig, link_target,
8538 worktree->base_commit_id, repo, progress_cb,
8539 progress_arg);
8540 } else {
8541 int local_changes_subsumed;
8543 err = got_path_dirname(&parent, ondisk_path);
8544 if (err)
8545 return err;
8547 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8548 parent) == -1) {
8549 err = got_error_from_errno("asprintf");
8550 base_path = NULL;
8551 goto done;
8554 err = got_opentemp_named(&blob_base_path, &f_base, base_path);
8555 if (err)
8556 goto done;
8557 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8558 blob_base);
8559 if (err)
8560 goto done;
8563 * In order the run a 3-way merge with a symlink we copy the symlink's
8564 * target path into a temporary file and use that file with diff3.
8566 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8567 err = dump_symlink_target_path_to_file(&f_deriv2,
8568 ondisk_path);
8569 if (err)
8570 goto done;
8571 } else {
8572 int fd;
8573 fd = open(ondisk_path,
8574 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8575 if (fd == -1) {
8576 err = got_error_from_errno2("open", ondisk_path);
8577 goto done;
8579 f_deriv2 = fdopen(fd, "r");
8580 if (f_deriv2 == NULL) {
8581 err = got_error_from_errno2("fdopen", ondisk_path);
8582 close(fd);
8583 goto done;
8587 err = merge_file(&local_changes_subsumed, worktree,
8588 f_base, f, f_deriv2, ondisk_path, ie->path,
8589 got_fileindex_perms_to_st(ie),
8590 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8591 repo, progress_cb, progress_arg);
8593 if (err)
8594 goto done;
8596 if (new_staged_blob_id) {
8597 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8598 SHA1_DIGEST_LENGTH);
8599 } else {
8600 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8601 got_fileindex_entry_staged_filetype_set(ie, 0);
8603 done:
8604 free(new_staged_blob_id);
8605 if (path_unstaged_content &&
8606 unlink(path_unstaged_content) == -1 && err == NULL)
8607 err = got_error_from_errno2("unlink", path_unstaged_content);
8608 if (path_new_staged_content &&
8609 unlink(path_new_staged_content) == -1 && err == NULL)
8610 err = got_error_from_errno2("unlink", path_new_staged_content);
8611 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8612 err = got_error_from_errno2("unlink", blob_base_path);
8613 if (f_base && fclose(f_base) == EOF && err == NULL)
8614 err = got_error_from_errno2("fclose", path_unstaged_content);
8615 if (f && fclose(f) == EOF && err == NULL)
8616 err = got_error_from_errno2("fclose", path_unstaged_content);
8617 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8618 err = got_error_from_errno2("fclose", ondisk_path);
8619 free(path_unstaged_content);
8620 free(path_new_staged_content);
8621 free(blob_base_path);
8622 free(parent);
8623 free(base_path);
8624 return err;
8627 static const struct got_error *
8628 unstage_path(void *arg, unsigned char status,
8629 unsigned char staged_status, const char *relpath,
8630 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8631 struct got_object_id *commit_id, int dirfd, const char *de_name)
8633 const struct got_error *err = NULL;
8634 struct unstage_path_arg *a = arg;
8635 struct got_fileindex_entry *ie;
8636 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8637 char *ondisk_path = NULL;
8638 char *id_str = NULL, *label_orig = NULL;
8639 int local_changes_subsumed;
8640 struct stat sb;
8641 int fd1 = -1, fd2 = -1;
8643 if (staged_status != GOT_STATUS_ADD &&
8644 staged_status != GOT_STATUS_MODIFY &&
8645 staged_status != GOT_STATUS_DELETE)
8646 return NULL;
8648 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8649 if (ie == NULL)
8650 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8652 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8653 == -1)
8654 return got_error_from_errno("asprintf");
8656 err = got_object_id_str(&id_str,
8657 commit_id ? commit_id : a->worktree->base_commit_id);
8658 if (err)
8659 goto done;
8660 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8661 id_str) == -1) {
8662 err = got_error_from_errno("asprintf");
8663 goto done;
8666 fd1 = got_opentempfd();
8667 if (fd1 == -1) {
8668 err = got_error_from_errno("got_opentempfd");
8669 goto done;
8671 fd2 = got_opentempfd();
8672 if (fd2 == -1) {
8673 err = got_error_from_errno("got_opentempfd");
8674 goto done;
8677 switch (staged_status) {
8678 case GOT_STATUS_MODIFY:
8679 err = got_object_open_as_blob(&blob_base, a->repo,
8680 blob_id, 8192, fd1);
8681 if (err)
8682 break;
8683 /* fall through */
8684 case GOT_STATUS_ADD:
8685 if (a->patch_cb) {
8686 if (staged_status == GOT_STATUS_ADD) {
8687 int choice = GOT_PATCH_CHOICE_NONE;
8688 err = (*a->patch_cb)(&choice, a->patch_arg,
8689 staged_status, ie->path, NULL, 1, 1);
8690 if (err)
8691 break;
8692 if (choice != GOT_PATCH_CHOICE_YES)
8693 break;
8694 } else {
8695 err = unstage_hunks(staged_blob_id,
8696 blob_base, blob_id, ie, ondisk_path,
8697 label_orig, a->worktree, a->repo,
8698 a->patch_cb, a->patch_arg,
8699 a->progress_cb, a->progress_arg);
8700 break; /* Done with this file. */
8703 err = got_object_open_as_blob(&blob_staged, a->repo,
8704 staged_blob_id, 8192, fd2);
8705 if (err)
8706 break;
8707 switch (got_fileindex_entry_staged_filetype_get(ie)) {
8708 case GOT_FILEIDX_MODE_BAD_SYMLINK:
8709 case GOT_FILEIDX_MODE_REGULAR_FILE:
8710 err = merge_blob(&local_changes_subsumed, a->worktree,
8711 blob_base, ondisk_path, relpath,
8712 got_fileindex_perms_to_st(ie), label_orig,
8713 blob_staged, commit_id ? commit_id :
8714 a->worktree->base_commit_id, a->repo,
8715 a->progress_cb, a->progress_arg);
8716 break;
8717 case GOT_FILEIDX_MODE_SYMLINK:
8718 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8719 char *staged_target;
8720 err = got_object_blob_read_to_str(
8721 &staged_target, blob_staged);
8722 if (err)
8723 goto done;
8724 err = merge_symlink(a->worktree, blob_base,
8725 ondisk_path, relpath, label_orig,
8726 staged_target, commit_id ? commit_id :
8727 a->worktree->base_commit_id,
8728 a->repo, a->progress_cb, a->progress_arg);
8729 free(staged_target);
8730 } else {
8731 err = merge_blob(&local_changes_subsumed,
8732 a->worktree, blob_base, ondisk_path,
8733 relpath, got_fileindex_perms_to_st(ie),
8734 label_orig, blob_staged,
8735 commit_id ? commit_id :
8736 a->worktree->base_commit_id, a->repo,
8737 a->progress_cb, a->progress_arg);
8739 break;
8740 default:
8741 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8742 break;
8744 if (err == NULL) {
8745 got_fileindex_entry_stage_set(ie,
8746 GOT_FILEIDX_STAGE_NONE);
8747 got_fileindex_entry_staged_filetype_set(ie, 0);
8749 break;
8750 case GOT_STATUS_DELETE:
8751 if (a->patch_cb) {
8752 int choice = GOT_PATCH_CHOICE_NONE;
8753 err = (*a->patch_cb)(&choice, a->patch_arg,
8754 staged_status, ie->path, NULL, 1, 1);
8755 if (err)
8756 break;
8757 if (choice == GOT_PATCH_CHOICE_NO)
8758 break;
8759 if (choice != GOT_PATCH_CHOICE_YES) {
8760 err = got_error(GOT_ERR_PATCH_CHOICE);
8761 break;
8764 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8765 got_fileindex_entry_staged_filetype_set(ie, 0);
8766 err = get_file_status(&status, &sb, ie, ondisk_path,
8767 dirfd, de_name, a->repo);
8768 if (err)
8769 break;
8770 err = (*a->progress_cb)(a->progress_arg, status, relpath);
8771 break;
8773 done:
8774 free(ondisk_path);
8775 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8776 err = got_error_from_errno("close");
8777 if (blob_base)
8778 got_object_blob_close(blob_base);
8779 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8780 err = got_error_from_errno("close");
8781 if (blob_staged)
8782 got_object_blob_close(blob_staged);
8783 free(id_str);
8784 free(label_orig);
8785 return err;
8788 const struct got_error *
8789 got_worktree_unstage(struct got_worktree *worktree,
8790 struct got_pathlist_head *paths,
8791 got_worktree_checkout_cb progress_cb, void *progress_arg,
8792 got_worktree_patch_cb patch_cb, void *patch_arg,
8793 struct got_repository *repo)
8795 const struct got_error *err = NULL, *sync_err, *unlockerr;
8796 struct got_pathlist_entry *pe;
8797 struct got_fileindex *fileindex = NULL;
8798 char *fileindex_path = NULL;
8799 struct unstage_path_arg upa;
8801 err = lock_worktree(worktree, LOCK_EX);
8802 if (err)
8803 return err;
8805 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8806 if (err)
8807 goto done;
8809 upa.worktree = worktree;
8810 upa.fileindex = fileindex;
8811 upa.repo = repo;
8812 upa.progress_cb = progress_cb;
8813 upa.progress_arg = progress_arg;
8814 upa.patch_cb = patch_cb;
8815 upa.patch_arg = patch_arg;
8816 TAILQ_FOREACH(pe, paths, entry) {
8817 err = worktree_status(worktree, pe->path, fileindex, repo,
8818 unstage_path, &upa, NULL, NULL, 1, 0);
8819 if (err)
8820 goto done;
8823 sync_err = sync_fileindex(fileindex, fileindex_path);
8824 if (sync_err && err == NULL)
8825 err = sync_err;
8826 done:
8827 free(fileindex_path);
8828 if (fileindex)
8829 got_fileindex_free(fileindex);
8830 unlockerr = lock_worktree(worktree, LOCK_SH);
8831 if (unlockerr && err == NULL)
8832 err = unlockerr;
8833 return err;
8836 struct report_file_info_arg {
8837 struct got_worktree *worktree;
8838 got_worktree_path_info_cb info_cb;
8839 void *info_arg;
8840 struct got_pathlist_head *paths;
8841 got_cancel_cb cancel_cb;
8842 void *cancel_arg;
8845 static const struct got_error *
8846 report_file_info(void *arg, struct got_fileindex_entry *ie)
8848 struct report_file_info_arg *a = arg;
8849 struct got_pathlist_entry *pe;
8850 struct got_object_id blob_id, staged_blob_id, commit_id;
8851 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8852 struct got_object_id *commit_idp = NULL;
8853 int stage;
8855 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8856 return got_error(GOT_ERR_CANCELLED);
8858 TAILQ_FOREACH(pe, a->paths, entry) {
8859 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8860 got_path_is_child(ie->path, pe->path, pe->path_len))
8861 break;
8863 if (pe == NULL) /* not found */
8864 return NULL;
8866 if (got_fileindex_entry_has_blob(ie)) {
8867 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8868 blob_idp = &blob_id;
8870 stage = got_fileindex_entry_stage_get(ie);
8871 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8872 stage == GOT_FILEIDX_STAGE_ADD) {
8873 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8874 SHA1_DIGEST_LENGTH);
8875 staged_blob_idp = &staged_blob_id;
8878 if (got_fileindex_entry_has_commit(ie)) {
8879 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8880 commit_idp = &commit_id;
8883 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8884 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8887 const struct got_error *
8888 got_worktree_path_info(struct got_worktree *worktree,
8889 struct got_pathlist_head *paths,
8890 got_worktree_path_info_cb info_cb, void *info_arg,
8891 got_cancel_cb cancel_cb, void *cancel_arg)
8894 const struct got_error *err = NULL, *unlockerr;
8895 struct got_fileindex *fileindex = NULL;
8896 char *fileindex_path = NULL;
8897 struct report_file_info_arg arg;
8899 err = lock_worktree(worktree, LOCK_SH);
8900 if (err)
8901 return err;
8903 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8904 if (err)
8905 goto done;
8907 arg.worktree = worktree;
8908 arg.info_cb = info_cb;
8909 arg.info_arg = info_arg;
8910 arg.paths = paths;
8911 arg.cancel_cb = cancel_cb;
8912 arg.cancel_arg = cancel_arg;
8913 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8914 &arg);
8915 done:
8916 free(fileindex_path);
8917 if (fileindex)
8918 got_fileindex_free(fileindex);
8919 unlockerr = lock_worktree(worktree, LOCK_UN);
8920 if (unlockerr && err == NULL)
8921 err = unlockerr;
8922 return err;
8925 static const struct got_error *
8926 patch_check_path(const char *p, char **path, unsigned char *status,
8927 unsigned char *staged_status, struct got_fileindex *fileindex,
8928 struct got_worktree *worktree, struct got_repository *repo)
8930 const struct got_error *err;
8931 struct got_fileindex_entry *ie;
8932 struct stat sb;
8933 char *ondisk_path = NULL;
8935 err = got_worktree_resolve_path(path, worktree, p);
8936 if (err)
8937 return err;
8939 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
8940 *path[0] ? "/" : "", *path) == -1)
8941 return got_error_from_errno("asprintf");
8943 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
8944 if (ie) {
8945 *staged_status = get_staged_status(ie);
8946 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
8947 repo);
8948 if (err)
8949 goto done;
8950 } else {
8951 *staged_status = GOT_STATUS_NO_CHANGE;
8952 *status = GOT_STATUS_UNVERSIONED;
8953 if (lstat(ondisk_path, &sb) == -1) {
8954 if (errno != ENOENT) {
8955 err = got_error_from_errno2("lstat",
8956 ondisk_path);
8957 goto done;
8959 *status = GOT_STATUS_NONEXISTENT;
8963 done:
8964 free(ondisk_path);
8965 return err;
8968 static const struct got_error *
8969 patch_can_rm(const char *path, unsigned char status,
8970 unsigned char staged_status)
8972 if (status == GOT_STATUS_NONEXISTENT)
8973 return got_error_set_errno(ENOENT, path);
8974 if (status != GOT_STATUS_NO_CHANGE &&
8975 status != GOT_STATUS_ADD &&
8976 status != GOT_STATUS_MODIFY &&
8977 status != GOT_STATUS_MODE_CHANGE)
8978 return got_error_path(path, GOT_ERR_FILE_STATUS);
8979 if (staged_status == GOT_STATUS_DELETE)
8980 return got_error_path(path, GOT_ERR_FILE_STATUS);
8981 return NULL;
8984 static const struct got_error *
8985 patch_can_add(const char *path, unsigned char status)
8987 if (status != GOT_STATUS_NONEXISTENT)
8988 return got_error_path(path, GOT_ERR_FILE_STATUS);
8989 return NULL;
8992 static const struct got_error *
8993 patch_can_edit(const char *path, unsigned char status,
8994 unsigned char staged_status)
8996 if (status == GOT_STATUS_NONEXISTENT)
8997 return got_error_set_errno(ENOENT, path);
8998 if (status != GOT_STATUS_NO_CHANGE &&
8999 status != GOT_STATUS_ADD &&
9000 status != GOT_STATUS_MODIFY)
9001 return got_error_path(path, GOT_ERR_FILE_STATUS);
9002 if (staged_status == GOT_STATUS_DELETE)
9003 return got_error_path(path, GOT_ERR_FILE_STATUS);
9004 return NULL;
9007 const struct got_error *
9008 got_worktree_patch_prepare(struct got_fileindex **fileindex,
9009 char **fileindex_path, struct got_worktree *worktree)
9011 return open_fileindex(fileindex, fileindex_path, worktree);
9014 const struct got_error *
9015 got_worktree_patch_check_path(const char *old, const char *new,
9016 char **oldpath, char **newpath, struct got_worktree *worktree,
9017 struct got_repository *repo, struct got_fileindex *fileindex)
9019 const struct got_error *err = NULL;
9020 int file_renamed = 0;
9021 unsigned char status_old, staged_status_old;
9022 unsigned char status_new, staged_status_new;
9024 *oldpath = NULL;
9025 *newpath = NULL;
9027 err = patch_check_path(old != NULL ? old : new, oldpath,
9028 &status_old, &staged_status_old, fileindex, worktree, repo);
9029 if (err)
9030 goto done;
9032 err = patch_check_path(new != NULL ? new : old, newpath,
9033 &status_new, &staged_status_new, fileindex, worktree, repo);
9034 if (err)
9035 goto done;
9037 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9038 file_renamed = 1;
9040 if (old != NULL && new == NULL)
9041 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9042 else if (file_renamed) {
9043 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9044 if (err == NULL)
9045 err = patch_can_add(*newpath, status_new);
9046 } else if (old == NULL)
9047 err = patch_can_add(*newpath, status_new);
9048 else
9049 err = patch_can_edit(*newpath, status_new, staged_status_new);
9051 done:
9052 if (err) {
9053 free(*oldpath);
9054 *oldpath = NULL;
9055 free(*newpath);
9056 *newpath = NULL;
9058 return err;
9061 const struct got_error *
9062 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9063 struct got_worktree *worktree, struct got_fileindex *fileindex,
9064 got_worktree_checkout_cb progress_cb, void *progress_arg)
9066 struct schedule_addition_args saa;
9068 memset(&saa, 0, sizeof(saa));
9069 saa.worktree = worktree;
9070 saa.fileindex = fileindex;
9071 saa.progress_cb = progress_cb;
9072 saa.progress_arg = progress_arg;
9073 saa.repo = repo;
9075 return worktree_status(worktree, path, fileindex, repo,
9076 schedule_addition, &saa, NULL, NULL, 1, 0);
9079 const struct got_error *
9080 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9081 struct got_worktree *worktree, struct got_fileindex *fileindex,
9082 got_worktree_delete_cb progress_cb, void *progress_arg)
9084 struct schedule_deletion_args sda;
9086 memset(&sda, 0, sizeof(sda));
9087 sda.worktree = worktree;
9088 sda.fileindex = fileindex;
9089 sda.progress_cb = progress_cb;
9090 sda.progress_arg = progress_arg;
9091 sda.repo = repo;
9092 sda.delete_local_mods = 0;
9093 sda.keep_on_disk = 0;
9094 sda.ignore_missing_paths = 0;
9095 sda.status_codes = NULL;
9097 return worktree_status(worktree, path, fileindex, repo,
9098 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9101 const struct got_error *
9102 got_worktree_patch_complete(struct got_fileindex *fileindex,
9103 const char *fileindex_path)
9105 const struct got_error *err = NULL;
9107 err = sync_fileindex(fileindex, fileindex_path);
9108 got_fileindex_free(fileindex);
9110 return err;