Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/stat.h>
18 #include <sys/queue.h>
19 #include <sys/tree.h>
21 #include <dirent.h>
22 #include <limits.h>
23 #include <stddef.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <time.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <sha1.h>
32 #include <zlib.h>
33 #include <fnmatch.h>
34 #include <libgen.h>
35 #include <uuid.h>
36 #include <util.h>
38 #include "got_error.h"
39 #include "got_repository.h"
40 #include "got_reference.h"
41 #include "got_object.h"
42 #include "got_path.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_opentemp.h"
46 #include "got_diff.h"
48 #include "got_lib_worktree.h"
49 #include "got_lib_sha1.h"
50 #include "got_lib_fileindex.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_object_idset.h"
57 #include "got_lib_diff.h"
58 #include "got_lib_gotconfig.h"
60 #ifndef MIN
61 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
62 #endif
64 #define GOT_MERGE_LABEL_MERGED "merged change"
65 #define GOT_MERGE_LABEL_BASE "3-way merge base"
67 static const struct got_error *
68 create_meta_file(const char *path_got, const char *name, const char *content)
69 {
70 const struct got_error *err = NULL;
71 char *path;
73 if (asprintf(&path, "%s/%s", path_got, name) == -1)
74 return got_error_from_errno("asprintf");
76 err = got_path_create_file(path, content);
77 free(path);
78 return err;
79 }
81 static const struct got_error *
82 update_meta_file(const char *path_got, const char *name, const char *content)
83 {
84 const struct got_error *err = NULL;
85 FILE *tmpfile = NULL;
86 char *tmppath = NULL;
87 char *path = NULL;
89 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
90 err = got_error_from_errno("asprintf");
91 path = NULL;
92 goto done;
93 }
95 err = got_opentemp_named(&tmppath, &tmpfile, path);
96 if (err)
97 goto done;
99 if (content) {
100 int len = fprintf(tmpfile, "%s\n", content);
101 if (len != strlen(content) + 1) {
102 err = got_error_from_errno2("fprintf", tmppath);
103 goto done;
107 if (rename(tmppath, path) != 0) {
108 err = got_error_from_errno3("rename", tmppath, path);
109 unlink(tmppath);
110 goto done;
113 done:
114 if (fclose(tmpfile) == EOF && err == NULL)
115 err = got_error_from_errno2("fclose", tmppath);
116 free(tmppath);
117 return err;
120 static const struct got_error *
121 write_head_ref(const char *path_got, struct got_reference *head_ref)
123 const struct got_error *err = NULL;
124 char *refstr = NULL;
126 if (got_ref_is_symbolic(head_ref)) {
127 refstr = got_ref_to_str(head_ref);
128 if (refstr == NULL)
129 return got_error_from_errno("got_ref_to_str");
130 } else {
131 refstr = strdup(got_ref_get_name(head_ref));
132 if (refstr == NULL)
133 return got_error_from_errno("strdup");
135 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
136 free(refstr);
137 return err;
140 const struct got_error *
141 got_worktree_init(const char *path, struct got_reference *head_ref,
142 const char *prefix, struct got_repository *repo)
144 const struct got_error *err = NULL;
145 struct got_object_id *commit_id = NULL;
146 uuid_t uuid;
147 uint32_t uuid_status;
148 int obj_type;
149 char *path_got = NULL;
150 char *formatstr = NULL;
151 char *absprefix = NULL;
152 char *basestr = NULL;
153 char *uuidstr = NULL;
155 if (strcmp(path, got_repo_get_path(repo)) == 0) {
156 err = got_error(GOT_ERR_WORKTREE_REPO);
157 goto done;
160 err = got_ref_resolve(&commit_id, repo, head_ref);
161 if (err)
162 return err;
163 err = got_object_get_type(&obj_type, repo, commit_id);
164 if (err)
165 return err;
166 if (obj_type != GOT_OBJ_TYPE_COMMIT)
167 return got_error(GOT_ERR_OBJ_TYPE);
169 if (!got_path_is_absolute(prefix)) {
170 if (asprintf(&absprefix, "/%s", prefix) == -1)
171 return got_error_from_errno("asprintf");
174 /* Create top-level directory (may already exist). */
175 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
176 err = got_error_from_errno2("mkdir", path);
177 goto done;
180 /* Create .got directory (may already exist). */
181 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
182 err = got_error_from_errno("asprintf");
183 goto done;
185 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
186 err = got_error_from_errno2("mkdir", path_got);
187 goto done;
190 /* Create an empty lock file. */
191 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
192 if (err)
193 goto done;
195 /* Create an empty file index. */
196 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
197 if (err)
198 goto done;
200 /* Write the HEAD reference. */
201 err = write_head_ref(path_got, head_ref);
202 if (err)
203 goto done;
205 /* Record our base commit. */
206 err = got_object_id_str(&basestr, commit_id);
207 if (err)
208 goto done;
209 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
210 if (err)
211 goto done;
213 /* Store path to repository. */
214 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
215 got_repo_get_path(repo));
216 if (err)
217 goto done;
219 /* Store in-repository path prefix. */
220 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
221 absprefix ? absprefix : prefix);
222 if (err)
223 goto done;
225 /* Generate UUID. */
226 uuid_create(&uuid, &uuid_status);
227 if (uuid_status != uuid_s_ok) {
228 err = got_error_uuid(uuid_status, "uuid_create");
229 goto done;
231 uuid_to_string(&uuid, &uuidstr, &uuid_status);
232 if (uuid_status != uuid_s_ok) {
233 err = got_error_uuid(uuid_status, "uuid_to_string");
234 goto done;
236 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
237 if (err)
238 goto done;
240 /* Stamp work tree with format file. */
241 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
242 err = got_error_from_errno("asprintf");
243 goto done;
245 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
246 if (err)
247 goto done;
249 done:
250 free(commit_id);
251 free(path_got);
252 free(formatstr);
253 free(absprefix);
254 free(basestr);
255 free(uuidstr);
256 return err;
259 const struct got_error *
260 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
261 const char *path_prefix)
263 char *absprefix = NULL;
265 if (!got_path_is_absolute(path_prefix)) {
266 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
267 return got_error_from_errno("asprintf");
269 *match = (strcmp(absprefix ? absprefix : path_prefix,
270 worktree->path_prefix) == 0);
271 free(absprefix);
272 return NULL;
275 const char *
276 got_worktree_get_head_ref_name(struct got_worktree *worktree)
278 return worktree->head_ref_name;
281 const struct got_error *
282 got_worktree_set_head_ref(struct got_worktree *worktree,
283 struct got_reference *head_ref)
285 const struct got_error *err = NULL;
286 char *path_got = NULL, *head_ref_name = NULL;
288 if (asprintf(&path_got, "%s/%s", worktree->root_path,
289 GOT_WORKTREE_GOT_DIR) == -1) {
290 err = got_error_from_errno("asprintf");
291 path_got = NULL;
292 goto done;
295 head_ref_name = strdup(got_ref_get_name(head_ref));
296 if (head_ref_name == NULL) {
297 err = got_error_from_errno("strdup");
298 goto done;
301 err = write_head_ref(path_got, head_ref);
302 if (err)
303 goto done;
305 free(worktree->head_ref_name);
306 worktree->head_ref_name = head_ref_name;
307 done:
308 free(path_got);
309 if (err)
310 free(head_ref_name);
311 return err;
314 struct got_object_id *
315 got_worktree_get_base_commit_id(struct got_worktree *worktree)
317 return worktree->base_commit_id;
320 const struct got_error *
321 got_worktree_set_base_commit_id(struct got_worktree *worktree,
322 struct got_repository *repo, struct got_object_id *commit_id)
324 const struct got_error *err;
325 struct got_object *obj = NULL;
326 char *id_str = NULL;
327 char *path_got = NULL;
329 if (asprintf(&path_got, "%s/%s", worktree->root_path,
330 GOT_WORKTREE_GOT_DIR) == -1) {
331 err = got_error_from_errno("asprintf");
332 path_got = NULL;
333 goto done;
336 err = got_object_open(&obj, repo, commit_id);
337 if (err)
338 return err;
340 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
341 err = got_error(GOT_ERR_OBJ_TYPE);
342 goto done;
345 /* Record our base commit. */
346 err = got_object_id_str(&id_str, commit_id);
347 if (err)
348 goto done;
349 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
350 if (err)
351 goto done;
353 free(worktree->base_commit_id);
354 worktree->base_commit_id = got_object_id_dup(commit_id);
355 if (worktree->base_commit_id == NULL) {
356 err = got_error_from_errno("got_object_id_dup");
357 goto done;
359 done:
360 if (obj)
361 got_object_close(obj);
362 free(id_str);
363 free(path_got);
364 return err;
367 const struct got_gotconfig *
368 got_worktree_get_gotconfig(struct got_worktree *worktree)
370 return worktree->gotconfig;
373 static const struct got_error *
374 lock_worktree(struct got_worktree *worktree, int operation)
376 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
377 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
378 : got_error_from_errno2("flock",
379 got_worktree_get_root_path(worktree)));
380 return NULL;
383 static const struct got_error *
384 add_dir_on_disk(struct got_worktree *worktree, const char *path)
386 const struct got_error *err = NULL;
387 char *abspath;
389 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
390 return got_error_from_errno("asprintf");
392 err = got_path_mkdir(abspath);
393 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
394 struct stat sb;
395 err = NULL;
396 if (lstat(abspath, &sb) == -1) {
397 err = got_error_from_errno2("lstat", abspath);
398 } else if (!S_ISDIR(sb.st_mode)) {
399 /* TODO directory is obstructed; do something */
400 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
403 free(abspath);
404 return err;
407 static const struct got_error *
408 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
410 const struct got_error *err = NULL;
411 uint8_t fbuf1[8192];
412 uint8_t fbuf2[8192];
413 size_t flen1 = 0, flen2 = 0;
415 *same = 1;
417 for (;;) {
418 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
419 if (flen1 == 0 && ferror(f1)) {
420 err = got_error_from_errno("fread");
421 break;
423 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
424 if (flen2 == 0 && ferror(f2)) {
425 err = got_error_from_errno("fread");
426 break;
428 if (flen1 == 0) {
429 if (flen2 != 0)
430 *same = 0;
431 break;
432 } else if (flen2 == 0) {
433 if (flen1 != 0)
434 *same = 0;
435 break;
436 } else if (flen1 == flen2) {
437 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
438 *same = 0;
439 break;
441 } else {
442 *same = 0;
443 break;
447 return err;
450 static const struct got_error *
451 check_files_equal(int *same, FILE *f1, FILE *f2)
453 struct stat sb;
454 size_t size1, size2;
456 *same = 1;
458 if (fstat(fileno(f1), &sb) != 0)
459 return got_error_from_errno("fstat");
460 size1 = sb.st_size;
462 if (fstat(fileno(f2), &sb) != 0)
463 return got_error_from_errno("fstat");
464 size2 = sb.st_size;
466 if (size1 != size2) {
467 *same = 0;
468 return NULL;
471 if (fseek(f1, 0L, SEEK_SET) == -1)
472 return got_ferror(f1, GOT_ERR_IO);
473 if (fseek(f2, 0L, SEEK_SET) == -1)
474 return got_ferror(f2, GOT_ERR_IO);
476 return check_file_contents_equal(same, f1, f2);
479 static const struct got_error *
480 copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
482 uint8_t fbuf[65536];
483 size_t flen;
484 ssize_t outlen;
486 *outsize = 0;
488 if (fseek(f, 0L, SEEK_SET) == -1)
489 return got_ferror(f, GOT_ERR_IO);
491 for (;;) {
492 flen = fread(fbuf, 1, sizeof(fbuf), f);
493 if (flen == 0) {
494 if (ferror(f))
495 return got_error_from_errno("fread");
496 if (feof(f))
497 break;
499 outlen = write(outfd, fbuf, flen);
500 if (outlen == -1)
501 return got_error_from_errno("write");
502 if (outlen != flen)
503 return got_error(GOT_ERR_IO);
504 *outsize += outlen;
507 return NULL;
510 static const struct got_error *
511 merge_binary_file(int *overlapcnt, int merged_fd,
512 FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
513 const char *label_deriv, const char *label_orig, const char *label_deriv2,
514 const char *ondisk_path)
516 const struct got_error *err = NULL;
517 int same_content, changed_deriv, changed_deriv2;
518 int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
519 off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
520 char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
521 char *base_path_orig = NULL, *base_path_deriv = NULL;
522 char *base_path_deriv2 = NULL;
524 *overlapcnt = 0;
526 err = check_files_equal(&same_content, f_deriv, f_deriv2);
527 if (err)
528 return err;
530 if (same_content)
531 return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
533 err = check_files_equal(&same_content, f_deriv, f_orig);
534 if (err)
535 return err;
536 changed_deriv = !same_content;
537 err = check_files_equal(&same_content, f_deriv2, f_orig);
538 if (err)
539 return err;
540 changed_deriv2 = !same_content;
542 if (changed_deriv && changed_deriv2) {
543 *overlapcnt = 1;
544 if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
545 err = got_error_from_errno("asprintf");
546 goto done;
548 if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
549 err = got_error_from_errno("asprintf");
550 goto done;
552 if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
553 err = got_error_from_errno("asprintf");
554 goto done;
556 err = got_opentemp_named_fd(&path_orig, &fd_orig,
557 base_path_orig);
558 if (err)
559 goto done;
560 err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
561 base_path_deriv);
562 if (err)
563 goto done;
564 err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
565 base_path_deriv2);
566 if (err)
567 goto done;
568 err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
569 if (err)
570 goto done;
571 err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
572 if (err)
573 goto done;
574 err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
575 if (err)
576 goto done;
577 if (dprintf(merged_fd, "Binary files differ and cannot be "
578 "merged automatically:\n") < 0) {
579 err = got_error_from_errno("dprintf");
580 goto done;
582 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
583 GOT_DIFF_CONFLICT_MARKER_BEGIN,
584 label_deriv ? " " : "",
585 label_deriv ? label_deriv : "",
586 path_deriv) < 0) {
587 err = got_error_from_errno("dprintf");
588 goto done;
590 if (size_orig > 0) {
591 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
592 GOT_DIFF_CONFLICT_MARKER_ORIG,
593 label_orig ? " " : "",
594 label_orig ? label_orig : "",
595 path_orig) < 0) {
596 err = got_error_from_errno("dprintf");
597 goto done;
600 if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
601 GOT_DIFF_CONFLICT_MARKER_SEP,
602 path_deriv2,
603 GOT_DIFF_CONFLICT_MARKER_END,
604 label_deriv2 ? " " : "",
605 label_deriv2 ? label_deriv2 : "") < 0) {
606 err = got_error_from_errno("dprintf");
607 goto done;
609 } else if (changed_deriv)
610 err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
611 else if (changed_deriv2)
612 err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
613 done:
614 if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
615 err == NULL)
616 err = got_error_from_errno2("unlink", path_orig);
617 if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
618 err = got_error_from_errno2("close", path_orig);
619 if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
620 err = got_error_from_errno2("close", path_deriv);
621 if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
622 err = got_error_from_errno2("close", path_deriv2);
623 free(path_orig);
624 free(path_deriv);
625 free(path_deriv2);
626 free(base_path_orig);
627 free(base_path_deriv);
628 free(base_path_deriv2);
629 return err;
632 /*
633 * Perform a 3-way merge where the file f_orig acts as the common
634 * ancestor, the file f_deriv acts as the first derived version,
635 * and the file f_deriv2 acts as the second derived version.
636 * The merge result will be written to a new file at ondisk_path; any
637 * existing file at this path will be replaced.
638 */
639 static const struct got_error *
640 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
641 FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
642 const char *path, uint16_t st_mode,
643 const char *label_orig, const char *label_deriv, const char *label_deriv2,
644 enum got_diff_algorithm diff_algo, struct got_repository *repo,
645 got_worktree_checkout_cb progress_cb, void *progress_arg)
647 const struct got_error *err = NULL;
648 int merged_fd = -1;
649 FILE *f_merged = NULL;
650 char *merged_path = NULL, *base_path = NULL;
651 int overlapcnt = 0;
652 char *parent = NULL;
654 *local_changes_subsumed = 0;
656 err = got_path_dirname(&parent, ondisk_path);
657 if (err)
658 return err;
660 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
661 err = got_error_from_errno("asprintf");
662 goto done;
665 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
666 if (err)
667 goto done;
669 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
670 f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
671 if (err) {
672 if (err->code != GOT_ERR_FILE_BINARY)
673 goto done;
674 err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
675 f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
676 ondisk_path);
677 if (err)
678 goto done;
681 err = (*progress_cb)(progress_arg,
682 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
683 if (err)
684 goto done;
686 if (fsync(merged_fd) != 0) {
687 err = got_error_from_errno("fsync");
688 goto done;
691 f_merged = fdopen(merged_fd, "r");
692 if (f_merged == NULL) {
693 err = got_error_from_errno("fdopen");
694 goto done;
696 merged_fd = -1;
698 /* Check if a clean merge has subsumed all local changes. */
699 if (overlapcnt == 0) {
700 err = check_files_equal(local_changes_subsumed, f_deriv,
701 f_merged);
702 if (err)
703 goto done;
706 if (fchmod(fileno(f_merged), st_mode) != 0) {
707 err = got_error_from_errno2("fchmod", merged_path);
708 goto done;
711 if (rename(merged_path, ondisk_path) != 0) {
712 err = got_error_from_errno3("rename", merged_path,
713 ondisk_path);
714 goto done;
716 done:
717 if (err) {
718 if (merged_path)
719 unlink(merged_path);
721 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
722 err = got_error_from_errno("close");
723 if (f_merged && fclose(f_merged) == EOF && err == NULL)
724 err = got_error_from_errno("fclose");
725 free(merged_path);
726 free(base_path);
727 free(parent);
728 return err;
731 static const struct got_error *
732 update_symlink(const char *ondisk_path, const char *target_path,
733 size_t target_len)
735 /* This is not atomic but matches what 'ln -sf' does. */
736 if (unlink(ondisk_path) == -1)
737 return got_error_from_errno2("unlink", ondisk_path);
738 if (symlink(target_path, ondisk_path) == -1)
739 return got_error_from_errno3("symlink", target_path,
740 ondisk_path);
741 return NULL;
744 /*
745 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
746 * in the work tree with a file that contains conflict markers and the
747 * conflicting target paths of the original version, a "derived version"
748 * of a symlink from an incoming change, and a local version of the symlink.
750 * The original versions's target path can be NULL if it is not available,
751 * such as if both derived versions added a new symlink at the same path.
753 * The incoming derived symlink target is NULL in case the incoming change
754 * has deleted this symlink.
755 */
756 static const struct got_error *
757 install_symlink_conflict(const char *deriv_target,
758 struct got_object_id *deriv_base_commit_id, const char *orig_target,
759 const char *label_orig, const char *local_target, const char *ondisk_path)
761 const struct got_error *err;
762 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
763 FILE *f = NULL;
765 err = got_object_id_str(&id_str, deriv_base_commit_id);
766 if (err)
767 return got_error_from_errno("asprintf");
769 if (asprintf(&label_deriv, "%s: commit %s",
770 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
771 err = got_error_from_errno("asprintf");
772 goto done;
775 err = got_opentemp_named(&path, &f, "got-symlink-conflict");
776 if (err)
777 goto done;
779 if (fchmod(fileno(f), GOT_DEFAULT_FILE_MODE) == -1) {
780 err = got_error_from_errno2("fchmod", path);
781 goto done;
784 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
785 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
786 deriv_target ? deriv_target : "(symlink was deleted)",
787 orig_target ? label_orig : "",
788 orig_target ? "\n" : "",
789 orig_target ? orig_target : "",
790 orig_target ? "\n" : "",
791 GOT_DIFF_CONFLICT_MARKER_SEP,
792 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
793 err = got_error_from_errno2("fprintf", path);
794 goto done;
797 if (unlink(ondisk_path) == -1) {
798 err = got_error_from_errno2("unlink", ondisk_path);
799 goto done;
801 if (rename(path, ondisk_path) == -1) {
802 err = got_error_from_errno3("rename", path, ondisk_path);
803 goto done;
805 done:
806 if (f != NULL && fclose(f) == EOF && err == NULL)
807 err = got_error_from_errno2("fclose", path);
808 free(path);
809 free(id_str);
810 free(label_deriv);
811 return err;
814 /* forward declaration */
815 static const struct got_error *
816 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
817 const char *, const char *, uint16_t, const char *,
818 struct got_blob_object *, struct got_object_id *,
819 struct got_repository *, got_worktree_checkout_cb, void *);
821 /*
822 * Merge a symlink into the work tree, where blob_orig acts as the common
823 * ancestor, deriv_target is the link target of the first derived version,
824 * and the symlink on disk acts as the second derived version.
825 * Assume that contents of both blobs represent symlinks.
826 */
827 static const struct got_error *
828 merge_symlink(struct got_worktree *worktree,
829 struct got_blob_object *blob_orig, const char *ondisk_path,
830 const char *path, const char *label_orig, const char *deriv_target,
831 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
832 got_worktree_checkout_cb progress_cb, void *progress_arg)
834 const struct got_error *err = NULL;
835 char *ancestor_target = NULL;
836 struct stat sb;
837 ssize_t ondisk_len, deriv_len;
838 char ondisk_target[PATH_MAX];
839 int have_local_change = 0;
840 int have_incoming_change = 0;
842 if (lstat(ondisk_path, &sb) == -1)
843 return got_error_from_errno2("lstat", ondisk_path);
845 ondisk_len = readlink(ondisk_path, ondisk_target,
846 sizeof(ondisk_target));
847 if (ondisk_len == -1) {
848 err = got_error_from_errno2("readlink",
849 ondisk_path);
850 goto done;
852 ondisk_target[ondisk_len] = '\0';
854 if (blob_orig) {
855 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
856 if (err)
857 goto done;
860 if (ancestor_target == NULL ||
861 (ondisk_len != strlen(ancestor_target) ||
862 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
863 have_local_change = 1;
865 deriv_len = strlen(deriv_target);
866 if (ancestor_target == NULL ||
867 (deriv_len != strlen(ancestor_target) ||
868 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
869 have_incoming_change = 1;
871 if (!have_local_change && !have_incoming_change) {
872 if (ancestor_target) {
873 /* Both sides made the same change. */
874 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
875 path);
876 } else if (deriv_len == ondisk_len &&
877 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
878 /* Both sides added the same symlink. */
879 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
880 path);
881 } else {
882 /* Both sides added symlinks which don't match. */
883 err = install_symlink_conflict(deriv_target,
884 deriv_base_commit_id, ancestor_target,
885 label_orig, ondisk_target, ondisk_path);
886 if (err)
887 goto done;
888 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
889 path);
891 } else if (!have_local_change && have_incoming_change) {
892 /* Apply the incoming change. */
893 err = update_symlink(ondisk_path, deriv_target,
894 strlen(deriv_target));
895 if (err)
896 goto done;
897 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
898 } else if (have_local_change && have_incoming_change) {
899 if (deriv_len == ondisk_len &&
900 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
901 /* Both sides made the same change. */
902 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
903 path);
904 } else {
905 err = install_symlink_conflict(deriv_target,
906 deriv_base_commit_id, ancestor_target, label_orig,
907 ondisk_target, ondisk_path);
908 if (err)
909 goto done;
910 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
911 path);
915 done:
916 free(ancestor_target);
917 return err;
920 static const struct got_error *
921 dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
923 const struct got_error *err = NULL;
924 char target_path[PATH_MAX];
925 ssize_t target_len;
926 size_t n;
927 FILE *f;
929 *outfile = NULL;
931 f = got_opentemp();
932 if (f == NULL)
933 return got_error_from_errno("got_opentemp");
934 target_len = readlink(ondisk_path, target_path, sizeof(target_path));
935 if (target_len == -1) {
936 err = got_error_from_errno2("readlink", ondisk_path);
937 goto done;
939 n = fwrite(target_path, 1, target_len, f);
940 if (n != target_len) {
941 err = got_ferror(f, GOT_ERR_IO);
942 goto done;
944 if (fflush(f) == EOF) {
945 err = got_error_from_errno("fflush");
946 goto done;
948 if (fseek(f, 0L, SEEK_SET) == -1) {
949 err = got_ferror(f, GOT_ERR_IO);
950 goto done;
952 done:
953 if (err)
954 fclose(f);
955 else
956 *outfile = f;
957 return err;
960 /*
961 * Perform a 3-way merge where blob_orig acts as the common ancestor,
962 * blob_deriv acts as the first derived version, and the file on disk
963 * acts as the second derived version.
964 */
965 static const struct got_error *
966 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
967 struct got_blob_object *blob_orig, const char *ondisk_path,
968 const char *path, uint16_t st_mode, const char *label_orig,
969 struct got_blob_object *blob_deriv,
970 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
971 got_worktree_checkout_cb progress_cb, void *progress_arg)
973 const struct got_error *err = NULL;
974 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
975 char *blob_orig_path = NULL;
976 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
977 char *label_deriv = NULL, *parent = NULL;
979 *local_changes_subsumed = 0;
981 err = got_path_dirname(&parent, ondisk_path);
982 if (err)
983 return err;
985 if (blob_orig) {
986 if (asprintf(&base_path, "%s/got-merge-blob-orig",
987 parent) == -1) {
988 err = got_error_from_errno("asprintf");
989 base_path = NULL;
990 goto done;
993 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
994 if (err)
995 goto done;
996 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
997 blob_orig);
998 if (err)
999 goto done;
1000 free(base_path);
1001 } else {
1003 * No common ancestor exists. This is an "add vs add" conflict
1004 * and we simply use an empty ancestor file to make both files
1005 * appear in the merged result in their entirety.
1007 f_orig = got_opentemp();
1008 if (f_orig == NULL) {
1009 err = got_error_from_errno("got_opentemp");
1010 goto done;
1014 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1015 err = got_error_from_errno("asprintf");
1016 base_path = NULL;
1017 goto done;
1020 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1021 if (err)
1022 goto done;
1023 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1024 blob_deriv);
1025 if (err)
1026 goto done;
1028 err = got_object_id_str(&id_str, deriv_base_commit_id);
1029 if (err)
1030 goto done;
1031 if (asprintf(&label_deriv, "%s: commit %s",
1032 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1033 err = got_error_from_errno("asprintf");
1034 goto done;
1038 * In order the run a 3-way merge with a symlink we copy the symlink's
1039 * target path into a temporary file and use that file with diff3.
1041 if (S_ISLNK(st_mode)) {
1042 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1043 if (err)
1044 goto done;
1045 } else {
1046 int fd;
1047 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1048 if (fd == -1) {
1049 err = got_error_from_errno2("open", ondisk_path);
1050 goto done;
1052 f_deriv2 = fdopen(fd, "r");
1053 if (f_deriv2 == NULL) {
1054 err = got_error_from_errno2("fdopen", ondisk_path);
1055 close(fd);
1056 goto done;
1060 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1061 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1062 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1063 done:
1064 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1065 err = got_error_from_errno("fclose");
1066 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1067 err = got_error_from_errno("fclose");
1068 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1069 err = got_error_from_errno("fclose");
1070 free(base_path);
1071 if (blob_orig_path) {
1072 unlink(blob_orig_path);
1073 free(blob_orig_path);
1075 if (blob_deriv_path) {
1076 unlink(blob_deriv_path);
1077 free(blob_deriv_path);
1079 free(id_str);
1080 free(label_deriv);
1081 free(parent);
1082 return err;
1085 static const struct got_error *
1086 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1087 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1088 int wt_fd, const char *path, struct got_object_id *blob_id)
1090 const struct got_error *err = NULL;
1091 struct got_fileindex_entry *new_ie;
1093 *new_iep = NULL;
1095 err = got_fileindex_entry_alloc(&new_ie, path);
1096 if (err)
1097 return err;
1099 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1100 blob_id->sha1, base_commit_id->sha1, 1);
1101 if (err)
1102 goto done;
1104 err = got_fileindex_entry_add(fileindex, new_ie);
1105 done:
1106 if (err)
1107 got_fileindex_entry_free(new_ie);
1108 else
1109 *new_iep = new_ie;
1110 return err;
1113 static mode_t
1114 get_ondisk_perms(int executable, mode_t st_mode)
1116 mode_t xbits = S_IXUSR;
1118 if (executable) {
1119 /* Map read bits to execute bits. */
1120 if (st_mode & S_IRGRP)
1121 xbits |= S_IXGRP;
1122 if (st_mode & S_IROTH)
1123 xbits |= S_IXOTH;
1124 return st_mode | xbits;
1127 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1130 /* forward declaration */
1131 static const struct got_error *
1132 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1133 const char *path, mode_t te_mode, mode_t st_mode,
1134 struct got_blob_object *blob, int restoring_missing_file,
1135 int reverting_versioned_file, int installing_bad_symlink,
1136 int path_is_unversioned, struct got_repository *repo,
1137 got_worktree_checkout_cb progress_cb, void *progress_arg);
1140 * This function assumes that the provided symlink target points at a
1141 * safe location in the work tree!
1143 static const struct got_error *
1144 replace_existing_symlink(int *did_something, const char *ondisk_path,
1145 const char *target_path, size_t target_len)
1147 const struct got_error *err = NULL;
1148 ssize_t elen;
1149 char etarget[PATH_MAX];
1150 int fd;
1152 *did_something = 0;
1155 * "Bad" symlinks (those pointing outside the work tree or into the
1156 * .got directory) are installed in the work tree as a regular file
1157 * which contains the bad symlink target path.
1158 * The new symlink target has already been checked for safety by our
1159 * caller. If we can successfully open a regular file then we simply
1160 * replace this file with a symlink below.
1162 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1163 if (fd == -1) {
1164 if (!got_err_open_nofollow_on_symlink())
1165 return got_error_from_errno2("open", ondisk_path);
1167 /* We are updating an existing on-disk symlink. */
1168 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1169 if (elen == -1)
1170 return got_error_from_errno2("readlink", ondisk_path);
1172 if (elen == target_len &&
1173 memcmp(etarget, target_path, target_len) == 0)
1174 return NULL; /* nothing to do */
1177 *did_something = 1;
1178 err = update_symlink(ondisk_path, target_path, target_len);
1179 if (fd != -1 && close(fd) == -1 && err == NULL)
1180 err = got_error_from_errno2("close", ondisk_path);
1181 return err;
1184 static const struct got_error *
1185 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1186 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1188 const struct got_error *err = NULL;
1189 char canonpath[PATH_MAX];
1190 char *path_got = NULL;
1192 *is_bad_symlink = 0;
1194 if (target_len >= sizeof(canonpath)) {
1195 *is_bad_symlink = 1;
1196 return NULL;
1200 * We do not use realpath(3) to resolve the symlink's target
1201 * path because we don't want to resolve symlinks recursively.
1202 * Instead we make the path absolute and then canonicalize it.
1203 * Relative symlink target lookup should begin at the directory
1204 * in which the blob object is being installed.
1206 if (!got_path_is_absolute(target_path)) {
1207 char *abspath, *parent;
1208 err = got_path_dirname(&parent, ondisk_path);
1209 if (err)
1210 return err;
1211 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1212 free(parent);
1213 return got_error_from_errno("asprintf");
1215 free(parent);
1216 if (strlen(abspath) >= sizeof(canonpath)) {
1217 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1218 free(abspath);
1219 return err;
1221 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1222 free(abspath);
1223 if (err)
1224 return err;
1225 } else {
1226 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1227 if (err)
1228 return err;
1231 /* Only allow symlinks pointing at paths within the work tree. */
1232 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1233 *is_bad_symlink = 1;
1234 return NULL;
1237 /* Do not allow symlinks pointing into the .got directory. */
1238 if (asprintf(&path_got, "%s/%s", wtroot_path,
1239 GOT_WORKTREE_GOT_DIR) == -1)
1240 return got_error_from_errno("asprintf");
1241 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1242 *is_bad_symlink = 1;
1244 free(path_got);
1245 return NULL;
1248 static const struct got_error *
1249 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1250 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1251 int restoring_missing_file, int reverting_versioned_file,
1252 int path_is_unversioned, int allow_bad_symlinks,
1253 struct got_repository *repo,
1254 got_worktree_checkout_cb progress_cb, void *progress_arg)
1256 const struct got_error *err = NULL;
1257 char target_path[PATH_MAX];
1258 size_t len, target_len = 0;
1259 char *path_got = NULL;
1260 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1261 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1263 *is_bad_symlink = 0;
1266 * Blob object content specifies the target path of the link.
1267 * If a symbolic link cannot be installed we instead create
1268 * a regular file which contains the link target path stored
1269 * in the blob object.
1271 do {
1272 err = got_object_blob_read_block(&len, blob);
1273 if (len + target_len >= sizeof(target_path)) {
1274 /* Path too long; install as a regular file. */
1275 *is_bad_symlink = 1;
1276 got_object_blob_rewind(blob);
1277 return install_blob(worktree, ondisk_path, path,
1278 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1279 restoring_missing_file, reverting_versioned_file,
1280 1, path_is_unversioned, repo, progress_cb,
1281 progress_arg);
1283 if (len > 0) {
1284 /* Skip blob object header first time around. */
1285 memcpy(target_path + target_len, buf + hdrlen,
1286 len - hdrlen);
1287 target_len += len - hdrlen;
1288 hdrlen = 0;
1290 } while (len != 0);
1291 target_path[target_len] = '\0';
1293 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1294 ondisk_path, worktree->root_path);
1295 if (err)
1296 return err;
1298 if (*is_bad_symlink && !allow_bad_symlinks) {
1299 /* install as a regular file */
1300 got_object_blob_rewind(blob);
1301 err = install_blob(worktree, ondisk_path, path,
1302 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1303 restoring_missing_file, reverting_versioned_file, 1,
1304 path_is_unversioned, repo, progress_cb, progress_arg);
1305 goto done;
1308 if (symlink(target_path, ondisk_path) == -1) {
1309 if (errno == EEXIST) {
1310 int symlink_replaced;
1311 if (path_is_unversioned) {
1312 err = (*progress_cb)(progress_arg,
1313 GOT_STATUS_UNVERSIONED, path);
1314 goto done;
1316 err = replace_existing_symlink(&symlink_replaced,
1317 ondisk_path, target_path, target_len);
1318 if (err)
1319 goto done;
1320 if (progress_cb) {
1321 if (symlink_replaced) {
1322 err = (*progress_cb)(progress_arg,
1323 reverting_versioned_file ?
1324 GOT_STATUS_REVERT :
1325 GOT_STATUS_UPDATE, path);
1326 } else {
1327 err = (*progress_cb)(progress_arg,
1328 GOT_STATUS_EXISTS, path);
1331 goto done; /* Nothing else to do. */
1334 if (errno == ENOENT) {
1335 char *parent;
1336 err = got_path_dirname(&parent, ondisk_path);
1337 if (err)
1338 goto done;
1339 err = add_dir_on_disk(worktree, parent);
1340 free(parent);
1341 if (err)
1342 goto done;
1344 * Retry, and fall through to error handling
1345 * below if this second attempt fails.
1347 if (symlink(target_path, ondisk_path) != -1) {
1348 err = NULL; /* success */
1349 goto done;
1353 /* Handle errors from first or second creation attempt. */
1354 if (errno == ENAMETOOLONG) {
1355 /* bad target path; install as a regular file */
1356 *is_bad_symlink = 1;
1357 got_object_blob_rewind(blob);
1358 err = install_blob(worktree, ondisk_path, path,
1359 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1360 restoring_missing_file, reverting_versioned_file, 1,
1361 path_is_unversioned, repo,
1362 progress_cb, progress_arg);
1363 } else if (errno == ENOTDIR) {
1364 err = got_error_path(ondisk_path,
1365 GOT_ERR_FILE_OBSTRUCTED);
1366 } else {
1367 err = got_error_from_errno3("symlink",
1368 target_path, ondisk_path);
1370 } else if (progress_cb)
1371 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1372 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1373 done:
1374 free(path_got);
1375 return err;
1378 static const struct got_error *
1379 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1380 const char *path, mode_t te_mode, mode_t st_mode,
1381 struct got_blob_object *blob, int restoring_missing_file,
1382 int reverting_versioned_file, int installing_bad_symlink,
1383 int path_is_unversioned, struct got_repository *repo,
1384 got_worktree_checkout_cb progress_cb, void *progress_arg)
1386 const struct got_error *err = NULL;
1387 int fd = -1;
1388 size_t len, hdrlen;
1389 int update = 0;
1390 char *tmppath = NULL;
1392 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1393 O_CLOEXEC, GOT_DEFAULT_FILE_MODE);
1394 if (fd == -1) {
1395 if (errno == ENOENT) {
1396 char *parent;
1397 err = got_path_dirname(&parent, path);
1398 if (err)
1399 return err;
1400 err = add_dir_on_disk(worktree, parent);
1401 free(parent);
1402 if (err)
1403 return err;
1404 fd = open(ondisk_path,
1405 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1406 GOT_DEFAULT_FILE_MODE);
1407 if (fd == -1)
1408 return got_error_from_errno2("open",
1409 ondisk_path);
1410 } else if (errno == EEXIST) {
1411 if (path_is_unversioned) {
1412 err = (*progress_cb)(progress_arg,
1413 GOT_STATUS_UNVERSIONED, path);
1414 goto done;
1416 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1417 !S_ISREG(st_mode) && !installing_bad_symlink) {
1418 /* TODO file is obstructed; do something */
1419 err = got_error_path(ondisk_path,
1420 GOT_ERR_FILE_OBSTRUCTED);
1421 goto done;
1422 } else {
1423 err = got_opentemp_named_fd(&tmppath, &fd,
1424 ondisk_path);
1425 if (err)
1426 goto done;
1427 update = 1;
1429 } else
1430 return got_error_from_errno2("open", ondisk_path);
1433 if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1434 err = got_error_from_errno2("fchmod",
1435 update ? tmppath : ondisk_path);
1436 goto done;
1439 if (progress_cb) {
1440 if (restoring_missing_file)
1441 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1442 path);
1443 else if (reverting_versioned_file)
1444 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1445 path);
1446 else
1447 err = (*progress_cb)(progress_arg,
1448 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1449 if (err)
1450 goto done;
1453 hdrlen = got_object_blob_get_hdrlen(blob);
1454 do {
1455 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1456 err = got_object_blob_read_block(&len, blob);
1457 if (err)
1458 break;
1459 if (len > 0) {
1460 /* Skip blob object header first time around. */
1461 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1462 if (outlen == -1) {
1463 err = got_error_from_errno("write");
1464 goto done;
1465 } else if (outlen != len - hdrlen) {
1466 err = got_error(GOT_ERR_IO);
1467 goto done;
1469 hdrlen = 0;
1471 } while (len != 0);
1473 if (fsync(fd) != 0) {
1474 err = got_error_from_errno("fsync");
1475 goto done;
1478 if (update) {
1479 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1480 err = got_error_from_errno2("unlink", ondisk_path);
1481 goto done;
1483 if (rename(tmppath, ondisk_path) != 0) {
1484 err = got_error_from_errno3("rename", tmppath,
1485 ondisk_path);
1486 goto done;
1488 free(tmppath);
1489 tmppath = NULL;
1492 done:
1493 if (fd != -1 && close(fd) == -1 && err == NULL)
1494 err = got_error_from_errno("close");
1495 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1496 err = got_error_from_errno2("unlink", tmppath);
1497 free(tmppath);
1498 return err;
1501 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1502 static const struct got_error *
1503 get_modified_file_content_status(unsigned char *status, FILE *f)
1505 const struct got_error *err = NULL;
1506 const char *markers[3] = {
1507 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1508 GOT_DIFF_CONFLICT_MARKER_SEP,
1509 GOT_DIFF_CONFLICT_MARKER_END
1511 int i = 0;
1512 char *line = NULL;
1513 size_t linesize = 0;
1514 ssize_t linelen;
1516 while (*status == GOT_STATUS_MODIFY) {
1517 linelen = getline(&line, &linesize, f);
1518 if (linelen == -1) {
1519 if (feof(f))
1520 break;
1521 err = got_ferror(f, GOT_ERR_IO);
1522 break;
1525 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1526 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1527 == 0)
1528 *status = GOT_STATUS_CONFLICT;
1529 else
1530 i++;
1533 free(line);
1535 return err;
1538 static int
1539 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1541 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1542 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1545 static int
1546 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1548 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1549 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1550 ie->mtime_sec == sb->st_mtim.tv_sec &&
1551 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1552 ie->size == (sb->st_size & 0xffffffff) &&
1553 !xbit_differs(ie, sb->st_mode));
1556 static unsigned char
1557 get_staged_status(struct got_fileindex_entry *ie)
1559 switch (got_fileindex_entry_stage_get(ie)) {
1560 case GOT_FILEIDX_STAGE_ADD:
1561 return GOT_STATUS_ADD;
1562 case GOT_FILEIDX_STAGE_DELETE:
1563 return GOT_STATUS_DELETE;
1564 case GOT_FILEIDX_STAGE_MODIFY:
1565 return GOT_STATUS_MODIFY;
1566 default:
1567 return GOT_STATUS_NO_CHANGE;
1571 static const struct got_error *
1572 get_symlink_modification_status(unsigned char *status,
1573 struct got_fileindex_entry *ie, const char *abspath,
1574 int dirfd, const char *de_name, struct got_blob_object *blob)
1576 const struct got_error *err = NULL;
1577 char target_path[PATH_MAX];
1578 char etarget[PATH_MAX];
1579 ssize_t elen;
1580 size_t len, target_len = 0;
1581 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1582 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1584 *status = GOT_STATUS_NO_CHANGE;
1586 /* Blob object content specifies the target path of the link. */
1587 do {
1588 err = got_object_blob_read_block(&len, blob);
1589 if (err)
1590 return err;
1591 if (len + target_len >= sizeof(target_path)) {
1593 * Should not happen. The blob contents were OK
1594 * when this symlink was installed.
1596 return got_error(GOT_ERR_NO_SPACE);
1598 if (len > 0) {
1599 /* Skip blob object header first time around. */
1600 memcpy(target_path + target_len, buf + hdrlen,
1601 len - hdrlen);
1602 target_len += len - hdrlen;
1603 hdrlen = 0;
1605 } while (len != 0);
1606 target_path[target_len] = '\0';
1608 if (dirfd != -1) {
1609 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1610 if (elen == -1)
1611 return got_error_from_errno2("readlinkat", abspath);
1612 } else {
1613 elen = readlink(abspath, etarget, sizeof(etarget));
1614 if (elen == -1)
1615 return got_error_from_errno2("readlink", abspath);
1618 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1619 *status = GOT_STATUS_MODIFY;
1621 return NULL;
1624 static const struct got_error *
1625 get_file_status(unsigned char *status, struct stat *sb,
1626 struct got_fileindex_entry *ie, const char *abspath,
1627 int dirfd, const char *de_name, struct got_repository *repo)
1629 const struct got_error *err = NULL;
1630 struct got_object_id id;
1631 size_t hdrlen;
1632 int fd = -1, fd1 = -1;
1633 FILE *f = NULL;
1634 uint8_t fbuf[8192];
1635 struct got_blob_object *blob = NULL;
1636 size_t flen, blen;
1637 unsigned char staged_status = get_staged_status(ie);
1639 *status = GOT_STATUS_NO_CHANGE;
1640 memset(sb, 0, sizeof(*sb));
1643 * Whenever the caller provides a directory descriptor and a
1644 * directory entry name for the file, use them! This prevents
1645 * race conditions if filesystem paths change beneath our feet.
1647 if (dirfd != -1) {
1648 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1649 if (errno == ENOENT) {
1650 if (got_fileindex_entry_has_file_on_disk(ie))
1651 *status = GOT_STATUS_MISSING;
1652 else
1653 *status = GOT_STATUS_DELETE;
1654 goto done;
1656 err = got_error_from_errno2("fstatat", abspath);
1657 goto done;
1659 } else {
1660 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1661 if (fd == -1 && errno != ENOENT &&
1662 !got_err_open_nofollow_on_symlink())
1663 return got_error_from_errno2("open", abspath);
1664 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1665 if (lstat(abspath, sb) == -1)
1666 return got_error_from_errno2("lstat", abspath);
1667 } else if (fd == -1 || fstat(fd, sb) == -1) {
1668 if (errno == ENOENT) {
1669 if (got_fileindex_entry_has_file_on_disk(ie))
1670 *status = GOT_STATUS_MISSING;
1671 else
1672 *status = GOT_STATUS_DELETE;
1673 goto done;
1675 err = got_error_from_errno2("fstat", abspath);
1676 goto done;
1680 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1681 *status = GOT_STATUS_OBSTRUCTED;
1682 goto done;
1685 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1686 *status = GOT_STATUS_DELETE;
1687 goto done;
1688 } else if (!got_fileindex_entry_has_blob(ie) &&
1689 staged_status != GOT_STATUS_ADD) {
1690 *status = GOT_STATUS_ADD;
1691 goto done;
1694 if (!stat_info_differs(ie, sb))
1695 goto done;
1697 if (S_ISLNK(sb->st_mode) &&
1698 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1699 *status = GOT_STATUS_MODIFY;
1700 goto done;
1703 if (staged_status == GOT_STATUS_MODIFY ||
1704 staged_status == GOT_STATUS_ADD)
1705 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1706 else
1707 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1709 fd1 = got_opentempfd();
1710 if (fd1 == -1) {
1711 err = got_error_from_errno("got_opentempfd");
1712 goto done;
1714 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1715 if (err)
1716 goto done;
1718 if (S_ISLNK(sb->st_mode)) {
1719 err = get_symlink_modification_status(status, ie,
1720 abspath, dirfd, de_name, blob);
1721 goto done;
1724 if (dirfd != -1) {
1725 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1726 if (fd == -1) {
1727 err = got_error_from_errno2("openat", abspath);
1728 goto done;
1732 f = fdopen(fd, "r");
1733 if (f == NULL) {
1734 err = got_error_from_errno2("fdopen", abspath);
1735 goto done;
1737 fd = -1;
1738 hdrlen = got_object_blob_get_hdrlen(blob);
1739 for (;;) {
1740 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1741 err = got_object_blob_read_block(&blen, blob);
1742 if (err)
1743 goto done;
1744 /* Skip length of blob object header first time around. */
1745 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1746 if (flen == 0 && ferror(f)) {
1747 err = got_error_from_errno("fread");
1748 goto done;
1750 if (blen - hdrlen == 0) {
1751 if (flen != 0)
1752 *status = GOT_STATUS_MODIFY;
1753 break;
1754 } else if (flen == 0) {
1755 if (blen - hdrlen != 0)
1756 *status = GOT_STATUS_MODIFY;
1757 break;
1758 } else if (blen - hdrlen == flen) {
1759 /* Skip blob object header first time around. */
1760 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1761 *status = GOT_STATUS_MODIFY;
1762 break;
1764 } else {
1765 *status = GOT_STATUS_MODIFY;
1766 break;
1768 hdrlen = 0;
1771 if (*status == GOT_STATUS_MODIFY) {
1772 rewind(f);
1773 err = get_modified_file_content_status(status, f);
1774 } else if (xbit_differs(ie, sb->st_mode))
1775 *status = GOT_STATUS_MODE_CHANGE;
1776 done:
1777 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1778 err = got_error_from_errno("close");
1779 if (blob)
1780 got_object_blob_close(blob);
1781 if (f != NULL && fclose(f) == EOF && err == NULL)
1782 err = got_error_from_errno2("fclose", abspath);
1783 if (fd != -1 && close(fd) == -1 && err == NULL)
1784 err = got_error_from_errno2("close", abspath);
1785 return err;
1789 * Update timestamps in the file index if a file is unmodified and
1790 * we had to run a full content comparison to find out.
1792 static const struct got_error *
1793 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1794 struct got_fileindex_entry *ie, struct stat *sb)
1796 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1797 return got_fileindex_entry_update(ie, wt_fd, path,
1798 ie->blob_sha1, ie->commit_sha1, 1);
1800 return NULL;
1803 static const struct got_error *
1804 update_blob(struct got_worktree *worktree,
1805 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1806 struct got_tree_entry *te, const char *path,
1807 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1808 void *progress_arg)
1810 const struct got_error *err = NULL;
1811 struct got_blob_object *blob = NULL;
1812 char *ondisk_path = NULL;
1813 unsigned char status = GOT_STATUS_NO_CHANGE;
1814 struct stat sb;
1815 int fd1 = -1, fd2 = -1;
1817 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1818 return got_error_from_errno("asprintf");
1820 if (ie) {
1821 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1822 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1823 goto done;
1825 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1826 repo);
1827 if (err)
1828 goto done;
1829 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1830 sb.st_mode = got_fileindex_perms_to_st(ie);
1831 } else {
1832 if (stat(ondisk_path, &sb) == -1) {
1833 if (errno != ENOENT) {
1834 err = got_error_from_errno2("stat",
1835 ondisk_path);
1836 goto done;
1838 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1839 status = GOT_STATUS_UNVERSIONED;
1840 } else {
1841 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1842 status = GOT_STATUS_UNVERSIONED;
1843 else
1844 status = GOT_STATUS_OBSTRUCTED;
1848 if (status == GOT_STATUS_OBSTRUCTED) {
1849 if (ie)
1850 got_fileindex_entry_mark_skipped(ie);
1851 err = (*progress_cb)(progress_arg, status, path);
1852 goto done;
1854 if (status == GOT_STATUS_CONFLICT) {
1855 if (ie)
1856 got_fileindex_entry_mark_skipped(ie);
1857 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1858 path);
1859 goto done;
1862 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1863 (S_ISLNK(te->mode) ||
1864 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1866 * This is a regular file or an installed bad symlink.
1867 * If the file index indicates that this file is already
1868 * up-to-date with respect to the repository we can skip
1869 * updating contents of this file.
1871 if (got_fileindex_entry_has_commit(ie) &&
1872 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1873 SHA1_DIGEST_LENGTH) == 0) {
1874 /* Same commit. */
1875 err = sync_timestamps(worktree->root_fd,
1876 path, status, ie, &sb);
1877 if (err)
1878 goto done;
1879 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1880 path);
1881 goto done;
1883 if (got_fileindex_entry_has_blob(ie) &&
1884 memcmp(ie->blob_sha1, te->id.sha1,
1885 SHA1_DIGEST_LENGTH) == 0) {
1886 /* Different commit but the same blob. */
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;
1897 fd1 = got_opentempfd();
1898 if (fd1 == -1) {
1899 err = got_error_from_errno("got_opentempfd");
1900 goto done;
1902 err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
1903 if (err)
1904 goto done;
1906 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1907 int update_timestamps;
1908 struct got_blob_object *blob2 = NULL;
1909 char *label_orig = NULL;
1910 if (got_fileindex_entry_has_blob(ie)) {
1911 fd2 = got_opentempfd();
1912 if (fd2 == -1) {
1913 err = got_error_from_errno("got_opentempfd");
1914 goto done;
1916 struct got_object_id id2;
1917 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1918 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
1919 fd2);
1920 if (err)
1921 goto done;
1923 if (got_fileindex_entry_has_commit(ie)) {
1924 char id_str[SHA1_DIGEST_STRING_LENGTH];
1925 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1926 sizeof(id_str)) == NULL) {
1927 err = got_error_path(id_str,
1928 GOT_ERR_BAD_OBJ_ID_STR);
1929 goto done;
1931 if (asprintf(&label_orig, "%s: commit %s",
1932 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1933 err = got_error_from_errno("asprintf");
1934 goto done;
1937 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1938 char *link_target;
1939 err = got_object_blob_read_to_str(&link_target, blob);
1940 if (err)
1941 goto done;
1942 err = merge_symlink(worktree, blob2, ondisk_path, path,
1943 label_orig, link_target, worktree->base_commit_id,
1944 repo, progress_cb, progress_arg);
1945 free(link_target);
1946 } else {
1947 err = merge_blob(&update_timestamps, worktree, blob2,
1948 ondisk_path, path, sb.st_mode, label_orig, blob,
1949 worktree->base_commit_id, repo,
1950 progress_cb, progress_arg);
1952 free(label_orig);
1953 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
1954 err = got_error_from_errno("close");
1955 goto done;
1957 if (blob2)
1958 got_object_blob_close(blob2);
1959 if (err)
1960 goto done;
1962 * Do not update timestamps of files with local changes.
1963 * Otherwise, a future status walk would treat them as
1964 * unmodified files again.
1966 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1967 blob->id.sha1, worktree->base_commit_id->sha1,
1968 update_timestamps);
1969 } else if (status == GOT_STATUS_MODE_CHANGE) {
1970 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1971 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1972 } else if (status == GOT_STATUS_DELETE) {
1973 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1974 if (err)
1975 goto done;
1976 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1977 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1978 if (err)
1979 goto done;
1980 } else {
1981 int is_bad_symlink = 0;
1982 if (S_ISLNK(te->mode)) {
1983 err = install_symlink(&is_bad_symlink, worktree,
1984 ondisk_path, path, blob,
1985 status == GOT_STATUS_MISSING, 0,
1986 status == GOT_STATUS_UNVERSIONED, 0,
1987 repo, progress_cb, progress_arg);
1988 } else {
1989 err = install_blob(worktree, ondisk_path, path,
1990 te->mode, sb.st_mode, blob,
1991 status == GOT_STATUS_MISSING, 0, 0,
1992 status == GOT_STATUS_UNVERSIONED, repo,
1993 progress_cb, progress_arg);
1995 if (err)
1996 goto done;
1998 if (ie) {
1999 err = got_fileindex_entry_update(ie,
2000 worktree->root_fd, path, blob->id.sha1,
2001 worktree->base_commit_id->sha1, 1);
2002 } else {
2003 err = create_fileindex_entry(&ie, fileindex,
2004 worktree->base_commit_id, worktree->root_fd, path,
2005 &blob->id);
2007 if (err)
2008 goto done;
2010 if (is_bad_symlink) {
2011 got_fileindex_entry_filetype_set(ie,
2012 GOT_FILEIDX_MODE_BAD_SYMLINK);
2016 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2017 err = got_error_from_errno("close");
2018 goto done;
2020 got_object_blob_close(blob);
2021 done:
2022 free(ondisk_path);
2023 return err;
2026 static const struct got_error *
2027 remove_ondisk_file(const char *root_path, const char *path)
2029 const struct got_error *err = NULL;
2030 char *ondisk_path = NULL, *parent = NULL;
2032 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2033 return got_error_from_errno("asprintf");
2035 if (unlink(ondisk_path) == -1) {
2036 if (errno != ENOENT)
2037 err = got_error_from_errno2("unlink", ondisk_path);
2038 } else {
2039 size_t root_len = strlen(root_path);
2040 err = got_path_dirname(&parent, ondisk_path);
2041 if (err)
2042 goto done;
2043 while (got_path_cmp(parent, root_path,
2044 strlen(parent), root_len) != 0) {
2045 free(ondisk_path);
2046 ondisk_path = parent;
2047 parent = NULL;
2048 if (rmdir(ondisk_path) == -1) {
2049 if (errno != ENOTEMPTY)
2050 err = got_error_from_errno2("rmdir",
2051 ondisk_path);
2052 break;
2054 err = got_path_dirname(&parent, ondisk_path);
2055 if (err)
2056 break;
2059 done:
2060 free(ondisk_path);
2061 free(parent);
2062 return err;
2065 static const struct got_error *
2066 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2067 struct got_fileindex_entry *ie, struct got_repository *repo,
2068 got_worktree_checkout_cb progress_cb, void *progress_arg)
2070 const struct got_error *err = NULL;
2071 unsigned char status;
2072 struct stat sb;
2073 char *ondisk_path;
2075 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2076 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2078 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2079 == -1)
2080 return got_error_from_errno("asprintf");
2082 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2083 if (err)
2084 goto done;
2086 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2087 char ondisk_target[PATH_MAX];
2088 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2089 sizeof(ondisk_target));
2090 if (ondisk_len == -1) {
2091 err = got_error_from_errno2("readlink", ondisk_path);
2092 goto done;
2094 ondisk_target[ondisk_len] = '\0';
2095 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2096 NULL, NULL, /* XXX pass common ancestor info? */
2097 ondisk_target, ondisk_path);
2098 if (err)
2099 goto done;
2100 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2101 ie->path);
2102 goto done;
2105 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2106 status == GOT_STATUS_ADD) {
2107 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2108 if (err)
2109 goto done;
2111 * Preserve the working file and change the deleted blob's
2112 * entry into a schedule-add entry.
2114 err = got_fileindex_entry_update(ie, worktree->root_fd,
2115 ie->path, NULL, NULL, 0);
2116 } else {
2117 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2118 if (err)
2119 goto done;
2120 if (status == GOT_STATUS_NO_CHANGE) {
2121 err = remove_ondisk_file(worktree->root_path, ie->path);
2122 if (err)
2123 goto done;
2125 got_fileindex_entry_remove(fileindex, ie);
2127 done:
2128 free(ondisk_path);
2129 return err;
2132 struct diff_cb_arg {
2133 struct got_fileindex *fileindex;
2134 struct got_worktree *worktree;
2135 struct got_repository *repo;
2136 got_worktree_checkout_cb progress_cb;
2137 void *progress_arg;
2138 got_cancel_cb cancel_cb;
2139 void *cancel_arg;
2142 static const struct got_error *
2143 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2144 struct got_tree_entry *te, const char *parent_path)
2146 struct diff_cb_arg *a = arg;
2148 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2149 return got_error(GOT_ERR_CANCELLED);
2151 return update_blob(a->worktree, a->fileindex, ie, te,
2152 ie->path, a->repo, a->progress_cb, a->progress_arg);
2155 static const struct got_error *
2156 diff_old(void *arg, struct got_fileindex_entry *ie, 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 delete_blob(a->worktree, a->fileindex, ie,
2164 a->repo, a->progress_cb, a->progress_arg);
2167 static const struct got_error *
2168 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2170 struct diff_cb_arg *a = arg;
2171 const struct got_error *err;
2172 char *path;
2174 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2175 return got_error(GOT_ERR_CANCELLED);
2177 if (got_object_tree_entry_is_submodule(te))
2178 return NULL;
2180 if (asprintf(&path, "%s%s%s", parent_path,
2181 parent_path[0] ? "/" : "", te->name)
2182 == -1)
2183 return got_error_from_errno("asprintf");
2185 if (S_ISDIR(te->mode))
2186 err = add_dir_on_disk(a->worktree, path);
2187 else
2188 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2189 a->repo, a->progress_cb, a->progress_arg);
2191 free(path);
2192 return err;
2195 const struct got_error *
2196 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2198 uint32_t uuid_status;
2200 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2201 if (uuid_status != uuid_s_ok) {
2202 *uuidstr = NULL;
2203 return got_error_uuid(uuid_status, "uuid_to_string");
2206 return NULL;
2209 static const struct got_error *
2210 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2212 const struct got_error *err = NULL;
2213 char *uuidstr = NULL;
2215 *refname = NULL;
2217 err = got_worktree_get_uuid(&uuidstr, worktree);
2218 if (err)
2219 return err;
2221 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2222 err = got_error_from_errno("asprintf");
2223 *refname = NULL;
2225 free(uuidstr);
2226 return err;
2229 const struct got_error *
2230 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2232 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2235 static const struct got_error *
2236 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2238 return get_ref_name(refname, worktree,
2239 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2242 static const struct got_error *
2243 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2245 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2248 static const struct got_error *
2249 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2251 return get_ref_name(refname, worktree,
2252 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2255 static const struct got_error *
2256 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2258 return get_ref_name(refname, worktree,
2259 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2262 static const struct got_error *
2263 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2265 return get_ref_name(refname, worktree,
2266 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2269 static const struct got_error *
2270 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2272 return get_ref_name(refname, worktree,
2273 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2276 static const struct got_error *
2277 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2279 return get_ref_name(refname, worktree,
2280 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2283 static const struct got_error *
2284 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2286 return get_ref_name(refname, worktree,
2287 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2290 const struct got_error *
2291 got_worktree_get_histedit_script_path(char **path,
2292 struct got_worktree *worktree)
2294 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2295 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2296 *path = NULL;
2297 return got_error_from_errno("asprintf");
2299 return NULL;
2302 static const struct got_error *
2303 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2305 return get_ref_name(refname, worktree,
2306 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2309 static const struct got_error *
2310 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2312 return get_ref_name(refname, worktree,
2313 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2317 * Prevent Git's garbage collector from deleting our base commit by
2318 * setting a reference to our base commit's ID.
2320 static const struct got_error *
2321 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2323 const struct got_error *err = NULL;
2324 struct got_reference *ref = NULL;
2325 char *refname;
2327 err = got_worktree_get_base_ref_name(&refname, worktree);
2328 if (err)
2329 return err;
2331 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2332 if (err)
2333 goto done;
2335 err = got_ref_write(ref, repo);
2336 done:
2337 free(refname);
2338 if (ref)
2339 got_ref_close(ref);
2340 return err;
2343 static const struct got_error *
2344 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2346 const struct got_error *err = NULL;
2348 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2349 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2350 err = got_error_from_errno("asprintf");
2351 *fileindex_path = NULL;
2353 return err;
2357 static const struct got_error *
2358 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2359 struct got_worktree *worktree)
2361 const struct got_error *err = NULL;
2362 FILE *index = NULL;
2364 *fileindex_path = NULL;
2365 *fileindex = got_fileindex_alloc();
2366 if (*fileindex == NULL)
2367 return got_error_from_errno("got_fileindex_alloc");
2369 err = get_fileindex_path(fileindex_path, worktree);
2370 if (err)
2371 goto done;
2373 index = fopen(*fileindex_path, "rbe");
2374 if (index == NULL) {
2375 if (errno != ENOENT)
2376 err = got_error_from_errno2("fopen", *fileindex_path);
2377 } else {
2378 err = got_fileindex_read(*fileindex, index);
2379 if (fclose(index) == EOF && err == NULL)
2380 err = got_error_from_errno("fclose");
2382 done:
2383 if (err) {
2384 free(*fileindex_path);
2385 *fileindex_path = NULL;
2386 got_fileindex_free(*fileindex);
2387 *fileindex = NULL;
2389 return err;
2392 struct bump_base_commit_id_arg {
2393 struct got_object_id *base_commit_id;
2394 const char *path;
2395 size_t path_len;
2396 const char *entry_name;
2397 got_worktree_checkout_cb progress_cb;
2398 void *progress_arg;
2401 /* Bump base commit ID of all files within an updated part of the work tree. */
2402 static const struct got_error *
2403 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2405 const struct got_error *err;
2406 struct bump_base_commit_id_arg *a = arg;
2408 if (a->entry_name) {
2409 if (strcmp(ie->path, a->path) != 0)
2410 return NULL;
2411 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2412 return NULL;
2414 if (got_fileindex_entry_was_skipped(ie))
2415 return NULL;
2417 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2418 SHA1_DIGEST_LENGTH) == 0)
2419 return NULL;
2421 if (a->progress_cb) {
2422 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2423 ie->path);
2424 if (err)
2425 return err;
2427 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2428 return NULL;
2431 static const struct got_error *
2432 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2433 struct got_fileindex *fileindex,
2434 got_worktree_checkout_cb progress_cb, void *progress_arg)
2436 struct bump_base_commit_id_arg bbc_arg;
2438 bbc_arg.base_commit_id = worktree->base_commit_id;
2439 bbc_arg.entry_name = NULL;
2440 bbc_arg.path = "";
2441 bbc_arg.path_len = 0;
2442 bbc_arg.progress_cb = progress_cb;
2443 bbc_arg.progress_arg = progress_arg;
2445 return got_fileindex_for_each_entry_safe(fileindex,
2446 bump_base_commit_id, &bbc_arg);
2449 static const struct got_error *
2450 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2452 const struct got_error *err = NULL;
2453 char *new_fileindex_path = NULL;
2454 FILE *new_index = NULL;
2455 struct timespec timeout;
2457 err = got_opentemp_named(&new_fileindex_path, &new_index,
2458 fileindex_path);
2459 if (err)
2460 goto done;
2462 err = got_fileindex_write(fileindex, new_index);
2463 if (err)
2464 goto done;
2466 if (rename(new_fileindex_path, fileindex_path) != 0) {
2467 err = got_error_from_errno3("rename", new_fileindex_path,
2468 fileindex_path);
2469 unlink(new_fileindex_path);
2473 * Sleep for a short amount of time to ensure that files modified after
2474 * this program exits have a different time stamp from the one which
2475 * was recorded in the file index.
2477 timeout.tv_sec = 0;
2478 timeout.tv_nsec = 1;
2479 nanosleep(&timeout, NULL);
2480 done:
2481 if (new_index)
2482 fclose(new_index);
2483 free(new_fileindex_path);
2484 return err;
2487 static const struct got_error *
2488 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2489 struct got_object_id **tree_id, const char *wt_relpath,
2490 struct got_commit_object *base_commit, struct got_worktree *worktree,
2491 struct got_repository *repo)
2493 const struct got_error *err = NULL;
2494 struct got_object_id *id = NULL;
2495 char *in_repo_path = NULL;
2496 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2498 *entry_type = GOT_OBJ_TYPE_ANY;
2499 *tree_relpath = NULL;
2500 *tree_id = NULL;
2502 if (wt_relpath[0] == '\0') {
2503 /* Check out all files within the work tree. */
2504 *entry_type = GOT_OBJ_TYPE_TREE;
2505 *tree_relpath = strdup("");
2506 if (*tree_relpath == NULL) {
2507 err = got_error_from_errno("strdup");
2508 goto done;
2510 err = got_object_id_by_path(tree_id, repo, base_commit,
2511 worktree->path_prefix);
2512 if (err)
2513 goto done;
2514 return NULL;
2517 /* Check out a subset of files in the work tree. */
2519 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2520 is_root_wt ? "" : "/", wt_relpath) == -1) {
2521 err = got_error_from_errno("asprintf");
2522 goto done;
2525 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2526 if (err)
2527 goto done;
2529 free(in_repo_path);
2530 in_repo_path = NULL;
2532 err = got_object_get_type(entry_type, repo, id);
2533 if (err)
2534 goto done;
2536 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2537 /* Check out a single file. */
2538 if (strchr(wt_relpath, '/') == NULL) {
2539 /* Check out a single file in work tree's root dir. */
2540 in_repo_path = strdup(worktree->path_prefix);
2541 if (in_repo_path == NULL) {
2542 err = got_error_from_errno("strdup");
2543 goto done;
2545 *tree_relpath = strdup("");
2546 if (*tree_relpath == NULL) {
2547 err = got_error_from_errno("strdup");
2548 goto done;
2550 } else {
2551 /* Check out a single file in a subdirectory. */
2552 err = got_path_dirname(tree_relpath, wt_relpath);
2553 if (err)
2554 return err;
2555 if (asprintf(&in_repo_path, "%s%s%s",
2556 worktree->path_prefix, is_root_wt ? "" : "/",
2557 *tree_relpath) == -1) {
2558 err = got_error_from_errno("asprintf");
2559 goto done;
2562 err = got_object_id_by_path(tree_id, repo,
2563 base_commit, in_repo_path);
2564 } else {
2565 /* Check out all files within a subdirectory. */
2566 *tree_id = got_object_id_dup(id);
2567 if (*tree_id == NULL) {
2568 err = got_error_from_errno("got_object_id_dup");
2569 goto done;
2571 *tree_relpath = strdup(wt_relpath);
2572 if (*tree_relpath == NULL) {
2573 err = got_error_from_errno("strdup");
2574 goto done;
2577 done:
2578 free(id);
2579 free(in_repo_path);
2580 if (err) {
2581 *entry_type = GOT_OBJ_TYPE_ANY;
2582 free(*tree_relpath);
2583 *tree_relpath = NULL;
2584 free(*tree_id);
2585 *tree_id = NULL;
2587 return err;
2590 static const struct got_error *
2591 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2592 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2593 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2594 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2596 const struct got_error *err = NULL;
2597 struct got_commit_object *commit = NULL;
2598 struct got_tree_object *tree = NULL;
2599 struct got_fileindex_diff_tree_cb diff_cb;
2600 struct diff_cb_arg arg;
2602 err = ref_base_commit(worktree, repo);
2603 if (err) {
2604 if (!(err->code == GOT_ERR_ERRNO &&
2605 (errno == EACCES || errno == EROFS)))
2606 goto done;
2607 err = (*progress_cb)(progress_arg,
2608 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2609 if (err)
2610 return err;
2613 err = got_object_open_as_commit(&commit, repo,
2614 worktree->base_commit_id);
2615 if (err)
2616 goto done;
2618 err = got_object_open_as_tree(&tree, repo, tree_id);
2619 if (err)
2620 goto done;
2622 if (entry_name &&
2623 got_object_tree_find_entry(tree, entry_name) == NULL) {
2624 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2625 goto done;
2628 diff_cb.diff_old_new = diff_old_new;
2629 diff_cb.diff_old = diff_old;
2630 diff_cb.diff_new = diff_new;
2631 arg.fileindex = fileindex;
2632 arg.worktree = worktree;
2633 arg.repo = repo;
2634 arg.progress_cb = progress_cb;
2635 arg.progress_arg = progress_arg;
2636 arg.cancel_cb = cancel_cb;
2637 arg.cancel_arg = cancel_arg;
2638 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2639 entry_name, repo, &diff_cb, &arg);
2640 done:
2641 if (tree)
2642 got_object_tree_close(tree);
2643 if (commit)
2644 got_object_commit_close(commit);
2645 return err;
2648 const struct got_error *
2649 got_worktree_checkout_files(struct got_worktree *worktree,
2650 struct got_pathlist_head *paths, struct got_repository *repo,
2651 got_worktree_checkout_cb progress_cb, void *progress_arg,
2652 got_cancel_cb cancel_cb, void *cancel_arg)
2654 const struct got_error *err = NULL, *sync_err, *unlockerr;
2655 struct got_commit_object *commit = NULL;
2656 struct got_tree_object *tree = NULL;
2657 struct got_fileindex *fileindex = NULL;
2658 char *fileindex_path = NULL;
2659 struct got_pathlist_entry *pe;
2660 struct tree_path_data {
2661 STAILQ_ENTRY(tree_path_data) entry;
2662 struct got_object_id *tree_id;
2663 int entry_type;
2664 char *relpath;
2665 char *entry_name;
2666 } *tpd = NULL;
2667 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2669 STAILQ_INIT(&tree_paths);
2671 err = lock_worktree(worktree, LOCK_EX);
2672 if (err)
2673 return err;
2675 err = got_object_open_as_commit(&commit, repo,
2676 worktree->base_commit_id);
2677 if (err)
2678 goto done;
2680 /* Map all specified paths to in-repository trees. */
2681 TAILQ_FOREACH(pe, paths, entry) {
2682 tpd = malloc(sizeof(*tpd));
2683 if (tpd == NULL) {
2684 err = got_error_from_errno("malloc");
2685 goto done;
2688 err = find_tree_entry_for_checkout(&tpd->entry_type,
2689 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2690 worktree, repo);
2691 if (err) {
2692 free(tpd);
2693 goto done;
2696 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2697 err = got_path_basename(&tpd->entry_name, pe->path);
2698 if (err) {
2699 free(tpd->relpath);
2700 free(tpd->tree_id);
2701 free(tpd);
2702 goto done;
2704 } else
2705 tpd->entry_name = NULL;
2707 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2711 * Read the file index.
2712 * Checking out files is supposed to be an idempotent operation.
2713 * If the on-disk file index is incomplete we will try to complete it.
2715 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2716 if (err)
2717 goto done;
2719 tpd = STAILQ_FIRST(&tree_paths);
2720 TAILQ_FOREACH(pe, paths, entry) {
2721 struct bump_base_commit_id_arg bbc_arg;
2723 err = checkout_files(worktree, fileindex, tpd->relpath,
2724 tpd->tree_id, tpd->entry_name, repo,
2725 progress_cb, progress_arg, cancel_cb, cancel_arg);
2726 if (err)
2727 break;
2729 bbc_arg.base_commit_id = worktree->base_commit_id;
2730 bbc_arg.entry_name = tpd->entry_name;
2731 bbc_arg.path = pe->path;
2732 bbc_arg.path_len = pe->path_len;
2733 bbc_arg.progress_cb = progress_cb;
2734 bbc_arg.progress_arg = progress_arg;
2735 err = got_fileindex_for_each_entry_safe(fileindex,
2736 bump_base_commit_id, &bbc_arg);
2737 if (err)
2738 break;
2740 tpd = STAILQ_NEXT(tpd, entry);
2742 sync_err = sync_fileindex(fileindex, fileindex_path);
2743 if (sync_err && err == NULL)
2744 err = sync_err;
2745 done:
2746 free(fileindex_path);
2747 if (tree)
2748 got_object_tree_close(tree);
2749 if (commit)
2750 got_object_commit_close(commit);
2751 if (fileindex)
2752 got_fileindex_free(fileindex);
2753 while (!STAILQ_EMPTY(&tree_paths)) {
2754 tpd = STAILQ_FIRST(&tree_paths);
2755 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2756 free(tpd->relpath);
2757 free(tpd->tree_id);
2758 free(tpd);
2760 unlockerr = lock_worktree(worktree, LOCK_SH);
2761 if (unlockerr && err == NULL)
2762 err = unlockerr;
2763 return err;
2766 struct merge_file_cb_arg {
2767 struct got_worktree *worktree;
2768 struct got_fileindex *fileindex;
2769 got_worktree_checkout_cb progress_cb;
2770 void *progress_arg;
2771 got_cancel_cb cancel_cb;
2772 void *cancel_arg;
2773 const char *label_orig;
2774 struct got_object_id *commit_id2;
2775 int allow_bad_symlinks;
2778 static const struct got_error *
2779 merge_file_cb(void *arg, struct got_blob_object *blob1,
2780 struct got_blob_object *blob2, FILE *f1, FILE *f2,
2781 struct got_object_id *id1, struct got_object_id *id2,
2782 const char *path1, const char *path2,
2783 mode_t mode1, mode_t mode2, struct got_repository *repo)
2785 static const struct got_error *err = NULL;
2786 struct merge_file_cb_arg *a = arg;
2787 struct got_fileindex_entry *ie;
2788 char *ondisk_path = NULL;
2789 struct stat sb;
2790 unsigned char status;
2791 int local_changes_subsumed;
2792 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2793 char *id_str = NULL, *label_deriv2 = NULL;
2795 if (blob1 && blob2) {
2796 ie = got_fileindex_entry_get(a->fileindex, path2,
2797 strlen(path2));
2798 if (ie == NULL)
2799 return (*a->progress_cb)(a->progress_arg,
2800 GOT_STATUS_MISSING, path2);
2802 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2803 path2) == -1)
2804 return got_error_from_errno("asprintf");
2806 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2807 repo);
2808 if (err)
2809 goto done;
2811 if (status == GOT_STATUS_DELETE) {
2812 err = (*a->progress_cb)(a->progress_arg,
2813 GOT_STATUS_MERGE, path2);
2814 goto done;
2816 if (status != GOT_STATUS_NO_CHANGE &&
2817 status != GOT_STATUS_MODIFY &&
2818 status != GOT_STATUS_CONFLICT &&
2819 status != GOT_STATUS_ADD) {
2820 err = (*a->progress_cb)(a->progress_arg, status, path2);
2821 goto done;
2824 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2825 char *link_target2;
2826 err = got_object_blob_read_to_str(&link_target2, blob2);
2827 if (err)
2828 goto done;
2829 err = merge_symlink(a->worktree, blob1, ondisk_path,
2830 path2, a->label_orig, link_target2, a->commit_id2,
2831 repo, a->progress_cb, a->progress_arg);
2832 free(link_target2);
2833 } else {
2834 int fd;
2836 f_orig = got_opentemp();
2837 if (f_orig == NULL) {
2838 err = got_error_from_errno("got_opentemp");
2839 goto done;
2841 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2842 f_orig, blob1);
2843 if (err)
2844 goto done;
2846 f_deriv2 = got_opentemp();
2847 if (f_deriv2 == NULL)
2848 goto done;
2849 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2850 f_deriv2, blob2);
2851 if (err)
2852 goto done;
2854 fd = open(ondisk_path,
2855 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2856 if (fd == -1) {
2857 err = got_error_from_errno2("open",
2858 ondisk_path);
2859 goto done;
2861 f_deriv = fdopen(fd, "r");
2862 if (f_deriv == NULL) {
2863 err = got_error_from_errno2("fdopen",
2864 ondisk_path);
2865 close(fd);
2866 goto done;
2868 err = got_object_id_str(&id_str, a->commit_id2);
2869 if (err)
2870 goto done;
2871 if (asprintf(&label_deriv2, "%s: commit %s",
2872 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2873 err = got_error_from_errno("asprintf");
2874 goto done;
2876 err = merge_file(&local_changes_subsumed, a->worktree,
2877 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2878 sb.st_mode, a->label_orig, NULL, label_deriv2,
2879 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2880 a->progress_cb, a->progress_arg);
2882 } else if (blob1) {
2883 ie = got_fileindex_entry_get(a->fileindex, path1,
2884 strlen(path1));
2885 if (ie == NULL)
2886 return (*a->progress_cb)(a->progress_arg,
2887 GOT_STATUS_MISSING, path1);
2889 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2890 path1) == -1)
2891 return got_error_from_errno("asprintf");
2893 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2894 repo);
2895 if (err)
2896 goto done;
2898 switch (status) {
2899 case GOT_STATUS_NO_CHANGE:
2900 err = (*a->progress_cb)(a->progress_arg,
2901 GOT_STATUS_DELETE, path1);
2902 if (err)
2903 goto done;
2904 err = remove_ondisk_file(a->worktree->root_path, path1);
2905 if (err)
2906 goto done;
2907 if (ie)
2908 got_fileindex_entry_mark_deleted_from_disk(ie);
2909 break;
2910 case GOT_STATUS_DELETE:
2911 case GOT_STATUS_MISSING:
2912 err = (*a->progress_cb)(a->progress_arg,
2913 GOT_STATUS_DELETE, path1);
2914 if (err)
2915 goto done;
2916 if (ie)
2917 got_fileindex_entry_mark_deleted_from_disk(ie);
2918 break;
2919 case GOT_STATUS_ADD: {
2920 struct got_object_id *id;
2921 FILE *blob1_f;
2922 off_t blob1_size;
2924 * Delete the added file only if its content already
2925 * exists in the repository.
2927 err = got_object_blob_file_create(&id, &blob1_f,
2928 &blob1_size, path1);
2929 if (err)
2930 goto done;
2931 if (got_object_id_cmp(id, id1) == 0) {
2932 err = (*a->progress_cb)(a->progress_arg,
2933 GOT_STATUS_DELETE, path1);
2934 if (err)
2935 goto done;
2936 err = remove_ondisk_file(a->worktree->root_path,
2937 path1);
2938 if (err)
2939 goto done;
2940 if (ie)
2941 got_fileindex_entry_remove(a->fileindex,
2942 ie);
2943 } else {
2944 err = (*a->progress_cb)(a->progress_arg,
2945 GOT_STATUS_CANNOT_DELETE, path1);
2947 if (fclose(blob1_f) == EOF && err == NULL)
2948 err = got_error_from_errno("fclose");
2949 free(id);
2950 if (err)
2951 goto done;
2952 break;
2954 case GOT_STATUS_MODIFY:
2955 case GOT_STATUS_CONFLICT:
2956 err = (*a->progress_cb)(a->progress_arg,
2957 GOT_STATUS_CANNOT_DELETE, path1);
2958 if (err)
2959 goto done;
2960 break;
2961 case GOT_STATUS_OBSTRUCTED:
2962 err = (*a->progress_cb)(a->progress_arg, status, path1);
2963 if (err)
2964 goto done;
2965 break;
2966 default:
2967 break;
2969 } else if (blob2) {
2970 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2971 path2) == -1)
2972 return got_error_from_errno("asprintf");
2973 ie = got_fileindex_entry_get(a->fileindex, path2,
2974 strlen(path2));
2975 if (ie) {
2976 err = get_file_status(&status, &sb, ie, ondisk_path,
2977 -1, NULL, repo);
2978 if (err)
2979 goto done;
2980 if (status != GOT_STATUS_NO_CHANGE &&
2981 status != GOT_STATUS_MODIFY &&
2982 status != GOT_STATUS_CONFLICT &&
2983 status != GOT_STATUS_ADD) {
2984 err = (*a->progress_cb)(a->progress_arg,
2985 status, path2);
2986 goto done;
2988 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2989 char *link_target2;
2990 err = got_object_blob_read_to_str(&link_target2,
2991 blob2);
2992 if (err)
2993 goto done;
2994 err = merge_symlink(a->worktree, NULL,
2995 ondisk_path, path2, a->label_orig,
2996 link_target2, a->commit_id2, repo,
2997 a->progress_cb, a->progress_arg);
2998 free(link_target2);
2999 } else if (S_ISREG(sb.st_mode)) {
3000 err = merge_blob(&local_changes_subsumed,
3001 a->worktree, NULL, ondisk_path, path2,
3002 sb.st_mode, a->label_orig, blob2,
3003 a->commit_id2, repo, a->progress_cb,
3004 a->progress_arg);
3005 } else {
3006 err = got_error_path(ondisk_path,
3007 GOT_ERR_FILE_OBSTRUCTED);
3009 if (err)
3010 goto done;
3011 if (status == GOT_STATUS_DELETE) {
3012 err = got_fileindex_entry_update(ie,
3013 a->worktree->root_fd, path2, blob2->id.sha1,
3014 a->worktree->base_commit_id->sha1, 0);
3015 if (err)
3016 goto done;
3018 } else {
3019 int is_bad_symlink = 0;
3020 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3021 if (S_ISLNK(mode2)) {
3022 err = install_symlink(&is_bad_symlink,
3023 a->worktree, ondisk_path, path2, blob2, 0,
3024 0, 1, a->allow_bad_symlinks, repo,
3025 a->progress_cb, a->progress_arg);
3026 } else {
3027 err = install_blob(a->worktree, ondisk_path, path2,
3028 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3029 a->progress_cb, a->progress_arg);
3031 if (err)
3032 goto done;
3033 err = got_fileindex_entry_alloc(&ie, path2);
3034 if (err)
3035 goto done;
3036 err = got_fileindex_entry_update(ie,
3037 a->worktree->root_fd, path2, NULL, NULL, 1);
3038 if (err) {
3039 got_fileindex_entry_free(ie);
3040 goto done;
3042 err = got_fileindex_entry_add(a->fileindex, ie);
3043 if (err) {
3044 got_fileindex_entry_free(ie);
3045 goto done;
3047 if (is_bad_symlink) {
3048 got_fileindex_entry_filetype_set(ie,
3049 GOT_FILEIDX_MODE_BAD_SYMLINK);
3053 done:
3054 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3055 err = got_error_from_errno("fclose");
3056 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3057 err = got_error_from_errno("fclose");
3058 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3059 err = got_error_from_errno("fclose");
3060 free(id_str);
3061 free(label_deriv2);
3062 free(ondisk_path);
3063 return err;
3066 static const struct got_error *
3067 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3069 struct got_worktree *worktree = arg;
3071 /* Reject merges into a work tree with mixed base commits. */
3072 if (got_fileindex_entry_has_commit(ie) &&
3073 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3074 SHA1_DIGEST_LENGTH) != 0)
3075 return got_error(GOT_ERR_MIXED_COMMITS);
3077 return NULL;
3080 struct check_merge_conflicts_arg {
3081 struct got_worktree *worktree;
3082 struct got_fileindex *fileindex;
3083 struct got_repository *repo;
3086 static const struct got_error *
3087 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3088 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3089 struct got_object_id *id1, struct got_object_id *id2,
3090 const char *path1, const char *path2,
3091 mode_t mode1, mode_t mode2, struct got_repository *repo)
3093 const struct got_error *err = NULL;
3094 struct check_merge_conflicts_arg *a = arg;
3095 unsigned char status;
3096 struct stat sb;
3097 struct got_fileindex_entry *ie;
3098 const char *path = path2 ? path2 : path1;
3099 struct got_object_id *id = id2 ? id2 : id1;
3100 char *ondisk_path;
3102 if (id == NULL)
3103 return NULL;
3105 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3106 if (ie == NULL)
3107 return NULL;
3109 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3110 == -1)
3111 return got_error_from_errno("asprintf");
3113 /* Reject merges into a work tree with conflicted files. */
3114 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3115 free(ondisk_path);
3116 if (err)
3117 return err;
3118 if (status == GOT_STATUS_CONFLICT)
3119 return got_error(GOT_ERR_CONFLICTS);
3121 return NULL;
3124 static const struct got_error *
3125 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3126 const char *fileindex_path, struct got_object_id *commit_id1,
3127 struct got_object_id *commit_id2, struct got_repository *repo,
3128 got_worktree_checkout_cb progress_cb, void *progress_arg,
3129 got_cancel_cb cancel_cb, void *cancel_arg)
3131 const struct got_error *err = NULL, *sync_err;
3132 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3133 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3134 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3135 struct check_merge_conflicts_arg cmc_arg;
3136 struct merge_file_cb_arg arg;
3137 char *label_orig = NULL;
3138 FILE *f1 = NULL, *f2 = NULL;
3139 int fd1 = -1, fd2 = -1;
3141 if (commit_id1) {
3142 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3143 if (err)
3144 goto done;
3145 err = got_object_id_by_path(&tree_id1, repo, commit1,
3146 worktree->path_prefix);
3147 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3148 goto done;
3150 if (tree_id1) {
3151 char *id_str;
3153 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3154 if (err)
3155 goto done;
3157 err = got_object_id_str(&id_str, commit_id1);
3158 if (err)
3159 goto done;
3161 if (asprintf(&label_orig, "%s: commit %s",
3162 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3163 err = got_error_from_errno("asprintf");
3164 free(id_str);
3165 goto done;
3167 free(id_str);
3169 f1 = got_opentemp();
3170 if (f1 == NULL) {
3171 err = got_error_from_errno("got_opentemp");
3172 goto done;
3176 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3177 if (err)
3178 goto done;
3180 err = got_object_id_by_path(&tree_id2, repo, commit2,
3181 worktree->path_prefix);
3182 if (err)
3183 goto done;
3185 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3186 if (err)
3187 goto done;
3189 f2 = got_opentemp();
3190 if (f2 == NULL) {
3191 err = got_error_from_errno("got_opentemp");
3192 goto done;
3195 fd1 = got_opentempfd();
3196 if (fd1 == -1) {
3197 err = got_error_from_errno("got_opentempfd");
3198 goto done;
3201 fd2 = got_opentempfd();
3202 if (fd2 == -1) {
3203 err = got_error_from_errno("got_opentempfd");
3204 goto done;
3207 cmc_arg.worktree = worktree;
3208 cmc_arg.fileindex = fileindex;
3209 cmc_arg.repo = repo;
3210 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3211 check_merge_conflicts, &cmc_arg, 0);
3212 if (err)
3213 goto done;
3215 arg.worktree = worktree;
3216 arg.fileindex = fileindex;
3217 arg.progress_cb = progress_cb;
3218 arg.progress_arg = progress_arg;
3219 arg.cancel_cb = cancel_cb;
3220 arg.cancel_arg = cancel_arg;
3221 arg.label_orig = label_orig;
3222 arg.commit_id2 = commit_id2;
3223 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3224 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3225 merge_file_cb, &arg, 1);
3226 sync_err = sync_fileindex(fileindex, fileindex_path);
3227 if (sync_err && err == NULL)
3228 err = sync_err;
3229 done:
3230 if (commit1)
3231 got_object_commit_close(commit1);
3232 if (commit2)
3233 got_object_commit_close(commit2);
3234 if (tree1)
3235 got_object_tree_close(tree1);
3236 if (tree2)
3237 got_object_tree_close(tree2);
3238 if (f1 && fclose(f1) == EOF && err == NULL)
3239 err = got_error_from_errno("fclose");
3240 if (f2 && fclose(f2) == EOF && err == NULL)
3241 err = got_error_from_errno("fclose");
3242 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3243 err = got_error_from_errno("close");
3244 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3245 err = got_error_from_errno("close");
3246 free(label_orig);
3247 return err;
3250 const struct got_error *
3251 got_worktree_merge_files(struct got_worktree *worktree,
3252 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3253 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3254 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3256 const struct got_error *err, *unlockerr;
3257 char *fileindex_path = NULL;
3258 struct got_fileindex *fileindex = NULL;
3260 err = lock_worktree(worktree, LOCK_EX);
3261 if (err)
3262 return err;
3264 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3265 if (err)
3266 goto done;
3268 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3269 worktree);
3270 if (err)
3271 goto done;
3273 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3274 commit_id2, repo, progress_cb, progress_arg,
3275 cancel_cb, cancel_arg);
3276 done:
3277 if (fileindex)
3278 got_fileindex_free(fileindex);
3279 free(fileindex_path);
3280 unlockerr = lock_worktree(worktree, LOCK_SH);
3281 if (unlockerr && err == NULL)
3282 err = unlockerr;
3283 return err;
3286 struct diff_dir_cb_arg {
3287 struct got_fileindex *fileindex;
3288 struct got_worktree *worktree;
3289 const char *status_path;
3290 size_t status_path_len;
3291 struct got_repository *repo;
3292 got_worktree_status_cb status_cb;
3293 void *status_arg;
3294 got_cancel_cb cancel_cb;
3295 void *cancel_arg;
3296 /* A pathlist containing per-directory pathlists of ignore patterns. */
3297 struct got_pathlist_head *ignores;
3298 int report_unchanged;
3299 int no_ignores;
3302 static const struct got_error *
3303 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3304 int dirfd, const char *de_name,
3305 got_worktree_status_cb status_cb, void *status_arg,
3306 struct got_repository *repo, int report_unchanged)
3308 const struct got_error *err = NULL;
3309 unsigned char status = GOT_STATUS_NO_CHANGE;
3310 unsigned char staged_status = get_staged_status(ie);
3311 struct stat sb;
3312 struct got_object_id blob_id, commit_id, staged_blob_id;
3313 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3314 struct got_object_id *staged_blob_idp = NULL;
3316 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3317 if (err)
3318 return err;
3320 if (status == GOT_STATUS_NO_CHANGE &&
3321 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3322 return NULL;
3324 if (got_fileindex_entry_has_blob(ie)) {
3325 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3326 blob_idp = &blob_id;
3328 if (got_fileindex_entry_has_commit(ie)) {
3329 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3330 commit_idp = &commit_id;
3332 if (staged_status == GOT_STATUS_ADD ||
3333 staged_status == GOT_STATUS_MODIFY) {
3334 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3335 SHA1_DIGEST_LENGTH);
3336 staged_blob_idp = &staged_blob_id;
3339 return (*status_cb)(status_arg, status, staged_status,
3340 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3343 static const struct got_error *
3344 status_old_new(void *arg, struct got_fileindex_entry *ie,
3345 struct dirent *de, const char *parent_path, int dirfd)
3347 const struct got_error *err = NULL;
3348 struct diff_dir_cb_arg *a = arg;
3349 char *abspath;
3351 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3352 return got_error(GOT_ERR_CANCELLED);
3354 if (got_path_cmp(parent_path, a->status_path,
3355 strlen(parent_path), a->status_path_len) != 0 &&
3356 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3357 return NULL;
3359 if (parent_path[0]) {
3360 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3361 parent_path, de->d_name) == -1)
3362 return got_error_from_errno("asprintf");
3363 } else {
3364 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3365 de->d_name) == -1)
3366 return got_error_from_errno("asprintf");
3369 err = report_file_status(ie, abspath, dirfd, de->d_name,
3370 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3371 free(abspath);
3372 return err;
3375 static const struct got_error *
3376 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3378 struct diff_dir_cb_arg *a = arg;
3379 struct got_object_id blob_id, commit_id;
3380 unsigned char status;
3382 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3383 return got_error(GOT_ERR_CANCELLED);
3385 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3386 return NULL;
3388 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3389 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3390 if (got_fileindex_entry_has_file_on_disk(ie))
3391 status = GOT_STATUS_MISSING;
3392 else
3393 status = GOT_STATUS_DELETE;
3394 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3395 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3398 static void
3399 free_ignorelist(struct got_pathlist_head *ignorelist)
3401 struct got_pathlist_entry *pe;
3403 TAILQ_FOREACH(pe, ignorelist, entry)
3404 free((char *)pe->path);
3405 got_pathlist_free(ignorelist);
3408 static void
3409 free_ignores(struct got_pathlist_head *ignores)
3411 struct got_pathlist_entry *pe;
3413 TAILQ_FOREACH(pe, ignores, entry) {
3414 struct got_pathlist_head *ignorelist = pe->data;
3415 free_ignorelist(ignorelist);
3416 free((char *)pe->path);
3418 got_pathlist_free(ignores);
3421 static const struct got_error *
3422 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3424 const struct got_error *err = NULL;
3425 struct got_pathlist_entry *pe = NULL;
3426 struct got_pathlist_head *ignorelist;
3427 char *line = NULL, *pattern, *dirpath = NULL;
3428 size_t linesize = 0;
3429 ssize_t linelen;
3431 ignorelist = calloc(1, sizeof(*ignorelist));
3432 if (ignorelist == NULL)
3433 return got_error_from_errno("calloc");
3434 TAILQ_INIT(ignorelist);
3436 while ((linelen = getline(&line, &linesize, f)) != -1) {
3437 if (linelen > 0 && line[linelen - 1] == '\n')
3438 line[linelen - 1] = '\0';
3440 /* Git's ignores may contain comments. */
3441 if (line[0] == '#')
3442 continue;
3444 /* Git's negated patterns are not (yet?) supported. */
3445 if (line[0] == '!')
3446 continue;
3448 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3449 line) == -1) {
3450 err = got_error_from_errno("asprintf");
3451 goto done;
3453 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3454 if (err)
3455 goto done;
3457 if (ferror(f)) {
3458 err = got_error_from_errno("getline");
3459 goto done;
3462 dirpath = strdup(path);
3463 if (dirpath == NULL) {
3464 err = got_error_from_errno("strdup");
3465 goto done;
3467 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3468 done:
3469 free(line);
3470 if (err || pe == NULL) {
3471 free(dirpath);
3472 free_ignorelist(ignorelist);
3474 return err;
3477 static int
3478 match_ignores(struct got_pathlist_head *ignores, const char *path)
3480 struct got_pathlist_entry *pe;
3482 /* Handle patterns which match in all directories. */
3483 TAILQ_FOREACH(pe, ignores, entry) {
3484 struct got_pathlist_head *ignorelist = pe->data;
3485 struct got_pathlist_entry *pi;
3487 TAILQ_FOREACH(pi, ignorelist, entry) {
3488 const char *p, *pattern = pi->path;
3490 if (strncmp(pattern, "**/", 3) != 0)
3491 continue;
3492 pattern += 3;
3493 p = path;
3494 while (*p) {
3495 if (fnmatch(pattern, p,
3496 FNM_PATHNAME | FNM_LEADING_DIR)) {
3497 /* Retry in next directory. */
3498 while (*p && *p != '/')
3499 p++;
3500 while (*p == '/')
3501 p++;
3502 continue;
3504 return 1;
3510 * The ignores pathlist contains ignore lists from children before
3511 * parents, so we can find the most specific ignorelist by walking
3512 * ignores backwards.
3514 pe = TAILQ_LAST(ignores, got_pathlist_head);
3515 while (pe) {
3516 if (got_path_is_child(path, pe->path, pe->path_len)) {
3517 struct got_pathlist_head *ignorelist = pe->data;
3518 struct got_pathlist_entry *pi;
3519 TAILQ_FOREACH(pi, ignorelist, entry) {
3520 const char *pattern = pi->path;
3521 int flags = FNM_LEADING_DIR;
3522 if (strstr(pattern, "/**/") == NULL)
3523 flags |= FNM_PATHNAME;
3524 if (fnmatch(pattern, path, flags))
3525 continue;
3526 return 1;
3529 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3532 return 0;
3535 static const struct got_error *
3536 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3537 const char *path, int dirfd, const char *ignores_filename)
3539 const struct got_error *err = NULL;
3540 char *ignorespath;
3541 int fd = -1;
3542 FILE *ignoresfile = NULL;
3544 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3545 path[0] ? "/" : "", ignores_filename) == -1)
3546 return got_error_from_errno("asprintf");
3548 if (dirfd != -1) {
3549 fd = openat(dirfd, ignores_filename,
3550 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3551 if (fd == -1) {
3552 if (errno != ENOENT && errno != EACCES)
3553 err = got_error_from_errno2("openat",
3554 ignorespath);
3555 } else {
3556 ignoresfile = fdopen(fd, "r");
3557 if (ignoresfile == NULL)
3558 err = got_error_from_errno2("fdopen",
3559 ignorespath);
3560 else {
3561 fd = -1;
3562 err = read_ignores(ignores, path, ignoresfile);
3565 } else {
3566 ignoresfile = fopen(ignorespath, "re");
3567 if (ignoresfile == NULL) {
3568 if (errno != ENOENT && errno != EACCES)
3569 err = got_error_from_errno2("fopen",
3570 ignorespath);
3571 } else
3572 err = read_ignores(ignores, path, ignoresfile);
3575 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3576 err = got_error_from_errno2("fclose", path);
3577 if (fd != -1 && close(fd) == -1 && err == NULL)
3578 err = got_error_from_errno2("close", path);
3579 free(ignorespath);
3580 return err;
3583 static const struct got_error *
3584 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3585 int dirfd)
3587 const struct got_error *err = NULL;
3588 struct diff_dir_cb_arg *a = arg;
3589 char *path = NULL;
3591 if (ignore != NULL)
3592 *ignore = 0;
3594 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3595 return got_error(GOT_ERR_CANCELLED);
3597 if (parent_path[0]) {
3598 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3599 return got_error_from_errno("asprintf");
3600 } else {
3601 path = de->d_name;
3604 if (de->d_type == DT_DIR) {
3605 if (!a->no_ignores && ignore != NULL &&
3606 match_ignores(a->ignores, path))
3607 *ignore = 1;
3608 } else if (!match_ignores(a->ignores, path) &&
3609 got_path_is_child(path, a->status_path, a->status_path_len))
3610 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3611 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3612 if (parent_path[0])
3613 free(path);
3614 return err;
3617 static const struct got_error *
3618 status_traverse(void *arg, const char *path, int dirfd)
3620 const struct got_error *err = NULL;
3621 struct diff_dir_cb_arg *a = arg;
3623 if (a->no_ignores)
3624 return NULL;
3626 err = add_ignores(a->ignores, a->worktree->root_path,
3627 path, dirfd, ".cvsignore");
3628 if (err)
3629 return err;
3631 err = add_ignores(a->ignores, a->worktree->root_path, path,
3632 dirfd, ".gitignore");
3634 return err;
3637 static const struct got_error *
3638 report_single_file_status(const char *path, const char *ondisk_path,
3639 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3640 void *status_arg, struct got_repository *repo, int report_unchanged,
3641 struct got_pathlist_head *ignores, int no_ignores)
3643 struct got_fileindex_entry *ie;
3644 struct stat sb;
3646 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3647 if (ie)
3648 return report_file_status(ie, ondisk_path, -1, NULL,
3649 status_cb, status_arg, repo, report_unchanged);
3651 if (lstat(ondisk_path, &sb) == -1) {
3652 if (errno != ENOENT)
3653 return got_error_from_errno2("lstat", ondisk_path);
3654 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3655 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3658 if (!no_ignores && match_ignores(ignores, path))
3659 return NULL;
3661 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3662 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3663 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3665 return NULL;
3668 static const struct got_error *
3669 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3670 const char *root_path, const char *path)
3672 const struct got_error *err;
3673 char *parent_path, *next_parent_path = NULL;
3675 err = add_ignores(ignores, root_path, "", -1,
3676 ".cvsignore");
3677 if (err)
3678 return err;
3680 err = add_ignores(ignores, root_path, "", -1,
3681 ".gitignore");
3682 if (err)
3683 return err;
3685 err = got_path_dirname(&parent_path, path);
3686 if (err) {
3687 if (err->code == GOT_ERR_BAD_PATH)
3688 return NULL; /* cannot traverse parent */
3689 return err;
3691 for (;;) {
3692 err = add_ignores(ignores, root_path, parent_path, -1,
3693 ".cvsignore");
3694 if (err)
3695 break;
3696 err = add_ignores(ignores, root_path, parent_path, -1,
3697 ".gitignore");
3698 if (err)
3699 break;
3700 err = got_path_dirname(&next_parent_path, parent_path);
3701 if (err) {
3702 if (err->code == GOT_ERR_BAD_PATH)
3703 err = NULL; /* traversed everything */
3704 break;
3706 if (got_path_is_root_dir(parent_path))
3707 break;
3708 free(parent_path);
3709 parent_path = next_parent_path;
3710 next_parent_path = NULL;
3713 free(parent_path);
3714 free(next_parent_path);
3715 return err;
3718 static const struct got_error *
3719 worktree_status(struct got_worktree *worktree, const char *path,
3720 struct got_fileindex *fileindex, struct got_repository *repo,
3721 got_worktree_status_cb status_cb, void *status_arg,
3722 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3723 int report_unchanged)
3725 const struct got_error *err = NULL;
3726 int fd = -1;
3727 struct got_fileindex_diff_dir_cb fdiff_cb;
3728 struct diff_dir_cb_arg arg;
3729 char *ondisk_path = NULL;
3730 struct got_pathlist_head ignores;
3731 struct got_fileindex_entry *ie;
3733 TAILQ_INIT(&ignores);
3735 if (asprintf(&ondisk_path, "%s%s%s",
3736 worktree->root_path, path[0] ? "/" : "", path) == -1)
3737 return got_error_from_errno("asprintf");
3739 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3740 if (ie) {
3741 err = report_single_file_status(path, ondisk_path,
3742 fileindex, status_cb, status_arg, repo,
3743 report_unchanged, &ignores, no_ignores);
3744 goto done;
3747 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3748 if (fd == -1) {
3749 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3750 !got_err_open_nofollow_on_symlink())
3751 err = got_error_from_errno2("open", ondisk_path);
3752 else {
3753 if (!no_ignores) {
3754 err = add_ignores_from_parent_paths(&ignores,
3755 worktree->root_path, ondisk_path);
3756 if (err)
3757 goto done;
3759 err = report_single_file_status(path, ondisk_path,
3760 fileindex, status_cb, status_arg, repo,
3761 report_unchanged, &ignores, no_ignores);
3763 } else {
3764 fdiff_cb.diff_old_new = status_old_new;
3765 fdiff_cb.diff_old = status_old;
3766 fdiff_cb.diff_new = status_new;
3767 fdiff_cb.diff_traverse = status_traverse;
3768 arg.fileindex = fileindex;
3769 arg.worktree = worktree;
3770 arg.status_path = path;
3771 arg.status_path_len = strlen(path);
3772 arg.repo = repo;
3773 arg.status_cb = status_cb;
3774 arg.status_arg = status_arg;
3775 arg.cancel_cb = cancel_cb;
3776 arg.cancel_arg = cancel_arg;
3777 arg.report_unchanged = report_unchanged;
3778 arg.no_ignores = no_ignores;
3779 if (!no_ignores) {
3780 err = add_ignores_from_parent_paths(&ignores,
3781 worktree->root_path, path);
3782 if (err)
3783 goto done;
3785 arg.ignores = &ignores;
3786 err = got_fileindex_diff_dir(fileindex, fd,
3787 worktree->root_path, path, repo, &fdiff_cb, &arg);
3789 done:
3790 free_ignores(&ignores);
3791 if (fd != -1 && close(fd) == -1 && err == NULL)
3792 err = got_error_from_errno("close");
3793 free(ondisk_path);
3794 return err;
3797 const struct got_error *
3798 got_worktree_status(struct got_worktree *worktree,
3799 struct got_pathlist_head *paths, struct got_repository *repo,
3800 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3801 got_cancel_cb cancel_cb, void *cancel_arg)
3803 const struct got_error *err = NULL;
3804 char *fileindex_path = NULL;
3805 struct got_fileindex *fileindex = NULL;
3806 struct got_pathlist_entry *pe;
3808 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3809 if (err)
3810 return err;
3812 TAILQ_FOREACH(pe, paths, entry) {
3813 err = worktree_status(worktree, pe->path, fileindex, repo,
3814 status_cb, status_arg, cancel_cb, cancel_arg,
3815 no_ignores, 0);
3816 if (err)
3817 break;
3819 free(fileindex_path);
3820 got_fileindex_free(fileindex);
3821 return err;
3824 const struct got_error *
3825 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3826 const char *arg)
3828 const struct got_error *err = NULL;
3829 char *resolved = NULL, *cwd = NULL, *path = NULL;
3830 size_t len;
3831 struct stat sb;
3832 char *abspath = NULL;
3833 char canonpath[PATH_MAX];
3835 *wt_path = NULL;
3837 cwd = getcwd(NULL, 0);
3838 if (cwd == NULL)
3839 return got_error_from_errno("getcwd");
3841 if (lstat(arg, &sb) == -1) {
3842 if (errno != ENOENT) {
3843 err = got_error_from_errno2("lstat", arg);
3844 goto done;
3846 sb.st_mode = 0;
3848 if (S_ISLNK(sb.st_mode)) {
3850 * We cannot use realpath(3) with symlinks since we want to
3851 * operate on the symlink itself.
3852 * But we can make the path absolute, assuming it is relative
3853 * to the current working directory, and then canonicalize it.
3855 if (!got_path_is_absolute(arg)) {
3856 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3857 err = got_error_from_errno("asprintf");
3858 goto done;
3862 err = got_canonpath(abspath ? abspath : arg, canonpath,
3863 sizeof(canonpath));
3864 if (err)
3865 goto done;
3866 resolved = strdup(canonpath);
3867 if (resolved == NULL) {
3868 err = got_error_from_errno("strdup");
3869 goto done;
3871 } else {
3872 resolved = realpath(arg, NULL);
3873 if (resolved == NULL) {
3874 if (errno != ENOENT) {
3875 err = got_error_from_errno2("realpath", arg);
3876 goto done;
3878 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3879 err = got_error_from_errno("asprintf");
3880 goto done;
3882 err = got_canonpath(abspath, canonpath,
3883 sizeof(canonpath));
3884 if (err)
3885 goto done;
3886 resolved = strdup(canonpath);
3887 if (resolved == NULL) {
3888 err = got_error_from_errno("strdup");
3889 goto done;
3894 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3895 strlen(got_worktree_get_root_path(worktree)))) {
3896 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3897 goto done;
3900 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3901 err = got_path_skip_common_ancestor(&path,
3902 got_worktree_get_root_path(worktree), resolved);
3903 if (err)
3904 goto done;
3905 } else {
3906 path = strdup("");
3907 if (path == NULL) {
3908 err = got_error_from_errno("strdup");
3909 goto done;
3913 /* XXX status walk can't deal with trailing slash! */
3914 len = strlen(path);
3915 while (len > 0 && path[len - 1] == '/') {
3916 path[len - 1] = '\0';
3917 len--;
3919 done:
3920 free(abspath);
3921 free(resolved);
3922 free(cwd);
3923 if (err == NULL)
3924 *wt_path = path;
3925 else
3926 free(path);
3927 return err;
3930 struct schedule_addition_args {
3931 struct got_worktree *worktree;
3932 struct got_fileindex *fileindex;
3933 got_worktree_checkout_cb progress_cb;
3934 void *progress_arg;
3935 struct got_repository *repo;
3938 static const struct got_error *
3939 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3940 const char *relpath, struct got_object_id *blob_id,
3941 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3942 int dirfd, const char *de_name)
3944 struct schedule_addition_args *a = arg;
3945 const struct got_error *err = NULL;
3946 struct got_fileindex_entry *ie;
3947 struct stat sb;
3948 char *ondisk_path;
3950 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3951 relpath) == -1)
3952 return got_error_from_errno("asprintf");
3954 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3955 if (ie) {
3956 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3957 de_name, a->repo);
3958 if (err)
3959 goto done;
3960 /* Re-adding an existing entry is a no-op. */
3961 if (status == GOT_STATUS_ADD)
3962 goto done;
3963 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3964 if (err)
3965 goto done;
3968 if (status != GOT_STATUS_UNVERSIONED) {
3969 if (status == GOT_STATUS_NONEXISTENT)
3970 err = got_error_set_errno(ENOENT, ondisk_path);
3971 else
3972 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3973 goto done;
3976 err = got_fileindex_entry_alloc(&ie, relpath);
3977 if (err)
3978 goto done;
3979 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3980 relpath, NULL, NULL, 1);
3981 if (err) {
3982 got_fileindex_entry_free(ie);
3983 goto done;
3985 err = got_fileindex_entry_add(a->fileindex, ie);
3986 if (err) {
3987 got_fileindex_entry_free(ie);
3988 goto done;
3990 done:
3991 free(ondisk_path);
3992 if (err)
3993 return err;
3994 if (status == GOT_STATUS_ADD)
3995 return NULL;
3996 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3999 const struct got_error *
4000 got_worktree_schedule_add(struct got_worktree *worktree,
4001 struct got_pathlist_head *paths,
4002 got_worktree_checkout_cb progress_cb, void *progress_arg,
4003 struct got_repository *repo, int no_ignores)
4005 struct got_fileindex *fileindex = NULL;
4006 char *fileindex_path = NULL;
4007 const struct got_error *err = NULL, *sync_err, *unlockerr;
4008 struct got_pathlist_entry *pe;
4009 struct schedule_addition_args saa;
4011 err = lock_worktree(worktree, LOCK_EX);
4012 if (err)
4013 return err;
4015 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4016 if (err)
4017 goto done;
4019 saa.worktree = worktree;
4020 saa.fileindex = fileindex;
4021 saa.progress_cb = progress_cb;
4022 saa.progress_arg = progress_arg;
4023 saa.repo = repo;
4025 TAILQ_FOREACH(pe, paths, entry) {
4026 err = worktree_status(worktree, pe->path, fileindex, repo,
4027 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4028 if (err)
4029 break;
4031 sync_err = sync_fileindex(fileindex, fileindex_path);
4032 if (sync_err && err == NULL)
4033 err = sync_err;
4034 done:
4035 free(fileindex_path);
4036 if (fileindex)
4037 got_fileindex_free(fileindex);
4038 unlockerr = lock_worktree(worktree, LOCK_SH);
4039 if (unlockerr && err == NULL)
4040 err = unlockerr;
4041 return err;
4044 struct schedule_deletion_args {
4045 struct got_worktree *worktree;
4046 struct got_fileindex *fileindex;
4047 got_worktree_delete_cb progress_cb;
4048 void *progress_arg;
4049 struct got_repository *repo;
4050 int delete_local_mods;
4051 int keep_on_disk;
4052 int ignore_missing_paths;
4053 const char *status_codes;
4056 static const struct got_error *
4057 schedule_for_deletion(void *arg, unsigned char status,
4058 unsigned char staged_status, const char *relpath,
4059 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4060 struct got_object_id *commit_id, int dirfd, const char *de_name)
4062 struct schedule_deletion_args *a = arg;
4063 const struct got_error *err = NULL;
4064 struct got_fileindex_entry *ie = NULL;
4065 struct stat sb;
4066 char *ondisk_path;
4068 if (status == GOT_STATUS_NONEXISTENT) {
4069 if (a->ignore_missing_paths)
4070 return NULL;
4071 return got_error_set_errno(ENOENT, relpath);
4074 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4075 if (ie == NULL)
4076 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4078 staged_status = get_staged_status(ie);
4079 if (staged_status != GOT_STATUS_NO_CHANGE) {
4080 if (staged_status == GOT_STATUS_DELETE)
4081 return NULL;
4082 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4085 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4086 relpath) == -1)
4087 return got_error_from_errno("asprintf");
4089 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4090 a->repo);
4091 if (err)
4092 goto done;
4094 if (a->status_codes) {
4095 size_t ncodes = strlen(a->status_codes);
4096 int i;
4097 for (i = 0; i < ncodes ; i++) {
4098 if (status == a->status_codes[i])
4099 break;
4101 if (i == ncodes) {
4102 /* Do not delete files in non-matching status. */
4103 free(ondisk_path);
4104 return NULL;
4106 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4107 a->status_codes[i] != GOT_STATUS_MISSING) {
4108 static char msg[64];
4109 snprintf(msg, sizeof(msg),
4110 "invalid status code '%c'", a->status_codes[i]);
4111 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4112 goto done;
4116 if (status != GOT_STATUS_NO_CHANGE) {
4117 if (status == GOT_STATUS_DELETE)
4118 goto done;
4119 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4120 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4121 goto done;
4123 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4124 err = got_error_set_errno(ENOENT, relpath);
4125 goto done;
4127 if (status != GOT_STATUS_MODIFY &&
4128 status != GOT_STATUS_MISSING) {
4129 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4130 goto done;
4134 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4135 size_t root_len;
4137 if (dirfd != -1) {
4138 if (unlinkat(dirfd, de_name, 0) != 0) {
4139 err = got_error_from_errno2("unlinkat",
4140 ondisk_path);
4141 goto done;
4143 } else if (unlink(ondisk_path) != 0) {
4144 err = got_error_from_errno2("unlink", ondisk_path);
4145 goto done;
4148 root_len = strlen(a->worktree->root_path);
4149 do {
4150 char *parent;
4151 err = got_path_dirname(&parent, ondisk_path);
4152 if (err)
4153 goto done;
4154 free(ondisk_path);
4155 ondisk_path = parent;
4156 if (rmdir(ondisk_path) == -1) {
4157 if (errno != ENOTEMPTY)
4158 err = got_error_from_errno2("rmdir",
4159 ondisk_path);
4160 break;
4162 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4163 strlen(ondisk_path), root_len) != 0);
4166 got_fileindex_entry_mark_deleted_from_disk(ie);
4167 done:
4168 free(ondisk_path);
4169 if (err)
4170 return err;
4171 if (status == GOT_STATUS_DELETE)
4172 return NULL;
4173 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4174 staged_status, relpath);
4177 const struct got_error *
4178 got_worktree_schedule_delete(struct got_worktree *worktree,
4179 struct got_pathlist_head *paths, int delete_local_mods,
4180 const char *status_codes,
4181 got_worktree_delete_cb progress_cb, void *progress_arg,
4182 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4184 struct got_fileindex *fileindex = NULL;
4185 char *fileindex_path = NULL;
4186 const struct got_error *err = NULL, *sync_err, *unlockerr;
4187 struct got_pathlist_entry *pe;
4188 struct schedule_deletion_args sda;
4190 err = lock_worktree(worktree, LOCK_EX);
4191 if (err)
4192 return err;
4194 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4195 if (err)
4196 goto done;
4198 sda.worktree = worktree;
4199 sda.fileindex = fileindex;
4200 sda.progress_cb = progress_cb;
4201 sda.progress_arg = progress_arg;
4202 sda.repo = repo;
4203 sda.delete_local_mods = delete_local_mods;
4204 sda.keep_on_disk = keep_on_disk;
4205 sda.ignore_missing_paths = ignore_missing_paths;
4206 sda.status_codes = status_codes;
4208 TAILQ_FOREACH(pe, paths, entry) {
4209 err = worktree_status(worktree, pe->path, fileindex, repo,
4210 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4211 if (err)
4212 break;
4214 sync_err = sync_fileindex(fileindex, fileindex_path);
4215 if (sync_err && err == NULL)
4216 err = sync_err;
4217 done:
4218 free(fileindex_path);
4219 if (fileindex)
4220 got_fileindex_free(fileindex);
4221 unlockerr = lock_worktree(worktree, LOCK_SH);
4222 if (unlockerr && err == NULL)
4223 err = unlockerr;
4224 return err;
4227 static const struct got_error *
4228 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4230 const struct got_error *err = NULL;
4231 char *line = NULL;
4232 size_t linesize = 0, n;
4233 ssize_t linelen;
4235 linelen = getline(&line, &linesize, infile);
4236 if (linelen == -1) {
4237 if (ferror(infile)) {
4238 err = got_error_from_errno("getline");
4239 goto done;
4241 return NULL;
4243 if (outfile) {
4244 n = fwrite(line, 1, linelen, outfile);
4245 if (n != linelen) {
4246 err = got_ferror(outfile, GOT_ERR_IO);
4247 goto done;
4250 if (rejectfile) {
4251 n = fwrite(line, 1, linelen, rejectfile);
4252 if (n != linelen)
4253 err = got_ferror(outfile, GOT_ERR_IO);
4255 done:
4256 free(line);
4257 return err;
4260 static const struct got_error *
4261 skip_one_line(FILE *f)
4263 char *line = NULL;
4264 size_t linesize = 0;
4265 ssize_t linelen;
4267 linelen = getline(&line, &linesize, f);
4268 if (linelen == -1) {
4269 if (ferror(f))
4270 return got_error_from_errno("getline");
4271 return NULL;
4273 free(line);
4274 return NULL;
4277 static const struct got_error *
4278 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4279 int start_old, int end_old, int start_new, int end_new,
4280 FILE *outfile, FILE *rejectfile)
4282 const struct got_error *err;
4284 /* Copy old file's lines leading up to patch. */
4285 while (!feof(f1) && *line_cur1 < start_old) {
4286 err = copy_one_line(f1, outfile, NULL);
4287 if (err)
4288 return err;
4289 (*line_cur1)++;
4291 /* Skip new file's lines leading up to patch. */
4292 while (!feof(f2) && *line_cur2 < start_new) {
4293 if (rejectfile)
4294 err = copy_one_line(f2, NULL, rejectfile);
4295 else
4296 err = skip_one_line(f2);
4297 if (err)
4298 return err;
4299 (*line_cur2)++;
4301 /* Copy patched lines. */
4302 while (!feof(f2) && *line_cur2 <= end_new) {
4303 err = copy_one_line(f2, outfile, NULL);
4304 if (err)
4305 return err;
4306 (*line_cur2)++;
4308 /* Skip over old file's replaced lines. */
4309 while (!feof(f1) && *line_cur1 <= end_old) {
4310 if (rejectfile)
4311 err = copy_one_line(f1, NULL, rejectfile);
4312 else
4313 err = skip_one_line(f1);
4314 if (err)
4315 return err;
4316 (*line_cur1)++;
4319 return NULL;
4322 static const struct got_error *
4323 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4324 FILE *outfile, FILE *rejectfile)
4326 const struct got_error *err;
4328 if (outfile) {
4329 /* Copy old file's lines until EOF. */
4330 while (!feof(f1)) {
4331 err = copy_one_line(f1, outfile, NULL);
4332 if (err)
4333 return err;
4334 (*line_cur1)++;
4337 if (rejectfile) {
4338 /* Copy new file's lines until EOF. */
4339 while (!feof(f2)) {
4340 err = copy_one_line(f2, NULL, rejectfile);
4341 if (err)
4342 return err;
4343 (*line_cur2)++;
4347 return NULL;
4350 static const struct got_error *
4351 apply_or_reject_change(int *choice, int *nchunks_used,
4352 struct diff_result *diff_result, int n,
4353 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4354 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4355 got_worktree_patch_cb patch_cb, void *patch_arg)
4357 const struct got_error *err = NULL;
4358 struct diff_chunk_context cc = {};
4359 int start_old, end_old, start_new, end_new;
4360 FILE *hunkfile;
4361 struct diff_output_unidiff_state *diff_state;
4362 struct diff_input_info diff_info;
4363 int rc;
4365 *choice = GOT_PATCH_CHOICE_NONE;
4367 /* Get changed line numbers without context lines for copy_change(). */
4368 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4369 start_old = cc.left.start;
4370 end_old = cc.left.end;
4371 start_new = cc.right.start;
4372 end_new = cc.right.end;
4374 /* Get the same change with context lines for display. */
4375 memset(&cc, 0, sizeof(cc));
4376 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4378 memset(&diff_info, 0, sizeof(diff_info));
4379 diff_info.left_path = relpath;
4380 diff_info.right_path = relpath;
4382 diff_state = diff_output_unidiff_state_alloc();
4383 if (diff_state == NULL)
4384 return got_error_set_errno(ENOMEM,
4385 "diff_output_unidiff_state_alloc");
4387 hunkfile = got_opentemp();
4388 if (hunkfile == NULL) {
4389 err = got_error_from_errno("got_opentemp");
4390 goto done;
4393 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4394 diff_result, &cc);
4395 if (rc != DIFF_RC_OK) {
4396 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4397 goto done;
4400 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4401 err = got_ferror(hunkfile, GOT_ERR_IO);
4402 goto done;
4405 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4406 hunkfile, changeno, nchanges);
4407 if (err)
4408 goto done;
4410 switch (*choice) {
4411 case GOT_PATCH_CHOICE_YES:
4412 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4413 end_old, start_new, end_new, outfile, rejectfile);
4414 break;
4415 case GOT_PATCH_CHOICE_NO:
4416 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4417 end_old, start_new, end_new, rejectfile, outfile);
4418 break;
4419 case GOT_PATCH_CHOICE_QUIT:
4420 break;
4421 default:
4422 err = got_error(GOT_ERR_PATCH_CHOICE);
4423 break;
4425 done:
4426 diff_output_unidiff_state_free(diff_state);
4427 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4428 err = got_error_from_errno("fclose");
4429 return err;
4432 struct revert_file_args {
4433 struct got_worktree *worktree;
4434 struct got_fileindex *fileindex;
4435 got_worktree_checkout_cb progress_cb;
4436 void *progress_arg;
4437 got_worktree_patch_cb patch_cb;
4438 void *patch_arg;
4439 struct got_repository *repo;
4440 int unlink_added_files;
4443 static const struct got_error *
4444 create_patched_content(char **path_outfile, int reverse_patch,
4445 struct got_object_id *blob_id, const char *path2,
4446 int dirfd2, const char *de_name2,
4447 const char *relpath, struct got_repository *repo,
4448 got_worktree_patch_cb patch_cb, void *patch_arg)
4450 const struct got_error *err, *free_err;
4451 struct got_blob_object *blob = NULL;
4452 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4453 int fd = -1, fd2 = -1;
4454 char link_target[PATH_MAX];
4455 ssize_t link_len = 0;
4456 char *path1 = NULL, *id_str = NULL;
4457 struct stat sb2;
4458 struct got_diffreg_result *diffreg_result = NULL;
4459 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4460 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4462 *path_outfile = NULL;
4464 err = got_object_id_str(&id_str, blob_id);
4465 if (err)
4466 return err;
4468 if (dirfd2 != -1) {
4469 fd2 = openat(dirfd2, de_name2,
4470 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4471 if (fd2 == -1) {
4472 if (!got_err_open_nofollow_on_symlink()) {
4473 err = got_error_from_errno2("openat", path2);
4474 goto done;
4476 link_len = readlinkat(dirfd2, de_name2,
4477 link_target, sizeof(link_target));
4478 if (link_len == -1) {
4479 return got_error_from_errno2("readlinkat",
4480 path2);
4482 sb2.st_mode = S_IFLNK;
4483 sb2.st_size = link_len;
4485 } else {
4486 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4487 if (fd2 == -1) {
4488 if (!got_err_open_nofollow_on_symlink()) {
4489 err = got_error_from_errno2("open", path2);
4490 goto done;
4492 link_len = readlink(path2, link_target,
4493 sizeof(link_target));
4494 if (link_len == -1)
4495 return got_error_from_errno2("readlink", path2);
4496 sb2.st_mode = S_IFLNK;
4497 sb2.st_size = link_len;
4500 if (fd2 != -1) {
4501 if (fstat(fd2, &sb2) == -1) {
4502 err = got_error_from_errno2("fstat", path2);
4503 goto done;
4506 f2 = fdopen(fd2, "r");
4507 if (f2 == NULL) {
4508 err = got_error_from_errno2("fdopen", path2);
4509 goto done;
4511 fd2 = -1;
4512 } else {
4513 size_t n;
4514 f2 = got_opentemp();
4515 if (f2 == NULL) {
4516 err = got_error_from_errno2("got_opentemp", path2);
4517 goto done;
4519 n = fwrite(link_target, 1, link_len, f2);
4520 if (n != link_len) {
4521 err = got_ferror(f2, GOT_ERR_IO);
4522 goto done;
4524 if (fflush(f2) == EOF) {
4525 err = got_error_from_errno("fflush");
4526 goto done;
4528 rewind(f2);
4531 fd = got_opentempfd();
4532 if (fd == -1) {
4533 err = got_error_from_errno("got_opentempfd");
4534 goto done;
4537 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4538 if (err)
4539 goto done;
4541 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4542 if (err)
4543 goto done;
4545 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4546 if (err)
4547 goto done;
4549 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4550 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4551 if (err)
4552 goto done;
4554 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4555 if (err)
4556 goto done;
4558 if (fseek(f1, 0L, SEEK_SET) == -1)
4559 return got_ferror(f1, GOT_ERR_IO);
4560 if (fseek(f2, 0L, SEEK_SET) == -1)
4561 return got_ferror(f2, GOT_ERR_IO);
4563 /* Count the number of actual changes in the diff result. */
4564 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4565 struct diff_chunk_context cc = {};
4566 diff_chunk_context_load_change(&cc, &nchunks_used,
4567 diffreg_result->result, n, 0);
4568 nchanges++;
4570 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4571 int choice;
4572 err = apply_or_reject_change(&choice, &nchunks_used,
4573 diffreg_result->result, n, relpath, f1, f2,
4574 &line_cur1, &line_cur2,
4575 reverse_patch ? NULL : outfile,
4576 reverse_patch ? outfile : NULL,
4577 ++i, nchanges, patch_cb, patch_arg);
4578 if (err)
4579 goto done;
4580 if (choice == GOT_PATCH_CHOICE_YES)
4581 have_content = 1;
4582 else if (choice == GOT_PATCH_CHOICE_QUIT)
4583 break;
4585 if (have_content) {
4586 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4587 reverse_patch ? NULL : outfile,
4588 reverse_patch ? outfile : NULL);
4589 if (err)
4590 goto done;
4592 if (!S_ISLNK(sb2.st_mode)) {
4593 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4594 err = got_error_from_errno2("fchmod", path2);
4595 goto done;
4599 done:
4600 free(id_str);
4601 if (fd != -1 && close(fd) == -1 && err == NULL)
4602 err = got_error_from_errno("close");
4603 if (blob)
4604 got_object_blob_close(blob);
4605 free_err = got_diffreg_result_free(diffreg_result);
4606 if (err == NULL)
4607 err = free_err;
4608 if (f1 && fclose(f1) == EOF && err == NULL)
4609 err = got_error_from_errno2("fclose", path1);
4610 if (f2 && fclose(f2) == EOF && err == NULL)
4611 err = got_error_from_errno2("fclose", path2);
4612 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4613 err = got_error_from_errno2("close", path2);
4614 if (outfile && fclose(outfile) == EOF && err == NULL)
4615 err = got_error_from_errno2("fclose", *path_outfile);
4616 if (path1 && unlink(path1) == -1 && err == NULL)
4617 err = got_error_from_errno2("unlink", path1);
4618 if (err || !have_content) {
4619 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4620 err = got_error_from_errno2("unlink", *path_outfile);
4621 free(*path_outfile);
4622 *path_outfile = NULL;
4624 free(path1);
4625 return err;
4628 static const struct got_error *
4629 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4630 const char *relpath, struct got_object_id *blob_id,
4631 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4632 int dirfd, const char *de_name)
4634 struct revert_file_args *a = arg;
4635 const struct got_error *err = NULL;
4636 char *parent_path = NULL;
4637 struct got_fileindex_entry *ie;
4638 struct got_commit_object *base_commit = NULL;
4639 struct got_tree_object *tree = NULL;
4640 struct got_object_id *tree_id = NULL;
4641 const struct got_tree_entry *te = NULL;
4642 char *tree_path = NULL, *te_name;
4643 char *ondisk_path = NULL, *path_content = NULL;
4644 struct got_blob_object *blob = NULL;
4645 int fd = -1;
4647 /* Reverting a staged deletion is a no-op. */
4648 if (status == GOT_STATUS_DELETE &&
4649 staged_status != GOT_STATUS_NO_CHANGE)
4650 return NULL;
4652 if (status == GOT_STATUS_UNVERSIONED)
4653 return (*a->progress_cb)(a->progress_arg,
4654 GOT_STATUS_UNVERSIONED, relpath);
4656 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4657 if (ie == NULL)
4658 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4660 /* Construct in-repository path of tree which contains this blob. */
4661 err = got_path_dirname(&parent_path, ie->path);
4662 if (err) {
4663 if (err->code != GOT_ERR_BAD_PATH)
4664 goto done;
4665 parent_path = strdup("/");
4666 if (parent_path == NULL) {
4667 err = got_error_from_errno("strdup");
4668 goto done;
4671 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4672 tree_path = strdup(parent_path);
4673 if (tree_path == NULL) {
4674 err = got_error_from_errno("strdup");
4675 goto done;
4677 } else {
4678 if (got_path_is_root_dir(parent_path)) {
4679 tree_path = strdup(a->worktree->path_prefix);
4680 if (tree_path == NULL) {
4681 err = got_error_from_errno("strdup");
4682 goto done;
4684 } else {
4685 if (asprintf(&tree_path, "%s/%s",
4686 a->worktree->path_prefix, parent_path) == -1) {
4687 err = got_error_from_errno("asprintf");
4688 goto done;
4693 err = got_object_open_as_commit(&base_commit, a->repo,
4694 a->worktree->base_commit_id);
4695 if (err)
4696 goto done;
4698 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4699 if (err) {
4700 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4701 (status == GOT_STATUS_ADD ||
4702 staged_status == GOT_STATUS_ADD)))
4703 goto done;
4704 } else {
4705 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4706 if (err)
4707 goto done;
4709 err = got_path_basename(&te_name, ie->path);
4710 if (err)
4711 goto done;
4713 te = got_object_tree_find_entry(tree, te_name);
4714 free(te_name);
4715 if (te == NULL && status != GOT_STATUS_ADD &&
4716 staged_status != GOT_STATUS_ADD) {
4717 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4718 goto done;
4722 switch (status) {
4723 case GOT_STATUS_ADD:
4724 if (a->patch_cb) {
4725 int choice = GOT_PATCH_CHOICE_NONE;
4726 err = (*a->patch_cb)(&choice, a->patch_arg,
4727 status, ie->path, NULL, 1, 1);
4728 if (err)
4729 goto done;
4730 if (choice != GOT_PATCH_CHOICE_YES)
4731 break;
4733 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4734 ie->path);
4735 if (err)
4736 goto done;
4737 got_fileindex_entry_remove(a->fileindex, ie);
4738 if (a->unlink_added_files) {
4739 if (asprintf(&ondisk_path, "%s/%s",
4740 got_worktree_get_root_path(a->worktree),
4741 relpath) == -1) {
4742 err = got_error_from_errno("asprintf");
4743 goto done;
4745 if (unlink(ondisk_path) == -1) {
4746 err = got_error_from_errno2("unlink",
4747 ondisk_path);
4748 break;
4751 break;
4752 case GOT_STATUS_DELETE:
4753 if (a->patch_cb) {
4754 int choice = GOT_PATCH_CHOICE_NONE;
4755 err = (*a->patch_cb)(&choice, a->patch_arg,
4756 status, ie->path, NULL, 1, 1);
4757 if (err)
4758 goto done;
4759 if (choice != GOT_PATCH_CHOICE_YES)
4760 break;
4762 /* fall through */
4763 case GOT_STATUS_MODIFY:
4764 case GOT_STATUS_MODE_CHANGE:
4765 case GOT_STATUS_CONFLICT:
4766 case GOT_STATUS_MISSING: {
4767 struct got_object_id id;
4768 if (staged_status == GOT_STATUS_ADD ||
4769 staged_status == GOT_STATUS_MODIFY) {
4770 memcpy(id.sha1, ie->staged_blob_sha1,
4771 SHA1_DIGEST_LENGTH);
4772 } else
4773 memcpy(id.sha1, ie->blob_sha1,
4774 SHA1_DIGEST_LENGTH);
4775 fd = got_opentempfd();
4776 if (fd == -1) {
4777 err = got_error_from_errno("got_opentempfd");
4778 goto done;
4781 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
4782 if (err)
4783 goto done;
4785 if (asprintf(&ondisk_path, "%s/%s",
4786 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4787 err = got_error_from_errno("asprintf");
4788 goto done;
4791 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4792 status == GOT_STATUS_CONFLICT)) {
4793 int is_bad_symlink = 0;
4794 err = create_patched_content(&path_content, 1, &id,
4795 ondisk_path, dirfd, de_name, ie->path, a->repo,
4796 a->patch_cb, a->patch_arg);
4797 if (err || path_content == NULL)
4798 break;
4799 if (te && S_ISLNK(te->mode)) {
4800 if (unlink(path_content) == -1) {
4801 err = got_error_from_errno2("unlink",
4802 path_content);
4803 break;
4805 err = install_symlink(&is_bad_symlink,
4806 a->worktree, ondisk_path, ie->path,
4807 blob, 0, 1, 0, 0, a->repo,
4808 a->progress_cb, a->progress_arg);
4809 } else {
4810 if (rename(path_content, ondisk_path) == -1) {
4811 err = got_error_from_errno3("rename",
4812 path_content, ondisk_path);
4813 goto done;
4816 } else {
4817 int is_bad_symlink = 0;
4818 if (te && S_ISLNK(te->mode)) {
4819 err = install_symlink(&is_bad_symlink,
4820 a->worktree, ondisk_path, ie->path,
4821 blob, 0, 1, 0, 0, a->repo,
4822 a->progress_cb, a->progress_arg);
4823 } else {
4824 err = install_blob(a->worktree, ondisk_path,
4825 ie->path,
4826 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4827 got_fileindex_perms_to_st(ie), blob,
4828 0, 1, 0, 0, a->repo,
4829 a->progress_cb, a->progress_arg);
4831 if (err)
4832 goto done;
4833 if (status == GOT_STATUS_DELETE ||
4834 status == GOT_STATUS_MODE_CHANGE) {
4835 err = got_fileindex_entry_update(ie,
4836 a->worktree->root_fd, relpath,
4837 blob->id.sha1,
4838 a->worktree->base_commit_id->sha1, 1);
4839 if (err)
4840 goto done;
4842 if (is_bad_symlink) {
4843 got_fileindex_entry_filetype_set(ie,
4844 GOT_FILEIDX_MODE_BAD_SYMLINK);
4847 break;
4849 default:
4850 break;
4852 done:
4853 free(ondisk_path);
4854 free(path_content);
4855 free(parent_path);
4856 free(tree_path);
4857 if (fd != -1 && close(fd) == -1 && err == NULL)
4858 err = got_error_from_errno("close");
4859 if (blob)
4860 got_object_blob_close(blob);
4861 if (tree)
4862 got_object_tree_close(tree);
4863 free(tree_id);
4864 if (base_commit)
4865 got_object_commit_close(base_commit);
4866 return err;
4869 const struct got_error *
4870 got_worktree_revert(struct got_worktree *worktree,
4871 struct got_pathlist_head *paths,
4872 got_worktree_checkout_cb progress_cb, void *progress_arg,
4873 got_worktree_patch_cb patch_cb, void *patch_arg,
4874 struct got_repository *repo)
4876 struct got_fileindex *fileindex = NULL;
4877 char *fileindex_path = NULL;
4878 const struct got_error *err = NULL, *unlockerr = NULL;
4879 const struct got_error *sync_err = NULL;
4880 struct got_pathlist_entry *pe;
4881 struct revert_file_args rfa;
4883 err = lock_worktree(worktree, LOCK_EX);
4884 if (err)
4885 return err;
4887 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4888 if (err)
4889 goto done;
4891 rfa.worktree = worktree;
4892 rfa.fileindex = fileindex;
4893 rfa.progress_cb = progress_cb;
4894 rfa.progress_arg = progress_arg;
4895 rfa.patch_cb = patch_cb;
4896 rfa.patch_arg = patch_arg;
4897 rfa.repo = repo;
4898 rfa.unlink_added_files = 0;
4899 TAILQ_FOREACH(pe, paths, entry) {
4900 err = worktree_status(worktree, pe->path, fileindex, repo,
4901 revert_file, &rfa, NULL, NULL, 1, 0);
4902 if (err)
4903 break;
4905 sync_err = sync_fileindex(fileindex, fileindex_path);
4906 if (sync_err && err == NULL)
4907 err = sync_err;
4908 done:
4909 free(fileindex_path);
4910 if (fileindex)
4911 got_fileindex_free(fileindex);
4912 unlockerr = lock_worktree(worktree, LOCK_SH);
4913 if (unlockerr && err == NULL)
4914 err = unlockerr;
4915 return err;
4918 static void
4919 free_commitable(struct got_commitable *ct)
4921 free(ct->path);
4922 free(ct->in_repo_path);
4923 free(ct->ondisk_path);
4924 free(ct->blob_id);
4925 free(ct->base_blob_id);
4926 free(ct->staged_blob_id);
4927 free(ct->base_commit_id);
4928 free(ct);
4931 struct collect_commitables_arg {
4932 struct got_pathlist_head *commitable_paths;
4933 struct got_repository *repo;
4934 struct got_worktree *worktree;
4935 struct got_fileindex *fileindex;
4936 int have_staged_files;
4937 int allow_bad_symlinks;
4940 static const struct got_error *
4941 collect_commitables(void *arg, unsigned char status,
4942 unsigned char staged_status, const char *relpath,
4943 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4944 struct got_object_id *commit_id, int dirfd, const char *de_name)
4946 struct collect_commitables_arg *a = arg;
4947 const struct got_error *err = NULL;
4948 struct got_commitable *ct = NULL;
4949 struct got_pathlist_entry *new = NULL;
4950 char *parent_path = NULL, *path = NULL;
4951 struct stat sb;
4953 if (a->have_staged_files) {
4954 if (staged_status != GOT_STATUS_MODIFY &&
4955 staged_status != GOT_STATUS_ADD &&
4956 staged_status != GOT_STATUS_DELETE)
4957 return NULL;
4958 } else {
4959 if (status == GOT_STATUS_CONFLICT)
4960 return got_error(GOT_ERR_COMMIT_CONFLICT);
4962 if (status != GOT_STATUS_MODIFY &&
4963 status != GOT_STATUS_MODE_CHANGE &&
4964 status != GOT_STATUS_ADD &&
4965 status != GOT_STATUS_DELETE)
4966 return NULL;
4969 if (asprintf(&path, "/%s", relpath) == -1) {
4970 err = got_error_from_errno("asprintf");
4971 goto done;
4973 if (strcmp(path, "/") == 0) {
4974 parent_path = strdup("");
4975 if (parent_path == NULL)
4976 return got_error_from_errno("strdup");
4977 } else {
4978 err = got_path_dirname(&parent_path, path);
4979 if (err)
4980 return err;
4983 ct = calloc(1, sizeof(*ct));
4984 if (ct == NULL) {
4985 err = got_error_from_errno("calloc");
4986 goto done;
4989 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4990 relpath) == -1) {
4991 err = got_error_from_errno("asprintf");
4992 goto done;
4995 if (staged_status == GOT_STATUS_ADD ||
4996 staged_status == GOT_STATUS_MODIFY) {
4997 struct got_fileindex_entry *ie;
4998 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4999 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5000 case GOT_FILEIDX_MODE_REGULAR_FILE:
5001 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5002 ct->mode = S_IFREG;
5003 break;
5004 case GOT_FILEIDX_MODE_SYMLINK:
5005 ct->mode = S_IFLNK;
5006 break;
5007 default:
5008 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5009 goto done;
5011 ct->mode |= got_fileindex_entry_perms_get(ie);
5012 } else if (status != GOT_STATUS_DELETE &&
5013 staged_status != GOT_STATUS_DELETE) {
5014 if (dirfd != -1) {
5015 if (fstatat(dirfd, de_name, &sb,
5016 AT_SYMLINK_NOFOLLOW) == -1) {
5017 err = got_error_from_errno2("fstatat",
5018 ct->ondisk_path);
5019 goto done;
5021 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5022 err = got_error_from_errno2("lstat", ct->ondisk_path);
5023 goto done;
5025 ct->mode = sb.st_mode;
5028 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5029 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5030 relpath) == -1) {
5031 err = got_error_from_errno("asprintf");
5032 goto done;
5035 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5036 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5037 int is_bad_symlink;
5038 char target_path[PATH_MAX];
5039 ssize_t target_len;
5040 target_len = readlink(ct->ondisk_path, target_path,
5041 sizeof(target_path));
5042 if (target_len == -1) {
5043 err = got_error_from_errno2("readlink",
5044 ct->ondisk_path);
5045 goto done;
5047 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5048 target_len, ct->ondisk_path, a->worktree->root_path);
5049 if (err)
5050 goto done;
5051 if (is_bad_symlink) {
5052 err = got_error_path(ct->ondisk_path,
5053 GOT_ERR_BAD_SYMLINK);
5054 goto done;
5059 ct->status = status;
5060 ct->staged_status = staged_status;
5061 ct->blob_id = NULL; /* will be filled in when blob gets created */
5062 if (ct->status != GOT_STATUS_ADD &&
5063 ct->staged_status != GOT_STATUS_ADD) {
5064 ct->base_blob_id = got_object_id_dup(blob_id);
5065 if (ct->base_blob_id == NULL) {
5066 err = got_error_from_errno("got_object_id_dup");
5067 goto done;
5069 ct->base_commit_id = got_object_id_dup(commit_id);
5070 if (ct->base_commit_id == NULL) {
5071 err = got_error_from_errno("got_object_id_dup");
5072 goto done;
5075 if (ct->staged_status == GOT_STATUS_ADD ||
5076 ct->staged_status == GOT_STATUS_MODIFY) {
5077 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5078 if (ct->staged_blob_id == NULL) {
5079 err = got_error_from_errno("got_object_id_dup");
5080 goto done;
5083 ct->path = strdup(path);
5084 if (ct->path == NULL) {
5085 err = got_error_from_errno("strdup");
5086 goto done;
5088 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5089 done:
5090 if (ct && (err || new == NULL))
5091 free_commitable(ct);
5092 free(parent_path);
5093 free(path);
5094 return err;
5097 static const struct got_error *write_tree(struct got_object_id **, int *,
5098 struct got_tree_object *, const char *, struct got_pathlist_head *,
5099 got_worktree_status_cb status_cb, void *status_arg,
5100 struct got_repository *);
5102 static const struct got_error *
5103 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5104 struct got_tree_entry *te, const char *parent_path,
5105 struct got_pathlist_head *commitable_paths,
5106 got_worktree_status_cb status_cb, void *status_arg,
5107 struct got_repository *repo)
5109 const struct got_error *err = NULL;
5110 struct got_tree_object *subtree;
5111 char *subpath;
5113 if (asprintf(&subpath, "%s%s%s", parent_path,
5114 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5115 return got_error_from_errno("asprintf");
5117 err = got_object_open_as_tree(&subtree, repo, &te->id);
5118 if (err)
5119 return err;
5121 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5122 commitable_paths, status_cb, status_arg, repo);
5123 got_object_tree_close(subtree);
5124 free(subpath);
5125 return err;
5128 static const struct got_error *
5129 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5131 const struct got_error *err = NULL;
5132 char *ct_parent_path = NULL;
5134 *match = 0;
5136 if (strchr(ct->in_repo_path, '/') == NULL) {
5137 *match = got_path_is_root_dir(path);
5138 return NULL;
5141 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5142 if (err)
5143 return err;
5144 *match = (strcmp(path, ct_parent_path) == 0);
5145 free(ct_parent_path);
5146 return err;
5149 static mode_t
5150 get_ct_file_mode(struct got_commitable *ct)
5152 if (S_ISLNK(ct->mode))
5153 return S_IFLNK;
5155 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5158 static const struct got_error *
5159 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5160 struct got_tree_entry *te, struct got_commitable *ct)
5162 const struct got_error *err = NULL;
5164 *new_te = NULL;
5166 err = got_object_tree_entry_dup(new_te, te);
5167 if (err)
5168 goto done;
5170 (*new_te)->mode = get_ct_file_mode(ct);
5172 if (ct->staged_status == GOT_STATUS_MODIFY)
5173 memcpy(&(*new_te)->id, ct->staged_blob_id,
5174 sizeof((*new_te)->id));
5175 else
5176 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5177 done:
5178 if (err && *new_te) {
5179 free(*new_te);
5180 *new_te = NULL;
5182 return err;
5185 static const struct got_error *
5186 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5187 struct got_commitable *ct)
5189 const struct got_error *err = NULL;
5190 char *ct_name = NULL;
5192 *new_te = NULL;
5194 *new_te = calloc(1, sizeof(**new_te));
5195 if (*new_te == NULL)
5196 return got_error_from_errno("calloc");
5198 err = got_path_basename(&ct_name, ct->path);
5199 if (err)
5200 goto done;
5201 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5202 sizeof((*new_te)->name)) {
5203 err = got_error(GOT_ERR_NO_SPACE);
5204 goto done;
5207 (*new_te)->mode = get_ct_file_mode(ct);
5209 if (ct->staged_status == GOT_STATUS_ADD)
5210 memcpy(&(*new_te)->id, ct->staged_blob_id,
5211 sizeof((*new_te)->id));
5212 else
5213 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5214 done:
5215 free(ct_name);
5216 if (err && *new_te) {
5217 free(*new_te);
5218 *new_te = NULL;
5220 return err;
5223 static const struct got_error *
5224 insert_tree_entry(struct got_tree_entry *new_te,
5225 struct got_pathlist_head *paths)
5227 const struct got_error *err = NULL;
5228 struct got_pathlist_entry *new_pe;
5230 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5231 if (err)
5232 return err;
5233 if (new_pe == NULL)
5234 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5235 return NULL;
5238 static const struct got_error *
5239 report_ct_status(struct got_commitable *ct,
5240 got_worktree_status_cb status_cb, void *status_arg)
5242 const char *ct_path = ct->path;
5243 unsigned char status;
5245 if (status_cb == NULL) /* no commit progress output desired */
5246 return NULL;
5248 while (ct_path[0] == '/')
5249 ct_path++;
5251 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5252 status = ct->staged_status;
5253 else
5254 status = ct->status;
5256 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5257 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5260 static const struct got_error *
5261 match_modified_subtree(int *modified, struct got_tree_entry *te,
5262 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5264 const struct got_error *err = NULL;
5265 struct got_pathlist_entry *pe;
5266 char *te_path;
5268 *modified = 0;
5270 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5271 got_path_is_root_dir(base_tree_path) ? "" : "/",
5272 te->name) == -1)
5273 return got_error_from_errno("asprintf");
5275 TAILQ_FOREACH(pe, commitable_paths, entry) {
5276 struct got_commitable *ct = pe->data;
5277 *modified = got_path_is_child(ct->in_repo_path, te_path,
5278 strlen(te_path));
5279 if (*modified)
5280 break;
5283 free(te_path);
5284 return err;
5287 static const struct got_error *
5288 match_deleted_or_modified_ct(struct got_commitable **ctp,
5289 struct got_tree_entry *te, const char *base_tree_path,
5290 struct got_pathlist_head *commitable_paths)
5292 const struct got_error *err = NULL;
5293 struct got_pathlist_entry *pe;
5295 *ctp = NULL;
5297 TAILQ_FOREACH(pe, commitable_paths, entry) {
5298 struct got_commitable *ct = pe->data;
5299 char *ct_name = NULL;
5300 int path_matches;
5302 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5303 if (ct->status != GOT_STATUS_MODIFY &&
5304 ct->status != GOT_STATUS_MODE_CHANGE &&
5305 ct->status != GOT_STATUS_DELETE)
5306 continue;
5307 } else {
5308 if (ct->staged_status != GOT_STATUS_MODIFY &&
5309 ct->staged_status != GOT_STATUS_DELETE)
5310 continue;
5313 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5314 continue;
5316 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5317 if (err)
5318 return err;
5319 if (!path_matches)
5320 continue;
5322 err = got_path_basename(&ct_name, pe->path);
5323 if (err)
5324 return err;
5326 if (strcmp(te->name, ct_name) != 0) {
5327 free(ct_name);
5328 continue;
5330 free(ct_name);
5332 *ctp = ct;
5333 break;
5336 return err;
5339 static const struct got_error *
5340 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5341 const char *child_path, const char *path_base_tree,
5342 struct got_pathlist_head *commitable_paths,
5343 got_worktree_status_cb status_cb, void *status_arg,
5344 struct got_repository *repo)
5346 const struct got_error *err = NULL;
5347 struct got_tree_entry *new_te;
5348 char *subtree_path;
5349 struct got_object_id *id = NULL;
5350 int nentries;
5352 *new_tep = NULL;
5354 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5355 got_path_is_root_dir(path_base_tree) ? "" : "/",
5356 child_path) == -1)
5357 return got_error_from_errno("asprintf");
5359 new_te = calloc(1, sizeof(*new_te));
5360 if (new_te == NULL)
5361 return got_error_from_errno("calloc");
5362 new_te->mode = S_IFDIR;
5364 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5365 sizeof(new_te->name)) {
5366 err = got_error(GOT_ERR_NO_SPACE);
5367 goto done;
5369 err = write_tree(&id, &nentries, NULL, subtree_path,
5370 commitable_paths, status_cb, status_arg, repo);
5371 if (err) {
5372 free(new_te);
5373 goto done;
5375 memcpy(&new_te->id, id, sizeof(new_te->id));
5376 done:
5377 free(id);
5378 free(subtree_path);
5379 if (err == NULL)
5380 *new_tep = new_te;
5381 return err;
5384 static const struct got_error *
5385 write_tree(struct got_object_id **new_tree_id, int *nentries,
5386 struct got_tree_object *base_tree, const char *path_base_tree,
5387 struct got_pathlist_head *commitable_paths,
5388 got_worktree_status_cb status_cb, void *status_arg,
5389 struct got_repository *repo)
5391 const struct got_error *err = NULL;
5392 struct got_pathlist_head paths;
5393 struct got_tree_entry *te, *new_te = NULL;
5394 struct got_pathlist_entry *pe;
5396 TAILQ_INIT(&paths);
5397 *nentries = 0;
5399 /* Insert, and recurse into, newly added entries first. */
5400 TAILQ_FOREACH(pe, commitable_paths, entry) {
5401 struct got_commitable *ct = pe->data;
5402 char *child_path = NULL, *slash;
5404 if ((ct->status != GOT_STATUS_ADD &&
5405 ct->staged_status != GOT_STATUS_ADD) ||
5406 (ct->flags & GOT_COMMITABLE_ADDED))
5407 continue;
5409 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5410 strlen(path_base_tree)))
5411 continue;
5413 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5414 ct->in_repo_path);
5415 if (err)
5416 goto done;
5418 slash = strchr(child_path, '/');
5419 if (slash == NULL) {
5420 err = alloc_added_blob_tree_entry(&new_te, ct);
5421 if (err)
5422 goto done;
5423 err = report_ct_status(ct, status_cb, status_arg);
5424 if (err)
5425 goto done;
5426 ct->flags |= GOT_COMMITABLE_ADDED;
5427 err = insert_tree_entry(new_te, &paths);
5428 if (err)
5429 goto done;
5430 (*nentries)++;
5431 } else {
5432 *slash = '\0'; /* trim trailing path components */
5433 if (base_tree == NULL ||
5434 got_object_tree_find_entry(base_tree, child_path)
5435 == NULL) {
5436 err = make_subtree_for_added_blob(&new_te,
5437 child_path, path_base_tree,
5438 commitable_paths, status_cb, status_arg,
5439 repo);
5440 if (err)
5441 goto done;
5442 err = insert_tree_entry(new_te, &paths);
5443 if (err)
5444 goto done;
5445 (*nentries)++;
5450 if (base_tree) {
5451 int i, nbase_entries;
5452 /* Handle modified and deleted entries. */
5453 nbase_entries = got_object_tree_get_nentries(base_tree);
5454 for (i = 0; i < nbase_entries; i++) {
5455 struct got_commitable *ct = NULL;
5457 te = got_object_tree_get_entry(base_tree, i);
5458 if (got_object_tree_entry_is_submodule(te)) {
5459 /* Entry is a submodule; just copy it. */
5460 err = got_object_tree_entry_dup(&new_te, te);
5461 if (err)
5462 goto done;
5463 err = insert_tree_entry(new_te, &paths);
5464 if (err)
5465 goto done;
5466 (*nentries)++;
5467 continue;
5470 if (S_ISDIR(te->mode)) {
5471 int modified;
5472 err = got_object_tree_entry_dup(&new_te, te);
5473 if (err)
5474 goto done;
5475 err = match_modified_subtree(&modified, te,
5476 path_base_tree, commitable_paths);
5477 if (err)
5478 goto done;
5479 /* Avoid recursion into unmodified subtrees. */
5480 if (modified) {
5481 struct got_object_id *new_id;
5482 int nsubentries;
5483 err = write_subtree(&new_id,
5484 &nsubentries, te,
5485 path_base_tree, commitable_paths,
5486 status_cb, status_arg, repo);
5487 if (err)
5488 goto done;
5489 if (nsubentries == 0) {
5490 /* All entries were deleted. */
5491 free(new_id);
5492 continue;
5494 memcpy(&new_te->id, new_id,
5495 sizeof(new_te->id));
5496 free(new_id);
5498 err = insert_tree_entry(new_te, &paths);
5499 if (err)
5500 goto done;
5501 (*nentries)++;
5502 continue;
5505 err = match_deleted_or_modified_ct(&ct, te,
5506 path_base_tree, commitable_paths);
5507 if (err)
5508 goto done;
5509 if (ct) {
5510 /* NB: Deleted entries get dropped here. */
5511 if (ct->status == GOT_STATUS_MODIFY ||
5512 ct->status == GOT_STATUS_MODE_CHANGE ||
5513 ct->staged_status == GOT_STATUS_MODIFY) {
5514 err = alloc_modified_blob_tree_entry(
5515 &new_te, te, ct);
5516 if (err)
5517 goto done;
5518 err = insert_tree_entry(new_te, &paths);
5519 if (err)
5520 goto done;
5521 (*nentries)++;
5523 err = report_ct_status(ct, status_cb,
5524 status_arg);
5525 if (err)
5526 goto done;
5527 } else {
5528 /* Entry is unchanged; just copy it. */
5529 err = got_object_tree_entry_dup(&new_te, te);
5530 if (err)
5531 goto done;
5532 err = insert_tree_entry(new_te, &paths);
5533 if (err)
5534 goto done;
5535 (*nentries)++;
5540 /* Write new list of entries; deleted entries have been dropped. */
5541 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5542 done:
5543 got_pathlist_free(&paths);
5544 return err;
5547 static const struct got_error *
5548 update_fileindex_after_commit(struct got_worktree *worktree,
5549 struct got_pathlist_head *commitable_paths,
5550 struct got_object_id *new_base_commit_id,
5551 struct got_fileindex *fileindex, int have_staged_files)
5553 const struct got_error *err = NULL;
5554 struct got_pathlist_entry *pe;
5555 char *relpath = NULL;
5557 TAILQ_FOREACH(pe, commitable_paths, entry) {
5558 struct got_fileindex_entry *ie;
5559 struct got_commitable *ct = pe->data;
5561 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5563 err = got_path_skip_common_ancestor(&relpath,
5564 worktree->root_path, ct->ondisk_path);
5565 if (err)
5566 goto done;
5568 if (ie) {
5569 if (ct->status == GOT_STATUS_DELETE ||
5570 ct->staged_status == GOT_STATUS_DELETE) {
5571 got_fileindex_entry_remove(fileindex, ie);
5572 } else if (ct->staged_status == GOT_STATUS_ADD ||
5573 ct->staged_status == GOT_STATUS_MODIFY) {
5574 got_fileindex_entry_stage_set(ie,
5575 GOT_FILEIDX_STAGE_NONE);
5576 got_fileindex_entry_staged_filetype_set(ie, 0);
5578 err = got_fileindex_entry_update(ie,
5579 worktree->root_fd, relpath,
5580 ct->staged_blob_id->sha1,
5581 new_base_commit_id->sha1,
5582 !have_staged_files);
5583 } else
5584 err = got_fileindex_entry_update(ie,
5585 worktree->root_fd, relpath,
5586 ct->blob_id->sha1,
5587 new_base_commit_id->sha1,
5588 !have_staged_files);
5589 } else {
5590 err = got_fileindex_entry_alloc(&ie, pe->path);
5591 if (err)
5592 goto done;
5593 err = got_fileindex_entry_update(ie,
5594 worktree->root_fd, relpath, ct->blob_id->sha1,
5595 new_base_commit_id->sha1, 1);
5596 if (err) {
5597 got_fileindex_entry_free(ie);
5598 goto done;
5600 err = got_fileindex_entry_add(fileindex, ie);
5601 if (err) {
5602 got_fileindex_entry_free(ie);
5603 goto done;
5606 free(relpath);
5607 relpath = NULL;
5609 done:
5610 free(relpath);
5611 return err;
5615 static const struct got_error *
5616 check_out_of_date(const char *in_repo_path, unsigned char status,
5617 unsigned char staged_status, struct got_object_id *base_blob_id,
5618 struct got_object_id *base_commit_id,
5619 struct got_object_id *head_commit_id, struct got_repository *repo,
5620 int ood_errcode)
5622 const struct got_error *err = NULL;
5623 struct got_commit_object *commit = NULL;
5624 struct got_object_id *id = NULL;
5626 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5627 /* Trivial case: base commit == head commit */
5628 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5629 return NULL;
5631 * Ensure file content which local changes were based
5632 * on matches file content in the branch head.
5634 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5635 if (err)
5636 goto done;
5637 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5638 if (err) {
5639 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5640 err = got_error(ood_errcode);
5641 goto done;
5642 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5643 err = got_error(ood_errcode);
5644 } else {
5645 /* Require that added files don't exist in the branch head. */
5646 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5647 if (err)
5648 goto done;
5649 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5650 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5651 goto done;
5652 err = id ? got_error(ood_errcode) : NULL;
5654 done:
5655 free(id);
5656 if (commit)
5657 got_object_commit_close(commit);
5658 return err;
5661 static const struct got_error *
5662 commit_worktree(struct got_object_id **new_commit_id,
5663 struct got_pathlist_head *commitable_paths,
5664 struct got_object_id *head_commit_id,
5665 struct got_object_id *parent_id2,
5666 struct got_worktree *worktree,
5667 const char *author, const char *committer,
5668 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5669 got_worktree_status_cb status_cb, void *status_arg,
5670 struct got_repository *repo)
5672 const struct got_error *err = NULL, *unlockerr = NULL;
5673 struct got_pathlist_entry *pe;
5674 const char *head_ref_name = NULL;
5675 struct got_commit_object *head_commit = NULL;
5676 struct got_reference *head_ref2 = NULL;
5677 struct got_object_id *head_commit_id2 = NULL;
5678 struct got_tree_object *head_tree = NULL;
5679 struct got_object_id *new_tree_id = NULL;
5680 int nentries, nparents = 0;
5681 struct got_object_id_queue parent_ids;
5682 struct got_object_qid *pid = NULL;
5683 char *logmsg = NULL;
5685 *new_commit_id = NULL;
5687 STAILQ_INIT(&parent_ids);
5689 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5690 if (err)
5691 goto done;
5693 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5694 if (err)
5695 goto done;
5697 if (commit_msg_cb != NULL) {
5698 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5699 if (err)
5700 goto done;
5703 if (logmsg == NULL || strlen(logmsg) == 0) {
5704 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5705 goto done;
5708 /* Create blobs from added and modified files and record their IDs. */
5709 TAILQ_FOREACH(pe, commitable_paths, entry) {
5710 struct got_commitable *ct = pe->data;
5711 char *ondisk_path;
5713 /* Blobs for staged files already exist. */
5714 if (ct->staged_status == GOT_STATUS_ADD ||
5715 ct->staged_status == GOT_STATUS_MODIFY)
5716 continue;
5718 if (ct->status != GOT_STATUS_ADD &&
5719 ct->status != GOT_STATUS_MODIFY &&
5720 ct->status != GOT_STATUS_MODE_CHANGE)
5721 continue;
5723 if (asprintf(&ondisk_path, "%s/%s",
5724 worktree->root_path, pe->path) == -1) {
5725 err = got_error_from_errno("asprintf");
5726 goto done;
5728 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5729 free(ondisk_path);
5730 if (err)
5731 goto done;
5734 /* Recursively write new tree objects. */
5735 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5736 commitable_paths, status_cb, status_arg, repo);
5737 if (err)
5738 goto done;
5740 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5741 if (err)
5742 goto done;
5743 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5744 nparents++;
5745 if (parent_id2) {
5746 err = got_object_qid_alloc(&pid, parent_id2);
5747 if (err)
5748 goto done;
5749 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5750 nparents++;
5752 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5753 nparents, author, time(NULL), committer, time(NULL), logmsg, repo);
5754 if (logmsg != NULL)
5755 free(logmsg);
5756 if (err)
5757 goto done;
5759 /* Check if a concurrent commit to our branch has occurred. */
5760 head_ref_name = got_worktree_get_head_ref_name(worktree);
5761 if (head_ref_name == NULL) {
5762 err = got_error_from_errno("got_worktree_get_head_ref_name");
5763 goto done;
5765 /* Lock the reference here to prevent concurrent modification. */
5766 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5767 if (err)
5768 goto done;
5769 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5770 if (err)
5771 goto done;
5772 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5773 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5774 goto done;
5776 /* Update branch head in repository. */
5777 err = got_ref_change_ref(head_ref2, *new_commit_id);
5778 if (err)
5779 goto done;
5780 err = got_ref_write(head_ref2, repo);
5781 if (err)
5782 goto done;
5784 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5785 if (err)
5786 goto done;
5788 err = ref_base_commit(worktree, repo);
5789 if (err)
5790 goto done;
5791 done:
5792 got_object_id_queue_free(&parent_ids);
5793 if (head_tree)
5794 got_object_tree_close(head_tree);
5795 if (head_commit)
5796 got_object_commit_close(head_commit);
5797 free(head_commit_id2);
5798 if (head_ref2) {
5799 unlockerr = got_ref_unlock(head_ref2);
5800 if (unlockerr && err == NULL)
5801 err = unlockerr;
5802 got_ref_close(head_ref2);
5804 return err;
5807 static const struct got_error *
5808 check_path_is_commitable(const char *path,
5809 struct got_pathlist_head *commitable_paths)
5811 struct got_pathlist_entry *cpe = NULL;
5812 size_t path_len = strlen(path);
5814 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5815 struct got_commitable *ct = cpe->data;
5816 const char *ct_path = ct->path;
5818 while (ct_path[0] == '/')
5819 ct_path++;
5821 if (strcmp(path, ct_path) == 0 ||
5822 got_path_is_child(ct_path, path, path_len))
5823 break;
5826 if (cpe == NULL)
5827 return got_error_path(path, GOT_ERR_BAD_PATH);
5829 return NULL;
5832 static const struct got_error *
5833 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5835 int *have_staged_files = arg;
5837 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5838 *have_staged_files = 1;
5839 return got_error(GOT_ERR_CANCELLED);
5842 return NULL;
5845 static const struct got_error *
5846 check_non_staged_files(struct got_fileindex *fileindex,
5847 struct got_pathlist_head *paths)
5849 struct got_pathlist_entry *pe;
5850 struct got_fileindex_entry *ie;
5852 TAILQ_FOREACH(pe, paths, entry) {
5853 if (pe->path[0] == '\0')
5854 continue;
5855 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5856 if (ie == NULL)
5857 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5858 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5859 return got_error_path(pe->path,
5860 GOT_ERR_FILE_NOT_STAGED);
5863 return NULL;
5866 const struct got_error *
5867 got_worktree_commit(struct got_object_id **new_commit_id,
5868 struct got_worktree *worktree, struct got_pathlist_head *paths,
5869 const char *author, const char *committer, int allow_bad_symlinks,
5870 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5871 got_worktree_status_cb status_cb, void *status_arg,
5872 struct got_repository *repo)
5874 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5875 struct got_fileindex *fileindex = NULL;
5876 char *fileindex_path = NULL;
5877 struct got_pathlist_head commitable_paths;
5878 struct collect_commitables_arg cc_arg;
5879 struct got_pathlist_entry *pe;
5880 struct got_reference *head_ref = NULL;
5881 struct got_object_id *head_commit_id = NULL;
5882 int have_staged_files = 0;
5884 *new_commit_id = NULL;
5886 TAILQ_INIT(&commitable_paths);
5888 err = lock_worktree(worktree, LOCK_EX);
5889 if (err)
5890 goto done;
5892 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5893 if (err)
5894 goto done;
5896 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5897 if (err)
5898 goto done;
5900 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5901 if (err)
5902 goto done;
5904 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5905 &have_staged_files);
5906 if (err && err->code != GOT_ERR_CANCELLED)
5907 goto done;
5908 if (have_staged_files) {
5909 err = check_non_staged_files(fileindex, paths);
5910 if (err)
5911 goto done;
5914 cc_arg.commitable_paths = &commitable_paths;
5915 cc_arg.worktree = worktree;
5916 cc_arg.fileindex = fileindex;
5917 cc_arg.repo = repo;
5918 cc_arg.have_staged_files = have_staged_files;
5919 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5920 TAILQ_FOREACH(pe, paths, entry) {
5921 err = worktree_status(worktree, pe->path, fileindex, repo,
5922 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
5923 if (err)
5924 goto done;
5927 if (TAILQ_EMPTY(&commitable_paths)) {
5928 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5929 goto done;
5932 TAILQ_FOREACH(pe, paths, entry) {
5933 err = check_path_is_commitable(pe->path, &commitable_paths);
5934 if (err)
5935 goto done;
5938 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5939 struct got_commitable *ct = pe->data;
5940 const char *ct_path = ct->in_repo_path;
5942 while (ct_path[0] == '/')
5943 ct_path++;
5944 err = check_out_of_date(ct_path, ct->status,
5945 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5946 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5947 if (err)
5948 goto done;
5952 err = commit_worktree(new_commit_id, &commitable_paths,
5953 head_commit_id, NULL, worktree, author, committer,
5954 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5955 if (err)
5956 goto done;
5958 err = update_fileindex_after_commit(worktree, &commitable_paths,
5959 *new_commit_id, fileindex, have_staged_files);
5960 sync_err = sync_fileindex(fileindex, fileindex_path);
5961 if (sync_err && err == NULL)
5962 err = sync_err;
5963 done:
5964 if (fileindex)
5965 got_fileindex_free(fileindex);
5966 free(fileindex_path);
5967 unlockerr = lock_worktree(worktree, LOCK_SH);
5968 if (unlockerr && err == NULL)
5969 err = unlockerr;
5970 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5971 struct got_commitable *ct = pe->data;
5972 free_commitable(ct);
5974 got_pathlist_free(&commitable_paths);
5975 return err;
5978 const char *
5979 got_commitable_get_path(struct got_commitable *ct)
5981 return ct->path;
5984 unsigned int
5985 got_commitable_get_status(struct got_commitable *ct)
5987 return ct->status;
5990 struct check_rebase_ok_arg {
5991 struct got_worktree *worktree;
5992 struct got_repository *repo;
5995 static const struct got_error *
5996 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5998 const struct got_error *err = NULL;
5999 struct check_rebase_ok_arg *a = arg;
6000 unsigned char status;
6001 struct stat sb;
6002 char *ondisk_path;
6004 /* Reject rebase of a work tree with mixed base commits. */
6005 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6006 SHA1_DIGEST_LENGTH))
6007 return got_error(GOT_ERR_MIXED_COMMITS);
6009 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6010 == -1)
6011 return got_error_from_errno("asprintf");
6013 /* Reject rebase of a work tree with modified or staged files. */
6014 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6015 free(ondisk_path);
6016 if (err)
6017 return err;
6019 if (status != GOT_STATUS_NO_CHANGE)
6020 return got_error(GOT_ERR_MODIFIED);
6021 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6022 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6024 return NULL;
6027 const struct got_error *
6028 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6029 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6030 struct got_worktree *worktree, struct got_reference *branch,
6031 struct got_repository *repo)
6033 const struct got_error *err = NULL;
6034 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6035 char *branch_ref_name = NULL;
6036 char *fileindex_path = NULL;
6037 struct check_rebase_ok_arg ok_arg;
6038 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6039 struct got_object_id *wt_branch_tip = NULL;
6041 *new_base_branch_ref = NULL;
6042 *tmp_branch = NULL;
6043 *fileindex = NULL;
6045 err = lock_worktree(worktree, LOCK_EX);
6046 if (err)
6047 return err;
6049 err = open_fileindex(fileindex, &fileindex_path, worktree);
6050 if (err)
6051 goto done;
6053 ok_arg.worktree = worktree;
6054 ok_arg.repo = repo;
6055 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6056 &ok_arg);
6057 if (err)
6058 goto done;
6060 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6061 if (err)
6062 goto done;
6064 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6065 if (err)
6066 goto done;
6068 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6069 if (err)
6070 goto done;
6072 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6073 0);
6074 if (err)
6075 goto done;
6077 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6078 if (err)
6079 goto done;
6080 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6081 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6082 goto done;
6085 err = got_ref_alloc_symref(new_base_branch_ref,
6086 new_base_branch_ref_name, wt_branch);
6087 if (err)
6088 goto done;
6089 err = got_ref_write(*new_base_branch_ref, repo);
6090 if (err)
6091 goto done;
6093 /* TODO Lock original branch's ref while rebasing? */
6095 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6096 if (err)
6097 goto done;
6099 err = got_ref_write(branch_ref, repo);
6100 if (err)
6101 goto done;
6103 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6104 worktree->base_commit_id);
6105 if (err)
6106 goto done;
6107 err = got_ref_write(*tmp_branch, repo);
6108 if (err)
6109 goto done;
6111 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6112 if (err)
6113 goto done;
6114 done:
6115 free(fileindex_path);
6116 free(tmp_branch_name);
6117 free(new_base_branch_ref_name);
6118 free(branch_ref_name);
6119 if (branch_ref)
6120 got_ref_close(branch_ref);
6121 if (wt_branch)
6122 got_ref_close(wt_branch);
6123 free(wt_branch_tip);
6124 if (err) {
6125 if (*new_base_branch_ref) {
6126 got_ref_close(*new_base_branch_ref);
6127 *new_base_branch_ref = NULL;
6129 if (*tmp_branch) {
6130 got_ref_close(*tmp_branch);
6131 *tmp_branch = NULL;
6133 if (*fileindex) {
6134 got_fileindex_free(*fileindex);
6135 *fileindex = NULL;
6137 lock_worktree(worktree, LOCK_SH);
6139 return err;
6142 const struct got_error *
6143 got_worktree_rebase_continue(struct got_object_id **commit_id,
6144 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6145 struct got_reference **branch, struct got_fileindex **fileindex,
6146 struct got_worktree *worktree, struct got_repository *repo)
6148 const struct got_error *err;
6149 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6150 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6151 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6152 char *fileindex_path = NULL;
6153 int have_staged_files = 0;
6155 *commit_id = NULL;
6156 *new_base_branch = NULL;
6157 *tmp_branch = NULL;
6158 *branch = NULL;
6159 *fileindex = NULL;
6161 err = lock_worktree(worktree, LOCK_EX);
6162 if (err)
6163 return err;
6165 err = open_fileindex(fileindex, &fileindex_path, worktree);
6166 if (err)
6167 goto done;
6169 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6170 &have_staged_files);
6171 if (err && err->code != GOT_ERR_CANCELLED)
6172 goto done;
6173 if (have_staged_files) {
6174 err = got_error(GOT_ERR_STAGED_PATHS);
6175 goto done;
6178 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6179 if (err)
6180 goto done;
6182 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6183 if (err)
6184 goto done;
6186 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6187 if (err)
6188 goto done;
6190 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6191 if (err)
6192 goto done;
6194 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6195 if (err)
6196 goto done;
6198 err = got_ref_open(branch, repo,
6199 got_ref_get_symref_target(branch_ref), 0);
6200 if (err)
6201 goto done;
6203 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6204 if (err)
6205 goto done;
6207 err = got_ref_resolve(commit_id, repo, commit_ref);
6208 if (err)
6209 goto done;
6211 err = got_ref_open(new_base_branch, repo,
6212 new_base_branch_ref_name, 0);
6213 if (err)
6214 goto done;
6216 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6217 if (err)
6218 goto done;
6219 done:
6220 free(commit_ref_name);
6221 free(branch_ref_name);
6222 free(fileindex_path);
6223 if (commit_ref)
6224 got_ref_close(commit_ref);
6225 if (branch_ref)
6226 got_ref_close(branch_ref);
6227 if (err) {
6228 free(*commit_id);
6229 *commit_id = NULL;
6230 if (*tmp_branch) {
6231 got_ref_close(*tmp_branch);
6232 *tmp_branch = NULL;
6234 if (*new_base_branch) {
6235 got_ref_close(*new_base_branch);
6236 *new_base_branch = NULL;
6238 if (*branch) {
6239 got_ref_close(*branch);
6240 *branch = NULL;
6242 if (*fileindex) {
6243 got_fileindex_free(*fileindex);
6244 *fileindex = NULL;
6246 lock_worktree(worktree, LOCK_SH);
6248 return err;
6251 const struct got_error *
6252 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6254 const struct got_error *err;
6255 char *tmp_branch_name = NULL;
6257 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6258 if (err)
6259 return err;
6261 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6262 free(tmp_branch_name);
6263 return NULL;
6266 static const struct got_error *
6267 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6268 char **logmsg, void *arg)
6270 *logmsg = arg;
6271 return NULL;
6274 static const struct got_error *
6275 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6276 const char *path, struct got_object_id *blob_id,
6277 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6278 int dirfd, const char *de_name)
6280 return NULL;
6283 struct collect_merged_paths_arg {
6284 got_worktree_checkout_cb progress_cb;
6285 void *progress_arg;
6286 struct got_pathlist_head *merged_paths;
6289 static const struct got_error *
6290 collect_merged_paths(void *arg, unsigned char status, const char *path)
6292 const struct got_error *err;
6293 struct collect_merged_paths_arg *a = arg;
6294 char *p;
6295 struct got_pathlist_entry *new;
6297 err = (*a->progress_cb)(a->progress_arg, status, path);
6298 if (err)
6299 return err;
6301 if (status != GOT_STATUS_MERGE &&
6302 status != GOT_STATUS_ADD &&
6303 status != GOT_STATUS_DELETE &&
6304 status != GOT_STATUS_CONFLICT)
6305 return NULL;
6307 p = strdup(path);
6308 if (p == NULL)
6309 return got_error_from_errno("strdup");
6311 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6312 if (err || new == NULL)
6313 free(p);
6314 return err;
6317 void
6318 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6320 struct got_pathlist_entry *pe;
6322 TAILQ_FOREACH(pe, merged_paths, entry)
6323 free((char *)pe->path);
6325 got_pathlist_free(merged_paths);
6328 static const struct got_error *
6329 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6330 int is_rebase, struct got_repository *repo)
6332 const struct got_error *err;
6333 struct got_reference *commit_ref = NULL;
6335 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6336 if (err) {
6337 if (err->code != GOT_ERR_NOT_REF)
6338 goto done;
6339 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6340 if (err)
6341 goto done;
6342 err = got_ref_write(commit_ref, repo);
6343 if (err)
6344 goto done;
6345 } else if (is_rebase) {
6346 struct got_object_id *stored_id;
6347 int cmp;
6349 err = got_ref_resolve(&stored_id, repo, commit_ref);
6350 if (err)
6351 goto done;
6352 cmp = got_object_id_cmp(commit_id, stored_id);
6353 free(stored_id);
6354 if (cmp != 0) {
6355 err = got_error(GOT_ERR_REBASE_COMMITID);
6356 goto done;
6359 done:
6360 if (commit_ref)
6361 got_ref_close(commit_ref);
6362 return err;
6365 static const struct got_error *
6366 rebase_merge_files(struct got_pathlist_head *merged_paths,
6367 const char *commit_ref_name, struct got_worktree *worktree,
6368 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6369 struct got_object_id *commit_id, struct got_repository *repo,
6370 got_worktree_checkout_cb progress_cb, void *progress_arg,
6371 got_cancel_cb cancel_cb, void *cancel_arg)
6373 const struct got_error *err;
6374 struct got_reference *commit_ref = NULL;
6375 struct collect_merged_paths_arg cmp_arg;
6376 char *fileindex_path;
6378 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6380 err = get_fileindex_path(&fileindex_path, worktree);
6381 if (err)
6382 return err;
6384 cmp_arg.progress_cb = progress_cb;
6385 cmp_arg.progress_arg = progress_arg;
6386 cmp_arg.merged_paths = merged_paths;
6387 err = merge_files(worktree, fileindex, fileindex_path,
6388 parent_commit_id, commit_id, repo, collect_merged_paths,
6389 &cmp_arg, cancel_cb, cancel_arg);
6390 if (commit_ref)
6391 got_ref_close(commit_ref);
6392 return err;
6395 const struct got_error *
6396 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6397 struct got_worktree *worktree, struct got_fileindex *fileindex,
6398 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6399 struct got_repository *repo,
6400 got_worktree_checkout_cb progress_cb, void *progress_arg,
6401 got_cancel_cb cancel_cb, void *cancel_arg)
6403 const struct got_error *err;
6404 char *commit_ref_name;
6406 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6407 if (err)
6408 return err;
6410 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6411 if (err)
6412 goto done;
6414 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6415 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6416 progress_arg, cancel_cb, cancel_arg);
6417 done:
6418 free(commit_ref_name);
6419 return err;
6422 const struct got_error *
6423 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6424 struct got_worktree *worktree, struct got_fileindex *fileindex,
6425 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6426 struct got_repository *repo,
6427 got_worktree_checkout_cb progress_cb, void *progress_arg,
6428 got_cancel_cb cancel_cb, void *cancel_arg)
6430 const struct got_error *err;
6431 char *commit_ref_name;
6433 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6434 if (err)
6435 return err;
6437 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6438 if (err)
6439 goto done;
6441 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6442 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6443 progress_arg, cancel_cb, cancel_arg);
6444 done:
6445 free(commit_ref_name);
6446 return err;
6449 static const struct got_error *
6450 rebase_commit(struct got_object_id **new_commit_id,
6451 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6452 struct got_worktree *worktree, struct got_fileindex *fileindex,
6453 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6454 const char *new_logmsg, struct got_repository *repo)
6456 const struct got_error *err, *sync_err;
6457 struct got_pathlist_head commitable_paths;
6458 struct collect_commitables_arg cc_arg;
6459 char *fileindex_path = NULL;
6460 struct got_reference *head_ref = NULL;
6461 struct got_object_id *head_commit_id = NULL;
6462 char *logmsg = NULL;
6464 TAILQ_INIT(&commitable_paths);
6465 *new_commit_id = NULL;
6467 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6469 err = get_fileindex_path(&fileindex_path, worktree);
6470 if (err)
6471 return err;
6473 cc_arg.commitable_paths = &commitable_paths;
6474 cc_arg.worktree = worktree;
6475 cc_arg.repo = repo;
6476 cc_arg.have_staged_files = 0;
6478 * If possible get the status of individual files directly to
6479 * avoid crawling the entire work tree once per rebased commit.
6481 * Ideally, merged_paths would contain a list of commitables
6482 * we could use so we could skip worktree_status() entirely.
6483 * However, we would then need carefully keep track of cumulative
6484 * effects of operations such as file additions and deletions
6485 * in 'got histedit -f' (folding multiple commits into one),
6486 * and this extra complexity is not really worth it.
6488 if (merged_paths) {
6489 struct got_pathlist_entry *pe;
6490 TAILQ_FOREACH(pe, merged_paths, entry) {
6491 err = worktree_status(worktree, pe->path, fileindex,
6492 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6493 0);
6494 if (err)
6495 goto done;
6497 } else {
6498 err = worktree_status(worktree, "", fileindex, repo,
6499 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6500 if (err)
6501 goto done;
6504 if (TAILQ_EMPTY(&commitable_paths)) {
6505 /* No-op change; commit will be elided. */
6506 err = got_ref_delete(commit_ref, repo);
6507 if (err)
6508 goto done;
6509 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6510 goto done;
6513 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6514 if (err)
6515 goto done;
6517 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6518 if (err)
6519 goto done;
6521 if (new_logmsg) {
6522 logmsg = strdup(new_logmsg);
6523 if (logmsg == NULL) {
6524 err = got_error_from_errno("strdup");
6525 goto done;
6527 } else {
6528 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6529 if (err)
6530 goto done;
6533 /* NB: commit_worktree will call free(logmsg) */
6534 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6535 NULL, worktree, got_object_commit_get_author(orig_commit),
6536 got_object_commit_get_committer(orig_commit),
6537 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6538 if (err)
6539 goto done;
6541 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6542 if (err)
6543 goto done;
6545 err = got_ref_delete(commit_ref, repo);
6546 if (err)
6547 goto done;
6549 err = update_fileindex_after_commit(worktree, &commitable_paths,
6550 *new_commit_id, fileindex, 0);
6551 sync_err = sync_fileindex(fileindex, fileindex_path);
6552 if (sync_err && err == NULL)
6553 err = sync_err;
6554 done:
6555 free(fileindex_path);
6556 free(head_commit_id);
6557 if (head_ref)
6558 got_ref_close(head_ref);
6559 if (err) {
6560 free(*new_commit_id);
6561 *new_commit_id = NULL;
6563 return err;
6566 const struct got_error *
6567 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6568 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6569 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6570 struct got_commit_object *orig_commit,
6571 struct got_object_id *orig_commit_id, struct got_repository *repo)
6573 const struct got_error *err;
6574 char *commit_ref_name;
6575 struct got_reference *commit_ref = NULL;
6576 struct got_object_id *commit_id = NULL;
6578 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6579 if (err)
6580 return err;
6582 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6583 if (err)
6584 goto done;
6585 err = got_ref_resolve(&commit_id, repo, commit_ref);
6586 if (err)
6587 goto done;
6588 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6589 err = got_error(GOT_ERR_REBASE_COMMITID);
6590 goto done;
6593 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6594 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6595 done:
6596 if (commit_ref)
6597 got_ref_close(commit_ref);
6598 free(commit_ref_name);
6599 free(commit_id);
6600 return err;
6603 const struct got_error *
6604 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6605 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6606 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6607 struct got_commit_object *orig_commit,
6608 struct got_object_id *orig_commit_id, const char *new_logmsg,
6609 struct got_repository *repo)
6611 const struct got_error *err;
6612 char *commit_ref_name;
6613 struct got_reference *commit_ref = NULL;
6615 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6616 if (err)
6617 return err;
6619 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6620 if (err)
6621 goto done;
6623 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6624 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6625 done:
6626 if (commit_ref)
6627 got_ref_close(commit_ref);
6628 free(commit_ref_name);
6629 return err;
6632 const struct got_error *
6633 got_worktree_rebase_postpone(struct got_worktree *worktree,
6634 struct got_fileindex *fileindex)
6636 if (fileindex)
6637 got_fileindex_free(fileindex);
6638 return lock_worktree(worktree, LOCK_SH);
6641 static const struct got_error *
6642 delete_ref(const char *name, struct got_repository *repo)
6644 const struct got_error *err;
6645 struct got_reference *ref;
6647 err = got_ref_open(&ref, repo, name, 0);
6648 if (err) {
6649 if (err->code == GOT_ERR_NOT_REF)
6650 return NULL;
6651 return err;
6654 err = got_ref_delete(ref, repo);
6655 got_ref_close(ref);
6656 return err;
6659 static const struct got_error *
6660 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6662 const struct got_error *err;
6663 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6664 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6666 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6667 if (err)
6668 goto done;
6669 err = delete_ref(tmp_branch_name, repo);
6670 if (err)
6671 goto done;
6673 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6674 if (err)
6675 goto done;
6676 err = delete_ref(new_base_branch_ref_name, repo);
6677 if (err)
6678 goto done;
6680 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6681 if (err)
6682 goto done;
6683 err = delete_ref(branch_ref_name, repo);
6684 if (err)
6685 goto done;
6687 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6688 if (err)
6689 goto done;
6690 err = delete_ref(commit_ref_name, repo);
6691 if (err)
6692 goto done;
6694 done:
6695 free(tmp_branch_name);
6696 free(new_base_branch_ref_name);
6697 free(branch_ref_name);
6698 free(commit_ref_name);
6699 return err;
6702 static const struct got_error *
6703 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6704 struct got_object_id *new_commit_id, struct got_repository *repo)
6706 const struct got_error *err;
6707 struct got_reference *ref = NULL;
6708 struct got_object_id *old_commit_id = NULL;
6709 const char *branch_name = NULL;
6710 char *new_id_str = NULL;
6711 char *refname = NULL;
6713 branch_name = got_ref_get_name(branch);
6714 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6715 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6716 branch_name += 11;
6718 err = got_object_id_str(&new_id_str, new_commit_id);
6719 if (err)
6720 return err;
6722 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6723 new_id_str) == -1) {
6724 err = got_error_from_errno("asprintf");
6725 goto done;
6728 err = got_ref_resolve(&old_commit_id, repo, branch);
6729 if (err)
6730 goto done;
6732 err = got_ref_alloc(&ref, refname, old_commit_id);
6733 if (err)
6734 goto done;
6736 err = got_ref_write(ref, repo);
6737 done:
6738 free(new_id_str);
6739 free(refname);
6740 free(old_commit_id);
6741 if (ref)
6742 got_ref_close(ref);
6743 return err;
6746 const struct got_error *
6747 got_worktree_rebase_complete(struct got_worktree *worktree,
6748 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6749 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6750 struct got_repository *repo, int create_backup)
6752 const struct got_error *err, *unlockerr, *sync_err;
6753 struct got_object_id *new_head_commit_id = NULL;
6754 char *fileindex_path = NULL;
6756 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6757 if (err)
6758 return err;
6760 if (create_backup) {
6761 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6762 rebased_branch, new_head_commit_id, repo);
6763 if (err)
6764 goto done;
6767 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6768 if (err)
6769 goto done;
6771 err = got_ref_write(rebased_branch, repo);
6772 if (err)
6773 goto done;
6775 err = got_worktree_set_head_ref(worktree, rebased_branch);
6776 if (err)
6777 goto done;
6779 err = delete_rebase_refs(worktree, repo);
6780 if (err)
6781 goto done;
6783 err = get_fileindex_path(&fileindex_path, worktree);
6784 if (err)
6785 goto done;
6786 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6787 sync_err = sync_fileindex(fileindex, fileindex_path);
6788 if (sync_err && err == NULL)
6789 err = sync_err;
6790 done:
6791 got_fileindex_free(fileindex);
6792 free(fileindex_path);
6793 free(new_head_commit_id);
6794 unlockerr = lock_worktree(worktree, LOCK_SH);
6795 if (unlockerr && err == NULL)
6796 err = unlockerr;
6797 return err;
6800 const struct got_error *
6801 got_worktree_rebase_abort(struct got_worktree *worktree,
6802 struct got_fileindex *fileindex, struct got_repository *repo,
6803 struct got_reference *new_base_branch,
6804 got_worktree_checkout_cb progress_cb, void *progress_arg)
6806 const struct got_error *err, *unlockerr, *sync_err;
6807 struct got_reference *resolved = NULL;
6808 struct got_object_id *commit_id = NULL;
6809 struct got_commit_object *commit = NULL;
6810 char *fileindex_path = NULL;
6811 struct revert_file_args rfa;
6812 struct got_object_id *tree_id = NULL;
6814 err = lock_worktree(worktree, LOCK_EX);
6815 if (err)
6816 return err;
6818 err = got_object_open_as_commit(&commit, repo,
6819 worktree->base_commit_id);
6820 if (err)
6821 goto done;
6823 err = got_ref_open(&resolved, repo,
6824 got_ref_get_symref_target(new_base_branch), 0);
6825 if (err)
6826 goto done;
6828 err = got_worktree_set_head_ref(worktree, resolved);
6829 if (err)
6830 goto done;
6833 * XXX commits to the base branch could have happened while
6834 * we were busy rebasing; should we store the original commit ID
6835 * when rebase begins and read it back here?
6837 err = got_ref_resolve(&commit_id, repo, resolved);
6838 if (err)
6839 goto done;
6841 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6842 if (err)
6843 goto done;
6845 err = got_object_id_by_path(&tree_id, repo, commit,
6846 worktree->path_prefix);
6847 if (err)
6848 goto done;
6850 err = delete_rebase_refs(worktree, repo);
6851 if (err)
6852 goto done;
6854 err = get_fileindex_path(&fileindex_path, worktree);
6855 if (err)
6856 goto done;
6858 rfa.worktree = worktree;
6859 rfa.fileindex = fileindex;
6860 rfa.progress_cb = progress_cb;
6861 rfa.progress_arg = progress_arg;
6862 rfa.patch_cb = NULL;
6863 rfa.patch_arg = NULL;
6864 rfa.repo = repo;
6865 rfa.unlink_added_files = 0;
6866 err = worktree_status(worktree, "", fileindex, repo,
6867 revert_file, &rfa, NULL, NULL, 1, 0);
6868 if (err)
6869 goto sync;
6871 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6872 repo, progress_cb, progress_arg, NULL, NULL);
6873 sync:
6874 sync_err = sync_fileindex(fileindex, fileindex_path);
6875 if (sync_err && err == NULL)
6876 err = sync_err;
6877 done:
6878 got_ref_close(resolved);
6879 free(tree_id);
6880 free(commit_id);
6881 if (commit)
6882 got_object_commit_close(commit);
6883 if (fileindex)
6884 got_fileindex_free(fileindex);
6885 free(fileindex_path);
6887 unlockerr = lock_worktree(worktree, LOCK_SH);
6888 if (unlockerr && err == NULL)
6889 err = unlockerr;
6890 return err;
6893 const struct got_error *
6894 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6895 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6896 struct got_fileindex **fileindex, struct got_worktree *worktree,
6897 struct got_repository *repo)
6899 const struct got_error *err = NULL;
6900 char *tmp_branch_name = NULL;
6901 char *branch_ref_name = NULL;
6902 char *base_commit_ref_name = NULL;
6903 char *fileindex_path = NULL;
6904 struct check_rebase_ok_arg ok_arg;
6905 struct got_reference *wt_branch = NULL;
6906 struct got_reference *base_commit_ref = NULL;
6908 *tmp_branch = NULL;
6909 *branch_ref = NULL;
6910 *base_commit_id = NULL;
6911 *fileindex = NULL;
6913 err = lock_worktree(worktree, LOCK_EX);
6914 if (err)
6915 return err;
6917 err = open_fileindex(fileindex, &fileindex_path, worktree);
6918 if (err)
6919 goto done;
6921 ok_arg.worktree = worktree;
6922 ok_arg.repo = repo;
6923 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6924 &ok_arg);
6925 if (err)
6926 goto done;
6928 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6929 if (err)
6930 goto done;
6932 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6933 if (err)
6934 goto done;
6936 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6937 worktree);
6938 if (err)
6939 goto done;
6941 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6942 0);
6943 if (err)
6944 goto done;
6946 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6947 if (err)
6948 goto done;
6950 err = got_ref_write(*branch_ref, repo);
6951 if (err)
6952 goto done;
6954 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6955 worktree->base_commit_id);
6956 if (err)
6957 goto done;
6958 err = got_ref_write(base_commit_ref, repo);
6959 if (err)
6960 goto done;
6961 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6962 if (*base_commit_id == NULL) {
6963 err = got_error_from_errno("got_object_id_dup");
6964 goto done;
6967 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6968 worktree->base_commit_id);
6969 if (err)
6970 goto done;
6971 err = got_ref_write(*tmp_branch, repo);
6972 if (err)
6973 goto done;
6975 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6976 if (err)
6977 goto done;
6978 done:
6979 free(fileindex_path);
6980 free(tmp_branch_name);
6981 free(branch_ref_name);
6982 free(base_commit_ref_name);
6983 if (wt_branch)
6984 got_ref_close(wt_branch);
6985 if (err) {
6986 if (*branch_ref) {
6987 got_ref_close(*branch_ref);
6988 *branch_ref = NULL;
6990 if (*tmp_branch) {
6991 got_ref_close(*tmp_branch);
6992 *tmp_branch = NULL;
6994 free(*base_commit_id);
6995 if (*fileindex) {
6996 got_fileindex_free(*fileindex);
6997 *fileindex = NULL;
6999 lock_worktree(worktree, LOCK_SH);
7001 return err;
7004 const struct got_error *
7005 got_worktree_histedit_postpone(struct got_worktree *worktree,
7006 struct got_fileindex *fileindex)
7008 if (fileindex)
7009 got_fileindex_free(fileindex);
7010 return lock_worktree(worktree, LOCK_SH);
7013 const struct got_error *
7014 got_worktree_histedit_in_progress(int *in_progress,
7015 struct got_worktree *worktree)
7017 const struct got_error *err;
7018 char *tmp_branch_name = NULL;
7020 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7021 if (err)
7022 return err;
7024 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7025 free(tmp_branch_name);
7026 return NULL;
7029 const struct got_error *
7030 got_worktree_histedit_continue(struct got_object_id **commit_id,
7031 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7032 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7033 struct got_worktree *worktree, struct got_repository *repo)
7035 const struct got_error *err;
7036 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7037 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7038 struct got_reference *commit_ref = NULL;
7039 struct got_reference *base_commit_ref = NULL;
7040 char *fileindex_path = NULL;
7041 int have_staged_files = 0;
7043 *commit_id = NULL;
7044 *tmp_branch = NULL;
7045 *base_commit_id = NULL;
7046 *fileindex = NULL;
7048 err = lock_worktree(worktree, LOCK_EX);
7049 if (err)
7050 return err;
7052 err = open_fileindex(fileindex, &fileindex_path, worktree);
7053 if (err)
7054 goto done;
7056 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7057 &have_staged_files);
7058 if (err && err->code != GOT_ERR_CANCELLED)
7059 goto done;
7060 if (have_staged_files) {
7061 err = got_error(GOT_ERR_STAGED_PATHS);
7062 goto done;
7065 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7066 if (err)
7067 goto done;
7069 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7070 if (err)
7071 goto done;
7073 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7074 if (err)
7075 goto done;
7077 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7078 worktree);
7079 if (err)
7080 goto done;
7082 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7083 if (err)
7084 goto done;
7086 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7087 if (err)
7088 goto done;
7089 err = got_ref_resolve(commit_id, repo, commit_ref);
7090 if (err)
7091 goto done;
7093 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7094 if (err)
7095 goto done;
7096 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7097 if (err)
7098 goto done;
7100 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7101 if (err)
7102 goto done;
7103 done:
7104 free(commit_ref_name);
7105 free(branch_ref_name);
7106 free(fileindex_path);
7107 if (commit_ref)
7108 got_ref_close(commit_ref);
7109 if (base_commit_ref)
7110 got_ref_close(base_commit_ref);
7111 if (err) {
7112 free(*commit_id);
7113 *commit_id = NULL;
7114 free(*base_commit_id);
7115 *base_commit_id = NULL;
7116 if (*tmp_branch) {
7117 got_ref_close(*tmp_branch);
7118 *tmp_branch = NULL;
7120 if (*fileindex) {
7121 got_fileindex_free(*fileindex);
7122 *fileindex = NULL;
7124 lock_worktree(worktree, LOCK_EX);
7126 return err;
7129 static const struct got_error *
7130 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7132 const struct got_error *err;
7133 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7134 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7136 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7137 if (err)
7138 goto done;
7139 err = delete_ref(tmp_branch_name, repo);
7140 if (err)
7141 goto done;
7143 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7144 worktree);
7145 if (err)
7146 goto done;
7147 err = delete_ref(base_commit_ref_name, repo);
7148 if (err)
7149 goto done;
7151 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7152 if (err)
7153 goto done;
7154 err = delete_ref(branch_ref_name, repo);
7155 if (err)
7156 goto done;
7158 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7159 if (err)
7160 goto done;
7161 err = delete_ref(commit_ref_name, repo);
7162 if (err)
7163 goto done;
7164 done:
7165 free(tmp_branch_name);
7166 free(base_commit_ref_name);
7167 free(branch_ref_name);
7168 free(commit_ref_name);
7169 return err;
7172 const struct got_error *
7173 got_worktree_histedit_abort(struct got_worktree *worktree,
7174 struct got_fileindex *fileindex, struct got_repository *repo,
7175 struct got_reference *branch, struct got_object_id *base_commit_id,
7176 got_worktree_checkout_cb progress_cb, void *progress_arg)
7178 const struct got_error *err, *unlockerr, *sync_err;
7179 struct got_reference *resolved = NULL;
7180 char *fileindex_path = NULL;
7181 struct got_commit_object *commit = NULL;
7182 struct got_object_id *tree_id = NULL;
7183 struct revert_file_args rfa;
7185 err = lock_worktree(worktree, LOCK_EX);
7186 if (err)
7187 return err;
7189 err = got_object_open_as_commit(&commit, repo,
7190 worktree->base_commit_id);
7191 if (err)
7192 goto done;
7194 err = got_ref_open(&resolved, repo,
7195 got_ref_get_symref_target(branch), 0);
7196 if (err)
7197 goto done;
7199 err = got_worktree_set_head_ref(worktree, resolved);
7200 if (err)
7201 goto done;
7203 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7204 if (err)
7205 goto done;
7207 err = got_object_id_by_path(&tree_id, repo, commit,
7208 worktree->path_prefix);
7209 if (err)
7210 goto done;
7212 err = delete_histedit_refs(worktree, repo);
7213 if (err)
7214 goto done;
7216 err = get_fileindex_path(&fileindex_path, worktree);
7217 if (err)
7218 goto done;
7220 rfa.worktree = worktree;
7221 rfa.fileindex = fileindex;
7222 rfa.progress_cb = progress_cb;
7223 rfa.progress_arg = progress_arg;
7224 rfa.patch_cb = NULL;
7225 rfa.patch_arg = NULL;
7226 rfa.repo = repo;
7227 rfa.unlink_added_files = 0;
7228 err = worktree_status(worktree, "", fileindex, repo,
7229 revert_file, &rfa, NULL, NULL, 1, 0);
7230 if (err)
7231 goto sync;
7233 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7234 repo, progress_cb, progress_arg, NULL, NULL);
7235 sync:
7236 sync_err = sync_fileindex(fileindex, fileindex_path);
7237 if (sync_err && err == NULL)
7238 err = sync_err;
7239 done:
7240 got_ref_close(resolved);
7241 free(tree_id);
7242 free(fileindex_path);
7244 unlockerr = lock_worktree(worktree, LOCK_SH);
7245 if (unlockerr && err == NULL)
7246 err = unlockerr;
7247 return err;
7250 const struct got_error *
7251 got_worktree_histedit_complete(struct got_worktree *worktree,
7252 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7253 struct got_reference *edited_branch, struct got_repository *repo)
7255 const struct got_error *err, *unlockerr, *sync_err;
7256 struct got_object_id *new_head_commit_id = NULL;
7257 struct got_reference *resolved = NULL;
7258 char *fileindex_path = NULL;
7260 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7261 if (err)
7262 return err;
7264 err = got_ref_open(&resolved, repo,
7265 got_ref_get_symref_target(edited_branch), 0);
7266 if (err)
7267 goto done;
7269 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7270 resolved, new_head_commit_id, repo);
7271 if (err)
7272 goto done;
7274 err = got_ref_change_ref(resolved, new_head_commit_id);
7275 if (err)
7276 goto done;
7278 err = got_ref_write(resolved, repo);
7279 if (err)
7280 goto done;
7282 err = got_worktree_set_head_ref(worktree, resolved);
7283 if (err)
7284 goto done;
7286 err = delete_histedit_refs(worktree, repo);
7287 if (err)
7288 goto done;
7290 err = get_fileindex_path(&fileindex_path, worktree);
7291 if (err)
7292 goto done;
7293 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7294 sync_err = sync_fileindex(fileindex, fileindex_path);
7295 if (sync_err && err == NULL)
7296 err = sync_err;
7297 done:
7298 got_fileindex_free(fileindex);
7299 free(fileindex_path);
7300 free(new_head_commit_id);
7301 unlockerr = lock_worktree(worktree, LOCK_SH);
7302 if (unlockerr && err == NULL)
7303 err = unlockerr;
7304 return err;
7307 const struct got_error *
7308 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7309 struct got_object_id *commit_id, struct got_repository *repo)
7311 const struct got_error *err;
7312 char *commit_ref_name;
7314 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7315 if (err)
7316 return err;
7318 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7319 if (err)
7320 goto done;
7322 err = delete_ref(commit_ref_name, repo);
7323 done:
7324 free(commit_ref_name);
7325 return err;
7328 const struct got_error *
7329 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7330 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7331 struct got_worktree *worktree, const char *refname,
7332 struct got_repository *repo)
7334 const struct got_error *err = NULL;
7335 char *fileindex_path = NULL;
7336 struct check_rebase_ok_arg ok_arg;
7338 *fileindex = NULL;
7339 *branch_ref = NULL;
7340 *base_branch_ref = NULL;
7342 err = lock_worktree(worktree, LOCK_EX);
7343 if (err)
7344 return err;
7346 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7347 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7348 "cannot integrate a branch into itself; "
7349 "update -b or different branch name required");
7350 goto done;
7353 err = open_fileindex(fileindex, &fileindex_path, worktree);
7354 if (err)
7355 goto done;
7357 /* Preconditions are the same as for rebase. */
7358 ok_arg.worktree = worktree;
7359 ok_arg.repo = repo;
7360 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7361 &ok_arg);
7362 if (err)
7363 goto done;
7365 err = got_ref_open(branch_ref, repo, refname, 1);
7366 if (err)
7367 goto done;
7369 err = got_ref_open(base_branch_ref, repo,
7370 got_worktree_get_head_ref_name(worktree), 1);
7371 done:
7372 if (err) {
7373 if (*branch_ref) {
7374 got_ref_close(*branch_ref);
7375 *branch_ref = NULL;
7377 if (*base_branch_ref) {
7378 got_ref_close(*base_branch_ref);
7379 *base_branch_ref = NULL;
7381 if (*fileindex) {
7382 got_fileindex_free(*fileindex);
7383 *fileindex = NULL;
7385 lock_worktree(worktree, LOCK_SH);
7387 return err;
7390 const struct got_error *
7391 got_worktree_integrate_continue(struct got_worktree *worktree,
7392 struct got_fileindex *fileindex, struct got_repository *repo,
7393 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7394 got_worktree_checkout_cb progress_cb, void *progress_arg,
7395 got_cancel_cb cancel_cb, void *cancel_arg)
7397 const struct got_error *err = NULL, *sync_err, *unlockerr;
7398 char *fileindex_path = NULL;
7399 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7400 struct got_commit_object *commit = NULL;
7402 err = get_fileindex_path(&fileindex_path, worktree);
7403 if (err)
7404 goto done;
7406 err = got_ref_resolve(&commit_id, repo, branch_ref);
7407 if (err)
7408 goto done;
7410 err = got_object_open_as_commit(&commit, repo, commit_id);
7411 if (err)
7412 goto done;
7414 err = got_object_id_by_path(&tree_id, repo, commit,
7415 worktree->path_prefix);
7416 if (err)
7417 goto done;
7419 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7420 if (err)
7421 goto done;
7423 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7424 progress_cb, progress_arg, cancel_cb, cancel_arg);
7425 if (err)
7426 goto sync;
7428 err = got_ref_change_ref(base_branch_ref, commit_id);
7429 if (err)
7430 goto sync;
7432 err = got_ref_write(base_branch_ref, repo);
7433 if (err)
7434 goto sync;
7436 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7437 sync:
7438 sync_err = sync_fileindex(fileindex, fileindex_path);
7439 if (sync_err && err == NULL)
7440 err = sync_err;
7442 done:
7443 unlockerr = got_ref_unlock(branch_ref);
7444 if (unlockerr && err == NULL)
7445 err = unlockerr;
7446 got_ref_close(branch_ref);
7448 unlockerr = got_ref_unlock(base_branch_ref);
7449 if (unlockerr && err == NULL)
7450 err = unlockerr;
7451 got_ref_close(base_branch_ref);
7453 got_fileindex_free(fileindex);
7454 free(fileindex_path);
7455 free(tree_id);
7456 if (commit)
7457 got_object_commit_close(commit);
7459 unlockerr = lock_worktree(worktree, LOCK_SH);
7460 if (unlockerr && err == NULL)
7461 err = unlockerr;
7462 return err;
7465 const struct got_error *
7466 got_worktree_integrate_abort(struct got_worktree *worktree,
7467 struct got_fileindex *fileindex, struct got_repository *repo,
7468 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7470 const struct got_error *err = NULL, *unlockerr = NULL;
7472 got_fileindex_free(fileindex);
7474 err = lock_worktree(worktree, LOCK_SH);
7476 unlockerr = got_ref_unlock(branch_ref);
7477 if (unlockerr && err == NULL)
7478 err = unlockerr;
7479 got_ref_close(branch_ref);
7481 unlockerr = got_ref_unlock(base_branch_ref);
7482 if (unlockerr && err == NULL)
7483 err = unlockerr;
7484 got_ref_close(base_branch_ref);
7486 return err;
7489 const struct got_error *
7490 got_worktree_merge_postpone(struct got_worktree *worktree,
7491 struct got_fileindex *fileindex)
7493 const struct got_error *err, *sync_err;
7494 char *fileindex_path = NULL;
7496 err = get_fileindex_path(&fileindex_path, worktree);
7497 if (err)
7498 goto done;
7500 sync_err = sync_fileindex(fileindex, fileindex_path);
7502 err = lock_worktree(worktree, LOCK_SH);
7503 if (sync_err && err == NULL)
7504 err = sync_err;
7505 done:
7506 got_fileindex_free(fileindex);
7507 free(fileindex_path);
7508 return err;
7511 static const struct got_error *
7512 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7514 const struct got_error *err;
7515 char *branch_refname = NULL, *commit_refname = NULL;
7517 err = get_merge_branch_ref_name(&branch_refname, worktree);
7518 if (err)
7519 goto done;
7520 err = delete_ref(branch_refname, repo);
7521 if (err)
7522 goto done;
7524 err = get_merge_commit_ref_name(&commit_refname, worktree);
7525 if (err)
7526 goto done;
7527 err = delete_ref(commit_refname, repo);
7528 if (err)
7529 goto done;
7531 done:
7532 free(branch_refname);
7533 free(commit_refname);
7534 return err;
7537 struct merge_commit_msg_arg {
7538 struct got_worktree *worktree;
7539 const char *branch_name;
7542 static const struct got_error *
7543 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths, char **logmsg,
7544 void *arg)
7546 struct merge_commit_msg_arg *a = arg;
7548 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7549 got_worktree_get_head_ref_name(a->worktree)) == -1)
7550 return got_error_from_errno("asprintf");
7552 return NULL;
7556 const struct got_error *
7557 got_worktree_merge_branch(struct got_worktree *worktree,
7558 struct got_fileindex *fileindex,
7559 struct got_object_id *yca_commit_id,
7560 struct got_object_id *branch_tip,
7561 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7562 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7564 const struct got_error *err;
7565 char *fileindex_path = NULL;
7567 err = get_fileindex_path(&fileindex_path, worktree);
7568 if (err)
7569 goto done;
7571 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7572 worktree);
7573 if (err)
7574 goto done;
7576 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7577 branch_tip, repo, progress_cb, progress_arg,
7578 cancel_cb, cancel_arg);
7579 done:
7580 free(fileindex_path);
7581 return err;
7584 const struct got_error *
7585 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7586 struct got_worktree *worktree, struct got_fileindex *fileindex,
7587 const char *author, const char *committer, int allow_bad_symlinks,
7588 struct got_object_id *branch_tip, const char *branch_name,
7589 struct got_repository *repo,
7590 got_worktree_status_cb status_cb, void *status_arg)
7593 const struct got_error *err = NULL, *sync_err;
7594 struct got_pathlist_head commitable_paths;
7595 struct collect_commitables_arg cc_arg;
7596 struct got_pathlist_entry *pe;
7597 struct got_reference *head_ref = NULL;
7598 struct got_object_id *head_commit_id = NULL;
7599 int have_staged_files = 0;
7600 struct merge_commit_msg_arg mcm_arg;
7601 char *fileindex_path = NULL;
7603 *new_commit_id = NULL;
7605 TAILQ_INIT(&commitable_paths);
7607 err = get_fileindex_path(&fileindex_path, worktree);
7608 if (err)
7609 goto done;
7611 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7612 if (err)
7613 goto done;
7615 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7616 if (err)
7617 goto done;
7619 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7620 &have_staged_files);
7621 if (err && err->code != GOT_ERR_CANCELLED)
7622 goto done;
7623 if (have_staged_files) {
7624 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7625 goto done;
7628 cc_arg.commitable_paths = &commitable_paths;
7629 cc_arg.worktree = worktree;
7630 cc_arg.fileindex = fileindex;
7631 cc_arg.repo = repo;
7632 cc_arg.have_staged_files = have_staged_files;
7633 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7634 err = worktree_status(worktree, "", fileindex, repo,
7635 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7636 if (err)
7637 goto done;
7639 if (TAILQ_EMPTY(&commitable_paths)) {
7640 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7641 "merge of %s cannot proceed", branch_name);
7642 goto done;
7645 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7646 struct got_commitable *ct = pe->data;
7647 const char *ct_path = ct->in_repo_path;
7649 while (ct_path[0] == '/')
7650 ct_path++;
7651 err = check_out_of_date(ct_path, ct->status,
7652 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7653 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7654 if (err)
7655 goto done;
7659 mcm_arg.worktree = worktree;
7660 mcm_arg.branch_name = branch_name;
7661 err = commit_worktree(new_commit_id, &commitable_paths,
7662 head_commit_id, branch_tip, worktree, author, committer,
7663 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7664 if (err)
7665 goto done;
7667 err = update_fileindex_after_commit(worktree, &commitable_paths,
7668 *new_commit_id, fileindex, have_staged_files);
7669 sync_err = sync_fileindex(fileindex, fileindex_path);
7670 if (sync_err && err == NULL)
7671 err = sync_err;
7672 done:
7673 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7674 struct got_commitable *ct = pe->data;
7675 free_commitable(ct);
7677 got_pathlist_free(&commitable_paths);
7678 free(fileindex_path);
7679 return err;
7682 const struct got_error *
7683 got_worktree_merge_complete(struct got_worktree *worktree,
7684 struct got_fileindex *fileindex, struct got_repository *repo)
7686 const struct got_error *err, *unlockerr, *sync_err;
7687 char *fileindex_path = NULL;
7689 err = delete_merge_refs(worktree, repo);
7690 if (err)
7691 goto done;
7693 err = get_fileindex_path(&fileindex_path, worktree);
7694 if (err)
7695 goto done;
7696 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7697 sync_err = sync_fileindex(fileindex, fileindex_path);
7698 if (sync_err && err == NULL)
7699 err = sync_err;
7700 done:
7701 got_fileindex_free(fileindex);
7702 free(fileindex_path);
7703 unlockerr = lock_worktree(worktree, LOCK_SH);
7704 if (unlockerr && err == NULL)
7705 err = unlockerr;
7706 return err;
7709 const struct got_error *
7710 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7711 struct got_repository *repo)
7713 const struct got_error *err;
7714 char *branch_refname = NULL;
7715 struct got_reference *branch_ref = NULL;
7717 *in_progress = 0;
7719 err = get_merge_branch_ref_name(&branch_refname, worktree);
7720 if (err)
7721 return err;
7722 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7723 free(branch_refname);
7724 if (err) {
7725 if (err->code != GOT_ERR_NOT_REF)
7726 return err;
7727 } else
7728 *in_progress = 1;
7730 return NULL;
7733 const struct got_error *got_worktree_merge_prepare(
7734 struct got_fileindex **fileindex, struct got_worktree *worktree,
7735 struct got_reference *branch, struct got_repository *repo)
7737 const struct got_error *err = NULL;
7738 char *fileindex_path = NULL;
7739 char *branch_refname = NULL, *commit_refname = NULL;
7740 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
7741 struct got_reference *commit_ref = NULL;
7742 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
7743 struct check_rebase_ok_arg ok_arg;
7745 *fileindex = NULL;
7747 err = lock_worktree(worktree, LOCK_EX);
7748 if (err)
7749 return err;
7751 err = open_fileindex(fileindex, &fileindex_path, worktree);
7752 if (err)
7753 goto done;
7755 /* Preconditions are the same as for rebase. */
7756 ok_arg.worktree = worktree;
7757 ok_arg.repo = repo;
7758 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7759 &ok_arg);
7760 if (err)
7761 goto done;
7763 err = get_merge_branch_ref_name(&branch_refname, worktree);
7764 if (err)
7765 return err;
7767 err = get_merge_commit_ref_name(&commit_refname, worktree);
7768 if (err)
7769 return err;
7771 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7772 0);
7773 if (err)
7774 goto done;
7776 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
7777 if (err)
7778 goto done;
7780 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
7781 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
7782 goto done;
7785 err = got_ref_resolve(&branch_tip, repo, branch);
7786 if (err)
7787 goto done;
7789 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
7790 if (err)
7791 goto done;
7792 err = got_ref_write(branch_ref, repo);
7793 if (err)
7794 goto done;
7796 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
7797 if (err)
7798 goto done;
7799 err = got_ref_write(commit_ref, repo);
7800 if (err)
7801 goto done;
7803 done:
7804 free(branch_refname);
7805 free(commit_refname);
7806 free(fileindex_path);
7807 if (branch_ref)
7808 got_ref_close(branch_ref);
7809 if (commit_ref)
7810 got_ref_close(commit_ref);
7811 if (wt_branch)
7812 got_ref_close(wt_branch);
7813 free(wt_branch_tip);
7814 if (err) {
7815 if (*fileindex) {
7816 got_fileindex_free(*fileindex);
7817 *fileindex = NULL;
7819 lock_worktree(worktree, LOCK_SH);
7821 return err;
7824 const struct got_error *
7825 got_worktree_merge_continue(char **branch_name,
7826 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
7827 struct got_worktree *worktree, struct got_repository *repo)
7829 const struct got_error *err;
7830 char *commit_refname = NULL, *branch_refname = NULL;
7831 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
7832 char *fileindex_path = NULL;
7833 int have_staged_files = 0;
7835 *branch_name = NULL;
7836 *branch_tip = NULL;
7837 *fileindex = NULL;
7839 err = lock_worktree(worktree, LOCK_EX);
7840 if (err)
7841 return err;
7843 err = open_fileindex(fileindex, &fileindex_path, worktree);
7844 if (err)
7845 goto done;
7847 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7848 &have_staged_files);
7849 if (err && err->code != GOT_ERR_CANCELLED)
7850 goto done;
7851 if (have_staged_files) {
7852 err = got_error(GOT_ERR_STAGED_PATHS);
7853 goto done;
7856 err = get_merge_branch_ref_name(&branch_refname, worktree);
7857 if (err)
7858 goto done;
7860 err = get_merge_commit_ref_name(&commit_refname, worktree);
7861 if (err)
7862 goto done;
7864 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7865 if (err)
7866 goto done;
7868 if (!got_ref_is_symbolic(branch_ref)) {
7869 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7870 "%s is not a symbolic reference",
7871 got_ref_get_name(branch_ref));
7872 goto done;
7874 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
7875 if (*branch_name == NULL) {
7876 err = got_error_from_errno("strdup");
7877 goto done;
7880 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
7881 if (err)
7882 goto done;
7884 err = got_ref_resolve(branch_tip, repo, commit_ref);
7885 if (err)
7886 goto done;
7887 done:
7888 free(commit_refname);
7889 free(branch_refname);
7890 free(fileindex_path);
7891 if (commit_ref)
7892 got_ref_close(commit_ref);
7893 if (branch_ref)
7894 got_ref_close(branch_ref);
7895 if (err) {
7896 if (*branch_name) {
7897 free(*branch_name);
7898 *branch_name = NULL;
7900 free(*branch_tip);
7901 *branch_tip = NULL;
7902 if (*fileindex) {
7903 got_fileindex_free(*fileindex);
7904 *fileindex = NULL;
7906 lock_worktree(worktree, LOCK_SH);
7908 return err;
7911 const struct got_error *
7912 got_worktree_merge_abort(struct got_worktree *worktree,
7913 struct got_fileindex *fileindex, struct got_repository *repo,
7914 got_worktree_checkout_cb progress_cb, void *progress_arg)
7916 const struct got_error *err, *unlockerr, *sync_err;
7917 struct got_object_id *commit_id = NULL;
7918 struct got_commit_object *commit = NULL;
7919 char *fileindex_path = NULL;
7920 struct revert_file_args rfa;
7921 struct got_object_id *tree_id = NULL;
7923 err = got_object_open_as_commit(&commit, repo,
7924 worktree->base_commit_id);
7925 if (err)
7926 goto done;
7928 err = got_object_id_by_path(&tree_id, repo, commit,
7929 worktree->path_prefix);
7930 if (err)
7931 goto done;
7933 err = delete_merge_refs(worktree, repo);
7934 if (err)
7935 goto done;
7937 err = get_fileindex_path(&fileindex_path, worktree);
7938 if (err)
7939 goto done;
7941 rfa.worktree = worktree;
7942 rfa.fileindex = fileindex;
7943 rfa.progress_cb = progress_cb;
7944 rfa.progress_arg = progress_arg;
7945 rfa.patch_cb = NULL;
7946 rfa.patch_arg = NULL;
7947 rfa.repo = repo;
7948 rfa.unlink_added_files = 1;
7949 err = worktree_status(worktree, "", fileindex, repo,
7950 revert_file, &rfa, NULL, NULL, 1, 0);
7951 if (err)
7952 goto sync;
7954 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7955 repo, progress_cb, progress_arg, NULL, NULL);
7956 sync:
7957 sync_err = sync_fileindex(fileindex, fileindex_path);
7958 if (sync_err && err == NULL)
7959 err = sync_err;
7960 done:
7961 free(tree_id);
7962 free(commit_id);
7963 if (commit)
7964 got_object_commit_close(commit);
7965 if (fileindex)
7966 got_fileindex_free(fileindex);
7967 free(fileindex_path);
7969 unlockerr = lock_worktree(worktree, LOCK_SH);
7970 if (unlockerr && err == NULL)
7971 err = unlockerr;
7972 return err;
7975 struct check_stage_ok_arg {
7976 struct got_object_id *head_commit_id;
7977 struct got_worktree *worktree;
7978 struct got_fileindex *fileindex;
7979 struct got_repository *repo;
7980 int have_changes;
7983 static const struct got_error *
7984 check_stage_ok(void *arg, unsigned char status,
7985 unsigned char staged_status, const char *relpath,
7986 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7987 struct got_object_id *commit_id, int dirfd, const char *de_name)
7989 struct check_stage_ok_arg *a = arg;
7990 const struct got_error *err = NULL;
7991 struct got_fileindex_entry *ie;
7992 struct got_object_id base_commit_id;
7993 struct got_object_id *base_commit_idp = NULL;
7994 char *in_repo_path = NULL, *p;
7996 if (status == GOT_STATUS_UNVERSIONED ||
7997 status == GOT_STATUS_NO_CHANGE)
7998 return NULL;
7999 if (status == GOT_STATUS_NONEXISTENT)
8000 return got_error_set_errno(ENOENT, relpath);
8002 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8003 if (ie == NULL)
8004 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8006 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8007 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8008 relpath) == -1)
8009 return got_error_from_errno("asprintf");
8011 if (got_fileindex_entry_has_commit(ie)) {
8012 memcpy(base_commit_id.sha1, ie->commit_sha1,
8013 SHA1_DIGEST_LENGTH);
8014 base_commit_idp = &base_commit_id;
8017 if (status == GOT_STATUS_CONFLICT) {
8018 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8019 goto done;
8020 } else if (status != GOT_STATUS_ADD &&
8021 status != GOT_STATUS_MODIFY &&
8022 status != GOT_STATUS_DELETE) {
8023 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8024 goto done;
8027 a->have_changes = 1;
8029 p = in_repo_path;
8030 while (p[0] == '/')
8031 p++;
8032 err = check_out_of_date(p, status, staged_status,
8033 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8034 GOT_ERR_STAGE_OUT_OF_DATE);
8035 done:
8036 free(in_repo_path);
8037 return err;
8040 struct stage_path_arg {
8041 struct got_worktree *worktree;
8042 struct got_fileindex *fileindex;
8043 struct got_repository *repo;
8044 got_worktree_status_cb status_cb;
8045 void *status_arg;
8046 got_worktree_patch_cb patch_cb;
8047 void *patch_arg;
8048 int staged_something;
8049 int allow_bad_symlinks;
8052 static const struct got_error *
8053 stage_path(void *arg, unsigned char status,
8054 unsigned char staged_status, const char *relpath,
8055 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8056 struct got_object_id *commit_id, int dirfd, const char *de_name)
8058 struct stage_path_arg *a = arg;
8059 const struct got_error *err = NULL;
8060 struct got_fileindex_entry *ie;
8061 char *ondisk_path = NULL, *path_content = NULL;
8062 uint32_t stage;
8063 struct got_object_id *new_staged_blob_id = NULL;
8064 struct stat sb;
8066 if (status == GOT_STATUS_UNVERSIONED)
8067 return NULL;
8069 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8070 if (ie == NULL)
8071 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8073 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8074 relpath)== -1)
8075 return got_error_from_errno("asprintf");
8077 switch (status) {
8078 case GOT_STATUS_ADD:
8079 case GOT_STATUS_MODIFY:
8080 /* XXX could sb.st_mode be passed in by our caller? */
8081 if (lstat(ondisk_path, &sb) == -1) {
8082 err = got_error_from_errno2("lstat", ondisk_path);
8083 break;
8085 if (a->patch_cb) {
8086 if (status == GOT_STATUS_ADD) {
8087 int choice = GOT_PATCH_CHOICE_NONE;
8088 err = (*a->patch_cb)(&choice, a->patch_arg,
8089 status, ie->path, NULL, 1, 1);
8090 if (err)
8091 break;
8092 if (choice != GOT_PATCH_CHOICE_YES)
8093 break;
8094 } else {
8095 err = create_patched_content(&path_content, 0,
8096 staged_blob_id ? staged_blob_id : blob_id,
8097 ondisk_path, dirfd, de_name, ie->path,
8098 a->repo, a->patch_cb, a->patch_arg);
8099 if (err || path_content == NULL)
8100 break;
8103 err = got_object_blob_create(&new_staged_blob_id,
8104 path_content ? path_content : ondisk_path, a->repo);
8105 if (err)
8106 break;
8107 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8108 SHA1_DIGEST_LENGTH);
8109 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8110 stage = GOT_FILEIDX_STAGE_ADD;
8111 else
8112 stage = GOT_FILEIDX_STAGE_MODIFY;
8113 got_fileindex_entry_stage_set(ie, stage);
8114 if (S_ISLNK(sb.st_mode)) {
8115 int is_bad_symlink = 0;
8116 if (!a->allow_bad_symlinks) {
8117 char target_path[PATH_MAX];
8118 ssize_t target_len;
8119 target_len = readlink(ondisk_path, target_path,
8120 sizeof(target_path));
8121 if (target_len == -1) {
8122 err = got_error_from_errno2("readlink",
8123 ondisk_path);
8124 break;
8126 err = is_bad_symlink_target(&is_bad_symlink,
8127 target_path, target_len, ondisk_path,
8128 a->worktree->root_path);
8129 if (err)
8130 break;
8131 if (is_bad_symlink) {
8132 err = got_error_path(ondisk_path,
8133 GOT_ERR_BAD_SYMLINK);
8134 break;
8137 if (is_bad_symlink)
8138 got_fileindex_entry_staged_filetype_set(ie,
8139 GOT_FILEIDX_MODE_BAD_SYMLINK);
8140 else
8141 got_fileindex_entry_staged_filetype_set(ie,
8142 GOT_FILEIDX_MODE_SYMLINK);
8143 } else {
8144 got_fileindex_entry_staged_filetype_set(ie,
8145 GOT_FILEIDX_MODE_REGULAR_FILE);
8147 a->staged_something = 1;
8148 if (a->status_cb == NULL)
8149 break;
8150 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8151 get_staged_status(ie), relpath, blob_id,
8152 new_staged_blob_id, NULL, dirfd, de_name);
8153 if (err)
8154 break;
8156 * When staging the reverse of the staged diff,
8157 * implicitly unstage the file.
8159 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8160 sizeof(ie->blob_sha1)) == 0) {
8161 got_fileindex_entry_stage_set(ie,
8162 GOT_FILEIDX_STAGE_NONE);
8164 break;
8165 case GOT_STATUS_DELETE:
8166 if (staged_status == GOT_STATUS_DELETE)
8167 break;
8168 if (a->patch_cb) {
8169 int choice = GOT_PATCH_CHOICE_NONE;
8170 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8171 ie->path, NULL, 1, 1);
8172 if (err)
8173 break;
8174 if (choice == GOT_PATCH_CHOICE_NO)
8175 break;
8176 if (choice != GOT_PATCH_CHOICE_YES) {
8177 err = got_error(GOT_ERR_PATCH_CHOICE);
8178 break;
8181 stage = GOT_FILEIDX_STAGE_DELETE;
8182 got_fileindex_entry_stage_set(ie, stage);
8183 a->staged_something = 1;
8184 if (a->status_cb == NULL)
8185 break;
8186 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8187 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8188 de_name);
8189 break;
8190 case GOT_STATUS_NO_CHANGE:
8191 break;
8192 case GOT_STATUS_CONFLICT:
8193 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8194 break;
8195 case GOT_STATUS_NONEXISTENT:
8196 err = got_error_set_errno(ENOENT, relpath);
8197 break;
8198 default:
8199 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8200 break;
8203 if (path_content && unlink(path_content) == -1 && err == NULL)
8204 err = got_error_from_errno2("unlink", path_content);
8205 free(path_content);
8206 free(ondisk_path);
8207 free(new_staged_blob_id);
8208 return err;
8211 const struct got_error *
8212 got_worktree_stage(struct got_worktree *worktree,
8213 struct got_pathlist_head *paths,
8214 got_worktree_status_cb status_cb, void *status_arg,
8215 got_worktree_patch_cb patch_cb, void *patch_arg,
8216 int allow_bad_symlinks, struct got_repository *repo)
8218 const struct got_error *err = NULL, *sync_err, *unlockerr;
8219 struct got_pathlist_entry *pe;
8220 struct got_fileindex *fileindex = NULL;
8221 char *fileindex_path = NULL;
8222 struct got_reference *head_ref = NULL;
8223 struct got_object_id *head_commit_id = NULL;
8224 struct check_stage_ok_arg oka;
8225 struct stage_path_arg spa;
8227 err = lock_worktree(worktree, LOCK_EX);
8228 if (err)
8229 return err;
8231 err = got_ref_open(&head_ref, repo,
8232 got_worktree_get_head_ref_name(worktree), 0);
8233 if (err)
8234 goto done;
8235 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8236 if (err)
8237 goto done;
8238 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8239 if (err)
8240 goto done;
8242 /* Check pre-conditions before staging anything. */
8243 oka.head_commit_id = head_commit_id;
8244 oka.worktree = worktree;
8245 oka.fileindex = fileindex;
8246 oka.repo = repo;
8247 oka.have_changes = 0;
8248 TAILQ_FOREACH(pe, paths, entry) {
8249 err = worktree_status(worktree, pe->path, fileindex, repo,
8250 check_stage_ok, &oka, NULL, NULL, 1, 0);
8251 if (err)
8252 goto done;
8254 if (!oka.have_changes) {
8255 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8256 goto done;
8259 spa.worktree = worktree;
8260 spa.fileindex = fileindex;
8261 spa.repo = repo;
8262 spa.patch_cb = patch_cb;
8263 spa.patch_arg = patch_arg;
8264 spa.status_cb = status_cb;
8265 spa.status_arg = status_arg;
8266 spa.staged_something = 0;
8267 spa.allow_bad_symlinks = allow_bad_symlinks;
8268 TAILQ_FOREACH(pe, paths, entry) {
8269 err = worktree_status(worktree, pe->path, fileindex, repo,
8270 stage_path, &spa, NULL, NULL, 1, 0);
8271 if (err)
8272 goto done;
8274 if (!spa.staged_something) {
8275 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8276 goto done;
8279 sync_err = sync_fileindex(fileindex, fileindex_path);
8280 if (sync_err && err == NULL)
8281 err = sync_err;
8282 done:
8283 if (head_ref)
8284 got_ref_close(head_ref);
8285 free(head_commit_id);
8286 free(fileindex_path);
8287 if (fileindex)
8288 got_fileindex_free(fileindex);
8289 unlockerr = lock_worktree(worktree, LOCK_SH);
8290 if (unlockerr && err == NULL)
8291 err = unlockerr;
8292 return err;
8295 struct unstage_path_arg {
8296 struct got_worktree *worktree;
8297 struct got_fileindex *fileindex;
8298 struct got_repository *repo;
8299 got_worktree_checkout_cb progress_cb;
8300 void *progress_arg;
8301 got_worktree_patch_cb patch_cb;
8302 void *patch_arg;
8305 static const struct got_error *
8306 create_unstaged_content(char **path_unstaged_content,
8307 char **path_new_staged_content, struct got_object_id *blob_id,
8308 struct got_object_id *staged_blob_id, const char *relpath,
8309 struct got_repository *repo,
8310 got_worktree_patch_cb patch_cb, void *patch_arg)
8312 const struct got_error *err, *free_err;
8313 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8314 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8315 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8316 struct got_diffreg_result *diffreg_result = NULL;
8317 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8318 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8319 int fd1 = -1, fd2 = -1;
8321 *path_unstaged_content = NULL;
8322 *path_new_staged_content = NULL;
8324 err = got_object_id_str(&label1, blob_id);
8325 if (err)
8326 return err;
8328 fd1 = got_opentempfd();
8329 if (fd1 == -1) {
8330 err = got_error_from_errno("got_opentempfd");
8331 goto done;
8333 fd2 = got_opentempfd();
8334 if (fd2 == -1) {
8335 err = got_error_from_errno("got_opentempfd");
8336 goto done;
8339 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8340 if (err)
8341 goto done;
8343 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
8344 if (err)
8345 goto done;
8347 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8348 if (err)
8349 goto done;
8351 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8352 fd2);
8353 if (err)
8354 goto done;
8356 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
8357 if (err)
8358 goto done;
8360 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8361 if (err)
8362 goto done;
8364 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8365 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8366 if (err)
8367 goto done;
8369 err = got_opentemp_named(path_unstaged_content, &outfile,
8370 "got-unstaged-content");
8371 if (err)
8372 goto done;
8373 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8374 "got-new-staged-content");
8375 if (err)
8376 goto done;
8378 if (fseek(f1, 0L, SEEK_SET) == -1) {
8379 err = got_ferror(f1, GOT_ERR_IO);
8380 goto done;
8382 if (fseek(f2, 0L, SEEK_SET) == -1) {
8383 err = got_ferror(f2, GOT_ERR_IO);
8384 goto done;
8386 /* Count the number of actual changes in the diff result. */
8387 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8388 struct diff_chunk_context cc = {};
8389 diff_chunk_context_load_change(&cc, &nchunks_used,
8390 diffreg_result->result, n, 0);
8391 nchanges++;
8393 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8394 int choice;
8395 err = apply_or_reject_change(&choice, &nchunks_used,
8396 diffreg_result->result, n, relpath, f1, f2,
8397 &line_cur1, &line_cur2,
8398 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8399 if (err)
8400 goto done;
8401 if (choice == GOT_PATCH_CHOICE_YES)
8402 have_content = 1;
8403 else
8404 have_rejected_content = 1;
8405 if (choice == GOT_PATCH_CHOICE_QUIT)
8406 break;
8408 if (have_content || have_rejected_content)
8409 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8410 outfile, rejectfile);
8411 done:
8412 free(label1);
8413 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8414 err = got_error_from_errno("close");
8415 if (blob)
8416 got_object_blob_close(blob);
8417 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8418 err = got_error_from_errno("close");
8419 if (staged_blob)
8420 got_object_blob_close(staged_blob);
8421 free_err = got_diffreg_result_free(diffreg_result);
8422 if (free_err && err == NULL)
8423 err = free_err;
8424 if (f1 && fclose(f1) == EOF && err == NULL)
8425 err = got_error_from_errno2("fclose", path1);
8426 if (f2 && fclose(f2) == EOF && err == NULL)
8427 err = got_error_from_errno2("fclose", path2);
8428 if (outfile && fclose(outfile) == EOF && err == NULL)
8429 err = got_error_from_errno2("fclose", *path_unstaged_content);
8430 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8431 err = got_error_from_errno2("fclose", *path_new_staged_content);
8432 if (path1 && unlink(path1) == -1 && err == NULL)
8433 err = got_error_from_errno2("unlink", path1);
8434 if (path2 && unlink(path2) == -1 && err == NULL)
8435 err = got_error_from_errno2("unlink", path2);
8436 if (err || !have_content) {
8437 if (*path_unstaged_content &&
8438 unlink(*path_unstaged_content) == -1 && err == NULL)
8439 err = got_error_from_errno2("unlink",
8440 *path_unstaged_content);
8441 free(*path_unstaged_content);
8442 *path_unstaged_content = NULL;
8444 if (err || !have_content || !have_rejected_content) {
8445 if (*path_new_staged_content &&
8446 unlink(*path_new_staged_content) == -1 && err == NULL)
8447 err = got_error_from_errno2("unlink",
8448 *path_new_staged_content);
8449 free(*path_new_staged_content);
8450 *path_new_staged_content = NULL;
8452 free(path1);
8453 free(path2);
8454 return err;
8457 static const struct got_error *
8458 unstage_hunks(struct got_object_id *staged_blob_id,
8459 struct got_blob_object *blob_base,
8460 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8461 const char *ondisk_path, const char *label_orig,
8462 struct got_worktree *worktree, struct got_repository *repo,
8463 got_worktree_patch_cb patch_cb, void *patch_arg,
8464 got_worktree_checkout_cb progress_cb, void *progress_arg)
8466 const struct got_error *err = NULL;
8467 char *path_unstaged_content = NULL;
8468 char *path_new_staged_content = NULL;
8469 char *parent = NULL, *base_path = NULL;
8470 char *blob_base_path = NULL;
8471 struct got_object_id *new_staged_blob_id = NULL;
8472 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8473 struct stat sb;
8475 err = create_unstaged_content(&path_unstaged_content,
8476 &path_new_staged_content, blob_id, staged_blob_id,
8477 ie->path, repo, patch_cb, patch_arg);
8478 if (err)
8479 return err;
8481 if (path_unstaged_content == NULL)
8482 return NULL;
8484 if (path_new_staged_content) {
8485 err = got_object_blob_create(&new_staged_blob_id,
8486 path_new_staged_content, repo);
8487 if (err)
8488 goto done;
8491 f = fopen(path_unstaged_content, "re");
8492 if (f == NULL) {
8493 err = got_error_from_errno2("fopen",
8494 path_unstaged_content);
8495 goto done;
8497 if (fstat(fileno(f), &sb) == -1) {
8498 err = got_error_from_errno2("fstat", path_unstaged_content);
8499 goto done;
8501 if (got_fileindex_entry_staged_filetype_get(ie) ==
8502 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8503 char link_target[PATH_MAX];
8504 size_t r;
8505 r = fread(link_target, 1, sizeof(link_target), f);
8506 if (r == 0 && ferror(f)) {
8507 err = got_error_from_errno("fread");
8508 goto done;
8510 if (r >= sizeof(link_target)) { /* should not happen */
8511 err = got_error(GOT_ERR_NO_SPACE);
8512 goto done;
8514 link_target[r] = '\0';
8515 err = merge_symlink(worktree, blob_base,
8516 ondisk_path, ie->path, label_orig, link_target,
8517 worktree->base_commit_id, repo, progress_cb,
8518 progress_arg);
8519 } else {
8520 int local_changes_subsumed;
8522 err = got_path_dirname(&parent, ondisk_path);
8523 if (err)
8524 return err;
8526 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8527 parent) == -1) {
8528 err = got_error_from_errno("asprintf");
8529 base_path = NULL;
8530 goto done;
8533 err = got_opentemp_named(&blob_base_path, &f_base, base_path);
8534 if (err)
8535 goto done;
8536 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8537 blob_base);
8538 if (err)
8539 goto done;
8542 * In order the run a 3-way merge with a symlink we copy the symlink's
8543 * target path into a temporary file and use that file with diff3.
8545 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8546 err = dump_symlink_target_path_to_file(&f_deriv2,
8547 ondisk_path);
8548 if (err)
8549 goto done;
8550 } else {
8551 int fd;
8552 fd = open(ondisk_path,
8553 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8554 if (fd == -1) {
8555 err = got_error_from_errno2("open", ondisk_path);
8556 goto done;
8558 f_deriv2 = fdopen(fd, "r");
8559 if (f_deriv2 == NULL) {
8560 err = got_error_from_errno2("fdopen", ondisk_path);
8561 close(fd);
8562 goto done;
8566 err = merge_file(&local_changes_subsumed, worktree,
8567 f_base, f, f_deriv2, ondisk_path, ie->path,
8568 got_fileindex_perms_to_st(ie),
8569 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8570 repo, progress_cb, progress_arg);
8572 if (err)
8573 goto done;
8575 if (new_staged_blob_id) {
8576 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8577 SHA1_DIGEST_LENGTH);
8578 } else {
8579 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8580 got_fileindex_entry_staged_filetype_set(ie, 0);
8582 done:
8583 free(new_staged_blob_id);
8584 if (path_unstaged_content &&
8585 unlink(path_unstaged_content) == -1 && err == NULL)
8586 err = got_error_from_errno2("unlink", path_unstaged_content);
8587 if (path_new_staged_content &&
8588 unlink(path_new_staged_content) == -1 && err == NULL)
8589 err = got_error_from_errno2("unlink", path_new_staged_content);
8590 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8591 err = got_error_from_errno2("unlink", blob_base_path);
8592 if (f_base && fclose(f_base) == EOF && err == NULL)
8593 err = got_error_from_errno2("fclose", path_unstaged_content);
8594 if (f && fclose(f) == EOF && err == NULL)
8595 err = got_error_from_errno2("fclose", path_unstaged_content);
8596 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8597 err = got_error_from_errno2("fclose", ondisk_path);
8598 free(path_unstaged_content);
8599 free(path_new_staged_content);
8600 free(blob_base_path);
8601 free(parent);
8602 free(base_path);
8603 return err;
8606 static const struct got_error *
8607 unstage_path(void *arg, unsigned char status,
8608 unsigned char staged_status, const char *relpath,
8609 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8610 struct got_object_id *commit_id, int dirfd, const char *de_name)
8612 const struct got_error *err = NULL;
8613 struct unstage_path_arg *a = arg;
8614 struct got_fileindex_entry *ie;
8615 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8616 char *ondisk_path = NULL;
8617 char *id_str = NULL, *label_orig = NULL;
8618 int local_changes_subsumed;
8619 struct stat sb;
8620 int fd1 = -1, fd2 = -1;
8622 if (staged_status != GOT_STATUS_ADD &&
8623 staged_status != GOT_STATUS_MODIFY &&
8624 staged_status != GOT_STATUS_DELETE)
8625 return NULL;
8627 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8628 if (ie == NULL)
8629 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8631 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8632 == -1)
8633 return got_error_from_errno("asprintf");
8635 err = got_object_id_str(&id_str,
8636 commit_id ? commit_id : a->worktree->base_commit_id);
8637 if (err)
8638 goto done;
8639 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8640 id_str) == -1) {
8641 err = got_error_from_errno("asprintf");
8642 goto done;
8645 fd1 = got_opentempfd();
8646 if (fd1 == -1) {
8647 err = got_error_from_errno("got_opentempfd");
8648 goto done;
8650 fd2 = got_opentempfd();
8651 if (fd2 == -1) {
8652 err = got_error_from_errno("got_opentempfd");
8653 goto done;
8656 switch (staged_status) {
8657 case GOT_STATUS_MODIFY:
8658 err = got_object_open_as_blob(&blob_base, a->repo,
8659 blob_id, 8192, fd1);
8660 if (err)
8661 break;
8662 /* fall through */
8663 case GOT_STATUS_ADD:
8664 if (a->patch_cb) {
8665 if (staged_status == GOT_STATUS_ADD) {
8666 int choice = GOT_PATCH_CHOICE_NONE;
8667 err = (*a->patch_cb)(&choice, a->patch_arg,
8668 staged_status, ie->path, NULL, 1, 1);
8669 if (err)
8670 break;
8671 if (choice != GOT_PATCH_CHOICE_YES)
8672 break;
8673 } else {
8674 err = unstage_hunks(staged_blob_id,
8675 blob_base, blob_id, ie, ondisk_path,
8676 label_orig, a->worktree, a->repo,
8677 a->patch_cb, a->patch_arg,
8678 a->progress_cb, a->progress_arg);
8679 break; /* Done with this file. */
8682 err = got_object_open_as_blob(&blob_staged, a->repo,
8683 staged_blob_id, 8192, fd2);
8684 if (err)
8685 break;
8686 switch (got_fileindex_entry_staged_filetype_get(ie)) {
8687 case GOT_FILEIDX_MODE_BAD_SYMLINK:
8688 case GOT_FILEIDX_MODE_REGULAR_FILE:
8689 err = merge_blob(&local_changes_subsumed, a->worktree,
8690 blob_base, ondisk_path, relpath,
8691 got_fileindex_perms_to_st(ie), label_orig,
8692 blob_staged, commit_id ? commit_id :
8693 a->worktree->base_commit_id, a->repo,
8694 a->progress_cb, a->progress_arg);
8695 break;
8696 case GOT_FILEIDX_MODE_SYMLINK:
8697 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8698 char *staged_target;
8699 err = got_object_blob_read_to_str(
8700 &staged_target, blob_staged);
8701 if (err)
8702 goto done;
8703 err = merge_symlink(a->worktree, blob_base,
8704 ondisk_path, relpath, label_orig,
8705 staged_target, commit_id ? commit_id :
8706 a->worktree->base_commit_id,
8707 a->repo, a->progress_cb, a->progress_arg);
8708 free(staged_target);
8709 } else {
8710 err = merge_blob(&local_changes_subsumed,
8711 a->worktree, blob_base, ondisk_path,
8712 relpath, got_fileindex_perms_to_st(ie),
8713 label_orig, blob_staged,
8714 commit_id ? commit_id :
8715 a->worktree->base_commit_id, a->repo,
8716 a->progress_cb, a->progress_arg);
8718 break;
8719 default:
8720 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8721 break;
8723 if (err == NULL) {
8724 got_fileindex_entry_stage_set(ie,
8725 GOT_FILEIDX_STAGE_NONE);
8726 got_fileindex_entry_staged_filetype_set(ie, 0);
8728 break;
8729 case GOT_STATUS_DELETE:
8730 if (a->patch_cb) {
8731 int choice = GOT_PATCH_CHOICE_NONE;
8732 err = (*a->patch_cb)(&choice, a->patch_arg,
8733 staged_status, ie->path, NULL, 1, 1);
8734 if (err)
8735 break;
8736 if (choice == GOT_PATCH_CHOICE_NO)
8737 break;
8738 if (choice != GOT_PATCH_CHOICE_YES) {
8739 err = got_error(GOT_ERR_PATCH_CHOICE);
8740 break;
8743 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8744 got_fileindex_entry_staged_filetype_set(ie, 0);
8745 err = get_file_status(&status, &sb, ie, ondisk_path,
8746 dirfd, de_name, a->repo);
8747 if (err)
8748 break;
8749 err = (*a->progress_cb)(a->progress_arg, status, relpath);
8750 break;
8752 done:
8753 free(ondisk_path);
8754 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8755 err = got_error_from_errno("close");
8756 if (blob_base)
8757 got_object_blob_close(blob_base);
8758 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8759 err = got_error_from_errno("close");
8760 if (blob_staged)
8761 got_object_blob_close(blob_staged);
8762 free(id_str);
8763 free(label_orig);
8764 return err;
8767 const struct got_error *
8768 got_worktree_unstage(struct got_worktree *worktree,
8769 struct got_pathlist_head *paths,
8770 got_worktree_checkout_cb progress_cb, void *progress_arg,
8771 got_worktree_patch_cb patch_cb, void *patch_arg,
8772 struct got_repository *repo)
8774 const struct got_error *err = NULL, *sync_err, *unlockerr;
8775 struct got_pathlist_entry *pe;
8776 struct got_fileindex *fileindex = NULL;
8777 char *fileindex_path = NULL;
8778 struct unstage_path_arg upa;
8780 err = lock_worktree(worktree, LOCK_EX);
8781 if (err)
8782 return err;
8784 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8785 if (err)
8786 goto done;
8788 upa.worktree = worktree;
8789 upa.fileindex = fileindex;
8790 upa.repo = repo;
8791 upa.progress_cb = progress_cb;
8792 upa.progress_arg = progress_arg;
8793 upa.patch_cb = patch_cb;
8794 upa.patch_arg = patch_arg;
8795 TAILQ_FOREACH(pe, paths, entry) {
8796 err = worktree_status(worktree, pe->path, fileindex, repo,
8797 unstage_path, &upa, NULL, NULL, 1, 0);
8798 if (err)
8799 goto done;
8802 sync_err = sync_fileindex(fileindex, fileindex_path);
8803 if (sync_err && err == NULL)
8804 err = sync_err;
8805 done:
8806 free(fileindex_path);
8807 if (fileindex)
8808 got_fileindex_free(fileindex);
8809 unlockerr = lock_worktree(worktree, LOCK_SH);
8810 if (unlockerr && err == NULL)
8811 err = unlockerr;
8812 return err;
8815 struct report_file_info_arg {
8816 struct got_worktree *worktree;
8817 got_worktree_path_info_cb info_cb;
8818 void *info_arg;
8819 struct got_pathlist_head *paths;
8820 got_cancel_cb cancel_cb;
8821 void *cancel_arg;
8824 static const struct got_error *
8825 report_file_info(void *arg, struct got_fileindex_entry *ie)
8827 struct report_file_info_arg *a = arg;
8828 struct got_pathlist_entry *pe;
8829 struct got_object_id blob_id, staged_blob_id, commit_id;
8830 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8831 struct got_object_id *commit_idp = NULL;
8832 int stage;
8834 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8835 return got_error(GOT_ERR_CANCELLED);
8837 TAILQ_FOREACH(pe, a->paths, entry) {
8838 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8839 got_path_is_child(ie->path, pe->path, pe->path_len))
8840 break;
8842 if (pe == NULL) /* not found */
8843 return NULL;
8845 if (got_fileindex_entry_has_blob(ie)) {
8846 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8847 blob_idp = &blob_id;
8849 stage = got_fileindex_entry_stage_get(ie);
8850 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8851 stage == GOT_FILEIDX_STAGE_ADD) {
8852 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8853 SHA1_DIGEST_LENGTH);
8854 staged_blob_idp = &staged_blob_id;
8857 if (got_fileindex_entry_has_commit(ie)) {
8858 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8859 commit_idp = &commit_id;
8862 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8863 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8866 const struct got_error *
8867 got_worktree_path_info(struct got_worktree *worktree,
8868 struct got_pathlist_head *paths,
8869 got_worktree_path_info_cb info_cb, void *info_arg,
8870 got_cancel_cb cancel_cb, void *cancel_arg)
8873 const struct got_error *err = NULL, *unlockerr;
8874 struct got_fileindex *fileindex = NULL;
8875 char *fileindex_path = NULL;
8876 struct report_file_info_arg arg;
8878 err = lock_worktree(worktree, LOCK_SH);
8879 if (err)
8880 return err;
8882 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8883 if (err)
8884 goto done;
8886 arg.worktree = worktree;
8887 arg.info_cb = info_cb;
8888 arg.info_arg = info_arg;
8889 arg.paths = paths;
8890 arg.cancel_cb = cancel_cb;
8891 arg.cancel_arg = cancel_arg;
8892 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8893 &arg);
8894 done:
8895 free(fileindex_path);
8896 if (fileindex)
8897 got_fileindex_free(fileindex);
8898 unlockerr = lock_worktree(worktree, LOCK_UN);
8899 if (unlockerr && err == NULL)
8900 err = unlockerr;
8901 return err;
8904 static const struct got_error *
8905 patch_check_path(const char *p, char **path, unsigned char *status,
8906 unsigned char *staged_status, struct got_fileindex *fileindex,
8907 struct got_worktree *worktree, struct got_repository *repo)
8909 const struct got_error *err;
8910 struct got_fileindex_entry *ie;
8911 struct stat sb;
8912 char *ondisk_path = NULL;
8914 err = got_worktree_resolve_path(path, worktree, p);
8915 if (err)
8916 return err;
8918 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
8919 *path[0] ? "/" : "", *path) == -1)
8920 return got_error_from_errno("asprintf");
8922 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
8923 if (ie) {
8924 *staged_status = get_staged_status(ie);
8925 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
8926 repo);
8927 if (err)
8928 goto done;
8929 } else {
8930 *staged_status = GOT_STATUS_NO_CHANGE;
8931 *status = GOT_STATUS_UNVERSIONED;
8932 if (lstat(ondisk_path, &sb) == -1) {
8933 if (errno != ENOENT) {
8934 err = got_error_from_errno2("lstat",
8935 ondisk_path);
8936 goto done;
8938 *status = GOT_STATUS_NONEXISTENT;
8942 done:
8943 free(ondisk_path);
8944 return err;
8947 static const struct got_error *
8948 patch_can_rm(const char *path, unsigned char status,
8949 unsigned char staged_status)
8951 if (status == GOT_STATUS_NONEXISTENT)
8952 return got_error_set_errno(ENOENT, path);
8953 if (status != GOT_STATUS_NO_CHANGE &&
8954 status != GOT_STATUS_ADD &&
8955 status != GOT_STATUS_MODIFY &&
8956 status != GOT_STATUS_MODE_CHANGE)
8957 return got_error_path(path, GOT_ERR_FILE_STATUS);
8958 if (staged_status == GOT_STATUS_DELETE)
8959 return got_error_path(path, GOT_ERR_FILE_STATUS);
8960 return NULL;
8963 static const struct got_error *
8964 patch_can_add(const char *path, unsigned char status)
8966 if (status != GOT_STATUS_NONEXISTENT)
8967 return got_error_path(path, GOT_ERR_FILE_STATUS);
8968 return NULL;
8971 static const struct got_error *
8972 patch_can_edit(const char *path, unsigned char status,
8973 unsigned char staged_status)
8975 if (status == GOT_STATUS_NONEXISTENT)
8976 return got_error_set_errno(ENOENT, path);
8977 if (status != GOT_STATUS_NO_CHANGE &&
8978 status != GOT_STATUS_ADD &&
8979 status != GOT_STATUS_MODIFY)
8980 return got_error_path(path, GOT_ERR_FILE_STATUS);
8981 if (staged_status == GOT_STATUS_DELETE)
8982 return got_error_path(path, GOT_ERR_FILE_STATUS);
8983 return NULL;
8986 const struct got_error *
8987 got_worktree_patch_prepare(struct got_fileindex **fileindex,
8988 char **fileindex_path, struct got_worktree *worktree)
8990 return open_fileindex(fileindex, fileindex_path, worktree);
8993 const struct got_error *
8994 got_worktree_patch_check_path(const char *old, const char *new,
8995 char **oldpath, char **newpath, struct got_worktree *worktree,
8996 struct got_repository *repo, struct got_fileindex *fileindex)
8998 const struct got_error *err = NULL;
8999 int file_renamed = 0;
9000 unsigned char status_old, staged_status_old;
9001 unsigned char status_new, staged_status_new;
9003 *oldpath = NULL;
9004 *newpath = NULL;
9006 err = patch_check_path(old != NULL ? old : new, oldpath,
9007 &status_old, &staged_status_old, fileindex, worktree, repo);
9008 if (err)
9009 goto done;
9011 err = patch_check_path(new != NULL ? new : old, newpath,
9012 &status_new, &staged_status_new, fileindex, worktree, repo);
9013 if (err)
9014 goto done;
9016 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9017 file_renamed = 1;
9019 if (old != NULL && new == NULL)
9020 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9021 else if (file_renamed) {
9022 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9023 if (err == NULL)
9024 err = patch_can_add(*newpath, status_new);
9025 } else if (old == NULL)
9026 err = patch_can_add(*newpath, status_new);
9027 else
9028 err = patch_can_edit(*newpath, status_new, staged_status_new);
9030 done:
9031 if (err) {
9032 free(*oldpath);
9033 *oldpath = NULL;
9034 free(*newpath);
9035 *newpath = NULL;
9037 return err;
9040 const struct got_error *
9041 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9042 struct got_worktree *worktree, struct got_fileindex *fileindex,
9043 got_worktree_checkout_cb progress_cb, void *progress_arg)
9045 struct schedule_addition_args saa;
9047 memset(&saa, 0, sizeof(saa));
9048 saa.worktree = worktree;
9049 saa.fileindex = fileindex;
9050 saa.progress_cb = progress_cb;
9051 saa.progress_arg = progress_arg;
9052 saa.repo = repo;
9054 return worktree_status(worktree, path, fileindex, repo,
9055 schedule_addition, &saa, NULL, NULL, 1, 0);
9058 const struct got_error *
9059 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9060 struct got_worktree *worktree, struct got_fileindex *fileindex,
9061 got_worktree_delete_cb progress_cb, void *progress_arg)
9063 struct schedule_deletion_args sda;
9065 memset(&sda, 0, sizeof(sda));
9066 sda.worktree = worktree;
9067 sda.fileindex = fileindex;
9068 sda.progress_cb = progress_cb;
9069 sda.progress_arg = progress_arg;
9070 sda.repo = repo;
9071 sda.delete_local_mods = 0;
9072 sda.keep_on_disk = 0;
9073 sda.ignore_missing_paths = 0;
9074 sda.status_codes = NULL;
9076 return worktree_status(worktree, path, fileindex, repo,
9077 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9080 const struct got_error *
9081 got_worktree_patch_complete(struct got_fileindex *fileindex,
9082 const char *fileindex_path)
9084 const struct got_error *err = NULL;
9086 err = sync_fileindex(fileindex, fileindex_path);
9087 got_fileindex_free(fileindex);
9089 return err;