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_worktree *worktree, struct got_repository *repo)
2464 const struct got_error *err = NULL;
2465 struct got_object_id *id = NULL;
2466 char *in_repo_path = NULL;
2467 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2469 *entry_type = GOT_OBJ_TYPE_ANY;
2470 *tree_relpath = NULL;
2471 *tree_id = NULL;
2473 if (wt_relpath[0] == '\0') {
2474 /* Check out all files within the work tree. */
2475 *entry_type = GOT_OBJ_TYPE_TREE;
2476 *tree_relpath = strdup("");
2477 if (*tree_relpath == NULL) {
2478 err = got_error_from_errno("strdup");
2479 goto done;
2481 err = got_object_id_by_path(tree_id, repo,
2482 worktree->base_commit_id, worktree->path_prefix);
2483 if (err)
2484 goto done;
2485 return NULL;
2488 /* Check out a subset of files in the work tree. */
2490 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2491 is_root_wt ? "" : "/", wt_relpath) == -1) {
2492 err = got_error_from_errno("asprintf");
2493 goto done;
2496 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2497 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 worktree->base_commit_id, 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 /* Map all specified paths to in-repository trees. */
2648 TAILQ_FOREACH(pe, paths, entry) {
2649 tpd = malloc(sizeof(*tpd));
2650 if (tpd == NULL) {
2651 err = got_error_from_errno("malloc");
2652 goto done;
2655 err = find_tree_entry_for_checkout(&tpd->entry_type,
2656 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2657 if (err) {
2658 free(tpd);
2659 goto done;
2662 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2663 err = got_path_basename(&tpd->entry_name, pe->path);
2664 if (err) {
2665 free(tpd->relpath);
2666 free(tpd->tree_id);
2667 free(tpd);
2668 goto done;
2670 } else
2671 tpd->entry_name = NULL;
2673 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2677 * Read the file index.
2678 * Checking out files is supposed to be an idempotent operation.
2679 * If the on-disk file index is incomplete we will try to complete it.
2681 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2682 if (err)
2683 goto done;
2685 tpd = STAILQ_FIRST(&tree_paths);
2686 TAILQ_FOREACH(pe, paths, entry) {
2687 struct bump_base_commit_id_arg bbc_arg;
2689 err = checkout_files(worktree, fileindex, tpd->relpath,
2690 tpd->tree_id, tpd->entry_name, repo,
2691 progress_cb, progress_arg, cancel_cb, cancel_arg);
2692 if (err)
2693 break;
2695 bbc_arg.base_commit_id = worktree->base_commit_id;
2696 bbc_arg.entry_name = tpd->entry_name;
2697 bbc_arg.path = pe->path;
2698 bbc_arg.path_len = pe->path_len;
2699 bbc_arg.progress_cb = progress_cb;
2700 bbc_arg.progress_arg = progress_arg;
2701 err = got_fileindex_for_each_entry_safe(fileindex,
2702 bump_base_commit_id, &bbc_arg);
2703 if (err)
2704 break;
2706 tpd = STAILQ_NEXT(tpd, entry);
2708 sync_err = sync_fileindex(fileindex, fileindex_path);
2709 if (sync_err && err == NULL)
2710 err = sync_err;
2711 done:
2712 free(fileindex_path);
2713 if (tree)
2714 got_object_tree_close(tree);
2715 if (commit)
2716 got_object_commit_close(commit);
2717 if (fileindex)
2718 got_fileindex_free(fileindex);
2719 while (!STAILQ_EMPTY(&tree_paths)) {
2720 tpd = STAILQ_FIRST(&tree_paths);
2721 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2722 free(tpd->relpath);
2723 free(tpd->tree_id);
2724 free(tpd);
2726 unlockerr = lock_worktree(worktree, LOCK_SH);
2727 if (unlockerr && err == NULL)
2728 err = unlockerr;
2729 return err;
2732 struct merge_file_cb_arg {
2733 struct got_worktree *worktree;
2734 struct got_fileindex *fileindex;
2735 got_worktree_checkout_cb progress_cb;
2736 void *progress_arg;
2737 got_cancel_cb cancel_cb;
2738 void *cancel_arg;
2739 const char *label_orig;
2740 struct got_object_id *commit_id2;
2741 int allow_bad_symlinks;
2744 static const struct got_error *
2745 merge_file_cb(void *arg, struct got_blob_object *blob1,
2746 struct got_blob_object *blob2, struct got_object_id *id1,
2747 struct got_object_id *id2, const char *path1, const char *path2,
2748 mode_t mode1, mode_t mode2, struct got_repository *repo)
2750 static const struct got_error *err = NULL;
2751 struct merge_file_cb_arg *a = arg;
2752 struct got_fileindex_entry *ie;
2753 char *ondisk_path = NULL;
2754 struct stat sb;
2755 unsigned char status;
2756 int local_changes_subsumed;
2757 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2758 char *id_str = NULL, *label_deriv2 = NULL;
2760 if (blob1 && blob2) {
2761 ie = got_fileindex_entry_get(a->fileindex, path2,
2762 strlen(path2));
2763 if (ie == NULL)
2764 return (*a->progress_cb)(a->progress_arg,
2765 GOT_STATUS_MISSING, path2);
2767 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2768 path2) == -1)
2769 return got_error_from_errno("asprintf");
2771 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2772 repo);
2773 if (err)
2774 goto done;
2776 if (status == GOT_STATUS_DELETE) {
2777 err = (*a->progress_cb)(a->progress_arg,
2778 GOT_STATUS_MERGE, path2);
2779 goto done;
2781 if (status != GOT_STATUS_NO_CHANGE &&
2782 status != GOT_STATUS_MODIFY &&
2783 status != GOT_STATUS_CONFLICT &&
2784 status != GOT_STATUS_ADD) {
2785 err = (*a->progress_cb)(a->progress_arg, status, path2);
2786 goto done;
2789 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2790 char *link_target2;
2791 err = got_object_blob_read_to_str(&link_target2, blob2);
2792 if (err)
2793 goto done;
2794 err = merge_symlink(a->worktree, blob1, ondisk_path,
2795 path2, a->label_orig, link_target2, a->commit_id2,
2796 repo, a->progress_cb, a->progress_arg);
2797 free(link_target2);
2798 } else {
2799 int fd;
2801 f_orig = got_opentemp();
2802 if (f_orig == NULL) {
2803 err = got_error_from_errno("got_opentemp");
2804 goto done;
2806 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2807 f_orig, blob1);
2808 if (err)
2809 goto done;
2811 f_deriv2 = got_opentemp();
2812 if (f_deriv2 == NULL)
2813 goto done;
2814 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2815 f_deriv2, blob2);
2816 if (err)
2817 goto done;
2819 fd = open(ondisk_path,
2820 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2821 if (fd == -1) {
2822 err = got_error_from_errno2("open",
2823 ondisk_path);
2824 goto done;
2826 f_deriv = fdopen(fd, "r");
2827 if (f_deriv == NULL) {
2828 err = got_error_from_errno2("fdopen",
2829 ondisk_path);
2830 close(fd);
2831 goto done;
2833 err = got_object_id_str(&id_str, a->commit_id2);
2834 if (err)
2835 goto done;
2836 if (asprintf(&label_deriv2, "%s: commit %s",
2837 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2838 err = got_error_from_errno("asprintf");
2839 goto done;
2841 err = merge_file(&local_changes_subsumed, a->worktree,
2842 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2843 sb.st_mode, a->label_orig, NULL, label_deriv2,
2844 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2845 a->progress_cb, a->progress_arg);
2847 } else if (blob1) {
2848 ie = got_fileindex_entry_get(a->fileindex, path1,
2849 strlen(path1));
2850 if (ie == NULL)
2851 return (*a->progress_cb)(a->progress_arg,
2852 GOT_STATUS_MISSING, path1);
2854 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2855 path1) == -1)
2856 return got_error_from_errno("asprintf");
2858 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2859 repo);
2860 if (err)
2861 goto done;
2863 switch (status) {
2864 case GOT_STATUS_NO_CHANGE:
2865 err = (*a->progress_cb)(a->progress_arg,
2866 GOT_STATUS_DELETE, path1);
2867 if (err)
2868 goto done;
2869 err = remove_ondisk_file(a->worktree->root_path, path1);
2870 if (err)
2871 goto done;
2872 if (ie)
2873 got_fileindex_entry_mark_deleted_from_disk(ie);
2874 break;
2875 case GOT_STATUS_DELETE:
2876 case GOT_STATUS_MISSING:
2877 err = (*a->progress_cb)(a->progress_arg,
2878 GOT_STATUS_DELETE, path1);
2879 if (err)
2880 goto done;
2881 if (ie)
2882 got_fileindex_entry_mark_deleted_from_disk(ie);
2883 break;
2884 case GOT_STATUS_ADD: {
2885 struct got_object_id *id;
2886 FILE *blob1_f;
2887 off_t blob1_size;
2889 * Delete the added file only if its content already
2890 * exists in the repository.
2892 err = got_object_blob_file_create(&id, &blob1_f,
2893 &blob1_size, path1);
2894 if (err)
2895 goto done;
2896 if (got_object_id_cmp(id, id1) == 0) {
2897 err = (*a->progress_cb)(a->progress_arg,
2898 GOT_STATUS_DELETE, path1);
2899 if (err)
2900 goto done;
2901 err = remove_ondisk_file(a->worktree->root_path,
2902 path1);
2903 if (err)
2904 goto done;
2905 if (ie)
2906 got_fileindex_entry_remove(a->fileindex,
2907 ie);
2908 } else {
2909 err = (*a->progress_cb)(a->progress_arg,
2910 GOT_STATUS_CANNOT_DELETE, path1);
2912 if (fclose(blob1_f) == EOF && err == NULL)
2913 err = got_error_from_errno("fclose");
2914 free(id);
2915 if (err)
2916 goto done;
2917 break;
2919 case GOT_STATUS_MODIFY:
2920 case GOT_STATUS_CONFLICT:
2921 err = (*a->progress_cb)(a->progress_arg,
2922 GOT_STATUS_CANNOT_DELETE, path1);
2923 if (err)
2924 goto done;
2925 break;
2926 case GOT_STATUS_OBSTRUCTED:
2927 err = (*a->progress_cb)(a->progress_arg, status, path1);
2928 if (err)
2929 goto done;
2930 break;
2931 default:
2932 break;
2934 } else if (blob2) {
2935 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2936 path2) == -1)
2937 return got_error_from_errno("asprintf");
2938 ie = got_fileindex_entry_get(a->fileindex, path2,
2939 strlen(path2));
2940 if (ie) {
2941 err = get_file_status(&status, &sb, ie, ondisk_path,
2942 -1, NULL, repo);
2943 if (err)
2944 goto done;
2945 if (status != GOT_STATUS_NO_CHANGE &&
2946 status != GOT_STATUS_MODIFY &&
2947 status != GOT_STATUS_CONFLICT &&
2948 status != GOT_STATUS_ADD) {
2949 err = (*a->progress_cb)(a->progress_arg,
2950 status, path2);
2951 goto done;
2953 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2954 char *link_target2;
2955 err = got_object_blob_read_to_str(&link_target2,
2956 blob2);
2957 if (err)
2958 goto done;
2959 err = merge_symlink(a->worktree, NULL,
2960 ondisk_path, path2, a->label_orig,
2961 link_target2, a->commit_id2, repo,
2962 a->progress_cb, a->progress_arg);
2963 free(link_target2);
2964 } else if (S_ISREG(sb.st_mode)) {
2965 err = merge_blob(&local_changes_subsumed,
2966 a->worktree, NULL, ondisk_path, path2,
2967 sb.st_mode, a->label_orig, blob2,
2968 a->commit_id2, repo, a->progress_cb,
2969 a->progress_arg);
2970 } else {
2971 err = got_error_path(ondisk_path,
2972 GOT_ERR_FILE_OBSTRUCTED);
2974 if (err)
2975 goto done;
2976 if (status == GOT_STATUS_DELETE) {
2977 err = got_fileindex_entry_update(ie,
2978 a->worktree->root_fd, path2, blob2->id.sha1,
2979 a->worktree->base_commit_id->sha1, 0);
2980 if (err)
2981 goto done;
2983 } else {
2984 int is_bad_symlink = 0;
2985 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2986 if (S_ISLNK(mode2)) {
2987 err = install_symlink(&is_bad_symlink,
2988 a->worktree, ondisk_path, path2, blob2, 0,
2989 0, 1, a->allow_bad_symlinks, repo,
2990 a->progress_cb, a->progress_arg);
2991 } else {
2992 err = install_blob(a->worktree, ondisk_path, path2,
2993 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2994 a->progress_cb, a->progress_arg);
2996 if (err)
2997 goto done;
2998 err = got_fileindex_entry_alloc(&ie, path2);
2999 if (err)
3000 goto done;
3001 err = got_fileindex_entry_update(ie,
3002 a->worktree->root_fd, path2, NULL, NULL, 1);
3003 if (err) {
3004 got_fileindex_entry_free(ie);
3005 goto done;
3007 err = got_fileindex_entry_add(a->fileindex, ie);
3008 if (err) {
3009 got_fileindex_entry_free(ie);
3010 goto done;
3012 if (is_bad_symlink) {
3013 got_fileindex_entry_filetype_set(ie,
3014 GOT_FILEIDX_MODE_BAD_SYMLINK);
3018 done:
3019 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3020 err = got_error_from_errno("fclose");
3021 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3022 err = got_error_from_errno("fclose");
3023 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3024 err = got_error_from_errno("fclose");
3025 free(id_str);
3026 free(label_deriv2);
3027 free(ondisk_path);
3028 return err;
3031 static const struct got_error *
3032 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3034 struct got_worktree *worktree = arg;
3036 /* Reject merges into a work tree with mixed base commits. */
3037 if (got_fileindex_entry_has_commit(ie) &&
3038 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3039 SHA1_DIGEST_LENGTH) != 0)
3040 return got_error(GOT_ERR_MIXED_COMMITS);
3042 return NULL;
3045 struct check_merge_conflicts_arg {
3046 struct got_worktree *worktree;
3047 struct got_fileindex *fileindex;
3048 struct got_repository *repo;
3051 static const struct got_error *
3052 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3053 struct got_blob_object *blob2, struct got_object_id *id1,
3054 struct got_object_id *id2, const char *path1, const char *path2,
3055 mode_t mode1, mode_t mode2, struct got_repository *repo)
3057 const struct got_error *err = NULL;
3058 struct check_merge_conflicts_arg *a = arg;
3059 unsigned char status;
3060 struct stat sb;
3061 struct got_fileindex_entry *ie;
3062 const char *path = path2 ? path2 : path1;
3063 struct got_object_id *id = id2 ? id2 : id1;
3064 char *ondisk_path;
3066 if (id == NULL)
3067 return NULL;
3069 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3070 if (ie == NULL)
3071 return NULL;
3073 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3074 == -1)
3075 return got_error_from_errno("asprintf");
3077 /* Reject merges into a work tree with conflicted files. */
3078 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3079 free(ondisk_path);
3080 if (err)
3081 return err;
3082 if (status == GOT_STATUS_CONFLICT)
3083 return got_error(GOT_ERR_CONFLICTS);
3085 return NULL;
3088 static const struct got_error *
3089 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3090 const char *fileindex_path, struct got_object_id *commit_id1,
3091 struct got_object_id *commit_id2, struct got_repository *repo,
3092 got_worktree_checkout_cb progress_cb, void *progress_arg,
3093 got_cancel_cb cancel_cb, void *cancel_arg)
3095 const struct got_error *err = NULL, *sync_err;
3096 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3097 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3098 struct check_merge_conflicts_arg cmc_arg;
3099 struct merge_file_cb_arg arg;
3100 char *label_orig = NULL;
3102 if (commit_id1) {
3103 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3104 worktree->path_prefix);
3105 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3106 goto done;
3108 if (tree_id1) {
3109 char *id_str;
3111 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3112 if (err)
3113 goto done;
3115 err = got_object_id_str(&id_str, commit_id1);
3116 if (err)
3117 goto done;
3119 if (asprintf(&label_orig, "%s: commit %s",
3120 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3121 err = got_error_from_errno("asprintf");
3122 free(id_str);
3123 goto done;
3125 free(id_str);
3128 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3129 worktree->path_prefix);
3130 if (err)
3131 goto done;
3133 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3134 if (err)
3135 goto done;
3137 cmc_arg.worktree = worktree;
3138 cmc_arg.fileindex = fileindex;
3139 cmc_arg.repo = repo;
3140 err = got_diff_tree(tree1, tree2, "", "", repo,
3141 check_merge_conflicts, &cmc_arg, 0);
3142 if (err)
3143 goto done;
3145 arg.worktree = worktree;
3146 arg.fileindex = fileindex;
3147 arg.progress_cb = progress_cb;
3148 arg.progress_arg = progress_arg;
3149 arg.cancel_cb = cancel_cb;
3150 arg.cancel_arg = cancel_arg;
3151 arg.label_orig = label_orig;
3152 arg.commit_id2 = commit_id2;
3153 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3154 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3155 sync_err = sync_fileindex(fileindex, fileindex_path);
3156 if (sync_err && err == NULL)
3157 err = sync_err;
3158 done:
3159 if (tree1)
3160 got_object_tree_close(tree1);
3161 if (tree2)
3162 got_object_tree_close(tree2);
3163 free(label_orig);
3164 return err;
3167 const struct got_error *
3168 got_worktree_merge_files(struct got_worktree *worktree,
3169 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3170 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3171 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3173 const struct got_error *err, *unlockerr;
3174 char *fileindex_path = NULL;
3175 struct got_fileindex *fileindex = NULL;
3177 err = lock_worktree(worktree, LOCK_EX);
3178 if (err)
3179 return err;
3181 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3182 if (err)
3183 goto done;
3185 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3186 worktree);
3187 if (err)
3188 goto done;
3190 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3191 commit_id2, repo, progress_cb, progress_arg,
3192 cancel_cb, cancel_arg);
3193 done:
3194 if (fileindex)
3195 got_fileindex_free(fileindex);
3196 free(fileindex_path);
3197 unlockerr = lock_worktree(worktree, LOCK_SH);
3198 if (unlockerr && err == NULL)
3199 err = unlockerr;
3200 return err;
3203 struct diff_dir_cb_arg {
3204 struct got_fileindex *fileindex;
3205 struct got_worktree *worktree;
3206 const char *status_path;
3207 size_t status_path_len;
3208 struct got_repository *repo;
3209 got_worktree_status_cb status_cb;
3210 void *status_arg;
3211 got_cancel_cb cancel_cb;
3212 void *cancel_arg;
3213 /* A pathlist containing per-directory pathlists of ignore patterns. */
3214 struct got_pathlist_head *ignores;
3215 int report_unchanged;
3216 int no_ignores;
3219 static const struct got_error *
3220 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3221 int dirfd, const char *de_name,
3222 got_worktree_status_cb status_cb, void *status_arg,
3223 struct got_repository *repo, int report_unchanged)
3225 const struct got_error *err = NULL;
3226 unsigned char status = GOT_STATUS_NO_CHANGE;
3227 unsigned char staged_status = get_staged_status(ie);
3228 struct stat sb;
3229 struct got_object_id blob_id, commit_id, staged_blob_id;
3230 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3231 struct got_object_id *staged_blob_idp = NULL;
3233 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3234 if (err)
3235 return err;
3237 if (status == GOT_STATUS_NO_CHANGE &&
3238 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3239 return NULL;
3241 if (got_fileindex_entry_has_blob(ie)) {
3242 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3243 blob_idp = &blob_id;
3245 if (got_fileindex_entry_has_commit(ie)) {
3246 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3247 commit_idp = &commit_id;
3249 if (staged_status == GOT_STATUS_ADD ||
3250 staged_status == GOT_STATUS_MODIFY) {
3251 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3252 SHA1_DIGEST_LENGTH);
3253 staged_blob_idp = &staged_blob_id;
3256 return (*status_cb)(status_arg, status, staged_status,
3257 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3260 static const struct got_error *
3261 status_old_new(void *arg, struct got_fileindex_entry *ie,
3262 struct dirent *de, const char *parent_path, int dirfd)
3264 const struct got_error *err = NULL;
3265 struct diff_dir_cb_arg *a = arg;
3266 char *abspath;
3268 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3269 return got_error(GOT_ERR_CANCELLED);
3271 if (got_path_cmp(parent_path, a->status_path,
3272 strlen(parent_path), a->status_path_len) != 0 &&
3273 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3274 return NULL;
3276 if (parent_path[0]) {
3277 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3278 parent_path, de->d_name) == -1)
3279 return got_error_from_errno("asprintf");
3280 } else {
3281 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3282 de->d_name) == -1)
3283 return got_error_from_errno("asprintf");
3286 err = report_file_status(ie, abspath, dirfd, de->d_name,
3287 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3288 free(abspath);
3289 return err;
3292 static const struct got_error *
3293 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3295 struct diff_dir_cb_arg *a = arg;
3296 struct got_object_id blob_id, commit_id;
3297 unsigned char status;
3299 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3300 return got_error(GOT_ERR_CANCELLED);
3302 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3303 return NULL;
3305 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3306 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3307 if (got_fileindex_entry_has_file_on_disk(ie))
3308 status = GOT_STATUS_MISSING;
3309 else
3310 status = GOT_STATUS_DELETE;
3311 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3312 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3315 void
3316 free_ignorelist(struct got_pathlist_head *ignorelist)
3318 struct got_pathlist_entry *pe;
3320 TAILQ_FOREACH(pe, ignorelist, entry)
3321 free((char *)pe->path);
3322 got_pathlist_free(ignorelist);
3325 void
3326 free_ignores(struct got_pathlist_head *ignores)
3328 struct got_pathlist_entry *pe;
3330 TAILQ_FOREACH(pe, ignores, entry) {
3331 struct got_pathlist_head *ignorelist = pe->data;
3332 free_ignorelist(ignorelist);
3333 free((char *)pe->path);
3335 got_pathlist_free(ignores);
3338 static const struct got_error *
3339 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3341 const struct got_error *err = NULL;
3342 struct got_pathlist_entry *pe = NULL;
3343 struct got_pathlist_head *ignorelist;
3344 char *line = NULL, *pattern, *dirpath = NULL;
3345 size_t linesize = 0;
3346 ssize_t linelen;
3348 ignorelist = calloc(1, sizeof(*ignorelist));
3349 if (ignorelist == NULL)
3350 return got_error_from_errno("calloc");
3351 TAILQ_INIT(ignorelist);
3353 while ((linelen = getline(&line, &linesize, f)) != -1) {
3354 if (linelen > 0 && line[linelen - 1] == '\n')
3355 line[linelen - 1] = '\0';
3357 /* Git's ignores may contain comments. */
3358 if (line[0] == '#')
3359 continue;
3361 /* Git's negated patterns are not (yet?) supported. */
3362 if (line[0] == '!')
3363 continue;
3365 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3366 line) == -1) {
3367 err = got_error_from_errno("asprintf");
3368 goto done;
3370 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3371 if (err)
3372 goto done;
3374 if (ferror(f)) {
3375 err = got_error_from_errno("getline");
3376 goto done;
3379 dirpath = strdup(path);
3380 if (dirpath == NULL) {
3381 err = got_error_from_errno("strdup");
3382 goto done;
3384 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3385 done:
3386 free(line);
3387 if (err || pe == NULL) {
3388 free(dirpath);
3389 free_ignorelist(ignorelist);
3391 return err;
3394 int
3395 match_ignores(struct got_pathlist_head *ignores, const char *path)
3397 struct got_pathlist_entry *pe;
3399 /* Handle patterns which match in all directories. */
3400 TAILQ_FOREACH(pe, ignores, entry) {
3401 struct got_pathlist_head *ignorelist = pe->data;
3402 struct got_pathlist_entry *pi;
3404 TAILQ_FOREACH(pi, ignorelist, entry) {
3405 const char *p, *pattern = pi->path;
3407 if (strncmp(pattern, "**/", 3) != 0)
3408 continue;
3409 pattern += 3;
3410 p = path;
3411 while (*p) {
3412 if (fnmatch(pattern, p,
3413 FNM_PATHNAME | FNM_LEADING_DIR)) {
3414 /* Retry in next directory. */
3415 while (*p && *p != '/')
3416 p++;
3417 while (*p == '/')
3418 p++;
3419 continue;
3421 return 1;
3427 * The ignores pathlist contains ignore lists from children before
3428 * parents, so we can find the most specific ignorelist by walking
3429 * ignores backwards.
3431 pe = TAILQ_LAST(ignores, got_pathlist_head);
3432 while (pe) {
3433 if (got_path_is_child(path, pe->path, pe->path_len)) {
3434 struct got_pathlist_head *ignorelist = pe->data;
3435 struct got_pathlist_entry *pi;
3436 TAILQ_FOREACH(pi, ignorelist, entry) {
3437 const char *pattern = pi->path;
3438 int flags = FNM_LEADING_DIR;
3439 if (strstr(pattern, "/**/") == NULL)
3440 flags |= FNM_PATHNAME;
3441 if (fnmatch(pattern, path, flags))
3442 continue;
3443 return 1;
3446 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3449 return 0;
3452 static const struct got_error *
3453 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3454 const char *path, int dirfd, const char *ignores_filename)
3456 const struct got_error *err = NULL;
3457 char *ignorespath;
3458 int fd = -1;
3459 FILE *ignoresfile = NULL;
3461 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3462 path[0] ? "/" : "", ignores_filename) == -1)
3463 return got_error_from_errno("asprintf");
3465 if (dirfd != -1) {
3466 fd = openat(dirfd, ignores_filename,
3467 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3468 if (fd == -1) {
3469 if (errno != ENOENT && errno != EACCES)
3470 err = got_error_from_errno2("openat",
3471 ignorespath);
3472 } else {
3473 ignoresfile = fdopen(fd, "r");
3474 if (ignoresfile == NULL)
3475 err = got_error_from_errno2("fdopen",
3476 ignorespath);
3477 else {
3478 fd = -1;
3479 err = read_ignores(ignores, path, ignoresfile);
3482 } else {
3483 ignoresfile = fopen(ignorespath, "re");
3484 if (ignoresfile == NULL) {
3485 if (errno != ENOENT && errno != EACCES)
3486 err = got_error_from_errno2("fopen",
3487 ignorespath);
3488 } else
3489 err = read_ignores(ignores, path, ignoresfile);
3492 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3493 err = got_error_from_errno2("fclose", path);
3494 if (fd != -1 && close(fd) == -1 && err == NULL)
3495 err = got_error_from_errno2("close", path);
3496 free(ignorespath);
3497 return err;
3500 static const struct got_error *
3501 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3502 int dirfd)
3504 const struct got_error *err = NULL;
3505 struct diff_dir_cb_arg *a = arg;
3506 char *path = NULL;
3508 if (ignore != NULL)
3509 *ignore = 0;
3511 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3512 return got_error(GOT_ERR_CANCELLED);
3514 if (parent_path[0]) {
3515 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3516 return got_error_from_errno("asprintf");
3517 } else {
3518 path = de->d_name;
3521 if (de->d_type == DT_DIR) {
3522 if (!a->no_ignores && ignore != NULL &&
3523 match_ignores(a->ignores, path))
3524 *ignore = 1;
3525 } else if (!match_ignores(a->ignores, path) &&
3526 got_path_is_child(path, a->status_path, a->status_path_len))
3527 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3528 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3529 if (parent_path[0])
3530 free(path);
3531 return err;
3534 static const struct got_error *
3535 status_traverse(void *arg, const char *path, int dirfd)
3537 const struct got_error *err = NULL;
3538 struct diff_dir_cb_arg *a = arg;
3540 if (a->no_ignores)
3541 return NULL;
3543 err = add_ignores(a->ignores, a->worktree->root_path,
3544 path, dirfd, ".cvsignore");
3545 if (err)
3546 return err;
3548 err = add_ignores(a->ignores, a->worktree->root_path, path,
3549 dirfd, ".gitignore");
3551 return err;
3554 static const struct got_error *
3555 report_single_file_status(const char *path, const char *ondisk_path,
3556 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3557 void *status_arg, struct got_repository *repo, int report_unchanged,
3558 struct got_pathlist_head *ignores, int no_ignores)
3560 struct got_fileindex_entry *ie;
3561 struct stat sb;
3563 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3564 if (ie)
3565 return report_file_status(ie, ondisk_path, -1, NULL,
3566 status_cb, status_arg, repo, report_unchanged);
3568 if (lstat(ondisk_path, &sb) == -1) {
3569 if (errno != ENOENT)
3570 return got_error_from_errno2("lstat", ondisk_path);
3571 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3572 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3575 if (!no_ignores && match_ignores(ignores, path))
3576 return NULL;
3578 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3579 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3580 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3582 return NULL;
3585 static const struct got_error *
3586 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3587 const char *root_path, const char *path)
3589 const struct got_error *err;
3590 char *parent_path, *next_parent_path = NULL;
3592 err = add_ignores(ignores, root_path, "", -1,
3593 ".cvsignore");
3594 if (err)
3595 return err;
3597 err = add_ignores(ignores, root_path, "", -1,
3598 ".gitignore");
3599 if (err)
3600 return err;
3602 err = got_path_dirname(&parent_path, path);
3603 if (err) {
3604 if (err->code == GOT_ERR_BAD_PATH)
3605 return NULL; /* cannot traverse parent */
3606 return err;
3608 for (;;) {
3609 err = add_ignores(ignores, root_path, parent_path, -1,
3610 ".cvsignore");
3611 if (err)
3612 break;
3613 err = add_ignores(ignores, root_path, parent_path, -1,
3614 ".gitignore");
3615 if (err)
3616 break;
3617 err = got_path_dirname(&next_parent_path, parent_path);
3618 if (err) {
3619 if (err->code == GOT_ERR_BAD_PATH)
3620 err = NULL; /* traversed everything */
3621 break;
3623 if (got_path_is_root_dir(parent_path))
3624 break;
3625 free(parent_path);
3626 parent_path = next_parent_path;
3627 next_parent_path = NULL;
3630 free(parent_path);
3631 free(next_parent_path);
3632 return err;
3635 static const struct got_error *
3636 worktree_status(struct got_worktree *worktree, const char *path,
3637 struct got_fileindex *fileindex, struct got_repository *repo,
3638 got_worktree_status_cb status_cb, void *status_arg,
3639 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3640 int report_unchanged)
3642 const struct got_error *err = NULL;
3643 int fd = -1;
3644 struct got_fileindex_diff_dir_cb fdiff_cb;
3645 struct diff_dir_cb_arg arg;
3646 char *ondisk_path = NULL;
3647 struct got_pathlist_head ignores;
3648 struct got_fileindex_entry *ie;
3650 TAILQ_INIT(&ignores);
3652 if (asprintf(&ondisk_path, "%s%s%s",
3653 worktree->root_path, path[0] ? "/" : "", path) == -1)
3654 return got_error_from_errno("asprintf");
3656 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3657 if (ie) {
3658 err = report_single_file_status(path, ondisk_path,
3659 fileindex, status_cb, status_arg, repo,
3660 report_unchanged, &ignores, no_ignores);
3661 goto done;
3664 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3665 if (fd == -1) {
3666 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3667 !got_err_open_nofollow_on_symlink())
3668 err = got_error_from_errno2("open", ondisk_path);
3669 else {
3670 if (!no_ignores) {
3671 err = add_ignores_from_parent_paths(&ignores,
3672 worktree->root_path, ondisk_path);
3673 if (err)
3674 goto done;
3676 err = report_single_file_status(path, ondisk_path,
3677 fileindex, status_cb, status_arg, repo,
3678 report_unchanged, &ignores, no_ignores);
3680 } else {
3681 fdiff_cb.diff_old_new = status_old_new;
3682 fdiff_cb.diff_old = status_old;
3683 fdiff_cb.diff_new = status_new;
3684 fdiff_cb.diff_traverse = status_traverse;
3685 arg.fileindex = fileindex;
3686 arg.worktree = worktree;
3687 arg.status_path = path;
3688 arg.status_path_len = strlen(path);
3689 arg.repo = repo;
3690 arg.status_cb = status_cb;
3691 arg.status_arg = status_arg;
3692 arg.cancel_cb = cancel_cb;
3693 arg.cancel_arg = cancel_arg;
3694 arg.report_unchanged = report_unchanged;
3695 arg.no_ignores = no_ignores;
3696 if (!no_ignores) {
3697 err = add_ignores_from_parent_paths(&ignores,
3698 worktree->root_path, path);
3699 if (err)
3700 goto done;
3702 arg.ignores = &ignores;
3703 err = got_fileindex_diff_dir(fileindex, fd,
3704 worktree->root_path, path, repo, &fdiff_cb, &arg);
3706 done:
3707 free_ignores(&ignores);
3708 if (fd != -1 && close(fd) == -1 && err == NULL)
3709 err = got_error_from_errno("close");
3710 free(ondisk_path);
3711 return err;
3714 const struct got_error *
3715 got_worktree_status(struct got_worktree *worktree,
3716 struct got_pathlist_head *paths, struct got_repository *repo,
3717 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3718 got_cancel_cb cancel_cb, void *cancel_arg)
3720 const struct got_error *err = NULL;
3721 char *fileindex_path = NULL;
3722 struct got_fileindex *fileindex = NULL;
3723 struct got_pathlist_entry *pe;
3725 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3726 if (err)
3727 return err;
3729 TAILQ_FOREACH(pe, paths, entry) {
3730 err = worktree_status(worktree, pe->path, fileindex, repo,
3731 status_cb, status_arg, cancel_cb, cancel_arg,
3732 no_ignores, 0);
3733 if (err)
3734 break;
3736 free(fileindex_path);
3737 got_fileindex_free(fileindex);
3738 return err;
3741 const struct got_error *
3742 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3743 const char *arg)
3745 const struct got_error *err = NULL;
3746 char *resolved = NULL, *cwd = NULL, *path = NULL;
3747 size_t len;
3748 struct stat sb;
3749 char *abspath = NULL;
3750 char canonpath[PATH_MAX];
3752 *wt_path = NULL;
3754 cwd = getcwd(NULL, 0);
3755 if (cwd == NULL)
3756 return got_error_from_errno("getcwd");
3758 if (lstat(arg, &sb) == -1) {
3759 if (errno != ENOENT) {
3760 err = got_error_from_errno2("lstat", arg);
3761 goto done;
3763 sb.st_mode = 0;
3765 if (S_ISLNK(sb.st_mode)) {
3767 * We cannot use realpath(3) with symlinks since we want to
3768 * operate on the symlink itself.
3769 * But we can make the path absolute, assuming it is relative
3770 * to the current working directory, and then canonicalize it.
3772 if (!got_path_is_absolute(arg)) {
3773 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3774 err = got_error_from_errno("asprintf");
3775 goto done;
3779 err = got_canonpath(abspath ? abspath : arg, canonpath,
3780 sizeof(canonpath));
3781 if (err)
3782 goto done;
3783 resolved = strdup(canonpath);
3784 if (resolved == NULL) {
3785 err = got_error_from_errno("strdup");
3786 goto done;
3788 } else {
3789 resolved = realpath(arg, NULL);
3790 if (resolved == NULL) {
3791 if (errno != ENOENT) {
3792 err = got_error_from_errno2("realpath", arg);
3793 goto done;
3795 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3796 err = got_error_from_errno("asprintf");
3797 goto done;
3799 err = got_canonpath(abspath, canonpath,
3800 sizeof(canonpath));
3801 if (err)
3802 goto done;
3803 resolved = strdup(canonpath);
3804 if (resolved == NULL) {
3805 err = got_error_from_errno("strdup");
3806 goto done;
3811 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3812 strlen(got_worktree_get_root_path(worktree)))) {
3813 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3814 goto done;
3817 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3818 err = got_path_skip_common_ancestor(&path,
3819 got_worktree_get_root_path(worktree), resolved);
3820 if (err)
3821 goto done;
3822 } else {
3823 path = strdup("");
3824 if (path == NULL) {
3825 err = got_error_from_errno("strdup");
3826 goto done;
3830 /* XXX status walk can't deal with trailing slash! */
3831 len = strlen(path);
3832 while (len > 0 && path[len - 1] == '/') {
3833 path[len - 1] = '\0';
3834 len--;
3836 done:
3837 free(abspath);
3838 free(resolved);
3839 free(cwd);
3840 if (err == NULL)
3841 *wt_path = path;
3842 else
3843 free(path);
3844 return err;
3847 struct schedule_addition_args {
3848 struct got_worktree *worktree;
3849 struct got_fileindex *fileindex;
3850 got_worktree_checkout_cb progress_cb;
3851 void *progress_arg;
3852 struct got_repository *repo;
3855 static const struct got_error *
3856 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3857 const char *relpath, struct got_object_id *blob_id,
3858 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3859 int dirfd, const char *de_name)
3861 struct schedule_addition_args *a = arg;
3862 const struct got_error *err = NULL;
3863 struct got_fileindex_entry *ie;
3864 struct stat sb;
3865 char *ondisk_path;
3867 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3868 relpath) == -1)
3869 return got_error_from_errno("asprintf");
3871 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3872 if (ie) {
3873 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3874 de_name, a->repo);
3875 if (err)
3876 goto done;
3877 /* Re-adding an existing entry is a no-op. */
3878 if (status == GOT_STATUS_ADD)
3879 goto done;
3880 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3881 if (err)
3882 goto done;
3885 if (status != GOT_STATUS_UNVERSIONED) {
3886 if (status == GOT_STATUS_NONEXISTENT)
3887 err = got_error_set_errno(ENOENT, ondisk_path);
3888 else
3889 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3890 goto done;
3893 err = got_fileindex_entry_alloc(&ie, relpath);
3894 if (err)
3895 goto done;
3896 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3897 relpath, NULL, NULL, 1);
3898 if (err) {
3899 got_fileindex_entry_free(ie);
3900 goto done;
3902 err = got_fileindex_entry_add(a->fileindex, ie);
3903 if (err) {
3904 got_fileindex_entry_free(ie);
3905 goto done;
3907 done:
3908 free(ondisk_path);
3909 if (err)
3910 return err;
3911 if (status == GOT_STATUS_ADD)
3912 return NULL;
3913 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3916 const struct got_error *
3917 got_worktree_schedule_add(struct got_worktree *worktree,
3918 struct got_pathlist_head *paths,
3919 got_worktree_checkout_cb progress_cb, void *progress_arg,
3920 struct got_repository *repo, int no_ignores)
3922 struct got_fileindex *fileindex = NULL;
3923 char *fileindex_path = NULL;
3924 const struct got_error *err = NULL, *sync_err, *unlockerr;
3925 struct got_pathlist_entry *pe;
3926 struct schedule_addition_args saa;
3928 err = lock_worktree(worktree, LOCK_EX);
3929 if (err)
3930 return err;
3932 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3933 if (err)
3934 goto done;
3936 saa.worktree = worktree;
3937 saa.fileindex = fileindex;
3938 saa.progress_cb = progress_cb;
3939 saa.progress_arg = progress_arg;
3940 saa.repo = repo;
3942 TAILQ_FOREACH(pe, paths, entry) {
3943 err = worktree_status(worktree, pe->path, fileindex, repo,
3944 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3945 if (err)
3946 break;
3948 sync_err = sync_fileindex(fileindex, fileindex_path);
3949 if (sync_err && err == NULL)
3950 err = sync_err;
3951 done:
3952 free(fileindex_path);
3953 if (fileindex)
3954 got_fileindex_free(fileindex);
3955 unlockerr = lock_worktree(worktree, LOCK_SH);
3956 if (unlockerr && err == NULL)
3957 err = unlockerr;
3958 return err;
3961 struct schedule_deletion_args {
3962 struct got_worktree *worktree;
3963 struct got_fileindex *fileindex;
3964 got_worktree_delete_cb progress_cb;
3965 void *progress_arg;
3966 struct got_repository *repo;
3967 int delete_local_mods;
3968 int keep_on_disk;
3969 int ignore_missing_paths;
3970 const char *status_codes;
3973 static const struct got_error *
3974 schedule_for_deletion(void *arg, unsigned char status,
3975 unsigned char staged_status, const char *relpath,
3976 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3977 struct got_object_id *commit_id, int dirfd, const char *de_name)
3979 struct schedule_deletion_args *a = arg;
3980 const struct got_error *err = NULL;
3981 struct got_fileindex_entry *ie = NULL;
3982 struct stat sb;
3983 char *ondisk_path;
3985 if (status == GOT_STATUS_NONEXISTENT) {
3986 if (a->ignore_missing_paths)
3987 return NULL;
3988 return got_error_set_errno(ENOENT, relpath);
3991 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3992 if (ie == NULL)
3993 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
3995 staged_status = get_staged_status(ie);
3996 if (staged_status != GOT_STATUS_NO_CHANGE) {
3997 if (staged_status == GOT_STATUS_DELETE)
3998 return NULL;
3999 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4002 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4003 relpath) == -1)
4004 return got_error_from_errno("asprintf");
4006 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4007 a->repo);
4008 if (err)
4009 goto done;
4011 if (a->status_codes) {
4012 size_t ncodes = strlen(a->status_codes);
4013 int i;
4014 for (i = 0; i < ncodes ; i++) {
4015 if (status == a->status_codes[i])
4016 break;
4018 if (i == ncodes) {
4019 /* Do not delete files in non-matching status. */
4020 free(ondisk_path);
4021 return NULL;
4023 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4024 a->status_codes[i] != GOT_STATUS_MISSING) {
4025 static char msg[64];
4026 snprintf(msg, sizeof(msg),
4027 "invalid status code '%c'", a->status_codes[i]);
4028 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4029 goto done;
4033 if (status != GOT_STATUS_NO_CHANGE) {
4034 if (status == GOT_STATUS_DELETE)
4035 goto done;
4036 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4037 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4038 goto done;
4040 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4041 err = got_error_set_errno(ENOENT, relpath);
4042 goto done;
4044 if (status != GOT_STATUS_MODIFY &&
4045 status != GOT_STATUS_MISSING) {
4046 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4047 goto done;
4051 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4052 size_t root_len;
4054 if (dirfd != -1) {
4055 if (unlinkat(dirfd, de_name, 0) != 0) {
4056 err = got_error_from_errno2("unlinkat",
4057 ondisk_path);
4058 goto done;
4060 } else if (unlink(ondisk_path) != 0) {
4061 err = got_error_from_errno2("unlink", ondisk_path);
4062 goto done;
4065 root_len = strlen(a->worktree->root_path);
4066 do {
4067 char *parent;
4068 err = got_path_dirname(&parent, ondisk_path);
4069 if (err)
4070 goto done;
4071 free(ondisk_path);
4072 ondisk_path = parent;
4073 if (rmdir(ondisk_path) == -1) {
4074 if (errno != ENOTEMPTY)
4075 err = got_error_from_errno2("rmdir",
4076 ondisk_path);
4077 break;
4079 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4080 strlen(ondisk_path), root_len) != 0);
4083 got_fileindex_entry_mark_deleted_from_disk(ie);
4084 done:
4085 free(ondisk_path);
4086 if (err)
4087 return err;
4088 if (status == GOT_STATUS_DELETE)
4089 return NULL;
4090 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4091 staged_status, relpath);
4094 const struct got_error *
4095 got_worktree_schedule_delete(struct got_worktree *worktree,
4096 struct got_pathlist_head *paths, int delete_local_mods,
4097 const char *status_codes,
4098 got_worktree_delete_cb progress_cb, void *progress_arg,
4099 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4101 struct got_fileindex *fileindex = NULL;
4102 char *fileindex_path = NULL;
4103 const struct got_error *err = NULL, *sync_err, *unlockerr;
4104 struct got_pathlist_entry *pe;
4105 struct schedule_deletion_args sda;
4107 err = lock_worktree(worktree, LOCK_EX);
4108 if (err)
4109 return err;
4111 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4112 if (err)
4113 goto done;
4115 sda.worktree = worktree;
4116 sda.fileindex = fileindex;
4117 sda.progress_cb = progress_cb;
4118 sda.progress_arg = progress_arg;
4119 sda.repo = repo;
4120 sda.delete_local_mods = delete_local_mods;
4121 sda.keep_on_disk = keep_on_disk;
4122 sda.ignore_missing_paths = ignore_missing_paths;
4123 sda.status_codes = status_codes;
4125 TAILQ_FOREACH(pe, paths, entry) {
4126 err = worktree_status(worktree, pe->path, fileindex, repo,
4127 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4128 if (err)
4129 break;
4131 sync_err = sync_fileindex(fileindex, fileindex_path);
4132 if (sync_err && err == NULL)
4133 err = sync_err;
4134 done:
4135 free(fileindex_path);
4136 if (fileindex)
4137 got_fileindex_free(fileindex);
4138 unlockerr = lock_worktree(worktree, LOCK_SH);
4139 if (unlockerr && err == NULL)
4140 err = unlockerr;
4141 return err;
4144 static const struct got_error *
4145 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4147 const struct got_error *err = NULL;
4148 char *line = NULL;
4149 size_t linesize = 0, n;
4150 ssize_t linelen;
4152 linelen = getline(&line, &linesize, infile);
4153 if (linelen == -1) {
4154 if (ferror(infile)) {
4155 err = got_error_from_errno("getline");
4156 goto done;
4158 return NULL;
4160 if (outfile) {
4161 n = fwrite(line, 1, linelen, outfile);
4162 if (n != linelen) {
4163 err = got_ferror(outfile, GOT_ERR_IO);
4164 goto done;
4167 if (rejectfile) {
4168 n = fwrite(line, 1, linelen, rejectfile);
4169 if (n != linelen)
4170 err = got_ferror(outfile, GOT_ERR_IO);
4172 done:
4173 free(line);
4174 return err;
4177 static const struct got_error *
4178 skip_one_line(FILE *f)
4180 char *line = NULL;
4181 size_t linesize = 0;
4182 ssize_t linelen;
4184 linelen = getline(&line, &linesize, f);
4185 if (linelen == -1) {
4186 if (ferror(f))
4187 return got_error_from_errno("getline");
4188 return NULL;
4190 free(line);
4191 return NULL;
4194 static const struct got_error *
4195 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4196 int start_old, int end_old, int start_new, int end_new,
4197 FILE *outfile, FILE *rejectfile)
4199 const struct got_error *err;
4201 /* Copy old file's lines leading up to patch. */
4202 while (!feof(f1) && *line_cur1 < start_old) {
4203 err = copy_one_line(f1, outfile, NULL);
4204 if (err)
4205 return err;
4206 (*line_cur1)++;
4208 /* Skip new file's lines leading up to patch. */
4209 while (!feof(f2) && *line_cur2 < start_new) {
4210 if (rejectfile)
4211 err = copy_one_line(f2, NULL, rejectfile);
4212 else
4213 err = skip_one_line(f2);
4214 if (err)
4215 return err;
4216 (*line_cur2)++;
4218 /* Copy patched lines. */
4219 while (!feof(f2) && *line_cur2 <= end_new) {
4220 err = copy_one_line(f2, outfile, NULL);
4221 if (err)
4222 return err;
4223 (*line_cur2)++;
4225 /* Skip over old file's replaced lines. */
4226 while (!feof(f1) && *line_cur1 <= end_old) {
4227 if (rejectfile)
4228 err = copy_one_line(f1, NULL, rejectfile);
4229 else
4230 err = skip_one_line(f1);
4231 if (err)
4232 return err;
4233 (*line_cur1)++;
4236 return NULL;
4239 static const struct got_error *
4240 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4241 FILE *outfile, FILE *rejectfile)
4243 const struct got_error *err;
4245 if (outfile) {
4246 /* Copy old file's lines until EOF. */
4247 while (!feof(f1)) {
4248 err = copy_one_line(f1, outfile, NULL);
4249 if (err)
4250 return err;
4251 (*line_cur1)++;
4254 if (rejectfile) {
4255 /* Copy new file's lines until EOF. */
4256 while (!feof(f2)) {
4257 err = copy_one_line(f2, NULL, rejectfile);
4258 if (err)
4259 return err;
4260 (*line_cur2)++;
4264 return NULL;
4267 static const struct got_error *
4268 apply_or_reject_change(int *choice, int *nchunks_used,
4269 struct diff_result *diff_result, int n,
4270 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4271 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4272 got_worktree_patch_cb patch_cb, void *patch_arg)
4274 const struct got_error *err = NULL;
4275 struct diff_chunk_context cc = {};
4276 int start_old, end_old, start_new, end_new;
4277 FILE *hunkfile;
4278 struct diff_output_unidiff_state *diff_state;
4279 struct diff_input_info diff_info;
4280 int rc;
4282 *choice = GOT_PATCH_CHOICE_NONE;
4284 /* Get changed line numbers without context lines for copy_change(). */
4285 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4286 start_old = cc.left.start;
4287 end_old = cc.left.end;
4288 start_new = cc.right.start;
4289 end_new = cc.right.end;
4291 /* Get the same change with context lines for display. */
4292 memset(&cc, 0, sizeof(cc));
4293 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4295 memset(&diff_info, 0, sizeof(diff_info));
4296 diff_info.left_path = relpath;
4297 diff_info.right_path = relpath;
4299 diff_state = diff_output_unidiff_state_alloc();
4300 if (diff_state == NULL)
4301 return got_error_set_errno(ENOMEM,
4302 "diff_output_unidiff_state_alloc");
4304 hunkfile = got_opentemp();
4305 if (hunkfile == NULL) {
4306 err = got_error_from_errno("got_opentemp");
4307 goto done;
4310 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4311 diff_result, &cc);
4312 if (rc != DIFF_RC_OK) {
4313 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4314 goto done;
4317 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4318 err = got_ferror(hunkfile, GOT_ERR_IO);
4319 goto done;
4322 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4323 hunkfile, changeno, nchanges);
4324 if (err)
4325 goto done;
4327 switch (*choice) {
4328 case GOT_PATCH_CHOICE_YES:
4329 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4330 end_old, start_new, end_new, outfile, rejectfile);
4331 break;
4332 case GOT_PATCH_CHOICE_NO:
4333 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4334 end_old, start_new, end_new, rejectfile, outfile);
4335 break;
4336 case GOT_PATCH_CHOICE_QUIT:
4337 break;
4338 default:
4339 err = got_error(GOT_ERR_PATCH_CHOICE);
4340 break;
4342 done:
4343 diff_output_unidiff_state_free(diff_state);
4344 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4345 err = got_error_from_errno("fclose");
4346 return err;
4349 struct revert_file_args {
4350 struct got_worktree *worktree;
4351 struct got_fileindex *fileindex;
4352 got_worktree_checkout_cb progress_cb;
4353 void *progress_arg;
4354 got_worktree_patch_cb patch_cb;
4355 void *patch_arg;
4356 struct got_repository *repo;
4357 int unlink_added_files;
4360 static const struct got_error *
4361 create_patched_content(char **path_outfile, int reverse_patch,
4362 struct got_object_id *blob_id, const char *path2,
4363 int dirfd2, const char *de_name2,
4364 const char *relpath, struct got_repository *repo,
4365 got_worktree_patch_cb patch_cb, void *patch_arg)
4367 const struct got_error *err, *free_err;
4368 struct got_blob_object *blob = NULL;
4369 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4370 int fd2 = -1;
4371 char link_target[PATH_MAX];
4372 ssize_t link_len = 0;
4373 char *path1 = NULL, *id_str = NULL;
4374 struct stat sb2;
4375 struct got_diffreg_result *diffreg_result = NULL;
4376 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4377 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4379 *path_outfile = NULL;
4381 err = got_object_id_str(&id_str, blob_id);
4382 if (err)
4383 return err;
4385 if (dirfd2 != -1) {
4386 fd2 = openat(dirfd2, de_name2,
4387 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4388 if (fd2 == -1) {
4389 if (!got_err_open_nofollow_on_symlink()) {
4390 err = got_error_from_errno2("openat", path2);
4391 goto done;
4393 link_len = readlinkat(dirfd2, de_name2,
4394 link_target, sizeof(link_target));
4395 if (link_len == -1) {
4396 return got_error_from_errno2("readlinkat",
4397 path2);
4399 sb2.st_mode = S_IFLNK;
4400 sb2.st_size = link_len;
4402 } else {
4403 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4404 if (fd2 == -1) {
4405 if (!got_err_open_nofollow_on_symlink()) {
4406 err = got_error_from_errno2("open", path2);
4407 goto done;
4409 link_len = readlink(path2, link_target,
4410 sizeof(link_target));
4411 if (link_len == -1)
4412 return got_error_from_errno2("readlink", path2);
4413 sb2.st_mode = S_IFLNK;
4414 sb2.st_size = link_len;
4417 if (fd2 != -1) {
4418 if (fstat(fd2, &sb2) == -1) {
4419 err = got_error_from_errno2("fstat", path2);
4420 goto done;
4423 f2 = fdopen(fd2, "r");
4424 if (f2 == NULL) {
4425 err = got_error_from_errno2("fdopen", path2);
4426 goto done;
4428 fd2 = -1;
4429 } else {
4430 size_t n;
4431 f2 = got_opentemp();
4432 if (f2 == NULL) {
4433 err = got_error_from_errno2("got_opentemp", path2);
4434 goto done;
4436 n = fwrite(link_target, 1, link_len, f2);
4437 if (n != link_len) {
4438 err = got_ferror(f2, GOT_ERR_IO);
4439 goto done;
4441 if (fflush(f2) == EOF) {
4442 err = got_error_from_errno("fflush");
4443 goto done;
4445 rewind(f2);
4448 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4449 if (err)
4450 goto done;
4452 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4453 if (err)
4454 goto done;
4456 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4457 if (err)
4458 goto done;
4460 err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4461 NULL);
4462 if (err)
4463 goto done;
4465 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4466 if (err)
4467 goto done;
4469 if (fseek(f1, 0L, SEEK_SET) == -1)
4470 return got_ferror(f1, GOT_ERR_IO);
4471 if (fseek(f2, 0L, SEEK_SET) == -1)
4472 return got_ferror(f2, GOT_ERR_IO);
4474 /* Count the number of actual changes in the diff result. */
4475 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4476 struct diff_chunk_context cc = {};
4477 diff_chunk_context_load_change(&cc, &nchunks_used,
4478 diffreg_result->result, n, 0);
4479 nchanges++;
4481 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4482 int choice;
4483 err = apply_or_reject_change(&choice, &nchunks_used,
4484 diffreg_result->result, n, relpath, f1, f2,
4485 &line_cur1, &line_cur2,
4486 reverse_patch ? NULL : outfile,
4487 reverse_patch ? outfile : NULL,
4488 ++i, nchanges, patch_cb, patch_arg);
4489 if (err)
4490 goto done;
4491 if (choice == GOT_PATCH_CHOICE_YES)
4492 have_content = 1;
4493 else if (choice == GOT_PATCH_CHOICE_QUIT)
4494 break;
4496 if (have_content) {
4497 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4498 reverse_patch ? NULL : outfile,
4499 reverse_patch ? outfile : NULL);
4500 if (err)
4501 goto done;
4503 if (!S_ISLNK(sb2.st_mode)) {
4504 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4505 err = got_error_from_errno2("fchmod", path2);
4506 goto done;
4510 done:
4511 free(id_str);
4512 if (blob)
4513 got_object_blob_close(blob);
4514 free_err = got_diffreg_result_free(diffreg_result);
4515 if (err == NULL)
4516 err = free_err;
4517 if (f1 && fclose(f1) == EOF && err == NULL)
4518 err = got_error_from_errno2("fclose", path1);
4519 if (f2 && fclose(f2) == EOF && err == NULL)
4520 err = got_error_from_errno2("fclose", path2);
4521 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4522 err = got_error_from_errno2("close", path2);
4523 if (outfile && fclose(outfile) == EOF && err == NULL)
4524 err = got_error_from_errno2("fclose", *path_outfile);
4525 if (path1 && unlink(path1) == -1 && err == NULL)
4526 err = got_error_from_errno2("unlink", path1);
4527 if (err || !have_content) {
4528 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4529 err = got_error_from_errno2("unlink", *path_outfile);
4530 free(*path_outfile);
4531 *path_outfile = NULL;
4533 free(path1);
4534 return err;
4537 static const struct got_error *
4538 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4539 const char *relpath, struct got_object_id *blob_id,
4540 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4541 int dirfd, const char *de_name)
4543 struct revert_file_args *a = arg;
4544 const struct got_error *err = NULL;
4545 char *parent_path = NULL;
4546 struct got_fileindex_entry *ie;
4547 struct got_tree_object *tree = NULL;
4548 struct got_object_id *tree_id = NULL;
4549 const struct got_tree_entry *te = NULL;
4550 char *tree_path = NULL, *te_name;
4551 char *ondisk_path = NULL, *path_content = NULL;
4552 struct got_blob_object *blob = NULL;
4554 /* Reverting a staged deletion is a no-op. */
4555 if (status == GOT_STATUS_DELETE &&
4556 staged_status != GOT_STATUS_NO_CHANGE)
4557 return NULL;
4559 if (status == GOT_STATUS_UNVERSIONED)
4560 return (*a->progress_cb)(a->progress_arg,
4561 GOT_STATUS_UNVERSIONED, relpath);
4563 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4564 if (ie == NULL)
4565 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4567 /* Construct in-repository path of tree which contains this blob. */
4568 err = got_path_dirname(&parent_path, ie->path);
4569 if (err) {
4570 if (err->code != GOT_ERR_BAD_PATH)
4571 goto done;
4572 parent_path = strdup("/");
4573 if (parent_path == NULL) {
4574 err = got_error_from_errno("strdup");
4575 goto done;
4578 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4579 tree_path = strdup(parent_path);
4580 if (tree_path == NULL) {
4581 err = got_error_from_errno("strdup");
4582 goto done;
4584 } else {
4585 if (got_path_is_root_dir(parent_path)) {
4586 tree_path = strdup(a->worktree->path_prefix);
4587 if (tree_path == NULL) {
4588 err = got_error_from_errno("strdup");
4589 goto done;
4591 } else {
4592 if (asprintf(&tree_path, "%s/%s",
4593 a->worktree->path_prefix, parent_path) == -1) {
4594 err = got_error_from_errno("asprintf");
4595 goto done;
4600 err = got_object_id_by_path(&tree_id, a->repo,
4601 a->worktree->base_commit_id, tree_path);
4602 if (err) {
4603 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4604 (status == GOT_STATUS_ADD ||
4605 staged_status == GOT_STATUS_ADD)))
4606 goto done;
4607 } else {
4608 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4609 if (err)
4610 goto done;
4612 err = got_path_basename(&te_name, ie->path);
4613 if (err)
4614 goto done;
4616 te = got_object_tree_find_entry(tree, te_name);
4617 free(te_name);
4618 if (te == NULL && status != GOT_STATUS_ADD &&
4619 staged_status != GOT_STATUS_ADD) {
4620 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4621 goto done;
4625 switch (status) {
4626 case GOT_STATUS_ADD:
4627 if (a->patch_cb) {
4628 int choice = GOT_PATCH_CHOICE_NONE;
4629 err = (*a->patch_cb)(&choice, a->patch_arg,
4630 status, ie->path, NULL, 1, 1);
4631 if (err)
4632 goto done;
4633 if (choice != GOT_PATCH_CHOICE_YES)
4634 break;
4636 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4637 ie->path);
4638 if (err)
4639 goto done;
4640 got_fileindex_entry_remove(a->fileindex, ie);
4641 if (a->unlink_added_files) {
4642 if (asprintf(&ondisk_path, "%s/%s",
4643 got_worktree_get_root_path(a->worktree),
4644 relpath) == -1) {
4645 err = got_error_from_errno("asprintf");
4646 goto done;
4648 if (unlink(ondisk_path) == -1) {
4649 err = got_error_from_errno2("unlink",
4650 ondisk_path);
4651 break;
4654 break;
4655 case GOT_STATUS_DELETE:
4656 if (a->patch_cb) {
4657 int choice = GOT_PATCH_CHOICE_NONE;
4658 err = (*a->patch_cb)(&choice, a->patch_arg,
4659 status, ie->path, NULL, 1, 1);
4660 if (err)
4661 goto done;
4662 if (choice != GOT_PATCH_CHOICE_YES)
4663 break;
4665 /* fall through */
4666 case GOT_STATUS_MODIFY:
4667 case GOT_STATUS_MODE_CHANGE:
4668 case GOT_STATUS_CONFLICT:
4669 case GOT_STATUS_MISSING: {
4670 struct got_object_id id;
4671 if (staged_status == GOT_STATUS_ADD ||
4672 staged_status == GOT_STATUS_MODIFY) {
4673 memcpy(id.sha1, ie->staged_blob_sha1,
4674 SHA1_DIGEST_LENGTH);
4675 } else
4676 memcpy(id.sha1, ie->blob_sha1,
4677 SHA1_DIGEST_LENGTH);
4678 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4679 if (err)
4680 goto done;
4682 if (asprintf(&ondisk_path, "%s/%s",
4683 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4684 err = got_error_from_errno("asprintf");
4685 goto done;
4688 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4689 status == GOT_STATUS_CONFLICT)) {
4690 int is_bad_symlink = 0;
4691 err = create_patched_content(&path_content, 1, &id,
4692 ondisk_path, dirfd, de_name, ie->path, a->repo,
4693 a->patch_cb, a->patch_arg);
4694 if (err || path_content == NULL)
4695 break;
4696 if (te && S_ISLNK(te->mode)) {
4697 if (unlink(path_content) == -1) {
4698 err = got_error_from_errno2("unlink",
4699 path_content);
4700 break;
4702 err = install_symlink(&is_bad_symlink,
4703 a->worktree, ondisk_path, ie->path,
4704 blob, 0, 1, 0, 0, a->repo,
4705 a->progress_cb, a->progress_arg);
4706 } else {
4707 if (rename(path_content, ondisk_path) == -1) {
4708 err = got_error_from_errno3("rename",
4709 path_content, ondisk_path);
4710 goto done;
4713 } else {
4714 int is_bad_symlink = 0;
4715 if (te && S_ISLNK(te->mode)) {
4716 err = install_symlink(&is_bad_symlink,
4717 a->worktree, ondisk_path, ie->path,
4718 blob, 0, 1, 0, 0, a->repo,
4719 a->progress_cb, a->progress_arg);
4720 } else {
4721 err = install_blob(a->worktree, ondisk_path,
4722 ie->path,
4723 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4724 got_fileindex_perms_to_st(ie), blob,
4725 0, 1, 0, 0, a->repo,
4726 a->progress_cb, a->progress_arg);
4728 if (err)
4729 goto done;
4730 if (status == GOT_STATUS_DELETE ||
4731 status == GOT_STATUS_MODE_CHANGE) {
4732 err = got_fileindex_entry_update(ie,
4733 a->worktree->root_fd, relpath,
4734 blob->id.sha1,
4735 a->worktree->base_commit_id->sha1, 1);
4736 if (err)
4737 goto done;
4739 if (is_bad_symlink) {
4740 got_fileindex_entry_filetype_set(ie,
4741 GOT_FILEIDX_MODE_BAD_SYMLINK);
4744 break;
4746 default:
4747 break;
4749 done:
4750 free(ondisk_path);
4751 free(path_content);
4752 free(parent_path);
4753 free(tree_path);
4754 if (blob)
4755 got_object_blob_close(blob);
4756 if (tree)
4757 got_object_tree_close(tree);
4758 free(tree_id);
4759 return err;
4762 const struct got_error *
4763 got_worktree_revert(struct got_worktree *worktree,
4764 struct got_pathlist_head *paths,
4765 got_worktree_checkout_cb progress_cb, void *progress_arg,
4766 got_worktree_patch_cb patch_cb, void *patch_arg,
4767 struct got_repository *repo)
4769 struct got_fileindex *fileindex = NULL;
4770 char *fileindex_path = NULL;
4771 const struct got_error *err = NULL, *unlockerr = NULL;
4772 const struct got_error *sync_err = NULL;
4773 struct got_pathlist_entry *pe;
4774 struct revert_file_args rfa;
4776 err = lock_worktree(worktree, LOCK_EX);
4777 if (err)
4778 return err;
4780 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4781 if (err)
4782 goto done;
4784 rfa.worktree = worktree;
4785 rfa.fileindex = fileindex;
4786 rfa.progress_cb = progress_cb;
4787 rfa.progress_arg = progress_arg;
4788 rfa.patch_cb = patch_cb;
4789 rfa.patch_arg = patch_arg;
4790 rfa.repo = repo;
4791 rfa.unlink_added_files = 0;
4792 TAILQ_FOREACH(pe, paths, entry) {
4793 err = worktree_status(worktree, pe->path, fileindex, repo,
4794 revert_file, &rfa, NULL, NULL, 1, 0);
4795 if (err)
4796 break;
4798 sync_err = sync_fileindex(fileindex, fileindex_path);
4799 if (sync_err && err == NULL)
4800 err = sync_err;
4801 done:
4802 free(fileindex_path);
4803 if (fileindex)
4804 got_fileindex_free(fileindex);
4805 unlockerr = lock_worktree(worktree, LOCK_SH);
4806 if (unlockerr && err == NULL)
4807 err = unlockerr;
4808 return err;
4811 static void
4812 free_commitable(struct got_commitable *ct)
4814 free(ct->path);
4815 free(ct->in_repo_path);
4816 free(ct->ondisk_path);
4817 free(ct->blob_id);
4818 free(ct->base_blob_id);
4819 free(ct->staged_blob_id);
4820 free(ct->base_commit_id);
4821 free(ct);
4824 struct collect_commitables_arg {
4825 struct got_pathlist_head *commitable_paths;
4826 struct got_repository *repo;
4827 struct got_worktree *worktree;
4828 struct got_fileindex *fileindex;
4829 int have_staged_files;
4830 int allow_bad_symlinks;
4833 static const struct got_error *
4834 collect_commitables(void *arg, unsigned char status,
4835 unsigned char staged_status, const char *relpath,
4836 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4837 struct got_object_id *commit_id, int dirfd, const char *de_name)
4839 struct collect_commitables_arg *a = arg;
4840 const struct got_error *err = NULL;
4841 struct got_commitable *ct = NULL;
4842 struct got_pathlist_entry *new = NULL;
4843 char *parent_path = NULL, *path = NULL;
4844 struct stat sb;
4846 if (a->have_staged_files) {
4847 if (staged_status != GOT_STATUS_MODIFY &&
4848 staged_status != GOT_STATUS_ADD &&
4849 staged_status != GOT_STATUS_DELETE)
4850 return NULL;
4851 } else {
4852 if (status == GOT_STATUS_CONFLICT)
4853 return got_error(GOT_ERR_COMMIT_CONFLICT);
4855 if (status != GOT_STATUS_MODIFY &&
4856 status != GOT_STATUS_MODE_CHANGE &&
4857 status != GOT_STATUS_ADD &&
4858 status != GOT_STATUS_DELETE)
4859 return NULL;
4862 if (asprintf(&path, "/%s", relpath) == -1) {
4863 err = got_error_from_errno("asprintf");
4864 goto done;
4866 if (strcmp(path, "/") == 0) {
4867 parent_path = strdup("");
4868 if (parent_path == NULL)
4869 return got_error_from_errno("strdup");
4870 } else {
4871 err = got_path_dirname(&parent_path, path);
4872 if (err)
4873 return err;
4876 ct = calloc(1, sizeof(*ct));
4877 if (ct == NULL) {
4878 err = got_error_from_errno("calloc");
4879 goto done;
4882 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4883 relpath) == -1) {
4884 err = got_error_from_errno("asprintf");
4885 goto done;
4888 if (staged_status == GOT_STATUS_ADD ||
4889 staged_status == GOT_STATUS_MODIFY) {
4890 struct got_fileindex_entry *ie;
4891 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4892 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4893 case GOT_FILEIDX_MODE_REGULAR_FILE:
4894 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4895 ct->mode = S_IFREG;
4896 break;
4897 case GOT_FILEIDX_MODE_SYMLINK:
4898 ct->mode = S_IFLNK;
4899 break;
4900 default:
4901 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4902 goto done;
4904 ct->mode |= got_fileindex_entry_perms_get(ie);
4905 } else if (status != GOT_STATUS_DELETE &&
4906 staged_status != GOT_STATUS_DELETE) {
4907 if (dirfd != -1) {
4908 if (fstatat(dirfd, de_name, &sb,
4909 AT_SYMLINK_NOFOLLOW) == -1) {
4910 err = got_error_from_errno2("fstatat",
4911 ct->ondisk_path);
4912 goto done;
4914 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4915 err = got_error_from_errno2("lstat", ct->ondisk_path);
4916 goto done;
4918 ct->mode = sb.st_mode;
4921 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4922 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4923 relpath) == -1) {
4924 err = got_error_from_errno("asprintf");
4925 goto done;
4928 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4929 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4930 int is_bad_symlink;
4931 char target_path[PATH_MAX];
4932 ssize_t target_len;
4933 target_len = readlink(ct->ondisk_path, target_path,
4934 sizeof(target_path));
4935 if (target_len == -1) {
4936 err = got_error_from_errno2("readlink",
4937 ct->ondisk_path);
4938 goto done;
4940 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4941 target_len, ct->ondisk_path, a->worktree->root_path);
4942 if (err)
4943 goto done;
4944 if (is_bad_symlink) {
4945 err = got_error_path(ct->ondisk_path,
4946 GOT_ERR_BAD_SYMLINK);
4947 goto done;
4952 ct->status = status;
4953 ct->staged_status = staged_status;
4954 ct->blob_id = NULL; /* will be filled in when blob gets created */
4955 if (ct->status != GOT_STATUS_ADD &&
4956 ct->staged_status != GOT_STATUS_ADD) {
4957 ct->base_blob_id = got_object_id_dup(blob_id);
4958 if (ct->base_blob_id == NULL) {
4959 err = got_error_from_errno("got_object_id_dup");
4960 goto done;
4962 ct->base_commit_id = got_object_id_dup(commit_id);
4963 if (ct->base_commit_id == NULL) {
4964 err = got_error_from_errno("got_object_id_dup");
4965 goto done;
4968 if (ct->staged_status == GOT_STATUS_ADD ||
4969 ct->staged_status == GOT_STATUS_MODIFY) {
4970 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4971 if (ct->staged_blob_id == NULL) {
4972 err = got_error_from_errno("got_object_id_dup");
4973 goto done;
4976 ct->path = strdup(path);
4977 if (ct->path == NULL) {
4978 err = got_error_from_errno("strdup");
4979 goto done;
4981 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4982 done:
4983 if (ct && (err || new == NULL))
4984 free_commitable(ct);
4985 free(parent_path);
4986 free(path);
4987 return err;
4990 static const struct got_error *write_tree(struct got_object_id **, int *,
4991 struct got_tree_object *, const char *, struct got_pathlist_head *,
4992 got_worktree_status_cb status_cb, void *status_arg,
4993 struct got_repository *);
4995 static const struct got_error *
4996 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4997 struct got_tree_entry *te, const char *parent_path,
4998 struct got_pathlist_head *commitable_paths,
4999 got_worktree_status_cb status_cb, void *status_arg,
5000 struct got_repository *repo)
5002 const struct got_error *err = NULL;
5003 struct got_tree_object *subtree;
5004 char *subpath;
5006 if (asprintf(&subpath, "%s%s%s", parent_path,
5007 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5008 return got_error_from_errno("asprintf");
5010 err = got_object_open_as_tree(&subtree, repo, &te->id);
5011 if (err)
5012 return err;
5014 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5015 commitable_paths, status_cb, status_arg, repo);
5016 got_object_tree_close(subtree);
5017 free(subpath);
5018 return err;
5021 static const struct got_error *
5022 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5024 const struct got_error *err = NULL;
5025 char *ct_parent_path = NULL;
5027 *match = 0;
5029 if (strchr(ct->in_repo_path, '/') == NULL) {
5030 *match = got_path_is_root_dir(path);
5031 return NULL;
5034 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5035 if (err)
5036 return err;
5037 *match = (strcmp(path, ct_parent_path) == 0);
5038 free(ct_parent_path);
5039 return err;
5042 static mode_t
5043 get_ct_file_mode(struct got_commitable *ct)
5045 if (S_ISLNK(ct->mode))
5046 return S_IFLNK;
5048 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5051 static const struct got_error *
5052 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5053 struct got_tree_entry *te, struct got_commitable *ct)
5055 const struct got_error *err = NULL;
5057 *new_te = NULL;
5059 err = got_object_tree_entry_dup(new_te, te);
5060 if (err)
5061 goto done;
5063 (*new_te)->mode = get_ct_file_mode(ct);
5065 if (ct->staged_status == GOT_STATUS_MODIFY)
5066 memcpy(&(*new_te)->id, ct->staged_blob_id,
5067 sizeof((*new_te)->id));
5068 else
5069 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5070 done:
5071 if (err && *new_te) {
5072 free(*new_te);
5073 *new_te = NULL;
5075 return err;
5078 static const struct got_error *
5079 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5080 struct got_commitable *ct)
5082 const struct got_error *err = NULL;
5083 char *ct_name = NULL;
5085 *new_te = NULL;
5087 *new_te = calloc(1, sizeof(**new_te));
5088 if (*new_te == NULL)
5089 return got_error_from_errno("calloc");
5091 err = got_path_basename(&ct_name, ct->path);
5092 if (err)
5093 goto done;
5094 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5095 sizeof((*new_te)->name)) {
5096 err = got_error(GOT_ERR_NO_SPACE);
5097 goto done;
5100 (*new_te)->mode = get_ct_file_mode(ct);
5102 if (ct->staged_status == GOT_STATUS_ADD)
5103 memcpy(&(*new_te)->id, ct->staged_blob_id,
5104 sizeof((*new_te)->id));
5105 else
5106 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5107 done:
5108 free(ct_name);
5109 if (err && *new_te) {
5110 free(*new_te);
5111 *new_te = NULL;
5113 return err;
5116 static const struct got_error *
5117 insert_tree_entry(struct got_tree_entry *new_te,
5118 struct got_pathlist_head *paths)
5120 const struct got_error *err = NULL;
5121 struct got_pathlist_entry *new_pe;
5123 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5124 if (err)
5125 return err;
5126 if (new_pe == NULL)
5127 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5128 return NULL;
5131 static const struct got_error *
5132 report_ct_status(struct got_commitable *ct,
5133 got_worktree_status_cb status_cb, void *status_arg)
5135 const char *ct_path = ct->path;
5136 unsigned char status;
5138 if (status_cb == NULL) /* no commit progress output desired */
5139 return NULL;
5141 while (ct_path[0] == '/')
5142 ct_path++;
5144 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5145 status = ct->staged_status;
5146 else
5147 status = ct->status;
5149 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5150 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5153 static const struct got_error *
5154 match_modified_subtree(int *modified, struct got_tree_entry *te,
5155 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5157 const struct got_error *err = NULL;
5158 struct got_pathlist_entry *pe;
5159 char *te_path;
5161 *modified = 0;
5163 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5164 got_path_is_root_dir(base_tree_path) ? "" : "/",
5165 te->name) == -1)
5166 return got_error_from_errno("asprintf");
5168 TAILQ_FOREACH(pe, commitable_paths, entry) {
5169 struct got_commitable *ct = pe->data;
5170 *modified = got_path_is_child(ct->in_repo_path, te_path,
5171 strlen(te_path));
5172 if (*modified)
5173 break;
5176 free(te_path);
5177 return err;
5180 static const struct got_error *
5181 match_deleted_or_modified_ct(struct got_commitable **ctp,
5182 struct got_tree_entry *te, const char *base_tree_path,
5183 struct got_pathlist_head *commitable_paths)
5185 const struct got_error *err = NULL;
5186 struct got_pathlist_entry *pe;
5188 *ctp = NULL;
5190 TAILQ_FOREACH(pe, commitable_paths, entry) {
5191 struct got_commitable *ct = pe->data;
5192 char *ct_name = NULL;
5193 int path_matches;
5195 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5196 if (ct->status != GOT_STATUS_MODIFY &&
5197 ct->status != GOT_STATUS_MODE_CHANGE &&
5198 ct->status != GOT_STATUS_DELETE)
5199 continue;
5200 } else {
5201 if (ct->staged_status != GOT_STATUS_MODIFY &&
5202 ct->staged_status != GOT_STATUS_DELETE)
5203 continue;
5206 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5207 continue;
5209 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5210 if (err)
5211 return err;
5212 if (!path_matches)
5213 continue;
5215 err = got_path_basename(&ct_name, pe->path);
5216 if (err)
5217 return err;
5219 if (strcmp(te->name, ct_name) != 0) {
5220 free(ct_name);
5221 continue;
5223 free(ct_name);
5225 *ctp = ct;
5226 break;
5229 return err;
5232 static const struct got_error *
5233 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5234 const char *child_path, const char *path_base_tree,
5235 struct got_pathlist_head *commitable_paths,
5236 got_worktree_status_cb status_cb, void *status_arg,
5237 struct got_repository *repo)
5239 const struct got_error *err = NULL;
5240 struct got_tree_entry *new_te;
5241 char *subtree_path;
5242 struct got_object_id *id = NULL;
5243 int nentries;
5245 *new_tep = NULL;
5247 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5248 got_path_is_root_dir(path_base_tree) ? "" : "/",
5249 child_path) == -1)
5250 return got_error_from_errno("asprintf");
5252 new_te = calloc(1, sizeof(*new_te));
5253 if (new_te == NULL)
5254 return got_error_from_errno("calloc");
5255 new_te->mode = S_IFDIR;
5257 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5258 sizeof(new_te->name)) {
5259 err = got_error(GOT_ERR_NO_SPACE);
5260 goto done;
5262 err = write_tree(&id, &nentries, NULL, subtree_path,
5263 commitable_paths, status_cb, status_arg, repo);
5264 if (err) {
5265 free(new_te);
5266 goto done;
5268 memcpy(&new_te->id, id, sizeof(new_te->id));
5269 done:
5270 free(id);
5271 free(subtree_path);
5272 if (err == NULL)
5273 *new_tep = new_te;
5274 return err;
5277 static const struct got_error *
5278 write_tree(struct got_object_id **new_tree_id, int *nentries,
5279 struct got_tree_object *base_tree, const char *path_base_tree,
5280 struct got_pathlist_head *commitable_paths,
5281 got_worktree_status_cb status_cb, void *status_arg,
5282 struct got_repository *repo)
5284 const struct got_error *err = NULL;
5285 struct got_pathlist_head paths;
5286 struct got_tree_entry *te, *new_te = NULL;
5287 struct got_pathlist_entry *pe;
5289 TAILQ_INIT(&paths);
5290 *nentries = 0;
5292 /* Insert, and recurse into, newly added entries first. */
5293 TAILQ_FOREACH(pe, commitable_paths, entry) {
5294 struct got_commitable *ct = pe->data;
5295 char *child_path = NULL, *slash;
5297 if ((ct->status != GOT_STATUS_ADD &&
5298 ct->staged_status != GOT_STATUS_ADD) ||
5299 (ct->flags & GOT_COMMITABLE_ADDED))
5300 continue;
5302 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5303 strlen(path_base_tree)))
5304 continue;
5306 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5307 ct->in_repo_path);
5308 if (err)
5309 goto done;
5311 slash = strchr(child_path, '/');
5312 if (slash == NULL) {
5313 err = alloc_added_blob_tree_entry(&new_te, ct);
5314 if (err)
5315 goto done;
5316 err = report_ct_status(ct, status_cb, status_arg);
5317 if (err)
5318 goto done;
5319 ct->flags |= GOT_COMMITABLE_ADDED;
5320 err = insert_tree_entry(new_te, &paths);
5321 if (err)
5322 goto done;
5323 (*nentries)++;
5324 } else {
5325 *slash = '\0'; /* trim trailing path components */
5326 if (base_tree == NULL ||
5327 got_object_tree_find_entry(base_tree, child_path)
5328 == NULL) {
5329 err = make_subtree_for_added_blob(&new_te,
5330 child_path, path_base_tree,
5331 commitable_paths, status_cb, status_arg,
5332 repo);
5333 if (err)
5334 goto done;
5335 err = insert_tree_entry(new_te, &paths);
5336 if (err)
5337 goto done;
5338 (*nentries)++;
5343 if (base_tree) {
5344 int i, nbase_entries;
5345 /* Handle modified and deleted entries. */
5346 nbase_entries = got_object_tree_get_nentries(base_tree);
5347 for (i = 0; i < nbase_entries; i++) {
5348 struct got_commitable *ct = NULL;
5350 te = got_object_tree_get_entry(base_tree, i);
5351 if (got_object_tree_entry_is_submodule(te)) {
5352 /* Entry is a submodule; just copy it. */
5353 err = got_object_tree_entry_dup(&new_te, te);
5354 if (err)
5355 goto done;
5356 err = insert_tree_entry(new_te, &paths);
5357 if (err)
5358 goto done;
5359 (*nentries)++;
5360 continue;
5363 if (S_ISDIR(te->mode)) {
5364 int modified;
5365 err = got_object_tree_entry_dup(&new_te, te);
5366 if (err)
5367 goto done;
5368 err = match_modified_subtree(&modified, te,
5369 path_base_tree, commitable_paths);
5370 if (err)
5371 goto done;
5372 /* Avoid recursion into unmodified subtrees. */
5373 if (modified) {
5374 struct got_object_id *new_id;
5375 int nsubentries;
5376 err = write_subtree(&new_id,
5377 &nsubentries, te,
5378 path_base_tree, commitable_paths,
5379 status_cb, status_arg, repo);
5380 if (err)
5381 goto done;
5382 if (nsubentries == 0) {
5383 /* All entries were deleted. */
5384 free(new_id);
5385 continue;
5387 memcpy(&new_te->id, new_id,
5388 sizeof(new_te->id));
5389 free(new_id);
5391 err = insert_tree_entry(new_te, &paths);
5392 if (err)
5393 goto done;
5394 (*nentries)++;
5395 continue;
5398 err = match_deleted_or_modified_ct(&ct, te,
5399 path_base_tree, commitable_paths);
5400 if (err)
5401 goto done;
5402 if (ct) {
5403 /* NB: Deleted entries get dropped here. */
5404 if (ct->status == GOT_STATUS_MODIFY ||
5405 ct->status == GOT_STATUS_MODE_CHANGE ||
5406 ct->staged_status == GOT_STATUS_MODIFY) {
5407 err = alloc_modified_blob_tree_entry(
5408 &new_te, te, ct);
5409 if (err)
5410 goto done;
5411 err = insert_tree_entry(new_te, &paths);
5412 if (err)
5413 goto done;
5414 (*nentries)++;
5416 err = report_ct_status(ct, status_cb,
5417 status_arg);
5418 if (err)
5419 goto done;
5420 } else {
5421 /* Entry is unchanged; just copy it. */
5422 err = got_object_tree_entry_dup(&new_te, te);
5423 if (err)
5424 goto done;
5425 err = insert_tree_entry(new_te, &paths);
5426 if (err)
5427 goto done;
5428 (*nentries)++;
5433 /* Write new list of entries; deleted entries have been dropped. */
5434 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5435 done:
5436 got_pathlist_free(&paths);
5437 return err;
5440 static const struct got_error *
5441 update_fileindex_after_commit(struct got_worktree *worktree,
5442 struct got_pathlist_head *commitable_paths,
5443 struct got_object_id *new_base_commit_id,
5444 struct got_fileindex *fileindex, int have_staged_files)
5446 const struct got_error *err = NULL;
5447 struct got_pathlist_entry *pe;
5448 char *relpath = NULL;
5450 TAILQ_FOREACH(pe, commitable_paths, entry) {
5451 struct got_fileindex_entry *ie;
5452 struct got_commitable *ct = pe->data;
5454 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5456 err = got_path_skip_common_ancestor(&relpath,
5457 worktree->root_path, ct->ondisk_path);
5458 if (err)
5459 goto done;
5461 if (ie) {
5462 if (ct->status == GOT_STATUS_DELETE ||
5463 ct->staged_status == GOT_STATUS_DELETE) {
5464 got_fileindex_entry_remove(fileindex, ie);
5465 } else if (ct->staged_status == GOT_STATUS_ADD ||
5466 ct->staged_status == GOT_STATUS_MODIFY) {
5467 got_fileindex_entry_stage_set(ie,
5468 GOT_FILEIDX_STAGE_NONE);
5469 got_fileindex_entry_staged_filetype_set(ie, 0);
5471 err = got_fileindex_entry_update(ie,
5472 worktree->root_fd, relpath,
5473 ct->staged_blob_id->sha1,
5474 new_base_commit_id->sha1,
5475 !have_staged_files);
5476 } else
5477 err = got_fileindex_entry_update(ie,
5478 worktree->root_fd, relpath,
5479 ct->blob_id->sha1,
5480 new_base_commit_id->sha1,
5481 !have_staged_files);
5482 } else {
5483 err = got_fileindex_entry_alloc(&ie, pe->path);
5484 if (err)
5485 goto done;
5486 err = got_fileindex_entry_update(ie,
5487 worktree->root_fd, relpath, ct->blob_id->sha1,
5488 new_base_commit_id->sha1, 1);
5489 if (err) {
5490 got_fileindex_entry_free(ie);
5491 goto done;
5493 err = got_fileindex_entry_add(fileindex, ie);
5494 if (err) {
5495 got_fileindex_entry_free(ie);
5496 goto done;
5499 free(relpath);
5500 relpath = NULL;
5502 done:
5503 free(relpath);
5504 return err;
5508 static const struct got_error *
5509 check_out_of_date(const char *in_repo_path, unsigned char status,
5510 unsigned char staged_status, struct got_object_id *base_blob_id,
5511 struct got_object_id *base_commit_id,
5512 struct got_object_id *head_commit_id, struct got_repository *repo,
5513 int ood_errcode)
5515 const struct got_error *err = NULL;
5516 struct got_object_id *id = NULL;
5518 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5519 /* Trivial case: base commit == head commit */
5520 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5521 return NULL;
5523 * Ensure file content which local changes were based
5524 * on matches file content in the branch head.
5526 err = got_object_id_by_path(&id, repo, head_commit_id,
5527 in_repo_path);
5528 if (err) {
5529 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5530 err = got_error(ood_errcode);
5531 goto done;
5532 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5533 err = got_error(ood_errcode);
5534 } else {
5535 /* Require that added files don't exist in the branch head. */
5536 err = got_object_id_by_path(&id, repo, head_commit_id,
5537 in_repo_path);
5538 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5539 goto done;
5540 err = id ? got_error(ood_errcode) : NULL;
5542 done:
5543 free(id);
5544 return err;
5547 const struct got_error *
5548 commit_worktree(struct got_object_id **new_commit_id,
5549 struct got_pathlist_head *commitable_paths,
5550 struct got_object_id *head_commit_id,
5551 struct got_object_id *parent_id2,
5552 struct got_worktree *worktree,
5553 const char *author, const char *committer,
5554 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5555 got_worktree_status_cb status_cb, void *status_arg,
5556 struct got_repository *repo)
5558 const struct got_error *err = NULL, *unlockerr = NULL;
5559 struct got_pathlist_entry *pe;
5560 const char *head_ref_name = NULL;
5561 struct got_commit_object *head_commit = NULL;
5562 struct got_reference *head_ref2 = NULL;
5563 struct got_object_id *head_commit_id2 = NULL;
5564 struct got_tree_object *head_tree = NULL;
5565 struct got_object_id *new_tree_id = NULL;
5566 int nentries, nparents = 0;
5567 struct got_object_id_queue parent_ids;
5568 struct got_object_qid *pid = NULL;
5569 char *logmsg = NULL;
5571 *new_commit_id = NULL;
5573 STAILQ_INIT(&parent_ids);
5575 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5576 if (err)
5577 goto done;
5579 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5580 if (err)
5581 goto done;
5583 if (commit_msg_cb != NULL) {
5584 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5585 if (err)
5586 goto done;
5589 if (logmsg == NULL || strlen(logmsg) == 0) {
5590 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5591 goto done;
5594 /* Create blobs from added and modified files and record their IDs. */
5595 TAILQ_FOREACH(pe, commitable_paths, entry) {
5596 struct got_commitable *ct = pe->data;
5597 char *ondisk_path;
5599 /* Blobs for staged files already exist. */
5600 if (ct->staged_status == GOT_STATUS_ADD ||
5601 ct->staged_status == GOT_STATUS_MODIFY)
5602 continue;
5604 if (ct->status != GOT_STATUS_ADD &&
5605 ct->status != GOT_STATUS_MODIFY &&
5606 ct->status != GOT_STATUS_MODE_CHANGE)
5607 continue;
5609 if (asprintf(&ondisk_path, "%s/%s",
5610 worktree->root_path, pe->path) == -1) {
5611 err = got_error_from_errno("asprintf");
5612 goto done;
5614 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5615 free(ondisk_path);
5616 if (err)
5617 goto done;
5620 /* Recursively write new tree objects. */
5621 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5622 commitable_paths, status_cb, status_arg, repo);
5623 if (err)
5624 goto done;
5626 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5627 if (err)
5628 goto done;
5629 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5630 nparents++;
5631 if (parent_id2) {
5632 err = got_object_qid_alloc(&pid, parent_id2);
5633 if (err)
5634 goto done;
5635 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5636 nparents++;
5638 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5639 nparents, author, time(NULL), committer, time(NULL), logmsg, repo);
5640 if (logmsg != NULL)
5641 free(logmsg);
5642 if (err)
5643 goto done;
5645 /* Check if a concurrent commit to our branch has occurred. */
5646 head_ref_name = got_worktree_get_head_ref_name(worktree);
5647 if (head_ref_name == NULL) {
5648 err = got_error_from_errno("got_worktree_get_head_ref_name");
5649 goto done;
5651 /* Lock the reference here to prevent concurrent modification. */
5652 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5653 if (err)
5654 goto done;
5655 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5656 if (err)
5657 goto done;
5658 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5659 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5660 goto done;
5662 /* Update branch head in repository. */
5663 err = got_ref_change_ref(head_ref2, *new_commit_id);
5664 if (err)
5665 goto done;
5666 err = got_ref_write(head_ref2, repo);
5667 if (err)
5668 goto done;
5670 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5671 if (err)
5672 goto done;
5674 err = ref_base_commit(worktree, repo);
5675 if (err)
5676 goto done;
5677 done:
5678 got_object_id_queue_free(&parent_ids);
5679 if (head_tree)
5680 got_object_tree_close(head_tree);
5681 if (head_commit)
5682 got_object_commit_close(head_commit);
5683 free(head_commit_id2);
5684 if (head_ref2) {
5685 unlockerr = got_ref_unlock(head_ref2);
5686 if (unlockerr && err == NULL)
5687 err = unlockerr;
5688 got_ref_close(head_ref2);
5690 return err;
5693 static const struct got_error *
5694 check_path_is_commitable(const char *path,
5695 struct got_pathlist_head *commitable_paths)
5697 struct got_pathlist_entry *cpe = NULL;
5698 size_t path_len = strlen(path);
5700 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5701 struct got_commitable *ct = cpe->data;
5702 const char *ct_path = ct->path;
5704 while (ct_path[0] == '/')
5705 ct_path++;
5707 if (strcmp(path, ct_path) == 0 ||
5708 got_path_is_child(ct_path, path, path_len))
5709 break;
5712 if (cpe == NULL)
5713 return got_error_path(path, GOT_ERR_BAD_PATH);
5715 return NULL;
5718 static const struct got_error *
5719 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5721 int *have_staged_files = arg;
5723 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5724 *have_staged_files = 1;
5725 return got_error(GOT_ERR_CANCELLED);
5728 return NULL;
5731 static const struct got_error *
5732 check_non_staged_files(struct got_fileindex *fileindex,
5733 struct got_pathlist_head *paths)
5735 struct got_pathlist_entry *pe;
5736 struct got_fileindex_entry *ie;
5738 TAILQ_FOREACH(pe, paths, entry) {
5739 if (pe->path[0] == '\0')
5740 continue;
5741 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5742 if (ie == NULL)
5743 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5744 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5745 return got_error_path(pe->path,
5746 GOT_ERR_FILE_NOT_STAGED);
5749 return NULL;
5752 const struct got_error *
5753 got_worktree_commit(struct got_object_id **new_commit_id,
5754 struct got_worktree *worktree, struct got_pathlist_head *paths,
5755 const char *author, const char *committer, int allow_bad_symlinks,
5756 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5757 got_worktree_status_cb status_cb, void *status_arg,
5758 struct got_repository *repo)
5760 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5761 struct got_fileindex *fileindex = NULL;
5762 char *fileindex_path = NULL;
5763 struct got_pathlist_head commitable_paths;
5764 struct collect_commitables_arg cc_arg;
5765 struct got_pathlist_entry *pe;
5766 struct got_reference *head_ref = NULL;
5767 struct got_object_id *head_commit_id = NULL;
5768 int have_staged_files = 0;
5770 *new_commit_id = NULL;
5772 TAILQ_INIT(&commitable_paths);
5774 err = lock_worktree(worktree, LOCK_EX);
5775 if (err)
5776 goto done;
5778 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5779 if (err)
5780 goto done;
5782 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5783 if (err)
5784 goto done;
5786 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5787 if (err)
5788 goto done;
5790 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5791 &have_staged_files);
5792 if (err && err->code != GOT_ERR_CANCELLED)
5793 goto done;
5794 if (have_staged_files) {
5795 err = check_non_staged_files(fileindex, paths);
5796 if (err)
5797 goto done;
5800 cc_arg.commitable_paths = &commitable_paths;
5801 cc_arg.worktree = worktree;
5802 cc_arg.fileindex = fileindex;
5803 cc_arg.repo = repo;
5804 cc_arg.have_staged_files = have_staged_files;
5805 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5806 TAILQ_FOREACH(pe, paths, entry) {
5807 err = worktree_status(worktree, pe->path, fileindex, repo,
5808 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
5809 if (err)
5810 goto done;
5813 if (TAILQ_EMPTY(&commitable_paths)) {
5814 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5815 goto done;
5818 TAILQ_FOREACH(pe, paths, entry) {
5819 err = check_path_is_commitable(pe->path, &commitable_paths);
5820 if (err)
5821 goto done;
5824 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5825 struct got_commitable *ct = pe->data;
5826 const char *ct_path = ct->in_repo_path;
5828 while (ct_path[0] == '/')
5829 ct_path++;
5830 err = check_out_of_date(ct_path, ct->status,
5831 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5832 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5833 if (err)
5834 goto done;
5838 err = commit_worktree(new_commit_id, &commitable_paths,
5839 head_commit_id, NULL, worktree, author, committer,
5840 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5841 if (err)
5842 goto done;
5844 err = update_fileindex_after_commit(worktree, &commitable_paths,
5845 *new_commit_id, fileindex, have_staged_files);
5846 sync_err = sync_fileindex(fileindex, fileindex_path);
5847 if (sync_err && err == NULL)
5848 err = sync_err;
5849 done:
5850 if (fileindex)
5851 got_fileindex_free(fileindex);
5852 free(fileindex_path);
5853 unlockerr = lock_worktree(worktree, LOCK_SH);
5854 if (unlockerr && err == NULL)
5855 err = unlockerr;
5856 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5857 struct got_commitable *ct = pe->data;
5858 free_commitable(ct);
5860 got_pathlist_free(&commitable_paths);
5861 return err;
5864 const char *
5865 got_commitable_get_path(struct got_commitable *ct)
5867 return ct->path;
5870 unsigned int
5871 got_commitable_get_status(struct got_commitable *ct)
5873 return ct->status;
5876 struct check_rebase_ok_arg {
5877 struct got_worktree *worktree;
5878 struct got_repository *repo;
5881 static const struct got_error *
5882 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5884 const struct got_error *err = NULL;
5885 struct check_rebase_ok_arg *a = arg;
5886 unsigned char status;
5887 struct stat sb;
5888 char *ondisk_path;
5890 /* Reject rebase of a work tree with mixed base commits. */
5891 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5892 SHA1_DIGEST_LENGTH))
5893 return got_error(GOT_ERR_MIXED_COMMITS);
5895 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5896 == -1)
5897 return got_error_from_errno("asprintf");
5899 /* Reject rebase of a work tree with modified or staged files. */
5900 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5901 free(ondisk_path);
5902 if (err)
5903 return err;
5905 if (status != GOT_STATUS_NO_CHANGE)
5906 return got_error(GOT_ERR_MODIFIED);
5907 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5908 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5910 return NULL;
5913 const struct got_error *
5914 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5915 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5916 struct got_worktree *worktree, struct got_reference *branch,
5917 struct got_repository *repo)
5919 const struct got_error *err = NULL;
5920 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5921 char *branch_ref_name = NULL;
5922 char *fileindex_path = NULL;
5923 struct check_rebase_ok_arg ok_arg;
5924 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5925 struct got_object_id *wt_branch_tip = NULL;
5927 *new_base_branch_ref = NULL;
5928 *tmp_branch = NULL;
5929 *fileindex = NULL;
5931 err = lock_worktree(worktree, LOCK_EX);
5932 if (err)
5933 return err;
5935 err = open_fileindex(fileindex, &fileindex_path, worktree);
5936 if (err)
5937 goto done;
5939 ok_arg.worktree = worktree;
5940 ok_arg.repo = repo;
5941 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5942 &ok_arg);
5943 if (err)
5944 goto done;
5946 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5947 if (err)
5948 goto done;
5950 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5951 if (err)
5952 goto done;
5954 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5955 if (err)
5956 goto done;
5958 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5959 0);
5960 if (err)
5961 goto done;
5963 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5964 if (err)
5965 goto done;
5966 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5967 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5968 goto done;
5971 err = got_ref_alloc_symref(new_base_branch_ref,
5972 new_base_branch_ref_name, wt_branch);
5973 if (err)
5974 goto done;
5975 err = got_ref_write(*new_base_branch_ref, repo);
5976 if (err)
5977 goto done;
5979 /* TODO Lock original branch's ref while rebasing? */
5981 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5982 if (err)
5983 goto done;
5985 err = got_ref_write(branch_ref, repo);
5986 if (err)
5987 goto done;
5989 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5990 worktree->base_commit_id);
5991 if (err)
5992 goto done;
5993 err = got_ref_write(*tmp_branch, repo);
5994 if (err)
5995 goto done;
5997 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5998 if (err)
5999 goto done;
6000 done:
6001 free(fileindex_path);
6002 free(tmp_branch_name);
6003 free(new_base_branch_ref_name);
6004 free(branch_ref_name);
6005 if (branch_ref)
6006 got_ref_close(branch_ref);
6007 if (wt_branch)
6008 got_ref_close(wt_branch);
6009 free(wt_branch_tip);
6010 if (err) {
6011 if (*new_base_branch_ref) {
6012 got_ref_close(*new_base_branch_ref);
6013 *new_base_branch_ref = NULL;
6015 if (*tmp_branch) {
6016 got_ref_close(*tmp_branch);
6017 *tmp_branch = NULL;
6019 if (*fileindex) {
6020 got_fileindex_free(*fileindex);
6021 *fileindex = NULL;
6023 lock_worktree(worktree, LOCK_SH);
6025 return err;
6028 const struct got_error *
6029 got_worktree_rebase_continue(struct got_object_id **commit_id,
6030 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6031 struct got_reference **branch, struct got_fileindex **fileindex,
6032 struct got_worktree *worktree, struct got_repository *repo)
6034 const struct got_error *err;
6035 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6036 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6037 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6038 char *fileindex_path = NULL;
6039 int have_staged_files = 0;
6041 *commit_id = NULL;
6042 *new_base_branch = NULL;
6043 *tmp_branch = NULL;
6044 *branch = NULL;
6045 *fileindex = NULL;
6047 err = lock_worktree(worktree, LOCK_EX);
6048 if (err)
6049 return err;
6051 err = open_fileindex(fileindex, &fileindex_path, worktree);
6052 if (err)
6053 goto done;
6055 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6056 &have_staged_files);
6057 if (err && err->code != GOT_ERR_CANCELLED)
6058 goto done;
6059 if (have_staged_files) {
6060 err = got_error(GOT_ERR_STAGED_PATHS);
6061 goto done;
6064 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6065 if (err)
6066 goto done;
6068 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6069 if (err)
6070 goto done;
6072 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6073 if (err)
6074 goto done;
6076 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6077 if (err)
6078 goto done;
6080 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6081 if (err)
6082 goto done;
6084 err = got_ref_open(branch, repo,
6085 got_ref_get_symref_target(branch_ref), 0);
6086 if (err)
6087 goto done;
6089 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6090 if (err)
6091 goto done;
6093 err = got_ref_resolve(commit_id, repo, commit_ref);
6094 if (err)
6095 goto done;
6097 err = got_ref_open(new_base_branch, repo,
6098 new_base_branch_ref_name, 0);
6099 if (err)
6100 goto done;
6102 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6103 if (err)
6104 goto done;
6105 done:
6106 free(commit_ref_name);
6107 free(branch_ref_name);
6108 free(fileindex_path);
6109 if (commit_ref)
6110 got_ref_close(commit_ref);
6111 if (branch_ref)
6112 got_ref_close(branch_ref);
6113 if (err) {
6114 free(*commit_id);
6115 *commit_id = NULL;
6116 if (*tmp_branch) {
6117 got_ref_close(*tmp_branch);
6118 *tmp_branch = NULL;
6120 if (*new_base_branch) {
6121 got_ref_close(*new_base_branch);
6122 *new_base_branch = NULL;
6124 if (*branch) {
6125 got_ref_close(*branch);
6126 *branch = NULL;
6128 if (*fileindex) {
6129 got_fileindex_free(*fileindex);
6130 *fileindex = NULL;
6132 lock_worktree(worktree, LOCK_SH);
6134 return err;
6137 const struct got_error *
6138 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6140 const struct got_error *err;
6141 char *tmp_branch_name = NULL;
6143 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6144 if (err)
6145 return err;
6147 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6148 free(tmp_branch_name);
6149 return NULL;
6152 static const struct got_error *
6153 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6154 char **logmsg, void *arg)
6156 *logmsg = arg;
6157 return NULL;
6160 static const struct got_error *
6161 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6162 const char *path, struct got_object_id *blob_id,
6163 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6164 int dirfd, const char *de_name)
6166 return NULL;
6169 struct collect_merged_paths_arg {
6170 got_worktree_checkout_cb progress_cb;
6171 void *progress_arg;
6172 struct got_pathlist_head *merged_paths;
6175 static const struct got_error *
6176 collect_merged_paths(void *arg, unsigned char status, const char *path)
6178 const struct got_error *err;
6179 struct collect_merged_paths_arg *a = arg;
6180 char *p;
6181 struct got_pathlist_entry *new;
6183 err = (*a->progress_cb)(a->progress_arg, status, path);
6184 if (err)
6185 return err;
6187 if (status != GOT_STATUS_MERGE &&
6188 status != GOT_STATUS_ADD &&
6189 status != GOT_STATUS_DELETE &&
6190 status != GOT_STATUS_CONFLICT)
6191 return NULL;
6193 p = strdup(path);
6194 if (p == NULL)
6195 return got_error_from_errno("strdup");
6197 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6198 if (err || new == NULL)
6199 free(p);
6200 return err;
6203 void
6204 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6206 struct got_pathlist_entry *pe;
6208 TAILQ_FOREACH(pe, merged_paths, entry)
6209 free((char *)pe->path);
6211 got_pathlist_free(merged_paths);
6214 static const struct got_error *
6215 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6216 int is_rebase, struct got_repository *repo)
6218 const struct got_error *err;
6219 struct got_reference *commit_ref = NULL;
6221 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6222 if (err) {
6223 if (err->code != GOT_ERR_NOT_REF)
6224 goto done;
6225 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6226 if (err)
6227 goto done;
6228 err = got_ref_write(commit_ref, repo);
6229 if (err)
6230 goto done;
6231 } else if (is_rebase) {
6232 struct got_object_id *stored_id;
6233 int cmp;
6235 err = got_ref_resolve(&stored_id, repo, commit_ref);
6236 if (err)
6237 goto done;
6238 cmp = got_object_id_cmp(commit_id, stored_id);
6239 free(stored_id);
6240 if (cmp != 0) {
6241 err = got_error(GOT_ERR_REBASE_COMMITID);
6242 goto done;
6245 done:
6246 if (commit_ref)
6247 got_ref_close(commit_ref);
6248 return err;
6251 static const struct got_error *
6252 rebase_merge_files(struct got_pathlist_head *merged_paths,
6253 const char *commit_ref_name, struct got_worktree *worktree,
6254 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6255 struct got_object_id *commit_id, struct got_repository *repo,
6256 got_worktree_checkout_cb progress_cb, void *progress_arg,
6257 got_cancel_cb cancel_cb, void *cancel_arg)
6259 const struct got_error *err;
6260 struct got_reference *commit_ref = NULL;
6261 struct collect_merged_paths_arg cmp_arg;
6262 char *fileindex_path;
6264 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6266 err = get_fileindex_path(&fileindex_path, worktree);
6267 if (err)
6268 return err;
6270 cmp_arg.progress_cb = progress_cb;
6271 cmp_arg.progress_arg = progress_arg;
6272 cmp_arg.merged_paths = merged_paths;
6273 err = merge_files(worktree, fileindex, fileindex_path,
6274 parent_commit_id, commit_id, repo, collect_merged_paths,
6275 &cmp_arg, cancel_cb, cancel_arg);
6276 if (commit_ref)
6277 got_ref_close(commit_ref);
6278 return err;
6281 const struct got_error *
6282 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6283 struct got_worktree *worktree, struct got_fileindex *fileindex,
6284 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6285 struct got_repository *repo,
6286 got_worktree_checkout_cb progress_cb, void *progress_arg,
6287 got_cancel_cb cancel_cb, void *cancel_arg)
6289 const struct got_error *err;
6290 char *commit_ref_name;
6292 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6293 if (err)
6294 return err;
6296 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6297 if (err)
6298 goto done;
6300 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6301 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6302 progress_arg, cancel_cb, cancel_arg);
6303 done:
6304 free(commit_ref_name);
6305 return err;
6308 const struct got_error *
6309 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6310 struct got_worktree *worktree, struct got_fileindex *fileindex,
6311 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6312 struct got_repository *repo,
6313 got_worktree_checkout_cb progress_cb, void *progress_arg,
6314 got_cancel_cb cancel_cb, void *cancel_arg)
6316 const struct got_error *err;
6317 char *commit_ref_name;
6319 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6320 if (err)
6321 return err;
6323 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6324 if (err)
6325 goto done;
6327 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6328 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6329 progress_arg, cancel_cb, cancel_arg);
6330 done:
6331 free(commit_ref_name);
6332 return err;
6335 static const struct got_error *
6336 rebase_commit(struct got_object_id **new_commit_id,
6337 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6338 struct got_worktree *worktree, struct got_fileindex *fileindex,
6339 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6340 const char *new_logmsg, struct got_repository *repo)
6342 const struct got_error *err, *sync_err;
6343 struct got_pathlist_head commitable_paths;
6344 struct collect_commitables_arg cc_arg;
6345 char *fileindex_path = NULL;
6346 struct got_reference *head_ref = NULL;
6347 struct got_object_id *head_commit_id = NULL;
6348 char *logmsg = NULL;
6350 TAILQ_INIT(&commitable_paths);
6351 *new_commit_id = NULL;
6353 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6355 err = get_fileindex_path(&fileindex_path, worktree);
6356 if (err)
6357 return err;
6359 cc_arg.commitable_paths = &commitable_paths;
6360 cc_arg.worktree = worktree;
6361 cc_arg.repo = repo;
6362 cc_arg.have_staged_files = 0;
6364 * If possible get the status of individual files directly to
6365 * avoid crawling the entire work tree once per rebased commit.
6367 * Ideally, merged_paths would contain a list of commitables
6368 * we could use so we could skip worktree_status() entirely.
6369 * However, we would then need carefully keep track of cumulative
6370 * effects of operations such as file additions and deletions
6371 * in 'got histedit -f' (folding multiple commits into one),
6372 * and this extra complexity is not really worth it.
6374 if (merged_paths) {
6375 struct got_pathlist_entry *pe;
6376 TAILQ_FOREACH(pe, merged_paths, entry) {
6377 err = worktree_status(worktree, pe->path, fileindex,
6378 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6379 0);
6380 if (err)
6381 goto done;
6383 } else {
6384 err = worktree_status(worktree, "", fileindex, repo,
6385 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6386 if (err)
6387 goto done;
6390 if (TAILQ_EMPTY(&commitable_paths)) {
6391 /* No-op change; commit will be elided. */
6392 err = got_ref_delete(commit_ref, repo);
6393 if (err)
6394 goto done;
6395 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6396 goto done;
6399 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6400 if (err)
6401 goto done;
6403 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6404 if (err)
6405 goto done;
6407 if (new_logmsg) {
6408 logmsg = strdup(new_logmsg);
6409 if (logmsg == NULL) {
6410 err = got_error_from_errno("strdup");
6411 goto done;
6413 } else {
6414 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6415 if (err)
6416 goto done;
6419 /* NB: commit_worktree will call free(logmsg) */
6420 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6421 NULL, worktree, got_object_commit_get_author(orig_commit),
6422 got_object_commit_get_committer(orig_commit),
6423 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6424 if (err)
6425 goto done;
6427 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6428 if (err)
6429 goto done;
6431 err = got_ref_delete(commit_ref, repo);
6432 if (err)
6433 goto done;
6435 err = update_fileindex_after_commit(worktree, &commitable_paths,
6436 *new_commit_id, fileindex, 0);
6437 sync_err = sync_fileindex(fileindex, fileindex_path);
6438 if (sync_err && err == NULL)
6439 err = sync_err;
6440 done:
6441 free(fileindex_path);
6442 free(head_commit_id);
6443 if (head_ref)
6444 got_ref_close(head_ref);
6445 if (err) {
6446 free(*new_commit_id);
6447 *new_commit_id = NULL;
6449 return err;
6452 const struct got_error *
6453 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6454 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6455 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6456 struct got_commit_object *orig_commit,
6457 struct got_object_id *orig_commit_id, struct got_repository *repo)
6459 const struct got_error *err;
6460 char *commit_ref_name;
6461 struct got_reference *commit_ref = NULL;
6462 struct got_object_id *commit_id = NULL;
6464 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6465 if (err)
6466 return err;
6468 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6469 if (err)
6470 goto done;
6471 err = got_ref_resolve(&commit_id, repo, commit_ref);
6472 if (err)
6473 goto done;
6474 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6475 err = got_error(GOT_ERR_REBASE_COMMITID);
6476 goto done;
6479 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6480 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6481 done:
6482 if (commit_ref)
6483 got_ref_close(commit_ref);
6484 free(commit_ref_name);
6485 free(commit_id);
6486 return err;
6489 const struct got_error *
6490 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6491 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6492 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6493 struct got_commit_object *orig_commit,
6494 struct got_object_id *orig_commit_id, const char *new_logmsg,
6495 struct got_repository *repo)
6497 const struct got_error *err;
6498 char *commit_ref_name;
6499 struct got_reference *commit_ref = NULL;
6501 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6502 if (err)
6503 return err;
6505 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6506 if (err)
6507 goto done;
6509 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6510 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6511 done:
6512 if (commit_ref)
6513 got_ref_close(commit_ref);
6514 free(commit_ref_name);
6515 return err;
6518 const struct got_error *
6519 got_worktree_rebase_postpone(struct got_worktree *worktree,
6520 struct got_fileindex *fileindex)
6522 if (fileindex)
6523 got_fileindex_free(fileindex);
6524 return lock_worktree(worktree, LOCK_SH);
6527 static const struct got_error *
6528 delete_ref(const char *name, struct got_repository *repo)
6530 const struct got_error *err;
6531 struct got_reference *ref;
6533 err = got_ref_open(&ref, repo, name, 0);
6534 if (err) {
6535 if (err->code == GOT_ERR_NOT_REF)
6536 return NULL;
6537 return err;
6540 err = got_ref_delete(ref, repo);
6541 got_ref_close(ref);
6542 return err;
6545 static const struct got_error *
6546 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6548 const struct got_error *err;
6549 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6550 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6552 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6553 if (err)
6554 goto done;
6555 err = delete_ref(tmp_branch_name, repo);
6556 if (err)
6557 goto done;
6559 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6560 if (err)
6561 goto done;
6562 err = delete_ref(new_base_branch_ref_name, repo);
6563 if (err)
6564 goto done;
6566 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6567 if (err)
6568 goto done;
6569 err = delete_ref(branch_ref_name, repo);
6570 if (err)
6571 goto done;
6573 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6574 if (err)
6575 goto done;
6576 err = delete_ref(commit_ref_name, repo);
6577 if (err)
6578 goto done;
6580 done:
6581 free(tmp_branch_name);
6582 free(new_base_branch_ref_name);
6583 free(branch_ref_name);
6584 free(commit_ref_name);
6585 return err;
6588 const struct got_error *
6589 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6590 struct got_object_id *new_commit_id, struct got_repository *repo)
6592 const struct got_error *err;
6593 struct got_reference *ref = NULL;
6594 struct got_object_id *old_commit_id = NULL;
6595 const char *branch_name = NULL;
6596 char *new_id_str = NULL;
6597 char *refname = NULL;
6599 branch_name = got_ref_get_name(branch);
6600 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6601 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6602 branch_name += 11;
6604 err = got_object_id_str(&new_id_str, new_commit_id);
6605 if (err)
6606 return err;
6608 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6609 new_id_str) == -1) {
6610 err = got_error_from_errno("asprintf");
6611 goto done;
6614 err = got_ref_resolve(&old_commit_id, repo, branch);
6615 if (err)
6616 goto done;
6618 err = got_ref_alloc(&ref, refname, old_commit_id);
6619 if (err)
6620 goto done;
6622 err = got_ref_write(ref, repo);
6623 done:
6624 free(new_id_str);
6625 free(refname);
6626 free(old_commit_id);
6627 if (ref)
6628 got_ref_close(ref);
6629 return err;
6632 const struct got_error *
6633 got_worktree_rebase_complete(struct got_worktree *worktree,
6634 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6635 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6636 struct got_repository *repo, int create_backup)
6638 const struct got_error *err, *unlockerr, *sync_err;
6639 struct got_object_id *new_head_commit_id = NULL;
6640 char *fileindex_path = NULL;
6642 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6643 if (err)
6644 return err;
6646 if (create_backup) {
6647 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6648 rebased_branch, new_head_commit_id, repo);
6649 if (err)
6650 goto done;
6653 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6654 if (err)
6655 goto done;
6657 err = got_ref_write(rebased_branch, repo);
6658 if (err)
6659 goto done;
6661 err = got_worktree_set_head_ref(worktree, rebased_branch);
6662 if (err)
6663 goto done;
6665 err = delete_rebase_refs(worktree, repo);
6666 if (err)
6667 goto done;
6669 err = get_fileindex_path(&fileindex_path, worktree);
6670 if (err)
6671 goto done;
6672 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6673 sync_err = sync_fileindex(fileindex, fileindex_path);
6674 if (sync_err && err == NULL)
6675 err = sync_err;
6676 done:
6677 got_fileindex_free(fileindex);
6678 free(fileindex_path);
6679 free(new_head_commit_id);
6680 unlockerr = lock_worktree(worktree, LOCK_SH);
6681 if (unlockerr && err == NULL)
6682 err = unlockerr;
6683 return err;
6686 const struct got_error *
6687 got_worktree_rebase_abort(struct got_worktree *worktree,
6688 struct got_fileindex *fileindex, struct got_repository *repo,
6689 struct got_reference *new_base_branch,
6690 got_worktree_checkout_cb progress_cb, void *progress_arg)
6692 const struct got_error *err, *unlockerr, *sync_err;
6693 struct got_reference *resolved = NULL;
6694 struct got_object_id *commit_id = NULL;
6695 char *fileindex_path = NULL;
6696 struct revert_file_args rfa;
6697 struct got_object_id *tree_id = NULL;
6699 err = lock_worktree(worktree, LOCK_EX);
6700 if (err)
6701 return err;
6703 err = got_ref_open(&resolved, repo,
6704 got_ref_get_symref_target(new_base_branch), 0);
6705 if (err)
6706 goto done;
6708 err = got_worktree_set_head_ref(worktree, resolved);
6709 if (err)
6710 goto done;
6713 * XXX commits to the base branch could have happened while
6714 * we were busy rebasing; should we store the original commit ID
6715 * when rebase begins and read it back here?
6717 err = got_ref_resolve(&commit_id, repo, resolved);
6718 if (err)
6719 goto done;
6721 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6722 if (err)
6723 goto done;
6725 err = got_object_id_by_path(&tree_id, repo,
6726 worktree->base_commit_id, worktree->path_prefix);
6727 if (err)
6728 goto done;
6730 err = delete_rebase_refs(worktree, repo);
6731 if (err)
6732 goto done;
6734 err = get_fileindex_path(&fileindex_path, worktree);
6735 if (err)
6736 goto done;
6738 rfa.worktree = worktree;
6739 rfa.fileindex = fileindex;
6740 rfa.progress_cb = progress_cb;
6741 rfa.progress_arg = progress_arg;
6742 rfa.patch_cb = NULL;
6743 rfa.patch_arg = NULL;
6744 rfa.repo = repo;
6745 rfa.unlink_added_files = 0;
6746 err = worktree_status(worktree, "", fileindex, repo,
6747 revert_file, &rfa, NULL, NULL, 1, 0);
6748 if (err)
6749 goto sync;
6751 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6752 repo, progress_cb, progress_arg, NULL, NULL);
6753 sync:
6754 sync_err = sync_fileindex(fileindex, fileindex_path);
6755 if (sync_err && err == NULL)
6756 err = sync_err;
6757 done:
6758 got_ref_close(resolved);
6759 free(tree_id);
6760 free(commit_id);
6761 if (fileindex)
6762 got_fileindex_free(fileindex);
6763 free(fileindex_path);
6765 unlockerr = lock_worktree(worktree, LOCK_SH);
6766 if (unlockerr && err == NULL)
6767 err = unlockerr;
6768 return err;
6771 const struct got_error *
6772 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6773 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6774 struct got_fileindex **fileindex, struct got_worktree *worktree,
6775 struct got_repository *repo)
6777 const struct got_error *err = NULL;
6778 char *tmp_branch_name = NULL;
6779 char *branch_ref_name = NULL;
6780 char *base_commit_ref_name = NULL;
6781 char *fileindex_path = NULL;
6782 struct check_rebase_ok_arg ok_arg;
6783 struct got_reference *wt_branch = NULL;
6784 struct got_reference *base_commit_ref = NULL;
6786 *tmp_branch = NULL;
6787 *branch_ref = NULL;
6788 *base_commit_id = NULL;
6789 *fileindex = NULL;
6791 err = lock_worktree(worktree, LOCK_EX);
6792 if (err)
6793 return err;
6795 err = open_fileindex(fileindex, &fileindex_path, worktree);
6796 if (err)
6797 goto done;
6799 ok_arg.worktree = worktree;
6800 ok_arg.repo = repo;
6801 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6802 &ok_arg);
6803 if (err)
6804 goto done;
6806 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6807 if (err)
6808 goto done;
6810 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6811 if (err)
6812 goto done;
6814 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6815 worktree);
6816 if (err)
6817 goto done;
6819 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6820 0);
6821 if (err)
6822 goto done;
6824 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6825 if (err)
6826 goto done;
6828 err = got_ref_write(*branch_ref, repo);
6829 if (err)
6830 goto done;
6832 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6833 worktree->base_commit_id);
6834 if (err)
6835 goto done;
6836 err = got_ref_write(base_commit_ref, repo);
6837 if (err)
6838 goto done;
6839 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6840 if (*base_commit_id == NULL) {
6841 err = got_error_from_errno("got_object_id_dup");
6842 goto done;
6845 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6846 worktree->base_commit_id);
6847 if (err)
6848 goto done;
6849 err = got_ref_write(*tmp_branch, repo);
6850 if (err)
6851 goto done;
6853 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6854 if (err)
6855 goto done;
6856 done:
6857 free(fileindex_path);
6858 free(tmp_branch_name);
6859 free(branch_ref_name);
6860 free(base_commit_ref_name);
6861 if (wt_branch)
6862 got_ref_close(wt_branch);
6863 if (err) {
6864 if (*branch_ref) {
6865 got_ref_close(*branch_ref);
6866 *branch_ref = NULL;
6868 if (*tmp_branch) {
6869 got_ref_close(*tmp_branch);
6870 *tmp_branch = NULL;
6872 free(*base_commit_id);
6873 if (*fileindex) {
6874 got_fileindex_free(*fileindex);
6875 *fileindex = NULL;
6877 lock_worktree(worktree, LOCK_SH);
6879 return err;
6882 const struct got_error *
6883 got_worktree_histedit_postpone(struct got_worktree *worktree,
6884 struct got_fileindex *fileindex)
6886 if (fileindex)
6887 got_fileindex_free(fileindex);
6888 return lock_worktree(worktree, LOCK_SH);
6891 const struct got_error *
6892 got_worktree_histedit_in_progress(int *in_progress,
6893 struct got_worktree *worktree)
6895 const struct got_error *err;
6896 char *tmp_branch_name = NULL;
6898 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6899 if (err)
6900 return err;
6902 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6903 free(tmp_branch_name);
6904 return NULL;
6907 const struct got_error *
6908 got_worktree_histedit_continue(struct got_object_id **commit_id,
6909 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6910 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6911 struct got_worktree *worktree, struct got_repository *repo)
6913 const struct got_error *err;
6914 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6915 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6916 struct got_reference *commit_ref = NULL;
6917 struct got_reference *base_commit_ref = NULL;
6918 char *fileindex_path = NULL;
6919 int have_staged_files = 0;
6921 *commit_id = NULL;
6922 *tmp_branch = NULL;
6923 *base_commit_id = NULL;
6924 *fileindex = NULL;
6926 err = lock_worktree(worktree, LOCK_EX);
6927 if (err)
6928 return err;
6930 err = open_fileindex(fileindex, &fileindex_path, worktree);
6931 if (err)
6932 goto done;
6934 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6935 &have_staged_files);
6936 if (err && err->code != GOT_ERR_CANCELLED)
6937 goto done;
6938 if (have_staged_files) {
6939 err = got_error(GOT_ERR_STAGED_PATHS);
6940 goto done;
6943 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6944 if (err)
6945 goto done;
6947 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6948 if (err)
6949 goto done;
6951 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6952 if (err)
6953 goto done;
6955 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6956 worktree);
6957 if (err)
6958 goto done;
6960 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6961 if (err)
6962 goto done;
6964 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6965 if (err)
6966 goto done;
6967 err = got_ref_resolve(commit_id, repo, commit_ref);
6968 if (err)
6969 goto done;
6971 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6972 if (err)
6973 goto done;
6974 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6975 if (err)
6976 goto done;
6978 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6979 if (err)
6980 goto done;
6981 done:
6982 free(commit_ref_name);
6983 free(branch_ref_name);
6984 free(fileindex_path);
6985 if (commit_ref)
6986 got_ref_close(commit_ref);
6987 if (base_commit_ref)
6988 got_ref_close(base_commit_ref);
6989 if (err) {
6990 free(*commit_id);
6991 *commit_id = NULL;
6992 free(*base_commit_id);
6993 *base_commit_id = NULL;
6994 if (*tmp_branch) {
6995 got_ref_close(*tmp_branch);
6996 *tmp_branch = NULL;
6998 if (*fileindex) {
6999 got_fileindex_free(*fileindex);
7000 *fileindex = NULL;
7002 lock_worktree(worktree, LOCK_EX);
7004 return err;
7007 static const struct got_error *
7008 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7010 const struct got_error *err;
7011 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7012 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7014 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7015 if (err)
7016 goto done;
7017 err = delete_ref(tmp_branch_name, repo);
7018 if (err)
7019 goto done;
7021 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7022 worktree);
7023 if (err)
7024 goto done;
7025 err = delete_ref(base_commit_ref_name, repo);
7026 if (err)
7027 goto done;
7029 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7030 if (err)
7031 goto done;
7032 err = delete_ref(branch_ref_name, repo);
7033 if (err)
7034 goto done;
7036 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7037 if (err)
7038 goto done;
7039 err = delete_ref(commit_ref_name, repo);
7040 if (err)
7041 goto done;
7042 done:
7043 free(tmp_branch_name);
7044 free(base_commit_ref_name);
7045 free(branch_ref_name);
7046 free(commit_ref_name);
7047 return err;
7050 const struct got_error *
7051 got_worktree_histedit_abort(struct got_worktree *worktree,
7052 struct got_fileindex *fileindex, struct got_repository *repo,
7053 struct got_reference *branch, struct got_object_id *base_commit_id,
7054 got_worktree_checkout_cb progress_cb, void *progress_arg)
7056 const struct got_error *err, *unlockerr, *sync_err;
7057 struct got_reference *resolved = NULL;
7058 char *fileindex_path = NULL;
7059 struct got_object_id *tree_id = NULL;
7060 struct revert_file_args rfa;
7062 err = lock_worktree(worktree, LOCK_EX);
7063 if (err)
7064 return err;
7066 err = got_ref_open(&resolved, repo,
7067 got_ref_get_symref_target(branch), 0);
7068 if (err)
7069 goto done;
7071 err = got_worktree_set_head_ref(worktree, resolved);
7072 if (err)
7073 goto done;
7075 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7076 if (err)
7077 goto done;
7079 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
7080 worktree->path_prefix);
7081 if (err)
7082 goto done;
7084 err = delete_histedit_refs(worktree, repo);
7085 if (err)
7086 goto done;
7088 err = get_fileindex_path(&fileindex_path, worktree);
7089 if (err)
7090 goto done;
7092 rfa.worktree = worktree;
7093 rfa.fileindex = fileindex;
7094 rfa.progress_cb = progress_cb;
7095 rfa.progress_arg = progress_arg;
7096 rfa.patch_cb = NULL;
7097 rfa.patch_arg = NULL;
7098 rfa.repo = repo;
7099 rfa.unlink_added_files = 0;
7100 err = worktree_status(worktree, "", fileindex, repo,
7101 revert_file, &rfa, NULL, NULL, 1, 0);
7102 if (err)
7103 goto sync;
7105 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7106 repo, progress_cb, progress_arg, NULL, NULL);
7107 sync:
7108 sync_err = sync_fileindex(fileindex, fileindex_path);
7109 if (sync_err && err == NULL)
7110 err = sync_err;
7111 done:
7112 got_ref_close(resolved);
7113 free(tree_id);
7114 free(fileindex_path);
7116 unlockerr = lock_worktree(worktree, LOCK_SH);
7117 if (unlockerr && err == NULL)
7118 err = unlockerr;
7119 return err;
7122 const struct got_error *
7123 got_worktree_histedit_complete(struct got_worktree *worktree,
7124 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7125 struct got_reference *edited_branch, struct got_repository *repo)
7127 const struct got_error *err, *unlockerr, *sync_err;
7128 struct got_object_id *new_head_commit_id = NULL;
7129 struct got_reference *resolved = NULL;
7130 char *fileindex_path = NULL;
7132 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7133 if (err)
7134 return err;
7136 err = got_ref_open(&resolved, repo,
7137 got_ref_get_symref_target(edited_branch), 0);
7138 if (err)
7139 goto done;
7141 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7142 resolved, new_head_commit_id, repo);
7143 if (err)
7144 goto done;
7146 err = got_ref_change_ref(resolved, new_head_commit_id);
7147 if (err)
7148 goto done;
7150 err = got_ref_write(resolved, repo);
7151 if (err)
7152 goto done;
7154 err = got_worktree_set_head_ref(worktree, resolved);
7155 if (err)
7156 goto done;
7158 err = delete_histedit_refs(worktree, repo);
7159 if (err)
7160 goto done;
7162 err = get_fileindex_path(&fileindex_path, worktree);
7163 if (err)
7164 goto done;
7165 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7166 sync_err = sync_fileindex(fileindex, fileindex_path);
7167 if (sync_err && err == NULL)
7168 err = sync_err;
7169 done:
7170 got_fileindex_free(fileindex);
7171 free(fileindex_path);
7172 free(new_head_commit_id);
7173 unlockerr = lock_worktree(worktree, LOCK_SH);
7174 if (unlockerr && err == NULL)
7175 err = unlockerr;
7176 return err;
7179 const struct got_error *
7180 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7181 struct got_object_id *commit_id, struct got_repository *repo)
7183 const struct got_error *err;
7184 char *commit_ref_name;
7186 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7187 if (err)
7188 return err;
7190 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7191 if (err)
7192 goto done;
7194 err = delete_ref(commit_ref_name, repo);
7195 done:
7196 free(commit_ref_name);
7197 return err;
7200 const struct got_error *
7201 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7202 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7203 struct got_worktree *worktree, const char *refname,
7204 struct got_repository *repo)
7206 const struct got_error *err = NULL;
7207 char *fileindex_path = NULL;
7208 struct check_rebase_ok_arg ok_arg;
7210 *fileindex = NULL;
7211 *branch_ref = NULL;
7212 *base_branch_ref = NULL;
7214 err = lock_worktree(worktree, LOCK_EX);
7215 if (err)
7216 return err;
7218 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7219 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7220 "cannot integrate a branch into itself; "
7221 "update -b or different branch name required");
7222 goto done;
7225 err = open_fileindex(fileindex, &fileindex_path, worktree);
7226 if (err)
7227 goto done;
7229 /* Preconditions are the same as for rebase. */
7230 ok_arg.worktree = worktree;
7231 ok_arg.repo = repo;
7232 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7233 &ok_arg);
7234 if (err)
7235 goto done;
7237 err = got_ref_open(branch_ref, repo, refname, 1);
7238 if (err)
7239 goto done;
7241 err = got_ref_open(base_branch_ref, repo,
7242 got_worktree_get_head_ref_name(worktree), 1);
7243 done:
7244 if (err) {
7245 if (*branch_ref) {
7246 got_ref_close(*branch_ref);
7247 *branch_ref = NULL;
7249 if (*base_branch_ref) {
7250 got_ref_close(*base_branch_ref);
7251 *base_branch_ref = NULL;
7253 if (*fileindex) {
7254 got_fileindex_free(*fileindex);
7255 *fileindex = NULL;
7257 lock_worktree(worktree, LOCK_SH);
7259 return err;
7262 const struct got_error *
7263 got_worktree_integrate_continue(struct got_worktree *worktree,
7264 struct got_fileindex *fileindex, struct got_repository *repo,
7265 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7266 got_worktree_checkout_cb progress_cb, void *progress_arg,
7267 got_cancel_cb cancel_cb, void *cancel_arg)
7269 const struct got_error *err = NULL, *sync_err, *unlockerr;
7270 char *fileindex_path = NULL;
7271 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7273 err = get_fileindex_path(&fileindex_path, worktree);
7274 if (err)
7275 goto done;
7277 err = got_ref_resolve(&commit_id, repo, branch_ref);
7278 if (err)
7279 goto done;
7281 err = got_object_id_by_path(&tree_id, repo, commit_id,
7282 worktree->path_prefix);
7283 if (err)
7284 goto done;
7286 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7287 if (err)
7288 goto done;
7290 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7291 progress_cb, progress_arg, cancel_cb, cancel_arg);
7292 if (err)
7293 goto sync;
7295 err = got_ref_change_ref(base_branch_ref, commit_id);
7296 if (err)
7297 goto sync;
7299 err = got_ref_write(base_branch_ref, repo);
7300 if (err)
7301 goto sync;
7303 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7304 sync:
7305 sync_err = sync_fileindex(fileindex, fileindex_path);
7306 if (sync_err && err == NULL)
7307 err = sync_err;
7309 done:
7310 unlockerr = got_ref_unlock(branch_ref);
7311 if (unlockerr && err == NULL)
7312 err = unlockerr;
7313 got_ref_close(branch_ref);
7315 unlockerr = got_ref_unlock(base_branch_ref);
7316 if (unlockerr && err == NULL)
7317 err = unlockerr;
7318 got_ref_close(base_branch_ref);
7320 got_fileindex_free(fileindex);
7321 free(fileindex_path);
7322 free(tree_id);
7324 unlockerr = lock_worktree(worktree, LOCK_SH);
7325 if (unlockerr && err == NULL)
7326 err = unlockerr;
7327 return err;
7330 const struct got_error *
7331 got_worktree_integrate_abort(struct got_worktree *worktree,
7332 struct got_fileindex *fileindex, struct got_repository *repo,
7333 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7335 const struct got_error *err = NULL, *unlockerr = NULL;
7337 got_fileindex_free(fileindex);
7339 err = lock_worktree(worktree, LOCK_SH);
7341 unlockerr = got_ref_unlock(branch_ref);
7342 if (unlockerr && err == NULL)
7343 err = unlockerr;
7344 got_ref_close(branch_ref);
7346 unlockerr = got_ref_unlock(base_branch_ref);
7347 if (unlockerr && err == NULL)
7348 err = unlockerr;
7349 got_ref_close(base_branch_ref);
7351 return err;
7354 const struct got_error *
7355 got_worktree_merge_postpone(struct got_worktree *worktree,
7356 struct got_fileindex *fileindex)
7358 const struct got_error *err, *sync_err;
7359 char *fileindex_path = NULL;
7361 err = get_fileindex_path(&fileindex_path, worktree);
7362 if (err)
7363 goto done;
7365 sync_err = sync_fileindex(fileindex, fileindex_path);
7367 err = lock_worktree(worktree, LOCK_SH);
7368 if (sync_err && err == NULL)
7369 err = sync_err;
7370 done:
7371 got_fileindex_free(fileindex);
7372 free(fileindex_path);
7373 return err;
7376 static const struct got_error *
7377 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7379 const struct got_error *err;
7380 char *branch_refname = NULL, *commit_refname = NULL;
7382 err = get_merge_branch_ref_name(&branch_refname, worktree);
7383 if (err)
7384 goto done;
7385 err = delete_ref(branch_refname, repo);
7386 if (err)
7387 goto done;
7389 err = get_merge_commit_ref_name(&commit_refname, worktree);
7390 if (err)
7391 goto done;
7392 err = delete_ref(commit_refname, repo);
7393 if (err)
7394 goto done;
7396 done:
7397 free(branch_refname);
7398 free(commit_refname);
7399 return err;
7402 struct merge_commit_msg_arg {
7403 struct got_worktree *worktree;
7404 const char *branch_name;
7407 static const struct got_error *
7408 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths, char **logmsg,
7409 void *arg)
7411 struct merge_commit_msg_arg *a = arg;
7413 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7414 got_worktree_get_head_ref_name(a->worktree)) == -1)
7415 return got_error_from_errno("asprintf");
7417 return NULL;
7421 const struct got_error *
7422 got_worktree_merge_branch(struct got_worktree *worktree,
7423 struct got_fileindex *fileindex,
7424 struct got_object_id *yca_commit_id,
7425 struct got_object_id *branch_tip,
7426 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7427 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7429 const struct got_error *err;
7430 char *fileindex_path = NULL;
7432 err = get_fileindex_path(&fileindex_path, worktree);
7433 if (err)
7434 goto done;
7436 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7437 worktree);
7438 if (err)
7439 goto done;
7441 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7442 branch_tip, repo, progress_cb, progress_arg,
7443 cancel_cb, cancel_arg);
7444 done:
7445 free(fileindex_path);
7446 return err;
7449 const struct got_error *
7450 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7451 struct got_worktree *worktree, struct got_fileindex *fileindex,
7452 const char *author, const char *committer, int allow_bad_symlinks,
7453 struct got_object_id *branch_tip, const char *branch_name,
7454 struct got_repository *repo,
7455 got_worktree_status_cb status_cb, void *status_arg)
7458 const struct got_error *err = NULL, *sync_err;
7459 struct got_pathlist_head commitable_paths;
7460 struct collect_commitables_arg cc_arg;
7461 struct got_pathlist_entry *pe;
7462 struct got_reference *head_ref = NULL;
7463 struct got_object_id *head_commit_id = NULL;
7464 int have_staged_files = 0;
7465 struct merge_commit_msg_arg mcm_arg;
7466 char *fileindex_path = NULL;
7468 *new_commit_id = NULL;
7470 TAILQ_INIT(&commitable_paths);
7472 err = get_fileindex_path(&fileindex_path, worktree);
7473 if (err)
7474 goto done;
7476 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7477 if (err)
7478 goto done;
7480 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7481 if (err)
7482 goto done;
7484 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7485 &have_staged_files);
7486 if (err && err->code != GOT_ERR_CANCELLED)
7487 goto done;
7488 if (have_staged_files) {
7489 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7490 goto done;
7493 cc_arg.commitable_paths = &commitable_paths;
7494 cc_arg.worktree = worktree;
7495 cc_arg.fileindex = fileindex;
7496 cc_arg.repo = repo;
7497 cc_arg.have_staged_files = have_staged_files;
7498 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7499 err = worktree_status(worktree, "", fileindex, repo,
7500 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7501 if (err)
7502 goto done;
7504 if (TAILQ_EMPTY(&commitable_paths)) {
7505 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7506 "merge of %s cannot proceed", branch_name);
7507 goto done;
7510 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7511 struct got_commitable *ct = pe->data;
7512 const char *ct_path = ct->in_repo_path;
7514 while (ct_path[0] == '/')
7515 ct_path++;
7516 err = check_out_of_date(ct_path, ct->status,
7517 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7518 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7519 if (err)
7520 goto done;
7524 mcm_arg.worktree = worktree;
7525 mcm_arg.branch_name = branch_name;
7526 err = commit_worktree(new_commit_id, &commitable_paths,
7527 head_commit_id, branch_tip, worktree, author, committer,
7528 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7529 if (err)
7530 goto done;
7532 err = update_fileindex_after_commit(worktree, &commitable_paths,
7533 *new_commit_id, fileindex, have_staged_files);
7534 sync_err = sync_fileindex(fileindex, fileindex_path);
7535 if (sync_err && err == NULL)
7536 err = sync_err;
7537 done:
7538 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7539 struct got_commitable *ct = pe->data;
7540 free_commitable(ct);
7542 got_pathlist_free(&commitable_paths);
7543 free(fileindex_path);
7544 return err;
7547 const struct got_error *
7548 got_worktree_merge_complete(struct got_worktree *worktree,
7549 struct got_fileindex *fileindex, struct got_repository *repo)
7551 const struct got_error *err, *unlockerr, *sync_err;
7552 char *fileindex_path = NULL;
7554 err = delete_merge_refs(worktree, repo);
7555 if (err)
7556 goto done;
7558 err = get_fileindex_path(&fileindex_path, worktree);
7559 if (err)
7560 goto done;
7561 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7562 sync_err = sync_fileindex(fileindex, fileindex_path);
7563 if (sync_err && err == NULL)
7564 err = sync_err;
7565 done:
7566 got_fileindex_free(fileindex);
7567 free(fileindex_path);
7568 unlockerr = lock_worktree(worktree, LOCK_SH);
7569 if (unlockerr && err == NULL)
7570 err = unlockerr;
7571 return err;
7574 const struct got_error *
7575 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7576 struct got_repository *repo)
7578 const struct got_error *err;
7579 char *branch_refname = NULL;
7580 struct got_reference *branch_ref = NULL;
7582 *in_progress = 0;
7584 err = get_merge_branch_ref_name(&branch_refname, worktree);
7585 if (err)
7586 return err;
7587 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7588 free(branch_refname);
7589 if (err) {
7590 if (err->code != GOT_ERR_NOT_REF)
7591 return err;
7592 } else
7593 *in_progress = 1;
7595 return NULL;
7598 const struct got_error *got_worktree_merge_prepare(
7599 struct got_fileindex **fileindex, struct got_worktree *worktree,
7600 struct got_reference *branch, struct got_repository *repo)
7602 const struct got_error *err = NULL;
7603 char *fileindex_path = NULL;
7604 char *branch_refname = NULL, *commit_refname = NULL;
7605 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
7606 struct got_reference *commit_ref = NULL;
7607 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
7608 struct check_rebase_ok_arg ok_arg;
7610 *fileindex = NULL;
7612 err = lock_worktree(worktree, LOCK_EX);
7613 if (err)
7614 return err;
7616 err = open_fileindex(fileindex, &fileindex_path, worktree);
7617 if (err)
7618 goto done;
7620 /* Preconditions are the same as for rebase. */
7621 ok_arg.worktree = worktree;
7622 ok_arg.repo = repo;
7623 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7624 &ok_arg);
7625 if (err)
7626 goto done;
7628 err = get_merge_branch_ref_name(&branch_refname, worktree);
7629 if (err)
7630 return err;
7632 err = get_merge_commit_ref_name(&commit_refname, worktree);
7633 if (err)
7634 return err;
7636 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7637 0);
7638 if (err)
7639 goto done;
7641 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
7642 if (err)
7643 goto done;
7645 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
7646 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
7647 goto done;
7650 err = got_ref_resolve(&branch_tip, repo, branch);
7651 if (err)
7652 goto done;
7654 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
7655 if (err)
7656 goto done;
7657 err = got_ref_write(branch_ref, repo);
7658 if (err)
7659 goto done;
7661 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
7662 if (err)
7663 goto done;
7664 err = got_ref_write(commit_ref, repo);
7665 if (err)
7666 goto done;
7668 done:
7669 free(branch_refname);
7670 free(commit_refname);
7671 free(fileindex_path);
7672 if (branch_ref)
7673 got_ref_close(branch_ref);
7674 if (commit_ref)
7675 got_ref_close(commit_ref);
7676 if (wt_branch)
7677 got_ref_close(wt_branch);
7678 free(wt_branch_tip);
7679 if (err) {
7680 if (*fileindex) {
7681 got_fileindex_free(*fileindex);
7682 *fileindex = NULL;
7684 lock_worktree(worktree, LOCK_SH);
7686 return err;
7689 const struct got_error *
7690 got_worktree_merge_continue(char **branch_name,
7691 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
7692 struct got_worktree *worktree, struct got_repository *repo)
7694 const struct got_error *err;
7695 char *commit_refname = NULL, *branch_refname = NULL;
7696 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
7697 char *fileindex_path = NULL;
7698 int have_staged_files = 0;
7700 *branch_name = NULL;
7701 *branch_tip = NULL;
7702 *fileindex = NULL;
7704 err = lock_worktree(worktree, LOCK_EX);
7705 if (err)
7706 return err;
7708 err = open_fileindex(fileindex, &fileindex_path, worktree);
7709 if (err)
7710 goto done;
7712 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7713 &have_staged_files);
7714 if (err && err->code != GOT_ERR_CANCELLED)
7715 goto done;
7716 if (have_staged_files) {
7717 err = got_error(GOT_ERR_STAGED_PATHS);
7718 goto done;
7721 err = get_merge_branch_ref_name(&branch_refname, worktree);
7722 if (err)
7723 goto done;
7725 err = get_merge_commit_ref_name(&commit_refname, worktree);
7726 if (err)
7727 goto done;
7729 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7730 if (err)
7731 goto done;
7733 if (!got_ref_is_symbolic(branch_ref)) {
7734 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7735 "%s is not a symbolic reference",
7736 got_ref_get_name(branch_ref));
7737 goto done;
7739 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
7740 if (*branch_name == NULL) {
7741 err = got_error_from_errno("strdup");
7742 goto done;
7745 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
7746 if (err)
7747 goto done;
7749 err = got_ref_resolve(branch_tip, repo, commit_ref);
7750 if (err)
7751 goto done;
7752 done:
7753 free(commit_refname);
7754 free(branch_refname);
7755 free(fileindex_path);
7756 if (commit_ref)
7757 got_ref_close(commit_ref);
7758 if (branch_ref)
7759 got_ref_close(branch_ref);
7760 if (err) {
7761 if (*branch_name) {
7762 free(*branch_name);
7763 *branch_name = NULL;
7765 free(*branch_tip);
7766 *branch_tip = NULL;
7767 if (*fileindex) {
7768 got_fileindex_free(*fileindex);
7769 *fileindex = NULL;
7771 lock_worktree(worktree, LOCK_SH);
7773 return err;
7776 const struct got_error *
7777 got_worktree_merge_abort(struct got_worktree *worktree,
7778 struct got_fileindex *fileindex, struct got_repository *repo,
7779 got_worktree_checkout_cb progress_cb, void *progress_arg)
7781 const struct got_error *err, *unlockerr, *sync_err;
7782 struct got_object_id *commit_id = NULL;
7783 char *fileindex_path = NULL;
7784 struct revert_file_args rfa;
7785 struct got_object_id *tree_id = NULL;
7787 err = got_object_id_by_path(&tree_id, repo,
7788 worktree->base_commit_id, worktree->path_prefix);
7789 if (err)
7790 goto done;
7792 err = delete_merge_refs(worktree, repo);
7793 if (err)
7794 goto done;
7796 err = get_fileindex_path(&fileindex_path, worktree);
7797 if (err)
7798 goto done;
7800 rfa.worktree = worktree;
7801 rfa.fileindex = fileindex;
7802 rfa.progress_cb = progress_cb;
7803 rfa.progress_arg = progress_arg;
7804 rfa.patch_cb = NULL;
7805 rfa.patch_arg = NULL;
7806 rfa.repo = repo;
7807 rfa.unlink_added_files = 1;
7808 err = worktree_status(worktree, "", fileindex, repo,
7809 revert_file, &rfa, NULL, NULL, 1, 0);
7810 if (err)
7811 goto sync;
7813 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7814 repo, progress_cb, progress_arg, NULL, NULL);
7815 sync:
7816 sync_err = sync_fileindex(fileindex, fileindex_path);
7817 if (sync_err && err == NULL)
7818 err = sync_err;
7819 done:
7820 free(tree_id);
7821 free(commit_id);
7822 if (fileindex)
7823 got_fileindex_free(fileindex);
7824 free(fileindex_path);
7826 unlockerr = lock_worktree(worktree, LOCK_SH);
7827 if (unlockerr && err == NULL)
7828 err = unlockerr;
7829 return err;
7832 struct check_stage_ok_arg {
7833 struct got_object_id *head_commit_id;
7834 struct got_worktree *worktree;
7835 struct got_fileindex *fileindex;
7836 struct got_repository *repo;
7837 int have_changes;
7840 const struct got_error *
7841 check_stage_ok(void *arg, unsigned char status,
7842 unsigned char staged_status, const char *relpath,
7843 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7844 struct got_object_id *commit_id, int dirfd, const char *de_name)
7846 struct check_stage_ok_arg *a = arg;
7847 const struct got_error *err = NULL;
7848 struct got_fileindex_entry *ie;
7849 struct got_object_id base_commit_id;
7850 struct got_object_id *base_commit_idp = NULL;
7851 char *in_repo_path = NULL, *p;
7853 if (status == GOT_STATUS_UNVERSIONED ||
7854 status == GOT_STATUS_NO_CHANGE)
7855 return NULL;
7856 if (status == GOT_STATUS_NONEXISTENT)
7857 return got_error_set_errno(ENOENT, relpath);
7859 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7860 if (ie == NULL)
7861 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7863 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7864 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7865 relpath) == -1)
7866 return got_error_from_errno("asprintf");
7868 if (got_fileindex_entry_has_commit(ie)) {
7869 memcpy(base_commit_id.sha1, ie->commit_sha1,
7870 SHA1_DIGEST_LENGTH);
7871 base_commit_idp = &base_commit_id;
7874 if (status == GOT_STATUS_CONFLICT) {
7875 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7876 goto done;
7877 } else if (status != GOT_STATUS_ADD &&
7878 status != GOT_STATUS_MODIFY &&
7879 status != GOT_STATUS_DELETE) {
7880 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7881 goto done;
7884 a->have_changes = 1;
7886 p = in_repo_path;
7887 while (p[0] == '/')
7888 p++;
7889 err = check_out_of_date(p, status, staged_status,
7890 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7891 GOT_ERR_STAGE_OUT_OF_DATE);
7892 done:
7893 free(in_repo_path);
7894 return err;
7897 struct stage_path_arg {
7898 struct got_worktree *worktree;
7899 struct got_fileindex *fileindex;
7900 struct got_repository *repo;
7901 got_worktree_status_cb status_cb;
7902 void *status_arg;
7903 got_worktree_patch_cb patch_cb;
7904 void *patch_arg;
7905 int staged_something;
7906 int allow_bad_symlinks;
7909 static const struct got_error *
7910 stage_path(void *arg, unsigned char status,
7911 unsigned char staged_status, const char *relpath,
7912 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7913 struct got_object_id *commit_id, int dirfd, const char *de_name)
7915 struct stage_path_arg *a = arg;
7916 const struct got_error *err = NULL;
7917 struct got_fileindex_entry *ie;
7918 char *ondisk_path = NULL, *path_content = NULL;
7919 uint32_t stage;
7920 struct got_object_id *new_staged_blob_id = NULL;
7921 struct stat sb;
7923 if (status == GOT_STATUS_UNVERSIONED)
7924 return NULL;
7926 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7927 if (ie == NULL)
7928 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7930 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7931 relpath)== -1)
7932 return got_error_from_errno("asprintf");
7934 switch (status) {
7935 case GOT_STATUS_ADD:
7936 case GOT_STATUS_MODIFY:
7937 /* XXX could sb.st_mode be passed in by our caller? */
7938 if (lstat(ondisk_path, &sb) == -1) {
7939 err = got_error_from_errno2("lstat", ondisk_path);
7940 break;
7942 if (a->patch_cb) {
7943 if (status == GOT_STATUS_ADD) {
7944 int choice = GOT_PATCH_CHOICE_NONE;
7945 err = (*a->patch_cb)(&choice, a->patch_arg,
7946 status, ie->path, NULL, 1, 1);
7947 if (err)
7948 break;
7949 if (choice != GOT_PATCH_CHOICE_YES)
7950 break;
7951 } else {
7952 err = create_patched_content(&path_content, 0,
7953 staged_blob_id ? staged_blob_id : blob_id,
7954 ondisk_path, dirfd, de_name, ie->path,
7955 a->repo, a->patch_cb, a->patch_arg);
7956 if (err || path_content == NULL)
7957 break;
7960 err = got_object_blob_create(&new_staged_blob_id,
7961 path_content ? path_content : ondisk_path, a->repo);
7962 if (err)
7963 break;
7964 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7965 SHA1_DIGEST_LENGTH);
7966 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7967 stage = GOT_FILEIDX_STAGE_ADD;
7968 else
7969 stage = GOT_FILEIDX_STAGE_MODIFY;
7970 got_fileindex_entry_stage_set(ie, stage);
7971 if (S_ISLNK(sb.st_mode)) {
7972 int is_bad_symlink = 0;
7973 if (!a->allow_bad_symlinks) {
7974 char target_path[PATH_MAX];
7975 ssize_t target_len;
7976 target_len = readlink(ondisk_path, target_path,
7977 sizeof(target_path));
7978 if (target_len == -1) {
7979 err = got_error_from_errno2("readlink",
7980 ondisk_path);
7981 break;
7983 err = is_bad_symlink_target(&is_bad_symlink,
7984 target_path, target_len, ondisk_path,
7985 a->worktree->root_path);
7986 if (err)
7987 break;
7988 if (is_bad_symlink) {
7989 err = got_error_path(ondisk_path,
7990 GOT_ERR_BAD_SYMLINK);
7991 break;
7994 if (is_bad_symlink)
7995 got_fileindex_entry_staged_filetype_set(ie,
7996 GOT_FILEIDX_MODE_BAD_SYMLINK);
7997 else
7998 got_fileindex_entry_staged_filetype_set(ie,
7999 GOT_FILEIDX_MODE_SYMLINK);
8000 } else {
8001 got_fileindex_entry_staged_filetype_set(ie,
8002 GOT_FILEIDX_MODE_REGULAR_FILE);
8004 a->staged_something = 1;
8005 if (a->status_cb == NULL)
8006 break;
8007 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8008 get_staged_status(ie), relpath, blob_id,
8009 new_staged_blob_id, NULL, dirfd, de_name);
8010 break;
8011 case GOT_STATUS_DELETE:
8012 if (staged_status == GOT_STATUS_DELETE)
8013 break;
8014 if (a->patch_cb) {
8015 int choice = GOT_PATCH_CHOICE_NONE;
8016 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8017 ie->path, NULL, 1, 1);
8018 if (err)
8019 break;
8020 if (choice == GOT_PATCH_CHOICE_NO)
8021 break;
8022 if (choice != GOT_PATCH_CHOICE_YES) {
8023 err = got_error(GOT_ERR_PATCH_CHOICE);
8024 break;
8027 stage = GOT_FILEIDX_STAGE_DELETE;
8028 got_fileindex_entry_stage_set(ie, stage);
8029 a->staged_something = 1;
8030 if (a->status_cb == NULL)
8031 break;
8032 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8033 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8034 de_name);
8035 break;
8036 case GOT_STATUS_NO_CHANGE:
8037 break;
8038 case GOT_STATUS_CONFLICT:
8039 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8040 break;
8041 case GOT_STATUS_NONEXISTENT:
8042 err = got_error_set_errno(ENOENT, relpath);
8043 break;
8044 default:
8045 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8046 break;
8049 if (path_content && unlink(path_content) == -1 && err == NULL)
8050 err = got_error_from_errno2("unlink", path_content);
8051 free(path_content);
8052 free(ondisk_path);
8053 free(new_staged_blob_id);
8054 return err;
8057 const struct got_error *
8058 got_worktree_stage(struct got_worktree *worktree,
8059 struct got_pathlist_head *paths,
8060 got_worktree_status_cb status_cb, void *status_arg,
8061 got_worktree_patch_cb patch_cb, void *patch_arg,
8062 int allow_bad_symlinks, struct got_repository *repo)
8064 const struct got_error *err = NULL, *sync_err, *unlockerr;
8065 struct got_pathlist_entry *pe;
8066 struct got_fileindex *fileindex = NULL;
8067 char *fileindex_path = NULL;
8068 struct got_reference *head_ref = NULL;
8069 struct got_object_id *head_commit_id = NULL;
8070 struct check_stage_ok_arg oka;
8071 struct stage_path_arg spa;
8073 err = lock_worktree(worktree, LOCK_EX);
8074 if (err)
8075 return err;
8077 err = got_ref_open(&head_ref, repo,
8078 got_worktree_get_head_ref_name(worktree), 0);
8079 if (err)
8080 goto done;
8081 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8082 if (err)
8083 goto done;
8084 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8085 if (err)
8086 goto done;
8088 /* Check pre-conditions before staging anything. */
8089 oka.head_commit_id = head_commit_id;
8090 oka.worktree = worktree;
8091 oka.fileindex = fileindex;
8092 oka.repo = repo;
8093 oka.have_changes = 0;
8094 TAILQ_FOREACH(pe, paths, entry) {
8095 err = worktree_status(worktree, pe->path, fileindex, repo,
8096 check_stage_ok, &oka, NULL, NULL, 1, 0);
8097 if (err)
8098 goto done;
8100 if (!oka.have_changes) {
8101 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8102 goto done;
8105 spa.worktree = worktree;
8106 spa.fileindex = fileindex;
8107 spa.repo = repo;
8108 spa.patch_cb = patch_cb;
8109 spa.patch_arg = patch_arg;
8110 spa.status_cb = status_cb;
8111 spa.status_arg = status_arg;
8112 spa.staged_something = 0;
8113 spa.allow_bad_symlinks = allow_bad_symlinks;
8114 TAILQ_FOREACH(pe, paths, entry) {
8115 err = worktree_status(worktree, pe->path, fileindex, repo,
8116 stage_path, &spa, NULL, NULL, 1, 0);
8117 if (err)
8118 goto done;
8120 if (!spa.staged_something) {
8121 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8122 goto done;
8125 sync_err = sync_fileindex(fileindex, fileindex_path);
8126 if (sync_err && err == NULL)
8127 err = sync_err;
8128 done:
8129 if (head_ref)
8130 got_ref_close(head_ref);
8131 free(head_commit_id);
8132 free(fileindex_path);
8133 if (fileindex)
8134 got_fileindex_free(fileindex);
8135 unlockerr = lock_worktree(worktree, LOCK_SH);
8136 if (unlockerr && err == NULL)
8137 err = unlockerr;
8138 return err;
8141 struct unstage_path_arg {
8142 struct got_worktree *worktree;
8143 struct got_fileindex *fileindex;
8144 struct got_repository *repo;
8145 got_worktree_checkout_cb progress_cb;
8146 void *progress_arg;
8147 got_worktree_patch_cb patch_cb;
8148 void *patch_arg;
8151 static const struct got_error *
8152 create_unstaged_content(char **path_unstaged_content,
8153 char **path_new_staged_content, struct got_object_id *blob_id,
8154 struct got_object_id *staged_blob_id, const char *relpath,
8155 struct got_repository *repo,
8156 got_worktree_patch_cb patch_cb, void *patch_arg)
8158 const struct got_error *err, *free_err;
8159 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8160 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8161 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8162 struct got_diffreg_result *diffreg_result = NULL;
8163 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8164 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8166 *path_unstaged_content = NULL;
8167 *path_new_staged_content = NULL;
8169 err = got_object_id_str(&label1, blob_id);
8170 if (err)
8171 return err;
8172 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
8173 if (err)
8174 goto done;
8176 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
8177 if (err)
8178 goto done;
8180 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8181 if (err)
8182 goto done;
8184 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
8185 if (err)
8186 goto done;
8188 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
8189 if (err)
8190 goto done;
8192 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8193 if (err)
8194 goto done;
8196 err = got_diff_files(&diffreg_result, f1, label1, f2,
8197 path2, 3, 0, 1, NULL);
8198 if (err)
8199 goto done;
8201 err = got_opentemp_named(path_unstaged_content, &outfile,
8202 "got-unstaged-content");
8203 if (err)
8204 goto done;
8205 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8206 "got-new-staged-content");
8207 if (err)
8208 goto done;
8210 if (fseek(f1, 0L, SEEK_SET) == -1) {
8211 err = got_ferror(f1, GOT_ERR_IO);
8212 goto done;
8214 if (fseek(f2, 0L, SEEK_SET) == -1) {
8215 err = got_ferror(f2, GOT_ERR_IO);
8216 goto done;
8218 /* Count the number of actual changes in the diff result. */
8219 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8220 struct diff_chunk_context cc = {};
8221 diff_chunk_context_load_change(&cc, &nchunks_used,
8222 diffreg_result->result, n, 0);
8223 nchanges++;
8225 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8226 int choice;
8227 err = apply_or_reject_change(&choice, &nchunks_used,
8228 diffreg_result->result, n, relpath, f1, f2,
8229 &line_cur1, &line_cur2,
8230 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8231 if (err)
8232 goto done;
8233 if (choice == GOT_PATCH_CHOICE_YES)
8234 have_content = 1;
8235 else
8236 have_rejected_content = 1;
8237 if (choice == GOT_PATCH_CHOICE_QUIT)
8238 break;
8240 if (have_content || have_rejected_content)
8241 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8242 outfile, rejectfile);
8243 done:
8244 free(label1);
8245 if (blob)
8246 got_object_blob_close(blob);
8247 if (staged_blob)
8248 got_object_blob_close(staged_blob);
8249 free_err = got_diffreg_result_free(diffreg_result);
8250 if (free_err && err == NULL)
8251 err = free_err;
8252 if (f1 && fclose(f1) == EOF && err == NULL)
8253 err = got_error_from_errno2("fclose", path1);
8254 if (f2 && fclose(f2) == EOF && err == NULL)
8255 err = got_error_from_errno2("fclose", path2);
8256 if (outfile && fclose(outfile) == EOF && err == NULL)
8257 err = got_error_from_errno2("fclose", *path_unstaged_content);
8258 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8259 err = got_error_from_errno2("fclose", *path_new_staged_content);
8260 if (path1 && unlink(path1) == -1 && err == NULL)
8261 err = got_error_from_errno2("unlink", path1);
8262 if (path2 && unlink(path2) == -1 && err == NULL)
8263 err = got_error_from_errno2("unlink", path2);
8264 if (err || !have_content) {
8265 if (*path_unstaged_content &&
8266 unlink(*path_unstaged_content) == -1 && err == NULL)
8267 err = got_error_from_errno2("unlink",
8268 *path_unstaged_content);
8269 free(*path_unstaged_content);
8270 *path_unstaged_content = NULL;
8272 if (err || !have_content || !have_rejected_content) {
8273 if (*path_new_staged_content &&
8274 unlink(*path_new_staged_content) == -1 && err == NULL)
8275 err = got_error_from_errno2("unlink",
8276 *path_new_staged_content);
8277 free(*path_new_staged_content);
8278 *path_new_staged_content = NULL;
8280 free(path1);
8281 free(path2);
8282 return err;
8285 static const struct got_error *
8286 unstage_hunks(struct got_object_id *staged_blob_id,
8287 struct got_blob_object *blob_base,
8288 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8289 const char *ondisk_path, const char *label_orig,
8290 struct got_worktree *worktree, struct got_repository *repo,
8291 got_worktree_patch_cb patch_cb, void *patch_arg,
8292 got_worktree_checkout_cb progress_cb, void *progress_arg)
8294 const struct got_error *err = NULL;
8295 char *path_unstaged_content = NULL;
8296 char *path_new_staged_content = NULL;
8297 char *parent = NULL, *base_path = NULL;
8298 char *blob_base_path = NULL;
8299 struct got_object_id *new_staged_blob_id = NULL;
8300 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8301 struct stat sb;
8303 err = create_unstaged_content(&path_unstaged_content,
8304 &path_new_staged_content, blob_id, staged_blob_id,
8305 ie->path, repo, patch_cb, patch_arg);
8306 if (err)
8307 return err;
8309 if (path_unstaged_content == NULL)
8310 return NULL;
8312 if (path_new_staged_content) {
8313 err = got_object_blob_create(&new_staged_blob_id,
8314 path_new_staged_content, repo);
8315 if (err)
8316 goto done;
8319 f = fopen(path_unstaged_content, "re");
8320 if (f == NULL) {
8321 err = got_error_from_errno2("fopen",
8322 path_unstaged_content);
8323 goto done;
8325 if (fstat(fileno(f), &sb) == -1) {
8326 err = got_error_from_errno2("fstat", path_unstaged_content);
8327 goto done;
8329 if (got_fileindex_entry_staged_filetype_get(ie) ==
8330 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8331 char link_target[PATH_MAX];
8332 size_t r;
8333 r = fread(link_target, 1, sizeof(link_target), f);
8334 if (r == 0 && ferror(f)) {
8335 err = got_error_from_errno("fread");
8336 goto done;
8338 if (r >= sizeof(link_target)) { /* should not happen */
8339 err = got_error(GOT_ERR_NO_SPACE);
8340 goto done;
8342 link_target[r] = '\0';
8343 err = merge_symlink(worktree, blob_base,
8344 ondisk_path, ie->path, label_orig, link_target,
8345 worktree->base_commit_id, repo, progress_cb,
8346 progress_arg);
8347 } else {
8348 int local_changes_subsumed;
8350 err = got_path_dirname(&parent, ondisk_path);
8351 if (err)
8352 return err;
8354 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8355 parent) == -1) {
8356 err = got_error_from_errno("asprintf");
8357 base_path = NULL;
8358 goto done;
8361 err = got_opentemp_named(&blob_base_path, &f_base, base_path);
8362 if (err)
8363 goto done;
8364 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8365 blob_base);
8366 if (err)
8367 goto done;
8370 * In order the run a 3-way merge with a symlink we copy the symlink's
8371 * target path into a temporary file and use that file with diff3.
8373 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8374 err = dump_symlink_target_path_to_file(&f_deriv2,
8375 ondisk_path);
8376 if (err)
8377 goto done;
8378 } else {
8379 int fd;
8380 fd = open(ondisk_path,
8381 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8382 if (fd == -1) {
8383 err = got_error_from_errno2("open", ondisk_path);
8384 goto done;
8386 f_deriv2 = fdopen(fd, "r");
8387 if (f_deriv2 == NULL) {
8388 err = got_error_from_errno2("fdopen", ondisk_path);
8389 close(fd);
8390 goto done;
8394 err = merge_file(&local_changes_subsumed, worktree,
8395 f_base, f, f_deriv2, ondisk_path, ie->path,
8396 got_fileindex_perms_to_st(ie),
8397 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8398 repo, progress_cb, progress_arg);
8400 if (err)
8401 goto done;
8403 if (new_staged_blob_id) {
8404 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8405 SHA1_DIGEST_LENGTH);
8406 } else {
8407 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8408 got_fileindex_entry_staged_filetype_set(ie, 0);
8410 done:
8411 free(new_staged_blob_id);
8412 if (path_unstaged_content &&
8413 unlink(path_unstaged_content) == -1 && err == NULL)
8414 err = got_error_from_errno2("unlink", path_unstaged_content);
8415 if (path_new_staged_content &&
8416 unlink(path_new_staged_content) == -1 && err == NULL)
8417 err = got_error_from_errno2("unlink", path_new_staged_content);
8418 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8419 err = got_error_from_errno2("unlink", blob_base_path);
8420 if (f_base && fclose(f_base) == EOF && err == NULL)
8421 err = got_error_from_errno2("fclose", path_unstaged_content);
8422 if (f && fclose(f) == EOF && err == NULL)
8423 err = got_error_from_errno2("fclose", path_unstaged_content);
8424 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8425 err = got_error_from_errno2("fclose", ondisk_path);
8426 free(path_unstaged_content);
8427 free(path_new_staged_content);
8428 free(blob_base_path);
8429 free(parent);
8430 free(base_path);
8431 return err;
8434 static const struct got_error *
8435 unstage_path(void *arg, unsigned char status,
8436 unsigned char staged_status, const char *relpath,
8437 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8438 struct got_object_id *commit_id, int dirfd, const char *de_name)
8440 const struct got_error *err = NULL;
8441 struct unstage_path_arg *a = arg;
8442 struct got_fileindex_entry *ie;
8443 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8444 char *ondisk_path = NULL;
8445 char *id_str = NULL, *label_orig = NULL;
8446 int local_changes_subsumed;
8447 struct stat sb;
8449 if (staged_status != GOT_STATUS_ADD &&
8450 staged_status != GOT_STATUS_MODIFY &&
8451 staged_status != GOT_STATUS_DELETE)
8452 return NULL;
8454 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8455 if (ie == NULL)
8456 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8458 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8459 == -1)
8460 return got_error_from_errno("asprintf");
8462 err = got_object_id_str(&id_str,
8463 commit_id ? commit_id : a->worktree->base_commit_id);
8464 if (err)
8465 goto done;
8466 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8467 id_str) == -1) {
8468 err = got_error_from_errno("asprintf");
8469 goto done;
8472 switch (staged_status) {
8473 case GOT_STATUS_MODIFY:
8474 err = got_object_open_as_blob(&blob_base, a->repo,
8475 blob_id, 8192);
8476 if (err)
8477 break;
8478 /* fall through */
8479 case GOT_STATUS_ADD:
8480 if (a->patch_cb) {
8481 if (staged_status == GOT_STATUS_ADD) {
8482 int choice = GOT_PATCH_CHOICE_NONE;
8483 err = (*a->patch_cb)(&choice, a->patch_arg,
8484 staged_status, ie->path, NULL, 1, 1);
8485 if (err)
8486 break;
8487 if (choice != GOT_PATCH_CHOICE_YES)
8488 break;
8489 } else {
8490 err = unstage_hunks(staged_blob_id,
8491 blob_base, blob_id, ie, ondisk_path,
8492 label_orig, a->worktree, a->repo,
8493 a->patch_cb, a->patch_arg,
8494 a->progress_cb, a->progress_arg);
8495 break; /* Done with this file. */
8498 err = got_object_open_as_blob(&blob_staged, a->repo,
8499 staged_blob_id, 8192);
8500 if (err)
8501 break;
8502 switch (got_fileindex_entry_staged_filetype_get(ie)) {
8503 case GOT_FILEIDX_MODE_BAD_SYMLINK:
8504 case GOT_FILEIDX_MODE_REGULAR_FILE:
8505 err = merge_blob(&local_changes_subsumed, a->worktree,
8506 blob_base, ondisk_path, relpath,
8507 got_fileindex_perms_to_st(ie), label_orig,
8508 blob_staged, commit_id ? commit_id :
8509 a->worktree->base_commit_id, a->repo,
8510 a->progress_cb, a->progress_arg);
8511 break;
8512 case GOT_FILEIDX_MODE_SYMLINK:
8513 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8514 char *staged_target;
8515 err = got_object_blob_read_to_str(
8516 &staged_target, blob_staged);
8517 if (err)
8518 goto done;
8519 err = merge_symlink(a->worktree, blob_base,
8520 ondisk_path, relpath, label_orig,
8521 staged_target, commit_id ? commit_id :
8522 a->worktree->base_commit_id,
8523 a->repo, a->progress_cb, a->progress_arg);
8524 free(staged_target);
8525 } else {
8526 err = merge_blob(&local_changes_subsumed,
8527 a->worktree, blob_base, ondisk_path,
8528 relpath, got_fileindex_perms_to_st(ie),
8529 label_orig, blob_staged,
8530 commit_id ? commit_id :
8531 a->worktree->base_commit_id, a->repo,
8532 a->progress_cb, a->progress_arg);
8534 break;
8535 default:
8536 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8537 break;
8539 if (err == NULL) {
8540 got_fileindex_entry_stage_set(ie,
8541 GOT_FILEIDX_STAGE_NONE);
8542 got_fileindex_entry_staged_filetype_set(ie, 0);
8544 break;
8545 case GOT_STATUS_DELETE:
8546 if (a->patch_cb) {
8547 int choice = GOT_PATCH_CHOICE_NONE;
8548 err = (*a->patch_cb)(&choice, a->patch_arg,
8549 staged_status, ie->path, NULL, 1, 1);
8550 if (err)
8551 break;
8552 if (choice == GOT_PATCH_CHOICE_NO)
8553 break;
8554 if (choice != GOT_PATCH_CHOICE_YES) {
8555 err = got_error(GOT_ERR_PATCH_CHOICE);
8556 break;
8559 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8560 got_fileindex_entry_staged_filetype_set(ie, 0);
8561 err = get_file_status(&status, &sb, ie, ondisk_path,
8562 dirfd, de_name, a->repo);
8563 if (err)
8564 break;
8565 err = (*a->progress_cb)(a->progress_arg, status, relpath);
8566 break;
8568 done:
8569 free(ondisk_path);
8570 if (blob_base)
8571 got_object_blob_close(blob_base);
8572 if (blob_staged)
8573 got_object_blob_close(blob_staged);
8574 free(id_str);
8575 free(label_orig);
8576 return err;
8579 const struct got_error *
8580 got_worktree_unstage(struct got_worktree *worktree,
8581 struct got_pathlist_head *paths,
8582 got_worktree_checkout_cb progress_cb, void *progress_arg,
8583 got_worktree_patch_cb patch_cb, void *patch_arg,
8584 struct got_repository *repo)
8586 const struct got_error *err = NULL, *sync_err, *unlockerr;
8587 struct got_pathlist_entry *pe;
8588 struct got_fileindex *fileindex = NULL;
8589 char *fileindex_path = NULL;
8590 struct unstage_path_arg upa;
8592 err = lock_worktree(worktree, LOCK_EX);
8593 if (err)
8594 return err;
8596 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8597 if (err)
8598 goto done;
8600 upa.worktree = worktree;
8601 upa.fileindex = fileindex;
8602 upa.repo = repo;
8603 upa.progress_cb = progress_cb;
8604 upa.progress_arg = progress_arg;
8605 upa.patch_cb = patch_cb;
8606 upa.patch_arg = patch_arg;
8607 TAILQ_FOREACH(pe, paths, entry) {
8608 err = worktree_status(worktree, pe->path, fileindex, repo,
8609 unstage_path, &upa, NULL, NULL, 1, 0);
8610 if (err)
8611 goto done;
8614 sync_err = sync_fileindex(fileindex, fileindex_path);
8615 if (sync_err && err == NULL)
8616 err = sync_err;
8617 done:
8618 free(fileindex_path);
8619 if (fileindex)
8620 got_fileindex_free(fileindex);
8621 unlockerr = lock_worktree(worktree, LOCK_SH);
8622 if (unlockerr && err == NULL)
8623 err = unlockerr;
8624 return err;
8627 struct report_file_info_arg {
8628 struct got_worktree *worktree;
8629 got_worktree_path_info_cb info_cb;
8630 void *info_arg;
8631 struct got_pathlist_head *paths;
8632 got_cancel_cb cancel_cb;
8633 void *cancel_arg;
8636 static const struct got_error *
8637 report_file_info(void *arg, struct got_fileindex_entry *ie)
8639 struct report_file_info_arg *a = arg;
8640 struct got_pathlist_entry *pe;
8641 struct got_object_id blob_id, staged_blob_id, commit_id;
8642 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8643 struct got_object_id *commit_idp = NULL;
8644 int stage;
8646 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8647 return got_error(GOT_ERR_CANCELLED);
8649 TAILQ_FOREACH(pe, a->paths, entry) {
8650 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8651 got_path_is_child(ie->path, pe->path, pe->path_len))
8652 break;
8654 if (pe == NULL) /* not found */
8655 return NULL;
8657 if (got_fileindex_entry_has_blob(ie)) {
8658 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8659 blob_idp = &blob_id;
8661 stage = got_fileindex_entry_stage_get(ie);
8662 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8663 stage == GOT_FILEIDX_STAGE_ADD) {
8664 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8665 SHA1_DIGEST_LENGTH);
8666 staged_blob_idp = &staged_blob_id;
8669 if (got_fileindex_entry_has_commit(ie)) {
8670 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8671 commit_idp = &commit_id;
8674 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8675 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8678 const struct got_error *
8679 got_worktree_path_info(struct got_worktree *worktree,
8680 struct got_pathlist_head *paths,
8681 got_worktree_path_info_cb info_cb, void *info_arg,
8682 got_cancel_cb cancel_cb, void *cancel_arg)
8685 const struct got_error *err = NULL, *unlockerr;
8686 struct got_fileindex *fileindex = NULL;
8687 char *fileindex_path = NULL;
8688 struct report_file_info_arg arg;
8690 err = lock_worktree(worktree, LOCK_SH);
8691 if (err)
8692 return err;
8694 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8695 if (err)
8696 goto done;
8698 arg.worktree = worktree;
8699 arg.info_cb = info_cb;
8700 arg.info_arg = info_arg;
8701 arg.paths = paths;
8702 arg.cancel_cb = cancel_cb;
8703 arg.cancel_arg = cancel_arg;
8704 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8705 &arg);
8706 done:
8707 free(fileindex_path);
8708 if (fileindex)
8709 got_fileindex_free(fileindex);
8710 unlockerr = lock_worktree(worktree, LOCK_UN);
8711 if (unlockerr && err == NULL)
8712 err = unlockerr;
8713 return err;
8716 static const struct got_error *
8717 patch_check_path(const char *p, char **path, unsigned char *status,
8718 unsigned char *staged_status, struct got_fileindex *fileindex,
8719 struct got_worktree *worktree, struct got_repository *repo)
8721 const struct got_error *err;
8722 struct got_fileindex_entry *ie;
8723 struct stat sb;
8724 char *ondisk_path = NULL;
8726 err = got_worktree_resolve_path(path, worktree, p);
8727 if (err)
8728 return err;
8730 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
8731 *path[0] ? "/" : "", *path) == -1)
8732 return got_error_from_errno("asprintf");
8734 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
8735 if (ie) {
8736 *staged_status = get_staged_status(ie);
8737 err = get_file_status(status, &sb, ie, *path, -1, NULL, repo);
8738 if (err)
8739 goto done;
8740 } else {
8741 *staged_status = GOT_STATUS_NO_CHANGE;
8742 *status = GOT_STATUS_UNVERSIONED;
8743 if (lstat(ondisk_path, &sb) == -1) {
8744 if (errno != ENOENT) {
8745 err = got_error_from_errno2("lstat",
8746 ondisk_path);
8747 goto done;
8749 *status = GOT_STATUS_NONEXISTENT;
8753 done:
8754 free(ondisk_path);
8755 return err;
8758 static const struct got_error *
8759 patch_can_rm(const char *path, unsigned char status,
8760 unsigned char staged_status)
8762 if (status == GOT_STATUS_NONEXISTENT)
8763 return got_error_set_errno(ENOENT, path);
8764 if (status != GOT_STATUS_NO_CHANGE &&
8765 status != GOT_STATUS_ADD &&
8766 status != GOT_STATUS_MODIFY &&
8767 status != GOT_STATUS_MODE_CHANGE)
8768 return got_error_path(path, GOT_ERR_FILE_STATUS);
8769 if (staged_status == GOT_STATUS_DELETE)
8770 return got_error_path(path, GOT_ERR_FILE_STATUS);
8771 return NULL;
8774 static const struct got_error *
8775 patch_can_add(const char *path, unsigned char status)
8777 if (status != GOT_STATUS_NONEXISTENT)
8778 return got_error_path(path, GOT_ERR_FILE_STATUS);
8779 return NULL;
8782 static const struct got_error *
8783 patch_can_edit(const char *path, unsigned char status,
8784 unsigned char staged_status)
8786 if (status == GOT_STATUS_NONEXISTENT)
8787 return got_error_set_errno(ENOENT, path);
8788 if (status != GOT_STATUS_NO_CHANGE &&
8789 status != GOT_STATUS_ADD &&
8790 status != GOT_STATUS_MODIFY)
8791 return got_error_path(path, GOT_ERR_FILE_STATUS);
8792 if (staged_status == GOT_STATUS_DELETE)
8793 return got_error_path(path, GOT_ERR_FILE_STATUS);
8794 return NULL;
8797 const struct got_error *
8798 got_worktree_patch_prepare(struct got_fileindex **fileindex,
8799 struct got_worktree *worktree)
8801 const struct got_error *err;
8802 char *fileindex_path = NULL;
8804 err = open_fileindex(fileindex, &fileindex_path, worktree);
8805 free(fileindex_path);
8806 return err;
8809 const struct got_error *
8810 got_worktree_patch_check_path(const char *old, const char *new,
8811 char **oldpath, char **newpath, struct got_worktree *worktree,
8812 struct got_repository *repo, struct got_fileindex *fileindex)
8814 const struct got_error *err = NULL;
8815 int file_renamed = 0;
8816 unsigned char status_old, staged_status_old;
8817 unsigned char status_new, staged_status_new;
8819 *oldpath = NULL;
8820 *newpath = NULL;
8822 err = patch_check_path(old != NULL ? old : new, oldpath,
8823 &status_old, &staged_status_old, fileindex, worktree, repo);
8824 if (err)
8825 goto done;
8827 err = patch_check_path(new != NULL ? new : old, newpath,
8828 &status_new, &staged_status_new, fileindex, worktree, repo);
8829 if (err)
8830 goto done;
8832 if (old != NULL && new != NULL && strcmp(old, new) != 0)
8833 file_renamed = 1;
8835 if (old != NULL && new == NULL)
8836 err = patch_can_rm(*oldpath, status_old, staged_status_old);
8837 else if (file_renamed) {
8838 err = patch_can_rm(*oldpath, status_old, staged_status_old);
8839 if (err == NULL)
8840 err = patch_can_add(*newpath, status_new);
8841 } else if (old == NULL)
8842 err = patch_can_add(*newpath, status_new);
8843 else
8844 err = patch_can_edit(*newpath, status_new, staged_status_new);
8846 done:
8847 if (err) {
8848 free(*oldpath);
8849 *oldpath = NULL;
8850 free(*newpath);
8851 *newpath = NULL;
8853 return err;
8856 const struct got_error *
8857 got_worktree_patch_complete(struct got_fileindex *fileindex)
8859 got_fileindex_free(fileindex);
8860 return NULL;