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, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
2820 if (fd == -1) {
2821 err = got_error_from_errno2("open",
2822 ondisk_path);
2823 goto done;
2825 f_deriv = fdopen(fd, "r");
2826 if (f_deriv == NULL) {
2827 err = got_error_from_errno2("fdopen",
2828 ondisk_path);
2829 close(fd);
2830 goto done;
2832 err = got_object_id_str(&id_str, a->commit_id2);
2833 if (err)
2834 goto done;
2835 if (asprintf(&label_deriv2, "%s: commit %s",
2836 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2837 err = got_error_from_errno("asprintf");
2838 goto done;
2840 err = merge_file(&local_changes_subsumed, a->worktree,
2841 f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2842 sb.st_mode, a->label_orig, NULL, label_deriv2,
2843 GOT_DIFF_ALGORITHM_PATIENCE, repo,
2844 a->progress_cb, a->progress_arg);
2846 } else if (blob1) {
2847 ie = got_fileindex_entry_get(a->fileindex, path1,
2848 strlen(path1));
2849 if (ie == NULL)
2850 return (*a->progress_cb)(a->progress_arg,
2851 GOT_STATUS_MISSING, path1);
2853 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2854 path1) == -1)
2855 return got_error_from_errno("asprintf");
2857 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2858 repo);
2859 if (err)
2860 goto done;
2862 switch (status) {
2863 case GOT_STATUS_NO_CHANGE:
2864 err = (*a->progress_cb)(a->progress_arg,
2865 GOT_STATUS_DELETE, path1);
2866 if (err)
2867 goto done;
2868 err = remove_ondisk_file(a->worktree->root_path, path1);
2869 if (err)
2870 goto done;
2871 if (ie)
2872 got_fileindex_entry_mark_deleted_from_disk(ie);
2873 break;
2874 case GOT_STATUS_DELETE:
2875 case GOT_STATUS_MISSING:
2876 err = (*a->progress_cb)(a->progress_arg,
2877 GOT_STATUS_DELETE, path1);
2878 if (err)
2879 goto done;
2880 if (ie)
2881 got_fileindex_entry_mark_deleted_from_disk(ie);
2882 break;
2883 case GOT_STATUS_ADD: {
2884 struct got_object_id *id;
2885 FILE *blob1_f;
2887 * Delete the added file only if its content already
2888 * exists in the repository.
2890 err = got_object_blob_file_create(&id, &blob1_f, path1);
2891 if (err)
2892 goto done;
2893 if (got_object_id_cmp(id, id1) == 0) {
2894 err = (*a->progress_cb)(a->progress_arg,
2895 GOT_STATUS_DELETE, path1);
2896 if (err)
2897 goto done;
2898 err = remove_ondisk_file(a->worktree->root_path,
2899 path1);
2900 if (err)
2901 goto done;
2902 if (ie)
2903 got_fileindex_entry_remove(a->fileindex,
2904 ie);
2905 } else {
2906 err = (*a->progress_cb)(a->progress_arg,
2907 GOT_STATUS_CANNOT_DELETE, path1);
2909 if (fclose(blob1_f) == EOF && err == NULL)
2910 err = got_error_from_errno("fclose");
2911 free(id);
2912 if (err)
2913 goto done;
2914 break;
2916 case GOT_STATUS_MODIFY:
2917 case GOT_STATUS_CONFLICT:
2918 err = (*a->progress_cb)(a->progress_arg,
2919 GOT_STATUS_CANNOT_DELETE, path1);
2920 if (err)
2921 goto done;
2922 break;
2923 case GOT_STATUS_OBSTRUCTED:
2924 err = (*a->progress_cb)(a->progress_arg, status, path1);
2925 if (err)
2926 goto done;
2927 break;
2928 default:
2929 break;
2931 } else if (blob2) {
2932 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2933 path2) == -1)
2934 return got_error_from_errno("asprintf");
2935 ie = got_fileindex_entry_get(a->fileindex, path2,
2936 strlen(path2));
2937 if (ie) {
2938 err = get_file_status(&status, &sb, ie, ondisk_path,
2939 -1, NULL, repo);
2940 if (err)
2941 goto done;
2942 if (status != GOT_STATUS_NO_CHANGE &&
2943 status != GOT_STATUS_MODIFY &&
2944 status != GOT_STATUS_CONFLICT &&
2945 status != GOT_STATUS_ADD) {
2946 err = (*a->progress_cb)(a->progress_arg,
2947 status, path2);
2948 goto done;
2950 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2951 char *link_target2;
2952 err = got_object_blob_read_to_str(&link_target2,
2953 blob2);
2954 if (err)
2955 goto done;
2956 err = merge_symlink(a->worktree, NULL,
2957 ondisk_path, path2, a->label_orig,
2958 link_target2, a->commit_id2, repo,
2959 a->progress_cb, a->progress_arg);
2960 free(link_target2);
2961 } else if (S_ISREG(sb.st_mode)) {
2962 err = merge_blob(&local_changes_subsumed,
2963 a->worktree, NULL, ondisk_path, path2,
2964 sb.st_mode, a->label_orig, blob2,
2965 a->commit_id2, repo, a->progress_cb,
2966 a->progress_arg);
2967 } else {
2968 err = got_error_path(ondisk_path,
2969 GOT_ERR_FILE_OBSTRUCTED);
2971 if (err)
2972 goto done;
2973 if (status == GOT_STATUS_DELETE) {
2974 err = got_fileindex_entry_update(ie,
2975 a->worktree->root_fd, path2, blob2->id.sha1,
2976 a->worktree->base_commit_id->sha1, 0);
2977 if (err)
2978 goto done;
2980 } else {
2981 int is_bad_symlink = 0;
2982 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2983 if (S_ISLNK(mode2)) {
2984 err = install_symlink(&is_bad_symlink,
2985 a->worktree, ondisk_path, path2, blob2, 0,
2986 0, 1, a->allow_bad_symlinks, repo,
2987 a->progress_cb, a->progress_arg);
2988 } else {
2989 err = install_blob(a->worktree, ondisk_path, path2,
2990 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2991 a->progress_cb, a->progress_arg);
2993 if (err)
2994 goto done;
2995 err = got_fileindex_entry_alloc(&ie, path2);
2996 if (err)
2997 goto done;
2998 err = got_fileindex_entry_update(ie,
2999 a->worktree->root_fd, path2, NULL, NULL, 1);
3000 if (err) {
3001 got_fileindex_entry_free(ie);
3002 goto done;
3004 err = got_fileindex_entry_add(a->fileindex, ie);
3005 if (err) {
3006 got_fileindex_entry_free(ie);
3007 goto done;
3009 if (is_bad_symlink) {
3010 got_fileindex_entry_filetype_set(ie,
3011 GOT_FILEIDX_MODE_BAD_SYMLINK);
3015 done:
3016 if (f_orig && fclose(f_orig) == EOF && err == NULL)
3017 err = got_error_from_errno("fclose");
3018 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3019 err = got_error_from_errno("fclose");
3020 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3021 err = got_error_from_errno("fclose");
3022 free(id_str);
3023 free(label_deriv2);
3024 free(ondisk_path);
3025 return err;
3028 static const struct got_error *
3029 check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3031 struct got_worktree *worktree = arg;
3033 /* Reject merges into a work tree with mixed base commits. */
3034 if (got_fileindex_entry_has_commit(ie) &&
3035 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3036 SHA1_DIGEST_LENGTH) != 0)
3037 return got_error(GOT_ERR_MIXED_COMMITS);
3039 return NULL;
3042 struct check_merge_conflicts_arg {
3043 struct got_worktree *worktree;
3044 struct got_fileindex *fileindex;
3045 struct got_repository *repo;
3048 static const struct got_error *
3049 check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3050 struct got_blob_object *blob2, struct got_object_id *id1,
3051 struct got_object_id *id2, const char *path1, const char *path2,
3052 mode_t mode1, mode_t mode2, struct got_repository *repo)
3054 const struct got_error *err = NULL;
3055 struct check_merge_conflicts_arg *a = arg;
3056 unsigned char status;
3057 struct stat sb;
3058 struct got_fileindex_entry *ie;
3059 const char *path = path2 ? path2 : path1;
3060 struct got_object_id *id = id2 ? id2 : id1;
3061 char *ondisk_path;
3063 if (id == NULL)
3064 return NULL;
3066 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3067 if (ie == NULL)
3068 return NULL;
3070 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3071 == -1)
3072 return got_error_from_errno("asprintf");
3074 /* Reject merges into a work tree with conflicted files. */
3075 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3076 free(ondisk_path);
3077 if (err)
3078 return err;
3079 if (status == GOT_STATUS_CONFLICT)
3080 return got_error(GOT_ERR_CONFLICTS);
3082 return NULL;
3085 static const struct got_error *
3086 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3087 const char *fileindex_path, struct got_object_id *commit_id1,
3088 struct got_object_id *commit_id2, struct got_repository *repo,
3089 got_worktree_checkout_cb progress_cb, void *progress_arg,
3090 got_cancel_cb cancel_cb, void *cancel_arg)
3092 const struct got_error *err = NULL, *sync_err;
3093 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3094 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3095 struct check_merge_conflicts_arg cmc_arg;
3096 struct merge_file_cb_arg arg;
3097 char *label_orig = NULL;
3099 if (commit_id1) {
3100 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3101 worktree->path_prefix);
3102 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3103 goto done;
3105 if (tree_id1) {
3106 char *id_str;
3108 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3109 if (err)
3110 goto done;
3112 err = got_object_id_str(&id_str, commit_id1);
3113 if (err)
3114 goto done;
3116 if (asprintf(&label_orig, "%s: commit %s",
3117 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3118 err = got_error_from_errno("asprintf");
3119 free(id_str);
3120 goto done;
3122 free(id_str);
3125 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3126 worktree->path_prefix);
3127 if (err)
3128 goto done;
3130 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3131 if (err)
3132 goto done;
3134 cmc_arg.worktree = worktree;
3135 cmc_arg.fileindex = fileindex;
3136 cmc_arg.repo = repo;
3137 err = got_diff_tree(tree1, tree2, "", "", repo,
3138 check_merge_conflicts, &cmc_arg, 0);
3139 if (err)
3140 goto done;
3142 arg.worktree = worktree;
3143 arg.fileindex = fileindex;
3144 arg.progress_cb = progress_cb;
3145 arg.progress_arg = progress_arg;
3146 arg.cancel_cb = cancel_cb;
3147 arg.cancel_arg = cancel_arg;
3148 arg.label_orig = label_orig;
3149 arg.commit_id2 = commit_id2;
3150 arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
3151 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3152 sync_err = sync_fileindex(fileindex, fileindex_path);
3153 if (sync_err && err == NULL)
3154 err = sync_err;
3155 done:
3156 if (tree1)
3157 got_object_tree_close(tree1);
3158 if (tree2)
3159 got_object_tree_close(tree2);
3160 free(label_orig);
3161 return err;
3164 const struct got_error *
3165 got_worktree_merge_files(struct got_worktree *worktree,
3166 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3167 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3168 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3170 const struct got_error *err, *unlockerr;
3171 char *fileindex_path = NULL;
3172 struct got_fileindex *fileindex = NULL;
3174 err = lock_worktree(worktree, LOCK_EX);
3175 if (err)
3176 return err;
3178 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3179 if (err)
3180 goto done;
3182 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3183 worktree);
3184 if (err)
3185 goto done;
3187 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3188 commit_id2, repo, progress_cb, progress_arg,
3189 cancel_cb, cancel_arg);
3190 done:
3191 if (fileindex)
3192 got_fileindex_free(fileindex);
3193 free(fileindex_path);
3194 unlockerr = lock_worktree(worktree, LOCK_SH);
3195 if (unlockerr && err == NULL)
3196 err = unlockerr;
3197 return err;
3200 struct diff_dir_cb_arg {
3201 struct got_fileindex *fileindex;
3202 struct got_worktree *worktree;
3203 const char *status_path;
3204 size_t status_path_len;
3205 struct got_repository *repo;
3206 got_worktree_status_cb status_cb;
3207 void *status_arg;
3208 got_cancel_cb cancel_cb;
3209 void *cancel_arg;
3210 /* A pathlist containing per-directory pathlists of ignore patterns. */
3211 struct got_pathlist_head *ignores;
3212 int report_unchanged;
3213 int no_ignores;
3216 static const struct got_error *
3217 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3218 int dirfd, const char *de_name,
3219 got_worktree_status_cb status_cb, void *status_arg,
3220 struct got_repository *repo, int report_unchanged)
3222 const struct got_error *err = NULL;
3223 unsigned char status = GOT_STATUS_NO_CHANGE;
3224 unsigned char staged_status = get_staged_status(ie);
3225 struct stat sb;
3226 struct got_object_id blob_id, commit_id, staged_blob_id;
3227 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3228 struct got_object_id *staged_blob_idp = NULL;
3230 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3231 if (err)
3232 return err;
3234 if (status == GOT_STATUS_NO_CHANGE &&
3235 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3236 return NULL;
3238 if (got_fileindex_entry_has_blob(ie)) {
3239 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3240 blob_idp = &blob_id;
3242 if (got_fileindex_entry_has_commit(ie)) {
3243 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3244 commit_idp = &commit_id;
3246 if (staged_status == GOT_STATUS_ADD ||
3247 staged_status == GOT_STATUS_MODIFY) {
3248 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3249 SHA1_DIGEST_LENGTH);
3250 staged_blob_idp = &staged_blob_id;
3253 return (*status_cb)(status_arg, status, staged_status,
3254 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3257 static const struct got_error *
3258 status_old_new(void *arg, struct got_fileindex_entry *ie,
3259 struct dirent *de, const char *parent_path, int dirfd)
3261 const struct got_error *err = NULL;
3262 struct diff_dir_cb_arg *a = arg;
3263 char *abspath;
3265 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3266 return got_error(GOT_ERR_CANCELLED);
3268 if (got_path_cmp(parent_path, a->status_path,
3269 strlen(parent_path), a->status_path_len) != 0 &&
3270 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3271 return NULL;
3273 if (parent_path[0]) {
3274 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3275 parent_path, de->d_name) == -1)
3276 return got_error_from_errno("asprintf");
3277 } else {
3278 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3279 de->d_name) == -1)
3280 return got_error_from_errno("asprintf");
3283 err = report_file_status(ie, abspath, dirfd, de->d_name,
3284 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3285 free(abspath);
3286 return err;
3289 static const struct got_error *
3290 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3292 struct diff_dir_cb_arg *a = arg;
3293 struct got_object_id blob_id, commit_id;
3294 unsigned char status;
3296 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3297 return got_error(GOT_ERR_CANCELLED);
3299 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3300 return NULL;
3302 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3303 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3304 if (got_fileindex_entry_has_file_on_disk(ie))
3305 status = GOT_STATUS_MISSING;
3306 else
3307 status = GOT_STATUS_DELETE;
3308 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3309 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3312 void
3313 free_ignorelist(struct got_pathlist_head *ignorelist)
3315 struct got_pathlist_entry *pe;
3317 TAILQ_FOREACH(pe, ignorelist, entry)
3318 free((char *)pe->path);
3319 got_pathlist_free(ignorelist);
3322 void
3323 free_ignores(struct got_pathlist_head *ignores)
3325 struct got_pathlist_entry *pe;
3327 TAILQ_FOREACH(pe, ignores, entry) {
3328 struct got_pathlist_head *ignorelist = pe->data;
3329 free_ignorelist(ignorelist);
3330 free((char *)pe->path);
3332 got_pathlist_free(ignores);
3335 static const struct got_error *
3336 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3338 const struct got_error *err = NULL;
3339 struct got_pathlist_entry *pe = NULL;
3340 struct got_pathlist_head *ignorelist;
3341 char *line = NULL, *pattern, *dirpath = NULL;
3342 size_t linesize = 0;
3343 ssize_t linelen;
3345 ignorelist = calloc(1, sizeof(*ignorelist));
3346 if (ignorelist == NULL)
3347 return got_error_from_errno("calloc");
3348 TAILQ_INIT(ignorelist);
3350 while ((linelen = getline(&line, &linesize, f)) != -1) {
3351 if (linelen > 0 && line[linelen - 1] == '\n')
3352 line[linelen - 1] = '\0';
3354 /* Git's ignores may contain comments. */
3355 if (line[0] == '#')
3356 continue;
3358 /* Git's negated patterns are not (yet?) supported. */
3359 if (line[0] == '!')
3360 continue;
3362 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3363 line) == -1) {
3364 err = got_error_from_errno("asprintf");
3365 goto done;
3367 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3368 if (err)
3369 goto done;
3371 if (ferror(f)) {
3372 err = got_error_from_errno("getline");
3373 goto done;
3376 dirpath = strdup(path);
3377 if (dirpath == NULL) {
3378 err = got_error_from_errno("strdup");
3379 goto done;
3381 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3382 done:
3383 free(line);
3384 if (err || pe == NULL) {
3385 free(dirpath);
3386 free_ignorelist(ignorelist);
3388 return err;
3391 int
3392 match_ignores(struct got_pathlist_head *ignores, const char *path)
3394 struct got_pathlist_entry *pe;
3396 /* Handle patterns which match in all directories. */
3397 TAILQ_FOREACH(pe, ignores, entry) {
3398 struct got_pathlist_head *ignorelist = pe->data;
3399 struct got_pathlist_entry *pi;
3401 TAILQ_FOREACH(pi, ignorelist, entry) {
3402 const char *p, *pattern = pi->path;
3404 if (strncmp(pattern, "**/", 3) != 0)
3405 continue;
3406 pattern += 3;
3407 p = path;
3408 while (*p) {
3409 if (fnmatch(pattern, p,
3410 FNM_PATHNAME | FNM_LEADING_DIR)) {
3411 /* Retry in next directory. */
3412 while (*p && *p != '/')
3413 p++;
3414 while (*p == '/')
3415 p++;
3416 continue;
3418 return 1;
3424 * The ignores pathlist contains ignore lists from children before
3425 * parents, so we can find the most specific ignorelist by walking
3426 * ignores backwards.
3428 pe = TAILQ_LAST(ignores, got_pathlist_head);
3429 while (pe) {
3430 if (got_path_is_child(path, pe->path, pe->path_len)) {
3431 struct got_pathlist_head *ignorelist = pe->data;
3432 struct got_pathlist_entry *pi;
3433 TAILQ_FOREACH(pi, ignorelist, entry) {
3434 const char *pattern = pi->path;
3435 int flags = FNM_LEADING_DIR;
3436 if (strstr(pattern, "/**/") == NULL)
3437 flags |= FNM_PATHNAME;
3438 if (fnmatch(pattern, path, flags))
3439 continue;
3440 return 1;
3443 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3446 return 0;
3449 static const struct got_error *
3450 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3451 const char *path, int dirfd, const char *ignores_filename)
3453 const struct got_error *err = NULL;
3454 char *ignorespath;
3455 int fd = -1;
3456 FILE *ignoresfile = NULL;
3458 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3459 path[0] ? "/" : "", ignores_filename) == -1)
3460 return got_error_from_errno("asprintf");
3462 if (dirfd != -1) {
3463 fd = openat(dirfd, ignores_filename,
3464 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
3465 if (fd == -1) {
3466 if (errno != ENOENT && errno != EACCES)
3467 err = got_error_from_errno2("openat",
3468 ignorespath);
3469 } else {
3470 ignoresfile = fdopen(fd, "r");
3471 if (ignoresfile == NULL)
3472 err = got_error_from_errno2("fdopen",
3473 ignorespath);
3474 else {
3475 fd = -1;
3476 err = read_ignores(ignores, path, ignoresfile);
3479 } else {
3480 ignoresfile = fopen(ignorespath, "re");
3481 if (ignoresfile == NULL) {
3482 if (errno != ENOENT && errno != EACCES)
3483 err = got_error_from_errno2("fopen",
3484 ignorespath);
3485 } else
3486 err = read_ignores(ignores, path, ignoresfile);
3489 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3490 err = got_error_from_errno2("fclose", path);
3491 if (fd != -1 && close(fd) == -1 && err == NULL)
3492 err = got_error_from_errno2("close", path);
3493 free(ignorespath);
3494 return err;
3497 static const struct got_error *
3498 status_new(int *ignore, void *arg, struct dirent *de, const char *parent_path,
3499 int dirfd)
3501 const struct got_error *err = NULL;
3502 struct diff_dir_cb_arg *a = arg;
3503 char *path = NULL;
3505 if (ignore != NULL)
3506 *ignore = 0;
3508 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3509 return got_error(GOT_ERR_CANCELLED);
3511 if (parent_path[0]) {
3512 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3513 return got_error_from_errno("asprintf");
3514 } else {
3515 path = de->d_name;
3518 if (de->d_type == DT_DIR) {
3519 if (!a->no_ignores && ignore != NULL &&
3520 match_ignores(a->ignores, path))
3521 *ignore = 1;
3522 } else if (!match_ignores(a->ignores, path) &&
3523 got_path_is_child(path, a->status_path, a->status_path_len))
3524 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3525 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3526 if (parent_path[0])
3527 free(path);
3528 return err;
3531 static const struct got_error *
3532 status_traverse(void *arg, const char *path, int dirfd)
3534 const struct got_error *err = NULL;
3535 struct diff_dir_cb_arg *a = arg;
3537 if (a->no_ignores)
3538 return NULL;
3540 err = add_ignores(a->ignores, a->worktree->root_path,
3541 path, dirfd, ".cvsignore");
3542 if (err)
3543 return err;
3545 err = add_ignores(a->ignores, a->worktree->root_path, path,
3546 dirfd, ".gitignore");
3548 return err;
3551 static const struct got_error *
3552 report_single_file_status(const char *path, const char *ondisk_path,
3553 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3554 void *status_arg, struct got_repository *repo, int report_unchanged,
3555 struct got_pathlist_head *ignores, int no_ignores)
3557 struct got_fileindex_entry *ie;
3558 struct stat sb;
3560 if (!no_ignores && match_ignores(ignores, path))
3561 return NULL;
3563 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3564 if (ie)
3565 return report_file_status(ie, ondisk_path, -1, NULL,
3566 status_cb, status_arg, repo, report_unchanged);
3568 if (lstat(ondisk_path, &sb) == -1) {
3569 if (errno != ENOENT)
3570 return got_error_from_errno2("lstat", ondisk_path);
3571 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3572 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3573 return NULL;
3576 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3577 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3578 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3580 return NULL;
3583 static const struct got_error *
3584 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3585 const char *root_path, const char *path)
3587 const struct got_error *err;
3588 char *parent_path, *next_parent_path = NULL;
3590 err = add_ignores(ignores, root_path, "", -1,
3591 ".cvsignore");
3592 if (err)
3593 return err;
3595 err = add_ignores(ignores, root_path, "", -1,
3596 ".gitignore");
3597 if (err)
3598 return err;
3600 err = got_path_dirname(&parent_path, path);
3601 if (err) {
3602 if (err->code == GOT_ERR_BAD_PATH)
3603 return NULL; /* cannot traverse parent */
3604 return err;
3606 for (;;) {
3607 err = add_ignores(ignores, root_path, parent_path, -1,
3608 ".cvsignore");
3609 if (err)
3610 break;
3611 err = add_ignores(ignores, root_path, parent_path, -1,
3612 ".gitignore");
3613 if (err)
3614 break;
3615 err = got_path_dirname(&next_parent_path, parent_path);
3616 if (err) {
3617 if (err->code == GOT_ERR_BAD_PATH)
3618 err = NULL; /* traversed everything */
3619 break;
3621 if (got_path_is_root_dir(parent_path))
3622 break;
3623 free(parent_path);
3624 parent_path = next_parent_path;
3625 next_parent_path = NULL;
3628 free(parent_path);
3629 free(next_parent_path);
3630 return err;
3633 static const struct got_error *
3634 worktree_status(struct got_worktree *worktree, const char *path,
3635 struct got_fileindex *fileindex, struct got_repository *repo,
3636 got_worktree_status_cb status_cb, void *status_arg,
3637 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3638 int report_unchanged)
3640 const struct got_error *err = NULL;
3641 int fd = -1;
3642 struct got_fileindex_diff_dir_cb fdiff_cb;
3643 struct diff_dir_cb_arg arg;
3644 char *ondisk_path = NULL;
3645 struct got_pathlist_head ignores;
3647 TAILQ_INIT(&ignores);
3649 if (asprintf(&ondisk_path, "%s%s%s",
3650 worktree->root_path, path[0] ? "/" : "", path) == -1)
3651 return got_error_from_errno("asprintf");
3653 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
3654 if (fd == -1) {
3655 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3656 !got_err_open_nofollow_on_symlink())
3657 err = got_error_from_errno2("open", ondisk_path);
3658 else {
3659 if (!no_ignores) {
3660 err = add_ignores_from_parent_paths(&ignores,
3661 worktree->root_path, ondisk_path);
3662 if (err)
3663 goto done;
3665 err = report_single_file_status(path, ondisk_path,
3666 fileindex, status_cb, status_arg, repo,
3667 report_unchanged, &ignores, no_ignores);
3669 } else {
3670 fdiff_cb.diff_old_new = status_old_new;
3671 fdiff_cb.diff_old = status_old;
3672 fdiff_cb.diff_new = status_new;
3673 fdiff_cb.diff_traverse = status_traverse;
3674 arg.fileindex = fileindex;
3675 arg.worktree = worktree;
3676 arg.status_path = path;
3677 arg.status_path_len = strlen(path);
3678 arg.repo = repo;
3679 arg.status_cb = status_cb;
3680 arg.status_arg = status_arg;
3681 arg.cancel_cb = cancel_cb;
3682 arg.cancel_arg = cancel_arg;
3683 arg.report_unchanged = report_unchanged;
3684 arg.no_ignores = no_ignores;
3685 if (!no_ignores) {
3686 err = add_ignores_from_parent_paths(&ignores,
3687 worktree->root_path, path);
3688 if (err)
3689 goto done;
3691 arg.ignores = &ignores;
3692 err = got_fileindex_diff_dir(fileindex, fd,
3693 worktree->root_path, path, repo, &fdiff_cb, &arg);
3695 done:
3696 free_ignores(&ignores);
3697 if (fd != -1 && close(fd) == -1 && err == NULL)
3698 err = got_error_from_errno("close");
3699 free(ondisk_path);
3700 return err;
3703 const struct got_error *
3704 got_worktree_status(struct got_worktree *worktree,
3705 struct got_pathlist_head *paths, struct got_repository *repo,
3706 int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3707 got_cancel_cb cancel_cb, void *cancel_arg)
3709 const struct got_error *err = NULL;
3710 char *fileindex_path = NULL;
3711 struct got_fileindex *fileindex = NULL;
3712 struct got_pathlist_entry *pe;
3714 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3715 if (err)
3716 return err;
3718 TAILQ_FOREACH(pe, paths, entry) {
3719 err = worktree_status(worktree, pe->path, fileindex, repo,
3720 status_cb, status_arg, cancel_cb, cancel_arg,
3721 no_ignores, 0);
3722 if (err)
3723 break;
3725 free(fileindex_path);
3726 got_fileindex_free(fileindex);
3727 return err;
3730 const struct got_error *
3731 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3732 const char *arg)
3734 const struct got_error *err = NULL;
3735 char *resolved = NULL, *cwd = NULL, *path = NULL;
3736 size_t len;
3737 struct stat sb;
3738 char *abspath = NULL;
3739 char canonpath[PATH_MAX];
3741 *wt_path = NULL;
3743 cwd = getcwd(NULL, 0);
3744 if (cwd == NULL)
3745 return got_error_from_errno("getcwd");
3747 if (lstat(arg, &sb) == -1) {
3748 if (errno != ENOENT) {
3749 err = got_error_from_errno2("lstat", arg);
3750 goto done;
3752 sb.st_mode = 0;
3754 if (S_ISLNK(sb.st_mode)) {
3756 * We cannot use realpath(3) with symlinks since we want to
3757 * operate on the symlink itself.
3758 * But we can make the path absolute, assuming it is relative
3759 * to the current working directory, and then canonicalize it.
3761 if (!got_path_is_absolute(arg)) {
3762 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3763 err = got_error_from_errno("asprintf");
3764 goto done;
3768 err = got_canonpath(abspath ? abspath : arg, canonpath,
3769 sizeof(canonpath));
3770 if (err)
3771 goto done;
3772 resolved = strdup(canonpath);
3773 if (resolved == NULL) {
3774 err = got_error_from_errno("strdup");
3775 goto done;
3777 } else {
3778 resolved = realpath(arg, NULL);
3779 if (resolved == NULL) {
3780 if (errno != ENOENT) {
3781 err = got_error_from_errno2("realpath", arg);
3782 goto done;
3784 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3785 err = got_error_from_errno("asprintf");
3786 goto done;
3788 err = got_canonpath(abspath, canonpath,
3789 sizeof(canonpath));
3790 if (err)
3791 goto done;
3792 resolved = strdup(canonpath);
3793 if (resolved == NULL) {
3794 err = got_error_from_errno("strdup");
3795 goto done;
3800 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3801 strlen(got_worktree_get_root_path(worktree)))) {
3802 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3803 goto done;
3806 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3807 err = got_path_skip_common_ancestor(&path,
3808 got_worktree_get_root_path(worktree), resolved);
3809 if (err)
3810 goto done;
3811 } else {
3812 path = strdup("");
3813 if (path == NULL) {
3814 err = got_error_from_errno("strdup");
3815 goto done;
3819 /* XXX status walk can't deal with trailing slash! */
3820 len = strlen(path);
3821 while (len > 0 && path[len - 1] == '/') {
3822 path[len - 1] = '\0';
3823 len--;
3825 done:
3826 free(abspath);
3827 free(resolved);
3828 free(cwd);
3829 if (err == NULL)
3830 *wt_path = path;
3831 else
3832 free(path);
3833 return err;
3836 struct schedule_addition_args {
3837 struct got_worktree *worktree;
3838 struct got_fileindex *fileindex;
3839 got_worktree_checkout_cb progress_cb;
3840 void *progress_arg;
3841 struct got_repository *repo;
3844 static const struct got_error *
3845 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3846 const char *relpath, struct got_object_id *blob_id,
3847 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3848 int dirfd, const char *de_name)
3850 struct schedule_addition_args *a = arg;
3851 const struct got_error *err = NULL;
3852 struct got_fileindex_entry *ie;
3853 struct stat sb;
3854 char *ondisk_path;
3856 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3857 relpath) == -1)
3858 return got_error_from_errno("asprintf");
3860 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3861 if (ie) {
3862 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3863 de_name, a->repo);
3864 if (err)
3865 goto done;
3866 /* Re-adding an existing entry is a no-op. */
3867 if (status == GOT_STATUS_ADD)
3868 goto done;
3869 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3870 if (err)
3871 goto done;
3874 if (status != GOT_STATUS_UNVERSIONED) {
3875 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3876 goto done;
3879 err = got_fileindex_entry_alloc(&ie, relpath);
3880 if (err)
3881 goto done;
3882 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3883 relpath, NULL, NULL, 1);
3884 if (err) {
3885 got_fileindex_entry_free(ie);
3886 goto done;
3888 err = got_fileindex_entry_add(a->fileindex, ie);
3889 if (err) {
3890 got_fileindex_entry_free(ie);
3891 goto done;
3893 done:
3894 free(ondisk_path);
3895 if (err)
3896 return err;
3897 if (status == GOT_STATUS_ADD)
3898 return NULL;
3899 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3902 const struct got_error *
3903 got_worktree_schedule_add(struct got_worktree *worktree,
3904 struct got_pathlist_head *paths,
3905 got_worktree_checkout_cb progress_cb, void *progress_arg,
3906 struct got_repository *repo, int no_ignores)
3908 struct got_fileindex *fileindex = NULL;
3909 char *fileindex_path = NULL;
3910 const struct got_error *err = NULL, *sync_err, *unlockerr;
3911 struct got_pathlist_entry *pe;
3912 struct schedule_addition_args saa;
3914 err = lock_worktree(worktree, LOCK_EX);
3915 if (err)
3916 return err;
3918 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3919 if (err)
3920 goto done;
3922 saa.worktree = worktree;
3923 saa.fileindex = fileindex;
3924 saa.progress_cb = progress_cb;
3925 saa.progress_arg = progress_arg;
3926 saa.repo = repo;
3928 TAILQ_FOREACH(pe, paths, entry) {
3929 err = worktree_status(worktree, pe->path, fileindex, repo,
3930 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3931 if (err)
3932 break;
3934 sync_err = sync_fileindex(fileindex, fileindex_path);
3935 if (sync_err && err == NULL)
3936 err = sync_err;
3937 done:
3938 free(fileindex_path);
3939 if (fileindex)
3940 got_fileindex_free(fileindex);
3941 unlockerr = lock_worktree(worktree, LOCK_SH);
3942 if (unlockerr && err == NULL)
3943 err = unlockerr;
3944 return err;
3947 struct schedule_deletion_args {
3948 struct got_worktree *worktree;
3949 struct got_fileindex *fileindex;
3950 got_worktree_delete_cb progress_cb;
3951 void *progress_arg;
3952 struct got_repository *repo;
3953 int delete_local_mods;
3954 int keep_on_disk;
3955 const char *status_codes;
3958 static const struct got_error *
3959 schedule_for_deletion(void *arg, unsigned char status,
3960 unsigned char staged_status, const char *relpath,
3961 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3962 struct got_object_id *commit_id, int dirfd, const char *de_name)
3964 struct schedule_deletion_args *a = arg;
3965 const struct got_error *err = NULL;
3966 struct got_fileindex_entry *ie = NULL;
3967 struct stat sb;
3968 char *ondisk_path;
3970 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3971 if (ie == NULL)
3972 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3974 staged_status = get_staged_status(ie);
3975 if (staged_status != GOT_STATUS_NO_CHANGE) {
3976 if (staged_status == GOT_STATUS_DELETE)
3977 return NULL;
3978 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3981 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3982 relpath) == -1)
3983 return got_error_from_errno("asprintf");
3985 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3986 a->repo);
3987 if (err)
3988 goto done;
3990 if (a->status_codes) {
3991 size_t ncodes = strlen(a->status_codes);
3992 int i;
3993 for (i = 0; i < ncodes ; i++) {
3994 if (status == a->status_codes[i])
3995 break;
3997 if (i == ncodes) {
3998 /* Do not delete files in non-matching status. */
3999 free(ondisk_path);
4000 return NULL;
4002 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4003 a->status_codes[i] != GOT_STATUS_MISSING) {
4004 static char msg[64];
4005 snprintf(msg, sizeof(msg),
4006 "invalid status code '%c'", a->status_codes[i]);
4007 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4008 goto done;
4012 if (status != GOT_STATUS_NO_CHANGE) {
4013 if (status == GOT_STATUS_DELETE)
4014 goto done;
4015 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4016 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4017 goto done;
4019 if (status != GOT_STATUS_MODIFY &&
4020 status != GOT_STATUS_MISSING) {
4021 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4022 goto done;
4026 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4027 size_t root_len;
4029 if (dirfd != -1) {
4030 if (unlinkat(dirfd, de_name, 0) != 0) {
4031 err = got_error_from_errno2("unlinkat",
4032 ondisk_path);
4033 goto done;
4035 } else if (unlink(ondisk_path) != 0) {
4036 err = got_error_from_errno2("unlink", ondisk_path);
4037 goto done;
4040 root_len = strlen(a->worktree->root_path);
4041 do {
4042 char *parent;
4043 err = got_path_dirname(&parent, ondisk_path);
4044 if (err)
4045 goto done;
4046 free(ondisk_path);
4047 ondisk_path = parent;
4048 if (rmdir(ondisk_path) == -1) {
4049 if (errno != ENOTEMPTY)
4050 err = got_error_from_errno2("rmdir",
4051 ondisk_path);
4052 break;
4054 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4055 strlen(ondisk_path), root_len) != 0);
4058 got_fileindex_entry_mark_deleted_from_disk(ie);
4059 done:
4060 free(ondisk_path);
4061 if (err)
4062 return err;
4063 if (status == GOT_STATUS_DELETE)
4064 return NULL;
4065 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4066 staged_status, relpath);
4069 const struct got_error *
4070 got_worktree_schedule_delete(struct got_worktree *worktree,
4071 struct got_pathlist_head *paths, int delete_local_mods,
4072 const char *status_codes,
4073 got_worktree_delete_cb progress_cb, void *progress_arg,
4074 struct got_repository *repo, int keep_on_disk)
4076 struct got_fileindex *fileindex = NULL;
4077 char *fileindex_path = NULL;
4078 const struct got_error *err = NULL, *sync_err, *unlockerr;
4079 struct got_pathlist_entry *pe;
4080 struct schedule_deletion_args sda;
4082 err = lock_worktree(worktree, LOCK_EX);
4083 if (err)
4084 return err;
4086 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4087 if (err)
4088 goto done;
4090 sda.worktree = worktree;
4091 sda.fileindex = fileindex;
4092 sda.progress_cb = progress_cb;
4093 sda.progress_arg = progress_arg;
4094 sda.repo = repo;
4095 sda.delete_local_mods = delete_local_mods;
4096 sda.keep_on_disk = keep_on_disk;
4097 sda.status_codes = status_codes;
4099 TAILQ_FOREACH(pe, paths, entry) {
4100 err = worktree_status(worktree, pe->path, fileindex, repo,
4101 schedule_for_deletion, &sda, NULL, NULL, 1, 1);
4102 if (err)
4103 break;
4105 sync_err = sync_fileindex(fileindex, fileindex_path);
4106 if (sync_err && err == NULL)
4107 err = sync_err;
4108 done:
4109 free(fileindex_path);
4110 if (fileindex)
4111 got_fileindex_free(fileindex);
4112 unlockerr = lock_worktree(worktree, LOCK_SH);
4113 if (unlockerr && err == NULL)
4114 err = unlockerr;
4115 return err;
4118 static const struct got_error *
4119 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4121 const struct got_error *err = NULL;
4122 char *line = NULL;
4123 size_t linesize = 0, n;
4124 ssize_t linelen;
4126 linelen = getline(&line, &linesize, infile);
4127 if (linelen == -1) {
4128 if (ferror(infile)) {
4129 err = got_error_from_errno("getline");
4130 goto done;
4132 return NULL;
4134 if (outfile) {
4135 n = fwrite(line, 1, linelen, outfile);
4136 if (n != linelen) {
4137 err = got_ferror(outfile, GOT_ERR_IO);
4138 goto done;
4141 if (rejectfile) {
4142 n = fwrite(line, 1, linelen, rejectfile);
4143 if (n != linelen)
4144 err = got_ferror(outfile, GOT_ERR_IO);
4146 done:
4147 free(line);
4148 return err;
4151 static const struct got_error *
4152 skip_one_line(FILE *f)
4154 char *line = NULL;
4155 size_t linesize = 0;
4156 ssize_t linelen;
4158 linelen = getline(&line, &linesize, f);
4159 if (linelen == -1) {
4160 if (ferror(f))
4161 return got_error_from_errno("getline");
4162 return NULL;
4164 free(line);
4165 return NULL;
4168 static const struct got_error *
4169 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4170 int start_old, int end_old, int start_new, int end_new,
4171 FILE *outfile, FILE *rejectfile)
4173 const struct got_error *err;
4175 /* Copy old file's lines leading up to patch. */
4176 while (!feof(f1) && *line_cur1 < start_old) {
4177 err = copy_one_line(f1, outfile, NULL);
4178 if (err)
4179 return err;
4180 (*line_cur1)++;
4182 /* Skip new file's lines leading up to patch. */
4183 while (!feof(f2) && *line_cur2 < start_new) {
4184 if (rejectfile)
4185 err = copy_one_line(f2, NULL, rejectfile);
4186 else
4187 err = skip_one_line(f2);
4188 if (err)
4189 return err;
4190 (*line_cur2)++;
4192 /* Copy patched lines. */
4193 while (!feof(f2) && *line_cur2 <= end_new) {
4194 err = copy_one_line(f2, outfile, NULL);
4195 if (err)
4196 return err;
4197 (*line_cur2)++;
4199 /* Skip over old file's replaced lines. */
4200 while (!feof(f1) && *line_cur1 <= end_old) {
4201 if (rejectfile)
4202 err = copy_one_line(f1, NULL, rejectfile);
4203 else
4204 err = skip_one_line(f1);
4205 if (err)
4206 return err;
4207 (*line_cur1)++;
4210 return NULL;
4213 static const struct got_error *
4214 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4215 FILE *outfile, FILE *rejectfile)
4217 const struct got_error *err;
4219 if (outfile) {
4220 /* Copy old file's lines until EOF. */
4221 while (!feof(f1)) {
4222 err = copy_one_line(f1, outfile, NULL);
4223 if (err)
4224 return err;
4225 (*line_cur1)++;
4228 if (rejectfile) {
4229 /* Copy new file's lines until EOF. */
4230 while (!feof(f2)) {
4231 err = copy_one_line(f2, NULL, rejectfile);
4232 if (err)
4233 return err;
4234 (*line_cur2)++;
4238 return NULL;
4241 static const struct got_error *
4242 apply_or_reject_change(int *choice, int *nchunks_used,
4243 struct diff_result *diff_result, int n,
4244 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4245 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4246 got_worktree_patch_cb patch_cb, void *patch_arg)
4248 const struct got_error *err = NULL;
4249 struct diff_chunk_context cc = {};
4250 int start_old, end_old, start_new, end_new;
4251 FILE *hunkfile;
4252 struct diff_output_unidiff_state *diff_state;
4253 struct diff_input_info diff_info;
4254 int rc;
4256 *choice = GOT_PATCH_CHOICE_NONE;
4258 /* Get changed line numbers without context lines for copy_change(). */
4259 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4260 start_old = cc.left.start;
4261 end_old = cc.left.end;
4262 start_new = cc.right.start;
4263 end_new = cc.right.end;
4265 /* Get the same change with context lines for display. */
4266 memset(&cc, 0, sizeof(cc));
4267 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4269 memset(&diff_info, 0, sizeof(diff_info));
4270 diff_info.left_path = relpath;
4271 diff_info.right_path = relpath;
4273 diff_state = diff_output_unidiff_state_alloc();
4274 if (diff_state == NULL)
4275 return got_error_set_errno(ENOMEM,
4276 "diff_output_unidiff_state_alloc");
4278 hunkfile = got_opentemp();
4279 if (hunkfile == NULL) {
4280 err = got_error_from_errno("got_opentemp");
4281 goto done;
4284 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4285 diff_result, &cc);
4286 if (rc != DIFF_RC_OK) {
4287 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4288 goto done;
4291 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4292 err = got_ferror(hunkfile, GOT_ERR_IO);
4293 goto done;
4296 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4297 hunkfile, changeno, nchanges);
4298 if (err)
4299 goto done;
4301 switch (*choice) {
4302 case GOT_PATCH_CHOICE_YES:
4303 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4304 end_old, start_new, end_new, outfile, rejectfile);
4305 break;
4306 case GOT_PATCH_CHOICE_NO:
4307 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4308 end_old, start_new, end_new, rejectfile, outfile);
4309 break;
4310 case GOT_PATCH_CHOICE_QUIT:
4311 break;
4312 default:
4313 err = got_error(GOT_ERR_PATCH_CHOICE);
4314 break;
4316 done:
4317 diff_output_unidiff_state_free(diff_state);
4318 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4319 err = got_error_from_errno("fclose");
4320 return err;
4323 struct revert_file_args {
4324 struct got_worktree *worktree;
4325 struct got_fileindex *fileindex;
4326 got_worktree_checkout_cb progress_cb;
4327 void *progress_arg;
4328 got_worktree_patch_cb patch_cb;
4329 void *patch_arg;
4330 struct got_repository *repo;
4331 int unlink_added_files;
4334 static const struct got_error *
4335 create_patched_content(char **path_outfile, int reverse_patch,
4336 struct got_object_id *blob_id, const char *path2,
4337 int dirfd2, const char *de_name2,
4338 const char *relpath, struct got_repository *repo,
4339 got_worktree_patch_cb patch_cb, void *patch_arg)
4341 const struct got_error *err, *free_err;
4342 struct got_blob_object *blob = NULL;
4343 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4344 int fd2 = -1;
4345 char link_target[PATH_MAX];
4346 ssize_t link_len = 0;
4347 char *path1 = NULL, *id_str = NULL;
4348 struct stat sb2;
4349 struct got_diffreg_result *diffreg_result = NULL;
4350 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4351 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4353 *path_outfile = NULL;
4355 err = got_object_id_str(&id_str, blob_id);
4356 if (err)
4357 return err;
4359 if (dirfd2 != -1) {
4360 fd2 = openat(dirfd2, de_name2,
4361 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4362 if (fd2 == -1) {
4363 if (!got_err_open_nofollow_on_symlink()) {
4364 err = got_error_from_errno2("openat", path2);
4365 goto done;
4367 link_len = readlinkat(dirfd2, de_name2,
4368 link_target, sizeof(link_target));
4369 if (link_len == -1)
4370 return got_error_from_errno2("readlinkat", path2);
4371 sb2.st_mode = S_IFLNK;
4372 sb2.st_size = link_len;
4374 } else {
4375 fd2 = open(path2, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4376 if (fd2 == -1) {
4377 if (!got_err_open_nofollow_on_symlink()) {
4378 err = got_error_from_errno2("open", path2);
4379 goto done;
4381 link_len = readlink(path2, link_target,
4382 sizeof(link_target));
4383 if (link_len == -1)
4384 return got_error_from_errno2("readlink", path2);
4385 sb2.st_mode = S_IFLNK;
4386 sb2.st_size = link_len;
4389 if (fd2 != -1) {
4390 if (fstat(fd2, &sb2) == -1) {
4391 err = got_error_from_errno2("fstat", path2);
4392 goto done;
4395 f2 = fdopen(fd2, "r");
4396 if (f2 == NULL) {
4397 err = got_error_from_errno2("fdopen", path2);
4398 goto done;
4400 fd2 = -1;
4401 } else {
4402 size_t n;
4403 f2 = got_opentemp();
4404 if (f2 == NULL) {
4405 err = got_error_from_errno2("got_opentemp", path2);
4406 goto done;
4408 n = fwrite(link_target, 1, link_len, f2);
4409 if (n != link_len) {
4410 err = got_ferror(f2, GOT_ERR_IO);
4411 goto done;
4413 if (fflush(f2) == EOF) {
4414 err = got_error_from_errno("fflush");
4415 goto done;
4417 rewind(f2);
4420 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4421 if (err)
4422 goto done;
4424 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4425 if (err)
4426 goto done;
4428 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4429 if (err)
4430 goto done;
4432 err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4433 NULL);
4434 if (err)
4435 goto done;
4437 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4438 if (err)
4439 goto done;
4441 if (fseek(f1, 0L, SEEK_SET) == -1)
4442 return got_ferror(f1, GOT_ERR_IO);
4443 if (fseek(f2, 0L, SEEK_SET) == -1)
4444 return got_ferror(f2, GOT_ERR_IO);
4446 /* Count the number of actual changes in the diff result. */
4447 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4448 struct diff_chunk_context cc = {};
4449 diff_chunk_context_load_change(&cc, &nchunks_used,
4450 diffreg_result->result, n, 0);
4451 nchanges++;
4453 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4454 int choice;
4455 err = apply_or_reject_change(&choice, &nchunks_used,
4456 diffreg_result->result, n, relpath, f1, f2,
4457 &line_cur1, &line_cur2,
4458 reverse_patch ? NULL : outfile,
4459 reverse_patch ? outfile : NULL,
4460 ++i, nchanges, patch_cb, patch_arg);
4461 if (err)
4462 goto done;
4463 if (choice == GOT_PATCH_CHOICE_YES)
4464 have_content = 1;
4465 else if (choice == GOT_PATCH_CHOICE_QUIT)
4466 break;
4468 if (have_content) {
4469 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4470 reverse_patch ? NULL : outfile,
4471 reverse_patch ? outfile : NULL);
4472 if (err)
4473 goto done;
4475 if (!S_ISLNK(sb2.st_mode)) {
4476 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4477 err = got_error_from_errno2("fchmod", path2);
4478 goto done;
4482 done:
4483 free(id_str);
4484 if (blob)
4485 got_object_blob_close(blob);
4486 free_err = got_diffreg_result_free(diffreg_result);
4487 if (err == NULL)
4488 err = free_err;
4489 if (f1 && fclose(f1) == EOF && err == NULL)
4490 err = got_error_from_errno2("fclose", path1);
4491 if (f2 && fclose(f2) == EOF && err == NULL)
4492 err = got_error_from_errno2("fclose", path2);
4493 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4494 err = got_error_from_errno2("close", path2);
4495 if (outfile && fclose(outfile) == EOF && err == NULL)
4496 err = got_error_from_errno2("fclose", *path_outfile);
4497 if (path1 && unlink(path1) == -1 && err == NULL)
4498 err = got_error_from_errno2("unlink", path1);
4499 if (err || !have_content) {
4500 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4501 err = got_error_from_errno2("unlink", *path_outfile);
4502 free(*path_outfile);
4503 *path_outfile = NULL;
4505 free(path1);
4506 return err;
4509 static const struct got_error *
4510 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4511 const char *relpath, struct got_object_id *blob_id,
4512 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4513 int dirfd, const char *de_name)
4515 struct revert_file_args *a = arg;
4516 const struct got_error *err = NULL;
4517 char *parent_path = NULL;
4518 struct got_fileindex_entry *ie;
4519 struct got_tree_object *tree = NULL;
4520 struct got_object_id *tree_id = NULL;
4521 const struct got_tree_entry *te = NULL;
4522 char *tree_path = NULL, *te_name;
4523 char *ondisk_path = NULL, *path_content = NULL;
4524 struct got_blob_object *blob = NULL;
4526 /* Reverting a staged deletion is a no-op. */
4527 if (status == GOT_STATUS_DELETE &&
4528 staged_status != GOT_STATUS_NO_CHANGE)
4529 return NULL;
4531 if (status == GOT_STATUS_UNVERSIONED)
4532 return (*a->progress_cb)(a->progress_arg,
4533 GOT_STATUS_UNVERSIONED, relpath);
4535 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4536 if (ie == NULL)
4537 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4539 /* Construct in-repository path of tree which contains this blob. */
4540 err = got_path_dirname(&parent_path, ie->path);
4541 if (err) {
4542 if (err->code != GOT_ERR_BAD_PATH)
4543 goto done;
4544 parent_path = strdup("/");
4545 if (parent_path == NULL) {
4546 err = got_error_from_errno("strdup");
4547 goto done;
4550 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4551 tree_path = strdup(parent_path);
4552 if (tree_path == NULL) {
4553 err = got_error_from_errno("strdup");
4554 goto done;
4556 } else {
4557 if (got_path_is_root_dir(parent_path)) {
4558 tree_path = strdup(a->worktree->path_prefix);
4559 if (tree_path == NULL) {
4560 err = got_error_from_errno("strdup");
4561 goto done;
4563 } else {
4564 if (asprintf(&tree_path, "%s/%s",
4565 a->worktree->path_prefix, parent_path) == -1) {
4566 err = got_error_from_errno("asprintf");
4567 goto done;
4572 err = got_object_id_by_path(&tree_id, a->repo,
4573 a->worktree->base_commit_id, tree_path);
4574 if (err) {
4575 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4576 (status == GOT_STATUS_ADD ||
4577 staged_status == GOT_STATUS_ADD)))
4578 goto done;
4579 } else {
4580 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4581 if (err)
4582 goto done;
4584 err = got_path_basename(&te_name, ie->path);
4585 if (err)
4586 goto done;
4588 te = got_object_tree_find_entry(tree, te_name);
4589 free(te_name);
4590 if (te == NULL && status != GOT_STATUS_ADD &&
4591 staged_status != GOT_STATUS_ADD) {
4592 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4593 goto done;
4597 switch (status) {
4598 case GOT_STATUS_ADD:
4599 if (a->patch_cb) {
4600 int choice = GOT_PATCH_CHOICE_NONE;
4601 err = (*a->patch_cb)(&choice, a->patch_arg,
4602 status, ie->path, NULL, 1, 1);
4603 if (err)
4604 goto done;
4605 if (choice != GOT_PATCH_CHOICE_YES)
4606 break;
4608 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4609 ie->path);
4610 if (err)
4611 goto done;
4612 got_fileindex_entry_remove(a->fileindex, ie);
4613 if (a->unlink_added_files) {
4614 if (asprintf(&ondisk_path, "%s/%s",
4615 got_worktree_get_root_path(a->worktree),
4616 relpath) == -1) {
4617 err = got_error_from_errno("asprintf");
4618 goto done;
4620 if (unlink(ondisk_path) == -1) {
4621 err = got_error_from_errno2("unlink",
4622 ondisk_path);
4623 break;
4626 break;
4627 case GOT_STATUS_DELETE:
4628 if (a->patch_cb) {
4629 int choice = GOT_PATCH_CHOICE_NONE;
4630 err = (*a->patch_cb)(&choice, a->patch_arg,
4631 status, ie->path, NULL, 1, 1);
4632 if (err)
4633 goto done;
4634 if (choice != GOT_PATCH_CHOICE_YES)
4635 break;
4637 /* fall through */
4638 case GOT_STATUS_MODIFY:
4639 case GOT_STATUS_MODE_CHANGE:
4640 case GOT_STATUS_CONFLICT:
4641 case GOT_STATUS_MISSING: {
4642 struct got_object_id id;
4643 if (staged_status == GOT_STATUS_ADD ||
4644 staged_status == GOT_STATUS_MODIFY) {
4645 memcpy(id.sha1, ie->staged_blob_sha1,
4646 SHA1_DIGEST_LENGTH);
4647 } else
4648 memcpy(id.sha1, ie->blob_sha1,
4649 SHA1_DIGEST_LENGTH);
4650 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4651 if (err)
4652 goto done;
4654 if (asprintf(&ondisk_path, "%s/%s",
4655 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4656 err = got_error_from_errno("asprintf");
4657 goto done;
4660 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4661 status == GOT_STATUS_CONFLICT)) {
4662 int is_bad_symlink = 0;
4663 err = create_patched_content(&path_content, 1, &id,
4664 ondisk_path, dirfd, de_name, ie->path, a->repo,
4665 a->patch_cb, a->patch_arg);
4666 if (err || path_content == NULL)
4667 break;
4668 if (te && S_ISLNK(te->mode)) {
4669 if (unlink(path_content) == -1) {
4670 err = got_error_from_errno2("unlink",
4671 path_content);
4672 break;
4674 err = install_symlink(&is_bad_symlink,
4675 a->worktree, ondisk_path, ie->path,
4676 blob, 0, 1, 0, 0, a->repo,
4677 a->progress_cb, a->progress_arg);
4678 } else {
4679 if (rename(path_content, ondisk_path) == -1) {
4680 err = got_error_from_errno3("rename",
4681 path_content, ondisk_path);
4682 goto done;
4685 } else {
4686 int is_bad_symlink = 0;
4687 if (te && S_ISLNK(te->mode)) {
4688 err = install_symlink(&is_bad_symlink,
4689 a->worktree, ondisk_path, ie->path,
4690 blob, 0, 1, 0, 0, a->repo,
4691 a->progress_cb, a->progress_arg);
4692 } else {
4693 err = install_blob(a->worktree, ondisk_path,
4694 ie->path,
4695 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4696 got_fileindex_perms_to_st(ie), blob,
4697 0, 1, 0, 0, a->repo,
4698 a->progress_cb, a->progress_arg);
4700 if (err)
4701 goto done;
4702 if (status == GOT_STATUS_DELETE ||
4703 status == GOT_STATUS_MODE_CHANGE) {
4704 err = got_fileindex_entry_update(ie,
4705 a->worktree->root_fd, relpath,
4706 blob->id.sha1,
4707 a->worktree->base_commit_id->sha1, 1);
4708 if (err)
4709 goto done;
4711 if (is_bad_symlink) {
4712 got_fileindex_entry_filetype_set(ie,
4713 GOT_FILEIDX_MODE_BAD_SYMLINK);
4716 break;
4718 default:
4719 break;
4721 done:
4722 free(ondisk_path);
4723 free(path_content);
4724 free(parent_path);
4725 free(tree_path);
4726 if (blob)
4727 got_object_blob_close(blob);
4728 if (tree)
4729 got_object_tree_close(tree);
4730 free(tree_id);
4731 return err;
4734 const struct got_error *
4735 got_worktree_revert(struct got_worktree *worktree,
4736 struct got_pathlist_head *paths,
4737 got_worktree_checkout_cb progress_cb, void *progress_arg,
4738 got_worktree_patch_cb patch_cb, void *patch_arg,
4739 struct got_repository *repo)
4741 struct got_fileindex *fileindex = NULL;
4742 char *fileindex_path = NULL;
4743 const struct got_error *err = NULL, *unlockerr = NULL;
4744 const struct got_error *sync_err = NULL;
4745 struct got_pathlist_entry *pe;
4746 struct revert_file_args rfa;
4748 err = lock_worktree(worktree, LOCK_EX);
4749 if (err)
4750 return err;
4752 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4753 if (err)
4754 goto done;
4756 rfa.worktree = worktree;
4757 rfa.fileindex = fileindex;
4758 rfa.progress_cb = progress_cb;
4759 rfa.progress_arg = progress_arg;
4760 rfa.patch_cb = patch_cb;
4761 rfa.patch_arg = patch_arg;
4762 rfa.repo = repo;
4763 rfa.unlink_added_files = 0;
4764 TAILQ_FOREACH(pe, paths, entry) {
4765 err = worktree_status(worktree, pe->path, fileindex, repo,
4766 revert_file, &rfa, NULL, NULL, 1, 0);
4767 if (err)
4768 break;
4770 sync_err = sync_fileindex(fileindex, fileindex_path);
4771 if (sync_err && err == NULL)
4772 err = sync_err;
4773 done:
4774 free(fileindex_path);
4775 if (fileindex)
4776 got_fileindex_free(fileindex);
4777 unlockerr = lock_worktree(worktree, LOCK_SH);
4778 if (unlockerr && err == NULL)
4779 err = unlockerr;
4780 return err;
4783 static void
4784 free_commitable(struct got_commitable *ct)
4786 free(ct->path);
4787 free(ct->in_repo_path);
4788 free(ct->ondisk_path);
4789 free(ct->blob_id);
4790 free(ct->base_blob_id);
4791 free(ct->staged_blob_id);
4792 free(ct->base_commit_id);
4793 free(ct);
4796 struct collect_commitables_arg {
4797 struct got_pathlist_head *commitable_paths;
4798 struct got_repository *repo;
4799 struct got_worktree *worktree;
4800 struct got_fileindex *fileindex;
4801 int have_staged_files;
4802 int allow_bad_symlinks;
4805 static const struct got_error *
4806 collect_commitables(void *arg, unsigned char status,
4807 unsigned char staged_status, const char *relpath,
4808 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4809 struct got_object_id *commit_id, int dirfd, const char *de_name)
4811 struct collect_commitables_arg *a = arg;
4812 const struct got_error *err = NULL;
4813 struct got_commitable *ct = NULL;
4814 struct got_pathlist_entry *new = NULL;
4815 char *parent_path = NULL, *path = NULL;
4816 struct stat sb;
4818 if (a->have_staged_files) {
4819 if (staged_status != GOT_STATUS_MODIFY &&
4820 staged_status != GOT_STATUS_ADD &&
4821 staged_status != GOT_STATUS_DELETE)
4822 return NULL;
4823 } else {
4824 if (status == GOT_STATUS_CONFLICT)
4825 return got_error(GOT_ERR_COMMIT_CONFLICT);
4827 if (status != GOT_STATUS_MODIFY &&
4828 status != GOT_STATUS_MODE_CHANGE &&
4829 status != GOT_STATUS_ADD &&
4830 status != GOT_STATUS_DELETE)
4831 return NULL;
4834 if (asprintf(&path, "/%s", relpath) == -1) {
4835 err = got_error_from_errno("asprintf");
4836 goto done;
4838 if (strcmp(path, "/") == 0) {
4839 parent_path = strdup("");
4840 if (parent_path == NULL)
4841 return got_error_from_errno("strdup");
4842 } else {
4843 err = got_path_dirname(&parent_path, path);
4844 if (err)
4845 return err;
4848 ct = calloc(1, sizeof(*ct));
4849 if (ct == NULL) {
4850 err = got_error_from_errno("calloc");
4851 goto done;
4854 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4855 relpath) == -1) {
4856 err = got_error_from_errno("asprintf");
4857 goto done;
4860 if (staged_status == GOT_STATUS_ADD ||
4861 staged_status == GOT_STATUS_MODIFY) {
4862 struct got_fileindex_entry *ie;
4863 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4864 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4865 case GOT_FILEIDX_MODE_REGULAR_FILE:
4866 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4867 ct->mode = S_IFREG;
4868 break;
4869 case GOT_FILEIDX_MODE_SYMLINK:
4870 ct->mode = S_IFLNK;
4871 break;
4872 default:
4873 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4874 goto done;
4876 ct->mode |= got_fileindex_entry_perms_get(ie);
4877 } else if (status != GOT_STATUS_DELETE &&
4878 staged_status != GOT_STATUS_DELETE) {
4879 if (dirfd != -1) {
4880 if (fstatat(dirfd, de_name, &sb,
4881 AT_SYMLINK_NOFOLLOW) == -1) {
4882 err = got_error_from_errno2("fstatat",
4883 ct->ondisk_path);
4884 goto done;
4886 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4887 err = got_error_from_errno2("lstat", ct->ondisk_path);
4888 goto done;
4890 ct->mode = sb.st_mode;
4893 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4894 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4895 relpath) == -1) {
4896 err = got_error_from_errno("asprintf");
4897 goto done;
4900 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4901 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4902 int is_bad_symlink;
4903 char target_path[PATH_MAX];
4904 ssize_t target_len;
4905 target_len = readlink(ct->ondisk_path, target_path,
4906 sizeof(target_path));
4907 if (target_len == -1) {
4908 err = got_error_from_errno2("readlink",
4909 ct->ondisk_path);
4910 goto done;
4912 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4913 target_len, ct->ondisk_path, a->worktree->root_path);
4914 if (err)
4915 goto done;
4916 if (is_bad_symlink) {
4917 err = got_error_path(ct->ondisk_path,
4918 GOT_ERR_BAD_SYMLINK);
4919 goto done;
4924 ct->status = status;
4925 ct->staged_status = staged_status;
4926 ct->blob_id = NULL; /* will be filled in when blob gets created */
4927 if (ct->status != GOT_STATUS_ADD &&
4928 ct->staged_status != GOT_STATUS_ADD) {
4929 ct->base_blob_id = got_object_id_dup(blob_id);
4930 if (ct->base_blob_id == NULL) {
4931 err = got_error_from_errno("got_object_id_dup");
4932 goto done;
4934 ct->base_commit_id = got_object_id_dup(commit_id);
4935 if (ct->base_commit_id == NULL) {
4936 err = got_error_from_errno("got_object_id_dup");
4937 goto done;
4940 if (ct->staged_status == GOT_STATUS_ADD ||
4941 ct->staged_status == GOT_STATUS_MODIFY) {
4942 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4943 if (ct->staged_blob_id == NULL) {
4944 err = got_error_from_errno("got_object_id_dup");
4945 goto done;
4948 ct->path = strdup(path);
4949 if (ct->path == NULL) {
4950 err = got_error_from_errno("strdup");
4951 goto done;
4953 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4954 done:
4955 if (ct && (err || new == NULL))
4956 free_commitable(ct);
4957 free(parent_path);
4958 free(path);
4959 return err;
4962 static const struct got_error *write_tree(struct got_object_id **, int *,
4963 struct got_tree_object *, const char *, struct got_pathlist_head *,
4964 got_worktree_status_cb status_cb, void *status_arg,
4965 struct got_repository *);
4967 static const struct got_error *
4968 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4969 struct got_tree_entry *te, const char *parent_path,
4970 struct got_pathlist_head *commitable_paths,
4971 got_worktree_status_cb status_cb, void *status_arg,
4972 struct got_repository *repo)
4974 const struct got_error *err = NULL;
4975 struct got_tree_object *subtree;
4976 char *subpath;
4978 if (asprintf(&subpath, "%s%s%s", parent_path,
4979 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4980 return got_error_from_errno("asprintf");
4982 err = got_object_open_as_tree(&subtree, repo, &te->id);
4983 if (err)
4984 return err;
4986 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4987 commitable_paths, status_cb, status_arg, repo);
4988 got_object_tree_close(subtree);
4989 free(subpath);
4990 return err;
4993 static const struct got_error *
4994 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4996 const struct got_error *err = NULL;
4997 char *ct_parent_path = NULL;
4999 *match = 0;
5001 if (strchr(ct->in_repo_path, '/') == NULL) {
5002 *match = got_path_is_root_dir(path);
5003 return NULL;
5006 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5007 if (err)
5008 return err;
5009 *match = (strcmp(path, ct_parent_path) == 0);
5010 free(ct_parent_path);
5011 return err;
5014 static mode_t
5015 get_ct_file_mode(struct got_commitable *ct)
5017 if (S_ISLNK(ct->mode))
5018 return S_IFLNK;
5020 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5023 static const struct got_error *
5024 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5025 struct got_tree_entry *te, struct got_commitable *ct)
5027 const struct got_error *err = NULL;
5029 *new_te = NULL;
5031 err = got_object_tree_entry_dup(new_te, te);
5032 if (err)
5033 goto done;
5035 (*new_te)->mode = get_ct_file_mode(ct);
5037 if (ct->staged_status == GOT_STATUS_MODIFY)
5038 memcpy(&(*new_te)->id, ct->staged_blob_id,
5039 sizeof((*new_te)->id));
5040 else
5041 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5042 done:
5043 if (err && *new_te) {
5044 free(*new_te);
5045 *new_te = NULL;
5047 return err;
5050 static const struct got_error *
5051 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5052 struct got_commitable *ct)
5054 const struct got_error *err = NULL;
5055 char *ct_name = NULL;
5057 *new_te = NULL;
5059 *new_te = calloc(1, sizeof(**new_te));
5060 if (*new_te == NULL)
5061 return got_error_from_errno("calloc");
5063 err = got_path_basename(&ct_name, ct->path);
5064 if (err)
5065 goto done;
5066 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5067 sizeof((*new_te)->name)) {
5068 err = got_error(GOT_ERR_NO_SPACE);
5069 goto done;
5072 (*new_te)->mode = get_ct_file_mode(ct);
5074 if (ct->staged_status == GOT_STATUS_ADD)
5075 memcpy(&(*new_te)->id, ct->staged_blob_id,
5076 sizeof((*new_te)->id));
5077 else
5078 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5079 done:
5080 free(ct_name);
5081 if (err && *new_te) {
5082 free(*new_te);
5083 *new_te = NULL;
5085 return err;
5088 static const struct got_error *
5089 insert_tree_entry(struct got_tree_entry *new_te,
5090 struct got_pathlist_head *paths)
5092 const struct got_error *err = NULL;
5093 struct got_pathlist_entry *new_pe;
5095 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5096 if (err)
5097 return err;
5098 if (new_pe == NULL)
5099 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5100 return NULL;
5103 static const struct got_error *
5104 report_ct_status(struct got_commitable *ct,
5105 got_worktree_status_cb status_cb, void *status_arg)
5107 const char *ct_path = ct->path;
5108 unsigned char status;
5110 if (status_cb == NULL) /* no commit progress output desired */
5111 return NULL;
5113 while (ct_path[0] == '/')
5114 ct_path++;
5116 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5117 status = ct->staged_status;
5118 else
5119 status = ct->status;
5121 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5122 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5125 static const struct got_error *
5126 match_modified_subtree(int *modified, struct got_tree_entry *te,
5127 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5129 const struct got_error *err = NULL;
5130 struct got_pathlist_entry *pe;
5131 char *te_path;
5133 *modified = 0;
5135 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5136 got_path_is_root_dir(base_tree_path) ? "" : "/",
5137 te->name) == -1)
5138 return got_error_from_errno("asprintf");
5140 TAILQ_FOREACH(pe, commitable_paths, entry) {
5141 struct got_commitable *ct = pe->data;
5142 *modified = got_path_is_child(ct->in_repo_path, te_path,
5143 strlen(te_path));
5144 if (*modified)
5145 break;
5148 free(te_path);
5149 return err;
5152 static const struct got_error *
5153 match_deleted_or_modified_ct(struct got_commitable **ctp,
5154 struct got_tree_entry *te, const char *base_tree_path,
5155 struct got_pathlist_head *commitable_paths)
5157 const struct got_error *err = NULL;
5158 struct got_pathlist_entry *pe;
5160 *ctp = NULL;
5162 TAILQ_FOREACH(pe, commitable_paths, entry) {
5163 struct got_commitable *ct = pe->data;
5164 char *ct_name = NULL;
5165 int path_matches;
5167 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5168 if (ct->status != GOT_STATUS_MODIFY &&
5169 ct->status != GOT_STATUS_MODE_CHANGE &&
5170 ct->status != GOT_STATUS_DELETE)
5171 continue;
5172 } else {
5173 if (ct->staged_status != GOT_STATUS_MODIFY &&
5174 ct->staged_status != GOT_STATUS_DELETE)
5175 continue;
5178 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5179 continue;
5181 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5182 if (err)
5183 return err;
5184 if (!path_matches)
5185 continue;
5187 err = got_path_basename(&ct_name, pe->path);
5188 if (err)
5189 return err;
5191 if (strcmp(te->name, ct_name) != 0) {
5192 free(ct_name);
5193 continue;
5195 free(ct_name);
5197 *ctp = ct;
5198 break;
5201 return err;
5204 static const struct got_error *
5205 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5206 const char *child_path, const char *path_base_tree,
5207 struct got_pathlist_head *commitable_paths,
5208 got_worktree_status_cb status_cb, void *status_arg,
5209 struct got_repository *repo)
5211 const struct got_error *err = NULL;
5212 struct got_tree_entry *new_te;
5213 char *subtree_path;
5214 struct got_object_id *id = NULL;
5215 int nentries;
5217 *new_tep = NULL;
5219 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5220 got_path_is_root_dir(path_base_tree) ? "" : "/",
5221 child_path) == -1)
5222 return got_error_from_errno("asprintf");
5224 new_te = calloc(1, sizeof(*new_te));
5225 if (new_te == NULL)
5226 return got_error_from_errno("calloc");
5227 new_te->mode = S_IFDIR;
5229 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5230 sizeof(new_te->name)) {
5231 err = got_error(GOT_ERR_NO_SPACE);
5232 goto done;
5234 err = write_tree(&id, &nentries, NULL, subtree_path,
5235 commitable_paths, status_cb, status_arg, repo);
5236 if (err) {
5237 free(new_te);
5238 goto done;
5240 memcpy(&new_te->id, id, sizeof(new_te->id));
5241 done:
5242 free(id);
5243 free(subtree_path);
5244 if (err == NULL)
5245 *new_tep = new_te;
5246 return err;
5249 static const struct got_error *
5250 write_tree(struct got_object_id **new_tree_id, int *nentries,
5251 struct got_tree_object *base_tree, const char *path_base_tree,
5252 struct got_pathlist_head *commitable_paths,
5253 got_worktree_status_cb status_cb, void *status_arg,
5254 struct got_repository *repo)
5256 const struct got_error *err = NULL;
5257 struct got_pathlist_head paths;
5258 struct got_tree_entry *te, *new_te = NULL;
5259 struct got_pathlist_entry *pe;
5261 TAILQ_INIT(&paths);
5262 *nentries = 0;
5264 /* Insert, and recurse into, newly added entries first. */
5265 TAILQ_FOREACH(pe, commitable_paths, entry) {
5266 struct got_commitable *ct = pe->data;
5267 char *child_path = NULL, *slash;
5269 if ((ct->status != GOT_STATUS_ADD &&
5270 ct->staged_status != GOT_STATUS_ADD) ||
5271 (ct->flags & GOT_COMMITABLE_ADDED))
5272 continue;
5274 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5275 strlen(path_base_tree)))
5276 continue;
5278 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5279 ct->in_repo_path);
5280 if (err)
5281 goto done;
5283 slash = strchr(child_path, '/');
5284 if (slash == NULL) {
5285 err = alloc_added_blob_tree_entry(&new_te, ct);
5286 if (err)
5287 goto done;
5288 err = report_ct_status(ct, status_cb, status_arg);
5289 if (err)
5290 goto done;
5291 ct->flags |= GOT_COMMITABLE_ADDED;
5292 err = insert_tree_entry(new_te, &paths);
5293 if (err)
5294 goto done;
5295 (*nentries)++;
5296 } else {
5297 *slash = '\0'; /* trim trailing path components */
5298 if (base_tree == NULL ||
5299 got_object_tree_find_entry(base_tree, child_path)
5300 == NULL) {
5301 err = make_subtree_for_added_blob(&new_te,
5302 child_path, path_base_tree,
5303 commitable_paths, status_cb, status_arg,
5304 repo);
5305 if (err)
5306 goto done;
5307 err = insert_tree_entry(new_te, &paths);
5308 if (err)
5309 goto done;
5310 (*nentries)++;
5315 if (base_tree) {
5316 int i, nbase_entries;
5317 /* Handle modified and deleted entries. */
5318 nbase_entries = got_object_tree_get_nentries(base_tree);
5319 for (i = 0; i < nbase_entries; i++) {
5320 struct got_commitable *ct = NULL;
5322 te = got_object_tree_get_entry(base_tree, i);
5323 if (got_object_tree_entry_is_submodule(te)) {
5324 /* Entry is a submodule; just copy it. */
5325 err = got_object_tree_entry_dup(&new_te, te);
5326 if (err)
5327 goto done;
5328 err = insert_tree_entry(new_te, &paths);
5329 if (err)
5330 goto done;
5331 (*nentries)++;
5332 continue;
5335 if (S_ISDIR(te->mode)) {
5336 int modified;
5337 err = got_object_tree_entry_dup(&new_te, te);
5338 if (err)
5339 goto done;
5340 err = match_modified_subtree(&modified, te,
5341 path_base_tree, commitable_paths);
5342 if (err)
5343 goto done;
5344 /* Avoid recursion into unmodified subtrees. */
5345 if (modified) {
5346 struct got_object_id *new_id;
5347 int nsubentries;
5348 err = write_subtree(&new_id,
5349 &nsubentries, te,
5350 path_base_tree, commitable_paths,
5351 status_cb, status_arg, repo);
5352 if (err)
5353 goto done;
5354 if (nsubentries == 0) {
5355 /* All entries were deleted. */
5356 free(new_id);
5357 continue;
5359 memcpy(&new_te->id, new_id,
5360 sizeof(new_te->id));
5361 free(new_id);
5363 err = insert_tree_entry(new_te, &paths);
5364 if (err)
5365 goto done;
5366 (*nentries)++;
5367 continue;
5370 err = match_deleted_or_modified_ct(&ct, te,
5371 path_base_tree, commitable_paths);
5372 if (err)
5373 goto done;
5374 if (ct) {
5375 /* NB: Deleted entries get dropped here. */
5376 if (ct->status == GOT_STATUS_MODIFY ||
5377 ct->status == GOT_STATUS_MODE_CHANGE ||
5378 ct->staged_status == GOT_STATUS_MODIFY) {
5379 err = alloc_modified_blob_tree_entry(
5380 &new_te, te, ct);
5381 if (err)
5382 goto done;
5383 err = insert_tree_entry(new_te, &paths);
5384 if (err)
5385 goto done;
5386 (*nentries)++;
5388 err = report_ct_status(ct, status_cb,
5389 status_arg);
5390 if (err)
5391 goto done;
5392 } else {
5393 /* Entry is unchanged; just copy it. */
5394 err = got_object_tree_entry_dup(&new_te, te);
5395 if (err)
5396 goto done;
5397 err = insert_tree_entry(new_te, &paths);
5398 if (err)
5399 goto done;
5400 (*nentries)++;
5405 /* Write new list of entries; deleted entries have been dropped. */
5406 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5407 done:
5408 got_pathlist_free(&paths);
5409 return err;
5412 static const struct got_error *
5413 update_fileindex_after_commit(struct got_worktree *worktree,
5414 struct got_pathlist_head *commitable_paths,
5415 struct got_object_id *new_base_commit_id,
5416 struct got_fileindex *fileindex, int have_staged_files)
5418 const struct got_error *err = NULL;
5419 struct got_pathlist_entry *pe;
5420 char *relpath = NULL;
5422 TAILQ_FOREACH(pe, commitable_paths, entry) {
5423 struct got_fileindex_entry *ie;
5424 struct got_commitable *ct = pe->data;
5426 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5428 err = got_path_skip_common_ancestor(&relpath,
5429 worktree->root_path, ct->ondisk_path);
5430 if (err)
5431 goto done;
5433 if (ie) {
5434 if (ct->status == GOT_STATUS_DELETE ||
5435 ct->staged_status == GOT_STATUS_DELETE) {
5436 got_fileindex_entry_remove(fileindex, ie);
5437 } else if (ct->staged_status == GOT_STATUS_ADD ||
5438 ct->staged_status == GOT_STATUS_MODIFY) {
5439 got_fileindex_entry_stage_set(ie,
5440 GOT_FILEIDX_STAGE_NONE);
5441 got_fileindex_entry_staged_filetype_set(ie, 0);
5443 err = got_fileindex_entry_update(ie,
5444 worktree->root_fd, relpath,
5445 ct->staged_blob_id->sha1,
5446 new_base_commit_id->sha1,
5447 !have_staged_files);
5448 } else
5449 err = got_fileindex_entry_update(ie,
5450 worktree->root_fd, relpath,
5451 ct->blob_id->sha1,
5452 new_base_commit_id->sha1,
5453 !have_staged_files);
5454 } else {
5455 err = got_fileindex_entry_alloc(&ie, pe->path);
5456 if (err)
5457 goto done;
5458 err = got_fileindex_entry_update(ie,
5459 worktree->root_fd, relpath, ct->blob_id->sha1,
5460 new_base_commit_id->sha1, 1);
5461 if (err) {
5462 got_fileindex_entry_free(ie);
5463 goto done;
5465 err = got_fileindex_entry_add(fileindex, ie);
5466 if (err) {
5467 got_fileindex_entry_free(ie);
5468 goto done;
5471 free(relpath);
5472 relpath = NULL;
5474 done:
5475 free(relpath);
5476 return err;
5480 static const struct got_error *
5481 check_out_of_date(const char *in_repo_path, unsigned char status,
5482 unsigned char staged_status, struct got_object_id *base_blob_id,
5483 struct got_object_id *base_commit_id,
5484 struct got_object_id *head_commit_id, struct got_repository *repo,
5485 int ood_errcode)
5487 const struct got_error *err = NULL;
5488 struct got_object_id *id = NULL;
5490 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5491 /* Trivial case: base commit == head commit */
5492 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5493 return NULL;
5495 * Ensure file content which local changes were based
5496 * on matches file content in the branch head.
5498 err = got_object_id_by_path(&id, repo, head_commit_id,
5499 in_repo_path);
5500 if (err) {
5501 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5502 err = got_error(ood_errcode);
5503 goto done;
5504 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5505 err = got_error(ood_errcode);
5506 } else {
5507 /* Require that added files don't exist in the branch head. */
5508 err = got_object_id_by_path(&id, repo, head_commit_id,
5509 in_repo_path);
5510 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5511 goto done;
5512 err = id ? got_error(ood_errcode) : NULL;
5514 done:
5515 free(id);
5516 return err;
5519 const struct got_error *
5520 commit_worktree(struct got_object_id **new_commit_id,
5521 struct got_pathlist_head *commitable_paths,
5522 struct got_object_id *head_commit_id,
5523 struct got_object_id *parent_id2,
5524 struct got_worktree *worktree,
5525 const char *author, const char *committer,
5526 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5527 got_worktree_status_cb status_cb, void *status_arg,
5528 struct got_repository *repo)
5530 const struct got_error *err = NULL, *unlockerr = NULL;
5531 struct got_pathlist_entry *pe;
5532 const char *head_ref_name = NULL;
5533 struct got_commit_object *head_commit = NULL;
5534 struct got_reference *head_ref2 = NULL;
5535 struct got_object_id *head_commit_id2 = NULL;
5536 struct got_tree_object *head_tree = NULL;
5537 struct got_object_id *new_tree_id = NULL;
5538 int nentries, nparents = 0;
5539 struct got_object_id_queue parent_ids;
5540 struct got_object_qid *pid = NULL;
5541 char *logmsg = NULL;
5543 *new_commit_id = NULL;
5545 STAILQ_INIT(&parent_ids);
5547 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5548 if (err)
5549 goto done;
5551 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5552 if (err)
5553 goto done;
5555 if (commit_msg_cb != NULL) {
5556 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5557 if (err)
5558 goto done;
5561 if (logmsg == NULL || strlen(logmsg) == 0) {
5562 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5563 goto done;
5566 /* Create blobs from added and modified files and record their IDs. */
5567 TAILQ_FOREACH(pe, commitable_paths, entry) {
5568 struct got_commitable *ct = pe->data;
5569 char *ondisk_path;
5571 /* Blobs for staged files already exist. */
5572 if (ct->staged_status == GOT_STATUS_ADD ||
5573 ct->staged_status == GOT_STATUS_MODIFY)
5574 continue;
5576 if (ct->status != GOT_STATUS_ADD &&
5577 ct->status != GOT_STATUS_MODIFY &&
5578 ct->status != GOT_STATUS_MODE_CHANGE)
5579 continue;
5581 if (asprintf(&ondisk_path, "%s/%s",
5582 worktree->root_path, pe->path) == -1) {
5583 err = got_error_from_errno("asprintf");
5584 goto done;
5586 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5587 free(ondisk_path);
5588 if (err)
5589 goto done;
5592 /* Recursively write new tree objects. */
5593 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5594 commitable_paths, status_cb, status_arg, repo);
5595 if (err)
5596 goto done;
5598 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5599 if (err)
5600 goto done;
5601 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5602 nparents++;
5603 if (parent_id2) {
5604 err = got_object_qid_alloc(&pid, parent_id2);
5605 if (err)
5606 goto done;
5607 STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5608 nparents++;
5610 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5611 nparents, author, time(NULL), committer, time(NULL), logmsg, repo);
5612 if (logmsg != NULL)
5613 free(logmsg);
5614 if (err)
5615 goto done;
5617 /* Check if a concurrent commit to our branch has occurred. */
5618 head_ref_name = got_worktree_get_head_ref_name(worktree);
5619 if (head_ref_name == NULL) {
5620 err = got_error_from_errno("got_worktree_get_head_ref_name");
5621 goto done;
5623 /* Lock the reference here to prevent concurrent modification. */
5624 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5625 if (err)
5626 goto done;
5627 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5628 if (err)
5629 goto done;
5630 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5631 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5632 goto done;
5634 /* Update branch head in repository. */
5635 err = got_ref_change_ref(head_ref2, *new_commit_id);
5636 if (err)
5637 goto done;
5638 err = got_ref_write(head_ref2, repo);
5639 if (err)
5640 goto done;
5642 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5643 if (err)
5644 goto done;
5646 err = ref_base_commit(worktree, repo);
5647 if (err)
5648 goto done;
5649 done:
5650 got_object_id_queue_free(&parent_ids);
5651 if (head_tree)
5652 got_object_tree_close(head_tree);
5653 if (head_commit)
5654 got_object_commit_close(head_commit);
5655 free(head_commit_id2);
5656 if (head_ref2) {
5657 unlockerr = got_ref_unlock(head_ref2);
5658 if (unlockerr && err == NULL)
5659 err = unlockerr;
5660 got_ref_close(head_ref2);
5662 return err;
5665 static const struct got_error *
5666 check_path_is_commitable(const char *path,
5667 struct got_pathlist_head *commitable_paths)
5669 struct got_pathlist_entry *cpe = NULL;
5670 size_t path_len = strlen(path);
5672 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5673 struct got_commitable *ct = cpe->data;
5674 const char *ct_path = ct->path;
5676 while (ct_path[0] == '/')
5677 ct_path++;
5679 if (strcmp(path, ct_path) == 0 ||
5680 got_path_is_child(ct_path, path, path_len))
5681 break;
5684 if (cpe == NULL)
5685 return got_error_path(path, GOT_ERR_BAD_PATH);
5687 return NULL;
5690 static const struct got_error *
5691 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5693 int *have_staged_files = arg;
5695 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5696 *have_staged_files = 1;
5697 return got_error(GOT_ERR_CANCELLED);
5700 return NULL;
5703 static const struct got_error *
5704 check_non_staged_files(struct got_fileindex *fileindex,
5705 struct got_pathlist_head *paths)
5707 struct got_pathlist_entry *pe;
5708 struct got_fileindex_entry *ie;
5710 TAILQ_FOREACH(pe, paths, entry) {
5711 if (pe->path[0] == '\0')
5712 continue;
5713 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5714 if (ie == NULL)
5715 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5716 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5717 return got_error_path(pe->path,
5718 GOT_ERR_FILE_NOT_STAGED);
5721 return NULL;
5724 const struct got_error *
5725 got_worktree_commit(struct got_object_id **new_commit_id,
5726 struct got_worktree *worktree, struct got_pathlist_head *paths,
5727 const char *author, const char *committer, int allow_bad_symlinks,
5728 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5729 got_worktree_status_cb status_cb, void *status_arg,
5730 struct got_repository *repo)
5732 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5733 struct got_fileindex *fileindex = NULL;
5734 char *fileindex_path = NULL;
5735 struct got_pathlist_head commitable_paths;
5736 struct collect_commitables_arg cc_arg;
5737 struct got_pathlist_entry *pe;
5738 struct got_reference *head_ref = NULL;
5739 struct got_object_id *head_commit_id = NULL;
5740 int have_staged_files = 0;
5742 *new_commit_id = NULL;
5744 TAILQ_INIT(&commitable_paths);
5746 err = lock_worktree(worktree, LOCK_EX);
5747 if (err)
5748 goto done;
5750 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5751 if (err)
5752 goto done;
5754 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5755 if (err)
5756 goto done;
5758 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5759 if (err)
5760 goto done;
5762 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5763 &have_staged_files);
5764 if (err && err->code != GOT_ERR_CANCELLED)
5765 goto done;
5766 if (have_staged_files) {
5767 err = check_non_staged_files(fileindex, paths);
5768 if (err)
5769 goto done;
5772 cc_arg.commitable_paths = &commitable_paths;
5773 cc_arg.worktree = worktree;
5774 cc_arg.fileindex = fileindex;
5775 cc_arg.repo = repo;
5776 cc_arg.have_staged_files = have_staged_files;
5777 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5778 TAILQ_FOREACH(pe, paths, entry) {
5779 err = worktree_status(worktree, pe->path, fileindex, repo,
5780 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
5781 if (err)
5782 goto done;
5785 if (TAILQ_EMPTY(&commitable_paths)) {
5786 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5787 goto done;
5790 TAILQ_FOREACH(pe, paths, entry) {
5791 err = check_path_is_commitable(pe->path, &commitable_paths);
5792 if (err)
5793 goto done;
5796 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5797 struct got_commitable *ct = pe->data;
5798 const char *ct_path = ct->in_repo_path;
5800 while (ct_path[0] == '/')
5801 ct_path++;
5802 err = check_out_of_date(ct_path, ct->status,
5803 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5804 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5805 if (err)
5806 goto done;
5810 err = commit_worktree(new_commit_id, &commitable_paths,
5811 head_commit_id, NULL, worktree, author, committer,
5812 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5813 if (err)
5814 goto done;
5816 err = update_fileindex_after_commit(worktree, &commitable_paths,
5817 *new_commit_id, fileindex, have_staged_files);
5818 sync_err = sync_fileindex(fileindex, fileindex_path);
5819 if (sync_err && err == NULL)
5820 err = sync_err;
5821 done:
5822 if (fileindex)
5823 got_fileindex_free(fileindex);
5824 free(fileindex_path);
5825 unlockerr = lock_worktree(worktree, LOCK_SH);
5826 if (unlockerr && err == NULL)
5827 err = unlockerr;
5828 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5829 struct got_commitable *ct = pe->data;
5830 free_commitable(ct);
5832 got_pathlist_free(&commitable_paths);
5833 return err;
5836 const char *
5837 got_commitable_get_path(struct got_commitable *ct)
5839 return ct->path;
5842 unsigned int
5843 got_commitable_get_status(struct got_commitable *ct)
5845 return ct->status;
5848 struct check_rebase_ok_arg {
5849 struct got_worktree *worktree;
5850 struct got_repository *repo;
5853 static const struct got_error *
5854 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5856 const struct got_error *err = NULL;
5857 struct check_rebase_ok_arg *a = arg;
5858 unsigned char status;
5859 struct stat sb;
5860 char *ondisk_path;
5862 /* Reject rebase of a work tree with mixed base commits. */
5863 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5864 SHA1_DIGEST_LENGTH))
5865 return got_error(GOT_ERR_MIXED_COMMITS);
5867 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5868 == -1)
5869 return got_error_from_errno("asprintf");
5871 /* Reject rebase of a work tree with modified or staged files. */
5872 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5873 free(ondisk_path);
5874 if (err)
5875 return err;
5877 if (status != GOT_STATUS_NO_CHANGE)
5878 return got_error(GOT_ERR_MODIFIED);
5879 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5880 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5882 return NULL;
5885 const struct got_error *
5886 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5887 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5888 struct got_worktree *worktree, struct got_reference *branch,
5889 struct got_repository *repo)
5891 const struct got_error *err = NULL;
5892 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5893 char *branch_ref_name = NULL;
5894 char *fileindex_path = NULL;
5895 struct check_rebase_ok_arg ok_arg;
5896 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5897 struct got_object_id *wt_branch_tip = NULL;
5899 *new_base_branch_ref = NULL;
5900 *tmp_branch = NULL;
5901 *fileindex = NULL;
5903 err = lock_worktree(worktree, LOCK_EX);
5904 if (err)
5905 return err;
5907 err = open_fileindex(fileindex, &fileindex_path, worktree);
5908 if (err)
5909 goto done;
5911 ok_arg.worktree = worktree;
5912 ok_arg.repo = repo;
5913 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5914 &ok_arg);
5915 if (err)
5916 goto done;
5918 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5919 if (err)
5920 goto done;
5922 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5923 if (err)
5924 goto done;
5926 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5927 if (err)
5928 goto done;
5930 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5931 0);
5932 if (err)
5933 goto done;
5935 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5936 if (err)
5937 goto done;
5938 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5939 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5940 goto done;
5943 err = got_ref_alloc_symref(new_base_branch_ref,
5944 new_base_branch_ref_name, wt_branch);
5945 if (err)
5946 goto done;
5947 err = got_ref_write(*new_base_branch_ref, repo);
5948 if (err)
5949 goto done;
5951 /* TODO Lock original branch's ref while rebasing? */
5953 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5954 if (err)
5955 goto done;
5957 err = got_ref_write(branch_ref, repo);
5958 if (err)
5959 goto done;
5961 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5962 worktree->base_commit_id);
5963 if (err)
5964 goto done;
5965 err = got_ref_write(*tmp_branch, repo);
5966 if (err)
5967 goto done;
5969 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5970 if (err)
5971 goto done;
5972 done:
5973 free(fileindex_path);
5974 free(tmp_branch_name);
5975 free(new_base_branch_ref_name);
5976 free(branch_ref_name);
5977 if (branch_ref)
5978 got_ref_close(branch_ref);
5979 if (wt_branch)
5980 got_ref_close(wt_branch);
5981 free(wt_branch_tip);
5982 if (err) {
5983 if (*new_base_branch_ref) {
5984 got_ref_close(*new_base_branch_ref);
5985 *new_base_branch_ref = NULL;
5987 if (*tmp_branch) {
5988 got_ref_close(*tmp_branch);
5989 *tmp_branch = NULL;
5991 if (*fileindex) {
5992 got_fileindex_free(*fileindex);
5993 *fileindex = NULL;
5995 lock_worktree(worktree, LOCK_SH);
5997 return err;
6000 const struct got_error *
6001 got_worktree_rebase_continue(struct got_object_id **commit_id,
6002 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6003 struct got_reference **branch, struct got_fileindex **fileindex,
6004 struct got_worktree *worktree, struct got_repository *repo)
6006 const struct got_error *err;
6007 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6008 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6009 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6010 char *fileindex_path = NULL;
6011 int have_staged_files = 0;
6013 *commit_id = NULL;
6014 *new_base_branch = NULL;
6015 *tmp_branch = NULL;
6016 *branch = NULL;
6017 *fileindex = NULL;
6019 err = lock_worktree(worktree, LOCK_EX);
6020 if (err)
6021 return err;
6023 err = open_fileindex(fileindex, &fileindex_path, worktree);
6024 if (err)
6025 goto done;
6027 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6028 &have_staged_files);
6029 if (err && err->code != GOT_ERR_CANCELLED)
6030 goto done;
6031 if (have_staged_files) {
6032 err = got_error(GOT_ERR_STAGED_PATHS);
6033 goto done;
6036 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6037 if (err)
6038 goto done;
6040 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6041 if (err)
6042 goto done;
6044 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6045 if (err)
6046 goto done;
6048 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6049 if (err)
6050 goto done;
6052 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6053 if (err)
6054 goto done;
6056 err = got_ref_open(branch, repo,
6057 got_ref_get_symref_target(branch_ref), 0);
6058 if (err)
6059 goto done;
6061 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6062 if (err)
6063 goto done;
6065 err = got_ref_resolve(commit_id, repo, commit_ref);
6066 if (err)
6067 goto done;
6069 err = got_ref_open(new_base_branch, repo,
6070 new_base_branch_ref_name, 0);
6071 if (err)
6072 goto done;
6074 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6075 if (err)
6076 goto done;
6077 done:
6078 free(commit_ref_name);
6079 free(branch_ref_name);
6080 free(fileindex_path);
6081 if (commit_ref)
6082 got_ref_close(commit_ref);
6083 if (branch_ref)
6084 got_ref_close(branch_ref);
6085 if (err) {
6086 free(*commit_id);
6087 *commit_id = NULL;
6088 if (*tmp_branch) {
6089 got_ref_close(*tmp_branch);
6090 *tmp_branch = NULL;
6092 if (*new_base_branch) {
6093 got_ref_close(*new_base_branch);
6094 *new_base_branch = NULL;
6096 if (*branch) {
6097 got_ref_close(*branch);
6098 *branch = NULL;
6100 if (*fileindex) {
6101 got_fileindex_free(*fileindex);
6102 *fileindex = NULL;
6104 lock_worktree(worktree, LOCK_SH);
6106 return err;
6109 const struct got_error *
6110 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6112 const struct got_error *err;
6113 char *tmp_branch_name = NULL;
6115 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6116 if (err)
6117 return err;
6119 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6120 free(tmp_branch_name);
6121 return NULL;
6124 static const struct got_error *
6125 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6126 char **logmsg, void *arg)
6128 *logmsg = arg;
6129 return NULL;
6132 static const struct got_error *
6133 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6134 const char *path, struct got_object_id *blob_id,
6135 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6136 int dirfd, const char *de_name)
6138 return NULL;
6141 struct collect_merged_paths_arg {
6142 got_worktree_checkout_cb progress_cb;
6143 void *progress_arg;
6144 struct got_pathlist_head *merged_paths;
6147 static const struct got_error *
6148 collect_merged_paths(void *arg, unsigned char status, const char *path)
6150 const struct got_error *err;
6151 struct collect_merged_paths_arg *a = arg;
6152 char *p;
6153 struct got_pathlist_entry *new;
6155 err = (*a->progress_cb)(a->progress_arg, status, path);
6156 if (err)
6157 return err;
6159 if (status != GOT_STATUS_MERGE &&
6160 status != GOT_STATUS_ADD &&
6161 status != GOT_STATUS_DELETE &&
6162 status != GOT_STATUS_CONFLICT)
6163 return NULL;
6165 p = strdup(path);
6166 if (p == NULL)
6167 return got_error_from_errno("strdup");
6169 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6170 if (err || new == NULL)
6171 free(p);
6172 return err;
6175 void
6176 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6178 struct got_pathlist_entry *pe;
6180 TAILQ_FOREACH(pe, merged_paths, entry)
6181 free((char *)pe->path);
6183 got_pathlist_free(merged_paths);
6186 static const struct got_error *
6187 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6188 int is_rebase, struct got_repository *repo)
6190 const struct got_error *err;
6191 struct got_reference *commit_ref = NULL;
6193 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6194 if (err) {
6195 if (err->code != GOT_ERR_NOT_REF)
6196 goto done;
6197 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6198 if (err)
6199 goto done;
6200 err = got_ref_write(commit_ref, repo);
6201 if (err)
6202 goto done;
6203 } else if (is_rebase) {
6204 struct got_object_id *stored_id;
6205 int cmp;
6207 err = got_ref_resolve(&stored_id, repo, commit_ref);
6208 if (err)
6209 goto done;
6210 cmp = got_object_id_cmp(commit_id, stored_id);
6211 free(stored_id);
6212 if (cmp != 0) {
6213 err = got_error(GOT_ERR_REBASE_COMMITID);
6214 goto done;
6217 done:
6218 if (commit_ref)
6219 got_ref_close(commit_ref);
6220 return err;
6223 static const struct got_error *
6224 rebase_merge_files(struct got_pathlist_head *merged_paths,
6225 const char *commit_ref_name, struct got_worktree *worktree,
6226 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6227 struct got_object_id *commit_id, struct got_repository *repo,
6228 got_worktree_checkout_cb progress_cb, void *progress_arg,
6229 got_cancel_cb cancel_cb, void *cancel_arg)
6231 const struct got_error *err;
6232 struct got_reference *commit_ref = NULL;
6233 struct collect_merged_paths_arg cmp_arg;
6234 char *fileindex_path;
6236 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6238 err = get_fileindex_path(&fileindex_path, worktree);
6239 if (err)
6240 return err;
6242 cmp_arg.progress_cb = progress_cb;
6243 cmp_arg.progress_arg = progress_arg;
6244 cmp_arg.merged_paths = merged_paths;
6245 err = merge_files(worktree, fileindex, fileindex_path,
6246 parent_commit_id, commit_id, repo, collect_merged_paths,
6247 &cmp_arg, cancel_cb, cancel_arg);
6248 if (commit_ref)
6249 got_ref_close(commit_ref);
6250 return err;
6253 const struct got_error *
6254 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6255 struct got_worktree *worktree, struct got_fileindex *fileindex,
6256 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6257 struct got_repository *repo,
6258 got_worktree_checkout_cb progress_cb, void *progress_arg,
6259 got_cancel_cb cancel_cb, void *cancel_arg)
6261 const struct got_error *err;
6262 char *commit_ref_name;
6264 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6265 if (err)
6266 return err;
6268 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6269 if (err)
6270 goto done;
6272 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6273 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6274 progress_arg, cancel_cb, cancel_arg);
6275 done:
6276 free(commit_ref_name);
6277 return err;
6280 const struct got_error *
6281 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6282 struct got_worktree *worktree, struct got_fileindex *fileindex,
6283 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6284 struct got_repository *repo,
6285 got_worktree_checkout_cb progress_cb, void *progress_arg,
6286 got_cancel_cb cancel_cb, void *cancel_arg)
6288 const struct got_error *err;
6289 char *commit_ref_name;
6291 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6292 if (err)
6293 return err;
6295 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6296 if (err)
6297 goto done;
6299 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6300 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6301 progress_arg, cancel_cb, cancel_arg);
6302 done:
6303 free(commit_ref_name);
6304 return err;
6307 static const struct got_error *
6308 rebase_commit(struct got_object_id **new_commit_id,
6309 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6310 struct got_worktree *worktree, struct got_fileindex *fileindex,
6311 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6312 const char *new_logmsg, struct got_repository *repo)
6314 const struct got_error *err, *sync_err;
6315 struct got_pathlist_head commitable_paths;
6316 struct collect_commitables_arg cc_arg;
6317 char *fileindex_path = NULL;
6318 struct got_reference *head_ref = NULL;
6319 struct got_object_id *head_commit_id = NULL;
6320 char *logmsg = NULL;
6322 TAILQ_INIT(&commitable_paths);
6323 *new_commit_id = NULL;
6325 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6327 err = get_fileindex_path(&fileindex_path, worktree);
6328 if (err)
6329 return err;
6331 cc_arg.commitable_paths = &commitable_paths;
6332 cc_arg.worktree = worktree;
6333 cc_arg.repo = repo;
6334 cc_arg.have_staged_files = 0;
6336 * If possible get the status of individual files directly to
6337 * avoid crawling the entire work tree once per rebased commit.
6339 * Ideally, merged_paths would contain a list of commitables
6340 * we could use so we could skip worktree_status() entirely.
6341 * However, we would then need carefully keep track of cumulative
6342 * effects of operations such as file additions and deletions
6343 * in 'got histedit -f' (folding multiple commits into one),
6344 * and this extra complexity is not really worth it.
6346 if (merged_paths) {
6347 struct got_pathlist_entry *pe;
6348 TAILQ_FOREACH(pe, merged_paths, entry) {
6349 err = worktree_status(worktree, pe->path, fileindex,
6350 repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6351 0);
6352 if (err)
6353 goto done;
6355 } else {
6356 err = worktree_status(worktree, "", fileindex, repo,
6357 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6358 if (err)
6359 goto done;
6362 if (TAILQ_EMPTY(&commitable_paths)) {
6363 /* No-op change; commit will be elided. */
6364 err = got_ref_delete(commit_ref, repo);
6365 if (err)
6366 goto done;
6367 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6368 goto done;
6371 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6372 if (err)
6373 goto done;
6375 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6376 if (err)
6377 goto done;
6379 if (new_logmsg) {
6380 logmsg = strdup(new_logmsg);
6381 if (logmsg == NULL) {
6382 err = got_error_from_errno("strdup");
6383 goto done;
6385 } else {
6386 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6387 if (err)
6388 goto done;
6391 /* NB: commit_worktree will call free(logmsg) */
6392 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6393 NULL, worktree, got_object_commit_get_author(orig_commit),
6394 got_object_commit_get_committer(orig_commit),
6395 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6396 if (err)
6397 goto done;
6399 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6400 if (err)
6401 goto done;
6403 err = got_ref_delete(commit_ref, repo);
6404 if (err)
6405 goto done;
6407 err = update_fileindex_after_commit(worktree, &commitable_paths,
6408 *new_commit_id, fileindex, 0);
6409 sync_err = sync_fileindex(fileindex, fileindex_path);
6410 if (sync_err && err == NULL)
6411 err = sync_err;
6412 done:
6413 free(fileindex_path);
6414 free(head_commit_id);
6415 if (head_ref)
6416 got_ref_close(head_ref);
6417 if (err) {
6418 free(*new_commit_id);
6419 *new_commit_id = NULL;
6421 return err;
6424 const struct got_error *
6425 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6426 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6427 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6428 struct got_commit_object *orig_commit,
6429 struct got_object_id *orig_commit_id, struct got_repository *repo)
6431 const struct got_error *err;
6432 char *commit_ref_name;
6433 struct got_reference *commit_ref = NULL;
6434 struct got_object_id *commit_id = NULL;
6436 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6437 if (err)
6438 return err;
6440 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6441 if (err)
6442 goto done;
6443 err = got_ref_resolve(&commit_id, repo, commit_ref);
6444 if (err)
6445 goto done;
6446 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6447 err = got_error(GOT_ERR_REBASE_COMMITID);
6448 goto done;
6451 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6452 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6453 done:
6454 if (commit_ref)
6455 got_ref_close(commit_ref);
6456 free(commit_ref_name);
6457 free(commit_id);
6458 return err;
6461 const struct got_error *
6462 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6463 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6464 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6465 struct got_commit_object *orig_commit,
6466 struct got_object_id *orig_commit_id, const char *new_logmsg,
6467 struct got_repository *repo)
6469 const struct got_error *err;
6470 char *commit_ref_name;
6471 struct got_reference *commit_ref = NULL;
6473 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6474 if (err)
6475 return err;
6477 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6478 if (err)
6479 goto done;
6481 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6482 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6483 done:
6484 if (commit_ref)
6485 got_ref_close(commit_ref);
6486 free(commit_ref_name);
6487 return err;
6490 const struct got_error *
6491 got_worktree_rebase_postpone(struct got_worktree *worktree,
6492 struct got_fileindex *fileindex)
6494 if (fileindex)
6495 got_fileindex_free(fileindex);
6496 return lock_worktree(worktree, LOCK_SH);
6499 static const struct got_error *
6500 delete_ref(const char *name, struct got_repository *repo)
6502 const struct got_error *err;
6503 struct got_reference *ref;
6505 err = got_ref_open(&ref, repo, name, 0);
6506 if (err) {
6507 if (err->code == GOT_ERR_NOT_REF)
6508 return NULL;
6509 return err;
6512 err = got_ref_delete(ref, repo);
6513 got_ref_close(ref);
6514 return err;
6517 static const struct got_error *
6518 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6520 const struct got_error *err;
6521 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6522 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6524 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6525 if (err)
6526 goto done;
6527 err = delete_ref(tmp_branch_name, repo);
6528 if (err)
6529 goto done;
6531 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6532 if (err)
6533 goto done;
6534 err = delete_ref(new_base_branch_ref_name, repo);
6535 if (err)
6536 goto done;
6538 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6539 if (err)
6540 goto done;
6541 err = delete_ref(branch_ref_name, repo);
6542 if (err)
6543 goto done;
6545 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6546 if (err)
6547 goto done;
6548 err = delete_ref(commit_ref_name, repo);
6549 if (err)
6550 goto done;
6552 done:
6553 free(tmp_branch_name);
6554 free(new_base_branch_ref_name);
6555 free(branch_ref_name);
6556 free(commit_ref_name);
6557 return err;
6560 const struct got_error *
6561 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6562 struct got_object_id *new_commit_id, struct got_repository *repo)
6564 const struct got_error *err;
6565 struct got_reference *ref = NULL;
6566 struct got_object_id *old_commit_id = NULL;
6567 const char *branch_name = NULL;
6568 char *new_id_str = NULL;
6569 char *refname = NULL;
6571 branch_name = got_ref_get_name(branch);
6572 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6573 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6574 branch_name += 11;
6576 err = got_object_id_str(&new_id_str, new_commit_id);
6577 if (err)
6578 return err;
6580 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6581 new_id_str) == -1) {
6582 err = got_error_from_errno("asprintf");
6583 goto done;
6586 err = got_ref_resolve(&old_commit_id, repo, branch);
6587 if (err)
6588 goto done;
6590 err = got_ref_alloc(&ref, refname, old_commit_id);
6591 if (err)
6592 goto done;
6594 err = got_ref_write(ref, repo);
6595 done:
6596 free(new_id_str);
6597 free(refname);
6598 free(old_commit_id);
6599 if (ref)
6600 got_ref_close(ref);
6601 return err;
6604 const struct got_error *
6605 got_worktree_rebase_complete(struct got_worktree *worktree,
6606 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6607 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6608 struct got_repository *repo, int create_backup)
6610 const struct got_error *err, *unlockerr, *sync_err;
6611 struct got_object_id *new_head_commit_id = NULL;
6612 char *fileindex_path = NULL;
6614 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6615 if (err)
6616 return err;
6618 if (create_backup) {
6619 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6620 rebased_branch, new_head_commit_id, repo);
6621 if (err)
6622 goto done;
6625 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6626 if (err)
6627 goto done;
6629 err = got_ref_write(rebased_branch, repo);
6630 if (err)
6631 goto done;
6633 err = got_worktree_set_head_ref(worktree, rebased_branch);
6634 if (err)
6635 goto done;
6637 err = delete_rebase_refs(worktree, repo);
6638 if (err)
6639 goto done;
6641 err = get_fileindex_path(&fileindex_path, worktree);
6642 if (err)
6643 goto done;
6644 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6645 sync_err = sync_fileindex(fileindex, fileindex_path);
6646 if (sync_err && err == NULL)
6647 err = sync_err;
6648 done:
6649 got_fileindex_free(fileindex);
6650 free(fileindex_path);
6651 free(new_head_commit_id);
6652 unlockerr = lock_worktree(worktree, LOCK_SH);
6653 if (unlockerr && err == NULL)
6654 err = unlockerr;
6655 return err;
6658 const struct got_error *
6659 got_worktree_rebase_abort(struct got_worktree *worktree,
6660 struct got_fileindex *fileindex, struct got_repository *repo,
6661 struct got_reference *new_base_branch,
6662 got_worktree_checkout_cb progress_cb, void *progress_arg)
6664 const struct got_error *err, *unlockerr, *sync_err;
6665 struct got_reference *resolved = NULL;
6666 struct got_object_id *commit_id = NULL;
6667 char *fileindex_path = NULL;
6668 struct revert_file_args rfa;
6669 struct got_object_id *tree_id = NULL;
6671 err = lock_worktree(worktree, LOCK_EX);
6672 if (err)
6673 return err;
6675 err = got_ref_open(&resolved, repo,
6676 got_ref_get_symref_target(new_base_branch), 0);
6677 if (err)
6678 goto done;
6680 err = got_worktree_set_head_ref(worktree, resolved);
6681 if (err)
6682 goto done;
6685 * XXX commits to the base branch could have happened while
6686 * we were busy rebasing; should we store the original commit ID
6687 * when rebase begins and read it back here?
6689 err = got_ref_resolve(&commit_id, repo, resolved);
6690 if (err)
6691 goto done;
6693 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6694 if (err)
6695 goto done;
6697 err = got_object_id_by_path(&tree_id, repo,
6698 worktree->base_commit_id, worktree->path_prefix);
6699 if (err)
6700 goto done;
6702 err = delete_rebase_refs(worktree, repo);
6703 if (err)
6704 goto done;
6706 err = get_fileindex_path(&fileindex_path, worktree);
6707 if (err)
6708 goto done;
6710 rfa.worktree = worktree;
6711 rfa.fileindex = fileindex;
6712 rfa.progress_cb = progress_cb;
6713 rfa.progress_arg = progress_arg;
6714 rfa.patch_cb = NULL;
6715 rfa.patch_arg = NULL;
6716 rfa.repo = repo;
6717 rfa.unlink_added_files = 0;
6718 err = worktree_status(worktree, "", fileindex, repo,
6719 revert_file, &rfa, NULL, NULL, 1, 0);
6720 if (err)
6721 goto sync;
6723 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6724 repo, progress_cb, progress_arg, NULL, NULL);
6725 sync:
6726 sync_err = sync_fileindex(fileindex, fileindex_path);
6727 if (sync_err && err == NULL)
6728 err = sync_err;
6729 done:
6730 got_ref_close(resolved);
6731 free(tree_id);
6732 free(commit_id);
6733 if (fileindex)
6734 got_fileindex_free(fileindex);
6735 free(fileindex_path);
6737 unlockerr = lock_worktree(worktree, LOCK_SH);
6738 if (unlockerr && err == NULL)
6739 err = unlockerr;
6740 return err;
6743 const struct got_error *
6744 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6745 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6746 struct got_fileindex **fileindex, struct got_worktree *worktree,
6747 struct got_repository *repo)
6749 const struct got_error *err = NULL;
6750 char *tmp_branch_name = NULL;
6751 char *branch_ref_name = NULL;
6752 char *base_commit_ref_name = NULL;
6753 char *fileindex_path = NULL;
6754 struct check_rebase_ok_arg ok_arg;
6755 struct got_reference *wt_branch = NULL;
6756 struct got_reference *base_commit_ref = NULL;
6758 *tmp_branch = NULL;
6759 *branch_ref = NULL;
6760 *base_commit_id = NULL;
6761 *fileindex = NULL;
6763 err = lock_worktree(worktree, LOCK_EX);
6764 if (err)
6765 return err;
6767 err = open_fileindex(fileindex, &fileindex_path, worktree);
6768 if (err)
6769 goto done;
6771 ok_arg.worktree = worktree;
6772 ok_arg.repo = repo;
6773 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6774 &ok_arg);
6775 if (err)
6776 goto done;
6778 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6779 if (err)
6780 goto done;
6782 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6783 if (err)
6784 goto done;
6786 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6787 worktree);
6788 if (err)
6789 goto done;
6791 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6792 0);
6793 if (err)
6794 goto done;
6796 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6797 if (err)
6798 goto done;
6800 err = got_ref_write(*branch_ref, repo);
6801 if (err)
6802 goto done;
6804 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6805 worktree->base_commit_id);
6806 if (err)
6807 goto done;
6808 err = got_ref_write(base_commit_ref, repo);
6809 if (err)
6810 goto done;
6811 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6812 if (*base_commit_id == NULL) {
6813 err = got_error_from_errno("got_object_id_dup");
6814 goto done;
6817 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6818 worktree->base_commit_id);
6819 if (err)
6820 goto done;
6821 err = got_ref_write(*tmp_branch, repo);
6822 if (err)
6823 goto done;
6825 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6826 if (err)
6827 goto done;
6828 done:
6829 free(fileindex_path);
6830 free(tmp_branch_name);
6831 free(branch_ref_name);
6832 free(base_commit_ref_name);
6833 if (wt_branch)
6834 got_ref_close(wt_branch);
6835 if (err) {
6836 if (*branch_ref) {
6837 got_ref_close(*branch_ref);
6838 *branch_ref = NULL;
6840 if (*tmp_branch) {
6841 got_ref_close(*tmp_branch);
6842 *tmp_branch = NULL;
6844 free(*base_commit_id);
6845 if (*fileindex) {
6846 got_fileindex_free(*fileindex);
6847 *fileindex = NULL;
6849 lock_worktree(worktree, LOCK_SH);
6851 return err;
6854 const struct got_error *
6855 got_worktree_histedit_postpone(struct got_worktree *worktree,
6856 struct got_fileindex *fileindex)
6858 if (fileindex)
6859 got_fileindex_free(fileindex);
6860 return lock_worktree(worktree, LOCK_SH);
6863 const struct got_error *
6864 got_worktree_histedit_in_progress(int *in_progress,
6865 struct got_worktree *worktree)
6867 const struct got_error *err;
6868 char *tmp_branch_name = NULL;
6870 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6871 if (err)
6872 return err;
6874 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6875 free(tmp_branch_name);
6876 return NULL;
6879 const struct got_error *
6880 got_worktree_histedit_continue(struct got_object_id **commit_id,
6881 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6882 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6883 struct got_worktree *worktree, struct got_repository *repo)
6885 const struct got_error *err;
6886 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6887 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6888 struct got_reference *commit_ref = NULL;
6889 struct got_reference *base_commit_ref = NULL;
6890 char *fileindex_path = NULL;
6891 int have_staged_files = 0;
6893 *commit_id = NULL;
6894 *tmp_branch = NULL;
6895 *base_commit_id = NULL;
6896 *fileindex = NULL;
6898 err = lock_worktree(worktree, LOCK_EX);
6899 if (err)
6900 return err;
6902 err = open_fileindex(fileindex, &fileindex_path, worktree);
6903 if (err)
6904 goto done;
6906 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6907 &have_staged_files);
6908 if (err && err->code != GOT_ERR_CANCELLED)
6909 goto done;
6910 if (have_staged_files) {
6911 err = got_error(GOT_ERR_STAGED_PATHS);
6912 goto done;
6915 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6916 if (err)
6917 goto done;
6919 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6920 if (err)
6921 goto done;
6923 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6924 if (err)
6925 goto done;
6927 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6928 worktree);
6929 if (err)
6930 goto done;
6932 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6933 if (err)
6934 goto done;
6936 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6937 if (err)
6938 goto done;
6939 err = got_ref_resolve(commit_id, repo, commit_ref);
6940 if (err)
6941 goto done;
6943 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6944 if (err)
6945 goto done;
6946 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6947 if (err)
6948 goto done;
6950 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6951 if (err)
6952 goto done;
6953 done:
6954 free(commit_ref_name);
6955 free(branch_ref_name);
6956 free(fileindex_path);
6957 if (commit_ref)
6958 got_ref_close(commit_ref);
6959 if (base_commit_ref)
6960 got_ref_close(base_commit_ref);
6961 if (err) {
6962 free(*commit_id);
6963 *commit_id = NULL;
6964 free(*base_commit_id);
6965 *base_commit_id = NULL;
6966 if (*tmp_branch) {
6967 got_ref_close(*tmp_branch);
6968 *tmp_branch = NULL;
6970 if (*fileindex) {
6971 got_fileindex_free(*fileindex);
6972 *fileindex = NULL;
6974 lock_worktree(worktree, LOCK_EX);
6976 return err;
6979 static const struct got_error *
6980 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6982 const struct got_error *err;
6983 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6984 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6986 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6987 if (err)
6988 goto done;
6989 err = delete_ref(tmp_branch_name, repo);
6990 if (err)
6991 goto done;
6993 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6994 worktree);
6995 if (err)
6996 goto done;
6997 err = delete_ref(base_commit_ref_name, repo);
6998 if (err)
6999 goto done;
7001 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7002 if (err)
7003 goto done;
7004 err = delete_ref(branch_ref_name, repo);
7005 if (err)
7006 goto done;
7008 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7009 if (err)
7010 goto done;
7011 err = delete_ref(commit_ref_name, repo);
7012 if (err)
7013 goto done;
7014 done:
7015 free(tmp_branch_name);
7016 free(base_commit_ref_name);
7017 free(branch_ref_name);
7018 free(commit_ref_name);
7019 return err;
7022 const struct got_error *
7023 got_worktree_histedit_abort(struct got_worktree *worktree,
7024 struct got_fileindex *fileindex, struct got_repository *repo,
7025 struct got_reference *branch, struct got_object_id *base_commit_id,
7026 got_worktree_checkout_cb progress_cb, void *progress_arg)
7028 const struct got_error *err, *unlockerr, *sync_err;
7029 struct got_reference *resolved = NULL;
7030 char *fileindex_path = NULL;
7031 struct got_object_id *tree_id = NULL;
7032 struct revert_file_args rfa;
7034 err = lock_worktree(worktree, LOCK_EX);
7035 if (err)
7036 return err;
7038 err = got_ref_open(&resolved, repo,
7039 got_ref_get_symref_target(branch), 0);
7040 if (err)
7041 goto done;
7043 err = got_worktree_set_head_ref(worktree, resolved);
7044 if (err)
7045 goto done;
7047 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7048 if (err)
7049 goto done;
7051 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
7052 worktree->path_prefix);
7053 if (err)
7054 goto done;
7056 err = delete_histedit_refs(worktree, repo);
7057 if (err)
7058 goto done;
7060 err = get_fileindex_path(&fileindex_path, worktree);
7061 if (err)
7062 goto done;
7064 rfa.worktree = worktree;
7065 rfa.fileindex = fileindex;
7066 rfa.progress_cb = progress_cb;
7067 rfa.progress_arg = progress_arg;
7068 rfa.patch_cb = NULL;
7069 rfa.patch_arg = NULL;
7070 rfa.repo = repo;
7071 rfa.unlink_added_files = 0;
7072 err = worktree_status(worktree, "", fileindex, repo,
7073 revert_file, &rfa, NULL, NULL, 1, 0);
7074 if (err)
7075 goto sync;
7077 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7078 repo, progress_cb, progress_arg, NULL, NULL);
7079 sync:
7080 sync_err = sync_fileindex(fileindex, fileindex_path);
7081 if (sync_err && err == NULL)
7082 err = sync_err;
7083 done:
7084 got_ref_close(resolved);
7085 free(tree_id);
7086 free(fileindex_path);
7088 unlockerr = lock_worktree(worktree, LOCK_SH);
7089 if (unlockerr && err == NULL)
7090 err = unlockerr;
7091 return err;
7094 const struct got_error *
7095 got_worktree_histedit_complete(struct got_worktree *worktree,
7096 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7097 struct got_reference *edited_branch, struct got_repository *repo)
7099 const struct got_error *err, *unlockerr, *sync_err;
7100 struct got_object_id *new_head_commit_id = NULL;
7101 struct got_reference *resolved = NULL;
7102 char *fileindex_path = NULL;
7104 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7105 if (err)
7106 return err;
7108 err = got_ref_open(&resolved, repo,
7109 got_ref_get_symref_target(edited_branch), 0);
7110 if (err)
7111 goto done;
7113 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7114 resolved, new_head_commit_id, repo);
7115 if (err)
7116 goto done;
7118 err = got_ref_change_ref(resolved, new_head_commit_id);
7119 if (err)
7120 goto done;
7122 err = got_ref_write(resolved, repo);
7123 if (err)
7124 goto done;
7126 err = got_worktree_set_head_ref(worktree, resolved);
7127 if (err)
7128 goto done;
7130 err = delete_histedit_refs(worktree, repo);
7131 if (err)
7132 goto done;
7134 err = get_fileindex_path(&fileindex_path, worktree);
7135 if (err)
7136 goto done;
7137 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7138 sync_err = sync_fileindex(fileindex, fileindex_path);
7139 if (sync_err && err == NULL)
7140 err = sync_err;
7141 done:
7142 got_fileindex_free(fileindex);
7143 free(fileindex_path);
7144 free(new_head_commit_id);
7145 unlockerr = lock_worktree(worktree, LOCK_SH);
7146 if (unlockerr && err == NULL)
7147 err = unlockerr;
7148 return err;
7151 const struct got_error *
7152 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7153 struct got_object_id *commit_id, struct got_repository *repo)
7155 const struct got_error *err;
7156 char *commit_ref_name;
7158 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7159 if (err)
7160 return err;
7162 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7163 if (err)
7164 goto done;
7166 err = delete_ref(commit_ref_name, repo);
7167 done:
7168 free(commit_ref_name);
7169 return err;
7172 const struct got_error *
7173 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7174 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7175 struct got_worktree *worktree, const char *refname,
7176 struct got_repository *repo)
7178 const struct got_error *err = NULL;
7179 char *fileindex_path = NULL;
7180 struct check_rebase_ok_arg ok_arg;
7182 *fileindex = NULL;
7183 *branch_ref = NULL;
7184 *base_branch_ref = NULL;
7186 err = lock_worktree(worktree, LOCK_EX);
7187 if (err)
7188 return err;
7190 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7191 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7192 "cannot integrate a branch into itself; "
7193 "update -b or different branch name required");
7194 goto done;
7197 err = open_fileindex(fileindex, &fileindex_path, worktree);
7198 if (err)
7199 goto done;
7201 /* Preconditions are the same as for rebase. */
7202 ok_arg.worktree = worktree;
7203 ok_arg.repo = repo;
7204 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7205 &ok_arg);
7206 if (err)
7207 goto done;
7209 err = got_ref_open(branch_ref, repo, refname, 1);
7210 if (err)
7211 goto done;
7213 err = got_ref_open(base_branch_ref, repo,
7214 got_worktree_get_head_ref_name(worktree), 1);
7215 done:
7216 if (err) {
7217 if (*branch_ref) {
7218 got_ref_close(*branch_ref);
7219 *branch_ref = NULL;
7221 if (*base_branch_ref) {
7222 got_ref_close(*base_branch_ref);
7223 *base_branch_ref = NULL;
7225 if (*fileindex) {
7226 got_fileindex_free(*fileindex);
7227 *fileindex = NULL;
7229 lock_worktree(worktree, LOCK_SH);
7231 return err;
7234 const struct got_error *
7235 got_worktree_integrate_continue(struct got_worktree *worktree,
7236 struct got_fileindex *fileindex, struct got_repository *repo,
7237 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7238 got_worktree_checkout_cb progress_cb, void *progress_arg,
7239 got_cancel_cb cancel_cb, void *cancel_arg)
7241 const struct got_error *err = NULL, *sync_err, *unlockerr;
7242 char *fileindex_path = NULL;
7243 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7245 err = get_fileindex_path(&fileindex_path, worktree);
7246 if (err)
7247 goto done;
7249 err = got_ref_resolve(&commit_id, repo, branch_ref);
7250 if (err)
7251 goto done;
7253 err = got_object_id_by_path(&tree_id, repo, commit_id,
7254 worktree->path_prefix);
7255 if (err)
7256 goto done;
7258 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7259 if (err)
7260 goto done;
7262 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7263 progress_cb, progress_arg, cancel_cb, cancel_arg);
7264 if (err)
7265 goto sync;
7267 err = got_ref_change_ref(base_branch_ref, commit_id);
7268 if (err)
7269 goto sync;
7271 err = got_ref_write(base_branch_ref, repo);
7272 if (err)
7273 goto sync;
7275 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7276 sync:
7277 sync_err = sync_fileindex(fileindex, fileindex_path);
7278 if (sync_err && err == NULL)
7279 err = sync_err;
7281 done:
7282 unlockerr = got_ref_unlock(branch_ref);
7283 if (unlockerr && err == NULL)
7284 err = unlockerr;
7285 got_ref_close(branch_ref);
7287 unlockerr = got_ref_unlock(base_branch_ref);
7288 if (unlockerr && err == NULL)
7289 err = unlockerr;
7290 got_ref_close(base_branch_ref);
7292 got_fileindex_free(fileindex);
7293 free(fileindex_path);
7294 free(tree_id);
7296 unlockerr = lock_worktree(worktree, LOCK_SH);
7297 if (unlockerr && err == NULL)
7298 err = unlockerr;
7299 return err;
7302 const struct got_error *
7303 got_worktree_integrate_abort(struct got_worktree *worktree,
7304 struct got_fileindex *fileindex, struct got_repository *repo,
7305 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7307 const struct got_error *err = NULL, *unlockerr = NULL;
7309 got_fileindex_free(fileindex);
7311 err = lock_worktree(worktree, LOCK_SH);
7313 unlockerr = got_ref_unlock(branch_ref);
7314 if (unlockerr && err == NULL)
7315 err = unlockerr;
7316 got_ref_close(branch_ref);
7318 unlockerr = got_ref_unlock(base_branch_ref);
7319 if (unlockerr && err == NULL)
7320 err = unlockerr;
7321 got_ref_close(base_branch_ref);
7323 return err;
7326 const struct got_error *
7327 got_worktree_merge_postpone(struct got_worktree *worktree,
7328 struct got_fileindex *fileindex)
7330 const struct got_error *err, *sync_err;
7331 char *fileindex_path = NULL;
7333 err = get_fileindex_path(&fileindex_path, worktree);
7334 if (err)
7335 goto done;
7337 sync_err = sync_fileindex(fileindex, fileindex_path);
7339 err = lock_worktree(worktree, LOCK_SH);
7340 if (sync_err && err == NULL)
7341 err = sync_err;
7342 done:
7343 got_fileindex_free(fileindex);
7344 free(fileindex_path);
7345 return err;
7348 static const struct got_error *
7349 delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7351 const struct got_error *err;
7352 char *branch_refname = NULL, *commit_refname = NULL;
7354 err = get_merge_branch_ref_name(&branch_refname, worktree);
7355 if (err)
7356 goto done;
7357 err = delete_ref(branch_refname, repo);
7358 if (err)
7359 goto done;
7361 err = get_merge_commit_ref_name(&commit_refname, worktree);
7362 if (err)
7363 goto done;
7364 err = delete_ref(commit_refname, repo);
7365 if (err)
7366 goto done;
7368 done:
7369 free(branch_refname);
7370 free(commit_refname);
7371 return err;
7374 struct merge_commit_msg_arg {
7375 struct got_worktree *worktree;
7376 const char *branch_name;
7379 static const struct got_error *
7380 merge_commit_msg_cb(struct got_pathlist_head *commitable_paths, char **logmsg,
7381 void *arg)
7383 struct merge_commit_msg_arg *a = arg;
7385 if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7386 got_worktree_get_head_ref_name(a->worktree)) == -1)
7387 return got_error_from_errno("asprintf");
7389 return NULL;
7393 const struct got_error *
7394 got_worktree_merge_branch(struct got_worktree *worktree,
7395 struct got_fileindex *fileindex,
7396 struct got_object_id *yca_commit_id,
7397 struct got_object_id *branch_tip,
7398 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7399 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7401 const struct got_error *err;
7402 char *fileindex_path = NULL;
7404 err = get_fileindex_path(&fileindex_path, worktree);
7405 if (err)
7406 goto done;
7408 err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7409 worktree);
7410 if (err)
7411 goto done;
7413 err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7414 branch_tip, repo, progress_cb, progress_arg,
7415 cancel_cb, cancel_arg);
7416 done:
7417 free(fileindex_path);
7418 return err;
7421 const struct got_error *
7422 got_worktree_merge_commit(struct got_object_id **new_commit_id,
7423 struct got_worktree *worktree, struct got_fileindex *fileindex,
7424 const char *author, const char *committer, int allow_bad_symlinks,
7425 struct got_object_id *branch_tip, const char *branch_name,
7426 struct got_repository *repo,
7427 got_worktree_status_cb status_cb, void *status_arg)
7430 const struct got_error *err = NULL, *sync_err;
7431 struct got_pathlist_head commitable_paths;
7432 struct collect_commitables_arg cc_arg;
7433 struct got_pathlist_entry *pe;
7434 struct got_reference *head_ref = NULL;
7435 struct got_object_id *head_commit_id = NULL;
7436 int have_staged_files = 0;
7437 struct merge_commit_msg_arg mcm_arg;
7438 char *fileindex_path = NULL;
7440 *new_commit_id = NULL;
7442 TAILQ_INIT(&commitable_paths);
7444 err = get_fileindex_path(&fileindex_path, worktree);
7445 if (err)
7446 goto done;
7448 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7449 if (err)
7450 goto done;
7452 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7453 if (err)
7454 goto done;
7456 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7457 &have_staged_files);
7458 if (err && err->code != GOT_ERR_CANCELLED)
7459 goto done;
7460 if (have_staged_files) {
7461 err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7462 goto done;
7465 cc_arg.commitable_paths = &commitable_paths;
7466 cc_arg.worktree = worktree;
7467 cc_arg.fileindex = fileindex;
7468 cc_arg.repo = repo;
7469 cc_arg.have_staged_files = have_staged_files;
7470 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7471 err = worktree_status(worktree, "", fileindex, repo,
7472 collect_commitables, &cc_arg, NULL, NULL, 1, 0);
7473 if (err)
7474 goto done;
7476 if (TAILQ_EMPTY(&commitable_paths)) {
7477 err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7478 "merge of %s cannot proceed", branch_name);
7479 goto done;
7482 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7483 struct got_commitable *ct = pe->data;
7484 const char *ct_path = ct->in_repo_path;
7486 while (ct_path[0] == '/')
7487 ct_path++;
7488 err = check_out_of_date(ct_path, ct->status,
7489 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7490 head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7491 if (err)
7492 goto done;
7496 mcm_arg.worktree = worktree;
7497 mcm_arg.branch_name = branch_name;
7498 err = commit_worktree(new_commit_id, &commitable_paths,
7499 head_commit_id, branch_tip, worktree, author, committer,
7500 merge_commit_msg_cb, &mcm_arg, status_cb, status_arg, repo);
7501 if (err)
7502 goto done;
7504 err = update_fileindex_after_commit(worktree, &commitable_paths,
7505 *new_commit_id, fileindex, have_staged_files);
7506 sync_err = sync_fileindex(fileindex, fileindex_path);
7507 if (sync_err && err == NULL)
7508 err = sync_err;
7509 done:
7510 TAILQ_FOREACH(pe, &commitable_paths, entry) {
7511 struct got_commitable *ct = pe->data;
7512 free_commitable(ct);
7514 got_pathlist_free(&commitable_paths);
7515 free(fileindex_path);
7516 return err;
7519 const struct got_error *
7520 got_worktree_merge_complete(struct got_worktree *worktree,
7521 struct got_fileindex *fileindex, struct got_repository *repo)
7523 const struct got_error *err, *unlockerr, *sync_err;
7524 char *fileindex_path = NULL;
7526 err = delete_merge_refs(worktree, repo);
7527 if (err)
7528 goto done;
7530 err = get_fileindex_path(&fileindex_path, worktree);
7531 if (err)
7532 goto done;
7533 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7534 sync_err = sync_fileindex(fileindex, fileindex_path);
7535 if (sync_err && err == NULL)
7536 err = sync_err;
7537 done:
7538 got_fileindex_free(fileindex);
7539 free(fileindex_path);
7540 unlockerr = lock_worktree(worktree, LOCK_SH);
7541 if (unlockerr && err == NULL)
7542 err = unlockerr;
7543 return err;
7546 const struct got_error *
7547 got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7548 struct got_repository *repo)
7550 const struct got_error *err;
7551 char *branch_refname = NULL;
7552 struct got_reference *branch_ref = NULL;
7554 *in_progress = 0;
7556 err = get_merge_branch_ref_name(&branch_refname, worktree);
7557 if (err)
7558 return err;
7559 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7560 free(branch_refname);
7561 if (err) {
7562 if (err->code != GOT_ERR_NOT_REF)
7563 return err;
7564 } else
7565 *in_progress = 1;
7567 return NULL;
7570 const struct got_error *got_worktree_merge_prepare(
7571 struct got_fileindex **fileindex, struct got_worktree *worktree,
7572 struct got_reference *branch, struct got_repository *repo)
7574 const struct got_error *err = NULL;
7575 char *fileindex_path = NULL;
7576 char *branch_refname = NULL, *commit_refname = NULL;
7577 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
7578 struct got_reference *commit_ref = NULL;
7579 struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
7580 struct check_rebase_ok_arg ok_arg;
7582 *fileindex = NULL;
7584 err = lock_worktree(worktree, LOCK_EX);
7585 if (err)
7586 return err;
7588 err = open_fileindex(fileindex, &fileindex_path, worktree);
7589 if (err)
7590 goto done;
7592 /* Preconditions are the same as for rebase. */
7593 ok_arg.worktree = worktree;
7594 ok_arg.repo = repo;
7595 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7596 &ok_arg);
7597 if (err)
7598 goto done;
7600 err = get_merge_branch_ref_name(&branch_refname, worktree);
7601 if (err)
7602 return err;
7604 err = get_merge_commit_ref_name(&commit_refname, worktree);
7605 if (err)
7606 return err;
7608 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7609 0);
7610 if (err)
7611 goto done;
7613 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
7614 if (err)
7615 goto done;
7617 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
7618 err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
7619 goto done;
7622 err = got_ref_resolve(&branch_tip, repo, branch);
7623 if (err)
7624 goto done;
7626 err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
7627 if (err)
7628 goto done;
7629 err = got_ref_write(branch_ref, repo);
7630 if (err)
7631 goto done;
7633 err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
7634 if (err)
7635 goto done;
7636 err = got_ref_write(commit_ref, repo);
7637 if (err)
7638 goto done;
7640 done:
7641 free(branch_refname);
7642 free(commit_refname);
7643 free(fileindex_path);
7644 if (branch_ref)
7645 got_ref_close(branch_ref);
7646 if (commit_ref)
7647 got_ref_close(commit_ref);
7648 if (wt_branch)
7649 got_ref_close(wt_branch);
7650 free(wt_branch_tip);
7651 if (err) {
7652 if (*fileindex) {
7653 got_fileindex_free(*fileindex);
7654 *fileindex = NULL;
7656 lock_worktree(worktree, LOCK_SH);
7658 return err;
7661 const struct got_error *
7662 got_worktree_merge_continue(char **branch_name,
7663 struct got_object_id **branch_tip, struct got_fileindex **fileindex,
7664 struct got_worktree *worktree, struct got_repository *repo)
7666 const struct got_error *err;
7667 char *commit_refname = NULL, *branch_refname = NULL;
7668 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
7669 char *fileindex_path = NULL;
7670 int have_staged_files = 0;
7672 *branch_name = NULL;
7673 *branch_tip = NULL;
7674 *fileindex = NULL;
7676 err = lock_worktree(worktree, LOCK_EX);
7677 if (err)
7678 return err;
7680 err = open_fileindex(fileindex, &fileindex_path, worktree);
7681 if (err)
7682 goto done;
7684 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7685 &have_staged_files);
7686 if (err && err->code != GOT_ERR_CANCELLED)
7687 goto done;
7688 if (have_staged_files) {
7689 err = got_error(GOT_ERR_STAGED_PATHS);
7690 goto done;
7693 err = get_merge_branch_ref_name(&branch_refname, worktree);
7694 if (err)
7695 goto done;
7697 err = get_merge_commit_ref_name(&commit_refname, worktree);
7698 if (err)
7699 goto done;
7701 err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7702 if (err)
7703 goto done;
7705 if (!got_ref_is_symbolic(branch_ref)) {
7706 err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7707 "%s is not a symbolic reference",
7708 got_ref_get_name(branch_ref));
7709 goto done;
7711 *branch_name = strdup(got_ref_get_symref_target(branch_ref));
7712 if (*branch_name == NULL) {
7713 err = got_error_from_errno("strdup");
7714 goto done;
7717 err = got_ref_open(&commit_ref, repo, commit_refname, 0);
7718 if (err)
7719 goto done;
7721 err = got_ref_resolve(branch_tip, repo, commit_ref);
7722 if (err)
7723 goto done;
7724 done:
7725 free(commit_refname);
7726 free(branch_refname);
7727 free(fileindex_path);
7728 if (commit_ref)
7729 got_ref_close(commit_ref);
7730 if (branch_ref)
7731 got_ref_close(branch_ref);
7732 if (err) {
7733 if (*branch_name) {
7734 free(*branch_name);
7735 *branch_name = NULL;
7737 free(*branch_tip);
7738 *branch_tip = NULL;
7739 if (*fileindex) {
7740 got_fileindex_free(*fileindex);
7741 *fileindex = NULL;
7743 lock_worktree(worktree, LOCK_SH);
7745 return err;
7748 const struct got_error *
7749 got_worktree_merge_abort(struct got_worktree *worktree,
7750 struct got_fileindex *fileindex, struct got_repository *repo,
7751 got_worktree_checkout_cb progress_cb, void *progress_arg)
7753 const struct got_error *err, *unlockerr, *sync_err;
7754 struct got_object_id *commit_id = NULL;
7755 char *fileindex_path = NULL;
7756 struct revert_file_args rfa;
7757 struct got_object_id *tree_id = NULL;
7759 err = got_object_id_by_path(&tree_id, repo,
7760 worktree->base_commit_id, worktree->path_prefix);
7761 if (err)
7762 goto done;
7764 err = delete_merge_refs(worktree, repo);
7765 if (err)
7766 goto done;
7768 err = get_fileindex_path(&fileindex_path, worktree);
7769 if (err)
7770 goto done;
7772 rfa.worktree = worktree;
7773 rfa.fileindex = fileindex;
7774 rfa.progress_cb = progress_cb;
7775 rfa.progress_arg = progress_arg;
7776 rfa.patch_cb = NULL;
7777 rfa.patch_arg = NULL;
7778 rfa.repo = repo;
7779 rfa.unlink_added_files = 1;
7780 err = worktree_status(worktree, "", fileindex, repo,
7781 revert_file, &rfa, NULL, NULL, 1, 0);
7782 if (err)
7783 goto sync;
7785 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7786 repo, progress_cb, progress_arg, NULL, NULL);
7787 sync:
7788 sync_err = sync_fileindex(fileindex, fileindex_path);
7789 if (sync_err && err == NULL)
7790 err = sync_err;
7791 done:
7792 free(tree_id);
7793 free(commit_id);
7794 if (fileindex)
7795 got_fileindex_free(fileindex);
7796 free(fileindex_path);
7798 unlockerr = lock_worktree(worktree, LOCK_SH);
7799 if (unlockerr && err == NULL)
7800 err = unlockerr;
7801 return err;
7804 struct check_stage_ok_arg {
7805 struct got_object_id *head_commit_id;
7806 struct got_worktree *worktree;
7807 struct got_fileindex *fileindex;
7808 struct got_repository *repo;
7809 int have_changes;
7812 const struct got_error *
7813 check_stage_ok(void *arg, unsigned char status,
7814 unsigned char staged_status, const char *relpath,
7815 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7816 struct got_object_id *commit_id, int dirfd, const char *de_name)
7818 struct check_stage_ok_arg *a = arg;
7819 const struct got_error *err = NULL;
7820 struct got_fileindex_entry *ie;
7821 struct got_object_id base_commit_id;
7822 struct got_object_id *base_commit_idp = NULL;
7823 char *in_repo_path = NULL, *p;
7825 if (status == GOT_STATUS_UNVERSIONED ||
7826 status == GOT_STATUS_NO_CHANGE)
7827 return NULL;
7828 if (status == GOT_STATUS_NONEXISTENT)
7829 return got_error_set_errno(ENOENT, relpath);
7831 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7832 if (ie == NULL)
7833 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7835 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7836 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7837 relpath) == -1)
7838 return got_error_from_errno("asprintf");
7840 if (got_fileindex_entry_has_commit(ie)) {
7841 memcpy(base_commit_id.sha1, ie->commit_sha1,
7842 SHA1_DIGEST_LENGTH);
7843 base_commit_idp = &base_commit_id;
7846 if (status == GOT_STATUS_CONFLICT) {
7847 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7848 goto done;
7849 } else if (status != GOT_STATUS_ADD &&
7850 status != GOT_STATUS_MODIFY &&
7851 status != GOT_STATUS_DELETE) {
7852 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7853 goto done;
7856 a->have_changes = 1;
7858 p = in_repo_path;
7859 while (p[0] == '/')
7860 p++;
7861 err = check_out_of_date(p, status, staged_status,
7862 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7863 GOT_ERR_STAGE_OUT_OF_DATE);
7864 done:
7865 free(in_repo_path);
7866 return err;
7869 struct stage_path_arg {
7870 struct got_worktree *worktree;
7871 struct got_fileindex *fileindex;
7872 struct got_repository *repo;
7873 got_worktree_status_cb status_cb;
7874 void *status_arg;
7875 got_worktree_patch_cb patch_cb;
7876 void *patch_arg;
7877 int staged_something;
7878 int allow_bad_symlinks;
7881 static const struct got_error *
7882 stage_path(void *arg, unsigned char status,
7883 unsigned char staged_status, const char *relpath,
7884 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7885 struct got_object_id *commit_id, int dirfd, const char *de_name)
7887 struct stage_path_arg *a = arg;
7888 const struct got_error *err = NULL;
7889 struct got_fileindex_entry *ie;
7890 char *ondisk_path = NULL, *path_content = NULL;
7891 uint32_t stage;
7892 struct got_object_id *new_staged_blob_id = NULL;
7893 struct stat sb;
7895 if (status == GOT_STATUS_UNVERSIONED)
7896 return NULL;
7898 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7899 if (ie == NULL)
7900 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7902 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7903 relpath)== -1)
7904 return got_error_from_errno("asprintf");
7906 switch (status) {
7907 case GOT_STATUS_ADD:
7908 case GOT_STATUS_MODIFY:
7909 /* XXX could sb.st_mode be passed in by our caller? */
7910 if (lstat(ondisk_path, &sb) == -1) {
7911 err = got_error_from_errno2("lstat", ondisk_path);
7912 break;
7914 if (a->patch_cb) {
7915 if (status == GOT_STATUS_ADD) {
7916 int choice = GOT_PATCH_CHOICE_NONE;
7917 err = (*a->patch_cb)(&choice, a->patch_arg,
7918 status, ie->path, NULL, 1, 1);
7919 if (err)
7920 break;
7921 if (choice != GOT_PATCH_CHOICE_YES)
7922 break;
7923 } else {
7924 err = create_patched_content(&path_content, 0,
7925 staged_blob_id ? staged_blob_id : blob_id,
7926 ondisk_path, dirfd, de_name, ie->path,
7927 a->repo, a->patch_cb, a->patch_arg);
7928 if (err || path_content == NULL)
7929 break;
7932 err = got_object_blob_create(&new_staged_blob_id,
7933 path_content ? path_content : ondisk_path, a->repo);
7934 if (err)
7935 break;
7936 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7937 SHA1_DIGEST_LENGTH);
7938 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7939 stage = GOT_FILEIDX_STAGE_ADD;
7940 else
7941 stage = GOT_FILEIDX_STAGE_MODIFY;
7942 got_fileindex_entry_stage_set(ie, stage);
7943 if (S_ISLNK(sb.st_mode)) {
7944 int is_bad_symlink = 0;
7945 if (!a->allow_bad_symlinks) {
7946 char target_path[PATH_MAX];
7947 ssize_t target_len;
7948 target_len = readlink(ondisk_path, target_path,
7949 sizeof(target_path));
7950 if (target_len == -1) {
7951 err = got_error_from_errno2("readlink",
7952 ondisk_path);
7953 break;
7955 err = is_bad_symlink_target(&is_bad_symlink,
7956 target_path, target_len, ondisk_path,
7957 a->worktree->root_path);
7958 if (err)
7959 break;
7960 if (is_bad_symlink) {
7961 err = got_error_path(ondisk_path,
7962 GOT_ERR_BAD_SYMLINK);
7963 break;
7966 if (is_bad_symlink)
7967 got_fileindex_entry_staged_filetype_set(ie,
7968 GOT_FILEIDX_MODE_BAD_SYMLINK);
7969 else
7970 got_fileindex_entry_staged_filetype_set(ie,
7971 GOT_FILEIDX_MODE_SYMLINK);
7972 } else {
7973 got_fileindex_entry_staged_filetype_set(ie,
7974 GOT_FILEIDX_MODE_REGULAR_FILE);
7976 a->staged_something = 1;
7977 if (a->status_cb == NULL)
7978 break;
7979 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7980 get_staged_status(ie), relpath, blob_id,
7981 new_staged_blob_id, NULL, dirfd, de_name);
7982 break;
7983 case GOT_STATUS_DELETE:
7984 if (staged_status == GOT_STATUS_DELETE)
7985 break;
7986 if (a->patch_cb) {
7987 int choice = GOT_PATCH_CHOICE_NONE;
7988 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7989 ie->path, NULL, 1, 1);
7990 if (err)
7991 break;
7992 if (choice == GOT_PATCH_CHOICE_NO)
7993 break;
7994 if (choice != GOT_PATCH_CHOICE_YES) {
7995 err = got_error(GOT_ERR_PATCH_CHOICE);
7996 break;
7999 stage = GOT_FILEIDX_STAGE_DELETE;
8000 got_fileindex_entry_stage_set(ie, stage);
8001 a->staged_something = 1;
8002 if (a->status_cb == NULL)
8003 break;
8004 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8005 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8006 de_name);
8007 break;
8008 case GOT_STATUS_NO_CHANGE:
8009 break;
8010 case GOT_STATUS_CONFLICT:
8011 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8012 break;
8013 case GOT_STATUS_NONEXISTENT:
8014 err = got_error_set_errno(ENOENT, relpath);
8015 break;
8016 default:
8017 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8018 break;
8021 if (path_content && unlink(path_content) == -1 && err == NULL)
8022 err = got_error_from_errno2("unlink", path_content);
8023 free(path_content);
8024 free(ondisk_path);
8025 free(new_staged_blob_id);
8026 return err;
8029 const struct got_error *
8030 got_worktree_stage(struct got_worktree *worktree,
8031 struct got_pathlist_head *paths,
8032 got_worktree_status_cb status_cb, void *status_arg,
8033 got_worktree_patch_cb patch_cb, void *patch_arg,
8034 int allow_bad_symlinks, struct got_repository *repo)
8036 const struct got_error *err = NULL, *sync_err, *unlockerr;
8037 struct got_pathlist_entry *pe;
8038 struct got_fileindex *fileindex = NULL;
8039 char *fileindex_path = NULL;
8040 struct got_reference *head_ref = NULL;
8041 struct got_object_id *head_commit_id = NULL;
8042 struct check_stage_ok_arg oka;
8043 struct stage_path_arg spa;
8045 err = lock_worktree(worktree, LOCK_EX);
8046 if (err)
8047 return err;
8049 err = got_ref_open(&head_ref, repo,
8050 got_worktree_get_head_ref_name(worktree), 0);
8051 if (err)
8052 goto done;
8053 err = got_ref_resolve(&head_commit_id, repo, head_ref);
8054 if (err)
8055 goto done;
8056 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8057 if (err)
8058 goto done;
8060 /* Check pre-conditions before staging anything. */
8061 oka.head_commit_id = head_commit_id;
8062 oka.worktree = worktree;
8063 oka.fileindex = fileindex;
8064 oka.repo = repo;
8065 oka.have_changes = 0;
8066 TAILQ_FOREACH(pe, paths, entry) {
8067 err = worktree_status(worktree, pe->path, fileindex, repo,
8068 check_stage_ok, &oka, NULL, NULL, 1, 0);
8069 if (err)
8070 goto done;
8072 if (!oka.have_changes) {
8073 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8074 goto done;
8077 spa.worktree = worktree;
8078 spa.fileindex = fileindex;
8079 spa.repo = repo;
8080 spa.patch_cb = patch_cb;
8081 spa.patch_arg = patch_arg;
8082 spa.status_cb = status_cb;
8083 spa.status_arg = status_arg;
8084 spa.staged_something = 0;
8085 spa.allow_bad_symlinks = allow_bad_symlinks;
8086 TAILQ_FOREACH(pe, paths, entry) {
8087 err = worktree_status(worktree, pe->path, fileindex, repo,
8088 stage_path, &spa, NULL, NULL, 1, 0);
8089 if (err)
8090 goto done;
8092 if (!spa.staged_something) {
8093 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8094 goto done;
8097 sync_err = sync_fileindex(fileindex, fileindex_path);
8098 if (sync_err && err == NULL)
8099 err = sync_err;
8100 done:
8101 if (head_ref)
8102 got_ref_close(head_ref);
8103 free(head_commit_id);
8104 free(fileindex_path);
8105 if (fileindex)
8106 got_fileindex_free(fileindex);
8107 unlockerr = lock_worktree(worktree, LOCK_SH);
8108 if (unlockerr && err == NULL)
8109 err = unlockerr;
8110 return err;
8113 struct unstage_path_arg {
8114 struct got_worktree *worktree;
8115 struct got_fileindex *fileindex;
8116 struct got_repository *repo;
8117 got_worktree_checkout_cb progress_cb;
8118 void *progress_arg;
8119 got_worktree_patch_cb patch_cb;
8120 void *patch_arg;
8123 static const struct got_error *
8124 create_unstaged_content(char **path_unstaged_content,
8125 char **path_new_staged_content, struct got_object_id *blob_id,
8126 struct got_object_id *staged_blob_id, const char *relpath,
8127 struct got_repository *repo,
8128 got_worktree_patch_cb patch_cb, void *patch_arg)
8130 const struct got_error *err, *free_err;
8131 struct got_blob_object *blob = NULL, *staged_blob = NULL;
8132 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8133 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8134 struct got_diffreg_result *diffreg_result = NULL;
8135 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8136 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8138 *path_unstaged_content = NULL;
8139 *path_new_staged_content = NULL;
8141 err = got_object_id_str(&label1, blob_id);
8142 if (err)
8143 return err;
8144 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
8145 if (err)
8146 goto done;
8148 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
8149 if (err)
8150 goto done;
8152 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8153 if (err)
8154 goto done;
8156 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
8157 if (err)
8158 goto done;
8160 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
8161 if (err)
8162 goto done;
8164 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8165 if (err)
8166 goto done;
8168 err = got_diff_files(&diffreg_result, f1, label1, f2,
8169 path2, 3, 0, 1, NULL);
8170 if (err)
8171 goto done;
8173 err = got_opentemp_named(path_unstaged_content, &outfile,
8174 "got-unstaged-content");
8175 if (err)
8176 goto done;
8177 err = got_opentemp_named(path_new_staged_content, &rejectfile,
8178 "got-new-staged-content");
8179 if (err)
8180 goto done;
8182 if (fseek(f1, 0L, SEEK_SET) == -1) {
8183 err = got_ferror(f1, GOT_ERR_IO);
8184 goto done;
8186 if (fseek(f2, 0L, SEEK_SET) == -1) {
8187 err = got_ferror(f2, GOT_ERR_IO);
8188 goto done;
8190 /* Count the number of actual changes in the diff result. */
8191 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8192 struct diff_chunk_context cc = {};
8193 diff_chunk_context_load_change(&cc, &nchunks_used,
8194 diffreg_result->result, n, 0);
8195 nchanges++;
8197 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8198 int choice;
8199 err = apply_or_reject_change(&choice, &nchunks_used,
8200 diffreg_result->result, n, relpath, f1, f2,
8201 &line_cur1, &line_cur2,
8202 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8203 if (err)
8204 goto done;
8205 if (choice == GOT_PATCH_CHOICE_YES)
8206 have_content = 1;
8207 else
8208 have_rejected_content = 1;
8209 if (choice == GOT_PATCH_CHOICE_QUIT)
8210 break;
8212 if (have_content || have_rejected_content)
8213 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8214 outfile, rejectfile);
8215 done:
8216 free(label1);
8217 if (blob)
8218 got_object_blob_close(blob);
8219 if (staged_blob)
8220 got_object_blob_close(staged_blob);
8221 free_err = got_diffreg_result_free(diffreg_result);
8222 if (free_err && err == NULL)
8223 err = free_err;
8224 if (f1 && fclose(f1) == EOF && err == NULL)
8225 err = got_error_from_errno2("fclose", path1);
8226 if (f2 && fclose(f2) == EOF && err == NULL)
8227 err = got_error_from_errno2("fclose", path2);
8228 if (outfile && fclose(outfile) == EOF && err == NULL)
8229 err = got_error_from_errno2("fclose", *path_unstaged_content);
8230 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8231 err = got_error_from_errno2("fclose", *path_new_staged_content);
8232 if (path1 && unlink(path1) == -1 && err == NULL)
8233 err = got_error_from_errno2("unlink", path1);
8234 if (path2 && unlink(path2) == -1 && err == NULL)
8235 err = got_error_from_errno2("unlink", path2);
8236 if (err || !have_content) {
8237 if (*path_unstaged_content &&
8238 unlink(*path_unstaged_content) == -1 && err == NULL)
8239 err = got_error_from_errno2("unlink",
8240 *path_unstaged_content);
8241 free(*path_unstaged_content);
8242 *path_unstaged_content = NULL;
8244 if (err || !have_content || !have_rejected_content) {
8245 if (*path_new_staged_content &&
8246 unlink(*path_new_staged_content) == -1 && err == NULL)
8247 err = got_error_from_errno2("unlink",
8248 *path_new_staged_content);
8249 free(*path_new_staged_content);
8250 *path_new_staged_content = NULL;
8252 free(path1);
8253 free(path2);
8254 return err;
8257 static const struct got_error *
8258 unstage_hunks(struct got_object_id *staged_blob_id,
8259 struct got_blob_object *blob_base,
8260 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8261 const char *ondisk_path, const char *label_orig,
8262 struct got_worktree *worktree, struct got_repository *repo,
8263 got_worktree_patch_cb patch_cb, void *patch_arg,
8264 got_worktree_checkout_cb progress_cb, void *progress_arg)
8266 const struct got_error *err = NULL;
8267 char *path_unstaged_content = NULL;
8268 char *path_new_staged_content = NULL;
8269 char *parent = NULL, *base_path = NULL;
8270 char *blob_base_path = NULL;
8271 struct got_object_id *new_staged_blob_id = NULL;
8272 FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8273 struct stat sb;
8275 err = create_unstaged_content(&path_unstaged_content,
8276 &path_new_staged_content, blob_id, staged_blob_id,
8277 ie->path, repo, patch_cb, patch_arg);
8278 if (err)
8279 return err;
8281 if (path_unstaged_content == NULL)
8282 return NULL;
8284 if (path_new_staged_content) {
8285 err = got_object_blob_create(&new_staged_blob_id,
8286 path_new_staged_content, repo);
8287 if (err)
8288 goto done;
8291 f = fopen(path_unstaged_content, "re");
8292 if (f == NULL) {
8293 err = got_error_from_errno2("fopen",
8294 path_unstaged_content);
8295 goto done;
8297 if (fstat(fileno(f), &sb) == -1) {
8298 err = got_error_from_errno2("fstat", path_unstaged_content);
8299 goto done;
8301 if (got_fileindex_entry_staged_filetype_get(ie) ==
8302 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8303 char link_target[PATH_MAX];
8304 size_t r;
8305 r = fread(link_target, 1, sizeof(link_target), f);
8306 if (r == 0 && ferror(f)) {
8307 err = got_error_from_errno("fread");
8308 goto done;
8310 if (r >= sizeof(link_target)) { /* should not happen */
8311 err = got_error(GOT_ERR_NO_SPACE);
8312 goto done;
8314 link_target[r] = '\0';
8315 err = merge_symlink(worktree, blob_base,
8316 ondisk_path, ie->path, label_orig, link_target,
8317 worktree->base_commit_id, repo, progress_cb,
8318 progress_arg);
8319 } else {
8320 int local_changes_subsumed;
8322 err = got_path_dirname(&parent, ondisk_path);
8323 if (err)
8324 return err;
8326 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8327 parent) == -1) {
8328 err = got_error_from_errno("asprintf");
8329 base_path = NULL;
8330 goto done;
8333 err = got_opentemp_named(&blob_base_path, &f_base, base_path);
8334 if (err)
8335 goto done;
8336 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8337 blob_base);
8338 if (err)
8339 goto done;
8342 * In order the run a 3-way merge with a symlink we copy the symlink's
8343 * target path into a temporary file and use that file with diff3.
8345 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8346 err = dump_symlink_target_path_to_file(&f_deriv2,
8347 ondisk_path);
8348 if (err)
8349 goto done;
8350 } else {
8351 int fd;
8352 fd = open(ondisk_path,
8353 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
8354 if (fd == -1) {
8355 err = got_error_from_errno2("open", ondisk_path);
8356 goto done;
8358 f_deriv2 = fdopen(fd, "r");
8359 if (f_deriv2 == NULL) {
8360 err = got_error_from_errno2("fdopen", ondisk_path);
8361 close(fd);
8362 goto done;
8366 err = merge_file(&local_changes_subsumed, worktree,
8367 f_base, f, f_deriv2, ondisk_path, ie->path,
8368 got_fileindex_perms_to_st(ie),
8369 label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8370 repo, progress_cb, progress_arg);
8372 if (err)
8373 goto done;
8375 if (new_staged_blob_id) {
8376 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8377 SHA1_DIGEST_LENGTH);
8378 } else {
8379 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8380 got_fileindex_entry_staged_filetype_set(ie, 0);
8382 done:
8383 free(new_staged_blob_id);
8384 if (path_unstaged_content &&
8385 unlink(path_unstaged_content) == -1 && err == NULL)
8386 err = got_error_from_errno2("unlink", path_unstaged_content);
8387 if (path_new_staged_content &&
8388 unlink(path_new_staged_content) == -1 && err == NULL)
8389 err = got_error_from_errno2("unlink", path_new_staged_content);
8390 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8391 err = got_error_from_errno2("unlink", blob_base_path);
8392 if (f_base && fclose(f_base) == EOF && err == NULL)
8393 err = got_error_from_errno2("fclose", path_unstaged_content);
8394 if (f && fclose(f) == EOF && err == NULL)
8395 err = got_error_from_errno2("fclose", path_unstaged_content);
8396 if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8397 err = got_error_from_errno2("fclose", ondisk_path);
8398 free(path_unstaged_content);
8399 free(path_new_staged_content);
8400 free(blob_base_path);
8401 free(parent);
8402 free(base_path);
8403 return err;
8406 static const struct got_error *
8407 unstage_path(void *arg, unsigned char status,
8408 unsigned char staged_status, const char *relpath,
8409 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8410 struct got_object_id *commit_id, int dirfd, const char *de_name)
8412 const struct got_error *err = NULL;
8413 struct unstage_path_arg *a = arg;
8414 struct got_fileindex_entry *ie;
8415 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8416 char *ondisk_path = NULL;
8417 char *id_str = NULL, *label_orig = NULL;
8418 int local_changes_subsumed;
8419 struct stat sb;
8421 if (staged_status != GOT_STATUS_ADD &&
8422 staged_status != GOT_STATUS_MODIFY &&
8423 staged_status != GOT_STATUS_DELETE)
8424 return NULL;
8426 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8427 if (ie == NULL)
8428 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8430 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8431 == -1)
8432 return got_error_from_errno("asprintf");
8434 err = got_object_id_str(&id_str,
8435 commit_id ? commit_id : a->worktree->base_commit_id);
8436 if (err)
8437 goto done;
8438 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8439 id_str) == -1) {
8440 err = got_error_from_errno("asprintf");
8441 goto done;
8444 switch (staged_status) {
8445 case GOT_STATUS_MODIFY:
8446 err = got_object_open_as_blob(&blob_base, a->repo,
8447 blob_id, 8192);
8448 if (err)
8449 break;
8450 /* fall through */
8451 case GOT_STATUS_ADD:
8452 if (a->patch_cb) {
8453 if (staged_status == GOT_STATUS_ADD) {
8454 int choice = GOT_PATCH_CHOICE_NONE;
8455 err = (*a->patch_cb)(&choice, a->patch_arg,
8456 staged_status, ie->path, NULL, 1, 1);
8457 if (err)
8458 break;
8459 if (choice != GOT_PATCH_CHOICE_YES)
8460 break;
8461 } else {
8462 err = unstage_hunks(staged_blob_id,
8463 blob_base, blob_id, ie, ondisk_path,
8464 label_orig, a->worktree, a->repo,
8465 a->patch_cb, a->patch_arg,
8466 a->progress_cb, a->progress_arg);
8467 break; /* Done with this file. */
8470 err = got_object_open_as_blob(&blob_staged, a->repo,
8471 staged_blob_id, 8192);
8472 if (err)
8473 break;
8474 switch (got_fileindex_entry_staged_filetype_get(ie)) {
8475 case GOT_FILEIDX_MODE_BAD_SYMLINK:
8476 case GOT_FILEIDX_MODE_REGULAR_FILE:
8477 err = merge_blob(&local_changes_subsumed, a->worktree,
8478 blob_base, ondisk_path, relpath,
8479 got_fileindex_perms_to_st(ie), label_orig,
8480 blob_staged, commit_id ? commit_id :
8481 a->worktree->base_commit_id, a->repo,
8482 a->progress_cb, a->progress_arg);
8483 break;
8484 case GOT_FILEIDX_MODE_SYMLINK:
8485 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8486 char *staged_target;
8487 err = got_object_blob_read_to_str(
8488 &staged_target, blob_staged);
8489 if (err)
8490 goto done;
8491 err = merge_symlink(a->worktree, blob_base,
8492 ondisk_path, relpath, label_orig,
8493 staged_target, commit_id ? commit_id :
8494 a->worktree->base_commit_id,
8495 a->repo, a->progress_cb, a->progress_arg);
8496 free(staged_target);
8497 } else {
8498 err = merge_blob(&local_changes_subsumed,
8499 a->worktree, blob_base, ondisk_path,
8500 relpath, got_fileindex_perms_to_st(ie),
8501 label_orig, blob_staged,
8502 commit_id ? commit_id :
8503 a->worktree->base_commit_id, a->repo,
8504 a->progress_cb, a->progress_arg);
8506 break;
8507 default:
8508 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8509 break;
8511 if (err == NULL) {
8512 got_fileindex_entry_stage_set(ie,
8513 GOT_FILEIDX_STAGE_NONE);
8514 got_fileindex_entry_staged_filetype_set(ie, 0);
8516 break;
8517 case GOT_STATUS_DELETE:
8518 if (a->patch_cb) {
8519 int choice = GOT_PATCH_CHOICE_NONE;
8520 err = (*a->patch_cb)(&choice, a->patch_arg,
8521 staged_status, ie->path, NULL, 1, 1);
8522 if (err)
8523 break;
8524 if (choice == GOT_PATCH_CHOICE_NO)
8525 break;
8526 if (choice != GOT_PATCH_CHOICE_YES) {
8527 err = got_error(GOT_ERR_PATCH_CHOICE);
8528 break;
8531 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8532 got_fileindex_entry_staged_filetype_set(ie, 0);
8533 err = get_file_status(&status, &sb, ie, ondisk_path,
8534 dirfd, de_name, a->repo);
8535 if (err)
8536 break;
8537 err = (*a->progress_cb)(a->progress_arg, status, relpath);
8538 break;
8540 done:
8541 free(ondisk_path);
8542 if (blob_base)
8543 got_object_blob_close(blob_base);
8544 if (blob_staged)
8545 got_object_blob_close(blob_staged);
8546 free(id_str);
8547 free(label_orig);
8548 return err;
8551 const struct got_error *
8552 got_worktree_unstage(struct got_worktree *worktree,
8553 struct got_pathlist_head *paths,
8554 got_worktree_checkout_cb progress_cb, void *progress_arg,
8555 got_worktree_patch_cb patch_cb, void *patch_arg,
8556 struct got_repository *repo)
8558 const struct got_error *err = NULL, *sync_err, *unlockerr;
8559 struct got_pathlist_entry *pe;
8560 struct got_fileindex *fileindex = NULL;
8561 char *fileindex_path = NULL;
8562 struct unstage_path_arg upa;
8564 err = lock_worktree(worktree, LOCK_EX);
8565 if (err)
8566 return err;
8568 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8569 if (err)
8570 goto done;
8572 upa.worktree = worktree;
8573 upa.fileindex = fileindex;
8574 upa.repo = repo;
8575 upa.progress_cb = progress_cb;
8576 upa.progress_arg = progress_arg;
8577 upa.patch_cb = patch_cb;
8578 upa.patch_arg = patch_arg;
8579 TAILQ_FOREACH(pe, paths, entry) {
8580 err = worktree_status(worktree, pe->path, fileindex, repo,
8581 unstage_path, &upa, NULL, NULL, 1, 0);
8582 if (err)
8583 goto done;
8586 sync_err = sync_fileindex(fileindex, fileindex_path);
8587 if (sync_err && err == NULL)
8588 err = sync_err;
8589 done:
8590 free(fileindex_path);
8591 if (fileindex)
8592 got_fileindex_free(fileindex);
8593 unlockerr = lock_worktree(worktree, LOCK_SH);
8594 if (unlockerr && err == NULL)
8595 err = unlockerr;
8596 return err;
8599 struct report_file_info_arg {
8600 struct got_worktree *worktree;
8601 got_worktree_path_info_cb info_cb;
8602 void *info_arg;
8603 struct got_pathlist_head *paths;
8604 got_cancel_cb cancel_cb;
8605 void *cancel_arg;
8608 static const struct got_error *
8609 report_file_info(void *arg, struct got_fileindex_entry *ie)
8611 struct report_file_info_arg *a = arg;
8612 struct got_pathlist_entry *pe;
8613 struct got_object_id blob_id, staged_blob_id, commit_id;
8614 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8615 struct got_object_id *commit_idp = NULL;
8616 int stage;
8618 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8619 return got_error(GOT_ERR_CANCELLED);
8621 TAILQ_FOREACH(pe, a->paths, entry) {
8622 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8623 got_path_is_child(ie->path, pe->path, pe->path_len))
8624 break;
8626 if (pe == NULL) /* not found */
8627 return NULL;
8629 if (got_fileindex_entry_has_blob(ie)) {
8630 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8631 blob_idp = &blob_id;
8633 stage = got_fileindex_entry_stage_get(ie);
8634 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8635 stage == GOT_FILEIDX_STAGE_ADD) {
8636 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8637 SHA1_DIGEST_LENGTH);
8638 staged_blob_idp = &staged_blob_id;
8641 if (got_fileindex_entry_has_commit(ie)) {
8642 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8643 commit_idp = &commit_id;
8646 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8647 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8650 const struct got_error *
8651 got_worktree_path_info(struct got_worktree *worktree,
8652 struct got_pathlist_head *paths,
8653 got_worktree_path_info_cb info_cb, void *info_arg,
8654 got_cancel_cb cancel_cb, void *cancel_arg)
8657 const struct got_error *err = NULL, *unlockerr;
8658 struct got_fileindex *fileindex = NULL;
8659 char *fileindex_path = NULL;
8660 struct report_file_info_arg arg;
8662 err = lock_worktree(worktree, LOCK_SH);
8663 if (err)
8664 return err;
8666 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8667 if (err)
8668 goto done;
8670 arg.worktree = worktree;
8671 arg.info_cb = info_cb;
8672 arg.info_arg = info_arg;
8673 arg.paths = paths;
8674 arg.cancel_cb = cancel_cb;
8675 arg.cancel_arg = cancel_arg;
8676 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8677 &arg);
8678 done:
8679 free(fileindex_path);
8680 if (fileindex)
8681 got_fileindex_free(fileindex);
8682 unlockerr = lock_worktree(worktree, LOCK_UN);
8683 if (unlockerr && err == NULL)
8684 err = unlockerr;
8685 return err;