Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/stat.h>
18 #include <sys/queue.h>
19 #include <sys/tree.h>
21 #include <dirent.h>
22 #include <limits.h>
23 #include <stddef.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <time.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <sha1.h>
32 #include <zlib.h>
33 #include <fnmatch.h>
34 #include <libgen.h>
35 #include <uuid.h>
36 #include <util.h>
38 #include "got_error.h"
39 #include "got_repository.h"
40 #include "got_reference.h"
41 #include "got_object.h"
42 #include "got_path.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_opentemp.h"
46 #include "got_diff.h"
48 #include "got_lib_worktree.h"
49 #include "got_lib_sha1.h"
50 #include "got_lib_fileindex.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_object_idset.h"
57 #include "got_lib_diff.h"
58 #include "got_lib_gotconfig.h"
60 #ifndef MIN
61 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
62 #endif
64 #define GOT_MERGE_LABEL_MERGED "merged change"
65 #define GOT_MERGE_LABEL_BASE "3-way merge base"
67 static const struct got_error *
68 create_meta_file(const char *path_got, const char *name, const char *content)
69 {
70 const struct got_error *err = NULL;
71 char *path;
73 if (asprintf(&path, "%s/%s", path_got, name) == -1)
74 return got_error_from_errno("asprintf");
76 err = got_path_create_file(path, content);
77 free(path);
78 return err;
79 }
81 static const struct got_error *
82 update_meta_file(const char *path_got, const char *name, const char *content)
83 {
84 const struct got_error *err = NULL;
85 FILE *tmpfile = NULL;
86 char *tmppath = NULL;
87 char *path = NULL;
89 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
90 err = got_error_from_errno("asprintf");
91 path = NULL;
92 goto done;
93 }
95 err = got_opentemp_named(&tmppath, &tmpfile, path);
96 if (err)
97 goto done;
99 if (content) {
100 int len = fprintf(tmpfile, "%s\n", content);
101 if (len != strlen(content) + 1) {
102 err = got_error_from_errno2("fprintf", tmppath);
103 goto done;
107 if (rename(tmppath, path) != 0) {
108 err = got_error_from_errno3("rename", tmppath, path);
109 unlink(tmppath);
110 goto done;
113 done:
114 if (fclose(tmpfile) == EOF && err == NULL)
115 err = got_error_from_errno2("fclose", tmppath);
116 free(tmppath);
117 return err;
120 static const struct got_error *
121 write_head_ref(const char *path_got, struct got_reference *head_ref)
123 const struct got_error *err = NULL;
124 char *refstr = NULL;
126 if (got_ref_is_symbolic(head_ref)) {
127 refstr = got_ref_to_str(head_ref);
128 if (refstr == NULL)
129 return got_error_from_errno("got_ref_to_str");
130 } else {
131 refstr = strdup(got_ref_get_name(head_ref));
132 if (refstr == NULL)
133 return got_error_from_errno("strdup");
135 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
136 free(refstr);
137 return err;
140 const struct got_error *
141 got_worktree_init(const char *path, struct got_reference *head_ref,
142 const char *prefix, struct got_repository *repo)
144 const struct got_error *err = NULL;
145 struct got_object_id *commit_id = NULL;
146 uuid_t uuid;
147 uint32_t uuid_status;
148 int obj_type;
149 char *path_got = NULL;
150 char *formatstr = NULL;
151 char *absprefix = NULL;
152 char *basestr = NULL;
153 char *uuidstr = NULL;
155 if (strcmp(path, got_repo_get_path(repo)) == 0) {
156 err = got_error(GOT_ERR_WORKTREE_REPO);
157 goto done;
160 err = got_ref_resolve(&commit_id, repo, head_ref);
161 if (err)
162 return err;
163 err = got_object_get_type(&obj_type, repo, commit_id);
164 if (err)
165 return err;
166 if (obj_type != GOT_OBJ_TYPE_COMMIT)
167 return got_error(GOT_ERR_OBJ_TYPE);
169 if (!got_path_is_absolute(prefix)) {
170 if (asprintf(&absprefix, "/%s", prefix) == -1)
171 return got_error_from_errno("asprintf");
174 /* Create top-level directory (may already exist). */
175 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
176 err = got_error_from_errno2("mkdir", path);
177 goto done;
180 /* Create .got directory (may already exist). */
181 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
182 err = got_error_from_errno("asprintf");
183 goto done;
185 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
186 err = got_error_from_errno2("mkdir", path_got);
187 goto done;
190 /* Create an empty lock file. */
191 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
192 if (err)
193 goto done;
195 /* Create an empty file index. */
196 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
197 if (err)
198 goto done;
200 /* Write the HEAD reference. */
201 err = write_head_ref(path_got, head_ref);
202 if (err)
203 goto done;
205 /* Record our base commit. */
206 err = got_object_id_str(&basestr, commit_id);
207 if (err)
208 goto done;
209 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
210 if (err)
211 goto done;
213 /* Store path to repository. */
214 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
215 got_repo_get_path(repo));
216 if (err)
217 goto done;
219 /* Store in-repository path prefix. */
220 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
221 absprefix ? absprefix : prefix);
222 if (err)
223 goto done;
225 /* Generate UUID. */
226 uuid_create(&uuid, &uuid_status);
227 if (uuid_status != uuid_s_ok) {
228 err = got_error_uuid(uuid_status, "uuid_create");
229 goto done;
231 uuid_to_string(&uuid, &uuidstr, &uuid_status);
232 if (uuid_status != uuid_s_ok) {
233 err = got_error_uuid(uuid_status, "uuid_to_string");
234 goto done;
236 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
237 if (err)
238 goto done;
240 /* Stamp work tree with format file. */
241 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
242 err = got_error_from_errno("asprintf");
243 goto done;
245 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
246 if (err)
247 goto done;
249 done:
250 free(commit_id);
251 free(path_got);
252 free(formatstr);
253 free(absprefix);
254 free(basestr);
255 free(uuidstr);
256 return err;
259 const struct got_error *
260 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
261 const char *path_prefix)
263 char *absprefix = NULL;
265 if (!got_path_is_absolute(path_prefix)) {
266 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
267 return got_error_from_errno("asprintf");
269 *match = (strcmp(absprefix ? absprefix : path_prefix,
270 worktree->path_prefix) == 0);
271 free(absprefix);
272 return NULL;
275 const char *
276 got_worktree_get_head_ref_name(struct got_worktree *worktree)
278 return worktree->head_ref_name;
281 const struct got_error *
282 got_worktree_set_head_ref(struct got_worktree *worktree,
283 struct got_reference *head_ref)
285 const struct got_error *err = NULL;
286 char *path_got = NULL, *head_ref_name = NULL;
288 if (asprintf(&path_got, "%s/%s", worktree->root_path,
289 GOT_WORKTREE_GOT_DIR) == -1) {
290 err = got_error_from_errno("asprintf");
291 path_got = NULL;
292 goto done;
295 head_ref_name = strdup(got_ref_get_name(head_ref));
296 if (head_ref_name == NULL) {
297 err = got_error_from_errno("strdup");
298 goto done;
301 err = write_head_ref(path_got, head_ref);
302 if (err)
303 goto done;
305 free(worktree->head_ref_name);
306 worktree->head_ref_name = head_ref_name;
307 done:
308 free(path_got);
309 if (err)
310 free(head_ref_name);
311 return err;
314 struct got_object_id *
315 got_worktree_get_base_commit_id(struct got_worktree *worktree)
317 return worktree->base_commit_id;
320 const struct got_error *
321 got_worktree_set_base_commit_id(struct got_worktree *worktree,
322 struct got_repository *repo, struct got_object_id *commit_id)
324 const struct got_error *err;
325 struct got_object *obj = NULL;
326 char *id_str = NULL;
327 char *path_got = NULL;
329 if (asprintf(&path_got, "%s/%s", worktree->root_path,
330 GOT_WORKTREE_GOT_DIR) == -1) {
331 err = got_error_from_errno("asprintf");
332 path_got = NULL;
333 goto done;
336 err = got_object_open(&obj, repo, commit_id);
337 if (err)
338 return err;
340 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
341 err = got_error(GOT_ERR_OBJ_TYPE);
342 goto done;
345 /* Record our base commit. */
346 err = got_object_id_str(&id_str, commit_id);
347 if (err)
348 goto done;
349 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
350 if (err)
351 goto done;
353 free(worktree->base_commit_id);
354 worktree->base_commit_id = got_object_id_dup(commit_id);
355 if (worktree->base_commit_id == NULL) {
356 err = got_error_from_errno("got_object_id_dup");
357 goto done;
359 done:
360 if (obj)
361 got_object_close(obj);
362 free(id_str);
363 free(path_got);
364 return err;
367 const struct got_gotconfig *
368 got_worktree_get_gotconfig(struct got_worktree *worktree)
370 return worktree->gotconfig;
373 static const struct got_error *
374 lock_worktree(struct got_worktree *worktree, int operation)
376 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
377 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
378 : got_error_from_errno2("flock",
379 got_worktree_get_root_path(worktree)));
380 return NULL;
383 static const struct got_error *
384 add_dir_on_disk(struct got_worktree *worktree, const char *path)
386 const struct got_error *err = NULL;
387 char *abspath;
389 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
390 return got_error_from_errno("asprintf");
392 err = got_path_mkdir(abspath);
393 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
394 struct stat sb;
395 err = NULL;
396 if (lstat(abspath, &sb) == -1) {
397 err = got_error_from_errno2("lstat", abspath);
398 } else if (!S_ISDIR(sb.st_mode)) {
399 /* TODO directory is obstructed; do something */
400 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
403 free(abspath);
404 return err;
407 static const struct got_error *
408 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
410 const struct got_error *err = NULL;
411 uint8_t fbuf1[8192];
412 uint8_t fbuf2[8192];
413 size_t flen1 = 0, flen2 = 0;
415 *same = 1;
417 for (;;) {
418 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
419 if (flen1 == 0 && ferror(f1)) {
420 err = got_error_from_errno("fread");
421 break;
423 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
424 if (flen2 == 0 && ferror(f2)) {
425 err = got_error_from_errno("fread");
426 break;
428 if (flen1 == 0) {
429 if (flen2 != 0)
430 *same = 0;
431 break;
432 } else if (flen2 == 0) {
433 if (flen1 != 0)
434 *same = 0;
435 break;
436 } else if (flen1 == flen2) {
437 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
438 *same = 0;
439 break;
441 } else {
442 *same = 0;
443 break;
447 return err;
450 static const struct got_error *
451 check_files_equal(int *same, FILE *f1, FILE *f2)
453 struct stat sb;
454 size_t size1, size2;
456 *same = 1;
458 if (fstat(fileno(f1), &sb) != 0)
459 return got_error_from_errno("fstat");
460 size1 = sb.st_size;
462 if (fstat(fileno(f2), &sb) != 0)
463 return got_error_from_errno("fstat");
464 size2 = sb.st_size;
466 if (size1 != size2) {
467 *same = 0;
468 return NULL;
471 if (fseek(f1, 0L, SEEK_SET) == -1)
472 return got_ferror(f1, GOT_ERR_IO);
473 if (fseek(f2, 0L, SEEK_SET) == -1)
474 return got_ferror(f2, GOT_ERR_IO);
476 return check_file_contents_equal(same, f1, f2);
479 static const struct got_error *
480 copy_file_to_fd(off_t *outsize, FILE *f, int outfd)
482 uint8_t fbuf[65536];
483 size_t flen;
484 ssize_t outlen;
486 *outsize = 0;
488 if (fseek(f, 0L, SEEK_SET) == -1)
489 return got_ferror(f, GOT_ERR_IO);
491 for (;;) {
492 flen = fread(fbuf, 1, sizeof(fbuf), f);
493 if (flen == 0) {
494 if (ferror(f))
495 return got_error_from_errno("fread");
496 if (feof(f))
497 break;
499 outlen = write(outfd, fbuf, flen);
500 if (outlen == -1)
501 return got_error_from_errno("write");
502 if (outlen != flen)
503 return got_error(GOT_ERR_IO);
504 *outsize += outlen;
507 return NULL;
510 static const struct got_error *
511 merge_binary_file(int *overlapcnt, int merged_fd,
512 FILE *f_deriv, FILE *f_orig, FILE *f_deriv2,
513 const char *label_deriv, const char *label_orig, const char *label_deriv2,
514 const char *ondisk_path)
516 const struct got_error *err = NULL;
517 int same_content, changed_deriv, changed_deriv2;
518 int fd_orig = -1, fd_deriv = -1, fd_deriv2 = -1;
519 off_t size_orig = 0, size_deriv = 0, size_deriv2 = 0;
520 char *path_orig = NULL, *path_deriv = NULL, *path_deriv2 = NULL;
521 char *base_path_orig = NULL, *base_path_deriv = NULL;
522 char *base_path_deriv2 = NULL;
524 *overlapcnt = 0;
526 err = check_files_equal(&same_content, f_deriv, f_deriv2);
527 if (err)
528 return err;
530 if (same_content)
531 return copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
533 err = check_files_equal(&same_content, f_deriv, f_orig);
534 if (err)
535 return err;
536 changed_deriv = !same_content;
537 err = check_files_equal(&same_content, f_deriv2, f_orig);
538 if (err)
539 return err;
540 changed_deriv2 = !same_content;
542 if (changed_deriv && changed_deriv2) {
543 *overlapcnt = 1;
544 if (asprintf(&base_path_orig, "%s-orig", ondisk_path) == -1) {
545 err = got_error_from_errno("asprintf");
546 goto done;
548 if (asprintf(&base_path_deriv, "%s-1", ondisk_path) == -1) {
549 err = got_error_from_errno("asprintf");
550 goto done;
552 if (asprintf(&base_path_deriv2, "%s-2", ondisk_path) == -1) {
553 err = got_error_from_errno("asprintf");
554 goto done;
556 err = got_opentemp_named_fd(&path_orig, &fd_orig,
557 base_path_orig);
558 if (err)
559 goto done;
560 err = got_opentemp_named_fd(&path_deriv, &fd_deriv,
561 base_path_deriv);
562 if (err)
563 goto done;
564 err = got_opentemp_named_fd(&path_deriv2, &fd_deriv2,
565 base_path_deriv2);
566 if (err)
567 goto done;
568 err = copy_file_to_fd(&size_orig, f_orig, fd_orig);
569 if (err)
570 goto done;
571 err = copy_file_to_fd(&size_deriv, f_deriv, fd_deriv);
572 if (err)
573 goto done;
574 err = copy_file_to_fd(&size_deriv2, f_deriv2, fd_deriv2);
575 if (err)
576 goto done;
577 if (dprintf(merged_fd, "Binary files differ and cannot be "
578 "merged automatically:\n") < 0) {
579 err = got_error_from_errno("dprintf");
580 goto done;
582 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
583 GOT_DIFF_CONFLICT_MARKER_BEGIN,
584 label_deriv ? " " : "",
585 label_deriv ? label_deriv : "",
586 path_deriv) < 0) {
587 err = got_error_from_errno("dprintf");
588 goto done;
590 if (size_orig > 0) {
591 if (dprintf(merged_fd, "%s%s%s\nfile %s\n",
592 GOT_DIFF_CONFLICT_MARKER_ORIG,
593 label_orig ? " " : "",
594 label_orig ? label_orig : "",
595 path_orig) < 0) {
596 err = got_error_from_errno("dprintf");
597 goto done;
600 if (dprintf(merged_fd, "%s\nfile %s\n%s%s%s\n",
601 GOT_DIFF_CONFLICT_MARKER_SEP,
602 path_deriv2,
603 GOT_DIFF_CONFLICT_MARKER_END,
604 label_deriv2 ? " " : "",
605 label_deriv2 ? label_deriv2 : "") < 0) {
606 err = got_error_from_errno("dprintf");
607 goto done;
609 } else if (changed_deriv)
610 err = copy_file_to_fd(&size_deriv, f_deriv, merged_fd);
611 else if (changed_deriv2)
612 err = copy_file_to_fd(&size_deriv2, f_deriv2, merged_fd);
613 done:
614 if (size_orig == 0 && path_orig && unlink(path_orig) == -1 &&
615 err == NULL)
616 err = got_error_from_errno2("unlink", path_orig);
617 if (fd_orig != -1 && close(fd_orig) == -1 && err == NULL)
618 err = got_error_from_errno2("close", path_orig);
619 if (fd_deriv != -1 && close(fd_deriv) == -1 && err == NULL)
620 err = got_error_from_errno2("close", path_deriv);
621 if (fd_deriv2 != -1 && close(fd_deriv2) == -1 && err == NULL)
622 err = got_error_from_errno2("close", path_deriv2);
623 free(path_orig);
624 free(path_deriv);
625 free(path_deriv2);
626 free(base_path_orig);
627 free(base_path_deriv);
628 free(base_path_deriv2);
629 return err;
632 /*
633 * Perform a 3-way merge where the file f_orig acts as the common
634 * ancestor, the file f_deriv acts as the first derived version,
635 * and the file f_deriv2 acts as the second derived version.
636 * The merge result will be written to a new file at ondisk_path; any
637 * existing file at this path will be replaced.
638 */
639 static const struct got_error *
640 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
641 FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
642 const char *path, uint16_t st_mode,
643 const char *label_orig, const char *label_deriv, const char *label_deriv2,
644 enum got_diff_algorithm diff_algo, struct got_repository *repo,
645 got_worktree_checkout_cb progress_cb, void *progress_arg)
647 const struct got_error *err = NULL;
648 int merged_fd = -1;
649 FILE *f_merged = NULL;
650 char *merged_path = NULL, *base_path = NULL;
651 int overlapcnt = 0;
652 char *parent = NULL;
654 *local_changes_subsumed = 0;
656 err = got_path_dirname(&parent, ondisk_path);
657 if (err)
658 return err;
660 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
661 err = got_error_from_errno("asprintf");
662 goto done;
665 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
666 if (err)
667 goto done;
669 err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
670 f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
671 if (err) {
672 if (err->code != GOT_ERR_FILE_BINARY)
673 goto done;
674 err = merge_binary_file(&overlapcnt, merged_fd, f_deriv,
675 f_orig, f_deriv2, label_deriv, label_orig, label_deriv2,
676 ondisk_path);
677 if (err)
678 goto done;
681 err = (*progress_cb)(progress_arg,
682 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
683 if (err)
684 goto done;
686 if (fsync(merged_fd) != 0) {
687 err = got_error_from_errno("fsync");
688 goto done;
691 f_merged = fdopen(merged_fd, "r");
692 if (f_merged == NULL) {
693 err = got_error_from_errno("fdopen");
694 goto done;
696 merged_fd = -1;
698 /* Check if a clean merge has subsumed all local changes. */
699 if (overlapcnt == 0) {
700 err = check_files_equal(local_changes_subsumed, f_deriv,
701 f_merged);
702 if (err)
703 goto done;
706 if (fchmod(fileno(f_merged), st_mode) != 0) {
707 err = got_error_from_errno2("fchmod", merged_path);
708 goto done;
711 if (rename(merged_path, ondisk_path) != 0) {
712 err = got_error_from_errno3("rename", merged_path,
713 ondisk_path);
714 goto done;
716 done:
717 if (err) {
718 if (merged_path)
719 unlink(merged_path);
721 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
722 err = got_error_from_errno("close");
723 if (f_merged && fclose(f_merged) == EOF && err == NULL)
724 err = got_error_from_errno("fclose");
725 free(merged_path);
726 free(base_path);
727 free(parent);
728 return err;
731 static const struct got_error *
732 update_symlink(const char *ondisk_path, const char *target_path,
733 size_t target_len)
735 /* This is not atomic but matches what 'ln -sf' does. */
736 if (unlink(ondisk_path) == -1)
737 return got_error_from_errno2("unlink", ondisk_path);
738 if (symlink(target_path, ondisk_path) == -1)
739 return got_error_from_errno3("symlink", target_path,
740 ondisk_path);
741 return NULL;
744 /*
745 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
746 * in the work tree with a file that contains conflict markers and the
747 * conflicting target paths of the original version, a "derived version"
748 * of a symlink from an incoming change, and a local version of the symlink.
750 * The original versions's target path can be NULL if it is not available,
751 * such as if both derived versions added a new symlink at the same path.
753 * The incoming derived symlink target is NULL in case the incoming change
754 * has deleted this symlink.
755 */
756 static const struct got_error *
757 install_symlink_conflict(const char *deriv_target,
758 struct got_object_id *deriv_base_commit_id, const char *orig_target,
759 const char *label_orig, const char *local_target, const char *ondisk_path)
761 const struct got_error *err;
762 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
763 FILE *f = NULL;
765 err = got_object_id_str(&id_str, deriv_base_commit_id);
766 if (err)
767 return got_error_from_errno("asprintf");
769 if (asprintf(&label_deriv, "%s: commit %s",
770 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
771 err = got_error_from_errno("asprintf");
772 goto done;
775 err = got_opentemp_named(&path, &f, "got-symlink-conflict");
776 if (err)
777 goto done;
779 if (fchmod(fileno(f), GOT_DEFAULT_FILE_MODE) == -1) {
780 err = got_error_from_errno2("fchmod", path);
781 goto done;
784 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
785 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
786 deriv_target ? deriv_target : "(symlink was deleted)",
787 orig_target ? label_orig : "",
788 orig_target ? "\n" : "",
789 orig_target ? orig_target : "",
790 orig_target ? "\n" : "",
791 GOT_DIFF_CONFLICT_MARKER_SEP,
792 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
793 err = got_error_from_errno2("fprintf", path);
794 goto done;
797 if (unlink(ondisk_path) == -1) {
798 err = got_error_from_errno2("unlink", ondisk_path);
799 goto done;
801 if (rename(path, ondisk_path) == -1) {
802 err = got_error_from_errno3("rename", path, ondisk_path);
803 goto done;
805 done:
806 if (f != NULL && fclose(f) == EOF && err == NULL)
807 err = got_error_from_errno2("fclose", path);
808 free(path);
809 free(id_str);
810 free(label_deriv);
811 return err;
814 /* forward declaration */
815 static const struct got_error *
816 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
817 const char *, const char *, uint16_t, const char *,
818 struct got_blob_object *, struct got_object_id *,
819 struct got_repository *, got_worktree_checkout_cb, void *);
821 /*
822 * Merge a symlink into the work tree, where blob_orig acts as the common
823 * ancestor, deriv_target is the link target of the first derived version,
824 * and the symlink on disk acts as the second derived version.
825 * Assume that contents of both blobs represent symlinks.
826 */
827 static const struct got_error *
828 merge_symlink(struct got_worktree *worktree,
829 struct got_blob_object *blob_orig, const char *ondisk_path,
830 const char *path, const char *label_orig, const char *deriv_target,
831 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
832 got_worktree_checkout_cb progress_cb, void *progress_arg)
834 const struct got_error *err = NULL;
835 char *ancestor_target = NULL;
836 struct stat sb;
837 ssize_t ondisk_len, deriv_len;
838 char ondisk_target[PATH_MAX];
839 int have_local_change = 0;
840 int have_incoming_change = 0;
842 if (lstat(ondisk_path, &sb) == -1)
843 return got_error_from_errno2("lstat", ondisk_path);
845 ondisk_len = readlink(ondisk_path, ondisk_target,
846 sizeof(ondisk_target));
847 if (ondisk_len == -1) {
848 err = got_error_from_errno2("readlink",
849 ondisk_path);
850 goto done;
852 ondisk_target[ondisk_len] = '\0';
854 if (blob_orig) {
855 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
856 if (err)
857 goto done;
860 if (ancestor_target == NULL ||
861 (ondisk_len != strlen(ancestor_target) ||
862 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
863 have_local_change = 1;
865 deriv_len = strlen(deriv_target);
866 if (ancestor_target == NULL ||
867 (deriv_len != strlen(ancestor_target) ||
868 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
869 have_incoming_change = 1;
871 if (!have_local_change && !have_incoming_change) {
872 if (ancestor_target) {
873 /* Both sides made the same change. */
874 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
875 path);
876 } else if (deriv_len == ondisk_len &&
877 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
878 /* Both sides added the same symlink. */
879 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
880 path);
881 } else {
882 /* Both sides added symlinks which don't match. */
883 err = install_symlink_conflict(deriv_target,
884 deriv_base_commit_id, ancestor_target,
885 label_orig, ondisk_target, ondisk_path);
886 if (err)
887 goto done;
888 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
889 path);
891 } else if (!have_local_change && have_incoming_change) {
892 /* Apply the incoming change. */
893 err = update_symlink(ondisk_path, deriv_target,
894 strlen(deriv_target));
895 if (err)
896 goto done;
897 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
898 } else if (have_local_change && have_incoming_change) {
899 if (deriv_len == ondisk_len &&
900 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
901 /* Both sides made the same change. */
902 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
903 path);
904 } else {
905 err = install_symlink_conflict(deriv_target,
906 deriv_base_commit_id, ancestor_target, label_orig,
907 ondisk_target, ondisk_path);
908 if (err)
909 goto done;
910 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
911 path);
915 done:
916 free(ancestor_target);
917 return err;
920 static const struct got_error *
921 dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
923 const struct got_error *err = NULL;
924 char target_path[PATH_MAX];
925 ssize_t target_len;
926 size_t n;
927 FILE *f;
929 *outfile = NULL;
931 f = got_opentemp();
932 if (f == NULL)
933 return got_error_from_errno("got_opentemp");
934 target_len = readlink(ondisk_path, target_path, sizeof(target_path));
935 if (target_len == -1) {
936 err = got_error_from_errno2("readlink", ondisk_path);
937 goto done;
939 n = fwrite(target_path, 1, target_len, f);
940 if (n != target_len) {
941 err = got_ferror(f, GOT_ERR_IO);
942 goto done;
944 if (fflush(f) == EOF) {
945 err = got_error_from_errno("fflush");
946 goto done;
948 if (fseek(f, 0L, SEEK_SET) == -1) {
949 err = got_ferror(f, GOT_ERR_IO);
950 goto done;
952 done:
953 if (err)
954 fclose(f);
955 else
956 *outfile = f;
957 return err;
960 /*
961 * Perform a 3-way merge where blob_orig acts as the common ancestor,
962 * blob_deriv acts as the first derived version, and the file on disk
963 * acts as the second derived version.
964 */
965 static const struct got_error *
966 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
967 struct got_blob_object *blob_orig, const char *ondisk_path,
968 const char *path, uint16_t st_mode, const char *label_orig,
969 struct got_blob_object *blob_deriv,
970 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
971 got_worktree_checkout_cb progress_cb, void *progress_arg)
973 const struct got_error *err = NULL;
974 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
975 char *blob_orig_path = NULL;
976 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
977 char *label_deriv = NULL, *parent = NULL;
979 *local_changes_subsumed = 0;
981 err = got_path_dirname(&parent, ondisk_path);
982 if (err)
983 return err;
985 if (blob_orig) {
986 if (asprintf(&base_path, "%s/got-merge-blob-orig",
987 parent) == -1) {
988 err = got_error_from_errno("asprintf");
989 base_path = NULL;
990 goto done;
993 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
994 if (err)
995 goto done;
996 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
997 blob_orig);
998 if (err)
999 goto done;
1000 free(base_path);
1001 } else {
1003 * No common ancestor exists. This is an "add vs add" conflict
1004 * and we simply use an empty ancestor file to make both files
1005 * appear in the merged result in their entirety.
1007 f_orig = got_opentemp();
1008 if (f_orig == NULL) {
1009 err = got_error_from_errno("got_opentemp");
1010 goto done;
1014 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1015 err = got_error_from_errno("asprintf");
1016 base_path = NULL;
1017 goto done;
1020 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1021 if (err)
1022 goto done;
1023 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1024 blob_deriv);
1025 if (err)
1026 goto done;
1028 err = got_object_id_str(&id_str, deriv_base_commit_id);
1029 if (err)
1030 goto done;
1031 if (asprintf(&label_deriv, "%s: commit %s",
1032 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1033 err = got_error_from_errno("asprintf");
1034 goto done;
1038 * In order the run a 3-way merge with a symlink we copy the symlink's
1039 * target path into a temporary file and use that file with diff3.
1041 if (S_ISLNK(st_mode)) {
1042 err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1043 if (err)
1044 goto done;
1045 } else {
1046 int fd;
1047 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1048 if (fd == -1) {
1049 err = got_error_from_errno2("open", ondisk_path);
1050 goto done;
1052 f_deriv2 = fdopen(fd, "r");
1053 if (f_deriv2 == NULL) {
1054 err = got_error_from_errno2("fdopen", ondisk_path);
1055 close(fd);
1056 goto done;
1060 err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1061 f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1062 NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1063 done:
1064 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1065 err = got_error_from_errno("fclose");
1066 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1067 err = got_error_from_errno("fclose");
1068 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1069 err = got_error_from_errno("fclose");
1070 free(base_path);
1071 if (blob_orig_path) {
1072 unlink(blob_orig_path);
1073 free(blob_orig_path);
1075 if (blob_deriv_path) {
1076 unlink(blob_deriv_path);
1077 free(blob_deriv_path);
1079 free(id_str);
1080 free(label_deriv);
1081 free(parent);
1082 return err;
1085 static const struct got_error *
1086 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1087 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1088 int wt_fd, const char *path, struct got_object_id *blob_id)
1090 const struct got_error *err = NULL;
1091 struct got_fileindex_entry *new_ie;
1093 *new_iep = NULL;
1095 err = got_fileindex_entry_alloc(&new_ie, path);
1096 if (err)
1097 return err;
1099 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1100 blob_id->sha1, base_commit_id->sha1, 1);
1101 if (err)
1102 goto done;
1104 err = got_fileindex_entry_add(fileindex, new_ie);
1105 done:
1106 if (err)
1107 got_fileindex_entry_free(new_ie);
1108 else
1109 *new_iep = new_ie;
1110 return err;
1113 static mode_t
1114 get_ondisk_perms(int executable, mode_t st_mode)
1116 mode_t xbits = S_IXUSR;
1118 if (executable) {
1119 /* Map read bits to execute bits. */
1120 if (st_mode & S_IRGRP)
1121 xbits |= S_IXGRP;
1122 if (st_mode & S_IROTH)
1123 xbits |= S_IXOTH;
1124 return st_mode | xbits;
1127 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1130 /* forward declaration */
1131 static const struct got_error *
1132 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1133 const char *path, mode_t te_mode, mode_t st_mode,
1134 struct got_blob_object *blob, int restoring_missing_file,
1135 int reverting_versioned_file, int installing_bad_symlink,
1136 int path_is_unversioned, struct got_repository *repo,
1137 got_worktree_checkout_cb progress_cb, void *progress_arg);
1140 * This function assumes that the provided symlink target points at a
1141 * safe location in the work tree!
1143 static const struct got_error *
1144 replace_existing_symlink(int *did_something, const char *ondisk_path,
1145 const char *target_path, size_t target_len)
1147 const struct got_error *err = NULL;
1148 ssize_t elen;
1149 char etarget[PATH_MAX];
1150 int fd;
1152 *did_something = 0;
1155 * "Bad" symlinks (those pointing outside the work tree or into the
1156 * .got directory) are installed in the work tree as a regular file
1157 * which contains the bad symlink target path.
1158 * The new symlink target has already been checked for safety by our
1159 * caller. If we can successfully open a regular file then we simply
1160 * replace this file with a symlink below.
1162 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW | O_CLOEXEC);
1163 if (fd == -1) {
1164 if (!got_err_open_nofollow_on_symlink())
1165 return got_error_from_errno2("open", ondisk_path);
1167 /* We are updating an existing on-disk symlink. */
1168 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1169 if (elen == -1)
1170 return got_error_from_errno2("readlink", ondisk_path);
1172 if (elen == target_len &&
1173 memcmp(etarget, target_path, target_len) == 0)
1174 return NULL; /* nothing to do */
1177 *did_something = 1;
1178 err = update_symlink(ondisk_path, target_path, target_len);
1179 if (fd != -1 && close(fd) == -1 && err == NULL)
1180 err = got_error_from_errno2("close", ondisk_path);
1181 return err;
1184 static const struct got_error *
1185 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1186 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1188 const struct got_error *err = NULL;
1189 char canonpath[PATH_MAX];
1190 char *path_got = NULL;
1192 *is_bad_symlink = 0;
1194 if (target_len >= sizeof(canonpath)) {
1195 *is_bad_symlink = 1;
1196 return NULL;
1200 * We do not use realpath(3) to resolve the symlink's target
1201 * path because we don't want to resolve symlinks recursively.
1202 * Instead we make the path absolute and then canonicalize it.
1203 * Relative symlink target lookup should begin at the directory
1204 * in which the blob object is being installed.
1206 if (!got_path_is_absolute(target_path)) {
1207 char *abspath, *parent;
1208 err = got_path_dirname(&parent, ondisk_path);
1209 if (err)
1210 return err;
1211 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1212 free(parent);
1213 return got_error_from_errno("asprintf");
1215 free(parent);
1216 if (strlen(abspath) >= sizeof(canonpath)) {
1217 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1218 free(abspath);
1219 return err;
1221 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1222 free(abspath);
1223 if (err)
1224 return err;
1225 } else {
1226 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1227 if (err)
1228 return err;
1231 /* Only allow symlinks pointing at paths within the work tree. */
1232 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1233 *is_bad_symlink = 1;
1234 return NULL;
1237 /* Do not allow symlinks pointing into the .got directory. */
1238 if (asprintf(&path_got, "%s/%s", wtroot_path,
1239 GOT_WORKTREE_GOT_DIR) == -1)
1240 return got_error_from_errno("asprintf");
1241 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1242 *is_bad_symlink = 1;
1244 free(path_got);
1245 return NULL;
1248 static const struct got_error *
1249 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1250 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1251 int restoring_missing_file, int reverting_versioned_file,
1252 int path_is_unversioned, int allow_bad_symlinks,
1253 struct got_repository *repo,
1254 got_worktree_checkout_cb progress_cb, void *progress_arg)
1256 const struct got_error *err = NULL;
1257 char target_path[PATH_MAX];
1258 size_t len, target_len = 0;
1259 char *path_got = NULL;
1260 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1261 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1263 *is_bad_symlink = 0;
1266 * Blob object content specifies the target path of the link.
1267 * If a symbolic link cannot be installed we instead create
1268 * a regular file which contains the link target path stored
1269 * in the blob object.
1271 do {
1272 err = got_object_blob_read_block(&len, blob);
1273 if (len + target_len >= sizeof(target_path)) {
1274 /* Path too long; install as a regular file. */
1275 *is_bad_symlink = 1;
1276 got_object_blob_rewind(blob);
1277 return install_blob(worktree, ondisk_path, path,
1278 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1279 restoring_missing_file, reverting_versioned_file,
1280 1, path_is_unversioned, repo, progress_cb,
1281 progress_arg);
1283 if (len > 0) {
1284 /* Skip blob object header first time around. */
1285 memcpy(target_path + target_len, buf + hdrlen,
1286 len - hdrlen);
1287 target_len += len - hdrlen;
1288 hdrlen = 0;
1290 } while (len != 0);
1291 target_path[target_len] = '\0';
1293 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1294 ondisk_path, worktree->root_path);
1295 if (err)
1296 return err;
1298 if (*is_bad_symlink && !allow_bad_symlinks) {
1299 /* install as a regular file */
1300 got_object_blob_rewind(blob);
1301 err = install_blob(worktree, ondisk_path, path,
1302 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1303 restoring_missing_file, reverting_versioned_file, 1,
1304 path_is_unversioned, repo, progress_cb, progress_arg);
1305 goto done;
1308 if (symlink(target_path, ondisk_path) == -1) {
1309 if (errno == EEXIST) {
1310 int symlink_replaced;
1311 if (path_is_unversioned) {
1312 err = (*progress_cb)(progress_arg,
1313 GOT_STATUS_UNVERSIONED, path);
1314 goto done;
1316 err = replace_existing_symlink(&symlink_replaced,
1317 ondisk_path, target_path, target_len);
1318 if (err)
1319 goto done;
1320 if (progress_cb) {
1321 if (symlink_replaced) {
1322 err = (*progress_cb)(progress_arg,
1323 reverting_versioned_file ?
1324 GOT_STATUS_REVERT :
1325 GOT_STATUS_UPDATE, path);
1326 } else {
1327 err = (*progress_cb)(progress_arg,
1328 GOT_STATUS_EXISTS, path);
1331 goto done; /* Nothing else to do. */
1334 if (errno == ENOENT) {
1335 char *parent;
1336 err = got_path_dirname(&parent, ondisk_path);
1337 if (err)
1338 goto done;
1339 err = add_dir_on_disk(worktree, parent);
1340 free(parent);
1341 if (err)
1342 goto done;
1344 * Retry, and fall through to error handling
1345 * below if this second attempt fails.
1347 if (symlink(target_path, ondisk_path) != -1) {
1348 err = NULL; /* success */
1349 goto done;
1353 /* Handle errors from first or second creation attempt. */
1354 if (errno == ENAMETOOLONG) {
1355 /* bad target path; install as a regular file */
1356 *is_bad_symlink = 1;
1357 got_object_blob_rewind(blob);
1358 err = install_blob(worktree, ondisk_path, path,
1359 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1360 restoring_missing_file, reverting_versioned_file, 1,
1361 path_is_unversioned, repo,
1362 progress_cb, progress_arg);
1363 } else if (errno == ENOTDIR) {
1364 err = got_error_path(ondisk_path,
1365 GOT_ERR_FILE_OBSTRUCTED);
1366 } else {
1367 err = got_error_from_errno3("symlink",
1368 target_path, ondisk_path);
1370 } else if (progress_cb)
1371 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1372 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1373 done:
1374 free(path_got);
1375 return err;
1378 static const struct got_error *
1379 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1380 const char *path, mode_t te_mode, mode_t st_mode,
1381 struct got_blob_object *blob, int restoring_missing_file,
1382 int reverting_versioned_file, int installing_bad_symlink,
1383 int path_is_unversioned, struct got_repository *repo,
1384 got_worktree_checkout_cb progress_cb, void *progress_arg)
1386 const struct got_error *err = NULL;
1387 int fd = -1;
1388 size_t len, hdrlen;
1389 int update = 0;
1390 char *tmppath = NULL;
1392 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW |
1393 O_CLOEXEC, GOT_DEFAULT_FILE_MODE);
1394 if (fd == -1) {
1395 if (errno == ENOENT) {
1396 char *parent;
1397 err = got_path_dirname(&parent, path);
1398 if (err)
1399 return err;
1400 err = add_dir_on_disk(worktree, parent);
1401 free(parent);
1402 if (err)
1403 return err;
1404 fd = open(ondisk_path,
1405 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
1406 GOT_DEFAULT_FILE_MODE);
1407 if (fd == -1)
1408 return got_error_from_errno2("open",
1409 ondisk_path);
1410 } else if (errno == EEXIST) {
1411 if (path_is_unversioned) {
1412 err = (*progress_cb)(progress_arg,
1413 GOT_STATUS_UNVERSIONED, path);
1414 goto done;
1416 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1417 !S_ISREG(st_mode) && !installing_bad_symlink) {
1418 /* TODO file is obstructed; do something */
1419 err = got_error_path(ondisk_path,
1420 GOT_ERR_FILE_OBSTRUCTED);
1421 goto done;
1422 } else {
1423 err = got_opentemp_named_fd(&tmppath, &fd,
1424 ondisk_path);
1425 if (err)
1426 goto done;
1427 update = 1;
1429 } else
1430 return got_error_from_errno2("open", ondisk_path);
1433 if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1434 err = got_error_from_errno2("fchmod",
1435 update ? tmppath : ondisk_path);
1436 goto done;
1439 if (progress_cb) {
1440 if (restoring_missing_file)
1441 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1442 path);
1443 else if (reverting_versioned_file)
1444 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1445 path);
1446 else
1447 err = (*progress_cb)(progress_arg,
1448 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1449 if (err)
1450 goto done;
1453 hdrlen = got_object_blob_get_hdrlen(blob);
1454 do {
1455 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1456 err = got_object_blob_read_block(&len, blob);
1457 if (err)
1458 break;
1459 if (len > 0) {
1460 /* Skip blob object header first time around. */
1461 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1462 if (outlen == -1) {
1463 err = got_error_from_errno("write");
1464 goto done;
1465 } else if (outlen != len - hdrlen) {
1466 err = got_error(GOT_ERR_IO);
1467 goto done;
1469 hdrlen = 0;
1471 } while (len != 0);
1473 if (fsync(fd) != 0) {
1474 err = got_error_from_errno("fsync");
1475 goto done;
1478 if (update) {
1479 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1480 err = got_error_from_errno2("unlink", ondisk_path);
1481 goto done;
1483 if (rename(tmppath, ondisk_path) != 0) {
1484 err = got_error_from_errno3("rename", tmppath,
1485 ondisk_path);
1486 goto done;
1488 free(tmppath);
1489 tmppath = NULL;
1492 done:
1493 if (fd != -1 && close(fd) == -1 && err == NULL)
1494 err = got_error_from_errno("close");
1495 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1496 err = got_error_from_errno2("unlink", tmppath);
1497 free(tmppath);
1498 return err;
1501 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1502 static const struct got_error *
1503 get_modified_file_content_status(unsigned char *status, FILE *f)
1505 const struct got_error *err = NULL;
1506 const char *markers[3] = {
1507 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1508 GOT_DIFF_CONFLICT_MARKER_SEP,
1509 GOT_DIFF_CONFLICT_MARKER_END
1511 int i = 0;
1512 char *line = NULL;
1513 size_t linesize = 0;
1514 ssize_t linelen;
1516 while (*status == GOT_STATUS_MODIFY) {
1517 linelen = getline(&line, &linesize, f);
1518 if (linelen == -1) {
1519 if (feof(f))
1520 break;
1521 err = got_ferror(f, GOT_ERR_IO);
1522 break;
1525 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1526 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1527 == 0)
1528 *status = GOT_STATUS_CONFLICT;
1529 else
1530 i++;
1533 free(line);
1535 return err;
1538 static int
1539 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1541 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1542 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1545 static int
1546 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1548 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1549 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1550 ie->mtime_sec == sb->st_mtim.tv_sec &&
1551 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1552 ie->size == (sb->st_size & 0xffffffff) &&
1553 !xbit_differs(ie, sb->st_mode));
1556 static unsigned char
1557 get_staged_status(struct got_fileindex_entry *ie)
1559 switch (got_fileindex_entry_stage_get(ie)) {
1560 case GOT_FILEIDX_STAGE_ADD:
1561 return GOT_STATUS_ADD;
1562 case GOT_FILEIDX_STAGE_DELETE:
1563 return GOT_STATUS_DELETE;
1564 case GOT_FILEIDX_STAGE_MODIFY:
1565 return GOT_STATUS_MODIFY;
1566 default:
1567 return GOT_STATUS_NO_CHANGE;
1571 static const struct got_error *
1572 get_symlink_modification_status(unsigned char *status,
1573 struct got_fileindex_entry *ie, const char *abspath,
1574 int dirfd, const char *de_name, struct got_blob_object *blob)
1576 const struct got_error *err = NULL;
1577 char target_path[PATH_MAX];
1578 char etarget[PATH_MAX];
1579 ssize_t elen;
1580 size_t len, target_len = 0;
1581 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1582 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1584 *status = GOT_STATUS_NO_CHANGE;
1586 /* Blob object content specifies the target path of the link. */
1587 do {
1588 err = got_object_blob_read_block(&len, blob);
1589 if (err)
1590 return err;
1591 if (len + target_len >= sizeof(target_path)) {
1593 * Should not happen. The blob contents were OK
1594 * when this symlink was installed.
1596 return got_error(GOT_ERR_NO_SPACE);
1598 if (len > 0) {
1599 /* Skip blob object header first time around. */
1600 memcpy(target_path + target_len, buf + hdrlen,
1601 len - hdrlen);
1602 target_len += len - hdrlen;
1603 hdrlen = 0;
1605 } while (len != 0);
1606 target_path[target_len] = '\0';
1608 if (dirfd != -1) {
1609 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1610 if (elen == -1)
1611 return got_error_from_errno2("readlinkat", abspath);
1612 } else {
1613 elen = readlink(abspath, etarget, sizeof(etarget));
1614 if (elen == -1)
1615 return got_error_from_errno2("readlink", abspath);
1618 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1619 *status = GOT_STATUS_MODIFY;
1621 return NULL;
1624 static const struct got_error *
1625 get_file_status(unsigned char *status, struct stat *sb,
1626 struct got_fileindex_entry *ie, const char *abspath,
1627 int dirfd, const char *de_name, struct got_repository *repo)
1629 const struct got_error *err = NULL;
1630 struct got_object_id id;
1631 size_t hdrlen;
1632 int fd = -1;
1633 FILE *f = NULL;
1634 uint8_t fbuf[8192];
1635 struct got_blob_object *blob = NULL;
1636 size_t flen, blen;
1637 unsigned char staged_status = get_staged_status(ie);
1639 *status = GOT_STATUS_NO_CHANGE;
1640 memset(sb, 0, sizeof(*sb));
1643 * Whenever the caller provides a directory descriptor and a
1644 * directory entry name for the file, use them! This prevents
1645 * race conditions if filesystem paths change beneath our feet.
1647 if (dirfd != -1) {
1648 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1649 if (errno == ENOENT) {
1650 if (got_fileindex_entry_has_file_on_disk(ie))
1651 *status = GOT_STATUS_MISSING;
1652 else
1653 *status = GOT_STATUS_DELETE;
1654 goto done;
1656 err = got_error_from_errno2("fstatat", abspath);
1657 goto done;
1659 } else {
1660 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1661 if (fd == -1 && errno != ENOENT &&
1662 !got_err_open_nofollow_on_symlink())
1663 return got_error_from_errno2("open", abspath);
1664 else if (fd == -1 && got_err_open_nofollow_on_symlink()) {
1665 if (lstat(abspath, sb) == -1)
1666 return got_error_from_errno2("lstat", abspath);
1667 } else if (fd == -1 || fstat(fd, sb) == -1) {
1668 if (errno == ENOENT) {
1669 if (got_fileindex_entry_has_file_on_disk(ie))
1670 *status = GOT_STATUS_MISSING;
1671 else
1672 *status = GOT_STATUS_DELETE;
1673 goto done;
1675 err = got_error_from_errno2("fstat", abspath);
1676 goto done;
1680 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1681 *status = GOT_STATUS_OBSTRUCTED;
1682 goto done;
1685 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1686 *status = GOT_STATUS_DELETE;
1687 goto done;
1688 } else if (!got_fileindex_entry_has_blob(ie) &&
1689 staged_status != GOT_STATUS_ADD) {
1690 *status = GOT_STATUS_ADD;
1691 goto done;
1694 if (!stat_info_differs(ie, sb))
1695 goto done;
1697 if (S_ISLNK(sb->st_mode) &&
1698 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1699 *status = GOT_STATUS_MODIFY;
1700 goto done;
1703 if (staged_status == GOT_STATUS_MODIFY ||
1704 staged_status == GOT_STATUS_ADD)
1705 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1706 else
1707 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1709 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1710 if (err)
1711 goto done;
1713 if (S_ISLNK(sb->st_mode)) {
1714 err = get_symlink_modification_status(status, ie,
1715 abspath, dirfd, de_name, blob);
1716 goto done;
1719 if (dirfd != -1) {
1720 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
1721 if (fd == -1) {
1722 err = got_error_from_errno2("openat", abspath);
1723 goto done;
1727 f = fdopen(fd, "r");
1728 if (f == NULL) {
1729 err = got_error_from_errno2("fdopen", abspath);
1730 goto done;
1732 fd = -1;
1733 hdrlen = got_object_blob_get_hdrlen(blob);
1734 for (;;) {
1735 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1736 err = got_object_blob_read_block(&blen, blob);
1737 if (err)
1738 goto done;
1739 /* Skip length of blob object header first time around. */
1740 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1741 if (flen == 0 && ferror(f)) {
1742 err = got_error_from_errno("fread");
1743 goto done;
1745 if (blen - hdrlen == 0) {
1746 if (flen != 0)
1747 *status = GOT_STATUS_MODIFY;
1748 break;
1749 } else if (flen == 0) {
1750 if (blen - hdrlen != 0)
1751 *status = GOT_STATUS_MODIFY;
1752 break;
1753 } else if (blen - hdrlen == flen) {
1754 /* Skip blob object header first time around. */
1755 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1756 *status = GOT_STATUS_MODIFY;
1757 break;
1759 } else {
1760 *status = GOT_STATUS_MODIFY;
1761 break;
1763 hdrlen = 0;
1766 if (*status == GOT_STATUS_MODIFY) {
1767 rewind(f);
1768 err = get_modified_file_content_status(status, f);
1769 } else if (xbit_differs(ie, sb->st_mode))
1770 *status = GOT_STATUS_MODE_CHANGE;
1771 done:
1772 if (blob)
1773 got_object_blob_close(blob);
1774 if (f != NULL && fclose(f) == EOF && err == NULL)
1775 err = got_error_from_errno2("fclose", abspath);
1776 if (fd != -1 && close(fd) == -1 && err == NULL)
1777 err = got_error_from_errno2("close", abspath);
1778 return err;
1782 * Update timestamps in the file index if a file is unmodified and
1783 * we had to run a full content comparison to find out.
1785 static const struct got_error *
1786 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1787 struct got_fileindex_entry *ie, struct stat *sb)
1789 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1790 return got_fileindex_entry_update(ie, wt_fd, path,
1791 ie->blob_sha1, ie->commit_sha1, 1);
1793 return NULL;
1796 static const struct got_error *
1797 update_blob(struct got_worktree *worktree,
1798 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1799 struct got_tree_entry *te, const char *path,
1800 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1801 void *progress_arg)
1803 const struct got_error *err = NULL;
1804 struct got_blob_object *blob = NULL;
1805 char *ondisk_path;
1806 unsigned char status = GOT_STATUS_NO_CHANGE;
1807 struct stat sb;
1809 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1810 return got_error_from_errno("asprintf");
1812 if (ie) {
1813 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1814 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1815 goto done;
1817 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1818 repo);
1819 if (err)
1820 goto done;
1821 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1822 sb.st_mode = got_fileindex_perms_to_st(ie);
1823 } else {
1824 if (stat(ondisk_path, &sb) == -1) {
1825 if (errno != ENOENT) {
1826 err = got_error_from_errno2("stat",
1827 ondisk_path);
1828 goto done;
1830 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1831 status = GOT_STATUS_UNVERSIONED;
1832 } else {
1833 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1834 status = GOT_STATUS_UNVERSIONED;
1835 else
1836 status = GOT_STATUS_OBSTRUCTED;
1840 if (status == GOT_STATUS_OBSTRUCTED) {
1841 if (ie)
1842 got_fileindex_entry_mark_skipped(ie);
1843 err = (*progress_cb)(progress_arg, status, path);
1844 goto done;
1846 if (status == GOT_STATUS_CONFLICT) {
1847 if (ie)
1848 got_fileindex_entry_mark_skipped(ie);
1849 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1850 path);
1851 goto done;
1854 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1855 (S_ISLNK(te->mode) ||
1856 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1858 * This is a regular file or an installed bad symlink.
1859 * If the file index indicates that this file is already
1860 * up-to-date with respect to the repository we can skip
1861 * updating contents of this file.
1863 if (got_fileindex_entry_has_commit(ie) &&
1864 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1865 SHA1_DIGEST_LENGTH) == 0) {
1866 /* Same commit. */
1867 err = sync_timestamps(worktree->root_fd,
1868 path, status, ie, &sb);
1869 if (err)
1870 goto done;
1871 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1872 path);
1873 goto done;
1875 if (got_fileindex_entry_has_blob(ie) &&
1876 memcmp(ie->blob_sha1, te->id.sha1,
1877 SHA1_DIGEST_LENGTH) == 0) {
1878 /* Different commit but the same blob. */
1879 err = sync_timestamps(worktree->root_fd,
1880 path, status, ie, &sb);
1881 if (err)
1882 goto done;
1883 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1884 path);
1885 goto done;
1889 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1890 if (err)
1891 goto done;
1893 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1894 int update_timestamps;
1895 struct got_blob_object *blob2 = NULL;
1896 char *label_orig = NULL;
1897 if (got_fileindex_entry_has_blob(ie)) {
1898 struct got_object_id id2;
1899 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1900 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1901 if (err)
1902 goto done;
1904 if (got_fileindex_entry_has_commit(ie)) {
1905 char id_str[SHA1_DIGEST_STRING_LENGTH];
1906 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1907 sizeof(id_str)) == NULL) {
1908 err = got_error_path(id_str,
1909 GOT_ERR_BAD_OBJ_ID_STR);
1910 goto done;
1912 if (asprintf(&label_orig, "%s: commit %s",
1913 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1914 err = got_error_from_errno("asprintf");
1915 goto done;
1918 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1919 char *link_target;
1920 err = got_object_blob_read_to_str(&link_target, blob);
1921 if (err)
1922 goto done;
1923 err = merge_symlink(worktree, blob2, ondisk_path, path,
1924 label_orig, link_target, worktree->base_commit_id,
1925 repo, progress_cb, progress_arg);
1926 free(link_target);
1927 } else {
1928 err = merge_blob(&update_timestamps, worktree, blob2,
1929 ondisk_path, path, sb.st_mode, label_orig, blob,
1930 worktree->base_commit_id, repo,
1931 progress_cb, progress_arg);
1933 free(label_orig);
1934 if (blob2)
1935 got_object_blob_close(blob2);
1936 if (err)
1937 goto done;
1939 * Do not update timestamps of files with local changes.
1940 * Otherwise, a future status walk would treat them as
1941 * unmodified files again.
1943 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1944 blob->id.sha1, worktree->base_commit_id->sha1,
1945 update_timestamps);
1946 } else if (status == GOT_STATUS_MODE_CHANGE) {
1947 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1948 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1949 } else if (status == GOT_STATUS_DELETE) {
1950 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1951 if (err)
1952 goto done;
1953 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
1954 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1955 if (err)
1956 goto done;
1957 } else {
1958 int is_bad_symlink = 0;
1959 if (S_ISLNK(te->mode)) {
1960 err = install_symlink(&is_bad_symlink, worktree,
1961 ondisk_path, path, blob,
1962 status == GOT_STATUS_MISSING, 0,
1963 status == GOT_STATUS_UNVERSIONED, 0,
1964 repo, progress_cb, progress_arg);
1965 } else {
1966 err = install_blob(worktree, ondisk_path, path,
1967 te->mode, sb.st_mode, blob,
1968 status == GOT_STATUS_MISSING, 0, 0,
1969 status == GOT_STATUS_UNVERSIONED, repo,
1970 progress_cb, progress_arg);
1972 if (err)
1973 goto done;
1975 if (ie) {
1976 err = got_fileindex_entry_update(ie,
1977 worktree->root_fd, path, blob->id.sha1,
1978 worktree->base_commit_id->sha1, 1);
1979 } else {
1980 err = create_fileindex_entry(&ie, fileindex,
1981 worktree->base_commit_id, worktree->root_fd, path,
1982 &blob->id);
1984 if (err)
1985 goto done;
1987 if (is_bad_symlink) {
1988 got_fileindex_entry_filetype_set(ie,
1989 GOT_FILEIDX_MODE_BAD_SYMLINK);
1992 got_object_blob_close(blob);
1993 done:
1994 free(ondisk_path);
1995 return err;
1998 static const struct got_error *
1999 remove_ondisk_file(const char *root_path, const char *path)
2001 const struct got_error *err = NULL;
2002 char *ondisk_path = NULL, *parent = NULL;
2004 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2005 return got_error_from_errno("asprintf");
2007 if (unlink(ondisk_path) == -1) {
2008 if (errno != ENOENT)
2009 err = got_error_from_errno2("unlink", ondisk_path);
2010 } else {
2011 size_t root_len = strlen(root_path);
2012 err = got_path_dirname(&parent, ondisk_path);
2013 if (err)
2014 goto done;
2015 while (got_path_cmp(parent, root_path,
2016 strlen(parent), root_len) != 0) {
2017 free(ondisk_path);
2018 ondisk_path = parent;
2019 parent = NULL;
2020 if (rmdir(ondisk_path) == -1) {
2021 if (errno != ENOTEMPTY)
2022 err = got_error_from_errno2("rmdir",
2023 ondisk_path);
2024 break;
2026 err = got_path_dirname(&parent, ondisk_path);
2027 if (err)
2028 break;
2031 done:
2032 free(ondisk_path);
2033 free(parent);
2034 return err;
2037 static const struct got_error *
2038 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2039 struct got_fileindex_entry *ie, struct got_repository *repo,
2040 got_worktree_checkout_cb progress_cb, void *progress_arg)
2042 const struct got_error *err = NULL;
2043 unsigned char status;
2044 struct stat sb;
2045 char *ondisk_path;
2047 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2048 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2050 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2051 == -1)
2052 return got_error_from_errno("asprintf");
2054 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2055 if (err)
2056 goto done;
2058 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2059 char ondisk_target[PATH_MAX];
2060 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2061 sizeof(ondisk_target));
2062 if (ondisk_len == -1) {
2063 err = got_error_from_errno2("readlink", ondisk_path);
2064 goto done;
2066 ondisk_target[ondisk_len] = '\0';
2067 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2068 NULL, NULL, /* XXX pass common ancestor info? */
2069 ondisk_target, ondisk_path);
2070 if (err)
2071 goto done;
2072 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2073 ie->path);
2074 goto done;
2077 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2078 status == GOT_STATUS_ADD) {
2079 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2080 if (err)
2081 goto done;
2083 * Preserve the working file and change the deleted blob's
2084 * entry into a schedule-add entry.
2086 err = got_fileindex_entry_update(ie, worktree->root_fd,
2087 ie->path, NULL, NULL, 0);
2088 } else {
2089 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2090 if (err)
2091 goto done;
2092 if (status == GOT_STATUS_NO_CHANGE) {
2093 err = remove_ondisk_file(worktree->root_path, ie->path);
2094 if (err)
2095 goto done;
2097 got_fileindex_entry_remove(fileindex, ie);
2099 done:
2100 free(ondisk_path);
2101 return err;
2104 struct diff_cb_arg {
2105 struct got_fileindex *fileindex;
2106 struct got_worktree *worktree;
2107 struct got_repository *repo;
2108 got_worktree_checkout_cb progress_cb;
2109 void *progress_arg;
2110 got_cancel_cb cancel_cb;
2111 void *cancel_arg;
2114 static const struct got_error *
2115 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2116 struct got_tree_entry *te, const char *parent_path)
2118 struct diff_cb_arg *a = arg;
2120 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2121 return got_error(GOT_ERR_CANCELLED);
2123 return update_blob(a->worktree, a->fileindex, ie, te,
2124 ie->path, a->repo, a->progress_cb, a->progress_arg);
2127 static const struct got_error *
2128 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2130 struct diff_cb_arg *a = arg;
2132 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2133 return got_error(GOT_ERR_CANCELLED);
2135 return delete_blob(a->worktree, a->fileindex, ie,
2136 a->repo, a->progress_cb, a->progress_arg);
2139 static const struct got_error *
2140 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2142 struct diff_cb_arg *a = arg;
2143 const struct got_error *err;
2144 char *path;
2146 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2147 return got_error(GOT_ERR_CANCELLED);
2149 if (got_object_tree_entry_is_submodule(te))
2150 return NULL;
2152 if (asprintf(&path, "%s%s%s", parent_path,
2153 parent_path[0] ? "/" : "", te->name)
2154 == -1)
2155 return got_error_from_errno("asprintf");
2157 if (S_ISDIR(te->mode))
2158 err = add_dir_on_disk(a->worktree, path);
2159 else
2160 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2161 a->repo, a->progress_cb, a->progress_arg);
2163 free(path);
2164 return err;
2167 const struct got_error *
2168 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2170 uint32_t uuid_status;
2172 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2173 if (uuid_status != uuid_s_ok) {
2174 *uuidstr = NULL;
2175 return got_error_uuid(uuid_status, "uuid_to_string");
2178 return NULL;
2181 static const struct got_error *
2182 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2184 const struct got_error *err = NULL;
2185 char *uuidstr = NULL;
2187 *refname = NULL;
2189 err = got_worktree_get_uuid(&uuidstr, worktree);
2190 if (err)
2191 return err;
2193 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2194 err = got_error_from_errno("asprintf");
2195 *refname = NULL;
2197 free(uuidstr);
2198 return err;
2201 const struct got_error *
2202 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2204 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2207 static const struct got_error *
2208 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2210 return get_ref_name(refname, worktree,
2211 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2214 static const struct got_error *
2215 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2217 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2220 static const struct got_error *
2221 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2223 return get_ref_name(refname, worktree,
2224 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2227 static const struct got_error *
2228 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2230 return get_ref_name(refname, worktree,
2231 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2234 static const struct got_error *
2235 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2237 return get_ref_name(refname, worktree,
2238 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2241 static const struct got_error *
2242 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2244 return get_ref_name(refname, worktree,
2245 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2248 static const struct got_error *
2249 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2251 return get_ref_name(refname, worktree,
2252 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2255 static const struct got_error *
2256 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2258 return get_ref_name(refname, worktree,
2259 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2262 const struct got_error *
2263 got_worktree_get_histedit_script_path(char **path,
2264 struct got_worktree *worktree)
2266 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2267 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2268 *path = NULL;
2269 return got_error_from_errno("asprintf");
2271 return NULL;
2274 static const struct got_error *
2275 get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2277 return get_ref_name(refname, worktree,
2278 GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2281 static const struct got_error *
2282 get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2284 return get_ref_name(refname, worktree,
2285 GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2289 * Prevent Git's garbage collector from deleting our base commit by
2290 * setting a reference to our base commit's ID.
2292 static const struct got_error *
2293 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2295 const struct got_error *err = NULL;
2296 struct got_reference *ref = NULL;
2297 char *refname;
2299 err = got_worktree_get_base_ref_name(&refname, worktree);
2300 if (err)
2301 return err;
2303 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2304 if (err)
2305 goto done;
2307 err = got_ref_write(ref, repo);
2308 done:
2309 free(refname);
2310 if (ref)
2311 got_ref_close(ref);
2312 return err;
2315 static const struct got_error *
2316 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2318 const struct got_error *err = NULL;
2320 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2321 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2322 err = got_error_from_errno("asprintf");
2323 *fileindex_path = NULL;
2325 return err;
2329 static const struct got_error *
2330 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2331 struct got_worktree *worktree)
2333 const struct got_error *err = NULL;
2334 FILE *index = NULL;
2336 *fileindex_path = NULL;
2337 *fileindex = got_fileindex_alloc();
2338 if (*fileindex == NULL)
2339 return got_error_from_errno("got_fileindex_alloc");
2341 err = get_fileindex_path(fileindex_path, worktree);
2342 if (err)
2343 goto done;
2345 index = fopen(*fileindex_path, "rbe");
2346 if (index == NULL) {
2347 if (errno != ENOENT)
2348 err = got_error_from_errno2("fopen", *fileindex_path);
2349 } else {
2350 err = got_fileindex_read(*fileindex, index);
2351 if (fclose(index) == EOF && err == NULL)
2352 err = got_error_from_errno("fclose");
2354 done:
2355 if (err) {
2356 free(*fileindex_path);
2357 *fileindex_path = NULL;
2358 got_fileindex_free(*fileindex);
2359 *fileindex = NULL;
2361 return err;
2364 struct bump_base_commit_id_arg {
2365 struct got_object_id *base_commit_id;
2366 const char *path;
2367 size_t path_len;
2368 const char *entry_name;
2369 got_worktree_checkout_cb progress_cb;
2370 void *progress_arg;
2373 /* Bump base commit ID of all files within an updated part of the work tree. */
2374 static const struct got_error *
2375 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2377 const struct got_error *err;
2378 struct bump_base_commit_id_arg *a = arg;
2380 if (a->entry_name) {
2381 if (strcmp(ie->path, a->path) != 0)
2382 return NULL;
2383 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2384 return NULL;
2386 if (got_fileindex_entry_was_skipped(ie))
2387 return NULL;
2389 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2390 SHA1_DIGEST_LENGTH) == 0)
2391 return NULL;
2393 if (a->progress_cb) {
2394 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2395 ie->path);
2396 if (err)
2397 return err;
2399 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2400 return NULL;
2403 static const struct got_error *
2404 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2405 struct got_fileindex *fileindex,
2406 got_worktree_checkout_cb progress_cb, void *progress_arg)
2408 struct bump_base_commit_id_arg bbc_arg;
2410 bbc_arg.base_commit_id = worktree->base_commit_id;
2411 bbc_arg.entry_name = NULL;
2412 bbc_arg.path = "";
2413 bbc_arg.path_len = 0;
2414 bbc_arg.progress_cb = progress_cb;
2415 bbc_arg.progress_arg = progress_arg;
2417 return got_fileindex_for_each_entry_safe(fileindex,
2418 bump_base_commit_id, &bbc_arg);
2421 static const struct got_error *
2422 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2424 const struct got_error *err = NULL;
2425 char *new_fileindex_path = NULL;
2426 FILE *new_index = NULL;
2427 struct timespec timeout;
2429 err = got_opentemp_named(&new_fileindex_path, &new_index,
2430 fileindex_path);
2431 if (err)
2432 goto done;
2434 err = got_fileindex_write(fileindex, new_index);
2435 if (err)
2436 goto done;
2438 if (rename(new_fileindex_path, fileindex_path) != 0) {
2439 err = got_error_from_errno3("rename", new_fileindex_path,
2440 fileindex_path);
2441 unlink(new_fileindex_path);
2445 * Sleep for a short amount of time to ensure that files modified after
2446 * this program exits have a different time stamp from the one which
2447 * was recorded in the file index.
2449 timeout.tv_sec = 0;
2450 timeout.tv_nsec = 1;
2451 nanosleep(&timeout, NULL);
2452 done:
2453 if (new_index)
2454 fclose(new_index);
2455 free(new_fileindex_path);
2456 return err;
2459 static const struct got_error *
2460 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2461 struct got_object_id **tree_id, const char *wt_relpath,
2462 struct got_worktree *worktree, struct got_repository *repo)
2464 const struct got_error *err = NULL;
2465 struct got_object_id *id = NULL;
2466 char *in_repo_path = NULL;
2467 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2469 *entry_type = GOT_OBJ_TYPE_ANY;
2470 *tree_relpath = NULL;
2471 *tree_id = NULL;
2473 if (wt_relpath[0] == '\0') {
2474 /* Check out all files within the work tree. */
2475 *entry_type = GOT_OBJ_TYPE_TREE;
2476 *tree_relpath = strdup("");
2477 if (*tree_relpath == NULL) {
2478 err = got_error_from_errno("strdup");
2479 goto done;
2481 err = got_object_id_by_path(tree_id, repo,
2482 worktree->base_commit_id, worktree->path_prefix);
2483 if (err)
2484 goto done;
2485 return NULL;
2488 /* Check out a subset of files in the work tree. */
2490 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2491 is_root_wt ? "" : "/", wt_relpath) == -1) {
2492 err = got_error_from_errno("asprintf");
2493 goto done;
2496 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2497 in_repo_path);
2498 if (err)
2499 goto done;
2501 free(in_repo_path);
2502 in_repo_path = NULL;
2504 err = got_object_get_type(entry_type, repo, id);
2505 if (err)
2506 goto done;
2508 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2509 /* Check out a single file. */
2510 if (strchr(wt_relpath, '/') == NULL) {
2511 /* Check out a single file in work tree's root dir. */
2512 in_repo_path = strdup(worktree->path_prefix);
2513 if (in_repo_path == NULL) {
2514 err = got_error_from_errno("strdup");
2515 goto done;
2517 *tree_relpath = strdup("");
2518 if (*tree_relpath == NULL) {
2519 err = got_error_from_errno("strdup");
2520 goto done;
2522 } else {
2523 /* Check out a single file in a subdirectory. */
2524 err = got_path_dirname(tree_relpath, wt_relpath);
2525 if (err)
2526 return err;
2527 if (asprintf(&in_repo_path, "%s%s%s",
2528 worktree->path_prefix, is_root_wt ? "" : "/",
2529 *tree_relpath) == -1) {
2530 err = got_error_from_errno("asprintf");
2531 goto done;
2534 err = got_object_id_by_path(tree_id, repo,
2535 worktree->base_commit_id, in_repo_path);
2536 } else {
2537 /* Check out all files within a subdirectory. */
2538 *tree_id = got_object_id_dup(id);
2539 if (*tree_id == NULL) {
2540 err = got_error_from_errno("got_object_id_dup");
2541 goto done;
2543 *tree_relpath = strdup(wt_relpath);
2544 if (*tree_relpath == NULL) {
2545 err = got_error_from_errno("strdup");
2546 goto done;
2549 done:
2550 free(id);
2551 free(in_repo_path);
2552 if (err) {
2553 *entry_type = GOT_OBJ_TYPE_ANY;
2554 free(*tree_relpath);
2555 *tree_relpath = NULL;
2556 free(*tree_id);
2557 *tree_id = NULL;
2559 return err;
2562 static const struct got_error *
2563 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2564 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2565 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2566 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2568 const struct got_error *err = NULL;
2569 struct got_commit_object *commit = NULL;
2570 struct got_tree_object *tree = NULL;
2571 struct got_fileindex_diff_tree_cb diff_cb;
2572 struct diff_cb_arg arg;
2574 err = ref_base_commit(worktree, repo);
2575 if (err) {
2576 if (!(err->code == GOT_ERR_ERRNO &&
2577 (errno == EACCES || errno == EROFS)))
2578 goto done;
2579 err = (*progress_cb)(progress_arg,
2580 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2581 if (err)
2582 return err;
2585 err = got_object_open_as_commit(&commit, repo,
2586 worktree->base_commit_id);
2587 if (err)
2588 goto done;
2590 err = got_object_open_as_tree(&tree, repo, tree_id);
2591 if (err)
2592 goto done;
2594 if (entry_name &&
2595 got_object_tree_find_entry(tree, entry_name) == NULL) {
2596 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2597 goto done;
2600 diff_cb.diff_old_new = diff_old_new;
2601 diff_cb.diff_old = diff_old;
2602 diff_cb.diff_new = diff_new;
2603 arg.fileindex = fileindex;
2604 arg.worktree = worktree;
2605 arg.repo = repo;
2606 arg.progress_cb = progress_cb;
2607 arg.progress_arg = progress_arg;
2608 arg.cancel_cb = cancel_cb;
2609 arg.cancel_arg = cancel_arg;
2610 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2611 entry_name, repo, &diff_cb, &arg);
2612 done:
2613 if (tree)
2614 got_object_tree_close(tree);
2615 if (commit)
2616 got_object_commit_close(commit);
2617 return err;
2620 const struct got_error *
2621 got_worktree_checkout_files(struct got_worktree *worktree,
2622 struct got_pathlist_head *paths, struct got_repository *repo,
2623 got_worktree_checkout_cb progress_cb, void *progress_arg,
2624 got_cancel_cb cancel_cb, void *cancel_arg)
2626 const struct got_error *err = NULL, *sync_err, *unlockerr;
2627 struct got_commit_object *commit = NULL;
2628 struct got_tree_object *tree = NULL;
2629 struct got_fileindex *fileindex = NULL;
2630 char *fileindex_path = NULL;
2631 struct got_pathlist_entry *pe;
2632 struct tree_path_data {
2633 STAILQ_ENTRY(tree_path_data) entry;
2634 struct got_object_id *tree_id;
2635 int entry_type;
2636 char *relpath;
2637 char *entry_name;
2638 } *tpd = NULL;
2639 STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2641 STAILQ_INIT(&tree_paths);
2643 err = lock_worktree(worktree, LOCK_EX);
2644 if (err)
2645 return err;
2647 /* Map all specified paths to in-repository trees. */
2648 TAILQ_FOREACH(pe, paths, entry) {
2649 tpd = malloc(sizeof(*tpd));
2650 if (tpd == NULL) {
2651 err = got_error_from_errno("malloc");
2652 goto done;
2655 err = find_tree_entry_for_checkout(&tpd->entry_type,
2656 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2657 if (err) {
2658 free(tpd);
2659 goto done;
2662 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2663 err = got_path_basename(&tpd->entry_name, pe->path);
2664 if (err) {
2665 free(tpd->relpath);
2666 free(tpd->tree_id);
2667 free(tpd);
2668 goto done;
2670 } else
2671 tpd->entry_name = NULL;
2673 STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2677 * Read the file index.
2678 * Checking out files is supposed to be an idempotent operation.
2679 * If the on-disk file index is incomplete we will try to complete it.
2681 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2682 if (err)
2683 goto done;
2685 tpd = STAILQ_FIRST(&tree_paths);
2686 TAILQ_FOREACH(pe, paths, entry) {
2687 struct bump_base_commit_id_arg bbc_arg;
2689 err = checkout_files(worktree, fileindex, tpd->relpath,
2690 tpd->tree_id, tpd->entry_name, repo,
2691 progress_cb, progress_arg, cancel_cb, cancel_arg);
2692 if (err)
2693 break;
2695 bbc_arg.base_commit_id = worktree->base_commit_id;
2696 bbc_arg.entry_name = tpd->entry_name;
2697 bbc_arg.path = pe->path;
2698 bbc_arg.path_len = pe->path_len;
2699 bbc_arg.progress_cb = progress_cb;
2700 bbc_arg.progress_arg = progress_arg;
2701 err = got_fileindex_for_each_entry_safe(fileindex,
2702 bump_base_commit_id, &bbc_arg);
2703 if (err)
2704 break;
2706 tpd = STAILQ_NEXT(tpd, entry);
2708 sync_err = sync_fileindex(fileindex, fileindex_path);
2709 if (sync_err && err == NULL)
2710 err = sync_err;
2711 done:
2712 free(fileindex_path);
2713 if (tree)
2714 got_object_tree_close(tree);
2715 if (commit)
2716 got_object_commit_close(commit);
2717 if (fileindex)
2718 got_fileindex_free(fileindex);
2719 while (!STAILQ_EMPTY(&tree_paths)) {
2720 tpd = STAILQ_FIRST(&tree_paths);
2721 STAILQ_REMOVE_HEAD(&tree_paths, entry);
2722 free(tpd->relpath);
2723 free(tpd->tree_id);
2724 free(tpd);
2726 unlockerr = lock_worktree(worktree, LOCK_SH);
2727 if (unlockerr && err == NULL)
2728 err = unlockerr;
2729 return err;
2732 struct merge_file_cb_arg {
2733 struct got_worktree *worktree;
2734 struct got_fileindex *fileindex;
2735 got_worktree_checkout_cb progress_cb;
2736 void *progress_arg;
2737 got_cancel_cb cancel_cb;
2738 void *cancel_arg;
2739 const char *label_orig;
2740 struct got_object_id *commit_id2;
2741 int allow_bad_symlinks;
2744 static const struct got_error *
2745 merge_file_cb(void *arg, struct got_blob_object *blob1,
2746 struct got_blob_object *blob2, struct got_object_id *id1,
2747 struct got_object_id *id2, const char *path1, const char *path2,
2748 mode_t mode1, mode_t mode2, struct got_repository *repo)
2750 static const struct got_error *err = NULL;
2751 struct merge_file_cb_arg *a = arg;
2752 struct got_fileindex_entry *ie;
2753 char *ondisk_path = NULL;
2754 struct stat sb;
2755 unsigned char status;
2756 int local_changes_subsumed;
2757 FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2758 char *id_str = NULL, *label_deriv2 = NULL;
2760 if (blob1 && blob2) {
2761 ie = got_fileindex_entry_get(a->fileindex, path2,
2762 strlen(path2));
2763 if (ie == NULL)
2764 return (*a->progress_cb)(a->progress_arg,
2765 GOT_STATUS_MISSING, path2);
2767 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2768 path2) == -1)
2769 return got_error_from_errno("asprintf");
2771 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2772 repo);
2773 if (err)
2774 goto done;
2776 if (status == GOT_STATUS_DELETE) {
2777 err = (*a->progress_cb)(a->progress_arg,
2778 GOT_STATUS_MERGE, path2);
2779 goto done;
2781 if (status != GOT_STATUS_NO_CHANGE &&
2782 status != GOT_STATUS_MODIFY &&
2783 status != GOT_STATUS_CONFLICT &&
2784 status != GOT_STATUS_ADD) {
2785 err = (*a->progress_cb)(a->progress_arg, status, path2);
2786 goto done;
2789 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2790 char *link_target2;
2791 err = got_object_blob_read_to_str(&link_target2, blob2);
2792 if (err)
2793 goto done;
2794 err = merge_symlink(a->worktree, blob1, ondisk_path,
2795 path2, a->label_orig, link_target2, a->commit_id2,
2796 repo, a->progress_cb, a->progress_arg);
2797 free(link_target2);
2798 } else {
2799 int fd;
2801 f_orig = got_opentemp();
2802 if (f_orig == NULL) {
2803 err = got_error_from_errno("got_opentemp");
2804 goto done;
2806 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2807 f_orig, blob1);
2808 if (err)
2809 goto done;
2811 f_deriv2 = got_opentemp();
2812 if (f_deriv2 == NULL)
2813 goto done;
2814 err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2815 f_deriv2, blob2);
2816 if (err)
2817 goto done;
2819 fd = open(ondisk_path,
2820 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2821 if (fd == -1) {
2822 err = got_error_from_errno2("open",
2823 ondisk_path);
2824 goto done;
2826 f_deriv = fdopen(fd, "r");
2827 if (f_deriv == NULL) {
2828 err = got_error_from_errno2("fdopen",
2829 ondisk_path);
2830 close(fd);
2831 goto done;
2833 err = got_object_id_str(&id_str, a->commit_id2);
2834 if (err)
2835 goto done;
2836 if (asprintf(&label_deriv2, "%s: commit %s",
2837 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2838 err = got_error_from_errno("asprintf");
2839 goto done;
2841 err = merge_file(&local_changes_subsumed, a->worktree,
2842 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2843 sb.st_mode, a->label_orig, NULL, label_deriv2,
2844 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2845 a->progress_cb, a->progress_arg);
2847 } else if (blob1) {
2848 ie = got_fileindex_entry_get(a->fileindex, path1,
2849 strlen(path1));
2850 if (ie == NULL)
2851 return (*a->progress_cb)(a->progress_arg,
2852 GOT_STATUS_MISSING, path1);
2854 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2855 path1) == -1)
2856 return got_error_from_errno("asprintf");
2858 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2859 repo);
2860 if (err)
2861 goto done;
2863 switch (status) {
2864 case GOT_STATUS_NO_CHANGE:
2865 err = (*a->progress_cb)(a->progress_arg,
2866 GOT_STATUS_DELETE, path1);
2867 if (err)
2868 goto done;
2869 err = remove_ondisk_file(a->worktree->root_path, path1);
2870 if (err)
2871 goto done;
2872 if (ie)
2873 got_fileindex_entry_mark_deleted_from_disk(ie);
2874 break;
2875 case GOT_STATUS_DELETE:
2876 case GOT_STATUS_MISSING:
2877 err = (*a->progress_cb)(a->progress_arg,
2878 GOT_STATUS_DELETE, path1);
2879 if (err)
2880 goto done;
2881 if (ie)
2882 got_fileindex_entry_mark_deleted_from_disk(ie);
2883 break;
2884 case GOT_STATUS_ADD: {
2885 struct got_object_id *id;
2886 FILE *blob1_f;
2888 * Delete the added file only if its content already
2889 * exists in the repository.
2891 err = got_object_blob_file_create(&id, &blob1_f, path1);
2892 if (err)
2893 goto done;
2894 if (got_object_id_cmp(id, id1) == 0) {
2895 err = (*a->progress_cb)(a->progress_arg,
2896 GOT_STATUS_DELETE, path1);
2897 if (err)
2898 goto done;
2899 err = remove_ondisk_file(a->worktree->root_path,
2900 path1);
2901 if (err)
2902 goto done;
2903 if (ie)
2904 got_fileindex_entry_remove(a->fileindex,
2905 ie);
2906 } else {
2907 err = (*a->progress_cb)(a->progress_arg,
2908 GOT_STATUS_CANNOT_DELETE, path1);
2910 if (fclose(blob1_f) == EOF && err == NULL)
2911 err = got_error_from_errno("fclose");
2912 free(id);
2913 if (err)
2914 goto done;
2915 break;
2917 case GOT_STATUS_MODIFY:
2918 case GOT_STATUS_CONFLICT:
2919 err = (*a->progress_cb)(a->progress_arg,
2920 GOT_STATUS_CANNOT_DELETE, path1);
2921 if (err)
2922 goto done;
2923 break;
2924 case GOT_STATUS_OBSTRUCTED:
2925 err = (*a->progress_cb)(a->progress_arg, status, path1);
2926 if (err)
2927 goto done;
2928 break;
2929 default:
2930 break;
2932 } else if (blob2) {
2933 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2934 path2) == -1)
2935 return got_error_from_errno("asprintf");
2936 ie = got_fileindex_entry_get(a->fileindex, path2,
2937 strlen(path2));
2938 if (ie) {
2939 err = get_file_status(&status, &sb, ie, ondisk_path,
2940 -1, NULL, repo);
2941 if (err)
2942 goto done;
2943 if (status != GOT_STATUS_NO_CHANGE &&
2944 status != GOT_STATUS_MODIFY &&
2945 status != GOT_STATUS_CONFLICT &&
2946 status != GOT_STATUS_ADD) {
2947 err = (*a->progress_cb)(a->progress_arg,
2948 status, path2);
2949 goto done;
2951 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2952 char *link_target2;
2953 err = got_object_blob_read_to_str(&link_target2,
2954 blob2);
2955 if (err)
2956 goto done;
2957 err = merge_symlink(a->worktree, NULL,
2958 ondisk_path, path2, a->label_orig,
2959 link_target2, a->commit_id2, repo,
2960 a->progress_cb, a->progress_arg);
2961 free(link_target2);
2962 } else if (S_ISREG(sb.st_mode)) {
2963 err = merge_blob(&local_changes_subsumed,
2964 a->worktree, NULL, ondisk_path, path2,
2965 sb.st_mode, a->label_orig, blob2,
2966 a->commit_id2, repo, a->progress_cb,
2967 a->progress_arg);
2968 } else {
2969 err = got_error_path(ondisk_path,
2970 GOT_ERR_FILE_OBSTRUCTED);
2972 if (err)
2973 goto done;
2974 if (status == GOT_STATUS_DELETE) {
2975 err = got_fileindex_entry_update(ie,
2976 a->worktree->root_fd, path2, blob2->id.sha1,
2977 a->worktree->base_commit_id->sha1, 0);
2978 if (err)
2979 goto done;
2981 } else {
2982 int is_bad_symlink = 0;
2983 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2984 if (S_ISLNK(mode2)) {
2985 err = install_symlink(&is_bad_symlink,
2986 a->worktree, ondisk_path, path2, blob2, 0,
2987 0, 1, a->allow_bad_symlinks, repo,
2988 a->progress_cb, a->progress_arg);
2989 } else {
2990 err = install_blob(a->worktree, ondisk_path, path2,
2991 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2992 a->progress_cb, a->progress_arg);
2994 if (err)
2995 goto done;
2996 err = got_fileindex_entry_alloc(&ie, path2);
2997 if (err)
2998 goto done;
2999 err = got_fileindex_entry_update(ie,
3000 a->worktree->root_fd, path2, NULL, NULL, 1);
3001 if (err) {
3002 got_fileindex_entry_free(ie);
3003 goto done;
3005 err = got_fileindex_entry_add(a->fileindex, ie);
3006 if (err) {
3007 got_fileindex_entry_free(ie);
3008 goto done;
3010 if (is_bad_symlink) {
3011 got_fileindex_entry_filetype_set(ie,
3012 GOT_FILEIDX_MODE_BAD_SYMLINK);
3016 done:
3017 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3018 err = got_error_from_errno("fclose");
3019 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3020 err = got_error_from_errno("fclose");
3021 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3022 err = got_error_from_errno("fclose");
3023 free(id_str);
3024 free(label_deriv2);
3025 free(ondisk_path);
3026 return err;
3029 static const struct got_error *
3030 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3032 struct got_worktree *worktree = arg;
3034 /* Reject merges into a work tree with mixed base commits. */
3035 if (got_fileindex_entry_has_commit(ie) &&
3036 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3037 SHA1_DIGEST_LENGTH) != 0)
3038 return got_error(GOT_ERR_MIXED_COMMITS);
3040 return NULL;
3043 struct check_merge_conflicts_arg {
3044 struct got_worktree *worktree;
3045 struct got_fileindex *fileindex;
3046 struct got_repository *repo;
3049 static const struct got_error *
3050 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3051 struct got_blob_object *blob2, struct got_object_id *id1,
3052 struct got_object_id *id2, const char *path1, const char *path2,
3053 mode_t mode1, mode_t mode2, struct got_repository *repo)
3055 const struct got_error *err = NULL;
3056 struct check_merge_conflicts_arg *a = arg;
3057 unsigned char status;
3058 struct stat sb;
3059 struct got_fileindex_entry *ie;
3060 const char *path = path2 ? path2 : path1;
3061 struct got_object_id *id = id2 ? id2 : id1;
3062 char *ondisk_path;
3064 if (id == NULL)
3065 return NULL;
3067 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3068 if (ie == NULL)
3069 return NULL;
3071 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3072 == -1)
3073 return got_error_from_errno("asprintf");
3075 /* Reject merges into a work tree with conflicted files. */
3076 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3077 free(ondisk_path);
3078 if (err)
3079 return err;
3080 if (status == GOT_STATUS_CONFLICT)
3081 return got_error(GOT_ERR_CONFLICTS);
3083 return NULL;
3086 static const struct got_error *
3087 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3088 const char *fileindex_path, struct got_object_id *commit_id1,
3089 struct got_object_id *commit_id2, struct got_repository *repo,
3090 got_worktree_checkout_cb progress_cb, void *progress_arg,
3091 got_cancel_cb cancel_cb, void *cancel_arg)
3093 const struct got_error *err = NULL, *sync_err;
3094 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3095 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3096 struct check_merge_conflicts_arg cmc_arg;
3097 struct merge_file_cb_arg arg;
3098 char *label_orig = NULL;
3100 if (commit_id1) {
3101 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3102 worktree->path_prefix);
3103 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3104 goto done;
3106 if (tree_id1) {
3107 char *id_str;
3109 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3110 if (err)
3111 goto done;
3113 err = got_object_id_str(&id_str, commit_id1);
3114 if (err)
3115 goto done;
3117 if (asprintf(&label_orig, "%s: commit %s",
3118 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3119 err = got_error_from_errno("asprintf");
3120 free(id_str);
3121 goto done;
3123 free(id_str);
3126 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3127 worktree->path_prefix);
3128 if (err)
3129 goto done;
3131 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3132 if (err)
3133 goto done;
3135 cmc_arg.worktree = worktree;
3136 cmc_arg.fileindex = fileindex;
3137 cmc_arg.repo = repo;
3138 err = got_diff_tree(tree1, tree2, "", "", repo,
3139 check_merge_conflicts, &cmc_arg, 0);
3140 if (err)
3141 goto done;
3143 arg.worktree = worktree;
3144 arg.fileindex = fileindex;
3145 arg.progress_cb = progress_cb;
3146 arg.progress_arg = progress_arg;
3147 arg.cancel_cb = cancel_cb;
3148 arg.cancel_arg = cancel_arg;
3149 arg.label_orig = label_orig;
3150 arg.commit_id2 = commit_id2;
3151 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3152 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3153 sync_err = sync_fileindex(fileindex, fileindex_path);
3154 if (sync_err && err == NULL)
3155 err = sync_err;
3156 done:
3157 if (tree1)
3158 got_object_tree_close(tree1);
3159 if (tree2)
3160 got_object_tree_close(tree2);
3161 free(label_orig);
3162 return err;
3165 const struct got_error *
3166 got_worktree_merge_files(struct got_worktree *worktree,
3167 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3168 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3169 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3171 const struct got_error *err, *unlockerr;
3172 char *fileindex_path = NULL;
3173 struct got_fileindex *fileindex = NULL;
3175 err = lock_worktree(worktree, LOCK_EX);
3176 if (err)
3177 return err;
3179 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3180 if (err)
3181 goto done;
3183 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3184 worktree);
3185 if (err)
3186 goto done;
3188 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3189 commit_id2, repo, progress_cb, progress_arg,
3190 cancel_cb, cancel_arg);
3191 done:
3192 if (fileindex)
3193 got_fileindex_free(fileindex);
3194 free(fileindex_path);
3195 unlockerr = lock_worktree(worktree, LOCK_SH);
3196 if (unlockerr && err == NULL)
3197 err = unlockerr;
3198 return err;
3201 struct diff_dir_cb_arg {
3202 struct got_fileindex *fileindex;
3203 struct got_worktree *worktree;
3204 const char *status_path;
3205 size_t status_path_len;
3206 struct got_repository *repo;
3207 got_worktree_status_cb status_cb;
3208 void *status_arg;
3209 got_cancel_cb cancel_cb;
3210 void *cancel_arg;
3211 /* A pathlist containing per-directory pathlists of ignore patterns. */
3212 struct got_pathlist_head *ignores;
3213 int report_unchanged;
3214 int no_ignores;
3217 static const struct got_error *
3218 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3219 int dirfd, const char *de_name,
3220 got_worktree_status_cb status_cb, void *status_arg,
3221 struct got_repository *repo, int report_unchanged)
3223 const struct got_error *err = NULL;
3224 unsigned char status = GOT_STATUS_NO_CHANGE;
3225 unsigned char staged_status = get_staged_status(ie);
3226 struct stat sb;
3227 struct got_object_id blob_id, commit_id, staged_blob_id;
3228 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3229 struct got_object_id *staged_blob_idp = NULL;
3231 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3232 if (err)
3233 return err;
3235 if (status == GOT_STATUS_NO_CHANGE &&
3236 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3237 return NULL;
3239 if (got_fileindex_entry_has_blob(ie)) {
3240 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3241 blob_idp = &blob_id;
3243 if (got_fileindex_entry_has_commit(ie)) {
3244 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3245 commit_idp = &commit_id;
3247 if (staged_status == GOT_STATUS_ADD ||
3248 staged_status == GOT_STATUS_MODIFY) {
3249 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3250 SHA1_DIGEST_LENGTH);
3251 staged_blob_idp = &staged_blob_id;
3254 return (*status_cb)(status_arg, status, staged_status,
3255 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3258 static const struct got_error *
3259 status_old_new(void *arg, struct got_fileindex_entry *ie,
3260 struct dirent *de, const char *parent_path, int dirfd)
3262 const struct got_error *err = NULL;
3263 struct diff_dir_cb_arg *a = arg;
3264 char *abspath;
3266 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3267 return got_error(GOT_ERR_CANCELLED);
3269 if (got_path_cmp(parent_path, a->status_path,
3270 strlen(parent_path), a->status_path_len) != 0 &&
3271 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3272 return NULL;
3274 if (parent_path[0]) {
3275 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3276 parent_path, de->d_name) == -1)
3277 return got_error_from_errno("asprintf");
3278 } else {
3279 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3280 de->d_name) == -1)
3281 return got_error_from_errno("asprintf");
3284 err = report_file_status(ie, abspath, dirfd, de->d_name,
3285 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3286 free(abspath);
3287 return err;
3290 static const struct got_error *
3291 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3293 struct diff_dir_cb_arg *a = arg;
3294 struct got_object_id blob_id, commit_id;
3295 unsigned char status;
3297 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3298 return got_error(GOT_ERR_CANCELLED);
3300 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3301 return NULL;
3303 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3304 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3305 if (got_fileindex_entry_has_file_on_disk(ie))
3306 status = GOT_STATUS_MISSING;
3307 else
3308 status = GOT_STATUS_DELETE;
3309 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3310 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3313 void
3314 free_ignorelist(struct got_pathlist_head *ignorelist)
3316 struct got_pathlist_entry *pe;
3318 TAILQ_FOREACH(pe, ignorelist, entry)
3319 free((char *)pe->path);
3320 got_pathlist_free(ignorelist);
3323 void
3324 free_ignores(struct got_pathlist_head *ignores)
3326 struct got_pathlist_entry *pe;
3328 TAILQ_FOREACH(pe, ignores, entry) {
3329 struct got_pathlist_head *ignorelist = pe->data;
3330 free_ignorelist(ignorelist);
3331 free((char *)pe->path);
3333 got_pathlist_free(ignores);
3336 static const struct got_error *
3337 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3339 const struct got_error *err = NULL;
3340 struct got_pathlist_entry *pe = NULL;
3341 struct got_pathlist_head *ignorelist;
3342 char *line = NULL, *pattern, *dirpath = NULL;
3343 size_t linesize = 0;
3344 ssize_t linelen;
3346 ignorelist = calloc(1, sizeof(*ignorelist));
3347 if (ignorelist == NULL)
3348 return got_error_from_errno("calloc");
3349 TAILQ_INIT(ignorelist);
3351 while ((linelen = getline(&line, &linesize, f)) != -1) {
3352 if (linelen > 0 && line[linelen - 1] == '\n')
3353 line[linelen - 1] = '\0';
3355 /* Git's ignores may contain comments. */
3356 if (line[0] == '#')
3357 continue;
3359 /* Git's negated patterns are not (yet?) supported. */
3360 if (line[0] == '!')
3361 continue;
3363 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3364 line) == -1) {
3365 err = got_error_from_errno("asprintf");
3366 goto done;
3368 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3369 if (err)
3370 goto done;
3372 if (ferror(f)) {
3373 err = got_error_from_errno("getline");
3374 goto done;
3377 dirpath = strdup(path);
3378 if (dirpath == NULL) {
3379 err = got_error_from_errno("strdup");
3380 goto done;
3382 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3383 done:
3384 free(line);
3385 if (err || pe == NULL) {
3386 free(dirpath);
3387 free_ignorelist(ignorelist);
3389 return err;
3392 int
3393 match_ignores(struct got_pathlist_head *ignores, const char *path)
3395 struct got_pathlist_entry *pe;
3397 /* Handle patterns which match in all directories. */
3398 TAILQ_FOREACH(pe, ignores, entry) {
3399 struct got_pathlist_head *ignorelist = pe->data;
3400 struct got_pathlist_entry *pi;
3402 TAILQ_FOREACH(pi, ignorelist, entry) {
3403 const char *p, *pattern = pi->path;
3405 if (strncmp(pattern, "**/", 3) != 0)
3406 continue;
3407 pattern += 3;
3408 p = path;
3409 while (*p) {
3410 if (fnmatch(pattern, p,
3411 FNM_PATHNAME | FNM_LEADING_DIR)) {
3412 /* Retry in next directory. */
3413 while (*p && *p != '/')
3414 p++;
3415 while (*p == '/')
3416 p++;
3417 continue;
3419 return 1;
3425 * The ignores pathlist contains ignore lists from children before
3426 * parents, so we can find the most specific ignorelist by walking
3427 * ignores backwards.
3429 pe = TAILQ_LAST(ignores, got_pathlist_head);
3430 while (pe) {
3431 if (got_path_is_child(path, pe->path, pe->path_len)) {
3432 struct got_pathlist_head *ignorelist = pe->data;
3433 struct got_pathlist_entry *pi;
3434 TAILQ_FOREACH(pi, ignorelist, entry) {
3435 const char *pattern = pi->path;
3436 int flags = FNM_LEADING_DIR;
3437 if (strstr(pattern, "/**/") == NULL)
3438 flags |= FNM_PATHNAME;
3439 if (fnmatch(pattern, path, flags))
3440 continue;
3441 return 1;
3444 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3447 return 0;
3450 static const struct got_error *
3451 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3452 const char *path, int dirfd, const char *ignores_filename)
3454 const struct got_error *err = NULL;
3455 char *ignorespath;
3456 int fd = -1;
3457 FILE *ignoresfile = NULL;
3459 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3460 path[0] ? "/" : "", ignores_filename) == -1)
3461 return got_error_from_errno("asprintf");
3463 if (dirfd != -1) {
3464 fd = openat(dirfd, ignores_filename,
3465 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3466 if (fd == -1) {
3467 if (errno != ENOENT && errno != EACCES)
3468 err = got_error_from_errno2("openat",
3469 ignorespath);
3470 } else {
3471 ignoresfile = fdopen(fd, "r");
3472 if (ignoresfile == NULL)
3473 err = got_error_from_errno2("fdopen",
3474 ignorespath);
3475 else {
3476 fd = -1;
3477 err = read_ignores(ignores, path, ignoresfile);
3480 } else {
3481 ignoresfile = fopen(ignorespath, "re");
3482 if (ignoresfile == NULL) {
3483 if (errno != ENOENT && errno != EACCES)
3484 err = got_error_from_errno2("fopen",
3485 ignorespath);
3486 } else
3487 err = read_ignores(ignores, path, ignoresfile);
3490 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3491 err = got_error_from_errno2("fclose", path);
3492 if (fd != -1 && close(fd) == -1 && err == NULL)
3493 err = got_error_from_errno2("close", path);
3494 free(ignorespath);
3495 return err;
3498 static const struct got_error *
3499 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3500 int dirfd)
3502 const struct got_error *err = NULL;
3503 struct diff_dir_cb_arg *a = arg;
3504 char *path = NULL;
3506 if (ignore != NULL)
3507 *ignore = 0;
3509 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3510 return got_error(GOT_ERR_CANCELLED);
3512 if (parent_path[0]) {
3513 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3514 return got_error_from_errno("asprintf");
3515 } else {
3516 path = de->d_name;
3519 if (de->d_type == DT_DIR) {
3520 if (!a->no_ignores && ignore != NULL &&
3521 match_ignores(a->ignores, path))
3522 *ignore = 1;
3523 } else if (!match_ignores(a->ignores, path) &&
3524 got_path_is_child(path, a->status_path, a->status_path_len))
3525 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3526 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3527 if (parent_path[0])
3528 free(path);
3529 return err;
3532 static const struct got_error *
3533 status_traverse(void *arg, const char *path, int dirfd)
3535 const struct got_error *err = NULL;
3536 struct diff_dir_cb_arg *a = arg;
3538 if (a->no_ignores)
3539 return NULL;
3541 err = add_ignores(a->ignores, a->worktree->root_path,
3542 path, dirfd, ".cvsignore");
3543 if (err)
3544 return err;
3546 err = add_ignores(a->ignores, a->worktree->root_path, path,
3547 dirfd, ".gitignore");
3549 return err;
3552 static const struct got_error *
3553 report_single_file_status(const char *path, const char *ondisk_path,
3554 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3555 void *status_arg, struct got_repository *repo, int report_unchanged,
3556 struct got_pathlist_head *ignores, int no_ignores)
3558 struct got_fileindex_entry *ie;
3559 struct stat sb;
3561 if (!no_ignores && match_ignores(ignores, path))
3562 return NULL;
3564 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3565 if (ie)
3566 return report_file_status(ie, ondisk_path, -1, NULL,
3567 status_cb, status_arg, repo, report_unchanged);
3569 if (lstat(ondisk_path, &sb) == -1) {
3570 if (errno != ENOENT)
3571 return got_error_from_errno2("lstat", ondisk_path);
3572 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3573 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3574 return NULL;
3577 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3578 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3579 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3581 return NULL;
3584 static const struct got_error *
3585 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3586 const char *root_path, const char *path)
3588 const struct got_error *err;
3589 char *parent_path, *next_parent_path = NULL;
3591 err = add_ignores(ignores, root_path, "", -1,
3592 ".cvsignore");
3593 if (err)
3594 return err;
3596 err = add_ignores(ignores, root_path, "", -1,
3597 ".gitignore");
3598 if (err)
3599 return err;
3601 err = got_path_dirname(&parent_path, path);
3602 if (err) {
3603 if (err->code == GOT_ERR_BAD_PATH)
3604 return NULL; /* cannot traverse parent */
3605 return err;
3607 for (;;) {
3608 err = add_ignores(ignores, root_path, parent_path, -1,
3609 ".cvsignore");
3610 if (err)
3611 break;
3612 err = add_ignores(ignores, root_path, parent_path, -1,
3613 ".gitignore");
3614 if (err)
3615 break;
3616 err = got_path_dirname(&next_parent_path, parent_path);
3617 if (err) {
3618 if (err->code == GOT_ERR_BAD_PATH)
3619 err = NULL; /* traversed everything */
3620 break;
3622 if (got_path_is_root_dir(parent_path))
3623 break;
3624 free(parent_path);
3625 parent_path = next_parent_path;
3626 next_parent_path = NULL;
3629 free(parent_path);
3630 free(next_parent_path);
3631 return err;
3634 static const struct got_error *
3635 worktree_status(struct got_worktree *worktree, const char *path,
3636 struct got_fileindex *fileindex, struct got_repository *repo,
3637 got_worktree_status_cb status_cb, void *status_arg,
3638 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3639 int report_unchanged)
3641 const struct got_error *err = NULL;
3642 int fd = -1;
3643 struct got_fileindex_diff_dir_cb fdiff_cb;
3644 struct diff_dir_cb_arg arg;
3645 char *ondisk_path = NULL;
3646 struct got_pathlist_head ignores;
3648 TAILQ_INIT(&ignores);
3650 if (asprintf(&ondisk_path, "%s%s%s",
3651 worktree->root_path, path[0] ? "/" : "", path) == -1)
3652 return got_error_from_errno("asprintf");
3654 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3655 if (fd == -1) {
3656 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3657 !got_err_open_nofollow_on_symlink())
3658 err = got_error_from_errno2("open", ondisk_path);
3659 else {
3660 if (!no_ignores) {
3661 err = add_ignores_from_parent_paths(&ignores,
3662 worktree->root_path, ondisk_path);
3663 if (err)
3664 goto done;
3666 err = report_single_file_status(path, ondisk_path,
3667 fileindex, status_cb, status_arg, repo,
3668 report_unchanged, &ignores, no_ignores);
3670 } else {
3671 fdiff_cb.diff_old_new = status_old_new;
3672 fdiff_cb.diff_old = status_old;
3673 fdiff_cb.diff_new = status_new;
3674 fdiff_cb.diff_traverse = status_traverse;
3675 arg.fileindex = fileindex;
3676 arg.worktree = worktree;
3677 arg.status_path = path;
3678 arg.status_path_len = strlen(path);
3679 arg.repo = repo;
3680 arg.status_cb = status_cb;
3681 arg.status_arg = status_arg;
3682 arg.cancel_cb = cancel_cb;
3683 arg.cancel_arg = cancel_arg;
3684 arg.report_unchanged = report_unchanged;
3685 arg.no_ignores = no_ignores;
3686 if (!no_ignores) {
3687 err = add_ignores_from_parent_paths(&ignores,
3688 worktree->root_path, path);
3689 if (err)
3690 goto done;
3692 arg.ignores = &ignores;
3693 err = got_fileindex_diff_dir(fileindex, fd,
3694 worktree->root_path, path, repo, &fdiff_cb, &arg);
3696 done:
3697 free_ignores(&ignores);
3698 if (fd != -1 && close(fd) == -1 && err == NULL)
3699 err = got_error_from_errno("close");
3700 free(ondisk_path);
3701 return err;
3704 const struct got_error *
3705 got_worktree_status(struct got_worktree *worktree,
3706 struct got_pathlist_head *paths, struct got_repository *repo,
3707 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3708 got_cancel_cb cancel_cb, void *cancel_arg)
3710 const struct got_error *err = NULL;
3711 char *fileindex_path = NULL;
3712 struct got_fileindex *fileindex = NULL;
3713 struct got_pathlist_entry *pe;
3715 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3716 if (err)
3717 return err;
3719 TAILQ_FOREACH(pe, paths, entry) {
3720 err = worktree_status(worktree, pe->path, fileindex, repo,
3721 status_cb, status_arg, cancel_cb, cancel_arg,
3722 no_ignores, 0);
3723 if (err)
3724 break;
3726 free(fileindex_path);
3727 got_fileindex_free(fileindex);
3728 return err;
3731 const struct got_error *
3732 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3733 const char *arg)
3735 const struct got_error *err = NULL;
3736 char *resolved = NULL, *cwd = NULL, *path = NULL;
3737 size_t len;
3738 struct stat sb;
3739 char *abspath = NULL;
3740 char canonpath[PATH_MAX];
3742 *wt_path = NULL;
3744 cwd = getcwd(NULL, 0);
3745 if (cwd == NULL)
3746 return got_error_from_errno("getcwd");
3748 if (lstat(arg, &sb) == -1) {
3749 if (errno != ENOENT) {
3750 err = got_error_from_errno2("lstat", arg);
3751 goto done;
3753 sb.st_mode = 0;
3755 if (S_ISLNK(sb.st_mode)) {
3757 * We cannot use realpath(3) with symlinks since we want to
3758 * operate on the symlink itself.
3759 * But we can make the path absolute, assuming it is relative
3760 * to the current working directory, and then canonicalize it.
3762 if (!got_path_is_absolute(arg)) {
3763 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3764 err = got_error_from_errno("asprintf");
3765 goto done;
3769 err = got_canonpath(abspath ? abspath : arg, canonpath,
3770 sizeof(canonpath));
3771 if (err)
3772 goto done;
3773 resolved = strdup(canonpath);
3774 if (resolved == NULL) {
3775 err = got_error_from_errno("strdup");
3776 goto done;
3778 } else {
3779 resolved = realpath(arg, NULL);
3780 if (resolved == NULL) {
3781 if (errno != ENOENT) {
3782 err = got_error_from_errno2("realpath", arg);
3783 goto done;
3785 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3786 err = got_error_from_errno("asprintf");
3787 goto done;
3789 err = got_canonpath(abspath, canonpath,
3790 sizeof(canonpath));
3791 if (err)
3792 goto done;
3793 resolved = strdup(canonpath);
3794 if (resolved == NULL) {
3795 err = got_error_from_errno("strdup");
3796 goto done;
3801 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3802 strlen(got_worktree_get_root_path(worktree)))) {
3803 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3804 goto done;
3807 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3808 err = got_path_skip_common_ancestor(&path,
3809 got_worktree_get_root_path(worktree), resolved);
3810 if (err)
3811 goto done;
3812 } else {
3813 path = strdup("");
3814 if (path == NULL) {
3815 err = got_error_from_errno("strdup");
3816 goto done;
3820 /* XXX status walk can't deal with trailing slash! */
3821 len = strlen(path);
3822 while (len > 0 && path[len - 1] == '/') {
3823 path[len - 1] = '\0';
3824 len--;
3826 done:
3827 free(abspath);
3828 free(resolved);
3829 free(cwd);
3830 if (err == NULL)
3831 *wt_path = path;
3832 else
3833 free(path);
3834 return err;
3837 struct schedule_addition_args {
3838 struct got_worktree *worktree;
3839 struct got_fileindex *fileindex;
3840 got_worktree_checkout_cb progress_cb;
3841 void *progress_arg;
3842 struct got_repository *repo;
3845 static const struct got_error *
3846 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3847 const char *relpath, struct got_object_id *blob_id,
3848 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3849 int dirfd, const char *de_name)
3851 struct schedule_addition_args *a = arg;
3852 const struct got_error *err = NULL;
3853 struct got_fileindex_entry *ie;
3854 struct stat sb;
3855 char *ondisk_path;
3857 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3858 relpath) == -1)
3859 return got_error_from_errno("asprintf");
3861 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3862 if (ie) {
3863 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3864 de_name, a->repo);
3865 if (err)
3866 goto done;
3867 /* Re-adding an existing entry is a no-op. */
3868 if (status == GOT_STATUS_ADD)
3869 goto done;
3870 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3871 if (err)
3872 goto done;
3875 if (status != GOT_STATUS_UNVERSIONED) {
3876 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3877 goto done;
3880 err = got_fileindex_entry_alloc(&ie, relpath);
3881 if (err)
3882 goto done;
3883 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3884 relpath, NULL, NULL, 1);
3885 if (err) {
3886 got_fileindex_entry_free(ie);
3887 goto done;
3889 err = got_fileindex_entry_add(a->fileindex, ie);
3890 if (err) {
3891 got_fileindex_entry_free(ie);
3892 goto done;
3894 done:
3895 free(ondisk_path);
3896 if (err)
3897 return err;
3898 if (status == GOT_STATUS_ADD)
3899 return NULL;
3900 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3903 const struct got_error *
3904 got_worktree_schedule_add(struct got_worktree *worktree,
3905 struct got_pathlist_head *paths,
3906 got_worktree_checkout_cb progress_cb, void *progress_arg,
3907 struct got_repository *repo, int no_ignores)
3909 struct got_fileindex *fileindex = NULL;
3910 char *fileindex_path = NULL;
3911 const struct got_error *err = NULL, *sync_err, *unlockerr;
3912 struct got_pathlist_entry *pe;
3913 struct schedule_addition_args saa;
3915 err = lock_worktree(worktree, LOCK_EX);
3916 if (err)
3917 return err;
3919 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3920 if (err)
3921 goto done;
3923 saa.worktree = worktree;
3924 saa.fileindex = fileindex;
3925 saa.progress_cb = progress_cb;
3926 saa.progress_arg = progress_arg;
3927 saa.repo = repo;
3929 TAILQ_FOREACH(pe, paths, entry) {
3930 err = worktree_status(worktree, pe->path, fileindex, repo,
3931 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3932 if (err)
3933 break;
3935 sync_err = sync_fileindex(fileindex, fileindex_path);
3936 if (sync_err && err == NULL)
3937 err = sync_err;
3938 done:
3939 free(fileindex_path);
3940 if (fileindex)
3941 got_fileindex_free(fileindex);
3942 unlockerr = lock_worktree(worktree, LOCK_SH);
3943 if (unlockerr && err == NULL)
3944 err = unlockerr;
3945 return err;
3948 struct schedule_deletion_args {
3949 struct got_worktree *worktree;
3950 struct got_fileindex *fileindex;
3951 got_worktree_delete_cb progress_cb;
3952 void *progress_arg;
3953 struct got_repository *repo;
3954 int delete_local_mods;
3955 int keep_on_disk;
3956 const char *status_codes;
3959 static const struct got_error *
3960 schedule_for_deletion(void *arg, unsigned char status,
3961 unsigned char staged_status, const char *relpath,
3962 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3963 struct got_object_id *commit_id, int dirfd, const char *de_name)
3965 struct schedule_deletion_args *a = arg;
3966 const struct got_error *err = NULL;
3967 struct got_fileindex_entry *ie = NULL;
3968 struct stat sb;
3969 char *ondisk_path;
3971 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3972 if (ie == NULL)
3973 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3975 staged_status = get_staged_status(ie);
3976 if (staged_status != GOT_STATUS_NO_CHANGE) {
3977 if (staged_status == GOT_STATUS_DELETE)
3978 return NULL;
3979 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3982 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3983 relpath) == -1)
3984 return got_error_from_errno("asprintf");
3986 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3987 a->repo);
3988 if (err)
3989 goto done;
3991 if (a->status_codes) {
3992 size_t ncodes = strlen(a->status_codes);
3993 int i;
3994 for (i = 0; i < ncodes ; i++) {
3995 if (status == a->status_codes[i])
3996 break;
3998 if (i == ncodes) {
3999 /* Do not delete files in non-matching status. */
4000 free(ondisk_path);
4001 return NULL;
4003 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4004 a->status_codes[i] != GOT_STATUS_MISSING) {
4005 static char msg[64];
4006 snprintf(msg, sizeof(msg),
4007 "invalid status code '%c'", a->status_codes[i]);
4008 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4009 goto done;
4013 if (status != GOT_STATUS_NO_CHANGE) {
4014 if (status == GOT_STATUS_DELETE)
4015 goto done;
4016 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4017 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4018 goto done;
4020 if (status != GOT_STATUS_MODIFY &&
4021 status != GOT_STATUS_MISSING) {
4022 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4023 goto done;
4027 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4028 size_t root_len;
4030 if (dirfd != -1) {
4031 if (unlinkat(dirfd, de_name, 0) != 0) {
4032 err = got_error_from_errno2("unlinkat",
4033 ondisk_path);
4034 goto done;
4036 } else if (unlink(ondisk_path) != 0) {
4037 err = got_error_from_errno2("unlink", ondisk_path);
4038 goto done;
4041 root_len = strlen(a->worktree->root_path);
4042 do {
4043 char *parent;
4044 err = got_path_dirname(&parent, ondisk_path);
4045 if (err)
4046 goto done;
4047 free(ondisk_path);
4048 ondisk_path = parent;
4049 if (rmdir(ondisk_path) == -1) {
4050 if (errno != ENOTEMPTY)
4051 err = got_error_from_errno2("rmdir",
4052 ondisk_path);
4053 break;
4055 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4056 strlen(ondisk_path), root_len) != 0);
4059 got_fileindex_entry_mark_deleted_from_disk(ie);
4060 done:
4061 free(ondisk_path);
4062 if (err)
4063 return err;
4064 if (status == GOT_STATUS_DELETE)
4065 return NULL;
4066 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4067 staged_status, relpath);
4070 const struct got_error *
4071 got_worktree_schedule_delete(struct got_worktree *worktree,
4072 struct got_pathlist_head *paths, int delete_local_mods,
4073 const char *status_codes,
4074 got_worktree_delete_cb progress_cb, void *progress_arg,
4075 struct got_repository *repo, int keep_on_disk)
4077 struct got_fileindex *fileindex = NULL;
4078 char *fileindex_path = NULL;
4079 const struct got_error *err = NULL, *sync_err, *unlockerr;
4080 struct got_pathlist_entry *pe;
4081 struct schedule_deletion_args sda;
4083 err = lock_worktree(worktree, LOCK_EX);
4084 if (err)
4085 return err;
4087 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4088 if (err)
4089 goto done;
4091 sda.worktree = worktree;
4092 sda.fileindex = fileindex;
4093 sda.progress_cb = progress_cb;
4094 sda.progress_arg = progress_arg;
4095 sda.repo = repo;
4096 sda.delete_local_mods = delete_local_mods;
4097 sda.keep_on_disk = keep_on_disk;
4098 sda.status_codes = status_codes;
4100 TAILQ_FOREACH(pe, paths, entry) {
4101 err = worktree_status(worktree, pe->path, fileindex, repo,
4102 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4103 if (err)
4104 break;
4106 sync_err = sync_fileindex(fileindex, fileindex_path);
4107 if (sync_err && err == NULL)
4108 err = sync_err;
4109 done:
4110 free(fileindex_path);
4111 if (fileindex)
4112 got_fileindex_free(fileindex);
4113 unlockerr = lock_worktree(worktree, LOCK_SH);
4114 if (unlockerr && err == NULL)
4115 err = unlockerr;
4116 return err;
4119 static const struct got_error *
4120 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4122 const struct got_error *err = NULL;
4123 char *line = NULL;
4124 size_t linesize = 0, n;
4125 ssize_t linelen;
4127 linelen = getline(&line, &linesize, infile);
4128 if (linelen == -1) {
4129 if (ferror(infile)) {
4130 err = got_error_from_errno("getline");
4131 goto done;
4133 return NULL;
4135 if (outfile) {
4136 n = fwrite(line, 1, linelen, outfile);
4137 if (n != linelen) {
4138 err = got_ferror(outfile, GOT_ERR_IO);
4139 goto done;
4142 if (rejectfile) {
4143 n = fwrite(line, 1, linelen, rejectfile);
4144 if (n != linelen)
4145 err = got_ferror(outfile, GOT_ERR_IO);
4147 done:
4148 free(line);
4149 return err;
4152 static const struct got_error *
4153 skip_one_line(FILE *f)
4155 char *line = NULL;
4156 size_t linesize = 0;
4157 ssize_t linelen;
4159 linelen = getline(&line, &linesize, f);
4160 if (linelen == -1) {
4161 if (ferror(f))
4162 return got_error_from_errno("getline");
4163 return NULL;
4165 free(line);
4166 return NULL;
4169 static const struct got_error *
4170 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4171 int start_old, int end_old, int start_new, int end_new,
4172 FILE *outfile, FILE *rejectfile)
4174 const struct got_error *err;
4176 /* Copy old file's lines leading up to patch. */
4177 while (!feof(f1) && *line_cur1 < start_old) {
4178 err = copy_one_line(f1, outfile, NULL);
4179 if (err)
4180 return err;
4181 (*line_cur1)++;
4183 /* Skip new file's lines leading up to patch. */
4184 while (!feof(f2) && *line_cur2 < start_new) {
4185 if (rejectfile)
4186 err = copy_one_line(f2, NULL, rejectfile);
4187 else
4188 err = skip_one_line(f2);
4189 if (err)
4190 return err;
4191 (*line_cur2)++;
4193 /* Copy patched lines. */
4194 while (!feof(f2) && *line_cur2 <= end_new) {
4195 err = copy_one_line(f2, outfile, NULL);
4196 if (err)
4197 return err;
4198 (*line_cur2)++;
4200 /* Skip over old file's replaced lines. */
4201 while (!feof(f1) && *line_cur1 <= end_old) {
4202 if (rejectfile)
4203 err = copy_one_line(f1, NULL, rejectfile);
4204 else
4205 err = skip_one_line(f1);
4206 if (err)
4207 return err;
4208 (*line_cur1)++;
4211 return NULL;
4214 static const struct got_error *
4215 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4216 FILE *outfile, FILE *rejectfile)
4218 const struct got_error *err;
4220 if (outfile) {
4221 /* Copy old file's lines until EOF. */
4222 while (!feof(f1)) {
4223 err = copy_one_line(f1, outfile, NULL);
4224 if (err)
4225 return err;
4226 (*line_cur1)++;
4229 if (rejectfile) {
4230 /* Copy new file's lines until EOF. */
4231 while (!feof(f2)) {
4232 err = copy_one_line(f2, NULL, rejectfile);
4233 if (err)
4234 return err;
4235 (*line_cur2)++;
4239 return NULL;
4242 static const struct got_error *
4243 apply_or_reject_change(int *choice, int *nchunks_used,
4244 struct diff_result *diff_result, int n,
4245 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4246 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4247 got_worktree_patch_cb patch_cb, void *patch_arg)
4249 const struct got_error *err = NULL;
4250 struct diff_chunk_context cc = {};
4251 int start_old, end_old, start_new, end_new;
4252 FILE *hunkfile;
4253 struct diff_output_unidiff_state *diff_state;
4254 struct diff_input_info diff_info;
4255 int rc;
4257 *choice = GOT_PATCH_CHOICE_NONE;
4259 /* Get changed line numbers without context lines for copy_change(). */
4260 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4261 start_old = cc.left.start;
4262 end_old = cc.left.end;
4263 start_new = cc.right.start;
4264 end_new = cc.right.end;
4266 /* Get the same change with context lines for display. */
4267 memset(&cc, 0, sizeof(cc));
4268 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4270 memset(&diff_info, 0, sizeof(diff_info));
4271 diff_info.left_path = relpath;
4272 diff_info.right_path = relpath;
4274 diff_state = diff_output_unidiff_state_alloc();
4275 if (diff_state == NULL)
4276 return got_error_set_errno(ENOMEM,
4277 "diff_output_unidiff_state_alloc");
4279 hunkfile = got_opentemp();
4280 if (hunkfile == NULL) {
4281 err = got_error_from_errno("got_opentemp");
4282 goto done;
4285 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4286 diff_result, &cc);
4287 if (rc != DIFF_RC_OK) {
4288 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4289 goto done;
4292 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4293 err = got_ferror(hunkfile, GOT_ERR_IO);
4294 goto done;
4297 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4298 hunkfile, changeno, nchanges);
4299 if (err)
4300 goto done;
4302 switch (*choice) {
4303 case GOT_PATCH_CHOICE_YES:
4304 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4305 end_old, start_new, end_new, outfile, rejectfile);
4306 break;
4307 case GOT_PATCH_CHOICE_NO:
4308 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4309 end_old, start_new, end_new, rejectfile, outfile);
4310 break;
4311 case GOT_PATCH_CHOICE_QUIT:
4312 break;
4313 default:
4314 err = got_error(GOT_ERR_PATCH_CHOICE);
4315 break;
4317 done:
4318 diff_output_unidiff_state_free(diff_state);
4319 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4320 err = got_error_from_errno("fclose");
4321 return err;
4324 struct revert_file_args {
4325 struct got_worktree *worktree;
4326 struct got_fileindex *fileindex;
4327 got_worktree_checkout_cb progress_cb;
4328 void *progress_arg;
4329 got_worktree_patch_cb patch_cb;
4330 void *patch_arg;
4331 struct got_repository *repo;
4332 int unlink_added_files;
4335 static const struct got_error *
4336 create_patched_content(char **path_outfile, int reverse_patch,
4337 struct got_object_id *blob_id, const char *path2,
4338 int dirfd2, const char *de_name2,
4339 const char *relpath, struct got_repository *repo,
4340 got_worktree_patch_cb patch_cb, void *patch_arg)
4342 const struct got_error *err, *free_err;
4343 struct got_blob_object *blob = NULL;
4344 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4345 int fd2 = -1;
4346 char link_target[PATH_MAX];
4347 ssize_t link_len = 0;
4348 char *path1 = NULL, *id_str = NULL;
4349 struct stat sb2;
4350 struct got_diffreg_result *diffreg_result = NULL;
4351 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4352 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4354 *path_outfile = NULL;
4356 err = got_object_id_str(&id_str, blob_id);
4357 if (err)
4358 return err;
4360 if (dirfd2 != -1) {
4361 fd2 = openat(dirfd2, de_name2,
4362 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4363 if (fd2 == -1) {
4364 if (!got_err_open_nofollow_on_symlink()) {
4365 err = got_error_from_errno2("openat", path2);
4366 goto done;
4368 link_len = readlinkat(dirfd2, de_name2,
4369 link_target, sizeof(link_target));
4370 if (link_len == -1) {
4371 return got_error_from_errno2("readlinkat",
4372 path2);
4374 sb2.st_mode = S_IFLNK;
4375 sb2.st_size = link_len;
4377 } else {
4378 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4379 if (fd2 == -1) {
4380 if (!got_err_open_nofollow_on_symlink()) {
4381 err = got_error_from_errno2("open", path2);
4382 goto done;
4384 link_len = readlink(path2, link_target,
4385 sizeof(link_target));
4386 if (link_len == -1)
4387 return got_error_from_errno2("readlink", path2);
4388 sb2.st_mode = S_IFLNK;
4389 sb2.st_size = link_len;
4392 if (fd2 != -1) {
4393 if (fstat(fd2, &sb2) == -1) {
4394 err = got_error_from_errno2("fstat", path2);
4395 goto done;
4398 f2 = fdopen(fd2, "r");
4399 if (f2 == NULL) {
4400 err = got_error_from_errno2("fdopen", path2);
4401 goto done;
4403 fd2 = -1;
4404 } else {
4405 size_t n;
4406 f2 = got_opentemp();
4407 if (f2 == NULL) {
4408 err = got_error_from_errno2("got_opentemp", path2);
4409 goto done;
4411 n = fwrite(link_target, 1, link_len, f2);
4412 if (n != link_len) {
4413 err = got_ferror(f2, GOT_ERR_IO);
4414 goto done;
4416 if (fflush(f2) == EOF) {
4417 err = got_error_from_errno("fflush");
4418 goto done;
4420 rewind(f2);
4423 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4424 if (err)
4425 goto done;
4427 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4428 if (err)
4429 goto done;
4431 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4432 if (err)
4433 goto done;
4435 err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4436 NULL);
4437 if (err)
4438 goto done;
4440 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4441 if (err)
4442 goto done;
4444 if (fseek(f1, 0L, SEEK_SET) == -1)
4445 return got_ferror(f1, GOT_ERR_IO);
4446 if (fseek(f2, 0L, SEEK_SET) == -1)
4447 return got_ferror(f2, GOT_ERR_IO);
4449 /* Count the number of actual changes in the diff result. */
4450 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4451 struct diff_chunk_context cc = {};
4452 diff_chunk_context_load_change(&cc, &nchunks_used,
4453 diffreg_result->result, n, 0);
4454 nchanges++;
4456 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4457 int choice;
4458 err = apply_or_reject_change(&choice, &nchunks_used,
4459 diffreg_result->result, n, relpath, f1, f2,
4460 &line_cur1, &line_cur2,
4461 reverse_patch ? NULL : outfile,
4462 reverse_patch ? outfile : NULL,
4463 ++i, nchanges, patch_cb, patch_arg);
4464 if (err)
4465 goto done;
4466 if (choice == GOT_PATCH_CHOICE_YES)
4467 have_content = 1;
4468 else if (choice == GOT_PATCH_CHOICE_QUIT)
4469 break;
4471 if (have_content) {
4472 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4473 reverse_patch ? NULL : outfile,
4474 reverse_patch ? outfile : NULL);
4475 if (err)
4476 goto done;
4478 if (!S_ISLNK(sb2.st_mode)) {
4479 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4480 err = got_error_from_errno2("fchmod", path2);
4481 goto done;
4485 done:
4486 free(id_str);
4487 if (blob)
4488 got_object_blob_close(blob);
4489 free_err = got_diffreg_result_free(diffreg_result);
4490 if (err == NULL)
4491 err = free_err;
4492 if (f1 && fclose(f1) == EOF && err == NULL)
4493 err = got_error_from_errno2("fclose", path1);
4494 if (f2 && fclose(f2) == EOF && err == NULL)
4495 err = got_error_from_errno2("fclose", path2);
4496 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4497 err = got_error_from_errno2("close", path2);
4498 if (outfile && fclose(outfile) == EOF && err == NULL)
4499 err = got_error_from_errno2("fclose", *path_outfile);
4500 if (path1 && unlink(path1) == -1 && err == NULL)
4501 err = got_error_from_errno2("unlink", path1);
4502 if (err || !have_content) {
4503 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4504 err = got_error_from_errno2("unlink", *path_outfile);
4505 free(*path_outfile);
4506 *path_outfile = NULL;
4508 free(path1);
4509 return err;
4512 static const struct got_error *
4513 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4514 const char *relpath, struct got_object_id *blob_id,
4515 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4516 int dirfd, const char *de_name)
4518 struct revert_file_args *a = arg;
4519 const struct got_error *err = NULL;
4520 char *parent_path = NULL;
4521 struct got_fileindex_entry *ie;
4522 struct got_tree_object *tree = NULL;
4523 struct got_object_id *tree_id = NULL;
4524 const struct got_tree_entry *te = NULL;
4525 char *tree_path = NULL, *te_name;
4526 char *ondisk_path = NULL, *path_content = NULL;
4527 struct got_blob_object *blob = NULL;
4529 /* Reverting a staged deletion is a no-op. */
4530 if (status == GOT_STATUS_DELETE &&
4531 staged_status != GOT_STATUS_NO_CHANGE)
4532 return NULL;
4534 if (status == GOT_STATUS_UNVERSIONED)
4535 return (*a->progress_cb)(a->progress_arg,
4536 GOT_STATUS_UNVERSIONED, relpath);
4538 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4539 if (ie == NULL)
4540 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4542 /* Construct in-repository path of tree which contains this blob. */
4543 err = got_path_dirname(&parent_path, ie->path);
4544 if (err) {
4545 if (err->code != GOT_ERR_BAD_PATH)
4546 goto done;
4547 parent_path = strdup("/");
4548 if (parent_path == NULL) {
4549 err = got_error_from_errno("strdup");
4550 goto done;
4553 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4554 tree_path = strdup(parent_path);
4555 if (tree_path == NULL) {
4556 err = got_error_from_errno("strdup");
4557 goto done;
4559 } else {
4560 if (got_path_is_root_dir(parent_path)) {
4561 tree_path = strdup(a->worktree->path_prefix);
4562 if (tree_path == NULL) {
4563 err = got_error_from_errno("strdup");
4564 goto done;
4566 } else {
4567 if (asprintf(&tree_path, "%s/%s",
4568 a->worktree->path_prefix, parent_path) == -1) {
4569 err = got_error_from_errno("asprintf");
4570 goto done;
4575 err = got_object_id_by_path(&tree_id, a->repo,
4576 a->worktree->base_commit_id, tree_path);
4577 if (err) {
4578 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4579 (status == GOT_STATUS_ADD ||
4580 staged_status == GOT_STATUS_ADD)))
4581 goto done;
4582 } else {
4583 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4584 if (err)
4585 goto done;
4587 err = got_path_basename(&te_name, ie->path);
4588 if (err)
4589 goto done;
4591 te = got_object_tree_find_entry(tree, te_name);
4592 free(te_name);
4593 if (te == NULL && status != GOT_STATUS_ADD &&
4594 staged_status != GOT_STATUS_ADD) {
4595 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4596 goto done;
4600 switch (status) {
4601 case GOT_STATUS_ADD:
4602 if (a->patch_cb) {
4603 int choice = GOT_PATCH_CHOICE_NONE;
4604 err = (*a->patch_cb)(&choice, a->patch_arg,
4605 status, ie->path, NULL, 1, 1);
4606 if (err)
4607 goto done;
4608 if (choice != GOT_PATCH_CHOICE_YES)
4609 break;
4611 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4612 ie->path);
4613 if (err)
4614 goto done;
4615 got_fileindex_entry_remove(a->fileindex, ie);
4616 if (a->unlink_added_files) {
4617 if (asprintf(&ondisk_path, "%s/%s",
4618 got_worktree_get_root_path(a->worktree),
4619 relpath) == -1) {
4620 err = got_error_from_errno("asprintf");
4621 goto done;
4623 if (unlink(ondisk_path) == -1) {
4624 err = got_error_from_errno2("unlink",
4625 ondisk_path);
4626 break;
4629 break;
4630 case GOT_STATUS_DELETE:
4631 if (a->patch_cb) {
4632 int choice = GOT_PATCH_CHOICE_NONE;
4633 err = (*a->patch_cb)(&choice, a->patch_arg,
4634 status, ie->path, NULL, 1, 1);
4635 if (err)
4636 goto done;
4637 if (choice != GOT_PATCH_CHOICE_YES)
4638 break;
4640 /* fall through */
4641 case GOT_STATUS_MODIFY:
4642 case GOT_STATUS_MODE_CHANGE:
4643 case GOT_STATUS_CONFLICT:
4644 case GOT_STATUS_MISSING: {
4645 struct got_object_id id;
4646 if (staged_status == GOT_STATUS_ADD ||
4647 staged_status == GOT_STATUS_MODIFY) {
4648 memcpy(id.sha1, ie->staged_blob_sha1,
4649 SHA1_DIGEST_LENGTH);
4650 } else
4651 memcpy(id.sha1, ie->blob_sha1,
4652 SHA1_DIGEST_LENGTH);
4653 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4654 if (err)
4655 goto done;
4657 if (asprintf(&ondisk_path, "%s/%s",
4658 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4659 err = got_error_from_errno("asprintf");
4660 goto done;
4663 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4664 status == GOT_STATUS_CONFLICT)) {
4665 int is_bad_symlink = 0;
4666 err = create_patched_content(&path_content, 1, &id,
4667 ondisk_path, dirfd, de_name, ie->path, a->repo,
4668 a->patch_cb, a->patch_arg);
4669 if (err || path_content == NULL)
4670 break;
4671 if (te && S_ISLNK(te->mode)) {
4672 if (unlink(path_content) == -1) {
4673 err = got_error_from_errno2("unlink",
4674 path_content);
4675 break;
4677 err = install_symlink(&is_bad_symlink,
4678 a->worktree, ondisk_path, ie->path,
4679 blob, 0, 1, 0, 0, a->repo,
4680 a->progress_cb, a->progress_arg);
4681 } else {
4682 if (rename(path_content, ondisk_path) == -1) {
4683 err = got_error_from_errno3("rename",
4684 path_content, ondisk_path);
4685 goto done;
4688 } else {
4689 int is_bad_symlink = 0;
4690 if (te && S_ISLNK(te->mode)) {
4691 err = install_symlink(&is_bad_symlink,
4692 a->worktree, ondisk_path, ie->path,
4693 blob, 0, 1, 0, 0, a->repo,
4694 a->progress_cb, a->progress_arg);
4695 } else {
4696 err = install_blob(a->worktree, ondisk_path,
4697 ie->path,
4698 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4699 got_fileindex_perms_to_st(ie), blob,
4700 0, 1, 0, 0, a->repo,
4701 a->progress_cb, a->progress_arg);
4703 if (err)
4704 goto done;
4705 if (status == GOT_STATUS_DELETE ||
4706 status == GOT_STATUS_MODE_CHANGE) {
4707 err = got_fileindex_entry_update(ie,
4708 a->worktree->root_fd, relpath,
4709 blob->id.sha1,
4710 a->worktree->base_commit_id->sha1, 1);
4711 if (err)
4712 goto done;
4714 if (is_bad_symlink) {
4715 got_fileindex_entry_filetype_set(ie,
4716 GOT_FILEIDX_MODE_BAD_SYMLINK);
4719 break;
4721 default:
4722 break;
4724 done:
4725 free(ondisk_path);
4726 free(path_content);
4727 free(parent_path);
4728 free(tree_path);
4729 if (blob)
4730 got_object_blob_close(blob);
4731 if (tree)
4732 got_object_tree_close(tree);
4733 free(tree_id);
4734 return err;
4737 const struct got_error *
4738 got_worktree_revert(struct got_worktree *worktree,
4739 struct got_pathlist_head *paths,
4740 got_worktree_checkout_cb progress_cb, void *progress_arg,
4741 got_worktree_patch_cb patch_cb, void *patch_arg,
4742 struct got_repository *repo)
4744 struct got_fileindex *fileindex = NULL;
4745 char *fileindex_path = NULL;
4746 const struct got_error *err = NULL, *unlockerr = NULL;
4747 const struct got_error *sync_err = NULL;
4748 struct got_pathlist_entry *pe;
4749 struct revert_file_args rfa;
4751 err = lock_worktree(worktree, LOCK_EX);
4752 if (err)
4753 return err;
4755 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4756 if (err)
4757 goto done;
4759 rfa.worktree = worktree;
4760 rfa.fileindex = fileindex;
4761 rfa.progress_cb = progress_cb;
4762 rfa.progress_arg = progress_arg;
4763 rfa.patch_cb = patch_cb;
4764 rfa.patch_arg = patch_arg;
4765 rfa.repo = repo;
4766 rfa.unlink_added_files = 0;
4767 TAILQ_FOREACH(pe, paths, entry) {
4768 err = worktree_status(worktree, pe->path, fileindex, repo,
4769 revert_file, &rfa, NULL, NULL, 1, 0);
4770 if (err)
4771 break;
4773 sync_err = sync_fileindex(fileindex, fileindex_path);
4774 if (sync_err && err == NULL)
4775 err = sync_err;
4776 done:
4777 free(fileindex_path);
4778 if (fileindex)
4779 got_fileindex_free(fileindex);
4780 unlockerr = lock_worktree(worktree, LOCK_SH);
4781 if (unlockerr && err == NULL)
4782 err = unlockerr;
4783 return err;
4786 static void
4787 free_commitable(struct got_commitable *ct)
4789 free(ct->path);
4790 free(ct->in_repo_path);
4791 free(ct->ondisk_path);
4792 free(ct->blob_id);
4793 free(ct->base_blob_id);
4794 free(ct->staged_blob_id);
4795 free(ct->base_commit_id);
4796 free(ct);
4799 struct collect_commitables_arg {
4800 struct got_pathlist_head *commitable_paths;
4801 struct got_repository *repo;
4802 struct got_worktree *worktree;
4803 struct got_fileindex *fileindex;
4804 int have_staged_files;
4805 int allow_bad_symlinks;
4808 static const struct got_error *
4809 collect_commitables(void *arg, unsigned char status,
4810 unsigned char staged_status, const char *relpath,
4811 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4812 struct got_object_id *commit_id, int dirfd, const char *de_name)
4814 struct collect_commitables_arg *a = arg;
4815 const struct got_error *err = NULL;
4816 struct got_commitable *ct = NULL;
4817 struct got_pathlist_entry *new = NULL;
4818 char *parent_path = NULL, *path = NULL;
4819 struct stat sb;
4821 if (a->have_staged_files) {
4822 if (staged_status != GOT_STATUS_MODIFY &&
4823 staged_status != GOT_STATUS_ADD &&
4824 staged_status != GOT_STATUS_DELETE)
4825 return NULL;
4826 } else {
4827 if (status == GOT_STATUS_CONFLICT)
4828 return got_error(GOT_ERR_COMMIT_CONFLICT);
4830 if (status != GOT_STATUS_MODIFY &&
4831 status != GOT_STATUS_MODE_CHANGE &&
4832 status != GOT_STATUS_ADD &&
4833 status != GOT_STATUS_DELETE)
4834 return NULL;
4837 if (asprintf(&path, "/%s", relpath) == -1) {
4838 err = got_error_from_errno("asprintf");
4839 goto done;
4841 if (strcmp(path, "/") == 0) {
4842 parent_path = strdup("");
4843 if (parent_path == NULL)
4844 return got_error_from_errno("strdup");
4845 } else {
4846 err = got_path_dirname(&parent_path, path);
4847 if (err)
4848 return err;
4851 ct = calloc(1, sizeof(*ct));
4852 if (ct == NULL) {
4853 err = got_error_from_errno("calloc");
4854 goto done;
4857 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4858 relpath) == -1) {
4859 err = got_error_from_errno("asprintf");
4860 goto done;
4863 if (staged_status == GOT_STATUS_ADD ||
4864 staged_status == GOT_STATUS_MODIFY) {
4865 struct got_fileindex_entry *ie;
4866 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4867 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4868 case GOT_FILEIDX_MODE_REGULAR_FILE:
4869 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4870 ct->mode = S_IFREG;
4871 break;
4872 case GOT_FILEIDX_MODE_SYMLINK:
4873 ct->mode = S_IFLNK;
4874 break;
4875 default:
4876 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4877 goto done;
4879 ct->mode |= got_fileindex_entry_perms_get(ie);
4880 } else if (status != GOT_STATUS_DELETE &&
4881 staged_status != GOT_STATUS_DELETE) {
4882 if (dirfd != -1) {
4883 if (fstatat(dirfd, de_name, &sb,
4884 AT_SYMLINK_NOFOLLOW) == -1) {
4885 err = got_error_from_errno2("fstatat",
4886 ct->ondisk_path);
4887 goto done;
4889 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4890 err = got_error_from_errno2("lstat", ct->ondisk_path);
4891 goto done;
4893 ct->mode = sb.st_mode;
4896 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4897 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4898 relpath) == -1) {
4899 err = got_error_from_errno("asprintf");
4900 goto done;
4903 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4904 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4905 int is_bad_symlink;
4906 char target_path[PATH_MAX];
4907 ssize_t target_len;
4908 target_len = readlink(ct->ondisk_path, target_path,
4909 sizeof(target_path));
4910 if (target_len == -1) {
4911 err = got_error_from_errno2("readlink",
4912 ct->ondisk_path);
4913 goto done;
4915 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4916 target_len, ct->ondisk_path, a->worktree->root_path);
4917 if (err)
4918 goto done;
4919 if (is_bad_symlink) {
4920 err = got_error_path(ct->ondisk_path,
4921 GOT_ERR_BAD_SYMLINK);
4922 goto done;
4927 ct->status = status;
4928 ct->staged_status = staged_status;
4929 ct->blob_id = NULL; /* will be filled in when blob gets created */
4930 if (ct->status != GOT_STATUS_ADD &&
4931 ct->staged_status != GOT_STATUS_ADD) {
4932 ct->base_blob_id = got_object_id_dup(blob_id);
4933 if (ct->base_blob_id == NULL) {
4934 err = got_error_from_errno("got_object_id_dup");
4935 goto done;
4937 ct->base_commit_id = got_object_id_dup(commit_id);
4938 if (ct->base_commit_id == NULL) {
4939 err = got_error_from_errno("got_object_id_dup");
4940 goto done;
4943 if (ct->staged_status == GOT_STATUS_ADD ||
4944 ct->staged_status == GOT_STATUS_MODIFY) {
4945 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4946 if (ct->staged_blob_id == NULL) {
4947 err = got_error_from_errno("got_object_id_dup");
4948 goto done;
4951 ct->path = strdup(path);
4952 if (ct->path == NULL) {
4953 err = got_error_from_errno("strdup");
4954 goto done;
4956 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4957 done:
4958 if (ct && (err || new == NULL))
4959 free_commitable(ct);
4960 free(parent_path);
4961 free(path);
4962 return err;
4965 static const struct got_error *write_tree(struct got_object_id **, int *,
4966 struct got_tree_object *, const char *, struct got_pathlist_head *,
4967 got_worktree_status_cb status_cb, void *status_arg,
4968 struct got_repository *);
4970 static const struct got_error *
4971 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4972 struct got_tree_entry *te, const char *parent_path,
4973 struct got_pathlist_head *commitable_paths,
4974 got_worktree_status_cb status_cb, void *status_arg,
4975 struct got_repository *repo)
4977 const struct got_error *err = NULL;
4978 struct got_tree_object *subtree;
4979 char *subpath;
4981 if (asprintf(&subpath, "%s%s%s", parent_path,
4982 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4983 return got_error_from_errno("asprintf");
4985 err = got_object_open_as_tree(&subtree, repo, &te->id);
4986 if (err)
4987 return err;
4989 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4990 commitable_paths, status_cb, status_arg, repo);
4991 got_object_tree_close(subtree);
4992 free(subpath);
4993 return err;
4996 static const struct got_error *
4997 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4999 const struct got_error *err = NULL;
5000 char *ct_parent_path = NULL;
5002 *match = 0;
5004 if (strchr(ct->in_repo_path, '/') == NULL) {
5005 *match = got_path_is_root_dir(path);
5006 return NULL;
5009 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5010 if (err)
5011 return err;
5012 *match = (strcmp(path, ct_parent_path) == 0);
5013 free(ct_parent_path);
5014 return err;
5017 static mode_t
5018 get_ct_file_mode(struct got_commitable *ct)
5020 if (S_ISLNK(ct->mode))
5021 return S_IFLNK;
5023 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5026 static const struct got_error *
5027 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5028 struct got_tree_entry *te, struct got_commitable *ct)
5030 const struct got_error *err = NULL;
5032 *new_te = NULL;
5034 err = got_object_tree_entry_dup(new_te, te);
5035 if (err)
5036 goto done;
5038 (*new_te)->mode = get_ct_file_mode(ct);
5040 if (ct->staged_status == GOT_STATUS_MODIFY)
5041 memcpy(&(*new_te)->id, ct->staged_blob_id,
5042 sizeof((*new_te)->id));
5043 else
5044 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5045 done:
5046 if (err && *new_te) {
5047 free(*new_te);
5048 *new_te = NULL;
5050 return err;
5053 static const struct got_error *
5054 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5055 struct got_commitable *ct)
5057 const struct got_error *err = NULL;
5058 char *ct_name = NULL;
5060 *new_te = NULL;
5062 *new_te = calloc(1, sizeof(**new_te));
5063 if (*new_te == NULL)
5064 return got_error_from_errno("calloc");
5066 err = got_path_basename(&ct_name, ct->path);
5067 if (err)
5068 goto done;
5069 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5070 sizeof((*new_te)->name)) {
5071 err = got_error(GOT_ERR_NO_SPACE);
5072 goto done;
5075 (*new_te)->mode = get_ct_file_mode(ct);
5077 if (ct->staged_status == GOT_STATUS_ADD)
5078 memcpy(&(*new_te)->id, ct->staged_blob_id,
5079 sizeof((*new_te)->id));
5080 else
5081 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5082 done:
5083 free(ct_name);
5084 if (err && *new_te) {
5085 free(*new_te);
5086 *new_te = NULL;
5088 return err;
5091 static const struct got_error *
5092 insert_tree_entry(struct got_tree_entry *new_te,
5093 struct got_pathlist_head *paths)
5095 const struct got_error *err = NULL;
5096 struct got_pathlist_entry *new_pe;
5098 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5099 if (err)
5100 return err;
5101 if (new_pe == NULL)
5102 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5103 return NULL;
5106 static const struct got_error *
5107 report_ct_status(struct got_commitable *ct,
5108 got_worktree_status_cb status_cb, void *status_arg)
5110 const char *ct_path = ct->path;
5111 unsigned char status;
5113 if (status_cb == NULL) /* no commit progress output desired */
5114 return NULL;
5116 while (ct_path[0] == '/')
5117 ct_path++;
5119 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5120 status = ct->staged_status;
5121 else
5122 status = ct->status;
5124 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5125 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5128 static const struct got_error *
5129 match_modified_subtree(int *modified, struct got_tree_entry *te,
5130 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5132 const struct got_error *err = NULL;
5133 struct got_pathlist_entry *pe;
5134 char *te_path;
5136 *modified = 0;
5138 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5139 got_path_is_root_dir(base_tree_path) ? "" : "/",
5140 te->name) == -1)
5141 return got_error_from_errno("asprintf");
5143 TAILQ_FOREACH(pe, commitable_paths, entry) {
5144 struct got_commitable *ct = pe->data;
5145 *modified = got_path_is_child(ct->in_repo_path, te_path,
5146 strlen(te_path));
5147 if (*modified)
5148 break;
5151 free(te_path);
5152 return err;
5155 static const struct got_error *
5156 match_deleted_or_modified_ct(struct got_commitable **ctp,
5157 struct got_tree_entry *te, const char *base_tree_path,
5158 struct got_pathlist_head *commitable_paths)
5160 const struct got_error *err = NULL;
5161 struct got_pathlist_entry *pe;
5163 *ctp = NULL;
5165 TAILQ_FOREACH(pe, commitable_paths, entry) {
5166 struct got_commitable *ct = pe->data;
5167 char *ct_name = NULL;
5168 int path_matches;
5170 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5171 if (ct->status != GOT_STATUS_MODIFY &&
5172 ct->status != GOT_STATUS_MODE_CHANGE &&
5173 ct->status != GOT_STATUS_DELETE)
5174 continue;
5175 } else {
5176 if (ct->staged_status != GOT_STATUS_MODIFY &&
5177 ct->staged_status != GOT_STATUS_DELETE)
5178 continue;
5181 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5182 continue;
5184 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5185 if (err)
5186 return err;
5187 if (!path_matches)
5188 continue;
5190 err = got_path_basename(&ct_name, pe->path);
5191 if (err)
5192 return err;
5194 if (strcmp(te->name, ct_name) != 0) {
5195 free(ct_name);
5196 continue;
5198 free(ct_name);
5200 *ctp = ct;
5201 break;
5204 return err;
5207 static const struct got_error *
5208 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5209 const char *child_path, const char *path_base_tree,
5210 struct got_pathlist_head *commitable_paths,
5211 got_worktree_status_cb status_cb, void *status_arg,
5212 struct got_repository *repo)
5214 const struct got_error *err = NULL;
5215 struct got_tree_entry *new_te;
5216 char *subtree_path;
5217 struct got_object_id *id = NULL;
5218 int nentries;
5220 *new_tep = NULL;
5222 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5223 got_path_is_root_dir(path_base_tree) ? "" : "/",
5224 child_path) == -1)
5225 return got_error_from_errno("asprintf");
5227 new_te = calloc(1, sizeof(*new_te));
5228 if (new_te == NULL)
5229 return got_error_from_errno("calloc");
5230 new_te->mode = S_IFDIR;
5232 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5233 sizeof(new_te->name)) {
5234 err = got_error(GOT_ERR_NO_SPACE);
5235 goto done;
5237 err = write_tree(&id, &nentries, NULL, subtree_path,
5238 commitable_paths, status_cb, status_arg, repo);
5239 if (err) {
5240 free(new_te);
5241 goto done;
5243 memcpy(&new_te->id, id, sizeof(new_te->id));
5244 done:
5245 free(id);
5246 free(subtree_path);
5247 if (err == NULL)
5248 *new_tep = new_te;
5249 return err;
5252 static const struct got_error *
5253 write_tree(struct got_object_id **new_tree_id, int *nentries,
5254 struct got_tree_object *base_tree, const char *path_base_tree,
5255 struct got_pathlist_head *commitable_paths,
5256 got_worktree_status_cb status_cb, void *status_arg,
5257 struct got_repository *repo)
5259 const struct got_error *err = NULL;
5260 struct got_pathlist_head paths;
5261 struct got_tree_entry *te, *new_te = NULL;
5262 struct got_pathlist_entry *pe;
5264 TAILQ_INIT(&paths);
5265 *nentries = 0;
5267 /* Insert, and recurse into, newly added entries first. */
5268 TAILQ_FOREACH(pe, commitable_paths, entry) {
5269 struct got_commitable *ct = pe->data;
5270 char *child_path = NULL, *slash;
5272 if ((ct->status != GOT_STATUS_ADD &&
5273 ct->staged_status != GOT_STATUS_ADD) ||
5274 (ct->flags & GOT_COMMITABLE_ADDED))
5275 continue;
5277 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5278 strlen(path_base_tree)))
5279 continue;
5281 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5282 ct->in_repo_path);
5283 if (err)
5284 goto done;
5286 slash = strchr(child_path, '/');
5287 if (slash == NULL) {
5288 err = alloc_added_blob_tree_entry(&new_te, ct);
5289 if (err)
5290 goto done;
5291 err = report_ct_status(ct, status_cb, status_arg);
5292 if (err)
5293 goto done;
5294 ct->flags |= GOT_COMMITABLE_ADDED;
5295 err = insert_tree_entry(new_te, &paths);
5296 if (err)
5297 goto done;
5298 (*nentries)++;
5299 } else {
5300 *slash = '\0'; /* trim trailing path components */
5301 if (base_tree == NULL ||
5302 got_object_tree_find_entry(base_tree, child_path)
5303 == NULL) {
5304 err = make_subtree_for_added_blob(&new_te,
5305 child_path, path_base_tree,
5306 commitable_paths, status_cb, status_arg,
5307 repo);
5308 if (err)
5309 goto done;
5310 err = insert_tree_entry(new_te, &paths);
5311 if (err)
5312 goto done;
5313 (*nentries)++;
5318 if (base_tree) {
5319 int i, nbase_entries;
5320 /* Handle modified and deleted entries. */
5321 nbase_entries = got_object_tree_get_nentries(base_tree);
5322 for (i = 0; i < nbase_entries; i++) {
5323 struct got_commitable *ct = NULL;
5325 te = got_object_tree_get_entry(base_tree, i);
5326 if (got_object_tree_entry_is_submodule(te)) {
5327 /* Entry is a submodule; just copy it. */
5328 err = got_object_tree_entry_dup(&new_te, te);
5329 if (err)
5330 goto done;
5331 err = insert_tree_entry(new_te, &paths);
5332 if (err)
5333 goto done;
5334 (*nentries)++;
5335 continue;
5338 if (S_ISDIR(te->mode)) {
5339 int modified;
5340 err = got_object_tree_entry_dup(&new_te, te);
5341 if (err)
5342 goto done;
5343 err = match_modified_subtree(&modified, te,
5344 path_base_tree, commitable_paths);
5345 if (err)
5346 goto done;
5347 /* Avoid recursion into unmodified subtrees. */
5348 if (modified) {
5349 struct got_object_id *new_id;
5350 int nsubentries;
5351 err = write_subtree(&new_id,
5352 &nsubentries, te,
5353 path_base_tree, commitable_paths,
5354 status_cb, status_arg, repo);
5355 if (err)
5356 goto done;
5357 if (nsubentries == 0) {
5358 /* All entries were deleted. */
5359 free(new_id);
5360 continue;
5362 memcpy(&new_te->id, new_id,
5363 sizeof(new_te->id));
5364 free(new_id);
5366 err = insert_tree_entry(new_te, &paths);
5367 if (err)
5368 goto done;
5369 (*nentries)++;
5370 continue;
5373 err = match_deleted_or_modified_ct(&ct, te,
5374 path_base_tree, commitable_paths);
5375 if (err)
5376 goto done;
5377 if (ct) {
5378 /* NB: Deleted entries get dropped here. */
5379 if (ct->status == GOT_STATUS_MODIFY ||
5380 ct->status == GOT_STATUS_MODE_CHANGE ||
5381 ct->staged_status == GOT_STATUS_MODIFY) {
5382 err = alloc_modified_blob_tree_entry(
5383 &new_te, te, ct);
5384 if (err)
5385 goto done;
5386 err = insert_tree_entry(new_te, &paths);
5387 if (err)
5388 goto done;
5389 (*nentries)++;
5391 err = report_ct_status(ct, status_cb,
5392 status_arg);
5393 if (err)
5394 goto done;
5395 } else {
5396 /* Entry is unchanged; just copy it. */
5397 err = got_object_tree_entry_dup(&new_te, te);
5398 if (err)
5399 goto done;
5400 err = insert_tree_entry(new_te, &paths);
5401 if (err)
5402 goto done;
5403 (*nentries)++;
5408 /* Write new list of entries; deleted entries have been dropped. */
5409 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5410 done:
5411 got_pathlist_free(&paths);
5412 return err;
5415 static const struct got_error *
5416 update_fileindex_after_commit(struct got_worktree *worktree,
5417 struct got_pathlist_head *commitable_paths,
5418 struct got_object_id *new_base_commit_id,
5419 struct got_fileindex *fileindex, int have_staged_files)
5421 const struct got_error *err = NULL;
5422 struct got_pathlist_entry *pe;
5423 char *relpath = NULL;
5425 TAILQ_FOREACH(pe, commitable_paths, entry) {
5426 struct got_fileindex_entry *ie;
5427 struct got_commitable *ct = pe->data;
5429 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5431 err = got_path_skip_common_ancestor(&relpath,
5432 worktree->root_path, ct->ondisk_path);
5433 if (err)
5434 goto done;
5436 if (ie) {
5437 if (ct->status == GOT_STATUS_DELETE ||
5438 ct->staged_status == GOT_STATUS_DELETE) {
5439 got_fileindex_entry_remove(fileindex, ie);
5440 } else if (ct->staged_status == GOT_STATUS_ADD ||
5441 ct->staged_status == GOT_STATUS_MODIFY) {
5442 got_fileindex_entry_stage_set(ie,
5443 GOT_FILEIDX_STAGE_NONE);
5444 got_fileindex_entry_staged_filetype_set(ie, 0);
5446 err = got_fileindex_entry_update(ie,
5447 worktree->root_fd, relpath,
5448 ct->staged_blob_id->sha1,
5449 new_base_commit_id->sha1,
5450 !have_staged_files);
5451 } else
5452 err = got_fileindex_entry_update(ie,
5453 worktree->root_fd, relpath,
5454 ct->blob_id->sha1,
5455 new_base_commit_id->sha1,
5456 !have_staged_files);
5457 } else {
5458 err = got_fileindex_entry_alloc(&ie, pe->path);
5459 if (err)
5460 goto done;
5461 err = got_fileindex_entry_update(ie,
5462 worktree->root_fd, relpath, ct->blob_id->sha1,
5463 new_base_commit_id->sha1, 1);
5464 if (err) {
5465 got_fileindex_entry_free(ie);
5466 goto done;
5468 err = got_fileindex_entry_add(fileindex, ie);
5469 if (err) {
5470 got_fileindex_entry_free(ie);
5471 goto done;
5474 free(relpath);
5475 relpath = NULL;
5477 done:
5478 free(relpath);
5479 return err;
5483 static const struct got_error *
5484 check_out_of_date(const char *in_repo_path, unsigned char status,
5485 unsigned char staged_status, struct got_object_id *base_blob_id,
5486 struct got_object_id *base_commit_id,
5487 struct got_object_id *head_commit_id, struct got_repository *repo,
5488 int ood_errcode)
5490 const struct got_error *err = NULL;
5491 struct got_object_id *id = NULL;
5493 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5494 /* Trivial case: base commit == head commit */
5495 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5496 return NULL;
5498 * Ensure file content which local changes were based
5499 * on matches file content in the branch head.
5501 err = got_object_id_by_path(&id, repo, head_commit_id,
5502 in_repo_path);
5503 if (err) {
5504 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5505 err = got_error(ood_errcode);
5506 goto done;
5507 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5508 err = got_error(ood_errcode);
5509 } else {
5510 /* Require that added files don't exist in the branch head. */
5511 err = got_object_id_by_path(&id, repo, head_commit_id,
5512 in_repo_path);
5513 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5514 goto done;
5515 err = id ? got_error(ood_errcode) : NULL;
5517 done:
5518 free(id);
5519 return err;
5522 const struct got_error *
5523 commit_worktree(struct got_object_id **new_commit_id,
5524 struct got_pathlist_head *commitable_paths,
5525 struct got_object_id *head_commit_id,
5526 struct got_object_id *parent_id2,
5527 struct got_worktree *worktree,
5528 const char *author, const char *committer,
5529 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5530 got_worktree_status_cb status_cb, void *status_arg,
5531 struct got_repository *repo)
5533 const struct got_error *err = NULL, *unlockerr = NULL;
5534 struct got_pathlist_entry *pe;
5535 const char *head_ref_name = NULL;
5536 struct got_commit_object *head_commit = NULL;
5537 struct got_reference *head_ref2 = NULL;
5538 struct got_object_id *head_commit_id2 = NULL;
5539 struct got_tree_object *head_tree = NULL;
5540 struct got_object_id *new_tree_id = NULL;
5541 int nentries, nparents = 0;
5542 struct got_object_id_queue parent_ids;
5543 struct got_object_qid *pid = NULL;
5544 char *logmsg = NULL;
5546 *new_commit_id = NULL;
5548 STAILQ_INIT(&parent_ids);
5550 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5551 if (err)
5552 goto done;
5554 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5555 if (err)
5556 goto done;
5558 if (commit_msg_cb != NULL) {
5559 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5560 if (err)
5561 goto done;
5564 if (logmsg == NULL || strlen(logmsg) == 0) {
5565 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5566 goto done;
5569 /* Create blobs from added and modified files and record their IDs. */
5570 TAILQ_FOREACH(pe, commitable_paths, entry) {
5571 struct got_commitable *ct = pe->data;
5572 char *ondisk_path;
5574 /* Blobs for staged files already exist. */
5575 if (ct->staged_status == GOT_STATUS_ADD ||
5576 ct->staged_status == GOT_STATUS_MODIFY)
5577 continue;
5579 if (ct->status != GOT_STATUS_ADD &&
5580 ct->status != GOT_STATUS_MODIFY &&
5581 ct->status != GOT_STATUS_MODE_CHANGE)
5582 continue;
5584 if (asprintf(&ondisk_path, "%s/%s",
5585 worktree->root_path, pe->path) == -1) {
5586 err = got_error_from_errno("asprintf");
5587 goto done;
5589 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5590 free(ondisk_path);
5591 if (err)
5592 goto done;
5595 /* Recursively write new tree objects. */
5596 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5597 commitable_paths, status_cb, status_arg, repo);
5598 if (err)
5599 goto done;
5601 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5602 if (err)
5603 goto done;
5604 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5605 nparents++;
5606 if (parent_id2) {
5607 err = got_object_qid_alloc(&pid, parent_id2);
5608 if (err)
5609 goto done;
5610 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5611 nparents++;
5613 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5614 nparents, author, time(NULL), committer, time(NULL), logmsg, repo);
5615 if (logmsg != NULL)
5616 free(logmsg);
5617 if (err)
5618 goto done;
5620 /* Check if a concurrent commit to our branch has occurred. */
5621 head_ref_name = got_worktree_get_head_ref_name(worktree);
5622 if (head_ref_name == NULL) {
5623 err = got_error_from_errno("got_worktree_get_head_ref_name");
5624 goto done;
5626 /* Lock the reference here to prevent concurrent modification. */
5627 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5628 if (err)
5629 goto done;
5630 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5631 if (err)
5632 goto done;
5633 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5634 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5635 goto done;
5637 /* Update branch head in repository. */
5638 err = got_ref_change_ref(head_ref2, *new_commit_id);
5639 if (err)
5640 goto done;
5641 err = got_ref_write(head_ref2, repo);
5642 if (err)
5643 goto done;
5645 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5646 if (err)
5647 goto done;
5649 err = ref_base_commit(worktree, repo);
5650 if (err)
5651 goto done;
5652 done:
5653 got_object_id_queue_free(&parent_ids);
5654 if (head_tree)
5655 got_object_tree_close(head_tree);
5656 if (head_commit)
5657 got_object_commit_close(head_commit);
5658 free(head_commit_id2);
5659 if (head_ref2) {
5660 unlockerr = got_ref_unlock(head_ref2);
5661 if (unlockerr && err == NULL)
5662 err = unlockerr;
5663 got_ref_close(head_ref2);
5665 return err;
5668 static const struct got_error *
5669 check_path_is_commitable(const char *path,
5670 struct got_pathlist_head *commitable_paths)
5672 struct got_pathlist_entry *cpe = NULL;
5673 size_t path_len = strlen(path);
5675 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5676 struct got_commitable *ct = cpe->data;
5677 const char *ct_path = ct->path;
5679 while (ct_path[0] == '/')
5680 ct_path++;
5682 if (strcmp(path, ct_path) == 0 ||
5683 got_path_is_child(ct_path, path, path_len))
5684 break;
5687 if (cpe == NULL)
5688 return got_error_path(path, GOT_ERR_BAD_PATH);
5690 return NULL;
5693 static const struct got_error *
5694 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5696 int *have_staged_files = arg;
5698 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5699 *have_staged_files = 1;
5700 return got_error(GOT_ERR_CANCELLED);
5703 return NULL;
5706 static const struct got_error *
5707 check_non_staged_files(struct got_fileindex *fileindex,
5708 struct got_pathlist_head *paths)
5710 struct got_pathlist_entry *pe;
5711 struct got_fileindex_entry *ie;
5713 TAILQ_FOREACH(pe, paths, entry) {
5714 if (pe->path[0] == '\0')
5715 continue;
5716 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5717 if (ie == NULL)
5718 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5719 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5720 return got_error_path(pe->path,
5721 GOT_ERR_FILE_NOT_STAGED);
5724 return NULL;
5727 const struct got_error *
5728 got_worktree_commit(struct got_object_id **new_commit_id,
5729 struct got_worktree *worktree, struct got_pathlist_head *paths,
5730 const char *author, const char *committer, int allow_bad_symlinks,
5731 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5732 got_worktree_status_cb status_cb, void *status_arg,
5733 struct got_repository *repo)
5735 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5736 struct got_fileindex *fileindex = NULL;
5737 char *fileindex_path = NULL;
5738 struct got_pathlist_head commitable_paths;
5739 struct collect_commitables_arg cc_arg;
5740 struct got_pathlist_entry *pe;
5741 struct got_reference *head_ref = NULL;
5742 struct got_object_id *head_commit_id = NULL;
5743 int have_staged_files = 0;
5745 *new_commit_id = NULL;
5747 TAILQ_INIT(&commitable_paths);
5749 err = lock_worktree(worktree, LOCK_EX);
5750 if (err)
5751 goto done;
5753 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5754 if (err)
5755 goto done;
5757 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5758 if (err)
5759 goto done;
5761 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5762 if (err)
5763 goto done;
5765 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5766 &have_staged_files);
5767 if (err && err->code != GOT_ERR_CANCELLED)
5768 goto done;
5769 if (have_staged_files) {
5770 err = check_non_staged_files(fileindex, paths);
5771 if (err)
5772 goto done;
5775 cc_arg.commitable_paths = &commitable_paths;
5776 cc_arg.worktree = worktree;
5777 cc_arg.fileindex = fileindex;
5778 cc_arg.repo = repo;
5779 cc_arg.have_staged_files = have_staged_files;
5780 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5781 TAILQ_FOREACH(pe, paths, entry) {
5782 err = worktree_status(worktree, pe->path, fileindex, repo,
5783 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
5784 if (err)
5785 goto done;
5788 if (TAILQ_EMPTY(&commitable_paths)) {
5789 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5790 goto done;
5793 TAILQ_FOREACH(pe, paths, entry) {
5794 err = check_path_is_commitable(pe->path, &commitable_paths);
5795 if (err)
5796 goto done;
5799 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5800 struct got_commitable *ct = pe->data;
5801 const char *ct_path = ct->in_repo_path;
5803 while (ct_path[0] == '/')
5804 ct_path++;
5805 err = check_out_of_date(ct_path, ct->status,
5806 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5807 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5808 if (err)
5809 goto done;
5813 err = commit_worktree(new_commit_id, &commitable_paths,
5814 head_commit_id, NULL, worktree, author, committer,
5815 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5816 if (err)
5817 goto done;
5819 err = update_fileindex_after_commit(worktree, &commitable_paths,
5820 *new_commit_id, fileindex, have_staged_files);
5821 sync_err = sync_fileindex(fileindex, fileindex_path);
5822 if (sync_err && err == NULL)
5823 err = sync_err;
5824 done:
5825 if (fileindex)
5826 got_fileindex_free(fileindex);
5827 free(fileindex_path);
5828 unlockerr = lock_worktree(worktree, LOCK_SH);
5829 if (unlockerr && err == NULL)
5830 err = unlockerr;
5831 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5832 struct got_commitable *ct = pe->data;
5833 free_commitable(ct);
5835 got_pathlist_free(&commitable_paths);
5836 return err;
5839 const char *
5840 got_commitable_get_path(struct got_commitable *ct)
5842 return ct->path;
5845 unsigned int
5846 got_commitable_get_status(struct got_commitable *ct)
5848 return ct->status;
5851 struct check_rebase_ok_arg {
5852 struct got_worktree *worktree;
5853 struct got_repository *repo;
5856 static const struct got_error *
5857 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5859 const struct got_error *err = NULL;
5860 struct check_rebase_ok_arg *a = arg;
5861 unsigned char status;
5862 struct stat sb;
5863 char *ondisk_path;
5865 /* Reject rebase of a work tree with mixed base commits. */
5866 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5867 SHA1_DIGEST_LENGTH))
5868 return got_error(GOT_ERR_MIXED_COMMITS);
5870 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5871 == -1)
5872 return got_error_from_errno("asprintf");
5874 /* Reject rebase of a work tree with modified or staged files. */
5875 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5876 free(ondisk_path);
5877 if (err)
5878 return err;
5880 if (status != GOT_STATUS_NO_CHANGE)
5881 return got_error(GOT_ERR_MODIFIED);
5882 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5883 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5885 return NULL;
5888 const struct got_error *
5889 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5890 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5891 struct got_worktree *worktree, struct got_reference *branch,
5892 struct got_repository *repo)
5894 const struct got_error *err = NULL;
5895 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5896 char *branch_ref_name = NULL;
5897 char *fileindex_path = NULL;
5898 struct check_rebase_ok_arg ok_arg;
5899 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5900 struct got_object_id *wt_branch_tip = NULL;
5902 *new_base_branch_ref = NULL;
5903 *tmp_branch = NULL;
5904 *fileindex = NULL;
5906 err = lock_worktree(worktree, LOCK_EX);
5907 if (err)
5908 return err;
5910 err = open_fileindex(fileindex, &fileindex_path, worktree);
5911 if (err)
5912 goto done;
5914 ok_arg.worktree = worktree;
5915 ok_arg.repo = repo;
5916 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5917 &ok_arg);
5918 if (err)
5919 goto done;
5921 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5922 if (err)
5923 goto done;
5925 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5926 if (err)
5927 goto done;
5929 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5930 if (err)
5931 goto done;
5933 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5934 0);
5935 if (err)
5936 goto done;
5938 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5939 if (err)
5940 goto done;
5941 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5942 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5943 goto done;
5946 err = got_ref_alloc_symref(new_base_branch_ref,
5947 new_base_branch_ref_name, wt_branch);
5948 if (err)
5949 goto done;
5950 err = got_ref_write(*new_base_branch_ref, repo);
5951 if (err)
5952 goto done;
5954 /* TODO Lock original branch's ref while rebasing? */
5956 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5957 if (err)
5958 goto done;
5960 err = got_ref_write(branch_ref, repo);
5961 if (err)
5962 goto done;
5964 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5965 worktree->base_commit_id);
5966 if (err)
5967 goto done;
5968 err = got_ref_write(*tmp_branch, repo);
5969 if (err)
5970 goto done;
5972 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5973 if (err)
5974 goto done;
5975 done:
5976 free(fileindex_path);
5977 free(tmp_branch_name);
5978 free(new_base_branch_ref_name);
5979 free(branch_ref_name);
5980 if (branch_ref)
5981 got_ref_close(branch_ref);
5982 if (wt_branch)
5983 got_ref_close(wt_branch);
5984 free(wt_branch_tip);
5985 if (err) {
5986 if (*new_base_branch_ref) {
5987 got_ref_close(*new_base_branch_ref);
5988 *new_base_branch_ref = NULL;
5990 if (*tmp_branch) {
5991 got_ref_close(*tmp_branch);
5992 *tmp_branch = NULL;
5994 if (*fileindex) {
5995 got_fileindex_free(*fileindex);
5996 *fileindex = NULL;
5998 lock_worktree(worktree, LOCK_SH);
6000 return err;
6003 const struct got_error *
6004 got_worktree_rebase_continue(struct got_object_id **commit_id,
6005 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6006 struct got_reference **branch, struct got_fileindex **fileindex,
6007 struct got_worktree *worktree, struct got_repository *repo)
6009 const struct got_error *err;
6010 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6011 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6012 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6013 char *fileindex_path = NULL;
6014 int have_staged_files = 0;
6016 *commit_id = NULL;
6017 *new_base_branch = NULL;
6018 *tmp_branch = NULL;
6019 *branch = NULL;
6020 *fileindex = NULL;
6022 err = lock_worktree(worktree, LOCK_EX);
6023 if (err)
6024 return err;
6026 err = open_fileindex(fileindex, &fileindex_path, worktree);
6027 if (err)
6028 goto done;
6030 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6031 &have_staged_files);
6032 if (err && err->code != GOT_ERR_CANCELLED)
6033 goto done;
6034 if (have_staged_files) {
6035 err = got_error(GOT_ERR_STAGED_PATHS);
6036 goto done;
6039 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6040 if (err)
6041 goto done;
6043 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6044 if (err)
6045 goto done;
6047 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6048 if (err)
6049 goto done;
6051 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6052 if (err)
6053 goto done;
6055 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6056 if (err)
6057 goto done;
6059 err = got_ref_open(branch, repo,
6060 got_ref_get_symref_target(branch_ref), 0);
6061 if (err)
6062 goto done;
6064 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6065 if (err)
6066 goto done;
6068 err = got_ref_resolve(commit_id, repo, commit_ref);
6069 if (err)
6070 goto done;
6072 err = got_ref_open(new_base_branch, repo,
6073 new_base_branch_ref_name, 0);
6074 if (err)
6075 goto done;
6077 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6078 if (err)
6079 goto done;
6080 done:
6081 free(commit_ref_name);
6082 free(branch_ref_name);
6083 free(fileindex_path);
6084 if (commit_ref)
6085 got_ref_close(commit_ref);
6086 if (branch_ref)
6087 got_ref_close(branch_ref);
6088 if (err) {
6089 free(*commit_id);
6090 *commit_id = NULL;
6091 if (*tmp_branch) {
6092 got_ref_close(*tmp_branch);
6093 *tmp_branch = NULL;
6095 if (*new_base_branch) {
6096 got_ref_close(*new_base_branch);
6097 *new_base_branch = NULL;
6099 if (*branch) {
6100 got_ref_close(*branch);
6101 *branch = NULL;
6103 if (*fileindex) {
6104 got_fileindex_free(*fileindex);
6105 *fileindex = NULL;
6107 lock_worktree(worktree, LOCK_SH);
6109 return err;
6112 const struct got_error *
6113 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6115 const struct got_error *err;
6116 char *tmp_branch_name = NULL;
6118 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6119 if (err)
6120 return err;
6122 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6123 free(tmp_branch_name);
6124 return NULL;
6127 static const struct got_error *
6128 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6129 char **logmsg, void *arg)
6131 *logmsg = arg;
6132 return NULL;
6135 static const struct got_error *
6136 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6137 const char *path, struct got_object_id *blob_id,
6138 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6139 int dirfd, const char *de_name)
6141 return NULL;
6144 struct collect_merged_paths_arg {
6145 got_worktree_checkout_cb progress_cb;
6146 void *progress_arg;
6147 struct got_pathlist_head *merged_paths;
6150 static const struct got_error *
6151 collect_merged_paths(void *arg, unsigned char status, const char *path)
6153 const struct got_error *err;
6154 struct collect_merged_paths_arg *a = arg;
6155 char *p;
6156 struct got_pathlist_entry *new;
6158 err = (*a->progress_cb)(a->progress_arg, status, path);
6159 if (err)
6160 return err;
6162 if (status != GOT_STATUS_MERGE &&
6163 status != GOT_STATUS_ADD &&
6164 status != GOT_STATUS_DELETE &&
6165 status != GOT_STATUS_CONFLICT)
6166 return NULL;
6168 p = strdup(path);
6169 if (p == NULL)
6170 return got_error_from_errno("strdup");
6172 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6173 if (err || new == NULL)
6174 free(p);
6175 return err;
6178 void
6179 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6181 struct got_pathlist_entry *pe;
6183 TAILQ_FOREACH(pe, merged_paths, entry)
6184 free((char *)pe->path);
6186 got_pathlist_free(merged_paths);
6189 static const struct got_error *
6190 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6191 int is_rebase, struct got_repository *repo)
6193 const struct got_error *err;
6194 struct got_reference *commit_ref = NULL;
6196 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6197 if (err) {
6198 if (err->code != GOT_ERR_NOT_REF)
6199 goto done;
6200 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6201 if (err)
6202 goto done;
6203 err = got_ref_write(commit_ref, repo);
6204 if (err)
6205 goto done;
6206 } else if (is_rebase) {
6207 struct got_object_id *stored_id;
6208 int cmp;
6210 err = got_ref_resolve(&stored_id, repo, commit_ref);
6211 if (err)
6212 goto done;
6213 cmp = got_object_id_cmp(commit_id, stored_id);
6214 free(stored_id);
6215 if (cmp != 0) {
6216 err = got_error(GOT_ERR_REBASE_COMMITID);
6217 goto done;
6220 done:
6221 if (commit_ref)
6222 got_ref_close(commit_ref);
6223 return err;
6226 static const struct got_error *
6227 rebase_merge_files(struct got_pathlist_head *merged_paths,
6228 const char *commit_ref_name, struct got_worktree *worktree,
6229 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6230 struct got_object_id *commit_id, struct got_repository *repo,
6231 got_worktree_checkout_cb progress_cb, void *progress_arg,
6232 got_cancel_cb cancel_cb, void *cancel_arg)
6234 const struct got_error *err;
6235 struct got_reference *commit_ref = NULL;
6236 struct collect_merged_paths_arg cmp_arg;
6237 char *fileindex_path;
6239 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6241 err = get_fileindex_path(&fileindex_path, worktree);
6242 if (err)
6243 return err;
6245 cmp_arg.progress_cb = progress_cb;
6246 cmp_arg.progress_arg = progress_arg;
6247 cmp_arg.merged_paths = merged_paths;
6248 err = merge_files(worktree, fileindex, fileindex_path,
6249 parent_commit_id, commit_id, repo, collect_merged_paths,
6250 &cmp_arg, cancel_cb, cancel_arg);
6251 if (commit_ref)
6252 got_ref_close(commit_ref);
6253 return err;
6256 const struct got_error *
6257 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6258 struct got_worktree *worktree, struct got_fileindex *fileindex,
6259 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6260 struct got_repository *repo,
6261 got_worktree_checkout_cb progress_cb, void *progress_arg,
6262 got_cancel_cb cancel_cb, void *cancel_arg)
6264 const struct got_error *err;
6265 char *commit_ref_name;
6267 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6268 if (err)
6269 return err;
6271 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6272 if (err)
6273 goto done;
6275 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6276 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6277 progress_arg, cancel_cb, cancel_arg);
6278 done:
6279 free(commit_ref_name);
6280 return err;
6283 const struct got_error *
6284 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6285 struct got_worktree *worktree, struct got_fileindex *fileindex,
6286 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6287 struct got_repository *repo,
6288 got_worktree_checkout_cb progress_cb, void *progress_arg,
6289 got_cancel_cb cancel_cb, void *cancel_arg)
6291 const struct got_error *err;
6292 char *commit_ref_name;
6294 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6295 if (err)
6296 return err;
6298 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6299 if (err)
6300 goto done;
6302 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6303 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6304 progress_arg, cancel_cb, cancel_arg);
6305 done:
6306 free(commit_ref_name);
6307 return err;
6310 static const struct got_error *
6311 rebase_commit(struct got_object_id **new_commit_id,
6312 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6313 struct got_worktree *worktree, struct got_fileindex *fileindex,
6314 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6315 const char *new_logmsg, struct got_repository *repo)
6317 const struct got_error *err, *sync_err;
6318 struct got_pathlist_head commitable_paths;
6319 struct collect_commitables_arg cc_arg;
6320 char *fileindex_path = NULL;
6321 struct got_reference *head_ref = NULL;
6322 struct got_object_id *head_commit_id = NULL;
6323 char *logmsg = NULL;
6325 TAILQ_INIT(&commitable_paths);
6326 *new_commit_id = NULL;
6328 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6330 err = get_fileindex_path(&fileindex_path, worktree);
6331 if (err)
6332 return err;
6334 cc_arg.commitable_paths = &commitable_paths;
6335 cc_arg.worktree = worktree;
6336 cc_arg.repo = repo;
6337 cc_arg.have_staged_files = 0;
6339 * If possible get the status of individual files directly to
6340 * avoid crawling the entire work tree once per rebased commit.
6342 * Ideally, merged_paths would contain a list of commitables
6343 * we could use so we could skip worktree_status() entirely.
6344 * However, we would then need carefully keep track of cumulative
6345 * effects of operations such as file additions and deletions
6346 * in 'got histedit -f' (folding multiple commits into one),
6347 * and this extra complexity is not really worth it.
6349 if (merged_paths) {
6350 struct got_pathlist_entry *pe;
6351 TAILQ_FOREACH(pe, merged_paths, entry) {
6352 err = worktree_status(worktree, pe->path, fileindex,
6353 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6354 0);
6355 if (err)
6356 goto done;
6358 } else {
6359 err = worktree_status(worktree, "", fileindex, repo,
6360 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6361 if (err)
6362 goto done;
6365 if (TAILQ_EMPTY(&commitable_paths)) {
6366 /* No-op change; commit will be elided. */
6367 err = got_ref_delete(commit_ref, repo);
6368 if (err)
6369 goto done;
6370 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6371 goto done;
6374 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6375 if (err)
6376 goto done;
6378 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6379 if (err)
6380 goto done;
6382 if (new_logmsg) {
6383 logmsg = strdup(new_logmsg);
6384 if (logmsg == NULL) {
6385 err = got_error_from_errno("strdup");
6386 goto done;
6388 } else {
6389 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6390 if (err)
6391 goto done;
6394 /* NB: commit_worktree will call free(logmsg) */
6395 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6396 NULL, worktree, got_object_commit_get_author(orig_commit),
6397 got_object_commit_get_committer(orig_commit),
6398 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6399 if (err)
6400 goto done;
6402 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6403 if (err)
6404 goto done;
6406 err = got_ref_delete(commit_ref, repo);
6407 if (err)
6408 goto done;
6410 err = update_fileindex_after_commit(worktree, &commitable_paths,
6411 *new_commit_id, fileindex, 0);
6412 sync_err = sync_fileindex(fileindex, fileindex_path);
6413 if (sync_err && err == NULL)
6414 err = sync_err;
6415 done:
6416 free(fileindex_path);
6417 free(head_commit_id);
6418 if (head_ref)
6419 got_ref_close(head_ref);
6420 if (err) {
6421 free(*new_commit_id);
6422 *new_commit_id = NULL;
6424 return err;
6427 const struct got_error *
6428 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6429 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6430 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6431 struct got_commit_object *orig_commit,
6432 struct got_object_id *orig_commit_id, struct got_repository *repo)
6434 const struct got_error *err;
6435 char *commit_ref_name;
6436 struct got_reference *commit_ref = NULL;
6437 struct got_object_id *commit_id = NULL;
6439 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6440 if (err)
6441 return err;
6443 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6444 if (err)
6445 goto done;
6446 err = got_ref_resolve(&commit_id, repo, commit_ref);
6447 if (err)
6448 goto done;
6449 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6450 err = got_error(GOT_ERR_REBASE_COMMITID);
6451 goto done;
6454 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6455 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6456 done:
6457 if (commit_ref)
6458 got_ref_close(commit_ref);
6459 free(commit_ref_name);
6460 free(commit_id);
6461 return err;
6464 const struct got_error *
6465 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6466 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6467 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6468 struct got_commit_object *orig_commit,
6469 struct got_object_id *orig_commit_id, const char *new_logmsg,
6470 struct got_repository *repo)
6472 const struct got_error *err;
6473 char *commit_ref_name;
6474 struct got_reference *commit_ref = NULL;
6476 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6477 if (err)
6478 return err;
6480 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6481 if (err)
6482 goto done;
6484 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6485 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6486 done:
6487 if (commit_ref)
6488 got_ref_close(commit_ref);
6489 free(commit_ref_name);
6490 return err;
6493 const struct got_error *
6494 got_worktree_rebase_postpone(struct got_worktree *worktree,
6495 struct got_fileindex *fileindex)
6497 if (fileindex)
6498 got_fileindex_free(fileindex);
6499 return lock_worktree(worktree, LOCK_SH);
6502 static const struct got_error *
6503 delete_ref(const char *name, struct got_repository *repo)
6505 const struct got_error *err;
6506 struct got_reference *ref;
6508 err = got_ref_open(&ref, repo, name, 0);
6509 if (err) {
6510 if (err->code == GOT_ERR_NOT_REF)
6511 return NULL;
6512 return err;
6515 err = got_ref_delete(ref, repo);
6516 got_ref_close(ref);
6517 return err;
6520 static const struct got_error *
6521 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6523 const struct got_error *err;
6524 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6525 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6527 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6528 if (err)
6529 goto done;
6530 err = delete_ref(tmp_branch_name, repo);
6531 if (err)
6532 goto done;
6534 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6535 if (err)
6536 goto done;
6537 err = delete_ref(new_base_branch_ref_name, repo);
6538 if (err)
6539 goto done;
6541 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6542 if (err)
6543 goto done;
6544 err = delete_ref(branch_ref_name, repo);
6545 if (err)
6546 goto done;
6548 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6549 if (err)
6550 goto done;
6551 err = delete_ref(commit_ref_name, repo);
6552 if (err)
6553 goto done;
6555 done:
6556 free(tmp_branch_name);
6557 free(new_base_branch_ref_name);
6558 free(branch_ref_name);
6559 free(commit_ref_name);
6560 return err;
6563 const struct got_error *
6564 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6565 struct got_object_id *new_commit_id, struct got_repository *repo)
6567 const struct got_error *err;
6568 struct got_reference *ref = NULL;
6569 struct got_object_id *old_commit_id = NULL;
6570 const char *branch_name = NULL;
6571 char *new_id_str = NULL;
6572 char *refname = NULL;
6574 branch_name = got_ref_get_name(branch);
6575 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6576 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6577 branch_name += 11;
6579 err = got_object_id_str(&new_id_str, new_commit_id);
6580 if (err)
6581 return err;
6583 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6584 new_id_str) == -1) {
6585 err = got_error_from_errno("asprintf");
6586 goto done;
6589 err = got_ref_resolve(&old_commit_id, repo, branch);
6590 if (err)
6591 goto done;
6593 err = got_ref_alloc(&ref, refname, old_commit_id);
6594 if (err)
6595 goto done;
6597 err = got_ref_write(ref, repo);
6598 done:
6599 free(new_id_str);
6600 free(refname);
6601 free(old_commit_id);
6602 if (ref)
6603 got_ref_close(ref);
6604 return err;
6607 const struct got_error *
6608 got_worktree_rebase_complete(struct got_worktree *worktree,
6609 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6610 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6611 struct got_repository *repo, int create_backup)
6613 const struct got_error *err, *unlockerr, *sync_err;
6614 struct got_object_id *new_head_commit_id = NULL;
6615 char *fileindex_path = NULL;
6617 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6618 if (err)
6619 return err;
6621 if (create_backup) {
6622 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6623 rebased_branch, new_head_commit_id, repo);
6624 if (err)
6625 goto done;
6628 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6629 if (err)
6630 goto done;
6632 err = got_ref_write(rebased_branch, repo);
6633 if (err)
6634 goto done;
6636 err = got_worktree_set_head_ref(worktree, rebased_branch);
6637 if (err)
6638 goto done;
6640 err = delete_rebase_refs(worktree, repo);
6641 if (err)
6642 goto done;
6644 err = get_fileindex_path(&fileindex_path, worktree);
6645 if (err)
6646 goto done;
6647 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6648 sync_err = sync_fileindex(fileindex, fileindex_path);
6649 if (sync_err && err == NULL)
6650 err = sync_err;
6651 done:
6652 got_fileindex_free(fileindex);
6653 free(fileindex_path);
6654 free(new_head_commit_id);
6655 unlockerr = lock_worktree(worktree, LOCK_SH);
6656 if (unlockerr && err == NULL)
6657 err = unlockerr;
6658 return err;
6661 const struct got_error *
6662 got_worktree_rebase_abort(struct got_worktree *worktree,
6663 struct got_fileindex *fileindex, struct got_repository *repo,
6664 struct got_reference *new_base_branch,
6665 got_worktree_checkout_cb progress_cb, void *progress_arg)
6667 const struct got_error *err, *unlockerr, *sync_err;
6668 struct got_reference *resolved = NULL;
6669 struct got_object_id *commit_id = NULL;
6670 char *fileindex_path = NULL;
6671 struct revert_file_args rfa;
6672 struct got_object_id *tree_id = NULL;
6674 err = lock_worktree(worktree, LOCK_EX);
6675 if (err)
6676 return err;
6678 err = got_ref_open(&resolved, repo,
6679 got_ref_get_symref_target(new_base_branch), 0);
6680 if (err)
6681 goto done;
6683 err = got_worktree_set_head_ref(worktree, resolved);
6684 if (err)
6685 goto done;
6688 * XXX commits to the base branch could have happened while
6689 * we were busy rebasing; should we store the original commit ID
6690 * when rebase begins and read it back here?
6692 err = got_ref_resolve(&commit_id, repo, resolved);
6693 if (err)
6694 goto done;
6696 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6697 if (err)
6698 goto done;
6700 err = got_object_id_by_path(&tree_id, repo,
6701 worktree->base_commit_id, worktree->path_prefix);
6702 if (err)
6703 goto done;
6705 err = delete_rebase_refs(worktree, repo);
6706 if (err)
6707 goto done;
6709 err = get_fileindex_path(&fileindex_path, worktree);
6710 if (err)
6711 goto done;
6713 rfa.worktree = worktree;
6714 rfa.fileindex = fileindex;
6715 rfa.progress_cb = progress_cb;
6716 rfa.progress_arg = progress_arg;
6717 rfa.patch_cb = NULL;
6718 rfa.patch_arg = NULL;
6719 rfa.repo = repo;
6720 rfa.unlink_added_files = 0;
6721 err = worktree_status(worktree, "", fileindex, repo,
6722 revert_file, &rfa, NULL, NULL, 1, 0);
6723 if (err)
6724 goto sync;
6726 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6727 repo, progress_cb, progress_arg, NULL, NULL);
6728 sync:
6729 sync_err = sync_fileindex(fileindex, fileindex_path);
6730 if (sync_err && err == NULL)
6731 err = sync_err;
6732 done:
6733 got_ref_close(resolved);
6734 free(tree_id);
6735 free(commit_id);
6736 if (fileindex)
6737 got_fileindex_free(fileindex);
6738 free(fileindex_path);
6740 unlockerr = lock_worktree(worktree, LOCK_SH);
6741 if (unlockerr && err == NULL)
6742 err = unlockerr;
6743 return err;
6746 const struct got_error *
6747 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6748 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6749 struct got_fileindex **fileindex, struct got_worktree *worktree,
6750 struct got_repository *repo)
6752 const struct got_error *err = NULL;
6753 char *tmp_branch_name = NULL;
6754 char *branch_ref_name = NULL;
6755 char *base_commit_ref_name = NULL;
6756 char *fileindex_path = NULL;
6757 struct check_rebase_ok_arg ok_arg;
6758 struct got_reference *wt_branch = NULL;
6759 struct got_reference *base_commit_ref = NULL;
6761 *tmp_branch = NULL;
6762 *branch_ref = NULL;
6763 *base_commit_id = NULL;
6764 *fileindex = NULL;
6766 err = lock_worktree(worktree, LOCK_EX);
6767 if (err)
6768 return err;
6770 err = open_fileindex(fileindex, &fileindex_path, worktree);
6771 if (err)
6772 goto done;
6774 ok_arg.worktree = worktree;
6775 ok_arg.repo = repo;
6776 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6777 &ok_arg);
6778 if (err)
6779 goto done;
6781 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6782 if (err)
6783 goto done;
6785 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6786 if (err)
6787 goto done;
6789 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6790 worktree);
6791 if (err)
6792 goto done;
6794 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6795 0);
6796 if (err)
6797 goto done;
6799 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6800 if (err)
6801 goto done;
6803 err = got_ref_write(*branch_ref, repo);
6804 if (err)
6805 goto done;
6807 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6808 worktree->base_commit_id);
6809 if (err)
6810 goto done;
6811 err = got_ref_write(base_commit_ref, repo);
6812 if (err)
6813 goto done;
6814 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6815 if (*base_commit_id == NULL) {
6816 err = got_error_from_errno("got_object_id_dup");
6817 goto done;
6820 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6821 worktree->base_commit_id);
6822 if (err)
6823 goto done;
6824 err = got_ref_write(*tmp_branch, repo);
6825 if (err)
6826 goto done;
6828 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6829 if (err)
6830 goto done;
6831 done:
6832 free(fileindex_path);
6833 free(tmp_branch_name);
6834 free(branch_ref_name);
6835 free(base_commit_ref_name);
6836 if (wt_branch)
6837 got_ref_close(wt_branch);
6838 if (err) {
6839 if (*branch_ref) {
6840 got_ref_close(*branch_ref);
6841 *branch_ref = NULL;
6843 if (*tmp_branch) {
6844 got_ref_close(*tmp_branch);
6845 *tmp_branch = NULL;
6847 free(*base_commit_id);
6848 if (*fileindex) {
6849 got_fileindex_free(*fileindex);
6850 *fileindex = NULL;
6852 lock_worktree(worktree, LOCK_SH);
6854 return err;
6857 const struct got_error *
6858 got_worktree_histedit_postpone(struct got_worktree *worktree,
6859 struct got_fileindex *fileindex)
6861 if (fileindex)
6862 got_fileindex_free(fileindex);
6863 return lock_worktree(worktree, LOCK_SH);
6866 const struct got_error *
6867 got_worktree_histedit_in_progress(int *in_progress,
6868 struct got_worktree *worktree)
6870 const struct got_error *err;
6871 char *tmp_branch_name = NULL;
6873 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6874 if (err)
6875 return err;
6877 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6878 free(tmp_branch_name);
6879 return NULL;
6882 const struct got_error *
6883 got_worktree_histedit_continue(struct got_object_id **commit_id,
6884 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6885 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6886 struct got_worktree *worktree, struct got_repository *repo)
6888 const struct got_error *err;
6889 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6890 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6891 struct got_reference *commit_ref = NULL;
6892 struct got_reference *base_commit_ref = NULL;
6893 char *fileindex_path = NULL;
6894 int have_staged_files = 0;
6896 *commit_id = NULL;
6897 *tmp_branch = NULL;
6898 *base_commit_id = NULL;
6899 *fileindex = NULL;
6901 err = lock_worktree(worktree, LOCK_EX);
6902 if (err)
6903 return err;
6905 err = open_fileindex(fileindex, &fileindex_path, worktree);
6906 if (err)
6907 goto done;
6909 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6910 &have_staged_files);
6911 if (err && err->code != GOT_ERR_CANCELLED)
6912 goto done;
6913 if (have_staged_files) {
6914 err = got_error(GOT_ERR_STAGED_PATHS);
6915 goto done;
6918 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6919 if (err)
6920 goto done;
6922 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6923 if (err)
6924 goto done;
6926 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6927 if (err)
6928 goto done;
6930 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6931 worktree);
6932 if (err)
6933 goto done;
6935 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6936 if (err)
6937 goto done;
6939 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6940 if (err)
6941 goto done;
6942 err = got_ref_resolve(commit_id, repo, commit_ref);
6943 if (err)
6944 goto done;
6946 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6947 if (err)
6948 goto done;
6949 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6950 if (err)
6951 goto done;
6953 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6954 if (err)
6955 goto done;
6956 done:
6957 free(commit_ref_name);
6958 free(branch_ref_name);
6959 free(fileindex_path);
6960 if (commit_ref)
6961 got_ref_close(commit_ref);
6962 if (base_commit_ref)
6963 got_ref_close(base_commit_ref);
6964 if (err) {
6965 free(*commit_id);
6966 *commit_id = NULL;
6967 free(*base_commit_id);
6968 *base_commit_id = NULL;
6969 if (*tmp_branch) {
6970 got_ref_close(*tmp_branch);
6971 *tmp_branch = NULL;
6973 if (*fileindex) {
6974 got_fileindex_free(*fileindex);
6975 *fileindex = NULL;
6977 lock_worktree(worktree, LOCK_EX);
6979 return err;
6982 static const struct got_error *
6983 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6985 const struct got_error *err;
6986 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6987 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6989 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6990 if (err)
6991 goto done;
6992 err = delete_ref(tmp_branch_name, repo);
6993 if (err)
6994 goto done;
6996 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6997 worktree);
6998 if (err)
6999 goto done;
7000 err = delete_ref(base_commit_ref_name, repo);
7001 if (err)
7002 goto done;
7004 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7005 if (err)
7006 goto done;
7007 err = delete_ref(branch_ref_name, repo);
7008 if (err)
7009 goto done;
7011 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7012 if (err)
7013 goto done;
7014 err = delete_ref(commit_ref_name, repo);
7015 if (err)
7016 goto done;
7017 done:
7018 free(tmp_branch_name);
7019 free(base_commit_ref_name);
7020 free(branch_ref_name);
7021 free(commit_ref_name);
7022 return err;
7025 const struct got_error *
7026 got_worktree_histedit_abort(struct got_worktree *worktree,
7027 struct got_fileindex *fileindex, struct got_repository *repo,
7028 struct got_reference *branch, struct got_object_id *base_commit_id,
7029 got_worktree_checkout_cb progress_cb, void *progress_arg)
7031 const struct got_error *err, *unlockerr, *sync_err;
7032 struct got_reference *resolved = NULL;
7033 char *fileindex_path = NULL;
7034 struct got_object_id *tree_id = NULL;
7035 struct revert_file_args rfa;
7037 err = lock_worktree(worktree, LOCK_EX);
7038 if (err)
7039 return err;
7041 err = got_ref_open(&resolved, repo,
7042 got_ref_get_symref_target(branch), 0);
7043 if (err)
7044 goto done;
7046 err = got_worktree_set_head_ref(worktree, resolved);
7047 if (err)
7048 goto done;
7050 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7051 if (err)
7052 goto done;
7054 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
7055 worktree->path_prefix);
7056 if (err)
7057 goto done;
7059 err = delete_histedit_refs(worktree, repo);
7060 if (err)
7061 goto done;
7063 err = get_fileindex_path(&fileindex_path, worktree);
7064 if (err)
7065 goto done;
7067 rfa.worktree = worktree;
7068 rfa.fileindex = fileindex;
7069 rfa.progress_cb = progress_cb;
7070 rfa.progress_arg = progress_arg;
7071 rfa.patch_cb = NULL;
7072 rfa.patch_arg = NULL;
7073 rfa.repo = repo;
7074 rfa.unlink_added_files = 0;
7075 err = worktree_status(worktree, "", fileindex, repo,
7076 revert_file, &rfa, NULL, NULL, 1, 0);
7077 if (err)
7078 goto sync;
7080 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7081 repo, progress_cb, progress_arg, NULL, NULL);
7082 sync:
7083 sync_err = sync_fileindex(fileindex, fileindex_path);
7084 if (sync_err && err == NULL)
7085 err = sync_err;
7086 done:
7087 got_ref_close(resolved);
7088 free(tree_id);
7089 free(fileindex_path);
7091 unlockerr = lock_worktree(worktree, LOCK_SH);
7092 if (unlockerr && err == NULL)
7093 err = unlockerr;
7094 return err;
7097 const struct got_error *
7098 got_worktree_histedit_complete(struct got_worktree *worktree,
7099 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7100 struct got_reference *edited_branch, struct got_repository *repo)
7102 const struct got_error *err, *unlockerr, *sync_err;
7103 struct got_object_id *new_head_commit_id = NULL;
7104 struct got_reference *resolved = NULL;
7105 char *fileindex_path = NULL;
7107 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7108 if (err)
7109 return err;
7111 err = got_ref_open(&resolved, repo,
7112 got_ref_get_symref_target(edited_branch), 0);
7113 if (err)
7114 goto done;
7116 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7117 resolved, new_head_commit_id, repo);
7118 if (err)
7119 goto done;
7121 err = got_ref_change_ref(resolved, new_head_commit_id);
7122 if (err)
7123 goto done;
7125 err = got_ref_write(resolved, repo);
7126 if (err)
7127 goto done;
7129 err = got_worktree_set_head_ref(worktree, resolved);
7130 if (err)
7131 goto done;
7133 err = delete_histedit_refs(worktree, repo);
7134 if (err)
7135 goto done;
7137 err = get_fileindex_path(&fileindex_path, worktree);
7138 if (err)
7139 goto done;
7140 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7141 sync_err = sync_fileindex(fileindex, fileindex_path);
7142 if (sync_err && err == NULL)
7143 err = sync_err;
7144 done:
7145 got_fileindex_free(fileindex);
7146 free(fileindex_path);
7147 free(new_head_commit_id);
7148 unlockerr = lock_worktree(worktree, LOCK_SH);
7149 if (unlockerr && err == NULL)
7150 err = unlockerr;
7151 return err;
7154 const struct got_error *
7155 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7156 struct got_object_id *commit_id, struct got_repository *repo)
7158 const struct got_error *err;
7159 char *commit_ref_name;
7161 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7162 if (err)
7163 return err;
7165 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7166 if (err)
7167 goto done;
7169 err = delete_ref(commit_ref_name, repo);
7170 done:
7171 free(commit_ref_name);
7172 return err;
7175 const struct got_error *
7176 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7177 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7178 struct got_worktree *worktree, const char *refname,
7179 struct got_repository *repo)
7181 const struct got_error *err = NULL;
7182 char *fileindex_path = NULL;
7183 struct check_rebase_ok_arg ok_arg;
7185 *fileindex = NULL;
7186 *branch_ref = NULL;
7187 *base_branch_ref = NULL;
7189 err = lock_worktree(worktree, LOCK_EX);
7190 if (err)
7191 return err;
7193 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7194 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7195 "cannot integrate a branch into itself; "
7196 "update -b or different branch name required");
7197 goto done;
7200 err = open_fileindex(fileindex, &fileindex_path, worktree);
7201 if (err)
7202 goto done;
7204 /* Preconditions are the same as for rebase. */
7205 ok_arg.worktree = worktree;
7206 ok_arg.repo = repo;
7207 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7208 &ok_arg);
7209 if (err)
7210 goto done;
7212 err = got_ref_open(branch_ref, repo, refname, 1);
7213 if (err)
7214 goto done;
7216 err = got_ref_open(base_branch_ref, repo,
7217 got_worktree_get_head_ref_name(worktree), 1);
7218 done:
7219 if (err) {
7220 if (*branch_ref) {
7221 got_ref_close(*branch_ref);
7222 *branch_ref = NULL;
7224 if (*base_branch_ref) {
7225 got_ref_close(*base_branch_ref);
7226 *base_branch_ref = NULL;
7228 if (*fileindex) {
7229 got_fileindex_free(*fileindex);
7230 *fileindex = NULL;
7232 lock_worktree(worktree, LOCK_SH);
7234 return err;
7237 const struct got_error *
7238 got_worktree_integrate_continue(struct got_worktree *worktree,
7239 struct got_fileindex *fileindex, struct got_repository *repo,
7240 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7241 got_worktree_checkout_cb progress_cb, void *progress_arg,
7242 got_cancel_cb cancel_cb, void *cancel_arg)
7244 const struct got_error *err = NULL, *sync_err, *unlockerr;
7245 char *fileindex_path = NULL;
7246 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7248 err = get_fileindex_path(&fileindex_path, worktree);
7249 if (err)
7250 goto done;
7252 err = got_ref_resolve(&commit_id, repo, branch_ref);
7253 if (err)
7254 goto done;
7256 err = got_object_id_by_path(&tree_id, repo, commit_id,
7257 worktree->path_prefix);
7258 if (err)
7259 goto done;
7261 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7262 if (err)
7263 goto done;
7265 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7266 progress_cb, progress_arg, cancel_cb, cancel_arg);
7267 if (err)
7268 goto sync;
7270 err = got_ref_change_ref(base_branch_ref, commit_id);
7271 if (err)
7272 goto sync;
7274 err = got_ref_write(base_branch_ref, repo);
7275 if (err)
7276 goto sync;
7278 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7279 sync:
7280 sync_err = sync_fileindex(fileindex, fileindex_path);
7281 if (sync_err && err == NULL)
7282 err = sync_err;
7284 done:
7285 unlockerr = got_ref_unlock(branch_ref);
7286 if (unlockerr && err == NULL)
7287 err = unlockerr;
7288 got_ref_close(branch_ref);
7290 unlockerr = got_ref_unlock(base_branch_ref);
7291 if (unlockerr && err == NULL)
7292 err = unlockerr;
7293 got_ref_close(base_branch_ref);
7295 got_fileindex_free(fileindex);
7296 free(fileindex_path);
7297 free(tree_id);
7299 unlockerr = lock_worktree(worktree, LOCK_SH);
7300 if (unlockerr && err == NULL)
7301 err = unlockerr;
7302 return err;
7305 const struct got_error *
7306 got_worktree_integrate_abort(struct got_worktree *worktree,
7307 struct got_fileindex *fileindex, struct got_repository *repo,
7308 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7310 const struct got_error *err = NULL, *unlockerr = NULL;
7312 got_fileindex_free(fileindex);
7314 err = lock_worktree(worktree, LOCK_SH);
7316 unlockerr = got_ref_unlock(branch_ref);
7317 if (unlockerr && err == NULL)
7318 err = unlockerr;
7319 got_ref_close(branch_ref);
7321 unlockerr = got_ref_unlock(base_branch_ref);
7322 if (unlockerr && err == NULL)
7323 err = unlockerr;
7324 got_ref_close(base_branch_ref);
7326 return err;
7329 const struct got_error *
7330 got_worktree_merge_postpone(struct got_worktree *worktree,
7331 struct got_fileindex *fileindex)
7333 const struct got_error *err, *sync_err;
7334 char *fileindex_path = NULL;
7336 err = get_fileindex_path(&fileindex_path, worktree);
7337 if (err)
7338 goto done;
7340 sync_err = sync_fileindex(fileindex, fileindex_path);
7342 err = lock_worktree(worktree, LOCK_SH);
7343 if (sync_err && err == NULL)
7344 err = sync_err;
7345 done:
7346 got_fileindex_free(fileindex);
7347 free(fileindex_path);
7348 return err;
7351 static const struct got_error *
7352 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7354 const struct got_error *err;
7355 char *branch_refname = NULL, *commit_refname = NULL;
7357 err = get_merge_branch_ref_name(&branch_refname, worktree);
7358 if (err)
7359 goto done;
7360 err = delete_ref(branch_refname, repo);
7361 if (err)
7362 goto done;
7364 err = get_merge_commit_ref_name(&commit_refname, worktree);
7365 if (err)
7366 goto done;
7367 err = delete_ref(commit_refname, repo);
7368 if (err)
7369 goto done;
7371 done:
7372 free(branch_refname);
7373 free(commit_refname);
7374 return err;
7377 struct merge_commit_msg_arg {
7378 struct got_worktree *worktree;
7379 const char *branch_name;
7382 static const struct got_error *
7383 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths, char **logmsg,
7384 void *arg)
7386 struct merge_commit_msg_arg *a = arg;
7388 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7389 got_worktree_get_head_ref_name(a->worktree)) == -1)
7390 return got_error_from_errno("asprintf");
7392 return NULL;
7396 const struct got_error *
7397 got_worktree_merge_branch(struct got_worktree *worktree,
7398 struct got_fileindex *fileindex,
7399 struct got_object_id *yca_commit_id,
7400 struct got_object_id *branch_tip,
7401 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7402 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7404 const struct got_error *err;
7405 char *fileindex_path = NULL;
7407 err = get_fileindex_path(&fileindex_path, worktree);
7408 if (err)
7409 goto done;
7411 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7412 worktree);
7413 if (err)
7414 goto done;
7416 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7417 branch_tip, repo, progress_cb, progress_arg,
7418 cancel_cb, cancel_arg);
7419 done:
7420 free(fileindex_path);
7421 return err;
7424 const struct got_error *
7425 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7426 struct got_worktree *worktree, struct got_fileindex *fileindex,
7427 const char *author, const char *committer, int allow_bad_symlinks,
7428 struct got_object_id *branch_tip, const char *branch_name,
7429 struct got_repository *repo,
7430 got_worktree_status_cb status_cb, void *status_arg)
7433 const struct got_error *err = NULL, *sync_err;
7434 struct got_pathlist_head commitable_paths;
7435 struct collect_commitables_arg cc_arg;
7436 struct got_pathlist_entry *pe;
7437 struct got_reference *head_ref = NULL;
7438 struct got_object_id *head_commit_id = NULL;
7439 int have_staged_files = 0;
7440 struct merge_commit_msg_arg mcm_arg;
7441 char *fileindex_path = NULL;
7443 *new_commit_id = NULL;
7445 TAILQ_INIT(&commitable_paths);
7447 err = get_fileindex_path(&fileindex_path, worktree);
7448 if (err)
7449 goto done;
7451 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7452 if (err)
7453 goto done;
7455 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7456 if (err)
7457 goto done;
7459 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7460 &have_staged_files);
7461 if (err && err->code != GOT_ERR_CANCELLED)
7462 goto done;
7463 if (have_staged_files) {
7464 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7465 goto done;
7468 cc_arg.commitable_paths = &commitable_paths;
7469 cc_arg.worktree = worktree;
7470 cc_arg.fileindex = fileindex;
7471 cc_arg.repo = repo;
7472 cc_arg.have_staged_files = have_staged_files;
7473 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7474 err = worktree_status(worktree, "", fileindex, repo,
7475 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7476 if (err)
7477 goto done;
7479 if (TAILQ_EMPTY(&commitable_paths)) {
7480 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7481 "merge of %s cannot proceed", branch_name);
7482 goto done;
7485 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7486 struct got_commitable *ct = pe->data;
7487 const char *ct_path = ct->in_repo_path;
7489 while (ct_path[0] == '/')
7490 ct_path++;
7491 err = check_out_of_date(ct_path, ct->status,
7492 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7493 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7494 if (err)
7495 goto done;
7499 mcm_arg.worktree = worktree;
7500 mcm_arg.branch_name = branch_name;
7501 err = commit_worktree(new_commit_id, &commitable_paths,
7502 head_commit_id, branch_tip, worktree, author, committer,
7503 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7504 if (err)
7505 goto done;
7507 err = update_fileindex_after_commit(worktree, &commitable_paths,
7508 *new_commit_id, fileindex, have_staged_files);
7509 sync_err = sync_fileindex(fileindex, fileindex_path);
7510 if (sync_err && err == NULL)
7511 err = sync_err;
7512 done:
7513 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7514 struct got_commitable *ct = pe->data;
7515 free_commitable(ct);
7517 got_pathlist_free(&commitable_paths);
7518 free(fileindex_path);
7519 return err;
7522 const struct got_error *
7523 got_worktree_merge_complete(struct got_worktree *worktree,
7524 struct got_fileindex *fileindex, struct got_repository *repo)
7526 const struct got_error *err, *unlockerr, *sync_err;
7527 char *fileindex_path = NULL;
7529 err = delete_merge_refs(worktree, repo);
7530 if (err)
7531 goto done;
7533 err = get_fileindex_path(&fileindex_path, worktree);
7534 if (err)
7535 goto done;
7536 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7537 sync_err = sync_fileindex(fileindex, fileindex_path);
7538 if (sync_err && err == NULL)
7539 err = sync_err;
7540 done:
7541 got_fileindex_free(fileindex);
7542 free(fileindex_path);
7543 unlockerr = lock_worktree(worktree, LOCK_SH);
7544 if (unlockerr && err == NULL)
7545 err = unlockerr;
7546 return err;
7549 const struct got_error *
7550 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7551 struct got_repository *repo)
7553 const struct got_error *err;
7554 char *branch_refname = NULL;
7555 struct got_reference *branch_ref = NULL;
7557 *in_progress = 0;
7559 err = get_merge_branch_ref_name(&branch_refname, worktree);
7560 if (err)
7561 return err;
7562 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7563 free(branch_refname);
7564 if (err) {
7565 if (err->code != GOT_ERR_NOT_REF)
7566 return err;
7567 } else
7568 *in_progress = 1;
7570 return NULL;
7573 const struct got_error *got_worktree_merge_prepare(
7574 struct got_fileindex **fileindex, struct got_worktree *worktree,
7575 struct got_reference *branch, struct got_repository *repo)
7577 const struct got_error *err = NULL;
7578 char *fileindex_path = NULL;
7579 char *branch_refname = NULL, *commit_refname = NULL;
7580 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
7581 struct got_reference *commit_ref = NULL;
7582 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
7583 struct check_rebase_ok_arg ok_arg;
7585 *fileindex = NULL;
7587 err = lock_worktree(worktree, LOCK_EX);
7588 if (err)
7589 return err;
7591 err = open_fileindex(fileindex, &fileindex_path, worktree);
7592 if (err)
7593 goto done;
7595 /* Preconditions are the same as for rebase. */
7596 ok_arg.worktree = worktree;
7597 ok_arg.repo = repo;
7598 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7599 &ok_arg);
7600 if (err)
7601 goto done;
7603 err = get_merge_branch_ref_name(&branch_refname, worktree);
7604 if (err)
7605 return err;
7607 err = get_merge_commit_ref_name(&commit_refname, worktree);
7608 if (err)
7609 return err;
7611 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7612 0);
7613 if (err)
7614 goto done;
7616 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
7617 if (err)
7618 goto done;
7620 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
7621 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
7622 goto done;
7625 err = got_ref_resolve(&branch_tip, repo, branch);
7626 if (err)
7627 goto done;
7629 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
7630 if (err)
7631 goto done;
7632 err = got_ref_write(branch_ref, repo);
7633 if (err)
7634 goto done;
7636 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
7637 if (err)
7638 goto done;
7639 err = got_ref_write(commit_ref, repo);
7640 if (err)
7641 goto done;
7643 done:
7644 free(branch_refname);
7645 free(commit_refname);
7646 free(fileindex_path);
7647 if (branch_ref)
7648 got_ref_close(branch_ref);
7649 if (commit_ref)
7650 got_ref_close(commit_ref);
7651 if (wt_branch)
7652 got_ref_close(wt_branch);
7653 free(wt_branch_tip);
7654 if (err) {
7655 if (*fileindex) {
7656 got_fileindex_free(*fileindex);
7657 *fileindex = NULL;
7659 lock_worktree(worktree, LOCK_SH);
7661 return err;
7664 const struct got_error *
7665 got_worktree_merge_continue(char **branch_name,
7666 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
7667 struct got_worktree *worktree, struct got_repository *repo)
7669 const struct got_error *err;
7670 char *commit_refname = NULL, *branch_refname = NULL;
7671 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
7672 char *fileindex_path = NULL;
7673 int have_staged_files = 0;
7675 *branch_name = NULL;
7676 *branch_tip = NULL;
7677 *fileindex = NULL;
7679 err = lock_worktree(worktree, LOCK_EX);
7680 if (err)
7681 return err;
7683 err = open_fileindex(fileindex, &fileindex_path, worktree);
7684 if (err)
7685 goto done;
7687 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7688 &have_staged_files);
7689 if (err && err->code != GOT_ERR_CANCELLED)
7690 goto done;
7691 if (have_staged_files) {
7692 err = got_error(GOT_ERR_STAGED_PATHS);
7693 goto done;
7696 err = get_merge_branch_ref_name(&branch_refname, worktree);
7697 if (err)
7698 goto done;
7700 err = get_merge_commit_ref_name(&commit_refname, worktree);
7701 if (err)
7702 goto done;
7704 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7705 if (err)
7706 goto done;
7708 if (!got_ref_is_symbolic(branch_ref)) {
7709 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7710 "%s is not a symbolic reference",
7711 got_ref_get_name(branch_ref));
7712 goto done;
7714 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
7715 if (*branch_name == NULL) {
7716 err = got_error_from_errno("strdup");
7717 goto done;
7720 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
7721 if (err)
7722 goto done;
7724 err = got_ref_resolve(branch_tip, repo, commit_ref);
7725 if (err)
7726 goto done;
7727 done:
7728 free(commit_refname);
7729 free(branch_refname);
7730 free(fileindex_path);
7731 if (commit_ref)
7732 got_ref_close(commit_ref);
7733 if (branch_ref)
7734 got_ref_close(branch_ref);
7735 if (err) {
7736 if (*branch_name) {
7737 free(*branch_name);
7738 *branch_name = NULL;
7740 free(*branch_tip);
7741 *branch_tip = NULL;
7742 if (*fileindex) {
7743 got_fileindex_free(*fileindex);
7744 *fileindex = NULL;
7746 lock_worktree(worktree, LOCK_SH);
7748 return err;
7751 const struct got_error *
7752 got_worktree_merge_abort(struct got_worktree *worktree,
7753 struct got_fileindex *fileindex, struct got_repository *repo,
7754 got_worktree_checkout_cb progress_cb, void *progress_arg)
7756 const struct got_error *err, *unlockerr, *sync_err;
7757 struct got_object_id *commit_id = NULL;
7758 char *fileindex_path = NULL;
7759 struct revert_file_args rfa;
7760 struct got_object_id *tree_id = NULL;
7762 err = got_object_id_by_path(&tree_id, repo,
7763 worktree->base_commit_id, worktree->path_prefix);
7764 if (err)
7765 goto done;
7767 err = delete_merge_refs(worktree, repo);
7768 if (err)
7769 goto done;
7771 err = get_fileindex_path(&fileindex_path, worktree);
7772 if (err)
7773 goto done;
7775 rfa.worktree = worktree;
7776 rfa.fileindex = fileindex;
7777 rfa.progress_cb = progress_cb;
7778 rfa.progress_arg = progress_arg;
7779 rfa.patch_cb = NULL;
7780 rfa.patch_arg = NULL;
7781 rfa.repo = repo;
7782 rfa.unlink_added_files = 1;
7783 err = worktree_status(worktree, "", fileindex, repo,
7784 revert_file, &rfa, NULL, NULL, 1, 0);
7785 if (err)
7786 goto sync;
7788 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7789 repo, progress_cb, progress_arg, NULL, NULL);
7790 sync:
7791 sync_err = sync_fileindex(fileindex, fileindex_path);
7792 if (sync_err && err == NULL)
7793 err = sync_err;
7794 done:
7795 free(tree_id);
7796 free(commit_id);
7797 if (fileindex)
7798 got_fileindex_free(fileindex);
7799 free(fileindex_path);
7801 unlockerr = lock_worktree(worktree, LOCK_SH);
7802 if (unlockerr && err == NULL)
7803 err = unlockerr;
7804 return err;
7807 struct check_stage_ok_arg {
7808 struct got_object_id *head_commit_id;
7809 struct got_worktree *worktree;
7810 struct got_fileindex *fileindex;
7811 struct got_repository *repo;
7812 int have_changes;
7815 const struct got_error *
7816 check_stage_ok(void *arg, unsigned char status,
7817 unsigned char staged_status, const char *relpath,
7818 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7819 struct got_object_id *commit_id, int dirfd, const char *de_name)
7821 struct check_stage_ok_arg *a = arg;
7822 const struct got_error *err = NULL;
7823 struct got_fileindex_entry *ie;
7824 struct got_object_id base_commit_id;
7825 struct got_object_id *base_commit_idp = NULL;
7826 char *in_repo_path = NULL, *p;
7828 if (status == GOT_STATUS_UNVERSIONED ||
7829 status == GOT_STATUS_NO_CHANGE)
7830 return NULL;
7831 if (status == GOT_STATUS_NONEXISTENT)
7832 return got_error_set_errno(ENOENT, relpath);
7834 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7835 if (ie == NULL)
7836 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7838 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7839 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7840 relpath) == -1)
7841 return got_error_from_errno("asprintf");
7843 if (got_fileindex_entry_has_commit(ie)) {
7844 memcpy(base_commit_id.sha1, ie->commit_sha1,
7845 SHA1_DIGEST_LENGTH);
7846 base_commit_idp = &base_commit_id;
7849 if (status == GOT_STATUS_CONFLICT) {
7850 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7851 goto done;
7852 } else if (status != GOT_STATUS_ADD &&
7853 status != GOT_STATUS_MODIFY &&
7854 status != GOT_STATUS_DELETE) {
7855 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7856 goto done;
7859 a->have_changes = 1;
7861 p = in_repo_path;
7862 while (p[0] == '/')
7863 p++;
7864 err = check_out_of_date(p, status, staged_status,
7865 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7866 GOT_ERR_STAGE_OUT_OF_DATE);
7867 done:
7868 free(in_repo_path);
7869 return err;
7872 struct stage_path_arg {
7873 struct got_worktree *worktree;
7874 struct got_fileindex *fileindex;
7875 struct got_repository *repo;
7876 got_worktree_status_cb status_cb;
7877 void *status_arg;
7878 got_worktree_patch_cb patch_cb;
7879 void *patch_arg;
7880 int staged_something;
7881 int allow_bad_symlinks;
7884 static const struct got_error *
7885 stage_path(void *arg, unsigned char status,
7886 unsigned char staged_status, const char *relpath,
7887 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7888 struct got_object_id *commit_id, int dirfd, const char *de_name)
7890 struct stage_path_arg *a = arg;
7891 const struct got_error *err = NULL;
7892 struct got_fileindex_entry *ie;
7893 char *ondisk_path = NULL, *path_content = NULL;
7894 uint32_t stage;
7895 struct got_object_id *new_staged_blob_id = NULL;
7896 struct stat sb;
7898 if (status == GOT_STATUS_UNVERSIONED)
7899 return NULL;
7901 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7902 if (ie == NULL)
7903 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7905 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7906 relpath)== -1)
7907 return got_error_from_errno("asprintf");
7909 switch (status) {
7910 case GOT_STATUS_ADD:
7911 case GOT_STATUS_MODIFY:
7912 /* XXX could sb.st_mode be passed in by our caller? */
7913 if (lstat(ondisk_path, &sb) == -1) {
7914 err = got_error_from_errno2("lstat", ondisk_path);
7915 break;
7917 if (a->patch_cb) {
7918 if (status == GOT_STATUS_ADD) {
7919 int choice = GOT_PATCH_CHOICE_NONE;
7920 err = (*a->patch_cb)(&choice, a->patch_arg,
7921 status, ie->path, NULL, 1, 1);
7922 if (err)
7923 break;
7924 if (choice != GOT_PATCH_CHOICE_YES)
7925 break;
7926 } else {
7927 err = create_patched_content(&path_content, 0,
7928 staged_blob_id ? staged_blob_id : blob_id,
7929 ondisk_path, dirfd, de_name, ie->path,
7930 a->repo, a->patch_cb, a->patch_arg);
7931 if (err || path_content == NULL)
7932 break;
7935 err = got_object_blob_create(&new_staged_blob_id,
7936 path_content ? path_content : ondisk_path, a->repo);
7937 if (err)
7938 break;
7939 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7940 SHA1_DIGEST_LENGTH);
7941 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7942 stage = GOT_FILEIDX_STAGE_ADD;
7943 else
7944 stage = GOT_FILEIDX_STAGE_MODIFY;
7945 got_fileindex_entry_stage_set(ie, stage);
7946 if (S_ISLNK(sb.st_mode)) {
7947 int is_bad_symlink = 0;
7948 if (!a->allow_bad_symlinks) {
7949 char target_path[PATH_MAX];
7950 ssize_t target_len;
7951 target_len = readlink(ondisk_path, target_path,
7952 sizeof(target_path));
7953 if (target_len == -1) {
7954 err = got_error_from_errno2("readlink",
7955 ondisk_path);
7956 break;
7958 err = is_bad_symlink_target(&is_bad_symlink,
7959 target_path, target_len, ondisk_path,
7960 a->worktree->root_path);
7961 if (err)
7962 break;
7963 if (is_bad_symlink) {
7964 err = got_error_path(ondisk_path,
7965 GOT_ERR_BAD_SYMLINK);
7966 break;
7969 if (is_bad_symlink)
7970 got_fileindex_entry_staged_filetype_set(ie,
7971 GOT_FILEIDX_MODE_BAD_SYMLINK);
7972 else
7973 got_fileindex_entry_staged_filetype_set(ie,
7974 GOT_FILEIDX_MODE_SYMLINK);
7975 } else {
7976 got_fileindex_entry_staged_filetype_set(ie,
7977 GOT_FILEIDX_MODE_REGULAR_FILE);
7979 a->staged_something = 1;
7980 if (a->status_cb == NULL)
7981 break;
7982 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7983 get_staged_status(ie), relpath, blob_id,
7984 new_staged_blob_id, NULL, dirfd, de_name);
7985 break;
7986 case GOT_STATUS_DELETE:
7987 if (staged_status == GOT_STATUS_DELETE)
7988 break;
7989 if (a->patch_cb) {
7990 int choice = GOT_PATCH_CHOICE_NONE;
7991 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7992 ie->path, NULL, 1, 1);
7993 if (err)
7994 break;
7995 if (choice == GOT_PATCH_CHOICE_NO)
7996 break;
7997 if (choice != GOT_PATCH_CHOICE_YES) {
7998 err = got_error(GOT_ERR_PATCH_CHOICE);
7999 break;
8002 stage = GOT_FILEIDX_STAGE_DELETE;
8003 got_fileindex_entry_stage_set(ie, stage);
8004 a->staged_something = 1;
8005 if (a->status_cb == NULL)
8006 break;
8007 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8008 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8009 de_name);
8010 break;
8011 case GOT_STATUS_NO_CHANGE:
8012 break;
8013 case GOT_STATUS_CONFLICT:
8014 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8015 break;
8016 case GOT_STATUS_NONEXISTENT:
8017 err = got_error_set_errno(ENOENT, relpath);
8018 break;
8019 default:
8020 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8021 break;
8024 if (path_content && unlink(path_content) == -1 && err == NULL)
8025 err = got_error_from_errno2("unlink", path_content);
8026 free(path_content);
8027 free(ondisk_path);
8028 free(new_staged_blob_id);
8029 return err;
8032 const struct got_error *
8033 got_worktree_stage(struct got_worktree *worktree,
8034 struct got_pathlist_head *paths,
8035 got_worktree_status_cb status_cb, void *status_arg,
8036 got_worktree_patch_cb patch_cb, void *patch_arg,
8037 int allow_bad_symlinks, struct got_repository *repo)
8039 const struct got_error *err = NULL, *sync_err, *unlockerr;
8040 struct got_pathlist_entry *pe;
8041 struct got_fileindex *fileindex = NULL;
8042 char *fileindex_path = NULL;
8043 struct got_reference *head_ref = NULL;
8044 struct got_object_id *head_commit_id = NULL;
8045 struct check_stage_ok_arg oka;
8046 struct stage_path_arg spa;
8048 err = lock_worktree(worktree, LOCK_EX);
8049 if (err)
8050 return err;
8052 err = got_ref_open(&head_ref, repo,
8053 got_worktree_get_head_ref_name(worktree), 0);
8054 if (err)
8055 goto done;
8056 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8057 if (err)
8058 goto done;
8059 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8060 if (err)
8061 goto done;
8063 /* Check pre-conditions before staging anything. */
8064 oka.head_commit_id = head_commit_id;
8065 oka.worktree = worktree;
8066 oka.fileindex = fileindex;
8067 oka.repo = repo;
8068 oka.have_changes = 0;
8069 TAILQ_FOREACH(pe, paths, entry) {
8070 err = worktree_status(worktree, pe->path, fileindex, repo,
8071 check_stage_ok, &oka, NULL, NULL, 1, 0);
8072 if (err)
8073 goto done;
8075 if (!oka.have_changes) {
8076 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8077 goto done;
8080 spa.worktree = worktree;
8081 spa.fileindex = fileindex;
8082 spa.repo = repo;
8083 spa.patch_cb = patch_cb;
8084 spa.patch_arg = patch_arg;
8085 spa.status_cb = status_cb;
8086 spa.status_arg = status_arg;
8087 spa.staged_something = 0;
8088 spa.allow_bad_symlinks = allow_bad_symlinks;
8089 TAILQ_FOREACH(pe, paths, entry) {
8090 err = worktree_status(worktree, pe->path, fileindex, repo,
8091 stage_path, &spa, NULL, NULL, 1, 0);
8092 if (err)
8093 goto done;
8095 if (!spa.staged_something) {
8096 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8097 goto done;
8100 sync_err = sync_fileindex(fileindex, fileindex_path);
8101 if (sync_err && err == NULL)
8102 err = sync_err;
8103 done:
8104 if (head_ref)
8105 got_ref_close(head_ref);
8106 free(head_commit_id);
8107 free(fileindex_path);
8108 if (fileindex)
8109 got_fileindex_free(fileindex);
8110 unlockerr = lock_worktree(worktree, LOCK_SH);
8111 if (unlockerr && err == NULL)
8112 err = unlockerr;
8113 return err;
8116 struct unstage_path_arg {
8117 struct got_worktree *worktree;
8118 struct got_fileindex *fileindex;
8119 struct got_repository *repo;
8120 got_worktree_checkout_cb progress_cb;
8121 void *progress_arg;
8122 got_worktree_patch_cb patch_cb;
8123 void *patch_arg;
8126 static const struct got_error *
8127 create_unstaged_content(char **path_unstaged_content,
8128 char **path_new_staged_content, struct got_object_id *blob_id,
8129 struct got_object_id *staged_blob_id, const char *relpath,
8130 struct got_repository *repo,
8131 got_worktree_patch_cb patch_cb, void *patch_arg)
8133 const struct got_error *err, *free_err;
8134 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8135 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8136 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8137 struct got_diffreg_result *diffreg_result = NULL;
8138 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8139 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8141 *path_unstaged_content = NULL;
8142 *path_new_staged_content = NULL;
8144 err = got_object_id_str(&label1, blob_id);
8145 if (err)
8146 return err;
8147 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
8148 if (err)
8149 goto done;
8151 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
8152 if (err)
8153 goto done;
8155 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8156 if (err)
8157 goto done;
8159 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
8160 if (err)
8161 goto done;
8163 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
8164 if (err)
8165 goto done;
8167 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8168 if (err)
8169 goto done;
8171 err = got_diff_files(&diffreg_result, f1, label1, f2,
8172 path2, 3, 0, 1, NULL);
8173 if (err)
8174 goto done;
8176 err = got_opentemp_named(path_unstaged_content, &outfile,
8177 "got-unstaged-content");
8178 if (err)
8179 goto done;
8180 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8181 "got-new-staged-content");
8182 if (err)
8183 goto done;
8185 if (fseek(f1, 0L, SEEK_SET) == -1) {
8186 err = got_ferror(f1, GOT_ERR_IO);
8187 goto done;
8189 if (fseek(f2, 0L, SEEK_SET) == -1) {
8190 err = got_ferror(f2, GOT_ERR_IO);
8191 goto done;
8193 /* Count the number of actual changes in the diff result. */
8194 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8195 struct diff_chunk_context cc = {};
8196 diff_chunk_context_load_change(&cc, &nchunks_used,
8197 diffreg_result->result, n, 0);
8198 nchanges++;
8200 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8201 int choice;
8202 err = apply_or_reject_change(&choice, &nchunks_used,
8203 diffreg_result->result, n, relpath, f1, f2,
8204 &line_cur1, &line_cur2,
8205 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8206 if (err)
8207 goto done;
8208 if (choice == GOT_PATCH_CHOICE_YES)
8209 have_content = 1;
8210 else
8211 have_rejected_content = 1;
8212 if (choice == GOT_PATCH_CHOICE_QUIT)
8213 break;
8215 if (have_content || have_rejected_content)
8216 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8217 outfile, rejectfile);
8218 done:
8219 free(label1);
8220 if (blob)
8221 got_object_blob_close(blob);
8222 if (staged_blob)
8223 got_object_blob_close(staged_blob);
8224 free_err = got_diffreg_result_free(diffreg_result);
8225 if (free_err && err == NULL)
8226 err = free_err;
8227 if (f1 && fclose(f1) == EOF && err == NULL)
8228 err = got_error_from_errno2("fclose", path1);
8229 if (f2 && fclose(f2) == EOF && err == NULL)
8230 err = got_error_from_errno2("fclose", path2);
8231 if (outfile && fclose(outfile) == EOF && err == NULL)
8232 err = got_error_from_errno2("fclose", *path_unstaged_content);
8233 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8234 err = got_error_from_errno2("fclose", *path_new_staged_content);
8235 if (path1 && unlink(path1) == -1 && err == NULL)
8236 err = got_error_from_errno2("unlink", path1);
8237 if (path2 && unlink(path2) == -1 && err == NULL)
8238 err = got_error_from_errno2("unlink", path2);
8239 if (err || !have_content) {
8240 if (*path_unstaged_content &&
8241 unlink(*path_unstaged_content) == -1 && err == NULL)
8242 err = got_error_from_errno2("unlink",
8243 *path_unstaged_content);
8244 free(*path_unstaged_content);
8245 *path_unstaged_content = NULL;
8247 if (err || !have_content || !have_rejected_content) {
8248 if (*path_new_staged_content &&
8249 unlink(*path_new_staged_content) == -1 && err == NULL)
8250 err = got_error_from_errno2("unlink",
8251 *path_new_staged_content);
8252 free(*path_new_staged_content);
8253 *path_new_staged_content = NULL;
8255 free(path1);
8256 free(path2);
8257 return err;
8260 static const struct got_error *
8261 unstage_hunks(struct got_object_id *staged_blob_id,
8262 struct got_blob_object *blob_base,
8263 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8264 const char *ondisk_path, const char *label_orig,
8265 struct got_worktree *worktree, struct got_repository *repo,
8266 got_worktree_patch_cb patch_cb, void *patch_arg,
8267 got_worktree_checkout_cb progress_cb, void *progress_arg)
8269 const struct got_error *err = NULL;
8270 char *path_unstaged_content = NULL;
8271 char *path_new_staged_content = NULL;
8272 char *parent = NULL, *base_path = NULL;
8273 char *blob_base_path = NULL;
8274 struct got_object_id *new_staged_blob_id = NULL;
8275 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8276 struct stat sb;
8278 err = create_unstaged_content(&path_unstaged_content,
8279 &path_new_staged_content, blob_id, staged_blob_id,
8280 ie->path, repo, patch_cb, patch_arg);
8281 if (err)
8282 return err;
8284 if (path_unstaged_content == NULL)
8285 return NULL;
8287 if (path_new_staged_content) {
8288 err = got_object_blob_create(&new_staged_blob_id,
8289 path_new_staged_content, repo);
8290 if (err)
8291 goto done;
8294 f = fopen(path_unstaged_content, "re");
8295 if (f == NULL) {
8296 err = got_error_from_errno2("fopen",
8297 path_unstaged_content);
8298 goto done;
8300 if (fstat(fileno(f), &sb) == -1) {
8301 err = got_error_from_errno2("fstat", path_unstaged_content);
8302 goto done;
8304 if (got_fileindex_entry_staged_filetype_get(ie) ==
8305 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8306 char link_target[PATH_MAX];
8307 size_t r;
8308 r = fread(link_target, 1, sizeof(link_target), f);
8309 if (r == 0 && ferror(f)) {
8310 err = got_error_from_errno("fread");
8311 goto done;
8313 if (r >= sizeof(link_target)) { /* should not happen */
8314 err = got_error(GOT_ERR_NO_SPACE);
8315 goto done;
8317 link_target[r] = '\0';
8318 err = merge_symlink(worktree, blob_base,
8319 ondisk_path, ie->path, label_orig, link_target,
8320 worktree->base_commit_id, repo, progress_cb,
8321 progress_arg);
8322 } else {
8323 int local_changes_subsumed;
8325 err = got_path_dirname(&parent, ondisk_path);
8326 if (err)
8327 return err;
8329 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8330 parent) == -1) {
8331 err = got_error_from_errno("asprintf");
8332 base_path = NULL;
8333 goto done;
8336 err = got_opentemp_named(&blob_base_path, &f_base, base_path);
8337 if (err)
8338 goto done;
8339 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8340 blob_base);
8341 if (err)
8342 goto done;
8345 * In order the run a 3-way merge with a symlink we copy the symlink's
8346 * target path into a temporary file and use that file with diff3.
8348 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8349 err = dump_symlink_target_path_to_file(&f_deriv2,
8350 ondisk_path);
8351 if (err)
8352 goto done;
8353 } else {
8354 int fd;
8355 fd = open(ondisk_path,
8356 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8357 if (fd == -1) {
8358 err = got_error_from_errno2("open", ondisk_path);
8359 goto done;
8361 f_deriv2 = fdopen(fd, "r");
8362 if (f_deriv2 == NULL) {
8363 err = got_error_from_errno2("fdopen", ondisk_path);
8364 close(fd);
8365 goto done;
8369 err = merge_file(&local_changes_subsumed, worktree,
8370 f_base, f, f_deriv2, ondisk_path, ie->path,
8371 got_fileindex_perms_to_st(ie),
8372 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8373 repo, progress_cb, progress_arg);
8375 if (err)
8376 goto done;
8378 if (new_staged_blob_id) {
8379 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8380 SHA1_DIGEST_LENGTH);
8381 } else {
8382 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8383 got_fileindex_entry_staged_filetype_set(ie, 0);
8385 done:
8386 free(new_staged_blob_id);
8387 if (path_unstaged_content &&
8388 unlink(path_unstaged_content) == -1 && err == NULL)
8389 err = got_error_from_errno2("unlink", path_unstaged_content);
8390 if (path_new_staged_content &&
8391 unlink(path_new_staged_content) == -1 && err == NULL)
8392 err = got_error_from_errno2("unlink", path_new_staged_content);
8393 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8394 err = got_error_from_errno2("unlink", blob_base_path);
8395 if (f_base && fclose(f_base) == EOF && err == NULL)
8396 err = got_error_from_errno2("fclose", path_unstaged_content);
8397 if (f && fclose(f) == EOF && err == NULL)
8398 err = got_error_from_errno2("fclose", path_unstaged_content);
8399 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8400 err = got_error_from_errno2("fclose", ondisk_path);
8401 free(path_unstaged_content);
8402 free(path_new_staged_content);
8403 free(blob_base_path);
8404 free(parent);
8405 free(base_path);
8406 return err;
8409 static const struct got_error *
8410 unstage_path(void *arg, unsigned char status,
8411 unsigned char staged_status, const char *relpath,
8412 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8413 struct got_object_id *commit_id, int dirfd, const char *de_name)
8415 const struct got_error *err = NULL;
8416 struct unstage_path_arg *a = arg;
8417 struct got_fileindex_entry *ie;
8418 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8419 char *ondisk_path = NULL;
8420 char *id_str = NULL, *label_orig = NULL;
8421 int local_changes_subsumed;
8422 struct stat sb;
8424 if (staged_status != GOT_STATUS_ADD &&
8425 staged_status != GOT_STATUS_MODIFY &&
8426 staged_status != GOT_STATUS_DELETE)
8427 return NULL;
8429 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8430 if (ie == NULL)
8431 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8433 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8434 == -1)
8435 return got_error_from_errno("asprintf");
8437 err = got_object_id_str(&id_str,
8438 commit_id ? commit_id : a->worktree->base_commit_id);
8439 if (err)
8440 goto done;
8441 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8442 id_str) == -1) {
8443 err = got_error_from_errno("asprintf");
8444 goto done;
8447 switch (staged_status) {
8448 case GOT_STATUS_MODIFY:
8449 err = got_object_open_as_blob(&blob_base, a->repo,
8450 blob_id, 8192);
8451 if (err)
8452 break;
8453 /* fall through */
8454 case GOT_STATUS_ADD:
8455 if (a->patch_cb) {
8456 if (staged_status == GOT_STATUS_ADD) {
8457 int choice = GOT_PATCH_CHOICE_NONE;
8458 err = (*a->patch_cb)(&choice, a->patch_arg,
8459 staged_status, ie->path, NULL, 1, 1);
8460 if (err)
8461 break;
8462 if (choice != GOT_PATCH_CHOICE_YES)
8463 break;
8464 } else {
8465 err = unstage_hunks(staged_blob_id,
8466 blob_base, blob_id, ie, ondisk_path,
8467 label_orig, a->worktree, a->repo,
8468 a->patch_cb, a->patch_arg,
8469 a->progress_cb, a->progress_arg);
8470 break; /* Done with this file. */
8473 err = got_object_open_as_blob(&blob_staged, a->repo,
8474 staged_blob_id, 8192);
8475 if (err)
8476 break;
8477 switch (got_fileindex_entry_staged_filetype_get(ie)) {
8478 case GOT_FILEIDX_MODE_BAD_SYMLINK:
8479 case GOT_FILEIDX_MODE_REGULAR_FILE:
8480 err = merge_blob(&local_changes_subsumed, a->worktree,
8481 blob_base, ondisk_path, relpath,
8482 got_fileindex_perms_to_st(ie), label_orig,
8483 blob_staged, commit_id ? commit_id :
8484 a->worktree->base_commit_id, a->repo,
8485 a->progress_cb, a->progress_arg);
8486 break;
8487 case GOT_FILEIDX_MODE_SYMLINK:
8488 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8489 char *staged_target;
8490 err = got_object_blob_read_to_str(
8491 &staged_target, blob_staged);
8492 if (err)
8493 goto done;
8494 err = merge_symlink(a->worktree, blob_base,
8495 ondisk_path, relpath, label_orig,
8496 staged_target, commit_id ? commit_id :
8497 a->worktree->base_commit_id,
8498 a->repo, a->progress_cb, a->progress_arg);
8499 free(staged_target);
8500 } else {
8501 err = merge_blob(&local_changes_subsumed,
8502 a->worktree, blob_base, ondisk_path,
8503 relpath, got_fileindex_perms_to_st(ie),
8504 label_orig, blob_staged,
8505 commit_id ? commit_id :
8506 a->worktree->base_commit_id, a->repo,
8507 a->progress_cb, a->progress_arg);
8509 break;
8510 default:
8511 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8512 break;
8514 if (err == NULL) {
8515 got_fileindex_entry_stage_set(ie,
8516 GOT_FILEIDX_STAGE_NONE);
8517 got_fileindex_entry_staged_filetype_set(ie, 0);
8519 break;
8520 case GOT_STATUS_DELETE:
8521 if (a->patch_cb) {
8522 int choice = GOT_PATCH_CHOICE_NONE;
8523 err = (*a->patch_cb)(&choice, a->patch_arg,
8524 staged_status, ie->path, NULL, 1, 1);
8525 if (err)
8526 break;
8527 if (choice == GOT_PATCH_CHOICE_NO)
8528 break;
8529 if (choice != GOT_PATCH_CHOICE_YES) {
8530 err = got_error(GOT_ERR_PATCH_CHOICE);
8531 break;
8534 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8535 got_fileindex_entry_staged_filetype_set(ie, 0);
8536 err = get_file_status(&status, &sb, ie, ondisk_path,
8537 dirfd, de_name, a->repo);
8538 if (err)
8539 break;
8540 err = (*a->progress_cb)(a->progress_arg, status, relpath);
8541 break;
8543 done:
8544 free(ondisk_path);
8545 if (blob_base)
8546 got_object_blob_close(blob_base);
8547 if (blob_staged)
8548 got_object_blob_close(blob_staged);
8549 free(id_str);
8550 free(label_orig);
8551 return err;
8554 const struct got_error *
8555 got_worktree_unstage(struct got_worktree *worktree,
8556 struct got_pathlist_head *paths,
8557 got_worktree_checkout_cb progress_cb, void *progress_arg,
8558 got_worktree_patch_cb patch_cb, void *patch_arg,
8559 struct got_repository *repo)
8561 const struct got_error *err = NULL, *sync_err, *unlockerr;
8562 struct got_pathlist_entry *pe;
8563 struct got_fileindex *fileindex = NULL;
8564 char *fileindex_path = NULL;
8565 struct unstage_path_arg upa;
8567 err = lock_worktree(worktree, LOCK_EX);
8568 if (err)
8569 return err;
8571 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8572 if (err)
8573 goto done;
8575 upa.worktree = worktree;
8576 upa.fileindex = fileindex;
8577 upa.repo = repo;
8578 upa.progress_cb = progress_cb;
8579 upa.progress_arg = progress_arg;
8580 upa.patch_cb = patch_cb;
8581 upa.patch_arg = patch_arg;
8582 TAILQ_FOREACH(pe, paths, entry) {
8583 err = worktree_status(worktree, pe->path, fileindex, repo,
8584 unstage_path, &upa, NULL, NULL, 1, 0);
8585 if (err)
8586 goto done;
8589 sync_err = sync_fileindex(fileindex, fileindex_path);
8590 if (sync_err && err == NULL)
8591 err = sync_err;
8592 done:
8593 free(fileindex_path);
8594 if (fileindex)
8595 got_fileindex_free(fileindex);
8596 unlockerr = lock_worktree(worktree, LOCK_SH);
8597 if (unlockerr && err == NULL)
8598 err = unlockerr;
8599 return err;
8602 struct report_file_info_arg {
8603 struct got_worktree *worktree;
8604 got_worktree_path_info_cb info_cb;
8605 void *info_arg;
8606 struct got_pathlist_head *paths;
8607 got_cancel_cb cancel_cb;
8608 void *cancel_arg;
8611 static const struct got_error *
8612 report_file_info(void *arg, struct got_fileindex_entry *ie)
8614 struct report_file_info_arg *a = arg;
8615 struct got_pathlist_entry *pe;
8616 struct got_object_id blob_id, staged_blob_id, commit_id;
8617 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8618 struct got_object_id *commit_idp = NULL;
8619 int stage;
8621 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8622 return got_error(GOT_ERR_CANCELLED);
8624 TAILQ_FOREACH(pe, a->paths, entry) {
8625 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8626 got_path_is_child(ie->path, pe->path, pe->path_len))
8627 break;
8629 if (pe == NULL) /* not found */
8630 return NULL;
8632 if (got_fileindex_entry_has_blob(ie)) {
8633 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8634 blob_idp = &blob_id;
8636 stage = got_fileindex_entry_stage_get(ie);
8637 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8638 stage == GOT_FILEIDX_STAGE_ADD) {
8639 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8640 SHA1_DIGEST_LENGTH);
8641 staged_blob_idp = &staged_blob_id;
8644 if (got_fileindex_entry_has_commit(ie)) {
8645 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8646 commit_idp = &commit_id;
8649 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8650 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8653 const struct got_error *
8654 got_worktree_path_info(struct got_worktree *worktree,
8655 struct got_pathlist_head *paths,
8656 got_worktree_path_info_cb info_cb, void *info_arg,
8657 got_cancel_cb cancel_cb, void *cancel_arg)
8660 const struct got_error *err = NULL, *unlockerr;
8661 struct got_fileindex *fileindex = NULL;
8662 char *fileindex_path = NULL;
8663 struct report_file_info_arg arg;
8665 err = lock_worktree(worktree, LOCK_SH);
8666 if (err)
8667 return err;
8669 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8670 if (err)
8671 goto done;
8673 arg.worktree = worktree;
8674 arg.info_cb = info_cb;
8675 arg.info_arg = info_arg;
8676 arg.paths = paths;
8677 arg.cancel_cb = cancel_cb;
8678 arg.cancel_arg = cancel_arg;
8679 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8680 &arg);
8681 done:
8682 free(fileindex_path);
8683 if (fileindex)
8684 got_fileindex_free(fileindex);
8685 unlockerr = lock_worktree(worktree, LOCK_UN);
8686 if (unlockerr && err == NULL)
8687 err = unlockerr;
8688 return err;