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 const struct got_error *
4198 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
4199 const char *relpath, struct got_object_id *blob_id,
4200 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4201 int dirfd, const char *de_name)
4203 struct schedule_addition_args *a = arg;
4204 const struct got_error *err = NULL;
4205 struct got_fileindex_entry *ie;
4206 struct stat sb;
4207 char *ondisk_path;
4209 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4210 relpath) == -1)
4211 return got_error_from_errno("asprintf");
4213 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4214 if (ie) {
4215 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
4216 de_name, a->repo);
4217 if (err)
4218 goto done;
4219 /* Re-adding an existing entry is a no-op. */
4220 if (status == GOT_STATUS_ADD)
4221 goto done;
4222 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4223 if (err)
4224 goto done;
4227 if (status != GOT_STATUS_UNVERSIONED) {
4228 if (status == GOT_STATUS_NONEXISTENT)
4229 err = got_error_set_errno(ENOENT, ondisk_path);
4230 else
4231 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
4232 goto done;
4235 err = got_fileindex_entry_alloc(&ie, relpath);
4236 if (err)
4237 goto done;
4238 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
4239 relpath, NULL, NULL, 1);
4240 if (err) {
4241 got_fileindex_entry_free(ie);
4242 goto done;
4244 err = got_fileindex_entry_add(a->fileindex, ie);
4245 if (err) {
4246 got_fileindex_entry_free(ie);
4247 goto done;
4249 done:
4250 free(ondisk_path);
4251 if (err)
4252 return err;
4253 if (status == GOT_STATUS_ADD)
4254 return NULL;
4255 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4258 const struct got_error *
4259 got_worktree_schedule_add(struct got_worktree *worktree,
4260 struct got_pathlist_head *paths,
4261 got_worktree_checkout_cb progress_cb, void *progress_arg,
4262 struct got_repository *repo, int no_ignores)
4264 struct got_fileindex *fileindex = NULL;
4265 char *fileindex_path = NULL;
4266 const struct got_error *err = NULL, *sync_err, *unlockerr;
4267 struct got_pathlist_entry *pe;
4268 struct schedule_addition_args saa;
4270 err = lock_worktree(worktree, LOCK_EX);
4271 if (err)
4272 return err;
4274 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4275 if (err)
4276 goto done;
4278 saa.worktree = worktree;
4279 saa.fileindex = fileindex;
4280 saa.progress_cb = progress_cb;
4281 saa.progress_arg = progress_arg;
4282 saa.repo = repo;
4284 TAILQ_FOREACH(pe, paths, entry) {
4285 err = worktree_status(worktree, pe->path, fileindex, repo,
4286 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4287 if (err)
4288 break;
4290 sync_err = sync_fileindex(fileindex, fileindex_path);
4291 if (sync_err && err == NULL)
4292 err = sync_err;
4293 done:
4294 free(fileindex_path);
4295 if (fileindex)
4296 got_fileindex_free(fileindex);
4297 unlockerr = lock_worktree(worktree, LOCK_SH);
4298 if (unlockerr && err == NULL)
4299 err = unlockerr;
4300 return err;
4303 struct schedule_deletion_args {
4304 struct got_worktree *worktree;
4305 struct got_fileindex *fileindex;
4306 got_worktree_delete_cb progress_cb;
4307 void *progress_arg;
4308 struct got_repository *repo;
4309 int delete_local_mods;
4310 int keep_on_disk;
4311 int ignore_missing_paths;
4312 const char *status_path;
4313 size_t status_path_len;
4314 const char *status_codes;
4317 static const struct got_error *
4318 schedule_for_deletion(void *arg, unsigned char status,
4319 unsigned char staged_status, const char *relpath,
4320 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4321 struct got_object_id *commit_id, int dirfd, const char *de_name)
4323 struct schedule_deletion_args *a = arg;
4324 const struct got_error *err = NULL;
4325 struct got_fileindex_entry *ie = NULL;
4326 struct stat sb;
4327 char *ondisk_path;
4329 if (status == GOT_STATUS_NONEXISTENT) {
4330 if (a->ignore_missing_paths)
4331 return NULL;
4332 return got_error_set_errno(ENOENT, relpath);
4335 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4336 if (ie == NULL)
4337 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4339 staged_status = get_staged_status(ie);
4340 if (staged_status != GOT_STATUS_NO_CHANGE) {
4341 if (staged_status == GOT_STATUS_DELETE)
4342 return NULL;
4343 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4346 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4347 relpath) == -1)
4348 return got_error_from_errno("asprintf");
4350 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4351 a->repo);
4352 if (err)
4353 goto done;
4355 if (a->status_codes) {
4356 size_t ncodes = strlen(a->status_codes);
4357 int i;
4358 for (i = 0; i < ncodes ; i++) {
4359 if (status == a->status_codes[i])
4360 break;
4362 if (i == ncodes) {
4363 /* Do not delete files in non-matching status. */
4364 free(ondisk_path);
4365 return NULL;
4367 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4368 a->status_codes[i] != GOT_STATUS_MISSING) {
4369 static char msg[64];
4370 snprintf(msg, sizeof(msg),
4371 "invalid status code '%c'", a->status_codes[i]);
4372 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4373 goto done;
4377 if (status != GOT_STATUS_NO_CHANGE) {
4378 if (status == GOT_STATUS_DELETE)
4379 goto done;
4380 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4381 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4382 goto done;
4384 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4385 err = got_error_set_errno(ENOENT, relpath);
4386 goto done;
4388 if (status != GOT_STATUS_MODIFY &&
4389 status != GOT_STATUS_MISSING) {
4390 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4391 goto done;
4395 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4396 size_t root_len;
4398 if (dirfd != -1) {
4399 if (unlinkat(dirfd, de_name, 0) == -1) {
4400 err = got_error_from_errno2("unlinkat",
4401 ondisk_path);
4402 goto done;
4404 } else if (unlink(ondisk_path) == -1) {
4405 err = got_error_from_errno2("unlink", ondisk_path);
4406 goto done;
4409 root_len = strlen(a->worktree->root_path);
4410 do {
4411 char *parent;
4413 err = got_path_dirname(&parent, ondisk_path);
4414 if (err)
4415 goto done;
4416 free(ondisk_path);
4417 ondisk_path = parent;
4418 if (got_path_cmp(ondisk_path, a->status_path,
4419 strlen(ondisk_path), a->status_path_len) != 0 &&
4420 !got_path_is_child(ondisk_path, a->status_path,
4421 a->status_path_len))
4422 break;
4423 if (rmdir(ondisk_path) == -1) {
4424 if (errno != ENOTEMPTY)
4425 err = got_error_from_errno2("rmdir",
4426 ondisk_path);
4427 break;
4429 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4430 strlen(ondisk_path), root_len) != 0);
4433 got_fileindex_entry_mark_deleted_from_disk(ie);
4434 done:
4435 free(ondisk_path);
4436 if (err)
4437 return err;
4438 if (status == GOT_STATUS_DELETE)
4439 return NULL;
4440 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4441 staged_status, relpath);
4444 const struct got_error *
4445 got_worktree_schedule_delete(struct got_worktree *worktree,
4446 struct got_pathlist_head *paths, int delete_local_mods,
4447 const char *status_codes,
4448 got_worktree_delete_cb progress_cb, void *progress_arg,
4449 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4451 struct got_fileindex *fileindex = NULL;
4452 char *fileindex_path = NULL;
4453 const struct got_error *err = NULL, *sync_err, *unlockerr;
4454 struct got_pathlist_entry *pe;
4455 struct schedule_deletion_args sda;
4457 err = lock_worktree(worktree, LOCK_EX);
4458 if (err)
4459 return err;
4461 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4462 if (err)
4463 goto done;
4465 sda.worktree = worktree;
4466 sda.fileindex = fileindex;
4467 sda.progress_cb = progress_cb;
4468 sda.progress_arg = progress_arg;
4469 sda.repo = repo;
4470 sda.delete_local_mods = delete_local_mods;
4471 sda.keep_on_disk = keep_on_disk;
4472 sda.ignore_missing_paths = ignore_missing_paths;
4473 sda.status_codes = status_codes;
4475 TAILQ_FOREACH(pe, paths, entry) {
4476 char *ondisk_status_path;
4478 if (asprintf(&ondisk_status_path, "%s%s%s",
4479 got_worktree_get_root_path(worktree),
4480 pe->path[0] == '\0' ? "" : "/", pe->path) == -1) {
4481 err = got_error_from_errno("asprintf");
4482 goto done;
4484 sda.status_path = ondisk_status_path;
4485 sda.status_path_len = strlen(ondisk_status_path);
4486 err = worktree_status(worktree, pe->path, fileindex, repo,
4487 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4488 free(ondisk_status_path);
4489 if (err)
4490 break;
4492 sync_err = sync_fileindex(fileindex, fileindex_path);
4493 if (sync_err && err == NULL)
4494 err = sync_err;
4495 done:
4496 free(fileindex_path);
4497 if (fileindex)
4498 got_fileindex_free(fileindex);
4499 unlockerr = lock_worktree(worktree, LOCK_SH);
4500 if (unlockerr && err == NULL)
4501 err = unlockerr;
4502 return err;
4505 static const struct got_error *
4506 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4508 const struct got_error *err = NULL;
4509 char *line = NULL;
4510 size_t linesize = 0, n;
4511 ssize_t linelen;
4513 linelen = getline(&line, &linesize, infile);
4514 if (linelen == -1) {
4515 if (ferror(infile)) {
4516 err = got_error_from_errno("getline");
4517 goto done;
4519 return NULL;
4521 if (outfile) {
4522 n = fwrite(line, 1, linelen, outfile);
4523 if (n != linelen) {
4524 err = got_ferror(outfile, GOT_ERR_IO);
4525 goto done;
4528 if (rejectfile) {
4529 n = fwrite(line, 1, linelen, rejectfile);
4530 if (n != linelen)
4531 err = got_ferror(rejectfile, GOT_ERR_IO);
4533 done:
4534 free(line);
4535 return err;
4538 static const struct got_error *
4539 skip_one_line(FILE *f)
4541 char *line = NULL;
4542 size_t linesize = 0;
4543 ssize_t linelen;
4545 linelen = getline(&line, &linesize, f);
4546 if (linelen == -1) {
4547 if (ferror(f))
4548 return got_error_from_errno("getline");
4549 return NULL;
4551 free(line);
4552 return NULL;
4555 static const struct got_error *
4556 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4557 int start_old, int end_old, int start_new, int end_new,
4558 FILE *outfile, FILE *rejectfile)
4560 const struct got_error *err;
4562 /* Copy old file's lines leading up to patch. */
4563 while (!feof(f1) && *line_cur1 < start_old) {
4564 err = copy_one_line(f1, outfile, NULL);
4565 if (err)
4566 return err;
4567 (*line_cur1)++;
4569 /* Skip new file's lines leading up to patch. */
4570 while (!feof(f2) && *line_cur2 < start_new) {
4571 if (rejectfile)
4572 err = copy_one_line(f2, NULL, rejectfile);
4573 else
4574 err = skip_one_line(f2);
4575 if (err)
4576 return err;
4577 (*line_cur2)++;
4579 /* Copy patched lines. */
4580 while (!feof(f2) && *line_cur2 <= end_new) {
4581 err = copy_one_line(f2, outfile, NULL);
4582 if (err)
4583 return err;
4584 (*line_cur2)++;
4586 /* Skip over old file's replaced lines. */
4587 while (!feof(f1) && *line_cur1 <= end_old) {
4588 if (rejectfile)
4589 err = copy_one_line(f1, NULL, rejectfile);
4590 else
4591 err = skip_one_line(f1);
4592 if (err)
4593 return err;
4594 (*line_cur1)++;
4597 return NULL;
4600 static const struct got_error *
4601 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4602 FILE *outfile, FILE *rejectfile)
4604 const struct got_error *err;
4606 if (outfile) {
4607 /* Copy old file's lines until EOF. */
4608 while (!feof(f1)) {
4609 err = copy_one_line(f1, outfile, NULL);
4610 if (err)
4611 return err;
4612 (*line_cur1)++;
4615 if (rejectfile) {
4616 /* Copy new file's lines until EOF. */
4617 while (!feof(f2)) {
4618 err = copy_one_line(f2, NULL, rejectfile);
4619 if (err)
4620 return err;
4621 (*line_cur2)++;
4625 return NULL;
4628 static const struct got_error *
4629 apply_or_reject_change(int *choice, int *nchunks_used,
4630 struct diff_result *diff_result, int n,
4631 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4632 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4633 got_worktree_patch_cb patch_cb, void *patch_arg)
4635 const struct got_error *err = NULL;
4636 struct diff_chunk_context cc = {};
4637 int start_old, end_old, start_new, end_new;
4638 FILE *hunkfile;
4639 struct diff_output_unidiff_state *diff_state;
4640 struct diff_input_info diff_info;
4641 int rc;
4643 *choice = GOT_PATCH_CHOICE_NONE;
4645 /* Get changed line numbers without context lines for copy_change(). */
4646 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4647 start_old = cc.left.start;
4648 end_old = cc.left.end;
4649 start_new = cc.right.start;
4650 end_new = cc.right.end;
4652 /* Get the same change with context lines for display. */
4653 memset(&cc, 0, sizeof(cc));
4654 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4656 memset(&diff_info, 0, sizeof(diff_info));
4657 diff_info.left_path = relpath;
4658 diff_info.right_path = relpath;
4660 diff_state = diff_output_unidiff_state_alloc();
4661 if (diff_state == NULL)
4662 return got_error_set_errno(ENOMEM,
4663 "diff_output_unidiff_state_alloc");
4665 hunkfile = got_opentemp();
4666 if (hunkfile == NULL) {
4667 err = got_error_from_errno("got_opentemp");
4668 goto done;
4671 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4672 diff_result, &cc);
4673 if (rc != DIFF_RC_OK) {
4674 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4675 goto done;
4678 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4679 err = got_ferror(hunkfile, GOT_ERR_IO);
4680 goto done;
4683 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4684 hunkfile, changeno, nchanges);
4685 if (err)
4686 goto done;
4688 switch (*choice) {
4689 case GOT_PATCH_CHOICE_YES:
4690 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4691 end_old, start_new, end_new, outfile, rejectfile);
4692 break;
4693 case GOT_PATCH_CHOICE_NO:
4694 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4695 end_old, start_new, end_new, rejectfile, outfile);
4696 break;
4697 case GOT_PATCH_CHOICE_QUIT:
4698 break;
4699 default:
4700 err = got_error(GOT_ERR_PATCH_CHOICE);
4701 break;
4703 done:
4704 diff_output_unidiff_state_free(diff_state);
4705 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4706 err = got_error_from_errno("fclose");
4707 return err;
4710 struct revert_file_args {
4711 struct got_worktree *worktree;
4712 struct got_fileindex *fileindex;
4713 got_worktree_checkout_cb progress_cb;
4714 void *progress_arg;
4715 got_worktree_patch_cb patch_cb;
4716 void *patch_arg;
4717 struct got_repository *repo;
4718 int unlink_added_files;
4719 struct got_pathlist_head *added_files_to_unlink;
4722 static const struct got_error *
4723 create_patched_content(char **path_outfile, int reverse_patch,
4724 struct got_object_id *blob_id, const char *path2,
4725 int dirfd2, const char *de_name2,
4726 const char *relpath, struct got_repository *repo,
4727 got_worktree_patch_cb patch_cb, void *patch_arg)
4729 const struct got_error *err, *free_err;
4730 struct got_blob_object *blob = NULL;
4731 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4732 int fd = -1, fd2 = -1;
4733 char link_target[PATH_MAX];
4734 ssize_t link_len = 0;
4735 char *path1 = NULL, *id_str = NULL;
4736 struct stat sb2;
4737 struct got_diffreg_result *diffreg_result = NULL;
4738 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4739 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4741 *path_outfile = NULL;
4743 err = got_object_id_str(&id_str, blob_id);
4744 if (err)
4745 return err;
4747 if (dirfd2 != -1) {
4748 fd2 = openat(dirfd2, de_name2,
4749 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4750 if (fd2 == -1) {
4751 if (!got_err_open_nofollow_on_symlink()) {
4752 err = got_error_from_errno2("openat", path2);
4753 goto done;
4755 link_len = readlinkat(dirfd2, de_name2,
4756 link_target, sizeof(link_target));
4757 if (link_len == -1) {
4758 return got_error_from_errno2("readlinkat",
4759 path2);
4761 sb2.st_mode = S_IFLNK;
4762 sb2.st_size = link_len;
4764 } else {
4765 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4766 if (fd2 == -1) {
4767 if (!got_err_open_nofollow_on_symlink()) {
4768 err = got_error_from_errno2("open", path2);
4769 goto done;
4771 link_len = readlink(path2, link_target,
4772 sizeof(link_target));
4773 if (link_len == -1)
4774 return got_error_from_errno2("readlink", path2);
4775 sb2.st_mode = S_IFLNK;
4776 sb2.st_size = link_len;
4779 if (fd2 != -1) {
4780 if (fstat(fd2, &sb2) == -1) {
4781 err = got_error_from_errno2("fstat", path2);
4782 goto done;
4785 f2 = fdopen(fd2, "r");
4786 if (f2 == NULL) {
4787 err = got_error_from_errno2("fdopen", path2);
4788 goto done;
4790 fd2 = -1;
4791 } else {
4792 size_t n;
4793 f2 = got_opentemp();
4794 if (f2 == NULL) {
4795 err = got_error_from_errno2("got_opentemp", path2);
4796 goto done;
4798 n = fwrite(link_target, 1, link_len, f2);
4799 if (n != link_len) {
4800 err = got_ferror(f2, GOT_ERR_IO);
4801 goto done;
4803 if (fflush(f2) == EOF) {
4804 err = got_error_from_errno("fflush");
4805 goto done;
4807 rewind(f2);
4810 fd = got_opentempfd();
4811 if (fd == -1) {
4812 err = got_error_from_errno("got_opentempfd");
4813 goto done;
4816 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4817 if (err)
4818 goto done;
4820 err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4821 if (err)
4822 goto done;
4824 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4825 if (err)
4826 goto done;
4828 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4829 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4830 if (err)
4831 goto done;
4833 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4834 "");
4835 if (err)
4836 goto done;
4838 if (fseek(f1, 0L, SEEK_SET) == -1)
4839 return got_ferror(f1, GOT_ERR_IO);
4840 if (fseek(f2, 0L, SEEK_SET) == -1)
4841 return got_ferror(f2, GOT_ERR_IO);
4843 /* Count the number of actual changes in the diff result. */
4844 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4845 struct diff_chunk_context cc = {};
4846 diff_chunk_context_load_change(&cc, &nchunks_used,
4847 diffreg_result->result, n, 0);
4848 nchanges++;
4850 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4851 int choice;
4852 err = apply_or_reject_change(&choice, &nchunks_used,
4853 diffreg_result->result, n, relpath, f1, f2,
4854 &line_cur1, &line_cur2,
4855 reverse_patch ? NULL : outfile,
4856 reverse_patch ? outfile : NULL,
4857 ++i, nchanges, patch_cb, patch_arg);
4858 if (err)
4859 goto done;
4860 if (choice == GOT_PATCH_CHOICE_YES)
4861 have_content = 1;
4862 else if (choice == GOT_PATCH_CHOICE_QUIT)
4863 break;
4865 if (have_content) {
4866 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4867 reverse_patch ? NULL : outfile,
4868 reverse_patch ? outfile : NULL);
4869 if (err)
4870 goto done;
4872 if (!S_ISLNK(sb2.st_mode)) {
4873 mode_t mode;
4875 mode = apply_umask(sb2.st_mode);
4876 if (fchmod(fileno(outfile), mode) == -1) {
4877 err = got_error_from_errno2("fchmod", path2);
4878 goto done;
4882 done:
4883 free(id_str);
4884 if (fd != -1 && close(fd) == -1 && err == NULL)
4885 err = got_error_from_errno("close");
4886 if (blob)
4887 got_object_blob_close(blob);
4888 free_err = got_diffreg_result_free(diffreg_result);
4889 if (err == NULL)
4890 err = free_err;
4891 if (f1 && fclose(f1) == EOF && err == NULL)
4892 err = got_error_from_errno2("fclose", path1);
4893 if (f2 && fclose(f2) == EOF && err == NULL)
4894 err = got_error_from_errno2("fclose", path2);
4895 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4896 err = got_error_from_errno2("close", path2);
4897 if (outfile && fclose(outfile) == EOF && err == NULL)
4898 err = got_error_from_errno2("fclose", *path_outfile);
4899 if (path1 && unlink(path1) == -1 && err == NULL)
4900 err = got_error_from_errno2("unlink", path1);
4901 if (err || !have_content) {
4902 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4903 err = got_error_from_errno2("unlink", *path_outfile);
4904 free(*path_outfile);
4905 *path_outfile = NULL;
4907 free(path1);
4908 return err;
4911 static const struct got_error *
4912 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4913 const char *relpath, struct got_object_id *blob_id,
4914 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4915 int dirfd, const char *de_name)
4917 struct revert_file_args *a = arg;
4918 const struct got_error *err = NULL;
4919 char *parent_path = NULL;
4920 struct got_fileindex_entry *ie;
4921 struct got_commit_object *base_commit = NULL;
4922 struct got_tree_object *tree = NULL;
4923 struct got_object_id *tree_id = NULL;
4924 const struct got_tree_entry *te = NULL;
4925 char *tree_path = NULL, *te_name;
4926 char *ondisk_path = NULL, *path_content = NULL;
4927 struct got_blob_object *blob = NULL;
4928 int fd = -1;
4930 /* Reverting a staged deletion is a no-op. */
4931 if (status == GOT_STATUS_DELETE &&
4932 staged_status != GOT_STATUS_NO_CHANGE)
4933 return NULL;
4935 if (status == GOT_STATUS_UNVERSIONED)
4936 return (*a->progress_cb)(a->progress_arg,
4937 GOT_STATUS_UNVERSIONED, relpath);
4939 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4940 if (ie == NULL)
4941 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4943 /* Construct in-repository path of tree which contains this blob. */
4944 err = got_path_dirname(&parent_path, ie->path);
4945 if (err) {
4946 if (err->code != GOT_ERR_BAD_PATH)
4947 goto done;
4948 parent_path = strdup("/");
4949 if (parent_path == NULL) {
4950 err = got_error_from_errno("strdup");
4951 goto done;
4954 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4955 tree_path = strdup(parent_path);
4956 if (tree_path == NULL) {
4957 err = got_error_from_errno("strdup");
4958 goto done;
4960 } else {
4961 if (got_path_is_root_dir(parent_path)) {
4962 tree_path = strdup(a->worktree->path_prefix);
4963 if (tree_path == NULL) {
4964 err = got_error_from_errno("strdup");
4965 goto done;
4967 } else {
4968 if (asprintf(&tree_path, "%s/%s",
4969 a->worktree->path_prefix, parent_path) == -1) {
4970 err = got_error_from_errno("asprintf");
4971 goto done;
4976 err = got_object_open_as_commit(&base_commit, a->repo,
4977 a->worktree->base_commit_id);
4978 if (err)
4979 goto done;
4981 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4982 if (err) {
4983 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4984 (status == GOT_STATUS_ADD ||
4985 staged_status == GOT_STATUS_ADD)))
4986 goto done;
4987 } else {
4988 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4989 if (err)
4990 goto done;
4992 err = got_path_basename(&te_name, ie->path);
4993 if (err)
4994 goto done;
4996 te = got_object_tree_find_entry(tree, te_name);
4997 free(te_name);
4998 if (te == NULL && status != GOT_STATUS_ADD &&
4999 staged_status != GOT_STATUS_ADD) {
5000 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
5001 goto done;
5005 switch (status) {
5006 case GOT_STATUS_ADD:
5007 if (a->patch_cb) {
5008 int choice = GOT_PATCH_CHOICE_NONE;
5009 err = (*a->patch_cb)(&choice, a->patch_arg,
5010 status, ie->path, NULL, 1, 1);
5011 if (err)
5012 goto done;
5013 if (choice != GOT_PATCH_CHOICE_YES)
5014 break;
5016 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
5017 ie->path);
5018 if (err)
5019 goto done;
5020 got_fileindex_entry_remove(a->fileindex, ie);
5021 if (a->unlink_added_files) {
5022 int do_unlink = a->added_files_to_unlink ? 0 : 1;
5024 if (a->added_files_to_unlink) {
5025 struct got_pathlist_entry *pe;
5027 TAILQ_FOREACH(pe, a->added_files_to_unlink,
5028 entry) {
5029 if (got_path_cmp(pe->path, relpath,
5030 pe->path_len, strlen(relpath)))
5031 continue;
5032 do_unlink = 1;
5033 break;
5037 if (do_unlink) {
5038 if (asprintf(&ondisk_path, "%s/%s",
5039 got_worktree_get_root_path(a->worktree),
5040 relpath) == -1) {
5041 err = got_error_from_errno("asprintf");
5042 goto done;
5044 if (unlink(ondisk_path) == -1) {
5045 err = got_error_from_errno2("unlink",
5046 ondisk_path);
5047 break;
5051 break;
5052 case GOT_STATUS_DELETE:
5053 if (a->patch_cb) {
5054 int choice = GOT_PATCH_CHOICE_NONE;
5055 err = (*a->patch_cb)(&choice, a->patch_arg,
5056 status, ie->path, NULL, 1, 1);
5057 if (err)
5058 goto done;
5059 if (choice != GOT_PATCH_CHOICE_YES)
5060 break;
5062 /* fall through */
5063 case GOT_STATUS_MODIFY:
5064 case GOT_STATUS_MODE_CHANGE:
5065 case GOT_STATUS_CONFLICT:
5066 case GOT_STATUS_MISSING: {
5067 struct got_object_id id;
5068 if (staged_status == GOT_STATUS_ADD ||
5069 staged_status == GOT_STATUS_MODIFY)
5070 got_fileindex_entry_get_staged_blob_id(&id, ie);
5071 else
5072 got_fileindex_entry_get_blob_id(&id, ie);
5073 fd = got_opentempfd();
5074 if (fd == -1) {
5075 err = got_error_from_errno("got_opentempfd");
5076 goto done;
5079 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
5080 if (err)
5081 goto done;
5083 if (asprintf(&ondisk_path, "%s/%s",
5084 got_worktree_get_root_path(a->worktree), relpath) == -1) {
5085 err = got_error_from_errno("asprintf");
5086 goto done;
5089 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
5090 status == GOT_STATUS_CONFLICT)) {
5091 int is_bad_symlink = 0;
5092 err = create_patched_content(&path_content, 1, &id,
5093 ondisk_path, dirfd, de_name, ie->path, a->repo,
5094 a->patch_cb, a->patch_arg);
5095 if (err || path_content == NULL)
5096 break;
5097 if (te && S_ISLNK(te->mode)) {
5098 if (unlink(path_content) == -1) {
5099 err = got_error_from_errno2("unlink",
5100 path_content);
5101 break;
5103 err = install_symlink(&is_bad_symlink,
5104 a->worktree, ondisk_path, ie->path,
5105 blob, 0, 1, 0, 0, a->repo,
5106 a->progress_cb, a->progress_arg);
5107 } else {
5108 if (rename(path_content, ondisk_path) == -1) {
5109 err = got_error_from_errno3("rename",
5110 path_content, ondisk_path);
5111 goto done;
5114 } else {
5115 int is_bad_symlink = 0;
5116 if (te && S_ISLNK(te->mode)) {
5117 err = install_symlink(&is_bad_symlink,
5118 a->worktree, ondisk_path, ie->path,
5119 blob, 0, 1, 0, 0, a->repo,
5120 a->progress_cb, a->progress_arg);
5121 } else {
5122 err = install_blob(a->worktree, ondisk_path,
5123 ie->path,
5124 te ? te->mode : GOT_DEFAULT_FILE_MODE,
5125 got_fileindex_perms_to_st(ie), blob,
5126 0, 1, 0, 0, a->repo,
5127 a->progress_cb, a->progress_arg);
5129 if (err)
5130 goto done;
5131 if (status == GOT_STATUS_DELETE ||
5132 status == GOT_STATUS_MODE_CHANGE) {
5133 err = got_fileindex_entry_update(ie,
5134 a->worktree->root_fd, relpath,
5135 blob->id.sha1,
5136 a->worktree->base_commit_id->sha1, 1);
5137 if (err)
5138 goto done;
5140 if (is_bad_symlink) {
5141 got_fileindex_entry_filetype_set(ie,
5142 GOT_FILEIDX_MODE_BAD_SYMLINK);
5145 break;
5147 default:
5148 break;
5150 done:
5151 free(ondisk_path);
5152 free(path_content);
5153 free(parent_path);
5154 free(tree_path);
5155 if (fd != -1 && close(fd) == -1 && err == NULL)
5156 err = got_error_from_errno("close");
5157 if (blob)
5158 got_object_blob_close(blob);
5159 if (tree)
5160 got_object_tree_close(tree);
5161 free(tree_id);
5162 if (base_commit)
5163 got_object_commit_close(base_commit);
5164 return err;
5167 const struct got_error *
5168 got_worktree_revert(struct got_worktree *worktree,
5169 struct got_pathlist_head *paths,
5170 got_worktree_checkout_cb progress_cb, void *progress_arg,
5171 got_worktree_patch_cb patch_cb, void *patch_arg,
5172 struct got_repository *repo)
5174 struct got_fileindex *fileindex = NULL;
5175 char *fileindex_path = NULL;
5176 const struct got_error *err = NULL, *unlockerr = NULL;
5177 const struct got_error *sync_err = NULL;
5178 struct got_pathlist_entry *pe;
5179 struct revert_file_args rfa;
5181 err = lock_worktree(worktree, LOCK_EX);
5182 if (err)
5183 return err;
5185 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5186 if (err)
5187 goto done;
5189 rfa.worktree = worktree;
5190 rfa.fileindex = fileindex;
5191 rfa.progress_cb = progress_cb;
5192 rfa.progress_arg = progress_arg;
5193 rfa.patch_cb = patch_cb;
5194 rfa.patch_arg = patch_arg;
5195 rfa.repo = repo;
5196 rfa.unlink_added_files = 0;
5197 TAILQ_FOREACH(pe, paths, entry) {
5198 err = worktree_status(worktree, pe->path, fileindex, repo,
5199 revert_file, &rfa, NULL, NULL, 1, 0);
5200 if (err)
5201 break;
5203 sync_err = sync_fileindex(fileindex, fileindex_path);
5204 if (sync_err && err == NULL)
5205 err = sync_err;
5206 done:
5207 free(fileindex_path);
5208 if (fileindex)
5209 got_fileindex_free(fileindex);
5210 unlockerr = lock_worktree(worktree, LOCK_SH);
5211 if (unlockerr && err == NULL)
5212 err = unlockerr;
5213 return err;
5216 static void
5217 free_commitable(struct got_commitable *ct)
5219 free(ct->path);
5220 free(ct->in_repo_path);
5221 free(ct->ondisk_path);
5222 free(ct->blob_id);
5223 free(ct->base_blob_id);
5224 free(ct->staged_blob_id);
5225 free(ct->base_commit_id);
5226 free(ct);
5229 struct collect_commitables_arg {
5230 struct got_pathlist_head *commitable_paths;
5231 struct got_repository *repo;
5232 struct got_worktree *worktree;
5233 struct got_fileindex *fileindex;
5234 int have_staged_files;
5235 int allow_bad_symlinks;
5236 int diff_header_shown;
5237 int commit_conflicts;
5238 FILE *diff_outfile;
5239 FILE *f1;
5240 FILE *f2;
5244 * Create a file which contains the target path of a symlink so we can feed
5245 * it as content to the diff engine.
5247 static const struct got_error *
5248 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5249 const char *abspath)
5251 const struct got_error *err = NULL;
5252 char target_path[PATH_MAX];
5253 ssize_t target_len, outlen;
5255 *fd = -1;
5257 if (dirfd != -1) {
5258 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5259 if (target_len == -1)
5260 return got_error_from_errno2("readlinkat", abspath);
5261 } else {
5262 target_len = readlink(abspath, target_path, PATH_MAX);
5263 if (target_len == -1)
5264 return got_error_from_errno2("readlink", abspath);
5267 *fd = got_opentempfd();
5268 if (*fd == -1)
5269 return got_error_from_errno("got_opentempfd");
5271 outlen = write(*fd, target_path, target_len);
5272 if (outlen == -1) {
5273 err = got_error_from_errno("got_opentempfd");
5274 goto done;
5277 if (lseek(*fd, 0, SEEK_SET) == -1) {
5278 err = got_error_from_errno2("lseek", abspath);
5279 goto done;
5281 done:
5282 if (err) {
5283 close(*fd);
5284 *fd = -1;
5286 return err;
5289 static const struct got_error *
5290 append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5291 FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5292 int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5294 const struct got_error *err = NULL;
5295 struct got_blob_object *blob1 = NULL;
5296 int fd = -1, fd1 = -1, fd2 = -1;
5297 FILE *ondisk_file = NULL;
5298 char *label1 = NULL;
5299 struct stat sb;
5300 off_t size1 = 0;
5301 int f2_exists = 0;
5302 char *id_str = NULL;
5304 memset(&sb, 0, sizeof(sb));
5306 if (diff_staged) {
5307 if (ct->staged_status != GOT_STATUS_MODIFY &&
5308 ct->staged_status != GOT_STATUS_ADD &&
5309 ct->staged_status != GOT_STATUS_DELETE)
5310 return NULL;
5311 } else {
5312 if (ct->status != GOT_STATUS_MODIFY &&
5313 ct->status != GOT_STATUS_ADD &&
5314 ct->status != GOT_STATUS_DELETE &&
5315 ct->status != GOT_STATUS_CONFLICT)
5316 return NULL;
5319 err = got_opentemp_truncate(f1);
5320 if (err)
5321 return got_error_from_errno("got_opentemp_truncate");
5322 err = got_opentemp_truncate(f2);
5323 if (err)
5324 return got_error_from_errno("got_opentemp_truncate");
5326 if (!*diff_header_shown) {
5327 err = got_object_id_str(&id_str, worktree->base_commit_id);
5328 if (err)
5329 return err;
5330 fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5331 got_worktree_get_root_path(worktree));
5332 fprintf(diff_outfile, "commit - %s\n", id_str);
5333 fprintf(diff_outfile, "path + %s%s\n",
5334 got_worktree_get_root_path(worktree),
5335 diff_staged ? " (staged changes)" : "");
5336 *diff_header_shown = 1;
5339 if (diff_staged) {
5340 const char *label1 = NULL, *label2 = NULL;
5341 switch (ct->staged_status) {
5342 case GOT_STATUS_MODIFY:
5343 label1 = ct->path;
5344 label2 = ct->path;
5345 break;
5346 case GOT_STATUS_ADD:
5347 label2 = ct->path;
5348 break;
5349 case GOT_STATUS_DELETE:
5350 label1 = ct->path;
5351 break;
5352 default:
5353 return got_error(GOT_ERR_FILE_STATUS);
5355 fd1 = got_opentempfd();
5356 if (fd1 == -1) {
5357 err = got_error_from_errno("got_opentempfd");
5358 goto done;
5360 fd2 = got_opentempfd();
5361 if (fd2 == -1) {
5362 err = got_error_from_errno("got_opentempfd");
5363 goto done;
5365 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5366 fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5367 label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5368 NULL, repo, diff_outfile);
5369 goto done;
5372 fd1 = got_opentempfd();
5373 if (fd1 == -1) {
5374 err = got_error_from_errno("got_opentempfd");
5375 goto done;
5378 if (ct->status != GOT_STATUS_ADD) {
5379 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5380 8192, fd1);
5381 if (err)
5382 goto done;
5385 if (ct->status != GOT_STATUS_DELETE) {
5386 if (dirfd != -1) {
5387 fd = openat(dirfd, de_name,
5388 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5389 if (fd == -1) {
5390 if (!got_err_open_nofollow_on_symlink()) {
5391 err = got_error_from_errno2("openat",
5392 ct->ondisk_path);
5393 goto done;
5395 err = get_symlink_target_file(&fd, dirfd,
5396 de_name, ct->ondisk_path);
5397 if (err)
5398 goto done;
5400 } else {
5401 fd = open(ct->ondisk_path,
5402 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5403 if (fd == -1) {
5404 if (!got_err_open_nofollow_on_symlink()) {
5405 err = got_error_from_errno2("open",
5406 ct->ondisk_path);
5407 goto done;
5409 err = get_symlink_target_file(&fd, dirfd,
5410 de_name, ct->ondisk_path);
5411 if (err)
5412 goto done;
5415 if (fstatat(fd, ct->ondisk_path, &sb,
5416 AT_SYMLINK_NOFOLLOW) == -1) {
5417 err = got_error_from_errno2("fstatat", ct->ondisk_path);
5418 goto done;
5420 ondisk_file = fdopen(fd, "r");
5421 if (ondisk_file == NULL) {
5422 err = got_error_from_errno2("fdopen", ct->ondisk_path);
5423 goto done;
5425 fd = -1;
5426 f2_exists = 1;
5429 if (blob1) {
5430 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5431 f1, blob1);
5432 if (err)
5433 goto done;
5436 err = got_diff_blob_file(blob1, f1, size1, label1,
5437 ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5438 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5439 done:
5440 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5441 err = got_error_from_errno("close");
5442 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5443 err = got_error_from_errno("close");
5444 if (blob1)
5445 got_object_blob_close(blob1);
5446 if (fd != -1 && close(fd) == -1 && err == NULL)
5447 err = got_error_from_errno("close");
5448 if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5449 err = got_error_from_errno("fclose");
5450 return err;
5453 static const struct got_error *
5454 collect_commitables(void *arg, unsigned char status,
5455 unsigned char staged_status, const char *relpath,
5456 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5457 struct got_object_id *commit_id, int dirfd, const char *de_name)
5459 struct collect_commitables_arg *a = arg;
5460 const struct got_error *err = NULL;
5461 struct got_commitable *ct = NULL;
5462 struct got_pathlist_entry *new = NULL;
5463 char *parent_path = NULL, *path = NULL;
5464 struct stat sb;
5466 if (a->have_staged_files) {
5467 if (staged_status != GOT_STATUS_MODIFY &&
5468 staged_status != GOT_STATUS_ADD &&
5469 staged_status != GOT_STATUS_DELETE)
5470 return NULL;
5471 } else {
5472 if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5473 printf("C %s\n", relpath);
5474 return got_error(GOT_ERR_COMMIT_CONFLICT);
5477 if (status != GOT_STATUS_MODIFY &&
5478 status != GOT_STATUS_MODE_CHANGE &&
5479 status != GOT_STATUS_ADD &&
5480 status != GOT_STATUS_DELETE &&
5481 status != GOT_STATUS_CONFLICT)
5482 return NULL;
5485 if (asprintf(&path, "/%s", relpath) == -1) {
5486 err = got_error_from_errno("asprintf");
5487 goto done;
5489 if (strcmp(path, "/") == 0) {
5490 parent_path = strdup("");
5491 if (parent_path == NULL)
5492 return got_error_from_errno("strdup");
5493 } else {
5494 err = got_path_dirname(&parent_path, path);
5495 if (err)
5496 return err;
5499 ct = calloc(1, sizeof(*ct));
5500 if (ct == NULL) {
5501 err = got_error_from_errno("calloc");
5502 goto done;
5505 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5506 relpath) == -1) {
5507 err = got_error_from_errno("asprintf");
5508 goto done;
5511 if (staged_status == GOT_STATUS_ADD ||
5512 staged_status == GOT_STATUS_MODIFY) {
5513 struct got_fileindex_entry *ie;
5514 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5515 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5516 case GOT_FILEIDX_MODE_REGULAR_FILE:
5517 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5518 ct->mode = S_IFREG;
5519 break;
5520 case GOT_FILEIDX_MODE_SYMLINK:
5521 ct->mode = S_IFLNK;
5522 break;
5523 default:
5524 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5525 goto done;
5527 ct->mode |= got_fileindex_entry_perms_get(ie);
5528 } else if (status != GOT_STATUS_DELETE &&
5529 staged_status != GOT_STATUS_DELETE) {
5530 if (dirfd != -1) {
5531 if (fstatat(dirfd, de_name, &sb,
5532 AT_SYMLINK_NOFOLLOW) == -1) {
5533 err = got_error_from_errno2("fstatat",
5534 ct->ondisk_path);
5535 goto done;
5537 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5538 err = got_error_from_errno2("lstat", ct->ondisk_path);
5539 goto done;
5541 ct->mode = sb.st_mode;
5544 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5545 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5546 relpath) == -1) {
5547 err = got_error_from_errno("asprintf");
5548 goto done;
5551 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5552 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5553 int is_bad_symlink;
5554 char target_path[PATH_MAX];
5555 ssize_t target_len;
5556 target_len = readlink(ct->ondisk_path, target_path,
5557 sizeof(target_path));
5558 if (target_len == -1) {
5559 err = got_error_from_errno2("readlink",
5560 ct->ondisk_path);
5561 goto done;
5563 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5564 target_len, ct->ondisk_path, a->worktree->root_path);
5565 if (err)
5566 goto done;
5567 if (is_bad_symlink) {
5568 err = got_error_path(ct->ondisk_path,
5569 GOT_ERR_BAD_SYMLINK);
5570 goto done;
5575 ct->status = status;
5576 ct->staged_status = staged_status;
5577 ct->blob_id = NULL; /* will be filled in when blob gets created */
5578 if (ct->status != GOT_STATUS_ADD &&
5579 ct->staged_status != GOT_STATUS_ADD) {
5580 ct->base_blob_id = got_object_id_dup(blob_id);
5581 if (ct->base_blob_id == NULL) {
5582 err = got_error_from_errno("got_object_id_dup");
5583 goto done;
5585 ct->base_commit_id = got_object_id_dup(commit_id);
5586 if (ct->base_commit_id == NULL) {
5587 err = got_error_from_errno("got_object_id_dup");
5588 goto done;
5591 if (ct->staged_status == GOT_STATUS_ADD ||
5592 ct->staged_status == GOT_STATUS_MODIFY) {
5593 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5594 if (ct->staged_blob_id == NULL) {
5595 err = got_error_from_errno("got_object_id_dup");
5596 goto done;
5599 ct->path = strdup(path);
5600 if (ct->path == NULL) {
5601 err = got_error_from_errno("strdup");
5602 goto done;
5604 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5605 if (err)
5606 goto done;
5608 if (a->diff_outfile && ct && new != NULL) {
5609 err = append_ct_diff(ct, &a->diff_header_shown,
5610 a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5611 a->have_staged_files, a->repo, a->worktree);
5612 if (err)
5613 goto done;
5615 done:
5616 if (ct && (err || new == NULL))
5617 free_commitable(ct);
5618 free(parent_path);
5619 free(path);
5620 return err;
5623 static const struct got_error *write_tree(struct got_object_id **, int *,
5624 struct got_tree_object *, const char *, struct got_pathlist_head *,
5625 got_worktree_status_cb status_cb, void *status_arg,
5626 struct got_repository *);
5628 static const struct got_error *
5629 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5630 struct got_tree_entry *te, const char *parent_path,
5631 struct got_pathlist_head *commitable_paths,
5632 got_worktree_status_cb status_cb, void *status_arg,
5633 struct got_repository *repo)
5635 const struct got_error *err = NULL;
5636 struct got_tree_object *subtree;
5637 char *subpath;
5639 if (asprintf(&subpath, "%s%s%s", parent_path,
5640 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5641 return got_error_from_errno("asprintf");
5643 err = got_object_open_as_tree(&subtree, repo, &te->id);
5644 if (err)
5645 return err;
5647 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5648 commitable_paths, status_cb, status_arg, repo);
5649 got_object_tree_close(subtree);
5650 free(subpath);
5651 return err;
5654 static const struct got_error *
5655 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5657 const struct got_error *err = NULL;
5658 char *ct_parent_path = NULL;
5660 *match = 0;
5662 if (strchr(ct->in_repo_path, '/') == NULL) {
5663 *match = got_path_is_root_dir(path);
5664 return NULL;
5667 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5668 if (err)
5669 return err;
5670 *match = (strcmp(path, ct_parent_path) == 0);
5671 free(ct_parent_path);
5672 return err;
5675 static mode_t
5676 get_ct_file_mode(struct got_commitable *ct)
5678 if (S_ISLNK(ct->mode))
5679 return S_IFLNK;
5681 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5684 static const struct got_error *
5685 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5686 struct got_tree_entry *te, struct got_commitable *ct)
5688 const struct got_error *err = NULL;
5690 *new_te = NULL;
5692 err = got_object_tree_entry_dup(new_te, te);
5693 if (err)
5694 goto done;
5696 (*new_te)->mode = get_ct_file_mode(ct);
5698 if (ct->staged_status == GOT_STATUS_MODIFY)
5699 memcpy(&(*new_te)->id, ct->staged_blob_id,
5700 sizeof((*new_te)->id));
5701 else
5702 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5703 done:
5704 if (err && *new_te) {
5705 free(*new_te);
5706 *new_te = NULL;
5708 return err;
5711 static const struct got_error *
5712 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5713 struct got_commitable *ct)
5715 const struct got_error *err = NULL;
5716 char *ct_name = NULL;
5718 *new_te = NULL;
5720 *new_te = calloc(1, sizeof(**new_te));
5721 if (*new_te == NULL)
5722 return got_error_from_errno("calloc");
5724 err = got_path_basename(&ct_name, ct->path);
5725 if (err)
5726 goto done;
5727 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5728 sizeof((*new_te)->name)) {
5729 err = got_error(GOT_ERR_NO_SPACE);
5730 goto done;
5733 (*new_te)->mode = get_ct_file_mode(ct);
5735 if (ct->staged_status == GOT_STATUS_ADD)
5736 memcpy(&(*new_te)->id, ct->staged_blob_id,
5737 sizeof((*new_te)->id));
5738 else
5739 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5740 done:
5741 free(ct_name);
5742 if (err && *new_te) {
5743 free(*new_te);
5744 *new_te = NULL;
5746 return err;
5749 static const struct got_error *
5750 insert_tree_entry(struct got_tree_entry *new_te,
5751 struct got_pathlist_head *paths)
5753 const struct got_error *err = NULL;
5754 struct got_pathlist_entry *new_pe;
5756 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5757 if (err)
5758 return err;
5759 if (new_pe == NULL)
5760 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5761 return NULL;
5764 static const struct got_error *
5765 report_ct_status(struct got_commitable *ct,
5766 got_worktree_status_cb status_cb, void *status_arg)
5768 const char *ct_path = ct->path;
5769 unsigned char status;
5771 if (status_cb == NULL) /* no commit progress output desired */
5772 return NULL;
5774 while (ct_path[0] == '/')
5775 ct_path++;
5777 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5778 status = ct->staged_status;
5779 else
5780 status = ct->status;
5782 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5783 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5786 static const struct got_error *
5787 match_modified_subtree(int *modified, struct got_tree_entry *te,
5788 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5790 const struct got_error *err = NULL;
5791 struct got_pathlist_entry *pe;
5792 char *te_path;
5794 *modified = 0;
5796 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5797 got_path_is_root_dir(base_tree_path) ? "" : "/",
5798 te->name) == -1)
5799 return got_error_from_errno("asprintf");
5801 TAILQ_FOREACH(pe, commitable_paths, entry) {
5802 struct got_commitable *ct = pe->data;
5803 *modified = got_path_is_child(ct->in_repo_path, te_path,
5804 strlen(te_path));
5805 if (*modified)
5806 break;
5809 free(te_path);
5810 return err;
5813 static const struct got_error *
5814 match_deleted_or_modified_ct(struct got_commitable **ctp,
5815 struct got_tree_entry *te, const char *base_tree_path,
5816 struct got_pathlist_head *commitable_paths)
5818 const struct got_error *err = NULL;
5819 struct got_pathlist_entry *pe;
5821 *ctp = NULL;
5823 TAILQ_FOREACH(pe, commitable_paths, entry) {
5824 struct got_commitable *ct = pe->data;
5825 char *ct_name = NULL;
5826 int path_matches;
5828 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5829 if (ct->status != GOT_STATUS_MODIFY &&
5830 ct->status != GOT_STATUS_MODE_CHANGE &&
5831 ct->status != GOT_STATUS_DELETE &&
5832 ct->status != GOT_STATUS_CONFLICT)
5833 continue;
5834 } else {
5835 if (ct->staged_status != GOT_STATUS_MODIFY &&
5836 ct->staged_status != GOT_STATUS_DELETE)
5837 continue;
5840 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5841 continue;
5843 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5844 if (err)
5845 return err;
5846 if (!path_matches)
5847 continue;
5849 err = got_path_basename(&ct_name, pe->path);
5850 if (err)
5851 return err;
5853 if (strcmp(te->name, ct_name) != 0) {
5854 free(ct_name);
5855 continue;
5857 free(ct_name);
5859 *ctp = ct;
5860 break;
5863 return err;
5866 static const struct got_error *
5867 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5868 const char *child_path, const char *path_base_tree,
5869 struct got_pathlist_head *commitable_paths,
5870 got_worktree_status_cb status_cb, void *status_arg,
5871 struct got_repository *repo)
5873 const struct got_error *err = NULL;
5874 struct got_tree_entry *new_te;
5875 char *subtree_path;
5876 struct got_object_id *id = NULL;
5877 int nentries;
5879 *new_tep = NULL;
5881 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5882 got_path_is_root_dir(path_base_tree) ? "" : "/",
5883 child_path) == -1)
5884 return got_error_from_errno("asprintf");
5886 new_te = calloc(1, sizeof(*new_te));
5887 if (new_te == NULL)
5888 return got_error_from_errno("calloc");
5889 new_te->mode = S_IFDIR;
5891 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5892 sizeof(new_te->name)) {
5893 err = got_error(GOT_ERR_NO_SPACE);
5894 goto done;
5896 err = write_tree(&id, &nentries, NULL, subtree_path,
5897 commitable_paths, status_cb, status_arg, repo);
5898 if (err) {
5899 free(new_te);
5900 goto done;
5902 memcpy(&new_te->id, id, sizeof(new_te->id));
5903 done:
5904 free(id);
5905 free(subtree_path);
5906 if (err == NULL)
5907 *new_tep = new_te;
5908 return err;
5911 static const struct got_error *
5912 write_tree(struct got_object_id **new_tree_id, int *nentries,
5913 struct got_tree_object *base_tree, const char *path_base_tree,
5914 struct got_pathlist_head *commitable_paths,
5915 got_worktree_status_cb status_cb, void *status_arg,
5916 struct got_repository *repo)
5918 const struct got_error *err = NULL;
5919 struct got_pathlist_head paths;
5920 struct got_tree_entry *te, *new_te = NULL;
5921 struct got_pathlist_entry *pe;
5923 TAILQ_INIT(&paths);
5924 *nentries = 0;
5926 /* Insert, and recurse into, newly added entries first. */
5927 TAILQ_FOREACH(pe, commitable_paths, entry) {
5928 struct got_commitable *ct = pe->data;
5929 char *child_path = NULL, *slash;
5931 if ((ct->status != GOT_STATUS_ADD &&
5932 ct->staged_status != GOT_STATUS_ADD) ||
5933 (ct->flags & GOT_COMMITABLE_ADDED))
5934 continue;
5936 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5937 strlen(path_base_tree)))
5938 continue;
5940 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5941 ct->in_repo_path);
5942 if (err)
5943 goto done;
5945 slash = strchr(child_path, '/');
5946 if (slash == NULL) {
5947 err = alloc_added_blob_tree_entry(&new_te, ct);
5948 if (err)
5949 goto done;
5950 err = report_ct_status(ct, status_cb, status_arg);
5951 if (err)
5952 goto done;
5953 ct->flags |= GOT_COMMITABLE_ADDED;
5954 err = insert_tree_entry(new_te, &paths);
5955 if (err)
5956 goto done;
5957 (*nentries)++;
5958 } else {
5959 *slash = '\0'; /* trim trailing path components */
5960 if (base_tree == NULL ||
5961 got_object_tree_find_entry(base_tree, child_path)
5962 == NULL) {
5963 err = make_subtree_for_added_blob(&new_te,
5964 child_path, path_base_tree,
5965 commitable_paths, status_cb, status_arg,
5966 repo);
5967 if (err)
5968 goto done;
5969 err = insert_tree_entry(new_te, &paths);
5970 if (err)
5971 goto done;
5972 (*nentries)++;
5977 if (base_tree) {
5978 int i, nbase_entries;
5979 /* Handle modified and deleted entries. */
5980 nbase_entries = got_object_tree_get_nentries(base_tree);
5981 for (i = 0; i < nbase_entries; i++) {
5982 struct got_commitable *ct = NULL;
5984 te = got_object_tree_get_entry(base_tree, i);
5985 if (got_object_tree_entry_is_submodule(te)) {
5986 /* Entry is a submodule; just copy it. */
5987 err = got_object_tree_entry_dup(&new_te, te);
5988 if (err)
5989 goto done;
5990 err = insert_tree_entry(new_te, &paths);
5991 if (err)
5992 goto done;
5993 (*nentries)++;
5994 continue;
5997 if (S_ISDIR(te->mode)) {
5998 int modified;
5999 err = got_object_tree_entry_dup(&new_te, te);
6000 if (err)
6001 goto done;
6002 err = match_modified_subtree(&modified, te,
6003 path_base_tree, commitable_paths);
6004 if (err)
6005 goto done;
6006 /* Avoid recursion into unmodified subtrees. */
6007 if (modified) {
6008 struct got_object_id *new_id;
6009 int nsubentries;
6010 err = write_subtree(&new_id,
6011 &nsubentries, te,
6012 path_base_tree, commitable_paths,
6013 status_cb, status_arg, repo);
6014 if (err)
6015 goto done;
6016 if (nsubentries == 0) {
6017 /* All entries were deleted. */
6018 free(new_id);
6019 continue;
6021 memcpy(&new_te->id, new_id,
6022 sizeof(new_te->id));
6023 free(new_id);
6025 err = insert_tree_entry(new_te, &paths);
6026 if (err)
6027 goto done;
6028 (*nentries)++;
6029 continue;
6032 err = match_deleted_or_modified_ct(&ct, te,
6033 path_base_tree, commitable_paths);
6034 if (err)
6035 goto done;
6036 if (ct) {
6037 /* NB: Deleted entries get dropped here. */
6038 if (ct->status == GOT_STATUS_MODIFY ||
6039 ct->status == GOT_STATUS_MODE_CHANGE ||
6040 ct->status == GOT_STATUS_CONFLICT ||
6041 ct->staged_status == GOT_STATUS_MODIFY) {
6042 err = alloc_modified_blob_tree_entry(
6043 &new_te, te, ct);
6044 if (err)
6045 goto done;
6046 err = insert_tree_entry(new_te, &paths);
6047 if (err)
6048 goto done;
6049 (*nentries)++;
6051 err = report_ct_status(ct, status_cb,
6052 status_arg);
6053 if (err)
6054 goto done;
6055 } else {
6056 /* Entry is unchanged; just copy it. */
6057 err = got_object_tree_entry_dup(&new_te, te);
6058 if (err)
6059 goto done;
6060 err = insert_tree_entry(new_te, &paths);
6061 if (err)
6062 goto done;
6063 (*nentries)++;
6068 /* Write new list of entries; deleted entries have been dropped. */
6069 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
6070 done:
6071 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
6072 return err;
6075 static const struct got_error *
6076 update_fileindex_after_commit(struct got_worktree *worktree,
6077 struct got_pathlist_head *commitable_paths,
6078 struct got_object_id *new_base_commit_id,
6079 struct got_fileindex *fileindex, int have_staged_files)
6081 const struct got_error *err = NULL;
6082 struct got_pathlist_entry *pe;
6083 char *relpath = NULL;
6085 TAILQ_FOREACH(pe, commitable_paths, entry) {
6086 struct got_fileindex_entry *ie;
6087 struct got_commitable *ct = pe->data;
6089 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6091 err = got_path_skip_common_ancestor(&relpath,
6092 worktree->root_path, ct->ondisk_path);
6093 if (err)
6094 goto done;
6096 if (ie) {
6097 if (ct->status == GOT_STATUS_DELETE ||
6098 ct->staged_status == GOT_STATUS_DELETE) {
6099 got_fileindex_entry_remove(fileindex, ie);
6100 } else if (ct->staged_status == GOT_STATUS_ADD ||
6101 ct->staged_status == GOT_STATUS_MODIFY) {
6102 got_fileindex_entry_stage_set(ie,
6103 GOT_FILEIDX_STAGE_NONE);
6104 got_fileindex_entry_staged_filetype_set(ie, 0);
6106 err = got_fileindex_entry_update(ie,
6107 worktree->root_fd, relpath,
6108 ct->staged_blob_id->sha1,
6109 new_base_commit_id->sha1,
6110 !have_staged_files);
6111 } else
6112 err = got_fileindex_entry_update(ie,
6113 worktree->root_fd, relpath,
6114 ct->blob_id->sha1,
6115 new_base_commit_id->sha1,
6116 !have_staged_files);
6117 } else {
6118 err = got_fileindex_entry_alloc(&ie, pe->path);
6119 if (err)
6120 goto done;
6121 err = got_fileindex_entry_update(ie,
6122 worktree->root_fd, relpath, ct->blob_id->sha1,
6123 new_base_commit_id->sha1, 1);
6124 if (err) {
6125 got_fileindex_entry_free(ie);
6126 goto done;
6128 err = got_fileindex_entry_add(fileindex, ie);
6129 if (err) {
6130 got_fileindex_entry_free(ie);
6131 goto done;
6134 free(relpath);
6135 relpath = NULL;
6137 done:
6138 free(relpath);
6139 return err;
6143 static const struct got_error *
6144 check_out_of_date(const char *in_repo_path, unsigned char status,
6145 unsigned char staged_status, struct got_object_id *base_blob_id,
6146 struct got_object_id *base_commit_id,
6147 struct got_object_id *head_commit_id, struct got_repository *repo,
6148 int ood_errcode)
6150 const struct got_error *err = NULL;
6151 struct got_commit_object *commit = NULL;
6152 struct got_object_id *id = NULL;
6154 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
6155 /* Trivial case: base commit == head commit */
6156 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
6157 return NULL;
6159 * Ensure file content which local changes were based
6160 * on matches file content in the branch head.
6162 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6163 if (err)
6164 goto done;
6165 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6166 if (err) {
6167 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6168 err = got_error(ood_errcode);
6169 goto done;
6170 } else if (got_object_id_cmp(id, base_blob_id) != 0)
6171 err = got_error(ood_errcode);
6172 } else {
6173 /* Require that added files don't exist in the branch head. */
6174 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6175 if (err)
6176 goto done;
6177 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6178 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
6179 goto done;
6180 err = id ? got_error(ood_errcode) : NULL;
6182 done:
6183 free(id);
6184 if (commit)
6185 got_object_commit_close(commit);
6186 return err;
6189 static const struct got_error *
6190 commit_worktree(struct got_object_id **new_commit_id,
6191 struct got_pathlist_head *commitable_paths,
6192 struct got_object_id *head_commit_id,
6193 struct got_object_id *parent_id2,
6194 struct got_worktree *worktree,
6195 const char *author, const char *committer, char *diff_path,
6196 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6197 got_worktree_status_cb status_cb, void *status_arg,
6198 struct got_repository *repo)
6200 const struct got_error *err = NULL, *unlockerr = NULL;
6201 struct got_pathlist_entry *pe;
6202 const char *head_ref_name = NULL;
6203 struct got_commit_object *head_commit = NULL;
6204 struct got_reference *head_ref2 = NULL;
6205 struct got_object_id *head_commit_id2 = NULL;
6206 struct got_tree_object *head_tree = NULL;
6207 struct got_object_id *new_tree_id = NULL;
6208 int nentries, nparents = 0;
6209 struct got_object_id_queue parent_ids;
6210 struct got_object_qid *pid = NULL;
6211 char *logmsg = NULL;
6212 time_t timestamp;
6214 *new_commit_id = NULL;
6216 STAILQ_INIT(&parent_ids);
6218 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6219 if (err)
6220 goto done;
6222 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6223 if (err)
6224 goto done;
6226 if (commit_msg_cb != NULL) {
6227 err = commit_msg_cb(commitable_paths, diff_path,
6228 &logmsg, commit_arg);
6229 if (err)
6230 goto done;
6233 if (logmsg == NULL || strlen(logmsg) == 0) {
6234 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6235 goto done;
6238 /* Create blobs from added and modified files and record their IDs. */
6239 TAILQ_FOREACH(pe, commitable_paths, entry) {
6240 struct got_commitable *ct = pe->data;
6241 char *ondisk_path;
6243 /* Blobs for staged files already exist. */
6244 if (ct->staged_status == GOT_STATUS_ADD ||
6245 ct->staged_status == GOT_STATUS_MODIFY)
6246 continue;
6248 if (ct->status != GOT_STATUS_ADD &&
6249 ct->status != GOT_STATUS_MODIFY &&
6250 ct->status != GOT_STATUS_MODE_CHANGE &&
6251 ct->status != GOT_STATUS_CONFLICT)
6252 continue;
6254 if (asprintf(&ondisk_path, "%s/%s",
6255 worktree->root_path, pe->path) == -1) {
6256 err = got_error_from_errno("asprintf");
6257 goto done;
6259 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6260 free(ondisk_path);
6261 if (err)
6262 goto done;
6265 /* Recursively write new tree objects. */
6266 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6267 commitable_paths, status_cb, status_arg, repo);
6268 if (err)
6269 goto done;
6271 err = got_object_qid_alloc(&pid, head_commit_id);
6272 if (err)
6273 goto done;
6274 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6275 nparents++;
6276 if (parent_id2) {
6277 err = got_object_qid_alloc(&pid, parent_id2);
6278 if (err)
6279 goto done;
6280 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6281 nparents++;
6283 timestamp = time(NULL);
6284 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6285 nparents, author, timestamp, committer, timestamp, logmsg, repo);
6286 if (logmsg != NULL)
6287 free(logmsg);
6288 if (err)
6289 goto done;
6291 /* Check if a concurrent commit to our branch has occurred. */
6292 head_ref_name = got_worktree_get_head_ref_name(worktree);
6293 if (head_ref_name == NULL) {
6294 err = got_error_from_errno("got_worktree_get_head_ref_name");
6295 goto done;
6297 /* Lock the reference here to prevent concurrent modification. */
6298 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6299 if (err)
6300 goto done;
6301 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6302 if (err)
6303 goto done;
6304 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6305 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6306 goto done;
6308 /* Update branch head in repository. */
6309 err = got_ref_change_ref(head_ref2, *new_commit_id);
6310 if (err)
6311 goto done;
6312 err = got_ref_write(head_ref2, repo);
6313 if (err)
6314 goto done;
6316 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6317 if (err)
6318 goto done;
6320 err = ref_base_commit(worktree, repo);
6321 if (err)
6322 goto done;
6323 done:
6324 got_object_id_queue_free(&parent_ids);
6325 if (head_tree)
6326 got_object_tree_close(head_tree);
6327 if (head_commit)
6328 got_object_commit_close(head_commit);
6329 free(head_commit_id2);
6330 if (head_ref2) {
6331 unlockerr = got_ref_unlock(head_ref2);
6332 if (unlockerr && err == NULL)
6333 err = unlockerr;
6334 got_ref_close(head_ref2);
6336 return err;
6339 static const struct got_error *
6340 check_path_is_commitable(const char *path,
6341 struct got_pathlist_head *commitable_paths)
6343 struct got_pathlist_entry *cpe = NULL;
6344 size_t path_len = strlen(path);
6346 TAILQ_FOREACH(cpe, commitable_paths, entry) {
6347 struct got_commitable *ct = cpe->data;
6348 const char *ct_path = ct->path;
6350 while (ct_path[0] == '/')
6351 ct_path++;
6353 if (strcmp(path, ct_path) == 0 ||
6354 got_path_is_child(ct_path, path, path_len))
6355 break;
6358 if (cpe == NULL)
6359 return got_error_path(path, GOT_ERR_BAD_PATH);
6361 return NULL;
6364 static const struct got_error *
6365 check_staged_file(void *arg, struct got_fileindex_entry *ie)
6367 int *have_staged_files = arg;
6369 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6370 *have_staged_files = 1;
6371 return got_error(GOT_ERR_CANCELLED);
6374 return NULL;
6377 static const struct got_error *
6378 check_non_staged_files(struct got_fileindex *fileindex,
6379 struct got_pathlist_head *paths)
6381 struct got_pathlist_entry *pe;
6382 struct got_fileindex_entry *ie;
6384 TAILQ_FOREACH(pe, paths, entry) {
6385 if (pe->path[0] == '\0')
6386 continue;
6387 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6388 if (ie == NULL)
6389 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6390 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6391 return got_error_path(pe->path,
6392 GOT_ERR_FILE_NOT_STAGED);
6395 return NULL;
6398 const struct got_error *
6399 got_worktree_commit(struct got_object_id **new_commit_id,
6400 struct got_worktree *worktree, struct got_pathlist_head *paths,
6401 const char *author, const char *committer, int allow_bad_symlinks,
6402 int show_diff, int commit_conflicts,
6403 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6404 got_worktree_status_cb status_cb, void *status_arg,
6405 struct got_repository *repo)
6407 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6408 struct got_fileindex *fileindex = NULL;
6409 char *fileindex_path = NULL;
6410 struct got_pathlist_head commitable_paths;
6411 struct collect_commitables_arg cc_arg;
6412 struct got_pathlist_entry *pe;
6413 struct got_reference *head_ref = NULL;
6414 struct got_object_id *head_commit_id = NULL;
6415 char *diff_path = NULL;
6416 int have_staged_files = 0;
6418 *new_commit_id = NULL;
6420 memset(&cc_arg, 0, sizeof(cc_arg));
6421 TAILQ_INIT(&commitable_paths);
6423 err = lock_worktree(worktree, LOCK_EX);
6424 if (err)
6425 goto done;
6427 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6428 if (err)
6429 goto done;
6431 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6432 if (err)
6433 goto done;
6435 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6436 if (err)
6437 goto done;
6439 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6440 &have_staged_files);
6441 if (err && err->code != GOT_ERR_CANCELLED)
6442 goto done;
6443 if (have_staged_files) {
6444 err = check_non_staged_files(fileindex, paths);
6445 if (err)
6446 goto done;
6449 cc_arg.commitable_paths = &commitable_paths;
6450 cc_arg.worktree = worktree;
6451 cc_arg.fileindex = fileindex;
6452 cc_arg.repo = repo;
6453 cc_arg.have_staged_files = have_staged_files;
6454 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6455 cc_arg.diff_header_shown = 0;
6456 cc_arg.commit_conflicts = commit_conflicts;
6457 if (show_diff) {
6458 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6459 GOT_TMPDIR_STR "/got", ".diff");
6460 if (err)
6461 goto done;
6462 cc_arg.f1 = got_opentemp();
6463 if (cc_arg.f1 == NULL) {
6464 err = got_error_from_errno("got_opentemp");
6465 goto done;
6467 cc_arg.f2 = got_opentemp();
6468 if (cc_arg.f2 == NULL) {
6469 err = got_error_from_errno("got_opentemp");
6470 goto done;
6474 TAILQ_FOREACH(pe, paths, entry) {
6475 err = worktree_status(worktree, pe->path, fileindex, repo,
6476 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6477 if (err)
6478 goto done;
6481 if (show_diff) {
6482 if (fflush(cc_arg.diff_outfile) == EOF) {
6483 err = got_error_from_errno("fflush");
6484 goto done;
6488 if (TAILQ_EMPTY(&commitable_paths)) {
6489 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6490 goto done;
6493 TAILQ_FOREACH(pe, paths, entry) {
6494 err = check_path_is_commitable(pe->path, &commitable_paths);
6495 if (err)
6496 goto done;
6499 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6500 struct got_commitable *ct = pe->data;
6501 const char *ct_path = ct->in_repo_path;
6503 while (ct_path[0] == '/')
6504 ct_path++;
6505 err = check_out_of_date(ct_path, ct->status,
6506 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6507 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6508 if (err)
6509 goto done;
6513 err = commit_worktree(new_commit_id, &commitable_paths,
6514 head_commit_id, NULL, worktree, author, committer,
6515 (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6516 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6517 if (err)
6518 goto done;
6520 err = update_fileindex_after_commit(worktree, &commitable_paths,
6521 *new_commit_id, fileindex, have_staged_files);
6522 sync_err = sync_fileindex(fileindex, fileindex_path);
6523 if (sync_err && err == NULL)
6524 err = sync_err;
6525 done:
6526 if (fileindex)
6527 got_fileindex_free(fileindex);
6528 free(fileindex_path);
6529 unlockerr = lock_worktree(worktree, LOCK_SH);
6530 if (unlockerr && err == NULL)
6531 err = unlockerr;
6532 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6533 struct got_commitable *ct = pe->data;
6535 free_commitable(ct);
6537 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6538 if (diff_path && unlink(diff_path) == -1 && err == NULL)
6539 err = got_error_from_errno2("unlink", diff_path);
6540 free(diff_path);
6541 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6542 err == NULL)
6543 err = got_error_from_errno("fclose");
6544 return err;
6547 const char *
6548 got_commitable_get_path(struct got_commitable *ct)
6550 return ct->path;
6553 unsigned int
6554 got_commitable_get_status(struct got_commitable *ct)
6556 return ct->status;
6559 struct check_rebase_ok_arg {
6560 struct got_worktree *worktree;
6561 struct got_repository *repo;
6564 static const struct got_error *
6565 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6567 const struct got_error *err = NULL;
6568 struct check_rebase_ok_arg *a = arg;
6569 unsigned char status;
6570 struct stat sb;
6571 char *ondisk_path;
6573 /* Reject rebase of a work tree with mixed base commits. */
6574 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6575 SHA1_DIGEST_LENGTH))
6576 return got_error(GOT_ERR_MIXED_COMMITS);
6578 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6579 == -1)
6580 return got_error_from_errno("asprintf");
6582 /* Reject rebase of a work tree with modified or staged files. */
6583 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6584 free(ondisk_path);
6585 if (err)
6586 return err;
6588 if (status != GOT_STATUS_NO_CHANGE)
6589 return got_error(GOT_ERR_MODIFIED);
6590 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6591 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6593 return NULL;
6596 const struct got_error *
6597 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6598 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6599 struct got_worktree *worktree, struct got_reference *branch,
6600 struct got_repository *repo)
6602 const struct got_error *err = NULL;
6603 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6604 char *branch_ref_name = NULL;
6605 char *fileindex_path = NULL;
6606 struct check_rebase_ok_arg ok_arg;
6607 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6608 struct got_object_id *wt_branch_tip = NULL;
6610 *new_base_branch_ref = NULL;
6611 *tmp_branch = NULL;
6612 *fileindex = NULL;
6614 err = lock_worktree(worktree, LOCK_EX);
6615 if (err)
6616 return err;
6618 err = open_fileindex(fileindex, &fileindex_path, worktree);
6619 if (err)
6620 goto done;
6622 ok_arg.worktree = worktree;
6623 ok_arg.repo = repo;
6624 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6625 &ok_arg);
6626 if (err)
6627 goto done;
6629 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6630 if (err)
6631 goto done;
6633 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6634 if (err)
6635 goto done;
6637 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6638 if (err)
6639 goto done;
6641 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6642 0);
6643 if (err)
6644 goto done;
6646 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6647 if (err)
6648 goto done;
6649 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6650 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6651 goto done;
6654 err = got_ref_alloc_symref(new_base_branch_ref,
6655 new_base_branch_ref_name, wt_branch);
6656 if (err)
6657 goto done;
6658 err = got_ref_write(*new_base_branch_ref, repo);
6659 if (err)
6660 goto done;
6662 /* TODO Lock original branch's ref while rebasing? */
6664 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6665 if (err)
6666 goto done;
6668 err = got_ref_write(branch_ref, repo);
6669 if (err)
6670 goto done;
6672 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6673 worktree->base_commit_id);
6674 if (err)
6675 goto done;
6676 err = got_ref_write(*tmp_branch, repo);
6677 if (err)
6678 goto done;
6680 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6681 if (err)
6682 goto done;
6683 done:
6684 free(fileindex_path);
6685 free(tmp_branch_name);
6686 free(new_base_branch_ref_name);
6687 free(branch_ref_name);
6688 if (branch_ref)
6689 got_ref_close(branch_ref);
6690 if (wt_branch)
6691 got_ref_close(wt_branch);
6692 free(wt_branch_tip);
6693 if (err) {
6694 if (*new_base_branch_ref) {
6695 got_ref_close(*new_base_branch_ref);
6696 *new_base_branch_ref = NULL;
6698 if (*tmp_branch) {
6699 got_ref_close(*tmp_branch);
6700 *tmp_branch = NULL;
6702 if (*fileindex) {
6703 got_fileindex_free(*fileindex);
6704 *fileindex = NULL;
6706 lock_worktree(worktree, LOCK_SH);
6708 return err;
6711 const struct got_error *
6712 got_worktree_rebase_continue(struct got_object_id **commit_id,
6713 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6714 struct got_reference **branch, struct got_fileindex **fileindex,
6715 struct got_worktree *worktree, struct got_repository *repo)
6717 const struct got_error *err;
6718 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6719 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6720 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6721 char *fileindex_path = NULL;
6722 int have_staged_files = 0;
6724 *commit_id = NULL;
6725 *new_base_branch = NULL;
6726 *tmp_branch = NULL;
6727 *branch = NULL;
6728 *fileindex = NULL;
6730 err = lock_worktree(worktree, LOCK_EX);
6731 if (err)
6732 return err;
6734 err = open_fileindex(fileindex, &fileindex_path, worktree);
6735 if (err)
6736 goto done;
6738 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6739 &have_staged_files);
6740 if (err && err->code != GOT_ERR_CANCELLED)
6741 goto done;
6742 if (have_staged_files) {
6743 err = got_error(GOT_ERR_STAGED_PATHS);
6744 goto done;
6747 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6748 if (err)
6749 goto done;
6751 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6752 if (err)
6753 goto done;
6755 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6756 if (err)
6757 goto done;
6759 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6760 if (err)
6761 goto done;
6763 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6764 if (err)
6765 goto done;
6767 err = got_ref_open(branch, repo,
6768 got_ref_get_symref_target(branch_ref), 0);
6769 if (err)
6770 goto done;
6772 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6773 if (err)
6774 goto done;
6776 err = got_ref_resolve(commit_id, repo, commit_ref);
6777 if (err)
6778 goto done;
6780 err = got_ref_open(new_base_branch, repo,
6781 new_base_branch_ref_name, 0);
6782 if (err)
6783 goto done;
6785 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6786 if (err)
6787 goto done;
6788 done:
6789 free(commit_ref_name);
6790 free(branch_ref_name);
6791 free(fileindex_path);
6792 if (commit_ref)
6793 got_ref_close(commit_ref);
6794 if (branch_ref)
6795 got_ref_close(branch_ref);
6796 if (err) {
6797 free(*commit_id);
6798 *commit_id = NULL;
6799 if (*tmp_branch) {
6800 got_ref_close(*tmp_branch);
6801 *tmp_branch = NULL;
6803 if (*new_base_branch) {
6804 got_ref_close(*new_base_branch);
6805 *new_base_branch = NULL;
6807 if (*branch) {
6808 got_ref_close(*branch);
6809 *branch = NULL;
6811 if (*fileindex) {
6812 got_fileindex_free(*fileindex);
6813 *fileindex = NULL;
6815 lock_worktree(worktree, LOCK_SH);
6817 return err;
6820 const struct got_error *
6821 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6823 const struct got_error *err;
6824 char *tmp_branch_name = NULL;
6826 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6827 if (err)
6828 return err;
6830 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6831 free(tmp_branch_name);
6832 return NULL;
6835 static const struct got_error *
6836 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6837 const char *diff_path, char **logmsg, void *arg)
6839 *logmsg = arg;
6840 return NULL;
6843 static const struct got_error *
6844 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6845 const char *path, struct got_object_id *blob_id,
6846 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6847 int dirfd, const char *de_name)
6849 return NULL;
6852 struct collect_merged_paths_arg {
6853 got_worktree_checkout_cb progress_cb;
6854 void *progress_arg;
6855 struct got_pathlist_head *merged_paths;
6858 static const struct got_error *
6859 collect_merged_paths(void *arg, unsigned char status, const char *path)
6861 const struct got_error *err;
6862 struct collect_merged_paths_arg *a = arg;
6863 char *p;
6864 struct got_pathlist_entry *new;
6866 err = (*a->progress_cb)(a->progress_arg, status, path);
6867 if (err)
6868 return err;
6870 if (status != GOT_STATUS_MERGE &&
6871 status != GOT_STATUS_ADD &&
6872 status != GOT_STATUS_DELETE &&
6873 status != GOT_STATUS_CONFLICT)
6874 return NULL;
6876 p = strdup(path);
6877 if (p == NULL)
6878 return got_error_from_errno("strdup");
6880 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6881 if (err || new == NULL)
6882 free(p);
6883 return err;
6886 static const struct got_error *
6887 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6888 int is_rebase, struct got_repository *repo)
6890 const struct got_error *err;
6891 struct got_reference *commit_ref = NULL;
6893 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6894 if (err) {
6895 if (err->code != GOT_ERR_NOT_REF)
6896 goto done;
6897 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6898 if (err)
6899 goto done;
6900 err = got_ref_write(commit_ref, repo);
6901 if (err)
6902 goto done;
6903 } else if (is_rebase) {
6904 struct got_object_id *stored_id;
6905 int cmp;
6907 err = got_ref_resolve(&stored_id, repo, commit_ref);
6908 if (err)
6909 goto done;
6910 cmp = got_object_id_cmp(commit_id, stored_id);
6911 free(stored_id);
6912 if (cmp != 0) {
6913 err = got_error(GOT_ERR_REBASE_COMMITID);
6914 goto done;
6917 done:
6918 if (commit_ref)
6919 got_ref_close(commit_ref);
6920 return err;
6923 static const struct got_error *
6924 rebase_merge_files(struct got_pathlist_head *merged_paths,
6925 const char *commit_ref_name, struct got_worktree *worktree,
6926 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6927 struct got_object_id *commit_id, struct got_repository *repo,
6928 got_worktree_checkout_cb progress_cb, void *progress_arg,
6929 got_cancel_cb cancel_cb, void *cancel_arg)
6931 const struct got_error *err;
6932 struct got_reference *commit_ref = NULL;
6933 struct collect_merged_paths_arg cmp_arg;
6934 char *fileindex_path;
6936 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6938 err = get_fileindex_path(&fileindex_path, worktree);
6939 if (err)
6940 return err;
6942 cmp_arg.progress_cb = progress_cb;
6943 cmp_arg.progress_arg = progress_arg;
6944 cmp_arg.merged_paths = merged_paths;
6945 err = merge_files(worktree, fileindex, fileindex_path,
6946 parent_commit_id, commit_id, repo, collect_merged_paths,
6947 &cmp_arg, cancel_cb, cancel_arg);
6948 if (commit_ref)
6949 got_ref_close(commit_ref);
6950 return err;
6953 const struct got_error *
6954 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6955 struct got_worktree *worktree, struct got_fileindex *fileindex,
6956 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6957 struct got_repository *repo,
6958 got_worktree_checkout_cb progress_cb, void *progress_arg,
6959 got_cancel_cb cancel_cb, void *cancel_arg)
6961 const struct got_error *err;
6962 char *commit_ref_name;
6964 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6965 if (err)
6966 return err;
6968 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6969 if (err)
6970 goto done;
6972 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6973 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6974 progress_arg, cancel_cb, cancel_arg);
6975 done:
6976 free(commit_ref_name);
6977 return err;
6980 const struct got_error *
6981 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6982 struct got_worktree *worktree, struct got_fileindex *fileindex,
6983 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6984 struct got_repository *repo,
6985 got_worktree_checkout_cb progress_cb, void *progress_arg,
6986 got_cancel_cb cancel_cb, void *cancel_arg)
6988 const struct got_error *err;
6989 char *commit_ref_name;
6991 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6992 if (err)
6993 return err;
6995 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6996 if (err)
6997 goto done;
6999 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
7000 fileindex, parent_commit_id, commit_id, repo, progress_cb,
7001 progress_arg, cancel_cb, cancel_arg);
7002 done:
7003 free(commit_ref_name);
7004 return err;
7007 static const struct got_error *
7008 rebase_commit(struct got_object_id **new_commit_id,
7009 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
7010 struct got_worktree *worktree, struct got_fileindex *fileindex,
7011 struct got_reference *tmp_branch, const char *committer,
7012 struct got_commit_object *orig_commit, const char *new_logmsg,
7013 int allow_conflict, struct got_repository *repo)
7015 const struct got_error *err, *sync_err;
7016 struct got_pathlist_head commitable_paths;
7017 struct collect_commitables_arg cc_arg;
7018 char *fileindex_path = NULL;
7019 struct got_reference *head_ref = NULL;
7020 struct got_object_id *head_commit_id = NULL;
7021 char *logmsg = NULL;
7023 memset(&cc_arg, 0, sizeof(cc_arg));
7024 TAILQ_INIT(&commitable_paths);
7025 *new_commit_id = NULL;
7027 /* Work tree is locked/unlocked during rebase preparation/teardown. */
7029 err = get_fileindex_path(&fileindex_path, worktree);
7030 if (err)
7031 return err;
7033 cc_arg.commitable_paths = &commitable_paths;
7034 cc_arg.worktree = worktree;
7035 cc_arg.repo = repo;
7036 cc_arg.have_staged_files = 0;
7037 cc_arg.commit_conflicts = allow_conflict;
7039 * If possible get the status of individual files directly to
7040 * avoid crawling the entire work tree once per rebased commit.
7042 * Ideally, merged_paths would contain a list of commitables
7043 * we could use so we could skip worktree_status() entirely.
7044 * However, we would then need carefully keep track of cumulative
7045 * effects of operations such as file additions and deletions
7046 * in 'got histedit -f' (folding multiple commits into one),
7047 * and this extra complexity is not really worth it.
7049 if (merged_paths) {
7050 struct got_pathlist_entry *pe;
7051 TAILQ_FOREACH(pe, merged_paths, entry) {
7052 err = worktree_status(worktree, pe->path, fileindex,
7053 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
7054 0);
7055 if (err)
7056 goto done;
7058 } else {
7059 err = worktree_status(worktree, "", fileindex, repo,
7060 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7061 if (err)
7062 goto done;
7065 if (TAILQ_EMPTY(&commitable_paths)) {
7066 /* No-op change; commit will be elided. */
7067 err = got_ref_delete(commit_ref, repo);
7068 if (err)
7069 goto done;
7070 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
7071 goto done;
7074 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7075 if (err)
7076 goto done;
7078 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7079 if (err)
7080 goto done;
7082 if (new_logmsg) {
7083 logmsg = strdup(new_logmsg);
7084 if (logmsg == NULL) {
7085 err = got_error_from_errno("strdup");
7086 goto done;
7088 } else {
7089 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
7090 if (err)
7091 goto done;
7094 /* NB: commit_worktree will call free(logmsg) */
7095 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
7096 NULL, worktree, got_object_commit_get_author(orig_commit),
7097 committer ? committer :
7098 got_object_commit_get_committer(orig_commit), NULL,
7099 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
7100 if (err)
7101 goto done;
7103 err = got_ref_change_ref(tmp_branch, *new_commit_id);
7104 if (err)
7105 goto done;
7107 err = got_ref_delete(commit_ref, repo);
7108 if (err)
7109 goto done;
7111 err = update_fileindex_after_commit(worktree, &commitable_paths,
7112 *new_commit_id, fileindex, 0);
7113 sync_err = sync_fileindex(fileindex, fileindex_path);
7114 if (sync_err && err == NULL)
7115 err = sync_err;
7116 done:
7117 free(fileindex_path);
7118 free(head_commit_id);
7119 if (head_ref)
7120 got_ref_close(head_ref);
7121 if (err) {
7122 free(*new_commit_id);
7123 *new_commit_id = NULL;
7125 return err;
7128 const struct got_error *
7129 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
7130 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7131 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7132 const char *committer, struct got_commit_object *orig_commit,
7133 struct got_object_id *orig_commit_id, int allow_conflict,
7134 struct got_repository *repo)
7136 const struct got_error *err;
7137 char *commit_ref_name;
7138 struct got_reference *commit_ref = NULL;
7139 struct got_object_id *commit_id = NULL;
7141 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7142 if (err)
7143 return err;
7145 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7146 if (err)
7147 goto done;
7148 err = got_ref_resolve(&commit_id, repo, commit_ref);
7149 if (err)
7150 goto done;
7151 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
7152 err = got_error(GOT_ERR_REBASE_COMMITID);
7153 goto done;
7156 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7157 worktree, fileindex, tmp_branch, committer, orig_commit,
7158 NULL, allow_conflict, repo);
7159 done:
7160 if (commit_ref)
7161 got_ref_close(commit_ref);
7162 free(commit_ref_name);
7163 free(commit_id);
7164 return err;
7167 const struct got_error *
7168 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
7169 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7170 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7171 const char *committer, struct got_commit_object *orig_commit,
7172 struct got_object_id *orig_commit_id, const char *new_logmsg,
7173 int allow_conflict, struct got_repository *repo)
7175 const struct got_error *err;
7176 char *commit_ref_name;
7177 struct got_reference *commit_ref = NULL;
7179 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7180 if (err)
7181 return err;
7183 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7184 if (err)
7185 goto done;
7187 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7188 worktree, fileindex, tmp_branch, committer, orig_commit,
7189 new_logmsg, allow_conflict, repo);
7190 done:
7191 if (commit_ref)
7192 got_ref_close(commit_ref);
7193 free(commit_ref_name);
7194 return err;
7197 const struct got_error *
7198 got_worktree_rebase_postpone(struct got_worktree *worktree,
7199 struct got_fileindex *fileindex)
7201 if (fileindex)
7202 got_fileindex_free(fileindex);
7203 return lock_worktree(worktree, LOCK_SH);
7206 static const struct got_error *
7207 delete_ref(const char *name, struct got_repository *repo)
7209 const struct got_error *err;
7210 struct got_reference *ref;
7212 err = got_ref_open(&ref, repo, name, 0);
7213 if (err) {
7214 if (err->code == GOT_ERR_NOT_REF)
7215 return NULL;
7216 return err;
7219 err = got_ref_delete(ref, repo);
7220 got_ref_close(ref);
7221 return err;
7224 static const struct got_error *
7225 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7227 const struct got_error *err;
7228 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7229 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7231 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7232 if (err)
7233 goto done;
7234 err = delete_ref(tmp_branch_name, repo);
7235 if (err)
7236 goto done;
7238 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7239 if (err)
7240 goto done;
7241 err = delete_ref(new_base_branch_ref_name, repo);
7242 if (err)
7243 goto done;
7245 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7246 if (err)
7247 goto done;
7248 err = delete_ref(branch_ref_name, repo);
7249 if (err)
7250 goto done;
7252 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7253 if (err)
7254 goto done;
7255 err = delete_ref(commit_ref_name, repo);
7256 if (err)
7257 goto done;
7259 done:
7260 free(tmp_branch_name);
7261 free(new_base_branch_ref_name);
7262 free(branch_ref_name);
7263 free(commit_ref_name);
7264 return err;
7267 static const struct got_error *
7268 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7269 struct got_object_id *new_commit_id, struct got_repository *repo)
7271 const struct got_error *err;
7272 struct got_reference *ref = NULL;
7273 struct got_object_id *old_commit_id = NULL;
7274 const char *branch_name = NULL;
7275 char *new_id_str = NULL;
7276 char *refname = NULL;
7278 branch_name = got_ref_get_name(branch);
7279 if (strncmp(branch_name, "refs/heads/", 11) != 0)
7280 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7281 branch_name += 11;
7283 err = got_object_id_str(&new_id_str, new_commit_id);
7284 if (err)
7285 return err;
7287 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7288 new_id_str) == -1) {
7289 err = got_error_from_errno("asprintf");
7290 goto done;
7293 err = got_ref_resolve(&old_commit_id, repo, branch);
7294 if (err)
7295 goto done;
7297 err = got_ref_alloc(&ref, refname, old_commit_id);
7298 if (err)
7299 goto done;
7301 err = got_ref_write(ref, repo);
7302 done:
7303 free(new_id_str);
7304 free(refname);
7305 free(old_commit_id);
7306 if (ref)
7307 got_ref_close(ref);
7308 return err;
7311 const struct got_error *
7312 got_worktree_rebase_complete(struct got_worktree *worktree,
7313 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7314 struct got_reference *rebased_branch, struct got_repository *repo,
7315 int create_backup)
7317 const struct got_error *err, *unlockerr, *sync_err;
7318 struct got_object_id *new_head_commit_id = NULL;
7319 char *fileindex_path = NULL;
7321 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7322 if (err)
7323 return err;
7325 if (create_backup) {
7326 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7327 rebased_branch, new_head_commit_id, repo);
7328 if (err)
7329 goto done;
7332 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7333 if (err)
7334 goto done;
7336 err = got_ref_write(rebased_branch, repo);
7337 if (err)
7338 goto done;
7340 err = got_worktree_set_head_ref(worktree, rebased_branch);
7341 if (err)
7342 goto done;
7344 err = delete_rebase_refs(worktree, repo);
7345 if (err)
7346 goto done;
7348 err = get_fileindex_path(&fileindex_path, worktree);
7349 if (err)
7350 goto done;
7351 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7352 sync_err = sync_fileindex(fileindex, fileindex_path);
7353 if (sync_err && err == NULL)
7354 err = sync_err;
7355 done:
7356 got_fileindex_free(fileindex);
7357 free(fileindex_path);
7358 free(new_head_commit_id);
7359 unlockerr = lock_worktree(worktree, LOCK_SH);
7360 if (unlockerr && err == NULL)
7361 err = unlockerr;
7362 return err;
7365 static const struct got_error *
7366 get_paths_changed_between_commits(struct got_pathlist_head *paths,
7367 struct got_object_id *id1, struct got_object_id *id2,
7368 struct got_repository *repo)
7370 const struct got_error *err;
7371 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
7372 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7374 if (id1) {
7375 err = got_object_open_as_commit(&commit1, repo, id1);
7376 if (err)
7377 goto done;
7379 err = got_object_open_as_tree(&tree1, repo,
7380 got_object_commit_get_tree_id(commit1));
7381 if (err)
7382 goto done;
7385 if (id2) {
7386 err = got_object_open_as_commit(&commit2, repo, id2);
7387 if (err)
7388 goto done;
7390 err = got_object_open_as_tree(&tree2, repo,
7391 got_object_commit_get_tree_id(commit2));
7392 if (err)
7393 goto done;
7396 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
7397 got_diff_tree_collect_changed_paths, paths, 0);
7398 if (err)
7399 goto done;
7400 done:
7401 if (commit1)
7402 got_object_commit_close(commit1);
7403 if (commit2)
7404 got_object_commit_close(commit2);
7405 if (tree1)
7406 got_object_tree_close(tree1);
7407 if (tree2)
7408 got_object_tree_close(tree2);
7409 return err;
7412 static const struct got_error *
7413 get_paths_added_between_commits(struct got_pathlist_head *added_paths,
7414 struct got_object_id *id1, struct got_object_id *id2,
7415 const char *path_prefix, struct got_repository *repo)
7417 const struct got_error *err;
7418 struct got_pathlist_head merged_paths;
7419 struct got_pathlist_entry *pe;
7420 char *abspath = NULL, *wt_path = NULL;
7422 TAILQ_INIT(&merged_paths);
7424 err = get_paths_changed_between_commits(&merged_paths, id1, id2, repo);
7425 if (err)
7426 goto done;
7428 TAILQ_FOREACH(pe, &merged_paths, entry) {
7429 struct got_diff_changed_path *change = pe->data;
7431 if (change->status != GOT_STATUS_ADD)
7432 continue;
7434 if (got_path_is_root_dir(path_prefix)) {
7435 wt_path = strdup(pe->path);
7436 if (wt_path == NULL) {
7437 err = got_error_from_errno("strdup");
7438 goto done;
7440 } else {
7441 if (asprintf(&abspath, "/%s", pe->path) == -1) {
7442 err = got_error_from_errno("asprintf");
7443 goto done;
7446 err = got_path_skip_common_ancestor(&wt_path,
7447 path_prefix, abspath);
7448 if (err)
7449 goto done;
7450 free(abspath);
7451 abspath = NULL;
7454 err = got_pathlist_append(added_paths, wt_path, NULL);
7455 if (err)
7456 goto done;
7457 wt_path = NULL;
7460 done:
7461 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_ALL);
7462 free(abspath);
7463 free(wt_path);
7464 return err;
7467 static const struct got_error *
7468 get_paths_added_in_commit(struct got_pathlist_head *added_paths,
7469 struct got_object_id *id, const char *path_prefix,
7470 struct got_repository *repo)
7472 const struct got_error *err;
7473 struct got_commit_object *commit = NULL;
7474 struct got_object_qid *pid;
7476 err = got_object_open_as_commit(&commit, repo, id);
7477 if (err)
7478 goto done;
7480 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7482 err = get_paths_added_between_commits(added_paths,
7483 pid ? &pid->id : NULL, id, path_prefix, repo);
7484 if (err)
7485 goto done;
7486 done:
7487 if (commit)
7488 got_object_commit_close(commit);
7489 return err;
7492 const struct got_error *
7493 got_worktree_rebase_abort(struct got_worktree *worktree,
7494 struct got_fileindex *fileindex, struct got_repository *repo,
7495 struct got_reference *new_base_branch,
7496 got_worktree_checkout_cb progress_cb, void *progress_arg)
7498 const struct got_error *err, *unlockerr, *sync_err;
7499 struct got_reference *resolved = NULL;
7500 struct got_object_id *commit_id = NULL;
7501 struct got_object_id *merged_commit_id = NULL;
7502 struct got_commit_object *commit = NULL;
7503 char *fileindex_path = NULL;
7504 char *commit_ref_name = NULL;
7505 struct got_reference *commit_ref = NULL;
7506 struct revert_file_args rfa;
7507 struct got_object_id *tree_id = NULL;
7508 struct got_pathlist_head added_paths;
7510 TAILQ_INIT(&added_paths);
7512 err = lock_worktree(worktree, LOCK_EX);
7513 if (err)
7514 return err;
7516 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7517 if (err)
7518 goto done;
7520 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7521 if (err)
7522 goto done;
7524 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
7525 if (err)
7526 goto done;
7529 * Determine which files in added status can be safely removed
7530 * from disk while reverting changes in the work tree.
7531 * We want to avoid deleting unrelated files which were added by
7532 * the user for conflict resolution purposes.
7534 err = get_paths_added_in_commit(&added_paths, merged_commit_id,
7535 got_worktree_get_path_prefix(worktree), repo);
7536 if (err)
7537 goto done;
7539 err = got_ref_open(&resolved, repo,
7540 got_ref_get_symref_target(new_base_branch), 0);
7541 if (err)
7542 goto done;
7544 err = got_worktree_set_head_ref(worktree, resolved);
7545 if (err)
7546 goto done;
7549 * XXX commits to the base branch could have happened while
7550 * we were busy rebasing; should we store the original commit ID
7551 * when rebase begins and read it back here?
7553 err = got_ref_resolve(&commit_id, repo, resolved);
7554 if (err)
7555 goto done;
7557 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7558 if (err)
7559 goto done;
7561 err = got_object_open_as_commit(&commit, repo,
7562 worktree->base_commit_id);
7563 if (err)
7564 goto done;
7566 err = got_object_id_by_path(&tree_id, repo, commit,
7567 worktree->path_prefix);
7568 if (err)
7569 goto done;
7571 err = delete_rebase_refs(worktree, repo);
7572 if (err)
7573 goto done;
7575 err = get_fileindex_path(&fileindex_path, worktree);
7576 if (err)
7577 goto done;
7579 rfa.worktree = worktree;
7580 rfa.fileindex = fileindex;
7581 rfa.progress_cb = progress_cb;
7582 rfa.progress_arg = progress_arg;
7583 rfa.patch_cb = NULL;
7584 rfa.patch_arg = NULL;
7585 rfa.repo = repo;
7586 rfa.unlink_added_files = 1;
7587 rfa.added_files_to_unlink = &added_paths;
7588 err = worktree_status(worktree, "", fileindex, repo,
7589 revert_file, &rfa, NULL, NULL, 1, 0);
7590 if (err)
7591 goto sync;
7593 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7594 repo, progress_cb, progress_arg, NULL, NULL);
7595 sync:
7596 sync_err = sync_fileindex(fileindex, fileindex_path);
7597 if (sync_err && err == NULL)
7598 err = sync_err;
7599 done:
7600 got_pathlist_free(&added_paths, GOT_PATHLIST_FREE_PATH);
7601 got_ref_close(resolved);
7602 free(tree_id);
7603 free(commit_id);
7604 free(merged_commit_id);
7605 if (commit)
7606 got_object_commit_close(commit);
7607 if (fileindex)
7608 got_fileindex_free(fileindex);
7609 free(fileindex_path);
7610 free(commit_ref_name);
7611 if (commit_ref)
7612 got_ref_close(commit_ref);
7614 unlockerr = lock_worktree(worktree, LOCK_SH);
7615 if (unlockerr && err == NULL)
7616 err = unlockerr;
7617 return err;
7620 const struct got_error *
7621 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7622 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7623 struct got_fileindex **fileindex, struct got_worktree *worktree,
7624 struct got_repository *repo)
7626 const struct got_error *err = NULL;
7627 char *tmp_branch_name = NULL;
7628 char *branch_ref_name = NULL;
7629 char *base_commit_ref_name = NULL;
7630 char *fileindex_path = NULL;
7631 struct check_rebase_ok_arg ok_arg;
7632 struct got_reference *wt_branch = NULL;
7633 struct got_reference *base_commit_ref = NULL;
7635 *tmp_branch = NULL;
7636 *branch_ref = NULL;
7637 *base_commit_id = NULL;
7638 *fileindex = NULL;
7640 err = lock_worktree(worktree, LOCK_EX);
7641 if (err)
7642 return err;
7644 err = open_fileindex(fileindex, &fileindex_path, worktree);
7645 if (err)
7646 goto done;
7648 ok_arg.worktree = worktree;
7649 ok_arg.repo = repo;
7650 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7651 &ok_arg);
7652 if (err)
7653 goto done;
7655 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7656 if (err)
7657 goto done;
7659 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7660 if (err)
7661 goto done;
7663 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7664 worktree);
7665 if (err)
7666 goto done;
7668 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7669 0);
7670 if (err)
7671 goto done;
7673 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7674 if (err)
7675 goto done;
7677 err = got_ref_write(*branch_ref, repo);
7678 if (err)
7679 goto done;
7681 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7682 worktree->base_commit_id);
7683 if (err)
7684 goto done;
7685 err = got_ref_write(base_commit_ref, repo);
7686 if (err)
7687 goto done;
7688 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7689 if (*base_commit_id == NULL) {
7690 err = got_error_from_errno("got_object_id_dup");
7691 goto done;
7694 err = got_ref_alloc(tmp_branch, tmp_branch_name,
7695 worktree->base_commit_id);
7696 if (err)
7697 goto done;
7698 err = got_ref_write(*tmp_branch, repo);
7699 if (err)
7700 goto done;
7702 err = got_worktree_set_head_ref(worktree, *tmp_branch);
7703 if (err)
7704 goto done;
7705 done:
7706 free(fileindex_path);
7707 free(tmp_branch_name);
7708 free(branch_ref_name);
7709 free(base_commit_ref_name);
7710 if (wt_branch)
7711 got_ref_close(wt_branch);
7712 if (err) {
7713 if (*branch_ref) {
7714 got_ref_close(*branch_ref);
7715 *branch_ref = NULL;
7717 if (*tmp_branch) {
7718 got_ref_close(*tmp_branch);
7719 *tmp_branch = NULL;
7721 free(*base_commit_id);
7722 if (*fileindex) {
7723 got_fileindex_free(*fileindex);
7724 *fileindex = NULL;
7726 lock_worktree(worktree, LOCK_SH);
7728 return err;
7731 const struct got_error *
7732 got_worktree_histedit_postpone(struct got_worktree *worktree,
7733 struct got_fileindex *fileindex)
7735 if (fileindex)
7736 got_fileindex_free(fileindex);
7737 return lock_worktree(worktree, LOCK_SH);
7740 const struct got_error *
7741 got_worktree_histedit_in_progress(int *in_progress,
7742 struct got_worktree *worktree)
7744 const struct got_error *err;
7745 char *tmp_branch_name = NULL;
7747 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7748 if (err)
7749 return err;
7751 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7752 free(tmp_branch_name);
7753 return NULL;
7756 const struct got_error *
7757 got_worktree_histedit_continue(struct got_object_id **commit_id,
7758 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7759 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7760 struct got_worktree *worktree, struct got_repository *repo)
7762 const struct got_error *err;
7763 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7764 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7765 struct got_reference *commit_ref = NULL;
7766 struct got_reference *base_commit_ref = NULL;
7767 char *fileindex_path = NULL;
7768 int have_staged_files = 0;
7770 *commit_id = NULL;
7771 *tmp_branch = NULL;
7772 *base_commit_id = NULL;
7773 *fileindex = NULL;
7775 err = lock_worktree(worktree, LOCK_EX);
7776 if (err)
7777 return err;
7779 err = open_fileindex(fileindex, &fileindex_path, worktree);
7780 if (err)
7781 goto done;
7783 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7784 &have_staged_files);
7785 if (err && err->code != GOT_ERR_CANCELLED)
7786 goto done;
7787 if (have_staged_files) {
7788 err = got_error(GOT_ERR_STAGED_PATHS);
7789 goto done;
7792 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7793 if (err)
7794 goto done;
7796 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7797 if (err)
7798 goto done;
7800 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7801 if (err)
7802 goto done;
7804 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7805 worktree);
7806 if (err)
7807 goto done;
7809 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7810 if (err)
7811 goto done;
7813 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7814 if (err)
7815 goto done;
7816 err = got_ref_resolve(commit_id, repo, commit_ref);
7817 if (err)
7818 goto done;
7820 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7821 if (err)
7822 goto done;
7823 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7824 if (err)
7825 goto done;
7827 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7828 if (err)
7829 goto done;
7830 done:
7831 free(commit_ref_name);
7832 free(branch_ref_name);
7833 free(fileindex_path);
7834 if (commit_ref)
7835 got_ref_close(commit_ref);
7836 if (base_commit_ref)
7837 got_ref_close(base_commit_ref);
7838 if (err) {
7839 free(*commit_id);
7840 *commit_id = NULL;
7841 free(*base_commit_id);
7842 *base_commit_id = NULL;
7843 if (*tmp_branch) {
7844 got_ref_close(*tmp_branch);
7845 *tmp_branch = NULL;
7847 if (*fileindex) {
7848 got_fileindex_free(*fileindex);
7849 *fileindex = NULL;
7851 lock_worktree(worktree, LOCK_EX);
7853 return err;
7856 static const struct got_error *
7857 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7859 const struct got_error *err;
7860 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7861 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7863 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7864 if (err)
7865 goto done;
7866 err = delete_ref(tmp_branch_name, repo);
7867 if (err)
7868 goto done;
7870 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7871 worktree);
7872 if (err)
7873 goto done;
7874 err = delete_ref(base_commit_ref_name, repo);
7875 if (err)
7876 goto done;
7878 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7879 if (err)
7880 goto done;
7881 err = delete_ref(branch_ref_name, repo);
7882 if (err)
7883 goto done;
7885 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7886 if (err)
7887 goto done;
7888 err = delete_ref(commit_ref_name, repo);
7889 if (err)
7890 goto done;
7891 done:
7892 free(tmp_branch_name);
7893 free(base_commit_ref_name);
7894 free(branch_ref_name);
7895 free(commit_ref_name);
7896 return err;
7899 const struct got_error *
7900 got_worktree_histedit_abort(struct got_worktree *worktree,
7901 struct got_fileindex *fileindex, struct got_repository *repo,
7902 struct got_reference *branch, struct got_object_id *base_commit_id,
7903 got_worktree_checkout_cb progress_cb, void *progress_arg)
7905 const struct got_error *err, *unlockerr, *sync_err;
7906 struct got_reference *resolved = NULL;
7907 char *fileindex_path = NULL;
7908 struct got_object_id *merged_commit_id = NULL;
7909 struct got_commit_object *commit = NULL;
7910 char *commit_ref_name = NULL;
7911 struct got_reference *commit_ref = NULL;
7912 struct got_object_id *tree_id = NULL;
7913 struct revert_file_args rfa;
7914 struct got_pathlist_head added_paths;
7916 TAILQ_INIT(&added_paths);
7918 err = lock_worktree(worktree, LOCK_EX);
7919 if (err)
7920 return err;
7922 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7923 if (err)
7924 goto done;
7926 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7927 if (err) {
7928 if (err->code != GOT_ERR_NOT_REF)
7929 goto done;
7930 /* Can happen on early abort due to invalid histedit script. */
7931 commit_ref = NULL;
7934 if (commit_ref) {
7935 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
7936 if (err)
7937 goto done;
7940 * Determine which files in added status can be safely removed
7941 * from disk while reverting changes in the work tree.
7942 * We want to avoid deleting unrelated files added by the
7943 * user during conflict resolution or during histedit -e.
7945 err = get_paths_added_in_commit(&added_paths, merged_commit_id,
7946 got_worktree_get_path_prefix(worktree), repo);
7947 if (err)
7948 goto done;
7951 err = got_ref_open(&resolved, repo,
7952 got_ref_get_symref_target(branch), 0);
7953 if (err)
7954 goto done;
7956 err = got_worktree_set_head_ref(worktree, resolved);
7957 if (err)
7958 goto done;
7960 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7961 if (err)
7962 goto done;
7964 err = got_object_open_as_commit(&commit, repo,
7965 worktree->base_commit_id);
7966 if (err)
7967 goto done;
7969 err = got_object_id_by_path(&tree_id, repo, commit,
7970 worktree->path_prefix);
7971 if (err)
7972 goto done;
7974 err = delete_histedit_refs(worktree, repo);
7975 if (err)
7976 goto done;
7978 err = get_fileindex_path(&fileindex_path, worktree);
7979 if (err)
7980 goto done;
7982 rfa.worktree = worktree;
7983 rfa.fileindex = fileindex;
7984 rfa.progress_cb = progress_cb;
7985 rfa.progress_arg = progress_arg;
7986 rfa.patch_cb = NULL;
7987 rfa.patch_arg = NULL;
7988 rfa.repo = repo;
7989 rfa.unlink_added_files = 1;
7990 rfa.added_files_to_unlink = &added_paths;
7991 err = worktree_status(worktree, "", fileindex, repo,
7992 revert_file, &rfa, NULL, NULL, 1, 0);
7993 if (err)
7994 goto sync;
7996 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7997 repo, progress_cb, progress_arg, NULL, NULL);
7998 sync:
7999 sync_err = sync_fileindex(fileindex, fileindex_path);
8000 if (sync_err && err == NULL)
8001 err = sync_err;
8002 done:
8003 if (resolved)
8004 got_ref_close(resolved);
8005 if (commit_ref)
8006 got_ref_close(commit_ref);
8007 free(merged_commit_id);
8008 free(tree_id);
8009 free(fileindex_path);
8010 free(commit_ref_name);
8012 unlockerr = lock_worktree(worktree, LOCK_SH);
8013 if (unlockerr && err == NULL)
8014 err = unlockerr;
8015 return err;
8018 const struct got_error *
8019 got_worktree_histedit_complete(struct got_worktree *worktree,
8020 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8021 struct got_reference *edited_branch, struct got_repository *repo)
8023 const struct got_error *err, *unlockerr, *sync_err;
8024 struct got_object_id *new_head_commit_id = NULL;
8025 struct got_reference *resolved = NULL;
8026 char *fileindex_path = NULL;
8028 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
8029 if (err)
8030 return err;
8032 err = got_ref_open(&resolved, repo,
8033 got_ref_get_symref_target(edited_branch), 0);
8034 if (err)
8035 goto done;
8037 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
8038 resolved, new_head_commit_id, repo);
8039 if (err)
8040 goto done;
8042 err = got_ref_change_ref(resolved, new_head_commit_id);
8043 if (err)
8044 goto done;
8046 err = got_ref_write(resolved, repo);
8047 if (err)
8048 goto done;
8050 err = got_worktree_set_head_ref(worktree, resolved);
8051 if (err)
8052 goto done;
8054 err = delete_histedit_refs(worktree, repo);
8055 if (err)
8056 goto done;
8058 err = get_fileindex_path(&fileindex_path, worktree);
8059 if (err)
8060 goto done;
8061 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8062 sync_err = sync_fileindex(fileindex, fileindex_path);
8063 if (sync_err && err == NULL)
8064 err = sync_err;
8065 done:
8066 got_fileindex_free(fileindex);
8067 free(fileindex_path);
8068 free(new_head_commit_id);
8069 unlockerr = lock_worktree(worktree, LOCK_SH);
8070 if (unlockerr && err == NULL)
8071 err = unlockerr;
8072 return err;
8075 const struct got_error *
8076 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
8077 struct got_object_id *commit_id, struct got_repository *repo)
8079 const struct got_error *err;
8080 char *commit_ref_name;
8082 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
8083 if (err)
8084 return err;
8086 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
8087 if (err)
8088 goto done;
8090 err = delete_ref(commit_ref_name, repo);
8091 done:
8092 free(commit_ref_name);
8093 return err;
8096 const struct got_error *
8097 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
8098 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
8099 struct got_worktree *worktree, const char *refname,
8100 struct got_repository *repo)
8102 const struct got_error *err = NULL;
8103 char *fileindex_path = NULL;
8104 struct check_rebase_ok_arg ok_arg;
8106 *fileindex = NULL;
8107 *branch_ref = NULL;
8108 *base_branch_ref = NULL;
8110 err = lock_worktree(worktree, LOCK_EX);
8111 if (err)
8112 return err;
8114 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
8115 err = got_error_msg(GOT_ERR_SAME_BRANCH,
8116 "cannot integrate a branch into itself; "
8117 "update -b or different branch name required");
8118 goto done;
8121 err = open_fileindex(fileindex, &fileindex_path, worktree);
8122 if (err)
8123 goto done;
8125 /* Preconditions are the same as for rebase. */
8126 ok_arg.worktree = worktree;
8127 ok_arg.repo = repo;
8128 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8129 &ok_arg);
8130 if (err)
8131 goto done;
8133 err = got_ref_open(branch_ref, repo, refname, 1);
8134 if (err)
8135 goto done;
8137 err = got_ref_open(base_branch_ref, repo,
8138 got_worktree_get_head_ref_name(worktree), 1);
8139 done:
8140 if (err) {
8141 if (*branch_ref) {
8142 got_ref_close(*branch_ref);
8143 *branch_ref = NULL;
8145 if (*base_branch_ref) {
8146 got_ref_close(*base_branch_ref);
8147 *base_branch_ref = NULL;
8149 if (*fileindex) {
8150 got_fileindex_free(*fileindex);
8151 *fileindex = NULL;
8153 lock_worktree(worktree, LOCK_SH);
8155 return err;
8158 const struct got_error *
8159 got_worktree_integrate_continue(struct got_worktree *worktree,
8160 struct got_fileindex *fileindex, struct got_repository *repo,
8161 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
8162 got_worktree_checkout_cb progress_cb, void *progress_arg,
8163 got_cancel_cb cancel_cb, void *cancel_arg)
8165 const struct got_error *err = NULL, *sync_err, *unlockerr;
8166 char *fileindex_path = NULL;
8167 struct got_object_id *tree_id = NULL, *commit_id = NULL;
8168 struct got_commit_object *commit = NULL;
8170 err = get_fileindex_path(&fileindex_path, worktree);
8171 if (err)
8172 goto done;
8174 err = got_ref_resolve(&commit_id, repo, branch_ref);
8175 if (err)
8176 goto done;
8178 err = got_object_open_as_commit(&commit, repo, commit_id);
8179 if (err)
8180 goto done;
8182 err = got_object_id_by_path(&tree_id, repo, commit,
8183 worktree->path_prefix);
8184 if (err)
8185 goto done;
8187 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
8188 if (err)
8189 goto done;
8191 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
8192 progress_cb, progress_arg, cancel_cb, cancel_arg);
8193 if (err)
8194 goto sync;
8196 err = got_ref_change_ref(base_branch_ref, commit_id);
8197 if (err)
8198 goto sync;
8200 err = got_ref_write(base_branch_ref, repo);
8201 if (err)
8202 goto sync;
8204 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8205 sync:
8206 sync_err = sync_fileindex(fileindex, fileindex_path);
8207 if (sync_err && err == NULL)
8208 err = sync_err;
8210 done:
8211 unlockerr = got_ref_unlock(branch_ref);
8212 if (unlockerr && err == NULL)
8213 err = unlockerr;
8214 got_ref_close(branch_ref);
8216 unlockerr = got_ref_unlock(base_branch_ref);
8217 if (unlockerr && err == NULL)
8218 err = unlockerr;
8219 got_ref_close(base_branch_ref);
8221 got_fileindex_free(fileindex);
8222 free(fileindex_path);
8223 free(tree_id);
8224 if (commit)
8225 got_object_commit_close(commit);
8227 unlockerr = lock_worktree(worktree, LOCK_SH);
8228 if (unlockerr && err == NULL)
8229 err = unlockerr;
8230 return err;
8233 const struct got_error *
8234 got_worktree_integrate_abort(struct got_worktree *worktree,
8235 struct got_fileindex *fileindex, struct got_repository *repo,
8236 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
8238 const struct got_error *err = NULL, *unlockerr = NULL;
8240 got_fileindex_free(fileindex);
8242 err = lock_worktree(worktree, LOCK_SH);
8244 unlockerr = got_ref_unlock(branch_ref);
8245 if (unlockerr && err == NULL)
8246 err = unlockerr;
8247 got_ref_close(branch_ref);
8249 unlockerr = got_ref_unlock(base_branch_ref);
8250 if (unlockerr && err == NULL)
8251 err = unlockerr;
8252 got_ref_close(base_branch_ref);
8254 return err;
8257 const struct got_error *
8258 got_worktree_merge_postpone(struct got_worktree *worktree,
8259 struct got_fileindex *fileindex)
8261 const struct got_error *err, *sync_err;
8262 char *fileindex_path = NULL;
8264 err = get_fileindex_path(&fileindex_path, worktree);
8265 if (err)
8266 goto done;
8268 sync_err = sync_fileindex(fileindex, fileindex_path);
8270 err = lock_worktree(worktree, LOCK_SH);
8271 if (sync_err && err == NULL)
8272 err = sync_err;
8273 done:
8274 got_fileindex_free(fileindex);
8275 free(fileindex_path);
8276 return err;
8279 static const struct got_error *
8280 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
8282 const struct got_error *err;
8283 char *branch_refname = NULL, *commit_refname = NULL;
8285 err = get_merge_branch_ref_name(&branch_refname, worktree);
8286 if (err)
8287 goto done;
8288 err = delete_ref(branch_refname, repo);
8289 if (err)
8290 goto done;
8292 err = get_merge_commit_ref_name(&commit_refname, worktree);
8293 if (err)
8294 goto done;
8295 err = delete_ref(commit_refname, repo);
8296 if (err)
8297 goto done;
8299 done:
8300 free(branch_refname);
8301 free(commit_refname);
8302 return err;
8305 struct merge_commit_msg_arg {
8306 struct got_worktree *worktree;
8307 const char *branch_name;
8310 static const struct got_error *
8311 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
8312 const char *diff_path, char **logmsg, void *arg)
8314 struct merge_commit_msg_arg *a = arg;
8316 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
8317 got_worktree_get_head_ref_name(a->worktree)) == -1)
8318 return got_error_from_errno("asprintf");
8320 return NULL;
8324 const struct got_error *
8325 got_worktree_merge_branch(struct got_worktree *worktree,
8326 struct got_fileindex *fileindex,
8327 struct got_object_id *yca_commit_id,
8328 struct got_object_id *branch_tip,
8329 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
8330 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
8332 const struct got_error *err;
8333 char *fileindex_path = NULL;
8335 err = get_fileindex_path(&fileindex_path, worktree);
8336 if (err)
8337 goto done;
8339 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
8340 worktree);
8341 if (err)
8342 goto done;
8344 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
8345 branch_tip, repo, progress_cb, progress_arg,
8346 cancel_cb, cancel_arg);
8347 done:
8348 free(fileindex_path);
8349 return err;
8352 const struct got_error *
8353 got_worktree_merge_commit(struct got_object_id **new_commit_id,
8354 struct got_worktree *worktree, struct got_fileindex *fileindex,
8355 const char *author, const char *committer, int allow_bad_symlinks,
8356 struct got_object_id *branch_tip, const char *branch_name,
8357 int allow_conflict, struct got_repository *repo,
8358 got_worktree_status_cb status_cb, void *status_arg)
8361 const struct got_error *err = NULL, *sync_err;
8362 struct got_pathlist_head commitable_paths;
8363 struct collect_commitables_arg cc_arg;
8364 struct got_pathlist_entry *pe;
8365 struct got_reference *head_ref = NULL;
8366 struct got_object_id *head_commit_id = NULL;
8367 int have_staged_files = 0;
8368 struct merge_commit_msg_arg mcm_arg;
8369 char *fileindex_path = NULL;
8371 memset(&cc_arg, 0, sizeof(cc_arg));
8372 *new_commit_id = NULL;
8374 TAILQ_INIT(&commitable_paths);
8376 err = get_fileindex_path(&fileindex_path, worktree);
8377 if (err)
8378 goto done;
8380 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
8381 if (err)
8382 goto done;
8384 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8385 if (err)
8386 goto done;
8388 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
8389 &have_staged_files);
8390 if (err && err->code != GOT_ERR_CANCELLED)
8391 goto done;
8392 if (have_staged_files) {
8393 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
8394 goto done;
8397 cc_arg.commitable_paths = &commitable_paths;
8398 cc_arg.worktree = worktree;
8399 cc_arg.fileindex = fileindex;
8400 cc_arg.repo = repo;
8401 cc_arg.have_staged_files = have_staged_files;
8402 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
8403 cc_arg.commit_conflicts = allow_conflict;
8404 err = worktree_status(worktree, "", fileindex, repo,
8405 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8406 if (err)
8407 goto done;
8409 mcm_arg.worktree = worktree;
8410 mcm_arg.branch_name = branch_name;
8411 err = commit_worktree(new_commit_id, &commitable_paths,
8412 head_commit_id, branch_tip, worktree, author, committer, NULL,
8413 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8414 if (err)
8415 goto done;
8417 err = update_fileindex_after_commit(worktree, &commitable_paths,
8418 *new_commit_id, fileindex, have_staged_files);
8419 sync_err = sync_fileindex(fileindex, fileindex_path);
8420 if (sync_err && err == NULL)
8421 err = sync_err;
8422 done:
8423 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8424 struct got_commitable *ct = pe->data;
8426 free_commitable(ct);
8428 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8429 free(fileindex_path);
8430 return err;
8433 const struct got_error *
8434 got_worktree_merge_complete(struct got_worktree *worktree,
8435 struct got_fileindex *fileindex, struct got_repository *repo)
8437 const struct got_error *err, *unlockerr, *sync_err;
8438 char *fileindex_path = NULL;
8440 err = delete_merge_refs(worktree, repo);
8441 if (err)
8442 goto done;
8444 err = get_fileindex_path(&fileindex_path, worktree);
8445 if (err)
8446 goto done;
8447 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8448 sync_err = sync_fileindex(fileindex, fileindex_path);
8449 if (sync_err && err == NULL)
8450 err = sync_err;
8451 done:
8452 got_fileindex_free(fileindex);
8453 free(fileindex_path);
8454 unlockerr = lock_worktree(worktree, LOCK_SH);
8455 if (unlockerr && err == NULL)
8456 err = unlockerr;
8457 return err;
8460 const struct got_error *
8461 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8462 struct got_repository *repo)
8464 const struct got_error *err;
8465 char *branch_refname = NULL;
8466 struct got_reference *branch_ref = NULL;
8468 *in_progress = 0;
8470 err = get_merge_branch_ref_name(&branch_refname, worktree);
8471 if (err)
8472 return err;
8473 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8474 free(branch_refname);
8475 if (err) {
8476 if (err->code != GOT_ERR_NOT_REF)
8477 return err;
8478 } else
8479 *in_progress = 1;
8481 return NULL;
8484 const struct got_error *got_worktree_merge_prepare(
8485 struct got_fileindex **fileindex, struct got_worktree *worktree,
8486 struct got_reference *branch, struct got_repository *repo)
8488 const struct got_error *err = NULL;
8489 char *fileindex_path = NULL;
8490 char *branch_refname = NULL, *commit_refname = NULL;
8491 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
8492 struct got_reference *commit_ref = NULL;
8493 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
8494 struct check_rebase_ok_arg ok_arg;
8496 *fileindex = NULL;
8498 err = lock_worktree(worktree, LOCK_EX);
8499 if (err)
8500 return err;
8502 err = open_fileindex(fileindex, &fileindex_path, worktree);
8503 if (err)
8504 goto done;
8506 /* Preconditions are the same as for rebase. */
8507 ok_arg.worktree = worktree;
8508 ok_arg.repo = repo;
8509 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8510 &ok_arg);
8511 if (err)
8512 goto done;
8514 err = get_merge_branch_ref_name(&branch_refname, worktree);
8515 if (err)
8516 return err;
8518 err = get_merge_commit_ref_name(&commit_refname, worktree);
8519 if (err)
8520 return err;
8522 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8523 0);
8524 if (err)
8525 goto done;
8527 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8528 if (err)
8529 goto done;
8531 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8532 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8533 goto done;
8536 err = got_ref_resolve(&branch_tip, repo, branch);
8537 if (err)
8538 goto done;
8540 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8541 if (err)
8542 goto done;
8543 err = got_ref_write(branch_ref, repo);
8544 if (err)
8545 goto done;
8547 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8548 if (err)
8549 goto done;
8550 err = got_ref_write(commit_ref, repo);
8551 if (err)
8552 goto done;
8554 done:
8555 free(branch_refname);
8556 free(commit_refname);
8557 free(fileindex_path);
8558 if (branch_ref)
8559 got_ref_close(branch_ref);
8560 if (commit_ref)
8561 got_ref_close(commit_ref);
8562 if (wt_branch)
8563 got_ref_close(wt_branch);
8564 free(wt_branch_tip);
8565 if (err) {
8566 if (*fileindex) {
8567 got_fileindex_free(*fileindex);
8568 *fileindex = NULL;
8570 lock_worktree(worktree, LOCK_SH);
8572 return err;
8575 const struct got_error *
8576 got_worktree_merge_continue(char **branch_name,
8577 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8578 struct got_worktree *worktree, struct got_repository *repo)
8580 const struct got_error *err;
8581 char *commit_refname = NULL, *branch_refname = NULL;
8582 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8583 char *fileindex_path = NULL;
8584 int have_staged_files = 0;
8586 *branch_name = NULL;
8587 *branch_tip = NULL;
8588 *fileindex = NULL;
8590 err = lock_worktree(worktree, LOCK_EX);
8591 if (err)
8592 return err;
8594 err = open_fileindex(fileindex, &fileindex_path, worktree);
8595 if (err)
8596 goto done;
8598 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8599 &have_staged_files);
8600 if (err && err->code != GOT_ERR_CANCELLED)
8601 goto done;
8602 if (have_staged_files) {
8603 err = got_error(GOT_ERR_STAGED_PATHS);
8604 goto done;
8607 err = get_merge_branch_ref_name(&branch_refname, worktree);
8608 if (err)
8609 goto done;
8611 err = get_merge_commit_ref_name(&commit_refname, worktree);
8612 if (err)
8613 goto done;
8615 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8616 if (err)
8617 goto done;
8619 if (!got_ref_is_symbolic(branch_ref)) {
8620 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8621 "%s is not a symbolic reference",
8622 got_ref_get_name(branch_ref));
8623 goto done;
8625 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8626 if (*branch_name == NULL) {
8627 err = got_error_from_errno("strdup");
8628 goto done;
8631 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8632 if (err)
8633 goto done;
8635 err = got_ref_resolve(branch_tip, repo, commit_ref);
8636 if (err)
8637 goto done;
8638 done:
8639 free(commit_refname);
8640 free(branch_refname);
8641 free(fileindex_path);
8642 if (commit_ref)
8643 got_ref_close(commit_ref);
8644 if (branch_ref)
8645 got_ref_close(branch_ref);
8646 if (err) {
8647 if (*branch_name) {
8648 free(*branch_name);
8649 *branch_name = NULL;
8651 free(*branch_tip);
8652 *branch_tip = NULL;
8653 if (*fileindex) {
8654 got_fileindex_free(*fileindex);
8655 *fileindex = NULL;
8657 lock_worktree(worktree, LOCK_SH);
8659 return err;
8662 const struct got_error *
8663 got_worktree_merge_abort(struct got_worktree *worktree,
8664 struct got_fileindex *fileindex, struct got_repository *repo,
8665 got_worktree_checkout_cb progress_cb, void *progress_arg)
8667 const struct got_error *err, *unlockerr, *sync_err;
8668 struct got_commit_object *commit = NULL;
8669 char *fileindex_path = NULL;
8670 struct revert_file_args rfa;
8671 char *commit_ref_name = NULL;
8672 struct got_reference *commit_ref = NULL;
8673 struct got_object_id *merged_commit_id = NULL;
8674 struct got_object_id *tree_id = NULL;
8675 struct got_pathlist_head added_paths;
8677 TAILQ_INIT(&added_paths);
8679 err = get_merge_commit_ref_name(&commit_ref_name, worktree);
8680 if (err)
8681 goto done;
8683 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
8684 if (err)
8685 goto done;
8687 err = got_ref_resolve(&merged_commit_id, repo, commit_ref);
8688 if (err)
8689 goto done;
8692 * Determine which files in added status can be safely removed
8693 * from disk while reverting changes in the work tree.
8694 * We want to avoid deleting unrelated files which were added by
8695 * the user for conflict resolution purposes.
8697 err = get_paths_added_between_commits(&added_paths,
8698 got_worktree_get_base_commit_id(worktree), merged_commit_id,
8699 got_worktree_get_path_prefix(worktree), repo);
8700 if (err)
8701 goto done;
8704 err = got_object_open_as_commit(&commit, repo,
8705 worktree->base_commit_id);
8706 if (err)
8707 goto done;
8709 err = got_object_id_by_path(&tree_id, repo, commit,
8710 worktree->path_prefix);
8711 if (err)
8712 goto done;
8714 err = delete_merge_refs(worktree, repo);
8715 if (err)
8716 goto done;
8718 err = get_fileindex_path(&fileindex_path, worktree);
8719 if (err)
8720 goto done;
8722 rfa.worktree = worktree;
8723 rfa.fileindex = fileindex;
8724 rfa.progress_cb = progress_cb;
8725 rfa.progress_arg = progress_arg;
8726 rfa.patch_cb = NULL;
8727 rfa.patch_arg = NULL;
8728 rfa.repo = repo;
8729 rfa.unlink_added_files = 1;
8730 rfa.added_files_to_unlink = &added_paths;
8731 err = worktree_status(worktree, "", fileindex, repo,
8732 revert_file, &rfa, NULL, NULL, 1, 0);
8733 if (err)
8734 goto sync;
8736 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8737 repo, progress_cb, progress_arg, NULL, NULL);
8738 sync:
8739 sync_err = sync_fileindex(fileindex, fileindex_path);
8740 if (sync_err && err == NULL)
8741 err = sync_err;
8742 done:
8743 free(tree_id);
8744 free(merged_commit_id);
8745 if (commit)
8746 got_object_commit_close(commit);
8747 if (fileindex)
8748 got_fileindex_free(fileindex);
8749 free(fileindex_path);
8750 if (commit_ref)
8751 got_ref_close(commit_ref);
8752 free(commit_ref_name);
8754 unlockerr = lock_worktree(worktree, LOCK_SH);
8755 if (unlockerr && err == NULL)
8756 err = unlockerr;
8757 return err;
8760 struct check_stage_ok_arg {
8761 struct got_object_id *head_commit_id;
8762 struct got_worktree *worktree;
8763 struct got_fileindex *fileindex;
8764 struct got_repository *repo;
8765 int have_changes;
8768 static const struct got_error *
8769 check_stage_ok(void *arg, unsigned char status,
8770 unsigned char staged_status, const char *relpath,
8771 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8772 struct got_object_id *commit_id, int dirfd, const char *de_name)
8774 struct check_stage_ok_arg *a = arg;
8775 const struct got_error *err = NULL;
8776 struct got_fileindex_entry *ie;
8777 struct got_object_id base_commit_id;
8778 struct got_object_id *base_commit_idp = NULL;
8779 char *in_repo_path = NULL, *p;
8781 if (status == GOT_STATUS_UNVERSIONED ||
8782 status == GOT_STATUS_NO_CHANGE)
8783 return NULL;
8784 if (status == GOT_STATUS_NONEXISTENT)
8785 return got_error_set_errno(ENOENT, relpath);
8787 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8788 if (ie == NULL)
8789 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8791 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8792 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8793 relpath) == -1)
8794 return got_error_from_errno("asprintf");
8796 if (got_fileindex_entry_has_commit(ie)) {
8797 base_commit_idp = got_fileindex_entry_get_commit_id(
8798 &base_commit_id, ie);
8801 if (status == GOT_STATUS_CONFLICT) {
8802 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8803 goto done;
8804 } else if (status != GOT_STATUS_ADD &&
8805 status != GOT_STATUS_MODIFY &&
8806 status != GOT_STATUS_DELETE) {
8807 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8808 goto done;
8811 a->have_changes = 1;
8813 p = in_repo_path;
8814 while (p[0] == '/')
8815 p++;
8816 err = check_out_of_date(p, status, staged_status,
8817 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8818 GOT_ERR_STAGE_OUT_OF_DATE);
8819 done:
8820 free(in_repo_path);
8821 return err;
8824 struct stage_path_arg {
8825 struct got_worktree *worktree;
8826 struct got_fileindex *fileindex;
8827 struct got_repository *repo;
8828 got_worktree_status_cb status_cb;
8829 void *status_arg;
8830 got_worktree_patch_cb patch_cb;
8831 void *patch_arg;
8832 int staged_something;
8833 int allow_bad_symlinks;
8836 static const struct got_error *
8837 stage_path(void *arg, unsigned char status,
8838 unsigned char staged_status, const char *relpath,
8839 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8840 struct got_object_id *commit_id, int dirfd, const char *de_name)
8842 struct stage_path_arg *a = arg;
8843 const struct got_error *err = NULL;
8844 struct got_fileindex_entry *ie;
8845 char *ondisk_path = NULL, *path_content = NULL;
8846 uint32_t stage;
8847 struct got_object_id *new_staged_blob_id = NULL;
8848 struct stat sb;
8850 if (status == GOT_STATUS_UNVERSIONED)
8851 return NULL;
8853 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8854 if (ie == NULL)
8855 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8857 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8858 relpath)== -1)
8859 return got_error_from_errno("asprintf");
8861 switch (status) {
8862 case GOT_STATUS_ADD:
8863 case GOT_STATUS_MODIFY:
8864 /* XXX could sb.st_mode be passed in by our caller? */
8865 if (lstat(ondisk_path, &sb) == -1) {
8866 err = got_error_from_errno2("lstat", ondisk_path);
8867 break;
8869 if (a->patch_cb) {
8870 if (status == GOT_STATUS_ADD) {
8871 int choice = GOT_PATCH_CHOICE_NONE;
8872 err = (*a->patch_cb)(&choice, a->patch_arg,
8873 status, ie->path, NULL, 1, 1);
8874 if (err)
8875 break;
8876 if (choice != GOT_PATCH_CHOICE_YES)
8877 break;
8878 } else {
8879 err = create_patched_content(&path_content, 0,
8880 staged_blob_id ? staged_blob_id : blob_id,
8881 ondisk_path, dirfd, de_name, ie->path,
8882 a->repo, a->patch_cb, a->patch_arg);
8883 if (err || path_content == NULL)
8884 break;
8887 err = got_object_blob_create(&new_staged_blob_id,
8888 path_content ? path_content : ondisk_path, a->repo);
8889 if (err)
8890 break;
8891 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8892 SHA1_DIGEST_LENGTH);
8893 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8894 stage = GOT_FILEIDX_STAGE_ADD;
8895 else
8896 stage = GOT_FILEIDX_STAGE_MODIFY;
8897 got_fileindex_entry_stage_set(ie, stage);
8898 if (S_ISLNK(sb.st_mode)) {
8899 int is_bad_symlink = 0;
8900 if (!a->allow_bad_symlinks) {
8901 char target_path[PATH_MAX];
8902 ssize_t target_len;
8903 target_len = readlink(ondisk_path, target_path,
8904 sizeof(target_path));
8905 if (target_len == -1) {
8906 err = got_error_from_errno2("readlink",
8907 ondisk_path);
8908 break;
8910 err = is_bad_symlink_target(&is_bad_symlink,
8911 target_path, target_len, ondisk_path,
8912 a->worktree->root_path);
8913 if (err)
8914 break;
8915 if (is_bad_symlink) {
8916 err = got_error_path(ondisk_path,
8917 GOT_ERR_BAD_SYMLINK);
8918 break;
8921 if (is_bad_symlink)
8922 got_fileindex_entry_staged_filetype_set(ie,
8923 GOT_FILEIDX_MODE_BAD_SYMLINK);
8924 else
8925 got_fileindex_entry_staged_filetype_set(ie,
8926 GOT_FILEIDX_MODE_SYMLINK);
8927 } else {
8928 got_fileindex_entry_staged_filetype_set(ie,
8929 GOT_FILEIDX_MODE_REGULAR_FILE);
8931 a->staged_something = 1;
8932 if (a->status_cb == NULL)
8933 break;
8934 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8935 get_staged_status(ie), relpath, blob_id,
8936 new_staged_blob_id, NULL, dirfd, de_name);
8937 if (err)
8938 break;
8940 * When staging the reverse of the staged diff,
8941 * implicitly unstage the file.
8943 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8944 sizeof(ie->blob_sha1)) == 0) {
8945 got_fileindex_entry_stage_set(ie,
8946 GOT_FILEIDX_STAGE_NONE);
8948 break;
8949 case GOT_STATUS_DELETE:
8950 if (staged_status == GOT_STATUS_DELETE)
8951 break;
8952 if (a->patch_cb) {
8953 int choice = GOT_PATCH_CHOICE_NONE;
8954 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8955 ie->path, NULL, 1, 1);
8956 if (err)
8957 break;
8958 if (choice == GOT_PATCH_CHOICE_NO)
8959 break;
8960 if (choice != GOT_PATCH_CHOICE_YES) {
8961 err = got_error(GOT_ERR_PATCH_CHOICE);
8962 break;
8965 stage = GOT_FILEIDX_STAGE_DELETE;
8966 got_fileindex_entry_stage_set(ie, stage);
8967 a->staged_something = 1;
8968 if (a->status_cb == NULL)
8969 break;
8970 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8971 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8972 de_name);
8973 break;
8974 case GOT_STATUS_NO_CHANGE:
8975 break;
8976 case GOT_STATUS_CONFLICT:
8977 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8978 break;
8979 case GOT_STATUS_NONEXISTENT:
8980 err = got_error_set_errno(ENOENT, relpath);
8981 break;
8982 default:
8983 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8984 break;
8987 if (path_content && unlink(path_content) == -1 && err == NULL)
8988 err = got_error_from_errno2("unlink", path_content);
8989 free(path_content);
8990 free(ondisk_path);
8991 free(new_staged_blob_id);
8992 return err;
8995 const struct got_error *
8996 got_worktree_stage(struct got_worktree *worktree,
8997 struct got_pathlist_head *paths,
8998 got_worktree_status_cb status_cb, void *status_arg,
8999 got_worktree_patch_cb patch_cb, void *patch_arg,
9000 int allow_bad_symlinks, struct got_repository *repo)
9002 const struct got_error *err = NULL, *sync_err, *unlockerr;
9003 struct got_pathlist_entry *pe;
9004 struct got_fileindex *fileindex = NULL;
9005 char *fileindex_path = NULL;
9006 struct got_reference *head_ref = NULL;
9007 struct got_object_id *head_commit_id = NULL;
9008 struct check_stage_ok_arg oka;
9009 struct stage_path_arg spa;
9011 err = lock_worktree(worktree, LOCK_EX);
9012 if (err)
9013 return err;
9015 err = got_ref_open(&head_ref, repo,
9016 got_worktree_get_head_ref_name(worktree), 0);
9017 if (err)
9018 goto done;
9019 err = got_ref_resolve(&head_commit_id, repo, head_ref);
9020 if (err)
9021 goto done;
9022 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9023 if (err)
9024 goto done;
9026 /* Check pre-conditions before staging anything. */
9027 oka.head_commit_id = head_commit_id;
9028 oka.worktree = worktree;
9029 oka.fileindex = fileindex;
9030 oka.repo = repo;
9031 oka.have_changes = 0;
9032 TAILQ_FOREACH(pe, paths, entry) {
9033 err = worktree_status(worktree, pe->path, fileindex, repo,
9034 check_stage_ok, &oka, NULL, NULL, 1, 0);
9035 if (err)
9036 goto done;
9038 if (!oka.have_changes) {
9039 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9040 goto done;
9043 spa.worktree = worktree;
9044 spa.fileindex = fileindex;
9045 spa.repo = repo;
9046 spa.patch_cb = patch_cb;
9047 spa.patch_arg = patch_arg;
9048 spa.status_cb = status_cb;
9049 spa.status_arg = status_arg;
9050 spa.staged_something = 0;
9051 spa.allow_bad_symlinks = allow_bad_symlinks;
9052 TAILQ_FOREACH(pe, paths, entry) {
9053 err = worktree_status(worktree, pe->path, fileindex, repo,
9054 stage_path, &spa, NULL, NULL, 1, 0);
9055 if (err)
9056 goto done;
9058 if (!spa.staged_something) {
9059 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
9060 goto done;
9063 sync_err = sync_fileindex(fileindex, fileindex_path);
9064 if (sync_err && err == NULL)
9065 err = sync_err;
9066 done:
9067 if (head_ref)
9068 got_ref_close(head_ref);
9069 free(head_commit_id);
9070 free(fileindex_path);
9071 if (fileindex)
9072 got_fileindex_free(fileindex);
9073 unlockerr = lock_worktree(worktree, LOCK_SH);
9074 if (unlockerr && err == NULL)
9075 err = unlockerr;
9076 return err;
9079 struct unstage_path_arg {
9080 struct got_worktree *worktree;
9081 struct got_fileindex *fileindex;
9082 struct got_repository *repo;
9083 got_worktree_checkout_cb progress_cb;
9084 void *progress_arg;
9085 got_worktree_patch_cb patch_cb;
9086 void *patch_arg;
9089 static const struct got_error *
9090 create_unstaged_content(char **path_unstaged_content,
9091 char **path_new_staged_content, struct got_object_id *blob_id,
9092 struct got_object_id *staged_blob_id, const char *relpath,
9093 struct got_repository *repo,
9094 got_worktree_patch_cb patch_cb, void *patch_arg)
9096 const struct got_error *err, *free_err;
9097 struct got_blob_object *blob = NULL, *staged_blob = NULL;
9098 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
9099 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
9100 struct got_diffreg_result *diffreg_result = NULL;
9101 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
9102 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
9103 int fd1 = -1, fd2 = -1;
9105 *path_unstaged_content = NULL;
9106 *path_new_staged_content = NULL;
9108 err = got_object_id_str(&label1, blob_id);
9109 if (err)
9110 return err;
9112 fd1 = got_opentempfd();
9113 if (fd1 == -1) {
9114 err = got_error_from_errno("got_opentempfd");
9115 goto done;
9117 fd2 = got_opentempfd();
9118 if (fd2 == -1) {
9119 err = got_error_from_errno("got_opentempfd");
9120 goto done;
9123 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
9124 if (err)
9125 goto done;
9127 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
9128 if (err)
9129 goto done;
9131 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
9132 if (err)
9133 goto done;
9135 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
9136 fd2);
9137 if (err)
9138 goto done;
9140 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
9141 if (err)
9142 goto done;
9144 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
9145 if (err)
9146 goto done;
9148 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
9149 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
9150 if (err)
9151 goto done;
9153 err = got_opentemp_named(path_unstaged_content, &outfile,
9154 "got-unstaged-content", "");
9155 if (err)
9156 goto done;
9157 err = got_opentemp_named(path_new_staged_content, &rejectfile,
9158 "got-new-staged-content", "");
9159 if (err)
9160 goto done;
9162 if (fseek(f1, 0L, SEEK_SET) == -1) {
9163 err = got_ferror(f1, GOT_ERR_IO);
9164 goto done;
9166 if (fseek(f2, 0L, SEEK_SET) == -1) {
9167 err = got_ferror(f2, GOT_ERR_IO);
9168 goto done;
9170 /* Count the number of actual changes in the diff result. */
9171 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9172 struct diff_chunk_context cc = {};
9173 diff_chunk_context_load_change(&cc, &nchunks_used,
9174 diffreg_result->result, n, 0);
9175 nchanges++;
9177 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
9178 int choice;
9179 err = apply_or_reject_change(&choice, &nchunks_used,
9180 diffreg_result->result, n, relpath, f1, f2,
9181 &line_cur1, &line_cur2,
9182 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
9183 if (err)
9184 goto done;
9185 if (choice == GOT_PATCH_CHOICE_YES)
9186 have_content = 1;
9187 else
9188 have_rejected_content = 1;
9189 if (choice == GOT_PATCH_CHOICE_QUIT)
9190 break;
9192 if (have_content || have_rejected_content)
9193 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
9194 outfile, rejectfile);
9195 done:
9196 free(label1);
9197 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9198 err = got_error_from_errno("close");
9199 if (blob)
9200 got_object_blob_close(blob);
9201 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9202 err = got_error_from_errno("close");
9203 if (staged_blob)
9204 got_object_blob_close(staged_blob);
9205 free_err = got_diffreg_result_free(diffreg_result);
9206 if (free_err && err == NULL)
9207 err = free_err;
9208 if (f1 && fclose(f1) == EOF && err == NULL)
9209 err = got_error_from_errno2("fclose", path1);
9210 if (f2 && fclose(f2) == EOF && err == NULL)
9211 err = got_error_from_errno2("fclose", path2);
9212 if (outfile && fclose(outfile) == EOF && err == NULL)
9213 err = got_error_from_errno2("fclose", *path_unstaged_content);
9214 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
9215 err = got_error_from_errno2("fclose", *path_new_staged_content);
9216 if (path1 && unlink(path1) == -1 && err == NULL)
9217 err = got_error_from_errno2("unlink", path1);
9218 if (path2 && unlink(path2) == -1 && err == NULL)
9219 err = got_error_from_errno2("unlink", path2);
9220 if (err || !have_content) {
9221 if (*path_unstaged_content &&
9222 unlink(*path_unstaged_content) == -1 && err == NULL)
9223 err = got_error_from_errno2("unlink",
9224 *path_unstaged_content);
9225 free(*path_unstaged_content);
9226 *path_unstaged_content = NULL;
9228 if (err || !have_content || !have_rejected_content) {
9229 if (*path_new_staged_content &&
9230 unlink(*path_new_staged_content) == -1 && err == NULL)
9231 err = got_error_from_errno2("unlink",
9232 *path_new_staged_content);
9233 free(*path_new_staged_content);
9234 *path_new_staged_content = NULL;
9236 free(path1);
9237 free(path2);
9238 return err;
9241 static const struct got_error *
9242 unstage_hunks(struct got_object_id *staged_blob_id,
9243 struct got_blob_object *blob_base,
9244 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
9245 const char *ondisk_path, const char *label_orig,
9246 struct got_worktree *worktree, struct got_repository *repo,
9247 got_worktree_patch_cb patch_cb, void *patch_arg,
9248 got_worktree_checkout_cb progress_cb, void *progress_arg)
9250 const struct got_error *err = NULL;
9251 char *path_unstaged_content = NULL;
9252 char *path_new_staged_content = NULL;
9253 char *parent = NULL, *base_path = NULL;
9254 char *blob_base_path = NULL;
9255 struct got_object_id *new_staged_blob_id = NULL;
9256 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
9257 struct stat sb;
9259 err = create_unstaged_content(&path_unstaged_content,
9260 &path_new_staged_content, blob_id, staged_blob_id,
9261 ie->path, repo, patch_cb, patch_arg);
9262 if (err)
9263 return err;
9265 if (path_unstaged_content == NULL)
9266 return NULL;
9268 if (path_new_staged_content) {
9269 err = got_object_blob_create(&new_staged_blob_id,
9270 path_new_staged_content, repo);
9271 if (err)
9272 goto done;
9275 f = fopen(path_unstaged_content, "re");
9276 if (f == NULL) {
9277 err = got_error_from_errno2("fopen",
9278 path_unstaged_content);
9279 goto done;
9281 if (fstat(fileno(f), &sb) == -1) {
9282 err = got_error_from_errno2("fstat", path_unstaged_content);
9283 goto done;
9285 if (got_fileindex_entry_staged_filetype_get(ie) ==
9286 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
9287 char link_target[PATH_MAX];
9288 size_t r;
9289 r = fread(link_target, 1, sizeof(link_target), f);
9290 if (r == 0 && ferror(f)) {
9291 err = got_error_from_errno("fread");
9292 goto done;
9294 if (r >= sizeof(link_target)) { /* should not happen */
9295 err = got_error(GOT_ERR_NO_SPACE);
9296 goto done;
9298 link_target[r] = '\0';
9299 err = merge_symlink(worktree, blob_base,
9300 ondisk_path, ie->path, label_orig, link_target,
9301 worktree->base_commit_id, repo, progress_cb,
9302 progress_arg);
9303 } else {
9304 int local_changes_subsumed;
9306 err = got_path_dirname(&parent, ondisk_path);
9307 if (err)
9308 return err;
9310 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
9311 parent) == -1) {
9312 err = got_error_from_errno("asprintf");
9313 base_path = NULL;
9314 goto done;
9317 err = got_opentemp_named(&blob_base_path, &f_base,
9318 base_path, "");
9319 if (err)
9320 goto done;
9321 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
9322 blob_base);
9323 if (err)
9324 goto done;
9327 * In order the run a 3-way merge with a symlink we copy the symlink's
9328 * target path into a temporary file and use that file with diff3.
9330 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9331 err = dump_symlink_target_path_to_file(&f_deriv2,
9332 ondisk_path);
9333 if (err)
9334 goto done;
9335 } else {
9336 int fd;
9337 fd = open(ondisk_path,
9338 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
9339 if (fd == -1) {
9340 err = got_error_from_errno2("open", ondisk_path);
9341 goto done;
9343 f_deriv2 = fdopen(fd, "r");
9344 if (f_deriv2 == NULL) {
9345 err = got_error_from_errno2("fdopen", ondisk_path);
9346 close(fd);
9347 goto done;
9351 err = merge_file(&local_changes_subsumed, worktree,
9352 f_base, f, f_deriv2, ondisk_path, ie->path,
9353 got_fileindex_perms_to_st(ie),
9354 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
9355 repo, progress_cb, progress_arg);
9357 if (err)
9358 goto done;
9360 if (new_staged_blob_id) {
9361 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9362 SHA1_DIGEST_LENGTH);
9363 } else {
9364 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9365 got_fileindex_entry_staged_filetype_set(ie, 0);
9367 done:
9368 free(new_staged_blob_id);
9369 if (path_unstaged_content &&
9370 unlink(path_unstaged_content) == -1 && err == NULL)
9371 err = got_error_from_errno2("unlink", path_unstaged_content);
9372 if (path_new_staged_content &&
9373 unlink(path_new_staged_content) == -1 && err == NULL)
9374 err = got_error_from_errno2("unlink", path_new_staged_content);
9375 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
9376 err = got_error_from_errno2("unlink", blob_base_path);
9377 if (f_base && fclose(f_base) == EOF && err == NULL)
9378 err = got_error_from_errno2("fclose", path_unstaged_content);
9379 if (f && fclose(f) == EOF && err == NULL)
9380 err = got_error_from_errno2("fclose", path_unstaged_content);
9381 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
9382 err = got_error_from_errno2("fclose", ondisk_path);
9383 free(path_unstaged_content);
9384 free(path_new_staged_content);
9385 free(blob_base_path);
9386 free(parent);
9387 free(base_path);
9388 return err;
9391 static const struct got_error *
9392 unstage_path(void *arg, unsigned char status,
9393 unsigned char staged_status, const char *relpath,
9394 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9395 struct got_object_id *commit_id, int dirfd, const char *de_name)
9397 const struct got_error *err = NULL;
9398 struct unstage_path_arg *a = arg;
9399 struct got_fileindex_entry *ie;
9400 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
9401 char *ondisk_path = NULL;
9402 char *id_str = NULL, *label_orig = NULL;
9403 int local_changes_subsumed;
9404 struct stat sb;
9405 int fd1 = -1, fd2 = -1;
9407 if (staged_status != GOT_STATUS_ADD &&
9408 staged_status != GOT_STATUS_MODIFY &&
9409 staged_status != GOT_STATUS_DELETE)
9410 return NULL;
9412 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9413 if (ie == NULL)
9414 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9416 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9417 == -1)
9418 return got_error_from_errno("asprintf");
9420 err = got_object_id_str(&id_str,
9421 commit_id ? commit_id : a->worktree->base_commit_id);
9422 if (err)
9423 goto done;
9424 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9425 id_str) == -1) {
9426 err = got_error_from_errno("asprintf");
9427 goto done;
9430 fd1 = got_opentempfd();
9431 if (fd1 == -1) {
9432 err = got_error_from_errno("got_opentempfd");
9433 goto done;
9435 fd2 = got_opentempfd();
9436 if (fd2 == -1) {
9437 err = got_error_from_errno("got_opentempfd");
9438 goto done;
9441 switch (staged_status) {
9442 case GOT_STATUS_MODIFY:
9443 err = got_object_open_as_blob(&blob_base, a->repo,
9444 blob_id, 8192, fd1);
9445 if (err)
9446 break;
9447 /* fall through */
9448 case GOT_STATUS_ADD:
9449 if (a->patch_cb) {
9450 if (staged_status == GOT_STATUS_ADD) {
9451 int choice = GOT_PATCH_CHOICE_NONE;
9452 err = (*a->patch_cb)(&choice, a->patch_arg,
9453 staged_status, ie->path, NULL, 1, 1);
9454 if (err)
9455 break;
9456 if (choice != GOT_PATCH_CHOICE_YES)
9457 break;
9458 } else {
9459 err = unstage_hunks(staged_blob_id,
9460 blob_base, blob_id, ie, ondisk_path,
9461 label_orig, a->worktree, a->repo,
9462 a->patch_cb, a->patch_arg,
9463 a->progress_cb, a->progress_arg);
9464 break; /* Done with this file. */
9467 err = got_object_open_as_blob(&blob_staged, a->repo,
9468 staged_blob_id, 8192, fd2);
9469 if (err)
9470 break;
9471 switch (got_fileindex_entry_staged_filetype_get(ie)) {
9472 case GOT_FILEIDX_MODE_BAD_SYMLINK:
9473 case GOT_FILEIDX_MODE_REGULAR_FILE:
9474 err = merge_blob(&local_changes_subsumed, a->worktree,
9475 blob_base, ondisk_path, relpath,
9476 got_fileindex_perms_to_st(ie), label_orig,
9477 blob_staged, commit_id ? commit_id :
9478 a->worktree->base_commit_id, a->repo,
9479 a->progress_cb, a->progress_arg);
9480 break;
9481 case GOT_FILEIDX_MODE_SYMLINK:
9482 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9483 char *staged_target;
9484 err = got_object_blob_read_to_str(
9485 &staged_target, blob_staged);
9486 if (err)
9487 goto done;
9488 err = merge_symlink(a->worktree, blob_base,
9489 ondisk_path, relpath, label_orig,
9490 staged_target, commit_id ? commit_id :
9491 a->worktree->base_commit_id,
9492 a->repo, a->progress_cb, a->progress_arg);
9493 free(staged_target);
9494 } else {
9495 err = merge_blob(&local_changes_subsumed,
9496 a->worktree, blob_base, ondisk_path,
9497 relpath, got_fileindex_perms_to_st(ie),
9498 label_orig, blob_staged,
9499 commit_id ? commit_id :
9500 a->worktree->base_commit_id, a->repo,
9501 a->progress_cb, a->progress_arg);
9503 break;
9504 default:
9505 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9506 break;
9508 if (err == NULL) {
9509 got_fileindex_entry_stage_set(ie,
9510 GOT_FILEIDX_STAGE_NONE);
9511 got_fileindex_entry_staged_filetype_set(ie, 0);
9513 break;
9514 case GOT_STATUS_DELETE:
9515 if (a->patch_cb) {
9516 int choice = GOT_PATCH_CHOICE_NONE;
9517 err = (*a->patch_cb)(&choice, a->patch_arg,
9518 staged_status, ie->path, NULL, 1, 1);
9519 if (err)
9520 break;
9521 if (choice == GOT_PATCH_CHOICE_NO)
9522 break;
9523 if (choice != GOT_PATCH_CHOICE_YES) {
9524 err = got_error(GOT_ERR_PATCH_CHOICE);
9525 break;
9528 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9529 got_fileindex_entry_staged_filetype_set(ie, 0);
9530 err = get_file_status(&status, &sb, ie, ondisk_path,
9531 dirfd, de_name, a->repo);
9532 if (err)
9533 break;
9534 err = (*a->progress_cb)(a->progress_arg, status, relpath);
9535 break;
9537 done:
9538 free(ondisk_path);
9539 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9540 err = got_error_from_errno("close");
9541 if (blob_base)
9542 got_object_blob_close(blob_base);
9543 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9544 err = got_error_from_errno("close");
9545 if (blob_staged)
9546 got_object_blob_close(blob_staged);
9547 free(id_str);
9548 free(label_orig);
9549 return err;
9552 const struct got_error *
9553 got_worktree_unstage(struct got_worktree *worktree,
9554 struct got_pathlist_head *paths,
9555 got_worktree_checkout_cb progress_cb, void *progress_arg,
9556 got_worktree_patch_cb patch_cb, void *patch_arg,
9557 struct got_repository *repo)
9559 const struct got_error *err = NULL, *sync_err, *unlockerr;
9560 struct got_pathlist_entry *pe;
9561 struct got_fileindex *fileindex = NULL;
9562 char *fileindex_path = NULL;
9563 struct unstage_path_arg upa;
9565 err = lock_worktree(worktree, LOCK_EX);
9566 if (err)
9567 return err;
9569 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9570 if (err)
9571 goto done;
9573 upa.worktree = worktree;
9574 upa.fileindex = fileindex;
9575 upa.repo = repo;
9576 upa.progress_cb = progress_cb;
9577 upa.progress_arg = progress_arg;
9578 upa.patch_cb = patch_cb;
9579 upa.patch_arg = patch_arg;
9580 TAILQ_FOREACH(pe, paths, entry) {
9581 err = worktree_status(worktree, pe->path, fileindex, repo,
9582 unstage_path, &upa, NULL, NULL, 1, 0);
9583 if (err)
9584 goto done;
9587 sync_err = sync_fileindex(fileindex, fileindex_path);
9588 if (sync_err && err == NULL)
9589 err = sync_err;
9590 done:
9591 free(fileindex_path);
9592 if (fileindex)
9593 got_fileindex_free(fileindex);
9594 unlockerr = lock_worktree(worktree, LOCK_SH);
9595 if (unlockerr && err == NULL)
9596 err = unlockerr;
9597 return err;
9600 struct report_file_info_arg {
9601 struct got_worktree *worktree;
9602 got_worktree_path_info_cb info_cb;
9603 void *info_arg;
9604 struct got_pathlist_head *paths;
9605 got_cancel_cb cancel_cb;
9606 void *cancel_arg;
9609 static const struct got_error *
9610 report_file_info(void *arg, struct got_fileindex_entry *ie)
9612 struct report_file_info_arg *a = arg;
9613 struct got_pathlist_entry *pe;
9614 struct got_object_id blob_id, staged_blob_id, commit_id;
9615 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9616 struct got_object_id *commit_idp = NULL;
9617 int stage;
9619 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9620 return got_error(GOT_ERR_CANCELLED);
9622 TAILQ_FOREACH(pe, a->paths, entry) {
9623 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9624 got_path_is_child(ie->path, pe->path, pe->path_len))
9625 break;
9627 if (pe == NULL) /* not found */
9628 return NULL;
9630 if (got_fileindex_entry_has_blob(ie))
9631 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9632 stage = got_fileindex_entry_stage_get(ie);
9633 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9634 stage == GOT_FILEIDX_STAGE_ADD) {
9635 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9636 &staged_blob_id, ie);
9639 if (got_fileindex_entry_has_commit(ie))
9640 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9642 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9643 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9646 const struct got_error *
9647 got_worktree_path_info(struct got_worktree *worktree,
9648 struct got_pathlist_head *paths,
9649 got_worktree_path_info_cb info_cb, void *info_arg,
9650 got_cancel_cb cancel_cb, void *cancel_arg)
9653 const struct got_error *err = NULL, *unlockerr;
9654 struct got_fileindex *fileindex = NULL;
9655 char *fileindex_path = NULL;
9656 struct report_file_info_arg arg;
9658 err = lock_worktree(worktree, LOCK_SH);
9659 if (err)
9660 return err;
9662 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9663 if (err)
9664 goto done;
9666 arg.worktree = worktree;
9667 arg.info_cb = info_cb;
9668 arg.info_arg = info_arg;
9669 arg.paths = paths;
9670 arg.cancel_cb = cancel_cb;
9671 arg.cancel_arg = cancel_arg;
9672 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9673 &arg);
9674 done:
9675 free(fileindex_path);
9676 if (fileindex)
9677 got_fileindex_free(fileindex);
9678 unlockerr = lock_worktree(worktree, LOCK_UN);
9679 if (unlockerr && err == NULL)
9680 err = unlockerr;
9681 return err;
9684 static const struct got_error *
9685 patch_check_path(const char *p, char **path, unsigned char *status,
9686 unsigned char *staged_status, struct got_fileindex *fileindex,
9687 struct got_worktree *worktree, struct got_repository *repo)
9689 const struct got_error *err;
9690 struct got_fileindex_entry *ie;
9691 struct stat sb;
9692 char *ondisk_path = NULL;
9694 err = got_worktree_resolve_path(path, worktree, p);
9695 if (err)
9696 return err;
9698 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9699 *path[0] ? "/" : "", *path) == -1)
9700 return got_error_from_errno("asprintf");
9702 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9703 if (ie) {
9704 *staged_status = get_staged_status(ie);
9705 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9706 repo);
9707 if (err)
9708 goto done;
9709 } else {
9710 *staged_status = GOT_STATUS_NO_CHANGE;
9711 *status = GOT_STATUS_UNVERSIONED;
9712 if (lstat(ondisk_path, &sb) == -1) {
9713 if (errno != ENOENT) {
9714 err = got_error_from_errno2("lstat",
9715 ondisk_path);
9716 goto done;
9718 *status = GOT_STATUS_NONEXISTENT;
9722 done:
9723 free(ondisk_path);
9724 return err;
9727 static const struct got_error *
9728 patch_can_rm(const char *path, unsigned char status,
9729 unsigned char staged_status)
9731 if (status == GOT_STATUS_NONEXISTENT)
9732 return got_error_set_errno(ENOENT, path);
9733 if (status != GOT_STATUS_NO_CHANGE &&
9734 status != GOT_STATUS_ADD &&
9735 status != GOT_STATUS_MODIFY &&
9736 status != GOT_STATUS_MODE_CHANGE)
9737 return got_error_path(path, GOT_ERR_FILE_STATUS);
9738 if (staged_status == GOT_STATUS_DELETE)
9739 return got_error_path(path, GOT_ERR_FILE_STATUS);
9740 return NULL;
9743 static const struct got_error *
9744 patch_can_add(const char *path, unsigned char status)
9746 if (status != GOT_STATUS_NONEXISTENT)
9747 return got_error_path(path, GOT_ERR_FILE_STATUS);
9748 return NULL;
9751 static const struct got_error *
9752 patch_can_edit(const char *path, unsigned char status,
9753 unsigned char staged_status)
9755 if (status == GOT_STATUS_NONEXISTENT)
9756 return got_error_set_errno(ENOENT, path);
9757 if (status != GOT_STATUS_NO_CHANGE &&
9758 status != GOT_STATUS_ADD &&
9759 status != GOT_STATUS_MODIFY)
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 const struct got_error *
9767 got_worktree_patch_prepare(struct got_fileindex **fileindex,
9768 char **fileindex_path, struct got_worktree *worktree)
9770 return open_fileindex(fileindex, fileindex_path, worktree);
9773 const struct got_error *
9774 got_worktree_patch_check_path(const char *old, const char *new,
9775 char **oldpath, char **newpath, struct got_worktree *worktree,
9776 struct got_repository *repo, struct got_fileindex *fileindex)
9778 const struct got_error *err = NULL;
9779 int file_renamed = 0;
9780 unsigned char status_old, staged_status_old;
9781 unsigned char status_new, staged_status_new;
9783 *oldpath = NULL;
9784 *newpath = NULL;
9786 err = patch_check_path(old != NULL ? old : new, oldpath,
9787 &status_old, &staged_status_old, fileindex, worktree, repo);
9788 if (err)
9789 goto done;
9791 err = patch_check_path(new != NULL ? new : old, newpath,
9792 &status_new, &staged_status_new, fileindex, worktree, repo);
9793 if (err)
9794 goto done;
9796 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9797 file_renamed = 1;
9799 if (old != NULL && new == NULL)
9800 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9801 else if (file_renamed) {
9802 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9803 if (err == NULL)
9804 err = patch_can_add(*newpath, status_new);
9805 } else if (old == NULL)
9806 err = patch_can_add(*newpath, status_new);
9807 else
9808 err = patch_can_edit(*newpath, status_new, staged_status_new);
9810 done:
9811 if (err) {
9812 free(*oldpath);
9813 *oldpath = NULL;
9814 free(*newpath);
9815 *newpath = NULL;
9817 return err;
9820 const struct got_error *
9821 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9822 struct got_worktree *worktree, struct got_fileindex *fileindex,
9823 got_worktree_checkout_cb progress_cb, void *progress_arg)
9825 struct schedule_addition_args saa;
9827 memset(&saa, 0, sizeof(saa));
9828 saa.worktree = worktree;
9829 saa.fileindex = fileindex;
9830 saa.progress_cb = progress_cb;
9831 saa.progress_arg = progress_arg;
9832 saa.repo = repo;
9834 return worktree_status(worktree, path, fileindex, repo,
9835 schedule_addition, &saa, NULL, NULL, 1, 0);
9838 const struct got_error *
9839 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9840 struct got_worktree *worktree, struct got_fileindex *fileindex,
9841 got_worktree_delete_cb progress_cb, void *progress_arg)
9843 const struct got_error *err;
9844 struct schedule_deletion_args sda;
9845 char *ondisk_status_path;
9847 memset(&sda, 0, sizeof(sda));
9848 sda.worktree = worktree;
9849 sda.fileindex = fileindex;
9850 sda.progress_cb = progress_cb;
9851 sda.progress_arg = progress_arg;
9852 sda.repo = repo;
9853 sda.delete_local_mods = 0;
9854 sda.keep_on_disk = 0;
9855 sda.ignore_missing_paths = 0;
9856 sda.status_codes = NULL;
9857 if (asprintf(&ondisk_status_path, "%s/%s",
9858 got_worktree_get_root_path(worktree), path) == -1)
9859 return got_error_from_errno("asprintf");
9860 sda.status_path = ondisk_status_path;
9861 sda.status_path_len = strlen(ondisk_status_path);
9863 err = worktree_status(worktree, path, fileindex, repo,
9864 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9865 free(ondisk_status_path);
9866 return err;
9869 const struct got_error *
9870 got_worktree_patch_complete(struct got_fileindex *fileindex,
9871 const char *fileindex_path)
9873 const struct got_error *err = NULL;
9875 err = sync_fileindex(fileindex, fileindex_path);
9876 got_fileindex_free(fileindex);
9878 return err;