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 "got_compat.h"
19 #include <sys/stat.h>
20 #include <sys/queue.h>
22 #include <dirent.h>
23 #include <limits.h>
24 #include <stddef.h>
25 #include <string.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <time.h>
29 #include <fcntl.h>
30 #include <errno.h>
31 #include <unistd.h>
32 #include <zlib.h>
33 #include <fnmatch.h>
34 #include <libgen.h>
36 #include "got_error.h"
37 #include "got_repository.h"
38 #include "got_reference.h"
39 #include "got_object.h"
40 #include "got_path.h"
41 #include "got_cancel.h"
42 #include "got_worktree.h"
43 #include "got_opentemp.h"
44 #include "got_diff.h"
46 #include "got_lib_worktree.h"
47 #include "got_lib_hash.h"
48 #include "got_lib_fileindex.h"
49 #include "got_lib_inflate.h"
50 #include "got_lib_delta.h"
51 #include "got_lib_object.h"
52 #include "got_lib_object_parse.h"
53 #include "got_lib_object_create.h"
54 #include "got_lib_object_idset.h"
55 #include "got_lib_diff.h"
56 #include "got_lib_gotconfig.h"
58 #ifndef MIN
59 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
60 #endif
62 #define GOT_MERGE_LABEL_MERGED "merged change"
63 #define GOT_MERGE_LABEL_BASE "3-way merge base"
65 static mode_t apply_umask(mode_t);
67 static const struct got_error *
68 create_meta_file(const char *path_got, const char *name, const char *content)
69 {
70 const struct got_error *err = NULL;
71 char *path;
73 if (asprintf(&path, "%s/%s", path_got, name) == -1)
74 return got_error_from_errno("asprintf");
76 err = got_path_create_file(path, content);
77 free(path);
78 return err;
79 }
81 static const struct got_error *
82 update_meta_file(const char *path_got, const char *name, const char *content)
83 {
84 const struct got_error *err = NULL;
85 FILE *tmpfile = NULL;
86 char *tmppath = NULL;
87 char *path = NULL;
89 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
90 err = got_error_from_errno("asprintf");
91 path = NULL;
92 goto done;
93 }
95 err = got_opentemp_named(&tmppath, &tmpfile, path, "");
96 if (err)
97 goto done;
99 if (content) {
100 int len = fprintf(tmpfile, "%s\n", content);
101 if (len != strlen(content) + 1) {
102 err = got_error_from_errno2("fprintf", tmppath);
103 goto done;
107 if (rename(tmppath, path) != 0) {
108 err = got_error_from_errno3("rename", tmppath, path);
109 unlink(tmppath);
110 goto done;
113 done:
114 if (fclose(tmpfile) == EOF && err == NULL)
115 err = got_error_from_errno2("fclose", tmppath);
116 free(tmppath);
117 return err;
120 static const struct got_error *
121 write_head_ref(const char *path_got, struct got_reference *head_ref)
123 const struct got_error *err = NULL;
124 char *refstr = NULL;
126 if (got_ref_is_symbolic(head_ref)) {
127 refstr = got_ref_to_str(head_ref);
128 if (refstr == NULL)
129 return got_error_from_errno("got_ref_to_str");
130 } else {
131 refstr = strdup(got_ref_get_name(head_ref));
132 if (refstr == NULL)
133 return got_error_from_errno("strdup");
135 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
136 free(refstr);
137 return err;
140 const struct got_error *
141 got_worktree_init(const char *path, struct got_reference *head_ref,
142 const char *prefix, struct got_repository *repo)
144 const struct got_error *err = NULL;
145 struct got_object_id *commit_id = NULL;
146 uuid_t uuid;
147 uint32_t uuid_status;
148 int obj_type;
149 char *path_got = NULL;
150 char *formatstr = NULL;
151 char *absprefix = NULL;
152 char *basestr = NULL;
153 char *uuidstr = NULL;
155 if (strcmp(path, got_repo_get_path(repo)) == 0) {
156 err = got_error(GOT_ERR_WORKTREE_REPO);
157 goto done;
160 err = got_ref_resolve(&commit_id, repo, head_ref);
161 if (err)
162 return err;
163 err = got_object_get_type(&obj_type, repo, commit_id);
164 if (err)
165 return err;
166 if (obj_type != GOT_OBJ_TYPE_COMMIT)
167 return got_error(GOT_ERR_OBJ_TYPE);
169 if (!got_path_is_absolute(prefix)) {
170 if (asprintf(&absprefix, "/%s", prefix) == -1)
171 return got_error_from_errno("asprintf");
174 /* Create top-level directory (may already exist). */
175 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
176 err = got_error_from_errno2("mkdir", path);
177 goto done;
180 /* Create .got directory (may already exist). */
181 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
182 err = got_error_from_errno("asprintf");
183 goto done;
185 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
186 err = got_error_from_errno2("mkdir", path_got);
187 goto done;
190 /* Create an empty lock file. */
191 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
192 if (err)
193 goto done;
195 /* Create an empty file index. */
196 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
197 if (err)
198 goto done;
200 /* Write the HEAD reference. */
201 err = write_head_ref(path_got, head_ref);
202 if (err)
203 goto done;
205 /* Record our base commit. */
206 err = got_object_id_str(&basestr, commit_id);
207 if (err)
208 goto done;
209 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
210 if (err)
211 goto done;
213 /* Store path to repository. */
214 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
215 got_repo_get_path(repo));
216 if (err)
217 goto done;
219 /* Store in-repository path prefix. */
220 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
221 absprefix ? absprefix : prefix);
222 if (err)
223 goto done;
225 /* Generate UUID. */
226 uuid_create(&uuid, &uuid_status);
227 if (uuid_status != uuid_s_ok) {
228 err = got_error_uuid(uuid_status, "uuid_create");
229 goto done;
231 uuid_to_string(&uuid, &uuidstr, &uuid_status);
232 if (uuid_status != uuid_s_ok) {
233 err = got_error_uuid(uuid_status, "uuid_to_string");
234 goto done;
236 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
237 if (err)
238 goto done;
240 /* Stamp work tree with format file. */
241 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
242 err = got_error_from_errno("asprintf");
243 goto done;
245 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
246 if (err)
247 goto done;
249 done:
250 free(commit_id);
251 free(path_got);
252 free(formatstr);
253 free(absprefix);
254 free(basestr);
255 free(uuidstr);
256 return err;
259 const struct got_error *
260 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
261 const char *path_prefix)
263 char *absprefix = NULL;
265 if (!got_path_is_absolute(path_prefix)) {
266 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
267 return got_error_from_errno("asprintf");
269 *match = (strcmp(absprefix ? absprefix : path_prefix,
270 worktree->path_prefix) == 0);
271 free(absprefix);
272 return NULL;
275 const char *
276 got_worktree_get_head_ref_name(struct got_worktree *worktree)
278 return worktree->head_ref_name;
281 const struct got_error *
282 got_worktree_set_head_ref(struct got_worktree *worktree,
283 struct got_reference *head_ref)
285 const struct got_error *err = NULL;
286 char *path_got = NULL, *head_ref_name = NULL;
288 if (asprintf(&path_got, "%s/%s", worktree->root_path,
289 GOT_WORKTREE_GOT_DIR) == -1) {
290 err = got_error_from_errno("asprintf");
291 path_got = NULL;
292 goto done;
295 head_ref_name = strdup(got_ref_get_name(head_ref));
296 if (head_ref_name == NULL) {
297 err = got_error_from_errno("strdup");
298 goto done;
301 err = write_head_ref(path_got, head_ref);
302 if (err)
303 goto done;
305 free(worktree->head_ref_name);
306 worktree->head_ref_name = head_ref_name;
307 done:
308 free(path_got);
309 if (err)
310 free(head_ref_name);
311 return err;
314 struct got_object_id *
315 got_worktree_get_base_commit_id(struct got_worktree *worktree)
317 return worktree->base_commit_id;
320 const struct got_error *
321 got_worktree_set_base_commit_id(struct got_worktree *worktree,
322 struct got_repository *repo, struct got_object_id *commit_id)
324 const struct got_error *err;
325 struct got_object *obj = NULL;
326 char *id_str = NULL;
327 char *path_got = NULL;
329 if (asprintf(&path_got, "%s/%s", worktree->root_path,
330 GOT_WORKTREE_GOT_DIR) == -1) {
331 err = got_error_from_errno("asprintf");
332 path_got = NULL;
333 goto done;
336 err = got_object_open(&obj, repo, commit_id);
337 if (err)
338 return err;
340 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
341 err = got_error(GOT_ERR_OBJ_TYPE);
342 goto done;
345 /* Record our base commit. */
346 err = got_object_id_str(&id_str, commit_id);
347 if (err)
348 goto done;
349 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
350 if (err)
351 goto done;
353 free(worktree->base_commit_id);
354 worktree->base_commit_id = got_object_id_dup(commit_id);
355 if (worktree->base_commit_id == NULL) {
356 err = got_error_from_errno("got_object_id_dup");
357 goto done;
359 done:
360 if (obj)
361 got_object_close(obj);
362 free(id_str);
363 free(path_got);
364 return err;
367 const struct got_gotconfig *
368 got_worktree_get_gotconfig(struct got_worktree *worktree)
370 return worktree->gotconfig;
373 static const struct got_error *
374 lock_worktree(struct got_worktree *worktree, int operation)
376 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
377 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
378 : got_error_from_errno2("flock",
379 got_worktree_get_root_path(worktree)));
380 return NULL;
383 static const struct got_error *
384 add_dir_on_disk(struct got_worktree *worktree, const char *path)
386 const struct got_error *err = NULL;
387 char *abspath;
389 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
390 return got_error_from_errno("asprintf");
392 err = got_path_mkdir(abspath);
393 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
394 struct stat sb;
395 err = NULL;
396 if (lstat(abspath, &sb) == -1) {
397 err = got_error_from_errno2("lstat", abspath);
398 } else if (!S_ISDIR(sb.st_mode)) {
399 /* TODO directory is obstructed; do something */
400 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
403 free(abspath);
404 return err;
407 static const struct got_error *
408 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
410 const struct got_error *err = NULL;
411 uint8_t fbuf1[8192];
412 uint8_t fbuf2[8192];
413 size_t flen1 = 0, flen2 = 0;
415 *same = 1;
417 for (;;) {
418 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
419 if (flen1 == 0 && ferror(f1)) {
420 err = got_error_from_errno("fread");
421 break;
423 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
424 if (flen2 == 0 && ferror(f2)) {
425 err = got_error_from_errno("fread");
426 break;
428 if (flen1 == 0) {
429 if (flen2 != 0)
430 *same = 0;
431 break;
432 } else if (flen2 == 0) {
433 if (flen1 != 0)
434 *same = 0;
435 break;
436 } else if (flen1 == flen2) {
437 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
438 *same = 0;
439 break;
441 } else {
442 *same = 0;
443 break;
447 return err;
450 static const struct got_error *
451 check_files_equal(int *same, FILE *f1, FILE *f2)
453 struct stat sb;
454 size_t size1, size2;
456 *same = 1;
458 if (fstat(fileno(f1), &sb) != 0)
459 return got_error_from_errno("fstat");
460 size1 = sb.st_size;
462 if (fstat(fileno(f2), &sb) != 0)
463 return got_error_from_errno("fstat");
464 size2 = sb.st_size;
466 if (size1 != size2) {
467 *same = 0;
468 return NULL;
471 if (fseek(f1, 0L, SEEK_SET) == -1)
472 return got_ferror(f1, GOT_ERR_IO);
473 if (fseek(f2, 0L, SEEK_SET) == -1)
474 return got_ferror(f2, GOT_ERR_IO);
476 return check_file_contents_equal(same, f1, f2);
479 static const struct got_error *
480 copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
482 uint8_t fbuf[65536];
483 size_t flen;
484 ssize_t outlen;
486 *outsize = 0;
488 if (fseek(f, 0L, SEEK_SET) == -1)
489 return got_ferror(f, GOT_ERR_IO);
491 for (;;) {
492 flen = fread(fbuf, 1, sizeof(fbuf), f);
493 if (flen == 0) {
494 if (ferror(f))
495 return got_error_from_errno("fread");
496 if (feof(f))
497 break;
499 outlen = write(outfd, fbuf, flen);
500 if (outlen == -1)
501 return got_error_from_errno("write");
502 if (outlen != flen)
503 return got_error(GOT_ERR_IO);
504 *outsize += outlen;
507 return NULL;
510 static const struct got_error *
511 merge_binary_file(int *overlapcnt, int merged_fd,
512 FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
513 const char *label_deriv, const char *label_orig, const char *label_deriv2,
514 const char *ondisk_path)
516 const struct got_error *err = NULL;
517 int same_content, changed_deriv, changed_deriv2;
518 int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
519 off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
520 char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
521 char *base_path_orig = NULL, *base_path_deriv = NULL;
522 char *base_path_deriv2 = NULL;
524 *overlapcnt = 0;
526 err = check_files_equal(&same_content, f_deriv, f_deriv2);
527 if (err)
528 return err;
530 if (same_content)
531 return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
533 err = check_files_equal(&same_content, f_deriv, f_orig);
534 if (err)
535 return err;
536 changed_deriv = !same_content;
537 err = check_files_equal(&same_content, f_deriv2, f_orig);
538 if (err)
539 return err;
540 changed_deriv2 = !same_content;
542 if (changed_deriv && changed_deriv2) {
543 *overlapcnt = 1;
544 if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
545 err = got_error_from_errno("asprintf");
546 goto done;
548 if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
549 err = got_error_from_errno("asprintf");
550 goto done;
552 if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
553 err = got_error_from_errno("asprintf");
554 goto done;
556 err = got_opentemp_named_fd(&path_orig, &fd_orig,
557 base_path_orig, "");
558 if (err)
559 goto done;
560 err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
561 base_path_deriv, "");
562 if (err)
563 goto done;
564 err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
565 base_path_deriv2, "");
566 if (err)
567 goto done;
568 err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
569 if (err)
570 goto done;
571 err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
572 if (err)
573 goto done;
574 err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
575 if (err)
576 goto done;
577 if (dprintf(merged_fd, "Binary files differ and cannot be "
578 "merged automatically:\n") < 0) {
579 err = got_error_from_errno("dprintf");
580 goto done;
582 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
583 GOT_DIFF_CONFLICT_MARKER_BEGIN,
584 label_deriv ? " " : "",
585 label_deriv ? label_deriv : "",
586 path_deriv) < 0) {
587 err = got_error_from_errno("dprintf");
588 goto done;
590 if (size_orig > 0) {
591 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
592 GOT_DIFF_CONFLICT_MARKER_ORIG,
593 label_orig ? " " : "",
594 label_orig ? label_orig : "",
595 path_orig) < 0) {
596 err = got_error_from_errno("dprintf");
597 goto done;
600 if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
601 GOT_DIFF_CONFLICT_MARKER_SEP,
602 path_deriv2,
603 GOT_DIFF_CONFLICT_MARKER_END,
604 label_deriv2 ? " " : "",
605 label_deriv2 ? label_deriv2 : "") < 0) {
606 err = got_error_from_errno("dprintf");
607 goto done;
609 } else if (changed_deriv)
610 err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
611 else if (changed_deriv2)
612 err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
613 done:
614 if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
615 err == NULL)
616 err = got_error_from_errno2("unlink", path_orig);
617 if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
618 err = got_error_from_errno2("close", path_orig);
619 if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
620 err = got_error_from_errno2("close", path_deriv);
621 if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
622 err = got_error_from_errno2("close", path_deriv2);
623 free(path_orig);
624 free(path_deriv);
625 free(path_deriv2);
626 free(base_path_orig);
627 free(base_path_deriv);
628 free(base_path_deriv2);
629 return err;
632 /*
633 * Perform a 3-way merge where the file f_orig acts as the common
634 * ancestor, the file f_deriv acts as the first derived version,
635 * and the file f_deriv2 acts as the second derived version.
636 * The merge result will be written to a new file at ondisk_path; any
637 * existing file at this path will be replaced.
638 */
639 static const struct got_error *
640 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
641 FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
642 const char *path, uint16_t st_mode,
643 const char *label_orig, const char *label_deriv, const char *label_deriv2,
644 enum got_diff_algorithm diff_algo, struct got_repository *repo,
645 got_worktree_checkout_cb progress_cb, void *progress_arg)
647 const struct got_error *err = NULL;
648 int merged_fd = -1;
649 FILE *f_merged = NULL;
650 char *merged_path = NULL, *base_path = NULL;
651 int overlapcnt = 0;
652 char *parent = NULL;
654 *local_changes_subsumed = 0;
656 err = got_path_dirname(&parent, ondisk_path);
657 if (err)
658 return err;
660 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
661 err = got_error_from_errno("asprintf");
662 goto done;
665 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path, "");
666 if (err)
667 goto done;
669 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
670 f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
671 if (err) {
672 if (err->code != GOT_ERR_FILE_BINARY)
673 goto done;
674 err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
675 f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
676 ondisk_path);
677 if (err)
678 goto done;
681 err = (*progress_cb)(progress_arg,
682 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
683 if (err)
684 goto done;
686 if (fsync(merged_fd) != 0) {
687 err = got_error_from_errno("fsync");
688 goto done;
691 f_merged = fdopen(merged_fd, "r");
692 if (f_merged == NULL) {
693 err = got_error_from_errno("fdopen");
694 goto done;
696 merged_fd = -1;
698 /* Check if a clean merge has subsumed all local changes. */
699 if (overlapcnt == 0) {
700 err = check_files_equal(local_changes_subsumed, f_deriv,
701 f_merged);
702 if (err)
703 goto done;
706 if (fchmod(fileno(f_merged), apply_umask(st_mode)) != 0) {
707 err = got_error_from_errno2("fchmod", merged_path);
708 goto done;
711 if (rename(merged_path, ondisk_path) != 0) {
712 err = got_error_from_errno3("rename", merged_path,
713 ondisk_path);
714 goto done;
716 done:
717 if (err) {
718 if (merged_path)
719 unlink(merged_path);
721 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
722 err = got_error_from_errno("close");
723 if (f_merged && fclose(f_merged) == EOF && err == NULL)
724 err = got_error_from_errno("fclose");
725 free(merged_path);
726 free(base_path);
727 free(parent);
728 return err;
731 static const struct got_error *
732 update_symlink(const char *ondisk_path, const char *target_path,
733 size_t target_len)
735 /* This is not atomic but matches what 'ln -sf' does. */
736 if (unlink(ondisk_path) == -1)
737 return got_error_from_errno2("unlink", ondisk_path);
738 if (symlink(target_path, ondisk_path) == -1)
739 return got_error_from_errno3("symlink", target_path,
740 ondisk_path);
741 return NULL;
744 /*
745 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
746 * in the work tree with a file that contains conflict markers and the
747 * conflicting target paths of the original version, a "derived version"
748 * of a symlink from an incoming change, and a local version of the symlink.
750 * The original versions's target path can be NULL if it is not available,
751 * such as if both derived versions added a new symlink at the same path.
753 * The incoming derived symlink target is NULL in case the incoming change
754 * has deleted this symlink.
755 */
756 static const struct got_error *
757 install_symlink_conflict(const char *deriv_target,
758 struct got_object_id *deriv_base_commit_id, const char *orig_target,
759 const char *label_orig, const char *local_target, const char *ondisk_path)
761 const struct got_error *err;
762 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
763 FILE *f = NULL;
765 err = got_object_id_str(&id_str, deriv_base_commit_id);
766 if (err)
767 return got_error_from_errno("asprintf");
769 if (asprintf(&label_deriv, "%s: commit %s",
770 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
771 err = got_error_from_errno("asprintf");
772 goto done;
775 err = got_opentemp_named(&path, &f, "got-symlink-conflict", "");
776 if (err)
777 goto done;
779 if (fchmod(fileno(f), apply_umask(GOT_DEFAULT_FILE_MODE)) == -1) {
780 err = got_error_from_errno2("fchmod", path);
781 goto done;
784 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
785 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
786 deriv_target ? deriv_target : "(symlink was deleted)",
787 orig_target ? label_orig : "",
788 orig_target ? "\n" : "",
789 orig_target ? orig_target : "",
790 orig_target ? "\n" : "",
791 GOT_DIFF_CONFLICT_MARKER_SEP,
792 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
793 err = got_error_from_errno2("fprintf", path);
794 goto done;
797 if (unlink(ondisk_path) == -1) {
798 err = got_error_from_errno2("unlink", ondisk_path);
799 goto done;
801 if (rename(path, ondisk_path) == -1) {
802 err = got_error_from_errno3("rename", path, ondisk_path);
803 goto done;
805 done:
806 if (f != NULL && fclose(f) == EOF && err == NULL)
807 err = got_error_from_errno2("fclose", path);
808 free(path);
809 free(id_str);
810 free(label_deriv);
811 return err;
814 /* forward declaration */
815 static const struct got_error *
816 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
817 const char *, const char *, uint16_t, const char *,
818 struct got_blob_object *, struct got_object_id *,
819 struct got_repository *, got_worktree_checkout_cb, void *);
821 /*
822 * Merge a symlink into the work tree, where blob_orig acts as the common
823 * ancestor, deriv_target is the link target of the first derived version,
824 * and the symlink on disk acts as the second derived version.
825 * Assume that contents of both blobs represent symlinks.
826 */
827 static const struct got_error *
828 merge_symlink(struct got_worktree *worktree,
829 struct got_blob_object *blob_orig, const char *ondisk_path,
830 const char *path, const char *label_orig, const char *deriv_target,
831 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
832 got_worktree_checkout_cb progress_cb, void *progress_arg)
834 const struct got_error *err = NULL;
835 char *ancestor_target = NULL;
836 struct stat sb;
837 ssize_t ondisk_len, deriv_len;
838 char ondisk_target[PATH_MAX];
839 int have_local_change = 0;
840 int have_incoming_change = 0;
842 if (lstat(ondisk_path, &sb) == -1)
843 return got_error_from_errno2("lstat", ondisk_path);
845 ondisk_len = readlink(ondisk_path, ondisk_target,
846 sizeof(ondisk_target));
847 if (ondisk_len == -1) {
848 err = got_error_from_errno2("readlink",
849 ondisk_path);
850 goto done;
852 ondisk_target[ondisk_len] = '\0';
854 if (blob_orig) {
855 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
856 if (err)
857 goto done;
860 if (ancestor_target == NULL ||
861 (ondisk_len != strlen(ancestor_target) ||
862 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
863 have_local_change = 1;
865 deriv_len = strlen(deriv_target);
866 if (ancestor_target == NULL ||
867 (deriv_len != strlen(ancestor_target) ||
868 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
869 have_incoming_change = 1;
871 if (!have_local_change && !have_incoming_change) {
872 if (ancestor_target) {
873 /* Both sides made the same change. */
874 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
875 path);
876 } else if (deriv_len == ondisk_len &&
877 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
878 /* Both sides added the same symlink. */
879 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
880 path);
881 } else {
882 /* Both sides added symlinks which don't match. */
883 err = install_symlink_conflict(deriv_target,
884 deriv_base_commit_id, ancestor_target,
885 label_orig, ondisk_target, ondisk_path);
886 if (err)
887 goto done;
888 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
889 path);
891 } else if (!have_local_change && have_incoming_change) {
892 /* Apply the incoming change. */
893 err = update_symlink(ondisk_path, deriv_target,
894 strlen(deriv_target));
895 if (err)
896 goto done;
897 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
898 } else if (have_local_change && have_incoming_change) {
899 if (deriv_len == ondisk_len &&
900 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
901 /* Both sides made the same change. */
902 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
903 path);
904 } else {
905 err = install_symlink_conflict(deriv_target,
906 deriv_base_commit_id, ancestor_target, label_orig,
907 ondisk_target, ondisk_path);
908 if (err)
909 goto done;
910 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
911 path);
915 done:
916 free(ancestor_target);
917 return err;
920 static const struct got_error *
921 dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
923 const struct got_error *err = NULL;
924 char target_path[PATH_MAX];
925 ssize_t target_len;
926 size_t n;
927 FILE *f;
929 *outfile = NULL;
931 f = got_opentemp();
932 if (f == NULL)
933 return got_error_from_errno("got_opentemp");
934 target_len = readlink(ondisk_path, target_path, sizeof(target_path));
935 if (target_len == -1) {
936 err = got_error_from_errno2("readlink", ondisk_path);
937 goto done;
939 n = fwrite(target_path, 1, target_len, f);
940 if (n != target_len) {
941 err = got_ferror(f, GOT_ERR_IO);
942 goto done;
944 if (fflush(f) == EOF) {
945 err = got_error_from_errno("fflush");
946 goto done;
948 if (fseek(f, 0L, SEEK_SET) == -1) {
949 err = got_ferror(f, GOT_ERR_IO);
950 goto done;
952 done:
953 if (err)
954 fclose(f);
955 else
956 *outfile = f;
957 return err;
960 /*
961 * Perform a 3-way merge where blob_orig acts as the common ancestor,
962 * blob_deriv acts as the first derived version, and the file on disk
963 * acts as the second derived version.
964 */
965 static const struct got_error *
966 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
967 struct got_blob_object *blob_orig, const char *ondisk_path,
968 const char *path, uint16_t st_mode, const char *label_orig,
969 struct got_blob_object *blob_deriv,
970 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
971 got_worktree_checkout_cb progress_cb, void *progress_arg)
973 const struct got_error *err = NULL;
974 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
975 char *blob_orig_path = NULL;
976 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
977 char *label_deriv = NULL, *parent = NULL;
979 *local_changes_subsumed = 0;
981 err = got_path_dirname(&parent, ondisk_path);
982 if (err)
983 return err;
985 if (blob_orig) {
986 if (asprintf(&base_path, "%s/got-merge-blob-orig",
987 parent) == -1) {
988 err = got_error_from_errno("asprintf");
989 base_path = NULL;
990 goto done;
993 err = got_opentemp_named(&blob_orig_path, &f_orig,
994 base_path, "");
995 if (err)
996 goto done;
997 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
998 blob_orig);
999 if (err)
1000 goto done;
1001 free(base_path);
1002 } else {
1004 * No common ancestor exists. This is an "add vs add" conflict
1005 * and we simply use an empty ancestor file to make both files
1006 * appear in the merged result in their entirety.
1008 f_orig = got_opentemp();
1009 if (f_orig == NULL) {
1010 err = got_error_from_errno("got_opentemp");
1011 goto done;
1015 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1016 err = got_error_from_errno("asprintf");
1017 base_path = NULL;
1018 goto done;
1021 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path, "");
1022 if (err)
1023 goto done;
1024 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1025 blob_deriv);
1026 if (err)
1027 goto done;
1029 err = got_object_id_str(&id_str, deriv_base_commit_id);
1030 if (err)
1031 goto done;
1032 if (asprintf(&label_deriv, "%s: commit %s",
1033 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1034 err = got_error_from_errno("asprintf");
1035 goto done;
1039 * In order the run a 3-way merge with a symlink we copy the symlink's
1040 * target path into a temporary file and use that file with diff3.
1042 if (S_ISLNK(st_mode)) {
1043 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1044 if (err)
1045 goto done;
1046 } else {
1047 int fd;
1048 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1049 if (fd == -1) {
1050 err = got_error_from_errno2("open", ondisk_path);
1051 goto done;
1053 f_deriv2 = fdopen(fd, "r");
1054 if (f_deriv2 == NULL) {
1055 err = got_error_from_errno2("fdopen", ondisk_path);
1056 close(fd);
1057 goto done;
1061 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1062 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1063 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1064 done:
1065 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1066 err = got_error_from_errno("fclose");
1067 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1068 err = got_error_from_errno("fclose");
1069 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1070 err = got_error_from_errno("fclose");
1071 free(base_path);
1072 if (blob_orig_path) {
1073 unlink(blob_orig_path);
1074 free(blob_orig_path);
1076 if (blob_deriv_path) {
1077 unlink(blob_deriv_path);
1078 free(blob_deriv_path);
1080 free(id_str);
1081 free(label_deriv);
1082 free(parent);
1083 return err;
1086 static const struct got_error *
1087 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1088 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1089 int wt_fd, const char *path, struct got_object_id *blob_id)
1091 const struct got_error *err = NULL;
1092 struct got_fileindex_entry *new_ie;
1094 *new_iep = NULL;
1096 err = got_fileindex_entry_alloc(&new_ie, path);
1097 if (err)
1098 return err;
1100 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1101 blob_id->sha1, base_commit_id->sha1, 1);
1102 if (err)
1103 goto done;
1105 err = got_fileindex_entry_add(fileindex, new_ie);
1106 done:
1107 if (err)
1108 got_fileindex_entry_free(new_ie);
1109 else
1110 *new_iep = new_ie;
1111 return err;
1114 static mode_t
1115 get_ondisk_perms(int executable, mode_t st_mode)
1117 mode_t xbits = S_IXUSR;
1119 if (executable) {
1120 /* Map read bits to execute bits. */
1121 if (st_mode & S_IRGRP)
1122 xbits |= S_IXGRP;
1123 if (st_mode & S_IROTH)
1124 xbits |= S_IXOTH;
1125 return st_mode | xbits;
1128 return st_mode;
1131 static mode_t
1132 apply_umask(mode_t mode)
1134 mode_t um;
1136 um = umask(000);
1137 umask(um);
1138 return mode & ~um;
1141 /* forward declaration */
1142 static const struct got_error *
1143 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1144 const char *path, mode_t te_mode, mode_t st_mode,
1145 struct got_blob_object *blob, int restoring_missing_file,
1146 int reverting_versioned_file, int installing_bad_symlink,
1147 int path_is_unversioned, struct got_repository *repo,
1148 got_worktree_checkout_cb progress_cb, void *progress_arg);
1151 * This function assumes that the provided symlink target points at a
1152 * safe location in the work tree!
1154 static const struct got_error *
1155 replace_existing_symlink(int *did_something, const char *ondisk_path,
1156 const char *target_path, size_t target_len)
1158 const struct got_error *err = NULL;
1159 ssize_t elen;
1160 char etarget[PATH_MAX];
1161 int fd;
1163 *did_something = 0;
1166 * "Bad" symlinks (those pointing outside the work tree or into the
1167 * .got directory) are installed in the work tree as a regular file
1168 * which contains the bad symlink target path.
1169 * The new symlink target has already been checked for safety by our
1170 * caller. If we can successfully open a regular file then we simply
1171 * replace this file with a symlink below.
1173 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1174 if (fd == -1) {
1175 if (!got_err_open_nofollow_on_symlink())
1176 return got_error_from_errno2("open", ondisk_path);
1178 /* We are updating an existing on-disk symlink. */
1179 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1180 if (elen == -1)
1181 return got_error_from_errno2("readlink", ondisk_path);
1183 if (elen == target_len &&
1184 memcmp(etarget, target_path, target_len) == 0)
1185 return NULL; /* nothing to do */
1188 *did_something = 1;
1189 err = update_symlink(ondisk_path, target_path, target_len);
1190 if (fd != -1 && close(fd) == -1 && err == NULL)
1191 err = got_error_from_errno2("close", ondisk_path);
1192 return err;
1195 static const struct got_error *
1196 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1197 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1199 const struct got_error *err = NULL;
1200 char canonpath[PATH_MAX];
1201 char *path_got = NULL;
1203 *is_bad_symlink = 0;
1205 if (target_len >= sizeof(canonpath)) {
1206 *is_bad_symlink = 1;
1207 return NULL;
1211 * We do not use realpath(3) to resolve the symlink's target
1212 * path because we don't want to resolve symlinks recursively.
1213 * Instead we make the path absolute and then canonicalize it.
1214 * Relative symlink target lookup should begin at the directory
1215 * in which the blob object is being installed.
1217 if (!got_path_is_absolute(target_path)) {
1218 char *abspath, *parent;
1219 err = got_path_dirname(&parent, ondisk_path);
1220 if (err)
1221 return err;
1222 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1223 free(parent);
1224 return got_error_from_errno("asprintf");
1226 free(parent);
1227 if (strlen(abspath) >= sizeof(canonpath)) {
1228 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1229 free(abspath);
1230 return err;
1232 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1233 free(abspath);
1234 if (err)
1235 return err;
1236 } else {
1237 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1238 if (err)
1239 return err;
1242 /* Only allow symlinks pointing at paths within the work tree. */
1243 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1244 *is_bad_symlink = 1;
1245 return NULL;
1248 /* Do not allow symlinks pointing into the .got directory. */
1249 if (asprintf(&path_got, "%s/%s", wtroot_path,
1250 GOT_WORKTREE_GOT_DIR) == -1)
1251 return got_error_from_errno("asprintf");
1252 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1253 *is_bad_symlink = 1;
1255 free(path_got);
1256 return NULL;
1259 static const struct got_error *
1260 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1261 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1262 int restoring_missing_file, int reverting_versioned_file,
1263 int path_is_unversioned, int allow_bad_symlinks,
1264 struct got_repository *repo,
1265 got_worktree_checkout_cb progress_cb, void *progress_arg)
1267 const struct got_error *err = NULL;
1268 char target_path[PATH_MAX];
1269 size_t len, target_len = 0;
1270 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1271 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1273 *is_bad_symlink = 0;
1276 * Blob object content specifies the target path of the link.
1277 * If a symbolic link cannot be installed we instead create
1278 * a regular file which contains the link target path stored
1279 * in the blob object.
1281 do {
1282 err = got_object_blob_read_block(&len, blob);
1283 if (err)
1284 return err;
1286 if (len + target_len >= sizeof(target_path)) {
1287 /* Path too long; install as a regular file. */
1288 *is_bad_symlink = 1;
1289 got_object_blob_rewind(blob);
1290 return install_blob(worktree, ondisk_path, path,
1291 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1292 restoring_missing_file, reverting_versioned_file,
1293 1, path_is_unversioned, repo, progress_cb,
1294 progress_arg);
1296 if (len > 0) {
1297 /* Skip blob object header first time around. */
1298 memcpy(target_path + target_len, buf + hdrlen,
1299 len - hdrlen);
1300 target_len += len - hdrlen;
1301 hdrlen = 0;
1303 } while (len != 0);
1304 target_path[target_len] = '\0';
1306 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1307 ondisk_path, worktree->root_path);
1308 if (err)
1309 return err;
1311 if (*is_bad_symlink && !allow_bad_symlinks) {
1312 /* install as a regular file */
1313 got_object_blob_rewind(blob);
1314 err = install_blob(worktree, ondisk_path, path,
1315 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1316 restoring_missing_file, reverting_versioned_file, 1,
1317 path_is_unversioned, repo, progress_cb, progress_arg);
1318 return err;
1321 if (symlink(target_path, ondisk_path) == -1) {
1322 if (errno == EEXIST) {
1323 int symlink_replaced;
1324 if (path_is_unversioned) {
1325 err = (*progress_cb)(progress_arg,
1326 GOT_STATUS_UNVERSIONED, path);
1327 return err;
1329 err = replace_existing_symlink(&symlink_replaced,
1330 ondisk_path, target_path, target_len);
1331 if (err)
1332 return err;
1333 if (progress_cb) {
1334 if (symlink_replaced) {
1335 err = (*progress_cb)(progress_arg,
1336 reverting_versioned_file ?
1337 GOT_STATUS_REVERT :
1338 GOT_STATUS_UPDATE, path);
1339 } else {
1340 err = (*progress_cb)(progress_arg,
1341 GOT_STATUS_EXISTS, path);
1344 return err; /* Nothing else to do. */
1347 if (errno == ENOENT) {
1348 char *parent;
1349 err = got_path_dirname(&parent, ondisk_path);
1350 if (err)
1351 return err;
1352 err = add_dir_on_disk(worktree, parent);
1353 free(parent);
1354 if (err)
1355 return err;
1357 * Retry, and fall through to error handling
1358 * below if this second attempt fails.
1360 if (symlink(target_path, ondisk_path) != -1) {
1361 err = NULL; /* success */
1362 return err;
1366 /* Handle errors from first or second creation attempt. */
1367 if (errno == ENAMETOOLONG) {
1368 /* bad target path; install as a regular file */
1369 *is_bad_symlink = 1;
1370 got_object_blob_rewind(blob);
1371 err = install_blob(worktree, ondisk_path, path,
1372 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1373 restoring_missing_file, reverting_versioned_file, 1,
1374 path_is_unversioned, repo,
1375 progress_cb, progress_arg);
1376 } else if (errno == ENOTDIR) {
1377 err = got_error_path(ondisk_path,
1378 GOT_ERR_FILE_OBSTRUCTED);
1379 } else {
1380 err = got_error_from_errno3("symlink",
1381 target_path, ondisk_path);
1383 } else if (progress_cb)
1384 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1385 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1386 return err;
1389 static const struct got_error *
1390 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1391 const char *path, mode_t te_mode, mode_t st_mode,
1392 struct got_blob_object *blob, int restoring_missing_file,
1393 int reverting_versioned_file, int installing_bad_symlink,
1394 int path_is_unversioned, struct got_repository *repo,
1395 got_worktree_checkout_cb progress_cb, void *progress_arg)
1397 const struct got_error *err = NULL;
1398 int fd = -1;
1399 size_t len, hdrlen;
1400 int update = 0;
1401 char *tmppath = NULL;
1402 mode_t mode;
1404 mode = get_ondisk_perms(te_mode & S_IXUSR, GOT_DEFAULT_FILE_MODE);
1405 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1406 O_CLOEXEC, mode);
1407 if (fd == -1) {
1408 if (errno == ENOENT || errno == ENOTDIR) {
1409 char *parent;
1410 err = got_path_dirname(&parent, path);
1411 if (err)
1412 return err;
1413 err = add_dir_on_disk(worktree, parent);
1414 if (err && err->code == GOT_ERR_FILE_OBSTRUCTED)
1415 err = got_error_path(path, err->code);
1416 free(parent);
1417 if (err)
1418 return err;
1419 fd = open(ondisk_path,
1420 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1421 mode);
1422 if (fd == -1)
1423 return got_error_from_errno2("open",
1424 ondisk_path);
1425 } else if (errno == EEXIST) {
1426 if (path_is_unversioned) {
1427 err = (*progress_cb)(progress_arg,
1428 GOT_STATUS_UNVERSIONED, path);
1429 goto done;
1431 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1432 !S_ISREG(st_mode) && !installing_bad_symlink) {
1433 /* TODO file is obstructed; do something */
1434 err = got_error_path(ondisk_path,
1435 GOT_ERR_FILE_OBSTRUCTED);
1436 goto done;
1437 } else {
1438 err = got_opentemp_named_fd(&tmppath, &fd,
1439 ondisk_path, "");
1440 if (err)
1441 goto done;
1442 update = 1;
1444 if (fchmod(fd, apply_umask(mode)) == -1) {
1445 err = got_error_from_errno2("fchmod",
1446 tmppath);
1447 goto done;
1450 } else
1451 return got_error_from_errno2("open", ondisk_path);
1454 if (progress_cb) {
1455 if (restoring_missing_file)
1456 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1457 path);
1458 else if (reverting_versioned_file)
1459 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1460 path);
1461 else
1462 err = (*progress_cb)(progress_arg,
1463 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1464 if (err)
1465 goto done;
1468 hdrlen = got_object_blob_get_hdrlen(blob);
1469 do {
1470 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1471 err = got_object_blob_read_block(&len, blob);
1472 if (err)
1473 break;
1474 if (len > 0) {
1475 /* Skip blob object header first time around. */
1476 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1477 if (outlen == -1) {
1478 err = got_error_from_errno("write");
1479 goto done;
1480 } else if (outlen != len - hdrlen) {
1481 err = got_error(GOT_ERR_IO);
1482 goto done;
1484 hdrlen = 0;
1486 } while (len != 0);
1488 if (fsync(fd) != 0) {
1489 err = got_error_from_errno("fsync");
1490 goto done;
1493 if (update) {
1494 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1495 err = got_error_from_errno2("unlink", ondisk_path);
1496 goto done;
1498 if (rename(tmppath, ondisk_path) != 0) {
1499 err = got_error_from_errno3("rename", tmppath,
1500 ondisk_path);
1501 goto done;
1503 free(tmppath);
1504 tmppath = NULL;
1507 done:
1508 if (fd != -1 && close(fd) == -1 && err == NULL)
1509 err = got_error_from_errno("close");
1510 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1511 err = got_error_from_errno2("unlink", tmppath);
1512 free(tmppath);
1513 return err;
1517 * Upgrade STATUS_MODIFY to STATUS_CONFLICT if a
1518 * conflict marker is found in newly added lines only.
1520 static const struct got_error *
1521 get_modified_file_content_status(unsigned char *status,
1522 struct got_blob_object *blob, const char *path, struct stat *sb,
1523 FILE *ondisk_file)
1525 const struct got_error *err, *free_err;
1526 const char *markers[3] = {
1527 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1528 GOT_DIFF_CONFLICT_MARKER_SEP,
1529 GOT_DIFF_CONFLICT_MARKER_END
1531 FILE *f1 = NULL;
1532 struct got_diffreg_result *diffreg_result = NULL;
1533 struct diff_result *r;
1534 int nchunks_parsed, n, i = 0, ln = 0;
1535 char *line = NULL;
1536 size_t linesize = 0;
1537 ssize_t linelen;
1539 if (*status != GOT_STATUS_MODIFY)
1540 return NULL;
1542 f1 = got_opentemp();
1543 if (f1 == NULL)
1544 return got_error_from_errno("got_opentemp");
1546 if (blob) {
1547 got_object_blob_rewind(blob);
1548 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
1549 if (err)
1550 goto done;
1553 err = got_diff_files(&diffreg_result, f1, 1, NULL, ondisk_file,
1554 1, NULL, 0, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
1555 if (err)
1556 goto done;
1558 r = diffreg_result->result;
1560 for (n = 0; n < r->chunks.len; n += nchunks_parsed) {
1561 struct diff_chunk *c;
1562 struct diff_chunk_context cc = {};
1563 off_t pos;
1566 * We can optimise a little by advancing straight
1567 * to the next chunk if this one has no added lines.
1569 c = diff_chunk_get(r, n);
1571 if (diff_chunk_type(c) != CHUNK_PLUS) {
1572 nchunks_parsed = 1;
1573 continue; /* removed or unchanged lines */
1576 pos = diff_chunk_get_right_start_pos(c);
1577 if (fseek(ondisk_file, pos, SEEK_SET) == -1) {
1578 err = got_ferror(ondisk_file, GOT_ERR_IO);
1579 goto done;
1582 diff_chunk_context_load_change(&cc, &nchunks_parsed, r, n, 0);
1583 ln = cc.right.start;
1585 while (ln < cc.right.end) {
1586 linelen = getline(&line, &linesize, ondisk_file);
1587 if (linelen == -1) {
1588 if (feof(ondisk_file))
1589 break;
1590 err = got_ferror(ondisk_file, GOT_ERR_IO);
1591 break;
1594 if (line && strncmp(line, markers[i],
1595 strlen(markers[i])) == 0) {
1596 if (strcmp(markers[i],
1597 GOT_DIFF_CONFLICT_MARKER_END) == 0) {
1598 *status = GOT_STATUS_CONFLICT;
1599 goto done;
1600 } else
1601 i++;
1603 ++ln;
1607 done:
1608 free(line);
1609 if (f1 != NULL && fclose(f1) == EOF && err == NULL)
1610 err = got_error_from_errno("fclose");
1611 free_err = got_diffreg_result_free(diffreg_result);
1612 if (err == NULL)
1613 err = free_err;
1615 return err;
1618 static int
1619 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1621 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1622 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1625 static int
1626 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1628 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1629 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1630 ie->mtime_sec == sb->st_mtim.tv_sec &&
1631 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1632 ie->size == (sb->st_size & 0xffffffff) &&
1633 !xbit_differs(ie, sb->st_mode));
1636 static unsigned char
1637 get_staged_status(struct got_fileindex_entry *ie)
1639 switch (got_fileindex_entry_stage_get(ie)) {
1640 case GOT_FILEIDX_STAGE_ADD:
1641 return GOT_STATUS_ADD;
1642 case GOT_FILEIDX_STAGE_DELETE:
1643 return GOT_STATUS_DELETE;
1644 case GOT_FILEIDX_STAGE_MODIFY:
1645 return GOT_STATUS_MODIFY;
1646 default:
1647 return GOT_STATUS_NO_CHANGE;
1651 static const struct got_error *
1652 get_symlink_modification_status(unsigned char *status,
1653 struct got_fileindex_entry *ie, const char *abspath,
1654 int dirfd, const char *de_name, struct got_blob_object *blob)
1656 const struct got_error *err = NULL;
1657 char target_path[PATH_MAX];
1658 char etarget[PATH_MAX];
1659 ssize_t elen;
1660 size_t len, target_len = 0;
1661 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1662 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1664 *status = GOT_STATUS_NO_CHANGE;
1666 /* Blob object content specifies the target path of the link. */
1667 do {
1668 err = got_object_blob_read_block(&len, blob);
1669 if (err)
1670 return err;
1671 if (len + target_len >= sizeof(target_path)) {
1673 * Should not happen. The blob contents were OK
1674 * when this symlink was installed.
1676 return got_error(GOT_ERR_NO_SPACE);
1678 if (len > 0) {
1679 /* Skip blob object header first time around. */
1680 memcpy(target_path + target_len, buf + hdrlen,
1681 len - hdrlen);
1682 target_len += len - hdrlen;
1683 hdrlen = 0;
1685 } while (len != 0);
1686 target_path[target_len] = '\0';
1688 if (dirfd != -1) {
1689 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1690 if (elen == -1)
1691 return got_error_from_errno2("readlinkat", abspath);
1692 } else {
1693 elen = readlink(abspath, etarget, sizeof(etarget));
1694 if (elen == -1)
1695 return got_error_from_errno2("readlink", abspath);
1698 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1699 *status = GOT_STATUS_MODIFY;
1701 return NULL;
1704 static const struct got_error *
1705 get_file_status(unsigned char *status, struct stat *sb,
1706 struct got_fileindex_entry *ie, const char *abspath,
1707 int dirfd, const char *de_name, struct got_repository *repo)
1709 const struct got_error *err = NULL;
1710 struct got_object_id id;
1711 size_t hdrlen;
1712 int fd = -1, fd1 = -1;
1713 FILE *f = NULL;
1714 uint8_t fbuf[8192];
1715 struct got_blob_object *blob = NULL;
1716 size_t flen, blen;
1717 unsigned char staged_status;
1719 staged_status = get_staged_status(ie);
1720 *status = GOT_STATUS_NO_CHANGE;
1721 memset(sb, 0, sizeof(*sb));
1724 * Whenever the caller provides a directory descriptor and a
1725 * directory entry name for the file, use them! This prevents
1726 * race conditions if filesystem paths change beneath our feet.
1728 if (dirfd != -1) {
1729 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1730 if (errno == ENOENT) {
1731 if (got_fileindex_entry_has_file_on_disk(ie))
1732 *status = GOT_STATUS_MISSING;
1733 else
1734 *status = GOT_STATUS_DELETE;
1735 goto done;
1737 err = got_error_from_errno2("fstatat", abspath);
1738 goto done;
1740 } else {
1741 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1742 if (fd == -1 && errno != ENOENT &&
1743 !got_err_open_nofollow_on_symlink())
1744 return got_error_from_errno2("open", abspath);
1745 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1746 if (lstat(abspath, sb) == -1)
1747 return got_error_from_errno2("lstat", abspath);
1748 } else if (fd == -1 || fstat(fd, sb) == -1) {
1749 if (errno == ENOENT) {
1750 if (got_fileindex_entry_has_file_on_disk(ie))
1751 *status = GOT_STATUS_MISSING;
1752 else
1753 *status = GOT_STATUS_DELETE;
1754 goto done;
1756 err = got_error_from_errno2("fstat", abspath);
1757 goto done;
1761 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1762 *status = GOT_STATUS_OBSTRUCTED;
1763 goto done;
1766 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1767 *status = GOT_STATUS_DELETE;
1768 goto done;
1769 } else if (!got_fileindex_entry_has_blob(ie) &&
1770 staged_status != GOT_STATUS_ADD) {
1771 *status = GOT_STATUS_ADD;
1772 goto done;
1775 if (!stat_info_differs(ie, sb))
1776 goto done;
1778 if (S_ISLNK(sb->st_mode) &&
1779 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1780 *status = GOT_STATUS_MODIFY;
1781 goto done;
1784 if (staged_status == GOT_STATUS_MODIFY ||
1785 staged_status == GOT_STATUS_ADD)
1786 got_fileindex_entry_get_staged_blob_id(&id, ie);
1787 else
1788 got_fileindex_entry_get_blob_id(&id, ie);
1790 fd1 = got_opentempfd();
1791 if (fd1 == -1) {
1792 err = got_error_from_errno("got_opentempfd");
1793 goto done;
1795 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf), fd1);
1796 if (err)
1797 goto done;
1799 if (S_ISLNK(sb->st_mode)) {
1800 err = get_symlink_modification_status(status, ie,
1801 abspath, dirfd, de_name, blob);
1802 goto done;
1805 if (dirfd != -1) {
1806 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1807 if (fd == -1) {
1808 err = got_error_from_errno2("openat", abspath);
1809 goto done;
1813 f = fdopen(fd, "r");
1814 if (f == NULL) {
1815 err = got_error_from_errno2("fdopen", abspath);
1816 goto done;
1818 fd = -1;
1819 hdrlen = got_object_blob_get_hdrlen(blob);
1820 for (;;) {
1821 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1822 err = got_object_blob_read_block(&blen, blob);
1823 if (err)
1824 goto done;
1825 /* Skip length of blob object header first time around. */
1826 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1827 if (flen == 0 && ferror(f)) {
1828 err = got_error_from_errno("fread");
1829 goto done;
1831 if (blen - hdrlen == 0) {
1832 if (flen != 0)
1833 *status = GOT_STATUS_MODIFY;
1834 break;
1835 } else if (flen == 0) {
1836 if (blen - hdrlen != 0)
1837 *status = GOT_STATUS_MODIFY;
1838 break;
1839 } else if (blen - hdrlen == flen) {
1840 /* Skip blob object header first time around. */
1841 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1842 *status = GOT_STATUS_MODIFY;
1843 break;
1845 } else {
1846 *status = GOT_STATUS_MODIFY;
1847 break;
1849 hdrlen = 0;
1852 if (*status == GOT_STATUS_MODIFY) {
1853 rewind(f);
1854 err = get_modified_file_content_status(status, blob, ie->path,
1855 sb, f);
1856 } else if (xbit_differs(ie, sb->st_mode))
1857 *status = GOT_STATUS_MODE_CHANGE;
1858 done:
1859 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
1860 err = got_error_from_errno("close");
1861 if (blob)
1862 got_object_blob_close(blob);
1863 if (f != NULL && fclose(f) == EOF && err == NULL)
1864 err = got_error_from_errno2("fclose", abspath);
1865 if (fd != -1 && close(fd) == -1 && err == NULL)
1866 err = got_error_from_errno2("close", abspath);
1867 return err;
1871 * Update timestamps in the file index if a file is unmodified and
1872 * we had to run a full content comparison to find out.
1874 static const struct got_error *
1875 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1876 struct got_fileindex_entry *ie, struct stat *sb)
1878 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1879 return got_fileindex_entry_update(ie, wt_fd, path,
1880 ie->blob_sha1, ie->commit_sha1, 1);
1882 return NULL;
1885 static const struct got_error *remove_ondisk_file(const char *, const char *);
1887 static const struct got_error *
1888 update_blob(struct got_worktree *worktree,
1889 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1890 struct got_tree_entry *te, const char *path,
1891 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1892 void *progress_arg)
1894 const struct got_error *err = NULL;
1895 struct got_blob_object *blob = NULL;
1896 char *ondisk_path = NULL;
1897 unsigned char status = GOT_STATUS_NO_CHANGE;
1898 struct stat sb;
1899 int fd1 = -1, fd2 = -1;
1901 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1902 return got_error_from_errno("asprintf");
1904 if (ie) {
1905 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1906 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1907 goto done;
1909 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1910 repo);
1911 if (err)
1912 goto done;
1913 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1914 sb.st_mode = got_fileindex_perms_to_st(ie);
1915 } else {
1916 if (stat(ondisk_path, &sb) == -1) {
1917 if (errno != ENOENT && errno != ENOTDIR) {
1918 err = got_error_from_errno2("stat",
1919 ondisk_path);
1920 goto done;
1922 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1923 status = GOT_STATUS_UNVERSIONED;
1924 } else {
1925 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1926 status = GOT_STATUS_UNVERSIONED;
1927 else
1928 status = GOT_STATUS_OBSTRUCTED;
1932 if (status == GOT_STATUS_OBSTRUCTED) {
1933 if (ie)
1934 got_fileindex_entry_mark_skipped(ie);
1935 err = (*progress_cb)(progress_arg, status, path);
1936 goto done;
1938 if (status == GOT_STATUS_CONFLICT) {
1939 if (ie)
1940 got_fileindex_entry_mark_skipped(ie);
1941 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1942 path);
1943 goto done;
1946 if (S_ISDIR(te->mode)) { /* file changing into a directory */
1947 if (status == GOT_STATUS_UNVERSIONED) {
1948 err = (*progress_cb)(progress_arg, status, path);
1949 } else if (status != GOT_STATUS_NO_CHANGE &&
1950 status != GOT_STATUS_DELETE &&
1951 status != GOT_STATUS_NONEXISTENT &&
1952 status != GOT_STATUS_MISSING) {
1953 err = (*progress_cb)(progress_arg,
1954 GOT_STATUS_CANNOT_DELETE, path);
1955 } else if (ie) {
1956 if (status != GOT_STATUS_DELETE &&
1957 status != GOT_STATUS_NONEXISTENT &&
1958 status != GOT_STATUS_MISSING) {
1959 err = remove_ondisk_file(worktree->root_path,
1960 ie->path);
1961 if (err && !(err->code == GOT_ERR_ERRNO &&
1962 errno == ENOENT))
1963 goto done;
1965 got_fileindex_entry_remove(fileindex, ie);
1966 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE,
1967 ie->path);
1969 goto done; /* nothing else to do */
1972 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1973 (S_ISLNK(te->mode) ||
1974 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1976 * This is a regular file or an installed bad symlink.
1977 * If the file index indicates that this file is already
1978 * up-to-date with respect to the repository we can skip
1979 * updating contents of this file.
1981 if (got_fileindex_entry_has_commit(ie) &&
1982 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1983 SHA1_DIGEST_LENGTH) == 0) {
1984 /* Same commit. */
1985 err = sync_timestamps(worktree->root_fd,
1986 path, status, ie, &sb);
1987 if (err)
1988 goto done;
1989 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1990 path);
1991 goto done;
1993 if (got_fileindex_entry_has_blob(ie) &&
1994 memcmp(ie->blob_sha1, te->id.sha1,
1995 SHA1_DIGEST_LENGTH) == 0) {
1996 /* Different commit but the same blob. */
1997 if (got_fileindex_entry_has_commit(ie)) {
1998 /* Update the base commit ID of this file. */
1999 memcpy(ie->commit_sha1,
2000 worktree->base_commit_id->sha1,
2001 sizeof(ie->commit_sha1));
2003 err = sync_timestamps(worktree->root_fd,
2004 path, status, ie, &sb);
2005 if (err)
2006 goto done;
2007 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2008 path);
2009 goto done;
2013 fd1 = got_opentempfd();
2014 if (fd1 == -1) {
2015 err = got_error_from_errno("got_opentempfd");
2016 goto done;
2018 err = got_object_open_as_blob(&blob, repo, &te->id, 8192, fd1);
2019 if (err)
2020 goto done;
2022 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
2023 int update_timestamps;
2024 struct got_blob_object *blob2 = NULL;
2025 char *label_orig = NULL;
2026 if (got_fileindex_entry_has_blob(ie)) {
2027 fd2 = got_opentempfd();
2028 if (fd2 == -1) {
2029 err = got_error_from_errno("got_opentempfd");
2030 goto done;
2032 struct got_object_id id2;
2033 got_fileindex_entry_get_blob_id(&id2, ie);
2034 err = got_object_open_as_blob(&blob2, repo, &id2, 8192,
2035 fd2);
2036 if (err)
2037 goto done;
2039 if (got_fileindex_entry_has_commit(ie)) {
2040 char id_str[SHA1_DIGEST_STRING_LENGTH];
2041 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2042 sizeof(id_str)) == NULL) {
2043 err = got_error_path(id_str,
2044 GOT_ERR_BAD_OBJ_ID_STR);
2045 goto done;
2047 if (asprintf(&label_orig, "%s: commit %s",
2048 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2049 err = got_error_from_errno("asprintf");
2050 goto done;
2053 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2054 char *link_target;
2055 err = got_object_blob_read_to_str(&link_target, blob);
2056 if (err)
2057 goto done;
2058 err = merge_symlink(worktree, blob2, ondisk_path, path,
2059 label_orig, link_target, worktree->base_commit_id,
2060 repo, progress_cb, progress_arg);
2061 free(link_target);
2062 } else {
2063 err = merge_blob(&update_timestamps, worktree, blob2,
2064 ondisk_path, path, sb.st_mode, label_orig, blob,
2065 worktree->base_commit_id, repo,
2066 progress_cb, progress_arg);
2068 free(label_orig);
2069 if (fd2 != -1 && close(fd2) == -1 && err == NULL) {
2070 err = got_error_from_errno("close");
2071 goto done;
2073 if (blob2)
2074 got_object_blob_close(blob2);
2075 if (err)
2076 goto done;
2078 * Do not update timestamps of files with local changes.
2079 * Otherwise, a future status walk would treat them as
2080 * unmodified files again.
2082 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2083 blob->id.sha1, worktree->base_commit_id->sha1,
2084 update_timestamps);
2085 } else if (status == GOT_STATUS_MODE_CHANGE) {
2086 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2087 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2088 } else if (status == GOT_STATUS_DELETE) {
2089 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2090 if (err)
2091 goto done;
2092 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2093 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2094 if (err)
2095 goto done;
2096 } else {
2097 int is_bad_symlink = 0;
2098 if (S_ISLNK(te->mode)) {
2099 err = install_symlink(&is_bad_symlink, worktree,
2100 ondisk_path, path, blob,
2101 status == GOT_STATUS_MISSING, 0,
2102 status == GOT_STATUS_UNVERSIONED, 0,
2103 repo, progress_cb, progress_arg);
2104 } else {
2105 err = install_blob(worktree, ondisk_path, path,
2106 te->mode, sb.st_mode, blob,
2107 status == GOT_STATUS_MISSING, 0, 0,
2108 status == GOT_STATUS_UNVERSIONED, repo,
2109 progress_cb, progress_arg);
2111 if (err)
2112 goto done;
2114 if (ie) {
2115 err = got_fileindex_entry_update(ie,
2116 worktree->root_fd, path, blob->id.sha1,
2117 worktree->base_commit_id->sha1, 1);
2118 } else {
2119 err = create_fileindex_entry(&ie, fileindex,
2120 worktree->base_commit_id, worktree->root_fd, path,
2121 &blob->id);
2123 if (err)
2124 goto done;
2126 if (is_bad_symlink) {
2127 got_fileindex_entry_filetype_set(ie,
2128 GOT_FILEIDX_MODE_BAD_SYMLINK);
2132 if (fd1 != -1 && close(fd1) == -1 && err == NULL) {
2133 err = got_error_from_errno("close");
2134 goto done;
2136 got_object_blob_close(blob);
2137 done:
2138 free(ondisk_path);
2139 return err;
2142 static const struct got_error *
2143 remove_ondisk_file(const char *root_path, const char *path)
2145 const struct got_error *err = NULL;
2146 char *ondisk_path = NULL, *parent = NULL;
2148 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2149 return got_error_from_errno("asprintf");
2151 if (unlink(ondisk_path) == -1) {
2152 if (errno != ENOENT)
2153 err = got_error_from_errno2("unlink", ondisk_path);
2154 } else {
2155 size_t root_len = strlen(root_path);
2156 err = got_path_dirname(&parent, ondisk_path);
2157 if (err)
2158 goto done;
2159 while (got_path_cmp(parent, root_path,
2160 strlen(parent), root_len) != 0) {
2161 free(ondisk_path);
2162 ondisk_path = parent;
2163 parent = NULL;
2164 if (rmdir(ondisk_path) == -1) {
2165 if (errno != ENOTEMPTY)
2166 err = got_error_from_errno2("rmdir",
2167 ondisk_path);
2168 break;
2170 err = got_path_dirname(&parent, ondisk_path);
2171 if (err)
2172 break;
2175 done:
2176 free(ondisk_path);
2177 free(parent);
2178 return err;
2181 static const struct got_error *
2182 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2183 struct got_fileindex_entry *ie, struct got_repository *repo,
2184 got_worktree_checkout_cb progress_cb, void *progress_arg)
2186 const struct got_error *err = NULL;
2187 unsigned char status;
2188 struct stat sb;
2189 char *ondisk_path;
2191 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2192 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2194 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2195 == -1)
2196 return got_error_from_errno("asprintf");
2198 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2199 if (err)
2200 goto done;
2202 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2203 char ondisk_target[PATH_MAX];
2204 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2205 sizeof(ondisk_target));
2206 if (ondisk_len == -1) {
2207 err = got_error_from_errno2("readlink", ondisk_path);
2208 goto done;
2210 ondisk_target[ondisk_len] = '\0';
2211 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2212 NULL, NULL, /* XXX pass common ancestor info? */
2213 ondisk_target, ondisk_path);
2214 if (err)
2215 goto done;
2216 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2217 ie->path);
2218 goto done;
2221 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2222 status == GOT_STATUS_ADD) {
2223 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2224 if (err)
2225 goto done;
2227 * Preserve the working file and change the deleted blob's
2228 * entry into a schedule-add entry.
2230 err = got_fileindex_entry_update(ie, worktree->root_fd,
2231 ie->path, NULL, NULL, 0);
2232 } else {
2233 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2234 if (err)
2235 goto done;
2236 if (status == GOT_STATUS_NO_CHANGE) {
2237 err = remove_ondisk_file(worktree->root_path, ie->path);
2238 if (err)
2239 goto done;
2241 got_fileindex_entry_remove(fileindex, ie);
2243 done:
2244 free(ondisk_path);
2245 return err;
2248 struct diff_cb_arg {
2249 struct got_fileindex *fileindex;
2250 struct got_worktree *worktree;
2251 struct got_repository *repo;
2252 got_worktree_checkout_cb progress_cb;
2253 void *progress_arg;
2254 got_cancel_cb cancel_cb;
2255 void *cancel_arg;
2258 static const struct got_error *
2259 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2260 struct got_tree_entry *te, const char *parent_path)
2262 struct diff_cb_arg *a = arg;
2264 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2265 return got_error(GOT_ERR_CANCELLED);
2267 return update_blob(a->worktree, a->fileindex, ie, te,
2268 ie->path, a->repo, a->progress_cb, a->progress_arg);
2271 static const struct got_error *
2272 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2274 struct diff_cb_arg *a = arg;
2276 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2277 return got_error(GOT_ERR_CANCELLED);
2279 return delete_blob(a->worktree, a->fileindex, ie,
2280 a->repo, a->progress_cb, a->progress_arg);
2283 static const struct got_error *
2284 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2286 struct diff_cb_arg *a = arg;
2287 const struct got_error *err;
2288 char *path;
2290 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2291 return got_error(GOT_ERR_CANCELLED);
2293 if (got_object_tree_entry_is_submodule(te))
2294 return NULL;
2296 if (asprintf(&path, "%s%s%s", parent_path,
2297 parent_path[0] ? "/" : "", te->name)
2298 == -1)
2299 return got_error_from_errno("asprintf");
2301 if (S_ISDIR(te->mode))
2302 err = add_dir_on_disk(a->worktree, path);
2303 else
2304 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2305 a->repo, a->progress_cb, a->progress_arg);
2307 free(path);
2308 return err;
2311 const struct got_error *
2312 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2314 uint32_t uuid_status;
2316 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2317 if (uuid_status != uuid_s_ok) {
2318 *uuidstr = NULL;
2319 return got_error_uuid(uuid_status, "uuid_to_string");
2322 return NULL;
2325 static const struct got_error *
2326 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2328 const struct got_error *err = NULL;
2329 char *uuidstr = NULL;
2331 *refname = NULL;
2333 err = got_worktree_get_uuid(&uuidstr, worktree);
2334 if (err)
2335 return err;
2337 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2338 err = got_error_from_errno("asprintf");
2339 *refname = NULL;
2341 free(uuidstr);
2342 return err;
2345 const struct got_error *
2346 got_worktree_get_logmsg_ref_name(char **refname, struct got_worktree *worktree,
2347 const char *prefix)
2349 return get_ref_name(refname, worktree, prefix);
2352 const struct got_error *
2353 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2355 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2358 static const struct got_error *
2359 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2361 return get_ref_name(refname, worktree,
2362 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2365 static const struct got_error *
2366 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2368 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2371 static const struct got_error *
2372 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2374 return get_ref_name(refname, worktree,
2375 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2378 static const struct got_error *
2379 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2381 return get_ref_name(refname, worktree,
2382 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2385 static const struct got_error *
2386 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2388 return get_ref_name(refname, worktree,
2389 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2392 static const struct got_error *
2393 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2395 return get_ref_name(refname, worktree,
2396 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2399 static const struct got_error *
2400 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2402 return get_ref_name(refname, worktree,
2403 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2406 static const struct got_error *
2407 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2409 return get_ref_name(refname, worktree,
2410 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2413 const struct got_error *
2414 got_worktree_get_histedit_script_path(char **path,
2415 struct got_worktree *worktree)
2417 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2418 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2419 *path = NULL;
2420 return got_error_from_errno("asprintf");
2422 return NULL;
2425 static const struct got_error *
2426 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2428 return get_ref_name(refname, worktree,
2429 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2432 static const struct got_error *
2433 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2435 return get_ref_name(refname, worktree,
2436 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2440 * Prevent Git's garbage collector from deleting our base commit by
2441 * setting a reference to our base commit's ID.
2443 static const struct got_error *
2444 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2446 const struct got_error *err = NULL;
2447 struct got_reference *ref = NULL;
2448 char *refname;
2450 err = got_worktree_get_base_ref_name(&refname, worktree);
2451 if (err)
2452 return err;
2454 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2455 if (err)
2456 goto done;
2458 err = got_ref_write(ref, repo);
2459 done:
2460 free(refname);
2461 if (ref)
2462 got_ref_close(ref);
2463 return err;
2466 static const struct got_error *
2467 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2469 const struct got_error *err = NULL;
2471 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2472 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2473 err = got_error_from_errno("asprintf");
2474 *fileindex_path = NULL;
2476 return err;
2480 static const struct got_error *
2481 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2482 struct got_worktree *worktree)
2484 const struct got_error *err = NULL;
2485 FILE *index = NULL;
2487 *fileindex_path = NULL;
2488 *fileindex = got_fileindex_alloc();
2489 if (*fileindex == NULL)
2490 return got_error_from_errno("got_fileindex_alloc");
2492 err = get_fileindex_path(fileindex_path, worktree);
2493 if (err)
2494 goto done;
2496 index = fopen(*fileindex_path, "rbe");
2497 if (index == NULL) {
2498 if (errno != ENOENT)
2499 err = got_error_from_errno2("fopen", *fileindex_path);
2500 } else {
2501 err = got_fileindex_read(*fileindex, index);
2502 if (fclose(index) == EOF && err == NULL)
2503 err = got_error_from_errno("fclose");
2505 done:
2506 if (err) {
2507 free(*fileindex_path);
2508 *fileindex_path = NULL;
2509 got_fileindex_free(*fileindex);
2510 *fileindex = NULL;
2512 return err;
2515 struct bump_base_commit_id_arg {
2516 struct got_object_id *base_commit_id;
2517 const char *path;
2518 size_t path_len;
2519 const char *entry_name;
2520 got_worktree_checkout_cb progress_cb;
2521 void *progress_arg;
2524 static const struct got_error *
2525 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2527 const struct got_error *err;
2528 struct bump_base_commit_id_arg *a = arg;
2530 if (a->entry_name) {
2531 if (strcmp(ie->path, a->path) != 0)
2532 return NULL;
2533 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2534 return NULL;
2536 if (got_fileindex_entry_was_skipped(ie))
2537 return NULL;
2539 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2540 SHA1_DIGEST_LENGTH) == 0)
2541 return NULL;
2543 if (a->progress_cb) {
2544 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2545 ie->path);
2546 if (err)
2547 return err;
2549 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2550 return NULL;
2553 /* Bump base commit ID of all files within an updated part of the work tree. */
2554 static const struct got_error *
2555 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2556 struct got_fileindex *fileindex,
2557 got_worktree_checkout_cb progress_cb, void *progress_arg)
2559 struct bump_base_commit_id_arg bbc_arg;
2561 bbc_arg.base_commit_id = worktree->base_commit_id;
2562 bbc_arg.entry_name = NULL;
2563 bbc_arg.path = "";
2564 bbc_arg.path_len = 0;
2565 bbc_arg.progress_cb = progress_cb;
2566 bbc_arg.progress_arg = progress_arg;
2568 return got_fileindex_for_each_entry_safe(fileindex,
2569 bump_base_commit_id, &bbc_arg);
2572 static const struct got_error *
2573 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2575 const struct got_error *err = NULL;
2576 char *new_fileindex_path = NULL;
2577 FILE *new_index = NULL;
2578 struct timespec timeout;
2580 err = got_opentemp_named(&new_fileindex_path, &new_index,
2581 fileindex_path, "");
2582 if (err)
2583 goto done;
2585 err = got_fileindex_write(fileindex, new_index);
2586 if (err)
2587 goto done;
2589 if (rename(new_fileindex_path, fileindex_path) != 0) {
2590 err = got_error_from_errno3("rename", new_fileindex_path,
2591 fileindex_path);
2592 unlink(new_fileindex_path);
2596 * Sleep for a short amount of time to ensure that files modified after
2597 * this program exits have a different time stamp from the one which
2598 * was recorded in the file index.
2600 timeout.tv_sec = 0;
2601 timeout.tv_nsec = 1;
2602 nanosleep(&timeout, NULL);
2603 done:
2604 if (new_index)
2605 fclose(new_index);
2606 free(new_fileindex_path);
2607 return err;
2610 static const struct got_error *
2611 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2612 struct got_object_id **tree_id, const char *wt_relpath,
2613 struct got_commit_object *base_commit, struct got_worktree *worktree,
2614 struct got_repository *repo)
2616 const struct got_error *err = NULL;
2617 struct got_object_id *id = NULL;
2618 char *in_repo_path = NULL;
2619 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2621 *entry_type = GOT_OBJ_TYPE_ANY;
2622 *tree_relpath = NULL;
2623 *tree_id = NULL;
2625 if (wt_relpath[0] == '\0') {
2626 /* Check out all files within the work tree. */
2627 *entry_type = GOT_OBJ_TYPE_TREE;
2628 *tree_relpath = strdup("");
2629 if (*tree_relpath == NULL) {
2630 err = got_error_from_errno("strdup");
2631 goto done;
2633 err = got_object_id_by_path(tree_id, repo, base_commit,
2634 worktree->path_prefix);
2635 if (err)
2636 goto done;
2637 return NULL;
2640 /* Check out a subset of files in the work tree. */
2642 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2643 is_root_wt ? "" : "/", wt_relpath) == -1) {
2644 err = got_error_from_errno("asprintf");
2645 goto done;
2648 err = got_object_id_by_path(&id, repo, base_commit, in_repo_path);
2649 if (err)
2650 goto done;
2652 free(in_repo_path);
2653 in_repo_path = NULL;
2655 err = got_object_get_type(entry_type, repo, id);
2656 if (err)
2657 goto done;
2659 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2660 /* Check out a single file. */
2661 if (strchr(wt_relpath, '/') == NULL) {
2662 /* Check out a single file in work tree's root dir. */
2663 in_repo_path = strdup(worktree->path_prefix);
2664 if (in_repo_path == NULL) {
2665 err = got_error_from_errno("strdup");
2666 goto done;
2668 *tree_relpath = strdup("");
2669 if (*tree_relpath == NULL) {
2670 err = got_error_from_errno("strdup");
2671 goto done;
2673 } else {
2674 /* Check out a single file in a subdirectory. */
2675 err = got_path_dirname(tree_relpath, wt_relpath);
2676 if (err)
2677 return err;
2678 if (asprintf(&in_repo_path, "%s%s%s",
2679 worktree->path_prefix, is_root_wt ? "" : "/",
2680 *tree_relpath) == -1) {
2681 err = got_error_from_errno("asprintf");
2682 goto done;
2685 err = got_object_id_by_path(tree_id, repo,
2686 base_commit, in_repo_path);
2687 } else {
2688 /* Check out all files within a subdirectory. */
2689 *tree_id = got_object_id_dup(id);
2690 if (*tree_id == NULL) {
2691 err = got_error_from_errno("got_object_id_dup");
2692 goto done;
2694 *tree_relpath = strdup(wt_relpath);
2695 if (*tree_relpath == NULL) {
2696 err = got_error_from_errno("strdup");
2697 goto done;
2700 done:
2701 free(id);
2702 free(in_repo_path);
2703 if (err) {
2704 *entry_type = GOT_OBJ_TYPE_ANY;
2705 free(*tree_relpath);
2706 *tree_relpath = NULL;
2707 free(*tree_id);
2708 *tree_id = NULL;
2710 return err;
2713 static const struct got_error *
2714 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2715 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2716 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2717 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2719 const struct got_error *err = NULL;
2720 struct got_commit_object *commit = NULL;
2721 struct got_tree_object *tree = NULL;
2722 struct got_fileindex_diff_tree_cb diff_cb;
2723 struct diff_cb_arg arg;
2725 err = ref_base_commit(worktree, repo);
2726 if (err) {
2727 if (!(err->code == GOT_ERR_ERRNO &&
2728 (errno == EACCES || errno == EROFS)))
2729 goto done;
2730 err = (*progress_cb)(progress_arg,
2731 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2732 if (err)
2733 return err;
2736 err = got_object_open_as_commit(&commit, repo,
2737 worktree->base_commit_id);
2738 if (err)
2739 goto done;
2741 err = got_object_open_as_tree(&tree, repo, tree_id);
2742 if (err)
2743 goto done;
2745 if (entry_name &&
2746 got_object_tree_find_entry(tree, entry_name) == NULL) {
2747 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2748 goto done;
2751 diff_cb.diff_old_new = diff_old_new;
2752 diff_cb.diff_old = diff_old;
2753 diff_cb.diff_new = diff_new;
2754 arg.fileindex = fileindex;
2755 arg.worktree = worktree;
2756 arg.repo = repo;
2757 arg.progress_cb = progress_cb;
2758 arg.progress_arg = progress_arg;
2759 arg.cancel_cb = cancel_cb;
2760 arg.cancel_arg = cancel_arg;
2761 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2762 entry_name, repo, &diff_cb, &arg);
2763 done:
2764 if (tree)
2765 got_object_tree_close(tree);
2766 if (commit)
2767 got_object_commit_close(commit);
2768 return err;
2771 const struct got_error *
2772 got_worktree_checkout_files(struct got_worktree *worktree,
2773 struct got_pathlist_head *paths, struct got_repository *repo,
2774 got_worktree_checkout_cb progress_cb, void *progress_arg,
2775 got_cancel_cb cancel_cb, void *cancel_arg)
2777 const struct got_error *err = NULL, *sync_err, *unlockerr;
2778 struct got_commit_object *commit = NULL;
2779 struct got_tree_object *tree = NULL;
2780 struct got_fileindex *fileindex = NULL;
2781 char *fileindex_path = NULL;
2782 struct got_pathlist_entry *pe;
2783 struct tree_path_data {
2784 STAILQ_ENTRY(tree_path_data) entry;
2785 struct got_object_id *tree_id;
2786 int entry_type;
2787 char *relpath;
2788 char *entry_name;
2789 } *tpd = NULL;
2790 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2792 STAILQ_INIT(&tree_paths);
2794 err = lock_worktree(worktree, LOCK_EX);
2795 if (err)
2796 return err;
2798 err = got_object_open_as_commit(&commit, repo,
2799 worktree->base_commit_id);
2800 if (err)
2801 goto done;
2803 /* Map all specified paths to in-repository trees. */
2804 TAILQ_FOREACH(pe, paths, entry) {
2805 tpd = malloc(sizeof(*tpd));
2806 if (tpd == NULL) {
2807 err = got_error_from_errno("malloc");
2808 goto done;
2811 err = find_tree_entry_for_checkout(&tpd->entry_type,
2812 &tpd->relpath, &tpd->tree_id, pe->path, commit,
2813 worktree, repo);
2814 if (err) {
2815 free(tpd);
2816 goto done;
2819 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2820 err = got_path_basename(&tpd->entry_name, pe->path);
2821 if (err) {
2822 free(tpd->relpath);
2823 free(tpd->tree_id);
2824 free(tpd);
2825 goto done;
2827 } else
2828 tpd->entry_name = NULL;
2830 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2834 * Read the file index.
2835 * Checking out files is supposed to be an idempotent operation.
2836 * If the on-disk file index is incomplete we will try to complete it.
2838 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2839 if (err)
2840 goto done;
2842 tpd = STAILQ_FIRST(&tree_paths);
2843 TAILQ_FOREACH(pe, paths, entry) {
2844 struct bump_base_commit_id_arg bbc_arg;
2846 err = checkout_files(worktree, fileindex, tpd->relpath,
2847 tpd->tree_id, tpd->entry_name, repo,
2848 progress_cb, progress_arg, cancel_cb, cancel_arg);
2849 if (err)
2850 break;
2852 bbc_arg.base_commit_id = worktree->base_commit_id;
2853 bbc_arg.entry_name = tpd->entry_name;
2854 bbc_arg.path = pe->path;
2855 bbc_arg.path_len = pe->path_len;
2856 bbc_arg.progress_cb = progress_cb;
2857 bbc_arg.progress_arg = progress_arg;
2858 err = got_fileindex_for_each_entry_safe(fileindex,
2859 bump_base_commit_id, &bbc_arg);
2860 if (err)
2861 break;
2863 tpd = STAILQ_NEXT(tpd, entry);
2865 sync_err = sync_fileindex(fileindex, fileindex_path);
2866 if (sync_err && err == NULL)
2867 err = sync_err;
2868 done:
2869 free(fileindex_path);
2870 if (tree)
2871 got_object_tree_close(tree);
2872 if (commit)
2873 got_object_commit_close(commit);
2874 if (fileindex)
2875 got_fileindex_free(fileindex);
2876 while (!STAILQ_EMPTY(&tree_paths)) {
2877 tpd = STAILQ_FIRST(&tree_paths);
2878 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2879 free(tpd->relpath);
2880 free(tpd->tree_id);
2881 free(tpd);
2883 unlockerr = lock_worktree(worktree, LOCK_SH);
2884 if (unlockerr && err == NULL)
2885 err = unlockerr;
2886 return err;
2889 static const struct got_error *
2890 add_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2891 struct got_fileindex_entry *ie, const char *ondisk_path,
2892 const char *path2, struct got_blob_object *blob2, mode_t mode2,
2893 int restoring_missing_file, int reverting_versioned_file,
2894 int path_is_unversioned, int allow_bad_symlinks,
2895 struct got_repository *repo,
2896 got_worktree_checkout_cb progress_cb, void *progress_arg)
2898 const struct got_error *err = NULL;
2899 int is_bad_symlink = 0;
2901 if (S_ISLNK(mode2)) {
2902 err = install_symlink(&is_bad_symlink,
2903 worktree, ondisk_path, path2, blob2,
2904 restoring_missing_file,
2905 reverting_versioned_file,
2906 path_is_unversioned, allow_bad_symlinks,
2907 repo, progress_cb, progress_arg);
2908 } else {
2909 err = install_blob(worktree, ondisk_path, path2,
2910 mode2, GOT_DEFAULT_FILE_MODE, blob2,
2911 restoring_missing_file, reverting_versioned_file, 0,
2912 path_is_unversioned, repo, progress_cb, progress_arg);
2914 if (err)
2915 return err;
2916 if (ie == NULL) {
2917 /* Adding an unversioned file. */
2918 err = got_fileindex_entry_alloc(&ie, path2);
2919 if (err)
2920 return err;
2921 err = got_fileindex_entry_update(ie,
2922 worktree->root_fd, path2, NULL, NULL, 1);
2923 if (err) {
2924 got_fileindex_entry_free(ie);
2925 return err;
2927 err = got_fileindex_entry_add(fileindex, ie);
2928 if (err) {
2929 got_fileindex_entry_free(ie);
2930 return err;
2932 } else {
2933 /* Re-adding a locally deleted file. */
2934 err = got_fileindex_entry_update(ie,
2935 worktree->root_fd, path2, ie->blob_sha1,
2936 worktree->base_commit_id->sha1, 0);
2937 if (err)
2938 return err;
2941 if (is_bad_symlink) {
2942 got_fileindex_entry_filetype_set(ie,
2943 GOT_FILEIDX_MODE_BAD_SYMLINK);
2946 return NULL;
2949 struct merge_file_cb_arg {
2950 struct got_worktree *worktree;
2951 struct got_fileindex *fileindex;
2952 got_worktree_checkout_cb progress_cb;
2953 void *progress_arg;
2954 got_cancel_cb cancel_cb;
2955 void *cancel_arg;
2956 const char *label_orig;
2957 struct got_object_id *commit_id2;
2958 int allow_bad_symlinks;
2961 static const struct got_error *
2962 merge_file_cb(void *arg, struct got_blob_object *blob1,
2963 struct got_blob_object *blob2, FILE *f1, FILE *f2,
2964 struct got_object_id *id1, struct got_object_id *id2,
2965 const char *path1, const char *path2,
2966 mode_t mode1, mode_t mode2, struct got_repository *repo)
2968 static const struct got_error *err = NULL;
2969 struct merge_file_cb_arg *a = arg;
2970 struct got_fileindex_entry *ie;
2971 char *ondisk_path = NULL;
2972 struct stat sb;
2973 unsigned char status;
2974 int local_changes_subsumed;
2975 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2976 char *id_str = NULL, *label_deriv2 = NULL;
2978 if (blob1 && blob2) {
2979 ie = got_fileindex_entry_get(a->fileindex, path2,
2980 strlen(path2));
2981 if (ie == NULL)
2982 return (*a->progress_cb)(a->progress_arg,
2983 GOT_STATUS_MISSING, path2);
2985 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2986 path2) == -1)
2987 return got_error_from_errno("asprintf");
2989 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2990 repo);
2991 if (err)
2992 goto done;
2994 if (status == GOT_STATUS_DELETE) {
2995 err = (*a->progress_cb)(a->progress_arg,
2996 GOT_STATUS_MERGE, path2);
2997 goto done;
2999 if (status != GOT_STATUS_NO_CHANGE &&
3000 status != GOT_STATUS_MODIFY &&
3001 status != GOT_STATUS_CONFLICT &&
3002 status != GOT_STATUS_ADD) {
3003 err = (*a->progress_cb)(a->progress_arg, status, path2);
3004 goto done;
3007 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
3008 char *link_target2;
3009 err = got_object_blob_read_to_str(&link_target2, blob2);
3010 if (err)
3011 goto done;
3012 err = merge_symlink(a->worktree, blob1, ondisk_path,
3013 path2, a->label_orig, link_target2, a->commit_id2,
3014 repo, a->progress_cb, a->progress_arg);
3015 free(link_target2);
3016 } else {
3017 int fd;
3019 f_orig = got_opentemp();
3020 if (f_orig == NULL) {
3021 err = got_error_from_errno("got_opentemp");
3022 goto done;
3024 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3025 f_orig, blob1);
3026 if (err)
3027 goto done;
3029 f_deriv2 = got_opentemp();
3030 if (f_deriv2 == NULL)
3031 goto done;
3032 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
3033 f_deriv2, blob2);
3034 if (err)
3035 goto done;
3037 fd = open(ondisk_path,
3038 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3039 if (fd == -1) {
3040 err = got_error_from_errno2("open",
3041 ondisk_path);
3042 goto done;
3044 f_deriv = fdopen(fd, "r");
3045 if (f_deriv == NULL) {
3046 err = got_error_from_errno2("fdopen",
3047 ondisk_path);
3048 close(fd);
3049 goto done;
3051 err = got_object_id_str(&id_str, a->commit_id2);
3052 if (err)
3053 goto done;
3054 if (asprintf(&label_deriv2, "%s: commit %s",
3055 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
3056 err = got_error_from_errno("asprintf");
3057 goto done;
3059 err = merge_file(&local_changes_subsumed, a->worktree,
3060 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
3061 mode2, a->label_orig, NULL, label_deriv2,
3062 GOT_DIFF_ALGORITHM_PATIENCE, repo,
3063 a->progress_cb, a->progress_arg);
3065 } else if (blob1) {
3066 ie = got_fileindex_entry_get(a->fileindex, path1,
3067 strlen(path1));
3068 if (ie == NULL)
3069 return (*a->progress_cb)(a->progress_arg,
3070 GOT_STATUS_MISSING, path1);
3072 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3073 path1) == -1)
3074 return got_error_from_errno("asprintf");
3076 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
3077 repo);
3078 if (err)
3079 goto done;
3081 switch (status) {
3082 case GOT_STATUS_NO_CHANGE:
3083 err = (*a->progress_cb)(a->progress_arg,
3084 GOT_STATUS_DELETE, path1);
3085 if (err)
3086 goto done;
3087 err = remove_ondisk_file(a->worktree->root_path, path1);
3088 if (err)
3089 goto done;
3090 if (ie)
3091 got_fileindex_entry_mark_deleted_from_disk(ie);
3092 break;
3093 case GOT_STATUS_DELETE:
3094 case GOT_STATUS_MISSING:
3095 err = (*a->progress_cb)(a->progress_arg,
3096 GOT_STATUS_DELETE, path1);
3097 if (err)
3098 goto done;
3099 if (ie)
3100 got_fileindex_entry_mark_deleted_from_disk(ie);
3101 break;
3102 case GOT_STATUS_ADD: {
3103 struct got_object_id *id;
3104 FILE *blob1_f;
3105 off_t blob1_size;
3107 * Delete the added file only if its content already
3108 * exists in the repository.
3110 err = got_object_blob_file_create(&id, &blob1_f,
3111 &blob1_size, path1);
3112 if (err)
3113 goto done;
3114 if (got_object_id_cmp(id, id1) == 0) {
3115 err = (*a->progress_cb)(a->progress_arg,
3116 GOT_STATUS_DELETE, path1);
3117 if (err)
3118 goto done;
3119 err = remove_ondisk_file(a->worktree->root_path,
3120 path1);
3121 if (err)
3122 goto done;
3123 if (ie)
3124 got_fileindex_entry_remove(a->fileindex,
3125 ie);
3126 } else {
3127 err = (*a->progress_cb)(a->progress_arg,
3128 GOT_STATUS_CANNOT_DELETE, path1);
3130 if (fclose(blob1_f) == EOF && err == NULL)
3131 err = got_error_from_errno("fclose");
3132 free(id);
3133 if (err)
3134 goto done;
3135 break;
3137 case GOT_STATUS_MODIFY:
3138 case GOT_STATUS_CONFLICT:
3139 err = (*a->progress_cb)(a->progress_arg,
3140 GOT_STATUS_CANNOT_DELETE, path1);
3141 if (err)
3142 goto done;
3143 break;
3144 case GOT_STATUS_OBSTRUCTED:
3145 err = (*a->progress_cb)(a->progress_arg, status, path1);
3146 if (err)
3147 goto done;
3148 break;
3149 default:
3150 break;
3152 } else if (blob2) {
3153 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3154 path2) == -1)
3155 return got_error_from_errno("asprintf");
3156 ie = got_fileindex_entry_get(a->fileindex, path2,
3157 strlen(path2));
3158 if (ie) {
3159 err = get_file_status(&status, &sb, ie, ondisk_path,
3160 -1, NULL, repo);
3161 if (err)
3162 goto done;
3163 if (status != GOT_STATUS_NO_CHANGE &&
3164 status != GOT_STATUS_MODIFY &&
3165 status != GOT_STATUS_CONFLICT &&
3166 status != GOT_STATUS_ADD &&
3167 status != GOT_STATUS_DELETE) {
3168 err = (*a->progress_cb)(a->progress_arg,
3169 status, path2);
3170 goto done;
3172 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3173 char *link_target2;
3174 err = got_object_blob_read_to_str(&link_target2,
3175 blob2);
3176 if (err)
3177 goto done;
3178 err = merge_symlink(a->worktree, NULL,
3179 ondisk_path, path2, a->label_orig,
3180 link_target2, a->commit_id2, repo,
3181 a->progress_cb, a->progress_arg);
3182 free(link_target2);
3183 } else if (S_ISREG(sb.st_mode)) {
3184 err = merge_blob(&local_changes_subsumed,
3185 a->worktree, NULL, ondisk_path, path2,
3186 sb.st_mode, a->label_orig, blob2,
3187 a->commit_id2, repo, a->progress_cb,
3188 a->progress_arg);
3189 } else if (status != GOT_STATUS_DELETE) {
3190 err = got_error_path(ondisk_path,
3191 GOT_ERR_FILE_OBSTRUCTED);
3193 if (err)
3194 goto done;
3195 if (status == GOT_STATUS_DELETE) {
3196 /* Re-add file with content from new blob. */
3197 err = add_file(a->worktree, a->fileindex, ie,
3198 ondisk_path, path2, blob2, mode2,
3199 0, 0, 0, a->allow_bad_symlinks,
3200 repo, a->progress_cb, a->progress_arg);
3201 if (err)
3202 goto done;
3204 } else {
3205 err = add_file(a->worktree, a->fileindex, NULL,
3206 ondisk_path, path2, blob2, mode2,
3207 0, 0, 1, a->allow_bad_symlinks,
3208 repo, a->progress_cb, a->progress_arg);
3209 if (err)
3210 goto done;
3213 done:
3214 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3215 err = got_error_from_errno("fclose");
3216 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3217 err = got_error_from_errno("fclose");
3218 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3219 err = got_error_from_errno("fclose");
3220 free(id_str);
3221 free(label_deriv2);
3222 free(ondisk_path);
3223 return err;
3226 static const struct got_error *
3227 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3229 struct got_worktree *worktree = arg;
3231 /* Reject merges into a work tree with mixed base commits. */
3232 if (got_fileindex_entry_has_commit(ie) &&
3233 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3234 SHA1_DIGEST_LENGTH) != 0)
3235 return got_error(GOT_ERR_MIXED_COMMITS);
3237 return NULL;
3240 struct check_merge_conflicts_arg {
3241 struct got_worktree *worktree;
3242 struct got_fileindex *fileindex;
3243 struct got_repository *repo;
3246 static const struct got_error *
3247 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3248 struct got_blob_object *blob2, FILE *f1, FILE *f2,
3249 struct got_object_id *id1, struct got_object_id *id2,
3250 const char *path1, const char *path2,
3251 mode_t mode1, mode_t mode2, struct got_repository *repo)
3253 const struct got_error *err = NULL;
3254 struct check_merge_conflicts_arg *a = arg;
3255 unsigned char status;
3256 struct stat sb;
3257 struct got_fileindex_entry *ie;
3258 const char *path = path2 ? path2 : path1;
3259 struct got_object_id *id = id2 ? id2 : id1;
3260 char *ondisk_path;
3262 if (id == NULL)
3263 return NULL;
3265 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3266 if (ie == NULL)
3267 return NULL;
3269 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3270 == -1)
3271 return got_error_from_errno("asprintf");
3273 /* Reject merges into a work tree with conflicted files. */
3274 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3275 free(ondisk_path);
3276 if (err)
3277 return err;
3278 if (status == GOT_STATUS_CONFLICT)
3279 return got_error(GOT_ERR_CONFLICTS);
3281 return NULL;
3284 static const struct got_error *
3285 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3286 const char *fileindex_path, struct got_object_id *commit_id1,
3287 struct got_object_id *commit_id2, struct got_repository *repo,
3288 got_worktree_checkout_cb progress_cb, void *progress_arg,
3289 got_cancel_cb cancel_cb, void *cancel_arg)
3291 const struct got_error *err = NULL, *sync_err;
3292 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3293 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3294 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3295 struct check_merge_conflicts_arg cmc_arg;
3296 struct merge_file_cb_arg arg;
3297 char *label_orig = NULL;
3298 FILE *f1 = NULL, *f2 = NULL;
3299 int fd1 = -1, fd2 = -1;
3301 if (commit_id1) {
3302 err = got_object_open_as_commit(&commit1, repo, commit_id1);
3303 if (err)
3304 goto done;
3305 err = got_object_id_by_path(&tree_id1, repo, commit1,
3306 worktree->path_prefix);
3307 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3308 goto done;
3310 if (tree_id1) {
3311 char *id_str;
3313 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3314 if (err)
3315 goto done;
3317 err = got_object_id_str(&id_str, commit_id1);
3318 if (err)
3319 goto done;
3321 if (asprintf(&label_orig, "%s: commit %s",
3322 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3323 err = got_error_from_errno("asprintf");
3324 free(id_str);
3325 goto done;
3327 free(id_str);
3329 f1 = got_opentemp();
3330 if (f1 == NULL) {
3331 err = got_error_from_errno("got_opentemp");
3332 goto done;
3336 err = got_object_open_as_commit(&commit2, repo, commit_id2);
3337 if (err)
3338 goto done;
3340 err = got_object_id_by_path(&tree_id2, repo, commit2,
3341 worktree->path_prefix);
3342 if (err)
3343 goto done;
3345 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3346 if (err)
3347 goto done;
3349 f2 = got_opentemp();
3350 if (f2 == NULL) {
3351 err = got_error_from_errno("got_opentemp");
3352 goto done;
3355 fd1 = got_opentempfd();
3356 if (fd1 == -1) {
3357 err = got_error_from_errno("got_opentempfd");
3358 goto done;
3361 fd2 = got_opentempfd();
3362 if (fd2 == -1) {
3363 err = got_error_from_errno("got_opentempfd");
3364 goto done;
3367 cmc_arg.worktree = worktree;
3368 cmc_arg.fileindex = fileindex;
3369 cmc_arg.repo = repo;
3370 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3371 check_merge_conflicts, &cmc_arg, 0);
3372 if (err)
3373 goto done;
3375 arg.worktree = worktree;
3376 arg.fileindex = fileindex;
3377 arg.progress_cb = progress_cb;
3378 arg.progress_arg = progress_arg;
3379 arg.cancel_cb = cancel_cb;
3380 arg.cancel_arg = cancel_arg;
3381 arg.label_orig = label_orig;
3382 arg.commit_id2 = commit_id2;
3383 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3384 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3385 merge_file_cb, &arg, 1);
3386 sync_err = sync_fileindex(fileindex, fileindex_path);
3387 if (sync_err && err == NULL)
3388 err = sync_err;
3389 done:
3390 if (commit1)
3391 got_object_commit_close(commit1);
3392 if (commit2)
3393 got_object_commit_close(commit2);
3394 if (tree1)
3395 got_object_tree_close(tree1);
3396 if (tree2)
3397 got_object_tree_close(tree2);
3398 if (f1 && fclose(f1) == EOF && err == NULL)
3399 err = got_error_from_errno("fclose");
3400 if (f2 && fclose(f2) == EOF && err == NULL)
3401 err = got_error_from_errno("fclose");
3402 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3403 err = got_error_from_errno("close");
3404 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3405 err = got_error_from_errno("close");
3406 free(label_orig);
3407 return err;
3410 const struct got_error *
3411 got_worktree_merge_files(struct got_worktree *worktree,
3412 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3413 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3414 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3416 const struct got_error *err, *unlockerr;
3417 char *fileindex_path = NULL;
3418 struct got_fileindex *fileindex = NULL;
3420 err = lock_worktree(worktree, LOCK_EX);
3421 if (err)
3422 return err;
3424 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3425 if (err)
3426 goto done;
3428 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3429 worktree);
3430 if (err)
3431 goto done;
3433 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3434 commit_id2, repo, progress_cb, progress_arg,
3435 cancel_cb, cancel_arg);
3436 done:
3437 if (fileindex)
3438 got_fileindex_free(fileindex);
3439 free(fileindex_path);
3440 unlockerr = lock_worktree(worktree, LOCK_SH);
3441 if (unlockerr && err == NULL)
3442 err = unlockerr;
3443 return err;
3446 struct diff_dir_cb_arg {
3447 struct got_fileindex *fileindex;
3448 struct got_worktree *worktree;
3449 const char *status_path;
3450 size_t status_path_len;
3451 struct got_repository *repo;
3452 got_worktree_status_cb status_cb;
3453 void *status_arg;
3454 got_cancel_cb cancel_cb;
3455 void *cancel_arg;
3456 /* A pathlist containing per-directory pathlists of ignore patterns. */
3457 struct got_pathlist_head *ignores;
3458 int report_unchanged;
3459 int no_ignores;
3462 static const struct got_error *
3463 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3464 int dirfd, const char *de_name,
3465 got_worktree_status_cb status_cb, void *status_arg,
3466 struct got_repository *repo, int report_unchanged)
3468 const struct got_error *err = NULL;
3469 unsigned char status = GOT_STATUS_NO_CHANGE;
3470 unsigned char staged_status;
3471 struct stat sb;
3472 struct got_object_id blob_id, commit_id, staged_blob_id;
3473 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3474 struct got_object_id *staged_blob_idp = NULL;
3476 staged_status = get_staged_status(ie);
3477 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3478 if (err)
3479 return err;
3481 if (status == GOT_STATUS_NO_CHANGE &&
3482 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3483 return NULL;
3485 if (got_fileindex_entry_has_blob(ie))
3486 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
3487 if (got_fileindex_entry_has_commit(ie))
3488 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
3489 if (staged_status == GOT_STATUS_ADD ||
3490 staged_status == GOT_STATUS_MODIFY) {
3491 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
3492 &staged_blob_id, ie);
3495 return (*status_cb)(status_arg, status, staged_status,
3496 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3499 static const struct got_error *
3500 status_old_new(void *arg, struct got_fileindex_entry *ie,
3501 struct dirent *de, const char *parent_path, int dirfd)
3503 const struct got_error *err = NULL;
3504 struct diff_dir_cb_arg *a = arg;
3505 char *abspath;
3507 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3508 return got_error(GOT_ERR_CANCELLED);
3510 if (got_path_cmp(parent_path, a->status_path,
3511 strlen(parent_path), a->status_path_len) != 0 &&
3512 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3513 return NULL;
3515 if (parent_path[0]) {
3516 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3517 parent_path, de->d_name) == -1)
3518 return got_error_from_errno("asprintf");
3519 } else {
3520 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3521 de->d_name) == -1)
3522 return got_error_from_errno("asprintf");
3525 err = report_file_status(ie, abspath, dirfd, de->d_name,
3526 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3527 free(abspath);
3528 return err;
3531 static const struct got_error *
3532 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3534 struct diff_dir_cb_arg *a = arg;
3535 struct got_object_id blob_id, commit_id;
3536 unsigned char status;
3538 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3539 return got_error(GOT_ERR_CANCELLED);
3541 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3542 return NULL;
3544 got_fileindex_entry_get_blob_id(&blob_id, ie);
3545 got_fileindex_entry_get_commit_id(&commit_id, ie);
3546 if (got_fileindex_entry_has_file_on_disk(ie))
3547 status = GOT_STATUS_MISSING;
3548 else
3549 status = GOT_STATUS_DELETE;
3550 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3551 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3554 static void
3555 free_ignores(struct got_pathlist_head *ignores)
3557 struct got_pathlist_entry *pe;
3559 TAILQ_FOREACH(pe, ignores, entry) {
3560 struct got_pathlist_head *ignorelist = pe->data;
3562 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3564 got_pathlist_free(ignores, GOT_PATHLIST_FREE_PATH);
3567 static const struct got_error *
3568 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3570 const struct got_error *err = NULL;
3571 struct got_pathlist_entry *pe = NULL;
3572 struct got_pathlist_head *ignorelist;
3573 char *line = NULL, *pattern, *dirpath = NULL;
3574 size_t linesize = 0;
3575 ssize_t linelen;
3577 ignorelist = calloc(1, sizeof(*ignorelist));
3578 if (ignorelist == NULL)
3579 return got_error_from_errno("calloc");
3580 TAILQ_INIT(ignorelist);
3582 while ((linelen = getline(&line, &linesize, f)) != -1) {
3583 if (linelen > 0 && line[linelen - 1] == '\n')
3584 line[linelen - 1] = '\0';
3586 /* Git's ignores may contain comments. */
3587 if (line[0] == '#')
3588 continue;
3590 /* Git's negated patterns are not (yet?) supported. */
3591 if (line[0] == '!')
3592 continue;
3594 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3595 line) == -1) {
3596 err = got_error_from_errno("asprintf");
3597 goto done;
3599 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3600 if (err)
3601 goto done;
3603 if (ferror(f)) {
3604 err = got_error_from_errno("getline");
3605 goto done;
3608 dirpath = strdup(path);
3609 if (dirpath == NULL) {
3610 err = got_error_from_errno("strdup");
3611 goto done;
3613 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3614 done:
3615 free(line);
3616 if (err || pe == NULL) {
3617 free(dirpath);
3618 got_pathlist_free(ignorelist, GOT_PATHLIST_FREE_PATH);
3620 return err;
3623 static int
3624 match_path(const char *pattern, size_t pattern_len, const char *path,
3625 int flags)
3627 char buf[PATH_MAX];
3630 * Trailing slashes signify directories.
3631 * Append a * to make such patterns conform to fnmatch rules.
3633 if (pattern_len > 0 && pattern[pattern_len - 1] == '/') {
3634 if (snprintf(buf, sizeof(buf), "%s*", pattern) >= sizeof(buf))
3635 return FNM_NOMATCH; /* XXX */
3637 return fnmatch(buf, path, flags);
3640 return fnmatch(pattern, path, flags);
3643 static int
3644 match_ignores(struct got_pathlist_head *ignores, const char *path)
3646 struct got_pathlist_entry *pe;
3648 /* Handle patterns which match in all directories. */
3649 TAILQ_FOREACH(pe, ignores, entry) {
3650 struct got_pathlist_head *ignorelist = pe->data;
3651 struct got_pathlist_entry *pi;
3653 TAILQ_FOREACH(pi, ignorelist, entry) {
3654 const char *p;
3656 if (pi->path_len < 3 ||
3657 strncmp(pi->path, "**/", 3) != 0)
3658 continue;
3659 p = path;
3660 while (*p) {
3661 if (match_path(pi->path + 3,
3662 pi->path_len - 3, p,
3663 FNM_PATHNAME | FNM_LEADING_DIR)) {
3664 /* Retry in next directory. */
3665 while (*p && *p != '/')
3666 p++;
3667 while (*p == '/')
3668 p++;
3669 continue;
3671 return 1;
3677 * The ignores pathlist contains ignore lists from children before
3678 * parents, so we can find the most specific ignorelist by walking
3679 * ignores backwards.
3681 pe = TAILQ_LAST(ignores, got_pathlist_head);
3682 while (pe) {
3683 if (got_path_is_child(path, pe->path, pe->path_len)) {
3684 struct got_pathlist_head *ignorelist = pe->data;
3685 struct got_pathlist_entry *pi;
3686 TAILQ_FOREACH(pi, ignorelist, entry) {
3687 int flags = FNM_LEADING_DIR;
3688 if (strstr(pi->path, "/**/") == NULL)
3689 flags |= FNM_PATHNAME;
3690 if (match_path(pi->path, pi->path_len,
3691 path, flags))
3692 continue;
3693 return 1;
3696 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3699 return 0;
3702 static const struct got_error *
3703 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3704 const char *path, int dirfd, const char *ignores_filename)
3706 const struct got_error *err = NULL;
3707 char *ignorespath;
3708 int fd = -1;
3709 FILE *ignoresfile = NULL;
3711 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3712 path[0] ? "/" : "", ignores_filename) == -1)
3713 return got_error_from_errno("asprintf");
3715 if (dirfd != -1) {
3716 fd = openat(dirfd, ignores_filename,
3717 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3718 if (fd == -1) {
3719 if (errno != ENOENT && errno != EACCES)
3720 err = got_error_from_errno2("openat",
3721 ignorespath);
3722 } else {
3723 ignoresfile = fdopen(fd, "r");
3724 if (ignoresfile == NULL)
3725 err = got_error_from_errno2("fdopen",
3726 ignorespath);
3727 else {
3728 fd = -1;
3729 err = read_ignores(ignores, path, ignoresfile);
3732 } else {
3733 ignoresfile = fopen(ignorespath, "re");
3734 if (ignoresfile == NULL) {
3735 if (errno != ENOENT && errno != EACCES)
3736 err = got_error_from_errno2("fopen",
3737 ignorespath);
3738 } else
3739 err = read_ignores(ignores, path, ignoresfile);
3742 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3743 err = got_error_from_errno2("fclose", path);
3744 if (fd != -1 && close(fd) == -1 && err == NULL)
3745 err = got_error_from_errno2("close", path);
3746 free(ignorespath);
3747 return err;
3750 static const struct got_error *
3751 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3752 int dirfd)
3754 const struct got_error *err = NULL;
3755 struct diff_dir_cb_arg *a = arg;
3756 char *path = NULL;
3758 if (ignore != NULL)
3759 *ignore = 0;
3761 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3762 return got_error(GOT_ERR_CANCELLED);
3764 if (parent_path[0]) {
3765 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3766 return got_error_from_errno("asprintf");
3767 } else {
3768 path = de->d_name;
3771 if (de->d_type == DT_DIR) {
3772 if (!a->no_ignores && ignore != NULL &&
3773 match_ignores(a->ignores, path))
3774 *ignore = 1;
3775 } else if (!match_ignores(a->ignores, path) &&
3776 got_path_is_child(path, a->status_path, a->status_path_len))
3777 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3778 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3779 if (parent_path[0])
3780 free(path);
3781 return err;
3784 static const struct got_error *
3785 status_traverse(void *arg, const char *path, int dirfd)
3787 const struct got_error *err = NULL;
3788 struct diff_dir_cb_arg *a = arg;
3790 if (a->no_ignores)
3791 return NULL;
3793 err = add_ignores(a->ignores, a->worktree->root_path,
3794 path, dirfd, ".cvsignore");
3795 if (err)
3796 return err;
3798 err = add_ignores(a->ignores, a->worktree->root_path, path,
3799 dirfd, ".gitignore");
3801 return err;
3804 static const struct got_error *
3805 report_single_file_status(const char *path, const char *ondisk_path,
3806 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3807 void *status_arg, struct got_repository *repo, int report_unchanged,
3808 struct got_pathlist_head *ignores, int no_ignores)
3810 struct got_fileindex_entry *ie;
3811 struct stat sb;
3813 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3814 if (ie)
3815 return report_file_status(ie, ondisk_path, -1, NULL,
3816 status_cb, status_arg, repo, report_unchanged);
3818 if (lstat(ondisk_path, &sb) == -1) {
3819 if (errno != ENOENT)
3820 return got_error_from_errno2("lstat", ondisk_path);
3821 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3822 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3825 if (!no_ignores && match_ignores(ignores, path))
3826 return NULL;
3828 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3829 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3830 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3832 return NULL;
3835 static const struct got_error *
3836 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3837 const char *root_path, const char *path)
3839 const struct got_error *err;
3840 char *parent_path, *next_parent_path = NULL;
3842 err = add_ignores(ignores, root_path, "", -1,
3843 ".cvsignore");
3844 if (err)
3845 return err;
3847 err = add_ignores(ignores, root_path, "", -1,
3848 ".gitignore");
3849 if (err)
3850 return err;
3852 err = got_path_dirname(&parent_path, path);
3853 if (err) {
3854 if (err->code == GOT_ERR_BAD_PATH)
3855 return NULL; /* cannot traverse parent */
3856 return err;
3858 for (;;) {
3859 err = add_ignores(ignores, root_path, parent_path, -1,
3860 ".cvsignore");
3861 if (err)
3862 break;
3863 err = add_ignores(ignores, root_path, parent_path, -1,
3864 ".gitignore");
3865 if (err)
3866 break;
3867 err = got_path_dirname(&next_parent_path, parent_path);
3868 if (err) {
3869 if (err->code == GOT_ERR_BAD_PATH)
3870 err = NULL; /* traversed everything */
3871 break;
3873 if (got_path_is_root_dir(parent_path))
3874 break;
3875 free(parent_path);
3876 parent_path = next_parent_path;
3877 next_parent_path = NULL;
3880 free(parent_path);
3881 free(next_parent_path);
3882 return err;
3885 struct find_missing_children_args {
3886 const char *parent_path;
3887 size_t parent_len;
3888 struct got_pathlist_head *children;
3889 got_cancel_cb cancel_cb;
3890 void *cancel_arg;
3893 static const struct got_error *
3894 find_missing_children(void *arg, struct got_fileindex_entry *ie)
3896 const struct got_error *err = NULL;
3897 struct find_missing_children_args *a = arg;
3899 if (a->cancel_cb) {
3900 err = a->cancel_cb(a->cancel_arg);
3901 if (err)
3902 return err;
3905 if (got_path_is_child(ie->path, a->parent_path, a->parent_len))
3906 err = got_pathlist_append(a->children, ie->path, NULL);
3908 return err;
3911 static const struct got_error *
3912 report_children(struct got_pathlist_head *children,
3913 struct got_worktree *worktree, struct got_fileindex *fileindex,
3914 struct got_repository *repo, int is_root_dir, int report_unchanged,
3915 struct got_pathlist_head *ignores, int no_ignores,
3916 got_worktree_status_cb status_cb, void *status_arg,
3917 got_cancel_cb cancel_cb, void *cancel_arg)
3919 const struct got_error *err = NULL;
3920 struct got_pathlist_entry *pe;
3921 char *ondisk_path = NULL;
3923 TAILQ_FOREACH(pe, children, entry) {
3924 if (cancel_cb) {
3925 err = cancel_cb(cancel_arg);
3926 if (err)
3927 break;
3930 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
3931 !is_root_dir ? "/" : "", pe->path) == -1) {
3932 err = got_error_from_errno("asprintf");
3933 ondisk_path = NULL;
3934 break;
3937 err = report_single_file_status(pe->path, ondisk_path,
3938 fileindex, status_cb, status_arg, repo, report_unchanged,
3939 ignores, no_ignores);
3940 if (err)
3941 break;
3943 free(ondisk_path);
3944 ondisk_path = NULL;
3947 free(ondisk_path);
3948 return err;
3951 static const struct got_error *
3952 worktree_status(struct got_worktree *worktree, const char *path,
3953 struct got_fileindex *fileindex, struct got_repository *repo,
3954 got_worktree_status_cb status_cb, void *status_arg,
3955 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3956 int report_unchanged)
3958 const struct got_error *err = NULL;
3959 int fd = -1;
3960 struct got_fileindex_diff_dir_cb fdiff_cb;
3961 struct diff_dir_cb_arg arg;
3962 char *ondisk_path = NULL;
3963 struct got_pathlist_head ignores, missing_children;
3964 struct got_fileindex_entry *ie;
3966 TAILQ_INIT(&ignores);
3967 TAILQ_INIT(&missing_children);
3969 if (asprintf(&ondisk_path, "%s%s%s",
3970 worktree->root_path, path[0] ? "/" : "", path) == -1)
3971 return got_error_from_errno("asprintf");
3973 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3974 if (ie) {
3975 err = report_single_file_status(path, ondisk_path,
3976 fileindex, status_cb, status_arg, repo,
3977 report_unchanged, &ignores, no_ignores);
3978 goto done;
3979 } else {
3980 struct find_missing_children_args fmca;
3981 fmca.parent_path = path;
3982 fmca.parent_len = strlen(path);
3983 fmca.children = &missing_children;
3984 fmca.cancel_cb = cancel_cb;
3985 fmca.cancel_arg = cancel_arg;
3986 err = got_fileindex_for_each_entry_safe(fileindex,
3987 find_missing_children, &fmca);
3988 if (err)
3989 goto done;
3992 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3993 if (fd == -1) {
3994 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3995 !got_err_open_nofollow_on_symlink())
3996 err = got_error_from_errno2("open", ondisk_path);
3997 else {
3998 if (!no_ignores) {
3999 err = add_ignores_from_parent_paths(&ignores,
4000 worktree->root_path, ondisk_path);
4001 if (err)
4002 goto done;
4004 if (TAILQ_EMPTY(&missing_children)) {
4005 err = report_single_file_status(path,
4006 ondisk_path, fileindex,
4007 status_cb, status_arg, repo,
4008 report_unchanged, &ignores, no_ignores);
4009 if (err)
4010 goto done;
4011 } else {
4012 err = report_children(&missing_children,
4013 worktree, fileindex, repo,
4014 (path[0] == '\0'), report_unchanged,
4015 &ignores, no_ignores,
4016 status_cb, status_arg,
4017 cancel_cb, cancel_arg);
4018 if (err)
4019 goto done;
4022 } else {
4023 fdiff_cb.diff_old_new = status_old_new;
4024 fdiff_cb.diff_old = status_old;
4025 fdiff_cb.diff_new = status_new;
4026 fdiff_cb.diff_traverse = status_traverse;
4027 arg.fileindex = fileindex;
4028 arg.worktree = worktree;
4029 arg.status_path = path;
4030 arg.status_path_len = strlen(path);
4031 arg.repo = repo;
4032 arg.status_cb = status_cb;
4033 arg.status_arg = status_arg;
4034 arg.cancel_cb = cancel_cb;
4035 arg.cancel_arg = cancel_arg;
4036 arg.report_unchanged = report_unchanged;
4037 arg.no_ignores = no_ignores;
4038 if (!no_ignores) {
4039 err = add_ignores_from_parent_paths(&ignores,
4040 worktree->root_path, path);
4041 if (err)
4042 goto done;
4044 arg.ignores = &ignores;
4045 err = got_fileindex_diff_dir(fileindex, fd,
4046 worktree->root_path, path, repo, &fdiff_cb, &arg);
4048 done:
4049 free_ignores(&ignores);
4050 if (fd != -1 && close(fd) == -1 && err == NULL)
4051 err = got_error_from_errno("close");
4052 free(ondisk_path);
4053 return err;
4056 const struct got_error *
4057 got_worktree_status(struct got_worktree *worktree,
4058 struct got_pathlist_head *paths, struct got_repository *repo,
4059 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
4060 got_cancel_cb cancel_cb, void *cancel_arg)
4062 const struct got_error *err = NULL;
4063 char *fileindex_path = NULL;
4064 struct got_fileindex *fileindex = NULL;
4065 struct got_pathlist_entry *pe;
4067 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4068 if (err)
4069 return err;
4071 TAILQ_FOREACH(pe, paths, entry) {
4072 err = worktree_status(worktree, pe->path, fileindex, repo,
4073 status_cb, status_arg, cancel_cb, cancel_arg,
4074 no_ignores, 0);
4075 if (err)
4076 break;
4078 free(fileindex_path);
4079 got_fileindex_free(fileindex);
4080 return err;
4083 const struct got_error *
4084 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
4085 const char *arg)
4087 const struct got_error *err = NULL;
4088 char *resolved = NULL, *cwd = NULL, *path = NULL;
4089 size_t len;
4090 struct stat sb;
4091 char *abspath = NULL;
4092 char canonpath[PATH_MAX];
4094 *wt_path = NULL;
4096 cwd = getcwd(NULL, 0);
4097 if (cwd == NULL)
4098 return got_error_from_errno("getcwd");
4100 if (lstat(arg, &sb) == -1) {
4101 if (errno != ENOENT) {
4102 err = got_error_from_errno2("lstat", arg);
4103 goto done;
4105 sb.st_mode = 0;
4107 if (S_ISLNK(sb.st_mode)) {
4109 * We cannot use realpath(3) with symlinks since we want to
4110 * operate on the symlink itself.
4111 * But we can make the path absolute, assuming it is relative
4112 * to the current working directory, and then canonicalize it.
4114 if (!got_path_is_absolute(arg)) {
4115 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4116 err = got_error_from_errno("asprintf");
4117 goto done;
4121 err = got_canonpath(abspath ? abspath : arg, canonpath,
4122 sizeof(canonpath));
4123 if (err)
4124 goto done;
4125 resolved = strdup(canonpath);
4126 if (resolved == NULL) {
4127 err = got_error_from_errno("strdup");
4128 goto done;
4130 } else {
4131 resolved = realpath(arg, NULL);
4132 if (resolved == NULL) {
4133 if (errno != ENOENT) {
4134 err = got_error_from_errno2("realpath", arg);
4135 goto done;
4137 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
4138 err = got_error_from_errno("asprintf");
4139 goto done;
4141 err = got_canonpath(abspath, canonpath,
4142 sizeof(canonpath));
4143 if (err)
4144 goto done;
4145 resolved = strdup(canonpath);
4146 if (resolved == NULL) {
4147 err = got_error_from_errno("strdup");
4148 goto done;
4153 if (strncmp(got_worktree_get_root_path(worktree), resolved,
4154 strlen(got_worktree_get_root_path(worktree)))) {
4155 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
4156 goto done;
4159 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
4160 err = got_path_skip_common_ancestor(&path,
4161 got_worktree_get_root_path(worktree), resolved);
4162 if (err)
4163 goto done;
4164 } else {
4165 path = strdup("");
4166 if (path == NULL) {
4167 err = got_error_from_errno("strdup");
4168 goto done;
4172 /* XXX status walk can't deal with trailing slash! */
4173 len = strlen(path);
4174 while (len > 0 && path[len - 1] == '/') {
4175 path[len - 1] = '\0';
4176 len--;
4178 done:
4179 free(abspath);
4180 free(resolved);
4181 free(cwd);
4182 if (err == NULL)
4183 *wt_path = path;
4184 else
4185 free(path);
4186 return err;
4189 struct schedule_addition_args {
4190 struct got_worktree *worktree;
4191 struct got_fileindex *fileindex;
4192 got_worktree_checkout_cb progress_cb;
4193 void *progress_arg;
4194 struct got_repository *repo;
4197 static int
4198 add_noop_status(unsigned char status)
4200 return (status == GOT_STATUS_ADD ||
4201 status == GOT_STATUS_MODIFY ||
4202 status == GOT_STATUS_CONFLICT ||
4203 status == GOT_STATUS_MODE_CHANGE ||
4204 status == GOT_STATUS_NO_CHANGE);
4207 static const struct got_error *
4208 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4209 const char *relpath, struct got_object_id *blob_id,
4210 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4211 int dirfd, const char *de_name)
4213 struct schedule_addition_args *a = arg;
4214 const struct got_error *err = NULL;
4215 struct got_fileindex_entry *ie;
4216 struct stat sb;
4217 char *ondisk_path;
4219 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4220 relpath) == -1)
4221 return got_error_from_errno("asprintf");
4223 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4224 if (ie) {
4225 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4226 de_name, a->repo);
4227 if (err)
4228 goto done;
4229 /* Re-adding an existing entry is a no-op. */
4230 if (staged_status == GOT_STATUS_NO_CHANGE &&
4231 add_noop_status(status))
4232 goto done;
4233 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4234 if (err)
4235 goto done;
4238 if (status != GOT_STATUS_UNVERSIONED) {
4239 if (status == GOT_STATUS_NONEXISTENT)
4240 err = got_error_set_errno(ENOENT, ondisk_path);
4241 else
4242 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4243 goto done;
4246 err = got_fileindex_entry_alloc(&ie, relpath);
4247 if (err)
4248 goto done;
4249 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4250 relpath, NULL, NULL, 1);
4251 if (err) {
4252 got_fileindex_entry_free(ie);
4253 goto done;
4255 err = got_fileindex_entry_add(a->fileindex, ie);
4256 if (err) {
4257 got_fileindex_entry_free(ie);
4258 goto done;
4260 done:
4261 free(ondisk_path);
4262 if (err)
4263 return err;
4264 if (staged_status == GOT_STATUS_NO_CHANGE && add_noop_status(status))
4265 return NULL;
4266 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4269 const struct got_error *
4270 got_worktree_schedule_add(struct got_worktree *worktree,
4271 struct got_pathlist_head *paths,
4272 got_worktree_checkout_cb progress_cb, void *progress_arg,
4273 struct got_repository *repo, int no_ignores)
4275 struct got_fileindex *fileindex = NULL;
4276 char *fileindex_path = NULL;
4277 const struct got_error *err = NULL, *sync_err, *unlockerr;
4278 struct got_pathlist_entry *pe;
4279 struct schedule_addition_args saa;
4281 err = lock_worktree(worktree, LOCK_EX);
4282 if (err)
4283 return err;
4285 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4286 if (err)
4287 goto done;
4289 saa.worktree = worktree;
4290 saa.fileindex = fileindex;
4291 saa.progress_cb = progress_cb;
4292 saa.progress_arg = progress_arg;
4293 saa.repo = repo;
4295 TAILQ_FOREACH(pe, paths, entry) {
4296 err = worktree_status(worktree, pe->path, fileindex, repo,
4297 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4298 if (err)
4299 break;
4301 sync_err = sync_fileindex(fileindex, fileindex_path);
4302 if (sync_err && err == NULL)
4303 err = sync_err;
4304 done:
4305 free(fileindex_path);
4306 if (fileindex)
4307 got_fileindex_free(fileindex);
4308 unlockerr = lock_worktree(worktree, LOCK_SH);
4309 if (unlockerr && err == NULL)
4310 err = unlockerr;
4311 return err;
4314 struct schedule_deletion_args {
4315 struct got_worktree *worktree;
4316 struct got_fileindex *fileindex;
4317 got_worktree_delete_cb progress_cb;
4318 void *progress_arg;
4319 struct got_repository *repo;
4320 int delete_local_mods;
4321 int keep_on_disk;
4322 int ignore_missing_paths;
4323 const char *status_path;
4324 size_t status_path_len;
4325 const char *status_codes;
4328 static const struct got_error *
4329 schedule_for_deletion(void *arg, unsigned char status,
4330 unsigned char staged_status, const char *relpath,
4331 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4332 struct got_object_id *commit_id, int dirfd, const char *de_name)
4334 struct schedule_deletion_args *a = arg;
4335 const struct got_error *err = NULL;
4336 struct got_fileindex_entry *ie = NULL;
4337 struct stat sb;
4338 char *ondisk_path;
4340 if (status == GOT_STATUS_NONEXISTENT) {
4341 if (a->ignore_missing_paths)
4342 return NULL;
4343 return got_error_set_errno(ENOENT, relpath);
4346 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4347 if (ie == NULL)
4348 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4350 staged_status = get_staged_status(ie);
4351 if (staged_status != GOT_STATUS_NO_CHANGE) {
4352 if (staged_status == GOT_STATUS_DELETE)
4353 return NULL;
4354 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4357 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4358 relpath) == -1)
4359 return got_error_from_errno("asprintf");
4361 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4362 a->repo);
4363 if (err)
4364 goto done;
4366 if (a->status_codes) {
4367 size_t ncodes = strlen(a->status_codes);
4368 int i;
4369 for (i = 0; i < ncodes ; i++) {
4370 if (status == a->status_codes[i])
4371 break;
4373 if (i == ncodes) {
4374 /* Do not delete files in non-matching status. */
4375 free(ondisk_path);
4376 return NULL;
4378 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4379 a->status_codes[i] != GOT_STATUS_MISSING) {
4380 static char msg[64];
4381 snprintf(msg, sizeof(msg),
4382 "invalid status code '%c'", a->status_codes[i]);
4383 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4384 goto done;
4388 if (status != GOT_STATUS_NO_CHANGE) {
4389 if (status == GOT_STATUS_DELETE)
4390 goto done;
4391 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4392 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4393 goto done;
4395 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4396 err = got_error_set_errno(ENOENT, relpath);
4397 goto done;
4399 if (status != GOT_STATUS_MODIFY &&
4400 status != GOT_STATUS_MISSING) {
4401 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4402 goto done;
4406 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4407 size_t root_len;
4409 if (dirfd != -1) {
4410 if (unlinkat(dirfd, de_name, 0) == -1) {
4411 err = got_error_from_errno2("unlinkat",
4412 ondisk_path);
4413 goto done;
4415 } else if (unlink(ondisk_path) == -1) {
4416 err = got_error_from_errno2("unlink", ondisk_path);
4417 goto done;
4420 root_len = strlen(a->worktree->root_path);
4421 do {
4422 char *parent;
4424 err = got_path_dirname(&parent, ondisk_path);
4425 if (err)
4426 goto done;
4427 free(ondisk_path);
4428 ondisk_path = parent;
4429 if (got_path_cmp(ondisk_path, a->status_path,
4430 strlen(ondisk_path), a->status_path_len) != 0 &&
4431 !got_path_is_child(ondisk_path, a->status_path,
4432 a->status_path_len))
4433 break;
4434 if (rmdir(ondisk_path) == -1) {
4435 if (errno != ENOTEMPTY)
4436 err = got_error_from_errno2("rmdir",
4437 ondisk_path);
4438 break;
4440 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4441 strlen(ondisk_path), root_len) != 0);
4444 got_fileindex_entry_mark_deleted_from_disk(ie);
4445 done:
4446 free(ondisk_path);
4447 if (err)
4448 return err;
4449 if (status == GOT_STATUS_DELETE)
4450 return NULL;
4451 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4452 staged_status, relpath);
4455 const struct got_error *
4456 got_worktree_schedule_delete(struct got_worktree *worktree,
4457 struct got_pathlist_head *paths, int delete_local_mods,
4458 const char *status_codes,
4459 got_worktree_delete_cb progress_cb, void *progress_arg,
4460 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4462 struct got_fileindex *fileindex = NULL;
4463 char *fileindex_path = NULL;
4464 const struct got_error *err = NULL, *sync_err, *unlockerr;
4465 struct got_pathlist_entry *pe;
4466 struct schedule_deletion_args sda;
4468 err = lock_worktree(worktree, LOCK_EX);
4469 if (err)
4470 return err;
4472 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4473 if (err)
4474 goto done;
4476 sda.worktree = worktree;
4477 sda.fileindex = fileindex;
4478 sda.progress_cb = progress_cb;
4479 sda.progress_arg = progress_arg;
4480 sda.repo = repo;
4481 sda.delete_local_mods = delete_local_mods;
4482 sda.keep_on_disk = keep_on_disk;
4483 sda.ignore_missing_paths = ignore_missing_paths;
4484 sda.status_codes = status_codes;
4486 TAILQ_FOREACH(pe, paths, entry) {
4487 char *ondisk_status_path;
4489 if (asprintf(&ondisk_status_path, "%s%s%s",
4490 got_worktree_get_root_path(worktree),
4491 pe->path[0] == '\0' ? "" : "/", pe->path) == -1) {
4492 err = got_error_from_errno("asprintf");
4493 goto done;
4495 sda.status_path = ondisk_status_path;
4496 sda.status_path_len = strlen(ondisk_status_path);
4497 err = worktree_status(worktree, pe->path, fileindex, repo,
4498 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4499 free(ondisk_status_path);
4500 if (err)
4501 break;
4503 sync_err = sync_fileindex(fileindex, fileindex_path);
4504 if (sync_err && err == NULL)
4505 err = sync_err;
4506 done:
4507 free(fileindex_path);
4508 if (fileindex)
4509 got_fileindex_free(fileindex);
4510 unlockerr = lock_worktree(worktree, LOCK_SH);
4511 if (unlockerr && err == NULL)
4512 err = unlockerr;
4513 return err;
4516 static const struct got_error *
4517 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4519 const struct got_error *err = NULL;
4520 char *line = NULL;
4521 size_t linesize = 0, n;
4522 ssize_t linelen;
4524 linelen = getline(&line, &linesize, infile);
4525 if (linelen == -1) {
4526 if (ferror(infile)) {
4527 err = got_error_from_errno("getline");
4528 goto done;
4530 return NULL;
4532 if (outfile) {
4533 n = fwrite(line, 1, linelen, outfile);
4534 if (n != linelen) {
4535 err = got_ferror(outfile, GOT_ERR_IO);
4536 goto done;
4539 if (rejectfile) {
4540 n = fwrite(line, 1, linelen, rejectfile);
4541 if (n != linelen)
4542 err = got_ferror(rejectfile, GOT_ERR_IO);
4544 done:
4545 free(line);
4546 return err;
4549 static const struct got_error *
4550 skip_one_line(FILE *f)
4552 char *line = NULL;
4553 size_t linesize = 0;
4554 ssize_t linelen;
4556 linelen = getline(&line, &linesize, f);
4557 if (linelen == -1) {
4558 if (ferror(f))
4559 return got_error_from_errno("getline");
4560 return NULL;
4562 free(line);
4563 return NULL;
4566 static const struct got_error *
4567 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4568 int start_old, int end_old, int start_new, int end_new,
4569 FILE *outfile, FILE *rejectfile)
4571 const struct got_error *err;
4573 /* Copy old file's lines leading up to patch. */
4574 while (!feof(f1) && *line_cur1 < start_old) {
4575 err = copy_one_line(f1, outfile, NULL);
4576 if (err)
4577 return err;
4578 (*line_cur1)++;
4580 /* Skip new file's lines leading up to patch. */
4581 while (!feof(f2) && *line_cur2 < start_new) {
4582 if (rejectfile)
4583 err = copy_one_line(f2, NULL, rejectfile);
4584 else
4585 err = skip_one_line(f2);
4586 if (err)
4587 return err;
4588 (*line_cur2)++;
4590 /* Copy patched lines. */
4591 while (!feof(f2) && *line_cur2 <= end_new) {
4592 err = copy_one_line(f2, outfile, NULL);
4593 if (err)
4594 return err;
4595 (*line_cur2)++;
4597 /* Skip over old file's replaced lines. */
4598 while (!feof(f1) && *line_cur1 <= end_old) {
4599 if (rejectfile)
4600 err = copy_one_line(f1, NULL, rejectfile);
4601 else
4602 err = skip_one_line(f1);
4603 if (err)
4604 return err;
4605 (*line_cur1)++;
4608 return NULL;
4611 static const struct got_error *
4612 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4613 FILE *outfile, FILE *rejectfile)
4615 const struct got_error *err;
4617 if (outfile) {
4618 /* Copy old file's lines until EOF. */
4619 while (!feof(f1)) {
4620 err = copy_one_line(f1, outfile, NULL);
4621 if (err)
4622 return err;
4623 (*line_cur1)++;
4626 if (rejectfile) {
4627 /* Copy new file's lines until EOF. */
4628 while (!feof(f2)) {
4629 err = copy_one_line(f2, NULL, rejectfile);
4630 if (err)
4631 return err;
4632 (*line_cur2)++;
4636 return NULL;
4639 static const struct got_error *
4640 apply_or_reject_change(int *choice, int *nchunks_used,
4641 struct diff_result *diff_result, int n,
4642 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4643 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4644 got_worktree_patch_cb patch_cb, void *patch_arg)
4646 const struct got_error *err = NULL;
4647 struct diff_chunk_context cc = {};
4648 int start_old, end_old, start_new, end_new;
4649 FILE *hunkfile;
4650 struct diff_output_unidiff_state *diff_state;
4651 struct diff_input_info diff_info;
4652 int rc;
4654 *choice = GOT_PATCH_CHOICE_NONE;
4656 /* Get changed line numbers without context lines for copy_change(). */
4657 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4658 start_old = cc.left.start;
4659 end_old = cc.left.end;
4660 start_new = cc.right.start;
4661 end_new = cc.right.end;
4663 /* Get the same change with context lines for display. */
4664 memset(&cc, 0, sizeof(cc));
4665 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4667 memset(&diff_info, 0, sizeof(diff_info));
4668 diff_info.left_path = relpath;
4669 diff_info.right_path = relpath;
4671 diff_state = diff_output_unidiff_state_alloc();
4672 if (diff_state == NULL)
4673 return got_error_set_errno(ENOMEM,
4674 "diff_output_unidiff_state_alloc");
4676 hunkfile = got_opentemp();
4677 if (hunkfile == NULL) {
4678 err = got_error_from_errno("got_opentemp");
4679 goto done;
4682 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4683 diff_result, &cc);
4684 if (rc != DIFF_RC_OK) {
4685 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4686 goto done;
4689 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4690 err = got_ferror(hunkfile, GOT_ERR_IO);
4691 goto done;
4694 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4695 hunkfile, changeno, nchanges);
4696 if (err)
4697 goto done;
4699 switch (*choice) {
4700 case GOT_PATCH_CHOICE_YES:
4701 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4702 end_old, start_new, end_new, outfile, rejectfile);
4703 break;
4704 case GOT_PATCH_CHOICE_NO:
4705 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4706 end_old, start_new, end_new, rejectfile, outfile);
4707 break;
4708 case GOT_PATCH_CHOICE_QUIT:
4709 break;
4710 default:
4711 err = got_error(GOT_ERR_PATCH_CHOICE);
4712 break;
4714 done:
4715 diff_output_unidiff_state_free(diff_state);
4716 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4717 err = got_error_from_errno("fclose");
4718 return err;
4721 struct revert_file_args {
4722 struct got_worktree *worktree;
4723 struct got_fileindex *fileindex;
4724 got_worktree_checkout_cb progress_cb;
4725 void *progress_arg;
4726 got_worktree_patch_cb patch_cb;
4727 void *patch_arg;
4728 struct got_repository *repo;
4729 int unlink_added_files;
4730 struct got_pathlist_head *added_files_to_unlink;
4733 static const struct got_error *
4734 create_patched_content(char **path_outfile, int reverse_patch,
4735 struct got_object_id *blob_id, const char *path2,
4736 int dirfd2, const char *de_name2,
4737 const char *relpath, struct got_repository *repo,
4738 got_worktree_patch_cb patch_cb, void *patch_arg)
4740 const struct got_error *err, *free_err;
4741 struct got_blob_object *blob = NULL;
4742 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4743 int fd = -1, fd2 = -1;
4744 char link_target[PATH_MAX];
4745 ssize_t link_len = 0;
4746 char *path1 = NULL, *id_str = NULL;
4747 struct stat sb2;
4748 struct got_diffreg_result *diffreg_result = NULL;
4749 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4750 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4752 *path_outfile = NULL;
4754 err = got_object_id_str(&id_str, blob_id);
4755 if (err)
4756 return err;
4758 if (dirfd2 != -1) {
4759 fd2 = openat(dirfd2, de_name2,
4760 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4761 if (fd2 == -1) {
4762 if (!got_err_open_nofollow_on_symlink()) {
4763 err = got_error_from_errno2("openat", path2);
4764 goto done;
4766 link_len = readlinkat(dirfd2, de_name2,
4767 link_target, sizeof(link_target));
4768 if (link_len == -1) {
4769 return got_error_from_errno2("readlinkat",
4770 path2);
4772 sb2.st_mode = S_IFLNK;
4773 sb2.st_size = link_len;
4775 } else {
4776 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4777 if (fd2 == -1) {
4778 if (!got_err_open_nofollow_on_symlink()) {
4779 err = got_error_from_errno2("open", path2);
4780 goto done;
4782 link_len = readlink(path2, link_target,
4783 sizeof(link_target));
4784 if (link_len == -1)
4785 return got_error_from_errno2("readlink", path2);
4786 sb2.st_mode = S_IFLNK;
4787 sb2.st_size = link_len;
4790 if (fd2 != -1) {
4791 if (fstat(fd2, &sb2) == -1) {
4792 err = got_error_from_errno2("fstat", path2);
4793 goto done;
4796 f2 = fdopen(fd2, "r");
4797 if (f2 == NULL) {
4798 err = got_error_from_errno2("fdopen", path2);
4799 goto done;
4801 fd2 = -1;
4802 } else {
4803 size_t n;
4804 f2 = got_opentemp();
4805 if (f2 == NULL) {
4806 err = got_error_from_errno2("got_opentemp", path2);
4807 goto done;
4809 n = fwrite(link_target, 1, link_len, f2);
4810 if (n != link_len) {
4811 err = got_ferror(f2, GOT_ERR_IO);
4812 goto done;
4814 if (fflush(f2) == EOF) {
4815 err = got_error_from_errno("fflush");
4816 goto done;
4818 rewind(f2);
4821 fd = got_opentempfd();
4822 if (fd == -1) {
4823 err = got_error_from_errno("got_opentempfd");
4824 goto done;
4827 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4828 if (err)
4829 goto done;
4831 err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4832 if (err)
4833 goto done;
4835 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4836 if (err)
4837 goto done;
4839 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4840 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4841 if (err)
4842 goto done;
4844 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4845 "");
4846 if (err)
4847 goto done;
4849 if (fseek(f1, 0L, SEEK_SET) == -1)
4850 return got_ferror(f1, GOT_ERR_IO);
4851 if (fseek(f2, 0L, SEEK_SET) == -1)
4852 return got_ferror(f2, GOT_ERR_IO);
4854 /* Count the number of actual changes in the diff result. */
4855 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4856 struct diff_chunk_context cc = {};
4857 diff_chunk_context_load_change(&cc, &nchunks_used,
4858 diffreg_result->result, n, 0);
4859 nchanges++;
4861 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4862 int choice;
4863 err = apply_or_reject_change(&choice, &nchunks_used,
4864 diffreg_result->result, n, relpath, f1, f2,
4865 &line_cur1, &line_cur2,
4866 reverse_patch ? NULL : outfile,
4867 reverse_patch ? outfile : NULL,
4868 ++i, nchanges, patch_cb, patch_arg);
4869 if (err)
4870 goto done;
4871 if (choice == GOT_PATCH_CHOICE_YES)
4872 have_content = 1;
4873 else if (choice == GOT_PATCH_CHOICE_QUIT)
4874 break;
4876 if (have_content) {
4877 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4878 reverse_patch ? NULL : outfile,
4879 reverse_patch ? outfile : NULL);
4880 if (err)
4881 goto done;
4883 if (!S_ISLNK(sb2.st_mode)) {
4884 mode_t mode;
4886 mode = apply_umask(sb2.st_mode);
4887 if (fchmod(fileno(outfile), mode) == -1) {
4888 err = got_error_from_errno2("fchmod", path2);
4889 goto done;
4893 done:
4894 free(id_str);
4895 if (fd != -1 && close(fd) == -1 && err == NULL)
4896 err = got_error_from_errno("close");
4897 if (blob)
4898 got_object_blob_close(blob);
4899 free_err = got_diffreg_result_free(diffreg_result);
4900 if (err == NULL)
4901 err = free_err;
4902 if (f1 && fclose(f1) == EOF && err == NULL)
4903 err = got_error_from_errno2("fclose", path1);
4904 if (f2 && fclose(f2) == EOF && err == NULL)
4905 err = got_error_from_errno2("fclose", path2);
4906 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4907 err = got_error_from_errno2("close", path2);
4908 if (outfile && fclose(outfile) == EOF && err == NULL)
4909 err = got_error_from_errno2("fclose", *path_outfile);
4910 if (path1 && unlink(path1) == -1 && err == NULL)
4911 err = got_error_from_errno2("unlink", path1);
4912 if (err || !have_content) {
4913 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4914 err = got_error_from_errno2("unlink", *path_outfile);
4915 free(*path_outfile);
4916 *path_outfile = NULL;
4918 free(path1);
4919 return err;
4922 static const struct got_error *
4923 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4924 const char *relpath, struct got_object_id *blob_id,
4925 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4926 int dirfd, const char *de_name)
4928 struct revert_file_args *a = arg;
4929 const struct got_error *err = NULL;
4930 char *parent_path = NULL;
4931 struct got_fileindex_entry *ie;
4932 struct got_commit_object *base_commit = NULL;
4933 struct got_tree_object *tree = NULL;
4934 struct got_object_id *tree_id = NULL;
4935 const struct got_tree_entry *te = NULL;
4936 char *tree_path = NULL, *te_name;
4937 char *ondisk_path = NULL, *path_content = NULL;
4938 struct got_blob_object *blob = NULL;
4939 int fd = -1;
4941 /* Reverting a staged deletion is a no-op. */
4942 if (status == GOT_STATUS_DELETE &&
4943 staged_status != GOT_STATUS_NO_CHANGE)
4944 return NULL;
4946 if (status == GOT_STATUS_UNVERSIONED)
4947 return (*a->progress_cb)(a->progress_arg,
4948 GOT_STATUS_UNVERSIONED, relpath);
4950 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4951 if (ie == NULL)
4952 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4954 /* Construct in-repository path of tree which contains this blob. */
4955 err = got_path_dirname(&parent_path, ie->path);
4956 if (err) {
4957 if (err->code != GOT_ERR_BAD_PATH)
4958 goto done;
4959 parent_path = strdup("/");
4960 if (parent_path == NULL) {
4961 err = got_error_from_errno("strdup");
4962 goto done;
4965 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4966 tree_path = strdup(parent_path);
4967 if (tree_path == NULL) {
4968 err = got_error_from_errno("strdup");
4969 goto done;
4971 } else {
4972 if (got_path_is_root_dir(parent_path)) {
4973 tree_path = strdup(a->worktree->path_prefix);
4974 if (tree_path == NULL) {
4975 err = got_error_from_errno("strdup");
4976 goto done;
4978 } else {
4979 if (asprintf(&tree_path, "%s/%s",
4980 a->worktree->path_prefix, parent_path) == -1) {
4981 err = got_error_from_errno("asprintf");
4982 goto done;
4987 err = got_object_open_as_commit(&base_commit, a->repo,
4988 a->worktree->base_commit_id);
4989 if (err)
4990 goto done;
4992 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4993 if (err) {
4994 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4995 (status == GOT_STATUS_ADD ||
4996 staged_status == GOT_STATUS_ADD)))
4997 goto done;
4998 } else {
4999 err = got_object_open_as_tree(&tree, a->repo, tree_id);
5000 if (err)
5001 goto done;
5003 err = got_path_basename(&te_name, ie->path);
5004 if (err)
5005 goto done;
5007 te = got_object_tree_find_entry(tree, te_name);
5008 free(te_name);
5009 if (te == NULL && status != GOT_STATUS_ADD &&
5010 staged_status != GOT_STATUS_ADD) {
5011 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
5012 goto done;
5016 switch (status) {
5017 case GOT_STATUS_ADD:
5018 if (a->patch_cb) {
5019 int choice = GOT_PATCH_CHOICE_NONE;
5020 err = (*a->patch_cb)(&choice, a->patch_arg,
5021 status, ie->path, NULL, 1, 1);
5022 if (err)
5023 goto done;
5024 if (choice != GOT_PATCH_CHOICE_YES)
5025 break;
5027 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
5028 ie->path);
5029 if (err)
5030 goto done;
5031 got_fileindex_entry_remove(a->fileindex, ie);
5032 if (a->unlink_added_files) {
5033 int do_unlink = a->added_files_to_unlink ? 0 : 1;
5035 if (a->added_files_to_unlink) {
5036 struct got_pathlist_entry *pe;
5038 TAILQ_FOREACH(pe, a->added_files_to_unlink,
5039 entry) {
5040 if (got_path_cmp(pe->path, relpath,
5041 pe->path_len, strlen(relpath)))
5042 continue;
5043 do_unlink = 1;
5044 break;
5048 if (do_unlink) {
5049 if (asprintf(&ondisk_path, "%s/%s",
5050 got_worktree_get_root_path(a->worktree),
5051 relpath) == -1) {
5052 err = got_error_from_errno("asprintf");
5053 goto done;
5055 if (unlink(ondisk_path) == -1) {
5056 err = got_error_from_errno2("unlink",
5057 ondisk_path);
5058 break;
5062 break;
5063 case GOT_STATUS_DELETE:
5064 if (a->patch_cb) {
5065 int choice = GOT_PATCH_CHOICE_NONE;
5066 err = (*a->patch_cb)(&choice, a->patch_arg,
5067 status, ie->path, NULL, 1, 1);
5068 if (err)
5069 goto done;
5070 if (choice != GOT_PATCH_CHOICE_YES)
5071 break;
5073 /* fall through */
5074 case GOT_STATUS_MODIFY:
5075 case GOT_STATUS_MODE_CHANGE:
5076 case GOT_STATUS_CONFLICT:
5077 case GOT_STATUS_MISSING: {
5078 struct got_object_id id;
5079 if (staged_status == GOT_STATUS_ADD ||
5080 staged_status == GOT_STATUS_MODIFY)
5081 got_fileindex_entry_get_staged_blob_id(&id, ie);
5082 else
5083 got_fileindex_entry_get_blob_id(&id, ie);
5084 fd = got_opentempfd();
5085 if (fd == -1) {
5086 err = got_error_from_errno("got_opentempfd");
5087 goto done;
5090 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
5091 if (err)
5092 goto done;
5094 if (asprintf(&ondisk_path, "%s/%s",
5095 got_worktree_get_root_path(a->worktree), relpath) == -1) {
5096 err = got_error_from_errno("asprintf");
5097 goto done;
5100 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
5101 status == GOT_STATUS_CONFLICT)) {
5102 int is_bad_symlink = 0;
5103 err = create_patched_content(&path_content, 1, &id,
5104 ondisk_path, dirfd, de_name, ie->path, a->repo,
5105 a->patch_cb, a->patch_arg);
5106 if (err || path_content == NULL)
5107 break;
5108 if (te && S_ISLNK(te->mode)) {
5109 if (unlink(path_content) == -1) {
5110 err = got_error_from_errno2("unlink",
5111 path_content);
5112 break;
5114 err = install_symlink(&is_bad_symlink,
5115 a->worktree, ondisk_path, ie->path,
5116 blob, 0, 1, 0, 0, a->repo,
5117 a->progress_cb, a->progress_arg);
5118 } else {
5119 if (rename(path_content, ondisk_path) == -1) {
5120 err = got_error_from_errno3("rename",
5121 path_content, ondisk_path);
5122 goto done;
5125 } else {
5126 int is_bad_symlink = 0;
5127 if (te && S_ISLNK(te->mode)) {
5128 err = install_symlink(&is_bad_symlink,
5129 a->worktree, ondisk_path, ie->path,
5130 blob, 0, 1, 0, 0, a->repo,
5131 a->progress_cb, a->progress_arg);
5132 } else {
5133 err = install_blob(a->worktree, ondisk_path,
5134 ie->path,
5135 te ? te->mode : GOT_DEFAULT_FILE_MODE,
5136 got_fileindex_perms_to_st(ie), blob,
5137 0, 1, 0, 0, a->repo,
5138 a->progress_cb, a->progress_arg);
5140 if (err)
5141 goto done;
5142 if (status == GOT_STATUS_DELETE ||
5143 status == GOT_STATUS_MODE_CHANGE) {
5144 err = got_fileindex_entry_update(ie,
5145 a->worktree->root_fd, relpath,
5146 blob->id.sha1,
5147 a->worktree->base_commit_id->sha1, 1);
5148 if (err)
5149 goto done;
5151 if (is_bad_symlink) {
5152 got_fileindex_entry_filetype_set(ie,
5153 GOT_FILEIDX_MODE_BAD_SYMLINK);
5156 break;
5158 default:
5159 break;
5161 done:
5162 free(ondisk_path);
5163 free(path_content);
5164 free(parent_path);
5165 free(tree_path);
5166 if (fd != -1 && close(fd) == -1 && err == NULL)
5167 err = got_error_from_errno("close");
5168 if (blob)
5169 got_object_blob_close(blob);
5170 if (tree)
5171 got_object_tree_close(tree);
5172 free(tree_id);
5173 if (base_commit)
5174 got_object_commit_close(base_commit);
5175 return err;
5178 const struct got_error *
5179 got_worktree_revert(struct got_worktree *worktree,
5180 struct got_pathlist_head *paths,
5181 got_worktree_checkout_cb progress_cb, void *progress_arg,
5182 got_worktree_patch_cb patch_cb, void *patch_arg,
5183 struct got_repository *repo)
5185 struct got_fileindex *fileindex = NULL;
5186 char *fileindex_path = NULL;
5187 const struct got_error *err = NULL, *unlockerr = NULL;
5188 const struct got_error *sync_err = NULL;
5189 struct got_pathlist_entry *pe;
5190 struct revert_file_args rfa;
5192 err = lock_worktree(worktree, LOCK_EX);
5193 if (err)
5194 return err;
5196 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5197 if (err)
5198 goto done;
5200 rfa.worktree = worktree;
5201 rfa.fileindex = fileindex;
5202 rfa.progress_cb = progress_cb;
5203 rfa.progress_arg = progress_arg;
5204 rfa.patch_cb = patch_cb;
5205 rfa.patch_arg = patch_arg;
5206 rfa.repo = repo;
5207 rfa.unlink_added_files = 0;
5208 TAILQ_FOREACH(pe, paths, entry) {
5209 err = worktree_status(worktree, pe->path, fileindex, repo,
5210 revert_file, &rfa, NULL, NULL, 1, 0);
5211 if (err)
5212 break;
5214 sync_err = sync_fileindex(fileindex, fileindex_path);
5215 if (sync_err && err == NULL)
5216 err = sync_err;
5217 done:
5218 free(fileindex_path);
5219 if (fileindex)
5220 got_fileindex_free(fileindex);
5221 unlockerr = lock_worktree(worktree, LOCK_SH);
5222 if (unlockerr && err == NULL)
5223 err = unlockerr;
5224 return err;
5227 static void
5228 free_commitable(struct got_commitable *ct)
5230 free(ct->path);
5231 free(ct->in_repo_path);
5232 free(ct->ondisk_path);
5233 free(ct->blob_id);
5234 free(ct->base_blob_id);
5235 free(ct->staged_blob_id);
5236 free(ct->base_commit_id);
5237 free(ct);
5240 struct collect_commitables_arg {
5241 struct got_pathlist_head *commitable_paths;
5242 struct got_repository *repo;
5243 struct got_worktree *worktree;
5244 struct got_fileindex *fileindex;
5245 int have_staged_files;
5246 int allow_bad_symlinks;
5247 int diff_header_shown;
5248 int commit_conflicts;
5249 FILE *diff_outfile;
5250 FILE *f1;
5251 FILE *f2;
5255 * Create a file which contains the target path of a symlink so we can feed
5256 * it as content to the diff engine.
5258 static const struct got_error *
5259 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5260 const char *abspath)
5262 const struct got_error *err = NULL;
5263 char target_path[PATH_MAX];
5264 ssize_t target_len, outlen;
5266 *fd = -1;
5268 if (dirfd != -1) {
5269 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5270 if (target_len == -1)
5271 return got_error_from_errno2("readlinkat", abspath);
5272 } else {
5273 target_len = readlink(abspath, target_path, PATH_MAX);
5274 if (target_len == -1)
5275 return got_error_from_errno2("readlink", abspath);
5278 *fd = got_opentempfd();
5279 if (*fd == -1)
5280 return got_error_from_errno("got_opentempfd");
5282 outlen = write(*fd, target_path, target_len);
5283 if (outlen == -1) {
5284 err = got_error_from_errno("got_opentempfd");
5285 goto done;
5288 if (lseek(*fd, 0, SEEK_SET) == -1) {
5289 err = got_error_from_errno2("lseek", abspath);
5290 goto done;
5292 done:
5293 if (err) {
5294 close(*fd);
5295 *fd = -1;
5297 return err;
5300 static const struct got_error *
5301 append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5302 FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5303 int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5305 const struct got_error *err = NULL;
5306 struct got_blob_object *blob1 = NULL;
5307 int fd = -1, fd1 = -1, fd2 = -1;
5308 FILE *ondisk_file = NULL;
5309 char *label1 = NULL;
5310 struct stat sb;
5311 off_t size1 = 0;
5312 int f2_exists = 0;
5313 char *id_str = NULL;
5315 memset(&sb, 0, sizeof(sb));
5317 if (diff_staged) {
5318 if (ct->staged_status != GOT_STATUS_MODIFY &&
5319 ct->staged_status != GOT_STATUS_ADD &&
5320 ct->staged_status != GOT_STATUS_DELETE)
5321 return NULL;
5322 } else {
5323 if (ct->status != GOT_STATUS_MODIFY &&
5324 ct->status != GOT_STATUS_ADD &&
5325 ct->status != GOT_STATUS_DELETE &&
5326 ct->status != GOT_STATUS_CONFLICT)
5327 return NULL;
5330 err = got_opentemp_truncate(f1);
5331 if (err)
5332 return got_error_from_errno("got_opentemp_truncate");
5333 err = got_opentemp_truncate(f2);
5334 if (err)
5335 return got_error_from_errno("got_opentemp_truncate");
5337 if (!*diff_header_shown) {
5338 err = got_object_id_str(&id_str, worktree->base_commit_id);
5339 if (err)
5340 return err;
5341 fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5342 got_worktree_get_root_path(worktree));
5343 fprintf(diff_outfile, "commit - %s\n", id_str);
5344 fprintf(diff_outfile, "path + %s%s\n",
5345 got_worktree_get_root_path(worktree),
5346 diff_staged ? " (staged changes)" : "");
5347 *diff_header_shown = 1;
5350 if (diff_staged) {
5351 const char *label1 = NULL, *label2 = NULL;
5352 switch (ct->staged_status) {
5353 case GOT_STATUS_MODIFY:
5354 label1 = ct->path;
5355 label2 = ct->path;
5356 break;
5357 case GOT_STATUS_ADD:
5358 label2 = ct->path;
5359 break;
5360 case GOT_STATUS_DELETE:
5361 label1 = ct->path;
5362 break;
5363 default:
5364 return got_error(GOT_ERR_FILE_STATUS);
5366 fd1 = got_opentempfd();
5367 if (fd1 == -1) {
5368 err = got_error_from_errno("got_opentempfd");
5369 goto done;
5371 fd2 = got_opentempfd();
5372 if (fd2 == -1) {
5373 err = got_error_from_errno("got_opentempfd");
5374 goto done;
5376 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5377 fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5378 label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5379 NULL, repo, diff_outfile);
5380 goto done;
5383 fd1 = got_opentempfd();
5384 if (fd1 == -1) {
5385 err = got_error_from_errno("got_opentempfd");
5386 goto done;
5389 if (ct->status != GOT_STATUS_ADD) {
5390 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5391 8192, fd1);
5392 if (err)
5393 goto done;
5396 if (ct->status != GOT_STATUS_DELETE) {
5397 if (dirfd != -1) {
5398 fd = openat(dirfd, de_name,
5399 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5400 if (fd == -1) {
5401 if (!got_err_open_nofollow_on_symlink()) {
5402 err = got_error_from_errno2("openat",
5403 ct->ondisk_path);
5404 goto done;
5406 err = get_symlink_target_file(&fd, dirfd,
5407 de_name, ct->ondisk_path);
5408 if (err)
5409 goto done;
5411 } else {
5412 fd = open(ct->ondisk_path,
5413 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5414 if (fd == -1) {
5415 if (!got_err_open_nofollow_on_symlink()) {
5416 err = got_error_from_errno2("open",
5417 ct->ondisk_path);
5418 goto done;
5420 err = get_symlink_target_file(&fd, dirfd,
5421 de_name, ct->ondisk_path);
5422 if (err)
5423 goto done;
5426 if (fstatat(fd, ct->ondisk_path, &sb,
5427 AT_SYMLINK_NOFOLLOW) == -1) {
5428 err = got_error_from_errno2("fstatat", ct->ondisk_path);
5429 goto done;
5431 ondisk_file = fdopen(fd, "r");
5432 if (ondisk_file == NULL) {
5433 err = got_error_from_errno2("fdopen", ct->ondisk_path);
5434 goto done;
5436 fd = -1;
5437 f2_exists = 1;
5440 if (blob1) {
5441 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5442 f1, blob1);
5443 if (err)
5444 goto done;
5447 err = got_diff_blob_file(blob1, f1, size1, label1,
5448 ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5449 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5450 done:
5451 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5452 err = got_error_from_errno("close");
5453 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5454 err = got_error_from_errno("close");
5455 if (blob1)
5456 got_object_blob_close(blob1);
5457 if (fd != -1 && close(fd) == -1 && err == NULL)
5458 err = got_error_from_errno("close");
5459 if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5460 err = got_error_from_errno("fclose");
5461 return err;
5464 static const struct got_error *
5465 collect_commitables(void *arg, unsigned char status,
5466 unsigned char staged_status, const char *relpath,
5467 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5468 struct got_object_id *commit_id, int dirfd, const char *de_name)
5470 struct collect_commitables_arg *a = arg;
5471 const struct got_error *err = NULL;
5472 struct got_commitable *ct = NULL;
5473 struct got_pathlist_entry *new = NULL;
5474 char *parent_path = NULL, *path = NULL;
5475 struct stat sb;
5477 if (a->have_staged_files) {
5478 if (staged_status != GOT_STATUS_MODIFY &&
5479 staged_status != GOT_STATUS_ADD &&
5480 staged_status != GOT_STATUS_DELETE)
5481 return NULL;
5482 } else {
5483 if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5484 printf("C %s\n", relpath);
5485 return got_error(GOT_ERR_COMMIT_CONFLICT);
5488 if (status != GOT_STATUS_MODIFY &&
5489 status != GOT_STATUS_MODE_CHANGE &&
5490 status != GOT_STATUS_ADD &&
5491 status != GOT_STATUS_DELETE &&
5492 status != GOT_STATUS_CONFLICT)
5493 return NULL;
5496 if (asprintf(&path, "/%s", relpath) == -1) {
5497 err = got_error_from_errno("asprintf");
5498 goto done;
5500 if (strcmp(path, "/") == 0) {
5501 parent_path = strdup("");
5502 if (parent_path == NULL)
5503 return got_error_from_errno("strdup");
5504 } else {
5505 err = got_path_dirname(&parent_path, path);
5506 if (err)
5507 return err;
5510 ct = calloc(1, sizeof(*ct));
5511 if (ct == NULL) {
5512 err = got_error_from_errno("calloc");
5513 goto done;
5516 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5517 relpath) == -1) {
5518 err = got_error_from_errno("asprintf");
5519 goto done;
5522 if (staged_status == GOT_STATUS_ADD ||
5523 staged_status == GOT_STATUS_MODIFY) {
5524 struct got_fileindex_entry *ie;
5525 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5526 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5527 case GOT_FILEIDX_MODE_REGULAR_FILE:
5528 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5529 ct->mode = S_IFREG;
5530 break;
5531 case GOT_FILEIDX_MODE_SYMLINK:
5532 ct->mode = S_IFLNK;
5533 break;
5534 default:
5535 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5536 goto done;
5538 ct->mode |= got_fileindex_entry_perms_get(ie);
5539 } else if (status != GOT_STATUS_DELETE &&
5540 staged_status != GOT_STATUS_DELETE) {
5541 if (dirfd != -1) {
5542 if (fstatat(dirfd, de_name, &sb,
5543 AT_SYMLINK_NOFOLLOW) == -1) {
5544 err = got_error_from_errno2("fstatat",
5545 ct->ondisk_path);
5546 goto done;
5548 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5549 err = got_error_from_errno2("lstat", ct->ondisk_path);
5550 goto done;
5552 ct->mode = sb.st_mode;
5555 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5556 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5557 relpath) == -1) {
5558 err = got_error_from_errno("asprintf");
5559 goto done;
5562 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5563 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5564 int is_bad_symlink;
5565 char target_path[PATH_MAX];
5566 ssize_t target_len;
5567 target_len = readlink(ct->ondisk_path, target_path,
5568 sizeof(target_path));
5569 if (target_len == -1) {
5570 err = got_error_from_errno2("readlink",
5571 ct->ondisk_path);
5572 goto done;
5574 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5575 target_len, ct->ondisk_path, a->worktree->root_path);
5576 if (err)
5577 goto done;
5578 if (is_bad_symlink) {
5579 err = got_error_path(ct->ondisk_path,
5580 GOT_ERR_BAD_SYMLINK);
5581 goto done;
5586 ct->status = status;
5587 ct->staged_status = staged_status;
5588 ct->blob_id = NULL; /* will be filled in when blob gets created */
5589 if (ct->status != GOT_STATUS_ADD &&
5590 ct->staged_status != GOT_STATUS_ADD) {
5591 ct->base_blob_id = got_object_id_dup(blob_id);
5592 if (ct->base_blob_id == NULL) {
5593 err = got_error_from_errno("got_object_id_dup");
5594 goto done;
5596 ct->base_commit_id = got_object_id_dup(commit_id);
5597 if (ct->base_commit_id == NULL) {
5598 err = got_error_from_errno("got_object_id_dup");
5599 goto done;
5602 if (ct->staged_status == GOT_STATUS_ADD ||
5603 ct->staged_status == GOT_STATUS_MODIFY) {
5604 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5605 if (ct->staged_blob_id == NULL) {
5606 err = got_error_from_errno("got_object_id_dup");
5607 goto done;
5610 ct->path = strdup(path);
5611 if (ct->path == NULL) {
5612 err = got_error_from_errno("strdup");
5613 goto done;
5615 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5616 if (err)
5617 goto done;
5619 if (a->diff_outfile && ct && new != NULL) {
5620 err = append_ct_diff(ct, &a->diff_header_shown,
5621 a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5622 a->have_staged_files, a->repo, a->worktree);
5623 if (err)
5624 goto done;
5626 done:
5627 if (ct && (err || new == NULL))
5628 free_commitable(ct);
5629 free(parent_path);
5630 free(path);
5631 return err;
5634 static const struct got_error *write_tree(struct got_object_id **, int *,
5635 struct got_tree_object *, const char *, struct got_pathlist_head *,
5636 got_worktree_status_cb status_cb, void *status_arg,
5637 struct got_repository *);
5639 static const struct got_error *
5640 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5641 struct got_tree_entry *te, const char *parent_path,
5642 struct got_pathlist_head *commitable_paths,
5643 got_worktree_status_cb status_cb, void *status_arg,
5644 struct got_repository *repo)
5646 const struct got_error *err = NULL;
5647 struct got_tree_object *subtree;
5648 char *subpath;
5650 if (asprintf(&subpath, "%s%s%s", parent_path,
5651 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5652 return got_error_from_errno("asprintf");
5654 err = got_object_open_as_tree(&subtree, repo, &te->id);
5655 if (err)
5656 return err;
5658 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5659 commitable_paths, status_cb, status_arg, repo);
5660 got_object_tree_close(subtree);
5661 free(subpath);
5662 return err;
5665 static const struct got_error *
5666 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5668 const struct got_error *err = NULL;
5669 char *ct_parent_path = NULL;
5671 *match = 0;
5673 if (strchr(ct->in_repo_path, '/') == NULL) {
5674 *match = got_path_is_root_dir(path);
5675 return NULL;
5678 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5679 if (err)
5680 return err;
5681 *match = (strcmp(path, ct_parent_path) == 0);
5682 free(ct_parent_path);
5683 return err;
5686 static mode_t
5687 get_ct_file_mode(struct got_commitable *ct)
5689 if (S_ISLNK(ct->mode))
5690 return S_IFLNK;
5692 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5695 static const struct got_error *
5696 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5697 struct got_tree_entry *te, struct got_commitable *ct)
5699 const struct got_error *err = NULL;
5701 *new_te = NULL;
5703 err = got_object_tree_entry_dup(new_te, te);
5704 if (err)
5705 goto done;
5707 (*new_te)->mode = get_ct_file_mode(ct);
5709 if (ct->staged_status == GOT_STATUS_MODIFY)
5710 memcpy(&(*new_te)->id, ct->staged_blob_id,
5711 sizeof((*new_te)->id));
5712 else
5713 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5714 done:
5715 if (err && *new_te) {
5716 free(*new_te);
5717 *new_te = NULL;
5719 return err;
5722 static const struct got_error *
5723 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5724 struct got_commitable *ct)
5726 const struct got_error *err = NULL;
5727 char *ct_name = NULL;
5729 *new_te = NULL;
5731 *new_te = calloc(1, sizeof(**new_te));
5732 if (*new_te == NULL)
5733 return got_error_from_errno("calloc");
5735 err = got_path_basename(&ct_name, ct->path);
5736 if (err)
5737 goto done;
5738 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5739 sizeof((*new_te)->name)) {
5740 err = got_error(GOT_ERR_NO_SPACE);
5741 goto done;
5744 (*new_te)->mode = get_ct_file_mode(ct);
5746 if (ct->staged_status == GOT_STATUS_ADD)
5747 memcpy(&(*new_te)->id, ct->staged_blob_id,
5748 sizeof((*new_te)->id));
5749 else
5750 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5751 done:
5752 free(ct_name);
5753 if (err && *new_te) {
5754 free(*new_te);
5755 *new_te = NULL;
5757 return err;
5760 static const struct got_error *
5761 insert_tree_entry(struct got_tree_entry *new_te,
5762 struct got_pathlist_head *paths)
5764 const struct got_error *err = NULL;
5765 struct got_pathlist_entry *new_pe;
5767 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5768 if (err)
5769 return err;
5770 if (new_pe == NULL)
5771 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5772 return NULL;
5775 static const struct got_error *
5776 report_ct_status(struct got_commitable *ct,
5777 got_worktree_status_cb status_cb, void *status_arg)
5779 const char *ct_path = ct->path;
5780 unsigned char status;
5782 if (status_cb == NULL) /* no commit progress output desired */
5783 return NULL;
5785 while (ct_path[0] == '/')
5786 ct_path++;
5788 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5789 status = ct->staged_status;
5790 else
5791 status = ct->status;
5793 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5794 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5797 static const struct got_error *
5798 match_modified_subtree(int *modified, struct got_tree_entry *te,
5799 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5801 const struct got_error *err = NULL;
5802 struct got_pathlist_entry *pe;
5803 char *te_path;
5805 *modified = 0;
5807 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5808 got_path_is_root_dir(base_tree_path) ? "" : "/",
5809 te->name) == -1)
5810 return got_error_from_errno("asprintf");
5812 TAILQ_FOREACH(pe, commitable_paths, entry) {
5813 struct got_commitable *ct = pe->data;
5814 *modified = got_path_is_child(ct->in_repo_path, te_path,
5815 strlen(te_path));
5816 if (*modified)
5817 break;
5820 free(te_path);
5821 return err;
5824 static const struct got_error *
5825 match_deleted_or_modified_ct(struct got_commitable **ctp,
5826 struct got_tree_entry *te, const char *base_tree_path,
5827 struct got_pathlist_head *commitable_paths)
5829 const struct got_error *err = NULL;
5830 struct got_pathlist_entry *pe;
5832 *ctp = NULL;
5834 TAILQ_FOREACH(pe, commitable_paths, entry) {
5835 struct got_commitable *ct = pe->data;
5836 char *ct_name = NULL;
5837 int path_matches;
5839 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5840 if (ct->status != GOT_STATUS_MODIFY &&
5841 ct->status != GOT_STATUS_MODE_CHANGE &&
5842 ct->status != GOT_STATUS_DELETE &&
5843 ct->status != GOT_STATUS_CONFLICT)
5844 continue;
5845 } else {
5846 if (ct->staged_status != GOT_STATUS_MODIFY &&
5847 ct->staged_status != GOT_STATUS_DELETE)
5848 continue;
5851 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5852 continue;
5854 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5855 if (err)
5856 return err;
5857 if (!path_matches)
5858 continue;
5860 err = got_path_basename(&ct_name, pe->path);
5861 if (err)
5862 return err;
5864 if (strcmp(te->name, ct_name) != 0) {
5865 free(ct_name);
5866 continue;
5868 free(ct_name);
5870 *ctp = ct;
5871 break;
5874 return err;
5877 static const struct got_error *
5878 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5879 const char *child_path, const char *path_base_tree,
5880 struct got_pathlist_head *commitable_paths,
5881 got_worktree_status_cb status_cb, void *status_arg,
5882 struct got_repository *repo)
5884 const struct got_error *err = NULL;
5885 struct got_tree_entry *new_te;
5886 char *subtree_path;
5887 struct got_object_id *id = NULL;
5888 int nentries;
5890 *new_tep = NULL;
5892 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5893 got_path_is_root_dir(path_base_tree) ? "" : "/",
5894 child_path) == -1)
5895 return got_error_from_errno("asprintf");
5897 new_te = calloc(1, sizeof(*new_te));
5898 if (new_te == NULL)
5899 return got_error_from_errno("calloc");
5900 new_te->mode = S_IFDIR;
5902 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5903 sizeof(new_te->name)) {
5904 err = got_error(GOT_ERR_NO_SPACE);
5905 goto done;
5907 err = write_tree(&id, &nentries, NULL, subtree_path,
5908 commitable_paths, status_cb, status_arg, repo);
5909 if (err) {
5910 free(new_te);
5911 goto done;
5913 memcpy(&new_te->id, id, sizeof(new_te->id));
5914 done:
5915 free(id);
5916 free(subtree_path);
5917 if (err == NULL)
5918 *new_tep = new_te;
5919 return err;
5922 static const struct got_error *
5923 write_tree(struct got_object_id **new_tree_id, int *nentries,
5924 struct got_tree_object *base_tree, const char *path_base_tree,
5925 struct got_pathlist_head *commitable_paths,
5926 got_worktree_status_cb status_cb, void *status_arg,
5927 struct got_repository *repo)
5929 const struct got_error *err = NULL;
5930 struct got_pathlist_head paths;
5931 struct got_tree_entry *te, *new_te = NULL;
5932 struct got_pathlist_entry *pe;
5934 TAILQ_INIT(&paths);
5935 *nentries = 0;
5937 /* Insert, and recurse into, newly added entries first. */
5938 TAILQ_FOREACH(pe, commitable_paths, entry) {
5939 struct got_commitable *ct = pe->data;
5940 char *child_path = NULL, *slash;
5942 if ((ct->status != GOT_STATUS_ADD &&
5943 ct->staged_status != GOT_STATUS_ADD) ||
5944 (ct->flags & GOT_COMMITABLE_ADDED))
5945 continue;
5947 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5948 strlen(path_base_tree)))
5949 continue;
5951 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5952 ct->in_repo_path);
5953 if (err)
5954 goto done;
5956 slash = strchr(child_path, '/');
5957 if (slash == NULL) {
5958 err = alloc_added_blob_tree_entry(&new_te, ct);
5959 if (err)
5960 goto done;
5961 err = report_ct_status(ct, status_cb, status_arg);
5962 if (err)
5963 goto done;
5964 ct->flags |= GOT_COMMITABLE_ADDED;
5965 err = insert_tree_entry(new_te, &paths);
5966 if (err)
5967 goto done;
5968 (*nentries)++;
5969 } else {
5970 *slash = '\0'; /* trim trailing path components */
5971 if (base_tree == NULL ||
5972 got_object_tree_find_entry(base_tree, child_path)
5973 == NULL) {
5974 err = make_subtree_for_added_blob(&new_te,
5975 child_path, path_base_tree,
5976 commitable_paths, status_cb, status_arg,
5977 repo);
5978 if (err)
5979 goto done;
5980 err = insert_tree_entry(new_te, &paths);
5981 if (err)
5982 goto done;
5983 (*nentries)++;
5988 if (base_tree) {
5989 int i, nbase_entries;
5990 /* Handle modified and deleted entries. */
5991 nbase_entries = got_object_tree_get_nentries(base_tree);
5992 for (i = 0; i < nbase_entries; i++) {
5993 struct got_commitable *ct = NULL;
5995 te = got_object_tree_get_entry(base_tree, i);
5996 if (got_object_tree_entry_is_submodule(te)) {
5997 /* Entry is a submodule; just copy it. */
5998 err = got_object_tree_entry_dup(&new_te, te);
5999 if (err)
6000 goto done;
6001 err = insert_tree_entry(new_te, &paths);
6002 if (err)
6003 goto done;
6004 (*nentries)++;
6005 continue;
6008 if (S_ISDIR(te->mode)) {
6009 int modified;
6010 err = got_object_tree_entry_dup(&new_te, te);
6011 if (err)
6012 goto done;
6013 err = match_modified_subtree(&modified, te,
6014 path_base_tree, commitable_paths);
6015 if (err)
6016 goto done;
6017 /* Avoid recursion into unmodified subtrees. */
6018 if (modified) {
6019 struct got_object_id *new_id;
6020 int nsubentries;
6021 err = write_subtree(&new_id,
6022 &nsubentries, te,
6023 path_base_tree, commitable_paths,
6024 status_cb, status_arg, repo);
6025 if (err)
6026 goto done;
6027 if (nsubentries == 0) {
6028 /* All entries were deleted. */
6029 free(new_id);
6030 continue;
6032 memcpy(&new_te->id, new_id,
6033 sizeof(new_te->id));
6034 free(new_id);
6036 err = insert_tree_entry(new_te, &paths);
6037 if (err)
6038 goto done;
6039 (*nentries)++;
6040 continue;
6043 err = match_deleted_or_modified_ct(&ct, te,
6044 path_base_tree, commitable_paths);
6045 if (err)
6046 goto done;
6047 if (ct) {
6048 /* NB: Deleted entries get dropped here. */
6049 if (ct->status == GOT_STATUS_MODIFY ||
6050 ct->status == GOT_STATUS_MODE_CHANGE ||
6051 ct->status == GOT_STATUS_CONFLICT ||
6052 ct->staged_status == GOT_STATUS_MODIFY) {
6053 err = alloc_modified_blob_tree_entry(
6054 &new_te, te, ct);
6055 if (err)
6056 goto done;
6057 err = insert_tree_entry(new_te, &paths);
6058 if (err)
6059 goto done;
6060 (*nentries)++;
6062 err = report_ct_status(ct, status_cb,
6063 status_arg);
6064 if (err)
6065 goto done;
6066 } else {
6067 /* Entry is unchanged; just copy it. */
6068 err = got_object_tree_entry_dup(&new_te, te);
6069 if (err)
6070 goto done;
6071 err = insert_tree_entry(new_te, &paths);
6072 if (err)
6073 goto done;
6074 (*nentries)++;
6079 /* Write new list of entries; deleted entries have been dropped. */
6080 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
6081 done:
6082 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
6083 return err;
6086 static const struct got_error *
6087 update_fileindex_after_commit(struct got_worktree *worktree,
6088 struct got_pathlist_head *commitable_paths,
6089 struct got_object_id *new_base_commit_id,
6090 struct got_fileindex *fileindex, int have_staged_files)
6092 const struct got_error *err = NULL;
6093 struct got_pathlist_entry *pe;
6094 char *relpath = NULL;
6096 TAILQ_FOREACH(pe, commitable_paths, entry) {
6097 struct got_fileindex_entry *ie;
6098 struct got_commitable *ct = pe->data;
6100 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6102 err = got_path_skip_common_ancestor(&relpath,
6103 worktree->root_path, ct->ondisk_path);
6104 if (err)
6105 goto done;
6107 if (ie) {
6108 if (ct->status == GOT_STATUS_DELETE ||
6109 ct->staged_status == GOT_STATUS_DELETE) {
6110 got_fileindex_entry_remove(fileindex, ie);
6111 } else if (ct->staged_status == GOT_STATUS_ADD ||
6112 ct->staged_status == GOT_STATUS_MODIFY) {
6113 got_fileindex_entry_stage_set(ie,
6114 GOT_FILEIDX_STAGE_NONE);
6115 got_fileindex_entry_staged_filetype_set(ie, 0);
6117 err = got_fileindex_entry_update(ie,
6118 worktree->root_fd, relpath,
6119 ct->staged_blob_id->sha1,
6120 new_base_commit_id->sha1,
6121 !have_staged_files);
6122 } else
6123 err = got_fileindex_entry_update(ie,
6124 worktree->root_fd, relpath,
6125 ct->blob_id->sha1,
6126 new_base_commit_id->sha1,
6127 !have_staged_files);
6128 } else {
6129 err = got_fileindex_entry_alloc(&ie, pe->path);
6130 if (err)
6131 goto done;
6132 err = got_fileindex_entry_update(ie,
6133 worktree->root_fd, relpath, ct->blob_id->sha1,
6134 new_base_commit_id->sha1, 1);
6135 if (err) {
6136 got_fileindex_entry_free(ie);
6137 goto done;
6139 err = got_fileindex_entry_add(fileindex, ie);
6140 if (err) {
6141 got_fileindex_entry_free(ie);
6142 goto done;
6145 free(relpath);
6146 relpath = NULL;
6148 done:
6149 free(relpath);
6150 return err;
6154 static const struct got_error *
6155 check_out_of_date(const char *in_repo_path, unsigned char status,
6156 unsigned char staged_status, struct got_object_id *base_blob_id,
6157 struct got_object_id *base_commit_id,
6158 struct got_object_id *head_commit_id, struct got_repository *repo,
6159 int ood_errcode)
6161 const struct got_error *err = NULL;
6162 struct got_commit_object *commit = NULL;
6163 struct got_object_id *id = NULL;
6165 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
6166 /* Trivial case: base commit == head commit */
6167 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
6168 return NULL;
6170 * Ensure file content which local changes were based
6171 * on matches file content in the branch head.
6173 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6174 if (err)
6175 goto done;
6176 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6177 if (err) {
6178 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6179 err = got_error(ood_errcode);
6180 goto done;
6181 } else if (got_object_id_cmp(id, base_blob_id) != 0)
6182 err = got_error(ood_errcode);
6183 } else {
6184 /* Require that added files don't exist in the branch head. */
6185 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6186 if (err)
6187 goto done;
6188 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6189 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
6190 goto done;
6191 err = id ? got_error(ood_errcode) : NULL;
6193 done:
6194 free(id);
6195 if (commit)
6196 got_object_commit_close(commit);
6197 return err;
6200 static const struct got_error *
6201 commit_worktree(struct got_object_id **new_commit_id,
6202 struct got_pathlist_head *commitable_paths,
6203 struct got_object_id *head_commit_id,
6204 struct got_object_id *parent_id2,
6205 struct got_worktree *worktree,
6206 const char *author, const char *committer, char *diff_path,
6207 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6208 got_worktree_status_cb status_cb, void *status_arg,
6209 struct got_repository *repo)
6211 const struct got_error *err = NULL, *unlockerr = NULL;
6212 struct got_pathlist_entry *pe;
6213 const char *head_ref_name = NULL;
6214 struct got_commit_object *head_commit = NULL;
6215 struct got_reference *head_ref2 = NULL;
6216 struct got_object_id *head_commit_id2 = NULL;
6217 struct got_tree_object *head_tree = NULL;
6218 struct got_object_id *new_tree_id = NULL;
6219 int nentries, nparents = 0;
6220 struct got_object_id_queue parent_ids;
6221 struct got_object_qid *pid = NULL;
6222 char *logmsg = NULL;
6223 time_t timestamp;
6225 *new_commit_id = NULL;
6227 STAILQ_INIT(&parent_ids);
6229 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6230 if (err)
6231 goto done;
6233 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6234 if (err)
6235 goto done;
6237 if (commit_msg_cb != NULL) {
6238 err = commit_msg_cb(commitable_paths, diff_path,
6239 &logmsg, commit_arg);
6240 if (err)
6241 goto done;
6244 if (logmsg == NULL || strlen(logmsg) == 0) {
6245 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6246 goto done;
6249 /* Create blobs from added and modified files and record their IDs. */
6250 TAILQ_FOREACH(pe, commitable_paths, entry) {
6251 struct got_commitable *ct = pe->data;
6252 char *ondisk_path;
6254 /* Blobs for staged files already exist. */
6255 if (ct->staged_status == GOT_STATUS_ADD ||
6256 ct->staged_status == GOT_STATUS_MODIFY)
6257 continue;
6259 if (ct->status != GOT_STATUS_ADD &&
6260 ct->status != GOT_STATUS_MODIFY &&
6261 ct->status != GOT_STATUS_MODE_CHANGE &&
6262 ct->status != GOT_STATUS_CONFLICT)
6263 continue;
6265 if (asprintf(&ondisk_path, "%s/%s",
6266 worktree->root_path, pe->path) == -1) {
6267 err = got_error_from_errno("asprintf");
6268 goto done;
6270 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6271 free(ondisk_path);
6272 if (err)
6273 goto done;
6276 /* Recursively write new tree objects. */
6277 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6278 commitable_paths, status_cb, status_arg, repo);
6279 if (err)
6280 goto done;
6282 err = got_object_qid_alloc(&pid, head_commit_id);
6283 if (err)
6284 goto done;
6285 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6286 nparents++;
6287 if (parent_id2) {
6288 err = got_object_qid_alloc(&pid, parent_id2);
6289 if (err)
6290 goto done;
6291 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6292 nparents++;
6294 timestamp = time(NULL);
6295 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6296 nparents, author, timestamp, committer, timestamp, logmsg, repo);
6297 if (logmsg != NULL)
6298 free(logmsg);
6299 if (err)
6300 goto done;
6302 /* Check if a concurrent commit to our branch has occurred. */
6303 head_ref_name = got_worktree_get_head_ref_name(worktree);
6304 if (head_ref_name == NULL) {
6305 err = got_error_from_errno("got_worktree_get_head_ref_name");
6306 goto done;
6308 /* Lock the reference here to prevent concurrent modification. */
6309 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6310 if (err)
6311 goto done;
6312 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6313 if (err)
6314 goto done;
6315 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6316 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6317 goto done;
6319 /* Update branch head in repository. */
6320 err = got_ref_change_ref(head_ref2, *new_commit_id);
6321 if (err)
6322 goto done;
6323 err = got_ref_write(head_ref2, repo);
6324 if (err)
6325 goto done;
6327 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6328 if (err)
6329 goto done;
6331 err = ref_base_commit(worktree, repo);
6332 if (err)
6333 goto done;
6334 done:
6335 got_object_id_queue_free(&parent_ids);
6336 if (head_tree)
6337 got_object_tree_close(head_tree);
6338 if (head_commit)
6339 got_object_commit_close(head_commit);
6340 free(head_commit_id2);
6341 if (head_ref2) {
6342 unlockerr = got_ref_unlock(head_ref2);
6343 if (unlockerr && err == NULL)
6344 err = unlockerr;
6345 got_ref_close(head_ref2);
6347 return err;
6350 static const struct got_error *
6351 check_path_is_commitable(const char *path,
6352 struct got_pathlist_head *commitable_paths)
6354 struct got_pathlist_entry *cpe = NULL;
6355 size_t path_len = strlen(path);
6357 TAILQ_FOREACH(cpe, commitable_paths, entry) {
6358 struct got_commitable *ct = cpe->data;
6359 const char *ct_path = ct->path;
6361 while (ct_path[0] == '/')
6362 ct_path++;
6364 if (strcmp(path, ct_path) == 0 ||
6365 got_path_is_child(ct_path, path, path_len))
6366 break;
6369 if (cpe == NULL)
6370 return got_error_path(path, GOT_ERR_BAD_PATH);
6372 return NULL;
6375 static const struct got_error *
6376 check_staged_file(void *arg, struct got_fileindex_entry *ie)
6378 int *have_staged_files = arg;
6380 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6381 *have_staged_files = 1;
6382 return got_error(GOT_ERR_CANCELLED);
6385 return NULL;
6388 static const struct got_error *
6389 check_non_staged_files(struct got_fileindex *fileindex,
6390 struct got_pathlist_head *paths)
6392 struct got_pathlist_entry *pe;
6393 struct got_fileindex_entry *ie;
6395 TAILQ_FOREACH(pe, paths, entry) {
6396 if (pe->path[0] == '\0')
6397 continue;
6398 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6399 if (ie == NULL)
6400 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6401 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6402 return got_error_path(pe->path,
6403 GOT_ERR_FILE_NOT_STAGED);
6406 return NULL;
6409 const struct got_error *
6410 got_worktree_commit(struct got_object_id **new_commit_id,
6411 struct got_worktree *worktree, struct got_pathlist_head *paths,
6412 const char *author, const char *committer, int allow_bad_symlinks,
6413 int show_diff, int commit_conflicts,
6414 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6415 got_worktree_status_cb status_cb, void *status_arg,
6416 struct got_repository *repo)
6418 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6419 struct got_fileindex *fileindex = NULL;
6420 char *fileindex_path = NULL;
6421 struct got_pathlist_head commitable_paths;
6422 struct collect_commitables_arg cc_arg;
6423 struct got_pathlist_entry *pe;
6424 struct got_reference *head_ref = NULL;
6425 struct got_object_id *head_commit_id = NULL;
6426 char *diff_path = NULL;
6427 int have_staged_files = 0;
6429 *new_commit_id = NULL;
6431 memset(&cc_arg, 0, sizeof(cc_arg));
6432 TAILQ_INIT(&commitable_paths);
6434 err = lock_worktree(worktree, LOCK_EX);
6435 if (err)
6436 goto done;
6438 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6439 if (err)
6440 goto done;
6442 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6443 if (err)
6444 goto done;
6446 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6447 if (err)
6448 goto done;
6450 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6451 &have_staged_files);
6452 if (err && err->code != GOT_ERR_CANCELLED)
6453 goto done;
6454 if (have_staged_files) {
6455 err = check_non_staged_files(fileindex, paths);
6456 if (err)
6457 goto done;
6460 cc_arg.commitable_paths = &commitable_paths;
6461 cc_arg.worktree = worktree;
6462 cc_arg.fileindex = fileindex;
6463 cc_arg.repo = repo;
6464 cc_arg.have_staged_files = have_staged_files;
6465 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6466 cc_arg.diff_header_shown = 0;
6467 cc_arg.commit_conflicts = commit_conflicts;
6468 if (show_diff) {
6469 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6470 GOT_TMPDIR_STR "/got", ".diff");
6471 if (err)
6472 goto done;
6473 cc_arg.f1 = got_opentemp();
6474 if (cc_arg.f1 == NULL) {
6475 err = got_error_from_errno("got_opentemp");
6476 goto done;
6478 cc_arg.f2 = got_opentemp();
6479 if (cc_arg.f2 == NULL) {
6480 err = got_error_from_errno("got_opentemp");
6481 goto done;
6485 TAILQ_FOREACH(pe, paths, entry) {
6486 err = worktree_status(worktree, pe->path, fileindex, repo,
6487 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6488 if (err)
6489 goto done;
6492 if (show_diff) {
6493 if (fflush(cc_arg.diff_outfile) == EOF) {
6494 err = got_error_from_errno("fflush");
6495 goto done;
6499 if (TAILQ_EMPTY(&commitable_paths)) {
6500 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6501 goto done;
6504 TAILQ_FOREACH(pe, paths, entry) {
6505 err = check_path_is_commitable(pe->path, &commitable_paths);
6506 if (err)
6507 goto done;
6510 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6511 struct got_commitable *ct = pe->data;
6512 const char *ct_path = ct->in_repo_path;
6514 while (ct_path[0] == '/')
6515 ct_path++;
6516 err = check_out_of_date(ct_path, ct->status,
6517 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6518 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6519 if (err)
6520 goto done;
6524 err = commit_worktree(new_commit_id, &commitable_paths,
6525 head_commit_id, NULL, worktree, author, committer,
6526 (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6527 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6528 if (err)
6529 goto done;
6531 err = update_fileindex_after_commit(worktree, &commitable_paths,
6532 *new_commit_id, fileindex, have_staged_files);
6533 sync_err = sync_fileindex(fileindex, fileindex_path);
6534 if (sync_err && err == NULL)
6535 err = sync_err;
6536 done:
6537 if (fileindex)
6538 got_fileindex_free(fileindex);
6539 free(fileindex_path);
6540 unlockerr = lock_worktree(worktree, LOCK_SH);
6541 if (unlockerr && err == NULL)
6542 err = unlockerr;
6543 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6544 struct got_commitable *ct = pe->data;
6546 free_commitable(ct);
6548 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6549 if (diff_path && unlink(diff_path) == -1 && err == NULL)
6550 err = got_error_from_errno2("unlink", diff_path);
6551 free(diff_path);
6552 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6553 err == NULL)
6554 err = got_error_from_errno("fclose");
6555 return err;
6558 const char *
6559 got_commitable_get_path(struct got_commitable *ct)
6561 return ct->path;
6564 unsigned int
6565 got_commitable_get_status(struct got_commitable *ct)
6567 return ct->status;
6570 struct check_rebase_ok_arg {
6571 struct got_worktree *worktree;
6572 struct got_repository *repo;
6575 static const struct got_error *
6576 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6578 const struct got_error *err = NULL;
6579 struct check_rebase_ok_arg *a = arg;
6580 unsigned char status;
6581 struct stat sb;
6582 char *ondisk_path;
6584 /* Reject rebase of a work tree with mixed base commits. */
6585 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6586 SHA1_DIGEST_LENGTH))
6587 return got_error(GOT_ERR_MIXED_COMMITS);
6589 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6590 == -1)
6591 return got_error_from_errno("asprintf");
6593 /* Reject rebase of a work tree with modified or staged files. */
6594 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6595 free(ondisk_path);
6596 if (err)
6597 return err;
6599 if (status != GOT_STATUS_NO_CHANGE)
6600 return got_error(GOT_ERR_MODIFIED);
6601 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6602 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6604 return NULL;
6607 const struct got_error *
6608 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6609 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6610 struct got_worktree *worktree, struct got_reference *branch,
6611 struct got_repository *repo)
6613 const struct got_error *err = NULL;
6614 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6615 char *branch_ref_name = NULL;
6616 char *fileindex_path = NULL;
6617 struct check_rebase_ok_arg ok_arg;
6618 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6619 struct got_object_id *wt_branch_tip = NULL;
6621 *new_base_branch_ref = NULL;
6622 *tmp_branch = NULL;
6623 *fileindex = NULL;
6625 err = lock_worktree(worktree, LOCK_EX);
6626 if (err)
6627 return err;
6629 err = open_fileindex(fileindex, &fileindex_path, worktree);
6630 if (err)
6631 goto done;
6633 ok_arg.worktree = worktree;
6634 ok_arg.repo = repo;
6635 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6636 &ok_arg);
6637 if (err)
6638 goto done;
6640 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6641 if (err)
6642 goto done;
6644 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6645 if (err)
6646 goto done;
6648 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6649 if (err)
6650 goto done;
6652 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6653 0);
6654 if (err)
6655 goto done;
6657 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6658 if (err)
6659 goto done;
6660 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6661 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6662 goto done;
6665 err = got_ref_alloc_symref(new_base_branch_ref,
6666 new_base_branch_ref_name, wt_branch);
6667 if (err)
6668 goto done;
6669 err = got_ref_write(*new_base_branch_ref, repo);
6670 if (err)
6671 goto done;
6673 /* TODO Lock original branch's ref while rebasing? */
6675 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6676 if (err)
6677 goto done;
6679 err = got_ref_write(branch_ref, repo);
6680 if (err)
6681 goto done;
6683 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6684 worktree->base_commit_id);
6685 if (err)
6686 goto done;
6687 err = got_ref_write(*tmp_branch, repo);
6688 if (err)
6689 goto done;
6691 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6692 if (err)
6693 goto done;
6694 done:
6695 free(fileindex_path);
6696 free(tmp_branch_name);
6697 free(new_base_branch_ref_name);
6698 free(branch_ref_name);
6699 if (branch_ref)
6700 got_ref_close(branch_ref);
6701 if (wt_branch)
6702 got_ref_close(wt_branch);
6703 free(wt_branch_tip);
6704 if (err) {
6705 if (*new_base_branch_ref) {
6706 got_ref_close(*new_base_branch_ref);
6707 *new_base_branch_ref = NULL;
6709 if (*tmp_branch) {
6710 got_ref_close(*tmp_branch);
6711 *tmp_branch = NULL;
6713 if (*fileindex) {
6714 got_fileindex_free(*fileindex);
6715 *fileindex = NULL;
6717 lock_worktree(worktree, LOCK_SH);
6719 return err;
6722 const struct got_error *
6723 got_worktree_rebase_continue(struct got_object_id **commit_id,
6724 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6725 struct got_reference **branch, struct got_fileindex **fileindex,
6726 struct got_worktree *worktree, struct got_repository *repo)
6728 const struct got_error *err;
6729 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6730 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6731 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6732 char *fileindex_path = NULL;
6733 int have_staged_files = 0;
6735 *commit_id = NULL;
6736 *new_base_branch = NULL;
6737 *tmp_branch = NULL;
6738 *branch = NULL;
6739 *fileindex = NULL;
6741 err = lock_worktree(worktree, LOCK_EX);
6742 if (err)
6743 return err;
6745 err = open_fileindex(fileindex, &fileindex_path, worktree);
6746 if (err)
6747 goto done;
6749 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6750 &have_staged_files);
6751 if (err && err->code != GOT_ERR_CANCELLED)
6752 goto done;
6753 if (have_staged_files) {
6754 err = got_error(GOT_ERR_STAGED_PATHS);
6755 goto done;
6758 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6759 if (err)
6760 goto done;
6762 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6763 if (err)
6764 goto done;
6766 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6767 if (err)
6768 goto done;
6770 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6771 if (err)
6772 goto done;
6774 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6775 if (err)
6776 goto done;
6778 err = got_ref_open(branch, repo,
6779 got_ref_get_symref_target(branch_ref), 0);
6780 if (err)
6781 goto done;
6783 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6784 if (err)
6785 goto done;
6787 err = got_ref_resolve(commit_id, repo, commit_ref);
6788 if (err)
6789 goto done;
6791 err = got_ref_open(new_base_branch, repo,
6792 new_base_branch_ref_name, 0);
6793 if (err)
6794 goto done;
6796 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6797 if (err)
6798 goto done;
6799 done:
6800 free(commit_ref_name);
6801 free(branch_ref_name);
6802 free(fileindex_path);
6803 if (commit_ref)
6804 got_ref_close(commit_ref);
6805 if (branch_ref)
6806 got_ref_close(branch_ref);
6807 if (err) {
6808 free(*commit_id);
6809 *commit_id = NULL;
6810 if (*tmp_branch) {
6811 got_ref_close(*tmp_branch);
6812 *tmp_branch = NULL;
6814 if (*new_base_branch) {
6815 got_ref_close(*new_base_branch);
6816 *new_base_branch = NULL;
6818 if (*branch) {
6819 got_ref_close(*branch);
6820 *branch = NULL;
6822 if (*fileindex) {
6823 got_fileindex_free(*fileindex);
6824 *fileindex = NULL;
6826 lock_worktree(worktree, LOCK_SH);
6828 return err;
6831 const struct got_error *
6832 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6834 const struct got_error *err;
6835 char *tmp_branch_name = NULL;
6837 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6838 if (err)
6839 return err;
6841 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6842 free(tmp_branch_name);
6843 return NULL;
6846 static const struct got_error *
6847 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6848 const char *diff_path, char **logmsg, void *arg)
6850 *logmsg = arg;
6851 return NULL;
6854 static const struct got_error *
6855 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6856 const char *path, struct got_object_id *blob_id,
6857 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6858 int dirfd, const char *de_name)
6860 return NULL;
6863 struct collect_merged_paths_arg {
6864 got_worktree_checkout_cb progress_cb;
6865 void *progress_arg;
6866 struct got_pathlist_head *merged_paths;
6869 static const struct got_error *
6870 collect_merged_paths(void *arg, unsigned char status, const char *path)
6872 const struct got_error *err;
6873 struct collect_merged_paths_arg *a = arg;
6874 char *p;
6875 struct got_pathlist_entry *new;
6877 err = (*a->progress_cb)(a->progress_arg, status, path);
6878 if (err)
6879 return err;
6881 if (status != GOT_STATUS_MERGE &&
6882 status != GOT_STATUS_ADD &&
6883 status != GOT_STATUS_DELETE &&
6884 status != GOT_STATUS_CONFLICT)
6885 return NULL;
6887 p = strdup(path);
6888 if (p == NULL)
6889 return got_error_from_errno("strdup");
6891 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6892 if (err || new == NULL)
6893 free(p);
6894 return err;
6897 static const struct got_error *
6898 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6899 int is_rebase, struct got_repository *repo)
6901 const struct got_error *err;
6902 struct got_reference *commit_ref = NULL;
6904 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6905 if (err) {
6906 if (err->code != GOT_ERR_NOT_REF)
6907 goto done;
6908 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6909 if (err)
6910 goto done;
6911 err = got_ref_write(commit_ref, repo);
6912 if (err)
6913 goto done;
6914 } else if (is_rebase) {
6915 struct got_object_id *stored_id;
6916 int cmp;
6918 err = got_ref_resolve(&stored_id, repo, commit_ref);
6919 if (err)
6920 goto done;
6921 cmp = got_object_id_cmp(commit_id, stored_id);
6922 free(stored_id);
6923 if (cmp != 0) {
6924 err = got_error(GOT_ERR_REBASE_COMMITID);
6925 goto done;
6928 done:
6929 if (commit_ref)
6930 got_ref_close(commit_ref);
6931 return err;
6934 static const struct got_error *
6935 rebase_merge_files(struct got_pathlist_head *merged_paths,
6936 const char *commit_ref_name, struct got_worktree *worktree,
6937 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6938 struct got_object_id *commit_id, struct got_repository *repo,
6939 got_worktree_checkout_cb progress_cb, void *progress_arg,
6940 got_cancel_cb cancel_cb, void *cancel_arg)
6942 const struct got_error *err;
6943 struct got_reference *commit_ref = NULL;
6944 struct collect_merged_paths_arg cmp_arg;
6945 char *fileindex_path;
6947 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6949 err = get_fileindex_path(&fileindex_path, worktree);
6950 if (err)
6951 return err;
6953 cmp_arg.progress_cb = progress_cb;
6954 cmp_arg.progress_arg = progress_arg;
6955 cmp_arg.merged_paths = merged_paths;
6956 err = merge_files(worktree, fileindex, fileindex_path,
6957 parent_commit_id, commit_id, repo, collect_merged_paths,
6958 &cmp_arg, cancel_cb, cancel_arg);
6959 if (commit_ref)
6960 got_ref_close(commit_ref);
6961 return err;
6964 const struct got_error *
6965 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6966 struct got_worktree *worktree, struct got_fileindex *fileindex,
6967 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6968 struct got_repository *repo,
6969 got_worktree_checkout_cb progress_cb, void *progress_arg,
6970 got_cancel_cb cancel_cb, void *cancel_arg)
6972 const struct got_error *err;
6973 char *commit_ref_name;
6975 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6976 if (err)
6977 return err;
6979 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6980 if (err)
6981 goto done;
6983 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6984 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6985 progress_arg, cancel_cb, cancel_arg);
6986 done:
6987 free(commit_ref_name);
6988 return err;
6991 const struct got_error *
6992 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6993 struct got_worktree *worktree, struct got_fileindex *fileindex,
6994 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6995 struct got_repository *repo,
6996 got_worktree_checkout_cb progress_cb, void *progress_arg,
6997 got_cancel_cb cancel_cb, void *cancel_arg)
6999 const struct got_error *err;
7000 char *commit_ref_name;
7002 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7003 if (err)
7004 return err;
7006 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7007 if (err)
7008 goto done;
7010 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7011 fileindex, parent_commit_id, commit_id, repo, progress_cb,
7012 progress_arg, cancel_cb, cancel_arg);
7013 done:
7014 free(commit_ref_name);
7015 return err;
7018 static const struct got_error *
7019 rebase_commit(struct got_object_id **new_commit_id,
7020 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
7021 struct got_worktree *worktree, struct got_fileindex *fileindex,
7022 struct got_reference *tmp_branch, const char *committer,
7023 struct got_commit_object *orig_commit, const char *new_logmsg,
7024 int allow_conflict, struct got_repository *repo)
7026 const struct got_error *err, *sync_err;
7027 struct got_pathlist_head commitable_paths;
7028 struct collect_commitables_arg cc_arg;
7029 char *fileindex_path = NULL;
7030 struct got_reference *head_ref = NULL;
7031 struct got_object_id *head_commit_id = NULL;
7032 char *logmsg = NULL;
7034 memset(&cc_arg, 0, sizeof(cc_arg));
7035 TAILQ_INIT(&commitable_paths);
7036 *new_commit_id = NULL;
7038 /* Work tree is locked/unlocked during rebase preparation/teardown. */
7040 err = get_fileindex_path(&fileindex_path, worktree);
7041 if (err)
7042 return err;
7044 cc_arg.commitable_paths = &commitable_paths;
7045 cc_arg.worktree = worktree;
7046 cc_arg.repo = repo;
7047 cc_arg.have_staged_files = 0;
7048 cc_arg.commit_conflicts = allow_conflict;
7050 * If possible get the status of individual files directly to
7051 * avoid crawling the entire work tree once per rebased commit.
7053 * Ideally, merged_paths would contain a list of commitables
7054 * we could use so we could skip worktree_status() entirely.
7055 * However, we would then need carefully keep track of cumulative
7056 * effects of operations such as file additions and deletions
7057 * in 'got histedit -f' (folding multiple commits into one),
7058 * and this extra complexity is not really worth it.
7060 if (merged_paths) {
7061 struct got_pathlist_entry *pe;
7062 TAILQ_FOREACH(pe, merged_paths, entry) {
7063 err = worktree_status(worktree, pe->path, fileindex,
7064 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
7065 0);
7066 if (err)
7067 goto done;
7069 } else {
7070 err = worktree_status(worktree, "", fileindex, repo,
7071 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7072 if (err)
7073 goto done;
7076 if (TAILQ_EMPTY(&commitable_paths)) {
7077 /* No-op change; commit will be elided. */
7078 err = got_ref_delete(commit_ref, repo);
7079 if (err)
7080 goto done;
7081 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
7082 goto done;
7085 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7086 if (err)
7087 goto done;
7089 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7090 if (err)
7091 goto done;
7093 if (new_logmsg) {
7094 logmsg = strdup(new_logmsg);
7095 if (logmsg == NULL) {
7096 err = got_error_from_errno("strdup");
7097 goto done;
7099 } else {
7100 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
7101 if (err)
7102 goto done;
7105 /* NB: commit_worktree will call free(logmsg) */
7106 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
7107 NULL, worktree, got_object_commit_get_author(orig_commit),
7108 committer ? committer :
7109 got_object_commit_get_committer(orig_commit), NULL,
7110 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
7111 if (err)
7112 goto done;
7114 err = got_ref_change_ref(tmp_branch, *new_commit_id);
7115 if (err)
7116 goto done;
7118 err = got_ref_delete(commit_ref, repo);
7119 if (err)
7120 goto done;
7122 err = update_fileindex_after_commit(worktree, &commitable_paths,
7123 *new_commit_id, fileindex, 0);
7124 sync_err = sync_fileindex(fileindex, fileindex_path);
7125 if (sync_err && err == NULL)
7126 err = sync_err;
7127 done:
7128 free(fileindex_path);
7129 free(head_commit_id);
7130 if (head_ref)
7131 got_ref_close(head_ref);
7132 if (err) {
7133 free(*new_commit_id);
7134 *new_commit_id = NULL;
7136 return err;
7139 const struct got_error *
7140 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
7141 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7142 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7143 const char *committer, struct got_commit_object *orig_commit,
7144 struct got_object_id *orig_commit_id, int allow_conflict,
7145 struct got_repository *repo)
7147 const struct got_error *err;
7148 char *commit_ref_name;
7149 struct got_reference *commit_ref = NULL;
7150 struct got_object_id *commit_id = NULL;
7152 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7153 if (err)
7154 return err;
7156 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7157 if (err)
7158 goto done;
7159 err = got_ref_resolve(&commit_id, repo, commit_ref);
7160 if (err)
7161 goto done;
7162 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
7163 err = got_error(GOT_ERR_REBASE_COMMITID);
7164 goto done;
7167 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7168 worktree, fileindex, tmp_branch, committer, orig_commit,
7169 NULL, allow_conflict, repo);
7170 done:
7171 if (commit_ref)
7172 got_ref_close(commit_ref);
7173 free(commit_ref_name);
7174 free(commit_id);
7175 return err;
7178 const struct got_error *
7179 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
7180 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7181 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7182 const char *committer, struct got_commit_object *orig_commit,
7183 struct got_object_id *orig_commit_id, const char *new_logmsg,
7184 int allow_conflict, struct got_repository *repo)
7186 const struct got_error *err;
7187 char *commit_ref_name;
7188 struct got_reference *commit_ref = NULL;
7190 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7191 if (err)
7192 return err;
7194 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7195 if (err)
7196 goto done;
7198 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7199 worktree, fileindex, tmp_branch, committer, orig_commit,
7200 new_logmsg, allow_conflict, repo);
7201 done:
7202 if (commit_ref)
7203 got_ref_close(commit_ref);
7204 free(commit_ref_name);
7205 return err;
7208 const struct got_error *
7209 got_worktree_rebase_postpone(struct got_worktree *worktree,
7210 struct got_fileindex *fileindex)
7212 if (fileindex)
7213 got_fileindex_free(fileindex);
7214 return lock_worktree(worktree, LOCK_SH);
7217 static const struct got_error *
7218 delete_ref(const char *name, struct got_repository *repo)
7220 const struct got_error *err;
7221 struct got_reference *ref;
7223 err = got_ref_open(&ref, repo, name, 0);
7224 if (err) {
7225 if (err->code == GOT_ERR_NOT_REF)
7226 return NULL;
7227 return err;
7230 err = got_ref_delete(ref, repo);
7231 got_ref_close(ref);
7232 return err;
7235 static const struct got_error *
7236 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7238 const struct got_error *err;
7239 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7240 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7242 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7243 if (err)
7244 goto done;
7245 err = delete_ref(tmp_branch_name, repo);
7246 if (err)
7247 goto done;
7249 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7250 if (err)
7251 goto done;
7252 err = delete_ref(new_base_branch_ref_name, repo);
7253 if (err)
7254 goto done;
7256 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7257 if (err)
7258 goto done;
7259 err = delete_ref(branch_ref_name, repo);
7260 if (err)
7261 goto done;
7263 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7264 if (err)
7265 goto done;
7266 err = delete_ref(commit_ref_name, repo);
7267 if (err)
7268 goto done;
7270 done:
7271 free(tmp_branch_name);
7272 free(new_base_branch_ref_name);
7273 free(branch_ref_name);
7274 free(commit_ref_name);
7275 return err;
7278 static const struct got_error *
7279 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7280 struct got_object_id *new_commit_id, struct got_repository *repo)
7282 const struct got_error *err;
7283 struct got_reference *ref = NULL;
7284 struct got_object_id *old_commit_id = NULL;
7285 const char *branch_name = NULL;
7286 char *new_id_str = NULL;
7287 char *refname = NULL;
7289 branch_name = got_ref_get_name(branch);
7290 if (strncmp(branch_name, "refs/heads/", 11) != 0)
7291 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7292 branch_name += 11;
7294 err = got_object_id_str(&new_id_str, new_commit_id);
7295 if (err)
7296 return err;
7298 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7299 new_id_str) == -1) {
7300 err = got_error_from_errno("asprintf");
7301 goto done;
7304 err = got_ref_resolve(&old_commit_id, repo, branch);
7305 if (err)
7306 goto done;
7308 err = got_ref_alloc(&ref, refname, old_commit_id);
7309 if (err)
7310 goto done;
7312 err = got_ref_write(ref, repo);
7313 done:
7314 free(new_id_str);
7315 free(refname);
7316 free(old_commit_id);
7317 if (ref)
7318 got_ref_close(ref);
7319 return err;
7322 const struct got_error *
7323 got_worktree_rebase_complete(struct got_worktree *worktree,
7324 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7325 struct got_reference *rebased_branch, struct got_repository *repo,
7326 int create_backup)
7328 const struct got_error *err, *unlockerr, *sync_err;
7329 struct got_object_id *new_head_commit_id = NULL;
7330 char *fileindex_path = NULL;
7332 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7333 if (err)
7334 return err;
7336 if (create_backup) {
7337 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7338 rebased_branch, new_head_commit_id, repo);
7339 if (err)
7340 goto done;
7343 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7344 if (err)
7345 goto done;
7347 err = got_ref_write(rebased_branch, repo);
7348 if (err)
7349 goto done;
7351 err = got_worktree_set_head_ref(worktree, rebased_branch);
7352 if (err)
7353 goto done;
7355 err = delete_rebase_refs(worktree, repo);
7356 if (err)
7357 goto done;
7359 err = get_fileindex_path(&fileindex_path, worktree);
7360 if (err)
7361 goto done;
7362 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7363 sync_err = sync_fileindex(fileindex, fileindex_path);
7364 if (sync_err && err == NULL)
7365 err = sync_err;
7366 done:
7367 got_fileindex_free(fileindex);
7368 free(fileindex_path);
7369 free(new_head_commit_id);
7370 unlockerr = lock_worktree(worktree, LOCK_SH);
7371 if (unlockerr && err == NULL)
7372 err = unlockerr;
7373 return err;
7376 static const struct got_error *
7377 get_paths_changed_between_commits(struct got_pathlist_head *paths,
7378 struct got_object_id *id1, struct got_object_id *id2,
7379 struct got_repository *repo)
7381 const struct got_error *err;
7382 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
7383 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7385 if (id1) {
7386 err = got_object_open_as_commit(&commit1, repo, id1);
7387 if (err)
7388 goto done;
7390 err = got_object_open_as_tree(&tree1, repo,
7391 got_object_commit_get_tree_id(commit1));
7392 if (err)
7393 goto done;
7396 if (id2) {
7397 err = got_object_open_as_commit(&commit2, repo, id2);
7398 if (err)
7399 goto done;
7401 err = got_object_open_as_tree(&tree2, repo,
7402 got_object_commit_get_tree_id(commit2));
7403 if (err)
7404 goto done;
7407 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
7408 got_diff_tree_collect_changed_paths, paths, 0);
7409 if (err)
7410 goto done;
7411 done:
7412 if (commit1)
7413 got_object_commit_close(commit1);
7414 if (commit2)
7415 got_object_commit_close(commit2);
7416 if (tree1)
7417 got_object_tree_close(tree1);
7418 if (tree2)
7419 got_object_tree_close(tree2);
7420 return err;
7423 static const struct got_error *
7424 get_paths_added_between_commits(struct got_pathlist_head *added_paths,
7425 struct got_object_id *id1, struct got_object_id *id2,
7426 const char *path_prefix, struct got_repository *repo)
7428 const struct got_error *err;
7429 struct got_pathlist_head merged_paths;
7430 struct got_pathlist_entry *pe;
7431 char *abspath = NULL, *wt_path = NULL;
7433 TAILQ_INIT(&merged_paths);
7435 err = get_paths_changed_between_commits(&merged_paths, id1, id2, repo);
7436 if (err)
7437 goto done;
7439 TAILQ_FOREACH(pe, &merged_paths, entry) {
7440 struct got_diff_changed_path *change = pe->data;
7442 if (change->status != GOT_STATUS_ADD)
7443 continue;
7445 if (got_path_is_root_dir(path_prefix)) {
7446 wt_path = strdup(pe->path);
7447 if (wt_path == NULL) {
7448 err = got_error_from_errno("strdup");
7449 goto done;
7451 } else {
7452 if (asprintf(&abspath, "/%s", pe->path) == -1) {
7453 err = got_error_from_errno("asprintf");
7454 goto done;
7457 err = got_path_skip_common_ancestor(&wt_path,
7458 path_prefix, abspath);
7459 if (err)
7460 goto done;
7461 free(abspath);
7462 abspath = NULL;
7465 err = got_pathlist_append(added_paths, wt_path, NULL);
7466 if (err)
7467 goto done;
7468 wt_path = NULL;
7471 done:
7472 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_ALL);
7473 free(abspath);
7474 free(wt_path);
7475 return err;
7478 static const struct got_error *
7479 get_paths_added_in_commit(struct got_pathlist_head *added_paths,
7480 struct got_object_id *id, const char *path_prefix,
7481 struct got_repository *repo)
7483 const struct got_error *err;
7484 struct got_commit_object *commit = NULL;
7485 struct got_object_qid *pid;
7487 err = got_object_open_as_commit(&commit, repo, id);
7488 if (err)
7489 goto done;
7491 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7493 err = get_paths_added_between_commits(added_paths,
7494 pid ? &pid->id : NULL, id, path_prefix, repo);
7495 if (err)
7496 goto done;
7497 done:
7498 if (commit)
7499 got_object_commit_close(commit);
7500 return err;
7503 const struct got_error *
7504 got_worktree_rebase_abort(struct got_worktree *worktree,
7505 struct got_fileindex *fileindex, struct got_repository *repo,
7506 struct got_reference *new_base_branch,
7507 got_worktree_checkout_cb progress_cb, void *progress_arg)
7509 const struct got_error *err, *unlockerr, *sync_err;
7510 struct got_reference *resolved = NULL;
7511 struct got_object_id *commit_id = NULL;
7512 struct got_object_id *merged_commit_id = NULL;
7513 struct got_commit_object *commit = NULL;
7514 char *fileindex_path = NULL;
7515 char *commit_ref_name = NULL;
7516 struct got_reference *commit_ref = NULL;
7517 struct revert_file_args rfa;
7518 struct got_object_id *tree_id = NULL;
7519 struct got_pathlist_head added_paths;
7521 TAILQ_INIT(&added_paths);
7523 err = lock_worktree(worktree, LOCK_EX);
7524 if (err)
7525 return err;
7527 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7528 if (err)
7529 goto done;
7531 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7532 if (err)
7533 goto done;
7535 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
7536 if (err)
7537 goto done;
7540 * Determine which files in added status can be safely removed
7541 * from disk while reverting changes in the work tree.
7542 * We want to avoid deleting unrelated files which were added by
7543 * the user for conflict resolution purposes.
7545 err = get_paths_added_in_commit(&added_paths, merged_commit_id,
7546 got_worktree_get_path_prefix(worktree), repo);
7547 if (err)
7548 goto done;
7550 err = got_ref_open(&resolved, repo,
7551 got_ref_get_symref_target(new_base_branch), 0);
7552 if (err)
7553 goto done;
7555 err = got_worktree_set_head_ref(worktree, resolved);
7556 if (err)
7557 goto done;
7560 * XXX commits to the base branch could have happened while
7561 * we were busy rebasing; should we store the original commit ID
7562 * when rebase begins and read it back here?
7564 err = got_ref_resolve(&commit_id, repo, resolved);
7565 if (err)
7566 goto done;
7568 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7569 if (err)
7570 goto done;
7572 err = got_object_open_as_commit(&commit, repo,
7573 worktree->base_commit_id);
7574 if (err)
7575 goto done;
7577 err = got_object_id_by_path(&tree_id, repo, commit,
7578 worktree->path_prefix);
7579 if (err)
7580 goto done;
7582 err = delete_rebase_refs(worktree, repo);
7583 if (err)
7584 goto done;
7586 err = get_fileindex_path(&fileindex_path, worktree);
7587 if (err)
7588 goto done;
7590 rfa.worktree = worktree;
7591 rfa.fileindex = fileindex;
7592 rfa.progress_cb = progress_cb;
7593 rfa.progress_arg = progress_arg;
7594 rfa.patch_cb = NULL;
7595 rfa.patch_arg = NULL;
7596 rfa.repo = repo;
7597 rfa.unlink_added_files = 1;
7598 rfa.added_files_to_unlink = &added_paths;
7599 err = worktree_status(worktree, "", fileindex, repo,
7600 revert_file, &rfa, NULL, NULL, 1, 0);
7601 if (err)
7602 goto sync;
7604 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7605 repo, progress_cb, progress_arg, NULL, NULL);
7606 sync:
7607 sync_err = sync_fileindex(fileindex, fileindex_path);
7608 if (sync_err && err == NULL)
7609 err = sync_err;
7610 done:
7611 got_pathlist_free(&added_paths, GOT_PATHLIST_FREE_PATH);
7612 got_ref_close(resolved);
7613 free(tree_id);
7614 free(commit_id);
7615 free(merged_commit_id);
7616 if (commit)
7617 got_object_commit_close(commit);
7618 if (fileindex)
7619 got_fileindex_free(fileindex);
7620 free(fileindex_path);
7621 free(commit_ref_name);
7622 if (commit_ref)
7623 got_ref_close(commit_ref);
7625 unlockerr = lock_worktree(worktree, LOCK_SH);
7626 if (unlockerr && err == NULL)
7627 err = unlockerr;
7628 return err;
7631 const struct got_error *
7632 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7633 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7634 struct got_fileindex **fileindex, struct got_worktree *worktree,
7635 struct got_repository *repo)
7637 const struct got_error *err = NULL;
7638 char *tmp_branch_name = NULL;
7639 char *branch_ref_name = NULL;
7640 char *base_commit_ref_name = NULL;
7641 char *fileindex_path = NULL;
7642 struct check_rebase_ok_arg ok_arg;
7643 struct got_reference *wt_branch = NULL;
7644 struct got_reference *base_commit_ref = NULL;
7646 *tmp_branch = NULL;
7647 *branch_ref = NULL;
7648 *base_commit_id = NULL;
7649 *fileindex = NULL;
7651 err = lock_worktree(worktree, LOCK_EX);
7652 if (err)
7653 return err;
7655 err = open_fileindex(fileindex, &fileindex_path, worktree);
7656 if (err)
7657 goto done;
7659 ok_arg.worktree = worktree;
7660 ok_arg.repo = repo;
7661 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7662 &ok_arg);
7663 if (err)
7664 goto done;
7666 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7667 if (err)
7668 goto done;
7670 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7671 if (err)
7672 goto done;
7674 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7675 worktree);
7676 if (err)
7677 goto done;
7679 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7680 0);
7681 if (err)
7682 goto done;
7684 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7685 if (err)
7686 goto done;
7688 err = got_ref_write(*branch_ref, repo);
7689 if (err)
7690 goto done;
7692 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7693 worktree->base_commit_id);
7694 if (err)
7695 goto done;
7696 err = got_ref_write(base_commit_ref, repo);
7697 if (err)
7698 goto done;
7699 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7700 if (*base_commit_id == NULL) {
7701 err = got_error_from_errno("got_object_id_dup");
7702 goto done;
7705 err = got_ref_alloc(tmp_branch, tmp_branch_name,
7706 worktree->base_commit_id);
7707 if (err)
7708 goto done;
7709 err = got_ref_write(*tmp_branch, repo);
7710 if (err)
7711 goto done;
7713 err = got_worktree_set_head_ref(worktree, *tmp_branch);
7714 if (err)
7715 goto done;
7716 done:
7717 free(fileindex_path);
7718 free(tmp_branch_name);
7719 free(branch_ref_name);
7720 free(base_commit_ref_name);
7721 if (wt_branch)
7722 got_ref_close(wt_branch);
7723 if (err) {
7724 if (*branch_ref) {
7725 got_ref_close(*branch_ref);
7726 *branch_ref = NULL;
7728 if (*tmp_branch) {
7729 got_ref_close(*tmp_branch);
7730 *tmp_branch = NULL;
7732 free(*base_commit_id);
7733 if (*fileindex) {
7734 got_fileindex_free(*fileindex);
7735 *fileindex = NULL;
7737 lock_worktree(worktree, LOCK_SH);
7739 return err;
7742 const struct got_error *
7743 got_worktree_histedit_postpone(struct got_worktree *worktree,
7744 struct got_fileindex *fileindex)
7746 if (fileindex)
7747 got_fileindex_free(fileindex);
7748 return lock_worktree(worktree, LOCK_SH);
7751 const struct got_error *
7752 got_worktree_histedit_in_progress(int *in_progress,
7753 struct got_worktree *worktree)
7755 const struct got_error *err;
7756 char *tmp_branch_name = NULL;
7758 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7759 if (err)
7760 return err;
7762 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7763 free(tmp_branch_name);
7764 return NULL;
7767 const struct got_error *
7768 got_worktree_histedit_continue(struct got_object_id **commit_id,
7769 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7770 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7771 struct got_worktree *worktree, struct got_repository *repo)
7773 const struct got_error *err;
7774 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7775 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7776 struct got_reference *commit_ref = NULL;
7777 struct got_reference *base_commit_ref = NULL;
7778 char *fileindex_path = NULL;
7779 int have_staged_files = 0;
7781 *commit_id = NULL;
7782 *tmp_branch = NULL;
7783 *base_commit_id = NULL;
7784 *fileindex = NULL;
7786 err = lock_worktree(worktree, LOCK_EX);
7787 if (err)
7788 return err;
7790 err = open_fileindex(fileindex, &fileindex_path, worktree);
7791 if (err)
7792 goto done;
7794 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7795 &have_staged_files);
7796 if (err && err->code != GOT_ERR_CANCELLED)
7797 goto done;
7798 if (have_staged_files) {
7799 err = got_error(GOT_ERR_STAGED_PATHS);
7800 goto done;
7803 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7804 if (err)
7805 goto done;
7807 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7808 if (err)
7809 goto done;
7811 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7812 if (err)
7813 goto done;
7815 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7816 worktree);
7817 if (err)
7818 goto done;
7820 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7821 if (err)
7822 goto done;
7824 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7825 if (err)
7826 goto done;
7827 err = got_ref_resolve(commit_id, repo, commit_ref);
7828 if (err)
7829 goto done;
7831 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7832 if (err)
7833 goto done;
7834 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7835 if (err)
7836 goto done;
7838 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7839 if (err)
7840 goto done;
7841 done:
7842 free(commit_ref_name);
7843 free(branch_ref_name);
7844 free(fileindex_path);
7845 if (commit_ref)
7846 got_ref_close(commit_ref);
7847 if (base_commit_ref)
7848 got_ref_close(base_commit_ref);
7849 if (err) {
7850 free(*commit_id);
7851 *commit_id = NULL;
7852 free(*base_commit_id);
7853 *base_commit_id = NULL;
7854 if (*tmp_branch) {
7855 got_ref_close(*tmp_branch);
7856 *tmp_branch = NULL;
7858 if (*fileindex) {
7859 got_fileindex_free(*fileindex);
7860 *fileindex = NULL;
7862 lock_worktree(worktree, LOCK_EX);
7864 return err;
7867 static const struct got_error *
7868 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7870 const struct got_error *err;
7871 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7872 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7874 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7875 if (err)
7876 goto done;
7877 err = delete_ref(tmp_branch_name, repo);
7878 if (err)
7879 goto done;
7881 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7882 worktree);
7883 if (err)
7884 goto done;
7885 err = delete_ref(base_commit_ref_name, repo);
7886 if (err)
7887 goto done;
7889 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7890 if (err)
7891 goto done;
7892 err = delete_ref(branch_ref_name, repo);
7893 if (err)
7894 goto done;
7896 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7897 if (err)
7898 goto done;
7899 err = delete_ref(commit_ref_name, repo);
7900 if (err)
7901 goto done;
7902 done:
7903 free(tmp_branch_name);
7904 free(base_commit_ref_name);
7905 free(branch_ref_name);
7906 free(commit_ref_name);
7907 return err;
7910 const struct got_error *
7911 got_worktree_histedit_abort(struct got_worktree *worktree,
7912 struct got_fileindex *fileindex, struct got_repository *repo,
7913 struct got_reference *branch, struct got_object_id *base_commit_id,
7914 got_worktree_checkout_cb progress_cb, void *progress_arg)
7916 const struct got_error *err, *unlockerr, *sync_err;
7917 struct got_reference *resolved = NULL;
7918 char *fileindex_path = NULL;
7919 struct got_object_id *merged_commit_id = NULL;
7920 struct got_commit_object *commit = NULL;
7921 char *commit_ref_name = NULL;
7922 struct got_reference *commit_ref = NULL;
7923 struct got_object_id *tree_id = NULL;
7924 struct revert_file_args rfa;
7925 struct got_pathlist_head added_paths;
7927 TAILQ_INIT(&added_paths);
7929 err = lock_worktree(worktree, LOCK_EX);
7930 if (err)
7931 return err;
7933 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7934 if (err)
7935 goto done;
7937 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7938 if (err) {
7939 if (err->code != GOT_ERR_NOT_REF)
7940 goto done;
7941 /* Can happen on early abort due to invalid histedit script. */
7942 commit_ref = NULL;
7945 if (commit_ref) {
7946 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
7947 if (err)
7948 goto done;
7951 * Determine which files in added status can be safely removed
7952 * from disk while reverting changes in the work tree.
7953 * We want to avoid deleting unrelated files added by the
7954 * user during conflict resolution or during histedit -e.
7956 err = get_paths_added_in_commit(&added_paths, merged_commit_id,
7957 got_worktree_get_path_prefix(worktree), repo);
7958 if (err)
7959 goto done;
7962 err = got_ref_open(&resolved, repo,
7963 got_ref_get_symref_target(branch), 0);
7964 if (err)
7965 goto done;
7967 err = got_worktree_set_head_ref(worktree, resolved);
7968 if (err)
7969 goto done;
7971 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7972 if (err)
7973 goto done;
7975 err = got_object_open_as_commit(&commit, repo,
7976 worktree->base_commit_id);
7977 if (err)
7978 goto done;
7980 err = got_object_id_by_path(&tree_id, repo, commit,
7981 worktree->path_prefix);
7982 if (err)
7983 goto done;
7985 err = delete_histedit_refs(worktree, repo);
7986 if (err)
7987 goto done;
7989 err = get_fileindex_path(&fileindex_path, worktree);
7990 if (err)
7991 goto done;
7993 rfa.worktree = worktree;
7994 rfa.fileindex = fileindex;
7995 rfa.progress_cb = progress_cb;
7996 rfa.progress_arg = progress_arg;
7997 rfa.patch_cb = NULL;
7998 rfa.patch_arg = NULL;
7999 rfa.repo = repo;
8000 rfa.unlink_added_files = 1;
8001 rfa.added_files_to_unlink = &added_paths;
8002 err = worktree_status(worktree, "", fileindex, repo,
8003 revert_file, &rfa, NULL, NULL, 1, 0);
8004 if (err)
8005 goto sync;
8007 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8008 repo, progress_cb, progress_arg, NULL, NULL);
8009 sync:
8010 sync_err = sync_fileindex(fileindex, fileindex_path);
8011 if (sync_err && err == NULL)
8012 err = sync_err;
8013 done:
8014 if (resolved)
8015 got_ref_close(resolved);
8016 if (commit_ref)
8017 got_ref_close(commit_ref);
8018 free(merged_commit_id);
8019 free(tree_id);
8020 free(fileindex_path);
8021 free(commit_ref_name);
8023 unlockerr = lock_worktree(worktree, LOCK_SH);
8024 if (unlockerr && err == NULL)
8025 err = unlockerr;
8026 return err;
8029 const struct got_error *
8030 got_worktree_histedit_complete(struct got_worktree *worktree,
8031 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8032 struct got_reference *edited_branch, struct got_repository *repo)
8034 const struct got_error *err, *unlockerr, *sync_err;
8035 struct got_object_id *new_head_commit_id = NULL;
8036 struct got_reference *resolved = NULL;
8037 char *fileindex_path = NULL;
8039 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
8040 if (err)
8041 return err;
8043 err = got_ref_open(&resolved, repo,
8044 got_ref_get_symref_target(edited_branch), 0);
8045 if (err)
8046 goto done;
8048 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
8049 resolved, new_head_commit_id, repo);
8050 if (err)
8051 goto done;
8053 err = got_ref_change_ref(resolved, new_head_commit_id);
8054 if (err)
8055 goto done;
8057 err = got_ref_write(resolved, repo);
8058 if (err)
8059 goto done;
8061 err = got_worktree_set_head_ref(worktree, resolved);
8062 if (err)
8063 goto done;
8065 err = delete_histedit_refs(worktree, repo);
8066 if (err)
8067 goto done;
8069 err = get_fileindex_path(&fileindex_path, worktree);
8070 if (err)
8071 goto done;
8072 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8073 sync_err = sync_fileindex(fileindex, fileindex_path);
8074 if (sync_err && err == NULL)
8075 err = sync_err;
8076 done:
8077 got_fileindex_free(fileindex);
8078 free(fileindex_path);
8079 free(new_head_commit_id);
8080 unlockerr = lock_worktree(worktree, LOCK_SH);
8081 if (unlockerr && err == NULL)
8082 err = unlockerr;
8083 return err;
8086 const struct got_error *
8087 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
8088 struct got_object_id *commit_id, struct got_repository *repo)
8090 const struct got_error *err;
8091 char *commit_ref_name;
8093 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8094 if (err)
8095 return err;
8097 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
8098 if (err)
8099 goto done;
8101 err = delete_ref(commit_ref_name, repo);
8102 done:
8103 free(commit_ref_name);
8104 return err;
8107 const struct got_error *
8108 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
8109 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
8110 struct got_worktree *worktree, const char *refname,
8111 struct got_repository *repo)
8113 const struct got_error *err = NULL;
8114 char *fileindex_path = NULL;
8115 struct check_rebase_ok_arg ok_arg;
8117 *fileindex = NULL;
8118 *branch_ref = NULL;
8119 *base_branch_ref = NULL;
8121 err = lock_worktree(worktree, LOCK_EX);
8122 if (err)
8123 return err;
8125 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
8126 err = got_error_msg(GOT_ERR_SAME_BRANCH,
8127 "cannot integrate a branch into itself; "
8128 "update -b or different branch name required");
8129 goto done;
8132 err = open_fileindex(fileindex, &fileindex_path, worktree);
8133 if (err)
8134 goto done;
8136 /* Preconditions are the same as for rebase. */
8137 ok_arg.worktree = worktree;
8138 ok_arg.repo = repo;
8139 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8140 &ok_arg);
8141 if (err)
8142 goto done;
8144 err = got_ref_open(branch_ref, repo, refname, 1);
8145 if (err)
8146 goto done;
8148 err = got_ref_open(base_branch_ref, repo,
8149 got_worktree_get_head_ref_name(worktree), 1);
8150 done:
8151 if (err) {
8152 if (*branch_ref) {
8153 got_ref_close(*branch_ref);
8154 *branch_ref = NULL;
8156 if (*base_branch_ref) {
8157 got_ref_close(*base_branch_ref);
8158 *base_branch_ref = NULL;
8160 if (*fileindex) {
8161 got_fileindex_free(*fileindex);
8162 *fileindex = NULL;
8164 lock_worktree(worktree, LOCK_SH);
8166 return err;
8169 const struct got_error *
8170 got_worktree_integrate_continue(struct got_worktree *worktree,
8171 struct got_fileindex *fileindex, struct got_repository *repo,
8172 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
8173 got_worktree_checkout_cb progress_cb, void *progress_arg,
8174 got_cancel_cb cancel_cb, void *cancel_arg)
8176 const struct got_error *err = NULL, *sync_err, *unlockerr;
8177 char *fileindex_path = NULL;
8178 struct got_object_id *tree_id = NULL, *commit_id = NULL;
8179 struct got_commit_object *commit = NULL;
8181 err = get_fileindex_path(&fileindex_path, worktree);
8182 if (err)
8183 goto done;
8185 err = got_ref_resolve(&commit_id, repo, branch_ref);
8186 if (err)
8187 goto done;
8189 err = got_object_open_as_commit(&commit, repo, commit_id);
8190 if (err)
8191 goto done;
8193 err = got_object_id_by_path(&tree_id, repo, commit,
8194 worktree->path_prefix);
8195 if (err)
8196 goto done;
8198 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
8199 if (err)
8200 goto done;
8202 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
8203 progress_cb, progress_arg, cancel_cb, cancel_arg);
8204 if (err)
8205 goto sync;
8207 err = got_ref_change_ref(base_branch_ref, commit_id);
8208 if (err)
8209 goto sync;
8211 err = got_ref_write(base_branch_ref, repo);
8212 if (err)
8213 goto sync;
8215 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8216 sync:
8217 sync_err = sync_fileindex(fileindex, fileindex_path);
8218 if (sync_err && err == NULL)
8219 err = sync_err;
8221 done:
8222 unlockerr = got_ref_unlock(branch_ref);
8223 if (unlockerr && err == NULL)
8224 err = unlockerr;
8225 got_ref_close(branch_ref);
8227 unlockerr = got_ref_unlock(base_branch_ref);
8228 if (unlockerr && err == NULL)
8229 err = unlockerr;
8230 got_ref_close(base_branch_ref);
8232 got_fileindex_free(fileindex);
8233 free(fileindex_path);
8234 free(tree_id);
8235 if (commit)
8236 got_object_commit_close(commit);
8238 unlockerr = lock_worktree(worktree, LOCK_SH);
8239 if (unlockerr && err == NULL)
8240 err = unlockerr;
8241 return err;
8244 const struct got_error *
8245 got_worktree_integrate_abort(struct got_worktree *worktree,
8246 struct got_fileindex *fileindex, struct got_repository *repo,
8247 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
8249 const struct got_error *err = NULL, *unlockerr = NULL;
8251 got_fileindex_free(fileindex);
8253 err = lock_worktree(worktree, LOCK_SH);
8255 unlockerr = got_ref_unlock(branch_ref);
8256 if (unlockerr && err == NULL)
8257 err = unlockerr;
8258 got_ref_close(branch_ref);
8260 unlockerr = got_ref_unlock(base_branch_ref);
8261 if (unlockerr && err == NULL)
8262 err = unlockerr;
8263 got_ref_close(base_branch_ref);
8265 return err;
8268 const struct got_error *
8269 got_worktree_merge_postpone(struct got_worktree *worktree,
8270 struct got_fileindex *fileindex)
8272 const struct got_error *err, *sync_err;
8273 char *fileindex_path = NULL;
8275 err = get_fileindex_path(&fileindex_path, worktree);
8276 if (err)
8277 goto done;
8279 sync_err = sync_fileindex(fileindex, fileindex_path);
8281 err = lock_worktree(worktree, LOCK_SH);
8282 if (sync_err && err == NULL)
8283 err = sync_err;
8284 done:
8285 got_fileindex_free(fileindex);
8286 free(fileindex_path);
8287 return err;
8290 static const struct got_error *
8291 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
8293 const struct got_error *err;
8294 char *branch_refname = NULL, *commit_refname = NULL;
8296 err = get_merge_branch_ref_name(&branch_refname, worktree);
8297 if (err)
8298 goto done;
8299 err = delete_ref(branch_refname, repo);
8300 if (err)
8301 goto done;
8303 err = get_merge_commit_ref_name(&commit_refname, worktree);
8304 if (err)
8305 goto done;
8306 err = delete_ref(commit_refname, repo);
8307 if (err)
8308 goto done;
8310 done:
8311 free(branch_refname);
8312 free(commit_refname);
8313 return err;
8316 struct merge_commit_msg_arg {
8317 struct got_worktree *worktree;
8318 const char *branch_name;
8321 static const struct got_error *
8322 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
8323 const char *diff_path, char **logmsg, void *arg)
8325 struct merge_commit_msg_arg *a = arg;
8327 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
8328 got_worktree_get_head_ref_name(a->worktree)) == -1)
8329 return got_error_from_errno("asprintf");
8331 return NULL;
8335 const struct got_error *
8336 got_worktree_merge_branch(struct got_worktree *worktree,
8337 struct got_fileindex *fileindex,
8338 struct got_object_id *yca_commit_id,
8339 struct got_object_id *branch_tip,
8340 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
8341 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
8343 const struct got_error *err;
8344 char *fileindex_path = NULL;
8346 err = get_fileindex_path(&fileindex_path, worktree);
8347 if (err)
8348 goto done;
8350 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
8351 worktree);
8352 if (err)
8353 goto done;
8355 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
8356 branch_tip, repo, progress_cb, progress_arg,
8357 cancel_cb, cancel_arg);
8358 done:
8359 free(fileindex_path);
8360 return err;
8363 const struct got_error *
8364 got_worktree_merge_commit(struct got_object_id **new_commit_id,
8365 struct got_worktree *worktree, struct got_fileindex *fileindex,
8366 const char *author, const char *committer, int allow_bad_symlinks,
8367 struct got_object_id *branch_tip, const char *branch_name,
8368 int allow_conflict, struct got_repository *repo,
8369 got_worktree_status_cb status_cb, void *status_arg)
8372 const struct got_error *err = NULL, *sync_err;
8373 struct got_pathlist_head commitable_paths;
8374 struct collect_commitables_arg cc_arg;
8375 struct got_pathlist_entry *pe;
8376 struct got_reference *head_ref = NULL;
8377 struct got_object_id *head_commit_id = NULL;
8378 int have_staged_files = 0;
8379 struct merge_commit_msg_arg mcm_arg;
8380 char *fileindex_path = NULL;
8382 memset(&cc_arg, 0, sizeof(cc_arg));
8383 *new_commit_id = NULL;
8385 TAILQ_INIT(&commitable_paths);
8387 err = get_fileindex_path(&fileindex_path, worktree);
8388 if (err)
8389 goto done;
8391 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
8392 if (err)
8393 goto done;
8395 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8396 if (err)
8397 goto done;
8399 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
8400 &have_staged_files);
8401 if (err && err->code != GOT_ERR_CANCELLED)
8402 goto done;
8403 if (have_staged_files) {
8404 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
8405 goto done;
8408 cc_arg.commitable_paths = &commitable_paths;
8409 cc_arg.worktree = worktree;
8410 cc_arg.fileindex = fileindex;
8411 cc_arg.repo = repo;
8412 cc_arg.have_staged_files = have_staged_files;
8413 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
8414 cc_arg.commit_conflicts = allow_conflict;
8415 err = worktree_status(worktree, "", fileindex, repo,
8416 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8417 if (err)
8418 goto done;
8420 mcm_arg.worktree = worktree;
8421 mcm_arg.branch_name = branch_name;
8422 err = commit_worktree(new_commit_id, &commitable_paths,
8423 head_commit_id, branch_tip, worktree, author, committer, NULL,
8424 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8425 if (err)
8426 goto done;
8428 err = update_fileindex_after_commit(worktree, &commitable_paths,
8429 *new_commit_id, fileindex, have_staged_files);
8430 sync_err = sync_fileindex(fileindex, fileindex_path);
8431 if (sync_err && err == NULL)
8432 err = sync_err;
8433 done:
8434 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8435 struct got_commitable *ct = pe->data;
8437 free_commitable(ct);
8439 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8440 free(fileindex_path);
8441 return err;
8444 const struct got_error *
8445 got_worktree_merge_complete(struct got_worktree *worktree,
8446 struct got_fileindex *fileindex, struct got_repository *repo)
8448 const struct got_error *err, *unlockerr, *sync_err;
8449 char *fileindex_path = NULL;
8451 err = delete_merge_refs(worktree, repo);
8452 if (err)
8453 goto done;
8455 err = get_fileindex_path(&fileindex_path, worktree);
8456 if (err)
8457 goto done;
8458 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8459 sync_err = sync_fileindex(fileindex, fileindex_path);
8460 if (sync_err && err == NULL)
8461 err = sync_err;
8462 done:
8463 got_fileindex_free(fileindex);
8464 free(fileindex_path);
8465 unlockerr = lock_worktree(worktree, LOCK_SH);
8466 if (unlockerr && err == NULL)
8467 err = unlockerr;
8468 return err;
8471 const struct got_error *
8472 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8473 struct got_repository *repo)
8475 const struct got_error *err;
8476 char *branch_refname = NULL;
8477 struct got_reference *branch_ref = NULL;
8479 *in_progress = 0;
8481 err = get_merge_branch_ref_name(&branch_refname, worktree);
8482 if (err)
8483 return err;
8484 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8485 free(branch_refname);
8486 if (err) {
8487 if (err->code != GOT_ERR_NOT_REF)
8488 return err;
8489 } else
8490 *in_progress = 1;
8492 return NULL;
8495 const struct got_error *got_worktree_merge_prepare(
8496 struct got_fileindex **fileindex, struct got_worktree *worktree,
8497 struct got_repository *repo)
8499 const struct got_error *err = NULL;
8500 char *fileindex_path = NULL;
8501 struct got_reference *wt_branch = NULL;
8502 struct got_object_id *wt_branch_tip = NULL;
8503 struct check_rebase_ok_arg ok_arg;
8505 *fileindex = NULL;
8507 err = lock_worktree(worktree, LOCK_EX);
8508 if (err)
8509 return err;
8511 err = open_fileindex(fileindex, &fileindex_path, worktree);
8512 if (err)
8513 goto done;
8515 /* Preconditions are the same as for rebase. */
8516 ok_arg.worktree = worktree;
8517 ok_arg.repo = repo;
8518 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8519 &ok_arg);
8520 if (err)
8521 goto done;
8523 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8524 0);
8525 if (err)
8526 goto done;
8528 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8529 if (err)
8530 goto done;
8532 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8533 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8534 goto done;
8537 done:
8538 free(fileindex_path);
8539 if (wt_branch)
8540 got_ref_close(wt_branch);
8541 free(wt_branch_tip);
8542 if (err) {
8543 if (*fileindex) {
8544 got_fileindex_free(*fileindex);
8545 *fileindex = NULL;
8547 lock_worktree(worktree, LOCK_SH);
8549 return err;
8552 const struct got_error *got_worktree_merge_write_refs(
8553 struct got_worktree *worktree, struct got_reference *branch,
8554 struct got_repository *repo)
8556 const struct got_error *err = NULL;
8557 char *branch_refname = NULL, *commit_refname = NULL;
8558 struct got_reference *branch_ref = NULL, *commit_ref = NULL;
8559 struct got_object_id *branch_tip = NULL;
8561 err = get_merge_branch_ref_name(&branch_refname, worktree);
8562 if (err)
8563 return err;
8565 err = get_merge_commit_ref_name(&commit_refname, worktree);
8566 if (err)
8567 return err;
8569 err = got_ref_resolve(&branch_tip, repo, branch);
8570 if (err)
8571 goto done;
8573 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8574 if (err)
8575 goto done;
8576 err = got_ref_write(branch_ref, repo);
8577 if (err)
8578 goto done;
8580 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8581 if (err)
8582 goto done;
8583 err = got_ref_write(commit_ref, repo);
8584 if (err)
8585 goto done;
8587 done:
8588 free(branch_refname);
8589 free(commit_refname);
8590 if (branch_ref)
8591 got_ref_close(branch_ref);
8592 if (commit_ref)
8593 got_ref_close(commit_ref);
8594 free(branch_tip);
8595 return err;
8598 const struct got_error *
8599 got_worktree_merge_continue(char **branch_name,
8600 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8601 struct got_worktree *worktree, struct got_repository *repo)
8603 const struct got_error *err;
8604 char *commit_refname = NULL, *branch_refname = NULL;
8605 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8606 char *fileindex_path = NULL;
8607 int have_staged_files = 0;
8609 *branch_name = NULL;
8610 *branch_tip = NULL;
8611 *fileindex = NULL;
8613 err = lock_worktree(worktree, LOCK_EX);
8614 if (err)
8615 return err;
8617 err = open_fileindex(fileindex, &fileindex_path, worktree);
8618 if (err)
8619 goto done;
8621 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8622 &have_staged_files);
8623 if (err && err->code != GOT_ERR_CANCELLED)
8624 goto done;
8625 if (have_staged_files) {
8626 err = got_error(GOT_ERR_STAGED_PATHS);
8627 goto done;
8630 err = get_merge_branch_ref_name(&branch_refname, worktree);
8631 if (err)
8632 goto done;
8634 err = get_merge_commit_ref_name(&commit_refname, worktree);
8635 if (err)
8636 goto done;
8638 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8639 if (err)
8640 goto done;
8642 if (!got_ref_is_symbolic(branch_ref)) {
8643 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8644 "%s is not a symbolic reference",
8645 got_ref_get_name(branch_ref));
8646 goto done;
8648 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8649 if (*branch_name == NULL) {
8650 err = got_error_from_errno("strdup");
8651 goto done;
8654 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8655 if (err)
8656 goto done;
8658 err = got_ref_resolve(branch_tip, repo, commit_ref);
8659 if (err)
8660 goto done;
8661 done:
8662 free(commit_refname);
8663 free(branch_refname);
8664 free(fileindex_path);
8665 if (commit_ref)
8666 got_ref_close(commit_ref);
8667 if (branch_ref)
8668 got_ref_close(branch_ref);
8669 if (err) {
8670 if (*branch_name) {
8671 free(*branch_name);
8672 *branch_name = NULL;
8674 free(*branch_tip);
8675 *branch_tip = NULL;
8676 if (*fileindex) {
8677 got_fileindex_free(*fileindex);
8678 *fileindex = NULL;
8680 lock_worktree(worktree, LOCK_SH);
8682 return err;
8685 const struct got_error *
8686 got_worktree_merge_abort(struct got_worktree *worktree,
8687 struct got_fileindex *fileindex, struct got_repository *repo,
8688 got_worktree_checkout_cb progress_cb, void *progress_arg)
8690 const struct got_error *err, *unlockerr, *sync_err;
8691 struct got_commit_object *commit = NULL;
8692 char *fileindex_path = NULL;
8693 struct revert_file_args rfa;
8694 char *commit_ref_name = NULL;
8695 struct got_reference *commit_ref = NULL;
8696 struct got_object_id *merged_commit_id = NULL;
8697 struct got_object_id *tree_id = NULL;
8698 struct got_pathlist_head added_paths;
8700 TAILQ_INIT(&added_paths);
8702 err = get_merge_commit_ref_name(&commit_ref_name, worktree);
8703 if (err)
8704 goto done;
8706 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8707 if (err)
8708 goto done;
8710 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8711 if (err)
8712 goto done;
8715 * Determine which files in added status can be safely removed
8716 * from disk while reverting changes in the work tree.
8717 * We want to avoid deleting unrelated files which were added by
8718 * the user for conflict resolution purposes.
8720 err = get_paths_added_between_commits(&added_paths,
8721 got_worktree_get_base_commit_id(worktree), merged_commit_id,
8722 got_worktree_get_path_prefix(worktree), repo);
8723 if (err)
8724 goto done;
8727 err = got_object_open_as_commit(&commit, repo,
8728 worktree->base_commit_id);
8729 if (err)
8730 goto done;
8732 err = got_object_id_by_path(&tree_id, repo, commit,
8733 worktree->path_prefix);
8734 if (err)
8735 goto done;
8737 err = delete_merge_refs(worktree, repo);
8738 if (err)
8739 goto done;
8741 err = get_fileindex_path(&fileindex_path, worktree);
8742 if (err)
8743 goto done;
8745 rfa.worktree = worktree;
8746 rfa.fileindex = fileindex;
8747 rfa.progress_cb = progress_cb;
8748 rfa.progress_arg = progress_arg;
8749 rfa.patch_cb = NULL;
8750 rfa.patch_arg = NULL;
8751 rfa.repo = repo;
8752 rfa.unlink_added_files = 1;
8753 rfa.added_files_to_unlink = &added_paths;
8754 err = worktree_status(worktree, "", fileindex, repo,
8755 revert_file, &rfa, NULL, NULL, 1, 0);
8756 if (err)
8757 goto sync;
8759 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8760 repo, progress_cb, progress_arg, NULL, NULL);
8761 sync:
8762 sync_err = sync_fileindex(fileindex, fileindex_path);
8763 if (sync_err && err == NULL)
8764 err = sync_err;
8765 done:
8766 free(tree_id);
8767 free(merged_commit_id);
8768 if (commit)
8769 got_object_commit_close(commit);
8770 if (fileindex)
8771 got_fileindex_free(fileindex);
8772 free(fileindex_path);
8773 if (commit_ref)
8774 got_ref_close(commit_ref);
8775 free(commit_ref_name);
8777 unlockerr = lock_worktree(worktree, LOCK_SH);
8778 if (unlockerr && err == NULL)
8779 err = unlockerr;
8780 return err;
8783 struct check_stage_ok_arg {
8784 struct got_object_id *head_commit_id;
8785 struct got_worktree *worktree;
8786 struct got_fileindex *fileindex;
8787 struct got_repository *repo;
8788 int have_changes;
8791 static const struct got_error *
8792 check_stage_ok(void *arg, unsigned char status,
8793 unsigned char staged_status, const char *relpath,
8794 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8795 struct got_object_id *commit_id, int dirfd, const char *de_name)
8797 struct check_stage_ok_arg *a = arg;
8798 const struct got_error *err = NULL;
8799 struct got_fileindex_entry *ie;
8800 struct got_object_id base_commit_id;
8801 struct got_object_id *base_commit_idp = NULL;
8802 char *in_repo_path = NULL, *p;
8804 if (status == GOT_STATUS_UNVERSIONED ||
8805 status == GOT_STATUS_NO_CHANGE)
8806 return NULL;
8807 if (status == GOT_STATUS_NONEXISTENT)
8808 return got_error_set_errno(ENOENT, relpath);
8810 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8811 if (ie == NULL)
8812 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8814 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8815 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8816 relpath) == -1)
8817 return got_error_from_errno("asprintf");
8819 if (got_fileindex_entry_has_commit(ie)) {
8820 base_commit_idp = got_fileindex_entry_get_commit_id(
8821 &base_commit_id, ie);
8824 if (status == GOT_STATUS_CONFLICT) {
8825 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8826 goto done;
8827 } else if (status != GOT_STATUS_ADD &&
8828 status != GOT_STATUS_MODIFY &&
8829 status != GOT_STATUS_DELETE) {
8830 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8831 goto done;
8834 a->have_changes = 1;
8836 p = in_repo_path;
8837 while (p[0] == '/')
8838 p++;
8839 err = check_out_of_date(p, status, staged_status,
8840 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8841 GOT_ERR_STAGE_OUT_OF_DATE);
8842 done:
8843 free(in_repo_path);
8844 return err;
8847 struct stage_path_arg {
8848 struct got_worktree *worktree;
8849 struct got_fileindex *fileindex;
8850 struct got_repository *repo;
8851 got_worktree_status_cb status_cb;
8852 void *status_arg;
8853 got_worktree_patch_cb patch_cb;
8854 void *patch_arg;
8855 int staged_something;
8856 int allow_bad_symlinks;
8859 static const struct got_error *
8860 stage_path(void *arg, unsigned char status,
8861 unsigned char staged_status, const char *relpath,
8862 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8863 struct got_object_id *commit_id, int dirfd, const char *de_name)
8865 struct stage_path_arg *a = arg;
8866 const struct got_error *err = NULL;
8867 struct got_fileindex_entry *ie;
8868 char *ondisk_path = NULL, *path_content = NULL;
8869 uint32_t stage;
8870 struct got_object_id *new_staged_blob_id = NULL;
8871 struct stat sb;
8873 if (status == GOT_STATUS_UNVERSIONED)
8874 return NULL;
8876 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8877 if (ie == NULL)
8878 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8880 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8881 relpath)== -1)
8882 return got_error_from_errno("asprintf");
8884 switch (status) {
8885 case GOT_STATUS_ADD:
8886 case GOT_STATUS_MODIFY:
8887 /* XXX could sb.st_mode be passed in by our caller? */
8888 if (lstat(ondisk_path, &sb) == -1) {
8889 err = got_error_from_errno2("lstat", ondisk_path);
8890 break;
8892 if (a->patch_cb) {
8893 if (status == GOT_STATUS_ADD) {
8894 int choice = GOT_PATCH_CHOICE_NONE;
8895 err = (*a->patch_cb)(&choice, a->patch_arg,
8896 status, ie->path, NULL, 1, 1);
8897 if (err)
8898 break;
8899 if (choice != GOT_PATCH_CHOICE_YES)
8900 break;
8901 } else {
8902 err = create_patched_content(&path_content, 0,
8903 staged_blob_id ? staged_blob_id : blob_id,
8904 ondisk_path, dirfd, de_name, ie->path,
8905 a->repo, a->patch_cb, a->patch_arg);
8906 if (err || path_content == NULL)
8907 break;
8910 err = got_object_blob_create(&new_staged_blob_id,
8911 path_content ? path_content : ondisk_path, a->repo);
8912 if (err)
8913 break;
8914 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8915 SHA1_DIGEST_LENGTH);
8916 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8917 stage = GOT_FILEIDX_STAGE_ADD;
8918 else
8919 stage = GOT_FILEIDX_STAGE_MODIFY;
8920 got_fileindex_entry_stage_set(ie, stage);
8921 if (S_ISLNK(sb.st_mode)) {
8922 int is_bad_symlink = 0;
8923 if (!a->allow_bad_symlinks) {
8924 char target_path[PATH_MAX];
8925 ssize_t target_len;
8926 target_len = readlink(ondisk_path, target_path,
8927 sizeof(target_path));
8928 if (target_len == -1) {
8929 err = got_error_from_errno2("readlink",
8930 ondisk_path);
8931 break;
8933 err = is_bad_symlink_target(&is_bad_symlink,
8934 target_path, target_len, ondisk_path,
8935 a->worktree->root_path);
8936 if (err)
8937 break;
8938 if (is_bad_symlink) {
8939 err = got_error_path(ondisk_path,
8940 GOT_ERR_BAD_SYMLINK);
8941 break;
8944 if (is_bad_symlink)
8945 got_fileindex_entry_staged_filetype_set(ie,
8946 GOT_FILEIDX_MODE_BAD_SYMLINK);
8947 else
8948 got_fileindex_entry_staged_filetype_set(ie,
8949 GOT_FILEIDX_MODE_SYMLINK);
8950 } else {
8951 got_fileindex_entry_staged_filetype_set(ie,
8952 GOT_FILEIDX_MODE_REGULAR_FILE);
8954 a->staged_something = 1;
8955 if (a->status_cb == NULL)
8956 break;
8957 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8958 get_staged_status(ie), relpath, blob_id,
8959 new_staged_blob_id, NULL, dirfd, de_name);
8960 if (err)
8961 break;
8963 * When staging the reverse of the staged diff,
8964 * implicitly unstage the file.
8966 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8967 sizeof(ie->blob_sha1)) == 0) {
8968 got_fileindex_entry_stage_set(ie,
8969 GOT_FILEIDX_STAGE_NONE);
8971 break;
8972 case GOT_STATUS_DELETE:
8973 if (staged_status == GOT_STATUS_DELETE)
8974 break;
8975 if (a->patch_cb) {
8976 int choice = GOT_PATCH_CHOICE_NONE;
8977 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8978 ie->path, NULL, 1, 1);
8979 if (err)
8980 break;
8981 if (choice == GOT_PATCH_CHOICE_NO)
8982 break;
8983 if (choice != GOT_PATCH_CHOICE_YES) {
8984 err = got_error(GOT_ERR_PATCH_CHOICE);
8985 break;
8988 stage = GOT_FILEIDX_STAGE_DELETE;
8989 got_fileindex_entry_stage_set(ie, stage);
8990 a->staged_something = 1;
8991 if (a->status_cb == NULL)
8992 break;
8993 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8994 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8995 de_name);
8996 break;
8997 case GOT_STATUS_NO_CHANGE:
8998 break;
8999 case GOT_STATUS_CONFLICT:
9000 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
9001 break;
9002 case GOT_STATUS_NONEXISTENT:
9003 err = got_error_set_errno(ENOENT, relpath);
9004 break;
9005 default:
9006 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
9007 break;
9010 if (path_content && unlink(path_content) == -1 && err == NULL)
9011 err = got_error_from_errno2("unlink", path_content);
9012 free(path_content);
9013 free(ondisk_path);
9014 free(new_staged_blob_id);
9015 return err;
9018 const struct got_error *
9019 got_worktree_stage(struct got_worktree *worktree,
9020 struct got_pathlist_head *paths,
9021 got_worktree_status_cb status_cb, void *status_arg,
9022 got_worktree_patch_cb patch_cb, void *patch_arg,
9023 int allow_bad_symlinks, struct got_repository *repo)
9025 const struct got_error *err = NULL, *sync_err, *unlockerr;
9026 struct got_pathlist_entry *pe;
9027 struct got_fileindex *fileindex = NULL;
9028 char *fileindex_path = NULL;
9029 struct got_reference *head_ref = NULL;
9030 struct got_object_id *head_commit_id = NULL;
9031 struct check_stage_ok_arg oka;
9032 struct stage_path_arg spa;
9034 err = lock_worktree(worktree, LOCK_EX);
9035 if (err)
9036 return err;
9038 err = got_ref_open(&head_ref, repo,
9039 got_worktree_get_head_ref_name(worktree), 0);
9040 if (err)
9041 goto done;
9042 err = got_ref_resolve(&head_commit_id, repo, head_ref);
9043 if (err)
9044 goto done;
9045 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9046 if (err)
9047 goto done;
9049 /* Check pre-conditions before staging anything. */
9050 oka.head_commit_id = head_commit_id;
9051 oka.worktree = worktree;
9052 oka.fileindex = fileindex;
9053 oka.repo = repo;
9054 oka.have_changes = 0;
9055 TAILQ_FOREACH(pe, paths, entry) {
9056 err = worktree_status(worktree, pe->path, fileindex, repo,
9057 check_stage_ok, &oka, NULL, NULL, 1, 0);
9058 if (err)
9059 goto done;
9061 if (!oka.have_changes) {
9062 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9063 goto done;
9066 spa.worktree = worktree;
9067 spa.fileindex = fileindex;
9068 spa.repo = repo;
9069 spa.patch_cb = patch_cb;
9070 spa.patch_arg = patch_arg;
9071 spa.status_cb = status_cb;
9072 spa.status_arg = status_arg;
9073 spa.staged_something = 0;
9074 spa.allow_bad_symlinks = allow_bad_symlinks;
9075 TAILQ_FOREACH(pe, paths, entry) {
9076 err = worktree_status(worktree, pe->path, fileindex, repo,
9077 stage_path, &spa, NULL, NULL, 1, 0);
9078 if (err)
9079 goto done;
9081 if (!spa.staged_something) {
9082 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9083 goto done;
9086 sync_err = sync_fileindex(fileindex, fileindex_path);
9087 if (sync_err && err == NULL)
9088 err = sync_err;
9089 done:
9090 if (head_ref)
9091 got_ref_close(head_ref);
9092 free(head_commit_id);
9093 free(fileindex_path);
9094 if (fileindex)
9095 got_fileindex_free(fileindex);
9096 unlockerr = lock_worktree(worktree, LOCK_SH);
9097 if (unlockerr && err == NULL)
9098 err = unlockerr;
9099 return err;
9102 struct unstage_path_arg {
9103 struct got_worktree *worktree;
9104 struct got_fileindex *fileindex;
9105 struct got_repository *repo;
9106 got_worktree_checkout_cb progress_cb;
9107 void *progress_arg;
9108 got_worktree_patch_cb patch_cb;
9109 void *patch_arg;
9112 static const struct got_error *
9113 create_unstaged_content(char **path_unstaged_content,
9114 char **path_new_staged_content, struct got_object_id *blob_id,
9115 struct got_object_id *staged_blob_id, const char *relpath,
9116 struct got_repository *repo,
9117 got_worktree_patch_cb patch_cb, void *patch_arg)
9119 const struct got_error *err, *free_err;
9120 struct got_blob_object *blob = NULL, *staged_blob = NULL;
9121 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
9122 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
9123 struct got_diffreg_result *diffreg_result = NULL;
9124 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
9125 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
9126 int fd1 = -1, fd2 = -1;
9128 *path_unstaged_content = NULL;
9129 *path_new_staged_content = NULL;
9131 err = got_object_id_str(&label1, blob_id);
9132 if (err)
9133 return err;
9135 fd1 = got_opentempfd();
9136 if (fd1 == -1) {
9137 err = got_error_from_errno("got_opentempfd");
9138 goto done;
9140 fd2 = got_opentempfd();
9141 if (fd2 == -1) {
9142 err = got_error_from_errno("got_opentempfd");
9143 goto done;
9146 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
9147 if (err)
9148 goto done;
9150 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
9151 if (err)
9152 goto done;
9154 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
9155 if (err)
9156 goto done;
9158 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
9159 fd2);
9160 if (err)
9161 goto done;
9163 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
9164 if (err)
9165 goto done;
9167 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
9168 if (err)
9169 goto done;
9171 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
9172 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
9173 if (err)
9174 goto done;
9176 err = got_opentemp_named(path_unstaged_content, &outfile,
9177 "got-unstaged-content", "");
9178 if (err)
9179 goto done;
9180 err = got_opentemp_named(path_new_staged_content, &rejectfile,
9181 "got-new-staged-content", "");
9182 if (err)
9183 goto done;
9185 if (fseek(f1, 0L, SEEK_SET) == -1) {
9186 err = got_ferror(f1, GOT_ERR_IO);
9187 goto done;
9189 if (fseek(f2, 0L, SEEK_SET) == -1) {
9190 err = got_ferror(f2, GOT_ERR_IO);
9191 goto done;
9193 /* Count the number of actual changes in the diff result. */
9194 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9195 struct diff_chunk_context cc = {};
9196 diff_chunk_context_load_change(&cc, &nchunks_used,
9197 diffreg_result->result, n, 0);
9198 nchanges++;
9200 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9201 int choice;
9202 err = apply_or_reject_change(&choice, &nchunks_used,
9203 diffreg_result->result, n, relpath, f1, f2,
9204 &line_cur1, &line_cur2,
9205 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
9206 if (err)
9207 goto done;
9208 if (choice == GOT_PATCH_CHOICE_YES)
9209 have_content = 1;
9210 else
9211 have_rejected_content = 1;
9212 if (choice == GOT_PATCH_CHOICE_QUIT)
9213 break;
9215 if (have_content || have_rejected_content)
9216 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
9217 outfile, rejectfile);
9218 done:
9219 free(label1);
9220 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9221 err = got_error_from_errno("close");
9222 if (blob)
9223 got_object_blob_close(blob);
9224 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9225 err = got_error_from_errno("close");
9226 if (staged_blob)
9227 got_object_blob_close(staged_blob);
9228 free_err = got_diffreg_result_free(diffreg_result);
9229 if (free_err && err == NULL)
9230 err = free_err;
9231 if (f1 && fclose(f1) == EOF && err == NULL)
9232 err = got_error_from_errno2("fclose", path1);
9233 if (f2 && fclose(f2) == EOF && err == NULL)
9234 err = got_error_from_errno2("fclose", path2);
9235 if (outfile && fclose(outfile) == EOF && err == NULL)
9236 err = got_error_from_errno2("fclose", *path_unstaged_content);
9237 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
9238 err = got_error_from_errno2("fclose", *path_new_staged_content);
9239 if (path1 && unlink(path1) == -1 && err == NULL)
9240 err = got_error_from_errno2("unlink", path1);
9241 if (path2 && unlink(path2) == -1 && err == NULL)
9242 err = got_error_from_errno2("unlink", path2);
9243 if (err || !have_content) {
9244 if (*path_unstaged_content &&
9245 unlink(*path_unstaged_content) == -1 && err == NULL)
9246 err = got_error_from_errno2("unlink",
9247 *path_unstaged_content);
9248 free(*path_unstaged_content);
9249 *path_unstaged_content = NULL;
9251 if (err || !have_content || !have_rejected_content) {
9252 if (*path_new_staged_content &&
9253 unlink(*path_new_staged_content) == -1 && err == NULL)
9254 err = got_error_from_errno2("unlink",
9255 *path_new_staged_content);
9256 free(*path_new_staged_content);
9257 *path_new_staged_content = NULL;
9259 free(path1);
9260 free(path2);
9261 return err;
9264 static const struct got_error *
9265 unstage_hunks(struct got_object_id *staged_blob_id,
9266 struct got_blob_object *blob_base,
9267 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
9268 const char *ondisk_path, const char *label_orig,
9269 struct got_worktree *worktree, struct got_repository *repo,
9270 got_worktree_patch_cb patch_cb, void *patch_arg,
9271 got_worktree_checkout_cb progress_cb, void *progress_arg)
9273 const struct got_error *err = NULL;
9274 char *path_unstaged_content = NULL;
9275 char *path_new_staged_content = NULL;
9276 char *parent = NULL, *base_path = NULL;
9277 char *blob_base_path = NULL;
9278 struct got_object_id *new_staged_blob_id = NULL;
9279 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
9280 struct stat sb;
9282 err = create_unstaged_content(&path_unstaged_content,
9283 &path_new_staged_content, blob_id, staged_blob_id,
9284 ie->path, repo, patch_cb, patch_arg);
9285 if (err)
9286 return err;
9288 if (path_unstaged_content == NULL)
9289 return NULL;
9291 if (path_new_staged_content) {
9292 err = got_object_blob_create(&new_staged_blob_id,
9293 path_new_staged_content, repo);
9294 if (err)
9295 goto done;
9298 f = fopen(path_unstaged_content, "re");
9299 if (f == NULL) {
9300 err = got_error_from_errno2("fopen",
9301 path_unstaged_content);
9302 goto done;
9304 if (fstat(fileno(f), &sb) == -1) {
9305 err = got_error_from_errno2("fstat", path_unstaged_content);
9306 goto done;
9308 if (got_fileindex_entry_staged_filetype_get(ie) ==
9309 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
9310 char link_target[PATH_MAX];
9311 size_t r;
9312 r = fread(link_target, 1, sizeof(link_target), f);
9313 if (r == 0 && ferror(f)) {
9314 err = got_error_from_errno("fread");
9315 goto done;
9317 if (r >= sizeof(link_target)) { /* should not happen */
9318 err = got_error(GOT_ERR_NO_SPACE);
9319 goto done;
9321 link_target[r] = '\0';
9322 err = merge_symlink(worktree, blob_base,
9323 ondisk_path, ie->path, label_orig, link_target,
9324 worktree->base_commit_id, repo, progress_cb,
9325 progress_arg);
9326 } else {
9327 int local_changes_subsumed;
9329 err = got_path_dirname(&parent, ondisk_path);
9330 if (err)
9331 return err;
9333 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
9334 parent) == -1) {
9335 err = got_error_from_errno("asprintf");
9336 base_path = NULL;
9337 goto done;
9340 err = got_opentemp_named(&blob_base_path, &f_base,
9341 base_path, "");
9342 if (err)
9343 goto done;
9344 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
9345 blob_base);
9346 if (err)
9347 goto done;
9350 * In order the run a 3-way merge with a symlink we copy the symlink's
9351 * target path into a temporary file and use that file with diff3.
9353 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9354 err = dump_symlink_target_path_to_file(&f_deriv2,
9355 ondisk_path);
9356 if (err)
9357 goto done;
9358 } else {
9359 int fd;
9360 fd = open(ondisk_path,
9361 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
9362 if (fd == -1) {
9363 err = got_error_from_errno2("open", ondisk_path);
9364 goto done;
9366 f_deriv2 = fdopen(fd, "r");
9367 if (f_deriv2 == NULL) {
9368 err = got_error_from_errno2("fdopen", ondisk_path);
9369 close(fd);
9370 goto done;
9374 err = merge_file(&local_changes_subsumed, worktree,
9375 f_base, f, f_deriv2, ondisk_path, ie->path,
9376 got_fileindex_perms_to_st(ie),
9377 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
9378 repo, progress_cb, progress_arg);
9380 if (err)
9381 goto done;
9383 if (new_staged_blob_id) {
9384 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9385 SHA1_DIGEST_LENGTH);
9386 } else {
9387 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9388 got_fileindex_entry_staged_filetype_set(ie, 0);
9390 done:
9391 free(new_staged_blob_id);
9392 if (path_unstaged_content &&
9393 unlink(path_unstaged_content) == -1 && err == NULL)
9394 err = got_error_from_errno2("unlink", path_unstaged_content);
9395 if (path_new_staged_content &&
9396 unlink(path_new_staged_content) == -1 && err == NULL)
9397 err = got_error_from_errno2("unlink", path_new_staged_content);
9398 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
9399 err = got_error_from_errno2("unlink", blob_base_path);
9400 if (f_base && fclose(f_base) == EOF && err == NULL)
9401 err = got_error_from_errno2("fclose", path_unstaged_content);
9402 if (f && fclose(f) == EOF && err == NULL)
9403 err = got_error_from_errno2("fclose", path_unstaged_content);
9404 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
9405 err = got_error_from_errno2("fclose", ondisk_path);
9406 free(path_unstaged_content);
9407 free(path_new_staged_content);
9408 free(blob_base_path);
9409 free(parent);
9410 free(base_path);
9411 return err;
9414 static const struct got_error *
9415 unstage_path(void *arg, unsigned char status,
9416 unsigned char staged_status, const char *relpath,
9417 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9418 struct got_object_id *commit_id, int dirfd, const char *de_name)
9420 const struct got_error *err = NULL;
9421 struct unstage_path_arg *a = arg;
9422 struct got_fileindex_entry *ie;
9423 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
9424 char *ondisk_path = NULL;
9425 char *id_str = NULL, *label_orig = NULL;
9426 int local_changes_subsumed;
9427 struct stat sb;
9428 int fd1 = -1, fd2 = -1;
9430 if (staged_status != GOT_STATUS_ADD &&
9431 staged_status != GOT_STATUS_MODIFY &&
9432 staged_status != GOT_STATUS_DELETE)
9433 return NULL;
9435 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9436 if (ie == NULL)
9437 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9439 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9440 == -1)
9441 return got_error_from_errno("asprintf");
9443 err = got_object_id_str(&id_str,
9444 commit_id ? commit_id : a->worktree->base_commit_id);
9445 if (err)
9446 goto done;
9447 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9448 id_str) == -1) {
9449 err = got_error_from_errno("asprintf");
9450 goto done;
9453 fd1 = got_opentempfd();
9454 if (fd1 == -1) {
9455 err = got_error_from_errno("got_opentempfd");
9456 goto done;
9458 fd2 = got_opentempfd();
9459 if (fd2 == -1) {
9460 err = got_error_from_errno("got_opentempfd");
9461 goto done;
9464 switch (staged_status) {
9465 case GOT_STATUS_MODIFY:
9466 err = got_object_open_as_blob(&blob_base, a->repo,
9467 blob_id, 8192, fd1);
9468 if (err)
9469 break;
9470 /* fall through */
9471 case GOT_STATUS_ADD:
9472 if (a->patch_cb) {
9473 if (staged_status == GOT_STATUS_ADD) {
9474 int choice = GOT_PATCH_CHOICE_NONE;
9475 err = (*a->patch_cb)(&choice, a->patch_arg,
9476 staged_status, ie->path, NULL, 1, 1);
9477 if (err)
9478 break;
9479 if (choice != GOT_PATCH_CHOICE_YES)
9480 break;
9481 } else {
9482 err = unstage_hunks(staged_blob_id,
9483 blob_base, blob_id, ie, ondisk_path,
9484 label_orig, a->worktree, a->repo,
9485 a->patch_cb, a->patch_arg,
9486 a->progress_cb, a->progress_arg);
9487 break; /* Done with this file. */
9490 err = got_object_open_as_blob(&blob_staged, a->repo,
9491 staged_blob_id, 8192, fd2);
9492 if (err)
9493 break;
9494 switch (got_fileindex_entry_staged_filetype_get(ie)) {
9495 case GOT_FILEIDX_MODE_BAD_SYMLINK:
9496 case GOT_FILEIDX_MODE_REGULAR_FILE:
9497 err = merge_blob(&local_changes_subsumed, a->worktree,
9498 blob_base, ondisk_path, relpath,
9499 got_fileindex_perms_to_st(ie), label_orig,
9500 blob_staged, commit_id ? commit_id :
9501 a->worktree->base_commit_id, a->repo,
9502 a->progress_cb, a->progress_arg);
9503 break;
9504 case GOT_FILEIDX_MODE_SYMLINK:
9505 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9506 char *staged_target;
9507 err = got_object_blob_read_to_str(
9508 &staged_target, blob_staged);
9509 if (err)
9510 goto done;
9511 err = merge_symlink(a->worktree, blob_base,
9512 ondisk_path, relpath, label_orig,
9513 staged_target, commit_id ? commit_id :
9514 a->worktree->base_commit_id,
9515 a->repo, a->progress_cb, a->progress_arg);
9516 free(staged_target);
9517 } else {
9518 err = merge_blob(&local_changes_subsumed,
9519 a->worktree, blob_base, ondisk_path,
9520 relpath, got_fileindex_perms_to_st(ie),
9521 label_orig, blob_staged,
9522 commit_id ? commit_id :
9523 a->worktree->base_commit_id, a->repo,
9524 a->progress_cb, a->progress_arg);
9526 break;
9527 default:
9528 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9529 break;
9531 if (err == NULL) {
9532 got_fileindex_entry_stage_set(ie,
9533 GOT_FILEIDX_STAGE_NONE);
9534 got_fileindex_entry_staged_filetype_set(ie, 0);
9536 break;
9537 case GOT_STATUS_DELETE:
9538 if (a->patch_cb) {
9539 int choice = GOT_PATCH_CHOICE_NONE;
9540 err = (*a->patch_cb)(&choice, a->patch_arg,
9541 staged_status, ie->path, NULL, 1, 1);
9542 if (err)
9543 break;
9544 if (choice == GOT_PATCH_CHOICE_NO)
9545 break;
9546 if (choice != GOT_PATCH_CHOICE_YES) {
9547 err = got_error(GOT_ERR_PATCH_CHOICE);
9548 break;
9551 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9552 got_fileindex_entry_staged_filetype_set(ie, 0);
9553 err = get_file_status(&status, &sb, ie, ondisk_path,
9554 dirfd, de_name, a->repo);
9555 if (err)
9556 break;
9557 err = (*a->progress_cb)(a->progress_arg, status, relpath);
9558 break;
9560 done:
9561 free(ondisk_path);
9562 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9563 err = got_error_from_errno("close");
9564 if (blob_base)
9565 got_object_blob_close(blob_base);
9566 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9567 err = got_error_from_errno("close");
9568 if (blob_staged)
9569 got_object_blob_close(blob_staged);
9570 free(id_str);
9571 free(label_orig);
9572 return err;
9575 const struct got_error *
9576 got_worktree_unstage(struct got_worktree *worktree,
9577 struct got_pathlist_head *paths,
9578 got_worktree_checkout_cb progress_cb, void *progress_arg,
9579 got_worktree_patch_cb patch_cb, void *patch_arg,
9580 struct got_repository *repo)
9582 const struct got_error *err = NULL, *sync_err, *unlockerr;
9583 struct got_pathlist_entry *pe;
9584 struct got_fileindex *fileindex = NULL;
9585 char *fileindex_path = NULL;
9586 struct unstage_path_arg upa;
9588 err = lock_worktree(worktree, LOCK_EX);
9589 if (err)
9590 return err;
9592 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9593 if (err)
9594 goto done;
9596 upa.worktree = worktree;
9597 upa.fileindex = fileindex;
9598 upa.repo = repo;
9599 upa.progress_cb = progress_cb;
9600 upa.progress_arg = progress_arg;
9601 upa.patch_cb = patch_cb;
9602 upa.patch_arg = patch_arg;
9603 TAILQ_FOREACH(pe, paths, entry) {
9604 err = worktree_status(worktree, pe->path, fileindex, repo,
9605 unstage_path, &upa, NULL, NULL, 1, 0);
9606 if (err)
9607 goto done;
9610 sync_err = sync_fileindex(fileindex, fileindex_path);
9611 if (sync_err && err == NULL)
9612 err = sync_err;
9613 done:
9614 free(fileindex_path);
9615 if (fileindex)
9616 got_fileindex_free(fileindex);
9617 unlockerr = lock_worktree(worktree, LOCK_SH);
9618 if (unlockerr && err == NULL)
9619 err = unlockerr;
9620 return err;
9623 struct report_file_info_arg {
9624 struct got_worktree *worktree;
9625 got_worktree_path_info_cb info_cb;
9626 void *info_arg;
9627 struct got_pathlist_head *paths;
9628 got_cancel_cb cancel_cb;
9629 void *cancel_arg;
9632 static const struct got_error *
9633 report_file_info(void *arg, struct got_fileindex_entry *ie)
9635 struct report_file_info_arg *a = arg;
9636 struct got_pathlist_entry *pe;
9637 struct got_object_id blob_id, staged_blob_id, commit_id;
9638 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9639 struct got_object_id *commit_idp = NULL;
9640 int stage;
9642 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9643 return got_error(GOT_ERR_CANCELLED);
9645 TAILQ_FOREACH(pe, a->paths, entry) {
9646 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9647 got_path_is_child(ie->path, pe->path, pe->path_len))
9648 break;
9650 if (pe == NULL) /* not found */
9651 return NULL;
9653 if (got_fileindex_entry_has_blob(ie))
9654 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9655 stage = got_fileindex_entry_stage_get(ie);
9656 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9657 stage == GOT_FILEIDX_STAGE_ADD) {
9658 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9659 &staged_blob_id, ie);
9662 if (got_fileindex_entry_has_commit(ie))
9663 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9665 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9666 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9669 const struct got_error *
9670 got_worktree_path_info(struct got_worktree *worktree,
9671 struct got_pathlist_head *paths,
9672 got_worktree_path_info_cb info_cb, void *info_arg,
9673 got_cancel_cb cancel_cb, void *cancel_arg)
9676 const struct got_error *err = NULL, *unlockerr;
9677 struct got_fileindex *fileindex = NULL;
9678 char *fileindex_path = NULL;
9679 struct report_file_info_arg arg;
9681 err = lock_worktree(worktree, LOCK_SH);
9682 if (err)
9683 return err;
9685 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9686 if (err)
9687 goto done;
9689 arg.worktree = worktree;
9690 arg.info_cb = info_cb;
9691 arg.info_arg = info_arg;
9692 arg.paths = paths;
9693 arg.cancel_cb = cancel_cb;
9694 arg.cancel_arg = cancel_arg;
9695 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9696 &arg);
9697 done:
9698 free(fileindex_path);
9699 if (fileindex)
9700 got_fileindex_free(fileindex);
9701 unlockerr = lock_worktree(worktree, LOCK_UN);
9702 if (unlockerr && err == NULL)
9703 err = unlockerr;
9704 return err;
9707 static const struct got_error *
9708 patch_check_path(const char *p, char **path, unsigned char *status,
9709 unsigned char *staged_status, struct got_fileindex *fileindex,
9710 struct got_worktree *worktree, struct got_repository *repo)
9712 const struct got_error *err;
9713 struct got_fileindex_entry *ie;
9714 struct stat sb;
9715 char *ondisk_path = NULL;
9717 err = got_worktree_resolve_path(path, worktree, p);
9718 if (err)
9719 return err;
9721 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9722 *path[0] ? "/" : "", *path) == -1)
9723 return got_error_from_errno("asprintf");
9725 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9726 if (ie) {
9727 *staged_status = get_staged_status(ie);
9728 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9729 repo);
9730 if (err)
9731 goto done;
9732 } else {
9733 *staged_status = GOT_STATUS_NO_CHANGE;
9734 *status = GOT_STATUS_UNVERSIONED;
9735 if (lstat(ondisk_path, &sb) == -1) {
9736 if (errno != ENOENT) {
9737 err = got_error_from_errno2("lstat",
9738 ondisk_path);
9739 goto done;
9741 *status = GOT_STATUS_NONEXISTENT;
9745 done:
9746 free(ondisk_path);
9747 return err;
9750 static const struct got_error *
9751 patch_can_rm(const char *path, unsigned char status,
9752 unsigned char staged_status)
9754 if (status == GOT_STATUS_NONEXISTENT)
9755 return got_error_set_errno(ENOENT, path);
9756 if (status != GOT_STATUS_NO_CHANGE &&
9757 status != GOT_STATUS_ADD &&
9758 status != GOT_STATUS_MODIFY &&
9759 status != GOT_STATUS_MODE_CHANGE)
9760 return got_error_path(path, GOT_ERR_FILE_STATUS);
9761 if (staged_status == GOT_STATUS_DELETE)
9762 return got_error_path(path, GOT_ERR_FILE_STATUS);
9763 return NULL;
9766 static const struct got_error *
9767 patch_can_add(const char *path, unsigned char status)
9769 if (status != GOT_STATUS_NONEXISTENT)
9770 return got_error_path(path, GOT_ERR_FILE_STATUS);
9771 return NULL;
9774 static const struct got_error *
9775 patch_can_edit(const char *path, unsigned char status,
9776 unsigned char staged_status)
9778 if (status == GOT_STATUS_NONEXISTENT)
9779 return got_error_set_errno(ENOENT, path);
9780 if (status != GOT_STATUS_NO_CHANGE &&
9781 status != GOT_STATUS_ADD &&
9782 status != GOT_STATUS_MODIFY)
9783 return got_error_path(path, GOT_ERR_FILE_STATUS);
9784 if (staged_status == GOT_STATUS_DELETE)
9785 return got_error_path(path, GOT_ERR_FILE_STATUS);
9786 return NULL;
9789 const struct got_error *
9790 got_worktree_patch_prepare(struct got_fileindex **fileindex,
9791 char **fileindex_path, struct got_worktree *worktree)
9793 return open_fileindex(fileindex, fileindex_path, worktree);
9796 const struct got_error *
9797 got_worktree_patch_check_path(const char *old, const char *new,
9798 char **oldpath, char **newpath, struct got_worktree *worktree,
9799 struct got_repository *repo, struct got_fileindex *fileindex)
9801 const struct got_error *err = NULL;
9802 int file_renamed = 0;
9803 unsigned char status_old, staged_status_old;
9804 unsigned char status_new, staged_status_new;
9806 *oldpath = NULL;
9807 *newpath = NULL;
9809 err = patch_check_path(old != NULL ? old : new, oldpath,
9810 &status_old, &staged_status_old, fileindex, worktree, repo);
9811 if (err)
9812 goto done;
9814 err = patch_check_path(new != NULL ? new : old, newpath,
9815 &status_new, &staged_status_new, fileindex, worktree, repo);
9816 if (err)
9817 goto done;
9819 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9820 file_renamed = 1;
9822 if (old != NULL && new == NULL)
9823 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9824 else if (file_renamed) {
9825 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9826 if (err == NULL)
9827 err = patch_can_add(*newpath, status_new);
9828 } else if (old == NULL)
9829 err = patch_can_add(*newpath, status_new);
9830 else
9831 err = patch_can_edit(*newpath, status_new, staged_status_new);
9833 done:
9834 if (err) {
9835 free(*oldpath);
9836 *oldpath = NULL;
9837 free(*newpath);
9838 *newpath = NULL;
9840 return err;
9843 const struct got_error *
9844 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9845 struct got_worktree *worktree, struct got_fileindex *fileindex,
9846 got_worktree_checkout_cb progress_cb, void *progress_arg)
9848 struct schedule_addition_args saa;
9850 memset(&saa, 0, sizeof(saa));
9851 saa.worktree = worktree;
9852 saa.fileindex = fileindex;
9853 saa.progress_cb = progress_cb;
9854 saa.progress_arg = progress_arg;
9855 saa.repo = repo;
9857 return worktree_status(worktree, path, fileindex, repo,
9858 schedule_addition, &saa, NULL, NULL, 1, 0);
9861 const struct got_error *
9862 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9863 struct got_worktree *worktree, struct got_fileindex *fileindex,
9864 got_worktree_delete_cb progress_cb, void *progress_arg)
9866 const struct got_error *err;
9867 struct schedule_deletion_args sda;
9868 char *ondisk_status_path;
9870 memset(&sda, 0, sizeof(sda));
9871 sda.worktree = worktree;
9872 sda.fileindex = fileindex;
9873 sda.progress_cb = progress_cb;
9874 sda.progress_arg = progress_arg;
9875 sda.repo = repo;
9876 sda.delete_local_mods = 0;
9877 sda.keep_on_disk = 0;
9878 sda.ignore_missing_paths = 0;
9879 sda.status_codes = NULL;
9880 if (asprintf(&ondisk_status_path, "%s/%s",
9881 got_worktree_get_root_path(worktree), path) == -1)
9882 return got_error_from_errno("asprintf");
9883 sda.status_path = ondisk_status_path;
9884 sda.status_path_len = strlen(ondisk_status_path);
9886 err = worktree_status(worktree, path, fileindex, repo,
9887 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9888 free(ondisk_status_path);
9889 return err;
9892 const struct got_error *
9893 got_worktree_patch_complete(struct got_fileindex *fileindex,
9894 const char *fileindex_path)
9896 const struct got_error *err = NULL;
9898 err = sync_fileindex(fileindex, fileindex_path);
9899 got_fileindex_free(fileindex);
9901 return err;