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_codes;
4315 static const struct got_error *
4316 schedule_for_deletion(void *arg, unsigned char status,
4317 unsigned char staged_status, const char *relpath,
4318 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4319 struct got_object_id *commit_id, int dirfd, const char *de_name)
4321 struct schedule_deletion_args *a = arg;
4322 const struct got_error *err = NULL;
4323 struct got_fileindex_entry *ie = NULL;
4324 struct stat sb;
4325 char *ondisk_path;
4327 if (status == GOT_STATUS_NONEXISTENT) {
4328 if (a->ignore_missing_paths)
4329 return NULL;
4330 return got_error_set_errno(ENOENT, relpath);
4333 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4334 if (ie == NULL)
4335 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
4337 staged_status = get_staged_status(ie);
4338 if (staged_status != GOT_STATUS_NO_CHANGE) {
4339 if (staged_status == GOT_STATUS_DELETE)
4340 return NULL;
4341 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4344 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4345 relpath) == -1)
4346 return got_error_from_errno("asprintf");
4348 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4349 a->repo);
4350 if (err)
4351 goto done;
4353 if (a->status_codes) {
4354 size_t ncodes = strlen(a->status_codes);
4355 int i;
4356 for (i = 0; i < ncodes ; i++) {
4357 if (status == a->status_codes[i])
4358 break;
4360 if (i == ncodes) {
4361 /* Do not delete files in non-matching status. */
4362 free(ondisk_path);
4363 return NULL;
4365 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4366 a->status_codes[i] != GOT_STATUS_MISSING) {
4367 static char msg[64];
4368 snprintf(msg, sizeof(msg),
4369 "invalid status code '%c'", a->status_codes[i]);
4370 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4371 goto done;
4375 if (status != GOT_STATUS_NO_CHANGE) {
4376 if (status == GOT_STATUS_DELETE)
4377 goto done;
4378 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4379 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4380 goto done;
4382 if (status == GOT_STATUS_MISSING && !a->ignore_missing_paths) {
4383 err = got_error_set_errno(ENOENT, relpath);
4384 goto done;
4386 if (status != GOT_STATUS_MODIFY &&
4387 status != GOT_STATUS_MISSING) {
4388 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4389 goto done;
4393 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4394 size_t root_len;
4396 if (dirfd != -1) {
4397 if (unlinkat(dirfd, de_name, 0) == -1) {
4398 err = got_error_from_errno2("unlinkat",
4399 ondisk_path);
4400 goto done;
4402 } else if (unlink(ondisk_path) == -1) {
4403 err = got_error_from_errno2("unlink", ondisk_path);
4404 goto done;
4407 root_len = strlen(a->worktree->root_path);
4408 do {
4409 char *parent;
4410 err = got_path_dirname(&parent, ondisk_path);
4411 if (err)
4412 goto done;
4413 free(ondisk_path);
4414 ondisk_path = parent;
4415 if (rmdir(ondisk_path) == -1) {
4416 if (errno != ENOTEMPTY)
4417 err = got_error_from_errno2("rmdir",
4418 ondisk_path);
4419 break;
4421 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4422 strlen(ondisk_path), root_len) != 0);
4425 got_fileindex_entry_mark_deleted_from_disk(ie);
4426 done:
4427 free(ondisk_path);
4428 if (err)
4429 return err;
4430 if (status == GOT_STATUS_DELETE)
4431 return NULL;
4432 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4433 staged_status, relpath);
4436 const struct got_error *
4437 got_worktree_schedule_delete(struct got_worktree *worktree,
4438 struct got_pathlist_head *paths, int delete_local_mods,
4439 const char *status_codes,
4440 got_worktree_delete_cb progress_cb, void *progress_arg,
4441 struct got_repository *repo, int keep_on_disk, int ignore_missing_paths)
4443 struct got_fileindex *fileindex = NULL;
4444 char *fileindex_path = NULL;
4445 const struct got_error *err = NULL, *sync_err, *unlockerr;
4446 struct got_pathlist_entry *pe;
4447 struct schedule_deletion_args sda;
4449 err = lock_worktree(worktree, LOCK_EX);
4450 if (err)
4451 return err;
4453 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4454 if (err)
4455 goto done;
4457 sda.worktree = worktree;
4458 sda.fileindex = fileindex;
4459 sda.progress_cb = progress_cb;
4460 sda.progress_arg = progress_arg;
4461 sda.repo = repo;
4462 sda.delete_local_mods = delete_local_mods;
4463 sda.keep_on_disk = keep_on_disk;
4464 sda.ignore_missing_paths = ignore_missing_paths;
4465 sda.status_codes = status_codes;
4467 TAILQ_FOREACH(pe, paths, entry) {
4468 err = worktree_status(worktree, pe->path, fileindex, repo,
4469 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4470 if (err)
4471 break;
4473 sync_err = sync_fileindex(fileindex, fileindex_path);
4474 if (sync_err && err == NULL)
4475 err = sync_err;
4476 done:
4477 free(fileindex_path);
4478 if (fileindex)
4479 got_fileindex_free(fileindex);
4480 unlockerr = lock_worktree(worktree, LOCK_SH);
4481 if (unlockerr && err == NULL)
4482 err = unlockerr;
4483 return err;
4486 static const struct got_error *
4487 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4489 const struct got_error *err = NULL;
4490 char *line = NULL;
4491 size_t linesize = 0, n;
4492 ssize_t linelen;
4494 linelen = getline(&line, &linesize, infile);
4495 if (linelen == -1) {
4496 if (ferror(infile)) {
4497 err = got_error_from_errno("getline");
4498 goto done;
4500 return NULL;
4502 if (outfile) {
4503 n = fwrite(line, 1, linelen, outfile);
4504 if (n != linelen) {
4505 err = got_ferror(outfile, GOT_ERR_IO);
4506 goto done;
4509 if (rejectfile) {
4510 n = fwrite(line, 1, linelen, rejectfile);
4511 if (n != linelen)
4512 err = got_ferror(rejectfile, GOT_ERR_IO);
4514 done:
4515 free(line);
4516 return err;
4519 static const struct got_error *
4520 skip_one_line(FILE *f)
4522 char *line = NULL;
4523 size_t linesize = 0;
4524 ssize_t linelen;
4526 linelen = getline(&line, &linesize, f);
4527 if (linelen == -1) {
4528 if (ferror(f))
4529 return got_error_from_errno("getline");
4530 return NULL;
4532 free(line);
4533 return NULL;
4536 static const struct got_error *
4537 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4538 int start_old, int end_old, int start_new, int end_new,
4539 FILE *outfile, FILE *rejectfile)
4541 const struct got_error *err;
4543 /* Copy old file's lines leading up to patch. */
4544 while (!feof(f1) && *line_cur1 < start_old) {
4545 err = copy_one_line(f1, outfile, NULL);
4546 if (err)
4547 return err;
4548 (*line_cur1)++;
4550 /* Skip new file's lines leading up to patch. */
4551 while (!feof(f2) && *line_cur2 < start_new) {
4552 if (rejectfile)
4553 err = copy_one_line(f2, NULL, rejectfile);
4554 else
4555 err = skip_one_line(f2);
4556 if (err)
4557 return err;
4558 (*line_cur2)++;
4560 /* Copy patched lines. */
4561 while (!feof(f2) && *line_cur2 <= end_new) {
4562 err = copy_one_line(f2, outfile, NULL);
4563 if (err)
4564 return err;
4565 (*line_cur2)++;
4567 /* Skip over old file's replaced lines. */
4568 while (!feof(f1) && *line_cur1 <= end_old) {
4569 if (rejectfile)
4570 err = copy_one_line(f1, NULL, rejectfile);
4571 else
4572 err = skip_one_line(f1);
4573 if (err)
4574 return err;
4575 (*line_cur1)++;
4578 return NULL;
4581 static const struct got_error *
4582 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4583 FILE *outfile, FILE *rejectfile)
4585 const struct got_error *err;
4587 if (outfile) {
4588 /* Copy old file's lines until EOF. */
4589 while (!feof(f1)) {
4590 err = copy_one_line(f1, outfile, NULL);
4591 if (err)
4592 return err;
4593 (*line_cur1)++;
4596 if (rejectfile) {
4597 /* Copy new file's lines until EOF. */
4598 while (!feof(f2)) {
4599 err = copy_one_line(f2, NULL, rejectfile);
4600 if (err)
4601 return err;
4602 (*line_cur2)++;
4606 return NULL;
4609 static const struct got_error *
4610 apply_or_reject_change(int *choice, int *nchunks_used,
4611 struct diff_result *diff_result, int n,
4612 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4613 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4614 got_worktree_patch_cb patch_cb, void *patch_arg)
4616 const struct got_error *err = NULL;
4617 struct diff_chunk_context cc = {};
4618 int start_old, end_old, start_new, end_new;
4619 FILE *hunkfile;
4620 struct diff_output_unidiff_state *diff_state;
4621 struct diff_input_info diff_info;
4622 int rc;
4624 *choice = GOT_PATCH_CHOICE_NONE;
4626 /* Get changed line numbers without context lines for copy_change(). */
4627 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4628 start_old = cc.left.start;
4629 end_old = cc.left.end;
4630 start_new = cc.right.start;
4631 end_new = cc.right.end;
4633 /* Get the same change with context lines for display. */
4634 memset(&cc, 0, sizeof(cc));
4635 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4637 memset(&diff_info, 0, sizeof(diff_info));
4638 diff_info.left_path = relpath;
4639 diff_info.right_path = relpath;
4641 diff_state = diff_output_unidiff_state_alloc();
4642 if (diff_state == NULL)
4643 return got_error_set_errno(ENOMEM,
4644 "diff_output_unidiff_state_alloc");
4646 hunkfile = got_opentemp();
4647 if (hunkfile == NULL) {
4648 err = got_error_from_errno("got_opentemp");
4649 goto done;
4652 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4653 diff_result, &cc);
4654 if (rc != DIFF_RC_OK) {
4655 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4656 goto done;
4659 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4660 err = got_ferror(hunkfile, GOT_ERR_IO);
4661 goto done;
4664 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4665 hunkfile, changeno, nchanges);
4666 if (err)
4667 goto done;
4669 switch (*choice) {
4670 case GOT_PATCH_CHOICE_YES:
4671 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4672 end_old, start_new, end_new, outfile, rejectfile);
4673 break;
4674 case GOT_PATCH_CHOICE_NO:
4675 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4676 end_old, start_new, end_new, rejectfile, outfile);
4677 break;
4678 case GOT_PATCH_CHOICE_QUIT:
4679 break;
4680 default:
4681 err = got_error(GOT_ERR_PATCH_CHOICE);
4682 break;
4684 done:
4685 diff_output_unidiff_state_free(diff_state);
4686 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4687 err = got_error_from_errno("fclose");
4688 return err;
4691 struct revert_file_args {
4692 struct got_worktree *worktree;
4693 struct got_fileindex *fileindex;
4694 got_worktree_checkout_cb progress_cb;
4695 void *progress_arg;
4696 got_worktree_patch_cb patch_cb;
4697 void *patch_arg;
4698 struct got_repository *repo;
4699 int unlink_added_files;
4702 static const struct got_error *
4703 create_patched_content(char **path_outfile, int reverse_patch,
4704 struct got_object_id *blob_id, const char *path2,
4705 int dirfd2, const char *de_name2,
4706 const char *relpath, struct got_repository *repo,
4707 got_worktree_patch_cb patch_cb, void *patch_arg)
4709 const struct got_error *err, *free_err;
4710 struct got_blob_object *blob = NULL;
4711 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4712 int fd = -1, fd2 = -1;
4713 char link_target[PATH_MAX];
4714 ssize_t link_len = 0;
4715 char *path1 = NULL, *id_str = NULL;
4716 struct stat sb2;
4717 struct got_diffreg_result *diffreg_result = NULL;
4718 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4719 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4721 *path_outfile = NULL;
4723 err = got_object_id_str(&id_str, blob_id);
4724 if (err)
4725 return err;
4727 if (dirfd2 != -1) {
4728 fd2 = openat(dirfd2, de_name2,
4729 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4730 if (fd2 == -1) {
4731 if (!got_err_open_nofollow_on_symlink()) {
4732 err = got_error_from_errno2("openat", path2);
4733 goto done;
4735 link_len = readlinkat(dirfd2, de_name2,
4736 link_target, sizeof(link_target));
4737 if (link_len == -1) {
4738 return got_error_from_errno2("readlinkat",
4739 path2);
4741 sb2.st_mode = S_IFLNK;
4742 sb2.st_size = link_len;
4744 } else {
4745 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4746 if (fd2 == -1) {
4747 if (!got_err_open_nofollow_on_symlink()) {
4748 err = got_error_from_errno2("open", path2);
4749 goto done;
4751 link_len = readlink(path2, link_target,
4752 sizeof(link_target));
4753 if (link_len == -1)
4754 return got_error_from_errno2("readlink", path2);
4755 sb2.st_mode = S_IFLNK;
4756 sb2.st_size = link_len;
4759 if (fd2 != -1) {
4760 if (fstat(fd2, &sb2) == -1) {
4761 err = got_error_from_errno2("fstat", path2);
4762 goto done;
4765 f2 = fdopen(fd2, "r");
4766 if (f2 == NULL) {
4767 err = got_error_from_errno2("fdopen", path2);
4768 goto done;
4770 fd2 = -1;
4771 } else {
4772 size_t n;
4773 f2 = got_opentemp();
4774 if (f2 == NULL) {
4775 err = got_error_from_errno2("got_opentemp", path2);
4776 goto done;
4778 n = fwrite(link_target, 1, link_len, f2);
4779 if (n != link_len) {
4780 err = got_ferror(f2, GOT_ERR_IO);
4781 goto done;
4783 if (fflush(f2) == EOF) {
4784 err = got_error_from_errno("fflush");
4785 goto done;
4787 rewind(f2);
4790 fd = got_opentempfd();
4791 if (fd == -1) {
4792 err = got_error_from_errno("got_opentempfd");
4793 goto done;
4796 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd);
4797 if (err)
4798 goto done;
4800 err = got_opentemp_named(&path1, &f1, "got-patched-blob", "");
4801 if (err)
4802 goto done;
4804 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4805 if (err)
4806 goto done;
4808 err = got_diff_files(&diffreg_result, f1, 1, id_str, f2, 1, path2,
4809 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
4810 if (err)
4811 goto done;
4813 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content",
4814 "");
4815 if (err)
4816 goto done;
4818 if (fseek(f1, 0L, SEEK_SET) == -1)
4819 return got_ferror(f1, GOT_ERR_IO);
4820 if (fseek(f2, 0L, SEEK_SET) == -1)
4821 return got_ferror(f2, GOT_ERR_IO);
4823 /* Count the number of actual changes in the diff result. */
4824 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4825 struct diff_chunk_context cc = {};
4826 diff_chunk_context_load_change(&cc, &nchunks_used,
4827 diffreg_result->result, n, 0);
4828 nchanges++;
4830 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4831 int choice;
4832 err = apply_or_reject_change(&choice, &nchunks_used,
4833 diffreg_result->result, n, relpath, f1, f2,
4834 &line_cur1, &line_cur2,
4835 reverse_patch ? NULL : outfile,
4836 reverse_patch ? outfile : NULL,
4837 ++i, nchanges, patch_cb, patch_arg);
4838 if (err)
4839 goto done;
4840 if (choice == GOT_PATCH_CHOICE_YES)
4841 have_content = 1;
4842 else if (choice == GOT_PATCH_CHOICE_QUIT)
4843 break;
4845 if (have_content) {
4846 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4847 reverse_patch ? NULL : outfile,
4848 reverse_patch ? outfile : NULL);
4849 if (err)
4850 goto done;
4852 if (!S_ISLNK(sb2.st_mode)) {
4853 mode_t mode;
4855 mode = apply_umask(sb2.st_mode);
4856 if (fchmod(fileno(outfile), mode) == -1) {
4857 err = got_error_from_errno2("fchmod", path2);
4858 goto done;
4862 done:
4863 free(id_str);
4864 if (fd != -1 && close(fd) == -1 && err == NULL)
4865 err = got_error_from_errno("close");
4866 if (blob)
4867 got_object_blob_close(blob);
4868 free_err = got_diffreg_result_free(diffreg_result);
4869 if (err == NULL)
4870 err = free_err;
4871 if (f1 && fclose(f1) == EOF && err == NULL)
4872 err = got_error_from_errno2("fclose", path1);
4873 if (f2 && fclose(f2) == EOF && err == NULL)
4874 err = got_error_from_errno2("fclose", path2);
4875 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4876 err = got_error_from_errno2("close", path2);
4877 if (outfile && fclose(outfile) == EOF && err == NULL)
4878 err = got_error_from_errno2("fclose", *path_outfile);
4879 if (path1 && unlink(path1) == -1 && err == NULL)
4880 err = got_error_from_errno2("unlink", path1);
4881 if (err || !have_content) {
4882 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4883 err = got_error_from_errno2("unlink", *path_outfile);
4884 free(*path_outfile);
4885 *path_outfile = NULL;
4887 free(path1);
4888 return err;
4891 static const struct got_error *
4892 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4893 const char *relpath, struct got_object_id *blob_id,
4894 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4895 int dirfd, const char *de_name)
4897 struct revert_file_args *a = arg;
4898 const struct got_error *err = NULL;
4899 char *parent_path = NULL;
4900 struct got_fileindex_entry *ie;
4901 struct got_commit_object *base_commit = NULL;
4902 struct got_tree_object *tree = NULL;
4903 struct got_object_id *tree_id = NULL;
4904 const struct got_tree_entry *te = NULL;
4905 char *tree_path = NULL, *te_name;
4906 char *ondisk_path = NULL, *path_content = NULL;
4907 struct got_blob_object *blob = NULL;
4908 int fd = -1;
4910 /* Reverting a staged deletion is a no-op. */
4911 if (status == GOT_STATUS_DELETE &&
4912 staged_status != GOT_STATUS_NO_CHANGE)
4913 return NULL;
4915 if (status == GOT_STATUS_UNVERSIONED)
4916 return (*a->progress_cb)(a->progress_arg,
4917 GOT_STATUS_UNVERSIONED, relpath);
4919 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4920 if (ie == NULL)
4921 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4923 /* Construct in-repository path of tree which contains this blob. */
4924 err = got_path_dirname(&parent_path, ie->path);
4925 if (err) {
4926 if (err->code != GOT_ERR_BAD_PATH)
4927 goto done;
4928 parent_path = strdup("/");
4929 if (parent_path == NULL) {
4930 err = got_error_from_errno("strdup");
4931 goto done;
4934 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4935 tree_path = strdup(parent_path);
4936 if (tree_path == NULL) {
4937 err = got_error_from_errno("strdup");
4938 goto done;
4940 } else {
4941 if (got_path_is_root_dir(parent_path)) {
4942 tree_path = strdup(a->worktree->path_prefix);
4943 if (tree_path == NULL) {
4944 err = got_error_from_errno("strdup");
4945 goto done;
4947 } else {
4948 if (asprintf(&tree_path, "%s/%s",
4949 a->worktree->path_prefix, parent_path) == -1) {
4950 err = got_error_from_errno("asprintf");
4951 goto done;
4956 err = got_object_open_as_commit(&base_commit, a->repo,
4957 a->worktree->base_commit_id);
4958 if (err)
4959 goto done;
4961 err = got_object_id_by_path(&tree_id, a->repo, base_commit, tree_path);
4962 if (err) {
4963 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4964 (status == GOT_STATUS_ADD ||
4965 staged_status == GOT_STATUS_ADD)))
4966 goto done;
4967 } else {
4968 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4969 if (err)
4970 goto done;
4972 err = got_path_basename(&te_name, ie->path);
4973 if (err)
4974 goto done;
4976 te = got_object_tree_find_entry(tree, te_name);
4977 free(te_name);
4978 if (te == NULL && status != GOT_STATUS_ADD &&
4979 staged_status != GOT_STATUS_ADD) {
4980 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4981 goto done;
4985 switch (status) {
4986 case GOT_STATUS_ADD:
4987 if (a->patch_cb) {
4988 int choice = GOT_PATCH_CHOICE_NONE;
4989 err = (*a->patch_cb)(&choice, a->patch_arg,
4990 status, ie->path, NULL, 1, 1);
4991 if (err)
4992 goto done;
4993 if (choice != GOT_PATCH_CHOICE_YES)
4994 break;
4996 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4997 ie->path);
4998 if (err)
4999 goto done;
5000 got_fileindex_entry_remove(a->fileindex, ie);
5001 if (a->unlink_added_files) {
5002 if (asprintf(&ondisk_path, "%s/%s",
5003 got_worktree_get_root_path(a->worktree),
5004 relpath) == -1) {
5005 err = got_error_from_errno("asprintf");
5006 goto done;
5008 if (unlink(ondisk_path) == -1) {
5009 err = got_error_from_errno2("unlink",
5010 ondisk_path);
5011 break;
5014 break;
5015 case GOT_STATUS_DELETE:
5016 if (a->patch_cb) {
5017 int choice = GOT_PATCH_CHOICE_NONE;
5018 err = (*a->patch_cb)(&choice, a->patch_arg,
5019 status, ie->path, NULL, 1, 1);
5020 if (err)
5021 goto done;
5022 if (choice != GOT_PATCH_CHOICE_YES)
5023 break;
5025 /* fall through */
5026 case GOT_STATUS_MODIFY:
5027 case GOT_STATUS_MODE_CHANGE:
5028 case GOT_STATUS_CONFLICT:
5029 case GOT_STATUS_MISSING: {
5030 struct got_object_id id;
5031 if (staged_status == GOT_STATUS_ADD ||
5032 staged_status == GOT_STATUS_MODIFY)
5033 got_fileindex_entry_get_staged_blob_id(&id, ie);
5034 else
5035 got_fileindex_entry_get_blob_id(&id, ie);
5036 fd = got_opentempfd();
5037 if (fd == -1) {
5038 err = got_error_from_errno("got_opentempfd");
5039 goto done;
5042 err = got_object_open_as_blob(&blob, a->repo, &id, 8192, fd);
5043 if (err)
5044 goto done;
5046 if (asprintf(&ondisk_path, "%s/%s",
5047 got_worktree_get_root_path(a->worktree), relpath) == -1) {
5048 err = got_error_from_errno("asprintf");
5049 goto done;
5052 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
5053 status == GOT_STATUS_CONFLICT)) {
5054 int is_bad_symlink = 0;
5055 err = create_patched_content(&path_content, 1, &id,
5056 ondisk_path, dirfd, de_name, ie->path, a->repo,
5057 a->patch_cb, a->patch_arg);
5058 if (err || path_content == NULL)
5059 break;
5060 if (te && S_ISLNK(te->mode)) {
5061 if (unlink(path_content) == -1) {
5062 err = got_error_from_errno2("unlink",
5063 path_content);
5064 break;
5066 err = install_symlink(&is_bad_symlink,
5067 a->worktree, ondisk_path, ie->path,
5068 blob, 0, 1, 0, 0, a->repo,
5069 a->progress_cb, a->progress_arg);
5070 } else {
5071 if (rename(path_content, ondisk_path) == -1) {
5072 err = got_error_from_errno3("rename",
5073 path_content, ondisk_path);
5074 goto done;
5077 } else {
5078 int is_bad_symlink = 0;
5079 if (te && S_ISLNK(te->mode)) {
5080 err = install_symlink(&is_bad_symlink,
5081 a->worktree, ondisk_path, ie->path,
5082 blob, 0, 1, 0, 0, a->repo,
5083 a->progress_cb, a->progress_arg);
5084 } else {
5085 err = install_blob(a->worktree, ondisk_path,
5086 ie->path,
5087 te ? te->mode : GOT_DEFAULT_FILE_MODE,
5088 got_fileindex_perms_to_st(ie), blob,
5089 0, 1, 0, 0, a->repo,
5090 a->progress_cb, a->progress_arg);
5092 if (err)
5093 goto done;
5094 if (status == GOT_STATUS_DELETE ||
5095 status == GOT_STATUS_MODE_CHANGE) {
5096 err = got_fileindex_entry_update(ie,
5097 a->worktree->root_fd, relpath,
5098 blob->id.sha1,
5099 a->worktree->base_commit_id->sha1, 1);
5100 if (err)
5101 goto done;
5103 if (is_bad_symlink) {
5104 got_fileindex_entry_filetype_set(ie,
5105 GOT_FILEIDX_MODE_BAD_SYMLINK);
5108 break;
5110 default:
5111 break;
5113 done:
5114 free(ondisk_path);
5115 free(path_content);
5116 free(parent_path);
5117 free(tree_path);
5118 if (fd != -1 && close(fd) == -1 && err == NULL)
5119 err = got_error_from_errno("close");
5120 if (blob)
5121 got_object_blob_close(blob);
5122 if (tree)
5123 got_object_tree_close(tree);
5124 free(tree_id);
5125 if (base_commit)
5126 got_object_commit_close(base_commit);
5127 return err;
5130 const struct got_error *
5131 got_worktree_revert(struct got_worktree *worktree,
5132 struct got_pathlist_head *paths,
5133 got_worktree_checkout_cb progress_cb, void *progress_arg,
5134 got_worktree_patch_cb patch_cb, void *patch_arg,
5135 struct got_repository *repo)
5137 struct got_fileindex *fileindex = NULL;
5138 char *fileindex_path = NULL;
5139 const struct got_error *err = NULL, *unlockerr = NULL;
5140 const struct got_error *sync_err = NULL;
5141 struct got_pathlist_entry *pe;
5142 struct revert_file_args rfa;
5144 err = lock_worktree(worktree, LOCK_EX);
5145 if (err)
5146 return err;
5148 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5149 if (err)
5150 goto done;
5152 rfa.worktree = worktree;
5153 rfa.fileindex = fileindex;
5154 rfa.progress_cb = progress_cb;
5155 rfa.progress_arg = progress_arg;
5156 rfa.patch_cb = patch_cb;
5157 rfa.patch_arg = patch_arg;
5158 rfa.repo = repo;
5159 rfa.unlink_added_files = 0;
5160 TAILQ_FOREACH(pe, paths, entry) {
5161 err = worktree_status(worktree, pe->path, fileindex, repo,
5162 revert_file, &rfa, NULL, NULL, 1, 0);
5163 if (err)
5164 break;
5166 sync_err = sync_fileindex(fileindex, fileindex_path);
5167 if (sync_err && err == NULL)
5168 err = sync_err;
5169 done:
5170 free(fileindex_path);
5171 if (fileindex)
5172 got_fileindex_free(fileindex);
5173 unlockerr = lock_worktree(worktree, LOCK_SH);
5174 if (unlockerr && err == NULL)
5175 err = unlockerr;
5176 return err;
5179 static void
5180 free_commitable(struct got_commitable *ct)
5182 free(ct->path);
5183 free(ct->in_repo_path);
5184 free(ct->ondisk_path);
5185 free(ct->blob_id);
5186 free(ct->base_blob_id);
5187 free(ct->staged_blob_id);
5188 free(ct->base_commit_id);
5189 free(ct);
5192 struct collect_commitables_arg {
5193 struct got_pathlist_head *commitable_paths;
5194 struct got_repository *repo;
5195 struct got_worktree *worktree;
5196 struct got_fileindex *fileindex;
5197 int have_staged_files;
5198 int allow_bad_symlinks;
5199 int diff_header_shown;
5200 int commit_conflicts;
5201 FILE *diff_outfile;
5202 FILE *f1;
5203 FILE *f2;
5207 * Create a file which contains the target path of a symlink so we can feed
5208 * it as content to the diff engine.
5210 static const struct got_error *
5211 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
5212 const char *abspath)
5214 const struct got_error *err = NULL;
5215 char target_path[PATH_MAX];
5216 ssize_t target_len, outlen;
5218 *fd = -1;
5220 if (dirfd != -1) {
5221 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
5222 if (target_len == -1)
5223 return got_error_from_errno2("readlinkat", abspath);
5224 } else {
5225 target_len = readlink(abspath, target_path, PATH_MAX);
5226 if (target_len == -1)
5227 return got_error_from_errno2("readlink", abspath);
5230 *fd = got_opentempfd();
5231 if (*fd == -1)
5232 return got_error_from_errno("got_opentempfd");
5234 outlen = write(*fd, target_path, target_len);
5235 if (outlen == -1) {
5236 err = got_error_from_errno("got_opentempfd");
5237 goto done;
5240 if (lseek(*fd, 0, SEEK_SET) == -1) {
5241 err = got_error_from_errno2("lseek", abspath);
5242 goto done;
5244 done:
5245 if (err) {
5246 close(*fd);
5247 *fd = -1;
5249 return err;
5252 static const struct got_error *
5253 append_ct_diff(struct got_commitable *ct, int *diff_header_shown,
5254 FILE *diff_outfile, FILE *f1, FILE *f2, int dirfd, const char *de_name,
5255 int diff_staged, struct got_repository *repo, struct got_worktree *worktree)
5257 const struct got_error *err = NULL;
5258 struct got_blob_object *blob1 = NULL;
5259 int fd = -1, fd1 = -1, fd2 = -1;
5260 FILE *ondisk_file = NULL;
5261 char *label1 = NULL;
5262 struct stat sb;
5263 off_t size1 = 0;
5264 int f2_exists = 0;
5265 char *id_str = NULL;
5267 memset(&sb, 0, sizeof(sb));
5269 if (diff_staged) {
5270 if (ct->staged_status != GOT_STATUS_MODIFY &&
5271 ct->staged_status != GOT_STATUS_ADD &&
5272 ct->staged_status != GOT_STATUS_DELETE)
5273 return NULL;
5274 } else {
5275 if (ct->status != GOT_STATUS_MODIFY &&
5276 ct->status != GOT_STATUS_ADD &&
5277 ct->status != GOT_STATUS_DELETE &&
5278 ct->status != GOT_STATUS_CONFLICT)
5279 return NULL;
5282 err = got_opentemp_truncate(f1);
5283 if (err)
5284 return got_error_from_errno("got_opentemp_truncate");
5285 err = got_opentemp_truncate(f2);
5286 if (err)
5287 return got_error_from_errno("got_opentemp_truncate");
5289 if (!*diff_header_shown) {
5290 err = got_object_id_str(&id_str, worktree->base_commit_id);
5291 if (err)
5292 return err;
5293 fprintf(diff_outfile, "diff %s%s\n", diff_staged ? "-s " : "",
5294 got_worktree_get_root_path(worktree));
5295 fprintf(diff_outfile, "commit - %s\n", id_str);
5296 fprintf(diff_outfile, "path + %s%s\n",
5297 got_worktree_get_root_path(worktree),
5298 diff_staged ? " (staged changes)" : "");
5299 *diff_header_shown = 1;
5302 if (diff_staged) {
5303 const char *label1 = NULL, *label2 = NULL;
5304 switch (ct->staged_status) {
5305 case GOT_STATUS_MODIFY:
5306 label1 = ct->path;
5307 label2 = ct->path;
5308 break;
5309 case GOT_STATUS_ADD:
5310 label2 = ct->path;
5311 break;
5312 case GOT_STATUS_DELETE:
5313 label1 = ct->path;
5314 break;
5315 default:
5316 return got_error(GOT_ERR_FILE_STATUS);
5318 fd1 = got_opentempfd();
5319 if (fd1 == -1) {
5320 err = got_error_from_errno("got_opentempfd");
5321 goto done;
5323 fd2 = got_opentempfd();
5324 if (fd2 == -1) {
5325 err = got_error_from_errno("got_opentempfd");
5326 goto done;
5328 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5329 fd1, fd2, ct->base_blob_id, ct->staged_blob_id,
5330 label1, label2, GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0,
5331 NULL, repo, diff_outfile);
5332 goto done;
5335 fd1 = got_opentempfd();
5336 if (fd1 == -1) {
5337 err = got_error_from_errno("got_opentempfd");
5338 goto done;
5341 if (ct->status != GOT_STATUS_ADD) {
5342 err = got_object_open_as_blob(&blob1, repo, ct->base_blob_id,
5343 8192, fd1);
5344 if (err)
5345 goto done;
5348 if (ct->status != GOT_STATUS_DELETE) {
5349 if (dirfd != -1) {
5350 fd = openat(dirfd, de_name,
5351 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5352 if (fd == -1) {
5353 if (!got_err_open_nofollow_on_symlink()) {
5354 err = got_error_from_errno2("openat",
5355 ct->ondisk_path);
5356 goto done;
5358 err = get_symlink_target_file(&fd, dirfd,
5359 de_name, ct->ondisk_path);
5360 if (err)
5361 goto done;
5363 } else {
5364 fd = open(ct->ondisk_path,
5365 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5366 if (fd == -1) {
5367 if (!got_err_open_nofollow_on_symlink()) {
5368 err = got_error_from_errno2("open",
5369 ct->ondisk_path);
5370 goto done;
5372 err = get_symlink_target_file(&fd, dirfd,
5373 de_name, ct->ondisk_path);
5374 if (err)
5375 goto done;
5378 if (fstatat(fd, ct->ondisk_path, &sb,
5379 AT_SYMLINK_NOFOLLOW) == -1) {
5380 err = got_error_from_errno2("fstatat", ct->ondisk_path);
5381 goto done;
5383 ondisk_file = fdopen(fd, "r");
5384 if (ondisk_file == NULL) {
5385 err = got_error_from_errno2("fdopen", ct->ondisk_path);
5386 goto done;
5388 fd = -1;
5389 f2_exists = 1;
5392 if (blob1) {
5393 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5394 f1, blob1);
5395 if (err)
5396 goto done;
5399 err = got_diff_blob_file(blob1, f1, size1, label1,
5400 ondisk_file ? ondisk_file : f2, f2_exists, &sb, ct->path,
5401 GOT_DIFF_ALGORITHM_PATIENCE, 3, 0, 0, NULL, diff_outfile);
5402 done:
5403 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5404 err = got_error_from_errno("close");
5405 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5406 err = got_error_from_errno("close");
5407 if (blob1)
5408 got_object_blob_close(blob1);
5409 if (fd != -1 && close(fd) == -1 && err == NULL)
5410 err = got_error_from_errno("close");
5411 if (ondisk_file && fclose(ondisk_file) == EOF && err == NULL)
5412 err = got_error_from_errno("fclose");
5413 return err;
5416 static const struct got_error *
5417 collect_commitables(void *arg, unsigned char status,
5418 unsigned char staged_status, const char *relpath,
5419 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
5420 struct got_object_id *commit_id, int dirfd, const char *de_name)
5422 struct collect_commitables_arg *a = arg;
5423 const struct got_error *err = NULL;
5424 struct got_commitable *ct = NULL;
5425 struct got_pathlist_entry *new = NULL;
5426 char *parent_path = NULL, *path = NULL;
5427 struct stat sb;
5429 if (a->have_staged_files) {
5430 if (staged_status != GOT_STATUS_MODIFY &&
5431 staged_status != GOT_STATUS_ADD &&
5432 staged_status != GOT_STATUS_DELETE)
5433 return NULL;
5434 } else {
5435 if (status == GOT_STATUS_CONFLICT && !a->commit_conflicts) {
5436 printf("C %s\n", relpath);
5437 return got_error(GOT_ERR_COMMIT_CONFLICT);
5440 if (status != GOT_STATUS_MODIFY &&
5441 status != GOT_STATUS_MODE_CHANGE &&
5442 status != GOT_STATUS_ADD &&
5443 status != GOT_STATUS_DELETE &&
5444 status != GOT_STATUS_CONFLICT)
5445 return NULL;
5448 if (asprintf(&path, "/%s", relpath) == -1) {
5449 err = got_error_from_errno("asprintf");
5450 goto done;
5452 if (strcmp(path, "/") == 0) {
5453 parent_path = strdup("");
5454 if (parent_path == NULL)
5455 return got_error_from_errno("strdup");
5456 } else {
5457 err = got_path_dirname(&parent_path, path);
5458 if (err)
5459 return err;
5462 ct = calloc(1, sizeof(*ct));
5463 if (ct == NULL) {
5464 err = got_error_from_errno("calloc");
5465 goto done;
5468 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
5469 relpath) == -1) {
5470 err = got_error_from_errno("asprintf");
5471 goto done;
5474 if (staged_status == GOT_STATUS_ADD ||
5475 staged_status == GOT_STATUS_MODIFY) {
5476 struct got_fileindex_entry *ie;
5477 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
5478 switch (got_fileindex_entry_staged_filetype_get(ie)) {
5479 case GOT_FILEIDX_MODE_REGULAR_FILE:
5480 case GOT_FILEIDX_MODE_BAD_SYMLINK:
5481 ct->mode = S_IFREG;
5482 break;
5483 case GOT_FILEIDX_MODE_SYMLINK:
5484 ct->mode = S_IFLNK;
5485 break;
5486 default:
5487 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
5488 goto done;
5490 ct->mode |= got_fileindex_entry_perms_get(ie);
5491 } else if (status != GOT_STATUS_DELETE &&
5492 staged_status != GOT_STATUS_DELETE) {
5493 if (dirfd != -1) {
5494 if (fstatat(dirfd, de_name, &sb,
5495 AT_SYMLINK_NOFOLLOW) == -1) {
5496 err = got_error_from_errno2("fstatat",
5497 ct->ondisk_path);
5498 goto done;
5500 } else if (lstat(ct->ondisk_path, &sb) == -1) {
5501 err = got_error_from_errno2("lstat", ct->ondisk_path);
5502 goto done;
5504 ct->mode = sb.st_mode;
5507 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5508 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5509 relpath) == -1) {
5510 err = got_error_from_errno("asprintf");
5511 goto done;
5514 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5515 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5516 int is_bad_symlink;
5517 char target_path[PATH_MAX];
5518 ssize_t target_len;
5519 target_len = readlink(ct->ondisk_path, target_path,
5520 sizeof(target_path));
5521 if (target_len == -1) {
5522 err = got_error_from_errno2("readlink",
5523 ct->ondisk_path);
5524 goto done;
5526 err = is_bad_symlink_target(&is_bad_symlink, target_path,
5527 target_len, ct->ondisk_path, a->worktree->root_path);
5528 if (err)
5529 goto done;
5530 if (is_bad_symlink) {
5531 err = got_error_path(ct->ondisk_path,
5532 GOT_ERR_BAD_SYMLINK);
5533 goto done;
5538 ct->status = status;
5539 ct->staged_status = staged_status;
5540 ct->blob_id = NULL; /* will be filled in when blob gets created */
5541 if (ct->status != GOT_STATUS_ADD &&
5542 ct->staged_status != GOT_STATUS_ADD) {
5543 ct->base_blob_id = got_object_id_dup(blob_id);
5544 if (ct->base_blob_id == NULL) {
5545 err = got_error_from_errno("got_object_id_dup");
5546 goto done;
5548 ct->base_commit_id = got_object_id_dup(commit_id);
5549 if (ct->base_commit_id == NULL) {
5550 err = got_error_from_errno("got_object_id_dup");
5551 goto done;
5554 if (ct->staged_status == GOT_STATUS_ADD ||
5555 ct->staged_status == GOT_STATUS_MODIFY) {
5556 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5557 if (ct->staged_blob_id == NULL) {
5558 err = got_error_from_errno("got_object_id_dup");
5559 goto done;
5562 ct->path = strdup(path);
5563 if (ct->path == NULL) {
5564 err = got_error_from_errno("strdup");
5565 goto done;
5567 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5568 if (err)
5569 goto done;
5571 if (a->diff_outfile && ct && new != NULL) {
5572 err = append_ct_diff(ct, &a->diff_header_shown,
5573 a->diff_outfile, a->f1, a->f2, dirfd, de_name,
5574 a->have_staged_files, a->repo, a->worktree);
5575 if (err)
5576 goto done;
5578 done:
5579 if (ct && (err || new == NULL))
5580 free_commitable(ct);
5581 free(parent_path);
5582 free(path);
5583 return err;
5586 static const struct got_error *write_tree(struct got_object_id **, int *,
5587 struct got_tree_object *, const char *, struct got_pathlist_head *,
5588 got_worktree_status_cb status_cb, void *status_arg,
5589 struct got_repository *);
5591 static const struct got_error *
5592 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5593 struct got_tree_entry *te, const char *parent_path,
5594 struct got_pathlist_head *commitable_paths,
5595 got_worktree_status_cb status_cb, void *status_arg,
5596 struct got_repository *repo)
5598 const struct got_error *err = NULL;
5599 struct got_tree_object *subtree;
5600 char *subpath;
5602 if (asprintf(&subpath, "%s%s%s", parent_path,
5603 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5604 return got_error_from_errno("asprintf");
5606 err = got_object_open_as_tree(&subtree, repo, &te->id);
5607 if (err)
5608 return err;
5610 err = write_tree(new_subtree_id, nentries, subtree, subpath,
5611 commitable_paths, status_cb, status_arg, repo);
5612 got_object_tree_close(subtree);
5613 free(subpath);
5614 return err;
5617 static const struct got_error *
5618 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5620 const struct got_error *err = NULL;
5621 char *ct_parent_path = NULL;
5623 *match = 0;
5625 if (strchr(ct->in_repo_path, '/') == NULL) {
5626 *match = got_path_is_root_dir(path);
5627 return NULL;
5630 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5631 if (err)
5632 return err;
5633 *match = (strcmp(path, ct_parent_path) == 0);
5634 free(ct_parent_path);
5635 return err;
5638 static mode_t
5639 get_ct_file_mode(struct got_commitable *ct)
5641 if (S_ISLNK(ct->mode))
5642 return S_IFLNK;
5644 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5647 static const struct got_error *
5648 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5649 struct got_tree_entry *te, struct got_commitable *ct)
5651 const struct got_error *err = NULL;
5653 *new_te = NULL;
5655 err = got_object_tree_entry_dup(new_te, te);
5656 if (err)
5657 goto done;
5659 (*new_te)->mode = get_ct_file_mode(ct);
5661 if (ct->staged_status == GOT_STATUS_MODIFY)
5662 memcpy(&(*new_te)->id, ct->staged_blob_id,
5663 sizeof((*new_te)->id));
5664 else
5665 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5666 done:
5667 if (err && *new_te) {
5668 free(*new_te);
5669 *new_te = NULL;
5671 return err;
5674 static const struct got_error *
5675 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5676 struct got_commitable *ct)
5678 const struct got_error *err = NULL;
5679 char *ct_name = NULL;
5681 *new_te = NULL;
5683 *new_te = calloc(1, sizeof(**new_te));
5684 if (*new_te == NULL)
5685 return got_error_from_errno("calloc");
5687 err = got_path_basename(&ct_name, ct->path);
5688 if (err)
5689 goto done;
5690 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5691 sizeof((*new_te)->name)) {
5692 err = got_error(GOT_ERR_NO_SPACE);
5693 goto done;
5696 (*new_te)->mode = get_ct_file_mode(ct);
5698 if (ct->staged_status == GOT_STATUS_ADD)
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 free(ct_name);
5705 if (err && *new_te) {
5706 free(*new_te);
5707 *new_te = NULL;
5709 return err;
5712 static const struct got_error *
5713 insert_tree_entry(struct got_tree_entry *new_te,
5714 struct got_pathlist_head *paths)
5716 const struct got_error *err = NULL;
5717 struct got_pathlist_entry *new_pe;
5719 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5720 if (err)
5721 return err;
5722 if (new_pe == NULL)
5723 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5724 return NULL;
5727 static const struct got_error *
5728 report_ct_status(struct got_commitable *ct,
5729 got_worktree_status_cb status_cb, void *status_arg)
5731 const char *ct_path = ct->path;
5732 unsigned char status;
5734 if (status_cb == NULL) /* no commit progress output desired */
5735 return NULL;
5737 while (ct_path[0] == '/')
5738 ct_path++;
5740 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5741 status = ct->staged_status;
5742 else
5743 status = ct->status;
5745 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5746 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5749 static const struct got_error *
5750 match_modified_subtree(int *modified, struct got_tree_entry *te,
5751 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5753 const struct got_error *err = NULL;
5754 struct got_pathlist_entry *pe;
5755 char *te_path;
5757 *modified = 0;
5759 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5760 got_path_is_root_dir(base_tree_path) ? "" : "/",
5761 te->name) == -1)
5762 return got_error_from_errno("asprintf");
5764 TAILQ_FOREACH(pe, commitable_paths, entry) {
5765 struct got_commitable *ct = pe->data;
5766 *modified = got_path_is_child(ct->in_repo_path, te_path,
5767 strlen(te_path));
5768 if (*modified)
5769 break;
5772 free(te_path);
5773 return err;
5776 static const struct got_error *
5777 match_deleted_or_modified_ct(struct got_commitable **ctp,
5778 struct got_tree_entry *te, const char *base_tree_path,
5779 struct got_pathlist_head *commitable_paths)
5781 const struct got_error *err = NULL;
5782 struct got_pathlist_entry *pe;
5784 *ctp = NULL;
5786 TAILQ_FOREACH(pe, commitable_paths, entry) {
5787 struct got_commitable *ct = pe->data;
5788 char *ct_name = NULL;
5789 int path_matches;
5791 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5792 if (ct->status != GOT_STATUS_MODIFY &&
5793 ct->status != GOT_STATUS_MODE_CHANGE &&
5794 ct->status != GOT_STATUS_DELETE &&
5795 ct->status != GOT_STATUS_CONFLICT)
5796 continue;
5797 } else {
5798 if (ct->staged_status != GOT_STATUS_MODIFY &&
5799 ct->staged_status != GOT_STATUS_DELETE)
5800 continue;
5803 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5804 continue;
5806 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5807 if (err)
5808 return err;
5809 if (!path_matches)
5810 continue;
5812 err = got_path_basename(&ct_name, pe->path);
5813 if (err)
5814 return err;
5816 if (strcmp(te->name, ct_name) != 0) {
5817 free(ct_name);
5818 continue;
5820 free(ct_name);
5822 *ctp = ct;
5823 break;
5826 return err;
5829 static const struct got_error *
5830 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5831 const char *child_path, const char *path_base_tree,
5832 struct got_pathlist_head *commitable_paths,
5833 got_worktree_status_cb status_cb, void *status_arg,
5834 struct got_repository *repo)
5836 const struct got_error *err = NULL;
5837 struct got_tree_entry *new_te;
5838 char *subtree_path;
5839 struct got_object_id *id = NULL;
5840 int nentries;
5842 *new_tep = NULL;
5844 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5845 got_path_is_root_dir(path_base_tree) ? "" : "/",
5846 child_path) == -1)
5847 return got_error_from_errno("asprintf");
5849 new_te = calloc(1, sizeof(*new_te));
5850 if (new_te == NULL)
5851 return got_error_from_errno("calloc");
5852 new_te->mode = S_IFDIR;
5854 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5855 sizeof(new_te->name)) {
5856 err = got_error(GOT_ERR_NO_SPACE);
5857 goto done;
5859 err = write_tree(&id, &nentries, NULL, subtree_path,
5860 commitable_paths, status_cb, status_arg, repo);
5861 if (err) {
5862 free(new_te);
5863 goto done;
5865 memcpy(&new_te->id, id, sizeof(new_te->id));
5866 done:
5867 free(id);
5868 free(subtree_path);
5869 if (err == NULL)
5870 *new_tep = new_te;
5871 return err;
5874 static const struct got_error *
5875 write_tree(struct got_object_id **new_tree_id, int *nentries,
5876 struct got_tree_object *base_tree, const char *path_base_tree,
5877 struct got_pathlist_head *commitable_paths,
5878 got_worktree_status_cb status_cb, void *status_arg,
5879 struct got_repository *repo)
5881 const struct got_error *err = NULL;
5882 struct got_pathlist_head paths;
5883 struct got_tree_entry *te, *new_te = NULL;
5884 struct got_pathlist_entry *pe;
5886 TAILQ_INIT(&paths);
5887 *nentries = 0;
5889 /* Insert, and recurse into, newly added entries first. */
5890 TAILQ_FOREACH(pe, commitable_paths, entry) {
5891 struct got_commitable *ct = pe->data;
5892 char *child_path = NULL, *slash;
5894 if ((ct->status != GOT_STATUS_ADD &&
5895 ct->staged_status != GOT_STATUS_ADD) ||
5896 (ct->flags & GOT_COMMITABLE_ADDED))
5897 continue;
5899 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5900 strlen(path_base_tree)))
5901 continue;
5903 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5904 ct->in_repo_path);
5905 if (err)
5906 goto done;
5908 slash = strchr(child_path, '/');
5909 if (slash == NULL) {
5910 err = alloc_added_blob_tree_entry(&new_te, ct);
5911 if (err)
5912 goto done;
5913 err = report_ct_status(ct, status_cb, status_arg);
5914 if (err)
5915 goto done;
5916 ct->flags |= GOT_COMMITABLE_ADDED;
5917 err = insert_tree_entry(new_te, &paths);
5918 if (err)
5919 goto done;
5920 (*nentries)++;
5921 } else {
5922 *slash = '\0'; /* trim trailing path components */
5923 if (base_tree == NULL ||
5924 got_object_tree_find_entry(base_tree, child_path)
5925 == NULL) {
5926 err = make_subtree_for_added_blob(&new_te,
5927 child_path, path_base_tree,
5928 commitable_paths, status_cb, status_arg,
5929 repo);
5930 if (err)
5931 goto done;
5932 err = insert_tree_entry(new_te, &paths);
5933 if (err)
5934 goto done;
5935 (*nentries)++;
5940 if (base_tree) {
5941 int i, nbase_entries;
5942 /* Handle modified and deleted entries. */
5943 nbase_entries = got_object_tree_get_nentries(base_tree);
5944 for (i = 0; i < nbase_entries; i++) {
5945 struct got_commitable *ct = NULL;
5947 te = got_object_tree_get_entry(base_tree, i);
5948 if (got_object_tree_entry_is_submodule(te)) {
5949 /* Entry is a submodule; just copy it. */
5950 err = got_object_tree_entry_dup(&new_te, te);
5951 if (err)
5952 goto done;
5953 err = insert_tree_entry(new_te, &paths);
5954 if (err)
5955 goto done;
5956 (*nentries)++;
5957 continue;
5960 if (S_ISDIR(te->mode)) {
5961 int modified;
5962 err = got_object_tree_entry_dup(&new_te, te);
5963 if (err)
5964 goto done;
5965 err = match_modified_subtree(&modified, te,
5966 path_base_tree, commitable_paths);
5967 if (err)
5968 goto done;
5969 /* Avoid recursion into unmodified subtrees. */
5970 if (modified) {
5971 struct got_object_id *new_id;
5972 int nsubentries;
5973 err = write_subtree(&new_id,
5974 &nsubentries, te,
5975 path_base_tree, commitable_paths,
5976 status_cb, status_arg, repo);
5977 if (err)
5978 goto done;
5979 if (nsubentries == 0) {
5980 /* All entries were deleted. */
5981 free(new_id);
5982 continue;
5984 memcpy(&new_te->id, new_id,
5985 sizeof(new_te->id));
5986 free(new_id);
5988 err = insert_tree_entry(new_te, &paths);
5989 if (err)
5990 goto done;
5991 (*nentries)++;
5992 continue;
5995 err = match_deleted_or_modified_ct(&ct, te,
5996 path_base_tree, commitable_paths);
5997 if (err)
5998 goto done;
5999 if (ct) {
6000 /* NB: Deleted entries get dropped here. */
6001 if (ct->status == GOT_STATUS_MODIFY ||
6002 ct->status == GOT_STATUS_MODE_CHANGE ||
6003 ct->status == GOT_STATUS_CONFLICT ||
6004 ct->staged_status == GOT_STATUS_MODIFY) {
6005 err = alloc_modified_blob_tree_entry(
6006 &new_te, te, ct);
6007 if (err)
6008 goto done;
6009 err = insert_tree_entry(new_te, &paths);
6010 if (err)
6011 goto done;
6012 (*nentries)++;
6014 err = report_ct_status(ct, status_cb,
6015 status_arg);
6016 if (err)
6017 goto done;
6018 } else {
6019 /* Entry is unchanged; just copy it. */
6020 err = got_object_tree_entry_dup(&new_te, te);
6021 if (err)
6022 goto done;
6023 err = insert_tree_entry(new_te, &paths);
6024 if (err)
6025 goto done;
6026 (*nentries)++;
6031 /* Write new list of entries; deleted entries have been dropped. */
6032 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
6033 done:
6034 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
6035 return err;
6038 static const struct got_error *
6039 update_fileindex_after_commit(struct got_worktree *worktree,
6040 struct got_pathlist_head *commitable_paths,
6041 struct got_object_id *new_base_commit_id,
6042 struct got_fileindex *fileindex, int have_staged_files)
6044 const struct got_error *err = NULL;
6045 struct got_pathlist_entry *pe;
6046 char *relpath = NULL;
6048 TAILQ_FOREACH(pe, commitable_paths, entry) {
6049 struct got_fileindex_entry *ie;
6050 struct got_commitable *ct = pe->data;
6052 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6054 err = got_path_skip_common_ancestor(&relpath,
6055 worktree->root_path, ct->ondisk_path);
6056 if (err)
6057 goto done;
6059 if (ie) {
6060 if (ct->status == GOT_STATUS_DELETE ||
6061 ct->staged_status == GOT_STATUS_DELETE) {
6062 got_fileindex_entry_remove(fileindex, ie);
6063 } else if (ct->staged_status == GOT_STATUS_ADD ||
6064 ct->staged_status == GOT_STATUS_MODIFY) {
6065 got_fileindex_entry_stage_set(ie,
6066 GOT_FILEIDX_STAGE_NONE);
6067 got_fileindex_entry_staged_filetype_set(ie, 0);
6069 err = got_fileindex_entry_update(ie,
6070 worktree->root_fd, relpath,
6071 ct->staged_blob_id->sha1,
6072 new_base_commit_id->sha1,
6073 !have_staged_files);
6074 } else
6075 err = got_fileindex_entry_update(ie,
6076 worktree->root_fd, relpath,
6077 ct->blob_id->sha1,
6078 new_base_commit_id->sha1,
6079 !have_staged_files);
6080 } else {
6081 err = got_fileindex_entry_alloc(&ie, pe->path);
6082 if (err)
6083 goto done;
6084 err = got_fileindex_entry_update(ie,
6085 worktree->root_fd, relpath, ct->blob_id->sha1,
6086 new_base_commit_id->sha1, 1);
6087 if (err) {
6088 got_fileindex_entry_free(ie);
6089 goto done;
6091 err = got_fileindex_entry_add(fileindex, ie);
6092 if (err) {
6093 got_fileindex_entry_free(ie);
6094 goto done;
6097 free(relpath);
6098 relpath = NULL;
6100 done:
6101 free(relpath);
6102 return err;
6106 static const struct got_error *
6107 check_out_of_date(const char *in_repo_path, unsigned char status,
6108 unsigned char staged_status, struct got_object_id *base_blob_id,
6109 struct got_object_id *base_commit_id,
6110 struct got_object_id *head_commit_id, struct got_repository *repo,
6111 int ood_errcode)
6113 const struct got_error *err = NULL;
6114 struct got_commit_object *commit = NULL;
6115 struct got_object_id *id = NULL;
6117 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
6118 /* Trivial case: base commit == head commit */
6119 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
6120 return NULL;
6122 * Ensure file content which local changes were based
6123 * on matches file content in the branch head.
6125 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6126 if (err)
6127 goto done;
6128 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6129 if (err) {
6130 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6131 err = got_error(ood_errcode);
6132 goto done;
6133 } else if (got_object_id_cmp(id, base_blob_id) != 0)
6134 err = got_error(ood_errcode);
6135 } else {
6136 /* Require that added files don't exist in the branch head. */
6137 err = got_object_open_as_commit(&commit, repo, head_commit_id);
6138 if (err)
6139 goto done;
6140 err = got_object_id_by_path(&id, repo, commit, in_repo_path);
6141 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
6142 goto done;
6143 err = id ? got_error(ood_errcode) : NULL;
6145 done:
6146 free(id);
6147 if (commit)
6148 got_object_commit_close(commit);
6149 return err;
6152 static const struct got_error *
6153 commit_worktree(struct got_object_id **new_commit_id,
6154 struct got_pathlist_head *commitable_paths,
6155 struct got_object_id *head_commit_id,
6156 struct got_object_id *parent_id2,
6157 struct got_worktree *worktree,
6158 const char *author, const char *committer, char *diff_path,
6159 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6160 got_worktree_status_cb status_cb, void *status_arg,
6161 struct got_repository *repo)
6163 const struct got_error *err = NULL, *unlockerr = NULL;
6164 struct got_pathlist_entry *pe;
6165 const char *head_ref_name = NULL;
6166 struct got_commit_object *head_commit = NULL;
6167 struct got_reference *head_ref2 = NULL;
6168 struct got_object_id *head_commit_id2 = NULL;
6169 struct got_tree_object *head_tree = NULL;
6170 struct got_object_id *new_tree_id = NULL;
6171 int nentries, nparents = 0;
6172 struct got_object_id_queue parent_ids;
6173 struct got_object_qid *pid = NULL;
6174 char *logmsg = NULL;
6175 time_t timestamp;
6177 *new_commit_id = NULL;
6179 STAILQ_INIT(&parent_ids);
6181 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
6182 if (err)
6183 goto done;
6185 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
6186 if (err)
6187 goto done;
6189 if (commit_msg_cb != NULL) {
6190 err = commit_msg_cb(commitable_paths, diff_path,
6191 &logmsg, commit_arg);
6192 if (err)
6193 goto done;
6196 if (logmsg == NULL || strlen(logmsg) == 0) {
6197 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
6198 goto done;
6201 /* Create blobs from added and modified files and record their IDs. */
6202 TAILQ_FOREACH(pe, commitable_paths, entry) {
6203 struct got_commitable *ct = pe->data;
6204 char *ondisk_path;
6206 /* Blobs for staged files already exist. */
6207 if (ct->staged_status == GOT_STATUS_ADD ||
6208 ct->staged_status == GOT_STATUS_MODIFY)
6209 continue;
6211 if (ct->status != GOT_STATUS_ADD &&
6212 ct->status != GOT_STATUS_MODIFY &&
6213 ct->status != GOT_STATUS_MODE_CHANGE &&
6214 ct->status != GOT_STATUS_CONFLICT)
6215 continue;
6217 if (asprintf(&ondisk_path, "%s/%s",
6218 worktree->root_path, pe->path) == -1) {
6219 err = got_error_from_errno("asprintf");
6220 goto done;
6222 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
6223 free(ondisk_path);
6224 if (err)
6225 goto done;
6228 /* Recursively write new tree objects. */
6229 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
6230 commitable_paths, status_cb, status_arg, repo);
6231 if (err)
6232 goto done;
6234 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
6235 if (err)
6236 goto done;
6237 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6238 nparents++;
6239 if (parent_id2) {
6240 err = got_object_qid_alloc(&pid, parent_id2);
6241 if (err)
6242 goto done;
6243 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
6244 nparents++;
6246 timestamp = time(NULL);
6247 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
6248 nparents, author, timestamp, committer, timestamp, logmsg, repo);
6249 if (logmsg != NULL)
6250 free(logmsg);
6251 if (err)
6252 goto done;
6254 /* Check if a concurrent commit to our branch has occurred. */
6255 head_ref_name = got_worktree_get_head_ref_name(worktree);
6256 if (head_ref_name == NULL) {
6257 err = got_error_from_errno("got_worktree_get_head_ref_name");
6258 goto done;
6260 /* Lock the reference here to prevent concurrent modification. */
6261 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
6262 if (err)
6263 goto done;
6264 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
6265 if (err)
6266 goto done;
6267 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
6268 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
6269 goto done;
6271 /* Update branch head in repository. */
6272 err = got_ref_change_ref(head_ref2, *new_commit_id);
6273 if (err)
6274 goto done;
6275 err = got_ref_write(head_ref2, repo);
6276 if (err)
6277 goto done;
6279 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
6280 if (err)
6281 goto done;
6283 err = ref_base_commit(worktree, repo);
6284 if (err)
6285 goto done;
6286 done:
6287 got_object_id_queue_free(&parent_ids);
6288 if (head_tree)
6289 got_object_tree_close(head_tree);
6290 if (head_commit)
6291 got_object_commit_close(head_commit);
6292 free(head_commit_id2);
6293 if (head_ref2) {
6294 unlockerr = got_ref_unlock(head_ref2);
6295 if (unlockerr && err == NULL)
6296 err = unlockerr;
6297 got_ref_close(head_ref2);
6299 return err;
6302 static const struct got_error *
6303 check_path_is_commitable(const char *path,
6304 struct got_pathlist_head *commitable_paths)
6306 struct got_pathlist_entry *cpe = NULL;
6307 size_t path_len = strlen(path);
6309 TAILQ_FOREACH(cpe, commitable_paths, entry) {
6310 struct got_commitable *ct = cpe->data;
6311 const char *ct_path = ct->path;
6313 while (ct_path[0] == '/')
6314 ct_path++;
6316 if (strcmp(path, ct_path) == 0 ||
6317 got_path_is_child(ct_path, path, path_len))
6318 break;
6321 if (cpe == NULL)
6322 return got_error_path(path, GOT_ERR_BAD_PATH);
6324 return NULL;
6327 static const struct got_error *
6328 check_staged_file(void *arg, struct got_fileindex_entry *ie)
6330 int *have_staged_files = arg;
6332 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
6333 *have_staged_files = 1;
6334 return got_error(GOT_ERR_CANCELLED);
6337 return NULL;
6340 static const struct got_error *
6341 check_non_staged_files(struct got_fileindex *fileindex,
6342 struct got_pathlist_head *paths)
6344 struct got_pathlist_entry *pe;
6345 struct got_fileindex_entry *ie;
6347 TAILQ_FOREACH(pe, paths, entry) {
6348 if (pe->path[0] == '\0')
6349 continue;
6350 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
6351 if (ie == NULL)
6352 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
6353 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
6354 return got_error_path(pe->path,
6355 GOT_ERR_FILE_NOT_STAGED);
6358 return NULL;
6361 const struct got_error *
6362 got_worktree_commit(struct got_object_id **new_commit_id,
6363 struct got_worktree *worktree, struct got_pathlist_head *paths,
6364 const char *author, const char *committer, int allow_bad_symlinks,
6365 int show_diff, int commit_conflicts,
6366 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
6367 got_worktree_status_cb status_cb, void *status_arg,
6368 struct got_repository *repo)
6370 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
6371 struct got_fileindex *fileindex = NULL;
6372 char *fileindex_path = NULL;
6373 struct got_pathlist_head commitable_paths;
6374 struct collect_commitables_arg cc_arg;
6375 struct got_pathlist_entry *pe;
6376 struct got_reference *head_ref = NULL;
6377 struct got_object_id *head_commit_id = NULL;
6378 char *diff_path = NULL;
6379 int have_staged_files = 0;
6381 *new_commit_id = NULL;
6383 memset(&cc_arg, 0, sizeof(cc_arg));
6384 TAILQ_INIT(&commitable_paths);
6386 err = lock_worktree(worktree, LOCK_EX);
6387 if (err)
6388 goto done;
6390 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6391 if (err)
6392 goto done;
6394 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6395 if (err)
6396 goto done;
6398 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6399 if (err)
6400 goto done;
6402 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
6403 &have_staged_files);
6404 if (err && err->code != GOT_ERR_CANCELLED)
6405 goto done;
6406 if (have_staged_files) {
6407 err = check_non_staged_files(fileindex, paths);
6408 if (err)
6409 goto done;
6412 cc_arg.commitable_paths = &commitable_paths;
6413 cc_arg.worktree = worktree;
6414 cc_arg.fileindex = fileindex;
6415 cc_arg.repo = repo;
6416 cc_arg.have_staged_files = have_staged_files;
6417 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
6418 cc_arg.diff_header_shown = 0;
6419 cc_arg.commit_conflicts = commit_conflicts;
6420 if (show_diff) {
6421 err = got_opentemp_named(&diff_path, &cc_arg.diff_outfile,
6422 GOT_TMPDIR_STR "/got", ".diff");
6423 if (err)
6424 goto done;
6425 cc_arg.f1 = got_opentemp();
6426 if (cc_arg.f1 == NULL) {
6427 err = got_error_from_errno("got_opentemp");
6428 goto done;
6430 cc_arg.f2 = got_opentemp();
6431 if (cc_arg.f2 == NULL) {
6432 err = got_error_from_errno("got_opentemp");
6433 goto done;
6437 TAILQ_FOREACH(pe, paths, entry) {
6438 err = worktree_status(worktree, pe->path, fileindex, repo,
6439 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6440 if (err)
6441 goto done;
6444 if (show_diff) {
6445 if (fflush(cc_arg.diff_outfile) == EOF) {
6446 err = got_error_from_errno("fflush");
6447 goto done;
6451 if (TAILQ_EMPTY(&commitable_paths)) {
6452 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6453 goto done;
6456 TAILQ_FOREACH(pe, paths, entry) {
6457 err = check_path_is_commitable(pe->path, &commitable_paths);
6458 if (err)
6459 goto done;
6462 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6463 struct got_commitable *ct = pe->data;
6464 const char *ct_path = ct->in_repo_path;
6466 while (ct_path[0] == '/')
6467 ct_path++;
6468 err = check_out_of_date(ct_path, ct->status,
6469 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
6470 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
6471 if (err)
6472 goto done;
6476 err = commit_worktree(new_commit_id, &commitable_paths,
6477 head_commit_id, NULL, worktree, author, committer,
6478 (diff_path && cc_arg.diff_header_shown) ? diff_path : NULL,
6479 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
6480 if (err)
6481 goto done;
6483 err = update_fileindex_after_commit(worktree, &commitable_paths,
6484 *new_commit_id, fileindex, have_staged_files);
6485 sync_err = sync_fileindex(fileindex, fileindex_path);
6486 if (sync_err && err == NULL)
6487 err = sync_err;
6488 done:
6489 if (fileindex)
6490 got_fileindex_free(fileindex);
6491 free(fileindex_path);
6492 unlockerr = lock_worktree(worktree, LOCK_SH);
6493 if (unlockerr && err == NULL)
6494 err = unlockerr;
6495 TAILQ_FOREACH(pe, &commitable_paths, entry) {
6496 struct got_commitable *ct = pe->data;
6498 free_commitable(ct);
6500 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
6501 if (diff_path && unlink(diff_path) == -1 && err == NULL)
6502 err = got_error_from_errno2("unlink", diff_path);
6503 free(diff_path);
6504 if (cc_arg.diff_outfile && fclose(cc_arg.diff_outfile) == EOF &&
6505 err == NULL)
6506 err = got_error_from_errno("fclose");
6507 return err;
6510 const char *
6511 got_commitable_get_path(struct got_commitable *ct)
6513 return ct->path;
6516 unsigned int
6517 got_commitable_get_status(struct got_commitable *ct)
6519 return ct->status;
6522 struct check_rebase_ok_arg {
6523 struct got_worktree *worktree;
6524 struct got_repository *repo;
6527 static const struct got_error *
6528 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
6530 const struct got_error *err = NULL;
6531 struct check_rebase_ok_arg *a = arg;
6532 unsigned char status;
6533 struct stat sb;
6534 char *ondisk_path;
6536 /* Reject rebase of a work tree with mixed base commits. */
6537 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
6538 SHA1_DIGEST_LENGTH))
6539 return got_error(GOT_ERR_MIXED_COMMITS);
6541 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
6542 == -1)
6543 return got_error_from_errno("asprintf");
6545 /* Reject rebase of a work tree with modified or staged files. */
6546 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
6547 free(ondisk_path);
6548 if (err)
6549 return err;
6551 if (status != GOT_STATUS_NO_CHANGE)
6552 return got_error(GOT_ERR_MODIFIED);
6553 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
6554 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
6556 return NULL;
6559 const struct got_error *
6560 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
6561 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
6562 struct got_worktree *worktree, struct got_reference *branch,
6563 struct got_repository *repo)
6565 const struct got_error *err = NULL;
6566 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6567 char *branch_ref_name = NULL;
6568 char *fileindex_path = NULL;
6569 struct check_rebase_ok_arg ok_arg;
6570 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6571 struct got_object_id *wt_branch_tip = NULL;
6573 *new_base_branch_ref = NULL;
6574 *tmp_branch = NULL;
6575 *fileindex = NULL;
6577 err = lock_worktree(worktree, LOCK_EX);
6578 if (err)
6579 return err;
6581 err = open_fileindex(fileindex, &fileindex_path, worktree);
6582 if (err)
6583 goto done;
6585 ok_arg.worktree = worktree;
6586 ok_arg.repo = repo;
6587 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6588 &ok_arg);
6589 if (err)
6590 goto done;
6592 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6593 if (err)
6594 goto done;
6596 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6597 if (err)
6598 goto done;
6600 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6601 if (err)
6602 goto done;
6604 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6605 0);
6606 if (err)
6607 goto done;
6609 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6610 if (err)
6611 goto done;
6612 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6613 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6614 goto done;
6617 err = got_ref_alloc_symref(new_base_branch_ref,
6618 new_base_branch_ref_name, wt_branch);
6619 if (err)
6620 goto done;
6621 err = got_ref_write(*new_base_branch_ref, repo);
6622 if (err)
6623 goto done;
6625 /* TODO Lock original branch's ref while rebasing? */
6627 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6628 if (err)
6629 goto done;
6631 err = got_ref_write(branch_ref, repo);
6632 if (err)
6633 goto done;
6635 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6636 worktree->base_commit_id);
6637 if (err)
6638 goto done;
6639 err = got_ref_write(*tmp_branch, repo);
6640 if (err)
6641 goto done;
6643 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6644 if (err)
6645 goto done;
6646 done:
6647 free(fileindex_path);
6648 free(tmp_branch_name);
6649 free(new_base_branch_ref_name);
6650 free(branch_ref_name);
6651 if (branch_ref)
6652 got_ref_close(branch_ref);
6653 if (wt_branch)
6654 got_ref_close(wt_branch);
6655 free(wt_branch_tip);
6656 if (err) {
6657 if (*new_base_branch_ref) {
6658 got_ref_close(*new_base_branch_ref);
6659 *new_base_branch_ref = NULL;
6661 if (*tmp_branch) {
6662 got_ref_close(*tmp_branch);
6663 *tmp_branch = NULL;
6665 if (*fileindex) {
6666 got_fileindex_free(*fileindex);
6667 *fileindex = NULL;
6669 lock_worktree(worktree, LOCK_SH);
6671 return err;
6674 const struct got_error *
6675 got_worktree_rebase_continue(struct got_object_id **commit_id,
6676 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6677 struct got_reference **branch, struct got_fileindex **fileindex,
6678 struct got_worktree *worktree, struct got_repository *repo)
6680 const struct got_error *err;
6681 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6682 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6683 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6684 char *fileindex_path = NULL;
6685 int have_staged_files = 0;
6687 *commit_id = NULL;
6688 *new_base_branch = NULL;
6689 *tmp_branch = NULL;
6690 *branch = NULL;
6691 *fileindex = NULL;
6693 err = lock_worktree(worktree, LOCK_EX);
6694 if (err)
6695 return err;
6697 err = open_fileindex(fileindex, &fileindex_path, worktree);
6698 if (err)
6699 goto done;
6701 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6702 &have_staged_files);
6703 if (err && err->code != GOT_ERR_CANCELLED)
6704 goto done;
6705 if (have_staged_files) {
6706 err = got_error(GOT_ERR_STAGED_PATHS);
6707 goto done;
6710 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6711 if (err)
6712 goto done;
6714 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6715 if (err)
6716 goto done;
6718 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6719 if (err)
6720 goto done;
6722 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6723 if (err)
6724 goto done;
6726 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6727 if (err)
6728 goto done;
6730 err = got_ref_open(branch, repo,
6731 got_ref_get_symref_target(branch_ref), 0);
6732 if (err)
6733 goto done;
6735 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6736 if (err)
6737 goto done;
6739 err = got_ref_resolve(commit_id, repo, commit_ref);
6740 if (err)
6741 goto done;
6743 err = got_ref_open(new_base_branch, repo,
6744 new_base_branch_ref_name, 0);
6745 if (err)
6746 goto done;
6748 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6749 if (err)
6750 goto done;
6751 done:
6752 free(commit_ref_name);
6753 free(branch_ref_name);
6754 free(fileindex_path);
6755 if (commit_ref)
6756 got_ref_close(commit_ref);
6757 if (branch_ref)
6758 got_ref_close(branch_ref);
6759 if (err) {
6760 free(*commit_id);
6761 *commit_id = NULL;
6762 if (*tmp_branch) {
6763 got_ref_close(*tmp_branch);
6764 *tmp_branch = NULL;
6766 if (*new_base_branch) {
6767 got_ref_close(*new_base_branch);
6768 *new_base_branch = NULL;
6770 if (*branch) {
6771 got_ref_close(*branch);
6772 *branch = NULL;
6774 if (*fileindex) {
6775 got_fileindex_free(*fileindex);
6776 *fileindex = NULL;
6778 lock_worktree(worktree, LOCK_SH);
6780 return err;
6783 const struct got_error *
6784 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6786 const struct got_error *err;
6787 char *tmp_branch_name = NULL;
6789 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6790 if (err)
6791 return err;
6793 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6794 free(tmp_branch_name);
6795 return NULL;
6798 static const struct got_error *
6799 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6800 const char *diff_path, char **logmsg, void *arg)
6802 *logmsg = arg;
6803 return NULL;
6806 static const struct got_error *
6807 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6808 const char *path, struct got_object_id *blob_id,
6809 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6810 int dirfd, const char *de_name)
6812 return NULL;
6815 struct collect_merged_paths_arg {
6816 got_worktree_checkout_cb progress_cb;
6817 void *progress_arg;
6818 struct got_pathlist_head *merged_paths;
6821 static const struct got_error *
6822 collect_merged_paths(void *arg, unsigned char status, const char *path)
6824 const struct got_error *err;
6825 struct collect_merged_paths_arg *a = arg;
6826 char *p;
6827 struct got_pathlist_entry *new;
6829 err = (*a->progress_cb)(a->progress_arg, status, path);
6830 if (err)
6831 return err;
6833 if (status != GOT_STATUS_MERGE &&
6834 status != GOT_STATUS_ADD &&
6835 status != GOT_STATUS_DELETE &&
6836 status != GOT_STATUS_CONFLICT)
6837 return NULL;
6839 p = strdup(path);
6840 if (p == NULL)
6841 return got_error_from_errno("strdup");
6843 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6844 if (err || new == NULL)
6845 free(p);
6846 return err;
6849 static const struct got_error *
6850 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6851 int is_rebase, struct got_repository *repo)
6853 const struct got_error *err;
6854 struct got_reference *commit_ref = NULL;
6856 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6857 if (err) {
6858 if (err->code != GOT_ERR_NOT_REF)
6859 goto done;
6860 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6861 if (err)
6862 goto done;
6863 err = got_ref_write(commit_ref, repo);
6864 if (err)
6865 goto done;
6866 } else if (is_rebase) {
6867 struct got_object_id *stored_id;
6868 int cmp;
6870 err = got_ref_resolve(&stored_id, repo, commit_ref);
6871 if (err)
6872 goto done;
6873 cmp = got_object_id_cmp(commit_id, stored_id);
6874 free(stored_id);
6875 if (cmp != 0) {
6876 err = got_error(GOT_ERR_REBASE_COMMITID);
6877 goto done;
6880 done:
6881 if (commit_ref)
6882 got_ref_close(commit_ref);
6883 return err;
6886 static const struct got_error *
6887 rebase_merge_files(struct got_pathlist_head *merged_paths,
6888 const char *commit_ref_name, struct got_worktree *worktree,
6889 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6890 struct got_object_id *commit_id, struct got_repository *repo,
6891 got_worktree_checkout_cb progress_cb, void *progress_arg,
6892 got_cancel_cb cancel_cb, void *cancel_arg)
6894 const struct got_error *err;
6895 struct got_reference *commit_ref = NULL;
6896 struct collect_merged_paths_arg cmp_arg;
6897 char *fileindex_path;
6899 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6901 err = get_fileindex_path(&fileindex_path, worktree);
6902 if (err)
6903 return err;
6905 cmp_arg.progress_cb = progress_cb;
6906 cmp_arg.progress_arg = progress_arg;
6907 cmp_arg.merged_paths = merged_paths;
6908 err = merge_files(worktree, fileindex, fileindex_path,
6909 parent_commit_id, commit_id, repo, collect_merged_paths,
6910 &cmp_arg, cancel_cb, cancel_arg);
6911 if (commit_ref)
6912 got_ref_close(commit_ref);
6913 return err;
6916 const struct got_error *
6917 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6918 struct got_worktree *worktree, struct got_fileindex *fileindex,
6919 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6920 struct got_repository *repo,
6921 got_worktree_checkout_cb progress_cb, void *progress_arg,
6922 got_cancel_cb cancel_cb, void *cancel_arg)
6924 const struct got_error *err;
6925 char *commit_ref_name;
6927 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6928 if (err)
6929 return err;
6931 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6932 if (err)
6933 goto done;
6935 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6936 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6937 progress_arg, cancel_cb, cancel_arg);
6938 done:
6939 free(commit_ref_name);
6940 return err;
6943 const struct got_error *
6944 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6945 struct got_worktree *worktree, struct got_fileindex *fileindex,
6946 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6947 struct got_repository *repo,
6948 got_worktree_checkout_cb progress_cb, void *progress_arg,
6949 got_cancel_cb cancel_cb, void *cancel_arg)
6951 const struct got_error *err;
6952 char *commit_ref_name;
6954 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6955 if (err)
6956 return err;
6958 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6959 if (err)
6960 goto done;
6962 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6963 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6964 progress_arg, cancel_cb, cancel_arg);
6965 done:
6966 free(commit_ref_name);
6967 return err;
6970 static const struct got_error *
6971 rebase_commit(struct got_object_id **new_commit_id,
6972 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6973 struct got_worktree *worktree, struct got_fileindex *fileindex,
6974 struct got_reference *tmp_branch, const char *committer,
6975 struct got_commit_object *orig_commit, const char *new_logmsg,
6976 int allow_conflict, struct got_repository *repo)
6978 const struct got_error *err, *sync_err;
6979 struct got_pathlist_head commitable_paths;
6980 struct collect_commitables_arg cc_arg;
6981 char *fileindex_path = NULL;
6982 struct got_reference *head_ref = NULL;
6983 struct got_object_id *head_commit_id = NULL;
6984 char *logmsg = NULL;
6986 memset(&cc_arg, 0, sizeof(cc_arg));
6987 TAILQ_INIT(&commitable_paths);
6988 *new_commit_id = NULL;
6990 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6992 err = get_fileindex_path(&fileindex_path, worktree);
6993 if (err)
6994 return err;
6996 cc_arg.commitable_paths = &commitable_paths;
6997 cc_arg.worktree = worktree;
6998 cc_arg.repo = repo;
6999 cc_arg.have_staged_files = 0;
7000 cc_arg.commit_conflicts = allow_conflict;
7002 * If possible get the status of individual files directly to
7003 * avoid crawling the entire work tree once per rebased commit.
7005 * Ideally, merged_paths would contain a list of commitables
7006 * we could use so we could skip worktree_status() entirely.
7007 * However, we would then need carefully keep track of cumulative
7008 * effects of operations such as file additions and deletions
7009 * in 'got histedit -f' (folding multiple commits into one),
7010 * and this extra complexity is not really worth it.
7012 if (merged_paths) {
7013 struct got_pathlist_entry *pe;
7014 TAILQ_FOREACH(pe, merged_paths, entry) {
7015 err = worktree_status(worktree, pe->path, fileindex,
7016 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
7017 0);
7018 if (err)
7019 goto done;
7021 } else {
7022 err = worktree_status(worktree, "", fileindex, repo,
7023 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7024 if (err)
7025 goto done;
7028 if (TAILQ_EMPTY(&commitable_paths)) {
7029 /* No-op change; commit will be elided. */
7030 err = got_ref_delete(commit_ref, repo);
7031 if (err)
7032 goto done;
7033 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
7034 goto done;
7037 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7038 if (err)
7039 goto done;
7041 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7042 if (err)
7043 goto done;
7045 if (new_logmsg) {
7046 logmsg = strdup(new_logmsg);
7047 if (logmsg == NULL) {
7048 err = got_error_from_errno("strdup");
7049 goto done;
7051 } else {
7052 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
7053 if (err)
7054 goto done;
7057 /* NB: commit_worktree will call free(logmsg) */
7058 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
7059 NULL, worktree, got_object_commit_get_author(orig_commit),
7060 committer ? committer :
7061 got_object_commit_get_committer(orig_commit), NULL,
7062 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
7063 if (err)
7064 goto done;
7066 err = got_ref_change_ref(tmp_branch, *new_commit_id);
7067 if (err)
7068 goto done;
7070 err = got_ref_delete(commit_ref, repo);
7071 if (err)
7072 goto done;
7074 err = update_fileindex_after_commit(worktree, &commitable_paths,
7075 *new_commit_id, fileindex, 0);
7076 sync_err = sync_fileindex(fileindex, fileindex_path);
7077 if (sync_err && err == NULL)
7078 err = sync_err;
7079 done:
7080 free(fileindex_path);
7081 free(head_commit_id);
7082 if (head_ref)
7083 got_ref_close(head_ref);
7084 if (err) {
7085 free(*new_commit_id);
7086 *new_commit_id = NULL;
7088 return err;
7091 const struct got_error *
7092 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
7093 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7094 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7095 const char *committer, struct got_commit_object *orig_commit,
7096 struct got_object_id *orig_commit_id, int allow_conflict,
7097 struct got_repository *repo)
7099 const struct got_error *err;
7100 char *commit_ref_name;
7101 struct got_reference *commit_ref = NULL;
7102 struct got_object_id *commit_id = NULL;
7104 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7105 if (err)
7106 return err;
7108 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7109 if (err)
7110 goto done;
7111 err = got_ref_resolve(&commit_id, repo, commit_ref);
7112 if (err)
7113 goto done;
7114 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
7115 err = got_error(GOT_ERR_REBASE_COMMITID);
7116 goto done;
7119 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7120 worktree, fileindex, tmp_branch, committer, orig_commit,
7121 NULL, allow_conflict, repo);
7122 done:
7123 if (commit_ref)
7124 got_ref_close(commit_ref);
7125 free(commit_ref_name);
7126 free(commit_id);
7127 return err;
7130 const struct got_error *
7131 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
7132 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
7133 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7134 const char *committer, struct got_commit_object *orig_commit,
7135 struct got_object_id *orig_commit_id, const char *new_logmsg,
7136 int allow_conflict, struct got_repository *repo)
7138 const struct got_error *err;
7139 char *commit_ref_name;
7140 struct got_reference *commit_ref = NULL;
7142 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7143 if (err)
7144 return err;
7146 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7147 if (err)
7148 goto done;
7150 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
7151 worktree, fileindex, tmp_branch, committer, orig_commit,
7152 new_logmsg, allow_conflict, repo);
7153 done:
7154 if (commit_ref)
7155 got_ref_close(commit_ref);
7156 free(commit_ref_name);
7157 return err;
7160 const struct got_error *
7161 got_worktree_rebase_postpone(struct got_worktree *worktree,
7162 struct got_fileindex *fileindex)
7164 if (fileindex)
7165 got_fileindex_free(fileindex);
7166 return lock_worktree(worktree, LOCK_SH);
7169 static const struct got_error *
7170 delete_ref(const char *name, struct got_repository *repo)
7172 const struct got_error *err;
7173 struct got_reference *ref;
7175 err = got_ref_open(&ref, repo, name, 0);
7176 if (err) {
7177 if (err->code == GOT_ERR_NOT_REF)
7178 return NULL;
7179 return err;
7182 err = got_ref_delete(ref, repo);
7183 got_ref_close(ref);
7184 return err;
7187 static const struct got_error *
7188 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
7190 const struct got_error *err;
7191 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
7192 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7194 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
7195 if (err)
7196 goto done;
7197 err = delete_ref(tmp_branch_name, repo);
7198 if (err)
7199 goto done;
7201 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
7202 if (err)
7203 goto done;
7204 err = delete_ref(new_base_branch_ref_name, repo);
7205 if (err)
7206 goto done;
7208 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
7209 if (err)
7210 goto done;
7211 err = delete_ref(branch_ref_name, repo);
7212 if (err)
7213 goto done;
7215 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
7216 if (err)
7217 goto done;
7218 err = delete_ref(commit_ref_name, repo);
7219 if (err)
7220 goto done;
7222 done:
7223 free(tmp_branch_name);
7224 free(new_base_branch_ref_name);
7225 free(branch_ref_name);
7226 free(commit_ref_name);
7227 return err;
7230 static const struct got_error *
7231 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
7232 struct got_object_id *new_commit_id, struct got_repository *repo)
7234 const struct got_error *err;
7235 struct got_reference *ref = NULL;
7236 struct got_object_id *old_commit_id = NULL;
7237 const char *branch_name = NULL;
7238 char *new_id_str = NULL;
7239 char *refname = NULL;
7241 branch_name = got_ref_get_name(branch);
7242 if (strncmp(branch_name, "refs/heads/", 11) != 0)
7243 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
7244 branch_name += 11;
7246 err = got_object_id_str(&new_id_str, new_commit_id);
7247 if (err)
7248 return err;
7250 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
7251 new_id_str) == -1) {
7252 err = got_error_from_errno("asprintf");
7253 goto done;
7256 err = got_ref_resolve(&old_commit_id, repo, branch);
7257 if (err)
7258 goto done;
7260 err = got_ref_alloc(&ref, refname, old_commit_id);
7261 if (err)
7262 goto done;
7264 err = got_ref_write(ref, repo);
7265 done:
7266 free(new_id_str);
7267 free(refname);
7268 free(old_commit_id);
7269 if (ref)
7270 got_ref_close(ref);
7271 return err;
7274 const struct got_error *
7275 got_worktree_rebase_complete(struct got_worktree *worktree,
7276 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7277 struct got_reference *rebased_branch, struct got_repository *repo,
7278 int create_backup)
7280 const struct got_error *err, *unlockerr, *sync_err;
7281 struct got_object_id *new_head_commit_id = NULL;
7282 char *fileindex_path = NULL;
7284 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7285 if (err)
7286 return err;
7288 if (create_backup) {
7289 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
7290 rebased_branch, new_head_commit_id, repo);
7291 if (err)
7292 goto done;
7295 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
7296 if (err)
7297 goto done;
7299 err = got_ref_write(rebased_branch, repo);
7300 if (err)
7301 goto done;
7303 err = got_worktree_set_head_ref(worktree, rebased_branch);
7304 if (err)
7305 goto done;
7307 err = delete_rebase_refs(worktree, repo);
7308 if (err)
7309 goto done;
7311 err = get_fileindex_path(&fileindex_path, worktree);
7312 if (err)
7313 goto done;
7314 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7315 sync_err = sync_fileindex(fileindex, fileindex_path);
7316 if (sync_err && err == NULL)
7317 err = sync_err;
7318 done:
7319 got_fileindex_free(fileindex);
7320 free(fileindex_path);
7321 free(new_head_commit_id);
7322 unlockerr = lock_worktree(worktree, LOCK_SH);
7323 if (unlockerr && err == NULL)
7324 err = unlockerr;
7325 return err;
7328 const struct got_error *
7329 got_worktree_rebase_abort(struct got_worktree *worktree,
7330 struct got_fileindex *fileindex, struct got_repository *repo,
7331 struct got_reference *new_base_branch,
7332 got_worktree_checkout_cb progress_cb, void *progress_arg)
7334 const struct got_error *err, *unlockerr, *sync_err;
7335 struct got_reference *resolved = NULL;
7336 struct got_object_id *commit_id = NULL;
7337 struct got_commit_object *commit = NULL;
7338 char *fileindex_path = NULL;
7339 struct revert_file_args rfa;
7340 struct got_object_id *tree_id = NULL;
7342 err = lock_worktree(worktree, LOCK_EX);
7343 if (err)
7344 return err;
7346 err = got_ref_open(&resolved, repo,
7347 got_ref_get_symref_target(new_base_branch), 0);
7348 if (err)
7349 goto done;
7351 err = got_worktree_set_head_ref(worktree, resolved);
7352 if (err)
7353 goto done;
7356 * XXX commits to the base branch could have happened while
7357 * we were busy rebasing; should we store the original commit ID
7358 * when rebase begins and read it back here?
7360 err = got_ref_resolve(&commit_id, repo, resolved);
7361 if (err)
7362 goto done;
7364 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7365 if (err)
7366 goto done;
7368 err = got_object_open_as_commit(&commit, repo,
7369 worktree->base_commit_id);
7370 if (err)
7371 goto done;
7373 err = got_object_id_by_path(&tree_id, repo, commit,
7374 worktree->path_prefix);
7375 if (err)
7376 goto done;
7378 err = delete_rebase_refs(worktree, repo);
7379 if (err)
7380 goto done;
7382 err = get_fileindex_path(&fileindex_path, worktree);
7383 if (err)
7384 goto done;
7386 rfa.worktree = worktree;
7387 rfa.fileindex = fileindex;
7388 rfa.progress_cb = progress_cb;
7389 rfa.progress_arg = progress_arg;
7390 rfa.patch_cb = NULL;
7391 rfa.patch_arg = NULL;
7392 rfa.repo = repo;
7393 rfa.unlink_added_files = 0;
7394 err = worktree_status(worktree, "", fileindex, repo,
7395 revert_file, &rfa, NULL, NULL, 1, 0);
7396 if (err)
7397 goto sync;
7399 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7400 repo, progress_cb, progress_arg, NULL, NULL);
7401 sync:
7402 sync_err = sync_fileindex(fileindex, fileindex_path);
7403 if (sync_err && err == NULL)
7404 err = sync_err;
7405 done:
7406 got_ref_close(resolved);
7407 free(tree_id);
7408 free(commit_id);
7409 if (commit)
7410 got_object_commit_close(commit);
7411 if (fileindex)
7412 got_fileindex_free(fileindex);
7413 free(fileindex_path);
7415 unlockerr = lock_worktree(worktree, LOCK_SH);
7416 if (unlockerr && err == NULL)
7417 err = unlockerr;
7418 return err;
7421 const struct got_error *
7422 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
7423 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
7424 struct got_fileindex **fileindex, struct got_worktree *worktree,
7425 struct got_repository *repo)
7427 const struct got_error *err = NULL;
7428 char *tmp_branch_name = NULL;
7429 char *branch_ref_name = NULL;
7430 char *base_commit_ref_name = NULL;
7431 char *fileindex_path = NULL;
7432 struct check_rebase_ok_arg ok_arg;
7433 struct got_reference *wt_branch = NULL;
7434 struct got_reference *base_commit_ref = NULL;
7436 *tmp_branch = NULL;
7437 *branch_ref = NULL;
7438 *base_commit_id = NULL;
7439 *fileindex = NULL;
7441 err = lock_worktree(worktree, LOCK_EX);
7442 if (err)
7443 return err;
7445 err = open_fileindex(fileindex, &fileindex_path, worktree);
7446 if (err)
7447 goto done;
7449 ok_arg.worktree = worktree;
7450 ok_arg.repo = repo;
7451 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7452 &ok_arg);
7453 if (err)
7454 goto done;
7456 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7457 if (err)
7458 goto done;
7460 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7461 if (err)
7462 goto done;
7464 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7465 worktree);
7466 if (err)
7467 goto done;
7469 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7470 0);
7471 if (err)
7472 goto done;
7474 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
7475 if (err)
7476 goto done;
7478 err = got_ref_write(*branch_ref, repo);
7479 if (err)
7480 goto done;
7482 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
7483 worktree->base_commit_id);
7484 if (err)
7485 goto done;
7486 err = got_ref_write(base_commit_ref, repo);
7487 if (err)
7488 goto done;
7489 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
7490 if (*base_commit_id == NULL) {
7491 err = got_error_from_errno("got_object_id_dup");
7492 goto done;
7495 err = got_ref_alloc(tmp_branch, tmp_branch_name,
7496 worktree->base_commit_id);
7497 if (err)
7498 goto done;
7499 err = got_ref_write(*tmp_branch, repo);
7500 if (err)
7501 goto done;
7503 err = got_worktree_set_head_ref(worktree, *tmp_branch);
7504 if (err)
7505 goto done;
7506 done:
7507 free(fileindex_path);
7508 free(tmp_branch_name);
7509 free(branch_ref_name);
7510 free(base_commit_ref_name);
7511 if (wt_branch)
7512 got_ref_close(wt_branch);
7513 if (err) {
7514 if (*branch_ref) {
7515 got_ref_close(*branch_ref);
7516 *branch_ref = NULL;
7518 if (*tmp_branch) {
7519 got_ref_close(*tmp_branch);
7520 *tmp_branch = NULL;
7522 free(*base_commit_id);
7523 if (*fileindex) {
7524 got_fileindex_free(*fileindex);
7525 *fileindex = NULL;
7527 lock_worktree(worktree, LOCK_SH);
7529 return err;
7532 const struct got_error *
7533 got_worktree_histedit_postpone(struct got_worktree *worktree,
7534 struct got_fileindex *fileindex)
7536 if (fileindex)
7537 got_fileindex_free(fileindex);
7538 return lock_worktree(worktree, LOCK_SH);
7541 const struct got_error *
7542 got_worktree_histedit_in_progress(int *in_progress,
7543 struct got_worktree *worktree)
7545 const struct got_error *err;
7546 char *tmp_branch_name = NULL;
7548 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7549 if (err)
7550 return err;
7552 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
7553 free(tmp_branch_name);
7554 return NULL;
7557 const struct got_error *
7558 got_worktree_histedit_continue(struct got_object_id **commit_id,
7559 struct got_reference **tmp_branch, struct got_reference **branch_ref,
7560 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
7561 struct got_worktree *worktree, struct got_repository *repo)
7563 const struct got_error *err;
7564 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
7565 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
7566 struct got_reference *commit_ref = NULL;
7567 struct got_reference *base_commit_ref = NULL;
7568 char *fileindex_path = NULL;
7569 int have_staged_files = 0;
7571 *commit_id = NULL;
7572 *tmp_branch = NULL;
7573 *base_commit_id = NULL;
7574 *fileindex = NULL;
7576 err = lock_worktree(worktree, LOCK_EX);
7577 if (err)
7578 return err;
7580 err = open_fileindex(fileindex, &fileindex_path, worktree);
7581 if (err)
7582 goto done;
7584 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7585 &have_staged_files);
7586 if (err && err->code != GOT_ERR_CANCELLED)
7587 goto done;
7588 if (have_staged_files) {
7589 err = got_error(GOT_ERR_STAGED_PATHS);
7590 goto done;
7593 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7594 if (err)
7595 goto done;
7597 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7598 if (err)
7599 goto done;
7601 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7602 if (err)
7603 goto done;
7605 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7606 worktree);
7607 if (err)
7608 goto done;
7610 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7611 if (err)
7612 goto done;
7614 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7615 if (err)
7616 goto done;
7617 err = got_ref_resolve(commit_id, repo, commit_ref);
7618 if (err)
7619 goto done;
7621 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7622 if (err)
7623 goto done;
7624 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7625 if (err)
7626 goto done;
7628 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7629 if (err)
7630 goto done;
7631 done:
7632 free(commit_ref_name);
7633 free(branch_ref_name);
7634 free(fileindex_path);
7635 if (commit_ref)
7636 got_ref_close(commit_ref);
7637 if (base_commit_ref)
7638 got_ref_close(base_commit_ref);
7639 if (err) {
7640 free(*commit_id);
7641 *commit_id = NULL;
7642 free(*base_commit_id);
7643 *base_commit_id = NULL;
7644 if (*tmp_branch) {
7645 got_ref_close(*tmp_branch);
7646 *tmp_branch = NULL;
7648 if (*fileindex) {
7649 got_fileindex_free(*fileindex);
7650 *fileindex = NULL;
7652 lock_worktree(worktree, LOCK_EX);
7654 return err;
7657 static const struct got_error *
7658 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7660 const struct got_error *err;
7661 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7662 char *branch_ref_name = NULL, *commit_ref_name = NULL;
7664 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7665 if (err)
7666 goto done;
7667 err = delete_ref(tmp_branch_name, repo);
7668 if (err)
7669 goto done;
7671 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7672 worktree);
7673 if (err)
7674 goto done;
7675 err = delete_ref(base_commit_ref_name, repo);
7676 if (err)
7677 goto done;
7679 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7680 if (err)
7681 goto done;
7682 err = delete_ref(branch_ref_name, repo);
7683 if (err)
7684 goto done;
7686 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7687 if (err)
7688 goto done;
7689 err = delete_ref(commit_ref_name, repo);
7690 if (err)
7691 goto done;
7692 done:
7693 free(tmp_branch_name);
7694 free(base_commit_ref_name);
7695 free(branch_ref_name);
7696 free(commit_ref_name);
7697 return err;
7700 const struct got_error *
7701 got_worktree_histedit_abort(struct got_worktree *worktree,
7702 struct got_fileindex *fileindex, struct got_repository *repo,
7703 struct got_reference *branch, struct got_object_id *base_commit_id,
7704 got_worktree_checkout_cb progress_cb, void *progress_arg)
7706 const struct got_error *err, *unlockerr, *sync_err;
7707 struct got_reference *resolved = NULL;
7708 char *fileindex_path = NULL;
7709 struct got_commit_object *commit = NULL;
7710 struct got_object_id *tree_id = NULL;
7711 struct revert_file_args rfa;
7713 err = lock_worktree(worktree, LOCK_EX);
7714 if (err)
7715 return err;
7717 err = got_ref_open(&resolved, repo,
7718 got_ref_get_symref_target(branch), 0);
7719 if (err)
7720 goto done;
7722 err = got_worktree_set_head_ref(worktree, resolved);
7723 if (err)
7724 goto done;
7726 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7727 if (err)
7728 goto done;
7730 err = got_object_open_as_commit(&commit, repo,
7731 worktree->base_commit_id);
7732 if (err)
7733 goto done;
7735 err = got_object_id_by_path(&tree_id, repo, commit,
7736 worktree->path_prefix);
7737 if (err)
7738 goto done;
7740 err = delete_histedit_refs(worktree, repo);
7741 if (err)
7742 goto done;
7744 err = get_fileindex_path(&fileindex_path, worktree);
7745 if (err)
7746 goto done;
7748 rfa.worktree = worktree;
7749 rfa.fileindex = fileindex;
7750 rfa.progress_cb = progress_cb;
7751 rfa.progress_arg = progress_arg;
7752 rfa.patch_cb = NULL;
7753 rfa.patch_arg = NULL;
7754 rfa.repo = repo;
7755 rfa.unlink_added_files = 0;
7756 err = worktree_status(worktree, "", fileindex, repo,
7757 revert_file, &rfa, NULL, NULL, 1, 0);
7758 if (err)
7759 goto sync;
7761 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7762 repo, progress_cb, progress_arg, NULL, NULL);
7763 sync:
7764 sync_err = sync_fileindex(fileindex, fileindex_path);
7765 if (sync_err && err == NULL)
7766 err = sync_err;
7767 done:
7768 got_ref_close(resolved);
7769 free(tree_id);
7770 free(fileindex_path);
7772 unlockerr = lock_worktree(worktree, LOCK_SH);
7773 if (unlockerr && err == NULL)
7774 err = unlockerr;
7775 return err;
7778 const struct got_error *
7779 got_worktree_histedit_complete(struct got_worktree *worktree,
7780 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7781 struct got_reference *edited_branch, struct got_repository *repo)
7783 const struct got_error *err, *unlockerr, *sync_err;
7784 struct got_object_id *new_head_commit_id = NULL;
7785 struct got_reference *resolved = NULL;
7786 char *fileindex_path = NULL;
7788 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7789 if (err)
7790 return err;
7792 err = got_ref_open(&resolved, repo,
7793 got_ref_get_symref_target(edited_branch), 0);
7794 if (err)
7795 goto done;
7797 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7798 resolved, new_head_commit_id, repo);
7799 if (err)
7800 goto done;
7802 err = got_ref_change_ref(resolved, new_head_commit_id);
7803 if (err)
7804 goto done;
7806 err = got_ref_write(resolved, repo);
7807 if (err)
7808 goto done;
7810 err = got_worktree_set_head_ref(worktree, resolved);
7811 if (err)
7812 goto done;
7814 err = delete_histedit_refs(worktree, repo);
7815 if (err)
7816 goto done;
7818 err = get_fileindex_path(&fileindex_path, worktree);
7819 if (err)
7820 goto done;
7821 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7822 sync_err = sync_fileindex(fileindex, fileindex_path);
7823 if (sync_err && err == NULL)
7824 err = sync_err;
7825 done:
7826 got_fileindex_free(fileindex);
7827 free(fileindex_path);
7828 free(new_head_commit_id);
7829 unlockerr = lock_worktree(worktree, LOCK_SH);
7830 if (unlockerr && err == NULL)
7831 err = unlockerr;
7832 return err;
7835 const struct got_error *
7836 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7837 struct got_object_id *commit_id, struct got_repository *repo)
7839 const struct got_error *err;
7840 char *commit_ref_name;
7842 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7843 if (err)
7844 return err;
7846 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7847 if (err)
7848 goto done;
7850 err = delete_ref(commit_ref_name, repo);
7851 done:
7852 free(commit_ref_name);
7853 return err;
7856 const struct got_error *
7857 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7858 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7859 struct got_worktree *worktree, const char *refname,
7860 struct got_repository *repo)
7862 const struct got_error *err = NULL;
7863 char *fileindex_path = NULL;
7864 struct check_rebase_ok_arg ok_arg;
7866 *fileindex = NULL;
7867 *branch_ref = NULL;
7868 *base_branch_ref = NULL;
7870 err = lock_worktree(worktree, LOCK_EX);
7871 if (err)
7872 return err;
7874 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7875 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7876 "cannot integrate a branch into itself; "
7877 "update -b or different branch name required");
7878 goto done;
7881 err = open_fileindex(fileindex, &fileindex_path, worktree);
7882 if (err)
7883 goto done;
7885 /* Preconditions are the same as for rebase. */
7886 ok_arg.worktree = worktree;
7887 ok_arg.repo = repo;
7888 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7889 &ok_arg);
7890 if (err)
7891 goto done;
7893 err = got_ref_open(branch_ref, repo, refname, 1);
7894 if (err)
7895 goto done;
7897 err = got_ref_open(base_branch_ref, repo,
7898 got_worktree_get_head_ref_name(worktree), 1);
7899 done:
7900 if (err) {
7901 if (*branch_ref) {
7902 got_ref_close(*branch_ref);
7903 *branch_ref = NULL;
7905 if (*base_branch_ref) {
7906 got_ref_close(*base_branch_ref);
7907 *base_branch_ref = NULL;
7909 if (*fileindex) {
7910 got_fileindex_free(*fileindex);
7911 *fileindex = NULL;
7913 lock_worktree(worktree, LOCK_SH);
7915 return err;
7918 const struct got_error *
7919 got_worktree_integrate_continue(struct got_worktree *worktree,
7920 struct got_fileindex *fileindex, struct got_repository *repo,
7921 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7922 got_worktree_checkout_cb progress_cb, void *progress_arg,
7923 got_cancel_cb cancel_cb, void *cancel_arg)
7925 const struct got_error *err = NULL, *sync_err, *unlockerr;
7926 char *fileindex_path = NULL;
7927 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7928 struct got_commit_object *commit = NULL;
7930 err = get_fileindex_path(&fileindex_path, worktree);
7931 if (err)
7932 goto done;
7934 err = got_ref_resolve(&commit_id, repo, branch_ref);
7935 if (err)
7936 goto done;
7938 err = got_object_open_as_commit(&commit, repo, commit_id);
7939 if (err)
7940 goto done;
7942 err = got_object_id_by_path(&tree_id, repo, commit,
7943 worktree->path_prefix);
7944 if (err)
7945 goto done;
7947 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7948 if (err)
7949 goto done;
7951 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7952 progress_cb, progress_arg, cancel_cb, cancel_arg);
7953 if (err)
7954 goto sync;
7956 err = got_ref_change_ref(base_branch_ref, commit_id);
7957 if (err)
7958 goto sync;
7960 err = got_ref_write(base_branch_ref, repo);
7961 if (err)
7962 goto sync;
7964 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7965 sync:
7966 sync_err = sync_fileindex(fileindex, fileindex_path);
7967 if (sync_err && err == NULL)
7968 err = sync_err;
7970 done:
7971 unlockerr = got_ref_unlock(branch_ref);
7972 if (unlockerr && err == NULL)
7973 err = unlockerr;
7974 got_ref_close(branch_ref);
7976 unlockerr = got_ref_unlock(base_branch_ref);
7977 if (unlockerr && err == NULL)
7978 err = unlockerr;
7979 got_ref_close(base_branch_ref);
7981 got_fileindex_free(fileindex);
7982 free(fileindex_path);
7983 free(tree_id);
7984 if (commit)
7985 got_object_commit_close(commit);
7987 unlockerr = lock_worktree(worktree, LOCK_SH);
7988 if (unlockerr && err == NULL)
7989 err = unlockerr;
7990 return err;
7993 const struct got_error *
7994 got_worktree_integrate_abort(struct got_worktree *worktree,
7995 struct got_fileindex *fileindex, struct got_repository *repo,
7996 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7998 const struct got_error *err = NULL, *unlockerr = NULL;
8000 got_fileindex_free(fileindex);
8002 err = lock_worktree(worktree, LOCK_SH);
8004 unlockerr = got_ref_unlock(branch_ref);
8005 if (unlockerr && err == NULL)
8006 err = unlockerr;
8007 got_ref_close(branch_ref);
8009 unlockerr = got_ref_unlock(base_branch_ref);
8010 if (unlockerr && err == NULL)
8011 err = unlockerr;
8012 got_ref_close(base_branch_ref);
8014 return err;
8017 const struct got_error *
8018 got_worktree_merge_postpone(struct got_worktree *worktree,
8019 struct got_fileindex *fileindex)
8021 const struct got_error *err, *sync_err;
8022 char *fileindex_path = NULL;
8024 err = get_fileindex_path(&fileindex_path, worktree);
8025 if (err)
8026 goto done;
8028 sync_err = sync_fileindex(fileindex, fileindex_path);
8030 err = lock_worktree(worktree, LOCK_SH);
8031 if (sync_err && err == NULL)
8032 err = sync_err;
8033 done:
8034 got_fileindex_free(fileindex);
8035 free(fileindex_path);
8036 return err;
8039 static const struct got_error *
8040 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
8042 const struct got_error *err;
8043 char *branch_refname = NULL, *commit_refname = NULL;
8045 err = get_merge_branch_ref_name(&branch_refname, worktree);
8046 if (err)
8047 goto done;
8048 err = delete_ref(branch_refname, repo);
8049 if (err)
8050 goto done;
8052 err = get_merge_commit_ref_name(&commit_refname, worktree);
8053 if (err)
8054 goto done;
8055 err = delete_ref(commit_refname, repo);
8056 if (err)
8057 goto done;
8059 done:
8060 free(branch_refname);
8061 free(commit_refname);
8062 return err;
8065 struct merge_commit_msg_arg {
8066 struct got_worktree *worktree;
8067 const char *branch_name;
8070 static const struct got_error *
8071 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths,
8072 const char *diff_path, char **logmsg, void *arg)
8074 struct merge_commit_msg_arg *a = arg;
8076 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
8077 got_worktree_get_head_ref_name(a->worktree)) == -1)
8078 return got_error_from_errno("asprintf");
8080 return NULL;
8084 const struct got_error *
8085 got_worktree_merge_branch(struct got_worktree *worktree,
8086 struct got_fileindex *fileindex,
8087 struct got_object_id *yca_commit_id,
8088 struct got_object_id *branch_tip,
8089 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
8090 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
8092 const struct got_error *err;
8093 char *fileindex_path = NULL;
8095 err = get_fileindex_path(&fileindex_path, worktree);
8096 if (err)
8097 goto done;
8099 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
8100 worktree);
8101 if (err)
8102 goto done;
8104 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
8105 branch_tip, repo, progress_cb, progress_arg,
8106 cancel_cb, cancel_arg);
8107 done:
8108 free(fileindex_path);
8109 return err;
8112 const struct got_error *
8113 got_worktree_merge_commit(struct got_object_id **new_commit_id,
8114 struct got_worktree *worktree, struct got_fileindex *fileindex,
8115 const char *author, const char *committer, int allow_bad_symlinks,
8116 struct got_object_id *branch_tip, const char *branch_name,
8117 int allow_conflict, struct got_repository *repo,
8118 got_worktree_status_cb status_cb, void *status_arg)
8121 const struct got_error *err = NULL, *sync_err;
8122 struct got_pathlist_head commitable_paths;
8123 struct collect_commitables_arg cc_arg;
8124 struct got_pathlist_entry *pe;
8125 struct got_reference *head_ref = NULL;
8126 struct got_object_id *head_commit_id = NULL;
8127 int have_staged_files = 0;
8128 struct merge_commit_msg_arg mcm_arg;
8129 char *fileindex_path = NULL;
8131 memset(&cc_arg, 0, sizeof(cc_arg));
8132 *new_commit_id = NULL;
8134 TAILQ_INIT(&commitable_paths);
8136 err = get_fileindex_path(&fileindex_path, worktree);
8137 if (err)
8138 goto done;
8140 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
8141 if (err)
8142 goto done;
8144 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8145 if (err)
8146 goto done;
8148 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
8149 &have_staged_files);
8150 if (err && err->code != GOT_ERR_CANCELLED)
8151 goto done;
8152 if (have_staged_files) {
8153 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
8154 goto done;
8157 cc_arg.commitable_paths = &commitable_paths;
8158 cc_arg.worktree = worktree;
8159 cc_arg.fileindex = fileindex;
8160 cc_arg.repo = repo;
8161 cc_arg.have_staged_files = have_staged_files;
8162 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
8163 cc_arg.commit_conflicts = allow_conflict;
8164 err = worktree_status(worktree, "", fileindex, repo,
8165 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
8166 if (err)
8167 goto done;
8169 if (TAILQ_EMPTY(&commitable_paths)) {
8170 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
8171 "merge of %s cannot proceed", branch_name);
8172 goto done;
8175 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8176 struct got_commitable *ct = pe->data;
8177 const char *ct_path = ct->in_repo_path;
8179 while (ct_path[0] == '/')
8180 ct_path++;
8181 err = check_out_of_date(ct_path, ct->status,
8182 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
8183 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
8184 if (err)
8185 goto done;
8189 mcm_arg.worktree = worktree;
8190 mcm_arg.branch_name = branch_name;
8191 err = commit_worktree(new_commit_id, &commitable_paths,
8192 head_commit_id, branch_tip, worktree, author, committer, NULL,
8193 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
8194 if (err)
8195 goto done;
8197 err = update_fileindex_after_commit(worktree, &commitable_paths,
8198 *new_commit_id, fileindex, have_staged_files);
8199 sync_err = sync_fileindex(fileindex, fileindex_path);
8200 if (sync_err && err == NULL)
8201 err = sync_err;
8202 done:
8203 TAILQ_FOREACH(pe, &commitable_paths, entry) {
8204 struct got_commitable *ct = pe->data;
8206 free_commitable(ct);
8208 got_pathlist_free(&commitable_paths, GOT_PATHLIST_FREE_NONE);
8209 free(fileindex_path);
8210 return err;
8213 const struct got_error *
8214 got_worktree_merge_complete(struct got_worktree *worktree,
8215 struct got_fileindex *fileindex, struct got_repository *repo)
8217 const struct got_error *err, *unlockerr, *sync_err;
8218 char *fileindex_path = NULL;
8220 err = delete_merge_refs(worktree, repo);
8221 if (err)
8222 goto done;
8224 err = get_fileindex_path(&fileindex_path, worktree);
8225 if (err)
8226 goto done;
8227 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
8228 sync_err = sync_fileindex(fileindex, fileindex_path);
8229 if (sync_err && err == NULL)
8230 err = sync_err;
8231 done:
8232 got_fileindex_free(fileindex);
8233 free(fileindex_path);
8234 unlockerr = lock_worktree(worktree, LOCK_SH);
8235 if (unlockerr && err == NULL)
8236 err = unlockerr;
8237 return err;
8240 const struct got_error *
8241 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
8242 struct got_repository *repo)
8244 const struct got_error *err;
8245 char *branch_refname = NULL;
8246 struct got_reference *branch_ref = NULL;
8248 *in_progress = 0;
8250 err = get_merge_branch_ref_name(&branch_refname, worktree);
8251 if (err)
8252 return err;
8253 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8254 free(branch_refname);
8255 if (err) {
8256 if (err->code != GOT_ERR_NOT_REF)
8257 return err;
8258 } else
8259 *in_progress = 1;
8261 return NULL;
8264 const struct got_error *got_worktree_merge_prepare(
8265 struct got_fileindex **fileindex, struct got_worktree *worktree,
8266 struct got_reference *branch, struct got_repository *repo)
8268 const struct got_error *err = NULL;
8269 char *fileindex_path = NULL;
8270 char *branch_refname = NULL, *commit_refname = NULL;
8271 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
8272 struct got_reference *commit_ref = NULL;
8273 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
8274 struct check_rebase_ok_arg ok_arg;
8276 *fileindex = NULL;
8278 err = lock_worktree(worktree, LOCK_EX);
8279 if (err)
8280 return err;
8282 err = open_fileindex(fileindex, &fileindex_path, worktree);
8283 if (err)
8284 goto done;
8286 /* Preconditions are the same as for rebase. */
8287 ok_arg.worktree = worktree;
8288 ok_arg.repo = repo;
8289 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
8290 &ok_arg);
8291 if (err)
8292 goto done;
8294 err = get_merge_branch_ref_name(&branch_refname, worktree);
8295 if (err)
8296 return err;
8298 err = get_merge_commit_ref_name(&commit_refname, worktree);
8299 if (err)
8300 return err;
8302 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
8303 0);
8304 if (err)
8305 goto done;
8307 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
8308 if (err)
8309 goto done;
8311 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
8312 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
8313 goto done;
8316 err = got_ref_resolve(&branch_tip, repo, branch);
8317 if (err)
8318 goto done;
8320 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
8321 if (err)
8322 goto done;
8323 err = got_ref_write(branch_ref, repo);
8324 if (err)
8325 goto done;
8327 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
8328 if (err)
8329 goto done;
8330 err = got_ref_write(commit_ref, repo);
8331 if (err)
8332 goto done;
8334 done:
8335 free(branch_refname);
8336 free(commit_refname);
8337 free(fileindex_path);
8338 if (branch_ref)
8339 got_ref_close(branch_ref);
8340 if (commit_ref)
8341 got_ref_close(commit_ref);
8342 if (wt_branch)
8343 got_ref_close(wt_branch);
8344 free(wt_branch_tip);
8345 if (err) {
8346 if (*fileindex) {
8347 got_fileindex_free(*fileindex);
8348 *fileindex = NULL;
8350 lock_worktree(worktree, LOCK_SH);
8352 return err;
8355 const struct got_error *
8356 got_worktree_merge_continue(char **branch_name,
8357 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
8358 struct got_worktree *worktree, struct got_repository *repo)
8360 const struct got_error *err;
8361 char *commit_refname = NULL, *branch_refname = NULL;
8362 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
8363 char *fileindex_path = NULL;
8364 int have_staged_files = 0;
8366 *branch_name = NULL;
8367 *branch_tip = NULL;
8368 *fileindex = NULL;
8370 err = lock_worktree(worktree, LOCK_EX);
8371 if (err)
8372 return err;
8374 err = open_fileindex(fileindex, &fileindex_path, worktree);
8375 if (err)
8376 goto done;
8378 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
8379 &have_staged_files);
8380 if (err && err->code != GOT_ERR_CANCELLED)
8381 goto done;
8382 if (have_staged_files) {
8383 err = got_error(GOT_ERR_STAGED_PATHS);
8384 goto done;
8387 err = get_merge_branch_ref_name(&branch_refname, worktree);
8388 if (err)
8389 goto done;
8391 err = get_merge_commit_ref_name(&commit_refname, worktree);
8392 if (err)
8393 goto done;
8395 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
8396 if (err)
8397 goto done;
8399 if (!got_ref_is_symbolic(branch_ref)) {
8400 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
8401 "%s is not a symbolic reference",
8402 got_ref_get_name(branch_ref));
8403 goto done;
8405 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
8406 if (*branch_name == NULL) {
8407 err = got_error_from_errno("strdup");
8408 goto done;
8411 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
8412 if (err)
8413 goto done;
8415 err = got_ref_resolve(branch_tip, repo, commit_ref);
8416 if (err)
8417 goto done;
8418 done:
8419 free(commit_refname);
8420 free(branch_refname);
8421 free(fileindex_path);
8422 if (commit_ref)
8423 got_ref_close(commit_ref);
8424 if (branch_ref)
8425 got_ref_close(branch_ref);
8426 if (err) {
8427 if (*branch_name) {
8428 free(*branch_name);
8429 *branch_name = NULL;
8431 free(*branch_tip);
8432 *branch_tip = NULL;
8433 if (*fileindex) {
8434 got_fileindex_free(*fileindex);
8435 *fileindex = NULL;
8437 lock_worktree(worktree, LOCK_SH);
8439 return err;
8442 const struct got_error *
8443 got_worktree_merge_abort(struct got_worktree *worktree,
8444 struct got_fileindex *fileindex, struct got_repository *repo,
8445 got_worktree_checkout_cb progress_cb, void *progress_arg)
8447 const struct got_error *err, *unlockerr, *sync_err;
8448 struct got_object_id *commit_id = NULL;
8449 struct got_commit_object *commit = NULL;
8450 char *fileindex_path = NULL;
8451 struct revert_file_args rfa;
8452 struct got_object_id *tree_id = NULL;
8454 err = got_object_open_as_commit(&commit, repo,
8455 worktree->base_commit_id);
8456 if (err)
8457 goto done;
8459 err = got_object_id_by_path(&tree_id, repo, commit,
8460 worktree->path_prefix);
8461 if (err)
8462 goto done;
8464 err = delete_merge_refs(worktree, repo);
8465 if (err)
8466 goto done;
8468 err = get_fileindex_path(&fileindex_path, worktree);
8469 if (err)
8470 goto done;
8472 rfa.worktree = worktree;
8473 rfa.fileindex = fileindex;
8474 rfa.progress_cb = progress_cb;
8475 rfa.progress_arg = progress_arg;
8476 rfa.patch_cb = NULL;
8477 rfa.patch_arg = NULL;
8478 rfa.repo = repo;
8479 rfa.unlink_added_files = 1;
8480 err = worktree_status(worktree, "", fileindex, repo,
8481 revert_file, &rfa, NULL, NULL, 1, 0);
8482 if (err)
8483 goto sync;
8485 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
8486 repo, progress_cb, progress_arg, NULL, NULL);
8487 sync:
8488 sync_err = sync_fileindex(fileindex, fileindex_path);
8489 if (sync_err && err == NULL)
8490 err = sync_err;
8491 done:
8492 free(tree_id);
8493 free(commit_id);
8494 if (commit)
8495 got_object_commit_close(commit);
8496 if (fileindex)
8497 got_fileindex_free(fileindex);
8498 free(fileindex_path);
8500 unlockerr = lock_worktree(worktree, LOCK_SH);
8501 if (unlockerr && err == NULL)
8502 err = unlockerr;
8503 return err;
8506 struct check_stage_ok_arg {
8507 struct got_object_id *head_commit_id;
8508 struct got_worktree *worktree;
8509 struct got_fileindex *fileindex;
8510 struct got_repository *repo;
8511 int have_changes;
8514 static const struct got_error *
8515 check_stage_ok(void *arg, unsigned char status,
8516 unsigned char staged_status, const char *relpath,
8517 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8518 struct got_object_id *commit_id, int dirfd, const char *de_name)
8520 struct check_stage_ok_arg *a = arg;
8521 const struct got_error *err = NULL;
8522 struct got_fileindex_entry *ie;
8523 struct got_object_id base_commit_id;
8524 struct got_object_id *base_commit_idp = NULL;
8525 char *in_repo_path = NULL, *p;
8527 if (status == GOT_STATUS_UNVERSIONED ||
8528 status == GOT_STATUS_NO_CHANGE)
8529 return NULL;
8530 if (status == GOT_STATUS_NONEXISTENT)
8531 return got_error_set_errno(ENOENT, relpath);
8533 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8534 if (ie == NULL)
8535 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8537 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
8538 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
8539 relpath) == -1)
8540 return got_error_from_errno("asprintf");
8542 if (got_fileindex_entry_has_commit(ie)) {
8543 base_commit_idp = got_fileindex_entry_get_commit_id(
8544 &base_commit_id, ie);
8547 if (status == GOT_STATUS_CONFLICT) {
8548 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
8549 goto done;
8550 } else if (status != GOT_STATUS_ADD &&
8551 status != GOT_STATUS_MODIFY &&
8552 status != GOT_STATUS_DELETE) {
8553 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
8554 goto done;
8557 a->have_changes = 1;
8559 p = in_repo_path;
8560 while (p[0] == '/')
8561 p++;
8562 err = check_out_of_date(p, status, staged_status,
8563 blob_id, base_commit_idp, a->head_commit_id, a->repo,
8564 GOT_ERR_STAGE_OUT_OF_DATE);
8565 done:
8566 free(in_repo_path);
8567 return err;
8570 struct stage_path_arg {
8571 struct got_worktree *worktree;
8572 struct got_fileindex *fileindex;
8573 struct got_repository *repo;
8574 got_worktree_status_cb status_cb;
8575 void *status_arg;
8576 got_worktree_patch_cb patch_cb;
8577 void *patch_arg;
8578 int staged_something;
8579 int allow_bad_symlinks;
8582 static const struct got_error *
8583 stage_path(void *arg, unsigned char status,
8584 unsigned char staged_status, const char *relpath,
8585 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8586 struct got_object_id *commit_id, int dirfd, const char *de_name)
8588 struct stage_path_arg *a = arg;
8589 const struct got_error *err = NULL;
8590 struct got_fileindex_entry *ie;
8591 char *ondisk_path = NULL, *path_content = NULL;
8592 uint32_t stage;
8593 struct got_object_id *new_staged_blob_id = NULL;
8594 struct stat sb;
8596 if (status == GOT_STATUS_UNVERSIONED)
8597 return NULL;
8599 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8600 if (ie == NULL)
8601 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8603 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8604 relpath)== -1)
8605 return got_error_from_errno("asprintf");
8607 switch (status) {
8608 case GOT_STATUS_ADD:
8609 case GOT_STATUS_MODIFY:
8610 /* XXX could sb.st_mode be passed in by our caller? */
8611 if (lstat(ondisk_path, &sb) == -1) {
8612 err = got_error_from_errno2("lstat", ondisk_path);
8613 break;
8615 if (a->patch_cb) {
8616 if (status == GOT_STATUS_ADD) {
8617 int choice = GOT_PATCH_CHOICE_NONE;
8618 err = (*a->patch_cb)(&choice, a->patch_arg,
8619 status, ie->path, NULL, 1, 1);
8620 if (err)
8621 break;
8622 if (choice != GOT_PATCH_CHOICE_YES)
8623 break;
8624 } else {
8625 err = create_patched_content(&path_content, 0,
8626 staged_blob_id ? staged_blob_id : blob_id,
8627 ondisk_path, dirfd, de_name, ie->path,
8628 a->repo, a->patch_cb, a->patch_arg);
8629 if (err || path_content == NULL)
8630 break;
8633 err = got_object_blob_create(&new_staged_blob_id,
8634 path_content ? path_content : ondisk_path, a->repo);
8635 if (err)
8636 break;
8637 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8638 SHA1_DIGEST_LENGTH);
8639 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8640 stage = GOT_FILEIDX_STAGE_ADD;
8641 else
8642 stage = GOT_FILEIDX_STAGE_MODIFY;
8643 got_fileindex_entry_stage_set(ie, stage);
8644 if (S_ISLNK(sb.st_mode)) {
8645 int is_bad_symlink = 0;
8646 if (!a->allow_bad_symlinks) {
8647 char target_path[PATH_MAX];
8648 ssize_t target_len;
8649 target_len = readlink(ondisk_path, target_path,
8650 sizeof(target_path));
8651 if (target_len == -1) {
8652 err = got_error_from_errno2("readlink",
8653 ondisk_path);
8654 break;
8656 err = is_bad_symlink_target(&is_bad_symlink,
8657 target_path, target_len, ondisk_path,
8658 a->worktree->root_path);
8659 if (err)
8660 break;
8661 if (is_bad_symlink) {
8662 err = got_error_path(ondisk_path,
8663 GOT_ERR_BAD_SYMLINK);
8664 break;
8667 if (is_bad_symlink)
8668 got_fileindex_entry_staged_filetype_set(ie,
8669 GOT_FILEIDX_MODE_BAD_SYMLINK);
8670 else
8671 got_fileindex_entry_staged_filetype_set(ie,
8672 GOT_FILEIDX_MODE_SYMLINK);
8673 } else {
8674 got_fileindex_entry_staged_filetype_set(ie,
8675 GOT_FILEIDX_MODE_REGULAR_FILE);
8677 a->staged_something = 1;
8678 if (a->status_cb == NULL)
8679 break;
8680 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8681 get_staged_status(ie), relpath, blob_id,
8682 new_staged_blob_id, NULL, dirfd, de_name);
8683 if (err)
8684 break;
8686 * When staging the reverse of the staged diff,
8687 * implicitly unstage the file.
8689 if (memcmp(ie->staged_blob_sha1, ie->blob_sha1,
8690 sizeof(ie->blob_sha1)) == 0) {
8691 got_fileindex_entry_stage_set(ie,
8692 GOT_FILEIDX_STAGE_NONE);
8694 break;
8695 case GOT_STATUS_DELETE:
8696 if (staged_status == GOT_STATUS_DELETE)
8697 break;
8698 if (a->patch_cb) {
8699 int choice = GOT_PATCH_CHOICE_NONE;
8700 err = (*a->patch_cb)(&choice, a->patch_arg, status,
8701 ie->path, NULL, 1, 1);
8702 if (err)
8703 break;
8704 if (choice == GOT_PATCH_CHOICE_NO)
8705 break;
8706 if (choice != GOT_PATCH_CHOICE_YES) {
8707 err = got_error(GOT_ERR_PATCH_CHOICE);
8708 break;
8711 stage = GOT_FILEIDX_STAGE_DELETE;
8712 got_fileindex_entry_stage_set(ie, stage);
8713 a->staged_something = 1;
8714 if (a->status_cb == NULL)
8715 break;
8716 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8717 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8718 de_name);
8719 break;
8720 case GOT_STATUS_NO_CHANGE:
8721 break;
8722 case GOT_STATUS_CONFLICT:
8723 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8724 break;
8725 case GOT_STATUS_NONEXISTENT:
8726 err = got_error_set_errno(ENOENT, relpath);
8727 break;
8728 default:
8729 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8730 break;
8733 if (path_content && unlink(path_content) == -1 && err == NULL)
8734 err = got_error_from_errno2("unlink", path_content);
8735 free(path_content);
8736 free(ondisk_path);
8737 free(new_staged_blob_id);
8738 return err;
8741 const struct got_error *
8742 got_worktree_stage(struct got_worktree *worktree,
8743 struct got_pathlist_head *paths,
8744 got_worktree_status_cb status_cb, void *status_arg,
8745 got_worktree_patch_cb patch_cb, void *patch_arg,
8746 int allow_bad_symlinks, struct got_repository *repo)
8748 const struct got_error *err = NULL, *sync_err, *unlockerr;
8749 struct got_pathlist_entry *pe;
8750 struct got_fileindex *fileindex = NULL;
8751 char *fileindex_path = NULL;
8752 struct got_reference *head_ref = NULL;
8753 struct got_object_id *head_commit_id = NULL;
8754 struct check_stage_ok_arg oka;
8755 struct stage_path_arg spa;
8757 err = lock_worktree(worktree, LOCK_EX);
8758 if (err)
8759 return err;
8761 err = got_ref_open(&head_ref, repo,
8762 got_worktree_get_head_ref_name(worktree), 0);
8763 if (err)
8764 goto done;
8765 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8766 if (err)
8767 goto done;
8768 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8769 if (err)
8770 goto done;
8772 /* Check pre-conditions before staging anything. */
8773 oka.head_commit_id = head_commit_id;
8774 oka.worktree = worktree;
8775 oka.fileindex = fileindex;
8776 oka.repo = repo;
8777 oka.have_changes = 0;
8778 TAILQ_FOREACH(pe, paths, entry) {
8779 err = worktree_status(worktree, pe->path, fileindex, repo,
8780 check_stage_ok, &oka, NULL, NULL, 1, 0);
8781 if (err)
8782 goto done;
8784 if (!oka.have_changes) {
8785 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8786 goto done;
8789 spa.worktree = worktree;
8790 spa.fileindex = fileindex;
8791 spa.repo = repo;
8792 spa.patch_cb = patch_cb;
8793 spa.patch_arg = patch_arg;
8794 spa.status_cb = status_cb;
8795 spa.status_arg = status_arg;
8796 spa.staged_something = 0;
8797 spa.allow_bad_symlinks = allow_bad_symlinks;
8798 TAILQ_FOREACH(pe, paths, entry) {
8799 err = worktree_status(worktree, pe->path, fileindex, repo,
8800 stage_path, &spa, NULL, NULL, 1, 0);
8801 if (err)
8802 goto done;
8804 if (!spa.staged_something) {
8805 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8806 goto done;
8809 sync_err = sync_fileindex(fileindex, fileindex_path);
8810 if (sync_err && err == NULL)
8811 err = sync_err;
8812 done:
8813 if (head_ref)
8814 got_ref_close(head_ref);
8815 free(head_commit_id);
8816 free(fileindex_path);
8817 if (fileindex)
8818 got_fileindex_free(fileindex);
8819 unlockerr = lock_worktree(worktree, LOCK_SH);
8820 if (unlockerr && err == NULL)
8821 err = unlockerr;
8822 return err;
8825 struct unstage_path_arg {
8826 struct got_worktree *worktree;
8827 struct got_fileindex *fileindex;
8828 struct got_repository *repo;
8829 got_worktree_checkout_cb progress_cb;
8830 void *progress_arg;
8831 got_worktree_patch_cb patch_cb;
8832 void *patch_arg;
8835 static const struct got_error *
8836 create_unstaged_content(char **path_unstaged_content,
8837 char **path_new_staged_content, struct got_object_id *blob_id,
8838 struct got_object_id *staged_blob_id, const char *relpath,
8839 struct got_repository *repo,
8840 got_worktree_patch_cb patch_cb, void *patch_arg)
8842 const struct got_error *err, *free_err;
8843 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8844 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8845 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8846 struct got_diffreg_result *diffreg_result = NULL;
8847 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8848 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8849 int fd1 = -1, fd2 = -1;
8851 *path_unstaged_content = NULL;
8852 *path_new_staged_content = NULL;
8854 err = got_object_id_str(&label1, blob_id);
8855 if (err)
8856 return err;
8858 fd1 = got_opentempfd();
8859 if (fd1 == -1) {
8860 err = got_error_from_errno("got_opentempfd");
8861 goto done;
8863 fd2 = got_opentempfd();
8864 if (fd2 == -1) {
8865 err = got_error_from_errno("got_opentempfd");
8866 goto done;
8869 err = got_object_open_as_blob(&blob, repo, blob_id, 8192, fd1);
8870 if (err)
8871 goto done;
8873 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base", "");
8874 if (err)
8875 goto done;
8877 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8878 if (err)
8879 goto done;
8881 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192,
8882 fd2);
8883 if (err)
8884 goto done;
8886 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged", "");
8887 if (err)
8888 goto done;
8890 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8891 if (err)
8892 goto done;
8894 err = got_diff_files(&diffreg_result, f1, 1, label1, f2, 1,
8895 path2, 3, 0, 1, NULL, GOT_DIFF_ALGORITHM_MYERS);
8896 if (err)
8897 goto done;
8899 err = got_opentemp_named(path_unstaged_content, &outfile,
8900 "got-unstaged-content", "");
8901 if (err)
8902 goto done;
8903 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8904 "got-new-staged-content", "");
8905 if (err)
8906 goto done;
8908 if (fseek(f1, 0L, SEEK_SET) == -1) {
8909 err = got_ferror(f1, GOT_ERR_IO);
8910 goto done;
8912 if (fseek(f2, 0L, SEEK_SET) == -1) {
8913 err = got_ferror(f2, GOT_ERR_IO);
8914 goto done;
8916 /* Count the number of actual changes in the diff result. */
8917 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8918 struct diff_chunk_context cc = {};
8919 diff_chunk_context_load_change(&cc, &nchunks_used,
8920 diffreg_result->result, n, 0);
8921 nchanges++;
8923 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8924 int choice;
8925 err = apply_or_reject_change(&choice, &nchunks_used,
8926 diffreg_result->result, n, relpath, f1, f2,
8927 &line_cur1, &line_cur2,
8928 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8929 if (err)
8930 goto done;
8931 if (choice == GOT_PATCH_CHOICE_YES)
8932 have_content = 1;
8933 else
8934 have_rejected_content = 1;
8935 if (choice == GOT_PATCH_CHOICE_QUIT)
8936 break;
8938 if (have_content || have_rejected_content)
8939 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8940 outfile, rejectfile);
8941 done:
8942 free(label1);
8943 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
8944 err = got_error_from_errno("close");
8945 if (blob)
8946 got_object_blob_close(blob);
8947 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
8948 err = got_error_from_errno("close");
8949 if (staged_blob)
8950 got_object_blob_close(staged_blob);
8951 free_err = got_diffreg_result_free(diffreg_result);
8952 if (free_err && err == NULL)
8953 err = free_err;
8954 if (f1 && fclose(f1) == EOF && err == NULL)
8955 err = got_error_from_errno2("fclose", path1);
8956 if (f2 && fclose(f2) == EOF && err == NULL)
8957 err = got_error_from_errno2("fclose", path2);
8958 if (outfile && fclose(outfile) == EOF && err == NULL)
8959 err = got_error_from_errno2("fclose", *path_unstaged_content);
8960 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8961 err = got_error_from_errno2("fclose", *path_new_staged_content);
8962 if (path1 && unlink(path1) == -1 && err == NULL)
8963 err = got_error_from_errno2("unlink", path1);
8964 if (path2 && unlink(path2) == -1 && err == NULL)
8965 err = got_error_from_errno2("unlink", path2);
8966 if (err || !have_content) {
8967 if (*path_unstaged_content &&
8968 unlink(*path_unstaged_content) == -1 && err == NULL)
8969 err = got_error_from_errno2("unlink",
8970 *path_unstaged_content);
8971 free(*path_unstaged_content);
8972 *path_unstaged_content = NULL;
8974 if (err || !have_content || !have_rejected_content) {
8975 if (*path_new_staged_content &&
8976 unlink(*path_new_staged_content) == -1 && err == NULL)
8977 err = got_error_from_errno2("unlink",
8978 *path_new_staged_content);
8979 free(*path_new_staged_content);
8980 *path_new_staged_content = NULL;
8982 free(path1);
8983 free(path2);
8984 return err;
8987 static const struct got_error *
8988 unstage_hunks(struct got_object_id *staged_blob_id,
8989 struct got_blob_object *blob_base,
8990 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8991 const char *ondisk_path, const char *label_orig,
8992 struct got_worktree *worktree, struct got_repository *repo,
8993 got_worktree_patch_cb patch_cb, void *patch_arg,
8994 got_worktree_checkout_cb progress_cb, void *progress_arg)
8996 const struct got_error *err = NULL;
8997 char *path_unstaged_content = NULL;
8998 char *path_new_staged_content = NULL;
8999 char *parent = NULL, *base_path = NULL;
9000 char *blob_base_path = NULL;
9001 struct got_object_id *new_staged_blob_id = NULL;
9002 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
9003 struct stat sb;
9005 err = create_unstaged_content(&path_unstaged_content,
9006 &path_new_staged_content, blob_id, staged_blob_id,
9007 ie->path, repo, patch_cb, patch_arg);
9008 if (err)
9009 return err;
9011 if (path_unstaged_content == NULL)
9012 return NULL;
9014 if (path_new_staged_content) {
9015 err = got_object_blob_create(&new_staged_blob_id,
9016 path_new_staged_content, repo);
9017 if (err)
9018 goto done;
9021 f = fopen(path_unstaged_content, "re");
9022 if (f == NULL) {
9023 err = got_error_from_errno2("fopen",
9024 path_unstaged_content);
9025 goto done;
9027 if (fstat(fileno(f), &sb) == -1) {
9028 err = got_error_from_errno2("fstat", path_unstaged_content);
9029 goto done;
9031 if (got_fileindex_entry_staged_filetype_get(ie) ==
9032 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
9033 char link_target[PATH_MAX];
9034 size_t r;
9035 r = fread(link_target, 1, sizeof(link_target), f);
9036 if (r == 0 && ferror(f)) {
9037 err = got_error_from_errno("fread");
9038 goto done;
9040 if (r >= sizeof(link_target)) { /* should not happen */
9041 err = got_error(GOT_ERR_NO_SPACE);
9042 goto done;
9044 link_target[r] = '\0';
9045 err = merge_symlink(worktree, blob_base,
9046 ondisk_path, ie->path, label_orig, link_target,
9047 worktree->base_commit_id, repo, progress_cb,
9048 progress_arg);
9049 } else {
9050 int local_changes_subsumed;
9052 err = got_path_dirname(&parent, ondisk_path);
9053 if (err)
9054 return err;
9056 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
9057 parent) == -1) {
9058 err = got_error_from_errno("asprintf");
9059 base_path = NULL;
9060 goto done;
9063 err = got_opentemp_named(&blob_base_path, &f_base,
9064 base_path, "");
9065 if (err)
9066 goto done;
9067 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
9068 blob_base);
9069 if (err)
9070 goto done;
9073 * In order the run a 3-way merge with a symlink we copy the symlink's
9074 * target path into a temporary file and use that file with diff3.
9076 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9077 err = dump_symlink_target_path_to_file(&f_deriv2,
9078 ondisk_path);
9079 if (err)
9080 goto done;
9081 } else {
9082 int fd;
9083 fd = open(ondisk_path,
9084 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
9085 if (fd == -1) {
9086 err = got_error_from_errno2("open", ondisk_path);
9087 goto done;
9089 f_deriv2 = fdopen(fd, "r");
9090 if (f_deriv2 == NULL) {
9091 err = got_error_from_errno2("fdopen", ondisk_path);
9092 close(fd);
9093 goto done;
9097 err = merge_file(&local_changes_subsumed, worktree,
9098 f_base, f, f_deriv2, ondisk_path, ie->path,
9099 got_fileindex_perms_to_st(ie),
9100 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
9101 repo, progress_cb, progress_arg);
9103 if (err)
9104 goto done;
9106 if (new_staged_blob_id) {
9107 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
9108 SHA1_DIGEST_LENGTH);
9109 } else {
9110 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9111 got_fileindex_entry_staged_filetype_set(ie, 0);
9113 done:
9114 free(new_staged_blob_id);
9115 if (path_unstaged_content &&
9116 unlink(path_unstaged_content) == -1 && err == NULL)
9117 err = got_error_from_errno2("unlink", path_unstaged_content);
9118 if (path_new_staged_content &&
9119 unlink(path_new_staged_content) == -1 && err == NULL)
9120 err = got_error_from_errno2("unlink", path_new_staged_content);
9121 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
9122 err = got_error_from_errno2("unlink", blob_base_path);
9123 if (f_base && fclose(f_base) == EOF && err == NULL)
9124 err = got_error_from_errno2("fclose", path_unstaged_content);
9125 if (f && fclose(f) == EOF && err == NULL)
9126 err = got_error_from_errno2("fclose", path_unstaged_content);
9127 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
9128 err = got_error_from_errno2("fclose", ondisk_path);
9129 free(path_unstaged_content);
9130 free(path_new_staged_content);
9131 free(blob_base_path);
9132 free(parent);
9133 free(base_path);
9134 return err;
9137 static const struct got_error *
9138 unstage_path(void *arg, unsigned char status,
9139 unsigned char staged_status, const char *relpath,
9140 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9141 struct got_object_id *commit_id, int dirfd, const char *de_name)
9143 const struct got_error *err = NULL;
9144 struct unstage_path_arg *a = arg;
9145 struct got_fileindex_entry *ie;
9146 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
9147 char *ondisk_path = NULL;
9148 char *id_str = NULL, *label_orig = NULL;
9149 int local_changes_subsumed;
9150 struct stat sb;
9151 int fd1 = -1, fd2 = -1;
9153 if (staged_status != GOT_STATUS_ADD &&
9154 staged_status != GOT_STATUS_MODIFY &&
9155 staged_status != GOT_STATUS_DELETE)
9156 return NULL;
9158 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
9159 if (ie == NULL)
9160 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
9162 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
9163 == -1)
9164 return got_error_from_errno("asprintf");
9166 err = got_object_id_str(&id_str,
9167 commit_id ? commit_id : a->worktree->base_commit_id);
9168 if (err)
9169 goto done;
9170 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
9171 id_str) == -1) {
9172 err = got_error_from_errno("asprintf");
9173 goto done;
9176 fd1 = got_opentempfd();
9177 if (fd1 == -1) {
9178 err = got_error_from_errno("got_opentempfd");
9179 goto done;
9181 fd2 = got_opentempfd();
9182 if (fd2 == -1) {
9183 err = got_error_from_errno("got_opentempfd");
9184 goto done;
9187 switch (staged_status) {
9188 case GOT_STATUS_MODIFY:
9189 err = got_object_open_as_blob(&blob_base, a->repo,
9190 blob_id, 8192, fd1);
9191 if (err)
9192 break;
9193 /* fall through */
9194 case GOT_STATUS_ADD:
9195 if (a->patch_cb) {
9196 if (staged_status == GOT_STATUS_ADD) {
9197 int choice = GOT_PATCH_CHOICE_NONE;
9198 err = (*a->patch_cb)(&choice, a->patch_arg,
9199 staged_status, ie->path, NULL, 1, 1);
9200 if (err)
9201 break;
9202 if (choice != GOT_PATCH_CHOICE_YES)
9203 break;
9204 } else {
9205 err = unstage_hunks(staged_blob_id,
9206 blob_base, blob_id, ie, ondisk_path,
9207 label_orig, a->worktree, a->repo,
9208 a->patch_cb, a->patch_arg,
9209 a->progress_cb, a->progress_arg);
9210 break; /* Done with this file. */
9213 err = got_object_open_as_blob(&blob_staged, a->repo,
9214 staged_blob_id, 8192, fd2);
9215 if (err)
9216 break;
9217 switch (got_fileindex_entry_staged_filetype_get(ie)) {
9218 case GOT_FILEIDX_MODE_BAD_SYMLINK:
9219 case GOT_FILEIDX_MODE_REGULAR_FILE:
9220 err = merge_blob(&local_changes_subsumed, a->worktree,
9221 blob_base, ondisk_path, relpath,
9222 got_fileindex_perms_to_st(ie), label_orig,
9223 blob_staged, commit_id ? commit_id :
9224 a->worktree->base_commit_id, a->repo,
9225 a->progress_cb, a->progress_arg);
9226 break;
9227 case GOT_FILEIDX_MODE_SYMLINK:
9228 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
9229 char *staged_target;
9230 err = got_object_blob_read_to_str(
9231 &staged_target, blob_staged);
9232 if (err)
9233 goto done;
9234 err = merge_symlink(a->worktree, blob_base,
9235 ondisk_path, relpath, label_orig,
9236 staged_target, commit_id ? commit_id :
9237 a->worktree->base_commit_id,
9238 a->repo, a->progress_cb, a->progress_arg);
9239 free(staged_target);
9240 } else {
9241 err = merge_blob(&local_changes_subsumed,
9242 a->worktree, blob_base, ondisk_path,
9243 relpath, got_fileindex_perms_to_st(ie),
9244 label_orig, blob_staged,
9245 commit_id ? commit_id :
9246 a->worktree->base_commit_id, a->repo,
9247 a->progress_cb, a->progress_arg);
9249 break;
9250 default:
9251 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
9252 break;
9254 if (err == NULL) {
9255 got_fileindex_entry_stage_set(ie,
9256 GOT_FILEIDX_STAGE_NONE);
9257 got_fileindex_entry_staged_filetype_set(ie, 0);
9259 break;
9260 case GOT_STATUS_DELETE:
9261 if (a->patch_cb) {
9262 int choice = GOT_PATCH_CHOICE_NONE;
9263 err = (*a->patch_cb)(&choice, a->patch_arg,
9264 staged_status, ie->path, NULL, 1, 1);
9265 if (err)
9266 break;
9267 if (choice == GOT_PATCH_CHOICE_NO)
9268 break;
9269 if (choice != GOT_PATCH_CHOICE_YES) {
9270 err = got_error(GOT_ERR_PATCH_CHOICE);
9271 break;
9274 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
9275 got_fileindex_entry_staged_filetype_set(ie, 0);
9276 err = get_file_status(&status, &sb, ie, ondisk_path,
9277 dirfd, de_name, a->repo);
9278 if (err)
9279 break;
9280 err = (*a->progress_cb)(a->progress_arg, status, relpath);
9281 break;
9283 done:
9284 free(ondisk_path);
9285 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
9286 err = got_error_from_errno("close");
9287 if (blob_base)
9288 got_object_blob_close(blob_base);
9289 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
9290 err = got_error_from_errno("close");
9291 if (blob_staged)
9292 got_object_blob_close(blob_staged);
9293 free(id_str);
9294 free(label_orig);
9295 return err;
9298 const struct got_error *
9299 got_worktree_unstage(struct got_worktree *worktree,
9300 struct got_pathlist_head *paths,
9301 got_worktree_checkout_cb progress_cb, void *progress_arg,
9302 got_worktree_patch_cb patch_cb, void *patch_arg,
9303 struct got_repository *repo)
9305 const struct got_error *err = NULL, *sync_err, *unlockerr;
9306 struct got_pathlist_entry *pe;
9307 struct got_fileindex *fileindex = NULL;
9308 char *fileindex_path = NULL;
9309 struct unstage_path_arg upa;
9311 err = lock_worktree(worktree, LOCK_EX);
9312 if (err)
9313 return err;
9315 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9316 if (err)
9317 goto done;
9319 upa.worktree = worktree;
9320 upa.fileindex = fileindex;
9321 upa.repo = repo;
9322 upa.progress_cb = progress_cb;
9323 upa.progress_arg = progress_arg;
9324 upa.patch_cb = patch_cb;
9325 upa.patch_arg = patch_arg;
9326 TAILQ_FOREACH(pe, paths, entry) {
9327 err = worktree_status(worktree, pe->path, fileindex, repo,
9328 unstage_path, &upa, NULL, NULL, 1, 0);
9329 if (err)
9330 goto done;
9333 sync_err = sync_fileindex(fileindex, fileindex_path);
9334 if (sync_err && err == NULL)
9335 err = sync_err;
9336 done:
9337 free(fileindex_path);
9338 if (fileindex)
9339 got_fileindex_free(fileindex);
9340 unlockerr = lock_worktree(worktree, LOCK_SH);
9341 if (unlockerr && err == NULL)
9342 err = unlockerr;
9343 return err;
9346 struct report_file_info_arg {
9347 struct got_worktree *worktree;
9348 got_worktree_path_info_cb info_cb;
9349 void *info_arg;
9350 struct got_pathlist_head *paths;
9351 got_cancel_cb cancel_cb;
9352 void *cancel_arg;
9355 static const struct got_error *
9356 report_file_info(void *arg, struct got_fileindex_entry *ie)
9358 struct report_file_info_arg *a = arg;
9359 struct got_pathlist_entry *pe;
9360 struct got_object_id blob_id, staged_blob_id, commit_id;
9361 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
9362 struct got_object_id *commit_idp = NULL;
9363 int stage;
9365 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
9366 return got_error(GOT_ERR_CANCELLED);
9368 TAILQ_FOREACH(pe, a->paths, entry) {
9369 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
9370 got_path_is_child(ie->path, pe->path, pe->path_len))
9371 break;
9373 if (pe == NULL) /* not found */
9374 return NULL;
9376 if (got_fileindex_entry_has_blob(ie))
9377 blob_idp = got_fileindex_entry_get_blob_id(&blob_id, ie);
9378 stage = got_fileindex_entry_stage_get(ie);
9379 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
9380 stage == GOT_FILEIDX_STAGE_ADD) {
9381 staged_blob_idp = got_fileindex_entry_get_staged_blob_id(
9382 &staged_blob_id, ie);
9385 if (got_fileindex_entry_has_commit(ie))
9386 commit_idp = got_fileindex_entry_get_commit_id(&commit_id, ie);
9388 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
9389 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
9392 const struct got_error *
9393 got_worktree_path_info(struct got_worktree *worktree,
9394 struct got_pathlist_head *paths,
9395 got_worktree_path_info_cb info_cb, void *info_arg,
9396 got_cancel_cb cancel_cb, void *cancel_arg)
9399 const struct got_error *err = NULL, *unlockerr;
9400 struct got_fileindex *fileindex = NULL;
9401 char *fileindex_path = NULL;
9402 struct report_file_info_arg arg;
9404 err = lock_worktree(worktree, LOCK_SH);
9405 if (err)
9406 return err;
9408 err = open_fileindex(&fileindex, &fileindex_path, worktree);
9409 if (err)
9410 goto done;
9412 arg.worktree = worktree;
9413 arg.info_cb = info_cb;
9414 arg.info_arg = info_arg;
9415 arg.paths = paths;
9416 arg.cancel_cb = cancel_cb;
9417 arg.cancel_arg = cancel_arg;
9418 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
9419 &arg);
9420 done:
9421 free(fileindex_path);
9422 if (fileindex)
9423 got_fileindex_free(fileindex);
9424 unlockerr = lock_worktree(worktree, LOCK_UN);
9425 if (unlockerr && err == NULL)
9426 err = unlockerr;
9427 return err;
9430 static const struct got_error *
9431 patch_check_path(const char *p, char **path, unsigned char *status,
9432 unsigned char *staged_status, struct got_fileindex *fileindex,
9433 struct got_worktree *worktree, struct got_repository *repo)
9435 const struct got_error *err;
9436 struct got_fileindex_entry *ie;
9437 struct stat sb;
9438 char *ondisk_path = NULL;
9440 err = got_worktree_resolve_path(path, worktree, p);
9441 if (err)
9442 return err;
9444 if (asprintf(&ondisk_path, "%s%s%s", worktree->root_path,
9445 *path[0] ? "/" : "", *path) == -1)
9446 return got_error_from_errno("asprintf");
9448 ie = got_fileindex_entry_get(fileindex, *path, strlen(*path));
9449 if (ie) {
9450 *staged_status = get_staged_status(ie);
9451 err = get_file_status(status, &sb, ie, ondisk_path, -1, NULL,
9452 repo);
9453 if (err)
9454 goto done;
9455 } else {
9456 *staged_status = GOT_STATUS_NO_CHANGE;
9457 *status = GOT_STATUS_UNVERSIONED;
9458 if (lstat(ondisk_path, &sb) == -1) {
9459 if (errno != ENOENT) {
9460 err = got_error_from_errno2("lstat",
9461 ondisk_path);
9462 goto done;
9464 *status = GOT_STATUS_NONEXISTENT;
9468 done:
9469 free(ondisk_path);
9470 return err;
9473 static const struct got_error *
9474 patch_can_rm(const char *path, unsigned char status,
9475 unsigned char staged_status)
9477 if (status == GOT_STATUS_NONEXISTENT)
9478 return got_error_set_errno(ENOENT, path);
9479 if (status != GOT_STATUS_NO_CHANGE &&
9480 status != GOT_STATUS_ADD &&
9481 status != GOT_STATUS_MODIFY &&
9482 status != GOT_STATUS_MODE_CHANGE)
9483 return got_error_path(path, GOT_ERR_FILE_STATUS);
9484 if (staged_status == GOT_STATUS_DELETE)
9485 return got_error_path(path, GOT_ERR_FILE_STATUS);
9486 return NULL;
9489 static const struct got_error *
9490 patch_can_add(const char *path, unsigned char status)
9492 if (status != GOT_STATUS_NONEXISTENT)
9493 return got_error_path(path, GOT_ERR_FILE_STATUS);
9494 return NULL;
9497 static const struct got_error *
9498 patch_can_edit(const char *path, unsigned char status,
9499 unsigned char staged_status)
9501 if (status == GOT_STATUS_NONEXISTENT)
9502 return got_error_set_errno(ENOENT, path);
9503 if (status != GOT_STATUS_NO_CHANGE &&
9504 status != GOT_STATUS_ADD &&
9505 status != GOT_STATUS_MODIFY)
9506 return got_error_path(path, GOT_ERR_FILE_STATUS);
9507 if (staged_status == GOT_STATUS_DELETE)
9508 return got_error_path(path, GOT_ERR_FILE_STATUS);
9509 return NULL;
9512 const struct got_error *
9513 got_worktree_patch_prepare(struct got_fileindex **fileindex,
9514 char **fileindex_path, struct got_worktree *worktree)
9516 return open_fileindex(fileindex, fileindex_path, worktree);
9519 const struct got_error *
9520 got_worktree_patch_check_path(const char *old, const char *new,
9521 char **oldpath, char **newpath, struct got_worktree *worktree,
9522 struct got_repository *repo, struct got_fileindex *fileindex)
9524 const struct got_error *err = NULL;
9525 int file_renamed = 0;
9526 unsigned char status_old, staged_status_old;
9527 unsigned char status_new, staged_status_new;
9529 *oldpath = NULL;
9530 *newpath = NULL;
9532 err = patch_check_path(old != NULL ? old : new, oldpath,
9533 &status_old, &staged_status_old, fileindex, worktree, repo);
9534 if (err)
9535 goto done;
9537 err = patch_check_path(new != NULL ? new : old, newpath,
9538 &status_new, &staged_status_new, fileindex, worktree, repo);
9539 if (err)
9540 goto done;
9542 if (old != NULL && new != NULL && strcmp(old, new) != 0)
9543 file_renamed = 1;
9545 if (old != NULL && new == NULL)
9546 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9547 else if (file_renamed) {
9548 err = patch_can_rm(*oldpath, status_old, staged_status_old);
9549 if (err == NULL)
9550 err = patch_can_add(*newpath, status_new);
9551 } else if (old == NULL)
9552 err = patch_can_add(*newpath, status_new);
9553 else
9554 err = patch_can_edit(*newpath, status_new, staged_status_new);
9556 done:
9557 if (err) {
9558 free(*oldpath);
9559 *oldpath = NULL;
9560 free(*newpath);
9561 *newpath = NULL;
9563 return err;
9566 const struct got_error *
9567 got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
9568 struct got_worktree *worktree, struct got_fileindex *fileindex,
9569 got_worktree_checkout_cb progress_cb, void *progress_arg)
9571 struct schedule_addition_args saa;
9573 memset(&saa, 0, sizeof(saa));
9574 saa.worktree = worktree;
9575 saa.fileindex = fileindex;
9576 saa.progress_cb = progress_cb;
9577 saa.progress_arg = progress_arg;
9578 saa.repo = repo;
9580 return worktree_status(worktree, path, fileindex, repo,
9581 schedule_addition, &saa, NULL, NULL, 1, 0);
9584 const struct got_error *
9585 got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
9586 struct got_worktree *worktree, struct got_fileindex *fileindex,
9587 got_worktree_delete_cb progress_cb, void *progress_arg)
9589 struct schedule_deletion_args sda;
9591 memset(&sda, 0, sizeof(sda));
9592 sda.worktree = worktree;
9593 sda.fileindex = fileindex;
9594 sda.progress_cb = progress_cb;
9595 sda.progress_arg = progress_arg;
9596 sda.repo = repo;
9597 sda.delete_local_mods = 0;
9598 sda.keep_on_disk = 0;
9599 sda.ignore_missing_paths = 0;
9600 sda.status_codes = NULL;
9602 return worktree_status(worktree, path, fileindex, repo,
9603 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
9606 const struct got_error *
9607 got_worktree_patch_complete(struct got_fileindex *fileindex,
9608 const char *fileindex_path)
9610 const struct got_error *err = NULL;
9612 err = sync_fileindex(fileindex, fileindex_path);
9613 got_fileindex_free(fileindex);
9615 return err;