Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/stat.h>
18 #include <sys/queue.h>
19 #include <sys/tree.h>
21 #include <dirent.h>
22 #include <limits.h>
23 #include <stddef.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <time.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <sha1.h>
32 #include <zlib.h>
33 #include <fnmatch.h>
34 #include <libgen.h>
35 #include <uuid.h>
36 #include <util.h>
38 #include "got_error.h"
39 #include "got_repository.h"
40 #include "got_reference.h"
41 #include "got_object.h"
42 #include "got_path.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_opentemp.h"
46 #include "got_diff.h"
48 #include "got_lib_worktree.h"
49 #include "got_lib_sha1.h"
50 #include "got_lib_fileindex.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_object_idset.h"
57 #include "got_lib_diff.h"
58 #include "got_lib_gotconfig.h"
60 #ifndef MIN
61 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
62 #endif
64 #define GOT_MERGE_LABEL_MERGED "merged change"
65 #define GOT_MERGE_LABEL_BASE "3-way merge base"
67 static const struct got_error *
68 create_meta_file(const char *path_got, const char *name, const char *content)
69 {
70 const struct got_error *err = NULL;
71 char *path;
73 if (asprintf(&path, "%s/%s", path_got, name) == -1)
74 return got_error_from_errno("asprintf");
76 err = got_path_create_file(path, content);
77 free(path);
78 return err;
79 }
81 static const struct got_error *
82 update_meta_file(const char *path_got, const char *name, const char *content)
83 {
84 const struct got_error *err = NULL;
85 FILE *tmpfile = NULL;
86 char *tmppath = NULL;
87 char *path = NULL;
89 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
90 err = got_error_from_errno("asprintf");
91 path = NULL;
92 goto done;
93 }
95 err = got_opentemp_named(&tmppath, &tmpfile, path);
96 if (err)
97 goto done;
99 if (content) {
100 int len = fprintf(tmpfile, "%s\n", content);
101 if (len != strlen(content) + 1) {
102 err = got_error_from_errno2("fprintf", tmppath);
103 goto done;
107 if (rename(tmppath, path) != 0) {
108 err = got_error_from_errno3("rename", tmppath, path);
109 unlink(tmppath);
110 goto done;
113 done:
114 if (fclose(tmpfile) == EOF && err == NULL)
115 err = got_error_from_errno2("fclose", tmppath);
116 free(tmppath);
117 return err;
120 static const struct got_error *
121 write_head_ref(const char *path_got, struct got_reference *head_ref)
123 const struct got_error *err = NULL;
124 char *refstr = NULL;
126 if (got_ref_is_symbolic(head_ref)) {
127 refstr = got_ref_to_str(head_ref);
128 if (refstr == NULL)
129 return got_error_from_errno("got_ref_to_str");
130 } else {
131 refstr = strdup(got_ref_get_name(head_ref));
132 if (refstr == NULL)
133 return got_error_from_errno("strdup");
135 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
136 free(refstr);
137 return err;
140 const struct got_error *
141 got_worktree_init(const char *path, struct got_reference *head_ref,
142 const char *prefix, struct got_repository *repo)
144 const struct got_error *err = NULL;
145 struct got_object_id *commit_id = NULL;
146 uuid_t uuid;
147 uint32_t uuid_status;
148 int obj_type;
149 char *path_got = NULL;
150 char *formatstr = NULL;
151 char *absprefix = NULL;
152 char *basestr = NULL;
153 char *uuidstr = NULL;
155 if (strcmp(path, got_repo_get_path(repo)) == 0) {
156 err = got_error(GOT_ERR_WORKTREE_REPO);
157 goto done;
160 err = got_ref_resolve(&commit_id, repo, head_ref);
161 if (err)
162 return err;
163 err = got_object_get_type(&obj_type, repo, commit_id);
164 if (err)
165 return err;
166 if (obj_type != GOT_OBJ_TYPE_COMMIT)
167 return got_error(GOT_ERR_OBJ_TYPE);
169 if (!got_path_is_absolute(prefix)) {
170 if (asprintf(&absprefix, "/%s", prefix) == -1)
171 return got_error_from_errno("asprintf");
174 /* Create top-level directory (may already exist). */
175 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
176 err = got_error_from_errno2("mkdir", path);
177 goto done;
180 /* Create .got directory (may already exist). */
181 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
182 err = got_error_from_errno("asprintf");
183 goto done;
185 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
186 err = got_error_from_errno2("mkdir", path_got);
187 goto done;
190 /* Create an empty lock file. */
191 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
192 if (err)
193 goto done;
195 /* Create an empty file index. */
196 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
197 if (err)
198 goto done;
200 /* Write the HEAD reference. */
201 err = write_head_ref(path_got, head_ref);
202 if (err)
203 goto done;
205 /* Record our base commit. */
206 err = got_object_id_str(&basestr, commit_id);
207 if (err)
208 goto done;
209 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
210 if (err)
211 goto done;
213 /* Store path to repository. */
214 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
215 got_repo_get_path(repo));
216 if (err)
217 goto done;
219 /* Store in-repository path prefix. */
220 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
221 absprefix ? absprefix : prefix);
222 if (err)
223 goto done;
225 /* Generate UUID. */
226 uuid_create(&uuid, &uuid_status);
227 if (uuid_status != uuid_s_ok) {
228 err = got_error_uuid(uuid_status, "uuid_create");
229 goto done;
231 uuid_to_string(&uuid, &uuidstr, &uuid_status);
232 if (uuid_status != uuid_s_ok) {
233 err = got_error_uuid(uuid_status, "uuid_to_string");
234 goto done;
236 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
237 if (err)
238 goto done;
240 /* Stamp work tree with format file. */
241 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
242 err = got_error_from_errno("asprintf");
243 goto done;
245 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
246 if (err)
247 goto done;
249 done:
250 free(commit_id);
251 free(path_got);
252 free(formatstr);
253 free(absprefix);
254 free(basestr);
255 free(uuidstr);
256 return err;
259 const struct got_error *
260 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
261 const char *path_prefix)
263 char *absprefix = NULL;
265 if (!got_path_is_absolute(path_prefix)) {
266 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
267 return got_error_from_errno("asprintf");
269 *match = (strcmp(absprefix ? absprefix : path_prefix,
270 worktree->path_prefix) == 0);
271 free(absprefix);
272 return NULL;
275 const char *
276 got_worktree_get_head_ref_name(struct got_worktree *worktree)
278 return worktree->head_ref_name;
281 const struct got_error *
282 got_worktree_set_head_ref(struct got_worktree *worktree,
283 struct got_reference *head_ref)
285 const struct got_error *err = NULL;
286 char *path_got = NULL, *head_ref_name = NULL;
288 if (asprintf(&path_got, "%s/%s", worktree->root_path,
289 GOT_WORKTREE_GOT_DIR) == -1) {
290 err = got_error_from_errno("asprintf");
291 path_got = NULL;
292 goto done;
295 head_ref_name = strdup(got_ref_get_name(head_ref));
296 if (head_ref_name == NULL) {
297 err = got_error_from_errno("strdup");
298 goto done;
301 err = write_head_ref(path_got, head_ref);
302 if (err)
303 goto done;
305 free(worktree->head_ref_name);
306 worktree->head_ref_name = head_ref_name;
307 done:
308 free(path_got);
309 if (err)
310 free(head_ref_name);
311 return err;
314 struct got_object_id *
315 got_worktree_get_base_commit_id(struct got_worktree *worktree)
317 return worktree->base_commit_id;
320 const struct got_error *
321 got_worktree_set_base_commit_id(struct got_worktree *worktree,
322 struct got_repository *repo, struct got_object_id *commit_id)
324 const struct got_error *err;
325 struct got_object *obj = NULL;
326 char *id_str = NULL;
327 char *path_got = NULL;
329 if (asprintf(&path_got, "%s/%s", worktree->root_path,
330 GOT_WORKTREE_GOT_DIR) == -1) {
331 err = got_error_from_errno("asprintf");
332 path_got = NULL;
333 goto done;
336 err = got_object_open(&obj, repo, commit_id);
337 if (err)
338 return err;
340 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
341 err = got_error(GOT_ERR_OBJ_TYPE);
342 goto done;
345 /* Record our base commit. */
346 err = got_object_id_str(&id_str, commit_id);
347 if (err)
348 goto done;
349 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
350 if (err)
351 goto done;
353 free(worktree->base_commit_id);
354 worktree->base_commit_id = got_object_id_dup(commit_id);
355 if (worktree->base_commit_id == NULL) {
356 err = got_error_from_errno("got_object_id_dup");
357 goto done;
359 done:
360 if (obj)
361 got_object_close(obj);
362 free(id_str);
363 free(path_got);
364 return err;
367 const struct got_gotconfig *
368 got_worktree_get_gotconfig(struct got_worktree *worktree)
370 return worktree->gotconfig;
373 static const struct got_error *
374 lock_worktree(struct got_worktree *worktree, int operation)
376 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
377 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
378 : got_error_from_errno2("flock",
379 got_worktree_get_root_path(worktree)));
380 return NULL;
383 static const struct got_error *
384 add_dir_on_disk(struct got_worktree *worktree, const char *path)
386 const struct got_error *err = NULL;
387 char *abspath;
389 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
390 return got_error_from_errno("asprintf");
392 err = got_path_mkdir(abspath);
393 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
394 struct stat sb;
395 err = NULL;
396 if (lstat(abspath, &sb) == -1) {
397 err = got_error_from_errno2("lstat", abspath);
398 } else if (!S_ISDIR(sb.st_mode)) {
399 /* TODO directory is obstructed; do something */
400 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
403 free(abspath);
404 return err;
407 static const struct got_error *
408 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
410 const struct got_error *err = NULL;
411 uint8_t fbuf1[8192];
412 uint8_t fbuf2[8192];
413 size_t flen1 = 0, flen2 = 0;
415 *same = 1;
417 for (;;) {
418 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
419 if (flen1 == 0 && ferror(f1)) {
420 err = got_error_from_errno("fread");
421 break;
423 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
424 if (flen2 == 0 && ferror(f2)) {
425 err = got_error_from_errno("fread");
426 break;
428 if (flen1 == 0) {
429 if (flen2 != 0)
430 *same = 0;
431 break;
432 } else if (flen2 == 0) {
433 if (flen1 != 0)
434 *same = 0;
435 break;
436 } else if (flen1 == flen2) {
437 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
438 *same = 0;
439 break;
441 } else {
442 *same = 0;
443 break;
447 return err;
450 static const struct got_error *
451 check_files_equal(int *same, FILE *f1, FILE *f2)
453 struct stat sb;
454 size_t size1, size2;
456 *same = 1;
458 if (fstat(fileno(f1), &sb) != 0)
459 return got_error_from_errno("fstat");
460 size1 = sb.st_size;
462 if (fstat(fileno(f2), &sb) != 0)
463 return got_error_from_errno("fstat");
464 size2 = sb.st_size;
466 if (size1 != size2) {
467 *same = 0;
468 return NULL;
471 if (fseek(f1, 0L, SEEK_SET) == -1)
472 return got_ferror(f1, GOT_ERR_IO);
473 if (fseek(f2, 0L, SEEK_SET) == -1)
474 return got_ferror(f2, GOT_ERR_IO);
476 return check_file_contents_equal(same, f1, f2);
479 static const struct got_error *
480 copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
482 uint8_t fbuf[65536];
483 size_t flen;
484 ssize_t outlen;
486 *outsize = 0;
488 if (fseek(f, 0L, SEEK_SET) == -1)
489 return got_ferror(f, GOT_ERR_IO);
491 for (;;) {
492 flen = fread(fbuf, 1, sizeof(fbuf), f);
493 if (flen == 0) {
494 if (ferror(f))
495 return got_error_from_errno("fread");
496 if (feof(f))
497 break;
499 outlen = write(outfd, fbuf, flen);
500 if (outlen == -1)
501 return got_error_from_errno("write");
502 if (outlen != flen)
503 return got_error(GOT_ERR_IO);
504 *outsize += outlen;
507 return NULL;
510 static const struct got_error *
511 merge_binary_file(int *overlapcnt, int merged_fd,
512 FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
513 const char *label_deriv, const char *label_orig, const char *label_deriv2,
514 const char *ondisk_path)
516 const struct got_error *err = NULL;
517 int same_content, changed_deriv, changed_deriv2;
518 int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
519 off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
520 char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
521 char *base_path_orig = NULL, *base_path_deriv = NULL;
522 char *base_path_deriv2 = NULL;
524 *overlapcnt = 0;
526 err = check_files_equal(&same_content, f_deriv, f_deriv2);
527 if (err)
528 return err;
530 if (same_content)
531 return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
533 err = check_files_equal(&same_content, f_deriv, f_orig);
534 if (err)
535 return err;
536 changed_deriv = !same_content;
537 err = check_files_equal(&same_content, f_deriv2, f_orig);
538 if (err)
539 return err;
540 changed_deriv2 = !same_content;
542 if (changed_deriv && changed_deriv2) {
543 *overlapcnt = 1;
544 if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
545 err = got_error_from_errno("asprintf");
546 goto done;
548 if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
549 err = got_error_from_errno("asprintf");
550 goto done;
552 if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
553 err = got_error_from_errno("asprintf");
554 goto done;
556 err = got_opentemp_named_fd(&path_orig, &fd_orig,
557 base_path_orig);
558 if (err)
559 goto done;
560 err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
561 base_path_deriv);
562 if (err)
563 goto done;
564 err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
565 base_path_deriv2);
566 if (err)
567 goto done;
568 err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
569 if (err)
570 goto done;
571 err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
572 if (err)
573 goto done;
574 err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
575 if (err)
576 goto done;
577 if (dprintf(merged_fd, "Binary files differ and cannot be "
578 "merged automatically:\n") < 0) {
579 err = got_error_from_errno("dprintf");
580 goto done;
582 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
583 GOT_DIFF_CONFLICT_MARKER_BEGIN,
584 label_deriv ? " " : "",
585 label_deriv ? label_deriv : "",
586 path_deriv) < 0) {
587 err = got_error_from_errno("dprintf");
588 goto done;
590 if (size_orig > 0) {
591 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
592 GOT_DIFF_CONFLICT_MARKER_ORIG,
593 label_orig ? " " : "",
594 label_orig ? label_orig : "",
595 path_orig) < 0) {
596 err = got_error_from_errno("dprintf");
597 goto done;
600 if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
601 GOT_DIFF_CONFLICT_MARKER_SEP,
602 path_deriv2,
603 GOT_DIFF_CONFLICT_MARKER_END,
604 label_deriv2 ? " " : "",
605 label_deriv2 ? label_deriv2 : "") < 0) {
606 err = got_error_from_errno("dprintf");
607 goto done;
609 } else if (changed_deriv)
610 err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
611 else if (changed_deriv2)
612 err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
613 done:
614 if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
615 err == NULL)
616 err = got_error_from_errno2("unlink", path_orig);
617 if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
618 err = got_error_from_errno2("close", path_orig);
619 if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
620 err = got_error_from_errno2("close", path_deriv);
621 if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
622 err = got_error_from_errno2("close", path_deriv2);
623 free(path_orig);
624 free(path_deriv);
625 free(path_deriv2);
626 free(base_path_orig);
627 free(base_path_deriv);
628 free(base_path_deriv2);
629 return err;
632 /*
633 * Perform a 3-way merge where the file f_orig acts as the common
634 * ancestor, the file f_deriv acts as the first derived version,
635 * and the file f_deriv2 acts as the second derived version.
636 * The merge result will be written to a new file at ondisk_path; any
637 * existing file at this path will be replaced.
638 */
639 static const struct got_error *
640 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
641 FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
642 const char *path, uint16_t st_mode,
643 const char *label_orig, const char *label_deriv, const char *label_deriv2,
644 enum got_diff_algorithm diff_algo, struct got_repository *repo,
645 got_worktree_checkout_cb progress_cb, void *progress_arg)
647 const struct got_error *err = NULL;
648 int merged_fd = -1;
649 FILE *f_merged = NULL;
650 char *merged_path = NULL, *base_path = NULL;
651 int overlapcnt = 0;
652 char *parent = NULL;
654 *local_changes_subsumed = 0;
656 err = got_path_dirname(&parent, ondisk_path);
657 if (err)
658 return err;
660 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
661 err = got_error_from_errno("asprintf");
662 goto done;
665 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
666 if (err)
667 goto done;
669 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
670 f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
671 if (err) {
672 if (err->code != GOT_ERR_FILE_BINARY)
673 goto done;
674 err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
675 f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
676 ondisk_path);
677 if (err)
678 goto done;
681 err = (*progress_cb)(progress_arg,
682 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
683 if (err)
684 goto done;
686 if (fsync(merged_fd) != 0) {
687 err = got_error_from_errno("fsync");
688 goto done;
691 f_merged = fdopen(merged_fd, "r");
692 if (f_merged == NULL) {
693 err = got_error_from_errno("fdopen");
694 goto done;
696 merged_fd = -1;
698 /* Check if a clean merge has subsumed all local changes. */
699 if (overlapcnt == 0) {
700 err = check_files_equal(local_changes_subsumed, f_deriv,
701 f_merged);
702 if (err)
703 goto done;
706 if (fchmod(fileno(f_merged), st_mode) != 0) {
707 err = got_error_from_errno2("fchmod", merged_path);
708 goto done;
711 if (rename(merged_path, ondisk_path) != 0) {
712 err = got_error_from_errno3("rename", merged_path,
713 ondisk_path);
714 goto done;
716 done:
717 if (err) {
718 if (merged_path)
719 unlink(merged_path);
721 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
722 err = got_error_from_errno("close");
723 if (f_merged && fclose(f_merged) == EOF && err == NULL)
724 err = got_error_from_errno("fclose");
725 free(merged_path);
726 free(base_path);
727 free(parent);
728 return err;
731 static const struct got_error *
732 update_symlink(const char *ondisk_path, const char *target_path,
733 size_t target_len)
735 /* This is not atomic but matches what 'ln -sf' does. */
736 if (unlink(ondisk_path) == -1)
737 return got_error_from_errno2("unlink", ondisk_path);
738 if (symlink(target_path, ondisk_path) == -1)
739 return got_error_from_errno3("symlink", target_path,
740 ondisk_path);
741 return NULL;
744 /*
745 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
746 * in the work tree with a file that contains conflict markers and the
747 * conflicting target paths of the original version, a "derived version"
748 * of a symlink from an incoming change, and a local version of the symlink.
750 * The original versions's target path can be NULL if it is not available,
751 * such as if both derived versions added a new symlink at the same path.
753 * The incoming derived symlink target is NULL in case the incoming change
754 * has deleted this symlink.
755 */
756 static const struct got_error *
757 install_symlink_conflict(const char *deriv_target,
758 struct got_object_id *deriv_base_commit_id, const char *orig_target,
759 const char *label_orig, const char *local_target, const char *ondisk_path)
761 const struct got_error *err;
762 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
763 FILE *f = NULL;
765 err = got_object_id_str(&id_str, deriv_base_commit_id);
766 if (err)
767 return got_error_from_errno("asprintf");
769 if (asprintf(&label_deriv, "%s: commit %s",
770 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
771 err = got_error_from_errno("asprintf");
772 goto done;
775 err = got_opentemp_named(&path, &f, "got-symlink-conflict");
776 if (err)
777 goto done;
779 if (fchmod(fileno(f), GOT_DEFAULT_FILE_MODE) == -1) {
780 err = got_error_from_errno2("fchmod", path);
781 goto done;
784 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
785 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
786 deriv_target ? deriv_target : "(symlink was deleted)",
787 orig_target ? label_orig : "",
788 orig_target ? "\n" : "",
789 orig_target ? orig_target : "",
790 orig_target ? "\n" : "",
791 GOT_DIFF_CONFLICT_MARKER_SEP,
792 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
793 err = got_error_from_errno2("fprintf", path);
794 goto done;
797 if (unlink(ondisk_path) == -1) {
798 err = got_error_from_errno2("unlink", ondisk_path);
799 goto done;
801 if (rename(path, ondisk_path) == -1) {
802 err = got_error_from_errno3("rename", path, ondisk_path);
803 goto done;
805 done:
806 if (f != NULL && fclose(f) == EOF && err == NULL)
807 err = got_error_from_errno2("fclose", path);
808 free(path);
809 free(id_str);
810 free(label_deriv);
811 return err;
814 /* forward declaration */
815 static const struct got_error *
816 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
817 const char *, const char *, uint16_t, const char *,
818 struct got_blob_object *, struct got_object_id *,
819 struct got_repository *, got_worktree_checkout_cb, void *);
821 /*
822 * Merge a symlink into the work tree, where blob_orig acts as the common
823 * ancestor, deriv_target is the link target of the first derived version,
824 * and the symlink on disk acts as the second derived version.
825 * Assume that contents of both blobs represent symlinks.
826 */
827 static const struct got_error *
828 merge_symlink(struct got_worktree *worktree,
829 struct got_blob_object *blob_orig, const char *ondisk_path,
830 const char *path, const char *label_orig, const char *deriv_target,
831 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
832 got_worktree_checkout_cb progress_cb, void *progress_arg)
834 const struct got_error *err = NULL;
835 char *ancestor_target = NULL;
836 struct stat sb;
837 ssize_t ondisk_len, deriv_len;
838 char ondisk_target[PATH_MAX];
839 int have_local_change = 0;
840 int have_incoming_change = 0;
842 if (lstat(ondisk_path, &sb) == -1)
843 return got_error_from_errno2("lstat", ondisk_path);
845 ondisk_len = readlink(ondisk_path, ondisk_target,
846 sizeof(ondisk_target));
847 if (ondisk_len == -1) {
848 err = got_error_from_errno2("readlink",
849 ondisk_path);
850 goto done;
852 ondisk_target[ondisk_len] = '\0';
854 if (blob_orig) {
855 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
856 if (err)
857 goto done;
860 if (ancestor_target == NULL ||
861 (ondisk_len != strlen(ancestor_target) ||
862 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
863 have_local_change = 1;
865 deriv_len = strlen(deriv_target);
866 if (ancestor_target == NULL ||
867 (deriv_len != strlen(ancestor_target) ||
868 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
869 have_incoming_change = 1;
871 if (!have_local_change && !have_incoming_change) {
872 if (ancestor_target) {
873 /* Both sides made the same change. */
874 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
875 path);
876 } else if (deriv_len == ondisk_len &&
877 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
878 /* Both sides added the same symlink. */
879 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
880 path);
881 } else {
882 /* Both sides added symlinks which don't match. */
883 err = install_symlink_conflict(deriv_target,
884 deriv_base_commit_id, ancestor_target,
885 label_orig, ondisk_target, ondisk_path);
886 if (err)
887 goto done;
888 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
889 path);
891 } else if (!have_local_change && have_incoming_change) {
892 /* Apply the incoming change. */
893 err = update_symlink(ondisk_path, deriv_target,
894 strlen(deriv_target));
895 if (err)
896 goto done;
897 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
898 } else if (have_local_change && have_incoming_change) {
899 if (deriv_len == ondisk_len &&
900 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
901 /* Both sides made the same change. */
902 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
903 path);
904 } else {
905 err = install_symlink_conflict(deriv_target,
906 deriv_base_commit_id, ancestor_target, label_orig,
907 ondisk_target, ondisk_path);
908 if (err)
909 goto done;
910 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
911 path);
915 done:
916 free(ancestor_target);
917 return err;
920 static const struct got_error *
921 dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
923 const struct got_error *err = NULL;
924 char target_path[PATH_MAX];
925 ssize_t target_len;
926 size_t n;
927 FILE *f;
929 *outfile = NULL;
931 f = got_opentemp();
932 if (f == NULL)
933 return got_error_from_errno("got_opentemp");
934 target_len = readlink(ondisk_path, target_path, sizeof(target_path));
935 if (target_len == -1) {
936 err = got_error_from_errno2("readlink", ondisk_path);
937 goto done;
939 n = fwrite(target_path, 1, target_len, f);
940 if (n != target_len) {
941 err = got_ferror(f, GOT_ERR_IO);
942 goto done;
944 if (fflush(f) == EOF) {
945 err = got_error_from_errno("fflush");
946 goto done;
948 if (fseek(f, 0L, SEEK_SET) == -1) {
949 err = got_ferror(f, GOT_ERR_IO);
950 goto done;
952 done:
953 if (err)
954 fclose(f);
955 else
956 *outfile = f;
957 return err;
960 /*
961 * Perform a 3-way merge where blob_orig acts as the common ancestor,
962 * blob_deriv acts as the first derived version, and the file on disk
963 * acts as the second derived version.
964 */
965 static const struct got_error *
966 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
967 struct got_blob_object *blob_orig, const char *ondisk_path,
968 const char *path, uint16_t st_mode, const char *label_orig,
969 struct got_blob_object *blob_deriv,
970 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
971 got_worktree_checkout_cb progress_cb, void *progress_arg)
973 const struct got_error *err = NULL;
974 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
975 char *blob_orig_path = NULL;
976 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
977 char *label_deriv = NULL, *parent = NULL;
979 *local_changes_subsumed = 0;
981 err = got_path_dirname(&parent, ondisk_path);
982 if (err)
983 return err;
985 if (blob_orig) {
986 if (asprintf(&base_path, "%s/got-merge-blob-orig",
987 parent) == -1) {
988 err = got_error_from_errno("asprintf");
989 base_path = NULL;
990 goto done;
993 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
994 if (err)
995 goto done;
996 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
997 blob_orig);
998 if (err)
999 goto done;
1000 free(base_path);
1001 } else {
1003 * No common ancestor exists. This is an "add vs add" conflict
1004 * and we simply use an empty ancestor file to make both files
1005 * appear in the merged result in their entirety.
1007 f_orig = got_opentemp();
1008 if (f_orig == NULL) {
1009 err = got_error_from_errno("got_opentemp");
1010 goto done;
1014 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1015 err = got_error_from_errno("asprintf");
1016 base_path = NULL;
1017 goto done;
1020 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1021 if (err)
1022 goto done;
1023 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1024 blob_deriv);
1025 if (err)
1026 goto done;
1028 err = got_object_id_str(&id_str, deriv_base_commit_id);
1029 if (err)
1030 goto done;
1031 if (asprintf(&label_deriv, "%s: commit %s",
1032 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1033 err = got_error_from_errno("asprintf");
1034 goto done;
1038 * In order the run a 3-way merge with a symlink we copy the symlink's
1039 * target path into a temporary file and use that file with diff3.
1041 if (S_ISLNK(st_mode)) {
1042 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1043 if (err)
1044 goto done;
1045 } else {
1046 int fd;
1047 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1048 if (fd == -1) {
1049 err = got_error_from_errno2("open", ondisk_path);
1050 goto done;
1052 f_deriv2 = fdopen(fd, "r");
1053 if (f_deriv2 == NULL) {
1054 err = got_error_from_errno2("fdopen", ondisk_path);
1055 close(fd);
1056 goto done;
1060 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1061 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1062 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1063 done:
1064 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1065 err = got_error_from_errno("fclose");
1066 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1067 err = got_error_from_errno("fclose");
1068 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1069 err = got_error_from_errno("fclose");
1070 free(base_path);
1071 if (blob_orig_path) {
1072 unlink(blob_orig_path);
1073 free(blob_orig_path);
1075 if (blob_deriv_path) {
1076 unlink(blob_deriv_path);
1077 free(blob_deriv_path);
1079 free(id_str);
1080 free(label_deriv);
1081 free(parent);
1082 return err;
1085 static const struct got_error *
1086 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1087 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1088 int wt_fd, const char *path, struct got_object_id *blob_id)
1090 const struct got_error *err = NULL;
1091 struct got_fileindex_entry *new_ie;
1093 *new_iep = NULL;
1095 err = got_fileindex_entry_alloc(&new_ie, path);
1096 if (err)
1097 return err;
1099 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1100 blob_id->sha1, base_commit_id->sha1, 1);
1101 if (err)
1102 goto done;
1104 err = got_fileindex_entry_add(fileindex, new_ie);
1105 done:
1106 if (err)
1107 got_fileindex_entry_free(new_ie);
1108 else
1109 *new_iep = new_ie;
1110 return err;
1113 static mode_t
1114 get_ondisk_perms(int executable, mode_t st_mode)
1116 mode_t xbits = S_IXUSR;
1118 if (executable) {
1119 /* Map read bits to execute bits. */
1120 if (st_mode & S_IRGRP)
1121 xbits |= S_IXGRP;
1122 if (st_mode & S_IROTH)
1123 xbits |= S_IXOTH;
1124 return st_mode | xbits;
1127 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1130 /* forward declaration */
1131 static const struct got_error *
1132 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1133 const char *path, mode_t te_mode, mode_t st_mode,
1134 struct got_blob_object *blob, int restoring_missing_file,
1135 int reverting_versioned_file, int installing_bad_symlink,
1136 int path_is_unversioned, struct got_repository *repo,
1137 got_worktree_checkout_cb progress_cb, void *progress_arg);
1140 * This function assumes that the provided symlink target points at a
1141 * safe location in the work tree!
1143 static const struct got_error *
1144 replace_existing_symlink(int *did_something, const char *ondisk_path,
1145 const char *target_path, size_t target_len)
1147 const struct got_error *err = NULL;
1148 ssize_t elen;
1149 char etarget[PATH_MAX];
1150 int fd;
1152 *did_something = 0;
1155 * "Bad" symlinks (those pointing outside the work tree or into the
1156 * .got directory) are installed in the work tree as a regular file
1157 * which contains the bad symlink target path.
1158 * The new symlink target has already been checked for safety by our
1159 * caller. If we can successfully open a regular file then we simply
1160 * replace this file with a symlink below.
1162 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1163 if (fd == -1) {
1164 if (!got_err_open_nofollow_on_symlink())
1165 return got_error_from_errno2("open", ondisk_path);
1167 /* We are updating an existing on-disk symlink. */
1168 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1169 if (elen == -1)
1170 return got_error_from_errno2("readlink", ondisk_path);
1172 if (elen == target_len &&
1173 memcmp(etarget, target_path, target_len) == 0)
1174 return NULL; /* nothing to do */
1177 *did_something = 1;
1178 err = update_symlink(ondisk_path, target_path, target_len);
1179 if (fd != -1 && close(fd) == -1 && err == NULL)
1180 err = got_error_from_errno2("close", ondisk_path);
1181 return err;
1184 static const struct got_error *
1185 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1186 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1188 const struct got_error *err = NULL;
1189 char canonpath[PATH_MAX];
1190 char *path_got = NULL;
1192 *is_bad_symlink = 0;
1194 if (target_len >= sizeof(canonpath)) {
1195 *is_bad_symlink = 1;
1196 return NULL;
1200 * We do not use realpath(3) to resolve the symlink's target
1201 * path because we don't want to resolve symlinks recursively.
1202 * Instead we make the path absolute and then canonicalize it.
1203 * Relative symlink target lookup should begin at the directory
1204 * in which the blob object is being installed.
1206 if (!got_path_is_absolute(target_path)) {
1207 char *abspath, *parent;
1208 err = got_path_dirname(&parent, ondisk_path);
1209 if (err)
1210 return err;
1211 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1212 free(parent);
1213 return got_error_from_errno("asprintf");
1215 free(parent);
1216 if (strlen(abspath) >= sizeof(canonpath)) {
1217 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1218 free(abspath);
1219 return err;
1221 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1222 free(abspath);
1223 if (err)
1224 return err;
1225 } else {
1226 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1227 if (err)
1228 return err;
1231 /* Only allow symlinks pointing at paths within the work tree. */
1232 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1233 *is_bad_symlink = 1;
1234 return NULL;
1237 /* Do not allow symlinks pointing into the .got directory. */
1238 if (asprintf(&path_got, "%s/%s", wtroot_path,
1239 GOT_WORKTREE_GOT_DIR) == -1)
1240 return got_error_from_errno("asprintf");
1241 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1242 *is_bad_symlink = 1;
1244 free(path_got);
1245 return NULL;
1248 static const struct got_error *
1249 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1250 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1251 int restoring_missing_file, int reverting_versioned_file,
1252 int path_is_unversioned, int allow_bad_symlinks,
1253 struct got_repository *repo,
1254 got_worktree_checkout_cb progress_cb, void *progress_arg)
1256 const struct got_error *err = NULL;
1257 char target_path[PATH_MAX];
1258 size_t len, target_len = 0;
1259 char *path_got = NULL;
1260 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1261 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1263 *is_bad_symlink = 0;
1266 * Blob object content specifies the target path of the link.
1267 * If a symbolic link cannot be installed we instead create
1268 * a regular file which contains the link target path stored
1269 * in the blob object.
1271 do {
1272 err = got_object_blob_read_block(&len, blob);
1273 if (len + target_len >= sizeof(target_path)) {
1274 /* Path too long; install as a regular file. */
1275 *is_bad_symlink = 1;
1276 got_object_blob_rewind(blob);
1277 return install_blob(worktree, ondisk_path, path,
1278 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1279 restoring_missing_file, reverting_versioned_file,
1280 1, path_is_unversioned, repo, progress_cb,
1281 progress_arg);
1283 if (len > 0) {
1284 /* Skip blob object header first time around. */
1285 memcpy(target_path + target_len, buf + hdrlen,
1286 len - hdrlen);
1287 target_len += len - hdrlen;
1288 hdrlen = 0;
1290 } while (len != 0);
1291 target_path[target_len] = '\0';
1293 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1294 ondisk_path, worktree->root_path);
1295 if (err)
1296 return err;
1298 if (*is_bad_symlink && !allow_bad_symlinks) {
1299 /* install as a regular file */
1300 got_object_blob_rewind(blob);
1301 err = install_blob(worktree, ondisk_path, path,
1302 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1303 restoring_missing_file, reverting_versioned_file, 1,
1304 path_is_unversioned, repo, progress_cb, progress_arg);
1305 goto done;
1308 if (symlink(target_path, ondisk_path) == -1) {
1309 if (errno == EEXIST) {
1310 int symlink_replaced;
1311 if (path_is_unversioned) {
1312 err = (*progress_cb)(progress_arg,
1313 GOT_STATUS_UNVERSIONED, path);
1314 goto done;
1316 err = replace_existing_symlink(&symlink_replaced,
1317 ondisk_path, target_path, target_len);
1318 if (err)
1319 goto done;
1320 if (progress_cb) {
1321 if (symlink_replaced) {
1322 err = (*progress_cb)(progress_arg,
1323 reverting_versioned_file ?
1324 GOT_STATUS_REVERT :
1325 GOT_STATUS_UPDATE, path);
1326 } else {
1327 err = (*progress_cb)(progress_arg,
1328 GOT_STATUS_EXISTS, path);
1331 goto done; /* Nothing else to do. */
1334 if (errno == ENOENT) {
1335 char *parent;
1336 err = got_path_dirname(&parent, ondisk_path);
1337 if (err)
1338 goto done;
1339 err = add_dir_on_disk(worktree, parent);
1340 free(parent);
1341 if (err)
1342 goto done;
1344 * Retry, and fall through to error handling
1345 * below if this second attempt fails.
1347 if (symlink(target_path, ondisk_path) != -1) {
1348 err = NULL; /* success */
1349 goto done;
1353 /* Handle errors from first or second creation attempt. */
1354 if (errno == ENAMETOOLONG) {
1355 /* bad target path; install as a regular file */
1356 *is_bad_symlink = 1;
1357 got_object_blob_rewind(blob);
1358 err = install_blob(worktree, ondisk_path, path,
1359 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1360 restoring_missing_file, reverting_versioned_file, 1,
1361 path_is_unversioned, repo,
1362 progress_cb, progress_arg);
1363 } else if (errno == ENOTDIR) {
1364 err = got_error_path(ondisk_path,
1365 GOT_ERR_FILE_OBSTRUCTED);
1366 } else {
1367 err = got_error_from_errno3("symlink",
1368 target_path, ondisk_path);
1370 } else if (progress_cb)
1371 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1372 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1373 done:
1374 free(path_got);
1375 return err;
1378 static const struct got_error *
1379 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1380 const char *path, mode_t te_mode, mode_t st_mode,
1381 struct got_blob_object *blob, int restoring_missing_file,
1382 int reverting_versioned_file, int installing_bad_symlink,
1383 int path_is_unversioned, struct got_repository *repo,
1384 got_worktree_checkout_cb progress_cb, void *progress_arg)
1386 const struct got_error *err = NULL;
1387 int fd = -1;
1388 size_t len, hdrlen;
1389 int update = 0;
1390 char *tmppath = NULL;
1392 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1393 O_CLOEXEC, GOT_DEFAULT_FILE_MODE);
1394 if (fd == -1) {
1395 if (errno == ENOENT) {
1396 char *parent;
1397 err = got_path_dirname(&parent, path);
1398 if (err)
1399 return err;
1400 err = add_dir_on_disk(worktree, parent);
1401 free(parent);
1402 if (err)
1403 return err;
1404 fd = open(ondisk_path,
1405 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1406 GOT_DEFAULT_FILE_MODE);
1407 if (fd == -1)
1408 return got_error_from_errno2("open",
1409 ondisk_path);
1410 } else if (errno == EEXIST) {
1411 if (path_is_unversioned) {
1412 err = (*progress_cb)(progress_arg,
1413 GOT_STATUS_UNVERSIONED, path);
1414 goto done;
1416 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1417 !S_ISREG(st_mode) && !installing_bad_symlink) {
1418 /* TODO file is obstructed; do something */
1419 err = got_error_path(ondisk_path,
1420 GOT_ERR_FILE_OBSTRUCTED);
1421 goto done;
1422 } else {
1423 err = got_opentemp_named_fd(&tmppath, &fd,
1424 ondisk_path);
1425 if (err)
1426 goto done;
1427 update = 1;
1429 } else
1430 return got_error_from_errno2("open", ondisk_path);
1433 if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1434 err = got_error_from_errno2("fchmod",
1435 update ? tmppath : ondisk_path);
1436 goto done;
1439 if (progress_cb) {
1440 if (restoring_missing_file)
1441 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1442 path);
1443 else if (reverting_versioned_file)
1444 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1445 path);
1446 else
1447 err = (*progress_cb)(progress_arg,
1448 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1449 if (err)
1450 goto done;
1453 hdrlen = got_object_blob_get_hdrlen(blob);
1454 do {
1455 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1456 err = got_object_blob_read_block(&len, blob);
1457 if (err)
1458 break;
1459 if (len > 0) {
1460 /* Skip blob object header first time around. */
1461 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1462 if (outlen == -1) {
1463 err = got_error_from_errno("write");
1464 goto done;
1465 } else if (outlen != len - hdrlen) {
1466 err = got_error(GOT_ERR_IO);
1467 goto done;
1469 hdrlen = 0;
1471 } while (len != 0);
1473 if (fsync(fd) != 0) {
1474 err = got_error_from_errno("fsync");
1475 goto done;
1478 if (update) {
1479 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1480 err = got_error_from_errno2("unlink", ondisk_path);
1481 goto done;
1483 if (rename(tmppath, ondisk_path) != 0) {
1484 err = got_error_from_errno3("rename", tmppath,
1485 ondisk_path);
1486 goto done;
1488 free(tmppath);
1489 tmppath = NULL;
1492 done:
1493 if (fd != -1 && close(fd) == -1 && err == NULL)
1494 err = got_error_from_errno("close");
1495 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1496 err = got_error_from_errno2("unlink", tmppath);
1497 free(tmppath);
1498 return err;
1501 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1502 static const struct got_error *
1503 get_modified_file_content_status(unsigned char *status, FILE *f)
1505 const struct got_error *err = NULL;
1506 const char *markers[3] = {
1507 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1508 GOT_DIFF_CONFLICT_MARKER_SEP,
1509 GOT_DIFF_CONFLICT_MARKER_END
1511 int i = 0;
1512 char *line = NULL;
1513 size_t linesize = 0;
1514 ssize_t linelen;
1516 while (*status == GOT_STATUS_MODIFY) {
1517 linelen = getline(&line, &linesize, f);
1518 if (linelen == -1) {
1519 if (feof(f))
1520 break;
1521 err = got_ferror(f, GOT_ERR_IO);
1522 break;
1525 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1526 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1527 == 0)
1528 *status = GOT_STATUS_CONFLICT;
1529 else
1530 i++;
1533 free(line);
1535 return err;
1538 static int
1539 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1541 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1542 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1545 static int
1546 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1548 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1549 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1550 ie->mtime_sec == sb->st_mtim.tv_sec &&
1551 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1552 ie->size == (sb->st_size & 0xffffffff) &&
1553 !xbit_differs(ie, sb->st_mode));
1556 static unsigned char
1557 get_staged_status(struct got_fileindex_entry *ie)
1559 switch (got_fileindex_entry_stage_get(ie)) {
1560 case GOT_FILEIDX_STAGE_ADD:
1561 return GOT_STATUS_ADD;
1562 case GOT_FILEIDX_STAGE_DELETE:
1563 return GOT_STATUS_DELETE;
1564 case GOT_FILEIDX_STAGE_MODIFY:
1565 return GOT_STATUS_MODIFY;
1566 default:
1567 return GOT_STATUS_NO_CHANGE;
1571 static const struct got_error *
1572 get_symlink_modification_status(unsigned char *status,
1573 struct got_fileindex_entry *ie, const char *abspath,
1574 int dirfd, const char *de_name, struct got_blob_object *blob)
1576 const struct got_error *err = NULL;
1577 char target_path[PATH_MAX];
1578 char etarget[PATH_MAX];
1579 ssize_t elen;
1580 size_t len, target_len = 0;
1581 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1582 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1584 *status = GOT_STATUS_NO_CHANGE;
1586 /* Blob object content specifies the target path of the link. */
1587 do {
1588 err = got_object_blob_read_block(&len, blob);
1589 if (err)
1590 return err;
1591 if (len + target_len >= sizeof(target_path)) {
1593 * Should not happen. The blob contents were OK
1594 * when this symlink was installed.
1596 return got_error(GOT_ERR_NO_SPACE);
1598 if (len > 0) {
1599 /* Skip blob object header first time around. */
1600 memcpy(target_path + target_len, buf + hdrlen,
1601 len - hdrlen);
1602 target_len += len - hdrlen;
1603 hdrlen = 0;
1605 } while (len != 0);
1606 target_path[target_len] = '\0';
1608 if (dirfd != -1) {
1609 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1610 if (elen == -1)
1611 return got_error_from_errno2("readlinkat", abspath);
1612 } else {
1613 elen = readlink(abspath, etarget, sizeof(etarget));
1614 if (elen == -1)
1615 return got_error_from_errno2("readlink", abspath);
1618 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1619 *status = GOT_STATUS_MODIFY;
1621 return NULL;
1624 static const struct got_error *
1625 get_file_status(unsigned char *status, struct stat *sb,
1626 struct got_fileindex_entry *ie, const char *abspath,
1627 int dirfd, const char *de_name, struct got_repository *repo)
1629 const struct got_error *err = NULL;
1630 struct got_object_id id;
1631 size_t hdrlen;
1632 int fd = -1;
1633 FILE *f = NULL;
1634 uint8_t fbuf[8192];
1635 struct got_blob_object *blob = NULL;
1636 size_t flen, blen;
1637 unsigned char staged_status = get_staged_status(ie);
1639 *status = GOT_STATUS_NO_CHANGE;
1640 memset(sb, 0, sizeof(*sb));
1643 * Whenever the caller provides a directory descriptor and a
1644 * directory entry name for the file, use them! This prevents
1645 * race conditions if filesystem paths change beneath our feet.
1647 if (dirfd != -1) {
1648 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1649 if (errno == ENOENT) {
1650 if (got_fileindex_entry_has_file_on_disk(ie))
1651 *status = GOT_STATUS_MISSING;
1652 else
1653 *status = GOT_STATUS_DELETE;
1654 goto done;
1656 err = got_error_from_errno2("fstatat", abspath);
1657 goto done;
1659 } else {
1660 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1661 if (fd == -1 && errno != ENOENT &&
1662 !got_err_open_nofollow_on_symlink())
1663 return got_error_from_errno2("open", abspath);
1664 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1665 if (lstat(abspath, sb) == -1)
1666 return got_error_from_errno2("lstat", abspath);
1667 } else if (fd == -1 || fstat(fd, sb) == -1) {
1668 if (errno == ENOENT) {
1669 if (got_fileindex_entry_has_file_on_disk(ie))
1670 *status = GOT_STATUS_MISSING;
1671 else
1672 *status = GOT_STATUS_DELETE;
1673 goto done;
1675 err = got_error_from_errno2("fstat", abspath);
1676 goto done;
1680 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1681 *status = GOT_STATUS_OBSTRUCTED;
1682 goto done;
1685 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1686 *status = GOT_STATUS_DELETE;
1687 goto done;
1688 } else if (!got_fileindex_entry_has_blob(ie) &&
1689 staged_status != GOT_STATUS_ADD) {
1690 *status = GOT_STATUS_ADD;
1691 goto done;
1694 if (!stat_info_differs(ie, sb))
1695 goto done;
1697 if (S_ISLNK(sb->st_mode) &&
1698 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1699 *status = GOT_STATUS_MODIFY;
1700 goto done;
1703 if (staged_status == GOT_STATUS_MODIFY ||
1704 staged_status == GOT_STATUS_ADD)
1705 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1706 else
1707 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1709 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1710 if (err)
1711 goto done;
1713 if (S_ISLNK(sb->st_mode)) {
1714 err = get_symlink_modification_status(status, ie,
1715 abspath, dirfd, de_name, blob);
1716 goto done;
1719 if (dirfd != -1) {
1720 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1721 if (fd == -1) {
1722 err = got_error_from_errno2("openat", abspath);
1723 goto done;
1727 f = fdopen(fd, "r");
1728 if (f == NULL) {
1729 err = got_error_from_errno2("fdopen", abspath);
1730 goto done;
1732 fd = -1;
1733 hdrlen = got_object_blob_get_hdrlen(blob);
1734 for (;;) {
1735 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1736 err = got_object_blob_read_block(&blen, blob);
1737 if (err)
1738 goto done;
1739 /* Skip length of blob object header first time around. */
1740 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1741 if (flen == 0 && ferror(f)) {
1742 err = got_error_from_errno("fread");
1743 goto done;
1745 if (blen - hdrlen == 0) {
1746 if (flen != 0)
1747 *status = GOT_STATUS_MODIFY;
1748 break;
1749 } else if (flen == 0) {
1750 if (blen - hdrlen != 0)
1751 *status = GOT_STATUS_MODIFY;
1752 break;
1753 } else if (blen - hdrlen == flen) {
1754 /* Skip blob object header first time around. */
1755 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1756 *status = GOT_STATUS_MODIFY;
1757 break;
1759 } else {
1760 *status = GOT_STATUS_MODIFY;
1761 break;
1763 hdrlen = 0;
1766 if (*status == GOT_STATUS_MODIFY) {
1767 rewind(f);
1768 err = get_modified_file_content_status(status, f);
1769 } else if (xbit_differs(ie, sb->st_mode))
1770 *status = GOT_STATUS_MODE_CHANGE;
1771 done:
1772 if (blob)
1773 got_object_blob_close(blob);
1774 if (f != NULL && fclose(f) == EOF && err == NULL)
1775 err = got_error_from_errno2("fclose", abspath);
1776 if (fd != -1 && close(fd) == -1 && err == NULL)
1777 err = got_error_from_errno2("close", abspath);
1778 return err;
1782 * Update timestamps in the file index if a file is unmodified and
1783 * we had to run a full content comparison to find out.
1785 static const struct got_error *
1786 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1787 struct got_fileindex_entry *ie, struct stat *sb)
1789 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1790 return got_fileindex_entry_update(ie, wt_fd, path,
1791 ie->blob_sha1, ie->commit_sha1, 1);
1793 return NULL;
1796 static const struct got_error *
1797 update_blob(struct got_worktree *worktree,
1798 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1799 struct got_tree_entry *te, const char *path,
1800 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1801 void *progress_arg)
1803 const struct got_error *err = NULL;
1804 struct got_blob_object *blob = NULL;
1805 char *ondisk_path;
1806 unsigned char status = GOT_STATUS_NO_CHANGE;
1807 struct stat sb;
1809 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1810 return got_error_from_errno("asprintf");
1812 if (ie) {
1813 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1814 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1815 goto done;
1817 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1818 repo);
1819 if (err)
1820 goto done;
1821 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1822 sb.st_mode = got_fileindex_perms_to_st(ie);
1823 } else {
1824 if (stat(ondisk_path, &sb) == -1) {
1825 if (errno != ENOENT) {
1826 err = got_error_from_errno2("stat",
1827 ondisk_path);
1828 goto done;
1830 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1831 status = GOT_STATUS_UNVERSIONED;
1832 } else {
1833 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1834 status = GOT_STATUS_UNVERSIONED;
1835 else
1836 status = GOT_STATUS_OBSTRUCTED;
1840 if (status == GOT_STATUS_OBSTRUCTED) {
1841 if (ie)
1842 got_fileindex_entry_mark_skipped(ie);
1843 err = (*progress_cb)(progress_arg, status, path);
1844 goto done;
1846 if (status == GOT_STATUS_CONFLICT) {
1847 if (ie)
1848 got_fileindex_entry_mark_skipped(ie);
1849 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1850 path);
1851 goto done;
1854 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1855 (S_ISLNK(te->mode) ||
1856 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1858 * This is a regular file or an installed bad symlink.
1859 * If the file index indicates that this file is already
1860 * up-to-date with respect to the repository we can skip
1861 * updating contents of this file.
1863 if (got_fileindex_entry_has_commit(ie) &&
1864 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1865 SHA1_DIGEST_LENGTH) == 0) {
1866 /* Same commit. */
1867 err = sync_timestamps(worktree->root_fd,
1868 path, status, ie, &sb);
1869 if (err)
1870 goto done;
1871 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1872 path);
1873 goto done;
1875 if (got_fileindex_entry_has_blob(ie) &&
1876 memcmp(ie->blob_sha1, te->id.sha1,
1877 SHA1_DIGEST_LENGTH) == 0) {
1878 /* Different commit but the same blob. */
1879 err = sync_timestamps(worktree->root_fd,
1880 path, status, ie, &sb);
1881 if (err)
1882 goto done;
1883 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1884 path);
1885 goto done;
1889 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1890 if (err)
1891 goto done;
1893 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1894 int update_timestamps;
1895 struct got_blob_object *blob2 = NULL;
1896 char *label_orig = NULL;
1897 if (got_fileindex_entry_has_blob(ie)) {
1898 struct got_object_id id2;
1899 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1900 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1901 if (err)
1902 goto done;
1904 if (got_fileindex_entry_has_commit(ie)) {
1905 char id_str[SHA1_DIGEST_STRING_LENGTH];
1906 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1907 sizeof(id_str)) == NULL) {
1908 err = got_error_path(id_str,
1909 GOT_ERR_BAD_OBJ_ID_STR);
1910 goto done;
1912 if (asprintf(&label_orig, "%s: commit %s",
1913 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1914 err = got_error_from_errno("asprintf");
1915 goto done;
1918 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1919 char *link_target;
1920 err = got_object_blob_read_to_str(&link_target, blob);
1921 if (err)
1922 goto done;
1923 err = merge_symlink(worktree, blob2, ondisk_path, path,
1924 label_orig, link_target, worktree->base_commit_id,
1925 repo, progress_cb, progress_arg);
1926 free(link_target);
1927 } else {
1928 err = merge_blob(&update_timestamps, worktree, blob2,
1929 ondisk_path, path, sb.st_mode, label_orig, blob,
1930 worktree->base_commit_id, repo,
1931 progress_cb, progress_arg);
1933 free(label_orig);
1934 if (blob2)
1935 got_object_blob_close(blob2);
1936 if (err)
1937 goto done;
1939 * Do not update timestamps of files with local changes.
1940 * Otherwise, a future status walk would treat them as
1941 * unmodified files again.
1943 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1944 blob->id.sha1, worktree->base_commit_id->sha1,
1945 update_timestamps);
1946 } else if (status == GOT_STATUS_MODE_CHANGE) {
1947 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1948 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1949 } else if (status == GOT_STATUS_DELETE) {
1950 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1951 if (err)
1952 goto done;
1953 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1954 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1955 if (err)
1956 goto done;
1957 } else {
1958 int is_bad_symlink = 0;
1959 if (S_ISLNK(te->mode)) {
1960 err = install_symlink(&is_bad_symlink, worktree,
1961 ondisk_path, path, blob,
1962 status == GOT_STATUS_MISSING, 0,
1963 status == GOT_STATUS_UNVERSIONED, 0,
1964 repo, progress_cb, progress_arg);
1965 } else {
1966 err = install_blob(worktree, ondisk_path, path,
1967 te->mode, sb.st_mode, blob,
1968 status == GOT_STATUS_MISSING, 0, 0,
1969 status == GOT_STATUS_UNVERSIONED, repo,
1970 progress_cb, progress_arg);
1972 if (err)
1973 goto done;
1975 if (ie) {
1976 err = got_fileindex_entry_update(ie,
1977 worktree->root_fd, path, blob->id.sha1,
1978 worktree->base_commit_id->sha1, 1);
1979 } else {
1980 err = create_fileindex_entry(&ie, fileindex,
1981 worktree->base_commit_id, worktree->root_fd, path,
1982 &blob->id);
1984 if (err)
1985 goto done;
1987 if (is_bad_symlink) {
1988 got_fileindex_entry_filetype_set(ie,
1989 GOT_FILEIDX_MODE_BAD_SYMLINK);
1992 got_object_blob_close(blob);
1993 done:
1994 free(ondisk_path);
1995 return err;
1998 static const struct got_error *
1999 remove_ondisk_file(const char *root_path, const char *path)
2001 const struct got_error *err = NULL;
2002 char *ondisk_path = NULL, *parent = NULL;
2004 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2005 return got_error_from_errno("asprintf");
2007 if (unlink(ondisk_path) == -1) {
2008 if (errno != ENOENT)
2009 err = got_error_from_errno2("unlink", ondisk_path);
2010 } else {
2011 size_t root_len = strlen(root_path);
2012 err = got_path_dirname(&parent, ondisk_path);
2013 if (err)
2014 goto done;
2015 while (got_path_cmp(parent, root_path,
2016 strlen(parent), root_len) != 0) {
2017 free(ondisk_path);
2018 ondisk_path = parent;
2019 parent = NULL;
2020 if (rmdir(ondisk_path) == -1) {
2021 if (errno != ENOTEMPTY)
2022 err = got_error_from_errno2("rmdir",
2023 ondisk_path);
2024 break;
2026 err = got_path_dirname(&parent, ondisk_path);
2027 if (err)
2028 break;
2031 done:
2032 free(ondisk_path);
2033 free(parent);
2034 return err;
2037 static const struct got_error *
2038 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2039 struct got_fileindex_entry *ie, struct got_repository *repo,
2040 got_worktree_checkout_cb progress_cb, void *progress_arg)
2042 const struct got_error *err = NULL;
2043 unsigned char status;
2044 struct stat sb;
2045 char *ondisk_path;
2047 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2048 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2050 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2051 == -1)
2052 return got_error_from_errno("asprintf");
2054 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2055 if (err)
2056 goto done;
2058 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2059 char ondisk_target[PATH_MAX];
2060 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2061 sizeof(ondisk_target));
2062 if (ondisk_len == -1) {
2063 err = got_error_from_errno2("readlink", ondisk_path);
2064 goto done;
2066 ondisk_target[ondisk_len] = '\0';
2067 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2068 NULL, NULL, /* XXX pass common ancestor info? */
2069 ondisk_target, ondisk_path);
2070 if (err)
2071 goto done;
2072 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2073 ie->path);
2074 goto done;
2077 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2078 status == GOT_STATUS_ADD) {
2079 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2080 if (err)
2081 goto done;
2083 * Preserve the working file and change the deleted blob's
2084 * entry into a schedule-add entry.
2086 err = got_fileindex_entry_update(ie, worktree->root_fd,
2087 ie->path, NULL, NULL, 0);
2088 } else {
2089 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2090 if (err)
2091 goto done;
2092 if (status == GOT_STATUS_NO_CHANGE) {
2093 err = remove_ondisk_file(worktree->root_path, ie->path);
2094 if (err)
2095 goto done;
2097 got_fileindex_entry_remove(fileindex, ie);
2099 done:
2100 free(ondisk_path);
2101 return err;
2104 struct diff_cb_arg {
2105 struct got_fileindex *fileindex;
2106 struct got_worktree *worktree;
2107 struct got_repository *repo;
2108 got_worktree_checkout_cb progress_cb;
2109 void *progress_arg;
2110 got_cancel_cb cancel_cb;
2111 void *cancel_arg;
2114 static const struct got_error *
2115 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2116 struct got_tree_entry *te, const char *parent_path)
2118 struct diff_cb_arg *a = arg;
2120 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2121 return got_error(GOT_ERR_CANCELLED);
2123 return update_blob(a->worktree, a->fileindex, ie, te,
2124 ie->path, a->repo, a->progress_cb, a->progress_arg);
2127 static const struct got_error *
2128 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2130 struct diff_cb_arg *a = arg;
2132 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2133 return got_error(GOT_ERR_CANCELLED);
2135 return delete_blob(a->worktree, a->fileindex, ie,
2136 a->repo, a->progress_cb, a->progress_arg);
2139 static const struct got_error *
2140 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2142 struct diff_cb_arg *a = arg;
2143 const struct got_error *err;
2144 char *path;
2146 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2147 return got_error(GOT_ERR_CANCELLED);
2149 if (got_object_tree_entry_is_submodule(te))
2150 return NULL;
2152 if (asprintf(&path, "%s%s%s", parent_path,
2153 parent_path[0] ? "/" : "", te->name)
2154 == -1)
2155 return got_error_from_errno("asprintf");
2157 if (S_ISDIR(te->mode))
2158 err = add_dir_on_disk(a->worktree, path);
2159 else
2160 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2161 a->repo, a->progress_cb, a->progress_arg);
2163 free(path);
2164 return err;
2167 const struct got_error *
2168 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2170 uint32_t uuid_status;
2172 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2173 if (uuid_status != uuid_s_ok) {
2174 *uuidstr = NULL;
2175 return got_error_uuid(uuid_status, "uuid_to_string");
2178 return NULL;
2181 static const struct got_error *
2182 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2184 const struct got_error *err = NULL;
2185 char *uuidstr = NULL;
2187 *refname = NULL;
2189 err = got_worktree_get_uuid(&uuidstr, worktree);
2190 if (err)
2191 return err;
2193 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2194 err = got_error_from_errno("asprintf");
2195 *refname = NULL;
2197 free(uuidstr);
2198 return err;
2201 const struct got_error *
2202 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2204 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2207 static const struct got_error *
2208 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2210 return get_ref_name(refname, worktree,
2211 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2214 static const struct got_error *
2215 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2217 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2220 static const struct got_error *
2221 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2223 return get_ref_name(refname, worktree,
2224 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2227 static const struct got_error *
2228 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2230 return get_ref_name(refname, worktree,
2231 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2234 static const struct got_error *
2235 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2237 return get_ref_name(refname, worktree,
2238 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2241 static const struct got_error *
2242 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2244 return get_ref_name(refname, worktree,
2245 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2248 static const struct got_error *
2249 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2251 return get_ref_name(refname, worktree,
2252 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2255 static const struct got_error *
2256 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2258 return get_ref_name(refname, worktree,
2259 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2262 const struct got_error *
2263 got_worktree_get_histedit_script_path(char **path,
2264 struct got_worktree *worktree)
2266 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2267 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2268 *path = NULL;
2269 return got_error_from_errno("asprintf");
2271 return NULL;
2274 static const struct got_error *
2275 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2277 return get_ref_name(refname, worktree,
2278 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2281 static const struct got_error *
2282 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2284 return get_ref_name(refname, worktree,
2285 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2289 * Prevent Git's garbage collector from deleting our base commit by
2290 * setting a reference to our base commit's ID.
2292 static const struct got_error *
2293 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2295 const struct got_error *err = NULL;
2296 struct got_reference *ref = NULL;
2297 char *refname;
2299 err = got_worktree_get_base_ref_name(&refname, worktree);
2300 if (err)
2301 return err;
2303 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2304 if (err)
2305 goto done;
2307 err = got_ref_write(ref, repo);
2308 done:
2309 free(refname);
2310 if (ref)
2311 got_ref_close(ref);
2312 return err;
2315 static const struct got_error *
2316 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2318 const struct got_error *err = NULL;
2320 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2321 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2322 err = got_error_from_errno("asprintf");
2323 *fileindex_path = NULL;
2325 return err;
2329 static const struct got_error *
2330 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2331 struct got_worktree *worktree)
2333 const struct got_error *err = NULL;
2334 FILE *index = NULL;
2336 *fileindex_path = NULL;
2337 *fileindex = got_fileindex_alloc();
2338 if (*fileindex == NULL)
2339 return got_error_from_errno("got_fileindex_alloc");
2341 err = get_fileindex_path(fileindex_path, worktree);
2342 if (err)
2343 goto done;
2345 index = fopen(*fileindex_path, "rbe");
2346 if (index == NULL) {
2347 if (errno != ENOENT)
2348 err = got_error_from_errno2("fopen", *fileindex_path);
2349 } else {
2350 err = got_fileindex_read(*fileindex, index);
2351 if (fclose(index) == EOF && err == NULL)
2352 err = got_error_from_errno("fclose");
2354 done:
2355 if (err) {
2356 free(*fileindex_path);
2357 *fileindex_path = NULL;
2358 got_fileindex_free(*fileindex);
2359 *fileindex = NULL;
2361 return err;
2364 struct bump_base_commit_id_arg {
2365 struct got_object_id *base_commit_id;
2366 const char *path;
2367 size_t path_len;
2368 const char *entry_name;
2369 got_worktree_checkout_cb progress_cb;
2370 void *progress_arg;
2373 /* Bump base commit ID of all files within an updated part of the work tree. */
2374 static const struct got_error *
2375 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2377 const struct got_error *err;
2378 struct bump_base_commit_id_arg *a = arg;
2380 if (a->entry_name) {
2381 if (strcmp(ie->path, a->path) != 0)
2382 return NULL;
2383 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2384 return NULL;
2386 if (got_fileindex_entry_was_skipped(ie))
2387 return NULL;
2389 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2390 SHA1_DIGEST_LENGTH) == 0)
2391 return NULL;
2393 if (a->progress_cb) {
2394 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2395 ie->path);
2396 if (err)
2397 return err;
2399 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2400 return NULL;
2403 static const struct got_error *
2404 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2405 struct got_fileindex *fileindex,
2406 got_worktree_checkout_cb progress_cb, void *progress_arg)
2408 struct bump_base_commit_id_arg bbc_arg;
2410 bbc_arg.base_commit_id = worktree->base_commit_id;
2411 bbc_arg.entry_name = NULL;
2412 bbc_arg.path = "";
2413 bbc_arg.path_len = 0;
2414 bbc_arg.progress_cb = progress_cb;
2415 bbc_arg.progress_arg = progress_arg;
2417 return got_fileindex_for_each_entry_safe(fileindex,
2418 bump_base_commit_id, &bbc_arg);
2421 static const struct got_error *
2422 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2424 const struct got_error *err = NULL;
2425 char *new_fileindex_path = NULL;
2426 FILE *new_index = NULL;
2427 struct timespec timeout;
2429 err = got_opentemp_named(&new_fileindex_path, &new_index,
2430 fileindex_path);
2431 if (err)
2432 goto done;
2434 err = got_fileindex_write(fileindex, new_index);
2435 if (err)
2436 goto done;
2438 if (rename(new_fileindex_path, fileindex_path) != 0) {
2439 err = got_error_from_errno3("rename", new_fileindex_path,
2440 fileindex_path);
2441 unlink(new_fileindex_path);
2445 * Sleep for a short amount of time to ensure that files modified after
2446 * this program exits have a different time stamp from the one which
2447 * was recorded in the file index.
2449 timeout.tv_sec = 0;
2450 timeout.tv_nsec = 1;
2451 nanosleep(&timeout, NULL);
2452 done:
2453 if (new_index)
2454 fclose(new_index);
2455 free(new_fileindex_path);
2456 return err;
2459 static const struct got_error *
2460 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2461 struct got_object_id **tree_id, const char *wt_relpath,
2462 struct got_commit_object *base_commit, struct got_worktree *worktree,
2463 struct got_repository *repo)
2465 const struct got_error *err = NULL;
2466 struct got_object_id *id = NULL;
2467 char *in_repo_path = NULL;
2468 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2470 *entry_type = GOT_OBJ_TYPE_ANY;
2471 *tree_relpath = NULL;
2472 *tree_id = NULL;
2474 if (wt_relpath[0] == '\0') {
2475 /* Check out all files within the work tree. */
2476 *entry_type = GOT_OBJ_TYPE_TREE;
2477 *tree_relpath = strdup("");
2478 if (*tree_relpath == NULL) {
2479 err = got_error_from_errno("strdup");
2480 goto done;
2482 err = got_object_id_by_path(tree_id, repo, base_commit,
2483 worktree->path_prefix);
2484 if (err)
2485 goto done;
2486 return NULL;
2489 /* Check out a subset of files in the work tree. */
2491 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2492 is_root_wt ? "" : "/", wt_relpath) == -1) {
2493 err = got_error_from_errno("asprintf");
2494 goto done;
2497 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2498 if (err)
2499 goto done;
2501 free(in_repo_path);
2502 in_repo_path = NULL;
2504 err = got_object_get_type(entry_type, repo, id);
2505 if (err)
2506 goto done;
2508 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2509 /* Check out a single file. */
2510 if (strchr(wt_relpath, '/') == NULL) {
2511 /* Check out a single file in work tree's root dir. */
2512 in_repo_path = strdup(worktree->path_prefix);
2513 if (in_repo_path == NULL) {
2514 err = got_error_from_errno("strdup");
2515 goto done;
2517 *tree_relpath = strdup("");
2518 if (*tree_relpath == NULL) {
2519 err = got_error_from_errno("strdup");
2520 goto done;
2522 } else {
2523 /* Check out a single file in a subdirectory. */
2524 err = got_path_dirname(tree_relpath, wt_relpath);
2525 if (err)
2526 return err;
2527 if (asprintf(&in_repo_path, "%s%s%s",
2528 worktree->path_prefix, is_root_wt ? "" : "/",
2529 *tree_relpath) == -1) {
2530 err = got_error_from_errno("asprintf");
2531 goto done;
2534 err = got_object_id_by_path(tree_id, repo,
2535 base_commit, in_repo_path);
2536 } else {
2537 /* Check out all files within a subdirectory. */
2538 *tree_id = got_object_id_dup(id);
2539 if (*tree_id == NULL) {
2540 err = got_error_from_errno("got_object_id_dup");
2541 goto done;
2543 *tree_relpath = strdup(wt_relpath);
2544 if (*tree_relpath == NULL) {
2545 err = got_error_from_errno("strdup");
2546 goto done;
2549 done:
2550 free(id);
2551 free(in_repo_path);
2552 if (err) {
2553 *entry_type = GOT_OBJ_TYPE_ANY;
2554 free(*tree_relpath);
2555 *tree_relpath = NULL;
2556 free(*tree_id);
2557 *tree_id = NULL;
2559 return err;
2562 static const struct got_error *
2563 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2564 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2565 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2566 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2568 const struct got_error *err = NULL;
2569 struct got_commit_object *commit = NULL;
2570 struct got_tree_object *tree = NULL;
2571 struct got_fileindex_diff_tree_cb diff_cb;
2572 struct diff_cb_arg arg;
2574 err = ref_base_commit(worktree, repo);
2575 if (err) {
2576 if (!(err->code == GOT_ERR_ERRNO &&
2577 (errno == EACCES || errno == EROFS)))
2578 goto done;
2579 err = (*progress_cb)(progress_arg,
2580 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2581 if (err)
2582 return err;
2585 err = got_object_open_as_commit(&commit, repo,
2586 worktree->base_commit_id);
2587 if (err)
2588 goto done;
2590 err = got_object_open_as_tree(&tree, repo, tree_id);
2591 if (err)
2592 goto done;
2594 if (entry_name &&
2595 got_object_tree_find_entry(tree, entry_name) == NULL) {
2596 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2597 goto done;
2600 diff_cb.diff_old_new = diff_old_new;
2601 diff_cb.diff_old = diff_old;
2602 diff_cb.diff_new = diff_new;
2603 arg.fileindex = fileindex;
2604 arg.worktree = worktree;
2605 arg.repo = repo;
2606 arg.progress_cb = progress_cb;
2607 arg.progress_arg = progress_arg;
2608 arg.cancel_cb = cancel_cb;
2609 arg.cancel_arg = cancel_arg;
2610 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2611 entry_name, repo, &diff_cb, &arg);
2612 done:
2613 if (tree)
2614 got_object_tree_close(tree);
2615 if (commit)
2616 got_object_commit_close(commit);
2617 return err;
2620 const struct got_error *
2621 got_worktree_checkout_files(struct got_worktree *worktree,
2622 struct got_pathlist_head *paths, struct got_repository *repo,
2623 got_worktree_checkout_cb progress_cb, void *progress_arg,
2624 got_cancel_cb cancel_cb, void *cancel_arg)
2626 const struct got_error *err = NULL, *sync_err, *unlockerr;
2627 struct got_commit_object *commit = NULL;
2628 struct got_tree_object *tree = NULL;
2629 struct got_fileindex *fileindex = NULL;
2630 char *fileindex_path = NULL;
2631 struct got_pathlist_entry *pe;
2632 struct tree_path_data {
2633 STAILQ_ENTRY(tree_path_data) entry;
2634 struct got_object_id *tree_id;
2635 int entry_type;
2636 char *relpath;
2637 char *entry_name;
2638 } *tpd = NULL;
2639 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2641 STAILQ_INIT(&tree_paths);
2643 err = lock_worktree(worktree, LOCK_EX);
2644 if (err)
2645 return err;
2647 err = got_object_open_as_commit(&commit, repo,
2648 worktree->base_commit_id);
2649 if (err)
2650 goto done;
2652 /* Map all specified paths to in-repository trees. */
2653 TAILQ_FOREACH(pe, paths, entry) {
2654 tpd = malloc(sizeof(*tpd));
2655 if (tpd == NULL) {
2656 err = got_error_from_errno("malloc");
2657 goto done;
2660 err = find_tree_entry_for_checkout(&tpd->entry_type,
2661 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2662 worktree, repo);
2663 if (err) {
2664 free(tpd);
2665 goto done;
2668 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2669 err = got_path_basename(&tpd->entry_name, pe->path);
2670 if (err) {
2671 free(tpd->relpath);
2672 free(tpd->tree_id);
2673 free(tpd);
2674 goto done;
2676 } else
2677 tpd->entry_name = NULL;
2679 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2683 * Read the file index.
2684 * Checking out files is supposed to be an idempotent operation.
2685 * If the on-disk file index is incomplete we will try to complete it.
2687 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2688 if (err)
2689 goto done;
2691 tpd = STAILQ_FIRST(&tree_paths);
2692 TAILQ_FOREACH(pe, paths, entry) {
2693 struct bump_base_commit_id_arg bbc_arg;
2695 err = checkout_files(worktree, fileindex, tpd->relpath,
2696 tpd->tree_id, tpd->entry_name, repo,
2697 progress_cb, progress_arg, cancel_cb, cancel_arg);
2698 if (err)
2699 break;
2701 bbc_arg.base_commit_id = worktree->base_commit_id;
2702 bbc_arg.entry_name = tpd->entry_name;
2703 bbc_arg.path = pe->path;
2704 bbc_arg.path_len = pe->path_len;
2705 bbc_arg.progress_cb = progress_cb;
2706 bbc_arg.progress_arg = progress_arg;
2707 err = got_fileindex_for_each_entry_safe(fileindex,
2708 bump_base_commit_id, &bbc_arg);
2709 if (err)
2710 break;
2712 tpd = STAILQ_NEXT(tpd, entry);
2714 sync_err = sync_fileindex(fileindex, fileindex_path);
2715 if (sync_err && err == NULL)
2716 err = sync_err;
2717 done:
2718 free(fileindex_path);
2719 if (tree)
2720 got_object_tree_close(tree);
2721 if (commit)
2722 got_object_commit_close(commit);
2723 if (fileindex)
2724 got_fileindex_free(fileindex);
2725 while (!STAILQ_EMPTY(&tree_paths)) {
2726 tpd = STAILQ_FIRST(&tree_paths);
2727 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2728 free(tpd->relpath);
2729 free(tpd->tree_id);
2730 free(tpd);
2732 unlockerr = lock_worktree(worktree, LOCK_SH);
2733 if (unlockerr && err == NULL)
2734 err = unlockerr;
2735 return err;
2738 struct merge_file_cb_arg {
2739 struct got_worktree *worktree;
2740 struct got_fileindex *fileindex;
2741 got_worktree_checkout_cb progress_cb;
2742 void *progress_arg;
2743 got_cancel_cb cancel_cb;
2744 void *cancel_arg;
2745 const char *label_orig;
2746 struct got_object_id *commit_id2;
2747 int allow_bad_symlinks;
2750 static const struct got_error *
2751 merge_file_cb(void *arg, struct got_blob_object *blob1,
2752 struct got_blob_object *blob2, struct got_object_id *id1,
2753 struct got_object_id *id2, const char *path1, const char *path2,
2754 mode_t mode1, mode_t mode2, struct got_repository *repo)
2756 static const struct got_error *err = NULL;
2757 struct merge_file_cb_arg *a = arg;
2758 struct got_fileindex_entry *ie;
2759 char *ondisk_path = NULL;
2760 struct stat sb;
2761 unsigned char status;
2762 int local_changes_subsumed;
2763 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2764 char *id_str = NULL, *label_deriv2 = NULL;
2766 if (blob1 && blob2) {
2767 ie = got_fileindex_entry_get(a->fileindex, path2,
2768 strlen(path2));
2769 if (ie == NULL)
2770 return (*a->progress_cb)(a->progress_arg,
2771 GOT_STATUS_MISSING, path2);
2773 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2774 path2) == -1)
2775 return got_error_from_errno("asprintf");
2777 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2778 repo);
2779 if (err)
2780 goto done;
2782 if (status == GOT_STATUS_DELETE) {
2783 err = (*a->progress_cb)(a->progress_arg,
2784 GOT_STATUS_MERGE, path2);
2785 goto done;
2787 if (status != GOT_STATUS_NO_CHANGE &&
2788 status != GOT_STATUS_MODIFY &&
2789 status != GOT_STATUS_CONFLICT &&
2790 status != GOT_STATUS_ADD) {
2791 err = (*a->progress_cb)(a->progress_arg, status, path2);
2792 goto done;
2795 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2796 char *link_target2;
2797 err = got_object_blob_read_to_str(&link_target2, blob2);
2798 if (err)
2799 goto done;
2800 err = merge_symlink(a->worktree, blob1, ondisk_path,
2801 path2, a->label_orig, link_target2, a->commit_id2,
2802 repo, a->progress_cb, a->progress_arg);
2803 free(link_target2);
2804 } else {
2805 int fd;
2807 f_orig = got_opentemp();
2808 if (f_orig == NULL) {
2809 err = got_error_from_errno("got_opentemp");
2810 goto done;
2812 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2813 f_orig, blob1);
2814 if (err)
2815 goto done;
2817 f_deriv2 = got_opentemp();
2818 if (f_deriv2 == NULL)
2819 goto done;
2820 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2821 f_deriv2, blob2);
2822 if (err)
2823 goto done;
2825 fd = open(ondisk_path,
2826 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2827 if (fd == -1) {
2828 err = got_error_from_errno2("open",
2829 ondisk_path);
2830 goto done;
2832 f_deriv = fdopen(fd, "r");
2833 if (f_deriv == NULL) {
2834 err = got_error_from_errno2("fdopen",
2835 ondisk_path);
2836 close(fd);
2837 goto done;
2839 err = got_object_id_str(&id_str, a->commit_id2);
2840 if (err)
2841 goto done;
2842 if (asprintf(&label_deriv2, "%s: commit %s",
2843 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2844 err = got_error_from_errno("asprintf");
2845 goto done;
2847 err = merge_file(&local_changes_subsumed, a->worktree,
2848 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2849 sb.st_mode, a->label_orig, NULL, label_deriv2,
2850 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2851 a->progress_cb, a->progress_arg);
2853 } else if (blob1) {
2854 ie = got_fileindex_entry_get(a->fileindex, path1,
2855 strlen(path1));
2856 if (ie == NULL)
2857 return (*a->progress_cb)(a->progress_arg,
2858 GOT_STATUS_MISSING, path1);
2860 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2861 path1) == -1)
2862 return got_error_from_errno("asprintf");
2864 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2865 repo);
2866 if (err)
2867 goto done;
2869 switch (status) {
2870 case GOT_STATUS_NO_CHANGE:
2871 err = (*a->progress_cb)(a->progress_arg,
2872 GOT_STATUS_DELETE, path1);
2873 if (err)
2874 goto done;
2875 err = remove_ondisk_file(a->worktree->root_path, path1);
2876 if (err)
2877 goto done;
2878 if (ie)
2879 got_fileindex_entry_mark_deleted_from_disk(ie);
2880 break;
2881 case GOT_STATUS_DELETE:
2882 case GOT_STATUS_MISSING:
2883 err = (*a->progress_cb)(a->progress_arg,
2884 GOT_STATUS_DELETE, path1);
2885 if (err)
2886 goto done;
2887 if (ie)
2888 got_fileindex_entry_mark_deleted_from_disk(ie);
2889 break;
2890 case GOT_STATUS_ADD: {
2891 struct got_object_id *id;
2892 FILE *blob1_f;
2893 off_t blob1_size;
2895 * Delete the added file only if its content already
2896 * exists in the repository.
2898 err = got_object_blob_file_create(&id, &blob1_f,
2899 &blob1_size, path1);
2900 if (err)
2901 goto done;
2902 if (got_object_id_cmp(id, id1) == 0) {
2903 err = (*a->progress_cb)(a->progress_arg,
2904 GOT_STATUS_DELETE, path1);
2905 if (err)
2906 goto done;
2907 err = remove_ondisk_file(a->worktree->root_path,
2908 path1);
2909 if (err)
2910 goto done;
2911 if (ie)
2912 got_fileindex_entry_remove(a->fileindex,
2913 ie);
2914 } else {
2915 err = (*a->progress_cb)(a->progress_arg,
2916 GOT_STATUS_CANNOT_DELETE, path1);
2918 if (fclose(blob1_f) == EOF && err == NULL)
2919 err = got_error_from_errno("fclose");
2920 free(id);
2921 if (err)
2922 goto done;
2923 break;
2925 case GOT_STATUS_MODIFY:
2926 case GOT_STATUS_CONFLICT:
2927 err = (*a->progress_cb)(a->progress_arg,
2928 GOT_STATUS_CANNOT_DELETE, path1);
2929 if (err)
2930 goto done;
2931 break;
2932 case GOT_STATUS_OBSTRUCTED:
2933 err = (*a->progress_cb)(a->progress_arg, status, path1);
2934 if (err)
2935 goto done;
2936 break;
2937 default:
2938 break;
2940 } else if (blob2) {
2941 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2942 path2) == -1)
2943 return got_error_from_errno("asprintf");
2944 ie = got_fileindex_entry_get(a->fileindex, path2,
2945 strlen(path2));
2946 if (ie) {
2947 err = get_file_status(&status, &sb, ie, ondisk_path,
2948 -1, NULL, repo);
2949 if (err)
2950 goto done;
2951 if (status != GOT_STATUS_NO_CHANGE &&
2952 status != GOT_STATUS_MODIFY &&
2953 status != GOT_STATUS_CONFLICT &&
2954 status != GOT_STATUS_ADD) {
2955 err = (*a->progress_cb)(a->progress_arg,
2956 status, path2);
2957 goto done;
2959 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2960 char *link_target2;
2961 err = got_object_blob_read_to_str(&link_target2,
2962 blob2);
2963 if (err)
2964 goto done;
2965 err = merge_symlink(a->worktree, NULL,
2966 ondisk_path, path2, a->label_orig,
2967 link_target2, a->commit_id2, repo,
2968 a->progress_cb, a->progress_arg);
2969 free(link_target2);
2970 } else if (S_ISREG(sb.st_mode)) {
2971 err = merge_blob(&local_changes_subsumed,
2972 a->worktree, NULL, ondisk_path, path2,
2973 sb.st_mode, a->label_orig, blob2,
2974 a->commit_id2, repo, a->progress_cb,
2975 a->progress_arg);
2976 } else {
2977 err = got_error_path(ondisk_path,
2978 GOT_ERR_FILE_OBSTRUCTED);
2980 if (err)
2981 goto done;
2982 if (status == GOT_STATUS_DELETE) {
2983 err = got_fileindex_entry_update(ie,
2984 a->worktree->root_fd, path2, blob2->id.sha1,
2985 a->worktree->base_commit_id->sha1, 0);
2986 if (err)
2987 goto done;
2989 } else {
2990 int is_bad_symlink = 0;
2991 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2992 if (S_ISLNK(mode2)) {
2993 err = install_symlink(&is_bad_symlink,
2994 a->worktree, ondisk_path, path2, blob2, 0,
2995 0, 1, a->allow_bad_symlinks, repo,
2996 a->progress_cb, a->progress_arg);
2997 } else {
2998 err = install_blob(a->worktree, ondisk_path, path2,
2999 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3000 a->progress_cb, a->progress_arg);
3002 if (err)
3003 goto done;
3004 err = got_fileindex_entry_alloc(&ie, path2);
3005 if (err)
3006 goto done;
3007 err = got_fileindex_entry_update(ie,
3008 a->worktree->root_fd, path2, NULL, NULL, 1);
3009 if (err) {
3010 got_fileindex_entry_free(ie);
3011 goto done;
3013 err = got_fileindex_entry_add(a->fileindex, ie);
3014 if (err) {
3015 got_fileindex_entry_free(ie);
3016 goto done;
3018 if (is_bad_symlink) {
3019 got_fileindex_entry_filetype_set(ie,
3020 GOT_FILEIDX_MODE_BAD_SYMLINK);
3024 done:
3025 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3026 err = got_error_from_errno("fclose");
3027 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3028 err = got_error_from_errno("fclose");
3029 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3030 err = got_error_from_errno("fclose");
3031 free(id_str);
3032 free(label_deriv2);
3033 free(ondisk_path);
3034 return err;
3037 static const struct got_error *
3038 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3040 struct got_worktree *worktree = arg;
3042 /* Reject merges into a work tree with mixed base commits. */
3043 if (got_fileindex_entry_has_commit(ie) &&
3044 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3045 SHA1_DIGEST_LENGTH) != 0)
3046 return got_error(GOT_ERR_MIXED_COMMITS);
3048 return NULL;
3051 struct check_merge_conflicts_arg {
3052 struct got_worktree *worktree;
3053 struct got_fileindex *fileindex;
3054 struct got_repository *repo;
3057 static const struct got_error *
3058 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3059 struct got_blob_object *blob2, struct got_object_id *id1,
3060 struct got_object_id *id2, const char *path1, const char *path2,
3061 mode_t mode1, mode_t mode2, struct got_repository *repo)
3063 const struct got_error *err = NULL;
3064 struct check_merge_conflicts_arg *a = arg;
3065 unsigned char status;
3066 struct stat sb;
3067 struct got_fileindex_entry *ie;
3068 const char *path = path2 ? path2 : path1;
3069 struct got_object_id *id = id2 ? id2 : id1;
3070 char *ondisk_path;
3072 if (id == NULL)
3073 return NULL;
3075 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3076 if (ie == NULL)
3077 return NULL;
3079 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3080 == -1)
3081 return got_error_from_errno("asprintf");
3083 /* Reject merges into a work tree with conflicted files. */
3084 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3085 free(ondisk_path);
3086 if (err)
3087 return err;
3088 if (status == GOT_STATUS_CONFLICT)
3089 return got_error(GOT_ERR_CONFLICTS);
3091 return NULL;
3094 static const struct got_error *
3095 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3096 const char *fileindex_path, struct got_object_id *commit_id1,
3097 struct got_object_id *commit_id2, struct got_repository *repo,
3098 got_worktree_checkout_cb progress_cb, void *progress_arg,
3099 got_cancel_cb cancel_cb, void *cancel_arg)
3101 const struct got_error *err = NULL, *sync_err;
3102 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3103 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3104 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3105 struct check_merge_conflicts_arg cmc_arg;
3106 struct merge_file_cb_arg arg;
3107 char *label_orig = NULL;
3109 if (commit_id1) {
3110 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3111 if (err)
3112 goto done;
3113 err = got_object_id_by_path(&tree_id1, repo, commit1,
3114 worktree->path_prefix);
3115 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3116 goto done;
3118 if (tree_id1) {
3119 char *id_str;
3121 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3122 if (err)
3123 goto done;
3125 err = got_object_id_str(&id_str, commit_id1);
3126 if (err)
3127 goto done;
3129 if (asprintf(&label_orig, "%s: commit %s",
3130 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3131 err = got_error_from_errno("asprintf");
3132 free(id_str);
3133 goto done;
3135 free(id_str);
3138 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3139 if (err)
3140 goto done;
3142 err = got_object_id_by_path(&tree_id2, repo, commit2,
3143 worktree->path_prefix);
3144 if (err)
3145 goto done;
3147 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3148 if (err)
3149 goto done;
3151 cmc_arg.worktree = worktree;
3152 cmc_arg.fileindex = fileindex;
3153 cmc_arg.repo = repo;
3154 err = got_diff_tree(tree1, tree2, "", "", repo,
3155 check_merge_conflicts, &cmc_arg, 0);
3156 if (err)
3157 goto done;
3159 arg.worktree = worktree;
3160 arg.fileindex = fileindex;
3161 arg.progress_cb = progress_cb;
3162 arg.progress_arg = progress_arg;
3163 arg.cancel_cb = cancel_cb;
3164 arg.cancel_arg = cancel_arg;
3165 arg.label_orig = label_orig;
3166 arg.commit_id2 = commit_id2;
3167 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3168 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3169 sync_err = sync_fileindex(fileindex, fileindex_path);
3170 if (sync_err && err == NULL)
3171 err = sync_err;
3172 done:
3173 if (commit1)
3174 got_object_commit_close(commit1);
3175 if (commit2)
3176 got_object_commit_close(commit2);
3177 if (tree1)
3178 got_object_tree_close(tree1);
3179 if (tree2)
3180 got_object_tree_close(tree2);
3181 free(label_orig);
3182 return err;
3185 const struct got_error *
3186 got_worktree_merge_files(struct got_worktree *worktree,
3187 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3188 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3189 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3191 const struct got_error *err, *unlockerr;
3192 char *fileindex_path = NULL;
3193 struct got_fileindex *fileindex = NULL;
3195 err = lock_worktree(worktree, LOCK_EX);
3196 if (err)
3197 return err;
3199 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3200 if (err)
3201 goto done;
3203 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3204 worktree);
3205 if (err)
3206 goto done;
3208 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3209 commit_id2, repo, progress_cb, progress_arg,
3210 cancel_cb, cancel_arg);
3211 done:
3212 if (fileindex)
3213 got_fileindex_free(fileindex);
3214 free(fileindex_path);
3215 unlockerr = lock_worktree(worktree, LOCK_SH);
3216 if (unlockerr && err == NULL)
3217 err = unlockerr;
3218 return err;
3221 struct diff_dir_cb_arg {
3222 struct got_fileindex *fileindex;
3223 struct got_worktree *worktree;
3224 const char *status_path;
3225 size_t status_path_len;
3226 struct got_repository *repo;
3227 got_worktree_status_cb status_cb;
3228 void *status_arg;
3229 got_cancel_cb cancel_cb;
3230 void *cancel_arg;
3231 /* A pathlist containing per-directory pathlists of ignore patterns. */
3232 struct got_pathlist_head *ignores;
3233 int report_unchanged;
3234 int no_ignores;
3237 static const struct got_error *
3238 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3239 int dirfd, const char *de_name,
3240 got_worktree_status_cb status_cb, void *status_arg,
3241 struct got_repository *repo, int report_unchanged)
3243 const struct got_error *err = NULL;
3244 unsigned char status = GOT_STATUS_NO_CHANGE;
3245 unsigned char staged_status = get_staged_status(ie);
3246 struct stat sb;
3247 struct got_object_id blob_id, commit_id, staged_blob_id;
3248 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3249 struct got_object_id *staged_blob_idp = NULL;
3251 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3252 if (err)
3253 return err;
3255 if (status == GOT_STATUS_NO_CHANGE &&
3256 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3257 return NULL;
3259 if (got_fileindex_entry_has_blob(ie)) {
3260 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3261 blob_idp = &blob_id;
3263 if (got_fileindex_entry_has_commit(ie)) {
3264 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3265 commit_idp = &commit_id;
3267 if (staged_status == GOT_STATUS_ADD ||
3268 staged_status == GOT_STATUS_MODIFY) {
3269 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3270 SHA1_DIGEST_LENGTH);
3271 staged_blob_idp = &staged_blob_id;
3274 return (*status_cb)(status_arg, status, staged_status,
3275 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3278 static const struct got_error *
3279 status_old_new(void *arg, struct got_fileindex_entry *ie,
3280 struct dirent *de, const char *parent_path, int dirfd)
3282 const struct got_error *err = NULL;
3283 struct diff_dir_cb_arg *a = arg;
3284 char *abspath;
3286 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3287 return got_error(GOT_ERR_CANCELLED);
3289 if (got_path_cmp(parent_path, a->status_path,
3290 strlen(parent_path), a->status_path_len) != 0 &&
3291 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3292 return NULL;
3294 if (parent_path[0]) {
3295 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3296 parent_path, de->d_name) == -1)
3297 return got_error_from_errno("asprintf");
3298 } else {
3299 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3300 de->d_name) == -1)
3301 return got_error_from_errno("asprintf");
3304 err = report_file_status(ie, abspath, dirfd, de->d_name,
3305 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3306 free(abspath);
3307 return err;
3310 static const struct got_error *
3311 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3313 struct diff_dir_cb_arg *a = arg;
3314 struct got_object_id blob_id, commit_id;
3315 unsigned char status;
3317 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3318 return got_error(GOT_ERR_CANCELLED);
3320 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3321 return NULL;
3323 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3324 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3325 if (got_fileindex_entry_has_file_on_disk(ie))
3326 status = GOT_STATUS_MISSING;
3327 else
3328 status = GOT_STATUS_DELETE;
3329 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3330 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3333 void
3334 free_ignorelist(struct got_pathlist_head *ignorelist)
3336 struct got_pathlist_entry *pe;
3338 TAILQ_FOREACH(pe, ignorelist, entry)
3339 free((char *)pe->path);
3340 got_pathlist_free(ignorelist);
3343 void
3344 free_ignores(struct got_pathlist_head *ignores)
3346 struct got_pathlist_entry *pe;
3348 TAILQ_FOREACH(pe, ignores, entry) {
3349 struct got_pathlist_head *ignorelist = pe->data;
3350 free_ignorelist(ignorelist);
3351 free((char *)pe->path);
3353 got_pathlist_free(ignores);
3356 static const struct got_error *
3357 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3359 const struct got_error *err = NULL;
3360 struct got_pathlist_entry *pe = NULL;
3361 struct got_pathlist_head *ignorelist;
3362 char *line = NULL, *pattern, *dirpath = NULL;
3363 size_t linesize = 0;
3364 ssize_t linelen;
3366 ignorelist = calloc(1, sizeof(*ignorelist));
3367 if (ignorelist == NULL)
3368 return got_error_from_errno("calloc");
3369 TAILQ_INIT(ignorelist);
3371 while ((linelen = getline(&line, &linesize, f)) != -1) {
3372 if (linelen > 0 && line[linelen - 1] == '\n')
3373 line[linelen - 1] = '\0';
3375 /* Git's ignores may contain comments. */
3376 if (line[0] == '#')
3377 continue;
3379 /* Git's negated patterns are not (yet?) supported. */
3380 if (line[0] == '!')
3381 continue;
3383 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3384 line) == -1) {
3385 err = got_error_from_errno("asprintf");
3386 goto done;
3388 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3389 if (err)
3390 goto done;
3392 if (ferror(f)) {
3393 err = got_error_from_errno("getline");
3394 goto done;
3397 dirpath = strdup(path);
3398 if (dirpath == NULL) {
3399 err = got_error_from_errno("strdup");
3400 goto done;
3402 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3403 done:
3404 free(line);
3405 if (err || pe == NULL) {
3406 free(dirpath);
3407 free_ignorelist(ignorelist);
3409 return err;
3412 int
3413 match_ignores(struct got_pathlist_head *ignores, const char *path)
3415 struct got_pathlist_entry *pe;
3417 /* Handle patterns which match in all directories. */
3418 TAILQ_FOREACH(pe, ignores, entry) {
3419 struct got_pathlist_head *ignorelist = pe->data;
3420 struct got_pathlist_entry *pi;
3422 TAILQ_FOREACH(pi, ignorelist, entry) {
3423 const char *p, *pattern = pi->path;
3425 if (strncmp(pattern, "**/", 3) != 0)
3426 continue;
3427 pattern += 3;
3428 p = path;
3429 while (*p) {
3430 if (fnmatch(pattern, p,
3431 FNM_PATHNAME | FNM_LEADING_DIR)) {
3432 /* Retry in next directory. */
3433 while (*p && *p != '/')
3434 p++;
3435 while (*p == '/')
3436 p++;
3437 continue;
3439 return 1;
3445 * The ignores pathlist contains ignore lists from children before
3446 * parents, so we can find the most specific ignorelist by walking
3447 * ignores backwards.
3449 pe = TAILQ_LAST(ignores, got_pathlist_head);
3450 while (pe) {
3451 if (got_path_is_child(path, pe->path, pe->path_len)) {
3452 struct got_pathlist_head *ignorelist = pe->data;
3453 struct got_pathlist_entry *pi;
3454 TAILQ_FOREACH(pi, ignorelist, entry) {
3455 const char *pattern = pi->path;
3456 int flags = FNM_LEADING_DIR;
3457 if (strstr(pattern, "/**/") == NULL)
3458 flags |= FNM_PATHNAME;
3459 if (fnmatch(pattern, path, flags))
3460 continue;
3461 return 1;
3464 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3467 return 0;
3470 static const struct got_error *
3471 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3472 const char *path, int dirfd, const char *ignores_filename)
3474 const struct got_error *err = NULL;
3475 char *ignorespath;
3476 int fd = -1;
3477 FILE *ignoresfile = NULL;
3479 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3480 path[0] ? "/" : "", ignores_filename) == -1)
3481 return got_error_from_errno("asprintf");
3483 if (dirfd != -1) {
3484 fd = openat(dirfd, ignores_filename,
3485 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3486 if (fd == -1) {
3487 if (errno != ENOENT && errno != EACCES)
3488 err = got_error_from_errno2("openat",
3489 ignorespath);
3490 } else {
3491 ignoresfile = fdopen(fd, "r");
3492 if (ignoresfile == NULL)
3493 err = got_error_from_errno2("fdopen",
3494 ignorespath);
3495 else {
3496 fd = -1;
3497 err = read_ignores(ignores, path, ignoresfile);
3500 } else {
3501 ignoresfile = fopen(ignorespath, "re");
3502 if (ignoresfile == NULL) {
3503 if (errno != ENOENT && errno != EACCES)
3504 err = got_error_from_errno2("fopen",
3505 ignorespath);
3506 } else
3507 err = read_ignores(ignores, path, ignoresfile);
3510 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3511 err = got_error_from_errno2("fclose", path);
3512 if (fd != -1 && close(fd) == -1 && err == NULL)
3513 err = got_error_from_errno2("close", path);
3514 free(ignorespath);
3515 return err;
3518 static const struct got_error *
3519 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3520 int dirfd)
3522 const struct got_error *err = NULL;
3523 struct diff_dir_cb_arg *a = arg;
3524 char *path = NULL;
3526 if (ignore != NULL)
3527 *ignore = 0;
3529 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3530 return got_error(GOT_ERR_CANCELLED);
3532 if (parent_path[0]) {
3533 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3534 return got_error_from_errno("asprintf");
3535 } else {
3536 path = de->d_name;
3539 if (de->d_type == DT_DIR) {
3540 if (!a->no_ignores && ignore != NULL &&
3541 match_ignores(a->ignores, path))
3542 *ignore = 1;
3543 } else if (!match_ignores(a->ignores, path) &&
3544 got_path_is_child(path, a->status_path, a->status_path_len))
3545 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3546 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3547 if (parent_path[0])
3548 free(path);
3549 return err;
3552 static const struct got_error *
3553 status_traverse(void *arg, const char *path, int dirfd)
3555 const struct got_error *err = NULL;
3556 struct diff_dir_cb_arg *a = arg;
3558 if (a->no_ignores)
3559 return NULL;
3561 err = add_ignores(a->ignores, a->worktree->root_path,
3562 path, dirfd, ".cvsignore");
3563 if (err)
3564 return err;
3566 err = add_ignores(a->ignores, a->worktree->root_path, path,
3567 dirfd, ".gitignore");
3569 return err;
3572 static const struct got_error *
3573 report_single_file_status(const char *path, const char *ondisk_path,
3574 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3575 void *status_arg, struct got_repository *repo, int report_unchanged,
3576 struct got_pathlist_head *ignores, int no_ignores)
3578 struct got_fileindex_entry *ie;
3579 struct stat sb;
3581 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3582 if (ie)
3583 return report_file_status(ie, ondisk_path, -1, NULL,
3584 status_cb, status_arg, repo, report_unchanged);
3586 if (lstat(ondisk_path, &sb) == -1) {
3587 if (errno != ENOENT)
3588 return got_error_from_errno2("lstat", ondisk_path);
3589 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3590 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3593 if (!no_ignores && match_ignores(ignores, path))
3594 return NULL;
3596 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3597 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3598 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3600 return NULL;
3603 static const struct got_error *
3604 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3605 const char *root_path, const char *path)
3607 const struct got_error *err;
3608 char *parent_path, *next_parent_path = NULL;
3610 err = add_ignores(ignores, root_path, "", -1,
3611 ".cvsignore");
3612 if (err)
3613 return err;
3615 err = add_ignores(ignores, root_path, "", -1,
3616 ".gitignore");
3617 if (err)
3618 return err;
3620 err = got_path_dirname(&parent_path, path);
3621 if (err) {
3622 if (err->code == GOT_ERR_BAD_PATH)
3623 return NULL; /* cannot traverse parent */
3624 return err;
3626 for (;;) {
3627 err = add_ignores(ignores, root_path, parent_path, -1,
3628 ".cvsignore");
3629 if (err)
3630 break;
3631 err = add_ignores(ignores, root_path, parent_path, -1,
3632 ".gitignore");
3633 if (err)
3634 break;
3635 err = got_path_dirname(&next_parent_path, parent_path);
3636 if (err) {
3637 if (err->code == GOT_ERR_BAD_PATH)
3638 err = NULL; /* traversed everything */
3639 break;
3641 if (got_path_is_root_dir(parent_path))
3642 break;
3643 free(parent_path);
3644 parent_path = next_parent_path;
3645 next_parent_path = NULL;
3648 free(parent_path);
3649 free(next_parent_path);
3650 return err;
3653 static const struct got_error *
3654 worktree_status(struct got_worktree *worktree, const char *path,
3655 struct got_fileindex *fileindex, struct got_repository *repo,
3656 got_worktree_status_cb status_cb, void *status_arg,
3657 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3658 int report_unchanged)
3660 const struct got_error *err = NULL;
3661 int fd = -1;
3662 struct got_fileindex_diff_dir_cb fdiff_cb;
3663 struct diff_dir_cb_arg arg;
3664 char *ondisk_path = NULL;
3665 struct got_pathlist_head ignores;
3666 struct got_fileindex_entry *ie;
3668 TAILQ_INIT(&ignores);
3670 if (asprintf(&ondisk_path, "%s%s%s",
3671 worktree->root_path, path[0] ? "/" : "", path) == -1)
3672 return got_error_from_errno("asprintf");
3674 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3675 if (ie) {
3676 err = report_single_file_status(path, ondisk_path,
3677 fileindex, status_cb, status_arg, repo,
3678 report_unchanged, &ignores, no_ignores);
3679 goto done;
3682 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3683 if (fd == -1) {
3684 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3685 !got_err_open_nofollow_on_symlink())
3686 err = got_error_from_errno2("open", ondisk_path);
3687 else {
3688 if (!no_ignores) {
3689 err = add_ignores_from_parent_paths(&ignores,
3690 worktree->root_path, ondisk_path);
3691 if (err)
3692 goto done;
3694 err = report_single_file_status(path, ondisk_path,
3695 fileindex, status_cb, status_arg, repo,
3696 report_unchanged, &ignores, no_ignores);
3698 } else {
3699 fdiff_cb.diff_old_new = status_old_new;
3700 fdiff_cb.diff_old = status_old;
3701 fdiff_cb.diff_new = status_new;
3702 fdiff_cb.diff_traverse = status_traverse;
3703 arg.fileindex = fileindex;
3704 arg.worktree = worktree;
3705 arg.status_path = path;
3706 arg.status_path_len = strlen(path);
3707 arg.repo = repo;
3708 arg.status_cb = status_cb;
3709 arg.status_arg = status_arg;
3710 arg.cancel_cb = cancel_cb;
3711 arg.cancel_arg = cancel_arg;
3712 arg.report_unchanged = report_unchanged;
3713 arg.no_ignores = no_ignores;
3714 if (!no_ignores) {
3715 err = add_ignores_from_parent_paths(&ignores,
3716 worktree->root_path, path);
3717 if (err)
3718 goto done;
3720 arg.ignores = &ignores;
3721 err = got_fileindex_diff_dir(fileindex, fd,
3722 worktree->root_path, path, repo, &fdiff_cb, &arg);
3724 done:
3725 free_ignores(&ignores);
3726 if (fd != -1 && close(fd) == -1 && err == NULL)
3727 err = got_error_from_errno("close");
3728 free(ondisk_path);
3729 return err;
3732 const struct got_error *
3733 got_worktree_status(struct got_worktree *worktree,
3734 struct got_pathlist_head *paths, struct got_repository *repo,
3735 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3736 got_cancel_cb cancel_cb, void *cancel_arg)
3738 const struct got_error *err = NULL;
3739 char *fileindex_path = NULL;
3740 struct got_fileindex *fileindex = NULL;
3741 struct got_pathlist_entry *pe;
3743 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3744 if (err)
3745 return err;
3747 TAILQ_FOREACH(pe, paths, entry) {
3748 err = worktree_status(worktree, pe->path, fileindex, repo,
3749 status_cb, status_arg, cancel_cb, cancel_arg,
3750 no_ignores, 0);
3751 if (err)
3752 break;
3754 free(fileindex_path);
3755 got_fileindex_free(fileindex);
3756 return err;
3759 const struct got_error *
3760 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3761 const char *arg)
3763 const struct got_error *err = NULL;
3764 char *resolved = NULL, *cwd = NULL, *path = NULL;
3765 size_t len;
3766 struct stat sb;
3767 char *abspath = NULL;
3768 char canonpath[PATH_MAX];
3770 *wt_path = NULL;
3772 cwd = getcwd(NULL, 0);
3773 if (cwd == NULL)
3774 return got_error_from_errno("getcwd");
3776 if (lstat(arg, &sb) == -1) {
3777 if (errno != ENOENT) {
3778 err = got_error_from_errno2("lstat", arg);
3779 goto done;
3781 sb.st_mode = 0;
3783 if (S_ISLNK(sb.st_mode)) {
3785 * We cannot use realpath(3) with symlinks since we want to
3786 * operate on the symlink itself.
3787 * But we can make the path absolute, assuming it is relative
3788 * to the current working directory, and then canonicalize it.
3790 if (!got_path_is_absolute(arg)) {
3791 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3792 err = got_error_from_errno("asprintf");
3793 goto done;
3797 err = got_canonpath(abspath ? abspath : arg, canonpath,
3798 sizeof(canonpath));
3799 if (err)
3800 goto done;
3801 resolved = strdup(canonpath);
3802 if (resolved == NULL) {
3803 err = got_error_from_errno("strdup");
3804 goto done;
3806 } else {
3807 resolved = realpath(arg, NULL);
3808 if (resolved == NULL) {
3809 if (errno != ENOENT) {
3810 err = got_error_from_errno2("realpath", arg);
3811 goto done;
3813 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3814 err = got_error_from_errno("asprintf");
3815 goto done;
3817 err = got_canonpath(abspath, canonpath,
3818 sizeof(canonpath));
3819 if (err)
3820 goto done;
3821 resolved = strdup(canonpath);
3822 if (resolved == NULL) {
3823 err = got_error_from_errno("strdup");
3824 goto done;
3829 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3830 strlen(got_worktree_get_root_path(worktree)))) {
3831 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3832 goto done;
3835 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3836 err = got_path_skip_common_ancestor(&path,
3837 got_worktree_get_root_path(worktree), resolved);
3838 if (err)
3839 goto done;
3840 } else {
3841 path = strdup("");
3842 if (path == NULL) {
3843 err = got_error_from_errno("strdup");
3844 goto done;
3848 /* XXX status walk can't deal with trailing slash! */
3849 len = strlen(path);
3850 while (len > 0 && path[len - 1] == '/') {
3851 path[len - 1] = '\0';
3852 len--;
3854 done:
3855 free(abspath);
3856 free(resolved);
3857 free(cwd);
3858 if (err == NULL)
3859 *wt_path = path;
3860 else
3861 free(path);
3862 return err;
3865 struct schedule_addition_args {
3866 struct got_worktree *worktree;
3867 struct got_fileindex *fileindex;
3868 got_worktree_checkout_cb progress_cb;
3869 void *progress_arg;
3870 struct got_repository *repo;
3873 static const struct got_error *
3874 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3875 const char *relpath, struct got_object_id *blob_id,
3876 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3877 int dirfd, const char *de_name)
3879 struct schedule_addition_args *a = arg;
3880 const struct got_error *err = NULL;
3881 struct got_fileindex_entry *ie;
3882 struct stat sb;
3883 char *ondisk_path;
3885 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3886 relpath) == -1)
3887 return got_error_from_errno("asprintf");
3889 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3890 if (ie) {
3891 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3892 de_name, a->repo);
3893 if (err)
3894 goto done;
3895 /* Re-adding an existing entry is a no-op. */
3896 if (status == GOT_STATUS_ADD)
3897 goto done;
3898 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3899 if (err)
3900 goto done;
3903 if (status != GOT_STATUS_UNVERSIONED) {
3904 if (status == GOT_STATUS_NONEXISTENT)
3905 err = got_error_set_errno(ENOENT, ondisk_path);
3906 else
3907 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3908 goto done;
3911 err = got_fileindex_entry_alloc(&ie, relpath);
3912 if (err)
3913 goto done;
3914 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3915 relpath, NULL, NULL, 1);
3916 if (err) {
3917 got_fileindex_entry_free(ie);
3918 goto done;
3920 err = got_fileindex_entry_add(a->fileindex, ie);
3921 if (err) {
3922 got_fileindex_entry_free(ie);
3923 goto done;
3925 done:
3926 free(ondisk_path);
3927 if (err)
3928 return err;
3929 if (status == GOT_STATUS_ADD)
3930 return NULL;
3931 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3934 const struct got_error *
3935 got_worktree_schedule_add(struct got_worktree *worktree,
3936 struct got_pathlist_head *paths,
3937 got_worktree_checkout_cb progress_cb, void *progress_arg,
3938 struct got_repository *repo, int no_ignores)
3940 struct got_fileindex *fileindex = NULL;
3941 char *fileindex_path = NULL;
3942 const struct got_error *err = NULL, *sync_err, *unlockerr;
3943 struct got_pathlist_entry *pe;
3944 struct schedule_addition_args saa;
3946 err = lock_worktree(worktree, LOCK_EX);
3947 if (err)
3948 return err;
3950 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3951 if (err)
3952 goto done;
3954 saa.worktree = worktree;
3955 saa.fileindex = fileindex;
3956 saa.progress_cb = progress_cb;
3957 saa.progress_arg = progress_arg;
3958 saa.repo = repo;
3960 TAILQ_FOREACH(pe, paths, entry) {
3961 err = worktree_status(worktree, pe->path, fileindex, repo,
3962 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3963 if (err)
3964 break;
3966 sync_err = sync_fileindex(fileindex, fileindex_path);
3967 if (sync_err && err == NULL)
3968 err = sync_err;
3969 done:
3970 free(fileindex_path);
3971 if (fileindex)
3972 got_fileindex_free(fileindex);
3973 unlockerr = lock_worktree(worktree, LOCK_SH);
3974 if (unlockerr && err == NULL)
3975 err = unlockerr;
3976 return err;
3979 struct schedule_deletion_args {
3980 struct got_worktree *worktree;
3981 struct got_fileindex *fileindex;
3982 got_worktree_delete_cb progress_cb;
3983 void *progress_arg;
3984 struct got_repository *repo;
3985 int delete_local_mods;
3986 int keep_on_disk;
3987 int ignore_missing_paths;
3988 const char *status_codes;
3991 static const struct got_error *
3992 schedule_for_deletion(void *arg, unsigned char status,
3993 unsigned char staged_status, const char *relpath,
3994 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3995 struct got_object_id *commit_id, int dirfd, const char *de_name)
3997 struct schedule_deletion_args *a = arg;
3998 const struct got_error *err = NULL;
3999 struct got_fileindex_entry *ie = NULL;
4000 struct stat sb;
4001 char *ondisk_path;
4003 if (status == GOT_STATUS_NONEXISTENT) {
4004 if (a->ignore_missing_paths)
4005 return NULL;
4006 return got_error_set_errno(ENOENT, relpath);
4009 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4010 if (ie == NULL)
4011 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4013 staged_status = get_staged_status(ie);
4014 if (staged_status != GOT_STATUS_NO_CHANGE) {
4015 if (staged_status == GOT_STATUS_DELETE)
4016 return NULL;
4017 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4020 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4021 relpath) == -1)
4022 return got_error_from_errno("asprintf");
4024 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4025 a->repo);
4026 if (err)
4027 goto done;
4029 if (a->status_codes) {
4030 size_t ncodes = strlen(a->status_codes);
4031 int i;
4032 for (i = 0; i < ncodes ; i++) {
4033 if (status == a->status_codes[i])
4034 break;
4036 if (i == ncodes) {
4037 /* Do not delete files in non-matching status. */
4038 free(ondisk_path);
4039 return NULL;
4041 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4042 a->status_codes[i] != GOT_STATUS_MISSING) {
4043 static char msg[64];
4044 snprintf(msg, sizeof(msg),
4045 "invalid status code '%c'", a->status_codes[i]);
4046 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4047 goto done;
4051 if (status != GOT_STATUS_NO_CHANGE) {
4052 if (status == GOT_STATUS_DELETE)
4053 goto done;
4054 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4055 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4056 goto done;
4058 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4059 err = got_error_set_errno(ENOENT, relpath);
4060 goto done;
4062 if (status != GOT_STATUS_MODIFY &&
4063 status != GOT_STATUS_MISSING) {
4064 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4065 goto done;
4069 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4070 size_t root_len;
4072 if (dirfd != -1) {
4073 if (unlinkat(dirfd, de_name, 0) != 0) {
4074 err = got_error_from_errno2("unlinkat",
4075 ondisk_path);
4076 goto done;
4078 } else if (unlink(ondisk_path) != 0) {
4079 err = got_error_from_errno2("unlink", ondisk_path);
4080 goto done;
4083 root_len = strlen(a->worktree->root_path);
4084 do {
4085 char *parent;
4086 err = got_path_dirname(&parent, ondisk_path);
4087 if (err)
4088 goto done;
4089 free(ondisk_path);
4090 ondisk_path = parent;
4091 if (rmdir(ondisk_path) == -1) {
4092 if (errno != ENOTEMPTY)
4093 err = got_error_from_errno2("rmdir",
4094 ondisk_path);
4095 break;
4097 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4098 strlen(ondisk_path), root_len) != 0);
4101 got_fileindex_entry_mark_deleted_from_disk(ie);
4102 done:
4103 free(ondisk_path);
4104 if (err)
4105 return err;
4106 if (status == GOT_STATUS_DELETE)
4107 return NULL;
4108 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4109 staged_status, relpath);
4112 const struct got_error *
4113 got_worktree_schedule_delete(struct got_worktree *worktree,
4114 struct got_pathlist_head *paths, int delete_local_mods,
4115 const char *status_codes,
4116 got_worktree_delete_cb progress_cb, void *progress_arg,
4117 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4119 struct got_fileindex *fileindex = NULL;
4120 char *fileindex_path = NULL;
4121 const struct got_error *err = NULL, *sync_err, *unlockerr;
4122 struct got_pathlist_entry *pe;
4123 struct schedule_deletion_args sda;
4125 err = lock_worktree(worktree, LOCK_EX);
4126 if (err)
4127 return err;
4129 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4130 if (err)
4131 goto done;
4133 sda.worktree = worktree;
4134 sda.fileindex = fileindex;
4135 sda.progress_cb = progress_cb;
4136 sda.progress_arg = progress_arg;
4137 sda.repo = repo;
4138 sda.delete_local_mods = delete_local_mods;
4139 sda.keep_on_disk = keep_on_disk;
4140 sda.ignore_missing_paths = ignore_missing_paths;
4141 sda.status_codes = status_codes;
4143 TAILQ_FOREACH(pe, paths, entry) {
4144 err = worktree_status(worktree, pe->path, fileindex, repo,
4145 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4146 if (err)
4147 break;
4149 sync_err = sync_fileindex(fileindex, fileindex_path);
4150 if (sync_err && err == NULL)
4151 err = sync_err;
4152 done:
4153 free(fileindex_path);
4154 if (fileindex)
4155 got_fileindex_free(fileindex);
4156 unlockerr = lock_worktree(worktree, LOCK_SH);
4157 if (unlockerr && err == NULL)
4158 err = unlockerr;
4159 return err;
4162 static const struct got_error *
4163 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4165 const struct got_error *err = NULL;
4166 char *line = NULL;
4167 size_t linesize = 0, n;
4168 ssize_t linelen;
4170 linelen = getline(&line, &linesize, infile);
4171 if (linelen == -1) {
4172 if (ferror(infile)) {
4173 err = got_error_from_errno("getline");
4174 goto done;
4176 return NULL;
4178 if (outfile) {
4179 n = fwrite(line, 1, linelen, outfile);
4180 if (n != linelen) {
4181 err = got_ferror(outfile, GOT_ERR_IO);
4182 goto done;
4185 if (rejectfile) {
4186 n = fwrite(line, 1, linelen, rejectfile);
4187 if (n != linelen)
4188 err = got_ferror(outfile, GOT_ERR_IO);
4190 done:
4191 free(line);
4192 return err;
4195 static const struct got_error *
4196 skip_one_line(FILE *f)
4198 char *line = NULL;
4199 size_t linesize = 0;
4200 ssize_t linelen;
4202 linelen = getline(&line, &linesize, f);
4203 if (linelen == -1) {
4204 if (ferror(f))
4205 return got_error_from_errno("getline");
4206 return NULL;
4208 free(line);
4209 return NULL;
4212 static const struct got_error *
4213 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4214 int start_old, int end_old, int start_new, int end_new,
4215 FILE *outfile, FILE *rejectfile)
4217 const struct got_error *err;
4219 /* Copy old file's lines leading up to patch. */
4220 while (!feof(f1) && *line_cur1 < start_old) {
4221 err = copy_one_line(f1, outfile, NULL);
4222 if (err)
4223 return err;
4224 (*line_cur1)++;
4226 /* Skip new file's lines leading up to patch. */
4227 while (!feof(f2) && *line_cur2 < start_new) {
4228 if (rejectfile)
4229 err = copy_one_line(f2, NULL, rejectfile);
4230 else
4231 err = skip_one_line(f2);
4232 if (err)
4233 return err;
4234 (*line_cur2)++;
4236 /* Copy patched lines. */
4237 while (!feof(f2) && *line_cur2 <= end_new) {
4238 err = copy_one_line(f2, outfile, NULL);
4239 if (err)
4240 return err;
4241 (*line_cur2)++;
4243 /* Skip over old file's replaced lines. */
4244 while (!feof(f1) && *line_cur1 <= end_old) {
4245 if (rejectfile)
4246 err = copy_one_line(f1, NULL, rejectfile);
4247 else
4248 err = skip_one_line(f1);
4249 if (err)
4250 return err;
4251 (*line_cur1)++;
4254 return NULL;
4257 static const struct got_error *
4258 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4259 FILE *outfile, FILE *rejectfile)
4261 const struct got_error *err;
4263 if (outfile) {
4264 /* Copy old file's lines until EOF. */
4265 while (!feof(f1)) {
4266 err = copy_one_line(f1, outfile, NULL);
4267 if (err)
4268 return err;
4269 (*line_cur1)++;
4272 if (rejectfile) {
4273 /* Copy new file's lines until EOF. */
4274 while (!feof(f2)) {
4275 err = copy_one_line(f2, NULL, rejectfile);
4276 if (err)
4277 return err;
4278 (*line_cur2)++;
4282 return NULL;
4285 static const struct got_error *
4286 apply_or_reject_change(int *choice, int *nchunks_used,
4287 struct diff_result *diff_result, int n,
4288 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4289 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4290 got_worktree_patch_cb patch_cb, void *patch_arg)
4292 const struct got_error *err = NULL;
4293 struct diff_chunk_context cc = {};
4294 int start_old, end_old, start_new, end_new;
4295 FILE *hunkfile;
4296 struct diff_output_unidiff_state *diff_state;
4297 struct diff_input_info diff_info;
4298 int rc;
4300 *choice = GOT_PATCH_CHOICE_NONE;
4302 /* Get changed line numbers without context lines for copy_change(). */
4303 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4304 start_old = cc.left.start;
4305 end_old = cc.left.end;
4306 start_new = cc.right.start;
4307 end_new = cc.right.end;
4309 /* Get the same change with context lines for display. */
4310 memset(&cc, 0, sizeof(cc));
4311 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4313 memset(&diff_info, 0, sizeof(diff_info));
4314 diff_info.left_path = relpath;
4315 diff_info.right_path = relpath;
4317 diff_state = diff_output_unidiff_state_alloc();
4318 if (diff_state == NULL)
4319 return got_error_set_errno(ENOMEM,
4320 "diff_output_unidiff_state_alloc");
4322 hunkfile = got_opentemp();
4323 if (hunkfile == NULL) {
4324 err = got_error_from_errno("got_opentemp");
4325 goto done;
4328 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4329 diff_result, &cc);
4330 if (rc != DIFF_RC_OK) {
4331 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4332 goto done;
4335 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4336 err = got_ferror(hunkfile, GOT_ERR_IO);
4337 goto done;
4340 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4341 hunkfile, changeno, nchanges);
4342 if (err)
4343 goto done;
4345 switch (*choice) {
4346 case GOT_PATCH_CHOICE_YES:
4347 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4348 end_old, start_new, end_new, outfile, rejectfile);
4349 break;
4350 case GOT_PATCH_CHOICE_NO:
4351 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4352 end_old, start_new, end_new, rejectfile, outfile);
4353 break;
4354 case GOT_PATCH_CHOICE_QUIT:
4355 break;
4356 default:
4357 err = got_error(GOT_ERR_PATCH_CHOICE);
4358 break;
4360 done:
4361 diff_output_unidiff_state_free(diff_state);
4362 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4363 err = got_error_from_errno("fclose");
4364 return err;
4367 struct revert_file_args {
4368 struct got_worktree *worktree;
4369 struct got_fileindex *fileindex;
4370 got_worktree_checkout_cb progress_cb;
4371 void *progress_arg;
4372 got_worktree_patch_cb patch_cb;
4373 void *patch_arg;
4374 struct got_repository *repo;
4375 int unlink_added_files;
4378 static const struct got_error *
4379 create_patched_content(char **path_outfile, int reverse_patch,
4380 struct got_object_id *blob_id, const char *path2,
4381 int dirfd2, const char *de_name2,
4382 const char *relpath, struct got_repository *repo,
4383 got_worktree_patch_cb patch_cb, void *patch_arg)
4385 const struct got_error *err, *free_err;
4386 struct got_blob_object *blob = NULL;
4387 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4388 int fd2 = -1;
4389 char link_target[PATH_MAX];
4390 ssize_t link_len = 0;
4391 char *path1 = NULL, *id_str = NULL;
4392 struct stat sb2;
4393 struct got_diffreg_result *diffreg_result = NULL;
4394 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4395 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4397 *path_outfile = NULL;
4399 err = got_object_id_str(&id_str, blob_id);
4400 if (err)
4401 return err;
4403 if (dirfd2 != -1) {
4404 fd2 = openat(dirfd2, de_name2,
4405 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4406 if (fd2 == -1) {
4407 if (!got_err_open_nofollow_on_symlink()) {
4408 err = got_error_from_errno2("openat", path2);
4409 goto done;
4411 link_len = readlinkat(dirfd2, de_name2,
4412 link_target, sizeof(link_target));
4413 if (link_len == -1) {
4414 return got_error_from_errno2("readlinkat",
4415 path2);
4417 sb2.st_mode = S_IFLNK;
4418 sb2.st_size = link_len;
4420 } else {
4421 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4422 if (fd2 == -1) {
4423 if (!got_err_open_nofollow_on_symlink()) {
4424 err = got_error_from_errno2("open", path2);
4425 goto done;
4427 link_len = readlink(path2, link_target,
4428 sizeof(link_target));
4429 if (link_len == -1)
4430 return got_error_from_errno2("readlink", path2);
4431 sb2.st_mode = S_IFLNK;
4432 sb2.st_size = link_len;
4435 if (fd2 != -1) {
4436 if (fstat(fd2, &sb2) == -1) {
4437 err = got_error_from_errno2("fstat", path2);
4438 goto done;
4441 f2 = fdopen(fd2, "r");
4442 if (f2 == NULL) {
4443 err = got_error_from_errno2("fdopen", path2);
4444 goto done;
4446 fd2 = -1;
4447 } else {
4448 size_t n;
4449 f2 = got_opentemp();
4450 if (f2 == NULL) {
4451 err = got_error_from_errno2("got_opentemp", path2);
4452 goto done;
4454 n = fwrite(link_target, 1, link_len, f2);
4455 if (n != link_len) {
4456 err = got_ferror(f2, GOT_ERR_IO);
4457 goto done;
4459 if (fflush(f2) == EOF) {
4460 err = got_error_from_errno("fflush");
4461 goto done;
4463 rewind(f2);
4466 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4467 if (err)
4468 goto done;
4470 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4471 if (err)
4472 goto done;
4474 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4475 if (err)
4476 goto done;
4478 err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4479 NULL);
4480 if (err)
4481 goto done;
4483 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4484 if (err)
4485 goto done;
4487 if (fseek(f1, 0L, SEEK_SET) == -1)
4488 return got_ferror(f1, GOT_ERR_IO);
4489 if (fseek(f2, 0L, SEEK_SET) == -1)
4490 return got_ferror(f2, GOT_ERR_IO);
4492 /* Count the number of actual changes in the diff result. */
4493 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4494 struct diff_chunk_context cc = {};
4495 diff_chunk_context_load_change(&cc, &nchunks_used,
4496 diffreg_result->result, n, 0);
4497 nchanges++;
4499 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4500 int choice;
4501 err = apply_or_reject_change(&choice, &nchunks_used,
4502 diffreg_result->result, n, relpath, f1, f2,
4503 &line_cur1, &line_cur2,
4504 reverse_patch ? NULL : outfile,
4505 reverse_patch ? outfile : NULL,
4506 ++i, nchanges, patch_cb, patch_arg);
4507 if (err)
4508 goto done;
4509 if (choice == GOT_PATCH_CHOICE_YES)
4510 have_content = 1;
4511 else if (choice == GOT_PATCH_CHOICE_QUIT)
4512 break;
4514 if (have_content) {
4515 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4516 reverse_patch ? NULL : outfile,
4517 reverse_patch ? outfile : NULL);
4518 if (err)
4519 goto done;
4521 if (!S_ISLNK(sb2.st_mode)) {
4522 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4523 err = got_error_from_errno2("fchmod", path2);
4524 goto done;
4528 done:
4529 free(id_str);
4530 if (blob)
4531 got_object_blob_close(blob);
4532 free_err = got_diffreg_result_free(diffreg_result);
4533 if (err == NULL)
4534 err = free_err;
4535 if (f1 && fclose(f1) == EOF && err == NULL)
4536 err = got_error_from_errno2("fclose", path1);
4537 if (f2 && fclose(f2) == EOF && err == NULL)
4538 err = got_error_from_errno2("fclose", path2);
4539 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4540 err = got_error_from_errno2("close", path2);
4541 if (outfile && fclose(outfile) == EOF && err == NULL)
4542 err = got_error_from_errno2("fclose", *path_outfile);
4543 if (path1 && unlink(path1) == -1 && err == NULL)
4544 err = got_error_from_errno2("unlink", path1);
4545 if (err || !have_content) {
4546 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4547 err = got_error_from_errno2("unlink", *path_outfile);
4548 free(*path_outfile);
4549 *path_outfile = NULL;
4551 free(path1);
4552 return err;
4555 static const struct got_error *
4556 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4557 const char *relpath, struct got_object_id *blob_id,
4558 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4559 int dirfd, const char *de_name)
4561 struct revert_file_args *a = arg;
4562 const struct got_error *err = NULL;
4563 char *parent_path = NULL;
4564 struct got_fileindex_entry *ie;
4565 struct got_commit_object *base_commit = NULL;
4566 struct got_tree_object *tree = NULL;
4567 struct got_object_id *tree_id = NULL;
4568 const struct got_tree_entry *te = NULL;
4569 char *tree_path = NULL, *te_name;
4570 char *ondisk_path = NULL, *path_content = NULL;
4571 struct got_blob_object *blob = NULL;
4573 /* Reverting a staged deletion is a no-op. */
4574 if (status == GOT_STATUS_DELETE &&
4575 staged_status != GOT_STATUS_NO_CHANGE)
4576 return NULL;
4578 if (status == GOT_STATUS_UNVERSIONED)
4579 return (*a->progress_cb)(a->progress_arg,
4580 GOT_STATUS_UNVERSIONED, relpath);
4582 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4583 if (ie == NULL)
4584 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4586 /* Construct in-repository path of tree which contains this blob. */
4587 err = got_path_dirname(&parent_path, ie->path);
4588 if (err) {
4589 if (err->code != GOT_ERR_BAD_PATH)
4590 goto done;
4591 parent_path = strdup("/");
4592 if (parent_path == NULL) {
4593 err = got_error_from_errno("strdup");
4594 goto done;
4597 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4598 tree_path = strdup(parent_path);
4599 if (tree_path == NULL) {
4600 err = got_error_from_errno("strdup");
4601 goto done;
4603 } else {
4604 if (got_path_is_root_dir(parent_path)) {
4605 tree_path = strdup(a->worktree->path_prefix);
4606 if (tree_path == NULL) {
4607 err = got_error_from_errno("strdup");
4608 goto done;
4610 } else {
4611 if (asprintf(&tree_path, "%s/%s",
4612 a->worktree->path_prefix, parent_path) == -1) {
4613 err = got_error_from_errno("asprintf");
4614 goto done;
4619 err = got_object_open_as_commit(&base_commit, a->repo,
4620 a->worktree->base_commit_id);
4621 if (err)
4622 goto done;
4624 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4625 if (err) {
4626 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4627 (status == GOT_STATUS_ADD ||
4628 staged_status == GOT_STATUS_ADD)))
4629 goto done;
4630 } else {
4631 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4632 if (err)
4633 goto done;
4635 err = got_path_basename(&te_name, ie->path);
4636 if (err)
4637 goto done;
4639 te = got_object_tree_find_entry(tree, te_name);
4640 free(te_name);
4641 if (te == NULL && status != GOT_STATUS_ADD &&
4642 staged_status != GOT_STATUS_ADD) {
4643 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4644 goto done;
4648 switch (status) {
4649 case GOT_STATUS_ADD:
4650 if (a->patch_cb) {
4651 int choice = GOT_PATCH_CHOICE_NONE;
4652 err = (*a->patch_cb)(&choice, a->patch_arg,
4653 status, ie->path, NULL, 1, 1);
4654 if (err)
4655 goto done;
4656 if (choice != GOT_PATCH_CHOICE_YES)
4657 break;
4659 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4660 ie->path);
4661 if (err)
4662 goto done;
4663 got_fileindex_entry_remove(a->fileindex, ie);
4664 if (a->unlink_added_files) {
4665 if (asprintf(&ondisk_path, "%s/%s",
4666 got_worktree_get_root_path(a->worktree),
4667 relpath) == -1) {
4668 err = got_error_from_errno("asprintf");
4669 goto done;
4671 if (unlink(ondisk_path) == -1) {
4672 err = got_error_from_errno2("unlink",
4673 ondisk_path);
4674 break;
4677 break;
4678 case GOT_STATUS_DELETE:
4679 if (a->patch_cb) {
4680 int choice = GOT_PATCH_CHOICE_NONE;
4681 err = (*a->patch_cb)(&choice, a->patch_arg,
4682 status, ie->path, NULL, 1, 1);
4683 if (err)
4684 goto done;
4685 if (choice != GOT_PATCH_CHOICE_YES)
4686 break;
4688 /* fall through */
4689 case GOT_STATUS_MODIFY:
4690 case GOT_STATUS_MODE_CHANGE:
4691 case GOT_STATUS_CONFLICT:
4692 case GOT_STATUS_MISSING: {
4693 struct got_object_id id;
4694 if (staged_status == GOT_STATUS_ADD ||
4695 staged_status == GOT_STATUS_MODIFY) {
4696 memcpy(id.sha1, ie->staged_blob_sha1,
4697 SHA1_DIGEST_LENGTH);
4698 } else
4699 memcpy(id.sha1, ie->blob_sha1,
4700 SHA1_DIGEST_LENGTH);
4701 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4702 if (err)
4703 goto done;
4705 if (asprintf(&ondisk_path, "%s/%s",
4706 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4707 err = got_error_from_errno("asprintf");
4708 goto done;
4711 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4712 status == GOT_STATUS_CONFLICT)) {
4713 int is_bad_symlink = 0;
4714 err = create_patched_content(&path_content, 1, &id,
4715 ondisk_path, dirfd, de_name, ie->path, a->repo,
4716 a->patch_cb, a->patch_arg);
4717 if (err || path_content == NULL)
4718 break;
4719 if (te && S_ISLNK(te->mode)) {
4720 if (unlink(path_content) == -1) {
4721 err = got_error_from_errno2("unlink",
4722 path_content);
4723 break;
4725 err = install_symlink(&is_bad_symlink,
4726 a->worktree, ondisk_path, ie->path,
4727 blob, 0, 1, 0, 0, a->repo,
4728 a->progress_cb, a->progress_arg);
4729 } else {
4730 if (rename(path_content, ondisk_path) == -1) {
4731 err = got_error_from_errno3("rename",
4732 path_content, ondisk_path);
4733 goto done;
4736 } else {
4737 int is_bad_symlink = 0;
4738 if (te && S_ISLNK(te->mode)) {
4739 err = install_symlink(&is_bad_symlink,
4740 a->worktree, ondisk_path, ie->path,
4741 blob, 0, 1, 0, 0, a->repo,
4742 a->progress_cb, a->progress_arg);
4743 } else {
4744 err = install_blob(a->worktree, ondisk_path,
4745 ie->path,
4746 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4747 got_fileindex_perms_to_st(ie), blob,
4748 0, 1, 0, 0, a->repo,
4749 a->progress_cb, a->progress_arg);
4751 if (err)
4752 goto done;
4753 if (status == GOT_STATUS_DELETE ||
4754 status == GOT_STATUS_MODE_CHANGE) {
4755 err = got_fileindex_entry_update(ie,
4756 a->worktree->root_fd, relpath,
4757 blob->id.sha1,
4758 a->worktree->base_commit_id->sha1, 1);
4759 if (err)
4760 goto done;
4762 if (is_bad_symlink) {
4763 got_fileindex_entry_filetype_set(ie,
4764 GOT_FILEIDX_MODE_BAD_SYMLINK);
4767 break;
4769 default:
4770 break;
4772 done:
4773 free(ondisk_path);
4774 free(path_content);
4775 free(parent_path);
4776 free(tree_path);
4777 if (blob)
4778 got_object_blob_close(blob);
4779 if (tree)
4780 got_object_tree_close(tree);
4781 free(tree_id);
4782 if (base_commit)
4783 got_object_commit_close(base_commit);
4784 return err;
4787 const struct got_error *
4788 got_worktree_revert(struct got_worktree *worktree,
4789 struct got_pathlist_head *paths,
4790 got_worktree_checkout_cb progress_cb, void *progress_arg,
4791 got_worktree_patch_cb patch_cb, void *patch_arg,
4792 struct got_repository *repo)
4794 struct got_fileindex *fileindex = NULL;
4795 char *fileindex_path = NULL;
4796 const struct got_error *err = NULL, *unlockerr = NULL;
4797 const struct got_error *sync_err = NULL;
4798 struct got_pathlist_entry *pe;
4799 struct revert_file_args rfa;
4801 err = lock_worktree(worktree, LOCK_EX);
4802 if (err)
4803 return err;
4805 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4806 if (err)
4807 goto done;
4809 rfa.worktree = worktree;
4810 rfa.fileindex = fileindex;
4811 rfa.progress_cb = progress_cb;
4812 rfa.progress_arg = progress_arg;
4813 rfa.patch_cb = patch_cb;
4814 rfa.patch_arg = patch_arg;
4815 rfa.repo = repo;
4816 rfa.unlink_added_files = 0;
4817 TAILQ_FOREACH(pe, paths, entry) {
4818 err = worktree_status(worktree, pe->path, fileindex, repo,
4819 revert_file, &rfa, NULL, NULL, 1, 0);
4820 if (err)
4821 break;
4823 sync_err = sync_fileindex(fileindex, fileindex_path);
4824 if (sync_err && err == NULL)
4825 err = sync_err;
4826 done:
4827 free(fileindex_path);
4828 if (fileindex)
4829 got_fileindex_free(fileindex);
4830 unlockerr = lock_worktree(worktree, LOCK_SH);
4831 if (unlockerr && err == NULL)
4832 err = unlockerr;
4833 return err;
4836 static void
4837 free_commitable(struct got_commitable *ct)
4839 free(ct->path);
4840 free(ct->in_repo_path);
4841 free(ct->ondisk_path);
4842 free(ct->blob_id);
4843 free(ct->base_blob_id);
4844 free(ct->staged_blob_id);
4845 free(ct->base_commit_id);
4846 free(ct);
4849 struct collect_commitables_arg {
4850 struct got_pathlist_head *commitable_paths;
4851 struct got_repository *repo;
4852 struct got_worktree *worktree;
4853 struct got_fileindex *fileindex;
4854 int have_staged_files;
4855 int allow_bad_symlinks;
4858 static const struct got_error *
4859 collect_commitables(void *arg, unsigned char status,
4860 unsigned char staged_status, const char *relpath,
4861 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4862 struct got_object_id *commit_id, int dirfd, const char *de_name)
4864 struct collect_commitables_arg *a = arg;
4865 const struct got_error *err = NULL;
4866 struct got_commitable *ct = NULL;
4867 struct got_pathlist_entry *new = NULL;
4868 char *parent_path = NULL, *path = NULL;
4869 struct stat sb;
4871 if (a->have_staged_files) {
4872 if (staged_status != GOT_STATUS_MODIFY &&
4873 staged_status != GOT_STATUS_ADD &&
4874 staged_status != GOT_STATUS_DELETE)
4875 return NULL;
4876 } else {
4877 if (status == GOT_STATUS_CONFLICT)
4878 return got_error(GOT_ERR_COMMIT_CONFLICT);
4880 if (status != GOT_STATUS_MODIFY &&
4881 status != GOT_STATUS_MODE_CHANGE &&
4882 status != GOT_STATUS_ADD &&
4883 status != GOT_STATUS_DELETE)
4884 return NULL;
4887 if (asprintf(&path, "/%s", relpath) == -1) {
4888 err = got_error_from_errno("asprintf");
4889 goto done;
4891 if (strcmp(path, "/") == 0) {
4892 parent_path = strdup("");
4893 if (parent_path == NULL)
4894 return got_error_from_errno("strdup");
4895 } else {
4896 err = got_path_dirname(&parent_path, path);
4897 if (err)
4898 return err;
4901 ct = calloc(1, sizeof(*ct));
4902 if (ct == NULL) {
4903 err = got_error_from_errno("calloc");
4904 goto done;
4907 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4908 relpath) == -1) {
4909 err = got_error_from_errno("asprintf");
4910 goto done;
4913 if (staged_status == GOT_STATUS_ADD ||
4914 staged_status == GOT_STATUS_MODIFY) {
4915 struct got_fileindex_entry *ie;
4916 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4917 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4918 case GOT_FILEIDX_MODE_REGULAR_FILE:
4919 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4920 ct->mode = S_IFREG;
4921 break;
4922 case GOT_FILEIDX_MODE_SYMLINK:
4923 ct->mode = S_IFLNK;
4924 break;
4925 default:
4926 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4927 goto done;
4929 ct->mode |= got_fileindex_entry_perms_get(ie);
4930 } else if (status != GOT_STATUS_DELETE &&
4931 staged_status != GOT_STATUS_DELETE) {
4932 if (dirfd != -1) {
4933 if (fstatat(dirfd, de_name, &sb,
4934 AT_SYMLINK_NOFOLLOW) == -1) {
4935 err = got_error_from_errno2("fstatat",
4936 ct->ondisk_path);
4937 goto done;
4939 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4940 err = got_error_from_errno2("lstat", ct->ondisk_path);
4941 goto done;
4943 ct->mode = sb.st_mode;
4946 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4947 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4948 relpath) == -1) {
4949 err = got_error_from_errno("asprintf");
4950 goto done;
4953 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4954 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4955 int is_bad_symlink;
4956 char target_path[PATH_MAX];
4957 ssize_t target_len;
4958 target_len = readlink(ct->ondisk_path, target_path,
4959 sizeof(target_path));
4960 if (target_len == -1) {
4961 err = got_error_from_errno2("readlink",
4962 ct->ondisk_path);
4963 goto done;
4965 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4966 target_len, ct->ondisk_path, a->worktree->root_path);
4967 if (err)
4968 goto done;
4969 if (is_bad_symlink) {
4970 err = got_error_path(ct->ondisk_path,
4971 GOT_ERR_BAD_SYMLINK);
4972 goto done;
4977 ct->status = status;
4978 ct->staged_status = staged_status;
4979 ct->blob_id = NULL; /* will be filled in when blob gets created */
4980 if (ct->status != GOT_STATUS_ADD &&
4981 ct->staged_status != GOT_STATUS_ADD) {
4982 ct->base_blob_id = got_object_id_dup(blob_id);
4983 if (ct->base_blob_id == NULL) {
4984 err = got_error_from_errno("got_object_id_dup");
4985 goto done;
4987 ct->base_commit_id = got_object_id_dup(commit_id);
4988 if (ct->base_commit_id == NULL) {
4989 err = got_error_from_errno("got_object_id_dup");
4990 goto done;
4993 if (ct->staged_status == GOT_STATUS_ADD ||
4994 ct->staged_status == GOT_STATUS_MODIFY) {
4995 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4996 if (ct->staged_blob_id == NULL) {
4997 err = got_error_from_errno("got_object_id_dup");
4998 goto done;
5001 ct->path = strdup(path);
5002 if (ct->path == NULL) {
5003 err = got_error_from_errno("strdup");
5004 goto done;
5006 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5007 done:
5008 if (ct && (err || new == NULL))
5009 free_commitable(ct);
5010 free(parent_path);
5011 free(path);
5012 return err;
5015 static const struct got_error *write_tree(struct got_object_id **, int *,
5016 struct got_tree_object *, const char *, struct got_pathlist_head *,
5017 got_worktree_status_cb status_cb, void *status_arg,
5018 struct got_repository *);
5020 static const struct got_error *
5021 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5022 struct got_tree_entry *te, const char *parent_path,
5023 struct got_pathlist_head *commitable_paths,
5024 got_worktree_status_cb status_cb, void *status_arg,
5025 struct got_repository *repo)
5027 const struct got_error *err = NULL;
5028 struct got_tree_object *subtree;
5029 char *subpath;
5031 if (asprintf(&subpath, "%s%s%s", parent_path,
5032 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5033 return got_error_from_errno("asprintf");
5035 err = got_object_open_as_tree(&subtree, repo, &te->id);
5036 if (err)
5037 return err;
5039 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5040 commitable_paths, status_cb, status_arg, repo);
5041 got_object_tree_close(subtree);
5042 free(subpath);
5043 return err;
5046 static const struct got_error *
5047 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5049 const struct got_error *err = NULL;
5050 char *ct_parent_path = NULL;
5052 *match = 0;
5054 if (strchr(ct->in_repo_path, '/') == NULL) {
5055 *match = got_path_is_root_dir(path);
5056 return NULL;
5059 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5060 if (err)
5061 return err;
5062 *match = (strcmp(path, ct_parent_path) == 0);
5063 free(ct_parent_path);
5064 return err;
5067 static mode_t
5068 get_ct_file_mode(struct got_commitable *ct)
5070 if (S_ISLNK(ct->mode))
5071 return S_IFLNK;
5073 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5076 static const struct got_error *
5077 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5078 struct got_tree_entry *te, struct got_commitable *ct)
5080 const struct got_error *err = NULL;
5082 *new_te = NULL;
5084 err = got_object_tree_entry_dup(new_te, te);
5085 if (err)
5086 goto done;
5088 (*new_te)->mode = get_ct_file_mode(ct);
5090 if (ct->staged_status == GOT_STATUS_MODIFY)
5091 memcpy(&(*new_te)->id, ct->staged_blob_id,
5092 sizeof((*new_te)->id));
5093 else
5094 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5095 done:
5096 if (err && *new_te) {
5097 free(*new_te);
5098 *new_te = NULL;
5100 return err;
5103 static const struct got_error *
5104 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5105 struct got_commitable *ct)
5107 const struct got_error *err = NULL;
5108 char *ct_name = NULL;
5110 *new_te = NULL;
5112 *new_te = calloc(1, sizeof(**new_te));
5113 if (*new_te == NULL)
5114 return got_error_from_errno("calloc");
5116 err = got_path_basename(&ct_name, ct->path);
5117 if (err)
5118 goto done;
5119 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5120 sizeof((*new_te)->name)) {
5121 err = got_error(GOT_ERR_NO_SPACE);
5122 goto done;
5125 (*new_te)->mode = get_ct_file_mode(ct);
5127 if (ct->staged_status == GOT_STATUS_ADD)
5128 memcpy(&(*new_te)->id, ct->staged_blob_id,
5129 sizeof((*new_te)->id));
5130 else
5131 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5132 done:
5133 free(ct_name);
5134 if (err && *new_te) {
5135 free(*new_te);
5136 *new_te = NULL;
5138 return err;
5141 static const struct got_error *
5142 insert_tree_entry(struct got_tree_entry *new_te,
5143 struct got_pathlist_head *paths)
5145 const struct got_error *err = NULL;
5146 struct got_pathlist_entry *new_pe;
5148 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5149 if (err)
5150 return err;
5151 if (new_pe == NULL)
5152 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5153 return NULL;
5156 static const struct got_error *
5157 report_ct_status(struct got_commitable *ct,
5158 got_worktree_status_cb status_cb, void *status_arg)
5160 const char *ct_path = ct->path;
5161 unsigned char status;
5163 if (status_cb == NULL) /* no commit progress output desired */
5164 return NULL;
5166 while (ct_path[0] == '/')
5167 ct_path++;
5169 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5170 status = ct->staged_status;
5171 else
5172 status = ct->status;
5174 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5175 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5178 static const struct got_error *
5179 match_modified_subtree(int *modified, struct got_tree_entry *te,
5180 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5182 const struct got_error *err = NULL;
5183 struct got_pathlist_entry *pe;
5184 char *te_path;
5186 *modified = 0;
5188 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5189 got_path_is_root_dir(base_tree_path) ? "" : "/",
5190 te->name) == -1)
5191 return got_error_from_errno("asprintf");
5193 TAILQ_FOREACH(pe, commitable_paths, entry) {
5194 struct got_commitable *ct = pe->data;
5195 *modified = got_path_is_child(ct->in_repo_path, te_path,
5196 strlen(te_path));
5197 if (*modified)
5198 break;
5201 free(te_path);
5202 return err;
5205 static const struct got_error *
5206 match_deleted_or_modified_ct(struct got_commitable **ctp,
5207 struct got_tree_entry *te, const char *base_tree_path,
5208 struct got_pathlist_head *commitable_paths)
5210 const struct got_error *err = NULL;
5211 struct got_pathlist_entry *pe;
5213 *ctp = NULL;
5215 TAILQ_FOREACH(pe, commitable_paths, entry) {
5216 struct got_commitable *ct = pe->data;
5217 char *ct_name = NULL;
5218 int path_matches;
5220 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5221 if (ct->status != GOT_STATUS_MODIFY &&
5222 ct->status != GOT_STATUS_MODE_CHANGE &&
5223 ct->status != GOT_STATUS_DELETE)
5224 continue;
5225 } else {
5226 if (ct->staged_status != GOT_STATUS_MODIFY &&
5227 ct->staged_status != GOT_STATUS_DELETE)
5228 continue;
5231 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5232 continue;
5234 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5235 if (err)
5236 return err;
5237 if (!path_matches)
5238 continue;
5240 err = got_path_basename(&ct_name, pe->path);
5241 if (err)
5242 return err;
5244 if (strcmp(te->name, ct_name) != 0) {
5245 free(ct_name);
5246 continue;
5248 free(ct_name);
5250 *ctp = ct;
5251 break;
5254 return err;
5257 static const struct got_error *
5258 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5259 const char *child_path, const char *path_base_tree,
5260 struct got_pathlist_head *commitable_paths,
5261 got_worktree_status_cb status_cb, void *status_arg,
5262 struct got_repository *repo)
5264 const struct got_error *err = NULL;
5265 struct got_tree_entry *new_te;
5266 char *subtree_path;
5267 struct got_object_id *id = NULL;
5268 int nentries;
5270 *new_tep = NULL;
5272 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5273 got_path_is_root_dir(path_base_tree) ? "" : "/",
5274 child_path) == -1)
5275 return got_error_from_errno("asprintf");
5277 new_te = calloc(1, sizeof(*new_te));
5278 if (new_te == NULL)
5279 return got_error_from_errno("calloc");
5280 new_te->mode = S_IFDIR;
5282 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5283 sizeof(new_te->name)) {
5284 err = got_error(GOT_ERR_NO_SPACE);
5285 goto done;
5287 err = write_tree(&id, &nentries, NULL, subtree_path,
5288 commitable_paths, status_cb, status_arg, repo);
5289 if (err) {
5290 free(new_te);
5291 goto done;
5293 memcpy(&new_te->id, id, sizeof(new_te->id));
5294 done:
5295 free(id);
5296 free(subtree_path);
5297 if (err == NULL)
5298 *new_tep = new_te;
5299 return err;
5302 static const struct got_error *
5303 write_tree(struct got_object_id **new_tree_id, int *nentries,
5304 struct got_tree_object *base_tree, const char *path_base_tree,
5305 struct got_pathlist_head *commitable_paths,
5306 got_worktree_status_cb status_cb, void *status_arg,
5307 struct got_repository *repo)
5309 const struct got_error *err = NULL;
5310 struct got_pathlist_head paths;
5311 struct got_tree_entry *te, *new_te = NULL;
5312 struct got_pathlist_entry *pe;
5314 TAILQ_INIT(&paths);
5315 *nentries = 0;
5317 /* Insert, and recurse into, newly added entries first. */
5318 TAILQ_FOREACH(pe, commitable_paths, entry) {
5319 struct got_commitable *ct = pe->data;
5320 char *child_path = NULL, *slash;
5322 if ((ct->status != GOT_STATUS_ADD &&
5323 ct->staged_status != GOT_STATUS_ADD) ||
5324 (ct->flags & GOT_COMMITABLE_ADDED))
5325 continue;
5327 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5328 strlen(path_base_tree)))
5329 continue;
5331 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5332 ct->in_repo_path);
5333 if (err)
5334 goto done;
5336 slash = strchr(child_path, '/');
5337 if (slash == NULL) {
5338 err = alloc_added_blob_tree_entry(&new_te, ct);
5339 if (err)
5340 goto done;
5341 err = report_ct_status(ct, status_cb, status_arg);
5342 if (err)
5343 goto done;
5344 ct->flags |= GOT_COMMITABLE_ADDED;
5345 err = insert_tree_entry(new_te, &paths);
5346 if (err)
5347 goto done;
5348 (*nentries)++;
5349 } else {
5350 *slash = '\0'; /* trim trailing path components */
5351 if (base_tree == NULL ||
5352 got_object_tree_find_entry(base_tree, child_path)
5353 == NULL) {
5354 err = make_subtree_for_added_blob(&new_te,
5355 child_path, path_base_tree,
5356 commitable_paths, status_cb, status_arg,
5357 repo);
5358 if (err)
5359 goto done;
5360 err = insert_tree_entry(new_te, &paths);
5361 if (err)
5362 goto done;
5363 (*nentries)++;
5368 if (base_tree) {
5369 int i, nbase_entries;
5370 /* Handle modified and deleted entries. */
5371 nbase_entries = got_object_tree_get_nentries(base_tree);
5372 for (i = 0; i < nbase_entries; i++) {
5373 struct got_commitable *ct = NULL;
5375 te = got_object_tree_get_entry(base_tree, i);
5376 if (got_object_tree_entry_is_submodule(te)) {
5377 /* Entry is a submodule; just copy it. */
5378 err = got_object_tree_entry_dup(&new_te, te);
5379 if (err)
5380 goto done;
5381 err = insert_tree_entry(new_te, &paths);
5382 if (err)
5383 goto done;
5384 (*nentries)++;
5385 continue;
5388 if (S_ISDIR(te->mode)) {
5389 int modified;
5390 err = got_object_tree_entry_dup(&new_te, te);
5391 if (err)
5392 goto done;
5393 err = match_modified_subtree(&modified, te,
5394 path_base_tree, commitable_paths);
5395 if (err)
5396 goto done;
5397 /* Avoid recursion into unmodified subtrees. */
5398 if (modified) {
5399 struct got_object_id *new_id;
5400 int nsubentries;
5401 err = write_subtree(&new_id,
5402 &nsubentries, te,
5403 path_base_tree, commitable_paths,
5404 status_cb, status_arg, repo);
5405 if (err)
5406 goto done;
5407 if (nsubentries == 0) {
5408 /* All entries were deleted. */
5409 free(new_id);
5410 continue;
5412 memcpy(&new_te->id, new_id,
5413 sizeof(new_te->id));
5414 free(new_id);
5416 err = insert_tree_entry(new_te, &paths);
5417 if (err)
5418 goto done;
5419 (*nentries)++;
5420 continue;
5423 err = match_deleted_or_modified_ct(&ct, te,
5424 path_base_tree, commitable_paths);
5425 if (err)
5426 goto done;
5427 if (ct) {
5428 /* NB: Deleted entries get dropped here. */
5429 if (ct->status == GOT_STATUS_MODIFY ||
5430 ct->status == GOT_STATUS_MODE_CHANGE ||
5431 ct->staged_status == GOT_STATUS_MODIFY) {
5432 err = alloc_modified_blob_tree_entry(
5433 &new_te, te, ct);
5434 if (err)
5435 goto done;
5436 err = insert_tree_entry(new_te, &paths);
5437 if (err)
5438 goto done;
5439 (*nentries)++;
5441 err = report_ct_status(ct, status_cb,
5442 status_arg);
5443 if (err)
5444 goto done;
5445 } else {
5446 /* Entry is unchanged; just copy it. */
5447 err = got_object_tree_entry_dup(&new_te, te);
5448 if (err)
5449 goto done;
5450 err = insert_tree_entry(new_te, &paths);
5451 if (err)
5452 goto done;
5453 (*nentries)++;
5458 /* Write new list of entries; deleted entries have been dropped. */
5459 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5460 done:
5461 got_pathlist_free(&paths);
5462 return err;
5465 static const struct got_error *
5466 update_fileindex_after_commit(struct got_worktree *worktree,
5467 struct got_pathlist_head *commitable_paths,
5468 struct got_object_id *new_base_commit_id,
5469 struct got_fileindex *fileindex, int have_staged_files)
5471 const struct got_error *err = NULL;
5472 struct got_pathlist_entry *pe;
5473 char *relpath = NULL;
5475 TAILQ_FOREACH(pe, commitable_paths, entry) {
5476 struct got_fileindex_entry *ie;
5477 struct got_commitable *ct = pe->data;
5479 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5481 err = got_path_skip_common_ancestor(&relpath,
5482 worktree->root_path, ct->ondisk_path);
5483 if (err)
5484 goto done;
5486 if (ie) {
5487 if (ct->status == GOT_STATUS_DELETE ||
5488 ct->staged_status == GOT_STATUS_DELETE) {
5489 got_fileindex_entry_remove(fileindex, ie);
5490 } else if (ct->staged_status == GOT_STATUS_ADD ||
5491 ct->staged_status == GOT_STATUS_MODIFY) {
5492 got_fileindex_entry_stage_set(ie,
5493 GOT_FILEIDX_STAGE_NONE);
5494 got_fileindex_entry_staged_filetype_set(ie, 0);
5496 err = got_fileindex_entry_update(ie,
5497 worktree->root_fd, relpath,
5498 ct->staged_blob_id->sha1,
5499 new_base_commit_id->sha1,
5500 !have_staged_files);
5501 } else
5502 err = got_fileindex_entry_update(ie,
5503 worktree->root_fd, relpath,
5504 ct->blob_id->sha1,
5505 new_base_commit_id->sha1,
5506 !have_staged_files);
5507 } else {
5508 err = got_fileindex_entry_alloc(&ie, pe->path);
5509 if (err)
5510 goto done;
5511 err = got_fileindex_entry_update(ie,
5512 worktree->root_fd, relpath, ct->blob_id->sha1,
5513 new_base_commit_id->sha1, 1);
5514 if (err) {
5515 got_fileindex_entry_free(ie);
5516 goto done;
5518 err = got_fileindex_entry_add(fileindex, ie);
5519 if (err) {
5520 got_fileindex_entry_free(ie);
5521 goto done;
5524 free(relpath);
5525 relpath = NULL;
5527 done:
5528 free(relpath);
5529 return err;
5533 static const struct got_error *
5534 check_out_of_date(const char *in_repo_path, unsigned char status,
5535 unsigned char staged_status, struct got_object_id *base_blob_id,
5536 struct got_object_id *base_commit_id,
5537 struct got_object_id *head_commit_id, struct got_repository *repo,
5538 int ood_errcode)
5540 const struct got_error *err = NULL;
5541 struct got_commit_object *commit = NULL;
5542 struct got_object_id *id = NULL;
5544 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5545 /* Trivial case: base commit == head commit */
5546 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5547 return NULL;
5549 * Ensure file content which local changes were based
5550 * on matches file content in the branch head.
5552 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5553 if (err)
5554 goto done;
5555 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5556 if (err) {
5557 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5558 err = got_error(ood_errcode);
5559 goto done;
5560 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5561 err = got_error(ood_errcode);
5562 } else {
5563 /* Require that added files don't exist in the branch head. */
5564 err = got_object_open_as_commit(&commit, repo, head_commit_id);
5565 if (err)
5566 goto done;
5567 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
5568 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5569 goto done;
5570 err = id ? got_error(ood_errcode) : NULL;
5572 done:
5573 free(id);
5574 if (commit)
5575 got_object_commit_close(commit);
5576 return err;
5579 const struct got_error *
5580 commit_worktree(struct got_object_id **new_commit_id,
5581 struct got_pathlist_head *commitable_paths,
5582 struct got_object_id *head_commit_id,
5583 struct got_object_id *parent_id2,
5584 struct got_worktree *worktree,
5585 const char *author, const char *committer,
5586 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5587 got_worktree_status_cb status_cb, void *status_arg,
5588 struct got_repository *repo)
5590 const struct got_error *err = NULL, *unlockerr = NULL;
5591 struct got_pathlist_entry *pe;
5592 const char *head_ref_name = NULL;
5593 struct got_commit_object *head_commit = NULL;
5594 struct got_reference *head_ref2 = NULL;
5595 struct got_object_id *head_commit_id2 = NULL;
5596 struct got_tree_object *head_tree = NULL;
5597 struct got_object_id *new_tree_id = NULL;
5598 int nentries, nparents = 0;
5599 struct got_object_id_queue parent_ids;
5600 struct got_object_qid *pid = NULL;
5601 char *logmsg = NULL;
5603 *new_commit_id = NULL;
5605 STAILQ_INIT(&parent_ids);
5607 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5608 if (err)
5609 goto done;
5611 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5612 if (err)
5613 goto done;
5615 if (commit_msg_cb != NULL) {
5616 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5617 if (err)
5618 goto done;
5621 if (logmsg == NULL || strlen(logmsg) == 0) {
5622 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5623 goto done;
5626 /* Create blobs from added and modified files and record their IDs. */
5627 TAILQ_FOREACH(pe, commitable_paths, entry) {
5628 struct got_commitable *ct = pe->data;
5629 char *ondisk_path;
5631 /* Blobs for staged files already exist. */
5632 if (ct->staged_status == GOT_STATUS_ADD ||
5633 ct->staged_status == GOT_STATUS_MODIFY)
5634 continue;
5636 if (ct->status != GOT_STATUS_ADD &&
5637 ct->status != GOT_STATUS_MODIFY &&
5638 ct->status != GOT_STATUS_MODE_CHANGE)
5639 continue;
5641 if (asprintf(&ondisk_path, "%s/%s",
5642 worktree->root_path, pe->path) == -1) {
5643 err = got_error_from_errno("asprintf");
5644 goto done;
5646 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5647 free(ondisk_path);
5648 if (err)
5649 goto done;
5652 /* Recursively write new tree objects. */
5653 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5654 commitable_paths, status_cb, status_arg, repo);
5655 if (err)
5656 goto done;
5658 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5659 if (err)
5660 goto done;
5661 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5662 nparents++;
5663 if (parent_id2) {
5664 err = got_object_qid_alloc(&pid, parent_id2);
5665 if (err)
5666 goto done;
5667 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5668 nparents++;
5670 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5671 nparents, author, time(NULL), committer, time(NULL), logmsg, repo);
5672 if (logmsg != NULL)
5673 free(logmsg);
5674 if (err)
5675 goto done;
5677 /* Check if a concurrent commit to our branch has occurred. */
5678 head_ref_name = got_worktree_get_head_ref_name(worktree);
5679 if (head_ref_name == NULL) {
5680 err = got_error_from_errno("got_worktree_get_head_ref_name");
5681 goto done;
5683 /* Lock the reference here to prevent concurrent modification. */
5684 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5685 if (err)
5686 goto done;
5687 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5688 if (err)
5689 goto done;
5690 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5691 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5692 goto done;
5694 /* Update branch head in repository. */
5695 err = got_ref_change_ref(head_ref2, *new_commit_id);
5696 if (err)
5697 goto done;
5698 err = got_ref_write(head_ref2, repo);
5699 if (err)
5700 goto done;
5702 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5703 if (err)
5704 goto done;
5706 err = ref_base_commit(worktree, repo);
5707 if (err)
5708 goto done;
5709 done:
5710 got_object_id_queue_free(&parent_ids);
5711 if (head_tree)
5712 got_object_tree_close(head_tree);
5713 if (head_commit)
5714 got_object_commit_close(head_commit);
5715 free(head_commit_id2);
5716 if (head_ref2) {
5717 unlockerr = got_ref_unlock(head_ref2);
5718 if (unlockerr && err == NULL)
5719 err = unlockerr;
5720 got_ref_close(head_ref2);
5722 return err;
5725 static const struct got_error *
5726 check_path_is_commitable(const char *path,
5727 struct got_pathlist_head *commitable_paths)
5729 struct got_pathlist_entry *cpe = NULL;
5730 size_t path_len = strlen(path);
5732 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5733 struct got_commitable *ct = cpe->data;
5734 const char *ct_path = ct->path;
5736 while (ct_path[0] == '/')
5737 ct_path++;
5739 if (strcmp(path, ct_path) == 0 ||
5740 got_path_is_child(ct_path, path, path_len))
5741 break;
5744 if (cpe == NULL)
5745 return got_error_path(path, GOT_ERR_BAD_PATH);
5747 return NULL;
5750 static const struct got_error *
5751 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5753 int *have_staged_files = arg;
5755 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5756 *have_staged_files = 1;
5757 return got_error(GOT_ERR_CANCELLED);
5760 return NULL;
5763 static const struct got_error *
5764 check_non_staged_files(struct got_fileindex *fileindex,
5765 struct got_pathlist_head *paths)
5767 struct got_pathlist_entry *pe;
5768 struct got_fileindex_entry *ie;
5770 TAILQ_FOREACH(pe, paths, entry) {
5771 if (pe->path[0] == '\0')
5772 continue;
5773 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5774 if (ie == NULL)
5775 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5776 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5777 return got_error_path(pe->path,
5778 GOT_ERR_FILE_NOT_STAGED);
5781 return NULL;
5784 const struct got_error *
5785 got_worktree_commit(struct got_object_id **new_commit_id,
5786 struct got_worktree *worktree, struct got_pathlist_head *paths,
5787 const char *author, const char *committer, int allow_bad_symlinks,
5788 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5789 got_worktree_status_cb status_cb, void *status_arg,
5790 struct got_repository *repo)
5792 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5793 struct got_fileindex *fileindex = NULL;
5794 char *fileindex_path = NULL;
5795 struct got_pathlist_head commitable_paths;
5796 struct collect_commitables_arg cc_arg;
5797 struct got_pathlist_entry *pe;
5798 struct got_reference *head_ref = NULL;
5799 struct got_object_id *head_commit_id = NULL;
5800 int have_staged_files = 0;
5802 *new_commit_id = NULL;
5804 TAILQ_INIT(&commitable_paths);
5806 err = lock_worktree(worktree, LOCK_EX);
5807 if (err)
5808 goto done;
5810 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5811 if (err)
5812 goto done;
5814 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5815 if (err)
5816 goto done;
5818 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5819 if (err)
5820 goto done;
5822 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5823 &have_staged_files);
5824 if (err && err->code != GOT_ERR_CANCELLED)
5825 goto done;
5826 if (have_staged_files) {
5827 err = check_non_staged_files(fileindex, paths);
5828 if (err)
5829 goto done;
5832 cc_arg.commitable_paths = &commitable_paths;
5833 cc_arg.worktree = worktree;
5834 cc_arg.fileindex = fileindex;
5835 cc_arg.repo = repo;
5836 cc_arg.have_staged_files = have_staged_files;
5837 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5838 TAILQ_FOREACH(pe, paths, entry) {
5839 err = worktree_status(worktree, pe->path, fileindex, repo,
5840 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
5841 if (err)
5842 goto done;
5845 if (TAILQ_EMPTY(&commitable_paths)) {
5846 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5847 goto done;
5850 TAILQ_FOREACH(pe, paths, entry) {
5851 err = check_path_is_commitable(pe->path, &commitable_paths);
5852 if (err)
5853 goto done;
5856 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5857 struct got_commitable *ct = pe->data;
5858 const char *ct_path = ct->in_repo_path;
5860 while (ct_path[0] == '/')
5861 ct_path++;
5862 err = check_out_of_date(ct_path, ct->status,
5863 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5864 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5865 if (err)
5866 goto done;
5870 err = commit_worktree(new_commit_id, &commitable_paths,
5871 head_commit_id, NULL, worktree, author, committer,
5872 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5873 if (err)
5874 goto done;
5876 err = update_fileindex_after_commit(worktree, &commitable_paths,
5877 *new_commit_id, fileindex, have_staged_files);
5878 sync_err = sync_fileindex(fileindex, fileindex_path);
5879 if (sync_err && err == NULL)
5880 err = sync_err;
5881 done:
5882 if (fileindex)
5883 got_fileindex_free(fileindex);
5884 free(fileindex_path);
5885 unlockerr = lock_worktree(worktree, LOCK_SH);
5886 if (unlockerr && err == NULL)
5887 err = unlockerr;
5888 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5889 struct got_commitable *ct = pe->data;
5890 free_commitable(ct);
5892 got_pathlist_free(&commitable_paths);
5893 return err;
5896 const char *
5897 got_commitable_get_path(struct got_commitable *ct)
5899 return ct->path;
5902 unsigned int
5903 got_commitable_get_status(struct got_commitable *ct)
5905 return ct->status;
5908 struct check_rebase_ok_arg {
5909 struct got_worktree *worktree;
5910 struct got_repository *repo;
5913 static const struct got_error *
5914 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5916 const struct got_error *err = NULL;
5917 struct check_rebase_ok_arg *a = arg;
5918 unsigned char status;
5919 struct stat sb;
5920 char *ondisk_path;
5922 /* Reject rebase of a work tree with mixed base commits. */
5923 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5924 SHA1_DIGEST_LENGTH))
5925 return got_error(GOT_ERR_MIXED_COMMITS);
5927 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5928 == -1)
5929 return got_error_from_errno("asprintf");
5931 /* Reject rebase of a work tree with modified or staged files. */
5932 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5933 free(ondisk_path);
5934 if (err)
5935 return err;
5937 if (status != GOT_STATUS_NO_CHANGE)
5938 return got_error(GOT_ERR_MODIFIED);
5939 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5940 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5942 return NULL;
5945 const struct got_error *
5946 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5947 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5948 struct got_worktree *worktree, struct got_reference *branch,
5949 struct got_repository *repo)
5951 const struct got_error *err = NULL;
5952 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5953 char *branch_ref_name = NULL;
5954 char *fileindex_path = NULL;
5955 struct check_rebase_ok_arg ok_arg;
5956 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5957 struct got_object_id *wt_branch_tip = NULL;
5959 *new_base_branch_ref = NULL;
5960 *tmp_branch = NULL;
5961 *fileindex = NULL;
5963 err = lock_worktree(worktree, LOCK_EX);
5964 if (err)
5965 return err;
5967 err = open_fileindex(fileindex, &fileindex_path, worktree);
5968 if (err)
5969 goto done;
5971 ok_arg.worktree = worktree;
5972 ok_arg.repo = repo;
5973 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5974 &ok_arg);
5975 if (err)
5976 goto done;
5978 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5979 if (err)
5980 goto done;
5982 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5983 if (err)
5984 goto done;
5986 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5987 if (err)
5988 goto done;
5990 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5991 0);
5992 if (err)
5993 goto done;
5995 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5996 if (err)
5997 goto done;
5998 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5999 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6000 goto done;
6003 err = got_ref_alloc_symref(new_base_branch_ref,
6004 new_base_branch_ref_name, wt_branch);
6005 if (err)
6006 goto done;
6007 err = got_ref_write(*new_base_branch_ref, repo);
6008 if (err)
6009 goto done;
6011 /* TODO Lock original branch's ref while rebasing? */
6013 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6014 if (err)
6015 goto done;
6017 err = got_ref_write(branch_ref, repo);
6018 if (err)
6019 goto done;
6021 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6022 worktree->base_commit_id);
6023 if (err)
6024 goto done;
6025 err = got_ref_write(*tmp_branch, repo);
6026 if (err)
6027 goto done;
6029 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6030 if (err)
6031 goto done;
6032 done:
6033 free(fileindex_path);
6034 free(tmp_branch_name);
6035 free(new_base_branch_ref_name);
6036 free(branch_ref_name);
6037 if (branch_ref)
6038 got_ref_close(branch_ref);
6039 if (wt_branch)
6040 got_ref_close(wt_branch);
6041 free(wt_branch_tip);
6042 if (err) {
6043 if (*new_base_branch_ref) {
6044 got_ref_close(*new_base_branch_ref);
6045 *new_base_branch_ref = NULL;
6047 if (*tmp_branch) {
6048 got_ref_close(*tmp_branch);
6049 *tmp_branch = NULL;
6051 if (*fileindex) {
6052 got_fileindex_free(*fileindex);
6053 *fileindex = NULL;
6055 lock_worktree(worktree, LOCK_SH);
6057 return err;
6060 const struct got_error *
6061 got_worktree_rebase_continue(struct got_object_id **commit_id,
6062 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6063 struct got_reference **branch, struct got_fileindex **fileindex,
6064 struct got_worktree *worktree, struct got_repository *repo)
6066 const struct got_error *err;
6067 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6068 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6069 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6070 char *fileindex_path = NULL;
6071 int have_staged_files = 0;
6073 *commit_id = NULL;
6074 *new_base_branch = NULL;
6075 *tmp_branch = NULL;
6076 *branch = NULL;
6077 *fileindex = NULL;
6079 err = lock_worktree(worktree, LOCK_EX);
6080 if (err)
6081 return err;
6083 err = open_fileindex(fileindex, &fileindex_path, worktree);
6084 if (err)
6085 goto done;
6087 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6088 &have_staged_files);
6089 if (err && err->code != GOT_ERR_CANCELLED)
6090 goto done;
6091 if (have_staged_files) {
6092 err = got_error(GOT_ERR_STAGED_PATHS);
6093 goto done;
6096 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6097 if (err)
6098 goto done;
6100 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6101 if (err)
6102 goto done;
6104 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6105 if (err)
6106 goto done;
6108 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6109 if (err)
6110 goto done;
6112 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6113 if (err)
6114 goto done;
6116 err = got_ref_open(branch, repo,
6117 got_ref_get_symref_target(branch_ref), 0);
6118 if (err)
6119 goto done;
6121 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6122 if (err)
6123 goto done;
6125 err = got_ref_resolve(commit_id, repo, commit_ref);
6126 if (err)
6127 goto done;
6129 err = got_ref_open(new_base_branch, repo,
6130 new_base_branch_ref_name, 0);
6131 if (err)
6132 goto done;
6134 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6135 if (err)
6136 goto done;
6137 done:
6138 free(commit_ref_name);
6139 free(branch_ref_name);
6140 free(fileindex_path);
6141 if (commit_ref)
6142 got_ref_close(commit_ref);
6143 if (branch_ref)
6144 got_ref_close(branch_ref);
6145 if (err) {
6146 free(*commit_id);
6147 *commit_id = NULL;
6148 if (*tmp_branch) {
6149 got_ref_close(*tmp_branch);
6150 *tmp_branch = NULL;
6152 if (*new_base_branch) {
6153 got_ref_close(*new_base_branch);
6154 *new_base_branch = NULL;
6156 if (*branch) {
6157 got_ref_close(*branch);
6158 *branch = NULL;
6160 if (*fileindex) {
6161 got_fileindex_free(*fileindex);
6162 *fileindex = NULL;
6164 lock_worktree(worktree, LOCK_SH);
6166 return err;
6169 const struct got_error *
6170 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6172 const struct got_error *err;
6173 char *tmp_branch_name = NULL;
6175 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6176 if (err)
6177 return err;
6179 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6180 free(tmp_branch_name);
6181 return NULL;
6184 static const struct got_error *
6185 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6186 char **logmsg, void *arg)
6188 *logmsg = arg;
6189 return NULL;
6192 static const struct got_error *
6193 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6194 const char *path, struct got_object_id *blob_id,
6195 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6196 int dirfd, const char *de_name)
6198 return NULL;
6201 struct collect_merged_paths_arg {
6202 got_worktree_checkout_cb progress_cb;
6203 void *progress_arg;
6204 struct got_pathlist_head *merged_paths;
6207 static const struct got_error *
6208 collect_merged_paths(void *arg, unsigned char status, const char *path)
6210 const struct got_error *err;
6211 struct collect_merged_paths_arg *a = arg;
6212 char *p;
6213 struct got_pathlist_entry *new;
6215 err = (*a->progress_cb)(a->progress_arg, status, path);
6216 if (err)
6217 return err;
6219 if (status != GOT_STATUS_MERGE &&
6220 status != GOT_STATUS_ADD &&
6221 status != GOT_STATUS_DELETE &&
6222 status != GOT_STATUS_CONFLICT)
6223 return NULL;
6225 p = strdup(path);
6226 if (p == NULL)
6227 return got_error_from_errno("strdup");
6229 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6230 if (err || new == NULL)
6231 free(p);
6232 return err;
6235 void
6236 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6238 struct got_pathlist_entry *pe;
6240 TAILQ_FOREACH(pe, merged_paths, entry)
6241 free((char *)pe->path);
6243 got_pathlist_free(merged_paths);
6246 static const struct got_error *
6247 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6248 int is_rebase, struct got_repository *repo)
6250 const struct got_error *err;
6251 struct got_reference *commit_ref = NULL;
6253 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6254 if (err) {
6255 if (err->code != GOT_ERR_NOT_REF)
6256 goto done;
6257 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6258 if (err)
6259 goto done;
6260 err = got_ref_write(commit_ref, repo);
6261 if (err)
6262 goto done;
6263 } else if (is_rebase) {
6264 struct got_object_id *stored_id;
6265 int cmp;
6267 err = got_ref_resolve(&stored_id, repo, commit_ref);
6268 if (err)
6269 goto done;
6270 cmp = got_object_id_cmp(commit_id, stored_id);
6271 free(stored_id);
6272 if (cmp != 0) {
6273 err = got_error(GOT_ERR_REBASE_COMMITID);
6274 goto done;
6277 done:
6278 if (commit_ref)
6279 got_ref_close(commit_ref);
6280 return err;
6283 static const struct got_error *
6284 rebase_merge_files(struct got_pathlist_head *merged_paths,
6285 const char *commit_ref_name, struct got_worktree *worktree,
6286 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6287 struct got_object_id *commit_id, struct got_repository *repo,
6288 got_worktree_checkout_cb progress_cb, void *progress_arg,
6289 got_cancel_cb cancel_cb, void *cancel_arg)
6291 const struct got_error *err;
6292 struct got_reference *commit_ref = NULL;
6293 struct collect_merged_paths_arg cmp_arg;
6294 char *fileindex_path;
6296 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6298 err = get_fileindex_path(&fileindex_path, worktree);
6299 if (err)
6300 return err;
6302 cmp_arg.progress_cb = progress_cb;
6303 cmp_arg.progress_arg = progress_arg;
6304 cmp_arg.merged_paths = merged_paths;
6305 err = merge_files(worktree, fileindex, fileindex_path,
6306 parent_commit_id, commit_id, repo, collect_merged_paths,
6307 &cmp_arg, cancel_cb, cancel_arg);
6308 if (commit_ref)
6309 got_ref_close(commit_ref);
6310 return err;
6313 const struct got_error *
6314 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6315 struct got_worktree *worktree, struct got_fileindex *fileindex,
6316 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6317 struct got_repository *repo,
6318 got_worktree_checkout_cb progress_cb, void *progress_arg,
6319 got_cancel_cb cancel_cb, void *cancel_arg)
6321 const struct got_error *err;
6322 char *commit_ref_name;
6324 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6325 if (err)
6326 return err;
6328 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6329 if (err)
6330 goto done;
6332 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6333 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6334 progress_arg, cancel_cb, cancel_arg);
6335 done:
6336 free(commit_ref_name);
6337 return err;
6340 const struct got_error *
6341 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6342 struct got_worktree *worktree, struct got_fileindex *fileindex,
6343 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6344 struct got_repository *repo,
6345 got_worktree_checkout_cb progress_cb, void *progress_arg,
6346 got_cancel_cb cancel_cb, void *cancel_arg)
6348 const struct got_error *err;
6349 char *commit_ref_name;
6351 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6352 if (err)
6353 return err;
6355 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6356 if (err)
6357 goto done;
6359 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6360 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6361 progress_arg, cancel_cb, cancel_arg);
6362 done:
6363 free(commit_ref_name);
6364 return err;
6367 static const struct got_error *
6368 rebase_commit(struct got_object_id **new_commit_id,
6369 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6370 struct got_worktree *worktree, struct got_fileindex *fileindex,
6371 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6372 const char *new_logmsg, struct got_repository *repo)
6374 const struct got_error *err, *sync_err;
6375 struct got_pathlist_head commitable_paths;
6376 struct collect_commitables_arg cc_arg;
6377 char *fileindex_path = NULL;
6378 struct got_reference *head_ref = NULL;
6379 struct got_object_id *head_commit_id = NULL;
6380 char *logmsg = NULL;
6382 TAILQ_INIT(&commitable_paths);
6383 *new_commit_id = NULL;
6385 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6387 err = get_fileindex_path(&fileindex_path, worktree);
6388 if (err)
6389 return err;
6391 cc_arg.commitable_paths = &commitable_paths;
6392 cc_arg.worktree = worktree;
6393 cc_arg.repo = repo;
6394 cc_arg.have_staged_files = 0;
6396 * If possible get the status of individual files directly to
6397 * avoid crawling the entire work tree once per rebased commit.
6399 * Ideally, merged_paths would contain a list of commitables
6400 * we could use so we could skip worktree_status() entirely.
6401 * However, we would then need carefully keep track of cumulative
6402 * effects of operations such as file additions and deletions
6403 * in 'got histedit -f' (folding multiple commits into one),
6404 * and this extra complexity is not really worth it.
6406 if (merged_paths) {
6407 struct got_pathlist_entry *pe;
6408 TAILQ_FOREACH(pe, merged_paths, entry) {
6409 err = worktree_status(worktree, pe->path, fileindex,
6410 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6411 0);
6412 if (err)
6413 goto done;
6415 } else {
6416 err = worktree_status(worktree, "", fileindex, repo,
6417 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6418 if (err)
6419 goto done;
6422 if (TAILQ_EMPTY(&commitable_paths)) {
6423 /* No-op change; commit will be elided. */
6424 err = got_ref_delete(commit_ref, repo);
6425 if (err)
6426 goto done;
6427 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6428 goto done;
6431 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6432 if (err)
6433 goto done;
6435 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6436 if (err)
6437 goto done;
6439 if (new_logmsg) {
6440 logmsg = strdup(new_logmsg);
6441 if (logmsg == NULL) {
6442 err = got_error_from_errno("strdup");
6443 goto done;
6445 } else {
6446 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6447 if (err)
6448 goto done;
6451 /* NB: commit_worktree will call free(logmsg) */
6452 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6453 NULL, worktree, got_object_commit_get_author(orig_commit),
6454 got_object_commit_get_committer(orig_commit),
6455 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6456 if (err)
6457 goto done;
6459 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6460 if (err)
6461 goto done;
6463 err = got_ref_delete(commit_ref, repo);
6464 if (err)
6465 goto done;
6467 err = update_fileindex_after_commit(worktree, &commitable_paths,
6468 *new_commit_id, fileindex, 0);
6469 sync_err = sync_fileindex(fileindex, fileindex_path);
6470 if (sync_err && err == NULL)
6471 err = sync_err;
6472 done:
6473 free(fileindex_path);
6474 free(head_commit_id);
6475 if (head_ref)
6476 got_ref_close(head_ref);
6477 if (err) {
6478 free(*new_commit_id);
6479 *new_commit_id = NULL;
6481 return err;
6484 const struct got_error *
6485 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6486 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6487 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6488 struct got_commit_object *orig_commit,
6489 struct got_object_id *orig_commit_id, struct got_repository *repo)
6491 const struct got_error *err;
6492 char *commit_ref_name;
6493 struct got_reference *commit_ref = NULL;
6494 struct got_object_id *commit_id = NULL;
6496 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6497 if (err)
6498 return err;
6500 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6501 if (err)
6502 goto done;
6503 err = got_ref_resolve(&commit_id, repo, commit_ref);
6504 if (err)
6505 goto done;
6506 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6507 err = got_error(GOT_ERR_REBASE_COMMITID);
6508 goto done;
6511 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6512 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6513 done:
6514 if (commit_ref)
6515 got_ref_close(commit_ref);
6516 free(commit_ref_name);
6517 free(commit_id);
6518 return err;
6521 const struct got_error *
6522 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6523 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6524 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6525 struct got_commit_object *orig_commit,
6526 struct got_object_id *orig_commit_id, const char *new_logmsg,
6527 struct got_repository *repo)
6529 const struct got_error *err;
6530 char *commit_ref_name;
6531 struct got_reference *commit_ref = NULL;
6533 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6534 if (err)
6535 return err;
6537 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6538 if (err)
6539 goto done;
6541 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6542 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6543 done:
6544 if (commit_ref)
6545 got_ref_close(commit_ref);
6546 free(commit_ref_name);
6547 return err;
6550 const struct got_error *
6551 got_worktree_rebase_postpone(struct got_worktree *worktree,
6552 struct got_fileindex *fileindex)
6554 if (fileindex)
6555 got_fileindex_free(fileindex);
6556 return lock_worktree(worktree, LOCK_SH);
6559 static const struct got_error *
6560 delete_ref(const char *name, struct got_repository *repo)
6562 const struct got_error *err;
6563 struct got_reference *ref;
6565 err = got_ref_open(&ref, repo, name, 0);
6566 if (err) {
6567 if (err->code == GOT_ERR_NOT_REF)
6568 return NULL;
6569 return err;
6572 err = got_ref_delete(ref, repo);
6573 got_ref_close(ref);
6574 return err;
6577 static const struct got_error *
6578 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6580 const struct got_error *err;
6581 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6582 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6584 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6585 if (err)
6586 goto done;
6587 err = delete_ref(tmp_branch_name, repo);
6588 if (err)
6589 goto done;
6591 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6592 if (err)
6593 goto done;
6594 err = delete_ref(new_base_branch_ref_name, repo);
6595 if (err)
6596 goto done;
6598 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6599 if (err)
6600 goto done;
6601 err = delete_ref(branch_ref_name, repo);
6602 if (err)
6603 goto done;
6605 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6606 if (err)
6607 goto done;
6608 err = delete_ref(commit_ref_name, repo);
6609 if (err)
6610 goto done;
6612 done:
6613 free(tmp_branch_name);
6614 free(new_base_branch_ref_name);
6615 free(branch_ref_name);
6616 free(commit_ref_name);
6617 return err;
6620 const struct got_error *
6621 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6622 struct got_object_id *new_commit_id, struct got_repository *repo)
6624 const struct got_error *err;
6625 struct got_reference *ref = NULL;
6626 struct got_object_id *old_commit_id = NULL;
6627 const char *branch_name = NULL;
6628 char *new_id_str = NULL;
6629 char *refname = NULL;
6631 branch_name = got_ref_get_name(branch);
6632 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6633 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6634 branch_name += 11;
6636 err = got_object_id_str(&new_id_str, new_commit_id);
6637 if (err)
6638 return err;
6640 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6641 new_id_str) == -1) {
6642 err = got_error_from_errno("asprintf");
6643 goto done;
6646 err = got_ref_resolve(&old_commit_id, repo, branch);
6647 if (err)
6648 goto done;
6650 err = got_ref_alloc(&ref, refname, old_commit_id);
6651 if (err)
6652 goto done;
6654 err = got_ref_write(ref, repo);
6655 done:
6656 free(new_id_str);
6657 free(refname);
6658 free(old_commit_id);
6659 if (ref)
6660 got_ref_close(ref);
6661 return err;
6664 const struct got_error *
6665 got_worktree_rebase_complete(struct got_worktree *worktree,
6666 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6667 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6668 struct got_repository *repo, int create_backup)
6670 const struct got_error *err, *unlockerr, *sync_err;
6671 struct got_object_id *new_head_commit_id = NULL;
6672 char *fileindex_path = NULL;
6674 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6675 if (err)
6676 return err;
6678 if (create_backup) {
6679 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6680 rebased_branch, new_head_commit_id, repo);
6681 if (err)
6682 goto done;
6685 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6686 if (err)
6687 goto done;
6689 err = got_ref_write(rebased_branch, repo);
6690 if (err)
6691 goto done;
6693 err = got_worktree_set_head_ref(worktree, rebased_branch);
6694 if (err)
6695 goto done;
6697 err = delete_rebase_refs(worktree, repo);
6698 if (err)
6699 goto done;
6701 err = get_fileindex_path(&fileindex_path, worktree);
6702 if (err)
6703 goto done;
6704 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6705 sync_err = sync_fileindex(fileindex, fileindex_path);
6706 if (sync_err && err == NULL)
6707 err = sync_err;
6708 done:
6709 got_fileindex_free(fileindex);
6710 free(fileindex_path);
6711 free(new_head_commit_id);
6712 unlockerr = lock_worktree(worktree, LOCK_SH);
6713 if (unlockerr && err == NULL)
6714 err = unlockerr;
6715 return err;
6718 const struct got_error *
6719 got_worktree_rebase_abort(struct got_worktree *worktree,
6720 struct got_fileindex *fileindex, struct got_repository *repo,
6721 struct got_reference *new_base_branch,
6722 got_worktree_checkout_cb progress_cb, void *progress_arg)
6724 const struct got_error *err, *unlockerr, *sync_err;
6725 struct got_reference *resolved = NULL;
6726 struct got_object_id *commit_id = NULL;
6727 struct got_commit_object *commit = NULL;
6728 char *fileindex_path = NULL;
6729 struct revert_file_args rfa;
6730 struct got_object_id *tree_id = NULL;
6732 err = lock_worktree(worktree, LOCK_EX);
6733 if (err)
6734 return err;
6736 err = got_object_open_as_commit(&commit, repo,
6737 worktree->base_commit_id);
6738 if (err)
6739 goto done;
6741 err = got_ref_open(&resolved, repo,
6742 got_ref_get_symref_target(new_base_branch), 0);
6743 if (err)
6744 goto done;
6746 err = got_worktree_set_head_ref(worktree, resolved);
6747 if (err)
6748 goto done;
6751 * XXX commits to the base branch could have happened while
6752 * we were busy rebasing; should we store the original commit ID
6753 * when rebase begins and read it back here?
6755 err = got_ref_resolve(&commit_id, repo, resolved);
6756 if (err)
6757 goto done;
6759 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6760 if (err)
6761 goto done;
6763 err = got_object_id_by_path(&tree_id, repo, commit,
6764 worktree->path_prefix);
6765 if (err)
6766 goto done;
6768 err = delete_rebase_refs(worktree, repo);
6769 if (err)
6770 goto done;
6772 err = get_fileindex_path(&fileindex_path, worktree);
6773 if (err)
6774 goto done;
6776 rfa.worktree = worktree;
6777 rfa.fileindex = fileindex;
6778 rfa.progress_cb = progress_cb;
6779 rfa.progress_arg = progress_arg;
6780 rfa.patch_cb = NULL;
6781 rfa.patch_arg = NULL;
6782 rfa.repo = repo;
6783 rfa.unlink_added_files = 0;
6784 err = worktree_status(worktree, "", fileindex, repo,
6785 revert_file, &rfa, NULL, NULL, 1, 0);
6786 if (err)
6787 goto sync;
6789 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6790 repo, progress_cb, progress_arg, NULL, NULL);
6791 sync:
6792 sync_err = sync_fileindex(fileindex, fileindex_path);
6793 if (sync_err && err == NULL)
6794 err = sync_err;
6795 done:
6796 got_ref_close(resolved);
6797 free(tree_id);
6798 free(commit_id);
6799 if (commit)
6800 got_object_commit_close(commit);
6801 if (fileindex)
6802 got_fileindex_free(fileindex);
6803 free(fileindex_path);
6805 unlockerr = lock_worktree(worktree, LOCK_SH);
6806 if (unlockerr && err == NULL)
6807 err = unlockerr;
6808 return err;
6811 const struct got_error *
6812 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6813 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6814 struct got_fileindex **fileindex, struct got_worktree *worktree,
6815 struct got_repository *repo)
6817 const struct got_error *err = NULL;
6818 char *tmp_branch_name = NULL;
6819 char *branch_ref_name = NULL;
6820 char *base_commit_ref_name = NULL;
6821 char *fileindex_path = NULL;
6822 struct check_rebase_ok_arg ok_arg;
6823 struct got_reference *wt_branch = NULL;
6824 struct got_reference *base_commit_ref = NULL;
6826 *tmp_branch = NULL;
6827 *branch_ref = NULL;
6828 *base_commit_id = NULL;
6829 *fileindex = NULL;
6831 err = lock_worktree(worktree, LOCK_EX);
6832 if (err)
6833 return err;
6835 err = open_fileindex(fileindex, &fileindex_path, worktree);
6836 if (err)
6837 goto done;
6839 ok_arg.worktree = worktree;
6840 ok_arg.repo = repo;
6841 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6842 &ok_arg);
6843 if (err)
6844 goto done;
6846 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6847 if (err)
6848 goto done;
6850 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6851 if (err)
6852 goto done;
6854 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6855 worktree);
6856 if (err)
6857 goto done;
6859 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6860 0);
6861 if (err)
6862 goto done;
6864 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6865 if (err)
6866 goto done;
6868 err = got_ref_write(*branch_ref, repo);
6869 if (err)
6870 goto done;
6872 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6873 worktree->base_commit_id);
6874 if (err)
6875 goto done;
6876 err = got_ref_write(base_commit_ref, repo);
6877 if (err)
6878 goto done;
6879 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6880 if (*base_commit_id == NULL) {
6881 err = got_error_from_errno("got_object_id_dup");
6882 goto done;
6885 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6886 worktree->base_commit_id);
6887 if (err)
6888 goto done;
6889 err = got_ref_write(*tmp_branch, repo);
6890 if (err)
6891 goto done;
6893 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6894 if (err)
6895 goto done;
6896 done:
6897 free(fileindex_path);
6898 free(tmp_branch_name);
6899 free(branch_ref_name);
6900 free(base_commit_ref_name);
6901 if (wt_branch)
6902 got_ref_close(wt_branch);
6903 if (err) {
6904 if (*branch_ref) {
6905 got_ref_close(*branch_ref);
6906 *branch_ref = NULL;
6908 if (*tmp_branch) {
6909 got_ref_close(*tmp_branch);
6910 *tmp_branch = NULL;
6912 free(*base_commit_id);
6913 if (*fileindex) {
6914 got_fileindex_free(*fileindex);
6915 *fileindex = NULL;
6917 lock_worktree(worktree, LOCK_SH);
6919 return err;
6922 const struct got_error *
6923 got_worktree_histedit_postpone(struct got_worktree *worktree,
6924 struct got_fileindex *fileindex)
6926 if (fileindex)
6927 got_fileindex_free(fileindex);
6928 return lock_worktree(worktree, LOCK_SH);
6931 const struct got_error *
6932 got_worktree_histedit_in_progress(int *in_progress,
6933 struct got_worktree *worktree)
6935 const struct got_error *err;
6936 char *tmp_branch_name = NULL;
6938 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6939 if (err)
6940 return err;
6942 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6943 free(tmp_branch_name);
6944 return NULL;
6947 const struct got_error *
6948 got_worktree_histedit_continue(struct got_object_id **commit_id,
6949 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6950 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6951 struct got_worktree *worktree, struct got_repository *repo)
6953 const struct got_error *err;
6954 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6955 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6956 struct got_reference *commit_ref = NULL;
6957 struct got_reference *base_commit_ref = NULL;
6958 char *fileindex_path = NULL;
6959 int have_staged_files = 0;
6961 *commit_id = NULL;
6962 *tmp_branch = NULL;
6963 *base_commit_id = NULL;
6964 *fileindex = NULL;
6966 err = lock_worktree(worktree, LOCK_EX);
6967 if (err)
6968 return err;
6970 err = open_fileindex(fileindex, &fileindex_path, worktree);
6971 if (err)
6972 goto done;
6974 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6975 &have_staged_files);
6976 if (err && err->code != GOT_ERR_CANCELLED)
6977 goto done;
6978 if (have_staged_files) {
6979 err = got_error(GOT_ERR_STAGED_PATHS);
6980 goto done;
6983 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6984 if (err)
6985 goto done;
6987 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6988 if (err)
6989 goto done;
6991 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6992 if (err)
6993 goto done;
6995 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6996 worktree);
6997 if (err)
6998 goto done;
7000 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7001 if (err)
7002 goto done;
7004 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7005 if (err)
7006 goto done;
7007 err = got_ref_resolve(commit_id, repo, commit_ref);
7008 if (err)
7009 goto done;
7011 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7012 if (err)
7013 goto done;
7014 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7015 if (err)
7016 goto done;
7018 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7019 if (err)
7020 goto done;
7021 done:
7022 free(commit_ref_name);
7023 free(branch_ref_name);
7024 free(fileindex_path);
7025 if (commit_ref)
7026 got_ref_close(commit_ref);
7027 if (base_commit_ref)
7028 got_ref_close(base_commit_ref);
7029 if (err) {
7030 free(*commit_id);
7031 *commit_id = NULL;
7032 free(*base_commit_id);
7033 *base_commit_id = NULL;
7034 if (*tmp_branch) {
7035 got_ref_close(*tmp_branch);
7036 *tmp_branch = NULL;
7038 if (*fileindex) {
7039 got_fileindex_free(*fileindex);
7040 *fileindex = NULL;
7042 lock_worktree(worktree, LOCK_EX);
7044 return err;
7047 static const struct got_error *
7048 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7050 const struct got_error *err;
7051 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7052 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7054 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7055 if (err)
7056 goto done;
7057 err = delete_ref(tmp_branch_name, repo);
7058 if (err)
7059 goto done;
7061 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7062 worktree);
7063 if (err)
7064 goto done;
7065 err = delete_ref(base_commit_ref_name, repo);
7066 if (err)
7067 goto done;
7069 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7070 if (err)
7071 goto done;
7072 err = delete_ref(branch_ref_name, repo);
7073 if (err)
7074 goto done;
7076 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7077 if (err)
7078 goto done;
7079 err = delete_ref(commit_ref_name, repo);
7080 if (err)
7081 goto done;
7082 done:
7083 free(tmp_branch_name);
7084 free(base_commit_ref_name);
7085 free(branch_ref_name);
7086 free(commit_ref_name);
7087 return err;
7090 const struct got_error *
7091 got_worktree_histedit_abort(struct got_worktree *worktree,
7092 struct got_fileindex *fileindex, struct got_repository *repo,
7093 struct got_reference *branch, struct got_object_id *base_commit_id,
7094 got_worktree_checkout_cb progress_cb, void *progress_arg)
7096 const struct got_error *err, *unlockerr, *sync_err;
7097 struct got_reference *resolved = NULL;
7098 char *fileindex_path = NULL;
7099 struct got_commit_object *commit = NULL;
7100 struct got_object_id *tree_id = NULL;
7101 struct revert_file_args rfa;
7103 err = lock_worktree(worktree, LOCK_EX);
7104 if (err)
7105 return err;
7107 err = got_object_open_as_commit(&commit, repo,
7108 worktree->base_commit_id);
7109 if (err)
7110 goto done;
7112 err = got_ref_open(&resolved, repo,
7113 got_ref_get_symref_target(branch), 0);
7114 if (err)
7115 goto done;
7117 err = got_worktree_set_head_ref(worktree, resolved);
7118 if (err)
7119 goto done;
7121 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7122 if (err)
7123 goto done;
7125 err = got_object_id_by_path(&tree_id, repo, commit,
7126 worktree->path_prefix);
7127 if (err)
7128 goto done;
7130 err = delete_histedit_refs(worktree, repo);
7131 if (err)
7132 goto done;
7134 err = get_fileindex_path(&fileindex_path, worktree);
7135 if (err)
7136 goto done;
7138 rfa.worktree = worktree;
7139 rfa.fileindex = fileindex;
7140 rfa.progress_cb = progress_cb;
7141 rfa.progress_arg = progress_arg;
7142 rfa.patch_cb = NULL;
7143 rfa.patch_arg = NULL;
7144 rfa.repo = repo;
7145 rfa.unlink_added_files = 0;
7146 err = worktree_status(worktree, "", fileindex, repo,
7147 revert_file, &rfa, NULL, NULL, 1, 0);
7148 if (err)
7149 goto sync;
7151 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7152 repo, progress_cb, progress_arg, NULL, NULL);
7153 sync:
7154 sync_err = sync_fileindex(fileindex, fileindex_path);
7155 if (sync_err && err == NULL)
7156 err = sync_err;
7157 done:
7158 got_ref_close(resolved);
7159 free(tree_id);
7160 free(fileindex_path);
7162 unlockerr = lock_worktree(worktree, LOCK_SH);
7163 if (unlockerr && err == NULL)
7164 err = unlockerr;
7165 return err;
7168 const struct got_error *
7169 got_worktree_histedit_complete(struct got_worktree *worktree,
7170 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7171 struct got_reference *edited_branch, struct got_repository *repo)
7173 const struct got_error *err, *unlockerr, *sync_err;
7174 struct got_object_id *new_head_commit_id = NULL;
7175 struct got_reference *resolved = NULL;
7176 char *fileindex_path = NULL;
7178 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7179 if (err)
7180 return err;
7182 err = got_ref_open(&resolved, repo,
7183 got_ref_get_symref_target(edited_branch), 0);
7184 if (err)
7185 goto done;
7187 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7188 resolved, new_head_commit_id, repo);
7189 if (err)
7190 goto done;
7192 err = got_ref_change_ref(resolved, new_head_commit_id);
7193 if (err)
7194 goto done;
7196 err = got_ref_write(resolved, repo);
7197 if (err)
7198 goto done;
7200 err = got_worktree_set_head_ref(worktree, resolved);
7201 if (err)
7202 goto done;
7204 err = delete_histedit_refs(worktree, repo);
7205 if (err)
7206 goto done;
7208 err = get_fileindex_path(&fileindex_path, worktree);
7209 if (err)
7210 goto done;
7211 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7212 sync_err = sync_fileindex(fileindex, fileindex_path);
7213 if (sync_err && err == NULL)
7214 err = sync_err;
7215 done:
7216 got_fileindex_free(fileindex);
7217 free(fileindex_path);
7218 free(new_head_commit_id);
7219 unlockerr = lock_worktree(worktree, LOCK_SH);
7220 if (unlockerr && err == NULL)
7221 err = unlockerr;
7222 return err;
7225 const struct got_error *
7226 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7227 struct got_object_id *commit_id, struct got_repository *repo)
7229 const struct got_error *err;
7230 char *commit_ref_name;
7232 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7233 if (err)
7234 return err;
7236 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7237 if (err)
7238 goto done;
7240 err = delete_ref(commit_ref_name, repo);
7241 done:
7242 free(commit_ref_name);
7243 return err;
7246 const struct got_error *
7247 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7248 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7249 struct got_worktree *worktree, const char *refname,
7250 struct got_repository *repo)
7252 const struct got_error *err = NULL;
7253 char *fileindex_path = NULL;
7254 struct check_rebase_ok_arg ok_arg;
7256 *fileindex = NULL;
7257 *branch_ref = NULL;
7258 *base_branch_ref = NULL;
7260 err = lock_worktree(worktree, LOCK_EX);
7261 if (err)
7262 return err;
7264 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7265 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7266 "cannot integrate a branch into itself; "
7267 "update -b or different branch name required");
7268 goto done;
7271 err = open_fileindex(fileindex, &fileindex_path, worktree);
7272 if (err)
7273 goto done;
7275 /* Preconditions are the same as for rebase. */
7276 ok_arg.worktree = worktree;
7277 ok_arg.repo = repo;
7278 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7279 &ok_arg);
7280 if (err)
7281 goto done;
7283 err = got_ref_open(branch_ref, repo, refname, 1);
7284 if (err)
7285 goto done;
7287 err = got_ref_open(base_branch_ref, repo,
7288 got_worktree_get_head_ref_name(worktree), 1);
7289 done:
7290 if (err) {
7291 if (*branch_ref) {
7292 got_ref_close(*branch_ref);
7293 *branch_ref = NULL;
7295 if (*base_branch_ref) {
7296 got_ref_close(*base_branch_ref);
7297 *base_branch_ref = NULL;
7299 if (*fileindex) {
7300 got_fileindex_free(*fileindex);
7301 *fileindex = NULL;
7303 lock_worktree(worktree, LOCK_SH);
7305 return err;
7308 const struct got_error *
7309 got_worktree_integrate_continue(struct got_worktree *worktree,
7310 struct got_fileindex *fileindex, struct got_repository *repo,
7311 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7312 got_worktree_checkout_cb progress_cb, void *progress_arg,
7313 got_cancel_cb cancel_cb, void *cancel_arg)
7315 const struct got_error *err = NULL, *sync_err, *unlockerr;
7316 char *fileindex_path = NULL;
7317 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7318 struct got_commit_object *commit = NULL;
7320 err = get_fileindex_path(&fileindex_path, worktree);
7321 if (err)
7322 goto done;
7324 err = got_ref_resolve(&commit_id, repo, branch_ref);
7325 if (err)
7326 goto done;
7328 err = got_object_open_as_commit(&commit, repo, commit_id);
7329 if (err)
7330 goto done;
7332 err = got_object_id_by_path(&tree_id, repo, commit,
7333 worktree->path_prefix);
7334 if (err)
7335 goto done;
7337 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7338 if (err)
7339 goto done;
7341 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7342 progress_cb, progress_arg, cancel_cb, cancel_arg);
7343 if (err)
7344 goto sync;
7346 err = got_ref_change_ref(base_branch_ref, commit_id);
7347 if (err)
7348 goto sync;
7350 err = got_ref_write(base_branch_ref, repo);
7351 if (err)
7352 goto sync;
7354 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7355 sync:
7356 sync_err = sync_fileindex(fileindex, fileindex_path);
7357 if (sync_err && err == NULL)
7358 err = sync_err;
7360 done:
7361 unlockerr = got_ref_unlock(branch_ref);
7362 if (unlockerr && err == NULL)
7363 err = unlockerr;
7364 got_ref_close(branch_ref);
7366 unlockerr = got_ref_unlock(base_branch_ref);
7367 if (unlockerr && err == NULL)
7368 err = unlockerr;
7369 got_ref_close(base_branch_ref);
7371 got_fileindex_free(fileindex);
7372 free(fileindex_path);
7373 free(tree_id);
7374 if (commit)
7375 got_object_commit_close(commit);
7377 unlockerr = lock_worktree(worktree, LOCK_SH);
7378 if (unlockerr && err == NULL)
7379 err = unlockerr;
7380 return err;
7383 const struct got_error *
7384 got_worktree_integrate_abort(struct got_worktree *worktree,
7385 struct got_fileindex *fileindex, struct got_repository *repo,
7386 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7388 const struct got_error *err = NULL, *unlockerr = NULL;
7390 got_fileindex_free(fileindex);
7392 err = lock_worktree(worktree, LOCK_SH);
7394 unlockerr = got_ref_unlock(branch_ref);
7395 if (unlockerr && err == NULL)
7396 err = unlockerr;
7397 got_ref_close(branch_ref);
7399 unlockerr = got_ref_unlock(base_branch_ref);
7400 if (unlockerr && err == NULL)
7401 err = unlockerr;
7402 got_ref_close(base_branch_ref);
7404 return err;
7407 const struct got_error *
7408 got_worktree_merge_postpone(struct got_worktree *worktree,
7409 struct got_fileindex *fileindex)
7411 const struct got_error *err, *sync_err;
7412 char *fileindex_path = NULL;
7414 err = get_fileindex_path(&fileindex_path, worktree);
7415 if (err)
7416 goto done;
7418 sync_err = sync_fileindex(fileindex, fileindex_path);
7420 err = lock_worktree(worktree, LOCK_SH);
7421 if (sync_err && err == NULL)
7422 err = sync_err;
7423 done:
7424 got_fileindex_free(fileindex);
7425 free(fileindex_path);
7426 return err;
7429 static const struct got_error *
7430 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7432 const struct got_error *err;
7433 char *branch_refname = NULL, *commit_refname = NULL;
7435 err = get_merge_branch_ref_name(&branch_refname, worktree);
7436 if (err)
7437 goto done;
7438 err = delete_ref(branch_refname, repo);
7439 if (err)
7440 goto done;
7442 err = get_merge_commit_ref_name(&commit_refname, worktree);
7443 if (err)
7444 goto done;
7445 err = delete_ref(commit_refname, repo);
7446 if (err)
7447 goto done;
7449 done:
7450 free(branch_refname);
7451 free(commit_refname);
7452 return err;
7455 struct merge_commit_msg_arg {
7456 struct got_worktree *worktree;
7457 const char *branch_name;
7460 static const struct got_error *
7461 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths, char **logmsg,
7462 void *arg)
7464 struct merge_commit_msg_arg *a = arg;
7466 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7467 got_worktree_get_head_ref_name(a->worktree)) == -1)
7468 return got_error_from_errno("asprintf");
7470 return NULL;
7474 const struct got_error *
7475 got_worktree_merge_branch(struct got_worktree *worktree,
7476 struct got_fileindex *fileindex,
7477 struct got_object_id *yca_commit_id,
7478 struct got_object_id *branch_tip,
7479 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7480 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7482 const struct got_error *err;
7483 char *fileindex_path = NULL;
7485 err = get_fileindex_path(&fileindex_path, worktree);
7486 if (err)
7487 goto done;
7489 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7490 worktree);
7491 if (err)
7492 goto done;
7494 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7495 branch_tip, repo, progress_cb, progress_arg,
7496 cancel_cb, cancel_arg);
7497 done:
7498 free(fileindex_path);
7499 return err;
7502 const struct got_error *
7503 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7504 struct got_worktree *worktree, struct got_fileindex *fileindex,
7505 const char *author, const char *committer, int allow_bad_symlinks,
7506 struct got_object_id *branch_tip, const char *branch_name,
7507 struct got_repository *repo,
7508 got_worktree_status_cb status_cb, void *status_arg)
7511 const struct got_error *err = NULL, *sync_err;
7512 struct got_pathlist_head commitable_paths;
7513 struct collect_commitables_arg cc_arg;
7514 struct got_pathlist_entry *pe;
7515 struct got_reference *head_ref = NULL;
7516 struct got_object_id *head_commit_id = NULL;
7517 int have_staged_files = 0;
7518 struct merge_commit_msg_arg mcm_arg;
7519 char *fileindex_path = NULL;
7521 *new_commit_id = NULL;
7523 TAILQ_INIT(&commitable_paths);
7525 err = get_fileindex_path(&fileindex_path, worktree);
7526 if (err)
7527 goto done;
7529 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7530 if (err)
7531 goto done;
7533 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7534 if (err)
7535 goto done;
7537 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7538 &have_staged_files);
7539 if (err && err->code != GOT_ERR_CANCELLED)
7540 goto done;
7541 if (have_staged_files) {
7542 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7543 goto done;
7546 cc_arg.commitable_paths = &commitable_paths;
7547 cc_arg.worktree = worktree;
7548 cc_arg.fileindex = fileindex;
7549 cc_arg.repo = repo;
7550 cc_arg.have_staged_files = have_staged_files;
7551 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7552 err = worktree_status(worktree, "", fileindex, repo,
7553 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7554 if (err)
7555 goto done;
7557 if (TAILQ_EMPTY(&commitable_paths)) {
7558 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7559 "merge of %s cannot proceed", branch_name);
7560 goto done;
7563 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7564 struct got_commitable *ct = pe->data;
7565 const char *ct_path = ct->in_repo_path;
7567 while (ct_path[0] == '/')
7568 ct_path++;
7569 err = check_out_of_date(ct_path, ct->status,
7570 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7571 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7572 if (err)
7573 goto done;
7577 mcm_arg.worktree = worktree;
7578 mcm_arg.branch_name = branch_name;
7579 err = commit_worktree(new_commit_id, &commitable_paths,
7580 head_commit_id, branch_tip, worktree, author, committer,
7581 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7582 if (err)
7583 goto done;
7585 err = update_fileindex_after_commit(worktree, &commitable_paths,
7586 *new_commit_id, fileindex, have_staged_files);
7587 sync_err = sync_fileindex(fileindex, fileindex_path);
7588 if (sync_err && err == NULL)
7589 err = sync_err;
7590 done:
7591 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7592 struct got_commitable *ct = pe->data;
7593 free_commitable(ct);
7595 got_pathlist_free(&commitable_paths);
7596 free(fileindex_path);
7597 return err;
7600 const struct got_error *
7601 got_worktree_merge_complete(struct got_worktree *worktree,
7602 struct got_fileindex *fileindex, struct got_repository *repo)
7604 const struct got_error *err, *unlockerr, *sync_err;
7605 char *fileindex_path = NULL;
7607 err = delete_merge_refs(worktree, repo);
7608 if (err)
7609 goto done;
7611 err = get_fileindex_path(&fileindex_path, worktree);
7612 if (err)
7613 goto done;
7614 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7615 sync_err = sync_fileindex(fileindex, fileindex_path);
7616 if (sync_err && err == NULL)
7617 err = sync_err;
7618 done:
7619 got_fileindex_free(fileindex);
7620 free(fileindex_path);
7621 unlockerr = lock_worktree(worktree, LOCK_SH);
7622 if (unlockerr && err == NULL)
7623 err = unlockerr;
7624 return err;
7627 const struct got_error *
7628 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7629 struct got_repository *repo)
7631 const struct got_error *err;
7632 char *branch_refname = NULL;
7633 struct got_reference *branch_ref = NULL;
7635 *in_progress = 0;
7637 err = get_merge_branch_ref_name(&branch_refname, worktree);
7638 if (err)
7639 return err;
7640 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7641 free(branch_refname);
7642 if (err) {
7643 if (err->code != GOT_ERR_NOT_REF)
7644 return err;
7645 } else
7646 *in_progress = 1;
7648 return NULL;
7651 const struct got_error *got_worktree_merge_prepare(
7652 struct got_fileindex **fileindex, struct got_worktree *worktree,
7653 struct got_reference *branch, struct got_repository *repo)
7655 const struct got_error *err = NULL;
7656 char *fileindex_path = NULL;
7657 char *branch_refname = NULL, *commit_refname = NULL;
7658 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
7659 struct got_reference *commit_ref = NULL;
7660 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
7661 struct check_rebase_ok_arg ok_arg;
7663 *fileindex = NULL;
7665 err = lock_worktree(worktree, LOCK_EX);
7666 if (err)
7667 return err;
7669 err = open_fileindex(fileindex, &fileindex_path, worktree);
7670 if (err)
7671 goto done;
7673 /* Preconditions are the same as for rebase. */
7674 ok_arg.worktree = worktree;
7675 ok_arg.repo = repo;
7676 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7677 &ok_arg);
7678 if (err)
7679 goto done;
7681 err = get_merge_branch_ref_name(&branch_refname, worktree);
7682 if (err)
7683 return err;
7685 err = get_merge_commit_ref_name(&commit_refname, worktree);
7686 if (err)
7687 return err;
7689 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7690 0);
7691 if (err)
7692 goto done;
7694 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
7695 if (err)
7696 goto done;
7698 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
7699 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
7700 goto done;
7703 err = got_ref_resolve(&branch_tip, repo, branch);
7704 if (err)
7705 goto done;
7707 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
7708 if (err)
7709 goto done;
7710 err = got_ref_write(branch_ref, repo);
7711 if (err)
7712 goto done;
7714 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
7715 if (err)
7716 goto done;
7717 err = got_ref_write(commit_ref, repo);
7718 if (err)
7719 goto done;
7721 done:
7722 free(branch_refname);
7723 free(commit_refname);
7724 free(fileindex_path);
7725 if (branch_ref)
7726 got_ref_close(branch_ref);
7727 if (commit_ref)
7728 got_ref_close(commit_ref);
7729 if (wt_branch)
7730 got_ref_close(wt_branch);
7731 free(wt_branch_tip);
7732 if (err) {
7733 if (*fileindex) {
7734 got_fileindex_free(*fileindex);
7735 *fileindex = NULL;
7737 lock_worktree(worktree, LOCK_SH);
7739 return err;
7742 const struct got_error *
7743 got_worktree_merge_continue(char **branch_name,
7744 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
7745 struct got_worktree *worktree, struct got_repository *repo)
7747 const struct got_error *err;
7748 char *commit_refname = NULL, *branch_refname = NULL;
7749 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
7750 char *fileindex_path = NULL;
7751 int have_staged_files = 0;
7753 *branch_name = NULL;
7754 *branch_tip = NULL;
7755 *fileindex = NULL;
7757 err = lock_worktree(worktree, LOCK_EX);
7758 if (err)
7759 return err;
7761 err = open_fileindex(fileindex, &fileindex_path, worktree);
7762 if (err)
7763 goto done;
7765 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7766 &have_staged_files);
7767 if (err && err->code != GOT_ERR_CANCELLED)
7768 goto done;
7769 if (have_staged_files) {
7770 err = got_error(GOT_ERR_STAGED_PATHS);
7771 goto done;
7774 err = get_merge_branch_ref_name(&branch_refname, worktree);
7775 if (err)
7776 goto done;
7778 err = get_merge_commit_ref_name(&commit_refname, worktree);
7779 if (err)
7780 goto done;
7782 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7783 if (err)
7784 goto done;
7786 if (!got_ref_is_symbolic(branch_ref)) {
7787 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7788 "%s is not a symbolic reference",
7789 got_ref_get_name(branch_ref));
7790 goto done;
7792 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
7793 if (*branch_name == NULL) {
7794 err = got_error_from_errno("strdup");
7795 goto done;
7798 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
7799 if (err)
7800 goto done;
7802 err = got_ref_resolve(branch_tip, repo, commit_ref);
7803 if (err)
7804 goto done;
7805 done:
7806 free(commit_refname);
7807 free(branch_refname);
7808 free(fileindex_path);
7809 if (commit_ref)
7810 got_ref_close(commit_ref);
7811 if (branch_ref)
7812 got_ref_close(branch_ref);
7813 if (err) {
7814 if (*branch_name) {
7815 free(*branch_name);
7816 *branch_name = NULL;
7818 free(*branch_tip);
7819 *branch_tip = NULL;
7820 if (*fileindex) {
7821 got_fileindex_free(*fileindex);
7822 *fileindex = NULL;
7824 lock_worktree(worktree, LOCK_SH);
7826 return err;
7829 const struct got_error *
7830 got_worktree_merge_abort(struct got_worktree *worktree,
7831 struct got_fileindex *fileindex, struct got_repository *repo,
7832 got_worktree_checkout_cb progress_cb, void *progress_arg)
7834 const struct got_error *err, *unlockerr, *sync_err;
7835 struct got_object_id *commit_id = NULL;
7836 struct got_commit_object *commit = NULL;
7837 char *fileindex_path = NULL;
7838 struct revert_file_args rfa;
7839 struct got_object_id *tree_id = NULL;
7841 err = got_object_open_as_commit(&commit, repo,
7842 worktree->base_commit_id);
7843 if (err)
7844 goto done;
7846 err = got_object_id_by_path(&tree_id, repo, commit,
7847 worktree->path_prefix);
7848 if (err)
7849 goto done;
7851 err = delete_merge_refs(worktree, repo);
7852 if (err)
7853 goto done;
7855 err = get_fileindex_path(&fileindex_path, worktree);
7856 if (err)
7857 goto done;
7859 rfa.worktree = worktree;
7860 rfa.fileindex = fileindex;
7861 rfa.progress_cb = progress_cb;
7862 rfa.progress_arg = progress_arg;
7863 rfa.patch_cb = NULL;
7864 rfa.patch_arg = NULL;
7865 rfa.repo = repo;
7866 rfa.unlink_added_files = 1;
7867 err = worktree_status(worktree, "", fileindex, repo,
7868 revert_file, &rfa, NULL, NULL, 1, 0);
7869 if (err)
7870 goto sync;
7872 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7873 repo, progress_cb, progress_arg, NULL, NULL);
7874 sync:
7875 sync_err = sync_fileindex(fileindex, fileindex_path);
7876 if (sync_err && err == NULL)
7877 err = sync_err;
7878 done:
7879 free(tree_id);
7880 free(commit_id);
7881 if (commit)
7882 got_object_commit_close(commit);
7883 if (fileindex)
7884 got_fileindex_free(fileindex);
7885 free(fileindex_path);
7887 unlockerr = lock_worktree(worktree, LOCK_SH);
7888 if (unlockerr && err == NULL)
7889 err = unlockerr;
7890 return err;
7893 struct check_stage_ok_arg {
7894 struct got_object_id *head_commit_id;
7895 struct got_worktree *worktree;
7896 struct got_fileindex *fileindex;
7897 struct got_repository *repo;
7898 int have_changes;
7901 const struct got_error *
7902 check_stage_ok(void *arg, unsigned char status,
7903 unsigned char staged_status, const char *relpath,
7904 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7905 struct got_object_id *commit_id, int dirfd, const char *de_name)
7907 struct check_stage_ok_arg *a = arg;
7908 const struct got_error *err = NULL;
7909 struct got_fileindex_entry *ie;
7910 struct got_object_id base_commit_id;
7911 struct got_object_id *base_commit_idp = NULL;
7912 char *in_repo_path = NULL, *p;
7914 if (status == GOT_STATUS_UNVERSIONED ||
7915 status == GOT_STATUS_NO_CHANGE)
7916 return NULL;
7917 if (status == GOT_STATUS_NONEXISTENT)
7918 return got_error_set_errno(ENOENT, relpath);
7920 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7921 if (ie == NULL)
7922 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7924 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7925 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7926 relpath) == -1)
7927 return got_error_from_errno("asprintf");
7929 if (got_fileindex_entry_has_commit(ie)) {
7930 memcpy(base_commit_id.sha1, ie->commit_sha1,
7931 SHA1_DIGEST_LENGTH);
7932 base_commit_idp = &base_commit_id;
7935 if (status == GOT_STATUS_CONFLICT) {
7936 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7937 goto done;
7938 } else if (status != GOT_STATUS_ADD &&
7939 status != GOT_STATUS_MODIFY &&
7940 status != GOT_STATUS_DELETE) {
7941 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7942 goto done;
7945 a->have_changes = 1;
7947 p = in_repo_path;
7948 while (p[0] == '/')
7949 p++;
7950 err = check_out_of_date(p, status, staged_status,
7951 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7952 GOT_ERR_STAGE_OUT_OF_DATE);
7953 done:
7954 free(in_repo_path);
7955 return err;
7958 struct stage_path_arg {
7959 struct got_worktree *worktree;
7960 struct got_fileindex *fileindex;
7961 struct got_repository *repo;
7962 got_worktree_status_cb status_cb;
7963 void *status_arg;
7964 got_worktree_patch_cb patch_cb;
7965 void *patch_arg;
7966 int staged_something;
7967 int allow_bad_symlinks;
7970 static const struct got_error *
7971 stage_path(void *arg, unsigned char status,
7972 unsigned char staged_status, const char *relpath,
7973 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7974 struct got_object_id *commit_id, int dirfd, const char *de_name)
7976 struct stage_path_arg *a = arg;
7977 const struct got_error *err = NULL;
7978 struct got_fileindex_entry *ie;
7979 char *ondisk_path = NULL, *path_content = NULL;
7980 uint32_t stage;
7981 struct got_object_id *new_staged_blob_id = NULL;
7982 struct stat sb;
7984 if (status == GOT_STATUS_UNVERSIONED)
7985 return NULL;
7987 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7988 if (ie == NULL)
7989 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7991 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7992 relpath)== -1)
7993 return got_error_from_errno("asprintf");
7995 switch (status) {
7996 case GOT_STATUS_ADD:
7997 case GOT_STATUS_MODIFY:
7998 /* XXX could sb.st_mode be passed in by our caller? */
7999 if (lstat(ondisk_path, &sb) == -1) {
8000 err = got_error_from_errno2("lstat", ondisk_path);
8001 break;
8003 if (a->patch_cb) {
8004 if (status == GOT_STATUS_ADD) {
8005 int choice = GOT_PATCH_CHOICE_NONE;
8006 err = (*a->patch_cb)(&choice, a->patch_arg,
8007 status, ie->path, NULL, 1, 1);
8008 if (err)
8009 break;
8010 if (choice != GOT_PATCH_CHOICE_YES)
8011 break;
8012 } else {
8013 err = create_patched_content(&path_content, 0,
8014 staged_blob_id ? staged_blob_id : blob_id,
8015 ondisk_path, dirfd, de_name, ie->path,
8016 a->repo, a->patch_cb, a->patch_arg);
8017 if (err || path_content == NULL)
8018 break;
8021 err = got_object_blob_create(&new_staged_blob_id,
8022 path_content ? path_content : ondisk_path, a->repo);
8023 if (err)
8024 break;
8025 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8026 SHA1_DIGEST_LENGTH);
8027 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8028 stage = GOT_FILEIDX_STAGE_ADD;
8029 else
8030 stage = GOT_FILEIDX_STAGE_MODIFY;
8031 got_fileindex_entry_stage_set(ie, stage);
8032 if (S_ISLNK(sb.st_mode)) {
8033 int is_bad_symlink = 0;
8034 if (!a->allow_bad_symlinks) {
8035 char target_path[PATH_MAX];
8036 ssize_t target_len;
8037 target_len = readlink(ondisk_path, target_path,
8038 sizeof(target_path));
8039 if (target_len == -1) {
8040 err = got_error_from_errno2("readlink",
8041 ondisk_path);
8042 break;
8044 err = is_bad_symlink_target(&is_bad_symlink,
8045 target_path, target_len, ondisk_path,
8046 a->worktree->root_path);
8047 if (err)
8048 break;
8049 if (is_bad_symlink) {
8050 err = got_error_path(ondisk_path,
8051 GOT_ERR_BAD_SYMLINK);
8052 break;
8055 if (is_bad_symlink)
8056 got_fileindex_entry_staged_filetype_set(ie,
8057 GOT_FILEIDX_MODE_BAD_SYMLINK);
8058 else
8059 got_fileindex_entry_staged_filetype_set(ie,
8060 GOT_FILEIDX_MODE_SYMLINK);
8061 } else {
8062 got_fileindex_entry_staged_filetype_set(ie,
8063 GOT_FILEIDX_MODE_REGULAR_FILE);
8065 a->staged_something = 1;
8066 if (a->status_cb == NULL)
8067 break;
8068 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8069 get_staged_status(ie), relpath, blob_id,
8070 new_staged_blob_id, NULL, dirfd, de_name);
8071 break;
8072 case GOT_STATUS_DELETE:
8073 if (staged_status == GOT_STATUS_DELETE)
8074 break;
8075 if (a->patch_cb) {
8076 int choice = GOT_PATCH_CHOICE_NONE;
8077 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8078 ie->path, NULL, 1, 1);
8079 if (err)
8080 break;
8081 if (choice == GOT_PATCH_CHOICE_NO)
8082 break;
8083 if (choice != GOT_PATCH_CHOICE_YES) {
8084 err = got_error(GOT_ERR_PATCH_CHOICE);
8085 break;
8088 stage = GOT_FILEIDX_STAGE_DELETE;
8089 got_fileindex_entry_stage_set(ie, stage);
8090 a->staged_something = 1;
8091 if (a->status_cb == NULL)
8092 break;
8093 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8094 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8095 de_name);
8096 break;
8097 case GOT_STATUS_NO_CHANGE:
8098 break;
8099 case GOT_STATUS_CONFLICT:
8100 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8101 break;
8102 case GOT_STATUS_NONEXISTENT:
8103 err = got_error_set_errno(ENOENT, relpath);
8104 break;
8105 default:
8106 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8107 break;
8110 if (path_content && unlink(path_content) == -1 && err == NULL)
8111 err = got_error_from_errno2("unlink", path_content);
8112 free(path_content);
8113 free(ondisk_path);
8114 free(new_staged_blob_id);
8115 return err;
8118 const struct got_error *
8119 got_worktree_stage(struct got_worktree *worktree,
8120 struct got_pathlist_head *paths,
8121 got_worktree_status_cb status_cb, void *status_arg,
8122 got_worktree_patch_cb patch_cb, void *patch_arg,
8123 int allow_bad_symlinks, struct got_repository *repo)
8125 const struct got_error *err = NULL, *sync_err, *unlockerr;
8126 struct got_pathlist_entry *pe;
8127 struct got_fileindex *fileindex = NULL;
8128 char *fileindex_path = NULL;
8129 struct got_reference *head_ref = NULL;
8130 struct got_object_id *head_commit_id = NULL;
8131 struct check_stage_ok_arg oka;
8132 struct stage_path_arg spa;
8134 err = lock_worktree(worktree, LOCK_EX);
8135 if (err)
8136 return err;
8138 err = got_ref_open(&head_ref, repo,
8139 got_worktree_get_head_ref_name(worktree), 0);
8140 if (err)
8141 goto done;
8142 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8143 if (err)
8144 goto done;
8145 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8146 if (err)
8147 goto done;
8149 /* Check pre-conditions before staging anything. */
8150 oka.head_commit_id = head_commit_id;
8151 oka.worktree = worktree;
8152 oka.fileindex = fileindex;
8153 oka.repo = repo;
8154 oka.have_changes = 0;
8155 TAILQ_FOREACH(pe, paths, entry) {
8156 err = worktree_status(worktree, pe->path, fileindex, repo,
8157 check_stage_ok, &oka, NULL, NULL, 1, 0);
8158 if (err)
8159 goto done;
8161 if (!oka.have_changes) {
8162 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8163 goto done;
8166 spa.worktree = worktree;
8167 spa.fileindex = fileindex;
8168 spa.repo = repo;
8169 spa.patch_cb = patch_cb;
8170 spa.patch_arg = patch_arg;
8171 spa.status_cb = status_cb;
8172 spa.status_arg = status_arg;
8173 spa.staged_something = 0;
8174 spa.allow_bad_symlinks = allow_bad_symlinks;
8175 TAILQ_FOREACH(pe, paths, entry) {
8176 err = worktree_status(worktree, pe->path, fileindex, repo,
8177 stage_path, &spa, NULL, NULL, 1, 0);
8178 if (err)
8179 goto done;
8181 if (!spa.staged_something) {
8182 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8183 goto done;
8186 sync_err = sync_fileindex(fileindex, fileindex_path);
8187 if (sync_err && err == NULL)
8188 err = sync_err;
8189 done:
8190 if (head_ref)
8191 got_ref_close(head_ref);
8192 free(head_commit_id);
8193 free(fileindex_path);
8194 if (fileindex)
8195 got_fileindex_free(fileindex);
8196 unlockerr = lock_worktree(worktree, LOCK_SH);
8197 if (unlockerr && err == NULL)
8198 err = unlockerr;
8199 return err;
8202 struct unstage_path_arg {
8203 struct got_worktree *worktree;
8204 struct got_fileindex *fileindex;
8205 struct got_repository *repo;
8206 got_worktree_checkout_cb progress_cb;
8207 void *progress_arg;
8208 got_worktree_patch_cb patch_cb;
8209 void *patch_arg;
8212 static const struct got_error *
8213 create_unstaged_content(char **path_unstaged_content,
8214 char **path_new_staged_content, struct got_object_id *blob_id,
8215 struct got_object_id *staged_blob_id, const char *relpath,
8216 struct got_repository *repo,
8217 got_worktree_patch_cb patch_cb, void *patch_arg)
8219 const struct got_error *err, *free_err;
8220 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8221 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8222 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8223 struct got_diffreg_result *diffreg_result = NULL;
8224 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8225 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8227 *path_unstaged_content = NULL;
8228 *path_new_staged_content = NULL;
8230 err = got_object_id_str(&label1, blob_id);
8231 if (err)
8232 return err;
8233 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
8234 if (err)
8235 goto done;
8237 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
8238 if (err)
8239 goto done;
8241 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8242 if (err)
8243 goto done;
8245 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
8246 if (err)
8247 goto done;
8249 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
8250 if (err)
8251 goto done;
8253 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8254 if (err)
8255 goto done;
8257 err = got_diff_files(&diffreg_result, f1, label1, f2,
8258 path2, 3, 0, 1, NULL);
8259 if (err)
8260 goto done;
8262 err = got_opentemp_named(path_unstaged_content, &outfile,
8263 "got-unstaged-content");
8264 if (err)
8265 goto done;
8266 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8267 "got-new-staged-content");
8268 if (err)
8269 goto done;
8271 if (fseek(f1, 0L, SEEK_SET) == -1) {
8272 err = got_ferror(f1, GOT_ERR_IO);
8273 goto done;
8275 if (fseek(f2, 0L, SEEK_SET) == -1) {
8276 err = got_ferror(f2, GOT_ERR_IO);
8277 goto done;
8279 /* Count the number of actual changes in the diff result. */
8280 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8281 struct diff_chunk_context cc = {};
8282 diff_chunk_context_load_change(&cc, &nchunks_used,
8283 diffreg_result->result, n, 0);
8284 nchanges++;
8286 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8287 int choice;
8288 err = apply_or_reject_change(&choice, &nchunks_used,
8289 diffreg_result->result, n, relpath, f1, f2,
8290 &line_cur1, &line_cur2,
8291 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8292 if (err)
8293 goto done;
8294 if (choice == GOT_PATCH_CHOICE_YES)
8295 have_content = 1;
8296 else
8297 have_rejected_content = 1;
8298 if (choice == GOT_PATCH_CHOICE_QUIT)
8299 break;
8301 if (have_content || have_rejected_content)
8302 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8303 outfile, rejectfile);
8304 done:
8305 free(label1);
8306 if (blob)
8307 got_object_blob_close(blob);
8308 if (staged_blob)
8309 got_object_blob_close(staged_blob);
8310 free_err = got_diffreg_result_free(diffreg_result);
8311 if (free_err && err == NULL)
8312 err = free_err;
8313 if (f1 && fclose(f1) == EOF && err == NULL)
8314 err = got_error_from_errno2("fclose", path1);
8315 if (f2 && fclose(f2) == EOF && err == NULL)
8316 err = got_error_from_errno2("fclose", path2);
8317 if (outfile && fclose(outfile) == EOF && err == NULL)
8318 err = got_error_from_errno2("fclose", *path_unstaged_content);
8319 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8320 err = got_error_from_errno2("fclose", *path_new_staged_content);
8321 if (path1 && unlink(path1) == -1 && err == NULL)
8322 err = got_error_from_errno2("unlink", path1);
8323 if (path2 && unlink(path2) == -1 && err == NULL)
8324 err = got_error_from_errno2("unlink", path2);
8325 if (err || !have_content) {
8326 if (*path_unstaged_content &&
8327 unlink(*path_unstaged_content) == -1 && err == NULL)
8328 err = got_error_from_errno2("unlink",
8329 *path_unstaged_content);
8330 free(*path_unstaged_content);
8331 *path_unstaged_content = NULL;
8333 if (err || !have_content || !have_rejected_content) {
8334 if (*path_new_staged_content &&
8335 unlink(*path_new_staged_content) == -1 && err == NULL)
8336 err = got_error_from_errno2("unlink",
8337 *path_new_staged_content);
8338 free(*path_new_staged_content);
8339 *path_new_staged_content = NULL;
8341 free(path1);
8342 free(path2);
8343 return err;
8346 static const struct got_error *
8347 unstage_hunks(struct got_object_id *staged_blob_id,
8348 struct got_blob_object *blob_base,
8349 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8350 const char *ondisk_path, const char *label_orig,
8351 struct got_worktree *worktree, struct got_repository *repo,
8352 got_worktree_patch_cb patch_cb, void *patch_arg,
8353 got_worktree_checkout_cb progress_cb, void *progress_arg)
8355 const struct got_error *err = NULL;
8356 char *path_unstaged_content = NULL;
8357 char *path_new_staged_content = NULL;
8358 char *parent = NULL, *base_path = NULL;
8359 char *blob_base_path = NULL;
8360 struct got_object_id *new_staged_blob_id = NULL;
8361 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8362 struct stat sb;
8364 err = create_unstaged_content(&path_unstaged_content,
8365 &path_new_staged_content, blob_id, staged_blob_id,
8366 ie->path, repo, patch_cb, patch_arg);
8367 if (err)
8368 return err;
8370 if (path_unstaged_content == NULL)
8371 return NULL;
8373 if (path_new_staged_content) {
8374 err = got_object_blob_create(&new_staged_blob_id,
8375 path_new_staged_content, repo);
8376 if (err)
8377 goto done;
8380 f = fopen(path_unstaged_content, "re");
8381 if (f == NULL) {
8382 err = got_error_from_errno2("fopen",
8383 path_unstaged_content);
8384 goto done;
8386 if (fstat(fileno(f), &sb) == -1) {
8387 err = got_error_from_errno2("fstat", path_unstaged_content);
8388 goto done;
8390 if (got_fileindex_entry_staged_filetype_get(ie) ==
8391 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8392 char link_target[PATH_MAX];
8393 size_t r;
8394 r = fread(link_target, 1, sizeof(link_target), f);
8395 if (r == 0 && ferror(f)) {
8396 err = got_error_from_errno("fread");
8397 goto done;
8399 if (r >= sizeof(link_target)) { /* should not happen */
8400 err = got_error(GOT_ERR_NO_SPACE);
8401 goto done;
8403 link_target[r] = '\0';
8404 err = merge_symlink(worktree, blob_base,
8405 ondisk_path, ie->path, label_orig, link_target,
8406 worktree->base_commit_id, repo, progress_cb,
8407 progress_arg);
8408 } else {
8409 int local_changes_subsumed;
8411 err = got_path_dirname(&parent, ondisk_path);
8412 if (err)
8413 return err;
8415 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8416 parent) == -1) {
8417 err = got_error_from_errno("asprintf");
8418 base_path = NULL;
8419 goto done;
8422 err = got_opentemp_named(&blob_base_path, &f_base, base_path);
8423 if (err)
8424 goto done;
8425 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8426 blob_base);
8427 if (err)
8428 goto done;
8431 * In order the run a 3-way merge with a symlink we copy the symlink's
8432 * target path into a temporary file and use that file with diff3.
8434 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8435 err = dump_symlink_target_path_to_file(&f_deriv2,
8436 ondisk_path);
8437 if (err)
8438 goto done;
8439 } else {
8440 int fd;
8441 fd = open(ondisk_path,
8442 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8443 if (fd == -1) {
8444 err = got_error_from_errno2("open", ondisk_path);
8445 goto done;
8447 f_deriv2 = fdopen(fd, "r");
8448 if (f_deriv2 == NULL) {
8449 err = got_error_from_errno2("fdopen", ondisk_path);
8450 close(fd);
8451 goto done;
8455 err = merge_file(&local_changes_subsumed, worktree,
8456 f_base, f, f_deriv2, ondisk_path, ie->path,
8457 got_fileindex_perms_to_st(ie),
8458 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8459 repo, progress_cb, progress_arg);
8461 if (err)
8462 goto done;
8464 if (new_staged_blob_id) {
8465 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8466 SHA1_DIGEST_LENGTH);
8467 } else {
8468 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8469 got_fileindex_entry_staged_filetype_set(ie, 0);
8471 done:
8472 free(new_staged_blob_id);
8473 if (path_unstaged_content &&
8474 unlink(path_unstaged_content) == -1 && err == NULL)
8475 err = got_error_from_errno2("unlink", path_unstaged_content);
8476 if (path_new_staged_content &&
8477 unlink(path_new_staged_content) == -1 && err == NULL)
8478 err = got_error_from_errno2("unlink", path_new_staged_content);
8479 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8480 err = got_error_from_errno2("unlink", blob_base_path);
8481 if (f_base && fclose(f_base) == EOF && err == NULL)
8482 err = got_error_from_errno2("fclose", path_unstaged_content);
8483 if (f && fclose(f) == EOF && err == NULL)
8484 err = got_error_from_errno2("fclose", path_unstaged_content);
8485 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8486 err = got_error_from_errno2("fclose", ondisk_path);
8487 free(path_unstaged_content);
8488 free(path_new_staged_content);
8489 free(blob_base_path);
8490 free(parent);
8491 free(base_path);
8492 return err;
8495 static const struct got_error *
8496 unstage_path(void *arg, unsigned char status,
8497 unsigned char staged_status, const char *relpath,
8498 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8499 struct got_object_id *commit_id, int dirfd, const char *de_name)
8501 const struct got_error *err = NULL;
8502 struct unstage_path_arg *a = arg;
8503 struct got_fileindex_entry *ie;
8504 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8505 char *ondisk_path = NULL;
8506 char *id_str = NULL, *label_orig = NULL;
8507 int local_changes_subsumed;
8508 struct stat sb;
8510 if (staged_status != GOT_STATUS_ADD &&
8511 staged_status != GOT_STATUS_MODIFY &&
8512 staged_status != GOT_STATUS_DELETE)
8513 return NULL;
8515 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8516 if (ie == NULL)
8517 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8519 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8520 == -1)
8521 return got_error_from_errno("asprintf");
8523 err = got_object_id_str(&id_str,
8524 commit_id ? commit_id : a->worktree->base_commit_id);
8525 if (err)
8526 goto done;
8527 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8528 id_str) == -1) {
8529 err = got_error_from_errno("asprintf");
8530 goto done;
8533 switch (staged_status) {
8534 case GOT_STATUS_MODIFY:
8535 err = got_object_open_as_blob(&blob_base, a->repo,
8536 blob_id, 8192);
8537 if (err)
8538 break;
8539 /* fall through */
8540 case GOT_STATUS_ADD:
8541 if (a->patch_cb) {
8542 if (staged_status == GOT_STATUS_ADD) {
8543 int choice = GOT_PATCH_CHOICE_NONE;
8544 err = (*a->patch_cb)(&choice, a->patch_arg,
8545 staged_status, ie->path, NULL, 1, 1);
8546 if (err)
8547 break;
8548 if (choice != GOT_PATCH_CHOICE_YES)
8549 break;
8550 } else {
8551 err = unstage_hunks(staged_blob_id,
8552 blob_base, blob_id, ie, ondisk_path,
8553 label_orig, a->worktree, a->repo,
8554 a->patch_cb, a->patch_arg,
8555 a->progress_cb, a->progress_arg);
8556 break; /* Done with this file. */
8559 err = got_object_open_as_blob(&blob_staged, a->repo,
8560 staged_blob_id, 8192);
8561 if (err)
8562 break;
8563 switch (got_fileindex_entry_staged_filetype_get(ie)) {
8564 case GOT_FILEIDX_MODE_BAD_SYMLINK:
8565 case GOT_FILEIDX_MODE_REGULAR_FILE:
8566 err = merge_blob(&local_changes_subsumed, a->worktree,
8567 blob_base, ondisk_path, relpath,
8568 got_fileindex_perms_to_st(ie), label_orig,
8569 blob_staged, commit_id ? commit_id :
8570 a->worktree->base_commit_id, a->repo,
8571 a->progress_cb, a->progress_arg);
8572 break;
8573 case GOT_FILEIDX_MODE_SYMLINK:
8574 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8575 char *staged_target;
8576 err = got_object_blob_read_to_str(
8577 &staged_target, blob_staged);
8578 if (err)
8579 goto done;
8580 err = merge_symlink(a->worktree, blob_base,
8581 ondisk_path, relpath, label_orig,
8582 staged_target, commit_id ? commit_id :
8583 a->worktree->base_commit_id,
8584 a->repo, a->progress_cb, a->progress_arg);
8585 free(staged_target);
8586 } else {
8587 err = merge_blob(&local_changes_subsumed,
8588 a->worktree, blob_base, ondisk_path,
8589 relpath, got_fileindex_perms_to_st(ie),
8590 label_orig, blob_staged,
8591 commit_id ? commit_id :
8592 a->worktree->base_commit_id, a->repo,
8593 a->progress_cb, a->progress_arg);
8595 break;
8596 default:
8597 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8598 break;
8600 if (err == NULL) {
8601 got_fileindex_entry_stage_set(ie,
8602 GOT_FILEIDX_STAGE_NONE);
8603 got_fileindex_entry_staged_filetype_set(ie, 0);
8605 break;
8606 case GOT_STATUS_DELETE:
8607 if (a->patch_cb) {
8608 int choice = GOT_PATCH_CHOICE_NONE;
8609 err = (*a->patch_cb)(&choice, a->patch_arg,
8610 staged_status, ie->path, NULL, 1, 1);
8611 if (err)
8612 break;
8613 if (choice == GOT_PATCH_CHOICE_NO)
8614 break;
8615 if (choice != GOT_PATCH_CHOICE_YES) {
8616 err = got_error(GOT_ERR_PATCH_CHOICE);
8617 break;
8620 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8621 got_fileindex_entry_staged_filetype_set(ie, 0);
8622 err = get_file_status(&status, &sb, ie, ondisk_path,
8623 dirfd, de_name, a->repo);
8624 if (err)
8625 break;
8626 err = (*a->progress_cb)(a->progress_arg, status, relpath);
8627 break;
8629 done:
8630 free(ondisk_path);
8631 if (blob_base)
8632 got_object_blob_close(blob_base);
8633 if (blob_staged)
8634 got_object_blob_close(blob_staged);
8635 free(id_str);
8636 free(label_orig);
8637 return err;
8640 const struct got_error *
8641 got_worktree_unstage(struct got_worktree *worktree,
8642 struct got_pathlist_head *paths,
8643 got_worktree_checkout_cb progress_cb, void *progress_arg,
8644 got_worktree_patch_cb patch_cb, void *patch_arg,
8645 struct got_repository *repo)
8647 const struct got_error *err = NULL, *sync_err, *unlockerr;
8648 struct got_pathlist_entry *pe;
8649 struct got_fileindex *fileindex = NULL;
8650 char *fileindex_path = NULL;
8651 struct unstage_path_arg upa;
8653 err = lock_worktree(worktree, LOCK_EX);
8654 if (err)
8655 return err;
8657 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8658 if (err)
8659 goto done;
8661 upa.worktree = worktree;
8662 upa.fileindex = fileindex;
8663 upa.repo = repo;
8664 upa.progress_cb = progress_cb;
8665 upa.progress_arg = progress_arg;
8666 upa.patch_cb = patch_cb;
8667 upa.patch_arg = patch_arg;
8668 TAILQ_FOREACH(pe, paths, entry) {
8669 err = worktree_status(worktree, pe->path, fileindex, repo,
8670 unstage_path, &upa, NULL, NULL, 1, 0);
8671 if (err)
8672 goto done;
8675 sync_err = sync_fileindex(fileindex, fileindex_path);
8676 if (sync_err && err == NULL)
8677 err = sync_err;
8678 done:
8679 free(fileindex_path);
8680 if (fileindex)
8681 got_fileindex_free(fileindex);
8682 unlockerr = lock_worktree(worktree, LOCK_SH);
8683 if (unlockerr && err == NULL)
8684 err = unlockerr;
8685 return err;
8688 struct report_file_info_arg {
8689 struct got_worktree *worktree;
8690 got_worktree_path_info_cb info_cb;
8691 void *info_arg;
8692 struct got_pathlist_head *paths;
8693 got_cancel_cb cancel_cb;
8694 void *cancel_arg;
8697 static const struct got_error *
8698 report_file_info(void *arg, struct got_fileindex_entry *ie)
8700 struct report_file_info_arg *a = arg;
8701 struct got_pathlist_entry *pe;
8702 struct got_object_id blob_id, staged_blob_id, commit_id;
8703 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8704 struct got_object_id *commit_idp = NULL;
8705 int stage;
8707 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8708 return got_error(GOT_ERR_CANCELLED);
8710 TAILQ_FOREACH(pe, a->paths, entry) {
8711 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8712 got_path_is_child(ie->path, pe->path, pe->path_len))
8713 break;
8715 if (pe == NULL) /* not found */
8716 return NULL;
8718 if (got_fileindex_entry_has_blob(ie)) {
8719 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8720 blob_idp = &blob_id;
8722 stage = got_fileindex_entry_stage_get(ie);
8723 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8724 stage == GOT_FILEIDX_STAGE_ADD) {
8725 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8726 SHA1_DIGEST_LENGTH);
8727 staged_blob_idp = &staged_blob_id;
8730 if (got_fileindex_entry_has_commit(ie)) {
8731 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8732 commit_idp = &commit_id;
8735 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8736 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8739 const struct got_error *
8740 got_worktree_path_info(struct got_worktree *worktree,
8741 struct got_pathlist_head *paths,
8742 got_worktree_path_info_cb info_cb, void *info_arg,
8743 got_cancel_cb cancel_cb, void *cancel_arg)
8746 const struct got_error *err = NULL, *unlockerr;
8747 struct got_fileindex *fileindex = NULL;
8748 char *fileindex_path = NULL;
8749 struct report_file_info_arg arg;
8751 err = lock_worktree(worktree, LOCK_SH);
8752 if (err)
8753 return err;
8755 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8756 if (err)
8757 goto done;
8759 arg.worktree = worktree;
8760 arg.info_cb = info_cb;
8761 arg.info_arg = info_arg;
8762 arg.paths = paths;
8763 arg.cancel_cb = cancel_cb;
8764 arg.cancel_arg = cancel_arg;
8765 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8766 &arg);
8767 done:
8768 free(fileindex_path);
8769 if (fileindex)
8770 got_fileindex_free(fileindex);
8771 unlockerr = lock_worktree(worktree, LOCK_UN);
8772 if (unlockerr && err == NULL)
8773 err = unlockerr;
8774 return err;
8777 static const struct got_error *
8778 patch_check_path(const char *p, char **path, unsigned char *status,
8779 unsigned char *staged_status, struct got_fileindex *fileindex,
8780 struct got_worktree *worktree, struct got_repository *repo)
8782 const struct got_error *err;
8783 struct got_fileindex_entry *ie;
8784 struct stat sb;
8785 char *ondisk_path = NULL;
8787 err = got_worktree_resolve_path(path, worktree, p);
8788 if (err)
8789 return err;
8791 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
8792 *path[0] ? "/" : "", *path) == -1)
8793 return got_error_from_errno("asprintf");
8795 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
8796 if (ie) {
8797 *staged_status = get_staged_status(ie);
8798 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
8799 repo);
8800 if (err)
8801 goto done;
8802 } else {
8803 *staged_status = GOT_STATUS_NO_CHANGE;
8804 *status = GOT_STATUS_UNVERSIONED;
8805 if (lstat(ondisk_path, &sb) == -1) {
8806 if (errno != ENOENT) {
8807 err = got_error_from_errno2("lstat",
8808 ondisk_path);
8809 goto done;
8811 *status = GOT_STATUS_NONEXISTENT;
8815 done:
8816 free(ondisk_path);
8817 return err;
8820 static const struct got_error *
8821 patch_can_rm(const char *path, unsigned char status,
8822 unsigned char staged_status)
8824 if (status == GOT_STATUS_NONEXISTENT)
8825 return got_error_set_errno(ENOENT, path);
8826 if (status != GOT_STATUS_NO_CHANGE &&
8827 status != GOT_STATUS_ADD &&
8828 status != GOT_STATUS_MODIFY &&
8829 status != GOT_STATUS_MODE_CHANGE)
8830 return got_error_path(path, GOT_ERR_FILE_STATUS);
8831 if (staged_status == GOT_STATUS_DELETE)
8832 return got_error_path(path, GOT_ERR_FILE_STATUS);
8833 return NULL;
8836 static const struct got_error *
8837 patch_can_add(const char *path, unsigned char status)
8839 if (status != GOT_STATUS_NONEXISTENT)
8840 return got_error_path(path, GOT_ERR_FILE_STATUS);
8841 return NULL;
8844 static const struct got_error *
8845 patch_can_edit(const char *path, unsigned char status,
8846 unsigned char staged_status)
8848 if (status == GOT_STATUS_NONEXISTENT)
8849 return got_error_set_errno(ENOENT, path);
8850 if (status != GOT_STATUS_NO_CHANGE &&
8851 status != GOT_STATUS_ADD &&
8852 status != GOT_STATUS_MODIFY)
8853 return got_error_path(path, GOT_ERR_FILE_STATUS);
8854 if (staged_status == GOT_STATUS_DELETE)
8855 return got_error_path(path, GOT_ERR_FILE_STATUS);
8856 return NULL;
8859 const struct got_error *
8860 got_worktree_patch_prepare(struct got_fileindex **fileindex,
8861 struct got_worktree *worktree)
8863 const struct got_error *err;
8864 char *fileindex_path = NULL;
8866 err = open_fileindex(fileindex, &fileindex_path, worktree);
8867 free(fileindex_path);
8868 return err;
8871 const struct got_error *
8872 got_worktree_patch_check_path(const char *old, const char *new,
8873 char **oldpath, char **newpath, struct got_worktree *worktree,
8874 struct got_repository *repo, struct got_fileindex *fileindex)
8876 const struct got_error *err = NULL;
8877 int file_renamed = 0;
8878 unsigned char status_old, staged_status_old;
8879 unsigned char status_new, staged_status_new;
8881 *oldpath = NULL;
8882 *newpath = NULL;
8884 err = patch_check_path(old != NULL ? old : new, oldpath,
8885 &status_old, &staged_status_old, fileindex, worktree, repo);
8886 if (err)
8887 goto done;
8889 err = patch_check_path(new != NULL ? new : old, newpath,
8890 &status_new, &staged_status_new, fileindex, worktree, repo);
8891 if (err)
8892 goto done;
8894 if (old != NULL && new != NULL && strcmp(old, new) != 0)
8895 file_renamed = 1;
8897 if (old != NULL && new == NULL)
8898 err = patch_can_rm(*oldpath, status_old, staged_status_old);
8899 else if (file_renamed) {
8900 err = patch_can_rm(*oldpath, status_old, staged_status_old);
8901 if (err == NULL)
8902 err = patch_can_add(*newpath, status_new);
8903 } else if (old == NULL)
8904 err = patch_can_add(*newpath, status_new);
8905 else
8906 err = patch_can_edit(*newpath, status_new, staged_status_new);
8908 done:
8909 if (err) {
8910 free(*oldpath);
8911 *oldpath = NULL;
8912 free(*newpath);
8913 *newpath = NULL;
8915 return err;
8918 const struct got_error *
8919 got_worktree_patch_complete(struct got_fileindex *fileindex)
8921 got_fileindex_free(fileindex);
8922 return NULL;